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
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional from packaging import version if TYPE_CHECKING: from ... import PreTrainedTokenizer, TensorType from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import is_torch_available, logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "bigscience/bloom": "https://huggingface.co/bigscience/bloom/resolve/main/config.json", "bigscience/bloom-560m": "https://huggingface.co/bigscience/bloom-560m/blob/main/config.json", "bigscience/bloom-1b1": "https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json", "bigscience/bloom-1b7": "https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json", "bigscience/bloom-3b": "https://huggingface.co/bigscience/bloom-3b/blob/main/config.json", "bigscience/bloom-7b1": "https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json", } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Dict = 'bloom' __UpperCAmelCase : str = ['past_key_values'] __UpperCAmelCase : Union[str, Any] = { 'num_hidden_layers': 'n_layer', 'num_attention_heads': 'n_head', } def __init__( self , _a=250_880 , _a=64 , _a=2 , _a=8 , _a=1E-5 , _a=0.02 , _a=True , _a=1 , _a=2 , _a=False , _a=0.0 , _a=0.0 , _a=1 , _a=False , **_a , ): __a = vocab_size # Backward compatibility with n_embed kwarg __a = kwargs.pop('''n_embed''' , _a ) __a = hidden_size if n_embed is None else n_embed __a = n_layer __a = n_head __a = layer_norm_epsilon __a = initializer_range __a = use_cache __a = pretraining_tp __a = apply_residual_connection_post_layernorm __a = hidden_dropout __a = attention_dropout __a = bos_token_id __a = eos_token_id __a = slow_but_exact super().__init__(bos_token_id=_a , eos_token_id=_a , **_a ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = version.parse('1.12' ) def __init__( self , _a , _a = "default" , _a = None , _a = False , ): super().__init__(_a , task=_a , patching_specs=_a , use_past=_a ) if not getattr(self._config , '''pad_token_id''' , _a ): # TODO: how to do that better? __a = 0 @property def __UpperCAmelCase ( self ): __a = OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}} ) if self.use_past: # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344 self.fill_with_past_key_values_(_a , direction='''inputs''' , inverted_values_shape=_a ) __a = {0: '''batch''', 1: '''past_sequence + sequence'''} else: __a = {0: '''batch''', 1: '''sequence'''} return common_inputs @property def __UpperCAmelCase ( self ): return self._config.n_layer @property def __UpperCAmelCase ( self ): return self._config.n_head @property def __UpperCAmelCase ( self ): return 1E-3 def __UpperCAmelCase ( self , _a , _a = -1 , _a = -1 , _a = False , _a = None , ): __a = super(_a , self ).generate_dummy_inputs( _a , batch_size=_a , seq_length=_a , is_pair=_a , framework=_a ) # We need to order the input in the way they appears in the forward() __a = OrderedDict({'''input_ids''': common_inputs['''input_ids''']} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch __a , __a = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values __a = seqlen + 2 __a = self._config.hidden_size // self.num_attention_heads __a = ( batch * self.num_attention_heads, head_dim, past_key_values_length, ) __a = ( batch * self.num_attention_heads, past_key_values_length, head_dim, ) __a = [ (torch.zeros(_a ), torch.zeros(_a )) for _ in range(self.num_layers ) ] __a = common_inputs['''attention_mask'''] if self.use_past: __a = ordered_inputs['''attention_mask'''].dtype __a = torch.cat( [ordered_inputs['''attention_mask'''], torch.ones(_a , _a , dtype=_a )] , dim=1 ) return ordered_inputs @property def __UpperCAmelCase ( self ): return 13
45
"""simple docstring""" from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : List[str] __UpperCAmelCase : Optional[str] = None # Automatically constructed __UpperCAmelCase : ClassVar[str] = "dict" __UpperCAmelCase : ClassVar[Any] = None __UpperCAmelCase : str = field(default='Translation' , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __call__( self ): return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def __UpperCAmelCase ( self ): from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : Optional[List] = None __UpperCAmelCase : Optional[int] = None __UpperCAmelCase : Optional[str] = None # Automatically constructed __UpperCAmelCase : ClassVar[str] = "dict" __UpperCAmelCase : ClassVar[Any] = None __UpperCAmelCase : str = field(default='TranslationVariableLanguages' , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __UpperCAmelCase ( self ): __a = sorted(set(self.languages ) ) if self.languages else None __a = len(self.languages ) if self.languages else None def __call__( self ): return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def __UpperCAmelCase ( self , _a ): __a = set(self.languages ) if self.languages and set(_a ) - lang_set: raise ValueError( f'''Some languages in example ({', '.join(sorted(set(_a ) - lang_set ) )}) are not in valid set ({', '.join(_a )}).''' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. __a = [] for lang, text in translation_dict.items(): if isinstance(_a , _a ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. __a , __a = zip(*sorted(_a ) ) return {"language": languages, "translation": translations} def __UpperCAmelCase ( self ): from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
45
1
"""simple docstring""" import unittest import numpy as np import torch from torch import nn from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import enable_full_determinism, skip_mps from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : List[str] = KandinskyVaaPriorPipeline __UpperCAmelCase : Optional[int] = ['prompt'] __UpperCAmelCase : int = ['prompt', 'negative_prompt'] __UpperCAmelCase : Optional[Any] = [ 'num_images_per_prompt', 'generator', 'num_inference_steps', 'latents', 'negative_prompt', 'guidance_scale', 'output_type', 'return_dict', ] __UpperCAmelCase : Any = False @property def __UpperCAmelCase ( self ): return 32 @property def __UpperCAmelCase ( self ): return 32 @property def __UpperCAmelCase ( self ): return self.time_input_dim @property def __UpperCAmelCase ( self ): return self.time_input_dim * 4 @property def __UpperCAmelCase ( self ): return 100 @property def __UpperCAmelCase ( self ): __a = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) return tokenizer @property def __UpperCAmelCase ( self ): torch.manual_seed(0 ) __a = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) return CLIPTextModelWithProjection(_a ) @property def __UpperCAmelCase ( self ): torch.manual_seed(0 ) __a = { '''num_attention_heads''': 2, '''attention_head_dim''': 12, '''embedding_dim''': self.text_embedder_hidden_size, '''num_layers''': 1, } __a = PriorTransformer(**_a ) # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0 __a = nn.Parameter(torch.ones(model.clip_std.shape ) ) return model @property def __UpperCAmelCase ( self ): torch.manual_seed(0 ) __a = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , image_size=224 , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_channels=3 , num_hidden_layers=5 , patch_size=14 , ) __a = CLIPVisionModelWithProjection(_a ) return model @property def __UpperCAmelCase ( self ): __a = CLIPImageProcessor( crop_size=224 , do_center_crop=_a , do_normalize=_a , do_resize=_a , image_mean=[0.4814_5466, 0.457_8275, 0.4082_1073] , image_std=[0.2686_2954, 0.2613_0258, 0.2757_7711] , resample=3 , size=224 , ) return image_processor def __UpperCAmelCase ( self ): __a = self.dummy_prior __a = self.dummy_image_encoder __a = self.dummy_text_encoder __a = self.dummy_tokenizer __a = self.dummy_image_processor __a = UnCLIPScheduler( variance_type='''fixed_small_log''' , prediction_type='''sample''' , num_train_timesteps=1_000 , clip_sample=_a , clip_sample_range=10.0 , ) __a = { '''prior''': prior, '''image_encoder''': image_encoder, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''scheduler''': scheduler, '''image_processor''': image_processor, } return components def __UpperCAmelCase ( self , _a , _a=0 ): if str(_a ).startswith('''mps''' ): __a = torch.manual_seed(_a ) else: __a = torch.Generator(device=_a ).manual_seed(_a ) __a = { '''prompt''': '''horse''', '''generator''': generator, '''guidance_scale''': 4.0, '''num_inference_steps''': 2, '''output_type''': '''np''', } return inputs def __UpperCAmelCase ( self ): __a = '''cpu''' __a = self.get_dummy_components() __a = self.pipeline_class(**_a ) __a = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) __a = pipe(**self.get_dummy_inputs(_a ) ) __a = output.image_embeds __a = pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] __a = image[0, -10:] __a = image_from_tuple[0, -10:] assert image.shape == (1, 32) __a = np.array( [-0.0532, 1.7120, 0.3656, -1.0852, -0.8946, -1.1756, 0.4348, 0.2482, 0.5146, -0.1156] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 @skip_mps def __UpperCAmelCase ( self ): __a = torch_device == '''cpu''' __a = True __a = False self._test_inference_batch_single_identical( test_max_difference=_a , relax_max_difference=_a , test_mean_pixel_difference=_a , ) @skip_mps def __UpperCAmelCase ( self ): __a = torch_device == '''cpu''' __a = False self._test_attention_slicing_forward_pass( test_max_difference=_a , test_mean_pixel_difference=_a , )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> List[Any]: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) if n == 0: return 0 __a = float('''-inf''' ) for i in range(1 , n + 1 ): __a = max( lowerCAmelCase__ , prices[i - 1] + naive_cut_rod_recursive(n - i , lowerCAmelCase__ ) ) return max_revue def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> List[str]: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) __a = [float('''-inf''' ) for _ in range(n + 1 )] return _top_down_cut_rod_recursive(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list , lowerCAmelCase__ : list ) -> Union[str, Any]: if max_rev[n] >= 0: return max_rev[n] elif n == 0: return 0 else: __a = float('''-inf''' ) for i in range(1 , n + 1 ): __a = max( lowerCAmelCase__ , prices[i - 1] + _top_down_cut_rod_recursive(n - i , lowerCAmelCase__ , lowerCAmelCase__ ) , ) __a = max_revenue return max_rev[n] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> Dict: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of # length 0. __a = [float('''-inf''' ) for _ in range(n + 1 )] __a = 0 for i in range(1 , n + 1 ): __a = max_rev[i] for j in range(1 , i + 1 ): __a = max(lowerCAmelCase__ , prices[j - 1] + max_rev[i - j] ) __a = max_revenue_i return max_rev[n] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> str: if n < 0: __a = f'''n must be greater than or equal to 0. Got n = {n}''' raise ValueError(lowerCAmelCase__ ) if n > len(lowerCAmelCase__ ): __a = ( '''Each integral piece of rod must have a corresponding price. ''' f'''Got n = {n} but length of prices = {len(lowerCAmelCase__ )}''' ) raise ValueError(lowerCAmelCase__ ) def lowercase ( ) -> int: __a = [6, 10, 12, 15, 20, 23] __a = len(lowerCAmelCase__ ) # the best revenue comes from cutting the rod into 6 pieces, each # of length 1 resulting in a revenue of 6 * 6 = 36. __a = 36 __a = top_down_cut_rod(lowerCAmelCase__ , lowerCAmelCase__ ) __a = bottom_up_cut_rod(lowerCAmelCase__ , lowerCAmelCase__ ) __a = naive_cut_rod_recursive(lowerCAmelCase__ , lowerCAmelCase__ ) assert expected_max_revenue == max_rev_top_down assert max_rev_top_down == max_rev_bottom_up assert max_rev_bottom_up == max_rev_naive if __name__ == "__main__": main()
45
1
"""simple docstring""" import fire from utils import calculate_rouge, save_json def lowercase ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : str , lowerCAmelCase__ : List[str]=None , **lowerCAmelCase__ : Dict ) -> int: __a = [x.strip() for x in open(lowerCAmelCase__ ).readlines()] __a = [x.strip() for x in open(lowerCAmelCase__ ).readlines()][: len(lowerCAmelCase__ )] __a = calculate_rouge(lowerCAmelCase__ , lowerCAmelCase__ , **lowerCAmelCase__ ) if save_path is not None: save_json(lowerCAmelCase__ , lowerCAmelCase__ , indent=lowerCAmelCase__ ) return metrics # these print nicely if __name__ == "__main__": fire.Fire(calculate_rouge_path)
45
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) lowercase_ = {"configuration_unispeech": ["UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP", "UniSpeechConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST", "UniSpeechForCTC", "UniSpeechForPreTraining", "UniSpeechForSequenceClassification", "UniSpeechModel", "UniSpeechPreTrainedModel", ] if TYPE_CHECKING: from .configuration_unispeech import UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_unispeech import ( UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST, UniSpeechForCTC, UniSpeechForPreTraining, UniSpeechForSequenceClassification, UniSpeechModel, UniSpeechPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
1
"""simple docstring""" # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer from .base import PipelineTool lowercase_ = { "Acehnese Arabic": "ace_Arab", "Acehnese Latin": "ace_Latn", "Mesopotamian Arabic": "acm_Arab", "Ta'izzi-Adeni Arabic": "acq_Arab", "Tunisian Arabic": "aeb_Arab", "Afrikaans": "afr_Latn", "South Levantine Arabic": "ajp_Arab", "Akan": "aka_Latn", "Amharic": "amh_Ethi", "North Levantine Arabic": "apc_Arab", "Modern Standard Arabic": "arb_Arab", "Modern Standard Arabic Romanized": "arb_Latn", "Najdi Arabic": "ars_Arab", "Moroccan Arabic": "ary_Arab", "Egyptian Arabic": "arz_Arab", "Assamese": "asm_Beng", "Asturian": "ast_Latn", "Awadhi": "awa_Deva", "Central Aymara": "ayr_Latn", "South Azerbaijani": "azb_Arab", "North Azerbaijani": "azj_Latn", "Bashkir": "bak_Cyrl", "Bambara": "bam_Latn", "Balinese": "ban_Latn", "Belarusian": "bel_Cyrl", "Bemba": "bem_Latn", "Bengali": "ben_Beng", "Bhojpuri": "bho_Deva", "Banjar Arabic": "bjn_Arab", "Banjar Latin": "bjn_Latn", "Standard Tibetan": "bod_Tibt", "Bosnian": "bos_Latn", "Buginese": "bug_Latn", "Bulgarian": "bul_Cyrl", "Catalan": "cat_Latn", "Cebuano": "ceb_Latn", "Czech": "ces_Latn", "Chokwe": "cjk_Latn", "Central Kurdish": "ckb_Arab", "Crimean Tatar": "crh_Latn", "Welsh": "cym_Latn", "Danish": "dan_Latn", "German": "deu_Latn", "Southwestern Dinka": "dik_Latn", "Dyula": "dyu_Latn", "Dzongkha": "dzo_Tibt", "Greek": "ell_Grek", "English": "eng_Latn", "Esperanto": "epo_Latn", "Estonian": "est_Latn", "Basque": "eus_Latn", "Ewe": "ewe_Latn", "Faroese": "fao_Latn", "Fijian": "fij_Latn", "Finnish": "fin_Latn", "Fon": "fon_Latn", "French": "fra_Latn", "Friulian": "fur_Latn", "Nigerian Fulfulde": "fuv_Latn", "Scottish Gaelic": "gla_Latn", "Irish": "gle_Latn", "Galician": "glg_Latn", "Guarani": "grn_Latn", "Gujarati": "guj_Gujr", "Haitian Creole": "hat_Latn", "Hausa": "hau_Latn", "Hebrew": "heb_Hebr", "Hindi": "hin_Deva", "Chhattisgarhi": "hne_Deva", "Croatian": "hrv_Latn", "Hungarian": "hun_Latn", "Armenian": "hye_Armn", "Igbo": "ibo_Latn", "Ilocano": "ilo_Latn", "Indonesian": "ind_Latn", "Icelandic": "isl_Latn", "Italian": "ita_Latn", "Javanese": "jav_Latn", "Japanese": "jpn_Jpan", "Kabyle": "kab_Latn", "Jingpho": "kac_Latn", "Kamba": "kam_Latn", "Kannada": "kan_Knda", "Kashmiri Arabic": "kas_Arab", "Kashmiri Devanagari": "kas_Deva", "Georgian": "kat_Geor", "Central Kanuri Arabic": "knc_Arab", "Central Kanuri Latin": "knc_Latn", "Kazakh": "kaz_Cyrl", "Kabiyè": "kbp_Latn", "Kabuverdianu": "kea_Latn", "Khmer": "khm_Khmr", "Kikuyu": "kik_Latn", "Kinyarwanda": "kin_Latn", "Kyrgyz": "kir_Cyrl", "Kimbundu": "kmb_Latn", "Northern Kurdish": "kmr_Latn", "Kikongo": "kon_Latn", "Korean": "kor_Hang", "Lao": "lao_Laoo", "Ligurian": "lij_Latn", "Limburgish": "lim_Latn", "Lingala": "lin_Latn", "Lithuanian": "lit_Latn", "Lombard": "lmo_Latn", "Latgalian": "ltg_Latn", "Luxembourgish": "ltz_Latn", "Luba-Kasai": "lua_Latn", "Ganda": "lug_Latn", "Luo": "luo_Latn", "Mizo": "lus_Latn", "Standard Latvian": "lvs_Latn", "Magahi": "mag_Deva", "Maithili": "mai_Deva", "Malayalam": "mal_Mlym", "Marathi": "mar_Deva", "Minangkabau Arabic ": "min_Arab", "Minangkabau Latin": "min_Latn", "Macedonian": "mkd_Cyrl", "Plateau Malagasy": "plt_Latn", "Maltese": "mlt_Latn", "Meitei Bengali": "mni_Beng", "Halh Mongolian": "khk_Cyrl", "Mossi": "mos_Latn", "Maori": "mri_Latn", "Burmese": "mya_Mymr", "Dutch": "nld_Latn", "Norwegian Nynorsk": "nno_Latn", "Norwegian Bokmål": "nob_Latn", "Nepali": "npi_Deva", "Northern Sotho": "nso_Latn", "Nuer": "nus_Latn", "Nyanja": "nya_Latn", "Occitan": "oci_Latn", "West Central Oromo": "gaz_Latn", "Odia": "ory_Orya", "Pangasinan": "pag_Latn", "Eastern Panjabi": "pan_Guru", "Papiamento": "pap_Latn", "Western Persian": "pes_Arab", "Polish": "pol_Latn", "Portuguese": "por_Latn", "Dari": "prs_Arab", "Southern Pashto": "pbt_Arab", "Ayacucho Quechua": "quy_Latn", "Romanian": "ron_Latn", "Rundi": "run_Latn", "Russian": "rus_Cyrl", "Sango": "sag_Latn", "Sanskrit": "san_Deva", "Santali": "sat_Olck", "Sicilian": "scn_Latn", "Shan": "shn_Mymr", "Sinhala": "sin_Sinh", "Slovak": "slk_Latn", "Slovenian": "slv_Latn", "Samoan": "smo_Latn", "Shona": "sna_Latn", "Sindhi": "snd_Arab", "Somali": "som_Latn", "Southern Sotho": "sot_Latn", "Spanish": "spa_Latn", "Tosk Albanian": "als_Latn", "Sardinian": "srd_Latn", "Serbian": "srp_Cyrl", "Swati": "ssw_Latn", "Sundanese": "sun_Latn", "Swedish": "swe_Latn", "Swahili": "swh_Latn", "Silesian": "szl_Latn", "Tamil": "tam_Taml", "Tatar": "tat_Cyrl", "Telugu": "tel_Telu", "Tajik": "tgk_Cyrl", "Tagalog": "tgl_Latn", "Thai": "tha_Thai", "Tigrinya": "tir_Ethi", "Tamasheq Latin": "taq_Latn", "Tamasheq Tifinagh": "taq_Tfng", "Tok Pisin": "tpi_Latn", "Tswana": "tsn_Latn", "Tsonga": "tso_Latn", "Turkmen": "tuk_Latn", "Tumbuka": "tum_Latn", "Turkish": "tur_Latn", "Twi": "twi_Latn", "Central Atlas Tamazight": "tzm_Tfng", "Uyghur": "uig_Arab", "Ukrainian": "ukr_Cyrl", "Umbundu": "umb_Latn", "Urdu": "urd_Arab", "Northern Uzbek": "uzn_Latn", "Venetian": "vec_Latn", "Vietnamese": "vie_Latn", "Waray": "war_Latn", "Wolof": "wol_Latn", "Xhosa": "xho_Latn", "Eastern Yiddish": "ydd_Hebr", "Yoruba": "yor_Latn", "Yue Chinese": "yue_Hant", "Chinese Simplified": "zho_Hans", "Chinese Traditional": "zho_Hant", "Standard Malay": "zsm_Latn", "Zulu": "zul_Latn", } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = 'facebook/nllb-200-distilled-600M' __UpperCAmelCase : int = ( 'This is a tool that translates text from a language to another. It takes three inputs: `text`, which should ' 'be the text to translate, `src_lang`, which should be the language of the text to translate and `tgt_lang`, ' 'which should be the language for the desired ouput language. Both `src_lang` and `tgt_lang` are written in ' 'plain English, such as \'Romanian\', or \'Albanian\'. It returns the text translated in `tgt_lang`.' ) __UpperCAmelCase : Tuple = 'translator' __UpperCAmelCase : int = AutoTokenizer __UpperCAmelCase : Dict = AutoModelForSeqaSeqLM __UpperCAmelCase : List[str] = LANGUAGE_CODES __UpperCAmelCase : Dict = ['text', 'text', 'text'] __UpperCAmelCase : Dict = ['text'] def __UpperCAmelCase ( self , _a , _a , _a ): if src_lang not in self.lang_to_code: raise ValueError(f'''{src_lang} is not a supported language.''' ) if tgt_lang not in self.lang_to_code: raise ValueError(f'''{tgt_lang} is not a supported language.''' ) __a = self.lang_to_code[src_lang] __a = self.lang_to_code[tgt_lang] return self.pre_processor._build_translation_inputs( _a , return_tensors='''pt''' , src_lang=_a , tgt_lang=_a ) def __UpperCAmelCase ( self , _a ): return self.model.generate(**_a ) def __UpperCAmelCase ( self , _a ): return self.post_processor.decode(outputs[0].tolist() , skip_special_tokens=_a )
45
"""simple docstring""" import unittest import torch from torch import nn from diffusers.models.activations import get_activation class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = get_activation('''swish''' ) self.assertIsInstance(_a , nn.SiLU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''silu''' ) self.assertIsInstance(_a , nn.SiLU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''mish''' ) self.assertIsInstance(_a , nn.Mish ) self.assertEqual(act(torch.tensor(-200 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''gelu''' ) self.assertIsInstance(_a , nn.GELU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 )
45
1
"""simple docstring""" import logging import os from .state import PartialState class __lowerCAmelCase ( logging.LoggerAdapter ): '''simple docstring''' @staticmethod def __UpperCAmelCase ( _a ): __a = PartialState() return not main_process_only or (main_process_only and state.is_main_process) def __UpperCAmelCase ( self , _a , _a , *_a , **_a ): if PartialState._shared_state == {}: raise RuntimeError( '''You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.''' ) __a = kwargs.pop('''main_process_only''' , _a ) __a = kwargs.pop('''in_order''' , _a ) if self.isEnabledFor(_a ): if self._should_log(_a ): __a , __a = self.process(_a , _a ) self.logger.log(_a , _a , *_a , **_a ) elif in_order: __a = PartialState() for i in range(state.num_processes ): if i == state.process_index: __a , __a = self.process(_a , _a ) self.logger.log(_a , _a , *_a , **_a ) state.wait_for_everyone() def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : str = None ) -> Any: if log_level is None: __a = os.environ.get('''ACCELERATE_LOG_LEVEL''' , lowerCAmelCase__ ) __a = logging.getLogger(lowerCAmelCase__ ) if log_level is not None: logger.setLevel(log_level.upper() ) logger.root.setLevel(log_level.upper() ) return MultiProcessAdapter(lowerCAmelCase__ , {} )
45
"""simple docstring""" from __future__ import annotations import unittest from transformers import EsmConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy import tensorflow as tf from transformers.models.esm.modeling_tf_esm import ( TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, TFEsmModel, ) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , ): __a = parent __a = 13 __a = 7 __a = True __a = True __a = True __a = 99 __a = 32 __a = 2 __a = 4 __a = 37 __a = '''gelu''' __a = 0.1 __a = 0.1 __a = 512 __a = 16 __a = 2 __a = 0.02 __a = 3 __a = 4 __a = None def __UpperCAmelCase ( self ): __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a = None if self.use_input_mask: __a = random_attention_mask([self.batch_size, self.seq_length] ) __a = None __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __a = ids_tensor([self.batch_size] , self.num_choices ) __a = EsmConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , pad_token_id=1 , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def __UpperCAmelCase ( self ): ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = self.prepare_config_and_inputs() __a = True __a = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __a = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , _a ): __a = TFEsmModel(config=_a ) __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} __a = model(_a ) __a = [input_ids, input_mask] __a = model(_a ) __a = model(_a ) 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 , _a , ): __a = True __a = TFEsmModel(config=_a ) __a = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''encoder_hidden_states''': encoder_hidden_states, '''encoder_attention_mask''': encoder_attention_mask, } __a = model(_a ) __a = [input_ids, input_mask] __a = model(_a , encoder_hidden_states=_a ) # Also check the case where encoder outputs are not passed __a = model(_a , attention_mask=_a ) 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 = TFEsmForMaskedLM(config=_a ) __a = model([input_ids, input_mask] ) 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 = self.num_labels __a = TFEsmForTokenClassification(config=_a ) __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} __a = model(_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = config_and_inputs __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = ( ( TFEsmModel, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, ) if is_tf_available() else () ) __UpperCAmelCase : Tuple = ( { 'feature-extraction': TFEsmModel, 'fill-mask': TFEsmForMaskedLM, 'text-classification': TFEsmForSequenceClassification, 'token-classification': TFEsmForTokenClassification, 'zero-shot': TFEsmForSequenceClassification, } if is_tf_available() else {} ) __UpperCAmelCase : Tuple = False __UpperCAmelCase : Union[str, Any] = False def __UpperCAmelCase ( self ): __a = TFEsmModelTester(self ) __a = ConfigTester(self , config_class=_a , hidden_size=37 ) def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_a ) @slow def __UpperCAmelCase ( self ): for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = TFEsmModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip('''Protein models do not support embedding resizing.''' ) def __UpperCAmelCase ( self ): pass @unittest.skip('''Protein models do not support embedding resizing.''' ) def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): __a , __a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a = model_class(_a ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class is TFEsmForMaskedLM: # Output embedding test differs from the main test because they're a matrix, not a layer __a = model.get_bias() assert isinstance(_a , _a ) for k, v in name.items(): assert isinstance(_a , tf.Variable ) else: __a = model.get_output_embeddings() assert x is None __a = model.get_bias() assert name is None @require_tf class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def __UpperCAmelCase ( self ): __a = TFEsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) __a = tf.constant([[0, 1, 2, 3, 4, 5]] ) __a = model(_a )[0] __a = [1, 6, 33] self.assertEqual(list(output.numpy().shape ) , _a ) # compare the actual values for a slice. __a = tf.constant( [ [ [8.92_1518, -10.58_9814, -6.467_1307], [-6.396_7156, -13.91_1377, -1.121_1915], [-7.78_1247, -13.95_1557, -3.74_0592], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-2 ) ) @slow def __UpperCAmelCase ( self ): __a = TFEsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) __a = tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) __a = model(_a )[0] # compare the actual values for a slice. __a = tf.constant( [ [ [0.1444_3092, 0.5412_5327, 0.324_7739], [0.3034_0484, 0.0052_6676, 0.3107_7722], [0.3227_8043, -0.2498_7096, 0.341_4628], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-4 ) )
45
1
"""simple docstring""" import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import YolosImageProcessor class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _a , _a=7 , _a=3 , _a=30 , _a=400 , _a=True , _a=None , _a=True , _a=[0.5, 0.5, 0.5] , _a=[0.5, 0.5, 0.5] , _a=True , _a=1 / 255 , _a=True , ): # by setting size["longest_edge"] > max_resolution we're effectively not testing this :p __a = size if size is not None else {'''shortest_edge''': 18, '''longest_edge''': 1_333} __a = parent __a = batch_size __a = num_channels __a = min_resolution __a = max_resolution __a = do_resize __a = size __a = do_normalize __a = image_mean __a = image_std __a = do_rescale __a = rescale_factor __a = do_pad def __UpperCAmelCase ( self ): return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def __UpperCAmelCase ( self , _a , _a=False ): if not batched: __a = image_inputs[0] if isinstance(_a , Image.Image ): __a , __a = image.size else: __a , __a = image.shape[1], image.shape[2] if w < h: __a = int(self.size['''shortest_edge'''] * h / w ) __a = self.size['''shortest_edge'''] elif w > h: __a = self.size['''shortest_edge'''] __a = int(self.size['''shortest_edge'''] * w / h ) else: __a = self.size['''shortest_edge'''] __a = self.size['''shortest_edge'''] else: __a = [] for image in image_inputs: __a , __a = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) __a = max(_a , key=lambda _a : item[0] )[0] __a = max(_a , key=lambda _a : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = YolosImageProcessor if is_vision_available() else None def __UpperCAmelCase ( self ): __a = YolosImageProcessingTester(self ) @property def __UpperCAmelCase ( self ): return self.image_processor_tester.prepare_image_processor_dict() def __UpperCAmelCase ( self ): __a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , '''image_mean''' ) ) self.assertTrue(hasattr(_a , '''image_std''' ) ) self.assertTrue(hasattr(_a , '''do_normalize''' ) ) self.assertTrue(hasattr(_a , '''do_resize''' ) ) self.assertTrue(hasattr(_a , '''size''' ) ) def __UpperCAmelCase ( self ): __a = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 18, '''longest_edge''': 1_333} ) self.assertEqual(image_processor.do_pad , _a ) __a = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=_a ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42, '''longest_edge''': 84} ) self.assertEqual(image_processor.do_pad , _a ) def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): # Initialize image_processing __a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __a = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input __a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values __a , __a = self.image_processor_tester.get_expected_values(_a ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched __a , __a = self.image_processor_tester.get_expected_values(_a , batched=_a ) __a = image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __UpperCAmelCase ( self ): # Initialize image_processing __a = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __a = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input __a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values __a , __a = self.image_processor_tester.get_expected_values(_a ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched __a = image_processing(_a , return_tensors='''pt''' ).pixel_values __a , __a = self.image_processor_tester.get_expected_values(_a , batched=_a ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __UpperCAmelCase ( self ): # Initialize image_processing __a = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __a = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input __a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values __a , __a = self.image_processor_tester.get_expected_values(_a ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched __a = image_processing(_a , return_tensors='''pt''' ).pixel_values __a , __a = self.image_processor_tester.get_expected_values(_a , batched=_a ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __UpperCAmelCase ( self ): # Initialize image_processings __a = self.image_processing_class(**self.image_processor_dict ) __a = self.image_processing_class(do_resize=_a , do_normalize=_a , do_rescale=_a ) # create random PyTorch tensors __a = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test whether the method "pad" and calling the image processor return the same tensors __a = image_processing_a.pad(_a , return_tensors='''pt''' ) __a = image_processing_a(_a , return_tensors='''pt''' ) self.assertTrue( torch.allclose(encoded_images_with_method['''pixel_values'''] , encoded_images['''pixel_values'''] , atol=1E-4 ) ) @slow def __UpperCAmelCase ( self ): # prepare image and target __a = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) with open('''./tests/fixtures/tests_samples/COCO/coco_annotations.txt''' , '''r''' ) as f: __a = json.loads(f.read() ) __a = {'''image_id''': 39_769, '''annotations''': target} # encode them __a = YolosImageProcessor.from_pretrained('''hustvl/yolos-small''' ) __a = image_processing(images=_a , annotations=_a , return_tensors='''pt''' ) # verify pixel values __a = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding['''pixel_values'''].shape , _a ) __a = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3] , _a , atol=1E-4 ) ) # verify area __a = torch.tensor([5887.9600, 1_1250.2061, 48_9353.8438, 83_7122.7500, 14_7967.5156, 16_5732.3438] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area'''] , _a ) ) # verify boxes __a = torch.Size([6, 4] ) self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape , _a ) __a = torch.tensor([0.5503, 0.2765, 0.0604, 0.2215] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0] , _a , atol=1E-3 ) ) # verify image_id __a = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id'''] , _a ) ) # verify is_crowd __a = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd'''] , _a ) ) # verify class_labels __a = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels'''] , _a ) ) # verify orig_size __a = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size'''] , _a ) ) # verify size __a = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size'''] , _a ) ) @slow def __UpperCAmelCase ( self ): # prepare image, target and masks_path __a = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) with open('''./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt''' , '''r''' ) as f: __a = json.loads(f.read() ) __a = {'''file_name''': '''000000039769.png''', '''image_id''': 39_769, '''segments_info''': target} __a = pathlib.Path('''./tests/fixtures/tests_samples/COCO/coco_panoptic''' ) # encode them __a = YolosImageProcessor(format='''coco_panoptic''' ) __a = image_processing(images=_a , annotations=_a , masks_path=_a , return_tensors='''pt''' ) # verify pixel values __a = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding['''pixel_values'''].shape , _a ) __a = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3] , _a , atol=1E-4 ) ) # verify area __a = torch.tensor([14_7979.6875, 16_5527.0469, 48_4638.5938, 1_1292.9375, 5879.6562, 7634.1147] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area'''] , _a ) ) # verify boxes __a = torch.Size([6, 4] ) self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape , _a ) __a = torch.tensor([0.2625, 0.5437, 0.4688, 0.8625] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0] , _a , atol=1E-3 ) ) # verify image_id __a = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id'''] , _a ) ) # verify is_crowd __a = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd'''] , _a ) ) # verify class_labels __a = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels'''] , _a ) ) # verify masks __a = 822_873 self.assertEqual(encoding['''labels'''][0]['''masks'''].sum().item() , _a ) # verify orig_size __a = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size'''] , _a ) ) # verify size __a = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size'''] , _a ) )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int ) -> str: if number > 0: raise ValueError('''input must be a negative integer''' ) __a = len(bin(lowerCAmelCase__ )[3:] ) __a = bin(abs(lowerCAmelCase__ ) - (1 << binary_number_length) )[3:] __a = ( ( '''1''' + '''0''' * (binary_number_length - len(lowerCAmelCase__ )) + twos_complement_number ) if number < 0 else '''0''' ) return "0b" + twos_complement_number if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler lowercase_ = 1_6 lowercase_ = 3_2 def lowercase ( lowerCAmelCase__ : Accelerator , lowerCAmelCase__ : int = 16 , lowerCAmelCase__ : str = "bert-base-cased" ) -> List[str]: __a = AutoTokenizer.from_pretrained(lowerCAmelCase__ ) __a = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(lowerCAmelCase__ : Any ): # max_length=None => use the model max length (it's actually the default) __a = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=lowerCAmelCase__ , max_length=lowerCAmelCase__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset __a = datasets.map( lowerCAmelCase__ , batched=lowerCAmelCase__ , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , load_from_cache_file=lowerCAmelCase__ ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library __a = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(lowerCAmelCase__ : Any ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(lowerCAmelCase__ , padding='''max_length''' , max_length=128 , return_tensors='''pt''' ) return tokenizer.pad(lowerCAmelCase__ , padding='''longest''' , return_tensors='''pt''' ) # Instantiate dataloaders. __a = DataLoader( tokenized_datasets['''train'''] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ ) __a = DataLoader( tokenized_datasets['''validation'''] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=lowerCAmelCase__ ) return train_dataloader, eval_dataloader def lowercase ( lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Any ) -> List[str]: # Initialize accelerator __a = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __a = config['''lr'''] __a = int(config['''num_epochs'''] ) __a = int(config['''seed'''] ) __a = int(config['''batch_size'''] ) __a = args.model_name_or_path set_seed(lowerCAmelCase__ ) __a , __a = get_dataloaders(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __a = AutoModelForSequenceClassification.from_pretrained(lowerCAmelCase__ , return_dict=lowerCAmelCase__ ) # Instantiate optimizer __a = ( AdamW if accelerator.state.deepspeed_plugin is None or '''optimizer''' not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) __a = optimizer_cls(params=model.parameters() , lr=lowerCAmelCase__ ) if accelerator.state.deepspeed_plugin is not None: __a = accelerator.state.deepspeed_plugin.deepspeed_config[ '''gradient_accumulation_steps''' ] else: __a = 1 __a = (len(lowerCAmelCase__ ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): __a = get_linear_schedule_with_warmup( optimizer=lowerCAmelCase__ , num_warmup_steps=0 , num_training_steps=lowerCAmelCase__ , ) else: __a = DummyScheduler(lowerCAmelCase__ , total_num_steps=lowerCAmelCase__ , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. __a , __a , __a , __a , __a = accelerator.prepare( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # We need to keep track of how many total steps we have iterated over __a = 0 # We also need to keep track of the stating epoch so files are named properly __a = 0 # Now we train the model __a = evaluate.load('''glue''' , '''mrpc''' ) __a = 0 __a = {} for epoch in range(lowerCAmelCase__ , lowerCAmelCase__ ): model.train() for step, batch in enumerate(lowerCAmelCase__ ): __a = model(**lowerCAmelCase__ ) __a = outputs.loss __a = loss / gradient_accumulation_steps accelerator.backward(lowerCAmelCase__ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 model.eval() __a = 0 for step, batch in enumerate(lowerCAmelCase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): __a = model(**lowerCAmelCase__ ) __a = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times __a , __a = accelerator.gather( (predictions, batch['''labels''']) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(lowerCAmelCase__ ) - 1: __a = predictions[: len(eval_dataloader.dataset ) - samples_seen] __a = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=lowerCAmelCase__ , references=lowerCAmelCase__ , ) __a = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f'''epoch {epoch}:''' , lowerCAmelCase__ ) __a = eval_metric['''accuracy'''] if best_performance < eval_metric["accuracy"]: __a = eval_metric['''accuracy'''] if args.performance_lower_bound is not None: assert ( args.performance_lower_bound <= best_performance ), f'''Best performance metric {best_performance} is lower than the lower bound {args.performance_lower_bound}''' accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , '''all_results.json''' ) , '''w''' ) as f: json.dump(lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( ) -> int: __a = argparse.ArgumentParser(description='''Simple example of training script tracking peak GPU memory usage.''' ) parser.add_argument( '''--model_name_or_path''' , type=lowerCAmelCase__ , default='''bert-base-cased''' , help='''Path to pretrained model or model identifier from huggingface.co/models.''' , required=lowerCAmelCase__ , ) parser.add_argument( '''--output_dir''' , type=lowerCAmelCase__ , default='''.''' , help='''Optional save directory where all checkpoint folders will be stored. Default is the current working directory.''' , ) parser.add_argument( '''--performance_lower_bound''' , type=lowerCAmelCase__ , default=lowerCAmelCase__ , help='''Optional lower bound for the performance metric. If set, the training will throw error when the performance metric drops below this value.''' , ) parser.add_argument( '''--num_epochs''' , type=lowerCAmelCase__ , default=3 , help='''Number of train epochs.''' , ) __a = parser.parse_args() __a = {'''lr''': 2e-5, '''num_epochs''': args.num_epochs, '''seed''': 42, '''batch_size''': 16} training_function(lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": main()
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : list[str] ) -> str: __a = '''''' for word_or_phrase in separated: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise Exception('''join() accepts only strings to be joined''' ) joined += word_or_phrase + separator return joined.strip(lowerCAmelCase__ ) if __name__ == "__main__": from doctest import testmod testmod()
45
1
"""simple docstring""" import csv import tweepy # Twitter API credentials lowercase_ = "" lowercase_ = "" lowercase_ = "" lowercase_ = "" def lowercase ( lowerCAmelCase__ : str ) -> None: # authorize twitter, initialize tweepy __a = tweepy.OAuthHandler(lowerCAmelCase__ , lowerCAmelCase__ ) auth.set_access_token(lowerCAmelCase__ , lowerCAmelCase__ ) __a = tweepy.API(lowerCAmelCase__ ) # initialize a list to hold all the tweepy Tweets __a = [] # make initial request for most recent tweets (200 is the maximum allowed count) __a = api.user_timeline(screen_name=lowerCAmelCase__ , count=200 ) # save most recent tweets alltweets.extend(lowerCAmelCase__ ) # save the id of the oldest tweet less one __a = alltweets[-1].id - 1 # keep grabbing tweets until there are no tweets left to grab while len(lowerCAmelCase__ ) > 0: print(f'''getting tweets before {oldest}''' ) # all subsequent requests use the max_id param to prevent duplicates __a = api.user_timeline( screen_name=lowerCAmelCase__ , count=200 , max_id=lowerCAmelCase__ ) # save most recent tweets alltweets.extend(lowerCAmelCase__ ) # update the id of the oldest tweet less one __a = alltweets[-1].id - 1 print(f'''...{len(lowerCAmelCase__ )} tweets downloaded so far''' ) # transform the tweepy tweets into a 2D array that will populate the csv __a = [[tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets] # write the csv with open(f'''new_{screen_name}_tweets.csv''' , '''w''' ) as f: __a = csv.writer(lowerCAmelCase__ ) writer.writerow(['''id''', '''created_at''', '''text'''] ) writer.writerows(lowerCAmelCase__ ) if __name__ == "__main__": # pass in the username of the account you want to download get_all_tweets("FirePing32")
45
"""simple docstring""" lowercase_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" def lowercase ( lowerCAmelCase__ : bytes ) -> bytes: # Make sure the supplied data is a bytes-like object if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = f'''a bytes-like object is required, not \'{data.__class__.__name__}\'''' raise TypeError(lowerCAmelCase__ ) __a = ''''''.join(bin(lowerCAmelCase__ )[2:].zfill(8 ) for byte in data ) __a = len(lowerCAmelCase__ ) % 6 != 0 if padding_needed: # The padding that will be added later __a = b'''=''' * ((6 - len(lowerCAmelCase__ ) % 6) // 2) # Append binary_stream with arbitrary binary digits (0's by default) to make its # length a multiple of 6. binary_stream += "0" * (6 - len(lowerCAmelCase__ ) % 6) else: __a = b'''''' # Encode every 6 binary digits to their corresponding Base64 character return ( "".join( B64_CHARSET[int(binary_stream[index : index + 6] , 2 )] for index in range(0 , len(lowerCAmelCase__ ) , 6 ) ).encode() + padding ) def lowercase ( lowerCAmelCase__ : str ) -> bytes: # Make sure encoded_data is either a string or a bytes-like object if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = ( '''argument should be a bytes-like object or ASCII string, ''' f'''not \'{encoded_data.__class__.__name__}\'''' ) raise TypeError(lowerCAmelCase__ ) # In case encoded_data is a bytes-like object, make sure it contains only # ASCII characters so we convert it to a string object if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): try: __a = encoded_data.decode('''utf-8''' ) except UnicodeDecodeError: raise ValueError('''base64 encoded data should only contain ASCII characters''' ) __a = encoded_data.count('''=''' ) # Check if the encoded string contains non base64 characters if padding: assert all( char in B64_CHARSET for char in encoded_data[:-padding] ), "Invalid base64 character(s) found." else: assert all( char in B64_CHARSET for char in encoded_data ), "Invalid base64 character(s) found." # Check the padding assert len(lowerCAmelCase__ ) % 4 == 0 and padding < 3, "Incorrect padding" if padding: # Remove padding if there is one __a = encoded_data[:-padding] __a = ''''''.join( bin(B64_CHARSET.index(lowerCAmelCase__ ) )[2:].zfill(6 ) for char in encoded_data )[: -padding * 2] else: __a = ''''''.join( bin(B64_CHARSET.index(lowerCAmelCase__ ) )[2:].zfill(6 ) for char in encoded_data ) __a = [ int(binary_stream[index : index + 8] , 2 ) for index in range(0 , len(lowerCAmelCase__ ) , 8 ) ] return bytes(lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" import logging import os from dataclasses import dataclass, field from functools import partial from pathlib import Path from tempfile import TemporaryDirectory from typing import List, Optional import faiss import torch from datasets import Features, Sequence, Value, load_dataset from transformers import DPRContextEncoder, DPRContextEncoderTokenizerFast, HfArgumentParser lowercase_ = logging.getLogger(__name__) torch.set_grad_enabled(False) lowercase_ = "cuda" if torch.cuda.is_available() else "cpu" def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : List[Any]=100 , lowerCAmelCase__ : Optional[int]=" " ) -> List[str]: __a = text.split(lowerCAmelCase__ ) return [character.join(text[i : i + n] ).strip() for i in range(0 , len(lowerCAmelCase__ ) , lowerCAmelCase__ )] def lowercase ( lowerCAmelCase__ : dict ) -> dict: __a , __a = [], [] for title, text in zip(documents['''title'''] , documents['''text'''] ): if text is not None: for passage in split_text(lowerCAmelCase__ ): titles.append(title if title is not None else '''''' ) texts.append(lowerCAmelCase__ ) return {"title": titles, "text": texts} def lowercase ( lowerCAmelCase__ : dict , lowerCAmelCase__ : DPRContextEncoder , lowerCAmelCase__ : DPRContextEncoderTokenizerFast ) -> dict: __a = ctx_tokenizer( documents['''title'''] , documents['''text'''] , truncation=lowerCAmelCase__ , padding='''longest''' , return_tensors='''pt''' )['''input_ids'''] __a = ctx_encoder(input_ids.to(device=lowerCAmelCase__ ) , return_dict=lowerCAmelCase__ ).pooler_output return {"embeddings": embeddings.detach().cpu().numpy()} def lowercase ( lowerCAmelCase__ : "RagExampleArguments" , lowerCAmelCase__ : "ProcessingArguments" , lowerCAmelCase__ : "IndexHnswArguments" , ) -> Tuple: ###################################### logger.info('''Step 1 - Create the dataset''' ) ###################################### # The dataset needed for RAG must have three columns: # - title (string): title of the document # - text (string): text of a passage of the document # - embeddings (array of dimension d): DPR representation of the passage # Let's say you have documents in tab-separated csv files with columns "title" and "text" assert os.path.isfile(rag_example_args.csv_path ), "Please provide a valid path to a csv file" # You can load a Dataset object this way __a = load_dataset( '''csv''' , data_files=[rag_example_args.csv_path] , split='''train''' , delimiter='''\t''' , column_names=['''title''', '''text'''] ) # More info about loading csv files in the documentation: https://huggingface.co/docs/datasets/loading_datasets.html?highlight=csv#csv-files # Then split the documents into passages of 100 words __a = dataset.map(lowerCAmelCase__ , batched=lowerCAmelCase__ , num_proc=processing_args.num_proc ) # And compute the embeddings __a = DPRContextEncoder.from_pretrained(rag_example_args.dpr_ctx_encoder_model_name ).to(device=lowerCAmelCase__ ) __a = DPRContextEncoderTokenizerFast.from_pretrained(rag_example_args.dpr_ctx_encoder_model_name ) __a = Features( {'''text''': Value('''string''' ), '''title''': Value('''string''' ), '''embeddings''': Sequence(Value('''float32''' ) )} ) # optional, save as float32 instead of float64 to save space __a = dataset.map( partial(lowerCAmelCase__ , ctx_encoder=lowerCAmelCase__ , ctx_tokenizer=lowerCAmelCase__ ) , batched=lowerCAmelCase__ , batch_size=processing_args.batch_size , features=lowerCAmelCase__ , ) # And finally save your dataset __a = os.path.join(rag_example_args.output_dir , '''my_knowledge_dataset''' ) dataset.save_to_disk(lowerCAmelCase__ ) # from datasets import load_from_disk # dataset = load_from_disk(passages_path) # to reload the dataset ###################################### logger.info('''Step 2 - Index the dataset''' ) ###################################### # Let's use the Faiss implementation of HNSW for fast approximate nearest neighbor search __a = faiss.IndexHNSWFlat(index_hnsw_args.d , index_hnsw_args.m , faiss.METRIC_INNER_PRODUCT ) dataset.add_faiss_index('''embeddings''' , custom_index=lowerCAmelCase__ ) # And save the index __a = os.path.join(rag_example_args.output_dir , '''my_knowledge_dataset_hnsw_index.faiss''' ) dataset.get_index('''embeddings''' ).save(lowerCAmelCase__ ) # dataset.load_faiss_index("embeddings", index_path) # to reload the index @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : str = field( default=str(Path(__SCREAMING_SNAKE_CASE ).parent / 'test_run' / 'dummy-kb' / 'my_knowledge_dataset.csv' ) , metadata={'help': 'Path to a tab-separated csv file with columns \'title\' and \'text\''} , ) __UpperCAmelCase : Optional[str] = field( default=__SCREAMING_SNAKE_CASE , metadata={'help': 'Question that is passed as input to RAG. Default is \'What does Moses\' rod turn into ?\'.'} , ) __UpperCAmelCase : str = field( default='facebook/rag-sequence-nq' , metadata={'help': 'The RAG model to use. Either \'facebook/rag-sequence-nq\' or \'facebook/rag-token-nq\''} , ) __UpperCAmelCase : str = field( default='facebook/dpr-ctx_encoder-multiset-base' , metadata={ 'help': ( 'The DPR context encoder model to use. Either \'facebook/dpr-ctx_encoder-single-nq-base\' or' ' \'facebook/dpr-ctx_encoder-multiset-base\'' ) } , ) __UpperCAmelCase : Optional[str] = field( default=str(Path(__SCREAMING_SNAKE_CASE ).parent / 'test_run' / 'dummy-kb' ) , metadata={'help': 'Path to a directory where the dataset passages and the index will be saved'} , ) @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : Optional[int] = field( default=__SCREAMING_SNAKE_CASE , metadata={ 'help': 'The number of processes to use to split the documents into passages. Default is single process.' } , ) __UpperCAmelCase : int = field( default=1_6 , metadata={ 'help': 'The batch size to use when computing the passages embeddings using the DPR context encoder.' } , ) @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : int = field( default=7_6_8 , metadata={'help': 'The dimension of the embeddings to pass to the HNSW Faiss index.'} , ) __UpperCAmelCase : int = field( default=1_2_8 , metadata={ 'help': ( 'The number of bi-directional links created for every new element during the HNSW index construction.' ) } , ) if __name__ == "__main__": logging.basicConfig(level=logging.WARNING) logger.setLevel(logging.INFO) lowercase_ = HfArgumentParser((RagExampleArguments, ProcessingArguments, IndexHnswArguments)) lowercase_ , lowercase_ , lowercase_ = parser.parse_args_into_dataclasses() with TemporaryDirectory() as tmp_dir: lowercase_ = rag_example_args.output_dir or tmp_dir main(rag_example_args, processing_args, index_hnsw_args)
45
"""simple docstring""" import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = inspect.getfile(accelerate.test_utils ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) __a = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_distributed_data_loop.py'''] ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_ops.py'''] ) @require_multi_gpu def __UpperCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices.''' ) __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_a , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices.''' ) __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.operation_file_path] print(f'''Command: {cmd}''' ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_a , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self ): __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_a , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices, using 2 devices only''' ) __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices='''0,1''' ): execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": lowercase_ = Accelerator() lowercase_ = (accelerator.state.process_index + 2, 1_0) lowercase_ = torch.randint(0, 1_0, shape).to(accelerator.device) lowercase_ = "" lowercase_ = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." lowercase_ = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." lowercase_ = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
45
1
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "facebook/xmod-base": "https://huggingface.co/facebook/xmod-base/resolve/main/config.json", "facebook/xmod-large-prenorm": "https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json", "facebook/xmod-base-13-125k": "https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json", "facebook/xmod-base-30-125k": "https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json", "facebook/xmod-base-30-195k": "https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json", "facebook/xmod-base-60-125k": "https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json", "facebook/xmod-base-60-265k": "https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json", "facebook/xmod-base-75-125k": "https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json", "facebook/xmod-base-75-269k": "https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json", } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Optional[int] = 'xmod' def __init__( self , _a=30_522 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=2 , _a=0.02 , _a=1E-12 , _a=1 , _a=0 , _a=2 , _a="absolute" , _a=True , _a=None , _a=False , _a=2 , _a=False , _a=True , _a=True , _a=("en_XX",) , _a=None , **_a , ): super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) __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 __a = pre_norm __a = adapter_reduction_factor __a = adapter_layer_norm __a = adapter_reuse_layer_norm __a = ln_before_adapter __a = list(_a ) __a = default_language class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def __UpperCAmelCase ( self ): 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), ] )
45
"""simple docstring""" import numpy as np def lowercase ( lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : float ) -> np.ndarray: return np.where(vector > 0 , lowerCAmelCase__ , (alpha * (np.exp(lowerCAmelCase__ ) - 1)) ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" import warnings from ...utils import logging from .image_processing_deformable_detr import DeformableDetrImageProcessor lowercase_ = logging.get_logger(__name__) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , *_a , **_a ): warnings.warn( '''The class DeformableDetrFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use DeformableDetrImageProcessor instead.''' , _a , ) super().__init__(*_a , **_a )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: __a = set() # Replace all the whitespace in our sentence __a = input_str.replace(''' ''' , '''''' ) for alpha in input_str: if "a" <= alpha.lower() <= "z": frequency.add(alpha.lower() ) return len(lowerCAmelCase__ ) == 26 def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: __a = [False] * 26 for char in input_str: if char.islower(): __a = True elif char.isupper(): __a = True return all(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: return len({char for char in input_str.lower() if char.isalpha()} ) == 26 def lowercase ( ) -> None: from timeit import timeit __a = '''from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest''' print(timeit('''is_pangram()''' , setup=lowerCAmelCase__ ) ) print(timeit('''is_pangram_faster()''' , setup=lowerCAmelCase__ ) ) print(timeit('''is_pangram_fastest()''' , setup=lowerCAmelCase__ ) ) # 5.348480500048026, 2.6477354579837993, 1.8470395830227062 # 5.036091582966037, 2.644472333951853, 1.8869528750656173 if __name__ == "__main__": import doctest doctest.testmod() benchmark()
45
1
"""simple docstring""" import logging import random import ray from transformers import RagConfig, RagRetriever, RagTokenizer from transformers.models.rag.retrieval_rag import CustomHFIndex lowercase_ = logging.getLogger(__name__) class __lowerCAmelCase : '''simple docstring''' def __init__( self ): __a = False def __UpperCAmelCase ( self , _a , _a , _a , _a ): if not self.initialized: __a = RagRetriever( _a , question_encoder_tokenizer=_a , generator_tokenizer=_a , index=_a , init_retrieval=_a , ) __a = True def __UpperCAmelCase ( self ): self.retriever.index.init_index() def __UpperCAmelCase ( self , _a , _a ): __a , __a = self.retriever._main_retrieve(_a , _a ) return doc_ids, retrieved_doc_embeds class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , _a , _a , _a , _a , _a=None ): if index is not None and index.is_initialized() and len(_a ) > 0: raise ValueError( '''When using Ray for distributed fine-tuning, ''' '''you\'ll need to provide the paths instead, ''' '''as the dataset and the index are loaded ''' '''separately. More info in examples/rag/use_own_knowledge_dataset.py ''' ) super().__init__( _a , question_encoder_tokenizer=_a , generator_tokenizer=_a , index=_a , init_retrieval=_a , ) __a = retrieval_workers if len(self.retrieval_workers ) > 0: ray.get( [ worker.create_rag_retriever.remote(_a , _a , _a , _a ) for worker in self.retrieval_workers ] ) def __UpperCAmelCase ( self ): logger.info('''initializing retrieval''' ) if len(self.retrieval_workers ) > 0: ray.get([worker.init_retrieval.remote() for worker in self.retrieval_workers] ) else: # Non-distributed training. Load index into this same process. self.index.init_index() def __UpperCAmelCase ( self , _a , _a ): if len(self.retrieval_workers ) > 0: # Select a random retrieval actor. __a = self.retrieval_workers[random.randint(0 , len(self.retrieval_workers ) - 1 )] __a , __a = ray.get(random_worker.retrieve.remote(_a , _a ) ) else: __a , __a = self._main_retrieve(_a , _a ) return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(_a ) @classmethod def __UpperCAmelCase ( cls , _a , _a=None , **_a ): return super(_a , cls ).get_tokenizers(_a , _a , **_a ) @classmethod def __UpperCAmelCase ( cls , _a , _a , _a=None , **_a ): __a = kwargs.pop('''config''' , _a ) or RagConfig.from_pretrained(_a , **_a ) __a = RagTokenizer.from_pretrained(_a , config=_a ) __a = rag_tokenizer.question_encoder __a = rag_tokenizer.generator if indexed_dataset is not None: __a = '''custom''' __a = CustomHFIndex(config.retrieval_vector_size , _a ) else: __a = cls._build_index(_a ) return cls( _a , question_encoder_tokenizer=_a , generator_tokenizer=_a , retrieval_workers=_a , index=_a , )
45
"""simple docstring""" import gc import unittest from transformers import CTRLConfig, is_torch_available from transformers.testing_utils import require_torch, slow, 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 ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, ) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a=14 , _a=7 , _a=True , _a=True , _a=True , _a=True , _a=True , _a=99 , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=3 , _a=4 , _a=None , ): __a = parent __a = batch_size __a = seq_length __a = is_training __a = use_token_type_ids __a = use_input_mask __a = use_labels __a = use_mc_token_ids __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = type_sequence_label_size __a = initializer_range __a = num_labels __a = num_choices __a = scope __a = self.vocab_size - 1 def __UpperCAmelCase ( self ): __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a = None if self.use_input_mask: __a = random_attention_mask([self.batch_size, self.seq_length] ) __a = None if self.use_token_type_ids: __a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __a = None if self.use_mc_token_ids: __a = ids_tensor([self.batch_size, self.num_choices] , self.seq_length ) __a = None __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __a = ids_tensor([self.batch_size] , self.num_choices ) __a = self.get_config() __a = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, token_type_ids, mc_token_ids, sequence_labels, token_labels, choice_labels, ) def __UpperCAmelCase ( self ): return CTRLConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , *_a ): __a = CTRLModel(config=_a ) model.to(_a ) model.eval() model(_a , token_type_ids=_a , head_mask=_a ) model(_a , token_type_ids=_a ) __a = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(len(result.past_key_values ) , config.n_layer ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , *_a ): __a = CTRLLMHeadModel(_a ) model.to(_a ) model.eval() __a = model(_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = config_and_inputs __a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask} return config, inputs_dict def __UpperCAmelCase ( self , _a , _a , _a , _a , *_a ): __a = self.num_labels __a = CTRLForSequenceClassification(_a ) model.to(_a ) model.eval() __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = model(_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) @require_torch class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else () __UpperCAmelCase : Union[str, Any] = (CTRLLMHeadModel,) if is_torch_available() else () __UpperCAmelCase : Union[str, Any] = ( { 'feature-extraction': CTRLModel, 'text-classification': CTRLForSequenceClassification, 'text-generation': CTRLLMHeadModel, 'zero-shot': CTRLForSequenceClassification, } if is_torch_available() else {} ) __UpperCAmelCase : Optional[Any] = True __UpperCAmelCase : List[Any] = False __UpperCAmelCase : str = False def __UpperCAmelCase ( self , _a , _a , _a , _a , _a ): if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny # config could not be created. return True return False def __UpperCAmelCase ( self ): __a = CTRLModelTester(self ) __a = ConfigTester(self , config_class=_a , n_embd=37 ) def __UpperCAmelCase ( self ): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_ctrl_model(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*_a ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __UpperCAmelCase ( self ): pass @slow def __UpperCAmelCase ( self ): for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = CTRLModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip('''The model doesn\'t support left padding''' ) # and it's not used enough to be worth fixing :) def __UpperCAmelCase ( self ): pass @require_torch class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() @slow def __UpperCAmelCase ( self ): __a = CTRLLMHeadModel.from_pretrained('''ctrl''' ) model.to(_a ) __a = torch.tensor( [[11_859, 0, 1_611, 8]] , dtype=torch.long , device=_a ) # Legal the president is __a = [ 11_859, 0, 1_611, 8, 5, 150, 26_449, 2, 19, 348, 469, 3, 2_595, 48, 20_740, 246_533, 246_533, 19, 30, 5, ] # Legal the president is a good guy and I don't want to lose my job. \n \n I have a __a = model.generate(_a , do_sample=_a ) self.assertListEqual(output_ids[0].tolist() , _a )
45
1
"""simple docstring""" import uuid from typing import Any, Dict, List, Optional, Union from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch lowercase_ = logging.get_logger(__name__) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a = None , _a = None , _a=None , _a=None ): if not conversation_id: __a = uuid.uuida() if past_user_inputs is None: __a = [] if generated_responses is None: __a = [] __a = conversation_id __a = past_user_inputs __a = generated_responses __a = text def __eq__( self , _a ): if not isinstance(_a , _a ): return False if self.uuid == other.uuid: return True return ( self.new_user_input == other.new_user_input and self.past_user_inputs == other.past_user_inputs and self.generated_responses == other.generated_responses ) def __UpperCAmelCase ( self , _a , _a = False ): if self.new_user_input: if overwrite: logger.warning( f'''User input added while unprocessed input was existing: "{self.new_user_input}" was overwritten ''' f'''with: "{text}".''' ) __a = text else: logger.warning( f'''User input added while unprocessed input was existing: "{self.new_user_input}" new input ''' f'''ignored: "{text}". Set `overwrite` to True to overwrite unprocessed user input''' ) else: __a = text def __UpperCAmelCase ( self ): if self.new_user_input: self.past_user_inputs.append(self.new_user_input ) __a = None def __UpperCAmelCase ( self , _a ): self.generated_responses.append(_a ) def __UpperCAmelCase ( self ): for user_input, generated_response in zip(self.past_user_inputs , self.generated_responses ): yield True, user_input yield False, generated_response if self.new_user_input: yield True, self.new_user_input def __repr__( self ): __a = f'''Conversation id: {self.uuid} \n''' for is_user, text in self.iter_texts(): __a = '''user''' if is_user else '''bot''' output += f'''{name} >> {text} \n''' return output @add_end_docstrings( __SCREAMING_SNAKE_CASE , r'\n min_length_for_response (`int`, *optional*, defaults to 32):\n The minimum length (in number of tokens) for a response.\n minimum_tokens (`int`, *optional*, defaults to 10):\n The minimum length of tokens to leave for a response.\n ' , ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , *_a , **_a ): super().__init__(*_a , **_a ) if self.tokenizer.pad_token_id is None: __a = self.tokenizer.eos_token def __UpperCAmelCase ( self , _a=None , _a=None , _a=None , **_a ): __a = {} __a = {} __a = {} if min_length_for_response is not None: __a = min_length_for_response if minimum_tokens is not None: __a = minimum_tokens if "max_length" in generate_kwargs: __a = generate_kwargs['''max_length'''] # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length) if clean_up_tokenization_spaces is not None: __a = clean_up_tokenization_spaces if generate_kwargs: forward_params.update(_a ) return preprocess_params, forward_params, postprocess_params def __call__( self , _a , _a=0 , **_a ): __a = super().__call__(_a , num_workers=_a , **_a ) if isinstance(_a , _a ) and len(_a ) == 1: return outputs[0] return outputs def __UpperCAmelCase ( self , _a , _a=32 ): if not isinstance(_a , _a ): raise ValueError('''ConversationalPipeline, expects Conversation as inputs''' ) if conversation.new_user_input is None: raise ValueError( f'''Conversation with UUID {type(conversation.uuid )} does not contain new user input to process. ''' '''Add user inputs with the conversation\'s `add_user_input` method''' ) if hasattr(self.tokenizer , '''_build_conversation_input_ids''' ): __a = self.tokenizer._build_conversation_input_ids(_a ) else: # If the tokenizer cannot handle conversations, we default to only the old version __a = self._legacy_parse_and_tokenize(_a ) if self.framework == "pt": __a = torch.LongTensor([input_ids] ) elif self.framework == "tf": __a = tf.constant([input_ids] ) return {"input_ids": input_ids, "conversation": conversation} def __UpperCAmelCase ( self , _a , _a=10 , **_a ): __a = generate_kwargs.get('''max_length''' , self.model.config.max_length ) __a = model_inputs['''input_ids'''].shape[1] if max_length - minimum_tokens < n: logger.warning(f'''Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})''' ) __a = max_length - minimum_tokens __a = model_inputs['''input_ids'''][:, -trim:] if "attention_mask" in model_inputs: __a = model_inputs['''attention_mask'''][:, -trim:] __a = model_inputs.pop('''conversation''' ) __a = max_length __a = self.model.generate(**_a , **_a ) if self.model.config.is_encoder_decoder: __a = 1 else: __a = n return {"output_ids": output_ids[:, start_position:], "conversation": conversation} def __UpperCAmelCase ( self , _a , _a=True ): __a = model_outputs['''output_ids'''] __a = self.tokenizer.decode( output_ids[0] , skip_special_tokens=_a , clean_up_tokenization_spaces=_a , ) __a = model_outputs['''conversation'''] conversation.mark_processed() conversation.append_response(_a ) return conversation def __UpperCAmelCase ( self , _a ): __a = self.tokenizer.eos_token_id __a = [] for is_user, text in conversation.iter_texts(): if eos_token_id is not None: input_ids.extend(self.tokenizer.encode(_a , add_special_tokens=_a ) + [eos_token_id] ) else: input_ids.extend(self.tokenizer.encode(_a , add_special_tokens=_a ) ) if len(_a ) > self.tokenizer.model_max_length: __a = input_ids[-self.tokenizer.model_max_length :] return input_ids
45
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase_ = {"configuration_xglm": ["XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XGLMConfig"]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["XGLMTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["XGLMTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "XGLM_PRETRAINED_MODEL_ARCHIVE_LIST", "XGLMForCausalLM", "XGLMModel", "XGLMPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "FlaxXGLMForCausalLM", "FlaxXGLMModel", "FlaxXGLMPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST", "TFXGLMForCausalLM", "TFXGLMModel", "TFXGLMPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure)
45
1
"""simple docstring""" lowercase_ = { "meter": "m", "kilometer": "km", "megametre": "Mm", "gigametre": "Gm", "terametre": "Tm", "petametre": "Pm", "exametre": "Em", "zettametre": "Zm", "yottametre": "Ym", } # Exponent of the factor(meter) lowercase_ = { "m": 0, "km": 3, "Mm": 6, "Gm": 9, "Tm": 1_2, "Pm": 1_5, "Em": 1_8, "Zm": 2_1, "Ym": 2_4, } def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : str , lowerCAmelCase__ : str ) -> float: __a = from_type.lower().strip('''s''' ) __a = to_type.lower().strip('''s''' ) __a = UNIT_SYMBOL.get(lowerCAmelCase__ , lowerCAmelCase__ ) __a = UNIT_SYMBOL.get(lowerCAmelCase__ , lowerCAmelCase__ ) if from_sanitized not in METRIC_CONVERSION: __a = ( f'''Invalid \'from_type\' value: {from_type!r}.\n''' f'''Conversion abbreviations are: {', '.join(lowerCAmelCase__ )}''' ) raise ValueError(lowerCAmelCase__ ) if to_sanitized not in METRIC_CONVERSION: __a = ( f'''Invalid \'to_type\' value: {to_type!r}.\n''' f'''Conversion abbreviations are: {', '.join(lowerCAmelCase__ )}''' ) raise ValueError(lowerCAmelCase__ ) __a = METRIC_CONVERSION[from_sanitized] __a = METRIC_CONVERSION[to_sanitized] __a = 1 if from_exponent > to_exponent: __a = from_exponent - to_exponent else: __a = -(to_exponent - from_exponent) return value * pow(10 , lowerCAmelCase__ ) if __name__ == "__main__": from doctest import testmod testmod()
45
"""simple docstring""" import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm lowercase_ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex lowercase_ = 1_0 lowercase_ = 2_5_6 def lowercase ( lowerCAmelCase__ : List[str] ) -> Optional[MinHash]: if len(lowerCAmelCase__ ) < MIN_NUM_TOKENS: return None __a = MinHash(num_perm=lowerCAmelCase__ ) for token in set(lowerCAmelCase__ ): min_hash.update(token.encode() ) return min_hash def lowercase ( lowerCAmelCase__ : str ) -> Set[str]: return {t for t in NON_ALPHA.split(lowerCAmelCase__ ) if len(t.strip() ) > 0} class __lowerCAmelCase : '''simple docstring''' def __init__( self , *, _a = 0.85 , ): __a = duplication_jaccard_threshold __a = NUM_PERM __a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) __a = defaultdict(_a ) def __UpperCAmelCase ( self , _a , _a ): __a = self._index.query(_a ) if code_key in self._index.keys: print(f'''Duplicate key {code_key}''' ) return self._index.insert(_a , _a ) if len(_a ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(_a ) break else: self._duplicate_clusters[close_duplicates[0]].add(_a ) def __UpperCAmelCase ( self ): __a = [] for base, duplicates in self._duplicate_clusters.items(): __a = [base] + list(_a ) # reformat the cluster to be a list of dict __a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(_a ) return duplicate_clusters def __UpperCAmelCase ( self , _a ): __a = self.get_duplicate_clusters() with open(_a , '''w''' ) as f: json.dump(_a , _a ) def lowercase ( lowerCAmelCase__ : List[str] ) -> int: __a , __a = element __a = get_min_hash([t for t in NON_ALPHA.split(data['''content'''] ) if len(t.strip() ) > 0] ) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def lowercase ( lowerCAmelCase__ : Type[Dataset] ) -> str: with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(lowerCAmelCase__ , max_queue_size=10000 ) , chunksize=100 , ): if data is not None: yield data def lowercase ( lowerCAmelCase__ : Type[Dataset] , lowerCAmelCase__ : float ) -> Dict: __a = DuplicationIndex(duplication_jaccard_threshold=lowerCAmelCase__ ) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(lowerCAmelCase__ ) ) , max_queue_size=100 ) ): di.add(lowerCAmelCase__ , lowerCAmelCase__ ) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : str ) -> float: __a = get_tokens(lowerCAmelCase__ ) __a = get_tokens(lowerCAmelCase__ ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) lowercase_ = None def lowercase ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Union[str, Any] ) -> Any: __a = [] for elementa in cluster: __a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: __a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(lowerCAmelCase__ , lowerCAmelCase__ ) >= jaccard_threshold: elementa["copies"] += 1 break else: __a = 1 extremes.append(lowerCAmelCase__ ) return extremes def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[int] ) -> Optional[int]: global _shared_dataset __a = dataset __a = [] __a = partial(_find_cluster_extremes_shared , jaccard_threshold=lowerCAmelCase__ ) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( lowerCAmelCase__ , lowerCAmelCase__ , ) , total=len(lowerCAmelCase__ ) , ): extremes_list.append(lowerCAmelCase__ ) return extremes_list def lowercase ( lowerCAmelCase__ : Type[Dataset] , lowerCAmelCase__ : float = 0.85 ) -> Tuple[Type[Dataset], List[List[Dict]]]: __a = make_duplicate_clusters(lowerCAmelCase__ , lowerCAmelCase__ ) __a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} __a = {} __a = find_extremes(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) for extremes in extremes_clusters: for element in extremes: __a = element __a = duplicate_indices - set(extreme_dict.keys() ) __a = dataset.filter(lambda lowerCAmelCase__ , lowerCAmelCase__ : idx not in remove_indices , with_indices=lowerCAmelCase__ ) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: __a = element['''base_index'''] in extreme_dict if element["is_extreme"]: __a = extreme_dict[element['''base_index''']]['''copies'''] print(f'''Original dataset size: {len(lowerCAmelCase__ )}''' ) print(f'''Number of duplicate clusters: {len(lowerCAmelCase__ )}''' ) print(f'''Files in duplicate cluster: {len(lowerCAmelCase__ )}''' ) print(f'''Unique files in duplicate cluster: {len(lowerCAmelCase__ )}''' ) print(f'''Filtered dataset size: {len(lowerCAmelCase__ )}''' ) return ds_filter, duplicate_clusters
45
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : float ) -> float: return 10 - x * x def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> float: # Bolzano theory in order to find if there is a root between a and b if equation(lowerCAmelCase__ ) * equation(lowerCAmelCase__ ) >= 0: raise ValueError('''Wrong space!''' ) __a = a while (b - a) >= 0.01: # Find middle point __a = (a + b) / 2 # Check if middle point is root if equation(lowerCAmelCase__ ) == 0.0: break # Decide the side to repeat the steps if equation(lowerCAmelCase__ ) * equation(lowerCAmelCase__ ) < 0: __a = c else: __a = c return c if __name__ == "__main__": import doctest doctest.testmod() print(bisection(-2, 5)) print(bisection(0, 6))
45
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase_ = { "configuration_roformer": ["ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "RoFormerConfig", "RoFormerOnnxConfig"], "tokenization_roformer": ["RoFormerTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["RoFormerTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "RoFormerForCausalLM", "RoFormerForMaskedLM", "RoFormerForMultipleChoice", "RoFormerForQuestionAnswering", "RoFormerForSequenceClassification", "RoFormerForTokenClassification", "RoFormerLayer", "RoFormerModel", "RoFormerPreTrainedModel", "load_tf_weights_in_roformer", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFRoFormerForCausalLM", "TFRoFormerForMaskedLM", "TFRoFormerForMultipleChoice", "TFRoFormerForQuestionAnswering", "TFRoFormerForSequenceClassification", "TFRoFormerForTokenClassification", "TFRoFormerLayer", "TFRoFormerModel", "TFRoFormerPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "FlaxRoFormerForMaskedLM", "FlaxRoFormerForMultipleChoice", "FlaxRoFormerForQuestionAnswering", "FlaxRoFormerForSequenceClassification", "FlaxRoFormerForTokenClassification", "FlaxRoFormerModel", "FlaxRoFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
1
"""simple docstring""" from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = { "configuration_informer": [ "INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "InformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "InformerForPrediction", "InformerModel", "InformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_informer import INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, InformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_informer import ( INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, InformerForPrediction, InformerModel, InformerPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
"""simple docstring""" import warnings from ..trainer import Trainer from ..utils import logging lowercase_ = logging.get_logger(__name__) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , _a=None , **_a ): warnings.warn( '''`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` ''' '''instead.''' , _a , ) super().__init__(args=_a , **_a )
45
1
"""simple docstring""" from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : List[str] __UpperCAmelCase : Optional[str] = None # Automatically constructed __UpperCAmelCase : ClassVar[str] = "dict" __UpperCAmelCase : ClassVar[Any] = None __UpperCAmelCase : str = field(default='Translation' , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __call__( self ): return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def __UpperCAmelCase ( self ): from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : Optional[List] = None __UpperCAmelCase : Optional[int] = None __UpperCAmelCase : Optional[str] = None # Automatically constructed __UpperCAmelCase : ClassVar[str] = "dict" __UpperCAmelCase : ClassVar[Any] = None __UpperCAmelCase : str = field(default='TranslationVariableLanguages' , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __UpperCAmelCase ( self ): __a = sorted(set(self.languages ) ) if self.languages else None __a = len(self.languages ) if self.languages else None def __call__( self ): return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def __UpperCAmelCase ( self , _a ): __a = set(self.languages ) if self.languages and set(_a ) - lang_set: raise ValueError( f'''Some languages in example ({', '.join(sorted(set(_a ) - lang_set ) )}) are not in valid set ({', '.join(_a )}).''' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. __a = [] for lang, text in translation_dict.items(): if isinstance(_a , _a ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. __a , __a = zip(*sorted(_a ) ) return {"language": languages, "translation": translations} def __UpperCAmelCase ( self ): from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
45
"""simple docstring""" import math def lowercase ( lowerCAmelCase__ : list , lowerCAmelCase__ : int ) -> int: __a = len(lowerCAmelCase__ ) __a = int(math.floor(math.sqrt(lowerCAmelCase__ ) ) ) __a = 0 while arr[min(lowerCAmelCase__ , lowerCAmelCase__ ) - 1] < x: __a = step step += int(math.floor(math.sqrt(lowerCAmelCase__ ) ) ) if prev >= n: return -1 while arr[prev] < x: __a = prev + 1 if prev == min(lowerCAmelCase__ , lowerCAmelCase__ ): return -1 if arr[prev] == x: return prev return -1 if __name__ == "__main__": lowercase_ = input("Enter numbers separated by a comma:\n").strip() lowercase_ = [int(item) for item in user_input.split(",")] lowercase_ = int(input("Enter the number to be searched:\n")) lowercase_ = jump_search(arr, x) if res == -1: print("Number not found!") else: print(F'''Number {x} is at index {res}''')
45
1
"""simple docstring""" import json import os import unittest from transformers.models.biogpt.tokenization_biogpt import VOCAB_FILES_NAMES, BioGptTokenizer from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = BioGptTokenizer __UpperCAmelCase : List[Any] = False def __UpperCAmelCase ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt __a = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''w</w>''', '''r</w>''', '''t</w>''', '''lo''', '''low''', '''er</w>''', '''low</w>''', '''lowest</w>''', '''newer</w>''', '''wider</w>''', '''<unk>''', ] __a = dict(zip(_a , range(len(_a ) ) ) ) __a = ['''l o 123''', '''lo w 1456''', '''e r</w> 1789''', ''''''] __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''' ) as fp: fp.write(json.dumps(_a ) ) with open(self.merges_file , '''w''' ) as fp: fp.write('''\n'''.join(_a ) ) def __UpperCAmelCase ( self , _a ): __a = '''lower newer''' __a = '''lower newer''' return input_text, output_text def __UpperCAmelCase ( self ): __a = BioGptTokenizer(self.vocab_file , self.merges_file ) __a = '''lower''' __a = ['''low''', '''er</w>'''] __a = tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) __a = tokens + ['''<unk>'''] __a = [14, 15, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , _a ) @slow def __UpperCAmelCase ( self ): __a = BioGptTokenizer.from_pretrained('''microsoft/biogpt''' ) __a = tokenizer.encode('''sequence builders''' , add_special_tokens=_a ) __a = tokenizer.encode('''multi-sequence build''' , add_special_tokens=_a ) __a = tokenizer.build_inputs_with_special_tokens(_a ) __a = tokenizer.build_inputs_with_special_tokens(_a , _a ) self.assertTrue(encoded_sentence == [2] + text ) self.assertTrue(encoded_pair == [2] + text + [2] + text_a )
45
"""simple docstring""" lowercase_ = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] lowercase_ = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] lowercase_ = { 0: "Sunday", 1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", 5: "Friday", 6: "Saturday", } def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int ) -> str: assert len(str(lowerCAmelCase__ ) ) > 2, "year should be in YYYY format" assert 1 <= month <= 12, "month should be between 1 to 12" assert 1 <= day <= 31, "day should be between 1 to 31" # Doomsday algorithm: __a = year // 100 __a = (5 * (century % 4) + 2) % 7 __a = year % 100 __a = centurian % 12 __a = ( (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor ) % 7 __a = ( DOOMSDAY_NOT_LEAP[month - 1] if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) else DOOMSDAY_LEAP[month - 1] ) __a = (dooms_day + day - day_anchor) % 7 return WEEK_DAY_NAMES[week_day] if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" 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 lowercase_ = [ "openmmlab/upernet-convnext-tiny", # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring lowercase_ = "UperNetConfig" class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a , _a = 0 , _a = False , _a = 1 , ): super().__init__() __a = nn.Convad( in_channels=_a , out_channels=_a , kernel_size=_a , padding=_a , bias=_a , dilation=_a , ) __a = nn.BatchNormad(_a ) __a = nn.ReLU() def __UpperCAmelCase ( self , _a ): __a = self.conv(_a ) __a = self.batch_norm(_a ) __a = self.activation(_a ) return output class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a ): super().__init__() __a = [ nn.AdaptiveAvgPoolad(_a ), UperNetConvModule(_a , _a , kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(_a ) , _a ) def __UpperCAmelCase ( self , _a ): __a = input for layer in self.layers: __a = layer(_a ) return hidden_state class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a , _a ): super().__init__() __a = pool_scales __a = align_corners __a = in_channels __a = channels __a = [] for i, pool_scale in enumerate(_a ): __a = UperNetPyramidPoolingBlock(pool_scale=_a , in_channels=_a , channels=_a ) self.blocks.append(_a ) self.add_module(str(_a ) , _a ) def __UpperCAmelCase ( self , _a ): __a = [] for ppm in self.blocks: __a = ppm(_a ) __a = nn.functional.interpolate( _a , size=x.size()[2:] , mode='''bilinear''' , align_corners=self.align_corners ) ppm_outs.append(_a ) return ppm_outs class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a ): super().__init__() __a = config __a = config.pool_scales # e.g. (1, 2, 3, 6) __a = in_channels __a = config.hidden_size __a = False __a = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) # PSP Module __a = UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) __a = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module __a = nn.ModuleList() __a = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer __a = UperNetConvModule(_a , self.channels , kernel_size=1 ) __a = UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1 ) self.lateral_convs.append(_a ) self.fpn_convs.append(_a ) __a = UperNetConvModule( len(self.in_channels ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def __UpperCAmelCase ( self ): self.apply(self._init_weights ) def __UpperCAmelCase ( self , _a ): if isinstance(_a , 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 __UpperCAmelCase ( self , _a ): __a = inputs[-1] __a = [x] psp_outs.extend(self.psp_modules(_a ) ) __a = torch.cat(_a , dim=1 ) __a = self.bottleneck(_a ) return output def __UpperCAmelCase ( self , _a ): # build laterals __a = [lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(_a ) ) # build top-down path __a = len(_a ) for i in range(used_backbone_levels - 1 , 0 , -1 ): __a = laterals[i - 1].shape[2:] __a = laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=_a , mode='''bilinear''' , align_corners=self.align_corners ) # build outputs __a = [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 ): __a = nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode='''bilinear''' , align_corners=self.align_corners ) __a = torch.cat(_a , dim=1 ) __a = self.fpn_bottleneck(_a ) __a = self.classifier(_a ) return output class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a = 2 , _a = 3 , _a = 1 ): super().__init__() __a = config __a = config.auxiliary_in_channels __a = config.auxiliary_channels __a = config.auxiliary_num_convs __a = config.auxiliary_concat_input __a = in_index __a = (kernel_size // 2) * dilation __a = [] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=_a , padding=_a , dilation=_a ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=_a , padding=_a , dilation=_a ) ) if self.num_convs == 0: __a = nn.Identity() else: __a = nn.Sequential(*_a ) if self.concat_input: __a = UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=_a , padding=kernel_size // 2 ) __a = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) def __UpperCAmelCase ( self ): self.apply(self._init_weights ) def __UpperCAmelCase ( self , _a ): if isinstance(_a , 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 __UpperCAmelCase ( self , _a ): # just take the relevant feature maps __a = encoder_hidden_states[self.in_index] __a = self.convs(_a ) if self.concat_input: __a = self.conv_cat(torch.cat([hidden_states, output] , dim=1 ) ) __a = self.classifier(_a ) return output class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : int = UperNetConfig __UpperCAmelCase : Union[str, Any] = 'pixel_values' __UpperCAmelCase : Dict = True def __UpperCAmelCase ( self , _a ): if isinstance(_a , _a ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def __UpperCAmelCase ( self ): self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def __UpperCAmelCase ( self , _a , _a=False ): if isinstance(_a , _a ): __a = value lowercase_ = r"\n Parameters:\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n config ([`UperNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n" lowercase_ = r"\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n output_attentions (`bool`, *optional*):\n Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n `attentions` under returned tensors for more detail.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n returned tensors for more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n" @add_start_docstrings( 'UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.' , __SCREAMING_SNAKE_CASE , ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , _a ): super().__init__(_a ) __a = AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) __a = UperNetHead(_a , in_channels=self.backbone.channels ) __a = UperNetFCNHead(_a ) 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=_a , config_class=_CONFIG_FOR_DOC ) def __UpperCAmelCase ( self , _a = None , _a = None , _a = None , _a = None , _a = None , ): __a = return_dict if return_dict is not None else self.config.use_return_dict __a = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __a = output_attentions if output_attentions is not None else self.config.output_attentions __a = self.backbone.forward_with_filtered_kwargs( _a , output_hidden_states=_a , output_attentions=_a ) __a = outputs.feature_maps __a = self.decode_head(_a ) __a = nn.functional.interpolate(_a , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=_a ) __a = None if self.auxiliary_head is not None: __a = self.auxiliary_head(_a ) __a = nn.functional.interpolate( _a , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=_a ) __a = 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 __a = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) __a = loss_fct(_a , _a ) __a = loss_fct(_a , _a ) __a = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: __a = (logits,) + outputs[1:] else: __a = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=_a , logits=_a , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : list ) -> bool: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise ValueError('''Input series is not valid, valid series - [2, 4, 6]''' ) if len(lowerCAmelCase__ ) == 0: raise ValueError('''Input list must be a non empty list''' ) if len(lowerCAmelCase__ ) == 1: return True __a = series[1] - series[0] for index in range(len(lowerCAmelCase__ ) - 1 ): if series[index + 1] - series[index] != common_diff: return False return True def lowercase ( lowerCAmelCase__ : list ) -> float: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise ValueError('''Input series is not valid, valid series - [2, 4, 6]''' ) if len(lowerCAmelCase__ ) == 0: raise ValueError('''Input list must be a non empty list''' ) __a = 0 for val in series: answer += val return answer / len(lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" from collections import namedtuple lowercase_ = namedtuple("from_to", "from_ to") lowercase_ = { "cubicmeter": from_to(1, 1), "litre": from_to(0.001, 1_0_0_0), "kilolitre": from_to(1, 1), "gallon": from_to(0.00454, 264.172), "cubicyard": from_to(0.76455, 1.30795), "cubicfoot": from_to(0.028, 35.3147), "cup": from_to(0.000236588, 4226.75), } def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : str , lowerCAmelCase__ : str ) -> float: if from_type not in METRIC_CONVERSION: raise ValueError( f'''Invalid \'from_type\' value: {from_type!r} Supported values are:\n''' + ''', '''.join(lowerCAmelCase__ ) ) if to_type not in METRIC_CONVERSION: raise ValueError( f'''Invalid \'to_type\' value: {to_type!r}. Supported values are:\n''' + ''', '''.join(lowerCAmelCase__ ) ) return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to if __name__ == "__main__": import doctest doctest.testmod()
45
"""simple docstring""" from argparse import ArgumentParser, Namespace from ..utils import logging from . import BaseTransformersCLICommand def lowercase ( lowerCAmelCase__ : Namespace ) -> Tuple: return ConvertCommand( args.model_type , args.tf_checkpoint , args.pytorch_dump_output , args.config , args.finetuning_task_name ) lowercase_ = "\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n" class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' @staticmethod def __UpperCAmelCase ( _a ): __a = parser.add_parser( '''convert''' , help='''CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.''' , ) train_parser.add_argument('''--model_type''' , type=_a , required=_a , help='''Model\'s type.''' ) train_parser.add_argument( '''--tf_checkpoint''' , type=_a , required=_a , help='''TensorFlow checkpoint path or folder.''' ) train_parser.add_argument( '''--pytorch_dump_output''' , type=_a , required=_a , help='''Path to the PyTorch saved model output.''' ) train_parser.add_argument('''--config''' , type=_a , default='''''' , help='''Configuration file path or folder.''' ) train_parser.add_argument( '''--finetuning_task_name''' , type=_a , default=_a , help='''Optional fine-tuning task name if the TF model was a finetuned model.''' , ) train_parser.set_defaults(func=_a ) def __init__( self , _a , _a , _a , _a , _a , *_a , ): __a = logging.get_logger('''transformers-cli/converting''' ) self._logger.info(f'''Loading model {model_type}''' ) __a = model_type __a = tf_checkpoint __a = pytorch_dump_output __a = config __a = finetuning_task_name def __UpperCAmelCase ( self ): if self._model_type == "albert": try: from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "bert": try: from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "funnel": try: from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "t5": try: from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "gpt": from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import ( convert_openai_checkpoint_to_pytorch, ) convert_openai_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "transfo_xl": try: from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import ( convert_transfo_xl_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) if "ckpt" in self._tf_checkpoint.lower(): __a = self._tf_checkpoint __a = '''''' else: __a = self._tf_checkpoint __a = '''''' convert_transfo_xl_checkpoint_to_pytorch( _a , self._config , self._pytorch_dump_output , _a ) elif self._model_type == "gpt2": try: from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import ( convert_gpta_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "xlnet": try: from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import ( convert_xlnet_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_xlnet_checkpoint_to_pytorch( self._tf_checkpoint , self._config , self._pytorch_dump_output , self._finetuning_task_name ) elif self._model_type == "xlm": from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import ( convert_xlm_checkpoint_to_pytorch, ) convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "lxmert": from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import ( convert_lxmert_checkpoint_to_pytorch, ) convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "rembert": from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import ( convert_rembert_tf_checkpoint_to_pytorch, ) convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) else: raise ValueError( '''--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]''' )
45
1
"""simple docstring""" import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( UniSpeechConfig, UniSpeechForCTC, UniSpeechForPreTraining, WavaVecaFeatureExtractor, WavaVecaPhonemeCTCTokenizer, WavaVecaProcessor, logging, ) logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) lowercase_ = { "post_extract_proj": "feature_projection.projection", "encoder.pos_conv.0": "encoder.pos_conv_embed.conv", "self_attn.k_proj": "encoder.layers.*.attention.k_proj", "self_attn.v_proj": "encoder.layers.*.attention.v_proj", "self_attn.q_proj": "encoder.layers.*.attention.q_proj", "self_attn.out_proj": "encoder.layers.*.attention.out_proj", "self_attn_layer_norm": "encoder.layers.*.layer_norm", "fc1": "encoder.layers.*.feed_forward.intermediate_dense", "fc2": "encoder.layers.*.feed_forward.output_dense", "final_layer_norm": "encoder.layers.*.final_layer_norm", "encoder.layer_norm": "encoder.layer_norm", "w2v_model.layer_norm": "feature_projection.layer_norm", "quantizer.weight_proj": "quantizer.weight_proj", "quantizer.vars": "quantizer.codevectors", "project_q": "project_q", "final_proj": "project_hid", "w2v_encoder.proj": "ctc_proj", "mask_emb": "masked_spec_embed", } lowercase_ = [ "ctc_proj", "quantizer.weight_proj", "quantizer.codevectors", "project_q", "project_hid", ] def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any ) -> Dict: for attribute in key.split('''.''' ): if is_finetuned: if attribute in ["quantizer", "project_q", "project_hid"]: # those layers are only relevant for pretraining and should be dropped return if attribute == "ctc_proj": # we should rename `ctc_proj` to `lm_head` for fine-tuned phoneme models __a = '''lm_head''' __a = getattr(lowerCAmelCase__ , lowerCAmelCase__ ) if weight_type is not None: __a = getattr(lowerCAmelCase__ , lowerCAmelCase__ ).shape else: __a = hf_pointer.shape assert hf_shape == value.shape, ( f'''Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be''' f''' {value.shape} for {full_name}''' ) if weight_type == "weight": __a = value elif weight_type == "weight_g": __a = value elif weight_type == "weight_v": __a = value elif weight_type == "bias": __a = value else: __a = value logger.info(f'''{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.''' ) def lowercase ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple ) -> str: __a = [] __a = fairseq_model.state_dict() __a = hf_model.unispeech.feature_extractor for name, value in fairseq_dict.items(): __a = False if "conv_layers" in name: load_conv_layer( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , hf_model.config.feat_extract_norm == '''group''' , ) __a = True else: for key, mapped_key in MAPPING.items(): __a = '''unispeech.''' + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split('''w2v_model.''' )[-1] == name.split('''.''' )[0]: __a = True if "*" in mapped_key: __a = name.split(lowerCAmelCase__ )[0].split('''.''' )[-2] __a = mapped_key.replace('''*''' , lowerCAmelCase__ ) if "weight_g" in name: __a = '''weight_g''' elif "weight_v" in name: __a = '''weight_v''' elif "bias" in name: __a = '''bias''' elif "weight" in name: # TODO: don't match quantizer.weight_proj __a = '''weight''' else: __a = None set_recursively(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) continue if not is_used: unused_weights.append(lowerCAmelCase__ ) logger.warning(f'''Unused weights: {unused_weights}''' ) def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : str , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int ) -> Optional[int]: __a = full_name.split('''conv_layers.''' )[-1] __a = name.split('''.''' ) __a = int(items[0] ) __a = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) __a = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) __a = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f'''{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was''' " found." ) __a = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.''' ) __a = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(lowerCAmelCase__ ) @torch.no_grad() def lowercase ( lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[int]=None , lowerCAmelCase__ : List[str]=None , lowerCAmelCase__ : Dict=True ) -> List[str]: if config_path is not None: __a = UniSpeechConfig.from_pretrained(lowerCAmelCase__ ) else: __a = UniSpeechConfig() if is_finetuned: if dict_path: __a = Dictionary.load_from_json(lowerCAmelCase__ ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq __a = target_dict.pad_index __a = target_dict.bos_index __a = target_dict.eos_index __a = len(target_dict.symbols ) __a = os.path.join(lowerCAmelCase__ , '''vocab.json''' ) if not os.path.isdir(lowerCAmelCase__ ): logger.error('''--pytorch_dump_folder_path ({}) should be a directory'''.format(lowerCAmelCase__ ) ) return os.makedirs(lowerCAmelCase__ , exist_ok=lowerCAmelCase__ ) __a = target_dict.indices # fairseq has the <pad> and <s> switched __a = 42 __a = 43 with open(lowerCAmelCase__ , '''w''' , encoding='''utf-8''' ) as vocab_handle: json.dump(lowerCAmelCase__ , lowerCAmelCase__ ) __a = WavaVecaPhonemeCTCTokenizer( lowerCAmelCase__ , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token='''|''' , do_lower_case=lowerCAmelCase__ , ) __a = True if config.feat_extract_norm == '''layer''' else False __a = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=lowerCAmelCase__ , return_attention_mask=lowerCAmelCase__ , ) __a = WavaVecaProcessor(feature_extractor=lowerCAmelCase__ , tokenizer=lowerCAmelCase__ ) processor.save_pretrained(lowerCAmelCase__ ) __a = UniSpeechForCTC(lowerCAmelCase__ ) else: __a = UniSpeechForPreTraining(lowerCAmelCase__ ) if is_finetuned: __a , __a , __a = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'''data''': '''/'''.join(dict_path.split('''/''' )[:-1] ), '''w2v_path''': checkpoint_path} ) else: __a , __a , __a = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) __a = model[0].eval() recursively_load_weights(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) hf_unispeech.save_pretrained(lowerCAmelCase__ ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint") parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") parser.add_argument( "--not_finetuned", action="store_true", help="Whether the model to convert is a fine-tuned model or not" ) lowercase_ = parser.parse_args() convert_unispeech_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
45
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "microsoft/beit-base-patch16-224-pt22k": ( "https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json" ), # See all BEiT models at https://huggingface.co/models?filter=beit } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = 'beit' def __init__( self , _a=8_192 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.0 , _a=0.0 , _a=0.02 , _a=1E-12 , _a=224 , _a=16 , _a=3 , _a=False , _a=False , _a=False , _a=False , _a=0.1 , _a=0.1 , _a=True , _a=[3, 5, 7, 11] , _a=[1, 2, 3, 6] , _a=True , _a=0.4 , _a=256 , _a=1 , _a=False , _a=255 , **_a , ): super().__init__(**_a ) __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 = initializer_range __a = layer_norm_eps __a = image_size __a = patch_size __a = num_channels __a = use_mask_token __a = use_absolute_position_embeddings __a = use_relative_position_bias __a = use_shared_relative_position_bias __a = layer_scale_init_value __a = drop_path_rate __a = use_mean_pooling # decode head attributes (semantic segmentation) __a = out_indices __a = pool_scales # auxiliary head attributes (semantic segmentation) __a = use_auxiliary_head __a = auxiliary_loss_weight __a = auxiliary_channels __a = auxiliary_num_convs __a = auxiliary_concat_input __a = semantic_loss_ignore_index class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[Any] = version.parse('1.11' ) @property def __UpperCAmelCase ( self ): return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __UpperCAmelCase ( self ): return 1E-4
45
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> List[Any]: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) if n == 0: return 0 __a = float('''-inf''' ) for i in range(1 , n + 1 ): __a = max( lowerCAmelCase__ , prices[i - 1] + naive_cut_rod_recursive(n - i , lowerCAmelCase__ ) ) return max_revue def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> List[str]: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) __a = [float('''-inf''' ) for _ in range(n + 1 )] return _top_down_cut_rod_recursive(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list , lowerCAmelCase__ : list ) -> Union[str, Any]: if max_rev[n] >= 0: return max_rev[n] elif n == 0: return 0 else: __a = float('''-inf''' ) for i in range(1 , n + 1 ): __a = max( lowerCAmelCase__ , prices[i - 1] + _top_down_cut_rod_recursive(n - i , lowerCAmelCase__ , lowerCAmelCase__ ) , ) __a = max_revenue return max_rev[n] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> Dict: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of # length 0. __a = [float('''-inf''' ) for _ in range(n + 1 )] __a = 0 for i in range(1 , n + 1 ): __a = max_rev[i] for j in range(1 , i + 1 ): __a = max(lowerCAmelCase__ , prices[j - 1] + max_rev[i - j] ) __a = max_revenue_i return max_rev[n] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> str: if n < 0: __a = f'''n must be greater than or equal to 0. Got n = {n}''' raise ValueError(lowerCAmelCase__ ) if n > len(lowerCAmelCase__ ): __a = ( '''Each integral piece of rod must have a corresponding price. ''' f'''Got n = {n} but length of prices = {len(lowerCAmelCase__ )}''' ) raise ValueError(lowerCAmelCase__ ) def lowercase ( ) -> int: __a = [6, 10, 12, 15, 20, 23] __a = len(lowerCAmelCase__ ) # the best revenue comes from cutting the rod into 6 pieces, each # of length 1 resulting in a revenue of 6 * 6 = 36. __a = 36 __a = top_down_cut_rod(lowerCAmelCase__ , lowerCAmelCase__ ) __a = bottom_up_cut_rod(lowerCAmelCase__ , lowerCAmelCase__ ) __a = naive_cut_rod_recursive(lowerCAmelCase__ , lowerCAmelCase__ ) assert expected_max_revenue == max_rev_top_down assert max_rev_top_down == max_rev_bottom_up assert max_rev_bottom_up == max_rev_naive if __name__ == "__main__": main()
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str ) -> list: if n_term == "": return [] __a = [] for temp in range(int(lowerCAmelCase__ ) ): series.append(f'''1/{temp + 1}''' if series else '''1''' ) return series if __name__ == "__main__": lowercase_ = input("Enter the last number (nth term) of the Harmonic Series") print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n") print(harmonic_series(nth_term))
45
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: __a = set() # Replace all the whitespace in our sentence __a = input_str.replace(''' ''' , '''''' ) for alpha in input_str: if "a" <= alpha.lower() <= "z": frequency.add(alpha.lower() ) return len(lowerCAmelCase__ ) == 26 def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: __a = [False] * 26 for char in input_str: if char.islower(): __a = True elif char.isupper(): __a = True return all(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: return len({char for char in input_str.lower() if char.isalpha()} ) == 26 def lowercase ( ) -> None: from timeit import timeit __a = '''from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest''' print(timeit('''is_pangram()''' , setup=lowerCAmelCase__ ) ) print(timeit('''is_pangram_faster()''' , setup=lowerCAmelCase__ ) ) print(timeit('''is_pangram_fastest()''' , setup=lowerCAmelCase__ ) ) # 5.348480500048026, 2.6477354579837993, 1.8470395830227062 # 5.036091582966037, 2.644472333951853, 1.8869528750656173 if __name__ == "__main__": import doctest doctest.testmod() benchmark()
45
"""simple docstring""" from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : List[str] __UpperCAmelCase : Optional[str] = None # Automatically constructed __UpperCAmelCase : ClassVar[str] = "dict" __UpperCAmelCase : ClassVar[Any] = None __UpperCAmelCase : str = field(default='Translation' , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __call__( self ): return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def __UpperCAmelCase ( self ): from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : Optional[List] = None __UpperCAmelCase : Optional[int] = None __UpperCAmelCase : Optional[str] = None # Automatically constructed __UpperCAmelCase : ClassVar[str] = "dict" __UpperCAmelCase : ClassVar[Any] = None __UpperCAmelCase : str = field(default='TranslationVariableLanguages' , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __UpperCAmelCase ( self ): __a = sorted(set(self.languages ) ) if self.languages else None __a = len(self.languages ) if self.languages else None def __call__( self ): return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def __UpperCAmelCase ( self , _a ): __a = set(self.languages ) if self.languages and set(_a ) - lang_set: raise ValueError( f'''Some languages in example ({', '.join(sorted(set(_a ) - lang_set ) )}) are not in valid set ({', '.join(_a )}).''' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. __a = [] for lang, text in translation_dict.items(): if isinstance(_a , _a ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. __a , __a = zip(*sorted(_a ) ) return {"language": languages, "translation": translations} def __UpperCAmelCase ( self ): from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
45
1
"""simple docstring""" from __future__ import annotations import collections import pprint from pathlib import Path def lowercase ( lowerCAmelCase__ : str ) -> str: return "".join(sorted(lowerCAmelCase__ ) ) def lowercase ( lowerCAmelCase__ : str ) -> list[str]: return word_by_signature[signature(lowerCAmelCase__ )] lowercase_ = Path(__file__).parent.joinpath("words.txt").read_text(encoding="utf-8") lowercase_ = sorted({word.strip().lower() for word in data.splitlines()}) lowercase_ = collections.defaultdict(list) for word in word_list: word_by_signature[signature(word)].append(word) if __name__ == "__main__": lowercase_ = {word: anagram(word) for word in word_list if len(anagram(word)) > 1} with open("anagrams.txt", "w") as file: file.write("all_anagrams = \n ") file.write(pprint.pformat(all_anagrams))
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> List[Any]: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) if n == 0: return 0 __a = float('''-inf''' ) for i in range(1 , n + 1 ): __a = max( lowerCAmelCase__ , prices[i - 1] + naive_cut_rod_recursive(n - i , lowerCAmelCase__ ) ) return max_revue def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> List[str]: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) __a = [float('''-inf''' ) for _ in range(n + 1 )] return _top_down_cut_rod_recursive(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list , lowerCAmelCase__ : list ) -> Union[str, Any]: if max_rev[n] >= 0: return max_rev[n] elif n == 0: return 0 else: __a = float('''-inf''' ) for i in range(1 , n + 1 ): __a = max( lowerCAmelCase__ , prices[i - 1] + _top_down_cut_rod_recursive(n - i , lowerCAmelCase__ , lowerCAmelCase__ ) , ) __a = max_revenue return max_rev[n] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> Dict: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of # length 0. __a = [float('''-inf''' ) for _ in range(n + 1 )] __a = 0 for i in range(1 , n + 1 ): __a = max_rev[i] for j in range(1 , i + 1 ): __a = max(lowerCAmelCase__ , prices[j - 1] + max_rev[i - j] ) __a = max_revenue_i return max_rev[n] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> str: if n < 0: __a = f'''n must be greater than or equal to 0. Got n = {n}''' raise ValueError(lowerCAmelCase__ ) if n > len(lowerCAmelCase__ ): __a = ( '''Each integral piece of rod must have a corresponding price. ''' f'''Got n = {n} but length of prices = {len(lowerCAmelCase__ )}''' ) raise ValueError(lowerCAmelCase__ ) def lowercase ( ) -> int: __a = [6, 10, 12, 15, 20, 23] __a = len(lowerCAmelCase__ ) # the best revenue comes from cutting the rod into 6 pieces, each # of length 1 resulting in a revenue of 6 * 6 = 36. __a = 36 __a = top_down_cut_rod(lowerCAmelCase__ , lowerCAmelCase__ ) __a = bottom_up_cut_rod(lowerCAmelCase__ , lowerCAmelCase__ ) __a = naive_cut_rod_recursive(lowerCAmelCase__ , lowerCAmelCase__ ) assert expected_max_revenue == max_rev_top_down assert max_rev_top_down == max_rev_bottom_up assert max_rev_bottom_up == max_rev_naive if __name__ == "__main__": main()
45
1
"""simple docstring""" import unittest from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __lowerCAmelCase : '''simple docstring''' @staticmethod def __UpperCAmelCase ( *_a , **_a ): pass @is_pipeline_test @require_vision @require_torch class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : List[str] = MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING def __UpperCAmelCase ( self , _a , _a , _a ): __a = pipeline( '''zero-shot-object-detection''' , model='''hf-internal-testing/tiny-random-owlvit-object-detection''' ) __a = [ { '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''', '''candidate_labels''': ['''cat''', '''remote''', '''couch'''], } ] return object_detector, examples def __UpperCAmelCase ( self , _a , _a ): __a = object_detector(examples[0] , threshold=0.0 ) __a = len(_a ) self.assertGreater(_a , 0 ) self.assertEqual( _a , [ { '''score''': ANY(_a ), '''label''': ANY(_a ), '''box''': {'''xmin''': ANY(_a ), '''ymin''': ANY(_a ), '''xmax''': ANY(_a ), '''ymax''': ANY(_a )}, } for i in range(_a ) ] , ) @require_tf @unittest.skip('''Zero Shot Object Detection not implemented in TF''' ) def __UpperCAmelCase ( self ): pass @require_torch def __UpperCAmelCase ( self ): __a = pipeline( '''zero-shot-object-detection''' , model='''hf-internal-testing/tiny-random-owlvit-object-detection''' ) __a = object_detector( '''./tests/fixtures/tests_samples/COCO/000000039769.png''' , candidate_labels=['''cat''', '''remote''', '''couch'''] , threshold=0.64 , ) self.assertEqual( nested_simplify(_a , decimals=4 ) , [ {'''score''': 0.7235, '''label''': '''cat''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}}, {'''score''': 0.7218, '''label''': '''remote''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}}, {'''score''': 0.7184, '''label''': '''couch''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}}, {'''score''': 0.6748, '''label''': '''remote''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}}, {'''score''': 0.6656, '''label''': '''cat''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}}, {'''score''': 0.6614, '''label''': '''couch''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}}, {'''score''': 0.6456, '''label''': '''remote''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}}, {'''score''': 0.642, '''label''': '''remote''', '''box''': {'''xmin''': 67, '''ymin''': 274, '''xmax''': 93, '''ymax''': 297}}, {'''score''': 0.6419, '''label''': '''cat''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}}, ] , ) __a = object_detector( [ { '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''', '''candidate_labels''': ['''cat''', '''remote''', '''couch'''], } ] , threshold=0.64 , ) self.assertEqual( nested_simplify(_a , decimals=4 ) , [ [ {'''score''': 0.7235, '''label''': '''cat''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}}, {'''score''': 0.7218, '''label''': '''remote''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}}, {'''score''': 0.7184, '''label''': '''couch''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}}, {'''score''': 0.6748, '''label''': '''remote''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}}, {'''score''': 0.6656, '''label''': '''cat''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}}, {'''score''': 0.6614, '''label''': '''couch''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}}, {'''score''': 0.6456, '''label''': '''remote''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}}, {'''score''': 0.642, '''label''': '''remote''', '''box''': {'''xmin''': 67, '''ymin''': 274, '''xmax''': 93, '''ymax''': 297}}, {'''score''': 0.6419, '''label''': '''cat''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}}, ] ] , ) @require_torch @slow def __UpperCAmelCase ( self ): __a = pipeline('''zero-shot-object-detection''' ) __a = object_detector( '''http://images.cocodataset.org/val2017/000000039769.jpg''' , candidate_labels=['''cat''', '''remote''', '''couch'''] , ) self.assertEqual( nested_simplify(_a , decimals=4 ) , [ {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}}, {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}}, {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}}, {'''score''': 0.1474, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}}, {'''score''': 0.1208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}}, ] , ) __a = object_detector( [ { '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''candidate_labels''': ['''cat''', '''remote''', '''couch'''], }, { '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''candidate_labels''': ['''cat''', '''remote''', '''couch'''], }, ] , ) self.assertEqual( nested_simplify(_a , decimals=4 ) , [ [ {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}}, {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}}, {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}}, {'''score''': 0.1474, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}}, {'''score''': 0.1208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}}, ], [ {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}}, {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}}, {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}}, {'''score''': 0.1474, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}}, {'''score''': 0.1208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}}, ], ] , ) @require_tf @unittest.skip('''Zero Shot Object Detection not implemented in TF''' ) def __UpperCAmelCase ( self ): pass @require_torch @slow def __UpperCAmelCase ( self ): __a = 0.2 __a = pipeline('''zero-shot-object-detection''' ) __a = object_detector( '''http://images.cocodataset.org/val2017/000000039769.jpg''' , candidate_labels=['''cat''', '''remote''', '''couch'''] , threshold=_a , ) self.assertEqual( nested_simplify(_a , decimals=4 ) , [ {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}}, {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}}, {'''score''': 0.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}}, ] , ) @require_torch @slow def __UpperCAmelCase ( self ): __a = 2 __a = pipeline('''zero-shot-object-detection''' ) __a = object_detector( '''http://images.cocodataset.org/val2017/000000039769.jpg''' , candidate_labels=['''cat''', '''remote''', '''couch'''] , top_k=_a , ) self.assertEqual( nested_simplify(_a , decimals=4 ) , [ {'''score''': 0.2868, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}}, {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}}, ] , )
45
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) lowercase_ = {"configuration_unispeech": ["UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP", "UniSpeechConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST", "UniSpeechForCTC", "UniSpeechForPreTraining", "UniSpeechForSequenceClassification", "UniSpeechModel", "UniSpeechPreTrainedModel", ] if TYPE_CHECKING: from .configuration_unispeech import UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_unispeech import ( UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST, UniSpeechForCTC, UniSpeechForPreTraining, UniSpeechForSequenceClassification, UniSpeechModel, UniSpeechPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
1
"""simple docstring""" 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 lowercase_ = "." if __name__ == "__main__": lowercase_ = os.path.join(REPO_PATH, "utils/documentation_tests.txt") lowercase_ = [] lowercase_ = [] with open(doctest_file_path) as fp: for line in fp: lowercase_ = line.strip() lowercase_ = 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: lowercase_ = "\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.")
45
"""simple docstring""" import unittest import torch from torch import nn from diffusers.models.activations import get_activation class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = get_activation('''swish''' ) self.assertIsInstance(_a , nn.SiLU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''silu''' ) self.assertIsInstance(_a , nn.SiLU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''mish''' ) self.assertIsInstance(_a , nn.Mish ) self.assertEqual(act(torch.tensor(-200 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''gelu''' ) self.assertIsInstance(_a , nn.GELU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 )
45
1
"""simple docstring""" import argparse import torch from transformers import ( UniSpeechSatConfig, UniSpeechSatForAudioFrameClassification, UniSpeechSatForSequenceClassification, UniSpeechSatForXVector, WavaVecaFeatureExtractor, logging, ) logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) def lowercase ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : int , lowerCAmelCase__ : Dict ) -> Any: __a = UniSpeechSatForSequenceClassification.from_pretrained(lowerCAmelCase__ , config=lowerCAmelCase__ ) __a = downstream_dict['''projector.weight'''] __a = downstream_dict['''projector.bias'''] __a = downstream_dict['''model.post_net.linear.weight'''] __a = downstream_dict['''model.post_net.linear.bias'''] return model def lowercase ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any] ) -> Any: __a = UniSpeechSatForAudioFrameClassification.from_pretrained(lowerCAmelCase__ , config=lowerCAmelCase__ ) __a = downstream_dict['''model.linear.weight'''] __a = downstream_dict['''model.linear.bias'''] return model def lowercase ( lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : Any ) -> str: __a = UniSpeechSatForXVector.from_pretrained(lowerCAmelCase__ , config=lowerCAmelCase__ ) __a = downstream_dict['''connector.weight'''] __a = downstream_dict['''connector.bias'''] for i, kernel_size in enumerate(hf_config.tdnn_kernel ): __a = downstream_dict[ f'''model.framelevel_feature_extractor.module.{i}.kernel.weight''' ] __a = downstream_dict[f'''model.framelevel_feature_extractor.module.{i}.kernel.bias'''] __a = downstream_dict['''model.utterancelevel_feature_extractor.linear1.weight'''] __a = downstream_dict['''model.utterancelevel_feature_extractor.linear1.bias'''] __a = downstream_dict['''model.utterancelevel_feature_extractor.linear2.weight'''] __a = downstream_dict['''model.utterancelevel_feature_extractor.linear2.bias'''] __a = downstream_dict['''objective.W'''] return model @torch.no_grad() def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Dict ) -> int: __a = torch.load(lowerCAmelCase__ , map_location='''cpu''' ) __a = checkpoint['''Downstream'''] __a = UniSpeechSatConfig.from_pretrained(lowerCAmelCase__ ) __a = WavaVecaFeatureExtractor.from_pretrained( lowerCAmelCase__ , return_attention_mask=lowerCAmelCase__ , do_normalize=lowerCAmelCase__ ) __a = hf_config.architectures[0] if arch.endswith('''ForSequenceClassification''' ): __a = convert_classification(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) elif arch.endswith('''ForAudioFrameClassification''' ): __a = convert_diarization(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) elif arch.endswith('''ForXVector''' ): __a = convert_xvector(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) else: raise NotImplementedError(f'''S3PRL weights conversion is not supported for {arch}''' ) if hf_config.use_weighted_layer_sum: __a = checkpoint['''Featurizer''']['''weights'''] hf_feature_extractor.save_pretrained(lowerCAmelCase__ ) hf_model.save_pretrained(lowerCAmelCase__ ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument( "--base_model_name", default=None, type=str, help="Name of the huggingface pretrained base model." ) parser.add_argument("--config_path", default=None, type=str, help="Path to the huggingface classifier config.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to the s3prl checkpoint.") parser.add_argument("--model_dump_path", default=None, type=str, help="Path to the final converted model.") lowercase_ = parser.parse_args() convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)
45
"""simple docstring""" from __future__ import annotations import unittest from transformers import EsmConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy import tensorflow as tf from transformers.models.esm.modeling_tf_esm import ( TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, TFEsmModel, ) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , ): __a = parent __a = 13 __a = 7 __a = True __a = True __a = True __a = 99 __a = 32 __a = 2 __a = 4 __a = 37 __a = '''gelu''' __a = 0.1 __a = 0.1 __a = 512 __a = 16 __a = 2 __a = 0.02 __a = 3 __a = 4 __a = None def __UpperCAmelCase ( self ): __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a = None if self.use_input_mask: __a = random_attention_mask([self.batch_size, self.seq_length] ) __a = None __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __a = ids_tensor([self.batch_size] , self.num_choices ) __a = EsmConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , pad_token_id=1 , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def __UpperCAmelCase ( self ): ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = self.prepare_config_and_inputs() __a = True __a = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __a = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , _a ): __a = TFEsmModel(config=_a ) __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} __a = model(_a ) __a = [input_ids, input_mask] __a = model(_a ) __a = model(_a ) 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 , _a , ): __a = True __a = TFEsmModel(config=_a ) __a = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''encoder_hidden_states''': encoder_hidden_states, '''encoder_attention_mask''': encoder_attention_mask, } __a = model(_a ) __a = [input_ids, input_mask] __a = model(_a , encoder_hidden_states=_a ) # Also check the case where encoder outputs are not passed __a = model(_a , attention_mask=_a ) 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 = TFEsmForMaskedLM(config=_a ) __a = model([input_ids, input_mask] ) 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 = self.num_labels __a = TFEsmForTokenClassification(config=_a ) __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} __a = model(_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = config_and_inputs __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = ( ( TFEsmModel, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, ) if is_tf_available() else () ) __UpperCAmelCase : Tuple = ( { 'feature-extraction': TFEsmModel, 'fill-mask': TFEsmForMaskedLM, 'text-classification': TFEsmForSequenceClassification, 'token-classification': TFEsmForTokenClassification, 'zero-shot': TFEsmForSequenceClassification, } if is_tf_available() else {} ) __UpperCAmelCase : Tuple = False __UpperCAmelCase : Union[str, Any] = False def __UpperCAmelCase ( self ): __a = TFEsmModelTester(self ) __a = ConfigTester(self , config_class=_a , hidden_size=37 ) def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_a ) @slow def __UpperCAmelCase ( self ): for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = TFEsmModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip('''Protein models do not support embedding resizing.''' ) def __UpperCAmelCase ( self ): pass @unittest.skip('''Protein models do not support embedding resizing.''' ) def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): __a , __a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a = model_class(_a ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class is TFEsmForMaskedLM: # Output embedding test differs from the main test because they're a matrix, not a layer __a = model.get_bias() assert isinstance(_a , _a ) for k, v in name.items(): assert isinstance(_a , tf.Variable ) else: __a = model.get_output_embeddings() assert x is None __a = model.get_bias() assert name is None @require_tf class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def __UpperCAmelCase ( self ): __a = TFEsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) __a = tf.constant([[0, 1, 2, 3, 4, 5]] ) __a = model(_a )[0] __a = [1, 6, 33] self.assertEqual(list(output.numpy().shape ) , _a ) # compare the actual values for a slice. __a = tf.constant( [ [ [8.92_1518, -10.58_9814, -6.467_1307], [-6.396_7156, -13.91_1377, -1.121_1915], [-7.78_1247, -13.95_1557, -3.74_0592], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-2 ) ) @slow def __UpperCAmelCase ( self ): __a = TFEsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) __a = tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) __a = model(_a )[0] # compare the actual values for a slice. __a = tf.constant( [ [ [0.1444_3092, 0.5412_5327, 0.324_7739], [0.3034_0484, 0.0052_6676, 0.3107_7722], [0.3227_8043, -0.2498_7096, 0.341_4628], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-4 ) )
45
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = { "configuration_bigbird_pegasus": [ "BIGBIRD_PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP", "BigBirdPegasusConfig", "BigBirdPegasusOnnxConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "BIGBIRD_PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST", "BigBirdPegasusForCausalLM", "BigBirdPegasusForConditionalGeneration", "BigBirdPegasusForQuestionAnswering", "BigBirdPegasusForSequenceClassification", "BigBirdPegasusModel", "BigBirdPegasusPreTrainedModel", ] if TYPE_CHECKING: from .configuration_bigbird_pegasus import ( BIGBIRD_PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP, BigBirdPegasusConfig, BigBirdPegasusOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bigbird_pegasus import ( BIGBIRD_PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST, BigBirdPegasusForCausalLM, BigBirdPegasusForConditionalGeneration, BigBirdPegasusForQuestionAnswering, BigBirdPegasusForSequenceClassification, BigBirdPegasusModel, BigBirdPegasusPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int ) -> str: if number > 0: raise ValueError('''input must be a negative integer''' ) __a = len(bin(lowerCAmelCase__ )[3:] ) __a = bin(abs(lowerCAmelCase__ ) - (1 << binary_number_length) )[3:] __a = ( ( '''1''' + '''0''' * (binary_number_length - len(lowerCAmelCase__ )) + twos_complement_number ) if number < 0 else '''0''' ) return "0b" + twos_complement_number if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" import gc import unittest from transformers import CTRLConfig, is_torch_available from transformers.testing_utils import require_torch, slow, 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 ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, ) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a=14 , _a=7 , _a=True , _a=True , _a=True , _a=True , _a=True , _a=99 , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=3 , _a=4 , _a=None , ): __a = parent __a = batch_size __a = seq_length __a = is_training __a = use_token_type_ids __a = use_input_mask __a = use_labels __a = use_mc_token_ids __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = type_sequence_label_size __a = initializer_range __a = num_labels __a = num_choices __a = scope __a = self.vocab_size - 1 def __UpperCAmelCase ( self ): __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a = None if self.use_input_mask: __a = random_attention_mask([self.batch_size, self.seq_length] ) __a = None if self.use_token_type_ids: __a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __a = None if self.use_mc_token_ids: __a = ids_tensor([self.batch_size, self.num_choices] , self.seq_length ) __a = None __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __a = ids_tensor([self.batch_size] , self.num_choices ) __a = self.get_config() __a = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, token_type_ids, mc_token_ids, sequence_labels, token_labels, choice_labels, ) def __UpperCAmelCase ( self ): return CTRLConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , *_a ): __a = CTRLModel(config=_a ) model.to(_a ) model.eval() model(_a , token_type_ids=_a , head_mask=_a ) model(_a , token_type_ids=_a ) __a = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(len(result.past_key_values ) , config.n_layer ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , *_a ): __a = CTRLLMHeadModel(_a ) model.to(_a ) model.eval() __a = model(_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = config_and_inputs __a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask} return config, inputs_dict def __UpperCAmelCase ( self , _a , _a , _a , _a , *_a ): __a = self.num_labels __a = CTRLForSequenceClassification(_a ) model.to(_a ) model.eval() __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = model(_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) @require_torch class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else () __UpperCAmelCase : Union[str, Any] = (CTRLLMHeadModel,) if is_torch_available() else () __UpperCAmelCase : Union[str, Any] = ( { 'feature-extraction': CTRLModel, 'text-classification': CTRLForSequenceClassification, 'text-generation': CTRLLMHeadModel, 'zero-shot': CTRLForSequenceClassification, } if is_torch_available() else {} ) __UpperCAmelCase : Optional[Any] = True __UpperCAmelCase : List[Any] = False __UpperCAmelCase : str = False def __UpperCAmelCase ( self , _a , _a , _a , _a , _a ): if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny # config could not be created. return True return False def __UpperCAmelCase ( self ): __a = CTRLModelTester(self ) __a = ConfigTester(self , config_class=_a , n_embd=37 ) def __UpperCAmelCase ( self ): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_ctrl_model(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*_a ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __UpperCAmelCase ( self ): pass @slow def __UpperCAmelCase ( self ): for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = CTRLModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip('''The model doesn\'t support left padding''' ) # and it's not used enough to be worth fixing :) def __UpperCAmelCase ( self ): pass @require_torch class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() @slow def __UpperCAmelCase ( self ): __a = CTRLLMHeadModel.from_pretrained('''ctrl''' ) model.to(_a ) __a = torch.tensor( [[11_859, 0, 1_611, 8]] , dtype=torch.long , device=_a ) # Legal the president is __a = [ 11_859, 0, 1_611, 8, 5, 150, 26_449, 2, 19, 348, 469, 3, 2_595, 48, 20_740, 246_533, 246_533, 19, 30, 5, ] # Legal the president is a good guy and I don't want to lose my job. \n \n I have a __a = model.generate(_a , do_sample=_a ) self.assertListEqual(output_ids[0].tolist() , _a )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : list[str] ) -> str: __a = '''''' for word_or_phrase in separated: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise Exception('''join() accepts only strings to be joined''' ) joined += word_or_phrase + separator return joined.strip(lowerCAmelCase__ ) if __name__ == "__main__": from doctest import testmod testmod()
45
1
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = { "configuration_trajectory_transformer": [ "TRAJECTORY_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TrajectoryTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TRAJECTORY_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TrajectoryTransformerModel", "TrajectoryTransformerPreTrainedModel", "load_tf_weights_in_trajectory_transformer", ] if TYPE_CHECKING: from .configuration_trajectory_transformer import ( TRAJECTORY_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TrajectoryTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_trajectory_transformer import ( TRAJECTORY_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TrajectoryTransformerModel, TrajectoryTransformerPreTrainedModel, load_tf_weights_in_trajectory_transformer, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
"""simple docstring""" lowercase_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" def lowercase ( lowerCAmelCase__ : bytes ) -> bytes: # Make sure the supplied data is a bytes-like object if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = f'''a bytes-like object is required, not \'{data.__class__.__name__}\'''' raise TypeError(lowerCAmelCase__ ) __a = ''''''.join(bin(lowerCAmelCase__ )[2:].zfill(8 ) for byte in data ) __a = len(lowerCAmelCase__ ) % 6 != 0 if padding_needed: # The padding that will be added later __a = b'''=''' * ((6 - len(lowerCAmelCase__ ) % 6) // 2) # Append binary_stream with arbitrary binary digits (0's by default) to make its # length a multiple of 6. binary_stream += "0" * (6 - len(lowerCAmelCase__ ) % 6) else: __a = b'''''' # Encode every 6 binary digits to their corresponding Base64 character return ( "".join( B64_CHARSET[int(binary_stream[index : index + 6] , 2 )] for index in range(0 , len(lowerCAmelCase__ ) , 6 ) ).encode() + padding ) def lowercase ( lowerCAmelCase__ : str ) -> bytes: # Make sure encoded_data is either a string or a bytes-like object if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = ( '''argument should be a bytes-like object or ASCII string, ''' f'''not \'{encoded_data.__class__.__name__}\'''' ) raise TypeError(lowerCAmelCase__ ) # In case encoded_data is a bytes-like object, make sure it contains only # ASCII characters so we convert it to a string object if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): try: __a = encoded_data.decode('''utf-8''' ) except UnicodeDecodeError: raise ValueError('''base64 encoded data should only contain ASCII characters''' ) __a = encoded_data.count('''=''' ) # Check if the encoded string contains non base64 characters if padding: assert all( char in B64_CHARSET for char in encoded_data[:-padding] ), "Invalid base64 character(s) found." else: assert all( char in B64_CHARSET for char in encoded_data ), "Invalid base64 character(s) found." # Check the padding assert len(lowerCAmelCase__ ) % 4 == 0 and padding < 3, "Incorrect padding" if padding: # Remove padding if there is one __a = encoded_data[:-padding] __a = ''''''.join( bin(B64_CHARSET.index(lowerCAmelCase__ ) )[2:].zfill(6 ) for char in encoded_data )[: -padding * 2] else: __a = ''''''.join( bin(B64_CHARSET.index(lowerCAmelCase__ ) )[2:].zfill(6 ) for char in encoded_data ) __a = [ int(binary_stream[index : index + 8] , 2 ) for index in range(0 , len(lowerCAmelCase__ ) , 8 ) ] return bytes(lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_realm import RealmTokenizer lowercase_ = logging.get_logger(__name__) lowercase_ = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} lowercase_ = { "vocab_file": { "google/realm-cc-news-pretrained-embedder": ( "https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/vocab.txt" ), "google/realm-cc-news-pretrained-encoder": ( "https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/vocab.txt" ), "google/realm-cc-news-pretrained-scorer": ( "https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/vocab.txt" ), "google/realm-cc-news-pretrained-openqa": ( "https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/vocab.txt" ), "google/realm-orqa-nq-openqa": "https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/vocab.txt", "google/realm-orqa-nq-reader": "https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/vocab.txt", "google/realm-orqa-wq-openqa": "https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/vocab.txt", "google/realm-orqa-wq-reader": "https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/vocab.txt", }, "tokenizer_file": { "google/realm-cc-news-pretrained-embedder": ( "https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/tokenizer.jsont" ), "google/realm-cc-news-pretrained-encoder": ( "https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/tokenizer.json" ), "google/realm-cc-news-pretrained-scorer": ( "https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/tokenizer.json" ), "google/realm-cc-news-pretrained-openqa": ( "https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/tokenizer.json" ), "google/realm-orqa-nq-openqa": ( "https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/tokenizer.json" ), "google/realm-orqa-nq-reader": ( "https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/tokenizer.json" ), "google/realm-orqa-wq-openqa": ( "https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/tokenizer.json" ), "google/realm-orqa-wq-reader": ( "https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/tokenizer.json" ), }, } lowercase_ = { "google/realm-cc-news-pretrained-embedder": 5_1_2, "google/realm-cc-news-pretrained-encoder": 5_1_2, "google/realm-cc-news-pretrained-scorer": 5_1_2, "google/realm-cc-news-pretrained-openqa": 5_1_2, "google/realm-orqa-nq-openqa": 5_1_2, "google/realm-orqa-nq-reader": 5_1_2, "google/realm-orqa-wq-openqa": 5_1_2, "google/realm-orqa-wq-reader": 5_1_2, } lowercase_ = { "google/realm-cc-news-pretrained-embedder": {"do_lower_case": True}, "google/realm-cc-news-pretrained-encoder": {"do_lower_case": True}, "google/realm-cc-news-pretrained-scorer": {"do_lower_case": True}, "google/realm-cc-news-pretrained-openqa": {"do_lower_case": True}, "google/realm-orqa-nq-openqa": {"do_lower_case": True}, "google/realm-orqa-nq-reader": {"do_lower_case": True}, "google/realm-orqa-wq-openqa": {"do_lower_case": True}, "google/realm-orqa-wq-reader": {"do_lower_case": True}, } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = VOCAB_FILES_NAMES __UpperCAmelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __UpperCAmelCase : Optional[int] = PRETRAINED_INIT_CONFIGURATION __UpperCAmelCase : int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCAmelCase : Tuple = RealmTokenizer def __init__( self , _a=None , _a=None , _a=True , _a="[UNK]" , _a="[SEP]" , _a="[PAD]" , _a="[CLS]" , _a="[MASK]" , _a=True , _a=None , **_a , ): super().__init__( _a , tokenizer_file=_a , do_lower_case=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , tokenize_chinese_chars=_a , strip_accents=_a , **_a , ) __a = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , _a ) != do_lower_case or normalizer_state.get('''strip_accents''' , _a ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , _a ) != tokenize_chinese_chars ): __a = getattr(_a , normalizer_state.pop('''type''' ) ) __a = do_lower_case __a = strip_accents __a = tokenize_chinese_chars __a = normalizer_class(**_a ) __a = do_lower_case def __UpperCAmelCase ( self , _a , **_a ): __a = PaddingStrategy.MAX_LENGTH __a = text __a = kwargs.pop('''text_pair''' , _a ) __a = kwargs.pop('''return_tensors''' , _a ) __a = { '''input_ids''': [], '''attention_mask''': [], '''token_type_ids''': [], } for idx, candidate_text in enumerate(_a ): if batch_text_pair is not None: __a = batch_text_pair[idx] else: __a = None __a = super().__call__(_a , _a , return_tensors=_a , **_a ) __a = encoded_candidates.get('''input_ids''' ) __a = encoded_candidates.get('''attention_mask''' ) __a = encoded_candidates.get('''token_type_ids''' ) if encoded_input_ids is not None: output_data["input_ids"].append(_a ) if encoded_attention_mask is not None: output_data["attention_mask"].append(_a ) if encoded_token_type_ids is not None: output_data["token_type_ids"].append(_a ) __a = {key: item for key, item in output_data.items() if len(_a ) != 0} return BatchEncoding(_a , tensor_type=_a ) def __UpperCAmelCase ( self , _a , _a=None ): __a = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __UpperCAmelCase ( self , _a , _a = None ): __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 __UpperCAmelCase ( self , _a , _a = None ): __a = self._tokenizer.model.save(_a , name=_a ) return tuple(_a )
45
"""simple docstring""" import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = inspect.getfile(accelerate.test_utils ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) __a = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_distributed_data_loop.py'''] ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_ops.py'''] ) @require_multi_gpu def __UpperCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices.''' ) __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_a , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices.''' ) __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.operation_file_path] print(f'''Command: {cmd}''' ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_a , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self ): __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_a , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices, using 2 devices only''' ) __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices='''0,1''' ): execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": lowercase_ = Accelerator() lowercase_ = (accelerator.state.process_index + 2, 1_0) lowercase_ = torch.randint(0, 1_0, shape).to(accelerator.device) lowercase_ = "" lowercase_ = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." lowercase_ = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." lowercase_ = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
45
1
"""simple docstring""" import math import os import sys def lowercase ( lowerCAmelCase__ : str ) -> str: __a = '''''' try: with open(lowerCAmelCase__ , '''rb''' ) as binary_file: __a = binary_file.read() for dat in data: __a = f'''{dat:08b}''' result += curr_byte return result except OSError: print('''File not accessible''' ) sys.exit() def lowercase ( lowerCAmelCase__ : dict[str, str] , lowerCAmelCase__ : str , lowerCAmelCase__ : int , lowerCAmelCase__ : str ) -> None: lexicon.pop(lowerCAmelCase__ ) __a = last_match_id if math.loga(lowerCAmelCase__ ).is_integer(): for curr_key in lexicon: __a = '''0''' + lexicon[curr_key] __a = bin(lowerCAmelCase__ )[2:] def lowercase ( lowerCAmelCase__ : str ) -> str: __a = {'''0''': '''0''', '''1''': '''1'''} __a , __a = '''''', '''''' __a = len(lowerCAmelCase__ ) for i in range(len(lowerCAmelCase__ ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue __a = lexicon[curr_string] result += last_match_id add_key_to_lexicon(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) index += 1 __a = '''''' while curr_string != "" and curr_string not in lexicon: curr_string += "0" if curr_string != "": __a = lexicon[curr_string] result += last_match_id return result def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : str ) -> str: __a = os.path.getsize(lowerCAmelCase__ ) __a = bin(lowerCAmelCase__ )[2:] __a = len(lowerCAmelCase__ ) return "0" * (length_length - 1) + file_length_binary + compressed def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : str ) -> None: __a = 8 try: with open(lowerCAmelCase__ , '''wb''' ) as opened_file: __a = [ to_write[i : i + byte_length] for i in range(0 , len(lowerCAmelCase__ ) , lowerCAmelCase__ ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append('''10000000''' ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array: opened_file.write(int(lowerCAmelCase__ , 2 ).to_bytes(1 , byteorder='''big''' ) ) except OSError: print('''File not accessible''' ) sys.exit() def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : str ) -> None: __a = read_file_binary(lowerCAmelCase__ ) __a = compress_data(lowerCAmelCase__ ) __a = add_file_length(lowerCAmelCase__ , lowerCAmelCase__ ) write_file_binary(lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
45
"""simple docstring""" import numpy as np def lowercase ( lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : float ) -> np.ndarray: return np.where(vector > 0 , lowerCAmelCase__ , (alpha * (np.exp(lowerCAmelCase__ ) - 1)) ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "facebook/data2vec-text-base": "https://huggingface.co/data2vec/resolve/main/config.json", } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = 'data2vec-text' def __init__( self , _a=30_522 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=2 , _a=0.02 , _a=1E-12 , _a=1 , _a=0 , _a=2 , _a="absolute" , _a=True , _a=None , **_a , ): super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) __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 __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def __UpperCAmelCase ( self ): 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), ] )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: __a = set() # Replace all the whitespace in our sentence __a = input_str.replace(''' ''' , '''''' ) for alpha in input_str: if "a" <= alpha.lower() <= "z": frequency.add(alpha.lower() ) return len(lowerCAmelCase__ ) == 26 def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: __a = [False] * 26 for char in input_str: if char.islower(): __a = True elif char.isupper(): __a = True return all(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: return len({char for char in input_str.lower() if char.isalpha()} ) == 26 def lowercase ( ) -> None: from timeit import timeit __a = '''from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest''' print(timeit('''is_pangram()''' , setup=lowerCAmelCase__ ) ) print(timeit('''is_pangram_faster()''' , setup=lowerCAmelCase__ ) ) print(timeit('''is_pangram_fastest()''' , setup=lowerCAmelCase__ ) ) # 5.348480500048026, 2.6477354579837993, 1.8470395830227062 # 5.036091582966037, 2.644472333951853, 1.8869528750656173 if __name__ == "__main__": import doctest doctest.testmod() benchmark()
45
1
"""simple docstring""" import unittest import torch from torch import nn from diffusers.models.activations import get_activation class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = get_activation('''swish''' ) self.assertIsInstance(_a , nn.SiLU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''silu''' ) self.assertIsInstance(_a , nn.SiLU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''mish''' ) self.assertIsInstance(_a , nn.Mish ) self.assertEqual(act(torch.tensor(-200 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''gelu''' ) self.assertIsInstance(_a , nn.GELU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 )
45
"""simple docstring""" import gc import unittest from transformers import CTRLConfig, is_torch_available from transformers.testing_utils import require_torch, slow, 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 ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, ) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a=14 , _a=7 , _a=True , _a=True , _a=True , _a=True , _a=True , _a=99 , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=3 , _a=4 , _a=None , ): __a = parent __a = batch_size __a = seq_length __a = is_training __a = use_token_type_ids __a = use_input_mask __a = use_labels __a = use_mc_token_ids __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = type_sequence_label_size __a = initializer_range __a = num_labels __a = num_choices __a = scope __a = self.vocab_size - 1 def __UpperCAmelCase ( self ): __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a = None if self.use_input_mask: __a = random_attention_mask([self.batch_size, self.seq_length] ) __a = None if self.use_token_type_ids: __a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __a = None if self.use_mc_token_ids: __a = ids_tensor([self.batch_size, self.num_choices] , self.seq_length ) __a = None __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __a = ids_tensor([self.batch_size] , self.num_choices ) __a = self.get_config() __a = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, token_type_ids, mc_token_ids, sequence_labels, token_labels, choice_labels, ) def __UpperCAmelCase ( self ): return CTRLConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , *_a ): __a = CTRLModel(config=_a ) model.to(_a ) model.eval() model(_a , token_type_ids=_a , head_mask=_a ) model(_a , token_type_ids=_a ) __a = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(len(result.past_key_values ) , config.n_layer ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , *_a ): __a = CTRLLMHeadModel(_a ) model.to(_a ) model.eval() __a = model(_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = config_and_inputs __a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask} return config, inputs_dict def __UpperCAmelCase ( self , _a , _a , _a , _a , *_a ): __a = self.num_labels __a = CTRLForSequenceClassification(_a ) model.to(_a ) model.eval() __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = model(_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) @require_torch class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else () __UpperCAmelCase : Union[str, Any] = (CTRLLMHeadModel,) if is_torch_available() else () __UpperCAmelCase : Union[str, Any] = ( { 'feature-extraction': CTRLModel, 'text-classification': CTRLForSequenceClassification, 'text-generation': CTRLLMHeadModel, 'zero-shot': CTRLForSequenceClassification, } if is_torch_available() else {} ) __UpperCAmelCase : Optional[Any] = True __UpperCAmelCase : List[Any] = False __UpperCAmelCase : str = False def __UpperCAmelCase ( self , _a , _a , _a , _a , _a ): if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny # config could not be created. return True return False def __UpperCAmelCase ( self ): __a = CTRLModelTester(self ) __a = ConfigTester(self , config_class=_a , n_embd=37 ) def __UpperCAmelCase ( self ): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_ctrl_model(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*_a ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __UpperCAmelCase ( self ): pass @slow def __UpperCAmelCase ( self ): for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = CTRLModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip('''The model doesn\'t support left padding''' ) # and it's not used enough to be worth fixing :) def __UpperCAmelCase ( self ): pass @require_torch class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() @slow def __UpperCAmelCase ( self ): __a = CTRLLMHeadModel.from_pretrained('''ctrl''' ) model.to(_a ) __a = torch.tensor( [[11_859, 0, 1_611, 8]] , dtype=torch.long , device=_a ) # Legal the president is __a = [ 11_859, 0, 1_611, 8, 5, 150, 26_449, 2, 19, 348, 469, 3, 2_595, 48, 20_740, 246_533, 246_533, 19, 30, 5, ] # Legal the president is a good guy and I don't want to lose my job. \n \n I have a __a = model.generate(_a , do_sample=_a ) self.assertListEqual(output_ids[0].tolist() , _a )
45
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowercase_ = { "configuration_biogpt": ["BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BioGptConfig"], "tokenization_biogpt": ["BioGptTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "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 lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase_ = {"configuration_xglm": ["XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XGLMConfig"]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["XGLMTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["XGLMTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "XGLM_PRETRAINED_MODEL_ARCHIVE_LIST", "XGLMForCausalLM", "XGLMModel", "XGLMPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "FlaxXGLMForCausalLM", "FlaxXGLMModel", "FlaxXGLMPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST", "TFXGLMForCausalLM", "TFXGLMModel", "TFXGLMPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure)
45
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "facebook/vit-mae-base": "https://huggingface.co/facebook/vit-mae-base/resolve/main/config.json", # See all ViT MAE models at https://huggingface.co/models?filter=vit-mae } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = 'vit_mae' def __init__( self , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.0 , _a=0.0 , _a=0.02 , _a=1E-12 , _a=224 , _a=16 , _a=3 , _a=True , _a=16 , _a=512 , _a=8 , _a=2_048 , _a=0.75 , _a=False , **_a , ): super().__init__(**_a ) __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 = initializer_range __a = layer_norm_eps __a = image_size __a = patch_size __a = num_channels __a = qkv_bias __a = decoder_num_attention_heads __a = decoder_hidden_size __a = decoder_num_hidden_layers __a = decoder_intermediate_size __a = mask_ratio __a = norm_pix_loss
45
"""simple docstring""" import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm lowercase_ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex lowercase_ = 1_0 lowercase_ = 2_5_6 def lowercase ( lowerCAmelCase__ : List[str] ) -> Optional[MinHash]: if len(lowerCAmelCase__ ) < MIN_NUM_TOKENS: return None __a = MinHash(num_perm=lowerCAmelCase__ ) for token in set(lowerCAmelCase__ ): min_hash.update(token.encode() ) return min_hash def lowercase ( lowerCAmelCase__ : str ) -> Set[str]: return {t for t in NON_ALPHA.split(lowerCAmelCase__ ) if len(t.strip() ) > 0} class __lowerCAmelCase : '''simple docstring''' def __init__( self , *, _a = 0.85 , ): __a = duplication_jaccard_threshold __a = NUM_PERM __a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) __a = defaultdict(_a ) def __UpperCAmelCase ( self , _a , _a ): __a = self._index.query(_a ) if code_key in self._index.keys: print(f'''Duplicate key {code_key}''' ) return self._index.insert(_a , _a ) if len(_a ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(_a ) break else: self._duplicate_clusters[close_duplicates[0]].add(_a ) def __UpperCAmelCase ( self ): __a = [] for base, duplicates in self._duplicate_clusters.items(): __a = [base] + list(_a ) # reformat the cluster to be a list of dict __a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(_a ) return duplicate_clusters def __UpperCAmelCase ( self , _a ): __a = self.get_duplicate_clusters() with open(_a , '''w''' ) as f: json.dump(_a , _a ) def lowercase ( lowerCAmelCase__ : List[str] ) -> int: __a , __a = element __a = get_min_hash([t for t in NON_ALPHA.split(data['''content'''] ) if len(t.strip() ) > 0] ) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def lowercase ( lowerCAmelCase__ : Type[Dataset] ) -> str: with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(lowerCAmelCase__ , max_queue_size=10000 ) , chunksize=100 , ): if data is not None: yield data def lowercase ( lowerCAmelCase__ : Type[Dataset] , lowerCAmelCase__ : float ) -> Dict: __a = DuplicationIndex(duplication_jaccard_threshold=lowerCAmelCase__ ) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(lowerCAmelCase__ ) ) , max_queue_size=100 ) ): di.add(lowerCAmelCase__ , lowerCAmelCase__ ) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : str ) -> float: __a = get_tokens(lowerCAmelCase__ ) __a = get_tokens(lowerCAmelCase__ ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) lowercase_ = None def lowercase ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Union[str, Any] ) -> Any: __a = [] for elementa in cluster: __a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: __a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(lowerCAmelCase__ , lowerCAmelCase__ ) >= jaccard_threshold: elementa["copies"] += 1 break else: __a = 1 extremes.append(lowerCAmelCase__ ) return extremes def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[int] ) -> Optional[int]: global _shared_dataset __a = dataset __a = [] __a = partial(_find_cluster_extremes_shared , jaccard_threshold=lowerCAmelCase__ ) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( lowerCAmelCase__ , lowerCAmelCase__ , ) , total=len(lowerCAmelCase__ ) , ): extremes_list.append(lowerCAmelCase__ ) return extremes_list def lowercase ( lowerCAmelCase__ : Type[Dataset] , lowerCAmelCase__ : float = 0.85 ) -> Tuple[Type[Dataset], List[List[Dict]]]: __a = make_duplicate_clusters(lowerCAmelCase__ , lowerCAmelCase__ ) __a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} __a = {} __a = find_extremes(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) for extremes in extremes_clusters: for element in extremes: __a = element __a = duplicate_indices - set(extreme_dict.keys() ) __a = dataset.filter(lambda lowerCAmelCase__ , lowerCAmelCase__ : idx not in remove_indices , with_indices=lowerCAmelCase__ ) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: __a = element['''base_index'''] in extreme_dict if element["is_extreme"]: __a = extreme_dict[element['''base_index''']]['''copies'''] print(f'''Original dataset size: {len(lowerCAmelCase__ )}''' ) print(f'''Number of duplicate clusters: {len(lowerCAmelCase__ )}''' ) print(f'''Files in duplicate cluster: {len(lowerCAmelCase__ )}''' ) print(f'''Unique files in duplicate cluster: {len(lowerCAmelCase__ )}''' ) print(f'''Filtered dataset size: {len(lowerCAmelCase__ )}''' ) return ds_filter, duplicate_clusters
45
1
"""simple docstring""" from maths.prime_factors import prime_factors def lowercase ( lowerCAmelCase__ : int ) -> int: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = f'''Input value of [number={number}] must be an integer''' raise TypeError(lowerCAmelCase__ ) if number < 1: raise ValueError('''Input must be a positive integer''' ) return -1 if len(prime_factors(lowerCAmelCase__ ) ) % 2 else 1 if __name__ == "__main__": import doctest doctest.testmod()
45
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase_ = { "configuration_roformer": ["ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "RoFormerConfig", "RoFormerOnnxConfig"], "tokenization_roformer": ["RoFormerTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["RoFormerTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "RoFormerForCausalLM", "RoFormerForMaskedLM", "RoFormerForMultipleChoice", "RoFormerForQuestionAnswering", "RoFormerForSequenceClassification", "RoFormerForTokenClassification", "RoFormerLayer", "RoFormerModel", "RoFormerPreTrainedModel", "load_tf_weights_in_roformer", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFRoFormerForCausalLM", "TFRoFormerForMaskedLM", "TFRoFormerForMultipleChoice", "TFRoFormerForQuestionAnswering", "TFRoFormerForSequenceClassification", "TFRoFormerForTokenClassification", "TFRoFormerLayer", "TFRoFormerModel", "TFRoFormerPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "FlaxRoFormerForMaskedLM", "FlaxRoFormerForMultipleChoice", "FlaxRoFormerForQuestionAnswering", "FlaxRoFormerForSequenceClassification", "FlaxRoFormerForTokenClassification", "FlaxRoFormerModel", "FlaxRoFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
1
"""simple docstring""" import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "asapp/sew-tiny-100k": "https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json", # See all SEW models at https://huggingface.co/models?filter=sew } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : int = 'sew' def __init__( self , _a=32 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a=2 , _a="gelu" , _a=0.1 , _a=0.1 , _a=0.1 , _a=0.0 , _a=0.1 , _a=0.1 , _a=0.02 , _a=1E-5 , _a="group" , _a="gelu" , _a=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , _a=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , _a=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , _a=False , _a=128 , _a=16 , _a=True , _a=0.05 , _a=10 , _a=2 , _a=0.0 , _a=10 , _a=0 , _a="mean" , _a=False , _a=False , _a=256 , _a=0 , _a=1 , _a=2 , **_a , ): super().__init__(**_a , pad_token_id=_a , bos_token_id=_a , eos_token_id=_a ) __a = hidden_size __a = feat_extract_norm __a = feat_extract_activation __a = list(_a ) __a = list(_a ) __a = list(_a ) __a = conv_bias __a = num_conv_pos_embeddings __a = num_conv_pos_embedding_groups __a = len(self.conv_dim ) __a = num_hidden_layers __a = intermediate_size __a = squeeze_factor __a = hidden_act __a = num_attention_heads __a = hidden_dropout __a = attention_dropout __a = activation_dropout __a = feat_proj_dropout __a = final_dropout __a = layerdrop __a = layer_norm_eps __a = initializer_range __a = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect.''' '''It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,''' f'''but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)''' f'''= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __a = apply_spec_augment __a = mask_time_prob __a = mask_time_length __a = mask_time_min_masks __a = mask_feature_prob __a = mask_feature_length __a = mask_feature_min_masks # ctc loss __a = ctc_loss_reduction __a = ctc_zero_infinity # sequence classification __a = use_weighted_layer_sum __a = classifier_proj_size @property def __UpperCAmelCase ( self ): return functools.reduce(operator.mul , self.conv_stride , 1 )
45
"""simple docstring""" import warnings from ..trainer import Trainer from ..utils import logging lowercase_ = logging.get_logger(__name__) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , _a=None , **_a ): warnings.warn( '''`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` ''' '''instead.''' , _a , ) super().__init__(args=_a , **_a )
45
1
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "facebook/levit-128S": "https://huggingface.co/facebook/levit-128S/resolve/main/config.json", # See all LeViT models at https://huggingface.co/models?filter=levit } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = 'levit' def __init__( self , _a=224 , _a=3 , _a=3 , _a=2 , _a=1 , _a=16 , _a=[128, 256, 384] , _a=[4, 8, 12] , _a=[4, 4, 4] , _a=[16, 16, 16] , _a=0 , _a=[2, 2, 2] , _a=[2, 2, 2] , _a=0.02 , **_a , ): super().__init__(**_a ) __a = image_size __a = num_channels __a = kernel_size __a = stride __a = padding __a = hidden_sizes __a = num_attention_heads __a = depths __a = key_dim __a = drop_path_rate __a = patch_size __a = attention_ratio __a = mlp_ratio __a = initializer_range __a = [ ['''Subsample''', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2], ['''Subsample''', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2], ] class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[Any] = version.parse('1.11' ) @property def __UpperCAmelCase ( self ): return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __UpperCAmelCase ( self ): return 1E-4
45
"""simple docstring""" import math def lowercase ( lowerCAmelCase__ : list , lowerCAmelCase__ : int ) -> int: __a = len(lowerCAmelCase__ ) __a = int(math.floor(math.sqrt(lowerCAmelCase__ ) ) ) __a = 0 while arr[min(lowerCAmelCase__ , lowerCAmelCase__ ) - 1] < x: __a = step step += int(math.floor(math.sqrt(lowerCAmelCase__ ) ) ) if prev >= n: return -1 while arr[prev] < x: __a = prev + 1 if prev == min(lowerCAmelCase__ , lowerCAmelCase__ ): return -1 if arr[prev] == x: return prev return -1 if __name__ == "__main__": lowercase_ = input("Enter numbers separated by a comma:\n").strip() lowercase_ = [int(item) for item in user_input.split(",")] lowercase_ = int(input("Enter the number to be searched:\n")) lowercase_ = jump_search(arr, x) if res == -1: print("Number not found!") else: print(F'''Number {x} is at index {res}''')
45
1
"""simple docstring""" from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef import datasets lowercase_ = "\\n@inproceedings{wang2019glue,\n title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding},\n author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.},\n note={In the Proceedings of ICLR.},\n year={2019}\n}\n" lowercase_ = "\\nGLUE, the General Language Understanding Evaluation benchmark\n(https://gluebenchmark.com/) is a collection of resources for training,\nevaluating, and analyzing natural language understanding systems.\n" lowercase_ = "\nCompute GLUE evaluation metric associated to each GLUE dataset.\nArgs:\n predictions: list of predictions to score.\n Each translation should be tokenized into a list of tokens.\n references: list of lists of references for each translation.\n Each reference should be tokenized into a list of tokens.\nReturns: depending on the GLUE subset, one or several of:\n \"accuracy\": Accuracy\n \"f1\": F1 score\n \"pearson\": Pearson Correlation\n \"spearmanr\": Spearman Correlation\n \"matthews_correlation\": Matthew Correlation\nExamples:\n\n >>> glue_metric = datasets.load_metric('glue', 'sst2') # 'sst2' or any of [\"mnli\", \"mnli_mismatched\", \"mnli_matched\", \"qnli\", \"rte\", \"wnli\", \"hans\"]\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'accuracy': 1.0}\n\n >>> glue_metric = datasets.load_metric('glue', 'mrpc') # 'mrpc' or 'qqp'\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'accuracy': 1.0, 'f1': 1.0}\n\n >>> glue_metric = datasets.load_metric('glue', 'stsb')\n >>> references = [0., 1., 2., 3., 4., 5.]\n >>> predictions = [0., 1., 2., 3., 4., 5.]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print({\"pearson\": round(results[\"pearson\"], 2), \"spearmanr\": round(results[\"spearmanr\"], 2)})\n {'pearson': 1.0, 'spearmanr': 1.0}\n\n >>> glue_metric = datasets.load_metric('glue', 'cola')\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'matthews_correlation': 1.0}\n" def lowercase ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : str ) -> List[Any]: return float((preds == labels).mean() ) def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : Dict ) -> int: __a = simple_accuracy(lowerCAmelCase__ , lowerCAmelCase__ ) __a = float(fa_score(y_true=lowerCAmelCase__ , y_pred=lowerCAmelCase__ ) ) return { "accuracy": acc, "f1": fa, } def lowercase ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : str ) -> List[Any]: __a = float(pearsonr(lowerCAmelCase__ , lowerCAmelCase__ )[0] ) __a = float(spearmanr(lowerCAmelCase__ , lowerCAmelCase__ )[0] ) return { "pearson": pearson_corr, "spearmanr": spearman_corr, } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __lowerCAmelCase ( datasets.Metric ): '''simple docstring''' def __UpperCAmelCase ( self ): if self.config_name not in [ "sst2", "mnli", "mnli_mismatched", "mnli_matched", "cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans", ]: raise KeyError( '''You should supply a configuration name selected in ''' '''["sst2", "mnli", "mnli_mismatched", "mnli_matched", ''' '''"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]''' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''int64''' if self.config_name != '''stsb''' else '''float32''' ), '''references''': datasets.Value('''int64''' if self.config_name != '''stsb''' else '''float32''' ), } ) , codebase_urls=[] , reference_urls=[] , format='''numpy''' , ) def __UpperCAmelCase ( self , _a , _a ): if self.config_name == "cola": return {"matthews_correlation": matthews_corrcoef(_a , _a )} elif self.config_name == "stsb": return pearson_and_spearman(_a , _a ) elif self.config_name in ["mrpc", "qqp"]: return acc_and_fa(_a , _a ) elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]: return {"accuracy": simple_accuracy(_a , _a )} else: raise KeyError( '''You should supply a configuration name selected in ''' '''["sst2", "mnli", "mnli_mismatched", "mnli_matched", ''' '''"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]''' )
45
"""simple docstring""" lowercase_ = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] lowercase_ = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] lowercase_ = { 0: "Sunday", 1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", 5: "Friday", 6: "Saturday", } def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int ) -> str: assert len(str(lowerCAmelCase__ ) ) > 2, "year should be in YYYY format" assert 1 <= month <= 12, "month should be between 1 to 12" assert 1 <= day <= 31, "day should be between 1 to 31" # Doomsday algorithm: __a = year // 100 __a = (5 * (century % 4) + 2) % 7 __a = year % 100 __a = centurian % 12 __a = ( (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor ) % 7 __a = ( DOOMSDAY_NOT_LEAP[month - 1] if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) else DOOMSDAY_LEAP[month - 1] ) __a = (dooms_day + day - day_anchor) % 7 return WEEK_DAY_NAMES[week_day] if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def lowercase ( lowerCAmelCase__ : str ) -> int: __a = [ '''decoder.version''', '''decoder.output_projection.weight''', '''_float_tensor''', '''decoder.embed_positions._float_tensor''', ] for k in ignore_keys: state_dict.pop(lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Optional[int] ) -> str: __a , __a = emb.weight.shape __a = nn.Linear(lowerCAmelCase__ , lowerCAmelCase__ , bias=lowerCAmelCase__ ) __a = emb.weight.data return lin_layer def lowercase ( lowerCAmelCase__ : Optional[int] ) -> Tuple: __a = torch.load(lowerCAmelCase__ , map_location='''cpu''' ) __a = Namespace(**checkpoint['''cfg''']['''model'''] ) __a = checkpoint['''model'''] remove_ignore_keys_(lowerCAmelCase__ ) __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=lowerCAmelCase__ , 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(lowerCAmelCase__ ) __a = model.load_state_dict(lowerCAmelCase__ , strict=lowerCAmelCase__ ) print(lowerCAmelCase__ ) __a = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": lowercase_ = 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.") lowercase_ = parser.parse_args() lowercase_ = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : list ) -> bool: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise ValueError('''Input series is not valid, valid series - [2, 4, 6]''' ) if len(lowerCAmelCase__ ) == 0: raise ValueError('''Input list must be a non empty list''' ) if len(lowerCAmelCase__ ) == 1: return True __a = series[1] - series[0] for index in range(len(lowerCAmelCase__ ) - 1 ): if series[index + 1] - series[index] != common_diff: return False return True def lowercase ( lowerCAmelCase__ : list ) -> float: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise ValueError('''Input series is not valid, valid series - [2, 4, 6]''' ) if len(lowerCAmelCase__ ) == 0: raise ValueError('''Input list must be a non empty list''' ) __a = 0 for val in series: answer += val return answer / len(lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) lowercase_ = { "configuration_layoutlmv2": ["LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP", "LayoutLMv2Config"], "processing_layoutlmv2": ["LayoutLMv2Processor"], "tokenization_layoutlmv2": ["LayoutLMv2Tokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["LayoutLMv2TokenizerFast"] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["LayoutLMv2FeatureExtractor"] lowercase_ = ["LayoutLMv2ImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST", "LayoutLMv2ForQuestionAnswering", "LayoutLMv2ForSequenceClassification", "LayoutLMv2ForTokenClassification", "LayoutLMv2Layer", "LayoutLMv2Model", "LayoutLMv2PreTrainedModel", ] if TYPE_CHECKING: from .configuration_layoutlmva import LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig from .processing_layoutlmva import LayoutLMvaProcessor from .tokenization_layoutlmva import LayoutLMvaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor, LayoutLMvaImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_layoutlmva import ( LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaLayer, LayoutLMvaModel, LayoutLMvaPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
"""simple docstring""" from argparse import ArgumentParser, Namespace from ..utils import logging from . import BaseTransformersCLICommand def lowercase ( lowerCAmelCase__ : Namespace ) -> Tuple: return ConvertCommand( args.model_type , args.tf_checkpoint , args.pytorch_dump_output , args.config , args.finetuning_task_name ) lowercase_ = "\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n" class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' @staticmethod def __UpperCAmelCase ( _a ): __a = parser.add_parser( '''convert''' , help='''CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.''' , ) train_parser.add_argument('''--model_type''' , type=_a , required=_a , help='''Model\'s type.''' ) train_parser.add_argument( '''--tf_checkpoint''' , type=_a , required=_a , help='''TensorFlow checkpoint path or folder.''' ) train_parser.add_argument( '''--pytorch_dump_output''' , type=_a , required=_a , help='''Path to the PyTorch saved model output.''' ) train_parser.add_argument('''--config''' , type=_a , default='''''' , help='''Configuration file path or folder.''' ) train_parser.add_argument( '''--finetuning_task_name''' , type=_a , default=_a , help='''Optional fine-tuning task name if the TF model was a finetuned model.''' , ) train_parser.set_defaults(func=_a ) def __init__( self , _a , _a , _a , _a , _a , *_a , ): __a = logging.get_logger('''transformers-cli/converting''' ) self._logger.info(f'''Loading model {model_type}''' ) __a = model_type __a = tf_checkpoint __a = pytorch_dump_output __a = config __a = finetuning_task_name def __UpperCAmelCase ( self ): if self._model_type == "albert": try: from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "bert": try: from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "funnel": try: from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "t5": try: from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "gpt": from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import ( convert_openai_checkpoint_to_pytorch, ) convert_openai_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "transfo_xl": try: from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import ( convert_transfo_xl_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) if "ckpt" in self._tf_checkpoint.lower(): __a = self._tf_checkpoint __a = '''''' else: __a = self._tf_checkpoint __a = '''''' convert_transfo_xl_checkpoint_to_pytorch( _a , self._config , self._pytorch_dump_output , _a ) elif self._model_type == "gpt2": try: from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import ( convert_gpta_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "xlnet": try: from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import ( convert_xlnet_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_xlnet_checkpoint_to_pytorch( self._tf_checkpoint , self._config , self._pytorch_dump_output , self._finetuning_task_name ) elif self._model_type == "xlm": from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import ( convert_xlm_checkpoint_to_pytorch, ) convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "lxmert": from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import ( convert_lxmert_checkpoint_to_pytorch, ) convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "rembert": from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import ( convert_rembert_tf_checkpoint_to_pytorch, ) convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) else: raise ValueError( '''--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]''' )
45
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int ) -> bool: return number & 1 == 0 if __name__ == "__main__": import doctest doctest.testmod()
45
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "microsoft/beit-base-patch16-224-pt22k": ( "https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json" ), # See all BEiT models at https://huggingface.co/models?filter=beit } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = 'beit' def __init__( self , _a=8_192 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.0 , _a=0.0 , _a=0.02 , _a=1E-12 , _a=224 , _a=16 , _a=3 , _a=False , _a=False , _a=False , _a=False , _a=0.1 , _a=0.1 , _a=True , _a=[3, 5, 7, 11] , _a=[1, 2, 3, 6] , _a=True , _a=0.4 , _a=256 , _a=1 , _a=False , _a=255 , **_a , ): super().__init__(**_a ) __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 = initializer_range __a = layer_norm_eps __a = image_size __a = patch_size __a = num_channels __a = use_mask_token __a = use_absolute_position_embeddings __a = use_relative_position_bias __a = use_shared_relative_position_bias __a = layer_scale_init_value __a = drop_path_rate __a = use_mean_pooling # decode head attributes (semantic segmentation) __a = out_indices __a = pool_scales # auxiliary head attributes (semantic segmentation) __a = use_auxiliary_head __a = auxiliary_loss_weight __a = auxiliary_channels __a = auxiliary_num_convs __a = auxiliary_concat_input __a = semantic_loss_ignore_index class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[Any] = version.parse('1.11' ) @property def __UpperCAmelCase ( self ): return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __UpperCAmelCase ( self ): return 1E-4
45
1
"""simple docstring""" from typing import List, Optional, Union import torch from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) lowercase_ = logging.get_logger(__name__) # pylint: disable=invalid-name lowercase_ = "\n Examples:\n ```py\n >>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline\n >>> import torch\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\"kandinsky-community/kandinsky-2-2-prior\")\n >>> pipe_prior.to(\"cuda\")\n >>> prompt = \"red cat, 4k photo\"\n >>> out = pipe_prior(prompt)\n >>> image_emb = out.image_embeds\n >>> zero_image_emb = out.negative_image_embeds\n >>> pipe = KandinskyV22Pipeline.from_pretrained(\"kandinsky-community/kandinsky-2-2-decoder\")\n >>> pipe.to(\"cuda\")\n >>> image = pipe(\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... height=768,\n ... width=768,\n ... num_inference_steps=50,\n ... ).images\n >>> image[0].save(\"cat.png\")\n ```\n" def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[str]=8 ) -> str: __a = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 __a = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , _a , _a , _a , ): super().__init__() self.register_modules( unet=_a , scheduler=_a , movq=_a , ) __a = 2 ** (len(self.movq.config.block_out_channels ) - 1) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , _a ): if latents is None: __a = randn_tensor(_a , generator=_a , device=_a , dtype=_a ) else: if latents.shape != shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {shape}''' ) __a = latents.to(_a ) __a = latents * scheduler.init_noise_sigma return latents def __UpperCAmelCase ( self , _a=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) __a = torch.device(f'''cuda:{gpu_id}''' ) __a = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_a , _a ) def __UpperCAmelCase ( self , _a=0 ): 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.''' ) __a = torch.device(f'''cuda:{gpu_id}''' ) if self.device.type != "cpu": self.to('''cpu''' , silence_dtype_warnings=_a ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) __a = None for cpu_offloaded_model in [self.unet, self.movq]: __a , __a = cpu_offload_with_hook(_a , _a , prev_module_hook=_a ) # We'll offload the last model manually. __a = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def __UpperCAmelCase ( self ): if not hasattr(self.unet , '''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(_a , '''_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(_a ) def __call__( self , _a , _a , _a = 512 , _a = 512 , _a = 100 , _a = 4.0 , _a = 1 , _a = None , _a = None , _a = "pil" , _a = True , ): __a = self._execution_device __a = guidance_scale > 1.0 if isinstance(_a , _a ): __a = torch.cat(_a , dim=0 ) __a = image_embeds.shape[0] * num_images_per_prompt if isinstance(_a , _a ): __a = torch.cat(_a , dim=0 ) if do_classifier_free_guidance: __a = image_embeds.repeat_interleave(_a , dim=0 ) __a = negative_image_embeds.repeat_interleave(_a , dim=0 ) __a = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=_a ) self.scheduler.set_timesteps(_a , device=_a ) __a = self.scheduler.timesteps __a = self.unet.config.in_channels __a , __a = downscale_height_and_width(_a , _a , self.movq_scale_factor ) # create initial latent __a = self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , _a , _a , _a , self.scheduler , ) for i, t in enumerate(self.progress_bar(_a ) ): # expand the latents if we are doing classifier free guidance __a = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents __a = {'''image_embeds''': image_embeds} __a = self.unet( sample=_a , timestep=_a , encoder_hidden_states=_a , added_cond_kwargs=_a , return_dict=_a , )[0] if do_classifier_free_guidance: __a , __a = noise_pred.split(latents.shape[1] , dim=1 ) __a , __a = noise_pred.chunk(2 ) __a , __a = variance_pred.chunk(2 ) __a = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) __a = 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"] ): __a , __a = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 __a = self.scheduler.step( _a , _a , _a , generator=_a , )[0] # post-processing __a = self.movq.decode(_a , force_not_quantize=_a )['''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"]: __a = image * 0.5 + 0.5 __a = image.clamp(0 , 1 ) __a = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": __a = self.numpy_to_pil(_a ) if not return_dict: return (image,) return ImagePipelineOutput(images=_a )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str ) -> list: if n_term == "": return [] __a = [] for temp in range(int(lowerCAmelCase__ ) ): series.append(f'''1/{temp + 1}''' if series else '''1''' ) return series if __name__ == "__main__": lowercase_ = input("Enter the last number (nth term) of the Harmonic Series") print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n") print(harmonic_series(nth_term))
45
1
"""simple docstring""" from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig lowercase_ = logging.get_logger(__name__) # General docstring lowercase_ = "MobileNetV1Config" # Base docstring lowercase_ = "google/mobilenet_v1_1.0_224" lowercase_ = [1, 1_0_2_4, 7, 7] # Image classification docstring lowercase_ = "google/mobilenet_v1_1.0_224" lowercase_ = "tabby, tabby cat" lowercase_ = [ "google/mobilenet_v1_1.0_224", "google/mobilenet_v1_0.75_192", # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def lowercase ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[int]=None ) -> int: __a = {} if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = model.mobilenet_va else: __a = model __a = '''MobilenetV1/Conv2d_0/''' __a = backbone.conv_stem.convolution.weight __a = backbone.conv_stem.normalization.bias __a = backbone.conv_stem.normalization.weight __a = backbone.conv_stem.normalization.running_mean __a = backbone.conv_stem.normalization.running_var for i in range(13 ): __a = i + 1 __a = i * 2 __a = backbone.layer[pt_index] __a = f'''MobilenetV1/Conv2d_{tf_index}_depthwise/''' __a = pointer.convolution.weight __a = pointer.normalization.bias __a = pointer.normalization.weight __a = pointer.normalization.running_mean __a = pointer.normalization.running_var __a = backbone.layer[pt_index + 1] __a = f'''MobilenetV1/Conv2d_{tf_index}_pointwise/''' __a = pointer.convolution.weight __a = pointer.normalization.bias __a = pointer.normalization.weight __a = pointer.normalization.running_mean __a = pointer.normalization.running_var if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = '''MobilenetV1/Logits/Conv2d_1c_1x1/''' __a = model.classifier.weight __a = model.classifier.bias return tf_to_pt_map def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : int ) -> Tuple: try: import numpy as np import tensorflow as tf except ImportError: logger.error( '''Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see ''' '''https://www.tensorflow.org/install/ for installation instructions.''' ) raise # Load weights from TF model __a = tf.train.list_variables(lowerCAmelCase__ ) __a = {} for name, shape in init_vars: logger.info(f'''Loading TF weight {name} with shape {shape}''' ) __a = tf.train.load_variable(lowerCAmelCase__ , lowerCAmelCase__ ) __a = array # Build TF to PyTorch weights loading map __a = _build_tf_to_pytorch_map(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) for name, pointer in tf_to_pt_map.items(): logger.info(f'''Importing {name}''' ) if name not in tf_weights: logger.info(f'''{name} not in tf pre-trained weights, skipping''' ) continue __a = tf_weights[name] if "depthwise_weights" in name: logger.info('''Transposing depthwise''' ) __a = np.transpose(lowerCAmelCase__ , (2, 3, 0, 1) ) elif "weights" in name: logger.info('''Transposing''' ) if len(pointer.shape ) == 2: # copying into linear layer __a = array.squeeze().transpose() else: __a = np.transpose(lowerCAmelCase__ , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(f'''Pointer shape {pointer.shape} and array shape {array.shape} mismatched''' ) logger.info(f'''Initialize PyTorch weight {name} {array.shape}''' ) __a = torch.from_numpy(lowerCAmelCase__ ) tf_weights.pop(lowerCAmelCase__ , lowerCAmelCase__ ) tf_weights.pop(name + '''/RMSProp''' , lowerCAmelCase__ ) tf_weights.pop(name + '''/RMSProp_1''' , lowerCAmelCase__ ) tf_weights.pop(name + '''/ExponentialMovingAverage''' , lowerCAmelCase__ ) logger.info(f'''Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}''' ) return model def lowercase ( lowerCAmelCase__ : torch.Tensor , lowerCAmelCase__ : nn.Convad ) -> torch.Tensor: __a , __a = features.shape[-2:] __a , __a = conv_layer.stride __a , __a = conv_layer.kernel_size if in_height % stride_height == 0: __a = max(kernel_height - stride_height , 0 ) else: __a = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: __a = max(kernel_width - stride_width , 0 ) else: __a = max(kernel_width - (in_width % stride_width) , 0 ) __a = pad_along_width // 2 __a = pad_along_width - pad_left __a = pad_along_height // 2 __a = pad_along_height - pad_top __a = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(lowerCAmelCase__ , lowerCAmelCase__ , '''constant''' , 0.0 ) class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a , _a , _a = 1 , _a = 1 , _a = False , _a = True , _a = True , ): super().__init__() __a = config if in_channels % groups != 0: raise ValueError(f'''Input channels ({in_channels}) are not divisible by {groups} groups.''' ) if out_channels % groups != 0: raise ValueError(f'''Output channels ({out_channels}) are not divisible by {groups} groups.''' ) __a = 0 if config.tf_padding else int((kernel_size - 1) / 2 ) __a = nn.Convad( in_channels=_a , out_channels=_a , kernel_size=_a , stride=_a , padding=_a , groups=_a , bias=_a , padding_mode='''zeros''' , ) if use_normalization: __a = nn.BatchNormad( num_features=_a , eps=config.layer_norm_eps , momentum=0.9997 , affine=_a , track_running_stats=_a , ) else: __a = None if use_activation: if isinstance(_a , _a ): __a = ACTaFN[use_activation] elif isinstance(config.hidden_act , _a ): __a = ACTaFN[config.hidden_act] else: __a = config.hidden_act else: __a = None def __UpperCAmelCase ( self , _a ): if self.config.tf_padding: __a = apply_tf_padding(_a , self.convolution ) __a = self.convolution(_a ) if self.normalization is not None: __a = self.normalization(_a ) if self.activation is not None: __a = self.activation(_a ) return features class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : str = MobileNetVaConfig __UpperCAmelCase : Optional[int] = load_tf_weights_in_mobilenet_va __UpperCAmelCase : Optional[Any] = 'mobilenet_v1' __UpperCAmelCase : Tuple = 'pixel_values' __UpperCAmelCase : int = False def __UpperCAmelCase ( self , _a ): if isinstance(_a , (nn.Linear, nn.Convad) ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() elif isinstance(_a , nn.BatchNormad ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) lowercase_ = r"\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n\n Parameters:\n config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n" lowercase_ = r"\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`MobileNetV1ImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n" @add_start_docstrings( 'The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.' , __SCREAMING_SNAKE_CASE , ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , _a , _a = True ): super().__init__(_a ) __a = config __a = 32 __a = max(int(depth * config.depth_multiplier ) , config.min_depth ) __a = MobileNetVaConvLayer( _a , in_channels=config.num_channels , out_channels=_a , kernel_size=3 , stride=2 , ) __a = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] __a = nn.ModuleList() for i in range(13 ): __a = out_channels if strides[i] == 2 or i == 0: depth *= 2 __a = max(int(depth * config.depth_multiplier ) , config.min_depth ) self.layer.append( MobileNetVaConvLayer( _a , in_channels=_a , out_channels=_a , kernel_size=3 , stride=strides[i] , groups=_a , ) ) self.layer.append( MobileNetVaConvLayer( _a , in_channels=_a , out_channels=_a , kernel_size=1 , ) ) __a = nn.AdaptiveAvgPoolad((1, 1) ) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def __UpperCAmelCase ( self , _a ): raise NotImplementedError @add_start_docstrings_to_model_forward(_a ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=_a , config_class=_CONFIG_FOR_DOC , modality='''vision''' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def __UpperCAmelCase ( self , _a = None , _a = None , _a = None , ): __a = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __a = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError('''You have to specify pixel_values''' ) __a = self.conv_stem(_a ) __a = () if output_hidden_states else None for i, layer_module in enumerate(self.layer ): __a = layer_module(_a ) if output_hidden_states: __a = all_hidden_states + (hidden_states,) __a = hidden_states if self.pooler is not None: __a = torch.flatten(self.pooler(_a ) , start_dim=1 ) else: __a = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None ) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=_a , pooler_output=_a , hidden_states=_a , ) @add_start_docstrings( '\n MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n ' , __SCREAMING_SNAKE_CASE , ) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , _a ): super().__init__(_a ) __a = config.num_labels __a = MobileNetVaModel(_a ) __a = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head __a = nn.Dropout(config.classifier_dropout_prob , inplace=_a ) __a = nn.Linear(_a , config.num_labels ) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(_a ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=_a , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def __UpperCAmelCase ( self , _a = None , _a = None , _a = None , _a = None , ): __a = return_dict if return_dict is not None else self.config.use_return_dict __a = self.mobilenet_va(_a , output_hidden_states=_a , return_dict=_a ) __a = outputs.pooler_output if return_dict else outputs[1] __a = self.classifier(self.dropout(_a ) ) __a = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: __a = '''regression''' elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): __a = '''single_label_classification''' else: __a = '''multi_label_classification''' if self.config.problem_type == "regression": __a = MSELoss() if self.num_labels == 1: __a = loss_fct(logits.squeeze() , labels.squeeze() ) else: __a = loss_fct(_a , _a ) elif self.config.problem_type == "single_label_classification": __a = CrossEntropyLoss() __a = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": __a = BCEWithLogitsLoss() __a = loss_fct(_a , _a ) if not return_dict: __a = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=_a , logits=_a , hidden_states=outputs.hidden_states , )
45
"""simple docstring""" from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : List[str] __UpperCAmelCase : Optional[str] = None # Automatically constructed __UpperCAmelCase : ClassVar[str] = "dict" __UpperCAmelCase : ClassVar[Any] = None __UpperCAmelCase : str = field(default='Translation' , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __call__( self ): return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def __UpperCAmelCase ( self ): from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : Optional[List] = None __UpperCAmelCase : Optional[int] = None __UpperCAmelCase : Optional[str] = None # Automatically constructed __UpperCAmelCase : ClassVar[str] = "dict" __UpperCAmelCase : ClassVar[Any] = None __UpperCAmelCase : str = field(default='TranslationVariableLanguages' , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __UpperCAmelCase ( self ): __a = sorted(set(self.languages ) ) if self.languages else None __a = len(self.languages ) if self.languages else None def __call__( self ): return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def __UpperCAmelCase ( self , _a ): __a = set(self.languages ) if self.languages and set(_a ) - lang_set: raise ValueError( f'''Some languages in example ({', '.join(sorted(set(_a ) - lang_set ) )}) are not in valid set ({', '.join(_a )}).''' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. __a = [] for lang, text in translation_dict.items(): if isinstance(_a , _a ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. __a , __a = zip(*sorted(_a ) ) return {"language": languages, "translation": translations} def __UpperCAmelCase ( self ): from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
45
1
"""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 lowercase_ = "scheduler_config.json" class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : str = 1 __UpperCAmelCase : Dict = 2 __UpperCAmelCase : int = 3 __UpperCAmelCase : Tuple = 4 __UpperCAmelCase : Union[str, Any] = 5 @dataclass class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : jnp.ndarray class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : int = SCHEDULER_CONFIG_NAME __UpperCAmelCase : str = ['dtype'] __UpperCAmelCase : List[str] = [] __UpperCAmelCase : Any = True @classmethod def __UpperCAmelCase ( cls , _a = None , _a = None , _a=False , **_a , ): __a , __a = cls.load_config( pretrained_model_name_or_path=_a , subfolder=_a , return_unused_kwargs=_a , **_a , ) __a , __a = cls.from_config(_a , return_unused_kwargs=_a , **_a ) if hasattr(_a , '''create_state''' ) and getattr(_a , '''has_state''' , _a ): __a = scheduler.create_state() if return_unused_kwargs: return scheduler, state, unused_kwargs return scheduler, state def __UpperCAmelCase ( self , _a , _a = False , **_a ): self.save_config(save_directory=_a , push_to_hub=_a , **_a ) @property def __UpperCAmelCase ( self ): return self._get_compatibles() @classmethod def __UpperCAmelCase ( cls ): __a = list(set([cls.__name__] + cls._compatibles ) ) __a = importlib.import_module(__name__.split('''.''' )[0] ) __a = [ getattr(_a , _a ) for c in compatible_classes_str if hasattr(_a , _a ) ] return compatible_classes def lowercase ( lowerCAmelCase__ : jnp.ndarray , lowerCAmelCase__ : Tuple[int] ) -> jnp.ndarray: assert len(lowerCAmelCase__ ) >= x.ndim return jnp.broadcast_to(x.reshape(x.shape + (1,) * (len(lowerCAmelCase__ ) - x.ndim) ) , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : str=0.9_99 , lowerCAmelCase__ : List[str]=jnp.floataa ) -> jnp.ndarray: def alpha_bar(lowerCAmelCase__ : str ): return math.cos((time_step + 0.0_08) / 1.0_08 * math.pi / 2 ) ** 2 __a = [] for i in range(lowerCAmelCase__ ): __a = i / num_diffusion_timesteps __a = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar(lowerCAmelCase__ ) / alpha_bar(lowerCAmelCase__ ) , lowerCAmelCase__ ) ) return jnp.array(lowerCAmelCase__ , dtype=lowerCAmelCase__ ) @flax.struct.dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : jnp.ndarray __UpperCAmelCase : jnp.ndarray __UpperCAmelCase : jnp.ndarray @classmethod def __UpperCAmelCase ( cls , _a ): __a = scheduler.config if config.trained_betas is not None: __a = jnp.asarray(config.trained_betas , dtype=scheduler.dtype ) elif config.beta_schedule == "linear": __a = 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. __a = ( 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 __a = 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__}''' ) __a = 1.0 - betas __a = jnp.cumprod(_a , axis=0 ) return cls( alphas=_a , betas=_a , alphas_cumprod=_a , ) def lowercase ( lowerCAmelCase__ : CommonSchedulerState , lowerCAmelCase__ : jnp.ndarray , lowerCAmelCase__ : jnp.ndarray , lowerCAmelCase__ : jnp.ndarray ) -> Optional[int]: __a = state.alphas_cumprod __a = alphas_cumprod[timesteps] ** 0.5 __a = sqrt_alpha_prod.flatten() __a = broadcast_to_shape_from_left(lowerCAmelCase__ , original_samples.shape ) __a = (1 - alphas_cumprod[timesteps]) ** 0.5 __a = sqrt_one_minus_alpha_prod.flatten() __a = broadcast_to_shape_from_left(lowerCAmelCase__ , original_samples.shape ) return sqrt_alpha_prod, sqrt_one_minus_alpha_prod def lowercase ( lowerCAmelCase__ : CommonSchedulerState , lowerCAmelCase__ : jnp.ndarray , lowerCAmelCase__ : jnp.ndarray , lowerCAmelCase__ : jnp.ndarray ) -> Dict: __a , __a = get_sqrt_alpha_prod(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) __a = sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise return noisy_samples def lowercase ( lowerCAmelCase__ : CommonSchedulerState , lowerCAmelCase__ : jnp.ndarray , lowerCAmelCase__ : jnp.ndarray , lowerCAmelCase__ : jnp.ndarray ) -> List[Any]: __a , __a = get_sqrt_alpha_prod(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) __a = sqrt_alpha_prod * noise - sqrt_one_minus_alpha_prod * sample return velocity
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> List[Any]: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) if n == 0: return 0 __a = float('''-inf''' ) for i in range(1 , n + 1 ): __a = max( lowerCAmelCase__ , prices[i - 1] + naive_cut_rod_recursive(n - i , lowerCAmelCase__ ) ) return max_revue def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> List[str]: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) __a = [float('''-inf''' ) for _ in range(n + 1 )] return _top_down_cut_rod_recursive(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list , lowerCAmelCase__ : list ) -> Union[str, Any]: if max_rev[n] >= 0: return max_rev[n] elif n == 0: return 0 else: __a = float('''-inf''' ) for i in range(1 , n + 1 ): __a = max( lowerCAmelCase__ , prices[i - 1] + _top_down_cut_rod_recursive(n - i , lowerCAmelCase__ , lowerCAmelCase__ ) , ) __a = max_revenue return max_rev[n] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> Dict: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of # length 0. __a = [float('''-inf''' ) for _ in range(n + 1 )] __a = 0 for i in range(1 , n + 1 ): __a = max_rev[i] for j in range(1 , i + 1 ): __a = max(lowerCAmelCase__ , prices[j - 1] + max_rev[i - j] ) __a = max_revenue_i return max_rev[n] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> str: if n < 0: __a = f'''n must be greater than or equal to 0. Got n = {n}''' raise ValueError(lowerCAmelCase__ ) if n > len(lowerCAmelCase__ ): __a = ( '''Each integral piece of rod must have a corresponding price. ''' f'''Got n = {n} but length of prices = {len(lowerCAmelCase__ )}''' ) raise ValueError(lowerCAmelCase__ ) def lowercase ( ) -> int: __a = [6, 10, 12, 15, 20, 23] __a = len(lowerCAmelCase__ ) # the best revenue comes from cutting the rod into 6 pieces, each # of length 1 resulting in a revenue of 6 * 6 = 36. __a = 36 __a = top_down_cut_rod(lowerCAmelCase__ , lowerCAmelCase__ ) __a = bottom_up_cut_rod(lowerCAmelCase__ , lowerCAmelCase__ ) __a = naive_cut_rod_recursive(lowerCAmelCase__ , lowerCAmelCase__ ) assert expected_max_revenue == max_rev_top_down assert max_rev_top_down == max_rev_bottom_up assert max_rev_bottom_up == max_rev_naive if __name__ == "__main__": main()
45
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowercase_ = { "configuration_m2m_100": ["M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP", "M2M100Config", "M2M100OnnxConfig"], "tokenization_m2m_100": ["M2M100Tokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST", "M2M100ForConditionalGeneration", "M2M100Model", "M2M100PreTrainedModel", ] if TYPE_CHECKING: from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig from .tokenization_mam_aaa import MaMaaaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mam_aaa import ( M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST, MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) lowercase_ = {"configuration_unispeech": ["UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP", "UniSpeechConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST", "UniSpeechForCTC", "UniSpeechForPreTraining", "UniSpeechForSequenceClassification", "UniSpeechModel", "UniSpeechPreTrainedModel", ] if TYPE_CHECKING: from .configuration_unispeech import UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_unispeech import ( UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST, UniSpeechForCTC, UniSpeechForPreTraining, UniSpeechForSequenceClassification, UniSpeechModel, UniSpeechPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
1
"""simple docstring""" import os import pickle import unittest from transformers import AutoTokenizer from transformers.models.bert.tokenization_bert import BertTokenizer from transformers.models.bert_japanese.tokenization_bert_japanese import ( VOCAB_FILES_NAMES, BertJapaneseTokenizer, CharacterTokenizer, JumanppTokenizer, MecabTokenizer, SudachiTokenizer, WordpieceTokenizer, ) from transformers.testing_utils import custom_tokenizers, require_jumanpp, require_sudachi from ...test_tokenization_common import TokenizerTesterMixin @custom_tokenizers class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = BertJapaneseTokenizer __UpperCAmelCase : Optional[int] = False __UpperCAmelCase : Any = True def __UpperCAmelCase ( self ): super().setUp() __a = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''こんにちは''', '''こん''', '''にちは''', '''ばんは''', '''##こん''', '''##にちは''', '''##ばんは''', '''世界''', '''##世界''', '''、''', '''##、''', '''。''', '''##。''', ] __a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) def __UpperCAmelCase ( self , _a ): __a = '''こんにちは、世界。 \nこんばんは、世界。''' __a = '''こんにちは 、 世界 。 こんばんは 、 世界 。''' return input_text, output_text def __UpperCAmelCase ( self , _a ): __a , __a = self.get_input_output_texts(_a ) __a = tokenizer.encode(_a , add_special_tokens=_a ) __a = tokenizer.decode(_a , clean_up_tokenization_spaces=_a ) return text, ids def __UpperCAmelCase ( self ): pass # TODO add if relevant def __UpperCAmelCase ( self ): pass # TODO add if relevant def __UpperCAmelCase ( self ): pass # TODO add if relevant def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file ) __a = tokenizer.tokenize('''こんにちは、世界。\nこんばんは、世界。''' ) self.assertListEqual(_a , ['''こんにちは''', '''、''', '''世界''', '''。''', '''こん''', '''##ばんは''', '''、''', '''世界''', '''。'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file , word_tokenizer_type='''mecab''' ) self.assertIsNotNone(_a ) __a = '''こんにちは、世界。\nこんばんは、世界。''' __a = tokenizer.tokenize(_a ) self.assertListEqual(_a , ['''こんにちは''', '''、''', '''世界''', '''。''', '''こん''', '''##ばんは''', '''、''', '''世界''', '''。'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) __a = os.path.join(self.tmpdirname , '''tokenizer.bin''' ) with open(_a , '''wb''' ) as handle: pickle.dump(_a , _a ) with open(_a , '''rb''' ) as handle: __a = pickle.load(_a ) __a = tokenizer_new.tokenize(_a ) self.assertListEqual(_a , _a ) def __UpperCAmelCase ( self ): __a = MecabTokenizer(mecab_dic='''ipadic''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップルストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', '''。'''] , ) def __UpperCAmelCase ( self ): try: __a = MecabTokenizer(mecab_dic='''unidic_lite''' ) except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', '''。'''] , ) def __UpperCAmelCase ( self ): try: __a = MecabTokenizer(mecab_dic='''unidic''' ) except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', '''。'''] , ) def __UpperCAmelCase ( self ): __a = MecabTokenizer(do_lower_case=_a , mecab_dic='''ipadic''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップルストア''', '''で''', '''iphone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', '''。'''] , ) def __UpperCAmelCase ( self ): try: __a = MecabTokenizer( do_lower_case=_a , normalize_text=_a , mecab_option='''-d /usr/local/lib/mecab/dic/jumandic''' ) except RuntimeError: # if dict doesn't exist in the system, previous code raises this error. return self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップルストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れた''', '''\u3000''', '''。'''] , ) def __UpperCAmelCase ( self ): __a = MecabTokenizer(normalize_text=_a , mecab_dic='''ipadic''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップルストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', ''' ''', '''。'''] , ) @require_sudachi def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file , word_tokenizer_type='''sudachi''' ) self.assertIsNotNone(_a ) __a = '''こんにちは、世界。\nこんばんは、世界。''' __a = tokenizer.tokenize(_a ) self.assertListEqual(_a , ['''こんにちは''', '''、''', '''世界''', '''。''', '''こん''', '''##ばんは''', '''、''', '''世界''', '''。'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) __a = os.path.join(self.tmpdirname , '''tokenizer.bin''' ) with open(_a , '''wb''' ) as handle: pickle.dump(_a , _a ) with open(_a , '''rb''' ) as handle: __a = pickle.load(_a ) __a = tokenizer_new.tokenize(_a ) self.assertListEqual(_a , _a ) @require_sudachi def __UpperCAmelCase ( self ): __a = SudachiTokenizer(sudachi_dict_type='''core''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , [''' ''', '''\t''', '''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', ''' ''', '''が''', ''' ''', ''' ''', '''\n ''', '''発売''', '''さ''', '''れ''', '''た''', ''' ''', '''。''', ''' ''', ''' '''] , ) @require_sudachi def __UpperCAmelCase ( self ): __a = SudachiTokenizer(sudachi_dict_type='''core''' , sudachi_split_mode='''A''' ) self.assertListEqual(tokenizer.tokenize('''外国人参政権''' ) , ['''外国''', '''人''', '''参政''', '''権'''] ) @require_sudachi def __UpperCAmelCase ( self ): __a = SudachiTokenizer(sudachi_dict_type='''core''' , sudachi_split_mode='''B''' ) self.assertListEqual(tokenizer.tokenize('''外国人参政権''' ) , ['''外国人''', '''参政権'''] ) @require_sudachi def __UpperCAmelCase ( self ): __a = SudachiTokenizer(sudachi_dict_type='''core''' , sudachi_split_mode='''C''' ) self.assertListEqual(tokenizer.tokenize('''外国人参政権''' ) , ['''外国人参政権'''] ) @require_sudachi def __UpperCAmelCase ( self ): __a = SudachiTokenizer(do_lower_case=_a , sudachi_dict_type='''core''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , [''' ''', '''\t''', '''アップル''', '''ストア''', '''で''', '''iphone''', '''8''', ''' ''', '''が''', ''' ''', ''' ''', '''\n ''', '''発売''', '''さ''', '''れ''', '''た''', ''' ''', '''。''', ''' ''', ''' '''] , ) @require_sudachi def __UpperCAmelCase ( self ): __a = SudachiTokenizer(normalize_text=_a , sudachi_dict_type='''core''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , [''' ''', '''\t''', '''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', ''' ''', '''が''', ''' ''', ''' ''', '''\n ''', '''発売''', '''さ''', '''れ''', '''た''', '''\u3000''', '''。''', ''' ''', ''' '''] , ) @require_sudachi def __UpperCAmelCase ( self ): __a = SudachiTokenizer(trim_whitespace=_a , sudachi_dict_type='''core''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', '''。'''] , ) @require_jumanpp def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file , word_tokenizer_type='''jumanpp''' ) self.assertIsNotNone(_a ) __a = '''こんにちは、世界。\nこんばんは、世界。''' __a = tokenizer.tokenize(_a ) self.assertListEqual(_a , ['''こんにちは''', '''、''', '''世界''', '''。''', '''こん''', '''##ばんは''', '''、''', '''世界''', '''。'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) __a = os.path.join(self.tmpdirname , '''tokenizer.bin''' ) with open(_a , '''wb''' ) as handle: pickle.dump(_a , _a ) with open(_a , '''rb''' ) as handle: __a = pickle.load(_a ) __a = tokenizer_new.tokenize(_a ) self.assertListEqual(_a , _a ) @require_jumanpp def __UpperCAmelCase ( self ): __a = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''\u3000''', '''が''', '''\u3000''', '''\u3000''', '''\u3000''', '''発売''', '''さ''', '''れた''', '''\u3000''', '''。'''] , ) @require_jumanpp def __UpperCAmelCase ( self ): __a = JumanppTokenizer(do_lower_case=_a ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iphone''', '''8''', '''\u3000''', '''が''', '''\u3000''', '''\u3000''', '''\u3000''', '''発売''', '''さ''', '''れた''', '''\u3000''', '''。'''] , ) @require_jumanpp def __UpperCAmelCase ( self ): __a = JumanppTokenizer(normalize_text=_a ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''ア''', '''ッ''', '''フ''', '''゚''', '''ル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''\u3000''', '''が''', '''\u3000''', '''\u3000''', '''\u3000''', '''発売''', '''さ''', '''れた''', '''\u3000''', '''。'''] , ) @require_jumanpp def __UpperCAmelCase ( self ): __a = JumanppTokenizer(trim_whitespace=_a ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れた''', '''。'''] , ) @require_jumanpp def __UpperCAmelCase ( self ): __a = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize('''ありがとうございますm(_ _)m見つけるのが大変です。''' ) , ['''ありがとう''', '''ございます''', '''m(_ _)m''', '''見つける''', '''の''', '''が''', '''大変です''', '''。'''] , ) def __UpperCAmelCase ( self ): __a = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''こんにちは''', '''こん''', '''にちは''', '''ばんは''', '''##こん''', '''##にちは''', '''##ばんは'''] __a = {} for i, token in enumerate(_a ): __a = i __a = WordpieceTokenizer(vocab=_a , unk_token='''[UNK]''' ) self.assertListEqual(tokenizer.tokenize('''''' ) , [] ) self.assertListEqual(tokenizer.tokenize('''こんにちは''' ) , ['''こんにちは'''] ) self.assertListEqual(tokenizer.tokenize('''こんばんは''' ) , ['''こん''', '''##ばんは'''] ) self.assertListEqual(tokenizer.tokenize('''こんばんは こんばんにちは こんにちは''' ) , ['''こん''', '''##ばんは''', '''[UNK]''', '''こんにちは'''] ) def __UpperCAmelCase ( self ): __a = BertJapaneseTokenizer.from_pretrained('''nlp-waseda/roberta-base-japanese-with-auto-jumanpp''' ) __a = tokenizer.subword_tokenizer __a = subword_tokenizer.tokenize('''国境 の 長い トンネル を 抜ける と 雪国 であった 。''' ) self.assertListEqual(_a , ['''▁国境''', '''▁の''', '''▁長い''', '''▁トンネル''', '''▁を''', '''▁抜ける''', '''▁と''', '''▁雪''', '''国''', '''▁であった''', '''▁。'''] ) __a = subword_tokenizer.tokenize('''こんばんは こんばん にち は こんにちは''' ) self.assertListEqual(_a , ['''▁こん''', '''ばん''', '''は''', '''▁こん''', '''ばん''', '''▁に''', '''ち''', '''▁は''', '''▁こんにちは'''] ) def __UpperCAmelCase ( self ): __a = self.tokenizer_class.from_pretrained('''cl-tohoku/bert-base-japanese''' ) __a = tokenizer.encode('''ありがとう。''' , add_special_tokens=_a ) __a = tokenizer.encode('''どういたしまして。''' , add_special_tokens=_a ) __a = tokenizer.build_inputs_with_special_tokens(_a ) __a = tokenizer.build_inputs_with_special_tokens(_a , _a ) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Dict = BertJapaneseTokenizer __UpperCAmelCase : Any = False def __UpperCAmelCase ( self ): super().setUp() __a = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''こ''', '''ん''', '''に''', '''ち''', '''は''', '''ば''', '''世''', '''界''', '''、''', '''。'''] __a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) def __UpperCAmelCase ( self , **_a ): return BertJapaneseTokenizer.from_pretrained(self.tmpdirname , subword_tokenizer_type='''character''' , **_a ) def __UpperCAmelCase ( self , _a ): __a = '''こんにちは、世界。 \nこんばんは、世界。''' __a = '''こ ん に ち は 、 世 界 。 こ ん ば ん は 、 世 界 。''' return input_text, output_text def __UpperCAmelCase ( self ): pass # TODO add if relevant def __UpperCAmelCase ( self ): pass # TODO add if relevant def __UpperCAmelCase ( self ): pass # TODO add if relevant def __UpperCAmelCase ( self ): __a = self.tokenizer_class(self.vocab_file , subword_tokenizer_type='''character''' ) __a = tokenizer.tokenize('''こんにちは、世界。 \nこんばんは、世界。''' ) self.assertListEqual( _a , ['''こ''', '''ん''', '''に''', '''ち''', '''は''', '''、''', '''世''', '''界''', '''。''', '''こ''', '''ん''', '''ば''', '''ん''', '''は''', '''、''', '''世''', '''界''', '''。'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_a ) , [3, 4, 5, 6, 7, 11, 9, 10, 12, 3, 4, 8, 4, 7, 11, 9, 10, 12] ) def __UpperCAmelCase ( self ): __a = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''こ''', '''ん''', '''に''', '''ち''', '''は''', '''ば''', '''世''', '''界''', '''、''', '''。'''] __a = {} for i, token in enumerate(_a ): __a = i __a = CharacterTokenizer(vocab=_a , unk_token='''[UNK]''' ) self.assertListEqual(tokenizer.tokenize('''''' ) , [] ) self.assertListEqual(tokenizer.tokenize('''こんにちは''' ) , ['''こ''', '''ん''', '''に''', '''ち''', '''は'''] ) self.assertListEqual(tokenizer.tokenize('''こんにちほ''' ) , ['''こ''', '''ん''', '''に''', '''ち''', '''[UNK]'''] ) def __UpperCAmelCase ( self ): __a = self.tokenizer_class.from_pretrained('''cl-tohoku/bert-base-japanese-char''' ) __a = tokenizer.encode('''ありがとう。''' , add_special_tokens=_a ) __a = tokenizer.encode('''どういたしまして。''' , add_special_tokens=_a ) __a = tokenizer.build_inputs_with_special_tokens(_a ) __a = tokenizer.build_inputs_with_special_tokens(_a , _a ) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = '''cl-tohoku/bert-base-japanese''' __a = AutoTokenizer.from_pretrained(_a ) self.assertIsInstance(_a , _a ) class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = '''cl-tohoku/bert-base-japanese''' with self.assertLogs('''transformers''' , level='''WARNING''' ) as cm: BertTokenizer.from_pretrained(_a ) self.assertTrue( cm.records[0].message.startswith( '''The tokenizer class you load from this checkpoint is not the same type as the class this function''' ''' is called from.''' ) ) __a = '''bert-base-cased''' with self.assertLogs('''transformers''' , level='''WARNING''' ) as cm: BertJapaneseTokenizer.from_pretrained(_a ) self.assertTrue( cm.records[0].message.startswith( '''The tokenizer class you load from this checkpoint is not the same type as the class this function''' ''' is called from.''' ) )
45
"""simple docstring""" import unittest import torch from torch import nn from diffusers.models.activations import get_activation class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = get_activation('''swish''' ) self.assertIsInstance(_a , nn.SiLU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''silu''' ) self.assertIsInstance(_a , nn.SiLU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''mish''' ) self.assertIsInstance(_a , nn.Mish ) self.assertEqual(act(torch.tensor(-200 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''gelu''' ) self.assertIsInstance(_a , nn.GELU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 )
45
1
"""simple docstring""" from itertools import permutations def lowercase ( lowerCAmelCase__ : tuple ) -> bool: 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 __a = [7, 11, 13, 17] for i, test in enumerate(lowerCAmelCase__ ): if (num[i + 4] * 100 + num[i + 5] * 10 + num[i + 6]) % test != 0: return False return True def lowercase ( lowerCAmelCase__ : int = 10 ) -> int: return sum( int(''''''.join(map(lowerCAmelCase__ , lowerCAmelCase__ ) ) ) for num in permutations(range(lowerCAmelCase__ ) ) if is_substring_divisible(lowerCAmelCase__ ) ) if __name__ == "__main__": print(F'''{solution() = }''')
45
"""simple docstring""" from __future__ import annotations import unittest from transformers import EsmConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy import tensorflow as tf from transformers.models.esm.modeling_tf_esm import ( TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, TFEsmModel, ) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , ): __a = parent __a = 13 __a = 7 __a = True __a = True __a = True __a = 99 __a = 32 __a = 2 __a = 4 __a = 37 __a = '''gelu''' __a = 0.1 __a = 0.1 __a = 512 __a = 16 __a = 2 __a = 0.02 __a = 3 __a = 4 __a = None def __UpperCAmelCase ( self ): __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a = None if self.use_input_mask: __a = random_attention_mask([self.batch_size, self.seq_length] ) __a = None __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __a = ids_tensor([self.batch_size] , self.num_choices ) __a = EsmConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , pad_token_id=1 , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def __UpperCAmelCase ( self ): ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = self.prepare_config_and_inputs() __a = True __a = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __a = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , _a ): __a = TFEsmModel(config=_a ) __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} __a = model(_a ) __a = [input_ids, input_mask] __a = model(_a ) __a = model(_a ) 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 , _a , ): __a = True __a = TFEsmModel(config=_a ) __a = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''encoder_hidden_states''': encoder_hidden_states, '''encoder_attention_mask''': encoder_attention_mask, } __a = model(_a ) __a = [input_ids, input_mask] __a = model(_a , encoder_hidden_states=_a ) # Also check the case where encoder outputs are not passed __a = model(_a , attention_mask=_a ) 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 = TFEsmForMaskedLM(config=_a ) __a = model([input_ids, input_mask] ) 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 = self.num_labels __a = TFEsmForTokenClassification(config=_a ) __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} __a = model(_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = config_and_inputs __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = ( ( TFEsmModel, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, ) if is_tf_available() else () ) __UpperCAmelCase : Tuple = ( { 'feature-extraction': TFEsmModel, 'fill-mask': TFEsmForMaskedLM, 'text-classification': TFEsmForSequenceClassification, 'token-classification': TFEsmForTokenClassification, 'zero-shot': TFEsmForSequenceClassification, } if is_tf_available() else {} ) __UpperCAmelCase : Tuple = False __UpperCAmelCase : Union[str, Any] = False def __UpperCAmelCase ( self ): __a = TFEsmModelTester(self ) __a = ConfigTester(self , config_class=_a , hidden_size=37 ) def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_a ) @slow def __UpperCAmelCase ( self ): for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = TFEsmModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip('''Protein models do not support embedding resizing.''' ) def __UpperCAmelCase ( self ): pass @unittest.skip('''Protein models do not support embedding resizing.''' ) def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): __a , __a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a = model_class(_a ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class is TFEsmForMaskedLM: # Output embedding test differs from the main test because they're a matrix, not a layer __a = model.get_bias() assert isinstance(_a , _a ) for k, v in name.items(): assert isinstance(_a , tf.Variable ) else: __a = model.get_output_embeddings() assert x is None __a = model.get_bias() assert name is None @require_tf class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def __UpperCAmelCase ( self ): __a = TFEsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) __a = tf.constant([[0, 1, 2, 3, 4, 5]] ) __a = model(_a )[0] __a = [1, 6, 33] self.assertEqual(list(output.numpy().shape ) , _a ) # compare the actual values for a slice. __a = tf.constant( [ [ [8.92_1518, -10.58_9814, -6.467_1307], [-6.396_7156, -13.91_1377, -1.121_1915], [-7.78_1247, -13.95_1557, -3.74_0592], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-2 ) ) @slow def __UpperCAmelCase ( self ): __a = TFEsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) __a = tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) __a = model(_a )[0] # compare the actual values for a slice. __a = tf.constant( [ [ [0.1444_3092, 0.5412_5327, 0.324_7739], [0.3034_0484, 0.0052_6676, 0.3107_7722], [0.3227_8043, -0.2498_7096, 0.341_4628], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-4 ) )
45
1
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import PoolFormerImageProcessor class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _a , _a=7 , _a=3 , _a=30 , _a=400 , _a=True , _a=None , _a=0.9 , _a=None , _a=True , _a=[0.5, 0.5, 0.5] , _a=[0.5, 0.5, 0.5] , ): __a = size if size is not None else {'''shortest_edge''': 30} __a = crop_size if crop_size is not None else {'''height''': 30, '''width''': 30} __a = parent __a = batch_size __a = num_channels __a = min_resolution __a = max_resolution __a = do_resize_and_center_crop __a = size __a = crop_pct __a = crop_size __a = do_normalize __a = image_mean __a = image_std def __UpperCAmelCase ( self ): return { "size": self.size, "do_resize_and_center_crop": self.do_resize_and_center_crop, "crop_pct": self.crop_pct, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = PoolFormerImageProcessor if is_vision_available() else None def __UpperCAmelCase ( self ): __a = PoolFormerImageProcessingTester(self ) @property def __UpperCAmelCase ( self ): return self.image_processor_tester.prepare_image_processor_dict() def __UpperCAmelCase ( self ): __a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , '''do_resize_and_center_crop''' ) ) self.assertTrue(hasattr(_a , '''size''' ) ) self.assertTrue(hasattr(_a , '''crop_pct''' ) ) self.assertTrue(hasattr(_a , '''do_normalize''' ) ) self.assertTrue(hasattr(_a , '''image_mean''' ) ) self.assertTrue(hasattr(_a , '''image_std''' ) ) def __UpperCAmelCase ( self ): __a = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 30} ) self.assertEqual(image_processor.crop_size , {'''height''': 30, '''width''': 30} ) __a = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42} ) self.assertEqual(image_processor.crop_size , {'''height''': 84, '''width''': 84} ) def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): # Initialize image_processing __a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __a = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input __a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched __a = image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __UpperCAmelCase ( self ): # Initialize image_processing __a = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __a = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input __a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched __a = image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __UpperCAmelCase ( self ): # Initialize image_processing __a = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __a = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input __a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched __a = image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int ) -> str: if number > 0: raise ValueError('''input must be a negative integer''' ) __a = len(bin(lowerCAmelCase__ )[3:] ) __a = bin(abs(lowerCAmelCase__ ) - (1 << binary_number_length) )[3:] __a = ( ( '''1''' + '''0''' * (binary_number_length - len(lowerCAmelCase__ )) + twos_complement_number ) if number < 0 else '''0''' ) return "0b" + twos_complement_number if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" import argparse import torch from transformers import ( SpeechTaConfig, SpeechTaFeatureExtractor, SpeechTaForSpeechToSpeech, SpeechTaForSpeechToText, SpeechTaForTextToSpeech, SpeechTaProcessor, SpeechTaTokenizer, logging, ) from transformers.tokenization_utils import AddedToken logging.set_verbosity_info() lowercase_ = logging.get_logger("transformers.models.speecht5") lowercase_ = { "speech_encoder_prenet.layer_norm": "speecht5.encoder.prenet.feature_projection.layer_norm", "speech_encoder_prenet.post_extract_proj": "speecht5.encoder.prenet.feature_projection.projection", "speech_encoder_prenet.pos_conv.0": "speecht5.encoder.prenet.pos_conv_embed.conv", "speech_encoder_prenet.mask_emb": "speecht5.encoder.prenet.masked_spec_embed", } lowercase_ = { "text_encoder_prenet.encoder_prenet.0": "speecht5.encoder.prenet.embed_tokens", "text_encoder_prenet.encoder_prenet.1.alpha": "speecht5.encoder.prenet.encode_positions.alpha", } lowercase_ = { "speech_decoder_prenet.decoder_prenet.0.0.prenet.0.0": "speecht5.decoder.prenet.layers.0", "speech_decoder_prenet.decoder_prenet.0.0.prenet.1.0": "speecht5.decoder.prenet.layers.1", "speech_decoder_prenet.decoder_prenet.0.1": "speecht5.decoder.prenet.final_layer", "speech_decoder_prenet.decoder_prenet.1.alpha": "speecht5.decoder.prenet.encode_positions.alpha", "speech_decoder_prenet.spkembs_layer.0": "speecht5.decoder.prenet.speaker_embeds_layer", } lowercase_ = { "speech_decoder_postnet.feat_out": "speech_decoder_postnet.feat_out", "speech_decoder_postnet.prob_out": "speech_decoder_postnet.prob_out", "speech_decoder_postnet.postnet.postnet.0.0": "speech_decoder_postnet.layers.0.conv", "speech_decoder_postnet.postnet.postnet.0.1": "speech_decoder_postnet.layers.0.batch_norm", "speech_decoder_postnet.postnet.postnet.1.0": "speech_decoder_postnet.layers.1.conv", "speech_decoder_postnet.postnet.postnet.1.1": "speech_decoder_postnet.layers.1.batch_norm", "speech_decoder_postnet.postnet.postnet.2.0": "speech_decoder_postnet.layers.2.conv", "speech_decoder_postnet.postnet.postnet.2.1": "speech_decoder_postnet.layers.2.batch_norm", "speech_decoder_postnet.postnet.postnet.3.0": "speech_decoder_postnet.layers.3.conv", "speech_decoder_postnet.postnet.postnet.3.1": "speech_decoder_postnet.layers.3.batch_norm", "speech_decoder_postnet.postnet.postnet.4.0": "speech_decoder_postnet.layers.4.conv", "speech_decoder_postnet.postnet.postnet.4.1": "speech_decoder_postnet.layers.4.batch_norm", } lowercase_ = { "text_decoder_prenet.embed_tokens": "speecht5.decoder.prenet.embed_tokens", } lowercase_ = { "text_decoder_postnet.output_projection": "text_decoder_postnet.lm_head", } lowercase_ = { "encoder.layers.*.self_attn.k_proj": "speecht5.encoder.wrapped_encoder.layers.*.attention.k_proj", "encoder.layers.*.self_attn.v_proj": "speecht5.encoder.wrapped_encoder.layers.*.attention.v_proj", "encoder.layers.*.self_attn.q_proj": "speecht5.encoder.wrapped_encoder.layers.*.attention.q_proj", "encoder.layers.*.self_attn.out_proj": "speecht5.encoder.wrapped_encoder.layers.*.attention.out_proj", "encoder.layers.*.self_attn_layer_norm": "speecht5.encoder.wrapped_encoder.layers.*.layer_norm", "encoder.layers.*.fc1": "speecht5.encoder.wrapped_encoder.layers.*.feed_forward.intermediate_dense", "encoder.layers.*.fc2": "speecht5.encoder.wrapped_encoder.layers.*.feed_forward.output_dense", "encoder.layers.*.final_layer_norm": "speecht5.encoder.wrapped_encoder.layers.*.final_layer_norm", "encoder.layer_norm": "speecht5.encoder.wrapped_encoder.layer_norm", "encoder.pos_emb.pe_k": "speecht5.encoder.wrapped_encoder.embed_positions.pe_k", } lowercase_ = { "decoder.layers.*.self_attn.k_proj": "speecht5.decoder.wrapped_decoder.layers.*.self_attn.k_proj", "decoder.layers.*.self_attn.v_proj": "speecht5.decoder.wrapped_decoder.layers.*.self_attn.v_proj", "decoder.layers.*.self_attn.q_proj": "speecht5.decoder.wrapped_decoder.layers.*.self_attn.q_proj", "decoder.layers.*.self_attn.out_proj": "speecht5.decoder.wrapped_decoder.layers.*.self_attn.out_proj", "decoder.layers.*.self_attn_layer_norm": "speecht5.decoder.wrapped_decoder.layers.*.self_attn_layer_norm", "decoder.layers.*.encoder_attn.k_proj": "speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.k_proj", "decoder.layers.*.encoder_attn.v_proj": "speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.v_proj", "decoder.layers.*.encoder_attn.q_proj": "speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.q_proj", "decoder.layers.*.encoder_attn.out_proj": "speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.out_proj", "decoder.layers.*.encoder_attn_layer_norm": "speecht5.decoder.wrapped_decoder.layers.*.encoder_attn_layer_norm", "decoder.layers.*.fc1": "speecht5.decoder.wrapped_decoder.layers.*.feed_forward.intermediate_dense", "decoder.layers.*.fc2": "speecht5.decoder.wrapped_decoder.layers.*.feed_forward.output_dense", "decoder.layers.*.final_layer_norm": "speecht5.decoder.wrapped_decoder.layers.*.final_layer_norm", } lowercase_ = { **MAPPING_SPEECH_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_TEXT_DECODER_PRENET, **MAPPING_TEXT_DECODER_POSTNET, } lowercase_ = { **MAPPING_TEXT_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_SPEECH_DECODER_PRENET, **MAPPING_SPEECH_DECODER_POSTNET, } lowercase_ = { **MAPPING_SPEECH_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_SPEECH_DECODER_PRENET, **MAPPING_SPEECH_DECODER_POSTNET, } lowercase_ = [] lowercase_ = [ "encoder.version", "encoder.layers.*.norm_k.weight", "encoder.layers.*.norm_k.bias", "decoder.version", "decoder.layers.*.norm_k.weight", "decoder.layers.*.norm_k.bias", "decoder.pos_emb.pe_k", "speech_encoder_prenet.embed_positions._float_tensor", "text_decoder_prenet.embed_positions._float_tensor", ] lowercase_ = IGNORE_KEYS + [ "encoder.proj", "text_encoder_prenet.*", "speech_decoder_prenet.*", "speech_decoder_postnet.*", ] lowercase_ = IGNORE_KEYS + [ "encoder.proj", "speech_encoder_prenet.*", "text_decoder_prenet.*", "text_decoder_postnet.*", ] lowercase_ = IGNORE_KEYS + [ "encoder.proj", "text_encoder_prenet.*", "text_decoder_prenet.*", "text_decoder_postnet.*", ] def lowercase ( lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Any ) -> Tuple: for attribute in key.split('''.''' ): __a = getattr(lowerCAmelCase__ , lowerCAmelCase__ ) if weight_type is not None: __a = getattr(lowerCAmelCase__ , lowerCAmelCase__ ).shape else: __a = hf_pointer.shape if hf_shape != value.shape: raise ValueError( f'''Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be''' f''' {value.shape} for {full_name}''' ) if weight_type == "weight": __a = value elif weight_type == "weight_g": __a = value elif weight_type == "weight_v": __a = value elif weight_type == "bias": __a = value elif weight_type == "running_mean": __a = value elif weight_type == "running_var": __a = value elif weight_type == "num_batches_tracked": __a = value else: __a = value logger.info(f'''{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.''' ) def lowercase ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Any ) -> Optional[Any]: for key in ignore_keys: if key.endswith('''.*''' ): if name.startswith(key[:-1] ): return True elif ".*." in key: __a , __a = key.split('''.*.''' ) if prefix in name and suffix in name: return True elif key in name: return True return False def lowercase ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : str , lowerCAmelCase__ : List[str] ) -> int: __a = [] if task == "s2t": __a = hf_model.speechta.encoder.prenet.feature_encoder __a = MAPPING_S2T __a = IGNORE_KEYS_S2T elif task == "t2s": __a = None __a = MAPPING_T2S __a = IGNORE_KEYS_T2S elif task == "s2s": __a = hf_model.speechta.encoder.prenet.feature_encoder __a = MAPPING_S2S __a = IGNORE_KEYS_S2S else: raise ValueError(f'''Unsupported task: {task}''' ) for name, value in fairseq_dict.items(): if should_ignore(lowerCAmelCase__ , lowerCAmelCase__ ): logger.info(f'''{name} was ignored''' ) continue __a = False if "conv_layers" in name: load_conv_layer( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , hf_model.config.feat_extract_norm == '''group''' , ) __a = True else: for key, mapped_key in MAPPING.items(): # mapped_key = "speecht5." + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if "*" in key: __a , __a = key.split('''.*.''' ) if prefix in name and suffix in name: __a = suffix # if key in name or key.split("w2v_model.")[-1] == name.split(".")[0]: if key in name: __a = True if "*" in mapped_key: __a = name.split(lowerCAmelCase__ )[0].split('''.''' )[-2] __a = mapped_key.replace('''*''' , lowerCAmelCase__ ) if "weight_g" in name: __a = '''weight_g''' elif "weight_v" in name: __a = '''weight_v''' elif "bias" in name: __a = '''bias''' elif "weight" in name: __a = '''weight''' elif "running_mean" in name: __a = '''running_mean''' elif "running_var" in name: __a = '''running_var''' elif "num_batches_tracked" in name: __a = '''num_batches_tracked''' else: __a = None set_recursively(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) continue if not is_used: unused_weights.append(lowerCAmelCase__ ) logger.warning(f'''Unused weights: {unused_weights}''' ) def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Optional[int] ) -> Optional[int]: __a = full_name.split('''conv_layers.''' )[-1] __a = name.split('''.''' ) __a = int(items[0] ) __a = int(items[1] ) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) __a = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) __a = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found.''' ) __a = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found.''' ) __a = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(lowerCAmelCase__ ) @torch.no_grad() def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : List[Any]=None , lowerCAmelCase__ : List[str]=None , ) -> int: if config_path is not None: __a = SpeechTaConfig.from_pretrained(lowerCAmelCase__ ) else: __a = SpeechTaConfig() if task == "s2t": __a = config.max_text_positions __a = SpeechTaForSpeechToText(lowerCAmelCase__ ) elif task == "t2s": __a = 1876 __a = 600 __a = config.max_speech_positions __a = SpeechTaForTextToSpeech(lowerCAmelCase__ ) elif task == "s2s": __a = 1876 __a = config.max_speech_positions __a = SpeechTaForSpeechToSpeech(lowerCAmelCase__ ) else: raise ValueError(f'''Unknown task name: {task}''' ) if vocab_path: __a = SpeechTaTokenizer(lowerCAmelCase__ , model_max_length=config.max_text_positions ) # Mask token behaves like a normal word, i.e. include the space before it __a = AddedToken('''<mask>''' , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) __a = mask_token tokenizer.add_special_tokens({'''mask_token''': mask_token} ) tokenizer.add_tokens(['''<ctc_blank>'''] ) __a = SpeechTaFeatureExtractor() __a = SpeechTaProcessor(tokenizer=lowerCAmelCase__ , feature_extractor=lowerCAmelCase__ ) processor.save_pretrained(lowerCAmelCase__ ) __a = torch.load(lowerCAmelCase__ ) recursively_load_weights(fairseq_checkpoint['''model'''] , lowerCAmelCase__ , lowerCAmelCase__ ) model.save_pretrained(lowerCAmelCase__ ) if repo_id: print('''Pushing to the hub...''' ) processor.push_to_hub(lowerCAmelCase__ ) model.push_to_hub(lowerCAmelCase__ ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument( "--task", default="s2t", type=str, help="Type of the SpeechT5 model you'd like to convert. Should be one of 's2t', 't2s', 's2s'.", ) parser.add_argument("--checkpoint_path", required=True, default=None, type=str, help="Path to fairseq checkpoint") parser.add_argument("--vocab_path", default=None, type=str, help="Path to SentencePiece model") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") parser.add_argument( "--pytorch_dump_folder_path", required=True, default=None, type=str, help="Path to the output PyTorch model." ) parser.add_argument( "--push_to_hub", default=None, type=str, help="Where to upload the converted model on the 🤗 hub." ) lowercase_ = parser.parse_args() convert_speechta_checkpoint( args.task, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.vocab_path, args.push_to_hub, )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : list[str] ) -> str: __a = '''''' for word_or_phrase in separated: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise Exception('''join() accepts only strings to be joined''' ) joined += word_or_phrase + separator return joined.strip(lowerCAmelCase__ ) if __name__ == "__main__": from doctest import testmod testmod()
45
1
"""simple docstring""" import argparse import pytorch_lightning as pl import torch from torch import nn from transformers import LongformerForQuestionAnswering, LongformerModel class __lowerCAmelCase ( pl.LightningModule ): '''simple docstring''' def __init__( self , _a ): super().__init__() __a = model __a = 2 __a = nn.Linear(self.model.config.hidden_size , self.num_labels ) def __UpperCAmelCase ( self ): pass def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : str , lowerCAmelCase__ : str ) -> Optional[int]: # load longformer model from model identifier __a = LongformerModel.from_pretrained(lowerCAmelCase__ ) __a = LightningModel(lowerCAmelCase__ ) __a = torch.load(lowerCAmelCase__ , map_location=torch.device('''cpu''' ) ) lightning_model.load_state_dict(ckpt['''state_dict'''] ) # init longformer question answering model __a = LongformerForQuestionAnswering.from_pretrained(lowerCAmelCase__ ) # transfer weights longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict() ) longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict() ) longformer_for_qa.eval() # save model longformer_for_qa.save_pretrained(lowerCAmelCase__ ) print(f'''Conversion successful. Model saved under {pytorch_dump_folder_path}''' ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--longformer_model", default=None, type=str, required=True, help="model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.", ) parser.add_argument( "--longformer_question_answering_ckpt_path", default=None, type=str, required=True, help="Path the official PyTorch Lightning Checkpoint.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) lowercase_ = parser.parse_args() convert_longformer_qa_checkpoint_to_pytorch( args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path )
45
"""simple docstring""" lowercase_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" def lowercase ( lowerCAmelCase__ : bytes ) -> bytes: # Make sure the supplied data is a bytes-like object if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = f'''a bytes-like object is required, not \'{data.__class__.__name__}\'''' raise TypeError(lowerCAmelCase__ ) __a = ''''''.join(bin(lowerCAmelCase__ )[2:].zfill(8 ) for byte in data ) __a = len(lowerCAmelCase__ ) % 6 != 0 if padding_needed: # The padding that will be added later __a = b'''=''' * ((6 - len(lowerCAmelCase__ ) % 6) // 2) # Append binary_stream with arbitrary binary digits (0's by default) to make its # length a multiple of 6. binary_stream += "0" * (6 - len(lowerCAmelCase__ ) % 6) else: __a = b'''''' # Encode every 6 binary digits to their corresponding Base64 character return ( "".join( B64_CHARSET[int(binary_stream[index : index + 6] , 2 )] for index in range(0 , len(lowerCAmelCase__ ) , 6 ) ).encode() + padding ) def lowercase ( lowerCAmelCase__ : str ) -> bytes: # Make sure encoded_data is either a string or a bytes-like object if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = ( '''argument should be a bytes-like object or ASCII string, ''' f'''not \'{encoded_data.__class__.__name__}\'''' ) raise TypeError(lowerCAmelCase__ ) # In case encoded_data is a bytes-like object, make sure it contains only # ASCII characters so we convert it to a string object if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): try: __a = encoded_data.decode('''utf-8''' ) except UnicodeDecodeError: raise ValueError('''base64 encoded data should only contain ASCII characters''' ) __a = encoded_data.count('''=''' ) # Check if the encoded string contains non base64 characters if padding: assert all( char in B64_CHARSET for char in encoded_data[:-padding] ), "Invalid base64 character(s) found." else: assert all( char in B64_CHARSET for char in encoded_data ), "Invalid base64 character(s) found." # Check the padding assert len(lowerCAmelCase__ ) % 4 == 0 and padding < 3, "Incorrect padding" if padding: # Remove padding if there is one __a = encoded_data[:-padding] __a = ''''''.join( bin(B64_CHARSET.index(lowerCAmelCase__ ) )[2:].zfill(6 ) for char in encoded_data )[: -padding * 2] else: __a = ''''''.join( bin(B64_CHARSET.index(lowerCAmelCase__ ) )[2:].zfill(6 ) for char in encoded_data ) __a = [ int(binary_stream[index : index + 8] , 2 ) for index in range(0 , len(lowerCAmelCase__ ) , 8 ) ] return bytes(lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = 'Speech2TextFeatureExtractor' __UpperCAmelCase : Any = 'Speech2TextTokenizer' def __init__( self , _a , _a ): super().__init__(_a , _a ) __a = self.feature_extractor __a = False def __call__( self , *_a , **_a ): # For backward compatibility if self._in_target_context_manager: return self.current_processor(*_a , **_a ) if "raw_speech" in kwargs: warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.''' ) __a = kwargs.pop('''raw_speech''' ) else: __a = kwargs.pop('''audio''' , _a ) __a = kwargs.pop('''sampling_rate''' , _a ) __a = kwargs.pop('''text''' , _a ) if len(_a ) > 0: __a = args[0] __a = args[1:] if audio is None and text is None: raise ValueError('''You need to specify either an `audio` or `text` input to process.''' ) if audio is not None: __a = self.feature_extractor(_a , *_a , sampling_rate=_a , **_a ) if text is not None: __a = self.tokenizer(_a , **_a ) if text is None: return inputs elif audio is None: return encodings else: __a = encodings['''input_ids'''] return inputs def __UpperCAmelCase ( self , *_a , **_a ): return self.tokenizer.batch_decode(*_a , **_a ) def __UpperCAmelCase ( self , *_a , **_a ): return self.tokenizer.decode(*_a , **_a ) @contextmanager def __UpperCAmelCase ( self ): warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your audio inputs, or in a separate call.''' ) __a = True __a = self.tokenizer yield __a = self.feature_extractor __a = False
45
"""simple docstring""" import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = inspect.getfile(accelerate.test_utils ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) __a = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_distributed_data_loop.py'''] ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_ops.py'''] ) @require_multi_gpu def __UpperCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices.''' ) __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_a , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices.''' ) __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.operation_file_path] print(f'''Command: {cmd}''' ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_a , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self ): __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_a , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices, using 2 devices only''' ) __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices='''0,1''' ): execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": lowercase_ = Accelerator() lowercase_ = (accelerator.state.process_index + 2, 1_0) lowercase_ = torch.randint(0, 1_0, shape).to(accelerator.device) lowercase_ = "" lowercase_ = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." lowercase_ = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." lowercase_ = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
45
1
"""simple docstring""" import math import tensorflow as tf from packaging import version def lowercase ( lowerCAmelCase__ : Tuple ) -> Union[str, Any]: __a = tf.convert_to_tensor(lowerCAmelCase__ ) __a = 0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0 ) , x.dtype ) )) return x * cdf def lowercase ( lowerCAmelCase__ : Dict ) -> str: __a = tf.convert_to_tensor(lowerCAmelCase__ ) __a = tf.cast(math.pi , x.dtype ) __a = tf.cast(0.04_47_15 , x.dtype ) __a = 0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi ) * (x + coeff * tf.pow(lowerCAmelCase__ , 3 )) )) return x * cdf def lowercase ( lowerCAmelCase__ : Dict ) -> Optional[int]: __a = tf.convert_to_tensor(lowerCAmelCase__ ) return x * tf.tanh(tf.math.softplus(lowerCAmelCase__ ) ) def lowercase ( lowerCAmelCase__ : str ) -> Optional[Any]: __a = tf.convert_to_tensor(lowerCAmelCase__ ) __a = tf.cast(0.04_47_15 , x.dtype ) __a = tf.cast(0.79_78_84_56_08 , x.dtype ) return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x) )) def lowercase ( lowerCAmelCase__ : Optional[Any] ) -> Union[str, Any]: __a = tf.convert_to_tensor(lowerCAmelCase__ ) __a = tf.cast(1.7_02 , x.dtype ) return x * tf.math.sigmoid(coeff * x ) def lowercase ( lowerCAmelCase__ : Optional[Any] ) -> Union[str, Any]: return tf.clip_by_value(_gelu(lowerCAmelCase__ ) , -10 , 10 ) def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Any=-1 ) -> str: __a , __a = tf.split(lowerCAmelCase__ , 2 , axis=lowerCAmelCase__ ) return a * tf.math.sigmoid(lowerCAmelCase__ ) if version.parse(tf.version.VERSION) >= version.parse("2.4"): def lowercase ( lowerCAmelCase__ : Optional[int] ) -> List[str]: return tf.keras.activations.gelu(lowerCAmelCase__ , approximate=lowerCAmelCase__ ) lowercase_ = tf.keras.activations.gelu lowercase_ = approximate_gelu_wrap else: lowercase_ = _gelu lowercase_ = _gelu_new lowercase_ = { "gelu": gelu, "gelu_10": gelu_aa, "gelu_fast": gelu_fast, "gelu_new": gelu_new, "glu": glu, "mish": mish, "quick_gelu": quick_gelu, "relu": tf.keras.activations.relu, "sigmoid": tf.keras.activations.sigmoid, "silu": tf.keras.activations.swish, "swish": tf.keras.activations.swish, "tanh": tf.keras.activations.tanh, } def lowercase ( lowerCAmelCase__ : Union[str, Any] ) -> int: if activation_string in ACTaFN: return ACTaFN[activation_string] else: raise KeyError(f'''function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys() )}''' )
45
"""simple docstring""" import numpy as np def lowercase ( lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : float ) -> np.ndarray: return np.where(vector > 0 , lowerCAmelCase__ , (alpha * (np.exp(lowerCAmelCase__ ) - 1)) ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" # DISCLAIMER: This file is strongly influenced by https://github.com/ermongroup/ddim from dataclasses import dataclass from typing import Optional, Tuple, Union import flax import jax import jax.numpy as jnp from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils_flax import ( CommonSchedulerState, FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, add_noise_common, get_velocity_common, ) @flax.struct.dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : CommonSchedulerState # setable values __UpperCAmelCase : jnp.ndarray __UpperCAmelCase : jnp.ndarray __UpperCAmelCase : Optional[int] = None @classmethod def __UpperCAmelCase ( cls , _a , _a , _a ): return cls(common=_a , init_noise_sigma=_a , timesteps=_a ) @dataclass class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : DDPMSchedulerState class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Optional[int] = [e.name for e in FlaxKarrasDiffusionSchedulers] __UpperCAmelCase : jnp.dtype @property def __UpperCAmelCase ( self ): return True @register_to_config def __init__( self , _a = 1_000 , _a = 0.0001 , _a = 0.02 , _a = "linear" , _a = None , _a = "fixed_small" , _a = True , _a = "epsilon" , _a = jnp.floataa , ): __a = dtype def __UpperCAmelCase ( self , _a = None ): if common is None: __a = CommonSchedulerState.create(self ) # standard deviation of the initial noise distribution __a = jnp.array(1.0 , dtype=self.dtype ) __a = jnp.arange(0 , self.config.num_train_timesteps ).round()[::-1] return DDPMSchedulerState.create( common=_a , init_noise_sigma=_a , timesteps=_a , ) def __UpperCAmelCase ( self , _a , _a , _a = None ): return sample def __UpperCAmelCase ( self , _a , _a , _a = () ): __a = self.config.num_train_timesteps // num_inference_steps # creates integer timesteps by multiplying by ratio # rounding to avoid issues when num_inference_step is power of 3 __a = (jnp.arange(0 , _a ) * step_ratio).round()[::-1] return state.replace( num_inference_steps=_a , timesteps=_a , ) def __UpperCAmelCase ( self , _a , _a , _a=None , _a=None ): __a = state.common.alphas_cumprod[t] __a = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) # For t > 0, compute predicted variance βt (see formula (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf) # and sample from it to get previous sample # x_{t-1} ~ N(pred_prev_sample, variance) == add variance to pred_sample __a = (1 - alpha_prod_t_prev) / (1 - alpha_prod_t) * state.common.betas[t] if variance_type is None: __a = self.config.variance_type # hacks - were probably added for training stability if variance_type == "fixed_small": __a = jnp.clip(_a , a_min=1E-20 ) # for rl-diffuser https://arxiv.org/abs/2205.09991 elif variance_type == "fixed_small_log": __a = jnp.log(jnp.clip(_a , a_min=1E-20 ) ) elif variance_type == "fixed_large": __a = state.common.betas[t] elif variance_type == "fixed_large_log": # Glide max_log __a = jnp.log(state.common.betas[t] ) elif variance_type == "learned": return predicted_variance elif variance_type == "learned_range": __a = variance __a = state.common.betas[t] __a = (predicted_variance + 1) / 2 __a = frac * max_log + (1 - frac) * min_log return variance def __UpperCAmelCase ( self , _a , _a , _a , _a , _a = None , _a = True , ): __a = timestep if key is None: __a = jax.random.PRNGKey(0 ) if model_output.shape[1] == sample.shape[1] * 2 and self.config.variance_type in ["learned", "learned_range"]: __a , __a = jnp.split(_a , sample.shape[1] , axis=1 ) else: __a = None # 1. compute alphas, betas __a = state.common.alphas_cumprod[t] __a = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) __a = 1 - alpha_prod_t __a = 1 - alpha_prod_t_prev # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if self.config.prediction_type == "epsilon": __a = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif self.config.prediction_type == "sample": __a = model_output elif self.config.prediction_type == "v_prediction": __a = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output else: raise ValueError( f'''prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample` ''' ''' for the FlaxDDPMScheduler.''' ) # 3. Clip "predicted x_0" if self.config.clip_sample: __a = jnp.clip(_a , -1 , 1 ) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf __a = (alpha_prod_t_prev ** 0.5 * state.common.betas[t]) / beta_prod_t __a = state.common.alphas[t] ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf __a = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise def random_variance(): __a = jax.random.split(_a , num=1 ) __a = jax.random.normal(_a , shape=model_output.shape , dtype=self.dtype ) return (self._get_variance(_a , _a , predicted_variance=_a ) ** 0.5) * noise __a = jnp.where(t > 0 , random_variance() , jnp.zeros(model_output.shape , dtype=self.dtype ) ) __a = pred_prev_sample + variance if not return_dict: return (pred_prev_sample, state) return FlaxDDPMSchedulerOutput(prev_sample=_a , state=_a ) def __UpperCAmelCase ( self , _a , _a , _a , _a , ): return add_noise_common(state.common , _a , _a , _a ) def __UpperCAmelCase ( self , _a , _a , _a , _a , ): return get_velocity_common(state.common , _a , _a , _a ) def __len__( self ): return self.config.num_train_timesteps
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: __a = set() # Replace all the whitespace in our sentence __a = input_str.replace(''' ''' , '''''' ) for alpha in input_str: if "a" <= alpha.lower() <= "z": frequency.add(alpha.lower() ) return len(lowerCAmelCase__ ) == 26 def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: __a = [False] * 26 for char in input_str: if char.islower(): __a = True elif char.isupper(): __a = True return all(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: return len({char for char in input_str.lower() if char.isalpha()} ) == 26 def lowercase ( ) -> None: from timeit import timeit __a = '''from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest''' print(timeit('''is_pangram()''' , setup=lowerCAmelCase__ ) ) print(timeit('''is_pangram_faster()''' , setup=lowerCAmelCase__ ) ) print(timeit('''is_pangram_fastest()''' , setup=lowerCAmelCase__ ) ) # 5.348480500048026, 2.6477354579837993, 1.8470395830227062 # 5.036091582966037, 2.644472333951853, 1.8869528750656173 if __name__ == "__main__": import doctest doctest.testmod() benchmark()
45
1
"""simple docstring""" import argparse import json import os import fairseq import torch from torch import nn from transformers import ( SpeechaTextaConfig, SpeechaTextaForCausalLM, SpeechaTextaTokenizer, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) lowercase_ = { "post_extract_proj": "feature_projection.projection", "encoder.pos_conv.0": "encoder.pos_conv_embed.conv", "self_attn.k_proj": "encoder.layers.*.attention.k_proj", "self_attn.v_proj": "encoder.layers.*.attention.v_proj", "self_attn.q_proj": "encoder.layers.*.attention.q_proj", "self_attn.out_proj": "encoder.layers.*.attention.out_proj", "self_attn_layer_norm": "encoder.layers.*.layer_norm", "fc1": "encoder.layers.*.feed_forward.intermediate_dense", "fc2": "encoder.layers.*.feed_forward.output_dense", "final_layer_norm": "encoder.layers.*.final_layer_norm", "encoder.layer_norm": "encoder.layer_norm", "w2v_model.layer_norm": "feature_projection.layer_norm", "quantizer.weight_proj": "quantizer.weight_proj", "quantizer.vars": "quantizer.codevectors", "project_q": "project_q", "final_proj": "project_hid", "w2v_encoder.proj": "lm_head", "mask_emb": "masked_spec_embed", } lowercase_ = [ "lm_head", "quantizer.weight_proj", "quantizer.codevectors", "project_q", "project_hid", ] def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : int ) -> Union[str, Any]: for attribute in key.split('''.''' ): __a = getattr(lowerCAmelCase__ , lowerCAmelCase__ ) if weight_type is not None: __a = getattr(lowerCAmelCase__ , lowerCAmelCase__ ).shape else: __a = hf_pointer.shape assert hf_shape == value.shape, ( f'''Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be''' f''' {value.shape} for {full_name}''' ) if weight_type == "weight": __a = value elif weight_type == "weight_g": __a = value elif weight_type == "weight_v": __a = value elif weight_type == "bias": __a = value else: __a = value logger.info(f'''{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.''' ) def lowercase ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : int ) -> int: __a = [] __a = fairseq_model.state_dict() __a = hf_model.feature_extractor # if encoder has different dim to decoder -> use proj_weight __a = None for name, value in fairseq_dict.items(): __a = False if "conv_layers" in name: load_conv_layer( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , hf_model.config.feat_extract_norm == '''group''' , ) __a = True elif name.split('''.''' )[0] == "proj": __a = fairseq_model.proj __a = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split('''w2v_model.''' )[-1] == name.split('''.''' )[0]: __a = True if "*" in mapped_key: __a = name.split(lowerCAmelCase__ )[0].split('''.''' )[-2] __a = mapped_key.replace('''*''' , lowerCAmelCase__ ) if "weight_g" in name: __a = '''weight_g''' elif "weight_v" in name: __a = '''weight_v''' elif "bias" in name: __a = '''bias''' elif "weight" in name: __a = '''weight''' else: __a = None set_recursively(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) continue if not is_used: unused_weights.append(lowerCAmelCase__ ) logger.warning(f'''Unused weights: {unused_weights}''' ) return proj_weight def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : str , lowerCAmelCase__ : Any ) -> str: __a = full_name.split('''conv_layers.''' )[-1] __a = name.split('''.''' ) __a = int(items[0] ) __a = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) __a = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) __a = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f'''{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was''' " found." ) __a = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.''' ) __a = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : int ) -> List[str]: __a , __a = emb.weight.shape __a = nn.Linear(lowerCAmelCase__ , lowerCAmelCase__ , bias=lowerCAmelCase__ ) __a = emb.weight.data return lin_layer def lowercase ( lowerCAmelCase__ : Optional[Any] ) -> Tuple: with open(lowerCAmelCase__ , '''r''' , encoding='''utf-8''' ) as f: __a = f.readlines() __a = [line.split(''' ''' )[0] for line in lines] __a = len(lowerCAmelCase__ ) __a = { '''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3, } vocab_dict.update(dict(zip(lowerCAmelCase__ , range(4 , num_words + 4 ) ) ) ) return vocab_dict @torch.no_grad() def lowercase ( lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any] , ) -> Union[str, Any]: __a = WavaVecaConfig.from_pretrained(lowerCAmelCase__ ) __a = SpeechaTextaConfig.from_pretrained( lowerCAmelCase__ , vocab_size=lowerCAmelCase__ , decoder_layers=lowerCAmelCase__ , do_stable_layer_norm=lowerCAmelCase__ ) __a = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=lowerCAmelCase__ , return_attention_mask=lowerCAmelCase__ , ) __a , __a , __a = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'''data''': '''/'''.join(dict_path.split('''/''' )[:-1] )} ) __a = model[0].eval() # set weights for wav2vec2 encoder __a = WavaVecaModel(lowerCAmelCase__ ) __a = recursively_load_weights_wavaveca(model.encoder , lowerCAmelCase__ ) __a = SpeechaTextaForCausalLM(lowerCAmelCase__ ) __a , __a = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=lowerCAmelCase__ ) # set output linear layer unexpected_keys.remove('''embed_out''' ) __a = nn.Parameter(model.decoder.embed_out.detach() ) # layer norm is init to identity matrix so leaving it is fine logger.warning(f'''The following keys are missing when loading the decoder weights: {missing_keys}''' ) logger.warning(f'''The following keys are unexpected when loading the decoder weights: {unexpected_keys}''' ) __a = SpeechEncoderDecoderModel(encoder=lowerCAmelCase__ , decoder=lowerCAmelCase__ ) __a = False # add projection layer __a = nn.Parameter(projection_layer.weight ) __a = nn.Parameter(projection_layer.bias ) __a = create_vocab_dict(lowerCAmelCase__ ) with open(os.path.join(lowerCAmelCase__ , '''vocab.json''' ) , '''w''' ) as fp: json.dump(lowerCAmelCase__ , lowerCAmelCase__ ) __a = SpeechaTextaTokenizer(os.path.join(lowerCAmelCase__ , '''vocab.json''' ) ) tokenizer.save_pretrained(lowerCAmelCase__ ) __a = hf_wavavec.config.to_dict() __a = tokenizer.pad_token_id __a = tokenizer.bos_token_id __a = tokenizer.eos_token_id __a = '''speech_to_text_2''' __a = '''wav2vec2''' __a = SpeechEncoderDecoderConfig.from_dict(lowerCAmelCase__ ) hf_wavavec.save_pretrained(lowerCAmelCase__ ) feature_extractor.save_pretrained(lowerCAmelCase__ ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint") parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model") parser.add_argument( "--encoder_config_path", default="facebook/wav2vec2-large-lv60", type=str, help="Path to hf encoder wav2vec2 checkpoint config", ) parser.add_argument( "--decoder_config_path", default="facebook/s2t-small-mustc-en-fr-st", type=str, help="Path to hf decoder s2t checkpoint config", ) parser.add_argument("--vocab_size", default=1_0_2_2_4, type=int, help="Vocab size of decoder") parser.add_argument("--num_decoder_layers", default=7, type=int, help="Number of decoder layers") lowercase_ = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, vocab_size=args.vocab_size, num_decoder_layers=args.num_decoder_layers, )
45
"""simple docstring""" import gc import unittest from transformers import CTRLConfig, is_torch_available from transformers.testing_utils import require_torch, slow, 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 ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, ) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a=14 , _a=7 , _a=True , _a=True , _a=True , _a=True , _a=True , _a=99 , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=3 , _a=4 , _a=None , ): __a = parent __a = batch_size __a = seq_length __a = is_training __a = use_token_type_ids __a = use_input_mask __a = use_labels __a = use_mc_token_ids __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = type_sequence_label_size __a = initializer_range __a = num_labels __a = num_choices __a = scope __a = self.vocab_size - 1 def __UpperCAmelCase ( self ): __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a = None if self.use_input_mask: __a = random_attention_mask([self.batch_size, self.seq_length] ) __a = None if self.use_token_type_ids: __a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __a = None if self.use_mc_token_ids: __a = ids_tensor([self.batch_size, self.num_choices] , self.seq_length ) __a = None __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __a = ids_tensor([self.batch_size] , self.num_choices ) __a = self.get_config() __a = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, token_type_ids, mc_token_ids, sequence_labels, token_labels, choice_labels, ) def __UpperCAmelCase ( self ): return CTRLConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , *_a ): __a = CTRLModel(config=_a ) model.to(_a ) model.eval() model(_a , token_type_ids=_a , head_mask=_a ) model(_a , token_type_ids=_a ) __a = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(len(result.past_key_values ) , config.n_layer ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , *_a ): __a = CTRLLMHeadModel(_a ) model.to(_a ) model.eval() __a = model(_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = config_and_inputs __a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask} return config, inputs_dict def __UpperCAmelCase ( self , _a , _a , _a , _a , *_a ): __a = self.num_labels __a = CTRLForSequenceClassification(_a ) model.to(_a ) model.eval() __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = model(_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) @require_torch class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else () __UpperCAmelCase : Union[str, Any] = (CTRLLMHeadModel,) if is_torch_available() else () __UpperCAmelCase : Union[str, Any] = ( { 'feature-extraction': CTRLModel, 'text-classification': CTRLForSequenceClassification, 'text-generation': CTRLLMHeadModel, 'zero-shot': CTRLForSequenceClassification, } if is_torch_available() else {} ) __UpperCAmelCase : Optional[Any] = True __UpperCAmelCase : List[Any] = False __UpperCAmelCase : str = False def __UpperCAmelCase ( self , _a , _a , _a , _a , _a ): if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny # config could not be created. return True return False def __UpperCAmelCase ( self ): __a = CTRLModelTester(self ) __a = ConfigTester(self , config_class=_a , n_embd=37 ) def __UpperCAmelCase ( self ): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_ctrl_model(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*_a ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __UpperCAmelCase ( self ): pass @slow def __UpperCAmelCase ( self ): for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = CTRLModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip('''The model doesn\'t support left padding''' ) # and it's not used enough to be worth fixing :) def __UpperCAmelCase ( self ): pass @require_torch class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() @slow def __UpperCAmelCase ( self ): __a = CTRLLMHeadModel.from_pretrained('''ctrl''' ) model.to(_a ) __a = torch.tensor( [[11_859, 0, 1_611, 8]] , dtype=torch.long , device=_a ) # Legal the president is __a = [ 11_859, 0, 1_611, 8, 5, 150, 26_449, 2, 19, 348, 469, 3, 2_595, 48, 20_740, 246_533, 246_533, 19, 30, 5, ] # Legal the president is a good guy and I don't want to lose my job. \n \n I have a __a = model.generate(_a , do_sample=_a ) self.assertListEqual(output_ids[0].tolist() , _a )
45
1
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _a , _a=7 , _a=3 , _a=18 , _a=30 , _a=400 , _a=True , _a=None , _a=True , ): __a = size if size is not None else {'''height''': 18, '''width''': 18} __a = parent __a = batch_size __a = num_channels __a = image_size __a = min_resolution __a = max_resolution __a = do_resize __a = size __a = apply_ocr def __UpperCAmelCase ( self ): return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = LayoutLMvaImageProcessor if is_pytesseract_available() else None def __UpperCAmelCase ( self ): __a = LayoutLMvaImageProcessingTester(self ) @property def __UpperCAmelCase ( self ): return self.image_processor_tester.prepare_image_processor_dict() def __UpperCAmelCase ( self ): __a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , '''do_resize''' ) ) self.assertTrue(hasattr(_a , '''size''' ) ) self.assertTrue(hasattr(_a , '''apply_ocr''' ) ) def __UpperCAmelCase ( self ): __a = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) __a = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): # Initialize image_processing __a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __a = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input __a = image_processing(image_inputs[0] , return_tensors='''pt''' ) self.assertEqual( encoding.pixel_values.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) self.assertIsInstance(encoding.words , _a ) self.assertIsInstance(encoding.boxes , _a ) # Test batched __a = image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def __UpperCAmelCase ( self ): # Initialize image_processing __a = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __a = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input __a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a = image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def __UpperCAmelCase ( self ): # Initialize image_processing __a = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __a = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input __a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a = image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def __UpperCAmelCase ( self ): # with apply_OCR = True __a = LayoutLMvaImageProcessor() from datasets import load_dataset __a = load_dataset('''hf-internal-testing/fixtures_docvqa''' , split='''test''' ) __a = Image.open(ds[0]['''file'''] ).convert('''RGB''' ) __a = image_processing(_a , return_tensors='''pt''' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) , len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 __a = [['''11:14''', '''to''', '''11:39''', '''a.m''', '''11:39''', '''to''', '''11:44''', '''a.m.''', '''11:44''', '''a.m.''', '''to''', '''12:25''', '''p.m.''', '''12:25''', '''to''', '''12:58''', '''p.m.''', '''12:58''', '''to''', '''4:00''', '''p.m.''', '''2:00''', '''to''', '''5:00''', '''p.m.''', '''Coffee''', '''Break''', '''Coffee''', '''will''', '''be''', '''served''', '''for''', '''men''', '''and''', '''women''', '''in''', '''the''', '''lobby''', '''adjacent''', '''to''', '''exhibit''', '''area.''', '''Please''', '''move''', '''into''', '''exhibit''', '''area.''', '''(Exhibits''', '''Open)''', '''TRRF''', '''GENERAL''', '''SESSION''', '''(PART''', '''|)''', '''Presiding:''', '''Lee''', '''A.''', '''Waller''', '''TRRF''', '''Vice''', '''President''', '''“Introductory''', '''Remarks”''', '''Lee''', '''A.''', '''Waller,''', '''TRRF''', '''Vice''', '''Presi-''', '''dent''', '''Individual''', '''Interviews''', '''with''', '''TRRF''', '''Public''', '''Board''', '''Members''', '''and''', '''Sci-''', '''entific''', '''Advisory''', '''Council''', '''Mem-''', '''bers''', '''Conducted''', '''by''', '''TRRF''', '''Treasurer''', '''Philip''', '''G.''', '''Kuehn''', '''to''', '''get''', '''answers''', '''which''', '''the''', '''public''', '''refrigerated''', '''warehousing''', '''industry''', '''is''', '''looking''', '''for.''', '''Plus''', '''questions''', '''from''', '''the''', '''floor.''', '''Dr.''', '''Emil''', '''M.''', '''Mrak,''', '''University''', '''of''', '''Cal-''', '''ifornia,''', '''Chairman,''', '''TRRF''', '''Board;''', '''Sam''', '''R.''', '''Cecil,''', '''University''', '''of''', '''Georgia''', '''College''', '''of''', '''Agriculture;''', '''Dr.''', '''Stanley''', '''Charm,''', '''Tufts''', '''University''', '''School''', '''of''', '''Medicine;''', '''Dr.''', '''Robert''', '''H.''', '''Cotton,''', '''ITT''', '''Continental''', '''Baking''', '''Company;''', '''Dr.''', '''Owen''', '''Fennema,''', '''University''', '''of''', '''Wis-''', '''consin;''', '''Dr.''', '''Robert''', '''E.''', '''Hardenburg,''', '''USDA.''', '''Questions''', '''and''', '''Answers''', '''Exhibits''', '''Open''', '''Capt.''', '''Jack''', '''Stoney''', '''Room''', '''TRRF''', '''Scientific''', '''Advisory''', '''Council''', '''Meeting''', '''Ballroom''', '''Foyer''']] # noqa: E231 __a = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words , _a ) self.assertListEqual(encoding.boxes , _a ) # with apply_OCR = False __a = LayoutLMvaImageProcessor(apply_ocr=_a ) __a = image_processing(_a , return_tensors='''pt''' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) )
45
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase_ = {"configuration_xglm": ["XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XGLMConfig"]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["XGLMTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["XGLMTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "XGLM_PRETRAINED_MODEL_ARCHIVE_LIST", "XGLMForCausalLM", "XGLMModel", "XGLMPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "FlaxXGLMForCausalLM", "FlaxXGLMModel", "FlaxXGLMPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST", "TFXGLMForCausalLM", "TFXGLMModel", "TFXGLMPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure)
45
1
"""simple docstring""" import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = tempfile.mkdtemp() __a = 8 # DPR tok __a = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] __a = os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(_a , exist_ok=_a ) __a = os.path.join(_a , DPR_VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) # BART tok __a = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] __a = dict(zip(_a , range(len(_a ) ) ) ) __a = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] __a = {'''unk_token''': '''<unk>'''} __a = os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(_a , exist_ok=_a ) __a = os.path.join(_a , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) __a = os.path.join(_a , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(_a ) ) def __UpperCAmelCase ( self ): return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def __UpperCAmelCase ( self ): return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def __UpperCAmelCase ( self ): shutil.rmtree(self.tmpdirname ) @require_tokenizers def __UpperCAmelCase ( self ): __a = os.path.join(self.tmpdirname , '''rag_tokenizer''' ) __a = RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) __a = RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(_a ) rag_tokenizer.save_pretrained(_a ) __a = RagTokenizer.from_pretrained(_a , config=_a ) self.assertIsInstance(new_rag_tokenizer.question_encoder , _a ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , _a ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def __UpperCAmelCase ( self ): __a = RagTokenizer.from_pretrained('''facebook/rag-token-nq''' ) __a = [ '''who got the first nobel prize in physics''', '''when is the next deadpool movie being released''', '''which mode is used for short wave broadcast service''', '''who is the owner of reading football club''', '''when is the next scandal episode coming out''', '''when is the last time the philadelphia won the superbowl''', '''what is the most current adobe flash player version''', '''how many episodes are there in dragon ball z''', '''what is the first step in the evolution of the eye''', '''where is gall bladder situated in human body''', '''what is the main mineral in lithium batteries''', '''who is the president of usa right now''', '''where do the greasers live in the outsiders''', '''panda is a national animal of which country''', '''what is the name of manchester united stadium''', ] __a = tokenizer(_a ) self.assertIsNotNone(_a ) @slow def __UpperCAmelCase ( self ): __a = RagTokenizer.from_pretrained('''facebook/rag-sequence-nq''' ) __a = [ '''who got the first nobel prize in physics''', '''when is the next deadpool movie being released''', '''which mode is used for short wave broadcast service''', '''who is the owner of reading football club''', '''when is the next scandal episode coming out''', '''when is the last time the philadelphia won the superbowl''', '''what is the most current adobe flash player version''', '''how many episodes are there in dragon ball z''', '''what is the first step in the evolution of the eye''', '''where is gall bladder situated in human body''', '''what is the main mineral in lithium batteries''', '''who is the president of usa right now''', '''where do the greasers live in the outsiders''', '''panda is a national animal of which country''', '''what is the name of manchester united stadium''', ] __a = tokenizer(_a ) self.assertIsNotNone(_a )
45
"""simple docstring""" import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm lowercase_ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex lowercase_ = 1_0 lowercase_ = 2_5_6 def lowercase ( lowerCAmelCase__ : List[str] ) -> Optional[MinHash]: if len(lowerCAmelCase__ ) < MIN_NUM_TOKENS: return None __a = MinHash(num_perm=lowerCAmelCase__ ) for token in set(lowerCAmelCase__ ): min_hash.update(token.encode() ) return min_hash def lowercase ( lowerCAmelCase__ : str ) -> Set[str]: return {t for t in NON_ALPHA.split(lowerCAmelCase__ ) if len(t.strip() ) > 0} class __lowerCAmelCase : '''simple docstring''' def __init__( self , *, _a = 0.85 , ): __a = duplication_jaccard_threshold __a = NUM_PERM __a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) __a = defaultdict(_a ) def __UpperCAmelCase ( self , _a , _a ): __a = self._index.query(_a ) if code_key in self._index.keys: print(f'''Duplicate key {code_key}''' ) return self._index.insert(_a , _a ) if len(_a ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(_a ) break else: self._duplicate_clusters[close_duplicates[0]].add(_a ) def __UpperCAmelCase ( self ): __a = [] for base, duplicates in self._duplicate_clusters.items(): __a = [base] + list(_a ) # reformat the cluster to be a list of dict __a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(_a ) return duplicate_clusters def __UpperCAmelCase ( self , _a ): __a = self.get_duplicate_clusters() with open(_a , '''w''' ) as f: json.dump(_a , _a ) def lowercase ( lowerCAmelCase__ : List[str] ) -> int: __a , __a = element __a = get_min_hash([t for t in NON_ALPHA.split(data['''content'''] ) if len(t.strip() ) > 0] ) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def lowercase ( lowerCAmelCase__ : Type[Dataset] ) -> str: with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(lowerCAmelCase__ , max_queue_size=10000 ) , chunksize=100 , ): if data is not None: yield data def lowercase ( lowerCAmelCase__ : Type[Dataset] , lowerCAmelCase__ : float ) -> Dict: __a = DuplicationIndex(duplication_jaccard_threshold=lowerCAmelCase__ ) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(lowerCAmelCase__ ) ) , max_queue_size=100 ) ): di.add(lowerCAmelCase__ , lowerCAmelCase__ ) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : str ) -> float: __a = get_tokens(lowerCAmelCase__ ) __a = get_tokens(lowerCAmelCase__ ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) lowercase_ = None def lowercase ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Union[str, Any] ) -> Any: __a = [] for elementa in cluster: __a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: __a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(lowerCAmelCase__ , lowerCAmelCase__ ) >= jaccard_threshold: elementa["copies"] += 1 break else: __a = 1 extremes.append(lowerCAmelCase__ ) return extremes def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[int] ) -> Optional[int]: global _shared_dataset __a = dataset __a = [] __a = partial(_find_cluster_extremes_shared , jaccard_threshold=lowerCAmelCase__ ) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( lowerCAmelCase__ , lowerCAmelCase__ , ) , total=len(lowerCAmelCase__ ) , ): extremes_list.append(lowerCAmelCase__ ) return extremes_list def lowercase ( lowerCAmelCase__ : Type[Dataset] , lowerCAmelCase__ : float = 0.85 ) -> Tuple[Type[Dataset], List[List[Dict]]]: __a = make_duplicate_clusters(lowerCAmelCase__ , lowerCAmelCase__ ) __a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} __a = {} __a = find_extremes(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) for extremes in extremes_clusters: for element in extremes: __a = element __a = duplicate_indices - set(extreme_dict.keys() ) __a = dataset.filter(lambda lowerCAmelCase__ , lowerCAmelCase__ : idx not in remove_indices , with_indices=lowerCAmelCase__ ) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: __a = element['''base_index'''] in extreme_dict if element["is_extreme"]: __a = extreme_dict[element['''base_index''']]['''copies'''] print(f'''Original dataset size: {len(lowerCAmelCase__ )}''' ) print(f'''Number of duplicate clusters: {len(lowerCAmelCase__ )}''' ) print(f'''Files in duplicate cluster: {len(lowerCAmelCase__ )}''' ) print(f'''Unique files in duplicate cluster: {len(lowerCAmelCase__ )}''' ) print(f'''Filtered dataset size: {len(lowerCAmelCase__ )}''' ) return ds_filter, duplicate_clusters
45
1
"""simple docstring""" import json import os import tempfile import unittest import numpy as np from datasets import load_dataset from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ImageGPTImageProcessor class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _a , _a=7 , _a=3 , _a=18 , _a=30 , _a=400 , _a=True , _a=None , _a=True , ): __a = size if size is not None else {'''height''': 18, '''width''': 18} __a = parent __a = batch_size __a = num_channels __a = image_size __a = min_resolution __a = max_resolution __a = do_resize __a = size __a = do_normalize def __UpperCAmelCase ( self ): return { # here we create 2 clusters for the sake of simplicity "clusters": np.asarray( [ [0.8866_4436_3403_3203, 0.6618_8293_6954_4983, 0.3891_7464_0178_6804], [-0.6042_5591_4688_1104, -0.0_2295_0088_6052_8469, 0.5423_7973_6900_3296], ] ), "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, } @require_torch @require_vision class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : List[Any] = ImageGPTImageProcessor if is_vision_available() else None def __UpperCAmelCase ( self ): __a = ImageGPTImageProcessingTester(self ) @property def __UpperCAmelCase ( self ): return self.image_processor_tester.prepare_image_processor_dict() def __UpperCAmelCase ( self ): __a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , '''clusters''' ) ) self.assertTrue(hasattr(_a , '''do_resize''' ) ) self.assertTrue(hasattr(_a , '''size''' ) ) self.assertTrue(hasattr(_a , '''do_normalize''' ) ) def __UpperCAmelCase ( self ): __a = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) __a = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def __UpperCAmelCase ( self ): __a = self.image_processing_class(**self.image_processor_dict ) __a = json.loads(image_processor.to_json_string() ) for key, value in self.image_processor_dict.items(): if key == "clusters": self.assertTrue(np.array_equal(_a , obj[key] ) ) else: self.assertEqual(obj[key] , _a ) def __UpperCAmelCase ( self ): __a = self.image_processing_class(**self.image_processor_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __a = os.path.join(_a , '''image_processor.json''' ) image_processor_first.to_json_file(_a ) __a = self.image_processing_class.from_json_file(_a ).to_dict() __a = image_processor_first.to_dict() for key, value in image_processor_first.items(): if key == "clusters": self.assertTrue(np.array_equal(_a , image_processor_second[key] ) ) else: self.assertEqual(image_processor_first[key] , _a ) def __UpperCAmelCase ( self ): __a = self.image_processing_class(**self.image_processor_dict ) with tempfile.TemporaryDirectory() as tmpdirname: image_processor_first.save_pretrained(_a ) __a = self.image_processing_class.from_pretrained(_a ).to_dict() __a = image_processor_first.to_dict() for key, value in image_processor_first.items(): if key == "clusters": self.assertTrue(np.array_equal(_a , image_processor_second[key] ) ) else: self.assertEqual(image_processor_first[key] , _a ) @unittest.skip('''ImageGPT requires clusters at initialization''' ) def __UpperCAmelCase ( self ): pass def lowercase ( ) -> Any: __a = load_dataset('''hf-internal-testing/fixtures_image_utils''' , split='''test''' ) __a = Image.open(dataset[4]['''file'''] ) __a = Image.open(dataset[5]['''file'''] ) __a = [imagea, imagea] return images @require_vision @require_torch class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def __UpperCAmelCase ( self ): __a = ImageGPTImageProcessor.from_pretrained('''openai/imagegpt-small''' ) __a = prepare_images() # test non-batched __a = image_processing(images[0] , return_tensors='''pt''' ) self.assertIsInstance(encoding.input_ids , torch.LongTensor ) self.assertEqual(encoding.input_ids.shape , (1, 1_024) ) __a = [306, 191, 191] self.assertEqual(encoding.input_ids[0, :3].tolist() , _a ) # test batched __a = image_processing(_a , return_tensors='''pt''' ) self.assertIsInstance(encoding.input_ids , torch.LongTensor ) self.assertEqual(encoding.input_ids.shape , (2, 1_024) ) __a = [303, 13, 13] self.assertEqual(encoding.input_ids[1, -3:].tolist() , _a )
45
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase_ = { "configuration_roformer": ["ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "RoFormerConfig", "RoFormerOnnxConfig"], "tokenization_roformer": ["RoFormerTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["RoFormerTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "RoFormerForCausalLM", "RoFormerForMaskedLM", "RoFormerForMultipleChoice", "RoFormerForQuestionAnswering", "RoFormerForSequenceClassification", "RoFormerForTokenClassification", "RoFormerLayer", "RoFormerModel", "RoFormerPreTrainedModel", "load_tf_weights_in_roformer", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFRoFormerForCausalLM", "TFRoFormerForMaskedLM", "TFRoFormerForMultipleChoice", "TFRoFormerForQuestionAnswering", "TFRoFormerForSequenceClassification", "TFRoFormerForTokenClassification", "TFRoFormerLayer", "TFRoFormerModel", "TFRoFormerPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "FlaxRoFormerForMaskedLM", "FlaxRoFormerForMultipleChoice", "FlaxRoFormerForQuestionAnswering", "FlaxRoFormerForSequenceClassification", "FlaxRoFormerForTokenClassification", "FlaxRoFormerModel", "FlaxRoFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
1
"""simple docstring""" import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[Any] = ['image_processor', 'tokenizer'] __UpperCAmelCase : Optional[Any] = 'ViTImageProcessor' __UpperCAmelCase : str = ('CLIPTokenizer', 'CLIPTokenizerFast') def __init__( self , _a=None , _a=None , **_a ): __a = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , _a , ) __a = kwargs.pop('''feature_extractor''' ) __a = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(_a , _a ) def __call__( self , _a=None , _a=None , _a=None , _a=None , **_a ): if text is None and visual_prompt is None and images is None: raise ValueError('''You have to specify either text, visual prompt or images.''' ) if text is not None and visual_prompt is not None: raise ValueError('''You have to specify exactly one type of prompt. Either text or visual prompt.''' ) if text is not None: __a = self.tokenizer(_a , return_tensors=_a , **_a ) if visual_prompt is not None: __a = self.image_processor(_a , return_tensors=_a , **_a ) if images is not None: __a = self.image_processor(_a , return_tensors=_a , **_a ) if visual_prompt is not None and images is not None: __a = { '''pixel_values''': image_features.pixel_values, '''conditional_pixel_values''': prompt_features.pixel_values, } return encoding elif text is not None and images is not None: __a = image_features.pixel_values return encoding elif text is not None: return encoding elif visual_prompt is not None: __a = { '''conditional_pixel_values''': prompt_features.pixel_values, } return encoding else: return BatchEncoding(data=dict(**_a ) , tensor_type=_a ) def __UpperCAmelCase ( self , *_a , **_a ): return self.tokenizer.batch_decode(*_a , **_a ) def __UpperCAmelCase ( self , *_a , **_a ): return self.tokenizer.decode(*_a , **_a ) @property def __UpperCAmelCase ( self ): warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , _a , ) return self.image_processor_class @property def __UpperCAmelCase ( self ): warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , _a , ) return self.image_processor
45
"""simple docstring""" import warnings from ..trainer import Trainer from ..utils import logging lowercase_ = logging.get_logger(__name__) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , _a=None , **_a ): warnings.warn( '''`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` ''' '''instead.''' , _a , ) super().__init__(args=_a , **_a )
45
1
"""simple docstring""" import argparse import gc import json import os import re import torch from huggingface_hub import hf_hub_download from transformers import AutoModelForCausalLM, AutoTokenizer, PreTrainedTokenizerFast, RwkvConfig from transformers.modeling_utils import WEIGHTS_INDEX_NAME, shard_checkpoint lowercase_ = { "169M": 1_2, "430M": 2_4, "1B5": 2_4, "3B": 3_2, "7B": 3_2, "14B": 4_0, } lowercase_ = { "169M": 7_6_8, "430M": 1_0_2_4, "1B5": 2_0_4_8, "3B": 2_5_6_0, "7B": 4_0_9_6, "14B": 5_1_2_0, } def lowercase ( lowerCAmelCase__ : List[str] ) -> Optional[int]: __a = list(state_dict.keys() ) for name in state_dict_keys: __a = state_dict.pop(lowerCAmelCase__ ) # emb -> embedding if name.startswith('''emb.''' ): __a = name.replace('''emb.''' , '''embeddings.''' ) # ln_0 -> pre_ln (only present at block 0) if name.startswith('''blocks.0.ln0''' ): __a = name.replace('''blocks.0.ln0''' , '''blocks.0.pre_ln''' ) # att -> attention __a = re.sub(r'''blocks\.(\d+)\.att''' , r'''blocks.\1.attention''' , lowerCAmelCase__ ) # ffn -> feed_forward __a = re.sub(r'''blocks\.(\d+)\.ffn''' , r'''blocks.\1.feed_forward''' , lowerCAmelCase__ ) # time_mix_k -> time_mix_key and reshape if name.endswith('''.time_mix_k''' ): __a = name.replace('''.time_mix_k''' , '''.time_mix_key''' ) # time_mix_v -> time_mix_value and reshape if name.endswith('''.time_mix_v''' ): __a = name.replace('''.time_mix_v''' , '''.time_mix_value''' ) # time_mix_r -> time_mix_key and reshape if name.endswith('''.time_mix_r''' ): __a = name.replace('''.time_mix_r''' , '''.time_mix_receptance''' ) if name != "head.weight": __a = '''rwkv.''' + name __a = weight return state_dict def lowercase ( lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : Union[str, Any]=None , lowerCAmelCase__ : List[Any]=False , lowerCAmelCase__ : str=None ) -> Union[str, Any]: # 1. If possible, build the tokenizer. if tokenizer_file is None: print('''No `--tokenizer_file` provided, we will use the default tokenizer.''' ) __a = 50277 __a = AutoTokenizer.from_pretrained('''EleutherAI/gpt-neox-20b''' ) else: __a = PreTrainedTokenizerFast(tokenizer_file=lowerCAmelCase__ ) __a = len(lowerCAmelCase__ ) tokenizer.save_pretrained(lowerCAmelCase__ ) # 2. Build the config __a = list(NUM_HIDDEN_LAYERS_MAPPING.keys() ) if size is None: # Try to infer size from the checkpoint name for candidate in possible_sizes: if candidate in checkpoint_file: __a = candidate break if size is None: raise ValueError('''Could not infer the size, please provide it with the `--size` argument.''' ) if size not in possible_sizes: raise ValueError(f'''`size` should be one of {possible_sizes}, got {size}.''' ) __a = RwkvConfig( vocab_size=lowerCAmelCase__ , num_hidden_layers=NUM_HIDDEN_LAYERS_MAPPING[size] , hidden_size=HIDEN_SIZE_MAPPING[size] , ) config.save_pretrained(lowerCAmelCase__ ) # 3. Download model file then convert state_dict __a = hf_hub_download(lowerCAmelCase__ , lowerCAmelCase__ ) __a = torch.load(lowerCAmelCase__ , map_location='''cpu''' ) __a = convert_state_dict(lowerCAmelCase__ ) # 4. Split in shards and save __a , __a = shard_checkpoint(lowerCAmelCase__ ) for shard_file, shard in shards.items(): torch.save(lowerCAmelCase__ , os.path.join(lowerCAmelCase__ , lowerCAmelCase__ ) ) if index is not None: __a = os.path.join(lowerCAmelCase__ , lowerCAmelCase__ ) # Save the index as well with open(lowerCAmelCase__ , '''w''' , encoding='''utf-8''' ) as f: __a = json.dumps(lowerCAmelCase__ , indent=2 , sort_keys=lowerCAmelCase__ ) + '''\n''' f.write(lowerCAmelCase__ ) # 5. Clean up shards (for some reason the file PyTorch saves take the same space as the whole state_dict print( '''Cleaning up shards. This may error with an OOM error, it this is the case don\'t worry you still have converted the model.''' ) __a = list(shards.keys() ) del state_dict del shards gc.collect() for shard_file in shard_files: __a = torch.load(os.path.join(lowerCAmelCase__ , lowerCAmelCase__ ) ) torch.save({k: v.cpu().clone() for k, v in state_dict.items()} , os.path.join(lowerCAmelCase__ , lowerCAmelCase__ ) ) del state_dict gc.collect() if push_to_hub: if model_name is None: raise ValueError('''Please provide a `model_name` to push the model to the Hub.''' ) __a = AutoModelForCausalLM.from_pretrained(lowerCAmelCase__ ) model.push_to_hub(lowerCAmelCase__ , max_shard_size='''2GB''' ) tokenizer.push_to_hub(lowerCAmelCase__ ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--repo_id", default=None, type=str, required=True, help="Repo ID from which to pull the checkpoint." ) parser.add_argument( "--checkpoint_file", default=None, type=str, required=True, help="Name of the checkpoint file in the repo." ) parser.add_argument( "--output_dir", default=None, type=str, required=True, help="Where to save the converted model." ) parser.add_argument( "--tokenizer_file", default=None, type=str, help="Path to the tokenizer file to use (if not provided, only the model is converted).", ) parser.add_argument( "--size", default=None, type=str, help="Size of the model. Will be inferred from the `checkpoint_file` if not passed.", ) parser.add_argument( "--push_to_hub", action="store_true", help="Push to the Hub the converted model.", ) parser.add_argument( "--model_name", default=None, type=str, help="Name of the pushed model on the Hub, including the username / organization.", ) lowercase_ = parser.parse_args() convert_rmkv_checkpoint_to_hf_format( args.repo_id, args.checkpoint_file, args.output_dir, size=args.size, tokenizer_file=args.tokenizer_file, push_to_hub=args.push_to_hub, model_name=args.model_name, )
45
"""simple docstring""" import math def lowercase ( lowerCAmelCase__ : list , lowerCAmelCase__ : int ) -> int: __a = len(lowerCAmelCase__ ) __a = int(math.floor(math.sqrt(lowerCAmelCase__ ) ) ) __a = 0 while arr[min(lowerCAmelCase__ , lowerCAmelCase__ ) - 1] < x: __a = step step += int(math.floor(math.sqrt(lowerCAmelCase__ ) ) ) if prev >= n: return -1 while arr[prev] < x: __a = prev + 1 if prev == min(lowerCAmelCase__ , lowerCAmelCase__ ): return -1 if arr[prev] == x: return prev return -1 if __name__ == "__main__": lowercase_ = input("Enter numbers separated by a comma:\n").strip() lowercase_ = [int(item) for item in user_input.split(",")] lowercase_ = int(input("Enter the number to be searched:\n")) lowercase_ = jump_search(arr, x) if res == -1: print("Number not found!") else: print(F'''Number {x} is at index {res}''')
45
1
"""simple docstring""" from __future__ import annotations from math import pi def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> dict[str, float]: if (inductance, frequency, reactance).count(0 ) != 1: raise ValueError('''One and only one argument must be 0''' ) if inductance < 0: raise ValueError('''Inductance cannot be negative''' ) if frequency < 0: raise ValueError('''Frequency cannot be negative''' ) if reactance < 0: raise ValueError('''Inductive reactance cannot be negative''' ) if inductance == 0: return {"inductance": reactance / (2 * pi * frequency)} elif frequency == 0: return {"frequency": reactance / (2 * pi * inductance)} elif reactance == 0: return {"reactance": 2 * pi * frequency * inductance} else: raise ValueError('''Exactly one argument must be 0''' ) if __name__ == "__main__": import doctest doctest.testmod()
45
"""simple docstring""" lowercase_ = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] lowercase_ = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] lowercase_ = { 0: "Sunday", 1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", 5: "Friday", 6: "Saturday", } def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int ) -> str: assert len(str(lowerCAmelCase__ ) ) > 2, "year should be in YYYY format" assert 1 <= month <= 12, "month should be between 1 to 12" assert 1 <= day <= 31, "day should be between 1 to 31" # Doomsday algorithm: __a = year // 100 __a = (5 * (century % 4) + 2) % 7 __a = year % 100 __a = centurian % 12 __a = ( (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor ) % 7 __a = ( DOOMSDAY_NOT_LEAP[month - 1] if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) else DOOMSDAY_LEAP[month - 1] ) __a = (dooms_day + day - day_anchor) % 7 return WEEK_DAY_NAMES[week_day] if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowercase_ = logging.get_logger(__name__) lowercase_ = { "microsoft/focalnet-tiny": "https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json", } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Optional[int] = 'focalnet' def __init__( self , _a=224 , _a=4 , _a=3 , _a=96 , _a=False , _a=[192, 384, 768, 768] , _a=[2, 2, 6, 2] , _a=[2, 2, 2, 2] , _a=[3, 3, 3, 3] , _a="gelu" , _a=4.0 , _a=0.0 , _a=0.1 , _a=False , _a=1E-4 , _a=False , _a=False , _a=False , _a=0.02 , _a=1E-5 , _a=32 , _a=None , _a=None , **_a , ): super().__init__(**_a ) __a = image_size __a = patch_size __a = num_channels __a = embed_dim __a = use_conv_embed __a = hidden_sizes __a = depths __a = focal_levels __a = focal_windows __a = hidden_act __a = mlp_ratio __a = hidden_dropout_prob __a = drop_path_rate __a = use_layerscale __a = layerscale_value __a = use_post_layernorm __a = use_post_layernorm_in_modulation __a = normalize_modulator __a = initializer_range __a = layer_norm_eps __a = encoder_stride __a = ['''stem'''] + [f'''stage{idx}''' for idx in range(1 , len(self.depths ) + 1 )] __a , __a = get_aligned_output_features_output_indices( out_features=_a , out_indices=_a , stage_names=self.stage_names )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : list ) -> bool: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise ValueError('''Input series is not valid, valid series - [2, 4, 6]''' ) if len(lowerCAmelCase__ ) == 0: raise ValueError('''Input list must be a non empty list''' ) if len(lowerCAmelCase__ ) == 1: return True __a = series[1] - series[0] for index in range(len(lowerCAmelCase__ ) - 1 ): if series[index + 1] - series[index] != common_diff: return False return True def lowercase ( lowerCAmelCase__ : list ) -> float: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise ValueError('''Input series is not valid, valid series - [2, 4, 6]''' ) if len(lowerCAmelCase__ ) == 0: raise ValueError('''Input list must be a non empty list''' ) __a = 0 for val in series: answer += val return answer / len(lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" 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: lowercase_ = None lowercase_ = logging.get_logger(__name__) lowercase_ = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"} lowercase_ = { "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", }, } lowercase_ = { "albert-base-v1": 5_1_2, "albert-large-v1": 5_1_2, "albert-xlarge-v1": 5_1_2, "albert-xxlarge-v1": 5_1_2, "albert-base-v2": 5_1_2, "albert-large-v2": 5_1_2, "albert-xlarge-v2": 5_1_2, "albert-xxlarge-v2": 5_1_2, } lowercase_ = "▁" class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = VOCAB_FILES_NAMES __UpperCAmelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __UpperCAmelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCAmelCase : int = AlbertTokenizer def __init__( self , _a=None , _a=None , _a=True , _a=True , _a=False , _a="[CLS]" , _a="[SEP]" , _a="<unk>" , _a="[SEP]" , _a="<pad>" , _a="[CLS]" , _a="[MASK]" , **_a , ): # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. __a = ( AddedToken(_a , lstrip=_a , rstrip=_a , normalized=_a ) if isinstance(_a , _a ) else mask_token ) super().__init__( _a , tokenizer_file=_a , do_lower_case=_a , remove_space=_a , keep_accents=_a , bos_token=_a , eos_token=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , **_a , ) __a = do_lower_case __a = remove_space __a = keep_accents __a = vocab_file __a = False if not self.vocab_file else True def __UpperCAmelCase ( self , _a , _a = None ): __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 __UpperCAmelCase ( self , _a , _a = None ): __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 __UpperCAmelCase ( self , _a , _a = None ): if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(_a ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return __a = os.path.join( _a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ): copyfile(self.vocab_file , _a ) return (out_vocab_file,)
45
"""simple docstring""" from argparse import ArgumentParser, Namespace from ..utils import logging from . import BaseTransformersCLICommand def lowercase ( lowerCAmelCase__ : Namespace ) -> Tuple: return ConvertCommand( args.model_type , args.tf_checkpoint , args.pytorch_dump_output , args.config , args.finetuning_task_name ) lowercase_ = "\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n" class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' @staticmethod def __UpperCAmelCase ( _a ): __a = parser.add_parser( '''convert''' , help='''CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.''' , ) train_parser.add_argument('''--model_type''' , type=_a , required=_a , help='''Model\'s type.''' ) train_parser.add_argument( '''--tf_checkpoint''' , type=_a , required=_a , help='''TensorFlow checkpoint path or folder.''' ) train_parser.add_argument( '''--pytorch_dump_output''' , type=_a , required=_a , help='''Path to the PyTorch saved model output.''' ) train_parser.add_argument('''--config''' , type=_a , default='''''' , help='''Configuration file path or folder.''' ) train_parser.add_argument( '''--finetuning_task_name''' , type=_a , default=_a , help='''Optional fine-tuning task name if the TF model was a finetuned model.''' , ) train_parser.set_defaults(func=_a ) def __init__( self , _a , _a , _a , _a , _a , *_a , ): __a = logging.get_logger('''transformers-cli/converting''' ) self._logger.info(f'''Loading model {model_type}''' ) __a = model_type __a = tf_checkpoint __a = pytorch_dump_output __a = config __a = finetuning_task_name def __UpperCAmelCase ( self ): if self._model_type == "albert": try: from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "bert": try: from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "funnel": try: from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "t5": try: from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "gpt": from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import ( convert_openai_checkpoint_to_pytorch, ) convert_openai_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "transfo_xl": try: from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import ( convert_transfo_xl_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) if "ckpt" in self._tf_checkpoint.lower(): __a = self._tf_checkpoint __a = '''''' else: __a = self._tf_checkpoint __a = '''''' convert_transfo_xl_checkpoint_to_pytorch( _a , self._config , self._pytorch_dump_output , _a ) elif self._model_type == "gpt2": try: from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import ( convert_gpta_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "xlnet": try: from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import ( convert_xlnet_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_xlnet_checkpoint_to_pytorch( self._tf_checkpoint , self._config , self._pytorch_dump_output , self._finetuning_task_name ) elif self._model_type == "xlm": from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import ( convert_xlm_checkpoint_to_pytorch, ) convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "lxmert": from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import ( convert_lxmert_checkpoint_to_pytorch, ) convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "rembert": from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import ( convert_rembert_tf_checkpoint_to_pytorch, ) convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) else: raise ValueError( '''--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]''' )
45
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : list[list[int]] , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : set ) -> int: __a , __a = len(lowerCAmelCase__ ), len(grid[0] ) if ( min(lowerCAmelCase__ , lowerCAmelCase__ ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) __a = 0 count += depth_first_search(lowerCAmelCase__ , row + 1 , lowerCAmelCase__ , lowerCAmelCase__ ) count += depth_first_search(lowerCAmelCase__ , row - 1 , lowerCAmelCase__ , lowerCAmelCase__ ) count += depth_first_search(lowerCAmelCase__ , lowerCAmelCase__ , col + 1 , lowerCAmelCase__ ) count += depth_first_search(lowerCAmelCase__ , lowerCAmelCase__ , col - 1 , lowerCAmelCase__ ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
45
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "microsoft/beit-base-patch16-224-pt22k": ( "https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json" ), # See all BEiT models at https://huggingface.co/models?filter=beit } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = 'beit' def __init__( self , _a=8_192 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.0 , _a=0.0 , _a=0.02 , _a=1E-12 , _a=224 , _a=16 , _a=3 , _a=False , _a=False , _a=False , _a=False , _a=0.1 , _a=0.1 , _a=True , _a=[3, 5, 7, 11] , _a=[1, 2, 3, 6] , _a=True , _a=0.4 , _a=256 , _a=1 , _a=False , _a=255 , **_a , ): super().__init__(**_a ) __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 = initializer_range __a = layer_norm_eps __a = image_size __a = patch_size __a = num_channels __a = use_mask_token __a = use_absolute_position_embeddings __a = use_relative_position_bias __a = use_shared_relative_position_bias __a = layer_scale_init_value __a = drop_path_rate __a = use_mean_pooling # decode head attributes (semantic segmentation) __a = out_indices __a = pool_scales # auxiliary head attributes (semantic segmentation) __a = use_auxiliary_head __a = auxiliary_loss_weight __a = auxiliary_channels __a = auxiliary_num_convs __a = auxiliary_concat_input __a = semantic_loss_ignore_index class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[Any] = version.parse('1.11' ) @property def __UpperCAmelCase ( self ): return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __UpperCAmelCase ( self ): return 1E-4
45
1
"""simple docstring""" import socket def lowercase ( ) -> int: __a = socket.socket(socket.AF_INET , socket.SOCK_STREAM ) __a = socket.gethostname() __a = 12312 sock.connect((host, port) ) sock.send(b'''Hello server!''' ) with open('''Received_file''' , '''wb''' ) as out_file: print('''File opened''' ) print('''Receiving data...''' ) while True: __a = sock.recv(1024 ) if not data: break out_file.write(lowerCAmelCase__ ) print('''Successfully received the file''' ) sock.close() print('''Connection closed''' ) if __name__ == "__main__": main()
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str ) -> list: if n_term == "": return [] __a = [] for temp in range(int(lowerCAmelCase__ ) ): series.append(f'''1/{temp + 1}''' if series else '''1''' ) return series if __name__ == "__main__": lowercase_ = input("Enter the last number (nth term) of the Harmonic Series") print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n") print(harmonic_series(nth_term))
45
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Union[str, Any] ) -> List[str]: __a = 0 __a = len(lowerCAmelCase__ ) - 1 while left <= right: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None __a = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(lowerCAmelCase__ ): return None __a = sorted_collection[point] if current_item == item: return point else: if point < left: __a = left __a = point elif point > right: __a = right __a = point else: if item < current_item: __a = point - 1 else: __a = point + 1 return None def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Union[str, Any] ) -> List[str]: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None __a = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(lowerCAmelCase__ ): return None if sorted_collection[point] == item: return point elif point < left: return interpolation_search_by_recursion(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) elif point > right: return interpolation_search_by_recursion(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) else: if sorted_collection[point] > item: return interpolation_search_by_recursion( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , point - 1 ) else: return interpolation_search_by_recursion( lowerCAmelCase__ , lowerCAmelCase__ , point + 1 , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : int ) -> List[str]: if collection != sorted(lowerCAmelCase__ ): raise ValueError('''Collection must be ascending sorted''' ) return True if __name__ == "__main__": import sys lowercase_ = 0 if debug == 1: lowercase_ = [1_0, 3_0, 4_0, 4_5, 5_0, 6_6, 7_7, 9_3] try: __assert_sorted(collection) except ValueError: sys.exit("Sequence must be ascending sorted to apply interpolation search") lowercase_ = 6_7 lowercase_ = interpolation_search(collection, target) if result is not None: print(F'''{target} found at positions: {result}''') else: print("Not found")
45
"""simple docstring""" from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : List[str] __UpperCAmelCase : Optional[str] = None # Automatically constructed __UpperCAmelCase : ClassVar[str] = "dict" __UpperCAmelCase : ClassVar[Any] = None __UpperCAmelCase : str = field(default='Translation' , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __call__( self ): return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def __UpperCAmelCase ( self ): from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : Optional[List] = None __UpperCAmelCase : Optional[int] = None __UpperCAmelCase : Optional[str] = None # Automatically constructed __UpperCAmelCase : ClassVar[str] = "dict" __UpperCAmelCase : ClassVar[Any] = None __UpperCAmelCase : str = field(default='TranslationVariableLanguages' , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __UpperCAmelCase ( self ): __a = sorted(set(self.languages ) ) if self.languages else None __a = len(self.languages ) if self.languages else None def __call__( self ): return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def __UpperCAmelCase ( self , _a ): __a = set(self.languages ) if self.languages and set(_a ) - lang_set: raise ValueError( f'''Some languages in example ({', '.join(sorted(set(_a ) - lang_set ) )}) are not in valid set ({', '.join(_a )}).''' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. __a = [] for lang, text in translation_dict.items(): if isinstance(_a , _a ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. __a , __a = zip(*sorted(_a ) ) return {"language": languages, "translation": translations} def __UpperCAmelCase ( self ): from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
45
1
"""simple docstring""" import gc import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, CycleDiffusionPipeline, DDIMScheduler, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Optional[int] = CycleDiffusionPipeline __UpperCAmelCase : Any = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - { 'negative_prompt', 'height', 'width', 'negative_prompt_embeds', } __UpperCAmelCase : int = PipelineTesterMixin.required_optional_params - {'latents'} __UpperCAmelCase : Any = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({'source_prompt'} ) __UpperCAmelCase : Tuple = IMAGE_TO_IMAGE_IMAGE_PARAMS __UpperCAmelCase : int = IMAGE_TO_IMAGE_IMAGE_PARAMS def __UpperCAmelCase ( self ): torch.manual_seed(0 ) __a = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) __a = DDIMScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , num_train_timesteps=1_000 , clip_sample=_a , set_alpha_to_one=_a , ) torch.manual_seed(0 ) __a = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) __a = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) __a = CLIPTextModel(_a ) __a = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __a = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def __UpperCAmelCase ( self , _a , _a=0 ): __a = floats_tensor((1, 3, 32, 32) , rng=random.Random(_a ) ).to(_a ) __a = image / 2 + 0.5 if str(_a ).startswith('''mps''' ): __a = torch.manual_seed(_a ) else: __a = torch.Generator(device=_a ).manual_seed(_a ) __a = { '''prompt''': '''An astronaut riding an elephant''', '''source_prompt''': '''An astronaut riding a horse''', '''image''': image, '''generator''': generator, '''num_inference_steps''': 2, '''eta''': 0.1, '''strength''': 0.8, '''guidance_scale''': 3, '''source_guidance_scale''': 1, '''output_type''': '''numpy''', } return inputs def __UpperCAmelCase ( self ): __a = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a = self.get_dummy_components() __a = CycleDiffusionPipeline(**_a ) __a = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = pipe(**_a ) __a = output.images __a = images[0, -3:, -3:, -1] assert images.shape == (1, 32, 32, 3) __a = np.array([0.4459, 0.4943, 0.4544, 0.6643, 0.5474, 0.4327, 0.5701, 0.5959, 0.5179] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @unittest.skipIf(torch_device != '''cuda''' , '''This test requires a GPU''' ) def __UpperCAmelCase ( self ): __a = self.get_dummy_components() for name, module in components.items(): if hasattr(_a , '''half''' ): __a = module.half() __a = CycleDiffusionPipeline(**_a ) __a = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = pipe(**_a ) __a = output.images __a = images[0, -3:, -3:, -1] assert images.shape == (1, 32, 32, 3) __a = np.array([0.3506, 0.4543, 0.446, 0.4575, 0.5195, 0.4155, 0.5273, 0.518, 0.4116] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @skip_mps def __UpperCAmelCase ( self ): return super().test_save_load_local() @unittest.skip('''non-deterministic pipeline''' ) def __UpperCAmelCase ( self ): return super().test_inference_batch_single_identical() @skip_mps def __UpperCAmelCase ( self ): return super().test_dict_tuple_outputs_equivalent() @skip_mps def __UpperCAmelCase ( self ): return super().test_save_load_optional_components() @skip_mps def __UpperCAmelCase ( self ): return super().test_attention_slicing_forward_pass() @slow @require_torch_gpu class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self ): __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/cycle-diffusion/black_colored_car.png''' ) __a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/cycle-diffusion/blue_colored_car_fp16.npy''' ) __a = init_image.resize((512, 512) ) __a = '''CompVis/stable-diffusion-v1-4''' __a = DDIMScheduler.from_pretrained(_a , subfolder='''scheduler''' ) __a = CycleDiffusionPipeline.from_pretrained( _a , scheduler=_a , safety_checker=_a , torch_dtype=torch.floataa , revision='''fp16''' ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = '''A black colored car''' __a = '''A blue colored car''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , source_prompt=_a , image=_a , num_inference_steps=100 , eta=0.1 , strength=0.85 , guidance_scale=3 , source_guidance_scale=1 , generator=_a , output_type='''np''' , ) __a = output.images # the values aren't exactly equal, but the images look the same visually assert np.abs(image - expected_image ).max() < 5E-1 def __UpperCAmelCase ( self ): __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/cycle-diffusion/black_colored_car.png''' ) __a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/cycle-diffusion/blue_colored_car.npy''' ) __a = init_image.resize((512, 512) ) __a = '''CompVis/stable-diffusion-v1-4''' __a = DDIMScheduler.from_pretrained(_a , subfolder='''scheduler''' ) __a = CycleDiffusionPipeline.from_pretrained(_a , scheduler=_a , safety_checker=_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = '''A black colored car''' __a = '''A blue colored car''' __a = torch.manual_seed(0 ) __a = pipe( prompt=_a , source_prompt=_a , image=_a , num_inference_steps=100 , eta=0.1 , strength=0.85 , guidance_scale=3 , source_guidance_scale=1 , generator=_a , output_type='''np''' , ) __a = output.images assert np.abs(image - expected_image ).max() < 2E-2
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> List[Any]: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) if n == 0: return 0 __a = float('''-inf''' ) for i in range(1 , n + 1 ): __a = max( lowerCAmelCase__ , prices[i - 1] + naive_cut_rod_recursive(n - i , lowerCAmelCase__ ) ) return max_revue def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> List[str]: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) __a = [float('''-inf''' ) for _ in range(n + 1 )] return _top_down_cut_rod_recursive(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list , lowerCAmelCase__ : list ) -> Union[str, Any]: if max_rev[n] >= 0: return max_rev[n] elif n == 0: return 0 else: __a = float('''-inf''' ) for i in range(1 , n + 1 ): __a = max( lowerCAmelCase__ , prices[i - 1] + _top_down_cut_rod_recursive(n - i , lowerCAmelCase__ , lowerCAmelCase__ ) , ) __a = max_revenue return max_rev[n] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> Dict: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of # length 0. __a = [float('''-inf''' ) for _ in range(n + 1 )] __a = 0 for i in range(1 , n + 1 ): __a = max_rev[i] for j in range(1 , i + 1 ): __a = max(lowerCAmelCase__ , prices[j - 1] + max_rev[i - j] ) __a = max_revenue_i return max_rev[n] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> str: if n < 0: __a = f'''n must be greater than or equal to 0. Got n = {n}''' raise ValueError(lowerCAmelCase__ ) if n > len(lowerCAmelCase__ ): __a = ( '''Each integral piece of rod must have a corresponding price. ''' f'''Got n = {n} but length of prices = {len(lowerCAmelCase__ )}''' ) raise ValueError(lowerCAmelCase__ ) def lowercase ( ) -> int: __a = [6, 10, 12, 15, 20, 23] __a = len(lowerCAmelCase__ ) # the best revenue comes from cutting the rod into 6 pieces, each # of length 1 resulting in a revenue of 6 * 6 = 36. __a = 36 __a = top_down_cut_rod(lowerCAmelCase__ , lowerCAmelCase__ ) __a = bottom_up_cut_rod(lowerCAmelCase__ , lowerCAmelCase__ ) __a = naive_cut_rod_recursive(lowerCAmelCase__ , lowerCAmelCase__ ) assert expected_max_revenue == max_rev_top_down assert max_rev_top_down == max_rev_bottom_up assert max_rev_bottom_up == max_rev_naive if __name__ == "__main__": main()
45
1
"""simple docstring""" import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def lowercase ( lowerCAmelCase__ : Optional[Any] ) -> Optional[int]: __a = tmp_path / '''file.csv''' __a = textwrap.dedent( '''\ header1,header2 1,2 10,20 ''' ) with open(lowerCAmelCase__ , '''w''' ) as f: f.write(lowerCAmelCase__ ) return str(lowerCAmelCase__ ) @pytest.fixture def lowercase ( lowerCAmelCase__ : str ) -> Any: __a = tmp_path / '''malformed_file.csv''' __a = textwrap.dedent( '''\ header1,header2 1,2 10,20, ''' ) with open(lowerCAmelCase__ , '''w''' ) as f: f.write(lowerCAmelCase__ ) return str(lowerCAmelCase__ ) @pytest.fixture def lowercase ( lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Any ) -> List[str]: __a = tmp_path / '''csv_with_image.csv''' __a = textwrap.dedent( f'''\ image {image_file} ''' ) with open(lowerCAmelCase__ , '''w''' ) as f: f.write(lowerCAmelCase__ ) return str(lowerCAmelCase__ ) @pytest.fixture def lowercase ( lowerCAmelCase__ : Dict ) -> Union[str, Any]: __a = tmp_path / '''csv_with_label.csv''' __a = textwrap.dedent( '''\ label good bad good ''' ) with open(lowerCAmelCase__ , '''w''' ) as f: f.write(lowerCAmelCase__ ) return str(lowerCAmelCase__ ) @pytest.fixture def lowercase ( lowerCAmelCase__ : Optional[int] ) -> List[str]: __a = tmp_path / '''csv_with_int_list.csv''' __a = textwrap.dedent( '''\ int_list 1 2 3 4 5 6 7 8 9 ''' ) with open(lowerCAmelCase__ , '''w''' ) as f: f.write(lowerCAmelCase__ ) return str(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : Any ) -> Tuple: __a = Csv() __a = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(lowerCAmelCase__ , match='''Error tokenizing data''' ): for _ in generator: pass assert any( record.levelname == '''ERROR''' and '''Failed to read file''' in record.message and os.path.basename(lowerCAmelCase__ ) in record.message for record in caplog.records ) @require_pil def lowercase ( lowerCAmelCase__ : Optional[int] ) -> Union[str, Any]: with open(lowerCAmelCase__ , encoding='''utf-8''' ) as f: __a = f.read().splitlines()[1] __a = Csv(encoding='''utf-8''' , features=Features({'''image''': Image()} ) ) __a = csv._generate_tables([[csv_file_with_image]] ) __a = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''image''' ).type == Image()() __a = pa_table.to_pydict()['''image'''] assert generated_content == [{"path": image_file, "bytes": None}] def lowercase ( lowerCAmelCase__ : List[str] ) -> Optional[int]: with open(lowerCAmelCase__ , encoding='''utf-8''' ) as f: __a = f.read().splitlines()[1:] __a = Csv(encoding='''utf-8''' , features=Features({'''label''': ClassLabel(names=['''good''', '''bad'''] )} ) ) __a = csv._generate_tables([[csv_file_with_label]] ) __a = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('''label''' ).type == ClassLabel(names=['''good''', '''bad'''] )() __a = pa_table.to_pydict()['''label'''] assert generated_content == [ClassLabel(names=['''good''', '''bad'''] ).straint(lowerCAmelCase__ ) for label in labels] def lowercase ( lowerCAmelCase__ : Dict ) -> str: __a = Csv(encoding='''utf-8''' , sep=''',''' , converters={'''int_list''': lambda lowerCAmelCase__ : [int(lowerCAmelCase__ ) for i in x.split()]} ) __a = csv._generate_tables([[csv_file_with_int_list]] ) __a = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field('''int_list''' ).type ) __a = pa_table.to_pydict()['''int_list'''] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
45
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) lowercase_ = {"configuration_unispeech": ["UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP", "UniSpeechConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST", "UniSpeechForCTC", "UniSpeechForPreTraining", "UniSpeechForSequenceClassification", "UniSpeechModel", "UniSpeechPreTrainedModel", ] if TYPE_CHECKING: from .configuration_unispeech import UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_unispeech import ( UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST, UniSpeechForCTC, UniSpeechForPreTraining, UniSpeechForSequenceClassification, UniSpeechModel, UniSpeechPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str ) -> list: if n_term == "": return [] __a = [] for temp in range(int(lowerCAmelCase__ ) ): series.append(f'''1/{temp + 1}''' if series else '''1''' ) return series if __name__ == "__main__": lowercase_ = input("Enter the last number (nth term) of the Harmonic Series") print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n") print(harmonic_series(nth_term))
45
"""simple docstring""" import unittest import torch from torch import nn from diffusers.models.activations import get_activation class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = get_activation('''swish''' ) self.assertIsInstance(_a , nn.SiLU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''silu''' ) self.assertIsInstance(_a , nn.SiLU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''mish''' ) self.assertIsInstance(_a , nn.Mish ) self.assertEqual(act(torch.tensor(-200 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''gelu''' ) self.assertIsInstance(_a , nn.GELU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 )
45
1
"""simple docstring""" import copy import tempfile import unittest from transformers import MaMaaaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from transformers.utils import cached_property from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaTokenizer from transformers.models.mam_aaa.modeling_mam_aaa import MaMaaaDecoder, MaMaaaEncoder def lowercase ( lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : Union[str, Any]=None , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : str=None , ) -> List[str]: if attention_mask is None: __a = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: __a = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: __a = torch.ones(config.encoder_layers , config.encoder_attention_heads , device=lowerCAmelCase__ ) if decoder_head_mask is None: __a = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=lowerCAmelCase__ ) if cross_attn_head_mask is None: __a = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=lowerCAmelCase__ ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a=13 , _a=7 , _a=True , _a=False , _a=99 , _a=16 , _a=2 , _a=4 , _a=4 , _a="relu" , _a=0.1 , _a=0.1 , _a=0.0 , _a=0.0 , _a=20 , _a=2 , _a=1 , _a=0 , ): __a = parent __a = batch_size __a = seq_length __a = is_training __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 = encoder_layerdrop __a = decoder_layerdrop __a = max_position_embeddings __a = eos_token_id __a = pad_token_id __a = bos_token_id def __UpperCAmelCase ( self ): __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a = self.eos_token_id # Eos Token __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for M2M100 the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input __a = input_ids.clamp(self.pad_token_id + 1 ) __a = decoder_input_ids.clamp(self.pad_token_id + 1 ) __a = self.get_config() __a = prepare_mam_aaa_inputs_dict(_a , _a , _a ) return config, inputs_dict def __UpperCAmelCase ( self ): return MaMaaaConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , encoder_layerdrop=self.encoder_layerdrop , decoder_layerdrop=self.decoder_layerdrop , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , ) def __UpperCAmelCase ( self ): __a , __a = self.prepare_config_and_inputs() return config, inputs_dict def __UpperCAmelCase ( self , _a , _a ): __a = MaMaaaModel(config=_a ).get_decoder().to(_a ).eval() __a = inputs_dict['''input_ids'''] __a = inputs_dict['''attention_mask'''] __a = inputs_dict['''head_mask'''] # first forward pass __a = model(_a , attention_mask=_a , head_mask=_a , use_cache=_a ) __a , __a = outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids __a = ids_tensor((self.batch_size, 3) , config.vocab_size ) __a = ids_tensor((self.batch_size, 3) , 2 ) # append to next input_ids and __a = torch.cat([input_ids, next_tokens] , dim=-1 ) __a = torch.cat([attention_mask, next_attn_mask] , dim=-1 ) __a = model(_a , attention_mask=_a )['''last_hidden_state'''] __a = model(_a , attention_mask=_a , past_key_values=_a )[ '''last_hidden_state''' ] # select random slice __a = ids_tensor((1,) , output_from_past.shape[-1] ).item() __a = output_from_no_past[:, -3:, random_slice_idx].detach() __a = 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(_a , _a , atol=1E-2 ) ) def __UpperCAmelCase ( self , _a , _a ): __a = MaMaaaModel(config=_a ).to(_a ).eval() __a = model(**_a ) __a = outputs.encoder_last_hidden_state __a = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: __a = model.get_encoder() encoder.save_pretrained(_a ) __a = MaMaaaEncoder.from_pretrained(_a ).to(_a ) __a = encoder(inputs_dict['''input_ids'''] , attention_mask=inputs_dict['''attention_mask'''] )[ 0 ] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) with tempfile.TemporaryDirectory() as tmpdirname: __a = model.get_decoder() decoder.save_pretrained(_a ) __a = MaMaaaDecoder.from_pretrained(_a ).to(_a ) __a = decoder( input_ids=inputs_dict['''decoder_input_ids'''] , attention_mask=inputs_dict['''decoder_attention_mask'''] , encoder_hidden_states=_a , encoder_attention_mask=inputs_dict['''attention_mask'''] , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Any = ( ( MaMaaaModel, MaMaaaForConditionalGeneration, ) if is_torch_available() else () ) __UpperCAmelCase : Dict = (MaMaaaForConditionalGeneration,) if is_torch_available() else () __UpperCAmelCase : Optional[int] = ( { 'conversational': MaMaaaForConditionalGeneration, 'feature-extraction': MaMaaaModel, 'summarization': MaMaaaForConditionalGeneration, 'text2text-generation': MaMaaaForConditionalGeneration, 'translation': MaMaaaForConditionalGeneration, } if is_torch_available() else {} ) __UpperCAmelCase : int = True __UpperCAmelCase : Optional[int] = True __UpperCAmelCase : Dict = False __UpperCAmelCase : int = False def __UpperCAmelCase ( self , _a , _a , _a , _a , _a ): if pipeline_test_casse_name == "TranslationPipelineTests": # Get `ValueError: Translation requires a `src_lang` and a `tgt_lang` for this model`. # `M2M100Config` was never used in pipeline tests: cannot create a simple tokenizer. return True return False def __UpperCAmelCase ( self ): __a = MaMaaaModelTester(self ) __a = ConfigTester(self , config_class=_a ) def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() def __UpperCAmelCase ( self ): __a , __a = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: __a = model_class(_a ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_a ) __a , __a = model_class.from_pretrained(_a , output_loading_info=_a ) self.assertEqual(info['''missing_keys'''] , [] ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*_a ) def __UpperCAmelCase ( self ): __a , __a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in (MaMaaaModel, MaMaaaForConditionalGeneration): __a = model_class(_a ) model.to(_a ) model.eval() __a = copy.deepcopy(self._prepare_for_class(_a , _a ) ) if not self.is_encoder_decoder: __a = inputs['''input_ids'''] del inputs["input_ids"] else: __a = inputs['''input_ids'''] __a = inputs.get('''decoder_input_ids''' , _a ) del inputs["input_ids"] inputs.pop('''decoder_input_ids''' , _a ) __a = model.get_input_embeddings() if not self.is_encoder_decoder: __a = wte(_a ) else: __a = wte(_a ) __a = wte(_a ) with torch.no_grad(): model(**_a )[0] def __UpperCAmelCase ( self ): __a , __a = self.model_tester.prepare_config_and_inputs() __a = input_dict['''input_ids'''] __a = input_ids.ne(1 ).to(_a ) __a = MaMaaaForConditionalGeneration(_a ).eval().to(_a ) if torch_device == "cuda": model.half() model.generate(_a , attention_mask=_a ) model.generate(num_beams=4 , do_sample=_a , early_stopping=_a , num_return_sequences=3 ) def lowercase ( lowerCAmelCase__ : List[str] ) -> Tuple: return torch.tensor(lowerCAmelCase__ , dtype=torch.long , device=lowerCAmelCase__ ) lowercase_ = 1e-4 @require_torch @require_sentencepiece @require_tokenizers @slow class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @cached_property def __UpperCAmelCase ( self ): return MaMaaaTokenizer.from_pretrained('''facebook/m2m100_418M''' ) def __UpperCAmelCase ( self ): __a = MaMaaaModel.from_pretrained('''facebook/m2m100_418M''' ).to(_a ) __a = _long_tensor([[128_028, 98, 12, 30_527, 2_732, 159, 7_755, 61_904, 39_144, 38, 2]] ) __a = _long_tensor([[2, 128_028, 98, 12, 30_527, 2_732, 159, 7_755, 61_904, 39_144, 38]] ) __a = prepare_mam_aaa_inputs_dict(model.config , _a , _a ) with torch.no_grad(): __a = model(**_a )[0] __a = torch.Size((1, 11, 1_024) ) self.assertEqual(output.shape , _a ) # change to expected output here __a = torch.tensor( [[-0.7780, -0.1676, 0.1038], [-6.7556, -1.3992, 0.0567], [-7.5383, -0.5920, -0.2779]] , device=_a ) self.assertTrue(torch.allclose(output[:, :3, :3] , _a , atol=_a ) ) def __UpperCAmelCase ( self ): __a = MaMaaaForConditionalGeneration.from_pretrained('''facebook/m2m100_418M''' ).to(_a ) # change to intended input __a = _long_tensor([[128_028, 98, 12, 30_527, 2_732, 159, 7_755, 61_904, 39_144, 38, 2]] ) __a = _long_tensor([[2, 128_028, 98, 12, 30_527, 2_732, 159, 7_755, 61_904, 39_144, 38]] ) __a = prepare_mam_aaa_inputs_dict(model.config , _a , _a ) with torch.no_grad(): __a = model(**_a )[0] __a = torch.Size((1, 11, model.config.vocab_size) ) self.assertEqual(output.shape , _a ) # change to expected output here __a = torch.tensor( [[-1.0448, -1.0411, 3.7992], [-3.2191, -3.2386, -1.3451], [-3.6210, -3.5993, 0.4925]] , device=_a ) self.assertTrue(torch.allclose(output[:, :3, :3] , _a , atol=_a ) ) def __UpperCAmelCase ( self ): __a = MaMaaaForConditionalGeneration.from_pretrained('''facebook/m2m100_418M''' ).to(_a ) __a = MaMaaaTokenizer.from_pretrained('''facebook/m2m100_418M''' , src_lang='''fr''' , tgt_lang='''en''' ) __a = [ '''L\'affaire NSA souligne l\'absence totale de débat sur le renseignement''', '''Selon moi, il y a deux niveaux de réponse de la part du gouvernement français.''', '''Lorsque François Hollande téléphone à Barack Obama ou quand le ministre des affaires étrangères Laurent''' ''' Fabius convoque l\'ambassadeur des Etats-Unis, ils réagissent à une vraie découverte, qui est celle de''' ''' l\'ampleur de la surveillance américaine sur l\'ensemble des communications en France.''', ] # The below article tests that we don't add any hypotheses outside of the top n_beams __a = tokenizer(_a , padding=_a , return_tensors='''pt''' ) __a = model.generate( input_ids=dct['''input_ids'''].to(_a ) , attention_mask=dct['''attention_mask'''].to(_a ) , num_beams=5 , forced_bos_token_id=tokenizer.get_lang_id('''en''' ) , ) __a = [ '''The NSA case highlights the total absence of intelligence debate''', '''I think there are two levels of response from the French government.''', '''When François Hollande calls Barack Obama or when Foreign Minister Laurent Fabius calls the U.S.''' ''' Ambassador, they respond to a real discovery, which is that of the scale of U.S. surveillance on all''' ''' communications in France.''', ] __a = tokenizer.batch_decode( hypotheses_batch.tolist() , clean_up_tokenization_spaces=_a , skip_special_tokens=_a ) assert generated == expected_en
45
"""simple docstring""" from __future__ import annotations import unittest from transformers import EsmConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy import tensorflow as tf from transformers.models.esm.modeling_tf_esm import ( TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, TFEsmModel, ) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , ): __a = parent __a = 13 __a = 7 __a = True __a = True __a = True __a = 99 __a = 32 __a = 2 __a = 4 __a = 37 __a = '''gelu''' __a = 0.1 __a = 0.1 __a = 512 __a = 16 __a = 2 __a = 0.02 __a = 3 __a = 4 __a = None def __UpperCAmelCase ( self ): __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a = None if self.use_input_mask: __a = random_attention_mask([self.batch_size, self.seq_length] ) __a = None __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __a = ids_tensor([self.batch_size] , self.num_choices ) __a = EsmConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , pad_token_id=1 , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def __UpperCAmelCase ( self ): ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = self.prepare_config_and_inputs() __a = True __a = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __a = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , _a ): __a = TFEsmModel(config=_a ) __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} __a = model(_a ) __a = [input_ids, input_mask] __a = model(_a ) __a = model(_a ) 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 , _a , ): __a = True __a = TFEsmModel(config=_a ) __a = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''encoder_hidden_states''': encoder_hidden_states, '''encoder_attention_mask''': encoder_attention_mask, } __a = model(_a ) __a = [input_ids, input_mask] __a = model(_a , encoder_hidden_states=_a ) # Also check the case where encoder outputs are not passed __a = model(_a , attention_mask=_a ) 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 = TFEsmForMaskedLM(config=_a ) __a = model([input_ids, input_mask] ) 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 = self.num_labels __a = TFEsmForTokenClassification(config=_a ) __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} __a = model(_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = config_and_inputs __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = ( ( TFEsmModel, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, ) if is_tf_available() else () ) __UpperCAmelCase : Tuple = ( { 'feature-extraction': TFEsmModel, 'fill-mask': TFEsmForMaskedLM, 'text-classification': TFEsmForSequenceClassification, 'token-classification': TFEsmForTokenClassification, 'zero-shot': TFEsmForSequenceClassification, } if is_tf_available() else {} ) __UpperCAmelCase : Tuple = False __UpperCAmelCase : Union[str, Any] = False def __UpperCAmelCase ( self ): __a = TFEsmModelTester(self ) __a = ConfigTester(self , config_class=_a , hidden_size=37 ) def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_a ) @slow def __UpperCAmelCase ( self ): for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = TFEsmModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip('''Protein models do not support embedding resizing.''' ) def __UpperCAmelCase ( self ): pass @unittest.skip('''Protein models do not support embedding resizing.''' ) def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): __a , __a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a = model_class(_a ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class is TFEsmForMaskedLM: # Output embedding test differs from the main test because they're a matrix, not a layer __a = model.get_bias() assert isinstance(_a , _a ) for k, v in name.items(): assert isinstance(_a , tf.Variable ) else: __a = model.get_output_embeddings() assert x is None __a = model.get_bias() assert name is None @require_tf class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def __UpperCAmelCase ( self ): __a = TFEsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) __a = tf.constant([[0, 1, 2, 3, 4, 5]] ) __a = model(_a )[0] __a = [1, 6, 33] self.assertEqual(list(output.numpy().shape ) , _a ) # compare the actual values for a slice. __a = tf.constant( [ [ [8.92_1518, -10.58_9814, -6.467_1307], [-6.396_7156, -13.91_1377, -1.121_1915], [-7.78_1247, -13.95_1557, -3.74_0592], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-2 ) ) @slow def __UpperCAmelCase ( self ): __a = TFEsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) __a = tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) __a = model(_a )[0] # compare the actual values for a slice. __a = tf.constant( [ [ [0.1444_3092, 0.5412_5327, 0.324_7739], [0.3034_0484, 0.0052_6676, 0.3107_7722], [0.3227_8043, -0.2498_7096, 0.341_4628], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-4 ) )
45
1
"""simple docstring""" from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, apply_forward_hook from .modeling_utils import ModelMixin from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer @dataclass class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : torch.FloatTensor class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' @register_to_config def __init__( self , _a = 3 , _a = 3 , _a = ("DownEncoderBlock2D",) , _a = ("UpDecoderBlock2D",) , _a = (64,) , _a = 1 , _a = "silu" , _a = 3 , _a = 32 , _a = 256 , _a = 32 , _a = None , _a = 0.1_8215 , _a = "group" , ): super().__init__() # pass init params to Encoder __a = Encoder( in_channels=_a , out_channels=_a , down_block_types=_a , block_out_channels=_a , layers_per_block=_a , act_fn=_a , norm_num_groups=_a , double_z=_a , ) __a = vq_embed_dim if vq_embed_dim is not None else latent_channels __a = nn.Convad(_a , _a , 1 ) __a = VectorQuantizer(_a , _a , beta=0.25 , remap=_a , sane_index_shape=_a ) __a = nn.Convad(_a , _a , 1 ) # pass init params to Decoder __a = Decoder( in_channels=_a , out_channels=_a , up_block_types=_a , block_out_channels=_a , layers_per_block=_a , act_fn=_a , norm_num_groups=_a , norm_type=_a , ) @apply_forward_hook def __UpperCAmelCase ( self , _a , _a = True ): __a = self.encoder(_a ) __a = self.quant_conv(_a ) if not return_dict: return (h,) return VQEncoderOutput(latents=_a ) @apply_forward_hook def __UpperCAmelCase ( self , _a , _a = False , _a = True ): # also go through quantization layer if not force_not_quantize: __a , __a , __a = self.quantize(_a ) else: __a = h __a = self.post_quant_conv(_a ) __a = self.decoder(_a , quant if self.config.norm_type == '''spatial''' else None ) if not return_dict: return (dec,) return DecoderOutput(sample=_a ) def __UpperCAmelCase ( self , _a , _a = True ): __a = sample __a = self.encode(_a ).latents __a = self.decode(_a ).sample if not return_dict: return (dec,) return DecoderOutput(sample=_a )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int ) -> str: if number > 0: raise ValueError('''input must be a negative integer''' ) __a = len(bin(lowerCAmelCase__ )[3:] ) __a = bin(abs(lowerCAmelCase__ ) - (1 << binary_number_length) )[3:] __a = ( ( '''1''' + '''0''' * (binary_number_length - len(lowerCAmelCase__ )) + twos_complement_number ) if number < 0 else '''0''' ) return "0b" + twos_complement_number if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" from __future__ import annotations def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int ) -> list[str]: if partitions <= 0: raise ValueError('''partitions must be a positive number!''' ) if partitions > number_of_bytes: raise ValueError('''partitions can not > number_of_bytes!''' ) __a = number_of_bytes // partitions __a = [] for i in range(lowerCAmelCase__ ): __a = i * bytes_per_partition + 1 __a = ( number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition ) allocation_list.append(f'''{start_bytes}-{end_bytes}''' ) return allocation_list if __name__ == "__main__": import doctest doctest.testmod()
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : list[str] ) -> str: __a = '''''' for word_or_phrase in separated: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise Exception('''join() accepts only strings to be joined''' ) joined += word_or_phrase + separator return joined.strip(lowerCAmelCase__ ) if __name__ == "__main__": from doctest import testmod testmod()
45
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) lowercase_ = { "configuration_perceiver": ["PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP", "PerceiverConfig", "PerceiverOnnxConfig"], "tokenization_perceiver": ["PerceiverTokenizer"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["PerceiverFeatureExtractor"] lowercase_ = ["PerceiverImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST", "PerceiverForImageClassificationConvProcessing", "PerceiverForImageClassificationFourier", "PerceiverForImageClassificationLearned", "PerceiverForMaskedLM", "PerceiverForMultimodalAutoencoding", "PerceiverForOpticalFlow", "PerceiverForSequenceClassification", "PerceiverLayer", "PerceiverModel", "PerceiverPreTrainedModel", ] if TYPE_CHECKING: from .configuration_perceiver import PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP, PerceiverConfig, PerceiverOnnxConfig from .tokenization_perceiver import PerceiverTokenizer try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_perceiver import PerceiverFeatureExtractor from .image_processing_perceiver import PerceiverImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_perceiver import ( PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST, PerceiverForImageClassificationConvProcessing, PerceiverForImageClassificationFourier, PerceiverForImageClassificationLearned, PerceiverForMaskedLM, PerceiverForMultimodalAutoencoding, PerceiverForOpticalFlow, PerceiverForSequenceClassification, PerceiverLayer, PerceiverModel, PerceiverPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
"""simple docstring""" lowercase_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" def lowercase ( lowerCAmelCase__ : bytes ) -> bytes: # Make sure the supplied data is a bytes-like object if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = f'''a bytes-like object is required, not \'{data.__class__.__name__}\'''' raise TypeError(lowerCAmelCase__ ) __a = ''''''.join(bin(lowerCAmelCase__ )[2:].zfill(8 ) for byte in data ) __a = len(lowerCAmelCase__ ) % 6 != 0 if padding_needed: # The padding that will be added later __a = b'''=''' * ((6 - len(lowerCAmelCase__ ) % 6) // 2) # Append binary_stream with arbitrary binary digits (0's by default) to make its # length a multiple of 6. binary_stream += "0" * (6 - len(lowerCAmelCase__ ) % 6) else: __a = b'''''' # Encode every 6 binary digits to their corresponding Base64 character return ( "".join( B64_CHARSET[int(binary_stream[index : index + 6] , 2 )] for index in range(0 , len(lowerCAmelCase__ ) , 6 ) ).encode() + padding ) def lowercase ( lowerCAmelCase__ : str ) -> bytes: # Make sure encoded_data is either a string or a bytes-like object if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = ( '''argument should be a bytes-like object or ASCII string, ''' f'''not \'{encoded_data.__class__.__name__}\'''' ) raise TypeError(lowerCAmelCase__ ) # In case encoded_data is a bytes-like object, make sure it contains only # ASCII characters so we convert it to a string object if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): try: __a = encoded_data.decode('''utf-8''' ) except UnicodeDecodeError: raise ValueError('''base64 encoded data should only contain ASCII characters''' ) __a = encoded_data.count('''=''' ) # Check if the encoded string contains non base64 characters if padding: assert all( char in B64_CHARSET for char in encoded_data[:-padding] ), "Invalid base64 character(s) found." else: assert all( char in B64_CHARSET for char in encoded_data ), "Invalid base64 character(s) found." # Check the padding assert len(lowerCAmelCase__ ) % 4 == 0 and padding < 3, "Incorrect padding" if padding: # Remove padding if there is one __a = encoded_data[:-padding] __a = ''''''.join( bin(B64_CHARSET.index(lowerCAmelCase__ ) )[2:].zfill(6 ) for char in encoded_data )[: -padding * 2] else: __a = ''''''.join( bin(B64_CHARSET.index(lowerCAmelCase__ ) )[2:].zfill(6 ) for char in encoded_data ) __a = [ int(binary_stream[index : index + 8] , 2 ) for index in range(0 , len(lowerCAmelCase__ ) , 8 ) ] return bytes(lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.activations import gelu_new, gelu_python, get_activation @require_torch class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100] ) __a = get_activation('''gelu''' ) self.assertTrue(torch.allclose(gelu_python(_a ) , torch_builtin(_a ) ) ) self.assertFalse(torch.allclose(gelu_python(_a ) , gelu_new(_a ) ) ) def __UpperCAmelCase ( self ): __a = torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100] ) __a = get_activation('''gelu''' ) __a = get_activation('''gelu_10''' ) __a = torch_builtin(_a ) __a = geluaa(_a ) __a = torch.where(y_gelu_aa < 10.0 , 1 , 0 ) self.assertTrue(torch.max(_a ).item() == 10.0 ) self.assertTrue(torch.allclose(y_gelu * clipped_mask , y_gelu_aa * clipped_mask ) ) def __UpperCAmelCase ( self ): get_activation('''gelu''' ) get_activation('''gelu_10''' ) get_activation('''gelu_fast''' ) get_activation('''gelu_new''' ) get_activation('''gelu_python''' ) get_activation('''gelu_pytorch_tanh''' ) get_activation('''linear''' ) get_activation('''mish''' ) get_activation('''quick_gelu''' ) get_activation('''relu''' ) get_activation('''sigmoid''' ) get_activation('''silu''' ) get_activation('''swish''' ) get_activation('''tanh''' ) with self.assertRaises(_a ): get_activation('''bogus''' ) with self.assertRaises(_a ): get_activation(_a ) def __UpperCAmelCase ( self ): __a = get_activation('''gelu''' ) __a = 1 __a = get_activation('''gelu''' ) self.assertEqual(acta.a , 1 ) with self.assertRaises(_a ): __a = acta.a
45
"""simple docstring""" import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = inspect.getfile(accelerate.test_utils ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) __a = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_distributed_data_loop.py'''] ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_ops.py'''] ) @require_multi_gpu def __UpperCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices.''' ) __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_a , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices.''' ) __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.operation_file_path] print(f'''Command: {cmd}''' ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_a , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self ): __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_a , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices, using 2 devices only''' ) __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices='''0,1''' ): execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": lowercase_ = Accelerator() lowercase_ = (accelerator.state.process_index + 2, 1_0) lowercase_ = torch.randint(0, 1_0, shape).to(accelerator.device) lowercase_ = "" lowercase_ = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." lowercase_ = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." lowercase_ = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
45
1
"""simple docstring""" import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : int = 0 __UpperCAmelCase : bool = False __UpperCAmelCase : float = 3.0 class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'''a''': 2} ) self.assertDictEqual(MockClass(a=2 , b=_a ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'''a''': 2, '''c''': 2.25} ) @require_cuda def __UpperCAmelCase ( self ): # If no defaults are changed, `to_kwargs` returns an empty dict. __a = GradScalerKwargs(init_scale=1_024 , growth_factor=2 ) AcceleratorState._reset_state() __a = Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) __a = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2_000 ) self.assertEqual(scaler._enabled , _a ) @require_multi_gpu def __UpperCAmelCase ( self ): __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": lowercase_ = DistributedDataParallelKwargs(bucket_cap_mb=1_5, find_unused_parameters=True) lowercase_ = Accelerator(kwargs_handlers=[ddp_scaler]) lowercase_ = torch.nn.Linear(1_0_0, 2_0_0) lowercase_ = accelerator.prepare(model) # Check the values changed in kwargs lowercase_ = "" lowercase_ = model.bucket_bytes_cap // (1_0_2_4 * 1_0_2_4) if observed_bucket_cap_map != 1_5: error_msg += F"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += F"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += F"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += F"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += F"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
45
"""simple docstring""" import numpy as np def lowercase ( lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : float ) -> np.ndarray: return np.where(vector > 0 , lowerCAmelCase__ , (alpha * (np.exp(lowerCAmelCase__ ) - 1)) ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" import argparse import re from typing import Dict import torch from datasets import Audio, Dataset, load_dataset, load_metric from transformers import AutoFeatureExtractor, pipeline def lowercase ( lowerCAmelCase__ : Dataset , lowerCAmelCase__ : Dict[str, str] ) -> Dict: __a = args.log_outputs __a = '''_'''.join(args.dataset.split('''/''' ) + [args.config, args.split] ) # load metric __a = load_metric('''wer''' ) __a = load_metric('''cer''' ) # compute metrics __a = wer.compute(references=result['''target'''] , predictions=result['''prediction'''] ) __a = cer.compute(references=result['''target'''] , predictions=result['''prediction'''] ) # print & log results __a = f'''WER: {wer_result}\nCER: {cer_result}''' print(lowerCAmelCase__ ) with open(f'''{dataset_id}_eval_results.txt''' , '''w''' ) as f: f.write(lowerCAmelCase__ ) # log all results in text file. Possibly interesting for analysis if log_outputs is not None: __a = f'''log_{dataset_id}_predictions.txt''' __a = f'''log_{dataset_id}_targets.txt''' with open(lowerCAmelCase__ , '''w''' ) as p, open(lowerCAmelCase__ , '''w''' ) as t: # mapping function to write output def write_to_file(lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int ): p.write(f'''{i}''' + '''\n''' ) p.write(batch['''prediction'''] + '''\n''' ) t.write(f'''{i}''' + '''\n''' ) t.write(batch['''target'''] + '''\n''' ) result.map(lowerCAmelCase__ , with_indices=lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : str ) -> str: __a = '''[,?.!\-\;\:"“%‘”�—’…–]''' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training __a = re.sub(lowerCAmelCase__ , '''''' , text.lower() ) # In addition, we can normalize the target text, e.g. removing new lines characters etc... # note that order is important here! __a = ['''\n\n''', '''\n''', ''' ''', ''' '''] for t in token_sequences_to_ignore: __a = ''' '''.join(text.split(lowerCAmelCase__ ) ) return text def lowercase ( lowerCAmelCase__ : int ) -> Any: # load dataset __a = load_dataset(args.dataset , args.config , split=args.split , use_auth_token=lowerCAmelCase__ ) # for testing: only process the first two examples as a test # dataset = dataset.select(range(10)) # load processor __a = AutoFeatureExtractor.from_pretrained(args.model_id ) __a = feature_extractor.sampling_rate # resample audio __a = dataset.cast_column('''audio''' , Audio(sampling_rate=lowerCAmelCase__ ) ) # load eval pipeline if args.device is None: __a = 0 if torch.cuda.is_available() else -1 __a = pipeline('''automatic-speech-recognition''' , model=args.model_id , device=args.device ) # map function to decode audio def map_to_pred(lowerCAmelCase__ : Any ): __a = asr( batch['''audio''']['''array'''] , chunk_length_s=args.chunk_length_s , stride_length_s=args.stride_length_s ) __a = prediction['''text'''] __a = normalize_text(batch['''sentence'''] ) return batch # run inference on all examples __a = dataset.map(lowerCAmelCase__ , remove_columns=dataset.column_names ) # compute and log_results # do not change function below log_results(lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument( "--model_id", type=str, required=True, help="Model identifier. Should be loadable with 🤗 Transformers" ) parser.add_argument( "--dataset", type=str, required=True, help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets", ) parser.add_argument( "--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice" ) parser.add_argument("--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`") parser.add_argument( "--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to 5 seconds." ) parser.add_argument( "--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to 1 second." ) parser.add_argument( "--log_outputs", action="store_true", help="If defined, write outputs to log file for analysis." ) parser.add_argument( "--device", type=int, default=None, help="The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.", ) lowercase_ = parser.parse_args() main(args)
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: __a = set() # Replace all the whitespace in our sentence __a = input_str.replace(''' ''' , '''''' ) for alpha in input_str: if "a" <= alpha.lower() <= "z": frequency.add(alpha.lower() ) return len(lowerCAmelCase__ ) == 26 def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: __a = [False] * 26 for char in input_str: if char.islower(): __a = True elif char.isupper(): __a = True return all(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: return len({char for char in input_str.lower() if char.isalpha()} ) == 26 def lowercase ( ) -> None: from timeit import timeit __a = '''from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest''' print(timeit('''is_pangram()''' , setup=lowerCAmelCase__ ) ) print(timeit('''is_pangram_faster()''' , setup=lowerCAmelCase__ ) ) print(timeit('''is_pangram_fastest()''' , setup=lowerCAmelCase__ ) ) # 5.348480500048026, 2.6477354579837993, 1.8470395830227062 # 5.036091582966037, 2.644472333951853, 1.8869528750656173 if __name__ == "__main__": import doctest doctest.testmod() benchmark()
45
1
"""simple docstring""" import os try: from .build_directory_md import good_file_paths except ImportError: from build_directory_md import good_file_paths # type: ignore lowercase_ = list(good_file_paths()) assert filepaths, "good_file_paths() failed!" lowercase_ = [file for file in filepaths if file != file.lower()] if upper_files: print(F'''{len(upper_files)} files contain uppercase characters:''') print("\n".join(upper_files) + "\n") lowercase_ = [file for file in filepaths if " " in file] if space_files: print(F'''{len(space_files)} files contain space characters:''') print("\n".join(space_files) + "\n") lowercase_ = [file for file in filepaths if "-" in file] if hyphen_files: print(F'''{len(hyphen_files)} files contain hyphen characters:''') print("\n".join(hyphen_files) + "\n") lowercase_ = [file for file in filepaths if os.sep not in file] if nodir_files: print(F'''{len(nodir_files)} files are not in a directory:''') print("\n".join(nodir_files) + "\n") lowercase_ = len(upper_files + space_files + hyphen_files + nodir_files) if bad_files: import sys sys.exit(bad_files)
45
"""simple docstring""" import gc import unittest from transformers import CTRLConfig, is_torch_available from transformers.testing_utils import require_torch, slow, 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 ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, ) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a=14 , _a=7 , _a=True , _a=True , _a=True , _a=True , _a=True , _a=99 , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=3 , _a=4 , _a=None , ): __a = parent __a = batch_size __a = seq_length __a = is_training __a = use_token_type_ids __a = use_input_mask __a = use_labels __a = use_mc_token_ids __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = type_sequence_label_size __a = initializer_range __a = num_labels __a = num_choices __a = scope __a = self.vocab_size - 1 def __UpperCAmelCase ( self ): __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a = None if self.use_input_mask: __a = random_attention_mask([self.batch_size, self.seq_length] ) __a = None if self.use_token_type_ids: __a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __a = None if self.use_mc_token_ids: __a = ids_tensor([self.batch_size, self.num_choices] , self.seq_length ) __a = None __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __a = ids_tensor([self.batch_size] , self.num_choices ) __a = self.get_config() __a = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, token_type_ids, mc_token_ids, sequence_labels, token_labels, choice_labels, ) def __UpperCAmelCase ( self ): return CTRLConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , *_a ): __a = CTRLModel(config=_a ) model.to(_a ) model.eval() model(_a , token_type_ids=_a , head_mask=_a ) model(_a , token_type_ids=_a ) __a = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(len(result.past_key_values ) , config.n_layer ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , *_a ): __a = CTRLLMHeadModel(_a ) model.to(_a ) model.eval() __a = model(_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = config_and_inputs __a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask} return config, inputs_dict def __UpperCAmelCase ( self , _a , _a , _a , _a , *_a ): __a = self.num_labels __a = CTRLForSequenceClassification(_a ) model.to(_a ) model.eval() __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = model(_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) @require_torch class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else () __UpperCAmelCase : Union[str, Any] = (CTRLLMHeadModel,) if is_torch_available() else () __UpperCAmelCase : Union[str, Any] = ( { 'feature-extraction': CTRLModel, 'text-classification': CTRLForSequenceClassification, 'text-generation': CTRLLMHeadModel, 'zero-shot': CTRLForSequenceClassification, } if is_torch_available() else {} ) __UpperCAmelCase : Optional[Any] = True __UpperCAmelCase : List[Any] = False __UpperCAmelCase : str = False def __UpperCAmelCase ( self , _a , _a , _a , _a , _a ): if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny # config could not be created. return True return False def __UpperCAmelCase ( self ): __a = CTRLModelTester(self ) __a = ConfigTester(self , config_class=_a , n_embd=37 ) def __UpperCAmelCase ( self ): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_ctrl_model(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*_a ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __UpperCAmelCase ( self ): pass @slow def __UpperCAmelCase ( self ): for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = CTRLModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip('''The model doesn\'t support left padding''' ) # and it's not used enough to be worth fixing :) def __UpperCAmelCase ( self ): pass @require_torch class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() @slow def __UpperCAmelCase ( self ): __a = CTRLLMHeadModel.from_pretrained('''ctrl''' ) model.to(_a ) __a = torch.tensor( [[11_859, 0, 1_611, 8]] , dtype=torch.long , device=_a ) # Legal the president is __a = [ 11_859, 0, 1_611, 8, 5, 150, 26_449, 2, 19, 348, 469, 3, 2_595, 48, 20_740, 246_533, 246_533, 19, 30, 5, ] # Legal the president is a good guy and I don't want to lose my job. \n \n I have a __a = model.generate(_a , do_sample=_a ) self.assertListEqual(output_ids[0].tolist() , _a )
45
1
"""simple docstring""" import argparse import json from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import List import timm import torch import torch.nn as nn from huggingface_hub import hf_hub_download from torch import Tensor from transformers import AutoImageProcessor, ResNetConfig, ResNetForImageClassification from transformers.utils import logging logging.set_verbosity_info() lowercase_ = logging.get_logger() @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : nn.Module __UpperCAmelCase : List[nn.Module] = field(default_factory=__SCREAMING_SNAKE_CASE ) __UpperCAmelCase : list = field(default_factory=__SCREAMING_SNAKE_CASE ) def __UpperCAmelCase ( self , _a , _a , _a ): __a = len(list(m.modules() ) ) == 1 or isinstance(_a , nn.Convad ) or isinstance(_a , nn.BatchNormad ) if has_not_submodules: self.traced.append(_a ) def __call__( self , _a ): for m in self.module.modules(): self.handles.append(m.register_forward_hook(self._forward_hook ) ) self.module(_a ) [x.remove() for x in self.handles] return self @property def __UpperCAmelCase ( self ): # check the len of the state_dict keys to see if we have learnable params return list(filter(lambda _a : len(list(x.state_dict().keys() ) ) > 0 , self.traced ) ) @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : nn.Module __UpperCAmelCase : nn.Module __UpperCAmelCase : int = 0 __UpperCAmelCase : List = field(default_factory=__SCREAMING_SNAKE_CASE ) __UpperCAmelCase : List = field(default_factory=__SCREAMING_SNAKE_CASE ) def __call__( self , _a ): __a = Tracker(self.dest )(_a ).parametrized __a = Tracker(self.src )(_a ).parametrized __a = list(filter(lambda _a : type(_a ) not in self.src_skip , _a ) ) __a = list(filter(lambda _a : type(_a ) not in self.dest_skip , _a ) ) if len(_a ) != len(_a ): raise Exception( f'''Numbers of operations are different. Source module has {len(_a )} operations while''' f''' destination module has {len(_a )}.''' ) for dest_m, src_m in zip(_a , _a ): dest_m.load_state_dict(src_m.state_dict() ) if self.verbose == 1: print(f'''Transfered from={src_m} to={dest_m}''' ) def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : ResNetConfig , lowerCAmelCase__ : Path , lowerCAmelCase__ : bool = True ) -> Tuple: print(f'''Converting {name}...''' ) with torch.no_grad(): __a = timm.create_model(lowerCAmelCase__ , pretrained=lowerCAmelCase__ ).eval() __a = ResNetForImageClassification(lowerCAmelCase__ ).eval() __a = ModuleTransfer(src=lowerCAmelCase__ , dest=lowerCAmelCase__ ) __a = torch.randn((1, 3, 224, 224) ) module_transfer(lowerCAmelCase__ ) assert torch.allclose(from_model(lowerCAmelCase__ ) , our_model(lowerCAmelCase__ ).logits ), "The model logits don't match the original one." __a = f'''resnet{'-'.join(name.split('resnet' ) )}''' print(lowerCAmelCase__ ) if push_to_hub: our_model.push_to_hub( repo_path_or_name=save_directory / checkpoint_name , commit_message='''Add model''' , use_temp_dir=lowerCAmelCase__ , ) # we can use the convnext one __a = AutoImageProcessor.from_pretrained('''facebook/convnext-base-224-22k-1k''' ) image_processor.push_to_hub( repo_path_or_name=save_directory / checkpoint_name , commit_message='''Add image processor''' , use_temp_dir=lowerCAmelCase__ , ) print(f'''Pushed {checkpoint_name}''' ) def lowercase ( lowerCAmelCase__ : Path , lowerCAmelCase__ : str = None , lowerCAmelCase__ : bool = True ) -> Optional[Any]: __a = '''imagenet-1k-id2label.json''' __a = 1000 __a = (1, num_labels) __a = '''huggingface/label-files''' __a = num_labels __a = json.load(open(hf_hub_download(lowerCAmelCase__ , lowerCAmelCase__ , repo_type='''dataset''' ) , '''r''' ) ) __a = {int(lowerCAmelCase__ ): v for k, v in idalabel.items()} __a = idalabel __a = {v: k for k, v in idalabel.items()} __a = partial(lowerCAmelCase__ , num_labels=lowerCAmelCase__ , idalabel=lowerCAmelCase__ , labelaid=lowerCAmelCase__ ) __a = { '''resnet18''': ImageNetPreTrainedConfig( depths=[2, 2, 2, 2] , hidden_sizes=[64, 128, 256, 512] , layer_type='''basic''' ), '''resnet26''': ImageNetPreTrainedConfig( depths=[2, 2, 2, 2] , hidden_sizes=[256, 512, 1024, 2048] , layer_type='''bottleneck''' ), '''resnet34''': ImageNetPreTrainedConfig( depths=[3, 4, 6, 3] , hidden_sizes=[64, 128, 256, 512] , layer_type='''basic''' ), '''resnet50''': ImageNetPreTrainedConfig( depths=[3, 4, 6, 3] , hidden_sizes=[256, 512, 1024, 2048] , layer_type='''bottleneck''' ), '''resnet101''': ImageNetPreTrainedConfig( depths=[3, 4, 23, 3] , hidden_sizes=[256, 512, 1024, 2048] , layer_type='''bottleneck''' ), '''resnet152''': ImageNetPreTrainedConfig( depths=[3, 8, 36, 3] , hidden_sizes=[256, 512, 1024, 2048] , layer_type='''bottleneck''' ), } if model_name: convert_weight_and_push(lowerCAmelCase__ , names_to_config[model_name] , lowerCAmelCase__ , lowerCAmelCase__ ) else: for model_name, config in names_to_config.items(): convert_weight_and_push(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) return config, expected_shape if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default=None, type=str, help=( "The name of the model you wish to convert, it must be one of the supported resnet* architecture," " currently: resnet18,26,34,50,101,152. If `None`, all of them will the converted." ), ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=Path, required=True, help="Path to the output PyTorch model directory.", ) parser.add_argument( "--push_to_hub", default=True, type=bool, required=False, help="If True, push model and image processor to the hub.", ) lowercase_ = parser.parse_args() lowercase_ = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
45
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase_ = {"configuration_xglm": ["XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XGLMConfig"]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["XGLMTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["XGLMTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "XGLM_PRETRAINED_MODEL_ARCHIVE_LIST", "XGLMForCausalLM", "XGLMModel", "XGLMPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "FlaxXGLMForCausalLM", "FlaxXGLMModel", "FlaxXGLMPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST", "TFXGLMForCausalLM", "TFXGLMModel", "TFXGLMPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure)
45
1
"""simple docstring""" import numpy as np def lowercase ( lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : float ) -> np.ndarray: return np.where(vector > 0 , lowerCAmelCase__ , (alpha * (np.exp(lowerCAmelCase__ ) - 1)) ) if __name__ == "__main__": import doctest doctest.testmod()
45
"""simple docstring""" import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm lowercase_ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex lowercase_ = 1_0 lowercase_ = 2_5_6 def lowercase ( lowerCAmelCase__ : List[str] ) -> Optional[MinHash]: if len(lowerCAmelCase__ ) < MIN_NUM_TOKENS: return None __a = MinHash(num_perm=lowerCAmelCase__ ) for token in set(lowerCAmelCase__ ): min_hash.update(token.encode() ) return min_hash def lowercase ( lowerCAmelCase__ : str ) -> Set[str]: return {t for t in NON_ALPHA.split(lowerCAmelCase__ ) if len(t.strip() ) > 0} class __lowerCAmelCase : '''simple docstring''' def __init__( self , *, _a = 0.85 , ): __a = duplication_jaccard_threshold __a = NUM_PERM __a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) __a = defaultdict(_a ) def __UpperCAmelCase ( self , _a , _a ): __a = self._index.query(_a ) if code_key in self._index.keys: print(f'''Duplicate key {code_key}''' ) return self._index.insert(_a , _a ) if len(_a ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(_a ) break else: self._duplicate_clusters[close_duplicates[0]].add(_a ) def __UpperCAmelCase ( self ): __a = [] for base, duplicates in self._duplicate_clusters.items(): __a = [base] + list(_a ) # reformat the cluster to be a list of dict __a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(_a ) return duplicate_clusters def __UpperCAmelCase ( self , _a ): __a = self.get_duplicate_clusters() with open(_a , '''w''' ) as f: json.dump(_a , _a ) def lowercase ( lowerCAmelCase__ : List[str] ) -> int: __a , __a = element __a = get_min_hash([t for t in NON_ALPHA.split(data['''content'''] ) if len(t.strip() ) > 0] ) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def lowercase ( lowerCAmelCase__ : Type[Dataset] ) -> str: with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(lowerCAmelCase__ , max_queue_size=10000 ) , chunksize=100 , ): if data is not None: yield data def lowercase ( lowerCAmelCase__ : Type[Dataset] , lowerCAmelCase__ : float ) -> Dict: __a = DuplicationIndex(duplication_jaccard_threshold=lowerCAmelCase__ ) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(lowerCAmelCase__ ) ) , max_queue_size=100 ) ): di.add(lowerCAmelCase__ , lowerCAmelCase__ ) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : str ) -> float: __a = get_tokens(lowerCAmelCase__ ) __a = get_tokens(lowerCAmelCase__ ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) lowercase_ = None def lowercase ( lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Union[str, Any] ) -> Any: __a = [] for elementa in cluster: __a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: __a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(lowerCAmelCase__ , lowerCAmelCase__ ) >= jaccard_threshold: elementa["copies"] += 1 break else: __a = 1 extremes.append(lowerCAmelCase__ ) return extremes def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[int] ) -> Optional[int]: global _shared_dataset __a = dataset __a = [] __a = partial(_find_cluster_extremes_shared , jaccard_threshold=lowerCAmelCase__ ) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( lowerCAmelCase__ , lowerCAmelCase__ , ) , total=len(lowerCAmelCase__ ) , ): extremes_list.append(lowerCAmelCase__ ) return extremes_list def lowercase ( lowerCAmelCase__ : Type[Dataset] , lowerCAmelCase__ : float = 0.85 ) -> Tuple[Type[Dataset], List[List[Dict]]]: __a = make_duplicate_clusters(lowerCAmelCase__ , lowerCAmelCase__ ) __a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} __a = {} __a = find_extremes(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) for extremes in extremes_clusters: for element in extremes: __a = element __a = duplicate_indices - set(extreme_dict.keys() ) __a = dataset.filter(lambda lowerCAmelCase__ , lowerCAmelCase__ : idx not in remove_indices , with_indices=lowerCAmelCase__ ) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: __a = element['''base_index'''] in extreme_dict if element["is_extreme"]: __a = extreme_dict[element['''base_index''']]['''copies'''] print(f'''Original dataset size: {len(lowerCAmelCase__ )}''' ) print(f'''Number of duplicate clusters: {len(lowerCAmelCase__ )}''' ) print(f'''Files in duplicate cluster: {len(lowerCAmelCase__ )}''' ) print(f'''Unique files in duplicate cluster: {len(lowerCAmelCase__ )}''' ) print(f'''Filtered dataset size: {len(lowerCAmelCase__ )}''' ) return ds_filter, duplicate_clusters
45
1
"""simple docstring""" import re def lowercase ( lowerCAmelCase__ : str ) -> bool: __a = re.compile( r'''^(?:0|94|\+94|0{2}94)''' r'''7(0|1|2|4|5|6|7|8)''' r'''(-| |)''' r'''\d{7}$''' ) return bool(re.search(lowerCAmelCase__ , lowerCAmelCase__ ) ) if __name__ == "__main__": lowercase_ = "0094702343221" print(is_sri_lankan_phone_number(phone))
45
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase_ = { "configuration_roformer": ["ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "RoFormerConfig", "RoFormerOnnxConfig"], "tokenization_roformer": ["RoFormerTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["RoFormerTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "RoFormerForCausalLM", "RoFormerForMaskedLM", "RoFormerForMultipleChoice", "RoFormerForQuestionAnswering", "RoFormerForSequenceClassification", "RoFormerForTokenClassification", "RoFormerLayer", "RoFormerModel", "RoFormerPreTrainedModel", "load_tf_weights_in_roformer", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFRoFormerForCausalLM", "TFRoFormerForMaskedLM", "TFRoFormerForMultipleChoice", "TFRoFormerForQuestionAnswering", "TFRoFormerForSequenceClassification", "TFRoFormerForTokenClassification", "TFRoFormerLayer", "TFRoFormerModel", "TFRoFormerPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "FlaxRoFormerForMaskedLM", "FlaxRoFormerForMultipleChoice", "FlaxRoFormerForQuestionAnswering", "FlaxRoFormerForSequenceClassification", "FlaxRoFormerForTokenClassification", "FlaxRoFormerModel", "FlaxRoFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : list[list[int]] ) -> int: def update_area_of_max_square(lowerCAmelCase__ : int , lowerCAmelCase__ : int ) -> int: # BASE CASE if row >= rows or col >= cols: return 0 __a = update_area_of_max_square(lowerCAmelCase__ , col + 1 ) __a = update_area_of_max_square(row + 1 , col + 1 ) __a = update_area_of_max_square(row + 1 , lowerCAmelCase__ ) if mat[row][col]: __a = 1 + min([right, diagonal, down] ) __a = max(largest_square_area[0] , lowerCAmelCase__ ) return sub_problem_sol else: return 0 __a = [0] update_area_of_max_square(0 , 0 ) return largest_square_area[0] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : list[list[int]] ) -> int: def update_area_of_max_square_using_dp_array( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : list[list[int]] ) -> int: if row >= rows or col >= cols: return 0 if dp_array[row][col] != -1: return dp_array[row][col] __a = update_area_of_max_square_using_dp_array(lowerCAmelCase__ , col + 1 , lowerCAmelCase__ ) __a = update_area_of_max_square_using_dp_array(row + 1 , col + 1 , lowerCAmelCase__ ) __a = update_area_of_max_square_using_dp_array(row + 1 , lowerCAmelCase__ , lowerCAmelCase__ ) if mat[row][col]: __a = 1 + min([right, diagonal, down] ) __a = max(largest_square_area[0] , lowerCAmelCase__ ) __a = sub_problem_sol return sub_problem_sol else: return 0 __a = [0] __a = [[-1] * cols for _ in range(lowerCAmelCase__ )] update_area_of_max_square_using_dp_array(0 , 0 , lowerCAmelCase__ ) return largest_square_area[0] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : list[list[int]] ) -> int: __a = [[0] * (cols + 1) for _ in range(rows + 1 )] __a = 0 for row in range(rows - 1 , -1 , -1 ): for col in range(cols - 1 , -1 , -1 ): __a = dp_array[row][col + 1] __a = dp_array[row + 1][col + 1] __a = dp_array[row + 1][col] if mat[row][col] == 1: __a = 1 + min(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) __a = max(dp_array[row][col] , lowerCAmelCase__ ) else: __a = 0 return largest_square_area def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : list[list[int]] ) -> int: __a = [0] * (cols + 1) __a = [0] * (cols + 1) __a = 0 for row in range(rows - 1 , -1 , -1 ): for col in range(cols - 1 , -1 , -1 ): __a = current_row[col + 1] __a = next_row[col + 1] __a = next_row[col] if mat[row][col] == 1: __a = 1 + min(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) __a = max(current_row[col] , lowerCAmelCase__ ) else: __a = 0 __a = current_row return largest_square_area if __name__ == "__main__": import doctest doctest.testmod() print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))
45
"""simple docstring""" import warnings from ..trainer import Trainer from ..utils import logging lowercase_ = logging.get_logger(__name__) class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , _a=None , **_a ): warnings.warn( '''`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` ''' '''instead.''' , _a , ) super().__init__(args=_a , **_a )
45
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "sayakpaul/vit-msn-base": "https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json", # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = 'vit_msn' def __init__( self , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.0 , _a=0.0 , _a=0.02 , _a=1E-06 , _a=224 , _a=16 , _a=3 , _a=True , **_a , ): super().__init__(**_a ) __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 = initializer_range __a = layer_norm_eps __a = image_size __a = patch_size __a = num_channels __a = qkv_bias
45
"""simple docstring""" import math def lowercase ( lowerCAmelCase__ : list , lowerCAmelCase__ : int ) -> int: __a = len(lowerCAmelCase__ ) __a = int(math.floor(math.sqrt(lowerCAmelCase__ ) ) ) __a = 0 while arr[min(lowerCAmelCase__ , lowerCAmelCase__ ) - 1] < x: __a = step step += int(math.floor(math.sqrt(lowerCAmelCase__ ) ) ) if prev >= n: return -1 while arr[prev] < x: __a = prev + 1 if prev == min(lowerCAmelCase__ , lowerCAmelCase__ ): return -1 if arr[prev] == x: return prev return -1 if __name__ == "__main__": lowercase_ = input("Enter numbers separated by a comma:\n").strip() lowercase_ = [int(item) for item in user_input.split(",")] lowercase_ = int(input("Enter the number to be searched:\n")) lowercase_ = jump_search(arr, x) if res == -1: print("Number not found!") else: print(F'''Number {x} is at index {res}''')
45
1
"""simple docstring""" from bisect import bisect from itertools import accumulate def lowercase ( lowerCAmelCase__ : Dict , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Tuple ) -> Optional[int]: __a = sorted(zip(lowerCAmelCase__ , lowerCAmelCase__ ) , key=lambda lowerCAmelCase__ : x[0] / x[1] , reverse=lowerCAmelCase__ ) __a , __a = [i[0] for i in r], [i[1] for i in r] __a = list(accumulate(lowerCAmelCase__ ) ) __a = bisect(lowerCAmelCase__ , lowerCAmelCase__ ) return ( 0 if k == 0 else sum(vl[:k] ) + (w - acc[k - 1]) * (vl[k]) / (wt[k]) if k != n else sum(vl[:k] ) ) if __name__ == "__main__": import doctest doctest.testmod()
45
"""simple docstring""" lowercase_ = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] lowercase_ = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] lowercase_ = { 0: "Sunday", 1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", 5: "Friday", 6: "Saturday", } def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int ) -> str: assert len(str(lowerCAmelCase__ ) ) > 2, "year should be in YYYY format" assert 1 <= month <= 12, "month should be between 1 to 12" assert 1 <= day <= 31, "day should be between 1 to 31" # Doomsday algorithm: __a = year // 100 __a = (5 * (century % 4) + 2) % 7 __a = year % 100 __a = centurian % 12 __a = ( (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor ) % 7 __a = ( DOOMSDAY_NOT_LEAP[month - 1] if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) else DOOMSDAY_LEAP[month - 1] ) __a = (dooms_day + day - day_anchor) % 7 return WEEK_DAY_NAMES[week_day] if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" import json import os from collections import Counter import torch import torchvision import torchvision.transforms as transforms from PIL import Image from torch import nn from torch.utils.data import Dataset lowercase_ = {1: (1, 1), 2: (2, 1), 3: (3, 1), 4: (2, 2), 5: (5, 1), 6: (3, 2), 7: (7, 1), 8: (4, 2), 9: (3, 3)} class __lowerCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _a ): super().__init__() __a = torchvision.models.resnetaaa(pretrained=_a ) __a = list(model.children() )[:-2] __a = nn.Sequential(*_a ) __a = nn.AdaptiveAvgPoolad(POOLING_BREAKDOWN[args.num_image_embeds] ) def __UpperCAmelCase ( self , _a ): # Bx3x224x224 -> Bx2048x7x7 -> Bx2048xN -> BxNx2048 __a = self.pool(self.model(_a ) ) __a = torch.flatten(_a , start_dim=2 ) __a = out.transpose(1 , 2 ).contiguous() return out # BxNx2048 class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , _a , _a , _a , _a , _a ): __a = [json.loads(_a ) for l in open(_a )] __a = os.path.dirname(_a ) __a = tokenizer __a = labels __a = len(_a ) __a = max_seq_length __a = transforms def __len__( self ): return len(self.data ) def __getitem__( self , _a ): __a = torch.LongTensor(self.tokenizer.encode(self.data[index]['''text'''] , add_special_tokens=_a ) ) __a , __a , __a = sentence[0], sentence[1:-1], sentence[-1] __a = sentence[: self.max_seq_length] __a = torch.zeros(self.n_classes ) __a = 1 __a = Image.open(os.path.join(self.data_dir , self.data[index]['''img'''] ) ).convert('''RGB''' ) __a = self.transforms(_a ) return { "image_start_token": start_token, "image_end_token": end_token, "sentence": sentence, "image": image, "label": label, } def __UpperCAmelCase ( self ): __a = Counter() for row in self.data: label_freqs.update(row['''label'''] ) return label_freqs def lowercase ( lowerCAmelCase__ : Any ) -> List[str]: __a = [len(row['''sentence'''] ) for row in batch] __a , __a = len(lowerCAmelCase__ ), max(lowerCAmelCase__ ) __a = torch.zeros(lowerCAmelCase__ , lowerCAmelCase__ , dtype=torch.long ) __a = torch.zeros(lowerCAmelCase__ , lowerCAmelCase__ , dtype=torch.long ) for i_batch, (input_row, length) in enumerate(zip(lowerCAmelCase__ , lowerCAmelCase__ ) ): __a = input_row['''sentence'''] __a = 1 __a = torch.stack([row['''image'''] for row in batch] ) __a = torch.stack([row['''label'''] for row in batch] ) __a = torch.stack([row['''image_start_token'''] for row in batch] ) __a = torch.stack([row['''image_end_token'''] for row in batch] ) return text_tensor, mask_tensor, img_tensor, img_start_token, img_end_token, tgt_tensor def lowercase ( ) -> Tuple: return [ "Crime", "Drama", "Thriller", "Action", "Comedy", "Romance", "Documentary", "Short", "Mystery", "History", "Family", "Adventure", "Fantasy", "Sci-Fi", "Western", "Horror", "Sport", "War", "Music", "Musical", "Animation", "Biography", "Film-Noir", ] def lowercase ( ) -> Dict: return transforms.Compose( [ transforms.Resize(256 ), transforms.CenterCrop(224 ), transforms.ToTensor(), transforms.Normalize( mean=[0.46_77_70_44, 0.44_53_14_29, 0.40_66_10_17] , std=[0.12_22_19_94, 0.12_14_58_35, 0.14_38_04_69] , ), ] )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : list ) -> bool: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise ValueError('''Input series is not valid, valid series - [2, 4, 6]''' ) if len(lowerCAmelCase__ ) == 0: raise ValueError('''Input list must be a non empty list''' ) if len(lowerCAmelCase__ ) == 1: return True __a = series[1] - series[0] for index in range(len(lowerCAmelCase__ ) - 1 ): if series[index + 1] - series[index] != common_diff: return False return True def lowercase ( lowerCAmelCase__ : list ) -> float: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise ValueError('''Input series is not valid, valid series - [2, 4, 6]''' ) if len(lowerCAmelCase__ ) == 0: raise ValueError('''Input list must be a non empty list''' ) __a = 0 for val in series: answer += val return answer / len(lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" from argparse import ArgumentParser from .add_new_model import AddNewModelCommand from .add_new_model_like import AddNewModelLikeCommand from .convert import ConvertCommand from .download import DownloadCommand from .env import EnvironmentCommand from .lfs import LfsCommands from .pt_to_tf import PTtoTFCommand from .run import RunCommand from .serving import ServeCommand from .user import UserCommands def lowercase ( ) -> str: __a = ArgumentParser('''Transformers CLI tool''' , usage='''transformers-cli <command> [<args>]''' ) __a = parser.add_subparsers(help='''transformers-cli command helpers''' ) # Register commands ConvertCommand.register_subcommand(lowerCAmelCase__ ) DownloadCommand.register_subcommand(lowerCAmelCase__ ) EnvironmentCommand.register_subcommand(lowerCAmelCase__ ) RunCommand.register_subcommand(lowerCAmelCase__ ) ServeCommand.register_subcommand(lowerCAmelCase__ ) UserCommands.register_subcommand(lowerCAmelCase__ ) AddNewModelCommand.register_subcommand(lowerCAmelCase__ ) AddNewModelLikeCommand.register_subcommand(lowerCAmelCase__ ) LfsCommands.register_subcommand(lowerCAmelCase__ ) PTtoTFCommand.register_subcommand(lowerCAmelCase__ ) # Let's go __a = parser.parse_args() if not hasattr(lowerCAmelCase__ , '''func''' ): parser.print_help() exit(1 ) # Run __a = args.func(lowerCAmelCase__ ) service.run() if __name__ == "__main__": main()
45
"""simple docstring""" from argparse import ArgumentParser, Namespace from ..utils import logging from . import BaseTransformersCLICommand def lowercase ( lowerCAmelCase__ : Namespace ) -> Tuple: return ConvertCommand( args.model_type , args.tf_checkpoint , args.pytorch_dump_output , args.config , args.finetuning_task_name ) lowercase_ = "\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n" class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' @staticmethod def __UpperCAmelCase ( _a ): __a = parser.add_parser( '''convert''' , help='''CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.''' , ) train_parser.add_argument('''--model_type''' , type=_a , required=_a , help='''Model\'s type.''' ) train_parser.add_argument( '''--tf_checkpoint''' , type=_a , required=_a , help='''TensorFlow checkpoint path or folder.''' ) train_parser.add_argument( '''--pytorch_dump_output''' , type=_a , required=_a , help='''Path to the PyTorch saved model output.''' ) train_parser.add_argument('''--config''' , type=_a , default='''''' , help='''Configuration file path or folder.''' ) train_parser.add_argument( '''--finetuning_task_name''' , type=_a , default=_a , help='''Optional fine-tuning task name if the TF model was a finetuned model.''' , ) train_parser.set_defaults(func=_a ) def __init__( self , _a , _a , _a , _a , _a , *_a , ): __a = logging.get_logger('''transformers-cli/converting''' ) self._logger.info(f'''Loading model {model_type}''' ) __a = model_type __a = tf_checkpoint __a = pytorch_dump_output __a = config __a = finetuning_task_name def __UpperCAmelCase ( self ): if self._model_type == "albert": try: from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "bert": try: from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "funnel": try: from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "t5": try: from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "gpt": from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import ( convert_openai_checkpoint_to_pytorch, ) convert_openai_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "transfo_xl": try: from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import ( convert_transfo_xl_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) if "ckpt" in self._tf_checkpoint.lower(): __a = self._tf_checkpoint __a = '''''' else: __a = self._tf_checkpoint __a = '''''' convert_transfo_xl_checkpoint_to_pytorch( _a , self._config , self._pytorch_dump_output , _a ) elif self._model_type == "gpt2": try: from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import ( convert_gpta_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "xlnet": try: from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import ( convert_xlnet_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_xlnet_checkpoint_to_pytorch( self._tf_checkpoint , self._config , self._pytorch_dump_output , self._finetuning_task_name ) elif self._model_type == "xlm": from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import ( convert_xlm_checkpoint_to_pytorch, ) convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "lxmert": from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import ( convert_lxmert_checkpoint_to_pytorch, ) convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "rembert": from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import ( convert_rembert_tf_checkpoint_to_pytorch, ) convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) else: raise ValueError( '''--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]''' )
45
1
"""simple docstring""" from math import asin, atan, cos, radians, sin, sqrt, tan lowercase_ = 6378137.0 lowercase_ = 6356752.314245 lowercase_ = 6_3_7_8_1_3_7 def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> float: __a = (AXIS_A - AXIS_B) / AXIS_A __a = atan((1 - flattening) * tan(radians(lowerCAmelCase__ ) ) ) __a = atan((1 - flattening) * tan(radians(lowerCAmelCase__ ) ) ) __a = radians(lowerCAmelCase__ ) __a = radians(lowerCAmelCase__ ) # Equation __a = sin((phi_a - phi_a) / 2 ) __a = sin((lambda_a - lambda_a) / 2 ) # Square both values sin_sq_phi *= sin_sq_phi sin_sq_lambda *= sin_sq_lambda __a = sqrt(sin_sq_phi + (cos(lowerCAmelCase__ ) * cos(lowerCAmelCase__ ) * sin_sq_lambda) ) return 2 * RADIUS * asin(lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "microsoft/beit-base-patch16-224-pt22k": ( "https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json" ), # See all BEiT models at https://huggingface.co/models?filter=beit } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = 'beit' def __init__( self , _a=8_192 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.0 , _a=0.0 , _a=0.02 , _a=1E-12 , _a=224 , _a=16 , _a=3 , _a=False , _a=False , _a=False , _a=False , _a=0.1 , _a=0.1 , _a=True , _a=[3, 5, 7, 11] , _a=[1, 2, 3, 6] , _a=True , _a=0.4 , _a=256 , _a=1 , _a=False , _a=255 , **_a , ): super().__init__(**_a ) __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 = initializer_range __a = layer_norm_eps __a = image_size __a = patch_size __a = num_channels __a = use_mask_token __a = use_absolute_position_embeddings __a = use_relative_position_bias __a = use_shared_relative_position_bias __a = layer_scale_init_value __a = drop_path_rate __a = use_mean_pooling # decode head attributes (semantic segmentation) __a = out_indices __a = pool_scales # auxiliary head attributes (semantic segmentation) __a = use_auxiliary_head __a = auxiliary_loss_weight __a = auxiliary_channels __a = auxiliary_num_convs __a = auxiliary_concat_input __a = semantic_loss_ignore_index class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[Any] = version.parse('1.11' ) @property def __UpperCAmelCase ( self ): return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __UpperCAmelCase ( self ): return 1E-4
45
1
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaImgaImgPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = KandinskyVaaImgaImgPipeline __UpperCAmelCase : Optional[Any] = ['image_embeds', 'negative_image_embeds', 'image'] __UpperCAmelCase : List[Any] = [ 'image_embeds', 'negative_image_embeds', 'image', ] __UpperCAmelCase : Optional[Any] = [ 'generator', 'height', 'width', 'strength', 'guidance_scale', 'num_inference_steps', 'return_dict', 'guidance_scale', 'num_images_per_prompt', 'output_type', 'return_dict', ] __UpperCAmelCase : Any = False @property def __UpperCAmelCase ( self ): return 32 @property def __UpperCAmelCase ( self ): return 32 @property def __UpperCAmelCase ( self ): return self.time_input_dim @property def __UpperCAmelCase ( self ): return self.time_input_dim * 4 @property def __UpperCAmelCase ( self ): return 100 @property def __UpperCAmelCase ( self ): torch.manual_seed(0 ) __a = { '''in_channels''': 4, # Out channels is double in channels because predicts mean and variance '''out_channels''': 8, '''addition_embed_type''': '''image''', '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''), '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''), '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''', '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2), '''layers_per_block''': 1, '''encoder_hid_dim''': self.text_embedder_hidden_size, '''encoder_hid_dim_type''': '''image_proj''', '''cross_attention_dim''': self.cross_attention_dim, '''attention_head_dim''': 4, '''resnet_time_scale_shift''': '''scale_shift''', '''class_embed_type''': None, } __a = UNetaDConditionModel(**_a ) return model @property def __UpperCAmelCase ( self ): return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __UpperCAmelCase ( self ): torch.manual_seed(0 ) __a = VQModel(**self.dummy_movq_kwargs ) return model def __UpperCAmelCase ( self ): __a = self.dummy_unet __a = self.dummy_movq __a = { '''num_train_timesteps''': 1_000, '''beta_schedule''': '''linear''', '''beta_start''': 0.0_0085, '''beta_end''': 0.012, '''clip_sample''': False, '''set_alpha_to_one''': False, '''steps_offset''': 0, '''prediction_type''': '''epsilon''', '''thresholding''': False, } __a = DDIMScheduler(**_a ) __a = { '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def __UpperCAmelCase ( self , _a , _a=0 ): __a = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(_a ) ).to(_a ) __a = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( _a ) # create init_image __a = floats_tensor((1, 3, 64, 64) , rng=random.Random(_a ) ).to(_a ) __a = image.cpu().permute(0 , 2 , 3 , 1 )[0] __a = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((256, 256) ) if str(_a ).startswith('''mps''' ): __a = torch.manual_seed(_a ) else: __a = torch.Generator(device=_a ).manual_seed(_a ) __a = { '''image''': init_image, '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 64, '''width''': 64, '''num_inference_steps''': 10, '''guidance_scale''': 7.0, '''strength''': 0.2, '''output_type''': '''np''', } return inputs def __UpperCAmelCase ( self ): __a = '''cpu''' __a = self.get_dummy_components() __a = self.pipeline_class(**_a ) __a = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) __a = pipe(**self.get_dummy_inputs(_a ) ) __a = output.images __a = pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] __a = image[0, -3:, -3:, -1] __a = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __a = np.array( [0.619_9778, 0.6398_4406, 0.4614_5785, 0.6294_4984, 0.562_2215, 0.4730_6132, 0.4744_1456, 0.460_7606, 0.4871_9263] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), f''' expected_slice {expected_slice}, but got {image_slice.flatten()}''' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), f''' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}''' @slow @require_torch_gpu class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self ): __a = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinskyv22/kandinskyv22_img2img_frog.npy''' ) __a = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png''' ) __a = '''A red cartoon frog, 4k''' __a = KandinskyVaaPriorPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-2-prior''' , torch_dtype=torch.floataa ) pipe_prior.to(_a ) __a = KandinskyVaaImgaImgPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-2-decoder''' , torch_dtype=torch.floataa ) __a = pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) __a = torch.Generator(device='''cpu''' ).manual_seed(0 ) __a , __a = pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt='''''' , ).to_tuple() __a = pipeline( image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=100 , height=768 , width=768 , strength=0.2 , output_type='''np''' , ) __a = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_a , _a )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str ) -> list: if n_term == "": return [] __a = [] for temp in range(int(lowerCAmelCase__ ) ): series.append(f'''1/{temp + 1}''' if series else '''1''' ) return series if __name__ == "__main__": lowercase_ = input("Enter the last number (nth term) of the Harmonic Series") print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n") print(harmonic_series(nth_term))
45
1
"""simple docstring""" from collections import deque def lowercase ( lowerCAmelCase__ : str ) -> List[str]: __a = len(lowerCAmelCase__ ) __a = deque() __a = [False for _ in range(lowerCAmelCase__ )] __a = [-1 for _ in range(lowerCAmelCase__ )] __a = index_of[:] def strong_connect(lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : str ): __a = index # the number when this node is seen __a = index # lowest rank node reachable from here index += 1 stack.append(lowerCAmelCase__ ) __a = True for w in g[v]: if index_of[w] == -1: __a = strong_connect(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) __a = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) elif on_stack[w]: __a = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) if lowlink_of[v] == index_of[v]: __a = [] __a = stack.pop() __a = False component.append(lowerCAmelCase__ ) while w != v: __a = stack.pop() __a = False component.append(lowerCAmelCase__ ) components.append(lowerCAmelCase__ ) return index __a = [] for v in range(lowerCAmelCase__ ): if index_of[v] == -1: strong_connect(lowerCAmelCase__ , 0 , lowerCAmelCase__ ) return components def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : Union[str, Any] ) -> Optional[int]: __a = [[] for _ in range(lowerCAmelCase__ )] for u, v in edges: g[u].append(lowerCAmelCase__ ) return g if __name__ == "__main__": # Test lowercase_ = 7 lowercase_ = [0, 0, 1, 2, 3, 3, 4, 4, 6] lowercase_ = [1, 3, 2, 0, 1, 4, 5, 6, 5] lowercase_ = [(u, v) for u, v in zip(source, target)] lowercase_ = create_graph(n_vertices, edges) assert [[5], [6], [4], [3, 2, 1, 0]] == tarjan(g)
45
"""simple docstring""" from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : List[str] __UpperCAmelCase : Optional[str] = None # Automatically constructed __UpperCAmelCase : ClassVar[str] = "dict" __UpperCAmelCase : ClassVar[Any] = None __UpperCAmelCase : str = field(default='Translation' , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __call__( self ): return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def __UpperCAmelCase ( self ): from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : Optional[List] = None __UpperCAmelCase : Optional[int] = None __UpperCAmelCase : Optional[str] = None # Automatically constructed __UpperCAmelCase : ClassVar[str] = "dict" __UpperCAmelCase : ClassVar[Any] = None __UpperCAmelCase : str = field(default='TranslationVariableLanguages' , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __UpperCAmelCase ( self ): __a = sorted(set(self.languages ) ) if self.languages else None __a = len(self.languages ) if self.languages else None def __call__( self ): return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def __UpperCAmelCase ( self , _a ): __a = set(self.languages ) if self.languages and set(_a ) - lang_set: raise ValueError( f'''Some languages in example ({', '.join(sorted(set(_a ) - lang_set ) )}) are not in valid set ({', '.join(_a )}).''' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. __a = [] for lang, text in translation_dict.items(): if isinstance(_a , _a ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. __a , __a = zip(*sorted(_a ) ) return {"language": languages, "translation": translations} def __UpperCAmelCase ( self ): from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
45
1
"""simple docstring""" from __future__ import annotations def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : str ) -> bool: __a = get_failure_array(lowerCAmelCase__ ) # 2) Step through text searching for pattern __a , __a = 0, 0 # index into text, pattern while i < len(lowerCAmelCase__ ): if pattern[j] == text[i]: if j == (len(lowerCAmelCase__ ) - 1): return True j += 1 # if this is a prefix in our pattern # just go back far enough to continue elif j > 0: __a = failure[j - 1] continue i += 1 return False def lowercase ( lowerCAmelCase__ : str ) -> list[int]: __a = [0] __a = 0 __a = 1 while j < len(lowerCAmelCase__ ): if pattern[i] == pattern[j]: i += 1 elif i > 0: __a = failure[i - 1] continue j += 1 failure.append(lowerCAmelCase__ ) return failure if __name__ == "__main__": # Test 1) lowercase_ = "abc1abc12" lowercase_ = "alskfjaldsabc1abc1abc12k23adsfabcabc" lowercase_ = "alskfjaldsk23adsfabcabc" assert kmp(pattern, texta) and not kmp(pattern, texta) # Test 2) lowercase_ = "ABABX" lowercase_ = "ABABZABABYABABX" assert kmp(pattern, text) # Test 3) lowercase_ = "AAAB" lowercase_ = "ABAAAAAB" assert kmp(pattern, text) # Test 4) lowercase_ = "abcdabcy" lowercase_ = "abcxabcdabxabcdabcdabcy" assert kmp(pattern, text) # Test 5) lowercase_ = "aabaabaaa" assert get_failure_array(pattern) == [0, 1, 0, 1, 2, 3, 4, 5, 2]
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> List[Any]: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) if n == 0: return 0 __a = float('''-inf''' ) for i in range(1 , n + 1 ): __a = max( lowerCAmelCase__ , prices[i - 1] + naive_cut_rod_recursive(n - i , lowerCAmelCase__ ) ) return max_revue def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> List[str]: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) __a = [float('''-inf''' ) for _ in range(n + 1 )] return _top_down_cut_rod_recursive(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list , lowerCAmelCase__ : list ) -> Union[str, Any]: if max_rev[n] >= 0: return max_rev[n] elif n == 0: return 0 else: __a = float('''-inf''' ) for i in range(1 , n + 1 ): __a = max( lowerCAmelCase__ , prices[i - 1] + _top_down_cut_rod_recursive(n - i , lowerCAmelCase__ , lowerCAmelCase__ ) , ) __a = max_revenue return max_rev[n] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> Dict: _enforce_args(lowerCAmelCase__ , lowerCAmelCase__ ) # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of # length 0. __a = [float('''-inf''' ) for _ in range(n + 1 )] __a = 0 for i in range(1 , n + 1 ): __a = max_rev[i] for j in range(1 , i + 1 ): __a = max(lowerCAmelCase__ , prices[j - 1] + max_rev[i - j] ) __a = max_revenue_i return max_rev[n] def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : list ) -> str: if n < 0: __a = f'''n must be greater than or equal to 0. Got n = {n}''' raise ValueError(lowerCAmelCase__ ) if n > len(lowerCAmelCase__ ): __a = ( '''Each integral piece of rod must have a corresponding price. ''' f'''Got n = {n} but length of prices = {len(lowerCAmelCase__ )}''' ) raise ValueError(lowerCAmelCase__ ) def lowercase ( ) -> int: __a = [6, 10, 12, 15, 20, 23] __a = len(lowerCAmelCase__ ) # the best revenue comes from cutting the rod into 6 pieces, each # of length 1 resulting in a revenue of 6 * 6 = 36. __a = 36 __a = top_down_cut_rod(lowerCAmelCase__ , lowerCAmelCase__ ) __a = bottom_up_cut_rod(lowerCAmelCase__ , lowerCAmelCase__ ) __a = naive_cut_rod_recursive(lowerCAmelCase__ , lowerCAmelCase__ ) assert expected_max_revenue == max_rev_top_down assert max_rev_top_down == max_rev_bottom_up assert max_rev_bottom_up == max_rev_naive if __name__ == "__main__": main()
45
1
"""simple docstring""" import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _a , _a=7 , _a=3 , _a=18 , _a=30 , _a=400 , _a=True , _a=None , _a=True , _a=[0.5, 0.5, 0.5] , _a=[0.5, 0.5, 0.5] , ): __a = size if size is not None else {'''height''': 18, '''width''': 18} __a = parent __a = batch_size __a = num_channels __a = image_size __a = min_resolution __a = max_resolution __a = do_resize __a = size __a = do_normalize __a = image_mean __a = image_std def __UpperCAmelCase ( self ): return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Dict = DPTImageProcessor if is_vision_available() else None def __UpperCAmelCase ( self ): __a = DPTImageProcessingTester(self ) @property def __UpperCAmelCase ( self ): return self.image_processor_tester.prepare_image_processor_dict() def __UpperCAmelCase ( self ): __a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , '''image_mean''' ) ) self.assertTrue(hasattr(_a , '''image_std''' ) ) self.assertTrue(hasattr(_a , '''do_normalize''' ) ) self.assertTrue(hasattr(_a , '''do_resize''' ) ) self.assertTrue(hasattr(_a , '''size''' ) ) def __UpperCAmelCase ( self ): __a = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) __a = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def __UpperCAmelCase ( self ): # Initialize image_processing __a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __a = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input __a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a = image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def __UpperCAmelCase ( self ): # Initialize image_processing __a = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __a = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input __a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a = image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def __UpperCAmelCase ( self ): # Initialize image_processing __a = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __a = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input __a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __a = image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , )
45
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) lowercase_ = {"configuration_unispeech": ["UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP", "UniSpeechConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST", "UniSpeechForCTC", "UniSpeechForPreTraining", "UniSpeechForSequenceClassification", "UniSpeechModel", "UniSpeechPreTrainedModel", ] if TYPE_CHECKING: from .configuration_unispeech import UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_unispeech import ( UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST, UniSpeechForCTC, UniSpeechForPreTraining, UniSpeechForSequenceClassification, UniSpeechModel, UniSpeechPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
1
"""simple docstring""" import unittest from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow if is_flax_available(): import jax from transformers.models.auto.modeling_flax_auto import FlaxAutoModel from transformers.models.bert.modeling_flax_bert import FlaxBertModel from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel @require_flax class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def __UpperCAmelCase ( self ): for model_name in ["bert-base-cased", "bert-large-uncased"]: with self.subTest(_a ): __a = AutoConfig.from_pretrained(_a ) self.assertIsNotNone(_a ) self.assertIsInstance(_a , _a ) __a = FlaxAutoModel.from_pretrained(_a ) self.assertIsNotNone(_a ) self.assertIsInstance(_a , _a ) @slow def __UpperCAmelCase ( self ): for model_name in ["roberta-base", "roberta-large"]: with self.subTest(_a ): __a = AutoConfig.from_pretrained(_a ) self.assertIsNotNone(_a ) self.assertIsInstance(_a , _a ) __a = FlaxAutoModel.from_pretrained(_a ) self.assertIsNotNone(_a ) self.assertIsInstance(_a , _a ) @slow def __UpperCAmelCase ( self ): for model_name in ["bert-base-cased", "bert-large-uncased"]: __a = AutoTokenizer.from_pretrained(_a ) __a = FlaxBertModel.from_pretrained(_a ) __a = tokenizer('''Do you support jax jitted function?''' , return_tensors=TensorType.JAX ) @jax.jit def eval(**_a ): return model(**_a ) eval(**_a ).block_until_ready() @slow def __UpperCAmelCase ( self ): for model_name in ["roberta-base", "roberta-large"]: __a = AutoTokenizer.from_pretrained(_a ) __a = FlaxRobertaModel.from_pretrained(_a ) __a = tokenizer('''Do you support jax jitted function?''' , return_tensors=TensorType.JAX ) @jax.jit def eval(**_a ): return model(**_a ) eval(**_a ).block_until_ready() def __UpperCAmelCase ( self ): with self.assertRaisesRegex( _a , '''bert-base is not a local folder and is not a valid model identifier''' ): __a = FlaxAutoModel.from_pretrained('''bert-base''' ) def __UpperCAmelCase ( self ): with self.assertRaisesRegex( _a , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): __a = FlaxAutoModel.from_pretrained(_a , revision='''aaaaaa''' ) def __UpperCAmelCase ( self ): with self.assertRaisesRegex( _a , '''hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack''' , ): __a = FlaxAutoModel.from_pretrained('''hf-internal-testing/config-no-model''' ) def __UpperCAmelCase ( self ): with self.assertRaisesRegex(_a , '''Use `from_pt=True` to load this model''' ): __a = FlaxAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only''' )
45
"""simple docstring""" import unittest import torch from torch import nn from diffusers.models.activations import get_activation class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = get_activation('''swish''' ) self.assertIsInstance(_a , nn.SiLU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''silu''' ) self.assertIsInstance(_a , nn.SiLU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''mish''' ) self.assertIsInstance(_a , nn.Mish ) self.assertEqual(act(torch.tensor(-200 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''gelu''' ) self.assertIsInstance(_a , nn.GELU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 )
45
1
"""simple docstring""" from importlib import import_module from .logging import get_logger lowercase_ = get_logger(__name__) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a=None ): __a = attrs or [] if module is not None: for key in module.__dict__: if key in attrs or not key.startswith('''__''' ): setattr(self , _a , getattr(_a , _a ) ) __a = module._original_module if isinstance(_a , _PatchedModuleObj ) else module class __lowerCAmelCase : '''simple docstring''' __UpperCAmelCase : int = [] def __init__( self , _a , _a , _a , _a=None ): __a = obj __a = target __a = new __a = target.split('''.''' )[0] __a = {} __a = attrs or [] def __enter__( self ): *__a , __a = self.target.split('''.''' ) # Patch modules: # it's used to patch attributes of submodules like "os.path.join"; # in this case we need to patch "os" and "os.path" for i in range(len(_a ) ): try: __a = import_module('''.'''.join(submodules[: i + 1] ) ) except ModuleNotFoundError: continue # We iterate over all the globals in self.obj in case we find "os" or "os.path" for attr in self.obj.__dir__(): __a = getattr(self.obj , _a ) # We don't check for the name of the global, but rather if its value *is* "os" or "os.path". # This allows to patch renamed modules like "from os import path as ospath". if obj_attr is submodule or ( (isinstance(_a , _PatchedModuleObj ) and obj_attr._original_module is submodule) ): __a = obj_attr # patch at top level setattr(self.obj , _a , _PatchedModuleObj(_a , attrs=self.attrs ) ) __a = getattr(self.obj , _a ) # construct lower levels patches for key in submodules[i + 1 :]: setattr(_a , _a , _PatchedModuleObj(getattr(_a , _a , _a ) , attrs=self.attrs ) ) __a = getattr(_a , _a ) # finally set the target attribute setattr(_a , _a , self.new ) # Patch attribute itself: # it's used for builtins like "open", # and also to patch "os.path.join" we may also need to patch "join" # itself if it was imported as "from os.path import join". if submodules: # if it's an attribute of a submodule like "os.path.join" try: __a = getattr(import_module('''.'''.join(_a ) ) , _a ) except (AttributeError, ModuleNotFoundError): return # We iterate over all the globals in self.obj in case we find "os.path.join" for attr in self.obj.__dir__(): # We don't check for the name of the global, but rather if its value *is* "os.path.join". # This allows to patch renamed attributes like "from os.path import join as pjoin". if getattr(self.obj , _a ) is attr_value: __a = getattr(self.obj , _a ) setattr(self.obj , _a , self.new ) elif target_attr in globals()["__builtins__"]: # if it'a s builtin like "open" __a = globals()['''__builtins__'''][target_attr] setattr(self.obj , _a , self.new ) else: raise RuntimeError(f'''Tried to patch attribute {target_attr} instead of a submodule.''' ) def __exit__( self , *_a ): for attr in list(self.original ): setattr(self.obj , _a , self.original.pop(_a ) ) def __UpperCAmelCase ( self ): self.__enter__() self._active_patches.append(self ) def __UpperCAmelCase ( self ): try: self._active_patches.remove(self ) except ValueError: # If the patch hasn't been started this will fail return None return self.__exit__()
45
"""simple docstring""" from __future__ import annotations import unittest from transformers import EsmConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy import tensorflow as tf from transformers.models.esm.modeling_tf_esm import ( TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, TFEsmModel, ) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , ): __a = parent __a = 13 __a = 7 __a = True __a = True __a = True __a = 99 __a = 32 __a = 2 __a = 4 __a = 37 __a = '''gelu''' __a = 0.1 __a = 0.1 __a = 512 __a = 16 __a = 2 __a = 0.02 __a = 3 __a = 4 __a = None def __UpperCAmelCase ( self ): __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a = None if self.use_input_mask: __a = random_attention_mask([self.batch_size, self.seq_length] ) __a = None __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __a = ids_tensor([self.batch_size] , self.num_choices ) __a = EsmConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , pad_token_id=1 , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def __UpperCAmelCase ( self ): ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = self.prepare_config_and_inputs() __a = True __a = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __a = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , _a ): __a = TFEsmModel(config=_a ) __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} __a = model(_a ) __a = [input_ids, input_mask] __a = model(_a ) __a = model(_a ) 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 , _a , ): __a = True __a = TFEsmModel(config=_a ) __a = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''encoder_hidden_states''': encoder_hidden_states, '''encoder_attention_mask''': encoder_attention_mask, } __a = model(_a ) __a = [input_ids, input_mask] __a = model(_a , encoder_hidden_states=_a ) # Also check the case where encoder outputs are not passed __a = model(_a , attention_mask=_a ) 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 = TFEsmForMaskedLM(config=_a ) __a = model([input_ids, input_mask] ) 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 = self.num_labels __a = TFEsmForTokenClassification(config=_a ) __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} __a = model(_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = config_and_inputs __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = ( ( TFEsmModel, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, ) if is_tf_available() else () ) __UpperCAmelCase : Tuple = ( { 'feature-extraction': TFEsmModel, 'fill-mask': TFEsmForMaskedLM, 'text-classification': TFEsmForSequenceClassification, 'token-classification': TFEsmForTokenClassification, 'zero-shot': TFEsmForSequenceClassification, } if is_tf_available() else {} ) __UpperCAmelCase : Tuple = False __UpperCAmelCase : Union[str, Any] = False def __UpperCAmelCase ( self ): __a = TFEsmModelTester(self ) __a = ConfigTester(self , config_class=_a , hidden_size=37 ) def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_a ) @slow def __UpperCAmelCase ( self ): for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = TFEsmModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip('''Protein models do not support embedding resizing.''' ) def __UpperCAmelCase ( self ): pass @unittest.skip('''Protein models do not support embedding resizing.''' ) def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): __a , __a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a = model_class(_a ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class is TFEsmForMaskedLM: # Output embedding test differs from the main test because they're a matrix, not a layer __a = model.get_bias() assert isinstance(_a , _a ) for k, v in name.items(): assert isinstance(_a , tf.Variable ) else: __a = model.get_output_embeddings() assert x is None __a = model.get_bias() assert name is None @require_tf class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def __UpperCAmelCase ( self ): __a = TFEsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) __a = tf.constant([[0, 1, 2, 3, 4, 5]] ) __a = model(_a )[0] __a = [1, 6, 33] self.assertEqual(list(output.numpy().shape ) , _a ) # compare the actual values for a slice. __a = tf.constant( [ [ [8.92_1518, -10.58_9814, -6.467_1307], [-6.396_7156, -13.91_1377, -1.121_1915], [-7.78_1247, -13.95_1557, -3.74_0592], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-2 ) ) @slow def __UpperCAmelCase ( self ): __a = TFEsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) __a = tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) __a = model(_a )[0] # compare the actual values for a slice. __a = tf.constant( [ [ [0.1444_3092, 0.5412_5327, 0.324_7739], [0.3034_0484, 0.0052_6676, 0.3107_7722], [0.3227_8043, -0.2498_7096, 0.341_4628], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-4 ) )
45
1
"""simple docstring""" import inspect import unittest from transformers import MobileNetVaConfig 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 transformers import MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_a , '''tf_padding''' ) ) self.parent.assertTrue(hasattr(_a , '''depth_multiplier''' ) ) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a=13 , _a=3 , _a=32 , _a=0.25 , _a=8 , _a=8 , _a=6 , _a=32 , _a=True , _a=True , _a=True , _a="relu6" , _a=1_280 , _a=0.1 , _a=0.02 , _a=True , _a=True , _a=10 , _a=None , ): __a = parent __a = batch_size __a = num_channels __a = image_size __a = depth_multiplier __a = depth_divisible_by __a = min_depth __a = expand_ratio __a = tf_padding __a = output_stride __a = first_layer_is_expansion __a = finegrained_output __a = hidden_act __a = last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier ) __a = classifier_dropout_prob __a = use_labels __a = is_training __a = num_labels __a = initializer_range __a = scope def __UpperCAmelCase ( self ): __a = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.num_labels ) __a = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) __a = self.get_config() return config, pixel_values, labels, pixel_labels def __UpperCAmelCase ( self ): return MobileNetVaConfig( num_channels=self.num_channels , image_size=self.image_size , depth_multiplier=self.depth_multiplier , depth_divisible_by=self.depth_divisible_by , min_depth=self.min_depth , expand_ratio=self.expand_ratio , output_stride=self.output_stride , first_layer_is_expansion=self.first_layer_is_expansion , finegrained_output=self.finegrained_output , hidden_act=self.hidden_act , tf_padding=self.tf_padding , classifier_dropout_prob=self.classifier_dropout_prob , initializer_range=self.initializer_range , ) def __UpperCAmelCase ( self , _a , _a , _a , _a ): __a = MobileNetVaModel(config=_a ) model.to(_a ) model.eval() __a = model(_a ) self.parent.assertEqual( result.last_hidden_state.shape , ( self.batch_size, self.last_hidden_size, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) self.parent.assertEqual( result.pooler_output.shape , (self.batch_size, self.last_hidden_size) , ) def __UpperCAmelCase ( self , _a , _a , _a , _a ): __a = self.num_labels __a = MobileNetVaForImageClassification(_a ) model.to(_a ) model.eval() __a = model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __UpperCAmelCase ( self , _a , _a , _a , _a ): __a = self.num_labels __a = MobileNetVaForSemanticSegmentation(_a ) model.to(_a ) model.eval() __a = model(_a ) self.parent.assertEqual( result.logits.shape , ( self.batch_size, self.num_labels, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) __a = model(_a , labels=_a ) self.parent.assertEqual( result.logits.shape , ( self.batch_size, self.num_labels, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() __a , __a , __a , __a = config_and_inputs __a = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : int = ( (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation) if is_torch_available() else () ) __UpperCAmelCase : str = ( { 'feature-extraction': MobileNetVaModel, 'image-classification': MobileNetVaForImageClassification, 'image-segmentation': MobileNetVaForSemanticSegmentation, } if is_torch_available() else {} ) __UpperCAmelCase : List[str] = False __UpperCAmelCase : Optional[Any] = False __UpperCAmelCase : Optional[Any] = False __UpperCAmelCase : str = False def __UpperCAmelCase ( self ): __a = MobileNetVaModelTester(self ) __a = MobileNetVaConfigTester(self , config_class=_a , has_text_modality=_a ) def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() @unittest.skip(reason='''MobileNetV2 does not use inputs_embeds''' ) def __UpperCAmelCase ( self ): pass @unittest.skip(reason='''MobileNetV2 does not support input and output embeddings''' ) def __UpperCAmelCase ( self ): pass @unittest.skip(reason='''MobileNetV2 does not output attentions''' ) def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): __a , __a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a = model_class(_a ) __a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __a = [*signature.parameters.keys()] __a = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __UpperCAmelCase ( self ): def check_hidden_states_output(_a , _a , _a ): __a = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): __a = model(**self._prepare_for_class(_a , _a ) ) __a = outputs.hidden_states __a = 16 self.assertEqual(len(_a ) , _a ) __a , __a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a = True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __a = True check_hidden_states_output(_a , _a , _a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*_a ) @slow def __UpperCAmelCase ( self ): for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = MobileNetVaModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def lowercase ( ) -> Dict: __a = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @cached_property def __UpperCAmelCase ( self ): return ( MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v2_1.0_224''' ) if is_vision_available() else None ) @slow def __UpperCAmelCase ( self ): __a = MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v2_1.0_224''' ).to(_a ) __a = self.default_image_processor __a = prepare_img() __a = image_processor(images=_a , return_tensors='''pt''' ).to(_a ) # forward pass with torch.no_grad(): __a = model(**_a ) # verify the logits __a = torch.Size((1, 1_001) ) self.assertEqual(outputs.logits.shape , _a ) __a = torch.tensor([0.2445, -1.1993, 0.1905] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1E-4 ) ) @slow def __UpperCAmelCase ( self ): __a = MobileNetVaForSemanticSegmentation.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513''' ) __a = model.to(_a ) __a = MobileNetVaImageProcessor.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513''' ) __a = prepare_img() __a = image_processor(images=_a , return_tensors='''pt''' ).to(_a ) # forward pass with torch.no_grad(): __a = model(**_a ) __a = outputs.logits # verify the logits __a = torch.Size((1, 21, 65, 65) ) self.assertEqual(logits.shape , _a ) __a = torch.tensor( [ [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]], [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]], [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]], ] , device=_a , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , _a , atol=1E-4 ) )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : int ) -> str: if number > 0: raise ValueError('''input must be a negative integer''' ) __a = len(bin(lowerCAmelCase__ )[3:] ) __a = bin(abs(lowerCAmelCase__ ) - (1 << binary_number_length) )[3:] __a = ( ( '''1''' + '''0''' * (binary_number_length - len(lowerCAmelCase__ )) + twos_complement_number ) if number < 0 else '''0''' ) return "0b" + twos_complement_number if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" import argparse import re from pathlib import Path import requests import torch from PIL import Image from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor from transformers import ( EfficientFormerConfig, EfficientFormerForImageClassificationWithTeacher, EfficientFormerImageProcessor, ) from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Any ) -> str: __a = old_name if "patch_embed" in old_name: __a , __a , __a = old_name.split('''.''' ) if layer == "0": __a = old_name.replace('''0''' , '''convolution1''' ) elif layer == "1": __a = old_name.replace('''1''' , '''batchnorm_before''' ) elif layer == "3": __a = old_name.replace('''3''' , '''convolution2''' ) else: __a = old_name.replace('''4''' , '''batchnorm_after''' ) if "network" in old_name and re.search(r'''\d\.\d''' , lowerCAmelCase__ ): __a = r'''\b\d{2}\b''' if bool(re.search(lowerCAmelCase__ , lowerCAmelCase__ ) ): __a = re.search(r'''\d\.\d\d.''' , lowerCAmelCase__ ).group() else: __a = re.search(r'''\d\.\d.''' , lowerCAmelCase__ ).group() if int(match[0] ) < 6: __a = old_name.replace(lowerCAmelCase__ , '''''' ) __a = trimmed_name.replace('''network''' , match[0] + '''.meta4D_layers.blocks.''' + match[2:-1] ) __a = '''intermediate_stages.''' + trimmed_name else: __a = old_name.replace(lowerCAmelCase__ , '''''' ) if int(match[2] ) < num_meta4D_last_stage: __a = trimmed_name.replace('''network''' , '''meta4D_layers.blocks.''' + match[2] ) else: __a = str(int(match[2] ) - num_meta4D_last_stage ) __a = trimmed_name.replace('''network''' , '''meta3D_layers.blocks.''' + layer_index ) if "norm1" in old_name: __a = trimmed_name.replace('''norm1''' , '''layernorm1''' ) elif "norm2" in old_name: __a = trimmed_name.replace('''norm2''' , '''layernorm2''' ) elif "fc1" in old_name: __a = trimmed_name.replace('''fc1''' , '''linear_in''' ) elif "fc2" in old_name: __a = trimmed_name.replace('''fc2''' , '''linear_out''' ) __a = '''last_stage.''' + trimmed_name elif "network" in old_name and re.search(r'''.\d.''' , lowerCAmelCase__ ): __a = old_name.replace('''network''' , '''intermediate_stages''' ) if "fc" in new_name: __a = new_name.replace('''fc''' , '''convolution''' ) elif ("norm1" in new_name) and ("layernorm1" not in new_name): __a = new_name.replace('''norm1''' , '''batchnorm_before''' ) elif ("norm2" in new_name) and ("layernorm2" not in new_name): __a = new_name.replace('''norm2''' , '''batchnorm_after''' ) if "proj" in new_name: __a = new_name.replace('''proj''' , '''projection''' ) if "dist_head" in new_name: __a = new_name.replace('''dist_head''' , '''distillation_classifier''' ) elif "head" in new_name: __a = new_name.replace('''head''' , '''classifier''' ) elif "patch_embed" in new_name: __a = '''efficientformer.''' + new_name elif new_name == "norm.weight" or new_name == "norm.bias": __a = new_name.replace('''norm''' , '''layernorm''' ) __a = '''efficientformer.''' + new_name else: __a = '''efficientformer.encoder.''' + new_name return new_name def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Tuple ) -> Tuple: for key in checkpoint.copy().keys(): __a = checkpoint.pop(lowerCAmelCase__ ) __a = val return checkpoint def lowercase ( ) -> Any: __a = '''http://images.cocodataset.org/val2017/000000039769.jpg''' __a = Image.open(requests.get(lowerCAmelCase__ , stream=lowerCAmelCase__ ).raw ) return image def lowercase ( lowerCAmelCase__ : Path , lowerCAmelCase__ : Path , lowerCAmelCase__ : Path , lowerCAmelCase__ : bool ) -> Any: __a = torch.load(lowerCAmelCase__ , map_location='''cpu''' )['''model'''] __a = EfficientFormerConfig.from_json_file(lowerCAmelCase__ ) __a = EfficientFormerForImageClassificationWithTeacher(lowerCAmelCase__ ) __a = '''_'''.join(checkpoint_path.split('''/''' )[-1].split('''.''' )[0].split('''_''' )[:-1] ) __a = config.depths[-1] - config.num_metaad_blocks + 1 __a = convert_torch_checkpoint(lowerCAmelCase__ , lowerCAmelCase__ ) model.load_state_dict(lowerCAmelCase__ ) model.eval() __a = { '''bilinear''': PILImageResampling.BILINEAR, '''bicubic''': PILImageResampling.BICUBIC, '''nearest''': PILImageResampling.NEAREST, } # prepare image __a = prepare_img() __a = 256 __a = 224 __a = EfficientFormerImageProcessor( size={'''shortest_edge''': image_size} , crop_size={'''height''': crop_size, '''width''': crop_size} , resample=pillow_resamplings['''bicubic'''] , ) __a = processor(images=lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values # original processing pipeline __a = Compose( [ Resize(lowerCAmelCase__ , interpolation=pillow_resamplings['''bicubic'''] ), CenterCrop(lowerCAmelCase__ ), ToTensor(), Normalize(lowerCAmelCase__ , lowerCAmelCase__ ), ] ) __a = image_transforms(lowerCAmelCase__ ).unsqueeze(0 ) assert torch.allclose(lowerCAmelCase__ , lowerCAmelCase__ ) __a = model(lowerCAmelCase__ ) __a = outputs.logits __a = (1, 1000) if "l1" in model_name: __a = torch.Tensor( [-0.13_12, 0.43_53, -1.04_99, -0.51_24, 0.41_83, -0.67_93, -1.37_77, -0.08_93, -0.73_58, -2.43_28] ) assert torch.allclose(logits[0, :10] , lowerCAmelCase__ , atol=1e-3 ) assert logits.shape == expected_shape elif "l3" in model_name: __a = torch.Tensor( [-1.31_50, -1.54_56, -1.25_56, -0.84_96, -0.71_27, -0.78_97, -0.97_28, -0.30_52, 0.37_51, -0.31_27] ) assert torch.allclose(logits[0, :10] , lowerCAmelCase__ , atol=1e-3 ) assert logits.shape == expected_shape elif "l7" in model_name: __a = torch.Tensor( [-1.02_83, -1.41_31, -0.56_44, -1.31_15, -0.57_85, -1.20_49, -0.75_28, 0.19_92, -0.38_22, -0.08_78] ) assert logits.shape == expected_shape else: raise ValueError( f'''Unknown model checkpoint: {checkpoint_path}. Supported version of efficientformer are l1, l3 and l7''' ) # Save Checkpoints Path(lowerCAmelCase__ ).mkdir(exist_ok=lowerCAmelCase__ ) model.save_pretrained(lowerCAmelCase__ ) print(f'''Checkpoint successfuly converted. Model saved at {pytorch_dump_path}''' ) processor.save_pretrained(lowerCAmelCase__ ) print(f'''Processor successfuly saved at {pytorch_dump_path}''' ) if push_to_hub: print('''Pushing model to the hub...''' ) model.push_to_hub( repo_id=f'''Bearnardd/{pytorch_dump_path}''' , commit_message='''Add model''' , use_temp_dir=lowerCAmelCase__ , ) processor.push_to_hub( repo_id=f'''Bearnardd/{pytorch_dump_path}''' , commit_message='''Add image processor''' , use_temp_dir=lowerCAmelCase__ , ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--pytorch_model_path", default=None, type=str, required=True, help="Path to EfficientFormer pytorch checkpoint.", ) parser.add_argument( "--config_file", default=None, type=str, required=True, help="The json file for EfficientFormer model config.", ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument("--push_to_hub", action="store_true", help="Push model and image processor to the hub") parser.add_argument( "--no-push_to_hub", dest="push_to_hub", action="store_false", help="Do not push model and image processor to the hub", ) parser.set_defaults(push_to_hub=True) lowercase_ = parser.parse_args() convert_efficientformer_checkpoint( checkpoint_path=args.pytorch_model_path, efficientformer_config_file=args.config_file, pytorch_dump_path=args.pytorch_dump_path, push_to_hub=args.push_to_hub, )
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : list[str] ) -> str: __a = '''''' for word_or_phrase in separated: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise Exception('''join() accepts only strings to be joined''' ) joined += word_or_phrase + separator return joined.strip(lowerCAmelCase__ ) if __name__ == "__main__": from doctest import testmod testmod()
45
1
"""simple docstring""" import numpy as np import datasets lowercase_ = "\nCompute the Mahalanobis Distance\n\nMahalonobis distance is the distance between a point and a distribution.\nAnd not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance.\nIt was introduced by Prof. P. C. Mahalanobis in 1936\nand has been used in various statistical applications ever since\n[source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/]\n" lowercase_ = "\\n@article{de2000mahalanobis,\n title={The mahalanobis distance},\n author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L},\n journal={Chemometrics and intelligent laboratory systems},\n volume={50},\n number={1},\n pages={1--18},\n year={2000},\n publisher={Elsevier}\n}\n" lowercase_ = "\nArgs:\n X: List of datapoints to be compared with the `reference_distribution`.\n reference_distribution: List of datapoints from the reference distribution we want to compare to.\nReturns:\n mahalanobis: The Mahalonobis distance for each datapoint in `X`.\nExamples:\n\n >>> mahalanobis_metric = datasets.load_metric(\"mahalanobis\")\n >>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]])\n >>> print(results)\n {'mahalanobis': array([0.5])}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __lowerCAmelCase ( datasets.Metric ): '''simple docstring''' def __UpperCAmelCase ( self ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''X''': datasets.Sequence(datasets.Value('''float''' , id='''sequence''' ) , id='''X''' ), } ) , ) def __UpperCAmelCase ( self , _a , _a ): # convert to numpy arrays __a = np.array(_a ) __a = np.array(_a ) # Assert that arrays are 2D if len(X.shape ) != 2: raise ValueError('''Expected `X` to be a 2D vector''' ) if len(reference_distribution.shape ) != 2: raise ValueError('''Expected `reference_distribution` to be a 2D vector''' ) if reference_distribution.shape[0] < 2: raise ValueError( '''Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension''' ) # Get mahalanobis distance for each prediction __a = X - np.mean(_a ) __a = np.cov(reference_distribution.T ) try: __a = np.linalg.inv(_a ) except np.linalg.LinAlgError: __a = np.linalg.pinv(_a ) __a = np.dot(_a , _a ) __a = np.dot(_a , X_minus_mu.T ).diagonal() return {"mahalanobis": mahal_dist}
45
"""simple docstring""" lowercase_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" def lowercase ( lowerCAmelCase__ : bytes ) -> bytes: # Make sure the supplied data is a bytes-like object if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = f'''a bytes-like object is required, not \'{data.__class__.__name__}\'''' raise TypeError(lowerCAmelCase__ ) __a = ''''''.join(bin(lowerCAmelCase__ )[2:].zfill(8 ) for byte in data ) __a = len(lowerCAmelCase__ ) % 6 != 0 if padding_needed: # The padding that will be added later __a = b'''=''' * ((6 - len(lowerCAmelCase__ ) % 6) // 2) # Append binary_stream with arbitrary binary digits (0's by default) to make its # length a multiple of 6. binary_stream += "0" * (6 - len(lowerCAmelCase__ ) % 6) else: __a = b'''''' # Encode every 6 binary digits to their corresponding Base64 character return ( "".join( B64_CHARSET[int(binary_stream[index : index + 6] , 2 )] for index in range(0 , len(lowerCAmelCase__ ) , 6 ) ).encode() + padding ) def lowercase ( lowerCAmelCase__ : str ) -> bytes: # Make sure encoded_data is either a string or a bytes-like object if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): __a = ( '''argument should be a bytes-like object or ASCII string, ''' f'''not \'{encoded_data.__class__.__name__}\'''' ) raise TypeError(lowerCAmelCase__ ) # In case encoded_data is a bytes-like object, make sure it contains only # ASCII characters so we convert it to a string object if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): try: __a = encoded_data.decode('''utf-8''' ) except UnicodeDecodeError: raise ValueError('''base64 encoded data should only contain ASCII characters''' ) __a = encoded_data.count('''=''' ) # Check if the encoded string contains non base64 characters if padding: assert all( char in B64_CHARSET for char in encoded_data[:-padding] ), "Invalid base64 character(s) found." else: assert all( char in B64_CHARSET for char in encoded_data ), "Invalid base64 character(s) found." # Check the padding assert len(lowerCAmelCase__ ) % 4 == 0 and padding < 3, "Incorrect padding" if padding: # Remove padding if there is one __a = encoded_data[:-padding] __a = ''''''.join( bin(B64_CHARSET.index(lowerCAmelCase__ ) )[2:].zfill(6 ) for char in encoded_data )[: -padding * 2] else: __a = ''''''.join( bin(B64_CHARSET.index(lowerCAmelCase__ ) )[2:].zfill(6 ) for char in encoded_data ) __a = [ int(binary_stream[index : index + 8] , 2 ) for index in range(0 , len(lowerCAmelCase__ ) , 8 ) ] return bytes(lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" lowercase_ = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] lowercase_ = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] lowercase_ = { 0: "Sunday", 1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", 5: "Friday", 6: "Saturday", } def lowercase ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int ) -> str: assert len(str(lowerCAmelCase__ ) ) > 2, "year should be in YYYY format" assert 1 <= month <= 12, "month should be between 1 to 12" assert 1 <= day <= 31, "day should be between 1 to 31" # Doomsday algorithm: __a = year // 100 __a = (5 * (century % 4) + 2) % 7 __a = year % 100 __a = centurian % 12 __a = ( (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor ) % 7 __a = ( DOOMSDAY_NOT_LEAP[month - 1] if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) else DOOMSDAY_LEAP[month - 1] ) __a = (dooms_day + day - day_anchor) % 7 return WEEK_DAY_NAMES[week_day] if __name__ == "__main__": import doctest doctest.testmod()
45
"""simple docstring""" import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = inspect.getfile(accelerate.test_utils ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) __a = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_distributed_data_loop.py'''] ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_ops.py'''] ) @require_multi_gpu def __UpperCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices.''' ) __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_a , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices.''' ) __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.operation_file_path] print(f'''Command: {cmd}''' ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_a , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self ): __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_a , env=os.environ.copy() ) @require_multi_gpu def __UpperCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices, using 2 devices only''' ) __a = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices='''0,1''' ): execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": lowercase_ = Accelerator() lowercase_ = (accelerator.state.process_index + 2, 1_0) lowercase_ = torch.randint(0, 1_0, shape).to(accelerator.device) lowercase_ = "" lowercase_ = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." lowercase_ = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." lowercase_ = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
45
1
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionInstructPixaPixPipeline, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.utils import floats_tensor, load_image, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = StableDiffusionInstructPixaPixPipeline __UpperCAmelCase : Any = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'height', 'width', 'cross_attention_kwargs'} __UpperCAmelCase : Optional[int] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __UpperCAmelCase : Any = IMAGE_TO_IMAGE_IMAGE_PARAMS __UpperCAmelCase : Any = IMAGE_TO_IMAGE_IMAGE_PARAMS def __UpperCAmelCase ( self ): torch.manual_seed(0 ) __a = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=8 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) __a = PNDMScheduler(skip_prk_steps=_a ) torch.manual_seed(0 ) __a = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) __a = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) __a = CLIPTextModel(_a ) __a = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __a = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def __UpperCAmelCase ( self , _a , _a=0 ): __a = floats_tensor((1, 3, 32, 32) , rng=random.Random(_a ) ).to(_a ) __a = image.cpu().permute(0 , 2 , 3 , 1 )[0] __a = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ) if str(_a ).startswith('''mps''' ): __a = torch.manual_seed(_a ) else: __a = torch.Generator(device=_a ).manual_seed(_a ) __a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''image_guidance_scale''': 1, '''output_type''': '''numpy''', } return inputs def __UpperCAmelCase ( self ): __a = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a = self.get_dummy_components() __a = StableDiffusionInstructPixaPixPipeline(**_a ) __a = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = sd_pipe(**_a ).images __a = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) __a = np.array([0.7526, 0.3750, 0.4547, 0.6117, 0.5866, 0.5016, 0.4327, 0.5642, 0.4815] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __UpperCAmelCase ( self ): __a = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a = self.get_dummy_components() __a = StableDiffusionInstructPixaPixPipeline(**_a ) __a = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = '''french fries''' __a = sd_pipe(**_a , negative_prompt=_a ) __a = output.images __a = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) __a = np.array([0.7511, 0.3642, 0.4553, 0.6236, 0.5797, 0.5013, 0.4343, 0.5611, 0.4831] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __UpperCAmelCase ( self ): __a = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a = self.get_dummy_components() __a = StableDiffusionInstructPixaPixPipeline(**_a ) __a = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = [inputs['''prompt''']] * 2 __a = np.array(inputs['''image'''] ).astype(np.floataa ) / 255.0 __a = torch.from_numpy(_a ).unsqueeze(0 ).to(_a ) __a = image / 2 + 0.5 __a = image.permute(0 , 3 , 1 , 2 ) __a = image.repeat(2 , 1 , 1 , 1 ) __a = sd_pipe(**_a ).images __a = image[-1, -3:, -3:, -1] assert image.shape == (2, 32, 32, 3) __a = np.array([0.5812, 0.5748, 0.5222, 0.5908, 0.5695, 0.7174, 0.6804, 0.5523, 0.5579] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __UpperCAmelCase ( self ): __a = '''cpu''' # ensure determinism for the device-dependent torch.Generator __a = self.get_dummy_components() __a = EulerAncestralDiscreteScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule='''scaled_linear''' ) __a = StableDiffusionInstructPixaPixPipeline(**_a ) __a = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) __a = self.get_dummy_inputs(_a ) __a = sd_pipe(**_a ).images __a = image[0, -3:, -3:, -1] __a = [round(_a , 4 ) for x in image_slice.flatten().tolist()] print(''','''.join([str(_a ) for x in slice] ) ) assert image.shape == (1, 32, 32, 3) __a = np.array([0.7417, 0.3842, 0.4732, 0.5776, 0.5891, 0.5139, 0.4052, 0.5673, 0.4986] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __UpperCAmelCase ( self ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) def __UpperCAmelCase ( self ): __a = self.get_dummy_components() __a = StableDiffusionInstructPixaPixPipeline(**_a ) __a = VaeImageProcessor(do_resize=_a , do_normalize=_a ) __a = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) __a = pipe(**self.get_dummy_inputs_by_type(_a , input_image_type='''pt''' ) )[0] __a = components['''vae'''] __a = self.get_dummy_inputs_by_type(_a , input_image_type='''pt''' ) for image_param in self.image_latents_params: if image_param in inputs.keys(): __a = vae.encode(inputs[image_param] ).latent_dist.mode() __a = pipe(**_a )[0] __a = np.abs(out - out_latents_inputs ).max() self.assertLess(_a , 1E-4 , '''passing latents as image input generate different result from passing image''' ) @slow @require_torch_gpu class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self , _a=0 ): __a = torch.manual_seed(_a ) __a = load_image( '''https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg''' ) __a = { '''prompt''': '''turn him into a cyborg''', '''image''': image, '''generator''': generator, '''num_inference_steps''': 3, '''guidance_scale''': 7.5, '''image_guidance_scale''': 1.0, '''output_type''': '''numpy''', } return inputs def __UpperCAmelCase ( self ): __a = StableDiffusionInstructPixaPixPipeline.from_pretrained( '''timbrooks/instruct-pix2pix''' , safety_checker=_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = self.get_inputs() __a = pipe(**_a ).images __a = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) __a = np.array([0.5902, 0.6015, 0.6027, 0.5983, 0.6092, 0.6061, 0.5765, 0.5785, 0.5555] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __UpperCAmelCase ( self ): __a = StableDiffusionInstructPixaPixPipeline.from_pretrained( '''timbrooks/instruct-pix2pix''' , safety_checker=_a ) __a = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = self.get_inputs() __a = pipe(**_a ).images __a = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) __a = np.array([0.6578, 0.6817, 0.6972, 0.6761, 0.6856, 0.6916, 0.6428, 0.6516, 0.6301] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __UpperCAmelCase ( self ): __a = StableDiffusionInstructPixaPixPipeline.from_pretrained( '''timbrooks/instruct-pix2pix''' , safety_checker=_a ) __a = DDIMScheduler.from_config(pipe.scheduler.config ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = self.get_inputs() __a = pipe(**_a ).images __a = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) __a = np.array([0.3828, 0.3834, 0.3818, 0.3792, 0.3865, 0.3752, 0.3792, 0.3847, 0.3753] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __UpperCAmelCase ( self ): __a = 0 def callback_fn(_a , _a , _a ) -> None: __a = True nonlocal number_of_steps number_of_steps += 1 if step == 1: __a = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) __a = latents[0, -3:, -3:, -1] __a = np.array([-0.2463, -0.4644, -0.9756, 1.5176, 1.4414, 0.7866, 0.9897, 0.8521, 0.7983] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 elif step == 2: __a = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) __a = latents[0, -3:, -3:, -1] __a = np.array([-0.2644, -0.4626, -0.9653, 1.5176, 1.4551, 0.7686, 0.9805, 0.8452, 0.8115] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 __a = False __a = StableDiffusionInstructPixaPixPipeline.from_pretrained( '''timbrooks/instruct-pix2pix''' , safety_checker=_a , torch_dtype=torch.floataa ) __a = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = self.get_inputs() pipe(**_a , callback=_a , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def __UpperCAmelCase ( self ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __a = StableDiffusionInstructPixaPixPipeline.from_pretrained( '''timbrooks/instruct-pix2pix''' , safety_checker=_a , torch_dtype=torch.floataa ) __a = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() __a = self.get_inputs() __a = pipe(**_a ) __a = torch.cuda.max_memory_allocated() # make sure that less than 2.2 GB is allocated assert mem_bytes < 2.2 * 10**9 def __UpperCAmelCase ( self ): __a = self.get_inputs() # resize to resolution that is divisible by 8 but not 16 or 32 __a = inputs['''image'''].resize((504, 504) ) __a = '''timbrooks/instruct-pix2pix''' __a = StableDiffusionInstructPixaPixPipeline.from_pretrained( _a , safety_checker=_a , ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() __a = pipe(**_a ) __a = output.images[0] __a = image[255:258, 383:386, -1] assert image.shape == (504, 504, 3) __a = np.array([0.2726, 0.2529, 0.2664, 0.2655, 0.2641, 0.2642, 0.2591, 0.2649, 0.2590] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3
45
"""simple docstring""" import numpy as np def lowercase ( lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : float ) -> np.ndarray: return np.where(vector > 0 , lowerCAmelCase__ , (alpha * (np.exp(lowerCAmelCase__ ) - 1)) ) if __name__ == "__main__": import doctest doctest.testmod()
45
1
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "google/mobilenet_v1_1.0_224": "https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json", "google/mobilenet_v1_0.75_192": "https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json", # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Optional[int] = 'mobilenet_v1' def __init__( self , _a=3 , _a=224 , _a=1.0 , _a=8 , _a="relu6" , _a=True , _a=0.999 , _a=0.02 , _a=0.001 , **_a , ): super().__init__(**_a ) if depth_multiplier <= 0: raise ValueError('''depth_multiplier must be greater than zero.''' ) __a = num_channels __a = image_size __a = depth_multiplier __a = min_depth __a = hidden_act __a = tf_padding __a = classifier_dropout_prob __a = initializer_range __a = layer_norm_eps class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : Tuple = version.parse('1.11' ) @property def __UpperCAmelCase ( self ): return OrderedDict([('''pixel_values''', {0: '''batch'''})] ) @property def __UpperCAmelCase ( self ): if self.task == "image-classification": return OrderedDict([('''logits''', {0: '''batch'''})] ) else: return OrderedDict([('''last_hidden_state''', {0: '''batch'''}), ('''pooler_output''', {0: '''batch'''})] ) @property def __UpperCAmelCase ( self ): return 1E-4
45
"""simple docstring""" def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: __a = set() # Replace all the whitespace in our sentence __a = input_str.replace(''' ''' , '''''' ) for alpha in input_str: if "a" <= alpha.lower() <= "z": frequency.add(alpha.lower() ) return len(lowerCAmelCase__ ) == 26 def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: __a = [False] * 26 for char in input_str: if char.islower(): __a = True elif char.isupper(): __a = True return all(lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : str = "The quick brown fox jumps over the lazy dog" , ) -> bool: return len({char for char in input_str.lower() if char.isalpha()} ) == 26 def lowercase ( ) -> None: from timeit import timeit __a = '''from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest''' print(timeit('''is_pangram()''' , setup=lowerCAmelCase__ ) ) print(timeit('''is_pangram_faster()''' , setup=lowerCAmelCase__ ) ) print(timeit('''is_pangram_fastest()''' , setup=lowerCAmelCase__ ) ) # 5.348480500048026, 2.6477354579837993, 1.8470395830227062 # 5.036091582966037, 2.644472333951853, 1.8869528750656173 if __name__ == "__main__": import doctest doctest.testmod() benchmark()
45
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : int ) -> float: if digit_amount > 0: return round(number - int(lowerCAmelCase__ ) , lowerCAmelCase__ ) return number - int(lowerCAmelCase__ ) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.345, 1)) print(decimal_isolate(35.345, 2)) print(decimal_isolate(35.345, 3)) print(decimal_isolate(-14.789, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.123, 1)) print(decimal_isolate(-14.123, 2)) print(decimal_isolate(-14.123, 3))
45
"""simple docstring""" import gc import unittest from transformers import CTRLConfig, is_torch_available from transformers.testing_utils import require_torch, slow, 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 ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, ) class __lowerCAmelCase : '''simple docstring''' def __init__( self , _a , _a=14 , _a=7 , _a=True , _a=True , _a=True , _a=True , _a=True , _a=99 , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=3 , _a=4 , _a=None , ): __a = parent __a = batch_size __a = seq_length __a = is_training __a = use_token_type_ids __a = use_input_mask __a = use_labels __a = use_mc_token_ids __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = type_sequence_label_size __a = initializer_range __a = num_labels __a = num_choices __a = scope __a = self.vocab_size - 1 def __UpperCAmelCase ( self ): __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __a = None if self.use_input_mask: __a = random_attention_mask([self.batch_size, self.seq_length] ) __a = None if self.use_token_type_ids: __a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __a = None if self.use_mc_token_ids: __a = ids_tensor([self.batch_size, self.num_choices] , self.seq_length ) __a = None __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __a = ids_tensor([self.batch_size] , self.num_choices ) __a = self.get_config() __a = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, token_type_ids, mc_token_ids, sequence_labels, token_labels, choice_labels, ) def __UpperCAmelCase ( self ): return CTRLConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , *_a ): __a = CTRLModel(config=_a ) model.to(_a ) model.eval() model(_a , token_type_ids=_a , head_mask=_a ) model(_a , token_type_ids=_a ) __a = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(len(result.past_key_values ) , config.n_layer ) def __UpperCAmelCase ( self , _a , _a , _a , _a , _a , *_a ): __a = CTRLLMHeadModel(_a ) model.to(_a ) model.eval() __a = model(_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __UpperCAmelCase ( self ): __a = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = config_and_inputs __a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask} return config, inputs_dict def __UpperCAmelCase ( self , _a , _a , _a , _a , *_a ): __a = self.num_labels __a = CTRLForSequenceClassification(_a ) model.to(_a ) model.eval() __a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a = model(_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) @require_torch class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else () __UpperCAmelCase : Union[str, Any] = (CTRLLMHeadModel,) if is_torch_available() else () __UpperCAmelCase : Union[str, Any] = ( { 'feature-extraction': CTRLModel, 'text-classification': CTRLForSequenceClassification, 'text-generation': CTRLLMHeadModel, 'zero-shot': CTRLForSequenceClassification, } if is_torch_available() else {} ) __UpperCAmelCase : Optional[Any] = True __UpperCAmelCase : List[Any] = False __UpperCAmelCase : str = False def __UpperCAmelCase ( self , _a , _a , _a , _a , _a ): if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny # config could not be created. return True return False def __UpperCAmelCase ( self ): __a = CTRLModelTester(self ) __a = ConfigTester(self , config_class=_a , n_embd=37 ) def __UpperCAmelCase ( self ): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() def __UpperCAmelCase ( self ): self.config_tester.run_common_tests() def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_ctrl_model(*_a ) def __UpperCAmelCase ( self ): __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*_a ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __UpperCAmelCase ( self ): pass @slow def __UpperCAmelCase ( self ): for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = CTRLModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip('''The model doesn\'t support left padding''' ) # and it's not used enough to be worth fixing :) def __UpperCAmelCase ( self ): pass @require_torch class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() @slow def __UpperCAmelCase ( self ): __a = CTRLLMHeadModel.from_pretrained('''ctrl''' ) model.to(_a ) __a = torch.tensor( [[11_859, 0, 1_611, 8]] , dtype=torch.long , device=_a ) # Legal the president is __a = [ 11_859, 0, 1_611, 8, 5, 150, 26_449, 2, 19, 348, 469, 3, 2_595, 48, 20_740, 246_533, 246_533, 19, 30, 5, ] # Legal the president is a good guy and I don't want to lose my job. \n \n I have a __a = model.generate(_a , do_sample=_a ) self.assertListEqual(output_ids[0].tolist() , _a )
45
1