code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __lowerCamelCase = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = ['''NllbTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = ['''NllbTokenizerFast'''] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb import NllbTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb_fast import NllbTokenizerFast else: import sys __lowerCamelCase = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
221
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available __UpperCamelCase : str = { '''configuration_longt5''': ['''LONGT5_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''LongT5Config''', '''LongT5OnnxConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : str = [ '''LONGT5_PRETRAINED_MODEL_ARCHIVE_LIST''', '''LongT5EncoderModel''', '''LongT5ForConditionalGeneration''', '''LongT5Model''', '''LongT5PreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : int = [ '''FlaxLongT5ForConditionalGeneration''', '''FlaxLongT5Model''', '''FlaxLongT5PreTrainedModel''', ] if TYPE_CHECKING: from .configuration_longta import LONGT5_PRETRAINED_CONFIG_ARCHIVE_MAP, LongTaConfig, LongTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_longta import ( LONGT5_PRETRAINED_MODEL_ARCHIVE_LIST, LongTaEncoderModel, LongTaForConditionalGeneration, LongTaModel, LongTaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_longta import ( FlaxLongTaForConditionalGeneration, FlaxLongTaModel, FlaxLongTaPreTrainedModel, ) else: import sys __UpperCamelCase : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
106
0
'''simple docstring''' import argparse import glob import logging import os import time from argparse import Namespace import numpy as np import torch from lightning_base import BaseTransformer, add_generic_args, generic_train from torch.utils.data import DataLoader, TensorDataset from transformers import glue_compute_metrics as compute_metrics from transformers import glue_convert_examples_to_features as convert_examples_to_features from transformers import glue_output_modes, glue_tasks_num_labels from transformers import glue_processors as processors lowercase__ : Optional[int] = logging.getLogger(__name__) class SCREAMING_SNAKE_CASE (a__ ): lowerCAmelCase = '''sequence-classification''' def __init__( self , _UpperCAmelCase): '''simple docstring''' if type(_UpperCAmelCase) == dict: __A : Any = Namespace(**_UpperCAmelCase) __A : Union[str, Any] = glue_output_modes[hparams.task] __A : List[Any] = glue_tasks_num_labels[hparams.task] super().__init__(_UpperCAmelCase , _UpperCAmelCase , self.mode) def SCREAMING_SNAKE_CASE ( self , **_UpperCAmelCase): '''simple docstring''' return self.model(**_UpperCAmelCase) def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase , _UpperCAmelCase): '''simple docstring''' __A : Any = {'input_ids': batch[0], 'attention_mask': batch[1], 'labels': batch[3]} if self.config.model_type not in ["distilbert", "bart"]: __A : List[Any] = batch[2] if self.config.model_type in ['bert', 'xlnet', 'albert'] else None __A : Dict = self(**_UpperCAmelCase) __A : str = outputs[0] __A : str = self.trainer.lr_schedulers[0]['scheduler'] __A : List[Any] = {'loss': loss, 'rate': lr_scheduler.get_last_lr()[-1]} return {"loss": loss, "log": tensorboard_logs} def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : List[Any] = self.hparams __A : Optional[Any] = processors[args.task]() __A : Union[str, Any] = processor.get_labels() for mode in ["train", "dev"]: __A : int = self._feature_file(_UpperCAmelCase) if os.path.exists(_UpperCAmelCase) and not args.overwrite_cache: logger.info('Loading features from cached file %s' , _UpperCAmelCase) else: logger.info('Creating features from dataset file at %s' , args.data_dir) __A : Optional[int] = ( processor.get_dev_examples(args.data_dir) if mode == 'dev' else processor.get_train_examples(args.data_dir) ) __A : Any = convert_examples_to_features( _UpperCAmelCase , self.tokenizer , max_length=args.max_seq_length , label_list=self.labels , output_mode=args.glue_output_mode , ) logger.info('Saving features into cached file %s' , _UpperCAmelCase) torch.save(_UpperCAmelCase , _UpperCAmelCase) def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = False): '''simple docstring''' __A : Dict = 'dev' if mode == 'test' else mode __A : Optional[Any] = self._feature_file(_UpperCAmelCase) logger.info('Loading features from cached file %s' , _UpperCAmelCase) __A : Optional[Any] = torch.load(_UpperCAmelCase) __A : Union[str, Any] = torch.tensor([f.input_ids for f in features] , dtype=torch.long) __A : Any = torch.tensor([f.attention_mask for f in features] , dtype=torch.long) __A : Optional[Any] = torch.tensor([f.token_type_ids for f in features] , dtype=torch.long) if self.hparams.glue_output_mode == "classification": __A : Union[str, Any] = torch.tensor([f.label for f in features] , dtype=torch.long) elif self.hparams.glue_output_mode == "regression": __A : int = torch.tensor([f.label for f in features] , dtype=torch.float) return DataLoader( TensorDataset(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase) , batch_size=_UpperCAmelCase , shuffle=_UpperCAmelCase , ) def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase , _UpperCAmelCase): '''simple docstring''' __A : Tuple = {'input_ids': batch[0], 'attention_mask': batch[1], 'labels': batch[3]} if self.config.model_type not in ["distilbert", "bart"]: __A : Dict = batch[2] if self.config.model_type in ['bert', 'xlnet', 'albert'] else None __A : Any = self(**_UpperCAmelCase) __A ,__A : str = outputs[:2] __A : Optional[Any] = logits.detach().cpu().numpy() __A : List[str] = inputs['labels'].detach().cpu().numpy() return {"val_loss": tmp_eval_loss.detach().cpu(), "pred": preds, "target": out_label_ids} def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase): '''simple docstring''' __A : Dict = torch.stack([x['val_loss'] for x in outputs]).mean().detach().cpu().item() __A : str = np.concatenate([x['pred'] for x in outputs] , axis=0) if self.hparams.glue_output_mode == "classification": __A : int = np.argmax(_UpperCAmelCase , axis=1) elif self.hparams.glue_output_mode == "regression": __A : Dict = np.squeeze(_UpperCAmelCase) __A : Union[str, Any] = np.concatenate([x['target'] for x in outputs] , axis=0) __A : Dict = [[] for _ in range(out_label_ids.shape[0])] __A : int = [[] for _ in range(out_label_ids.shape[0])] __A : Dict = {**{'val_loss': val_loss_mean}, **compute_metrics(self.hparams.task , _UpperCAmelCase , _UpperCAmelCase)} __A : Tuple = dict(results.items()) __A : Optional[int] = results return ret, preds_list, out_label_list def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase): '''simple docstring''' __A ,__A ,__A : int = self._eval_end(_UpperCAmelCase) __A : Dict = ret['log'] return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs} def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase): '''simple docstring''' __A ,__A ,__A : Optional[Any] = self._eval_end(_UpperCAmelCase) __A : Tuple = ret['log'] # `val_loss` is the key returned by `self._eval_end()` but actually refers to `test_loss` return {"avg_test_loss": logs["val_loss"], "log": logs, "progress_bar": logs} @staticmethod def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase): '''simple docstring''' BaseTransformer.add_model_specific_args(_UpperCAmelCase , _UpperCAmelCase) parser.add_argument( '--max_seq_length' , default=128 , type=_UpperCAmelCase , help=( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) , ) parser.add_argument( '--task' , default='' , type=_UpperCAmelCase , required=_UpperCAmelCase , help='The GLUE task to run' , ) parser.add_argument( '--gpus' , default=0 , type=_UpperCAmelCase , help='The number of GPUs allocated for this, it is by default 0 meaning none' , ) parser.add_argument( '--overwrite_cache' , action='store_true' , help='Overwrite the cached training and evaluation sets') return parser def _lowerCAmelCase ( ) -> Dict: __A : int = argparse.ArgumentParser() add_generic_args(__snake_case , os.getcwd() ) __A : Tuple = GLUETransformer.add_model_specific_args(__snake_case , os.getcwd() ) __A : Union[str, Any] = parser.parse_args() # If output_dir not provided, a folder will be generated in pwd if args.output_dir is None: __A : List[str] = os.path.join( './results' , f'{args.task}_{time.strftime("%Y%m%d_%H%M%S" )}' , ) os.makedirs(args.output_dir ) __A : Any = GLUETransformer(__snake_case ) __A : List[Any] = generic_train(__snake_case , __snake_case ) # Optionally, predict on dev set and write to output_dir if args.do_predict: __A : List[str] = sorted(glob.glob(os.path.join(args.output_dir , 'checkpoint-epoch=*.ckpt' ) , recursive=__snake_case ) ) __A : List[Any] = model.load_from_checkpoint(checkpoints[-1] ) return trainer.test(__snake_case ) if __name__ == "__main__": main()
190
'''simple docstring''' import os import shutil import sys import tempfile import unittest from pathlib import Path import pytest import transformers from transformers import ( BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoTokenizer, BertConfig, BertTokenizer, BertTokenizerFast, CTRLTokenizer, GPTaTokenizer, GPTaTokenizerFast, PreTrainedTokenizerFast, RobertaTokenizer, RobertaTokenizerFast, is_tokenizers_available, ) from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig from transformers.models.auto.tokenization_auto import ( TOKENIZER_MAPPING, get_tokenizer_config, tokenizer_class_from_name, ) from transformers.models.roberta.configuration_roberta import RobertaConfig from transformers.testing_utils import ( DUMMY_DIFF_TOKENIZER_IDENTIFIER, DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tokenizers, slow, ) sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils''')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 if is_tokenizers_available(): from test_module.custom_tokenization_fast import CustomTokenizerFast class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Dict = 0 @slow def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' for model_name in (x for x in BERT_PRETRAINED_CONFIG_ARCHIVE_MAP.keys() if "japanese" not in x): __A : List[str] = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsNotNone(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , (BertTokenizer, BertTokenizerFast)) self.assertGreater(len(_UpperCAmelCase) , 0) for model_name in GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP.keys(): __A : Any = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsNotNone(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , (GPTaTokenizer, GPTaTokenizerFast)) self.assertGreater(len(_UpperCAmelCase) , 0) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Optional[int] = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , (BertTokenizer, BertTokenizerFast)) self.assertEqual(tokenizer.vocab_size , 12) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Optional[Any] = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , (RobertaTokenizer, RobertaTokenizerFast)) self.assertEqual(tokenizer.vocab_size , 20) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Tuple = AutoConfig.from_pretrained(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) # Check that tokenizer_type ≠ model_type __A : Optional[Any] = AutoTokenizer.from_pretrained(_UpperCAmelCase , config=_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , (BertTokenizer, BertTokenizerFast)) self.assertEqual(tokenizer.vocab_size , 12) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('./tests/fixtures/vocab.txt' , os.path.join(_UpperCAmelCase , 'vocab.txt')) __A : Union[str, Any] = AutoTokenizer.from_pretrained(_UpperCAmelCase , tokenizer_type='bert' , use_fast=_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('./tests/fixtures/vocab.json' , os.path.join(_UpperCAmelCase , 'vocab.json')) shutil.copy('./tests/fixtures/merges.txt' , os.path.join(_UpperCAmelCase , 'merges.txt')) __A : str = AutoTokenizer.from_pretrained(_UpperCAmelCase , tokenizer_type='gpt2' , use_fast=_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('./tests/fixtures/vocab.txt' , os.path.join(_UpperCAmelCase , 'vocab.txt')) __A : List[str] = AutoTokenizer.from_pretrained(_UpperCAmelCase , tokenizer_type='bert') self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('./tests/fixtures/vocab.json' , os.path.join(_UpperCAmelCase , 'vocab.json')) shutil.copy('./tests/fixtures/merges.txt' , os.path.join(_UpperCAmelCase , 'merges.txt')) __A : List[str] = AutoTokenizer.from_pretrained(_UpperCAmelCase , tokenizer_type='gpt2') self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' with pytest.raises(_UpperCAmelCase): AutoTokenizer.from_pretrained('./' , tokenizer_type='xxx') @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: __A : List[Any] = tokenizer_class.from_pretrained('wietsedv/bert-base-dutch-cased') self.assertIsInstance(_UpperCAmelCase , (BertTokenizer, BertTokenizerFast)) if isinstance(_UpperCAmelCase , _UpperCAmelCase): self.assertEqual(tokenizer.basic_tokenizer.do_lower_case , _UpperCAmelCase) else: self.assertEqual(tokenizer.do_lower_case , _UpperCAmelCase) self.assertEqual(tokenizer.model_max_length , 512) @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: with self.assertRaisesRegex( _UpperCAmelCase , 'julien-c/herlolip-not-exists is not a local folder and is not a valid model identifier' , ): __A : str = tokenizer_class.from_pretrained('julien-c/herlolip-not-exists') def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Any = TOKENIZER_MAPPING.values() __A : Union[str, Any] = [] for slow_tok, fast_tok in tokenizers: if slow_tok is not None: tokenizer_names.append(slow_tok.__name__) if fast_tok is not None: tokenizer_names.append(fast_tok.__name__) for tokenizer_name in tokenizer_names: # must find the right class tokenizer_class_from_name(_UpperCAmelCase) @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' self.assertIsInstance(AutoTokenizer.from_pretrained('bert-base-cased' , use_fast=_UpperCAmelCase) , _UpperCAmelCase) self.assertIsInstance(AutoTokenizer.from_pretrained('bert-base-cased') , _UpperCAmelCase) @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : List[str] = AutoTokenizer.from_pretrained('distilbert-base-uncased' , do_lower_case=_UpperCAmelCase) __A : str = 'Hello, world. How are you?' __A : List[str] = tokenizer.tokenize(_UpperCAmelCase) self.assertEqual('[UNK]' , tokens[0]) __A : Dict = AutoTokenizer.from_pretrained('microsoft/mpnet-base' , do_lower_case=_UpperCAmelCase) __A : List[Any] = tokenizer.tokenize(_UpperCAmelCase) self.assertEqual('[UNK]' , tokens[0]) @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Union[str, Any] = AutoTokenizer.from_pretrained('robot-test/dummy-tokenizer-fast-with-model-config') self.assertEqual(type(_UpperCAmelCase) , _UpperCAmelCase) self.assertEqual(tokenizer.model_max_length , 512) self.assertEqual(tokenizer.vocab_size , 3_0000) self.assertEqual(tokenizer.unk_token , '[UNK]') self.assertEqual(tokenizer.padding_side , 'right') self.assertEqual(tokenizer.truncation_side , 'right') def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Optional[Any] = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , (BertTokenizer, BertTokenizerFast)) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_UpperCAmelCase) __A : Any = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , tokenizer.__class__) self.assertEqual(tokenizera.vocab_size , 12) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Dict = AutoTokenizer.from_pretrained('ctrl') # There is no fast CTRL so this always gives us a slow tokenizer. self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : List[Any] = get_tokenizer_config('bert-base-cased') __A : Optional[int] = config.pop('_commit_hash' , _UpperCAmelCase) # If we ever update bert-base-cased tokenizer config, this dict here will need to be updated. self.assertEqual(_UpperCAmelCase , {'do_lower_case': False}) # This model does not have a tokenizer_config so we get back an empty dict. __A : Dict = get_tokenizer_config(_UpperCAmelCase) self.assertDictEqual(_UpperCAmelCase , {}) # A tokenizer saved with `save_pretrained` always creates a tokenizer config. __A : Any = AutoTokenizer.from_pretrained(_UpperCAmelCase) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_UpperCAmelCase) __A : Any = get_tokenizer_config(_UpperCAmelCase) # Check the class of the tokenizer was properly saved (note that it always saves the slow class). self.assertEqual(config['tokenizer_class'] , 'BertTokenizer') def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' try: AutoConfig.register('custom' , _UpperCAmelCase) AutoTokenizer.register(_UpperCAmelCase , slow_tokenizer_class=_UpperCAmelCase) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_UpperCAmelCase): AutoTokenizer.register(_UpperCAmelCase , slow_tokenizer_class=_UpperCAmelCase) __A : Optional[Any] = CustomTokenizer.from_pretrained(_UpperCAmelCase) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_UpperCAmelCase) __A : int = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' try: AutoConfig.register('custom' , _UpperCAmelCase) # Can register in two steps AutoTokenizer.register(_UpperCAmelCase , slow_tokenizer_class=_UpperCAmelCase) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, None)) AutoTokenizer.register(_UpperCAmelCase , fast_tokenizer_class=_UpperCAmelCase) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast)) del TOKENIZER_MAPPING._extra_content[CustomConfig] # Can register in one step AutoTokenizer.register( _UpperCAmelCase , slow_tokenizer_class=_UpperCAmelCase , fast_tokenizer_class=_UpperCAmelCase) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast)) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_UpperCAmelCase): AutoTokenizer.register(_UpperCAmelCase , fast_tokenizer_class=_UpperCAmelCase) # We pass through a bert tokenizer fast cause there is no converter slow to fast for our new toknizer # and that model does not have a tokenizer.json with tempfile.TemporaryDirectory() as tmp_dir: __A : Optional[int] = BertTokenizerFast.from_pretrained(_UpperCAmelCase) bert_tokenizer.save_pretrained(_UpperCAmelCase) __A : Dict = CustomTokenizerFast.from_pretrained(_UpperCAmelCase) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_UpperCAmelCase) __A : Union[str, Any] = AutoTokenizer.from_pretrained(_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) __A : Any = AutoTokenizer.from_pretrained(_UpperCAmelCase , use_fast=_UpperCAmelCase) self.assertIsInstance(_UpperCAmelCase , _UpperCAmelCase) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' with self.assertRaises(_UpperCAmelCase): __A : List[str] = AutoTokenizer.from_pretrained('hf-internal-testing/test_dynamic_tokenizer') # If remote code is disabled, we can't load this config. with self.assertRaises(_UpperCAmelCase): __A : Dict = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=_UpperCAmelCase) __A : str = AutoTokenizer.from_pretrained('hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=_UpperCAmelCase) self.assertTrue(tokenizer.special_attribute_present) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_UpperCAmelCase) __A : Dict = AutoTokenizer.from_pretrained(_UpperCAmelCase , trust_remote_code=_UpperCAmelCase) self.assertTrue(reloaded_tokenizer.special_attribute_present) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast') self.assertEqual(reloaded_tokenizer.__class__.__name__ , 'NewTokenizerFast') # Test we can also load the slow version __A : Union[str, Any] = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=_UpperCAmelCase , use_fast=_UpperCAmelCase) self.assertTrue(tokenizer.special_attribute_present) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer') # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_UpperCAmelCase) __A : Union[str, Any] = AutoTokenizer.from_pretrained(_UpperCAmelCase , trust_remote_code=_UpperCAmelCase , use_fast=_UpperCAmelCase) self.assertEqual(reloaded_tokenizer.__class__.__name__ , 'NewTokenizer') self.assertTrue(reloaded_tokenizer.special_attribute_present) else: self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer') self.assertEqual(reloaded_tokenizer.__class__.__name__ , 'NewTokenizer') @require_tokenizers def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' class SCREAMING_SNAKE_CASE (a__ ): lowerCAmelCase = False class SCREAMING_SNAKE_CASE (a__ ): lowerCAmelCase = NewTokenizer lowerCAmelCase = False try: AutoConfig.register('custom' , _UpperCAmelCase) AutoTokenizer.register(_UpperCAmelCase , slow_tokenizer_class=_UpperCAmelCase) AutoTokenizer.register(_UpperCAmelCase , fast_tokenizer_class=_UpperCAmelCase) # If remote code is not set, the default is to use local __A : List[Any] = AutoTokenizer.from_pretrained('hf-internal-testing/test_dynamic_tokenizer') self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast') self.assertFalse(tokenizer.special_attribute_present) __A : Dict = AutoTokenizer.from_pretrained('hf-internal-testing/test_dynamic_tokenizer' , use_fast=_UpperCAmelCase) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer') self.assertFalse(tokenizer.special_attribute_present) # If remote code is disabled, we load the local one. __A : Optional[Any] = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=_UpperCAmelCase) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast') self.assertFalse(tokenizer.special_attribute_present) __A : Any = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=_UpperCAmelCase , use_fast=_UpperCAmelCase) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer') self.assertFalse(tokenizer.special_attribute_present) # If remote is enabled, we load from the Hub __A : int = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=_UpperCAmelCase) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast') self.assertTrue(tokenizer.special_attribute_present) __A : Optional[Any] = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=_UpperCAmelCase , use_fast=_UpperCAmelCase) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer') self.assertTrue(tokenizer.special_attribute_present) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : int = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer_legacy' , trust_remote_code=_UpperCAmelCase) self.assertTrue(tokenizer.special_attribute_present) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast') # Test we can also load the slow version __A : int = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer_legacy' , trust_remote_code=_UpperCAmelCase , use_fast=_UpperCAmelCase) self.assertTrue(tokenizer.special_attribute_present) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer') else: self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer') def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' with self.assertRaisesRegex( _UpperCAmelCase , 'bert-base is not a local folder and is not a valid model identifier'): __A : Union[str, Any] = AutoTokenizer.from_pretrained('bert-base') def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' with self.assertRaisesRegex( _UpperCAmelCase , R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'): __A : Union[str, Any] = AutoTokenizer.from_pretrained(_UpperCAmelCase , revision='aaaaaa') def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Optional[int] = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-bert') with RequestCounter() as counter: __A : Union[str, Any] = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-bert') self.assertEqual(counter.get_request_count , 0) self.assertEqual(counter.head_request_count , 1) self.assertEqual(counter.other_request_count , 0)
190
1
from __future__ import annotations from math import pi from typing import Protocol import matplotlib.pyplot as plt import numpy as np class UpperCAmelCase_ ( a): def snake_case__ ( self, __a): '''simple docstring''' return 0.0 def A ( _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' _lowerCAmelCase : Optional[Any] = min([-20, np.min(fft_results[1 : samplerate // 2 - 1] )] ) _lowerCAmelCase : Optional[int] = max([20, np.max(fft_results[1 : samplerate // 2 - 1] )] ) return lowest, highest def A ( _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' _lowerCAmelCase : int = 512 _lowerCAmelCase : Union[str, Any] = [1] + [0] * (size - 1) _lowerCAmelCase : Optional[Any] = [filter_type.process(_lowerCamelCase ) for item in inputs] _lowerCAmelCase : int = [0] * (samplerate - size) # zero-padding outputs += filler _lowerCAmelCase : str = np.abs(np.fft.fft(_lowerCamelCase ) ) _lowerCAmelCase : Union[str, Any] = 20 * np.logaa(_lowerCamelCase ) # Frequencies on log scale from 24 to nyquist frequency plt.xlim(24 , samplerate / 2 - 1 ) plt.xlabel("Frequency (Hz)" ) plt.xscale("log" ) # Display within reasonable bounds _lowerCAmelCase : List[Any] = get_bounds(_lowerCamelCase , _lowerCamelCase ) plt.ylim(max([-80, bounds[0]] ) , min([80, bounds[1]] ) ) plt.ylabel("Gain (dB)" ) plt.plot(_lowerCamelCase ) plt.show() def A ( _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' _lowerCAmelCase : Union[str, Any] = 512 _lowerCAmelCase : Optional[Any] = [1] + [0] * (size - 1) _lowerCAmelCase : str = [filter_type.process(_lowerCamelCase ) for item in inputs] _lowerCAmelCase : Optional[Any] = [0] * (samplerate - size) # zero-padding outputs += filler _lowerCAmelCase : Optional[Any] = np.angle(np.fft.fft(_lowerCamelCase ) ) # Frequencies on log scale from 24 to nyquist frequency plt.xlim(24 , samplerate / 2 - 1 ) plt.xlabel("Frequency (Hz)" ) plt.xscale("log" ) plt.ylim(-2 * pi , 2 * pi ) plt.ylabel("Phase shift (Radians)" ) plt.plot(np.unwrap(_lowerCamelCase , -2 * pi ) ) plt.show()
36
import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( "kwargs, expected" , [ ({"num_shards": 0, "max_num_jobs": 1}, []), ({"num_shards": 10, "max_num_jobs": 1}, [range(10 )]), ({"num_shards": 10, "max_num_jobs": 10}, [range(_lowerCamelCase , i + 1 ) for i in range(10 )]), ({"num_shards": 1, "max_num_jobs": 10}, [range(1 )]), ({"num_shards": 10, "max_num_jobs": 3}, [range(0 , 4 ), range(4 , 7 ), range(7 , 10 )]), ({"num_shards": 3, "max_num_jobs": 10}, [range(0 , 1 ), range(1 , 2 ), range(2 , 3 )]), ] , ) def A ( _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' _lowerCAmelCase : List[Any] = _distribute_shards(**_lowerCamelCase ) assert out == expected @pytest.mark.parametrize( "gen_kwargs, max_num_jobs, expected" , [ ({"foo": 0}, 10, [{"foo": 0}]), ({"shards": [0, 1, 2, 3]}, 1, [{"shards": [0, 1, 2, 3]}]), ({"shards": [0, 1, 2, 3]}, 4, [{"shards": [0]}, {"shards": [1]}, {"shards": [2]}, {"shards": [3]}]), ({"shards": [0, 1]}, 4, [{"shards": [0]}, {"shards": [1]}]), ({"shards": [0, 1, 2, 3]}, 2, [{"shards": [0, 1]}, {"shards": [2, 3]}]), ] , ) def A ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' _lowerCAmelCase : Optional[int] = _split_gen_kwargs(_lowerCamelCase , _lowerCamelCase ) assert out == expected @pytest.mark.parametrize( "gen_kwargs, expected" , [ ({"foo": 0}, 1), ({"shards": [0]}, 1), ({"shards": [0, 1, 2, 3]}, 4), ({"shards": [0, 1, 2, 3], "foo": 0}, 4), ({"shards": [0, 1, 2, 3], "other": (0, 1)}, 4), ({"shards": [0, 1, 2, 3], "shards2": [0, 1]}, RuntimeError), ] , ) def A ( _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' if expected is RuntimeError: with pytest.raises(_lowerCamelCase ): _number_of_shards_in_gen_kwargs(_lowerCamelCase ) else: _lowerCAmelCase : Optional[int] = _number_of_shards_in_gen_kwargs(_lowerCamelCase ) assert out == expected
36
1
'''simple docstring''' import json from typing import Iterator, List, Union from tokenizers import AddedToken, Regex, Tokenizer, decoders, normalizers, pre_tokenizers, trainers from tokenizers.implementations.base_tokenizer import BaseTokenizer from tokenizers.models import Unigram from tokenizers.processors import TemplateProcessing class _lowerCAmelCase ( __A ): """simple docstring""" def __init__( self , _lowerCamelCase = "▁" , _lowerCamelCase = True , _lowerCamelCase = "<unk>" , _lowerCamelCase = "</s>" , _lowerCamelCase = "<pad>" , ) -> Tuple: A_ : List[str] = { """pad""": {"""id""": 0, """token""": pad_token}, """eos""": {"""id""": 1, """token""": eos_token}, """unk""": {"""id""": 2, """token""": unk_token}, } A_ : str = [None] * len(self.special_tokens ) for token_dict in self.special_tokens.values(): A_ : Dict = token_dict["""token"""] A_ : Optional[int] = Tokenizer(Unigram() ) A_ : List[str] = normalizers.Sequence( [ normalizers.Nmt(), normalizers.NFKC(), normalizers.Replace(Regex(""" {2,}""" ) , """ """ ), normalizers.Lowercase(), ] ) A_ : Tuple = pre_tokenizers.Sequence( [ pre_tokenizers.Metaspace(replacement=_lowerCamelCase , add_prefix_space=_lowerCamelCase ), pre_tokenizers.Digits(individual_digits=_lowerCamelCase ), pre_tokenizers.Punctuation(), ] ) A_ : List[str] = decoders.Metaspace(replacement=_lowerCamelCase , add_prefix_space=_lowerCamelCase ) A_ : Optional[int] = TemplateProcessing( single=F"$A {self.special_tokens['eos']['token']}" , special_tokens=[(self.special_tokens["""eos"""]["""token"""], self.special_tokens["""eos"""]["""id"""])] , ) A_ : List[str] = { """model""": """SentencePieceUnigram""", """replacement""": replacement, """add_prefix_space""": add_prefix_space, } super().__init__(_lowerCamelCase , _lowerCamelCase ) def UpperCAmelCase_ ( self , _lowerCamelCase , _lowerCamelCase = 8000 , _lowerCamelCase = True , ) -> int: A_ : Dict = trainers.UnigramTrainer( vocab_size=_lowerCamelCase , special_tokens=self.special_tokens_list , show_progress=_lowerCamelCase , ) if isinstance(_lowerCamelCase , _lowerCamelCase ): A_ : Union[str, Any] = [files] self._tokenizer.train(_lowerCamelCase , trainer=_lowerCamelCase ) self.add_unk_id() def UpperCAmelCase_ ( self , _lowerCamelCase , _lowerCamelCase = 8000 , _lowerCamelCase = True , ) -> int: A_ : str = trainers.UnigramTrainer( vocab_size=_lowerCamelCase , special_tokens=self.special_tokens_list , show_progress=_lowerCamelCase , ) self._tokenizer.train_from_iterator(_lowerCamelCase , trainer=_lowerCamelCase ) self.add_unk_id() def UpperCAmelCase_ ( self ) -> Optional[Any]: A_ : Any = json.loads(self._tokenizer.to_str() ) A_ : Optional[int] = self.special_tokens["""unk"""]["""id"""] A_ : Optional[int] = Tokenizer.from_str(json.dumps(_lowerCamelCase ) )
369
'''simple docstring''' from __future__ import annotations from collections.abc import Iterator from typing import Any class _lowerCAmelCase : """simple docstring""" def __init__( self , _lowerCamelCase ) -> Optional[Any]: A_ : Any = data A_ : Node | None = None class _lowerCAmelCase : """simple docstring""" def __init__( self ) -> List[str]: A_ : Tuple = None A_ : str = None def __iter__( self ) -> Iterator[Any]: A_ : Dict = self.head while self.head: yield node.data A_ : Optional[Any] = node.next if node == self.head: break def __len__( self ) -> int: return sum(1 for _ in self ) def __repr__( self ) -> str: return "->".join(str(_lowerCamelCase ) for item in iter(self ) ) def UpperCAmelCase_ ( self , _lowerCamelCase ) -> None: self.insert_nth(len(self ) , _lowerCamelCase ) def UpperCAmelCase_ ( self , _lowerCamelCase ) -> None: self.insert_nth(0 , _lowerCamelCase ) def UpperCAmelCase_ ( self , _lowerCamelCase , _lowerCamelCase ) -> None: if index < 0 or index > len(self ): raise IndexError("""list index out of range.""" ) A_ : Optional[int] = Node(_lowerCamelCase ) if self.head is None: A_ : str = new_node # first node points itself A_ : Union[str, Any] = new_node elif index == 0: # insert at head A_ : List[Any] = self.head A_ : List[Any] = new_node else: A_ : List[str] = self.head for _ in range(index - 1 ): A_ : Optional[int] = temp.next A_ : Tuple = temp.next A_ : str = new_node if index == len(self ) - 1: # insert at tail A_ : Optional[int] = new_node def UpperCAmelCase_ ( self ) -> List[Any]: return self.delete_nth(0 ) def UpperCAmelCase_ ( self ) -> Any: return self.delete_nth(len(self ) - 1 ) def UpperCAmelCase_ ( self , _lowerCamelCase = 0 ) -> Any: if not 0 <= index < len(self ): raise IndexError("""list index out of range.""" ) A_ : int = self.head if self.head == self.tail: # just one node A_ : int = None elif index == 0: # delete head node A_ : Union[str, Any] = self.tail.next.next A_ : Tuple = self.head.next else: A_ : Optional[int] = self.head for _ in range(index - 1 ): A_ : Tuple = temp.next A_ : Any = temp.next A_ : Tuple = temp.next.next if index == len(self ) - 1: # delete at tail A_ : List[str] = temp return delete_node.data def UpperCAmelCase_ ( self ) -> bool: return len(self ) == 0 def UpperCAmelCase ( ) -> None: """simple docstring""" A_ : Any = CircularLinkedList() assert len(a_ ) == 0 assert circular_linked_list.is_empty() is True assert str(a_ ) == "" try: circular_linked_list.delete_front() raise AssertionError # This should not happen except IndexError: assert True # This should happen try: circular_linked_list.delete_tail() raise AssertionError # This should not happen except IndexError: assert True # This should happen try: circular_linked_list.delete_nth(-1 ) raise AssertionError except IndexError: assert True try: circular_linked_list.delete_nth(0 ) raise AssertionError except IndexError: assert True assert circular_linked_list.is_empty() is True for i in range(5 ): assert len(a_ ) == i circular_linked_list.insert_nth(a_ , i + 1 ) assert str(a_ ) == "->".join(str(a_ ) for i in range(1 , 6 ) ) circular_linked_list.insert_tail(6 ) assert str(a_ ) == "->".join(str(a_ ) for i in range(1 , 7 ) ) circular_linked_list.insert_head(0 ) assert str(a_ ) == "->".join(str(a_ ) for i in range(0 , 7 ) ) assert circular_linked_list.delete_front() == 0 assert circular_linked_list.delete_tail() == 6 assert str(a_ ) == "->".join(str(a_ ) for i in range(1 , 6 ) ) assert circular_linked_list.delete_nth(2 ) == 3 circular_linked_list.insert_nth(2 , 3 ) assert str(a_ ) == "->".join(str(a_ ) for i in range(1 , 6 ) ) assert circular_linked_list.is_empty() is False if __name__ == "__main__": import doctest doctest.testmod()
164
0
"""simple docstring""" from collections import defaultdict from pathlib import Path import pandas as pd from rouge_cli import calculate_rouge_path from utils import calculate_rouge SCREAMING_SNAKE_CASE__ = [ "Prosecutor: \"No videos were used in the crash investigation\" German papers say they saw a cell phone video of the" " final seconds on board Flight 9525. The Germanwings co-pilot says he had a \"previous episode of severe" " depression\" German airline confirms it knew of Andreas Lubitz's depression years before he took control.", "The Palestinian Authority officially becomes the 123rd member of the International Criminal Court. The formal" " accession was marked with a ceremony at The Hague, in the Netherlands. The Palestinians signed the ICC's" " founding Rome Statute in January. Israel and the United States opposed the Palestinians' efforts to join the" " body.", "Amnesty International releases its annual report on the death penalty. The report catalogs the use of" " state-sanctioned killing as a punitive measure across the globe. At least 607 people were executed around the" " world in 2014, compared to 778 in 2013. The U.S. remains one of the worst offenders for imposing capital" " punishment.", ] SCREAMING_SNAKE_CASE__ = [ "Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports ." " Journalists at Bild and Paris Match are \"very confident\" the video clip is real, an editor says . Andreas Lubitz" " had informed his Lufthansa training school of an episode of severe depression, airline says .", "Membership gives the ICC jurisdiction over alleged crimes committed in Palestinian territories since last June ." " Israel and the United States opposed the move, which could open the door to war crimes investigations against" " Israelis .", "Amnesty's annual death penalty report catalogs encouraging signs, but setbacks in numbers of those sentenced to" " death . Organization claims that governments around the world are using the threat of terrorism to advance" " executions . The number of executions worldwide has gone down by almost 22% compared with 2013, but death" " sentences up by 28% .", ] def lowerCAmelCase__ ( ) -> str: """simple docstring""" snake_case = calculate_rouge(_UpperCamelCase , _UpperCamelCase , bootstrap_aggregation=_UpperCamelCase , rouge_keys=['rouge2', 'rougeL'] ) assert isinstance(_UpperCamelCase , _UpperCamelCase ) snake_case = calculate_rouge(_UpperCamelCase , _UpperCamelCase , bootstrap_aggregation=_UpperCamelCase , rouge_keys=['rouge2'] ) assert ( pd.DataFrame(no_aggregation['rouge2'] ).fmeasure.mean() == pd.DataFrame(no_aggregation_just_ra['rouge2'] ).fmeasure.mean() ) def lowerCAmelCase__ ( ) -> Dict: """simple docstring""" snake_case = 'rougeLsum' snake_case = calculate_rouge(_UpperCamelCase , _UpperCamelCase , newline_sep=_UpperCamelCase , rouge_keys=[k] )[k] snake_case = calculate_rouge(_UpperCamelCase , _UpperCamelCase , newline_sep=_UpperCamelCase , rouge_keys=[k] )[k] assert score > score_no_sep def lowerCAmelCase__ ( ) -> Any: """simple docstring""" snake_case = ['rouge1', 'rouge2', 'rougeL'] snake_case = calculate_rouge(_UpperCamelCase , _UpperCamelCase , newline_sep=_UpperCamelCase , rouge_keys=_UpperCamelCase ) snake_case = calculate_rouge(_UpperCamelCase , _UpperCamelCase , newline_sep=_UpperCamelCase , rouge_keys=_UpperCamelCase ) assert score_sep == score_no_sep def lowerCAmelCase__ ( ) -> str: """simple docstring""" snake_case = [ 'Her older sister, Margot Frank, died in 1945, a month earlier than previously thought.', 'Marseille prosecutor says "so far no videos were used in the crash investigation" despite media reports .', ] snake_case = [ 'Margot Frank, died in 1945, a month earlier than previously thought.', 'Prosecutor: "No videos were used in the crash investigation" German papers say they saw a cell phone video of' ' the final seconds on board Flight 9525.', ] assert calculate_rouge(_UpperCamelCase , _UpperCamelCase , newline_sep=_UpperCamelCase ) == calculate_rouge(_UpperCamelCase , _UpperCamelCase , newline_sep=_UpperCamelCase ) def lowerCAmelCase__ ( ) -> List[str]: """simple docstring""" snake_case = [ '" "a person who has such a video needs to immediately give it to the investigators," prosecutor says .<n> "it is a very disturbing scene," editor-in-chief of bild online tells "erin burnett: outfront" ' ] snake_case = [ ' Marseille prosecutor says "so far no videos were used in the crash investigation" despite media reports . Journalists at Bild and Paris Match are "very confident" the video clip is real, an editor says . Andreas Lubitz had informed his Lufthansa training school of an episode of severe depression, airline says .' ] snake_case = calculate_rouge(_UpperCamelCase , _UpperCamelCase , rouge_keys=['rougeLsum'] , newline_sep=_UpperCamelCase )['rougeLsum'] snake_case = calculate_rouge(_UpperCamelCase , _UpperCamelCase , rouge_keys=['rougeLsum'] )['rougeLsum'] assert new_score > prev_score def lowerCAmelCase__ ( ) -> str: """simple docstring""" snake_case = Path('examples/seq2seq/test_data/wmt_en_ro' ) snake_case = calculate_rouge_path(data_dir.joinpath('test.source' ) , data_dir.joinpath('test.target' ) ) assert isinstance(_UpperCamelCase , _UpperCamelCase ) snake_case = calculate_rouge_path( data_dir.joinpath('test.source' ) , data_dir.joinpath('test.target' ) , bootstrap_aggregation=_UpperCamelCase ) assert isinstance(_UpperCamelCase , _UpperCamelCase )
150
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available SCREAMING_SNAKE_CASE__ = { "configuration_bloom": ["BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP", "BloomConfig", "BloomOnnxConfig"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ["BloomTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ "BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST", "BloomForCausalLM", "BloomModel", "BloomPreTrainedModel", "BloomForSequenceClassification", "BloomForTokenClassification", "BloomForQuestionAnswering", ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
150
1
"""simple docstring""" def SCREAMING_SNAKE_CASE_ ( snake_case : int , snake_case : int , snake_case : int )-> int: if exponent == 1: return base if exponent % 2 == 0: _lowerCamelCase = _modexpt(snake_case , exponent // 2 , snake_case ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(snake_case , exponent - 1 , snake_case )) % modulo_value def SCREAMING_SNAKE_CASE_ ( snake_case : int = 1_777 , snake_case : int = 1_855 , snake_case : int = 8 )-> int: _lowerCamelCase = base for _ in range(1 , snake_case ): _lowerCamelCase = _modexpt(snake_case , snake_case , 10**digits ) return result if __name__ == "__main__": print(f'{solution() = }')
363
"""simple docstring""" from collections import defaultdict from math import gcd def SCREAMING_SNAKE_CASE_ ( snake_case : int = 1_500_000 )-> int: _lowerCamelCase = defaultdict(snake_case ) _lowerCamelCase = 2 while 2 * euclid_m * (euclid_m + 1) <= limit: for euclid_n in range((euclid_m % 2) + 1 , snake_case , 2 ): if gcd(snake_case , snake_case ) > 1: continue _lowerCamelCase = 2 * euclid_m * (euclid_m + euclid_n) for perimeter in range(snake_case , limit + 1 , snake_case ): frequencies[perimeter] += 1 euclid_m += 1 return sum(1 for frequency in frequencies.values() if frequency == 1 ) if __name__ == "__main__": print(f'{solution() = }')
80
0
from typing import List, Optional, Union import numpy as np import torch import torchaudio.compliance.kaldi as ta_kaldi from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging lowerCAmelCase_ = logging.get_logger(__name__) class __lowerCAmelCase ( _a ): lowerCamelCase_ : Any = ['''input_features''', '''attention_mask'''] def __init__(self , __magic_name__=80 , __magic_name__=1_6000 , __magic_name__=80 , __magic_name__=0.0 , __magic_name__=True , __magic_name__=True , __magic_name__=True , **__magic_name__ , ) -> Optional[Any]: '''simple docstring''' super().__init__(feature_size=__magic_name__ , sampling_rate=__magic_name__ , padding_value=__magic_name__ , **__magic_name__ ) snake_case_ : Union[str, Any] = num_mel_bins snake_case_ : Optional[Any] = do_ceptral_normalize snake_case_ : List[Any] = normalize_means snake_case_ : str = normalize_vars snake_case_ : Optional[Any] = True def lowerCamelCase (self , __magic_name__ , ) -> np.ndarray: '''simple docstring''' snake_case_ : List[Any] = waveform * (2**15) # Kaldi compliance: 16-bit signed integers snake_case_ : Tuple = torch.from_numpy(__magic_name__ ).unsqueeze(0 ) snake_case_ : str = ta_kaldi.fbank(__magic_name__ , num_mel_bins=self.num_mel_bins , sample_frequency=self.sampling_rate ) return features.numpy() @staticmethod def lowerCamelCase (__magic_name__ , __magic_name__ , __magic_name__ = True , __magic_name__ = True , __magic_name__ = 0.0 , ) -> np.ndarray: '''simple docstring''' if normalize_means: snake_case_ : Optional[int] = x[:input_length].mean(axis=0 ) snake_case_ : Optional[int] = np.subtract(__magic_name__ , __magic_name__ ) if normalize_vars: snake_case_ : Optional[int] = x[:input_length].std(axis=0 ) snake_case_ : Optional[Any] = np.divide(__magic_name__ , __magic_name__ ) if input_length < x.shape[0]: snake_case_ : List[str] = padding_value # make sure array is in float32 snake_case_ : Dict = x.astype(np.floataa ) return x def lowerCamelCase (self , __magic_name__ , __magic_name__ = None ) -> List[np.ndarray]: '''simple docstring''' snake_case_ : Dict = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [ self.utterance_cmvn(__magic_name__ , __magic_name__ , self.normalize_means , self.normalize_vars , self.padding_value ) for x, n in zip(__magic_name__ , __magic_name__ ) ] def __call__(self , __magic_name__ , __magic_name__ = False , __magic_name__ = None , __magic_name__ = False , __magic_name__ = None , __magic_name__ = None , __magic_name__ = None , __magic_name__ = None , **__magic_name__ , ) -> BatchFeature: '''simple docstring''' if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) snake_case_ : List[str] = isinstance(__magic_name__ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F'''Only mono-channel audio is supported for input to {self}''' ) snake_case_ : int = is_batched_numpy or ( isinstance(__magic_name__ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: snake_case_ : Union[str, Any] = [np.asarray(__magic_name__ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(__magic_name__ , np.ndarray ): snake_case_ : str = np.asarray(__magic_name__ , dtype=np.floataa ) elif isinstance(__magic_name__ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): snake_case_ : Dict = raw_speech.astype(np.floataa ) # always return batch if not is_batched: snake_case_ : Union[str, Any] = [raw_speech] # extract fbank features snake_case_ : List[str] = [self._extract_fbank_features(__magic_name__ ) for waveform in raw_speech] # convert into correct format for padding snake_case_ : Dict = BatchFeature({'''input_features''': features} ) snake_case_ : List[Any] = self.pad( __magic_name__ , padding=__magic_name__ , max_length=__magic_name__ , truncation=__magic_name__ , pad_to_multiple_of=__magic_name__ , return_attention_mask=__magic_name__ , **__magic_name__ , ) # make sure list is in array format snake_case_ : Tuple = padded_inputs.get('''input_features''' ) if isinstance(input_features[0] , __magic_name__ ): snake_case_ : str = [np.asarray(__magic_name__ , dtype=np.floataa ) for feature in input_features] snake_case_ : Optional[int] = padded_inputs.get('''attention_mask''' ) if attention_mask is not None: snake_case_ : int = [np.asarray(__magic_name__ , dtype=np.intaa ) for array in attention_mask] # Utterance-level cepstral mean and variance normalization if self.do_ceptral_normalize: snake_case_ : Optional[int] = ( np.array(__magic_name__ , dtype=np.intaa ) if self._get_padding_strategies(__magic_name__ , max_length=__magic_name__ ) is not PaddingStrategy.DO_NOT_PAD else None ) snake_case_ : List[Any] = self.normalize( padded_inputs['''input_features'''] , attention_mask=__magic_name__ ) if return_tensors is not None: snake_case_ : Any = padded_inputs.convert_to_tensors(__magic_name__ ) return padded_inputs
279
def lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase ) -> str: """simple docstring""" return "\n".join( f'''{number} * {i} = {number * i}''' for i in range(1 , number_of_terms + 1 ) ) if __name__ == "__main__": print(multiplication_table(number=5, number_of_terms=1_0))
279
1
"""simple docstring""" import numpy as np from PIL import Image def _snake_case ( _snake_case : np.ndarray , _snake_case : int , _snake_case : int ): lowerCAmelCase : Dict = np.array(_snake_case ) if arr.shape[0] != arr.shape[1]: raise ValueError('''The input array is not a square matrix''' ) lowerCAmelCase : int = 0 lowerCAmelCase : Dict = 0 lowerCAmelCase : str = 0 lowerCAmelCase : Union[str, Any] = 0 # compute the shape of the output matrix lowerCAmelCase : Tuple = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape maxpool_shape lowerCAmelCase : Dict = np.zeros((maxpool_shape, maxpool_shape) ) while i < arr.shape[0]: if i + size > arr.shape[0]: # if the end of the matrix is reached, break break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the maximum of the pooling matrix lowerCAmelCase : List[Any] = np.max(arr[i : i + size, j : j + size] ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 lowerCAmelCase : int = 0 lowerCAmelCase : Tuple = 0 return updated_arr def _snake_case ( _snake_case : np.ndarray , _snake_case : int , _snake_case : int ): lowerCAmelCase : Union[str, Any] = np.array(_snake_case ) if arr.shape[0] != arr.shape[1]: raise ValueError('''The input array is not a square matrix''' ) lowerCAmelCase : Optional[Any] = 0 lowerCAmelCase : Any = 0 lowerCAmelCase : int = 0 lowerCAmelCase : int = 0 # compute the shape of the output matrix lowerCAmelCase : str = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape avgpool_shape lowerCAmelCase : Dict = np.zeros((avgpool_shape, avgpool_shape) ) while i < arr.shape[0]: # if the end of the matrix is reached, break if i + size > arr.shape[0]: break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the average of the pooling matrix lowerCAmelCase : Optional[int] = int(np.average(arr[i : i + size, j : j + size] ) ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 lowerCAmelCase : str = 0 lowerCAmelCase : List[Any] = 0 return updated_arr # Main Function if __name__ == "__main__": from doctest import testmod testmod(name='''avgpooling''', verbose=True) # Loading the image snake_case__ : Optional[Any] = Image.open('''path_to_image''') # Converting the image to numpy array and maxpooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show() # Converting the image to numpy array and averagepooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()
359
"""simple docstring""" import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class snake_case_( a__ ): __UpperCamelCase = (DDPMScheduler,) def lowerCamelCase__ ( self : List[Any] , **UpperCamelCase_ : Union[str, Any] ): lowerCAmelCase : Optional[Any] = { '''num_train_timesteps''': 1_0_0_0, '''beta_start''': 0.0_001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', '''variance_type''': '''fixed_small''', '''clip_sample''': True, } config.update(**UpperCamelCase_ ) return config def lowerCamelCase__ ( self : Optional[int] ): for timesteps in [1, 5, 1_0_0, 1_0_0_0]: self.check_over_configs(num_train_timesteps=UpperCamelCase_ ) def lowerCamelCase__ ( self : Tuple ): for beta_start, beta_end in zip([0.0_001, 0.001, 0.01, 0.1] , [0.002, 0.02, 0.2, 2] ): self.check_over_configs(beta_start=UpperCamelCase_ , beta_end=UpperCamelCase_ ) def lowerCamelCase__ ( self : str ): for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=UpperCamelCase_ ) def lowerCamelCase__ ( self : Optional[Any] ): for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=UpperCamelCase_ ) def lowerCamelCase__ ( self : Optional[int] ): for clip_sample in [True, False]: self.check_over_configs(clip_sample=UpperCamelCase_ ) def lowerCamelCase__ ( self : Any ): self.check_over_configs(thresholding=UpperCamelCase_ ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=UpperCamelCase_ , prediction_type=UpperCamelCase_ , sample_max_value=UpperCamelCase_ , ) def lowerCamelCase__ ( self : Tuple ): for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=UpperCamelCase_ ) def lowerCamelCase__ ( self : str ): for t in [0, 5_0_0, 9_9_9]: self.check_over_forward(time_step=UpperCamelCase_ ) def lowerCamelCase__ ( self : int ): lowerCAmelCase : str = self.scheduler_classes[0] lowerCAmelCase : Dict = self.get_scheduler_config() lowerCAmelCase : Dict = scheduler_class(**UpperCamelCase_ ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(4_8_7 ) - 0.00_979 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(9_9_9 ) - 0.02 ) ) < 1E-5 def lowerCamelCase__ ( self : Tuple ): lowerCAmelCase : List[Any] = self.scheduler_classes[0] lowerCAmelCase : List[Any] = self.get_scheduler_config() lowerCAmelCase : List[str] = scheduler_class(**UpperCamelCase_ ) lowerCAmelCase : Union[str, Any] = len(UpperCamelCase_ ) lowerCAmelCase : List[str] = self.dummy_model() lowerCAmelCase : Union[str, Any] = self.dummy_sample_deter lowerCAmelCase : List[Any] = torch.manual_seed(0 ) for t in reversed(range(UpperCamelCase_ ) ): # 1. predict noise residual lowerCAmelCase : Optional[int] = model(UpperCamelCase_ , UpperCamelCase_ ) # 2. predict previous mean of sample x_t-1 lowerCAmelCase : Optional[Any] = scheduler.step(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , generator=UpperCamelCase_ ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance lowerCAmelCase : Union[str, Any] = pred_prev_sample lowerCAmelCase : str = torch.sum(torch.abs(UpperCamelCase_ ) ) lowerCAmelCase : int = torch.mean(torch.abs(UpperCamelCase_ ) ) assert abs(result_sum.item() - 258.9_606 ) < 1E-2 assert abs(result_mean.item() - 0.3_372 ) < 1E-3 def lowerCamelCase__ ( self : Any ): lowerCAmelCase : Optional[int] = self.scheduler_classes[0] lowerCAmelCase : Any = self.get_scheduler_config(prediction_type='''v_prediction''' ) lowerCAmelCase : Tuple = scheduler_class(**UpperCamelCase_ ) lowerCAmelCase : Dict = len(UpperCamelCase_ ) lowerCAmelCase : Any = self.dummy_model() lowerCAmelCase : Any = self.dummy_sample_deter lowerCAmelCase : List[Any] = torch.manual_seed(0 ) for t in reversed(range(UpperCamelCase_ ) ): # 1. predict noise residual lowerCAmelCase : str = model(UpperCamelCase_ , UpperCamelCase_ ) # 2. predict previous mean of sample x_t-1 lowerCAmelCase : List[Any] = scheduler.step(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , generator=UpperCamelCase_ ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance lowerCAmelCase : List[Any] = pred_prev_sample lowerCAmelCase : List[str] = torch.sum(torch.abs(UpperCamelCase_ ) ) lowerCAmelCase : Dict = torch.mean(torch.abs(UpperCamelCase_ ) ) assert abs(result_sum.item() - 202.0_296 ) < 1E-2 assert abs(result_mean.item() - 0.2_631 ) < 1E-3 def lowerCamelCase__ ( self : Any ): lowerCAmelCase : Dict = self.scheduler_classes[0] lowerCAmelCase : Tuple = self.get_scheduler_config() lowerCAmelCase : int = scheduler_class(**UpperCamelCase_ ) lowerCAmelCase : List[Any] = [1_0_0, 8_7, 5_0, 1, 0] scheduler.set_timesteps(timesteps=UpperCamelCase_ ) lowerCAmelCase : Dict = scheduler.timesteps for i, timestep in enumerate(UpperCamelCase_ ): if i == len(UpperCamelCase_ ) - 1: lowerCAmelCase : List[Any] = -1 else: lowerCAmelCase : Union[str, Any] = timesteps[i + 1] lowerCAmelCase : Any = scheduler.previous_timestep(UpperCamelCase_ ) lowerCAmelCase : Dict = prev_t.item() self.assertEqual(UpperCamelCase_ , UpperCamelCase_ ) def lowerCamelCase__ ( self : Dict ): lowerCAmelCase : Union[str, Any] = self.scheduler_classes[0] lowerCAmelCase : List[Any] = self.get_scheduler_config() lowerCAmelCase : Tuple = scheduler_class(**UpperCamelCase_ ) lowerCAmelCase : int = [1_0_0, 8_7, 5_0, 5_1, 0] with self.assertRaises(UpperCamelCase_ , msg='''`custom_timesteps` must be in descending order.''' ): scheduler.set_timesteps(timesteps=UpperCamelCase_ ) def lowerCamelCase__ ( self : Tuple ): lowerCAmelCase : Any = self.scheduler_classes[0] lowerCAmelCase : Optional[int] = self.get_scheduler_config() lowerCAmelCase : str = scheduler_class(**UpperCamelCase_ ) lowerCAmelCase : List[str] = [1_0_0, 8_7, 5_0, 1, 0] lowerCAmelCase : int = len(UpperCamelCase_ ) with self.assertRaises(UpperCamelCase_ , msg='''Can only pass one of `num_inference_steps` or `custom_timesteps`.''' ): scheduler.set_timesteps(num_inference_steps=UpperCamelCase_ , timesteps=UpperCamelCase_ ) def lowerCamelCase__ ( self : Optional[Any] ): lowerCAmelCase : List[Any] = self.scheduler_classes[0] lowerCAmelCase : Tuple = self.get_scheduler_config() lowerCAmelCase : Dict = scheduler_class(**UpperCamelCase_ ) lowerCAmelCase : Optional[Any] = [scheduler.config.num_train_timesteps] with self.assertRaises( UpperCamelCase_ , msg='''`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}''' , ): scheduler.set_timesteps(timesteps=UpperCamelCase_ )
314
0
import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging __a = logging.get_logger(__name__) __a = { 'BAAI/AltCLIP': 'https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json', # See all AltCLIP models at https://huggingface.co/models?filter=altclip } class lowercase__( UpperCAmelCase ): """simple docstring""" a :str = 'altclip_text_model' def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any]=2_5_0_0_0_2 , SCREAMING_SNAKE_CASE_ : int=1_0_2_4 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=2_4 , SCREAMING_SNAKE_CASE_ : Tuple=1_6 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=4_0_9_6 , SCREAMING_SNAKE_CASE_ : str="gelu" , SCREAMING_SNAKE_CASE_ : Dict=0.1 , SCREAMING_SNAKE_CASE_ : str=0.1 , SCREAMING_SNAKE_CASE_ : List[str]=5_1_4 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=1 , SCREAMING_SNAKE_CASE_ : Any=0.02 , SCREAMING_SNAKE_CASE_ : Optional[Any]=0.02 , SCREAMING_SNAKE_CASE_ : str=1e-05 , SCREAMING_SNAKE_CASE_ : List[str]=1 , SCREAMING_SNAKE_CASE_ : str=0 , SCREAMING_SNAKE_CASE_ : Dict=2 , SCREAMING_SNAKE_CASE_ : int="absolute" , SCREAMING_SNAKE_CASE_ : Tuple=True , SCREAMING_SNAKE_CASE_ : Optional[Any]=7_6_8 , **SCREAMING_SNAKE_CASE_ : int , ) -> Any: super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ , bos_token_id=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) lowercase_ = vocab_size lowercase_ = hidden_size lowercase_ = num_hidden_layers lowercase_ = num_attention_heads lowercase_ = hidden_act lowercase_ = intermediate_size lowercase_ = hidden_dropout_prob lowercase_ = attention_probs_dropout_prob lowercase_ = max_position_embeddings lowercase_ = type_vocab_size lowercase_ = initializer_range lowercase_ = initializer_factor lowercase_ = layer_norm_eps lowercase_ = position_embedding_type lowercase_ = use_cache lowercase_ = project_dim class lowercase__( UpperCAmelCase ): """simple docstring""" a :Tuple = 'altclip_vision_model' def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : List[Any]=7_6_8 , SCREAMING_SNAKE_CASE_ : Dict=3_0_7_2 , SCREAMING_SNAKE_CASE_ : List[Any]=5_1_2 , SCREAMING_SNAKE_CASE_ : Optional[Any]=1_2 , SCREAMING_SNAKE_CASE_ : List[Any]=1_2 , SCREAMING_SNAKE_CASE_ : Dict=3 , SCREAMING_SNAKE_CASE_ : Optional[int]=2_2_4 , SCREAMING_SNAKE_CASE_ : List[str]=3_2 , SCREAMING_SNAKE_CASE_ : Tuple="quick_gelu" , SCREAMING_SNAKE_CASE_ : Dict=1e-5 , SCREAMING_SNAKE_CASE_ : Dict=0.0 , SCREAMING_SNAKE_CASE_ : Dict=0.02 , SCREAMING_SNAKE_CASE_ : Tuple=1.0 , **SCREAMING_SNAKE_CASE_ : str , ) -> str: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = hidden_size lowercase_ = intermediate_size lowercase_ = projection_dim lowercase_ = num_hidden_layers lowercase_ = num_attention_heads lowercase_ = num_channels lowercase_ = patch_size lowercase_ = image_size lowercase_ = initializer_range lowercase_ = initializer_factor lowercase_ = attention_dropout lowercase_ = layer_norm_eps lowercase_ = hidden_act @classmethod def _lowercase ( cls : Dict , SCREAMING_SNAKE_CASE_ : Union[str, os.PathLike] , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> "PretrainedConfig": cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE_ ) lowercase_ , lowercase_ = cls.get_config_dict(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) # get the vision config dict if we are loading from AltCLIPConfig if config_dict.get('''model_type''' ) == "altclip": lowercase_ = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) class lowercase__( UpperCAmelCase ): """simple docstring""" a :Any = 'altclip' a :List[str] = True def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : int=None , SCREAMING_SNAKE_CASE_ : List[Any]=None , SCREAMING_SNAKE_CASE_ : List[str]=7_6_8 , SCREAMING_SNAKE_CASE_ : Dict=2.65_92 , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> List[str]: # If `_config_dict` exist, we use them for the backward compatibility. # We pop out these 2 attributes before calling `super().__init__` to avoid them being saved (which causes a lot # of confusion!). lowercase_ = kwargs.pop('''text_config_dict''' , SCREAMING_SNAKE_CASE_ ) lowercase_ = kwargs.pop('''vision_config_dict''' , SCREAMING_SNAKE_CASE_ ) super().__init__(**SCREAMING_SNAKE_CASE_ ) # Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in # `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most # cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`. if text_config_dict is not None: if text_config is None: lowercase_ = {} # This is the complete result when using `text_config_dict`. lowercase_ = AltCLIPTextConfig(**SCREAMING_SNAKE_CASE_ ).to_dict() # Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different. for key, value in _text_config_dict.items(): if key in text_config and value != text_config[key] and key not in ["transformers_version"]: # If specified in `text_config_dict` if key in text_config_dict: lowercase_ = ( f'''`{key}` is found in both `text_config_dict` and `text_config` but with different values. ''' f'''The value `text_config_dict["{key}"]` will be used instead.''' ) # If inferred from default argument values (just to be super careful) else: lowercase_ = ( f'''`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The ''' f'''value `text_config["{key}"]` will be overriden.''' ) logger.warning(SCREAMING_SNAKE_CASE_ ) # Update all values in `text_config` with the ones in `_text_config_dict`. text_config.update(_text_config_dict ) if vision_config_dict is not None: if vision_config is None: lowercase_ = {} # This is the complete result when using `vision_config_dict`. lowercase_ = AltCLIPVisionConfig(**SCREAMING_SNAKE_CASE_ ).to_dict() # convert keys to string instead of integer if "id2label" in _vision_config_dict: lowercase_ = { str(SCREAMING_SNAKE_CASE_ ): value for key, value in _vision_config_dict['''id2label'''].items() } # Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different. for key, value in _vision_config_dict.items(): if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]: # If specified in `vision_config_dict` if key in vision_config_dict: lowercase_ = ( f'''`{key}` is found in both `vision_config_dict` and `vision_config` but with different ''' f'''values. The value `vision_config_dict["{key}"]` will be used instead.''' ) # If inferred from default argument values (just to be super careful) else: lowercase_ = ( f'''`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. ''' f'''The value `vision_config["{key}"]` will be overriden.''' ) logger.warning(SCREAMING_SNAKE_CASE_ ) # Update all values in `vision_config` with the ones in `_vision_config_dict`. vision_config.update(_vision_config_dict ) if text_config is None: lowercase_ = {} logger.info('''`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values.''' ) if vision_config is None: lowercase_ = {} logger.info('''`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values.''' ) lowercase_ = AltCLIPTextConfig(**SCREAMING_SNAKE_CASE_ ) lowercase_ = AltCLIPVisionConfig(**SCREAMING_SNAKE_CASE_ ) lowercase_ = projection_dim lowercase_ = logit_scale_init_value lowercase_ = 1.0 @classmethod def _lowercase ( cls : Union[str, Any] , SCREAMING_SNAKE_CASE_ : AltCLIPTextConfig , SCREAMING_SNAKE_CASE_ : AltCLIPVisionConfig , **SCREAMING_SNAKE_CASE_ : Any ) -> List[Any]: return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : Union[str, Any] ) -> Dict: lowercase_ = copy.deepcopy(self.__dict__ ) lowercase_ = self.text_config.to_dict() lowercase_ = self.vision_config.to_dict() lowercase_ = self.__class__.model_type return output
30
from __future__ import annotations def a ( snake_case__: list[int] , snake_case__: int , snake_case__: int , snake_case__: int ): '''simple docstring''' if (direction == 1 and array[indexa] > array[indexa]) or ( direction == 0 and array[indexa] < array[indexa] ): lowercase_ , lowercase_ = array[indexa], array[indexa] def a ( snake_case__: list[int] , snake_case__: int , snake_case__: int , snake_case__: int ): '''simple docstring''' if length > 1: lowercase_ = int(length / 2 ) for i in range(snake_case__ , low + middle ): comp_and_swap(snake_case__ , snake_case__ , i + middle , snake_case__ ) bitonic_merge(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) bitonic_merge(snake_case__ , low + middle , snake_case__ , snake_case__ ) def a ( snake_case__: list[int] , snake_case__: int , snake_case__: int , snake_case__: int ): '''simple docstring''' if length > 1: lowercase_ = int(length / 2 ) bitonic_sort(snake_case__ , snake_case__ , snake_case__ , 1 ) bitonic_sort(snake_case__ , low + middle , snake_case__ , 0 ) bitonic_merge(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) if __name__ == "__main__": __a = input('Enter numbers separated by a comma:\n').strip() __a = [int(item.strip()) for item in user_input.split(',')] bitonic_sort(unsorted, 0, len(unsorted), 1) print('\nSorted array in ascending order is: ', end='') print(*unsorted, sep=', ') bitonic_merge(unsorted, 0, len(unsorted), 0) print('Sorted array in descending order is: ', end='') print(*unsorted, sep=', ')
30
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __A : Optional[Any] = { 'configuration_roberta': ['ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RobertaConfig', 'RobertaOnnxConfig'], 'tokenization_roberta': ['RobertaTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Optional[int] = ['RobertaTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : int = [ 'ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'RobertaForCausalLM', 'RobertaForMaskedLM', 'RobertaForMultipleChoice', 'RobertaForQuestionAnswering', 'RobertaForSequenceClassification', 'RobertaForTokenClassification', 'RobertaModel', 'RobertaPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = [ 'TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFRobertaForCausalLM', 'TFRobertaForMaskedLM', 'TFRobertaForMultipleChoice', 'TFRobertaForQuestionAnswering', 'TFRobertaForSequenceClassification', 'TFRobertaForTokenClassification', 'TFRobertaMainLayer', 'TFRobertaModel', 'TFRobertaPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = [ 'FlaxRobertaForCausalLM', 'FlaxRobertaForMaskedLM', 'FlaxRobertaForMultipleChoice', 'FlaxRobertaForQuestionAnswering', 'FlaxRobertaForSequenceClassification', 'FlaxRobertaForTokenClassification', 'FlaxRobertaModel', 'FlaxRobertaPreTrainedModel', ] if TYPE_CHECKING: from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig, RobertaOnnxConfig from .tokenization_roberta import RobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roberta_fast import RobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roberta import ( ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, RobertaForCausalLM, RobertaForMaskedLM, RobertaForMultipleChoice, RobertaForQuestionAnswering, RobertaForSequenceClassification, RobertaForTokenClassification, RobertaModel, RobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roberta import ( TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForMultipleChoice, TFRobertaForQuestionAnswering, TFRobertaForSequenceClassification, TFRobertaForTokenClassification, TFRobertaMainLayer, TFRobertaModel, TFRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, FlaxRobertaPreTrainedModel, ) else: import sys __A : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
360
import unittest import numpy as np from transformers import is_flax_available from transformers.testing_utils import require_flax from ..test_modeling_flax_common import ids_tensor if is_flax_available(): import jax import jax.numpy as jnp from transformers.generation import ( FlaxForcedBOSTokenLogitsProcessor, FlaxForcedEOSTokenLogitsProcessor, FlaxLogitsProcessorList, FlaxMinLengthLogitsProcessor, FlaxTemperatureLogitsWarper, FlaxTopKLogitsWarper, FlaxTopPLogitsWarper, ) @require_flax class _SCREAMING_SNAKE_CASE ( unittest.TestCase): def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )-> int: lowerCamelCase_ =jnp.ones((batch_size, length) ) / length return scores def _snake_case ( self )-> Dict: lowerCamelCase_ =None lowerCamelCase_ =20 lowerCamelCase_ =self._get_uniform_logits(batch_size=2 , length=_SCREAMING_SNAKE_CASE ) # tweak scores to not be uniform anymore lowerCamelCase_ =scores.at[1, 5].set((1 / length) + 0.1 ) # peak, 1st batch lowerCamelCase_ =scores.at[1, 10].set((1 / length) - 0.4 ) # valley, 1st batch # compute softmax lowerCamelCase_ =jax.nn.softmax(_SCREAMING_SNAKE_CASE , axis=-1 ) lowerCamelCase_ =FlaxTemperatureLogitsWarper(temperature=0.5 ) lowerCamelCase_ =FlaxTemperatureLogitsWarper(temperature=1.3 ) lowerCamelCase_ =jax.nn.softmax(temp_dist_warper_sharper(_SCREAMING_SNAKE_CASE , scores.copy() , cur_len=_SCREAMING_SNAKE_CASE ) , axis=-1 ) lowerCamelCase_ =jax.nn.softmax(temp_dist_warper_smoother(_SCREAMING_SNAKE_CASE , scores.copy() , cur_len=_SCREAMING_SNAKE_CASE ) , axis=-1 ) # uniform distribution stays uniform self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_sharp[0, :] , atol=1E-3 ) ) self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_smooth[0, :] , atol=1E-3 ) ) # sharp peaks get higher, valleys get lower self.assertLess(probs[1, :].max() , warped_prob_sharp[1, :].max() ) self.assertGreater(probs[1, :].min() , warped_prob_sharp[1, :].min() ) # smooth peaks get lower, valleys get higher self.assertGreater(probs[1, :].max() , warped_prob_smooth[1, :].max() ) self.assertLess(probs[1, :].min() , warped_prob_smooth[1, :].min() ) def _snake_case ( self )-> Any: lowerCamelCase_ =None lowerCamelCase_ =10 lowerCamelCase_ =2 # create ramp distribution lowerCamelCase_ =np.broadcast_to(np.arange(_SCREAMING_SNAKE_CASE )[None, :] , (batch_size, vocab_size) ).copy() lowerCamelCase_ =ramp_logits[1:, : vocab_size // 2] + vocab_size lowerCamelCase_ =FlaxTopKLogitsWarper(3 ) lowerCamelCase_ =top_k_warp(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) # check that correct tokens are filtered self.assertListEqual(jnp.isinf(scores[0] ).tolist() , 7 * [True] + 3 * [False] ) self.assertListEqual(jnp.isinf(scores[1] ).tolist() , 2 * [True] + 3 * [False] + 5 * [True] ) # check special case lowerCamelCase_ =5 lowerCamelCase_ =FlaxTopKLogitsWarper(top_k=1 , filter_value=0.0 , min_tokens_to_keep=3 ) lowerCamelCase_ =np.broadcast_to(np.arange(_SCREAMING_SNAKE_CASE )[None, :] , (batch_size, length) ).copy() lowerCamelCase_ =top_k_warp_safety_check(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) # min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified self.assertListEqual((scores == 0.0).sum(axis=-1 ).tolist() , [2, 2] ) def _snake_case ( self )-> Optional[int]: lowerCamelCase_ =None lowerCamelCase_ =10 lowerCamelCase_ =2 # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper) lowerCamelCase_ =np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.1_5, 0.3, 0.3, 0.2_5]] ) ) lowerCamelCase_ =FlaxTopPLogitsWarper(0.8 ) lowerCamelCase_ =np.exp(top_p_warp(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) ) # dist should be filtered to keep min num values so that sum is >= top_p # exp (-inf) => 0 lowerCamelCase_ =np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.2_5]] ) self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , atol=1E-3 ) ) # check edge cases with negative and extreme logits lowerCamelCase_ =np.broadcast_to(np.arange(_SCREAMING_SNAKE_CASE )[None, :] , (batch_size, vocab_size) ).copy() - ( vocab_size // 2 ) # make ramp_logits more extreme lowerCamelCase_ =ramp_logits[1] * 1_0_0.0 # make sure at least 2 tokens are kept lowerCamelCase_ =FlaxTopPLogitsWarper(0.9 , min_tokens_to_keep=2 , filter_value=0.0 ) lowerCamelCase_ =top_p_warp(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) # first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2. self.assertListEqual((filtered_dist != 0.0).sum(axis=-1 ).tolist() , [3, 2] ) def _snake_case ( self )-> List[Any]: lowerCamelCase_ =20 lowerCamelCase_ =4 lowerCamelCase_ =0 lowerCamelCase_ =FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=_SCREAMING_SNAKE_CASE ) # check that min length is applied at length 5 lowerCamelCase_ =ids_tensor((batch_size, 20) , vocab_size=20 ) lowerCamelCase_ =5 lowerCamelCase_ =self._get_uniform_logits(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCamelCase_ =min_dist_processor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist() , 4 * [-float("""inf""" )] ) # check that min length is not applied anymore at length 15 lowerCamelCase_ =self._get_uniform_logits(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCamelCase_ =15 lowerCamelCase_ =min_dist_processor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) self.assertFalse(jnp.isinf(_SCREAMING_SNAKE_CASE ).any() ) def _snake_case ( self )-> Optional[Any]: lowerCamelCase_ =20 lowerCamelCase_ =4 lowerCamelCase_ =0 lowerCamelCase_ =FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_SCREAMING_SNAKE_CASE ) # check that all scores are -inf except the bos_token_id score lowerCamelCase_ =ids_tensor((batch_size, 1) , vocab_size=20 ) lowerCamelCase_ =1 lowerCamelCase_ =self._get_uniform_logits(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCamelCase_ =logits_processor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :] ).all() ) self.assertListEqual(scores[:, bos_token_id].tolist() , 4 * [0] ) # score for bos_token_id shold be zero # check that bos_token_id is not forced if current length is greater than 1 lowerCamelCase_ =3 lowerCamelCase_ =self._get_uniform_logits(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCamelCase_ =logits_processor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) self.assertFalse(jnp.isinf(_SCREAMING_SNAKE_CASE ).any() ) def _snake_case ( self )-> int: lowerCamelCase_ =20 lowerCamelCase_ =4 lowerCamelCase_ =0 lowerCamelCase_ =5 lowerCamelCase_ =FlaxForcedEOSTokenLogitsProcessor(max_length=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE ) # check that all scores are -inf except the eos_token_id when max_length is reached lowerCamelCase_ =ids_tensor((batch_size, 4) , vocab_size=20 ) lowerCamelCase_ =4 lowerCamelCase_ =self._get_uniform_logits(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCamelCase_ =logits_processor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :] ).all() ) self.assertListEqual(scores[:, eos_token_id].tolist() , 4 * [0] ) # score for eos_token_id should be zero # check that eos_token_id is not forced if max_length is not reached lowerCamelCase_ =3 lowerCamelCase_ =self._get_uniform_logits(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCamelCase_ =logits_processor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) self.assertFalse(jnp.isinf(_SCREAMING_SNAKE_CASE ).any() ) def _snake_case ( self )-> Dict: lowerCamelCase_ =4 lowerCamelCase_ =10 lowerCamelCase_ =15 lowerCamelCase_ =2 lowerCamelCase_ =1 lowerCamelCase_ =15 # dummy input_ids and scores lowerCamelCase_ =ids_tensor((batch_size, sequence_length) , _SCREAMING_SNAKE_CASE ) lowerCamelCase_ =input_ids.copy() lowerCamelCase_ =self._get_uniform_logits(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCamelCase_ =scores.copy() # instantiate all dist processors lowerCamelCase_ =FlaxTemperatureLogitsWarper(temperature=0.5 ) lowerCamelCase_ =FlaxTopKLogitsWarper(3 ) lowerCamelCase_ =FlaxTopPLogitsWarper(0.8 ) # instantiate all logits processors lowerCamelCase_ =FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =FlaxForcedEOSTokenLogitsProcessor(max_length=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =10 # no processor list lowerCamelCase_ =temp_dist_warp(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =top_k_warp(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =top_p_warp(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =min_dist_proc(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =bos_dist_proc(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =eos_dist_proc(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) # with processor list lowerCamelCase_ =FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] ) lowerCamelCase_ =processor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) # scores should be equal self.assertTrue(jnp.allclose(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , atol=1E-3 ) ) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist() ) def _snake_case ( self )-> List[str]: lowerCamelCase_ =4 lowerCamelCase_ =10 lowerCamelCase_ =15 lowerCamelCase_ =2 lowerCamelCase_ =1 lowerCamelCase_ =15 # dummy input_ids and scores lowerCamelCase_ =ids_tensor((batch_size, sequence_length) , _SCREAMING_SNAKE_CASE ) lowerCamelCase_ =input_ids.copy() lowerCamelCase_ =self._get_uniform_logits(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCamelCase_ =scores.copy() # instantiate all dist processors lowerCamelCase_ =FlaxTemperatureLogitsWarper(temperature=0.5 ) lowerCamelCase_ =FlaxTopKLogitsWarper(3 ) lowerCamelCase_ =FlaxTopPLogitsWarper(0.8 ) # instantiate all logits processors lowerCamelCase_ =FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =FlaxForcedEOSTokenLogitsProcessor(max_length=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =10 # no processor list def run_no_processor_list(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): lowerCamelCase_ =temp_dist_warp(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =top_k_warp(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =top_p_warp(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =min_dist_proc(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =bos_dist_proc(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =eos_dist_proc(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) return scores # with processor list def run_processor_list(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): lowerCamelCase_ =FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] ) lowerCamelCase_ =processor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , cur_len=_SCREAMING_SNAKE_CASE ) return scores lowerCamelCase_ =jax.jit(_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =jax.jit(_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =jitted_run_no_processor_list(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCamelCase_ =jitted_run_processor_list(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # scores should be equal self.assertTrue(jnp.allclose(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , atol=1E-3 ) ) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist() )
49
0
"""simple docstring""" def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase ) -> List[Any]: lowercase__ : str = (boundary[1] - boundary[0]) / steps lowercase__ : Optional[int] = boundary[0] lowercase__ : Any = boundary[1] lowercase__ : Optional[int] = make_points(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) lowercase__ : str = 0.0 y += (h / 2.0) * f(SCREAMING_SNAKE_CASE_ ) for i in x_i: # print(i) y += h * f(SCREAMING_SNAKE_CASE_ ) y += (h / 2.0) * f(SCREAMING_SNAKE_CASE_ ) return y def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) -> Tuple: lowercase__ : List[Any] = a + h while x < (b - h): yield x lowercase__ : str = x + h def __UpperCAmelCase ( __lowerCamelCase ) -> Any: # enter your function here lowercase__ : Optional[int] = (x - 0) * (x - 0) return y def __UpperCAmelCase ( ) -> Union[str, Any]: lowercase__ : Tuple = 0.0 # Lower bound of integration lowercase__ : Tuple = 1.0 # Upper bound of integration lowercase__ : Dict = 1_0.0 # define number of steps or resolution lowercase__ : Any = [a, b] # define boundary of integration lowercase__ : Optional[Any] = method_a(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) print(f"""y = {y}""" ) if __name__ == "__main__": main()
16
import math def snake_case__ ( SCREAMING_SNAKE_CASE_ : int ): '''simple docstring''' lowercase__ : Optional[Any] = [] lowercase__ : str = 2 lowercase__ : Optional[Any] = int(math.sqrt(SCREAMING_SNAKE_CASE_ ) ) # Size of every segment lowercase__ : Dict = [True] * (end + 1) lowercase__ : Union[str, Any] = [] while start <= end: if temp[start] is True: in_prime.append(SCREAMING_SNAKE_CASE_ ) for i in range(start * start , end + 1 , SCREAMING_SNAKE_CASE_ ): lowercase__ : int = False start += 1 prime += in_prime lowercase__ : Optional[int] = end + 1 lowercase__ : List[str] = min(2 * end , SCREAMING_SNAKE_CASE_ ) while low <= n: lowercase__ : str = [True] * (high - low + 1) for each in in_prime: lowercase__ : str = math.floor(low / each ) * each if t < low: t += each for j in range(SCREAMING_SNAKE_CASE_ , high + 1 , SCREAMING_SNAKE_CASE_ ): lowercase__ : Optional[Any] = False for j in range(len(SCREAMING_SNAKE_CASE_ ) ): if temp[j] is True: prime.append(j + low ) lowercase__ : Optional[Any] = high + 1 lowercase__ : Optional[int] = min(high + end , SCREAMING_SNAKE_CASE_ ) return prime print(sieve(10**6))
214
0
'''simple docstring''' import os from pathlib import Path import numpy as np import pytest from pack_dataset import pack_data_dir from parameterized import parameterized from save_len_file import save_len_file from torch.utils.data import DataLoader from transformers import AutoTokenizer from transformers.models.mbart.modeling_mbart import shift_tokens_right from transformers.testing_utils import TestCasePlus, slow from utils import FAIRSEQ_AVAILABLE, DistributedSortishSampler, LegacySeqaSeqDataset, SeqaSeqDataset a__ : Tuple = 'bert-base-cased' a__ : str = 'google/pegasus-xsum' a__ : Any = [' Sam ate lunch today.', 'Sams lunch ingredients.'] a__ : int = ['A very interesting story about what I ate for lunch.', 'Avocado, celery, turkey, coffee'] a__ : Any = 'patrickvonplaten/t5-tiny-random' a__ : int = 'sshleifer/bart-tiny-random' a__ : Any = 'sshleifer/tiny-mbart' a__ : Union[str, Any] = 'sshleifer/tiny-marian-en-de' def _lowercase ( __A ,__A ): '''simple docstring''' __UpperCamelCase = '''\n'''.join(lowerCAmelCase__ ) Path(lowerCAmelCase__ ).open("""w""" ).writelines(lowerCAmelCase__ ) def _lowercase ( __A ): '''simple docstring''' for split in ["train", "val", "test"]: _dump_articles(os.path.join(lowerCAmelCase__ ,f"{split}.source" ) ,lowerCAmelCase__ ) _dump_articles(os.path.join(lowerCAmelCase__ ,f"{split}.target" ) ,lowerCAmelCase__ ) return tmp_dir class UpperCAmelCase__ ( __lowercase): @parameterized.expand( [ MBART_TINY, MARIAN_TINY, T5_TINY, BART_TINY, PEGASUS_XSUM, ] , ) @slow def __lowerCamelCase ( self , lowercase ) -> str: __UpperCamelCase = AutoTokenizer.from_pretrained(snake_case_ ) __UpperCamelCase = make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) __UpperCamelCase = max(len(tokenizer.encode(snake_case_ ) ) for a in ARTICLES ) __UpperCamelCase = max(len(tokenizer.encode(snake_case_ ) ) for a in SUMMARIES ) __UpperCamelCase = 4 __UpperCamelCase = 8 assert max_len_target > max_src_len # Will be truncated assert max_len_source > max_src_len # Will be truncated __UpperCamelCase = '''ro_RO''', '''de_DE''' # ignored for all but mbart, but never causes error. __UpperCamelCase = SeqaSeqDataset( snake_case_ , data_dir=snake_case_ , type_path="""train""" , max_source_length=snake_case_ , max_target_length=snake_case_ , src_lang=snake_case_ , tgt_lang=snake_case_ , ) __UpperCamelCase = DataLoader(snake_case_ , batch_size=2 , collate_fn=train_dataset.collate_fn ) for batch in dataloader: assert isinstance(snake_case_ , snake_case_ ) assert batch["attention_mask"].shape == batch["input_ids"].shape # show that articles were trimmed. assert batch["input_ids"].shape[1] == max_src_len # show that targets are the same len assert batch["labels"].shape[1] == max_tgt_len if tok_name != MBART_TINY: continue # check language codes in correct place __UpperCamelCase = shift_tokens_right(batch["""labels"""] , tokenizer.pad_token_id ) assert batch["decoder_input_ids"][0, 0].item() == tokenizer.lang_code_to_id[tgt_lang] assert batch["decoder_input_ids"][0, -1].item() == tokenizer.eos_token_id assert batch["input_ids"][0, -2].item() == tokenizer.eos_token_id assert batch["input_ids"][0, -1].item() == tokenizer.lang_code_to_id[src_lang] break # No need to test every batch @parameterized.expand([BART_TINY, BERT_BASE_CASED] ) def __lowerCamelCase ( self , lowercase ) -> Optional[Any]: __UpperCamelCase = AutoTokenizer.from_pretrained(snake_case_ ) __UpperCamelCase = make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) __UpperCamelCase = max(len(tokenizer.encode(snake_case_ ) ) for a in ARTICLES ) __UpperCamelCase = max(len(tokenizer.encode(snake_case_ ) ) for a in SUMMARIES ) __UpperCamelCase = 4 __UpperCamelCase = LegacySeqaSeqDataset( snake_case_ , data_dir=snake_case_ , type_path="""train""" , max_source_length=2_0 , max_target_length=snake_case_ , ) __UpperCamelCase = DataLoader(snake_case_ , batch_size=2 , collate_fn=train_dataset.collate_fn ) for batch in dataloader: assert batch["attention_mask"].shape == batch["input_ids"].shape # show that articles were trimmed. assert batch["input_ids"].shape[1] == max_len_source assert 2_0 >= batch["input_ids"].shape[1] # trimmed significantly # show that targets were truncated assert batch["labels"].shape[1] == trunc_target # Truncated assert max_len_target > trunc_target # Truncated break # No need to test every batch def __lowerCamelCase ( self ) -> Optional[Any]: __UpperCamelCase = AutoTokenizer.from_pretrained("""facebook/mbart-large-cc25""" ) __UpperCamelCase = Path(make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) ) __UpperCamelCase = tmp_dir.joinpath("""train.source""" ).open().readlines() __UpperCamelCase = Path(make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) ) pack_data_dir(snake_case_ , snake_case_ , 1_2_8 , snake_case_ ) __UpperCamelCase = {x.name for x in tmp_dir.iterdir()} __UpperCamelCase = {x.name for x in save_dir.iterdir()} __UpperCamelCase = save_dir.joinpath("""train.source""" ).open().readlines() # orig: [' Sam ate lunch today.\n', 'Sams lunch ingredients.'] # desired_packed: [' Sam ate lunch today.\n Sams lunch ingredients.'] assert len(snake_case_ ) < len(snake_case_ ) assert len(snake_case_ ) == 1 assert len(packed_examples[0] ) == sum(len(snake_case_ ) for x in orig_examples ) assert orig_paths == new_paths @pytest.mark.skipif(not FAIRSEQ_AVAILABLE , reason="""This test requires fairseq""" ) def __lowerCamelCase ( self ) -> Tuple: if not FAIRSEQ_AVAILABLE: return __UpperCamelCase = self._get_dataset(max_len=6_4 ) __UpperCamelCase = 6_4 __UpperCamelCase = ds.make_dynamic_sampler(snake_case_ , required_batch_size_multiple=snake_case_ ) __UpperCamelCase = [len(snake_case_ ) for x in batch_sampler] assert len(set(snake_case_ ) ) > 1 # it's not dynamic batch size if every batch is the same length assert sum(snake_case_ ) == len(snake_case_ ) # no dropped or added examples __UpperCamelCase = DataLoader(snake_case_ , batch_sampler=snake_case_ , collate_fn=ds.collate_fn , num_workers=2 ) __UpperCamelCase = [] __UpperCamelCase = [] for batch in data_loader: __UpperCamelCase = batch['''input_ids'''].shape __UpperCamelCase = src_shape[0] assert bs % required_batch_size_multiple == 0 or bs < required_batch_size_multiple __UpperCamelCase = np.product(batch["""input_ids"""].shape ) num_src_per_batch.append(snake_case_ ) if num_src_tokens > (max_tokens * 1.1): failures.append(snake_case_ ) assert num_src_per_batch[0] == max(snake_case_ ) if failures: raise AssertionError(f"too many tokens in {len(snake_case_ )} batches" ) def __lowerCamelCase ( self ) -> str: __UpperCamelCase = self._get_dataset(max_len=5_1_2 ) __UpperCamelCase = 2 __UpperCamelCase = ds.make_sortish_sampler(snake_case_ , shuffle=snake_case_ ) __UpperCamelCase = DataLoader(snake_case_ , batch_size=snake_case_ , collate_fn=ds.collate_fn , num_workers=2 ) __UpperCamelCase = DataLoader(snake_case_ , batch_size=snake_case_ , collate_fn=ds.collate_fn , num_workers=2 , sampler=snake_case_ ) __UpperCamelCase = tokenizer.pad_token_id def count_pad_tokens(lowercase , lowercase="input_ids" ): return [batch[k].eq(snake_case_ ).sum().item() for batch in data_loader] assert sum(count_pad_tokens(snake_case_ , k="""labels""" ) ) < sum(count_pad_tokens(snake_case_ , k="""labels""" ) ) assert sum(count_pad_tokens(snake_case_ ) ) < sum(count_pad_tokens(snake_case_ ) ) assert len(snake_case_ ) == len(snake_case_ ) def __lowerCamelCase ( self , lowercase=1_0_0_0 , lowercase=1_2_8 ) -> str: if os.getenv("""USE_REAL_DATA""" , snake_case_ ): __UpperCamelCase = '''examples/seq2seq/wmt_en_ro''' __UpperCamelCase = max_len * 2 * 6_4 if not Path(snake_case_ ).joinpath("""train.len""" ).exists(): save_len_file(snake_case_ , snake_case_ ) else: __UpperCamelCase = '''examples/seq2seq/test_data/wmt_en_ro''' __UpperCamelCase = max_len * 4 save_len_file(snake_case_ , snake_case_ ) __UpperCamelCase = AutoTokenizer.from_pretrained(snake_case_ ) __UpperCamelCase = SeqaSeqDataset( snake_case_ , data_dir=snake_case_ , type_path="""train""" , max_source_length=snake_case_ , max_target_length=snake_case_ , n_obs=snake_case_ , ) return ds, max_tokens, tokenizer def __lowerCamelCase ( self ) -> int: __UpperCamelCase = self._get_dataset() __UpperCamelCase = set(DistributedSortishSampler(snake_case_ , 2_5_6 , num_replicas=2 , rank=0 , add_extra_examples=snake_case_ ) ) __UpperCamelCase = set(DistributedSortishSampler(snake_case_ , 2_5_6 , num_replicas=2 , rank=1 , add_extra_examples=snake_case_ ) ) assert idsa.intersection(snake_case_ ) == set() @parameterized.expand( [ MBART_TINY, MARIAN_TINY, T5_TINY, BART_TINY, PEGASUS_XSUM, ] , ) def __lowerCamelCase ( self , lowercase ) -> Dict: __UpperCamelCase = AutoTokenizer.from_pretrained(snake_case_ , use_fast=snake_case_ ) if tok_name == MBART_TINY: __UpperCamelCase = SeqaSeqDataset( snake_case_ , data_dir=make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) , type_path="""train""" , max_source_length=4 , max_target_length=8 , src_lang="""EN""" , tgt_lang="""FR""" , ) __UpperCamelCase = train_dataset.dataset_kwargs assert "src_lang" in kwargs and "tgt_lang" in kwargs else: __UpperCamelCase = SeqaSeqDataset( snake_case_ , data_dir=make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) , type_path="""train""" , max_source_length=4 , max_target_length=8 , ) __UpperCamelCase = train_dataset.dataset_kwargs assert "add_prefix_space" not in kwargs if tok_name != BART_TINY else "add_prefix_space" in kwargs assert len(snake_case_ ) == 1 if tok_name == BART_TINY else len(snake_case_ ) == 0
352
'''simple docstring''' # Author: OMKAR PATHAK, Nwachukwu Chidiebere # Use a Python dictionary to construct the graph. from __future__ import annotations from pprint import pformat from typing import Generic, TypeVar a__ : int = TypeVar('T') class UpperCAmelCase__ ( Generic[T]): def __init__( self , lowercase = True ) -> None: __UpperCamelCase = {} # dictionary of lists __UpperCamelCase = directed def __lowerCamelCase ( self , lowercase , lowercase ) -> GraphAdjacencyList[T]: if not self.directed: # For undirected graphs # if both source vertex and destination vertex are both present in the # adjacency list, add destination vertex to source vertex list of adjacent # vertices and add source vertex to destination vertex list of adjacent # vertices. if source_vertex in self.adj_list and destination_vertex in self.adj_list: self.adj_list[source_vertex].append(lowercase ) self.adj_list[destination_vertex].append(lowercase ) # if only source vertex is present in adjacency list, add destination vertex # to source vertex list of adjacent vertices, then create a new vertex with # destination vertex as key and assign a list containing the source vertex # as it's first adjacent vertex. elif source_vertex in self.adj_list: self.adj_list[source_vertex].append(lowercase ) __UpperCamelCase = [source_vertex] # if only destination vertex is present in adjacency list, add source vertex # to destination vertex list of adjacent vertices, then create a new vertex # with source vertex as key and assign a list containing the source vertex # as it's first adjacent vertex. elif destination_vertex in self.adj_list: self.adj_list[destination_vertex].append(lowercase ) __UpperCamelCase = [destination_vertex] # if both source vertex and destination vertex are not present in adjacency # list, create a new vertex with source vertex as key and assign a list # containing the destination vertex as it's first adjacent vertex also # create a new vertex with destination vertex as key and assign a list # containing the source vertex as it's first adjacent vertex. else: __UpperCamelCase = [destination_vertex] __UpperCamelCase = [source_vertex] else: # For directed graphs # if both source vertex and destination vertex are present in adjacency # list, add destination vertex to source vertex list of adjacent vertices. if source_vertex in self.adj_list and destination_vertex in self.adj_list: self.adj_list[source_vertex].append(lowercase ) # if only source vertex is present in adjacency list, add destination # vertex to source vertex list of adjacent vertices and create a new vertex # with destination vertex as key, which has no adjacent vertex elif source_vertex in self.adj_list: self.adj_list[source_vertex].append(lowercase ) __UpperCamelCase = [] # if only destination vertex is present in adjacency list, create a new # vertex with source vertex as key and assign a list containing destination # vertex as first adjacent vertex elif destination_vertex in self.adj_list: __UpperCamelCase = [destination_vertex] # if both source vertex and destination vertex are not present in adjacency # list, create a new vertex with source vertex as key and a list containing # destination vertex as it's first adjacent vertex. Then create a new vertex # with destination vertex as key, which has no adjacent vertex else: __UpperCamelCase = [destination_vertex] __UpperCamelCase = [] return self def __repr__( self ) -> str: return pformat(self.adj_list )
243
0
from __future__ import annotations from numpy import array, cos, cross, floataa, radians, sin from numpy.typing import NDArray def _SCREAMING_SNAKE_CASE ( a , a , a = False ) -> list[float]: if radian_mode: return [magnitude * cos(a ), magnitude * sin(a )] return [magnitude * cos(radians(a ) ), magnitude * sin(radians(a ) )] def _SCREAMING_SNAKE_CASE ( a , a , a = 10**-1 ) -> bool: __A : NDArray[floataa] = cross(a , a ) __A : float = sum(a ) return abs(a ) < eps if __name__ == "__main__": # Test to check if it works UpperCAmelCase : Tuple = array( [ polar_force(718.4, 1_80 - 30), polar_force(879.54, 45), polar_force(1_00, -90), ] ) UpperCAmelCase : NDArray[floataa] = array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem 1 in image_data/2D_problems.jpg UpperCAmelCase : Dict = array( [ polar_force(30 * 9.81, 15), polar_force(2_15, 1_80 - 45), polar_force(2_64, 90 - 30), ] ) UpperCAmelCase : List[Any] = array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem in image_data/2D_problems_1.jpg UpperCAmelCase : Dict = array([[0, -20_00], [0, -12_00], [0, 1_56_00], [0, -1_24_00]]) UpperCAmelCase : int = array([[0, 0], [6, 0], [10, 0], [12, 0]]) assert in_static_equilibrium(forces, location) import doctest doctest.testmod()
280
from heapq import heappop, heappush import numpy as np def _SCREAMING_SNAKE_CASE ( a , a , a , a , ) -> tuple[float | int, list[tuple[int, int]]]: __A , __A : int = grid.shape __A : Any = [-1, 1, 0, 0] __A : Optional[Any] = [0, 0, -1, 1] if allow_diagonal: dx += [-1, -1, 1, 1] dy += [-1, 1, -1, 1] __A , __A : Optional[int] = [(0, source)], set() __A : Any = np.full((rows, cols) , np.inf ) __A : Any = 0 __A : Any = np.empty((rows, cols) , dtype=a ) __A : Optional[Any] = None while queue: ((__A) , (__A)) : List[str] = heappop(a ) if (x, y) in visited: continue visited.add((x, y) ) if (x, y) == destination: __A : int = [] while (x, y) != source: path.append((x, y) ) __A , __A : Optional[int] = predecessors[x, y] path.append(a ) # add the source manually path.reverse() return matrix[destination], path for i in range(len(a ) ): __A , __A : Union[str, Any] = x + dx[i], y + dy[i] if 0 <= nx < rows and 0 <= ny < cols: __A : Optional[int] = grid[nx][ny] if next_node == 1 and matrix[nx, ny] > dist + 1: heappush(a , (dist + 1, (nx, ny)) ) __A : List[Any] = dist + 1 __A : Union[str, Any] = (x, y) return np.inf, [] if __name__ == "__main__": import doctest doctest.testmod()
280
1
'''simple docstring''' from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class __snake_case( _lowerCAmelCase ): '''simple docstring''' def __snake_case ( self ) -> Tuple: return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def __snake_case ( self ) -> str: lowerCAmelCase = {"""col_1""": [3, 2, 1, 0], """col_2""": ["""a""", """b""", """c""", """d"""]} return Dataset.from_dict(A_ ) def __snake_case ( self ) -> Optional[int]: lowerCAmelCase = self._create_example_records() lowerCAmelCase = Dataset.from_list(A_ ) self.assertListEqual(dset.column_names , ["""col_1""", """col_2"""] ) for i, r in enumerate(A_ ): self.assertDictEqual(A_ , example_records[i] ) def __snake_case ( self ) -> int: lowerCAmelCase = self._create_example_records() lowerCAmelCase = Dataset.from_list(A_ ) lowerCAmelCase = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]} ) self.assertEqual(dset.info , dset_from_dict.info ) def __snake_case ( self ) -> List[Any]: # checks what happens with missing columns lowerCAmelCase = [{"""col_1""": 1}, {"""col_2""": """x"""}] lowerCAmelCase = Dataset.from_list(A_ ) self.assertDictEqual(dset[0] , {"""col_1""": 1} ) self.assertDictEqual(dset[1] , {"""col_1""": None} ) # NB: first record is used for columns def __snake_case ( self ) -> str: # checks if the type can be inferred from the second record lowerCAmelCase = [{"""col_1""": []}, {"""col_1""": [1, 2]}] lowerCAmelCase = Dataset.from_list(A_ ) self.assertEqual(dset.info.features["""col_1"""] , Sequence(Value("""int64""" ) ) ) def __snake_case ( self ) -> int: lowerCAmelCase = Dataset.from_list([] ) self.assertEqual(len(A_ ) , 0 ) self.assertListEqual(dset.column_names , [] )
187
'''simple docstring''' class __snake_case( _lowerCAmelCase ): '''simple docstring''' pass class __snake_case( _lowerCAmelCase ): '''simple docstring''' pass class __snake_case: '''simple docstring''' def __init__( self ) -> int: lowerCAmelCase = [ [], [], [], ] def __snake_case ( self , A_ , A_ ) -> None: try: if len(self.queues[priority] ) >= 100: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(A_ ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def __snake_case ( self ) -> int: for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self ) -> str: return "\n".join(f'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class __snake_case: '''simple docstring''' def __init__( self ) -> Dict: lowerCAmelCase = [] def __snake_case ( self , A_ ) -> None: if len(self.queue ) == 100: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(A_ ) def __snake_case ( self ) -> int: if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: lowerCAmelCase = min(self.queue ) self.queue.remove(A_ ) return data def __str__( self ) -> str: return str(self.queue ) def _snake_case ( ) -> Tuple: """simple docstring""" lowerCAmelCase = FixedPriorityQueue() fpq.enqueue(0 , 10 ) fpq.enqueue(1 , 70 ) fpq.enqueue(0 , 100 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 64 ) fpq.enqueue(0 , 128 ) print(_SCREAMING_SNAKE_CASE ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(_SCREAMING_SNAKE_CASE ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def _snake_case ( ) -> Optional[Any]: """simple docstring""" lowerCAmelCase = ElementPriorityQueue() epq.enqueue(10 ) epq.enqueue(70 ) epq.enqueue(100 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(64 ) epq.enqueue(128 ) print(_SCREAMING_SNAKE_CASE ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(_SCREAMING_SNAKE_CASE ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
187
1
'''simple docstring''' def lowerCamelCase_ ( SCREAMING_SNAKE_CASE__ : int = 1000 ) -> int: UpperCAmelCase_ : Union[str, Any] = 2**power UpperCAmelCase_ : List[Any] = 0 while n: UpperCAmelCase_ , UpperCAmelCase_ : Optional[Any] = r + n % 10, n // 10 return r if __name__ == "__main__": print(solution(int(str(input()).strip())))
125
'''simple docstring''' from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy import tensorflow as tf from transformers import ( TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST, BertConfig, DPRConfig, TFDPRContextEncoder, TFDPRQuestionEncoder, TFDPRReader, ) class __a : def __init__( self : Optional[Any] , __magic_name__ : Tuple , __magic_name__ : Optional[int]=13 , __magic_name__ : str=7 , __magic_name__ : Dict=True , __magic_name__ : Dict=True , __magic_name__ : Union[str, Any]=True , __magic_name__ : Union[str, Any]=True , __magic_name__ : Tuple=99 , __magic_name__ : List[str]=32 , __magic_name__ : int=2 , __magic_name__ : List[str]=4 , __magic_name__ : Tuple=37 , __magic_name__ : Dict="gelu" , __magic_name__ : int=0.1 , __magic_name__ : Optional[int]=0.1 , __magic_name__ : Optional[int]=5_12 , __magic_name__ : Tuple=16 , __magic_name__ : Optional[int]=2 , __magic_name__ : Optional[int]=0.0_2 , __magic_name__ : Dict=3 , __magic_name__ : str=4 , __magic_name__ : Optional[Any]=None , __magic_name__ : Any=0 , ) -> Any: """simple docstring""" UpperCAmelCase_ : str = parent UpperCAmelCase_ : List[Any] = batch_size UpperCAmelCase_ : List[Any] = seq_length UpperCAmelCase_ : Dict = is_training UpperCAmelCase_ : Optional[Any] = use_input_mask UpperCAmelCase_ : Tuple = use_token_type_ids UpperCAmelCase_ : int = use_labels UpperCAmelCase_ : Union[str, Any] = vocab_size UpperCAmelCase_ : Union[str, Any] = hidden_size UpperCAmelCase_ : Dict = num_hidden_layers UpperCAmelCase_ : Any = num_attention_heads UpperCAmelCase_ : Any = intermediate_size UpperCAmelCase_ : Dict = hidden_act UpperCAmelCase_ : Tuple = hidden_dropout_prob UpperCAmelCase_ : List[Any] = attention_probs_dropout_prob UpperCAmelCase_ : str = max_position_embeddings UpperCAmelCase_ : str = type_vocab_size UpperCAmelCase_ : List[str] = type_sequence_label_size UpperCAmelCase_ : Tuple = initializer_range UpperCAmelCase_ : str = num_labels UpperCAmelCase_ : Tuple = num_choices UpperCAmelCase_ : Union[str, Any] = scope UpperCAmelCase_ : Union[str, Any] = projection_dim def UpperCAmelCase__ ( self : Optional[Any] ) -> int: """simple docstring""" UpperCAmelCase_ : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase_ : Dict = None if self.use_input_mask: # follow test_modeling_tf_ctrl.py UpperCAmelCase_ : List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase_ : Tuple = None if self.use_token_type_ids: UpperCAmelCase_ : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCAmelCase_ : Optional[int] = None UpperCAmelCase_ : Optional[Any] = None UpperCAmelCase_ : int = None if self.use_labels: UpperCAmelCase_ : List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase_ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCAmelCase_ : Optional[int] = ids_tensor([self.batch_size] , self.num_choices ) UpperCAmelCase_ : Optional[Any] = BertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__magic_name__ , initializer_range=self.initializer_range , ) UpperCAmelCase_ : List[str] = DPRConfig(projection_dim=self.projection_dim , **config.to_dict() ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCAmelCase__ ( self : str , __magic_name__ : str , __magic_name__ : Tuple , __magic_name__ : int , __magic_name__ : List[str] , __magic_name__ : List[str] , __magic_name__ : Tuple , __magic_name__ : Any ) -> List[str]: """simple docstring""" UpperCAmelCase_ : Optional[Any] = TFDPRContextEncoder(config=__magic_name__ ) UpperCAmelCase_ : Tuple = model(__magic_name__ , attention_mask=__magic_name__ , token_type_ids=__magic_name__ ) UpperCAmelCase_ : int = model(__magic_name__ , token_type_ids=__magic_name__ ) UpperCAmelCase_ : Any = model(__magic_name__ ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.projection_dim or self.hidden_size) ) def UpperCAmelCase__ ( self : List[str] , __magic_name__ : int , __magic_name__ : Dict , __magic_name__ : Tuple , __magic_name__ : Dict , __magic_name__ : str , __magic_name__ : Any , __magic_name__ : Tuple ) -> int: """simple docstring""" UpperCAmelCase_ : List[str] = TFDPRQuestionEncoder(config=__magic_name__ ) UpperCAmelCase_ : Optional[int] = model(__magic_name__ , attention_mask=__magic_name__ , token_type_ids=__magic_name__ ) UpperCAmelCase_ : Optional[int] = model(__magic_name__ , token_type_ids=__magic_name__ ) UpperCAmelCase_ : List[Any] = model(__magic_name__ ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.projection_dim or self.hidden_size) ) def UpperCAmelCase__ ( self : List[Any] , __magic_name__ : List[Any] , __magic_name__ : List[Any] , __magic_name__ : Any , __magic_name__ : int , __magic_name__ : Optional[int] , __magic_name__ : Tuple , __magic_name__ : List[Any] ) -> List[str]: """simple docstring""" UpperCAmelCase_ : int = TFDPRReader(config=__magic_name__ ) UpperCAmelCase_ : Tuple = model(__magic_name__ , attention_mask=__magic_name__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.relevance_logits.shape , (self.batch_size,) ) def UpperCAmelCase__ ( self : Optional[Any] ) -> Any: """simple docstring""" UpperCAmelCase_ : List[Any] = self.prepare_config_and_inputs() ( ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ) : Optional[int] = config_and_inputs UpperCAmelCase_ : Any = {'''input_ids''': input_ids} return config, inputs_dict @require_tf class __a (lowerCamelCase , lowerCamelCase , unittest.TestCase ): __a : Any = ( ( TFDPRContextEncoder, TFDPRQuestionEncoder, TFDPRReader, ) if is_tf_available() else () ) __a : int = {"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {} __a : str = False __a : str = False __a : Dict = False __a : Optional[Any] = False __a : Any = False def UpperCAmelCase__ ( self : int ) -> Tuple: """simple docstring""" UpperCAmelCase_ : Optional[int] = TFDPRModelTester(self ) UpperCAmelCase_ : Dict = ConfigTester(self , config_class=__magic_name__ , hidden_size=37 ) def UpperCAmelCase__ ( self : List[str] ) -> Dict: """simple docstring""" self.config_tester.run_common_tests() def UpperCAmelCase__ ( self : Optional[int] ) -> Optional[Any]: """simple docstring""" UpperCAmelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_dpr_context_encoder(*__magic_name__ ) def UpperCAmelCase__ ( self : Optional[Any] ) -> Any: """simple docstring""" UpperCAmelCase_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_dpr_question_encoder(*__magic_name__ ) def UpperCAmelCase__ ( self : int ) -> List[str]: """simple docstring""" UpperCAmelCase_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_dpr_reader(*__magic_name__ ) @slow def UpperCAmelCase__ ( self : Tuple ) -> Union[str, Any]: """simple docstring""" for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : Union[str, Any] = TFDPRContextEncoder.from_pretrained(__magic_name__ ) self.assertIsNotNone(__magic_name__ ) for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : Optional[Any] = TFDPRContextEncoder.from_pretrained(__magic_name__ ) self.assertIsNotNone(__magic_name__ ) for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : Tuple = TFDPRQuestionEncoder.from_pretrained(__magic_name__ ) self.assertIsNotNone(__magic_name__ ) for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : Tuple = TFDPRReader.from_pretrained(__magic_name__ ) self.assertIsNotNone(__magic_name__ ) @require_tf class __a (unittest.TestCase ): @slow def UpperCAmelCase__ ( self : Optional[int] ) -> str: """simple docstring""" UpperCAmelCase_ : Any = TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base''' ) UpperCAmelCase_ : Optional[int] = tf.constant( [[1_01, 75_92, 10_10, 20_03, 20_26, 38_99, 1_01_40, 10_29, 1_02]] ) # [CLS] hello, is my dog cute? [SEP] UpperCAmelCase_ : List[Any] = model(__magic_name__ )[0] # embedding shape = (1, 768) # compare the actual values for a slice. UpperCAmelCase_ : List[str] = tf.constant( [ [ 0.0_3_2_3_6_2_5_3, 0.1_2_7_5_3_3_3_5, 0.1_6_8_1_8_5_0_9, 0.0_0_2_7_9_7_8_6, 0.3_8_9_6_9_3_3, 0.2_4_2_6_4_9_4_5, 0.2_1_7_8_9_7_1, -0.0_2_3_3_5_2_2_7, -0.0_8_4_8_1_9_5_9, -0.1_4_3_2_4_1_1_7, ] ] ) self.assertTrue(numpy.allclose(output[:, :10].numpy() , expected_slice.numpy() , atol=1E-4 ) )
125
1
'''simple docstring''' def _a( UpperCamelCase__ : int = 1_0_0_0_0_0_0 ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : str =1 SCREAMING_SNAKE_CASE__ : str =1 SCREAMING_SNAKE_CASE__ : Optional[Any] ={1: 1} for inputa in range(2, UpperCamelCase__ ): SCREAMING_SNAKE_CASE__ : Union[str, Any] =0 SCREAMING_SNAKE_CASE__ : List[Any] =inputa while True: if number in counters: counter += counters[number] break if number % 2 == 0: number //= 2 counter += 1 else: SCREAMING_SNAKE_CASE__ : Optional[int] =(3 * number) + 1 counter += 1 if inputa not in counters: SCREAMING_SNAKE_CASE__ : str =counter if counter > pre_counter: SCREAMING_SNAKE_CASE__ : List[str] =inputa SCREAMING_SNAKE_CASE__ : List[str] =counter return largest_number if __name__ == "__main__": print(solution(int(input().strip())))
353
'''simple docstring''' import argparse import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_dummies.py a_ = 'src/diffusers' # Matches is_xxx_available() a_ = re.compile(R'is\_([a-z_]*)_available\(\)') # Matches from xxx import bla a_ = re.compile(R'\s+from\s+\S*\s+import\s+([^\(\s].*)\n') a_ = '\n{0} = None\n' a_ = '\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n\n @classmethod\n def from_config(cls, *args, **kwargs):\n requires_backends(cls, {1})\n\n @classmethod\n def from_pretrained(cls, *args, **kwargs):\n requires_backends(cls, {1})\n' a_ = '\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n' def _a( UpperCamelCase__ : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Dict =_re_backend.findall(UpperCamelCase__ ) if len(UpperCamelCase__ ) == 0: return None return "_and_".join(UpperCamelCase__ ) def _a( ): '''simple docstring''' with open(os.path.join(UpperCamelCase__, '''__init__.py''' ), '''r''', encoding='''utf-8''', newline='''\n''' ) as f: SCREAMING_SNAKE_CASE__ : List[str] =f.readlines() # Get to the point we do the actual imports for type checking SCREAMING_SNAKE_CASE__ : Optional[int] =0 SCREAMING_SNAKE_CASE__ : List[str] ={} # Go through the end of the file while line_index < len(UpperCamelCase__ ): # If the line contains is_backend_available, we grab all objects associated with the `else` block SCREAMING_SNAKE_CASE__ : List[Any] =find_backend(lines[line_index] ) if backend is not None: while not lines[line_index].startswith('''else:''' ): line_index += 1 line_index += 1 SCREAMING_SNAKE_CASE__ : List[Any] =[] # Until we unindent, add backend objects to the list while line_index < len(UpperCamelCase__ ) and len(lines[line_index] ) > 1: SCREAMING_SNAKE_CASE__ : Optional[Any] =lines[line_index] SCREAMING_SNAKE_CASE__ : Optional[Any] =_re_single_line_import.search(UpperCamelCase__ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(''', ''' ) ) elif line.startswith(''' ''' * 8 ): objects.append(line[8:-2] ) line_index += 1 if len(UpperCamelCase__ ) > 0: SCREAMING_SNAKE_CASE__ : Any =objects else: line_index += 1 return backend_specific_objects def _a( UpperCamelCase__ : Union[str, Any], UpperCamelCase__ : List[Any] ): '''simple docstring''' if name.isupper(): return DUMMY_CONSTANT.format(UpperCamelCase__ ) elif name.islower(): return DUMMY_FUNCTION.format(UpperCamelCase__, UpperCamelCase__ ) else: return DUMMY_CLASS.format(UpperCamelCase__, UpperCamelCase__ ) def _a( UpperCamelCase__ : Any=None ): '''simple docstring''' if backend_specific_objects is None: SCREAMING_SNAKE_CASE__ : int =read_init() # For special correspondence backend to module name as used in the function requires_modulename SCREAMING_SNAKE_CASE__ : Optional[int] ={} for backend, objects in backend_specific_objects.items(): SCREAMING_SNAKE_CASE__ : Tuple ='''[''' + ''', '''.join(f"\"{b}\"" for b in backend.split('''_and_''' ) ) + ''']''' SCREAMING_SNAKE_CASE__ : List[str] ='''# This file is autogenerated by the command `make fix-copies`, do not edit.\n''' dummy_file += "from ..utils import DummyObject, requires_backends\n\n" dummy_file += "\n".join([create_dummy_object(UpperCamelCase__, UpperCamelCase__ ) for o in objects] ) SCREAMING_SNAKE_CASE__ : Tuple =dummy_file return dummy_files def _a( UpperCamelCase__ : Any=False ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Tuple =create_dummy_files() # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py SCREAMING_SNAKE_CASE__ : List[str] ={'''torch''': '''pt'''} # Locate actual dummy modules and read their content. SCREAMING_SNAKE_CASE__ : List[str] =os.path.join(UpperCamelCase__, '''utils''' ) SCREAMING_SNAKE_CASE__ : Union[str, Any] ={ backend: os.path.join(UpperCamelCase__, f"dummy_{short_names.get(UpperCamelCase__, UpperCamelCase__ )}_objects.py" ) for backend in dummy_files.keys() } SCREAMING_SNAKE_CASE__ : str ={} for backend, file_path in dummy_file_paths.items(): if os.path.isfile(UpperCamelCase__ ): with open(UpperCamelCase__, '''r''', encoding='''utf-8''', newline='''\n''' ) as f: SCREAMING_SNAKE_CASE__ : List[Any] =f.read() else: SCREAMING_SNAKE_CASE__ : int ='''''' for backend in dummy_files.keys(): if dummy_files[backend] != actual_dummies[backend]: if overwrite: print( f"Updating diffusers.utils.dummy_{short_names.get(UpperCamelCase__, UpperCamelCase__ )}_objects.py as the main " '''__init__ has new objects.''' ) with open(dummy_file_paths[backend], '''w''', encoding='''utf-8''', newline='''\n''' ) as f: f.write(dummy_files[backend] ) else: raise ValueError( '''The main __init__ has objects that are not present in ''' f"diffusers.utils.dummy_{short_names.get(UpperCamelCase__, UpperCamelCase__ )}_objects.py. Run `make fix-copies` " '''to fix this.''' ) if __name__ == "__main__": a_ = argparse.ArgumentParser() parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.') a_ = parser.parse_args() check_dummies(args.fix_and_overwrite)
222
0
'''simple docstring''' import json import os import re import shutil import tempfile import unittest from typing import Tuple from transformers import AddedToken, BatchEncoding, ByTaTokenizer from transformers.utils import cached_property, is_tf_available, is_torch_available from ...test_tokenization_common import TokenizerTesterMixin if is_torch_available(): __a = '''pt''' elif is_tf_available(): __a = '''tf''' else: __a = '''jax''' class UpperCAmelCase_ ( _lowercase , unittest.TestCase ): """simple docstring""" lowercase = ByTaTokenizer lowercase = False def lowerCamelCase ( self : str ): super().setUp() snake_case__ : str = ByTaTokenizer() tokenizer.save_pretrained(self.tmpdirname ) @cached_property def lowerCamelCase ( self : Optional[int] ): return ByTaTokenizer.from_pretrained("""google/byt5-small""" ) def lowerCamelCase ( self : Any , **snake_case_ : Tuple ): return self.tokenizer_class.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def lowerCamelCase ( self : Union[str, Any] , snake_case_ : str , snake_case_ : Any=False , snake_case_ : Union[str, Any]=20 , snake_case_ : Optional[int]=5 ): # XXX The default common tokenizer tests assume that every ID is decodable on its own. # This assumption is invalid for ByT5 because single bytes might not be # valid utf-8 (byte 128 for instance). # Here we're overriding the smallest possible method to provide # a clean sequence without making the same assumption. snake_case__ : List[str] = [] for i in range(len(UpperCamelCase__ ) ): try: snake_case__ : Tuple = tokenizer.decode([i] , clean_up_tokenization_spaces=UpperCamelCase__ ) except UnicodeDecodeError: pass toks.append((i, tok) ) snake_case__ : Union[str, Any] = list(filter(lambda snake_case_ : re.match(r"""^[ a-zA-Z]+$""" , t[1] ) , UpperCamelCase__ ) ) snake_case__ : Tuple = list(filter(lambda snake_case_ : [t[0]] == tokenizer.encode(t[1] , add_special_tokens=UpperCamelCase__ ) , UpperCamelCase__ ) ) if max_length is not None and len(UpperCamelCase__ ) > max_length: snake_case__ : Dict = toks[:max_length] if min_length is not None and len(UpperCamelCase__ ) < min_length and len(UpperCamelCase__ ) > 0: while len(UpperCamelCase__ ) < min_length: snake_case__ : Optional[Any] = toks + toks # toks_str = [t[1] for t in toks] snake_case__ : Union[str, Any] = [t[0] for t in toks] # Ensure consistency snake_case__ : Union[str, Any] = tokenizer.decode(UpperCamelCase__ , clean_up_tokenization_spaces=UpperCamelCase__ ) if " " not in output_txt and len(UpperCamelCase__ ) > 1: snake_case__ : Optional[int] = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=UpperCamelCase__ ) + """ """ + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=UpperCamelCase__ ) ) if with_prefix_space: snake_case__ : str = """ """ + output_txt snake_case__ : List[Any] = tokenizer.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) return output_txt, output_ids def lowerCamelCase ( self : Tuple ): snake_case__ : str = self.ta_base_tokenizer snake_case__ : Union[str, Any] = tokenizer(["""hi</s>""", """I went to the gym</s>""", """</s>"""] ) snake_case__ : Optional[int] = tokenizer(["""hi""", """I went to the gym""", """"""] ) self.assertListEqual(batch_with_eos_added["""input_ids"""] , batch_without_eos_added["""input_ids"""] ) def lowerCamelCase ( self : Tuple ): snake_case__ : Optional[Any] = self.ta_base_tokenizer snake_case__ : Dict = """Unicode €.""" snake_case__ : List[Any] = tokenizer(UpperCamelCase__ ) snake_case__ : List[str] = [88, 113, 108, 102, 114, 103, 104, 35, 229, 133, 175, 49, 1] self.assertEqual(encoded["""input_ids"""] , UpperCamelCase__ ) # decoding snake_case__ : Tuple = tokenizer.decode(UpperCamelCase__ ) self.assertEqual(UpperCamelCase__ , """Unicode €.</s>""" ) snake_case__ : List[Any] = tokenizer("""e è é ê ë""" ) snake_case__ : List[Any] = [104, 35, 198, 171, 35, 198, 172, 35, 198, 173, 35, 198, 174, 1] self.assertEqual(encoded["""input_ids"""] , UpperCamelCase__ ) # decoding snake_case__ : str = tokenizer.decode(UpperCamelCase__ ) self.assertEqual(UpperCamelCase__ , """e è é ê ë</s>""" ) # encode/decode, but with `encode` instead of `__call__` self.assertEqual(tokenizer.decode(tokenizer.encode("""e è é ê ë""" ) ) , """e è é ê ë</s>""" ) def lowerCamelCase ( self : Any ): snake_case__ : int = self.ta_base_tokenizer snake_case__ : Tuple = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] # fmt: off snake_case__ : List[str] = [68, 35, 111, 114, 113, 106, 35, 115, 100, 117, 100, 106, 117, 100, 115, 107, 35, 105, 114, 117, 35, 118, 120, 112, 112, 100, 117, 108, 125, 100, 119, 108, 114, 113, 49, 1, 0] # fmt: on snake_case__ : int = tokenizer(UpperCamelCase__ , padding=UpperCamelCase__ , return_tensors=UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) if FRAMEWORK != "jax": snake_case__ : Any = list(batch.input_ids.numpy()[0] ) else: snake_case__ : str = list(batch.input_ids.tolist()[0] ) self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) self.assertEqual((2, 37) , batch.input_ids.shape ) self.assertEqual((2, 37) , batch.attention_mask.shape ) def lowerCamelCase ( self : List[Any] ): snake_case__ : List[str] = self.ta_base_tokenizer snake_case__ : Tuple = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] snake_case__ : Tuple = tokenizer(UpperCamelCase__ , padding=UpperCamelCase__ , return_tensors=UpperCamelCase__ ) # check if input_ids are returned and no decoder_input_ids self.assertIn("""input_ids""" , UpperCamelCase__ ) self.assertIn("""attention_mask""" , UpperCamelCase__ ) self.assertNotIn("""decoder_input_ids""" , UpperCamelCase__ ) self.assertNotIn("""decoder_attention_mask""" , UpperCamelCase__ ) def lowerCamelCase ( self : Tuple ): snake_case__ : str = self.ta_base_tokenizer snake_case__ : List[Any] = [ """Summary of the text.""", """Another summary.""", ] snake_case__ : Union[str, Any] = tokenizer( text_target=UpperCamelCase__ , max_length=32 , padding="""max_length""" , truncation=UpperCamelCase__ , return_tensors=UpperCamelCase__ ) self.assertEqual(32 , targets["""input_ids"""].shape[1] ) def lowerCamelCase ( self : str ): snake_case__ : Tuple = self.ta_base_tokenizer snake_case__ : str = ["""A long paragraph for summarization. </s>"""] snake_case__ : Optional[Any] = ["""Summary of the text. </s>"""] # fmt: off snake_case__ : Tuple = [68, 35, 111, 114, 113, 106, 35, 115, 100, 117, 100, 106, 117, 100, 115, 107, 35, 105, 114, 117, 35, 118, 120, 112, 112, 100, 117, 108, 125, 100, 119, 108, 114, 113, 49, 35, 1] snake_case__ : Any = [86, 120, 112, 112, 100, 117, 124, 35, 114, 105, 35, 119, 107, 104, 35, 119, 104, 123, 119, 49, 35, 1] # fmt: on snake_case__ : Any = tokenizer(UpperCamelCase__ , text_target=UpperCamelCase__ ) self.assertEqual(UpperCamelCase__ , batch["""input_ids"""][0] ) self.assertEqual(UpperCamelCase__ , batch["""labels"""][0] ) def lowerCamelCase ( self : Optional[int] ): # safety check on max_len default value so we are sure the test works snake_case__ : Any = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): self.assertNotEqual(tokenizer.model_max_length , 42 ) # Now let's start the test snake_case__ : Any = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): # Isolate this from the other tests because we save additional tokens/etc snake_case__ : int = tempfile.mkdtemp() snake_case__ : List[str] = """ He is very happy, UNwant\u00E9d,running""" snake_case__ : List[Any] = tokenizer.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) tokenizer.save_pretrained(UpperCamelCase__ ) snake_case__ : List[Any] = tokenizer.__class__.from_pretrained(UpperCamelCase__ ) snake_case__ : Optional[int] = after_tokenizer.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) shutil.rmtree(UpperCamelCase__ ) snake_case__ : Any = self.get_tokenizers(model_max_length=42 ) for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): # Isolate this from the other tests because we save additional tokens/etc snake_case__ : Any = tempfile.mkdtemp() snake_case__ : Optional[Any] = """ He is very happy, UNwant\u00E9d,running""" tokenizer.add_tokens(["""bim""", """bambam"""] ) snake_case__ : Optional[int] = tokenizer.additional_special_tokens additional_special_tokens.append("""new_additional_special_token""" ) tokenizer.add_special_tokens({"""additional_special_tokens""": additional_special_tokens} ) snake_case__ : List[Any] = tokenizer.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) tokenizer.save_pretrained(UpperCamelCase__ ) snake_case__ : Union[str, Any] = tokenizer.__class__.from_pretrained(UpperCamelCase__ ) snake_case__ : int = after_tokenizer.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) self.assertIn("""new_additional_special_token""" , after_tokenizer.additional_special_tokens ) self.assertEqual(after_tokenizer.model_max_length , 42 ) snake_case__ : Any = tokenizer.__class__.from_pretrained(UpperCamelCase__ , model_max_length=43 ) self.assertEqual(tokenizer.model_max_length , 43 ) shutil.rmtree(UpperCamelCase__ ) def lowerCamelCase ( self : Optional[int] ): snake_case__ : List[Any] = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(UpperCamelCase__ ) with open(os.path.join(UpperCamelCase__ , """special_tokens_map.json""" ) , encoding="""utf-8""" ) as json_file: snake_case__ : Union[str, Any] = json.load(UpperCamelCase__ ) with open(os.path.join(UpperCamelCase__ , """tokenizer_config.json""" ) , encoding="""utf-8""" ) as json_file: snake_case__ : Optional[Any] = json.load(UpperCamelCase__ ) snake_case__ : Optional[int] = [f"<extra_id_{i}>" for i in range(125 )] snake_case__ : int = added_tokens_extra_ids + [ """an_additional_special_token""" ] snake_case__ : Optional[Any] = added_tokens_extra_ids + [ """an_additional_special_token""" ] with open(os.path.join(UpperCamelCase__ , """special_tokens_map.json""" ) , """w""" , encoding="""utf-8""" ) as outfile: json.dump(UpperCamelCase__ , UpperCamelCase__ ) with open(os.path.join(UpperCamelCase__ , """tokenizer_config.json""" ) , """w""" , encoding="""utf-8""" ) as outfile: json.dump(UpperCamelCase__ , UpperCamelCase__ ) # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and # "special_tokens_map.json" files snake_case__ : Dict = tokenizer_class.from_pretrained( UpperCamelCase__ , ) self.assertIn( """an_additional_special_token""" , tokenizer_without_change_in_init.additional_special_tokens ) # self.assertIn("an_additional_special_token",tokenizer_without_change_in_init.get_vocab()) # ByT5Tokenization no vocab self.assertEqual( ["""an_additional_special_token"""] , tokenizer_without_change_in_init.convert_ids_to_tokens( tokenizer_without_change_in_init.convert_tokens_to_ids(["""an_additional_special_token"""] ) ) , ) # Now we test that we can change the value of additional_special_tokens in the from_pretrained snake_case__ : Optional[Any] = added_tokens_extra_ids + [AddedToken("""a_new_additional_special_token""" , lstrip=UpperCamelCase__ )] snake_case__ : Any = tokenizer_class.from_pretrained( UpperCamelCase__ , additional_special_tokens=UpperCamelCase__ , ) self.assertIn("""a_new_additional_special_token""" , tokenizer.additional_special_tokens ) self.assertEqual( ["""a_new_additional_special_token"""] , tokenizer.convert_ids_to_tokens( tokenizer.convert_tokens_to_ids(["""a_new_additional_special_token"""] ) ) , ) def lowerCamelCase ( self : Union[str, Any] ): snake_case__ : Dict = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(UpperCamelCase__ ) snake_case__ : str = tokenizer_class.from_pretrained(UpperCamelCase__ ) self.assertTrue(tokenizer.decode([255] ) == """""" ) def lowerCamelCase ( self : Optional[int] ): pass def lowerCamelCase ( self : str ): pass def lowerCamelCase ( self : List[str] ): pass def lowerCamelCase ( self : Optional[int] ): pass def lowerCamelCase ( self : int ): # The default common tokenizer tests uses invalid tokens for ByT5 that can only accept one-character strings # and special added tokens as tokens snake_case__ : Dict = self.get_tokenizers(fast=UpperCamelCase__ , do_lower_case=UpperCamelCase__ ) for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): snake_case__ : Dict = ["""t""", """h""", """i""", """s""", """ """, """i""", """s""", """ """, """a""", """ """, """t""", """e""", """x""", """t""", """</s>"""] snake_case__ : Optional[int] = tokenizer.convert_tokens_to_string(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def lowerCamelCase ( self : Any ): snake_case__ : str = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): snake_case__ : str = [ """bos_token""", """eos_token""", """unk_token""", """sep_token""", """pad_token""", """cls_token""", """mask_token""", ] snake_case__ : str = 0 snake_case__ : Any = tokenizer.convert_ids_to_tokens( UpperCamelCase__ , skip_special_tokens=UpperCamelCase__ ) for attr in attributes_list: setattr(UpperCamelCase__ , attr + """_id""" , UpperCamelCase__ ) self.assertEqual(getattr(UpperCamelCase__ , UpperCamelCase__ ) , UpperCamelCase__ ) self.assertEqual(getattr(UpperCamelCase__ , attr + """_id""" ) , UpperCamelCase__ ) setattr(UpperCamelCase__ , attr + """_id""" , UpperCamelCase__ ) self.assertEqual(getattr(UpperCamelCase__ , UpperCamelCase__ ) , UpperCamelCase__ ) self.assertEqual(getattr(UpperCamelCase__ , attr + """_id""" ) , UpperCamelCase__ ) setattr(UpperCamelCase__ , """additional_special_tokens_ids""" , [] ) self.assertListEqual(getattr(UpperCamelCase__ , """additional_special_tokens""" ) , [] ) self.assertListEqual(getattr(UpperCamelCase__ , """additional_special_tokens_ids""" ) , [] ) setattr(UpperCamelCase__ , """additional_special_tokens_ids""" , [token_id_to_test_setters] ) self.assertListEqual(getattr(UpperCamelCase__ , """additional_special_tokens""" ) , [token_to_test_setters] ) self.assertListEqual(getattr(UpperCamelCase__ , """additional_special_tokens_ids""" ) , [token_id_to_test_setters] )
35
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _A : Dict ={ '''configuration_swinv2''': ['''SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Swinv2Config'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A : List[Any] =[ '''SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Swinv2ForImageClassification''', '''Swinv2ForMaskedImageModeling''', '''Swinv2Model''', '''Swinv2PreTrainedModel''', ] if TYPE_CHECKING: from .configuration_swinva import SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinvaConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swinva import ( SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST, SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel, SwinvaPreTrainedModel, ) else: import sys _A : Optional[Any] =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
41
0
import unittest from transformers import JukeboxTokenizer from transformers.testing_utils import require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" UpperCamelCase : Dict = JukeboxTokenizer UpperCamelCase : Any = { "artist": "Zac Brown Band", "genres": "Country", "lyrics": "I met a traveller from an antique land,\n Who said \"Two vast and trunkless legs of stone\n Stand in the desert. . . . Near them, on the sand,\n Half sunk a shattered visage lies, whose frown,\n And wrinkled lip, and sneer of cold command,\n Tell that its sculptor well those passions read\n Which yet survive, stamped on these lifeless things,\n The hand that mocked them, and the heart that fed;\n And on the pedestal, these words appear:\n My name is Ozymandias, King of Kings;\n Look on my Works, ye Mighty, and despair!\n Nothing beside remains. Round the decay\n Of that colossal Wreck, boundless and bare\n The lone and level sands stretch far away\n ", } @require_torch def __A ( self ) -> Dict: '''simple docstring''' import torch lowerCamelCase = JukeboxTokenizer.from_pretrained("""openai/jukebox-1b-lyrics""" ) lowerCamelCase = tokenizer(**self.metas )['input_ids'] # fmt: off lowerCamelCase = [ torch.tensor([[ 0, 0, 0, 71_69, 5_07, 9, 76, 39, 31, 46, 76, 27, 76, 46, 44, 27, 48, 31, 38, 38, 31, 44, 76, 32, 44, 41, 39, 76, 27, 40, 76, 27, 40, 46, 35, 43, 47, 31, 76, 38, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 41, 76, 45, 27, 35, 30, 76, 71, 20, 49, 41, 76, 48, 27, 45, 46, 76, 27, 40, 30, 76, 46, 44, 47, 40, 37, 38, 31, 45, 45, 76, 38, 31, 33, 45, 76, 41, 32, 76, 45, 46, 41, 40, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 19, 46, 27, 40, 30, 76, 35, 40, 76, 46, 34, 31, 76, 30, 31, 45, 31, 44, 46, 63, 76, 63, 76, 63, 76, 63, 76, 14, 31, 27, 44, 76, 46, 34, 31, 39, 64, 76, 41, 40, 76, 46, 34, 31, 76, 45, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 8, 27, 38, 32, 76, 45, 47, 40, 37, 76, 27, 76, 45, 34, 27, 46, 46, 31, 44, 31, 30, 76, 48, 35, 45, 27, 33, 31, 76, 38, 35, 31, 45, 64, 76, 49, 34, 41, 45, 31, 76, 32, 44, 41, 49, 40, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 49, 44, 35, 40, 37, 38, 31, 30, 76, 38, 35, 42, 64, 76, 27, 40, 30, 76, 45, 40, 31, 31, 44, 76, 41, 32, 76, 29, 41, 38, 30, 76, 29, 41, 39, 39, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 31, 38, 38, 76, 46, 34, 27, 46, 76, 35, 46, 45, 76, 45, 29, 47, 38, 42, 46, 41, 44, 76, 49, 31, 38, 38, 76, 46, 34, 41, 45, 31, 76, 42, 27, 45, 45, 35, 41, 40, 45, 76, 44, 31, 27, 30, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 35, 29, 34, 76, 51, 31, 46, 76, 45, 47, 44, 48, 35, 48, 31, 64, 76, 45, 46, 27, 39, 42, 31, 30, 76, 41, 40, 76, 46, 34, 31, 45, 31, 76, 38, 35, 32, 31, 38, 31, 45, 45, 76, 46, 34, 35, 40, 33, 45, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 34, 27, 40, 30, 76, 46, 34, 27, 46, 76, 39, 41, 29, 37, 31, 30, 76, 46, 34, 31, 39, 64, 76, 27, 40, 30, 76, 46, 34, 31, 76, 34, 31, 27, 44, 46, 76, 46, 34, 27, 46, 76, 32, 31, 30, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 41, 40, 76, 46, 34, 31, 76, 42, 31, 30, 31, 45, 46, 27, 38, 64, 76, 46, 34, 31, 45, 31, 76, 49, 41, 44, 30, 45, 76, 27, 42, 42, 31, 27, 44, 65, 78, 76, 76, 76, 76, 76, 76, 76, 76, 13, 51, 76, 40, 27, 39, 31, 76, 35, 45, 76, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 76, 11, 35, 40, 33, 76, 41, 32, 76, 11, 35, 40, 33, 45, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 12, 41, 41, 37, 76, 41, 40, 76, 39, 51, 76, 23, 41, 44, 37, 45, 64, 76, 51, 31, 76, 13, 35, 33, 34, 46, 51, 64, 76, 27, 40, 30, 76, 30, 31, 45, 42, 27, 35, 44, 67, 78, 76, 76, 76, 76, 76, 76, 76, 76, 14, 41, 46, 34, 35, 40, 33, 76, 28, 31, 45, 35, 30, 31, 76, 44, 31, 39, 27, 35, 40, 45, 63, 76, 18, 41, 47, 40, 30, 76, 46, 34, 31, 76, 30, 31, 29, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76, 15, 32, 76, 46, 34, 27, 46, 76, 29, 41, 38, 41, 45, 45, 27, 38, 76, 23, 44, 31, 29, 37, 64, 76, 28, 41, 47, 40, 30, 38, 31, 45, 45, 76, 27, 40, 30, 76, 28, 27, 44, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 38, 41, 40, 31, 76, 27, 40, 30, 76, 38, 31, 48, 31, 38, 76, 45, 27, 40, 30, 45, 76, 45, 46, 44, 31, 46, 29, 34, 76, 32, 27, 44, 76, 27, 49, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76]] ), torch.tensor([[0, 0, 0, 10_69, 11]] ), torch.tensor([[0, 0, 0, 10_69, 11]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) ) @require_torch def __A ( self ) -> int: '''simple docstring''' import torch lowerCamelCase = JukeboxTokenizer.from_pretrained("""openai/jukebox-5b-lyrics""" ) lowerCamelCase = tokenizer(**self.metas )['input_ids'] # fmt: off lowerCamelCase = [ torch.tensor([[ 0, 0, 0, 10_69, 11, -1, -1, -1, -1, 9, 77, 39, 31, 46, 77, 27, 77, 46, 44, 27, 48, 31, 38, 38, 31, 44, 77, 32, 44, 41, 39, 77, 27, 40, 77, 27, 40, 46, 35, 43, 47, 31, 77, 38, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 41, 77, 45, 27, 35, 30, 77, 72, 20, 49, 41, 77, 48, 27, 45, 46, 77, 27, 40, 30, 77, 46, 44, 47, 40, 37, 38, 31, 45, 45, 77, 38, 31, 33, 45, 77, 41, 32, 77, 45, 46, 41, 40, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 19, 46, 27, 40, 30, 77, 35, 40, 77, 46, 34, 31, 77, 30, 31, 45, 31, 44, 46, 63, 77, 63, 77, 63, 77, 63, 77, 14, 31, 27, 44, 77, 46, 34, 31, 39, 64, 77, 41, 40, 77, 46, 34, 31, 77, 45, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 8, 27, 38, 32, 77, 45, 47, 40, 37, 77, 27, 77, 45, 34, 27, 46, 46, 31, 44, 31, 30, 77, 48, 35, 45, 27, 33, 31, 77, 38, 35, 31, 45, 64, 77, 49, 34, 41, 45, 31, 77, 32, 44, 41, 49, 40, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 49, 44, 35, 40, 37, 38, 31, 30, 77, 38, 35, 42, 64, 77, 27, 40, 30, 77, 45, 40, 31, 31, 44, 77, 41, 32, 77, 29, 41, 38, 30, 77, 29, 41, 39, 39, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 31, 38, 38, 77, 46, 34, 27, 46, 77, 35, 46, 45, 77, 45, 29, 47, 38, 42, 46, 41, 44, 77, 49, 31, 38, 38, 77, 46, 34, 41, 45, 31, 77, 42, 27, 45, 45, 35, 41, 40, 45, 77, 44, 31, 27, 30, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 35, 29, 34, 77, 51, 31, 46, 77, 45, 47, 44, 48, 35, 48, 31, 64, 77, 45, 46, 27, 39, 42, 31, 30, 77, 41, 40, 77, 46, 34, 31, 45, 31, 77, 38, 35, 32, 31, 38, 31, 45, 45, 77, 46, 34, 35, 40, 33, 45, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 34, 27, 40, 30, 77, 46, 34, 27, 46, 77, 39, 41, 29, 37, 31, 30, 77, 46, 34, 31, 39, 64, 77, 27, 40, 30, 77, 46, 34, 31, 77, 34, 31, 27, 44, 46, 77, 46, 34, 27, 46, 77, 32, 31, 30, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 41, 40, 77, 46, 34, 31, 77, 42, 31, 30, 31, 45, 46, 27, 38, 64, 77, 46, 34, 31, 45, 31, 77, 49, 41, 44, 30, 45, 77, 27, 42, 42, 31, 27, 44, 65, 79, 77, 77, 77, 77, 77, 77, 77, 77, 13, 51, 77, 40, 27, 39, 31, 77, 35, 45, 77, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 77, 11, 35, 40, 33, 77, 41, 32, 77, 11, 35, 40, 33, 45, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 12, 41, 41, 37, 77, 41, 40, 77, 39, 51, 77, 23, 41, 44, 37, 45, 64, 77, 51, 31, 77, 13, 35, 33, 34, 46, 51, 64, 77, 27, 40, 30, 77, 30, 31, 45, 42, 27, 35, 44, 67, 79, 77, 77, 77, 77, 77, 77, 77, 77, 14, 41, 46, 34, 35, 40, 33, 77, 28, 31, 45, 35, 30, 31, 77, 44, 31, 39, 27, 35, 40, 45, 63, 77, 18, 41, 47, 40, 30, 77, 46, 34, 31, 77, 30, 31, 29, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77, 15, 32, 77, 46, 34, 27, 46, 77, 29, 41, 38, 41, 45, 45, 27, 38, 77, 23, 44, 31, 29, 37, 64, 77, 28, 41, 47, 40, 30, 38, 31, 45, 45, 77, 27, 40, 30, 77, 28, 27, 44, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 38, 41, 40, 31, 77, 27, 40, 30, 77, 38, 31, 48, 31, 38, 77, 45, 27, 40, 30, 45, 77, 45, 46, 44, 31, 46, 29, 34, 77, 32, 27, 44, 77, 27, 49, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77]] ), torch.tensor([[0, 0, 0, 10_69, 11, -1, -1, -1, -1]] ), torch.tensor([[0, 0, 0, 10_69, 11, -1, -1, -1, -1]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) )
350
from typing import Optional import numpy as np import torch from torch import nn from transformers import GPTaConfig, GPTaLMHeadModel from transformers.modeling_utils import ModuleUtilsMixin from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class __lowercase ( a_ , a_ , a_ ): """simple docstring""" UpperCamelCase : int = [r"h\.\d+\.attn\.bias", r"h\.\d+\.attn\.masked_bias"] @register_to_config def __init__( self , A , A , A = None , A = 5_02_57 , A = 10_24 , A = 7_68 , A = 12 , A = 12 , A = None , A = "gelu_new" , A = 0.1 , A = 0.1 , A = 0.1 , A = 1e-5 , A = 0.02 , A = True , A = True , A = False , A = False , ) -> int: '''simple docstring''' super().__init__() lowerCamelCase = prefix_length if prefix_inner_dim != n_embd and prefix_hidden_dim is None: raise ValueError( F'`prefix_hidden_dim` cannot be `None` when `prefix_inner_dim`: {prefix_hidden_dim} and' F' `n_embd`: {n_embd} are not equal.' ) lowerCamelCase = prefix_inner_dim lowerCamelCase = prefix_hidden_dim lowerCamelCase = ( nn.Linear(self.prefix_inner_dim , self.prefix_hidden_dim ) if self.prefix_hidden_dim is not None else nn.Identity() ) lowerCamelCase = ( nn.Linear(self.prefix_hidden_dim , A ) if self.prefix_hidden_dim is not None else nn.Identity() ) lowerCamelCase = GPTaConfig( vocab_size=A , n_positions=A , n_embd=A , n_layer=A , n_head=A , n_inner=A , activation_function=A , resid_pdrop=A , embd_pdrop=A , attn_pdrop=A , layer_norm_epsilon=A , initializer_range=A , scale_attn_weights=A , use_cache=A , scale_attn_by_inverse_layer_idx=A , reorder_and_upcast_attn=A , ) lowerCamelCase = GPTaLMHeadModel(A ) def __A ( self , A , A , A = None , A = None , ) -> Any: '''simple docstring''' lowerCamelCase = self.transformer.transformer.wte(A ) lowerCamelCase = self.encode_prefix(A ) lowerCamelCase = self.decode_prefix(A ) lowerCamelCase = torch.cat((prefix_embeds, embedding_text) , dim=1 ) if labels is not None: lowerCamelCase = self.get_dummy_token(input_ids.shape[0] , input_ids.device ) lowerCamelCase = torch.cat((dummy_token, input_ids) , dim=1 ) lowerCamelCase = self.transformer(inputs_embeds=A , labels=A , attention_mask=A ) if self.prefix_hidden_dim is not None: return out, hidden else: return out def __A ( self , A , A ) -> torch.Tensor: '''simple docstring''' return torch.zeros(A , self.prefix_length , dtype=torch.intaa , device=A ) def __A ( self , A ) -> int: '''simple docstring''' return self.encode_prefix(A ) @torch.no_grad() def __A ( self , A , A , A ) -> Union[str, Any]: '''simple docstring''' lowerCamelCase = torch.split(A , 1 , dim=0 ) lowerCamelCase = [] lowerCamelCase = [] for feature in features: lowerCamelCase = self.decode_prefix(feature.to(A ) ) # back to the clip feature # Only support beam search for now lowerCamelCase , lowerCamelCase = self.generate_beam( input_embeds=A , device=A , eos_token_id=A ) generated_tokens.append(output_tokens[0] ) generated_seq_lengths.append(seq_lengths[0] ) lowerCamelCase = torch.stack(A ) lowerCamelCase = torch.stack(A ) return generated_tokens, generated_seq_lengths @torch.no_grad() def __A ( self , A=None , A=None , A=None , A = 5 , A = 67 , A = 1.0 , A = None , ) -> Any: '''simple docstring''' lowerCamelCase = eos_token_id lowerCamelCase = None lowerCamelCase = None lowerCamelCase = torch.ones(A , device=A , dtype=torch.int ) lowerCamelCase = torch.zeros(A , device=A , dtype=torch.bool ) if input_embeds is not None: lowerCamelCase = input_embeds else: lowerCamelCase = self.transformer.transformer.wte(A ) for i in range(A ): lowerCamelCase = self.transformer(inputs_embeds=A ) lowerCamelCase = outputs.logits lowerCamelCase = logits[:, -1, :] / (temperature if temperature > 0 else 1.0) lowerCamelCase = logits.softmax(-1 ).log() if scores is None: lowerCamelCase , lowerCamelCase = logits.topk(A , -1 ) lowerCamelCase = generated.expand(A , *generated.shape[1:] ) lowerCamelCase , lowerCamelCase = next_tokens.permute(1 , 0 ), scores.squeeze(0 ) if tokens is None: lowerCamelCase = next_tokens else: lowerCamelCase = tokens.expand(A , *tokens.shape[1:] ) lowerCamelCase = torch.cat((tokens, next_tokens) , dim=1 ) else: lowerCamelCase = -float(np.inf ) lowerCamelCase = 0 lowerCamelCase = scores[:, None] + logits seq_lengths[~is_stopped] += 1 lowerCamelCase = scores_sum / seq_lengths[:, None] lowerCamelCase , lowerCamelCase = scores_sum_average.view(-1 ).topk(A , -1 ) lowerCamelCase = next_tokens // scores_sum.shape[1] lowerCamelCase = seq_lengths[next_tokens_source] lowerCamelCase = next_tokens % scores_sum.shape[1] lowerCamelCase = next_tokens.unsqueeze(1 ) lowerCamelCase = tokens[next_tokens_source] lowerCamelCase = torch.cat((tokens, next_tokens) , dim=1 ) lowerCamelCase = generated[next_tokens_source] lowerCamelCase = scores_sum_average * seq_lengths lowerCamelCase = is_stopped[next_tokens_source] lowerCamelCase = self.transformer.transformer.wte(next_tokens.squeeze() ).view(generated.shape[0] , 1 , -1 ) lowerCamelCase = torch.cat((generated, next_token_embed) , dim=1 ) lowerCamelCase = is_stopped + next_tokens.eq(A ).squeeze() if is_stopped.all(): break lowerCamelCase = scores / seq_lengths lowerCamelCase = scores.argsort(descending=A ) # tokens tensors are already padded to max_seq_length lowerCamelCase = [tokens[i] for i in order] lowerCamelCase = torch.stack(A , dim=0 ) lowerCamelCase = torch.tensor([seq_lengths[i] for i in order] , dtype=seq_lengths.dtype ) return output_texts, seq_lengths
66
0
# This code is adapted from OpenAI's release # https://github.com/openai/human-eval/blob/master/human_eval/execution.py import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def __lowercase ( a__ , a__ , a__ , a__ ) -> str: __SCREAMING_SNAKE_CASE = multiprocessing.Manager() __SCREAMING_SNAKE_CASE = manager.list() __SCREAMING_SNAKE_CASE = multiprocessing.Process(target=a__ , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append('timed out' ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def __lowercase ( a__ , a__ , a__ ) -> List[Any]: with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil __SCREAMING_SNAKE_CASE = shutil.rmtree __SCREAMING_SNAKE_CASE = os.rmdir __SCREAMING_SNAKE_CASE = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: __SCREAMING_SNAKE_CASE = {} with swallow_io(): with time_limit(a__ ): exec(a__ , a__ ) result.append('passed' ) except TimeoutException: result.append('timed out' ) except BaseException as e: result.append(f"""failed: {e}""" ) # Needed for cleaning up. __SCREAMING_SNAKE_CASE = rmtree __SCREAMING_SNAKE_CASE = rmdir __SCREAMING_SNAKE_CASE = chdir @contextlib.contextmanager def __lowercase ( a__ ) -> Optional[Any]: def signal_handler(a__ , a__ ): raise TimeoutException('Timed out!' ) signal.setitimer(signal.ITIMER_REAL , a__ ) signal.signal(signal.SIGALRM , a__ ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def __lowercase ( ) -> Tuple: __SCREAMING_SNAKE_CASE = WriteOnlyStringIO() with contextlib.redirect_stdout(a__ ): with contextlib.redirect_stderr(a__ ): with redirect_stdin(a__ ): yield @contextlib.contextmanager def __lowercase ( ) -> List[str]: with tempfile.TemporaryDirectory() as dirname: with chdir(a__ ): yield dirname class UpperCAmelCase_ ( UpperCamelCase_ ): '''simple docstring''' pass class UpperCAmelCase_ ( io.StringIO ): '''simple docstring''' def _A ( self , *_A , **_A ): '''simple docstring''' raise OSError def _A ( self , *_A , **_A ): '''simple docstring''' raise OSError def _A ( self , *_A , **_A ): '''simple docstring''' raise OSError def _A ( self , *_A , **_A ): '''simple docstring''' return False class UpperCAmelCase_ ( contextlib._RedirectStream ): # type: ignore '''simple docstring''' UpperCamelCase__ : Union[str, Any] = '''stdin''' @contextlib.contextmanager def __lowercase ( a__ ) -> Dict: if root == ".": yield return __SCREAMING_SNAKE_CASE = os.getcwd() os.chdir(a__ ) try: yield except BaseException as exc: raise exc finally: os.chdir(a__ ) def __lowercase ( a__=None ) -> Union[str, Any]: if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None import os __SCREAMING_SNAKE_CASE = '1' __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None import shutil __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None import subprocess __SCREAMING_SNAKE_CASE = None # type: ignore __SCREAMING_SNAKE_CASE = None import sys __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None
257
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": lowerCAmelCase__ : List[Any] =input('''Enter image url: ''').strip() print(F'''Downloading image from {url} ...''') lowerCAmelCase__ : int =BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image lowerCAmelCase__ : Union[str, Any] =soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] lowerCAmelCase__ : int =requests.get(image_url).content lowerCAmelCase__ : Optional[int] =F'''{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg''' with open(file_name, '''wb''') as fp: fp.write(image_data) print(F'''Done. Image saved to disk as {file_name}.''')
257
1
'''simple docstring''' from ..utils import DummyObject, requires_backends class UpperCamelCase_ (metaclass=a__ ): """simple docstring""" _lowerCAmelCase = ['keras_nlp'] def __init__( self : Optional[Any] , *_lowerCamelCase : Dict , **_lowerCamelCase : List[str] ): """simple docstring""" requires_backends(self , ['''keras_nlp'''] )
361
'''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. import numpy as np import torch from ..models.clipseg import CLIPSegForImageSegmentation from ..utils import is_vision_available, requires_backends from .base import PipelineTool if is_vision_available(): from PIL import Image class UpperCamelCase_ (a__ ): """simple docstring""" _lowerCAmelCase = ( 'This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image.' 'It takes two arguments named `image` which should be the original image, and `label` which should be a text ' 'describing the elements what should be identified in the segmentation mask. The tool returns the mask.' ) _lowerCAmelCase = 'CIDAS/clipseg-rd64-refined' _lowerCAmelCase = 'image_segmenter' _lowerCAmelCase = CLIPSegForImageSegmentation _lowerCAmelCase = ['image', 'text'] _lowerCAmelCase = ['image'] def __init__( self : Optional[int] , *_lowerCamelCase : Optional[int] , **_lowerCamelCase : Union[str, Any] ): """simple docstring""" requires_backends(self , ['''vision'''] ) super().__init__(*_lowerCamelCase , **_lowerCamelCase ) def _a ( self : List[str] , _lowerCamelCase : "Image" , _lowerCamelCase : str ): """simple docstring""" return self.pre_processor(text=[label] , images=[image] , padding=_lowerCamelCase , return_tensors='''pt''' ) def _a ( self : Union[str, Any] , _lowerCamelCase : Optional[int] ): """simple docstring""" with torch.no_grad(): A_ : Optional[int] = self.model(**_lowerCamelCase ).logits return logits def _a ( self : List[str] , _lowerCamelCase : Optional[int] ): """simple docstring""" A_ : int = outputs.cpu().detach().numpy() A_ : Tuple = 0 A_ : List[str] = 1 return Image.fromarray((array * 255).astype(np.uinta ) )
4
0
'''simple docstring''' import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets lowercase__ : Union[str, Any] = '''\ @inproceedings{snover-etal-2006-study, title = "A Study of Translation Edit Rate with Targeted Human Annotation", author = "Snover, Matthew and Dorr, Bonnie and Schwartz, Rich and Micciulla, Linnea and Makhoul, John", booktitle = "Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers", month = aug # " 8-12", year = "2006", address = "Cambridge, Massachusetts, USA", publisher = "Association for Machine Translation in the Americas", url = "https://aclanthology.org/2006.amta-papers.25", pages = "223--231", } @inproceedings{post-2018-call, title = "A Call for Clarity in Reporting {BLEU} Scores", author = "Post, Matt", booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers", month = oct, year = "2018", address = "Belgium, Brussels", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/W18-6319", pages = "186--191", } ''' lowercase__ : Optional[Any] = '''\ TER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a hypothesis requires to match a reference translation. We use the implementation that is already present in sacrebleu (https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the TERCOM implementation, which can be found here: https://github.com/jhclark/tercom. The implementation here is slightly different from sacrebleu in terms of the required input format. The length of the references and hypotheses lists need to be the same, so you may need to transpose your references compared to sacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534 See the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information. ''' lowercase__ : Optional[Any] = ''' Produces TER scores alongside the number of edits and reference length. Args: predictions (list of str): The system stream (a sequence of segments). references (list of list of str): A list of one or more reference streams (each a sequence of segments). normalized (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. ignore_punct (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. support_zh_ja_chars (boolean): If `True`, tokenization/normalization supports processing of Chinese characters, as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana. Only applies if `normalized = True`. Defaults to `False`. case_sensitive (boolean): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`. Returns: \'score\' (float): TER score (num_edits / sum_ref_lengths * 100) \'num_edits\' (int): The cumulative number of edits \'ref_length\' (float): The cumulative average reference length Examples: Example 1: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 150.0, \'num_edits\': 15, \'ref_length\': 10.0} Example 2: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 62.5, \'num_edits\': 5, \'ref_length\': 8.0} Example 3: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... normalized=True, ... case_sensitive=True) >>> print(results) {\'score\': 57.14285714285714, \'num_edits\': 6, \'ref_length\': 10.5} Example 4: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 0.0, \'num_edits\': 0, \'ref_length\': 8.0} Example 5: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 100.0, \'num_edits\': 10, \'ref_length\': 10.0} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE (datasets.Metric ): def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' if version.parse(scb.__version__) < version.parse('1.4.12'): raise ImportWarning( 'To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn\'t match this condition.\n' 'You can install it with `pip install "sacrebleu>=1.4.12"`.') return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='http://www.cs.umd.edu/~snover/tercom/' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' , id='sequence'), 'references': datasets.Sequence(datasets.Value('string' , id='sequence') , id='references'), }) , codebase_urls=['https://github.com/mjpost/sacreBLEU#ter'] , reference_urls=[ 'https://github.com/jhclark/tercom', ] , ) def SCREAMING_SNAKE_CASE ( self , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = False , _UpperCAmelCase = False , _UpperCAmelCase = False , _UpperCAmelCase = False , ): '''simple docstring''' __A : Union[str, Any] = len(references[0]) if any(len(_UpperCAmelCase) != references_per_prediction for refs in references): raise ValueError('Sacrebleu requires the same number of references for each prediction') __A : Optional[int] = [[refs[i] for refs in references] for i in range(_UpperCAmelCase)] __A : Any = TER( normalized=_UpperCAmelCase , no_punct=_UpperCAmelCase , asian_support=_UpperCAmelCase , case_sensitive=_UpperCAmelCase , ) __A : Tuple = sb_ter.corpus_score(_UpperCAmelCase , _UpperCAmelCase) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
190
'''simple docstring''' lowercase__ : Dict = { '''Pillow''': '''Pillow''', '''accelerate''': '''accelerate>=0.11.0''', '''compel''': '''compel==0.1.8''', '''black''': '''black~=23.1''', '''datasets''': '''datasets''', '''filelock''': '''filelock''', '''flax''': '''flax>=0.4.1''', '''hf-doc-builder''': '''hf-doc-builder>=0.3.0''', '''huggingface-hub''': '''huggingface-hub>=0.13.2''', '''requests-mock''': '''requests-mock==1.10.0''', '''importlib_metadata''': '''importlib_metadata''', '''invisible-watermark''': '''invisible-watermark''', '''isort''': '''isort>=5.5.4''', '''jax''': '''jax>=0.2.8,!=0.3.2''', '''jaxlib''': '''jaxlib>=0.1.65''', '''Jinja2''': '''Jinja2''', '''k-diffusion''': '''k-diffusion>=0.0.12''', '''torchsde''': '''torchsde''', '''note_seq''': '''note_seq''', '''librosa''': '''librosa''', '''numpy''': '''numpy''', '''omegaconf''': '''omegaconf''', '''parameterized''': '''parameterized''', '''protobuf''': '''protobuf>=3.20.3,<4''', '''pytest''': '''pytest''', '''pytest-timeout''': '''pytest-timeout''', '''pytest-xdist''': '''pytest-xdist''', '''ruff''': '''ruff>=0.0.241''', '''safetensors''': '''safetensors''', '''sentencepiece''': '''sentencepiece>=0.1.91,!=0.1.92''', '''scipy''': '''scipy''', '''onnx''': '''onnx''', '''regex''': '''regex!=2019.12.17''', '''requests''': '''requests''', '''tensorboard''': '''tensorboard''', '''torch''': '''torch>=1.4''', '''torchvision''': '''torchvision''', '''transformers''': '''transformers>=4.25.1''', '''urllib3''': '''urllib3<=2.0.0''', }
190
1
"""simple docstring""" import os import sys import unittest lowerCamelCase_ = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_dummies # noqa: E402 from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init # noqa: E402 # Align TRANSFORMERS_PATH in check_dummies with the current path lowerCamelCase_ = os.path.join(git_repo_path, "src", "diffusers") class _SCREAMING_SNAKE_CASE( unittest.TestCase ): def _UpperCamelCase ( self ) -> List[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE :int = find_backend(''' if not is_torch_available():''' ) self.assertEqual(SCREAMING_SNAKE_CASE__ ,'''torch''' ) # backend_with_underscore = find_backend(" if not is_tensorflow_text_available():") # self.assertEqual(backend_with_underscore, "tensorflow_text") __SCREAMING_SNAKE_CASE :List[Any] = find_backend(''' if not (is_torch_available() and is_transformers_available()):''' ) self.assertEqual(SCREAMING_SNAKE_CASE__ ,'''torch_and_transformers''' ) # double_backend_with_underscore = find_backend( # " if not (is_sentencepiece_available() and is_tensorflow_text_available()):" # ) # self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text") __SCREAMING_SNAKE_CASE :Any = find_backend( ''' if not (is_torch_available() and is_transformers_available() and is_onnx_available()):''' ) self.assertEqual(SCREAMING_SNAKE_CASE__ ,'''torch_and_transformers_and_onnx''' ) def _UpperCamelCase ( self ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE :Any = read_init() # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects self.assertIn('''torch''' ,SCREAMING_SNAKE_CASE__ ) self.assertIn('''torch_and_transformers''' ,SCREAMING_SNAKE_CASE__ ) self.assertIn('''flax_and_transformers''' ,SCREAMING_SNAKE_CASE__ ) self.assertIn('''torch_and_transformers_and_onnx''' ,SCREAMING_SNAKE_CASE__ ) # Likewise, we can't assert on the exact content of a key self.assertIn('''UNet2DModel''' ,objects['''torch'''] ) self.assertIn('''FlaxUNet2DConditionModel''' ,objects['''flax'''] ) self.assertIn('''StableDiffusionPipeline''' ,objects['''torch_and_transformers'''] ) self.assertIn('''FlaxStableDiffusionPipeline''' ,objects['''flax_and_transformers'''] ) self.assertIn('''LMSDiscreteScheduler''' ,objects['''torch_and_scipy'''] ) self.assertIn('''OnnxStableDiffusionPipeline''' ,objects['''torch_and_transformers_and_onnx'''] ) def _UpperCamelCase ( self ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE :str = create_dummy_object('''CONSTANT''' ,'''\'torch\'''' ) self.assertEqual(SCREAMING_SNAKE_CASE__ ,'''\nCONSTANT = None\n''' ) __SCREAMING_SNAKE_CASE :Optional[Any] = create_dummy_object('''function''' ,'''\'torch\'''' ) self.assertEqual( SCREAMING_SNAKE_CASE__ ,'''\ndef function(*args, **kwargs):\n requires_backends(function, \'torch\')\n''' ) __SCREAMING_SNAKE_CASE :List[Any] = ''' class FakeClass(metaclass=DummyObject): _backends = \'torch\' def __init__(self, *args, **kwargs): requires_backends(self, \'torch\') @classmethod def from_config(cls, *args, **kwargs): requires_backends(cls, \'torch\') @classmethod def from_pretrained(cls, *args, **kwargs): requires_backends(cls, \'torch\') ''' __SCREAMING_SNAKE_CASE :str = create_dummy_object('''FakeClass''' ,'''\'torch\'''' ) self.assertEqual(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE :List[Any] = '''# This file is autogenerated by the command `make fix-copies`, do not edit. from ..utils import DummyObject, requires_backends CONSTANT = None def function(*args, **kwargs): requires_backends(function, ["torch"]) class FakeClass(metaclass=DummyObject): _backends = ["torch"] def __init__(self, *args, **kwargs): requires_backends(self, ["torch"]) @classmethod def from_config(cls, *args, **kwargs): requires_backends(cls, ["torch"]) @classmethod def from_pretrained(cls, *args, **kwargs): requires_backends(cls, ["torch"]) ''' __SCREAMING_SNAKE_CASE :Optional[int] = create_dummy_files({'''torch''': ['''CONSTANT''', '''function''', '''FakeClass''']} ) self.assertEqual(dummy_files['''torch'''] ,SCREAMING_SNAKE_CASE__ )
239
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase_ = logging.get_logger(__name__) lowerCamelCase_ = { "google/vivit-b-16x2-kinetics400": ( "https://huggingface.co/google/vivit-b-16x2-kinetics400/resolve/main/config.json" ), # See all Vivit models at https://huggingface.co/models?filter=vivit } class _SCREAMING_SNAKE_CASE( A ): SCREAMING_SNAKE_CASE_ : Optional[Any] = '''vivit''' def __init__( self ,SCREAMING_SNAKE_CASE__=2_24 ,SCREAMING_SNAKE_CASE__=32 ,SCREAMING_SNAKE_CASE__=[2, 16, 16] ,SCREAMING_SNAKE_CASE__=3 ,SCREAMING_SNAKE_CASE__=7_68 ,SCREAMING_SNAKE_CASE__=12 ,SCREAMING_SNAKE_CASE__=12 ,SCREAMING_SNAKE_CASE__=30_72 ,SCREAMING_SNAKE_CASE__="gelu_fast" ,SCREAMING_SNAKE_CASE__=0.0 ,SCREAMING_SNAKE_CASE__=0.0 ,SCREAMING_SNAKE_CASE__=0.0_2 ,SCREAMING_SNAKE_CASE__=1E-06 ,SCREAMING_SNAKE_CASE__=True ,**SCREAMING_SNAKE_CASE__ ,) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE :Optional[Any] = hidden_size __SCREAMING_SNAKE_CASE :List[str] = num_hidden_layers __SCREAMING_SNAKE_CASE :List[str] = num_attention_heads __SCREAMING_SNAKE_CASE :Any = intermediate_size __SCREAMING_SNAKE_CASE :Optional[Any] = hidden_act __SCREAMING_SNAKE_CASE :int = hidden_dropout_prob __SCREAMING_SNAKE_CASE :Union[str, Any] = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE :Dict = initializer_range __SCREAMING_SNAKE_CASE :Dict = layer_norm_eps __SCREAMING_SNAKE_CASE :Any = image_size __SCREAMING_SNAKE_CASE :Any = num_frames __SCREAMING_SNAKE_CASE :Any = tubelet_size __SCREAMING_SNAKE_CASE :Tuple = num_channels __SCREAMING_SNAKE_CASE :List[str] = qkv_bias super().__init__(**SCREAMING_SNAKE_CASE__ )
239
1
import torch from diffusers import DiffusionPipeline class lowercase ( __UpperCAmelCase ): def __init__( self ,A__ ,A__): super().__init__() self.register_modules(unet=lowerCamelCase__ ,scheduler=lowerCamelCase__) def __call__( self): lowercase = torch.randn( (1, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size) ,) lowercase = 1 lowercase = self.unet(lowerCamelCase__ ,lowerCamelCase__).sample lowercase = self.scheduler.step(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__).prev_sample lowercase = scheduler_output - scheduler_output + torch.ones_like(lowerCamelCase__) return result
101
'''simple docstring''' import warnings from contextlib import contextmanager from ....processing_utils import ProcessorMixin class A ( __UpperCAmelCase ): lowerCamelCase : Union[str, Any] = """MCTCTFeatureExtractor""" lowerCamelCase : Dict = """AutoTokenizer""" def __init__( self , lowerCamelCase__ , lowerCamelCase__ ) -> Any: '''simple docstring''' super().__init__(lowerCamelCase__ , lowerCamelCase__ ) lowercase__ = self.feature_extractor lowercase__ = False def __call__( self , *lowerCamelCase__ , **lowerCamelCase__ ) -> Union[str, Any]: '''simple docstring''' if self._in_target_context_manager: return self.current_processor(*lowerCamelCase__ , **lowerCamelCase__ ) if "raw_speech" in kwargs: warnings.warn("""Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.""" ) lowercase__ = kwargs.pop("""raw_speech""" ) else: lowercase__ = kwargs.pop("""audio""" , lowerCamelCase__ ) lowercase__ = kwargs.pop("""sampling_rate""" , lowerCamelCase__ ) lowercase__ = kwargs.pop("""text""" , lowerCamelCase__ ) if len(lowerCamelCase__ ) > 0: lowercase__ = args[0] lowercase__ = 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: lowercase__ = self.feature_extractor(lowerCamelCase__ , *lowerCamelCase__ , sampling_rate=lowerCamelCase__ , **lowerCamelCase__ ) if text is not None: lowercase__ = self.tokenizer(lowerCamelCase__ , **lowerCamelCase__ ) if text is None: return inputs elif audio is None: return encodings else: lowercase__ = encodings["""input_ids"""] return inputs def A__ ( self , *lowerCamelCase__ , **lowerCamelCase__ ) -> int: '''simple docstring''' return self.tokenizer.batch_decode(*lowerCamelCase__ , **lowerCamelCase__ ) def A__ ( self , *lowerCamelCase__ , **lowerCamelCase__ ) -> Dict: '''simple docstring''' if self._in_target_context_manager: return self.current_processor.pad(*lowerCamelCase__ , **lowerCamelCase__ ) lowercase__ = kwargs.pop("""input_features""" , lowerCamelCase__ ) lowercase__ = kwargs.pop("""labels""" , lowerCamelCase__ ) if len(lowerCamelCase__ ) > 0: lowercase__ = args[0] lowercase__ = args[1:] if input_features is not None: lowercase__ = self.feature_extractor.pad(lowerCamelCase__ , *lowerCamelCase__ , **lowerCamelCase__ ) if labels is not None: lowercase__ = self.tokenizer.pad(lowerCamelCase__ , **lowerCamelCase__ ) if labels is None: return input_features elif input_features is None: return labels else: lowercase__ = labels["""input_ids"""] return input_features def A__ ( self , *lowerCamelCase__ , **lowerCamelCase__ ) -> Union[str, Any]: '''simple docstring''' return self.tokenizer.decode(*lowerCamelCase__ , **lowerCamelCase__ ) @contextmanager def A__ ( self ) -> Any: '''simple docstring''' 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.""" ) lowercase__ = True lowercase__ = self.tokenizer yield lowercase__ = self.feature_extractor lowercase__ = False
164
0
def lowerCAmelCase_ ( UpperCamelCase_ = 1000 ) -> int: UpperCamelCase_ = 3 UpperCamelCase_ = 0 while a < n: if a % 3 == 0 or a % 5 == 0: result += a elif a % 15 == 0: result -= a a += 1 return result if __name__ == "__main__": print(f'''{solution() = }''')
328
import requests from bsa import BeautifulSoup def lowerCAmelCase_ ( UpperCamelCase_ , UpperCamelCase_ ) -> str: UpperCamelCase_ = BeautifulSoup(requests.get(UpperCamelCase_ , params=UpperCamelCase_ ).content , "html.parser" ) UpperCamelCase_ = soup.find("div" , attrs={"class": "gs_ri"} ) UpperCamelCase_ = div.find("div" , attrs={"class": "gs_fl"} ).find_all("a" ) return anchors[2].get_text() if __name__ == "__main__": _UpperCAmelCase = { 'title': ( 'Precisely geometry controlled microsupercapacitors for ultrahigh areal ' 'capacitance, volumetric capacitance, and energy density' ), 'journal': 'Chem. Mater.', 'volume': 3_0, 'pages': '3979-3990', 'year': 2_0_1_8, 'hl': 'en', } print(get_citation('https://scholar.google.com/scholar_lookup', params=params))
328
1
'''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, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer snake_case_ : Optional[Any] = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast snake_case_ : int = TaTokenizerFast snake_case_ : List[Any] = {'configuration_mt5': ['MT5Config', 'MT5OnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case_ : str = [ 'MT5EncoderModel', 'MT5ForConditionalGeneration', 'MT5ForQuestionAnswering', 'MT5Model', 'MT5PreTrainedModel', 'MT5Stack', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case_ : int = ['TFMT5EncoderModel', 'TFMT5ForConditionalGeneration', 'TFMT5Model'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case_ : Dict = ['FlaxMT5EncoderModel', 'FlaxMT5ForConditionalGeneration', 'FlaxMT5Model'] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys snake_case_ : List[Any] = _LazyModule( __name__, globals()['__file__'], _import_structure, extra_objects={'MT5Tokenizer': MTaTokenizer, 'MT5TokenizerFast': MTaTokenizerFast}, module_spec=__spec__, )
83
'''simple docstring''' import argparse import collections import json import os import re import string import sys import numpy as np a__ : Optional[int] = re.compile(R'\b(a|an|the)\b', re.UNICODE) a__ : int = None def _UpperCamelCase ( ) -> Dict: '''simple docstring''' UpperCamelCase__ = argparse.ArgumentParser("Official evaluation script for SQuAD version 2.0." ) parser.add_argument("data_file" , metavar="data.json" , help="Input data JSON file." ) parser.add_argument("pred_file" , metavar="pred.json" , help="Model predictions." ) parser.add_argument( "--out-file" , "-o" , metavar="eval.json" , help="Write accuracy metrics to file (default is stdout)." ) parser.add_argument( "--na-prob-file" , "-n" , metavar="na_prob.json" , help="Model estimates of probability of no answer." ) parser.add_argument( "--na-prob-thresh" , "-t" , type=__A , default=1.0 , help="Predict \"\" if no-answer probability exceeds this (default = 1.0)." , ) parser.add_argument( "--out-image-dir" , "-p" , metavar="out_images" , default=__A , help="Save precision-recall curves to directory." ) parser.add_argument("--verbose" , "-v" , action="store_true" ) if len(sys.argv ) == 1: parser.print_help() sys.exit(1 ) return parser.parse_args() def _UpperCamelCase ( __A ) -> Dict: '''simple docstring''' UpperCamelCase__ = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: UpperCamelCase__ = bool(qa["answers"]["text"] ) return qid_to_has_ans def _UpperCamelCase ( __A ) -> Optional[Any]: '''simple docstring''' def remove_articles(__A ): return ARTICLES_REGEX.sub(" " , __A ) def white_space_fix(__A ): return " ".join(text.split() ) def remove_punc(__A ): UpperCamelCase__ = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__A ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__A ) ) ) ) def _UpperCamelCase ( __A ) -> int: '''simple docstring''' if not s: return [] return normalize_answer(__A ).split() def _UpperCamelCase ( __A , __A ) -> List[Any]: '''simple docstring''' return int(normalize_answer(__A ) == normalize_answer(__A ) ) def _UpperCamelCase ( __A , __A ) -> Optional[int]: '''simple docstring''' UpperCamelCase__ = get_tokens(__A ) UpperCamelCase__ = get_tokens(__A ) UpperCamelCase__ = collections.Counter(__A ) & collections.Counter(__A ) UpperCamelCase__ = sum(common.values() ) if len(__A ) == 0 or len(__A ) == 0: # If either is no-answer, then F1 is 1 if they agree, 0 otherwise return int(gold_toks == pred_toks ) if num_same == 0: return 0 UpperCamelCase__ = 1.0 * num_same / len(__A ) UpperCamelCase__ = 1.0 * num_same / len(__A ) UpperCamelCase__ = (2 * precision * recall) / (precision + recall) return fa def _UpperCamelCase ( __A , __A ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ = {} UpperCamelCase__ = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: UpperCamelCase__ = qa["id"] UpperCamelCase__ = [t for t in qa["answers"]["text"] if normalize_answer(__A )] if not gold_answers: # For unanswerable questions, only correct answer is empty string UpperCamelCase__ = [""] if qid not in preds: print(F'''Missing prediction for {qid}''' ) continue UpperCamelCase__ = preds[qid] # Take max over all gold answers UpperCamelCase__ = max(compute_exact(__A , __A ) for a in gold_answers ) UpperCamelCase__ = max(compute_fa(__A , __A ) for a in gold_answers ) return exact_scores, fa_scores def _UpperCamelCase ( __A , __A , __A , __A ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ = {} for qid, s in scores.items(): UpperCamelCase__ = na_probs[qid] > na_prob_thresh if pred_na: UpperCamelCase__ = float(not qid_to_has_ans[qid] ) else: UpperCamelCase__ = s return new_scores def _UpperCamelCase ( __A , __A , __A=None ) -> List[Any]: '''simple docstring''' if not qid_list: UpperCamelCase__ = len(__A ) return collections.OrderedDict( [ ("exact", 100.0 * sum(exact_scores.values() ) / total), ("f1", 100.0 * sum(fa_scores.values() ) / total), ("total", total), ] ) else: UpperCamelCase__ = len(__A ) return collections.OrderedDict( [ ("exact", 100.0 * sum(exact_scores[k] for k in qid_list ) / total), ("f1", 100.0 * sum(fa_scores[k] for k in qid_list ) / total), ("total", total), ] ) def _UpperCamelCase ( __A , __A , __A ) -> Optional[int]: '''simple docstring''' for k in new_eval: UpperCamelCase__ = new_eval[k] def _UpperCamelCase ( __A , __A , __A , __A ) -> Optional[int]: '''simple docstring''' plt.step(__A , __A , color="b" , alpha=0.2 , where="post" ) plt.fill_between(__A , __A , step="post" , alpha=0.2 , color="b" ) plt.xlabel("Recall" ) plt.ylabel("Precision" ) plt.xlim([0.0, 1.05] ) plt.ylim([0.0, 1.05] ) plt.title(__A ) plt.savefig(__A ) plt.clf() def _UpperCamelCase ( __A , __A , __A , __A , __A=None , __A=None ) -> Any: '''simple docstring''' UpperCamelCase__ = sorted(__A , key=lambda __A : na_probs[k] ) UpperCamelCase__ = 0.0 UpperCamelCase__ = 1.0 UpperCamelCase__ = 0.0 UpperCamelCase__ = [1.0] UpperCamelCase__ = [0.0] UpperCamelCase__ = 0.0 for i, qid in enumerate(__A ): if qid_to_has_ans[qid]: true_pos += scores[qid] UpperCamelCase__ = true_pos / float(i + 1 ) UpperCamelCase__ = true_pos / float(__A ) if i == len(__A ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]: # i.e., if we can put a threshold after this point avg_prec += cur_p * (cur_r - recalls[-1]) precisions.append(__A ) recalls.append(__A ) if out_image: plot_pr_curve(__A , __A , __A , __A ) return {"ap": 100.0 * avg_prec} def _UpperCamelCase ( __A , __A , __A , __A , __A , __A ) -> List[str]: '''simple docstring''' if out_image_dir and not os.path.exists(__A ): os.makedirs(__A ) UpperCamelCase__ = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return UpperCamelCase__ = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , "pr_exact.png" ) , title="Precision-Recall curve for Exact Match score" , ) UpperCamelCase__ = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , "pr_f1.png" ) , title="Precision-Recall curve for F1 score" , ) UpperCamelCase__ = {k: float(__A ) for k, v in qid_to_has_ans.items()} UpperCamelCase__ = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , "pr_oracle.png" ) , title="Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)" , ) merge_eval(__A , __A , "pr_exact" ) merge_eval(__A , __A , "pr_f1" ) merge_eval(__A , __A , "pr_oracle" ) def _UpperCamelCase ( __A , __A , __A , __A ) -> List[str]: '''simple docstring''' if not qid_list: return UpperCamelCase__ = [na_probs[k] for k in qid_list] UpperCamelCase__ = np.ones_like(__A ) / float(len(__A ) ) plt.hist(__A , weights=__A , bins=20 , range=(0.0, 1.0) ) plt.xlabel("Model probability of no-answer" ) plt.ylabel("Proportion of dataset" ) plt.title(F'''Histogram of no-answer probability: {name}''' ) plt.savefig(os.path.join(__A , F'''na_prob_hist_{name}.png''' ) ) plt.clf() def _UpperCamelCase ( __A , __A , __A , __A ) -> Tuple: '''simple docstring''' UpperCamelCase__ = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) UpperCamelCase__ = num_no_ans UpperCamelCase__ = cur_score UpperCamelCase__ = 0.0 UpperCamelCase__ = sorted(__A , key=lambda __A : na_probs[k] ) for i, qid in enumerate(__A ): if qid not in scores: continue if qid_to_has_ans[qid]: UpperCamelCase__ = scores[qid] else: if preds[qid]: UpperCamelCase__ = -1 else: UpperCamelCase__ = 0 cur_score += diff if cur_score > best_score: UpperCamelCase__ = cur_score UpperCamelCase__ = na_probs[qid] return 100.0 * best_score / len(__A ), best_thresh def _UpperCamelCase ( __A , __A , __A , __A , __A , __A ) -> Dict: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ = find_best_thresh(__A , __A , __A , __A ) UpperCamelCase__ , UpperCamelCase__ = find_best_thresh(__A , __A , __A , __A ) UpperCamelCase__ = best_exact UpperCamelCase__ = exact_thresh UpperCamelCase__ = best_fa UpperCamelCase__ = fa_thresh def _UpperCamelCase ( ) -> Any: '''simple docstring''' with open(OPTS.data_file ) as f: UpperCamelCase__ = json.load(__A ) UpperCamelCase__ = dataset_json["data"] with open(OPTS.pred_file ) as f: UpperCamelCase__ = json.load(__A ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: UpperCamelCase__ = json.load(__A ) else: UpperCamelCase__ = {k: 0.0 for k in preds} UpperCamelCase__ = make_qid_to_has_ans(__A ) # maps qid to True/False UpperCamelCase__ = [k for k, v in qid_to_has_ans.items() if v] UpperCamelCase__ = [k for k, v in qid_to_has_ans.items() if not v] UpperCamelCase__ , UpperCamelCase__ = get_raw_scores(__A , __A ) UpperCamelCase__ = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) UpperCamelCase__ = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) UpperCamelCase__ = make_eval_dict(__A , __A ) if has_ans_qids: UpperCamelCase__ = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , "HasAns" ) if no_ans_qids: UpperCamelCase__ = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , "NoAns" ) if OPTS.na_prob_file: find_all_best_thresh(__A , __A , __A , __A , __A , __A ) if OPTS.na_prob_file and OPTS.out_image_dir: run_precision_recall_analysis(__A , __A , __A , __A , __A , OPTS.out_image_dir ) histogram_na_prob(__A , __A , OPTS.out_image_dir , "hasAns" ) histogram_na_prob(__A , __A , OPTS.out_image_dir , "noAns" ) if OPTS.out_file: with open(OPTS.out_file , "w" ) as f: json.dump(__A , __A ) else: print(json.dumps(__A , indent=2 ) ) if __name__ == "__main__": a__ : Optional[int] = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt main()
80
0
"""simple docstring""" import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 UpperCamelCase : List[str] = get_tests_dir("fixtures") class __lowerCAmelCase ( unittest.TestCase ): def UpperCAmelCase ( self ): '''simple docstring''' __UpperCamelCase = mock.Mock() __UpperCamelCase = 500 __UpperCamelCase = {} __UpperCamelCase = HTTPError __UpperCamelCase = {} # Download this model to make sure it's in the cache. __UpperCamelCase = WavaVecaFeatureExtractor.from_pretrained('hf-internal-testing/tiny-random-wav2vec2' ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('requests.Session.request' , return_value=__UpperCAmelCase ) as mock_head: __UpperCamelCase = WavaVecaFeatureExtractor.from_pretrained('hf-internal-testing/tiny-random-wav2vec2' ) # This check we did call the fake head request mock_head.assert_called() def UpperCAmelCase ( self ): '''simple docstring''' __UpperCamelCase = WavaVecaFeatureExtractor.from_pretrained( 'https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json' ) @is_staging_test class __lowerCAmelCase ( unittest.TestCase ): @classmethod def UpperCAmelCase ( cls ): '''simple docstring''' __UpperCamelCase = TOKEN HfFolder.save_token(__UpperCAmelCase ) @classmethod def UpperCAmelCase ( cls ): '''simple docstring''' try: delete_repo(token=cls._token , repo_id='test-feature-extractor' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='valid_org/test-feature-extractor-org' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='test-dynamic-feature-extractor' ) except HTTPError: pass def UpperCAmelCase ( self ): '''simple docstring''' __UpperCamelCase = WavaVecaFeatureExtractor.from_pretrained(__UpperCAmelCase ) feature_extractor.push_to_hub('test-feature-extractor' , use_auth_token=self._token ) __UpperCamelCase = WavaVecaFeatureExtractor.from_pretrained(F'{USER}/test-feature-extractor' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(__UpperCAmelCase , getattr(__UpperCAmelCase , __UpperCAmelCase ) ) # Reset repo delete_repo(token=self._token , repo_id='test-feature-extractor' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( __UpperCAmelCase , repo_id='test-feature-extractor' , push_to_hub=__UpperCAmelCase , use_auth_token=self._token ) __UpperCamelCase = WavaVecaFeatureExtractor.from_pretrained(F'{USER}/test-feature-extractor' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(__UpperCAmelCase , getattr(__UpperCAmelCase , __UpperCAmelCase ) ) def UpperCAmelCase ( self ): '''simple docstring''' __UpperCamelCase = WavaVecaFeatureExtractor.from_pretrained(__UpperCAmelCase ) feature_extractor.push_to_hub('valid_org/test-feature-extractor' , use_auth_token=self._token ) __UpperCamelCase = WavaVecaFeatureExtractor.from_pretrained('valid_org/test-feature-extractor' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(__UpperCAmelCase , getattr(__UpperCAmelCase , __UpperCAmelCase ) ) # Reset repo delete_repo(token=self._token , repo_id='valid_org/test-feature-extractor' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( __UpperCAmelCase , repo_id='valid_org/test-feature-extractor-org' , push_to_hub=__UpperCAmelCase , use_auth_token=self._token ) __UpperCamelCase = WavaVecaFeatureExtractor.from_pretrained('valid_org/test-feature-extractor-org' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(__UpperCAmelCase , getattr(__UpperCAmelCase , __UpperCAmelCase ) ) def UpperCAmelCase ( self ): '''simple docstring''' CustomFeatureExtractor.register_for_auto_class() __UpperCamelCase = CustomFeatureExtractor.from_pretrained(__UpperCAmelCase ) feature_extractor.push_to_hub('test-dynamic-feature-extractor' , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( feature_extractor.auto_map , {'AutoFeatureExtractor': 'custom_feature_extraction.CustomFeatureExtractor'} , ) __UpperCamelCase = AutoFeatureExtractor.from_pretrained( F'{USER}/test-dynamic-feature-extractor' , trust_remote_code=__UpperCAmelCase ) # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module self.assertEqual(new_feature_extractor.__class__.__name__ , 'CustomFeatureExtractor' )
364
"""simple docstring""" from __future__ import annotations from typing import Any def A ( snake_case :list ) -> int: if not postfix_notation: return 0 __UpperCamelCase = {'+', '-', '*', '/'} __UpperCamelCase = [] for token in postfix_notation: if token in operations: __UpperCamelCase , __UpperCamelCase = stack.pop(), stack.pop() if token == "+": stack.append(a + b ) elif token == "-": stack.append(a - b ) elif token == "*": stack.append(a * b ) else: if a * b < 0 and a % b != 0: stack.append(a // b + 1 ) else: stack.append(a // b ) else: stack.append(int(snake_case ) ) return stack.pop() if __name__ == "__main__": import doctest doctest.testmod()
263
0
import argparse import datetime import json import time import warnings from logging import getLogger from pathlib import Path from typing import Dict, List import torch from tqdm import tqdm from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from utils import calculate_bleu, calculate_rouge, chunks, parse_numeric_n_bool_cl_kwargs, use_task_specific_params snake_case : Union[str, Any] = getLogger(__name__) snake_case : str = '''cuda''' if torch.cuda.is_available() else '''cpu''' def lowerCAmelCase_ ( _snake_case : str , _snake_case : Union[str, Any] , _snake_case : Union[str, Any] , _snake_case : List[str] = 8 , _snake_case : int = DEFAULT_DEVICE , _snake_case : Tuple=False , _snake_case : int="summarization" , _snake_case : str=None , **_snake_case : Optional[Any] , ) -> Union[str, Any]: '''simple docstring''' __magic_name__ : Optional[Any] = Path(_A ).open("w" , encoding="utf-8" ) __magic_name__ : List[str] = str(_A ) __magic_name__ : List[Any] = AutoModelForSeqaSeqLM.from_pretrained(_A ).to(_A ) if fpaa: __magic_name__ : Tuple = model.half() __magic_name__ : Tuple = AutoTokenizer.from_pretrained(_A ) logger.info(F'''Inferred tokenizer type: {tokenizer.__class__}''' ) # if this is wrong, check config.model_type. __magic_name__ : Dict = time.time() # update config with task specific params use_task_specific_params(_A , _A ) if prefix is None: __magic_name__ : str = prefix or getattr(model.config , "prefix" , "" ) or "" for examples_chunk in tqdm(list(chunks(_A , _A ) ) ): __magic_name__ : Optional[int] = [prefix + text for text in examples_chunk] __magic_name__ : List[str] = tokenizer(_A , return_tensors="pt" , truncation=_A , padding="longest" ).to(_A ) __magic_name__ : List[Any] = model.generate( input_ids=batch.input_ids , attention_mask=batch.attention_mask , **_A , ) __magic_name__ : Optional[int] = tokenizer.batch_decode(_A , skip_special_tokens=_A , clean_up_tokenization_spaces=_A ) for hypothesis in dec: fout.write(hypothesis + "\n" ) fout.flush() fout.close() __magic_name__ : Tuple = int(time.time() - start_time ) # seconds __magic_name__ : str = len(_A ) return {"n_obs": n_obs, "runtime": runtime, "seconds_per_sample": round(runtime / n_obs , 4 )} def lowerCAmelCase_ ( ) -> Dict: '''simple docstring''' return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S" ) def lowerCAmelCase_ ( _snake_case : Dict=True ) -> List[str]: '''simple docstring''' __magic_name__ : Optional[Any] = argparse.ArgumentParser() parser.add_argument("model_name" , type=_A , help="like facebook/bart-large-cnn,t5-base, etc." ) parser.add_argument("input_path" , type=_A , help="like cnn_dm/test.source" ) parser.add_argument("save_path" , type=_A , help="where to save summaries" ) parser.add_argument("--reference_path" , type=_A , required=_A , help="like cnn_dm/test.target" ) parser.add_argument("--score_path" , type=_A , required=_A , default="metrics.json" , help="where to save metrics" ) parser.add_argument("--device" , type=_A , required=_A , default=_A , help="cuda, cuda:1, cpu etc." ) parser.add_argument( "--prefix" , type=_A , required=_A , default=_A , help="will be added to the begininng of src examples" ) parser.add_argument("--task" , type=_A , default="summarization" , help="used for task_specific_params + metrics" ) parser.add_argument("--bs" , type=_A , default=8 , required=_A , help="batch size" ) parser.add_argument( "--n_obs" , type=_A , default=-1 , required=_A , help="How many observations. Defaults to all." ) parser.add_argument("--fp16" , action="store_true" ) parser.add_argument("--dump-args" , action="store_true" , help="print the custom hparams with the results" ) parser.add_argument( "--info" , nargs="?" , type=_A , const=datetime_now() , help=( "use in conjunction w/ --dump-args to print with the results whatever other info you\'d like, e.g." " lang=en-ru. If no value is passed, the current datetime string will be used." ) , ) # Unspecified args like --num_beams=2 --decoder_start_token_id=4 are passed to model.generate __magic_name__ , __magic_name__ : List[str] = parser.parse_known_args() __magic_name__ : Any = parse_numeric_n_bool_cl_kwargs(_A ) if parsed_args and verbose: print(F'''parsed the following generate kwargs: {parsed_args}''' ) __magic_name__ : int = [" " + x.rstrip() if "t5" in args.model_name else x.rstrip() for x in open(args.input_path ).readlines()] if args.n_obs > 0: __magic_name__ : List[str] = examples[: args.n_obs] Path(args.save_path ).parent.mkdir(exist_ok=_A ) if args.reference_path is None and Path(args.score_path ).exists(): warnings.warn(F'''score_path {args.score_path} will be overwritten unless you type ctrl-c.''' ) if args.device == "cpu" and args.fpaa: # this mix leads to RuntimeError: "threshold_cpu" not implemented for 'Half' raise ValueError("Can\'t mix --fp16 and --device cpu" ) __magic_name__ : Dict = generate_summaries_or_translations( _A , args.save_path , args.model_name , batch_size=args.bs , device=args.device , fpaa=args.fpaa , task=args.task , prefix=args.prefix , **_A , ) if args.reference_path is None: return {} # Compute scores __magic_name__ : List[Any] = calculate_bleu if "translation" in args.task else calculate_rouge __magic_name__ : str = [x.rstrip() for x in open(args.save_path ).readlines()] __magic_name__ : List[Any] = [x.rstrip() for x in open(args.reference_path ).readlines()][: len(_A )] __magic_name__ : Optional[Any] = score_fn(_A , _A ) scores.update(_A ) if args.dump_args: scores.update(_A ) if args.info: __magic_name__ : Union[str, Any] = args.info if verbose: print(_A ) if args.score_path is not None: json.dump(_A , open(args.score_path , "w" ) ) return scores if __name__ == "__main__": # Usage for MT: # python run_eval.py MODEL_NAME $DATA_DIR/test.source $save_dir/test_translations.txt --reference_path $DATA_DIR/test.target --score_path $save_dir/test_bleu.json --task translation $@ run_generate(verbose=True)
281
import numpy as np from PIL import Image def UpperCAmelCase_ ( _A , _A , _A ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = np.array(_A ) if arr.shape[0] != arr.shape[1]: raise ValueError('''The input array is not a square matrix''' ) SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 # compute the shape of the output matrix SCREAMING_SNAKE_CASE__ = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape maxpool_shape SCREAMING_SNAKE_CASE__ = np.zeros((maxpool_shape, maxpool_shape) ) while i < arr.shape[0]: if i + size > arr.shape[0]: # if the end of the matrix is reached, break break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the maximum of the pooling matrix SCREAMING_SNAKE_CASE__ = np.max(arr[i : i + size, j : j + size] ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 return updated_arr def UpperCAmelCase_ ( _A , _A , _A ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = np.array(_A ) if arr.shape[0] != arr.shape[1]: raise ValueError('''The input array is not a square matrix''' ) SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 # compute the shape of the output matrix SCREAMING_SNAKE_CASE__ = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape avgpool_shape SCREAMING_SNAKE_CASE__ = np.zeros((avgpool_shape, avgpool_shape) ) while i < arr.shape[0]: # if the end of the matrix is reached, break if i + size > arr.shape[0]: break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the average of the pooling matrix SCREAMING_SNAKE_CASE__ = int(np.average(arr[i : i + size, j : j + size] ) ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 return updated_arr # Main Function if __name__ == "__main__": from doctest import testmod testmod(name='''avgpooling''', verbose=True) # Loading the image _SCREAMING_SNAKE_CASE : Optional[int] = Image.open('''path_to_image''') # Converting the image to numpy array and maxpooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show() # Converting the image to numpy array and averagepooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()
314
0
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase_ : int = logging.get_logger(__name__) lowerCamelCase_ : Optional[int] = { """facebook/data2vec-text-base""": """https://huggingface.co/data2vec/resolve/main/config.json""", } class __A ( lowerCAmelCase_ ): """simple docstring""" __lowerCAmelCase = """data2vec-text""" def __init__( self , __A=3_0522 , __A=768 , __A=12 , __A=12 , __A=3072 , __A="gelu" , __A=0.1 , __A=0.1 , __A=512 , __A=2 , __A=0.02 , __A=1E-1_2 , __A=1 , __A=0 , __A=2 , __A="absolute" , __A=True , __A=None , **__A , ) -> Optional[int]: super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE , bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a =vocab_size a =hidden_size a =num_hidden_layers a =num_attention_heads a =hidden_act a =intermediate_size a =hidden_dropout_prob a =attention_probs_dropout_prob a =max_position_embeddings a =type_vocab_size a =initializer_range a =layer_norm_eps a =position_embedding_type a =use_cache a =classifier_dropout class __A ( lowerCAmelCase_ ): """simple docstring""" @property def SCREAMING_SNAKE_CASE ( self ) -> List[Any]: 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), ] )
352
"""simple docstring""" import itertools import os import random import tempfile import unittest import numpy as np from datasets import load_dataset from transformers import is_speech_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_speech_available(): from transformers import WhisperFeatureExtractor if is_torch_available(): import torch lowerCamelCase_ : Any = random.Random() def _A ( lowercase , lowercase=1.0 , lowercase=None , lowercase=None ): """simple docstring""" if rng is None: a =global_rng a =[] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch @require_torchaudio class __A ( unittest.TestCase ): """simple docstring""" def __init__( self , __A , __A=7 , __A=400 , __A=2000 , __A=10 , __A=160 , __A=8 , __A=0.0 , __A=4000 , __A=False , __A=True , ) -> Optional[Any]: a =parent a =batch_size a =min_seq_length a =max_seq_length a =(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) a =padding_value a =sampling_rate a =return_attention_mask a =do_normalize a =feature_size a =chunk_length a =hop_length def SCREAMING_SNAKE_CASE ( self ) -> str: return { "feature_size": self.feature_size, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def SCREAMING_SNAKE_CASE ( self , __A=False , __A=False ) -> str: def _flatten(__A ): return list(itertools.chain(*__A ) ) if equal_length: a =[floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size a =[ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: a =[np.asarray(__A ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class __A ( _SCREAMING_SNAKE_CASE, unittest.TestCase ): """simple docstring""" __lowerCAmelCase = WhisperFeatureExtractor if is_speech_available() else None def SCREAMING_SNAKE_CASE ( self ) -> Dict: a =WhisperFeatureExtractionTester(self ) def SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: a =self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: a =feat_extract_first.save_pretrained(__A )[0] check_json_file_has_correct_format(__A ) a =self.feature_extraction_class.from_pretrained(__A ) a =feat_extract_first.to_dict() a =feat_extract_second.to_dict() a =feat_extract_first.mel_filters a =feat_extract_second.mel_filters self.assertTrue(np.allclose(__A , __A ) ) self.assertEqual(__A , __A ) def SCREAMING_SNAKE_CASE ( self ) -> str: a =self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: a =os.path.join(__A , '''feat_extract.json''' ) feat_extract_first.to_json_file(__A ) a =self.feature_extraction_class.from_json_file(__A ) a =feat_extract_first.to_dict() a =feat_extract_second.to_dict() a =feat_extract_first.mel_filters a =feat_extract_second.mel_filters self.assertTrue(np.allclose(__A , __A ) ) self.assertEqual(__A , __A ) def SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: # Tests that all call wrap to encode_plus and batch_encode_plus a =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 a =[floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] a =[np.asarray(__A ) for speech_input in speech_inputs] # Test feature size a =feature_extractor(__A , padding='''max_length''' , return_tensors='''np''' ).input_features self.assertTrue(input_features.ndim == 3 ) self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames ) self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size ) # Test not batched input a =feature_extractor(speech_inputs[0] , return_tensors='''np''' ).input_features a =feature_extractor(np_speech_inputs[0] , return_tensors='''np''' ).input_features self.assertTrue(np.allclose(__A , __A , atol=1E-3 ) ) # Test batched a =feature_extractor(__A , return_tensors='''np''' ).input_features a =feature_extractor(__A , return_tensors='''np''' ).input_features for enc_seq_a, enc_seq_a in zip(__A , __A ): self.assertTrue(np.allclose(__A , __A , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. a =[floats_list((1, x) )[0] for x in (800, 800, 800)] a =np.asarray(__A ) a =feature_extractor(__A , return_tensors='''np''' ).input_features a =feature_extractor(__A , return_tensors='''np''' ).input_features for enc_seq_a, enc_seq_a in zip(__A , __A ): self.assertTrue(np.allclose(__A , __A , atol=1E-3 ) ) # Test truncation required a =[floats_list((1, x) )[0] for x in range(200 , (feature_extractor.n_samples + 500) , 200 )] a =[np.asarray(__A ) for speech_input in speech_inputs] a =[x[: feature_extractor.n_samples] for x in speech_inputs] a =[np.asarray(__A ) for speech_input in speech_inputs_truncated] a =feature_extractor(__A , return_tensors='''np''' ).input_features a =feature_extractor(__A , return_tensors='''np''' ).input_features for enc_seq_a, enc_seq_a in zip(__A , __A ): self.assertTrue(np.allclose(__A , __A , atol=1E-3 ) ) def SCREAMING_SNAKE_CASE ( self ) -> Tuple: import torch a =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) a =np.random.rand(100 , 32 ).astype(np.floataa ) a =np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: a =feature_extractor.pad([{'''input_features''': inputs}] , return_tensors='''np''' ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) a =feature_extractor.pad([{'''input_features''': inputs}] , return_tensors='''pt''' ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def SCREAMING_SNAKE_CASE ( self , __A ) -> Dict: a =load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech a =ds.sort('''id''' ).select(range(__A ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def SCREAMING_SNAKE_CASE ( self ) -> Any: # fmt: off a =torch.tensor( [ 0.1_193, -0.0_946, -0.1_098, -0.0_196, 0.0_225, -0.0_690, -0.1_736, 0.0_951, 0.0_971, -0.0_817, -0.0_702, 0.0_162, 0.0_260, 0.0_017, -0.0_192, -0.1_678, 0.0_709, -0.1_867, -0.0_655, -0.0_274, -0.0_234, -0.1_884, -0.0_516, -0.0_554, -0.0_274, -0.1_425, -0.1_423, 0.0_837, 0.0_377, -0.0_854 ] ) # fmt: on a =self._load_datasamples(1 ) a =WhisperFeatureExtractor() a =feature_extractor(__A , return_tensors='''pt''' ).input_features self.assertEqual(input_features.shape , (1, 80, 3000) ) self.assertTrue(torch.allclose(input_features[0, 0, :30] , __A , atol=1E-4 ) ) def SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: a =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) a =self._load_datasamples(1 )[0] a =((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535 # Rescale to [0, 65535] to show issue a =feat_extract.zero_mean_unit_var_norm([audio] , attention_mask=__A )[0] self.assertTrue(np.all(np.mean(__A ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(__A ) - 1 ) < 1E-3 ) )
215
0
from collections.abc import Generator from math import sin def __A ( __lowerCAmelCase )-> Union[str, Any]: """simple docstring""" if len(_UpperCAmelCase ) != 32: raise ValueError('Input must be of length 32' ) _UpperCAmelCase = b'' for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def __A ( __lowerCAmelCase )-> Optional[Any]: """simple docstring""" if i < 0: raise ValueError('Input must be non-negative' ) _UpperCAmelCase = format(_UpperCAmelCase , '08x' )[-8:] _UpperCAmelCase = b'' for i in [3, 2, 1, 0]: little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode('utf-8' ) return little_endian_hex def __A ( __lowerCAmelCase )-> List[str]: """simple docstring""" _UpperCAmelCase = b'' for char in message: bit_string += format(_UpperCAmelCase , '08b' ).encode('utf-8' ) _UpperCAmelCase = format(len(_UpperCAmelCase ) , '064b' ).encode('utf-8' ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(_UpperCAmelCase ) % 512 != 448: bit_string += b"0" bit_string += to_little_endian(start_len[32:] ) + to_little_endian(start_len[:32] ) return bit_string def __A ( __lowerCAmelCase )-> Dict: """simple docstring""" if len(_UpperCAmelCase ) % 512 != 0: raise ValueError('Input must have length that\'s a multiple of 512' ) for pos in range(0 , len(_UpperCAmelCase ) , 512 ): _UpperCAmelCase = bit_string[pos : pos + 512] _UpperCAmelCase = [] for i in range(0 , 512 , 32 ): block_words.append(int(to_little_endian(block[i : i + 32] ) , 2 ) ) yield block_words def __A ( __lowerCAmelCase )-> Tuple: """simple docstring""" if i < 0: raise ValueError('Input must be non-negative' ) _UpperCAmelCase = format(_UpperCAmelCase , '032b' ) _UpperCAmelCase = '' for c in i_str: new_str += "1" if c == "0" else "0" return int(_UpperCAmelCase , 2 ) def __A ( __lowerCAmelCase , __lowerCAmelCase )-> Optional[int]: """simple docstring""" return (a + b) % 2**32 def __A ( __lowerCAmelCase , __lowerCAmelCase )-> Any: """simple docstring""" if i < 0: raise ValueError('Input must be non-negative' ) if shift < 0: raise ValueError('Shift must be non-negative' ) return ((i << shift) ^ (i >> (32 - shift))) % 2**32 def __A ( __lowerCAmelCase )-> Union[str, Any]: """simple docstring""" _UpperCAmelCase = preprocess(_UpperCAmelCase ) _UpperCAmelCase = [int(2**32 * abs(sin(i + 1 ) ) ) for i in range(64 )] # Starting states _UpperCAmelCase = 0X6_7_4_5_2_3_0_1 _UpperCAmelCase = 0XE_F_C_D_A_B_8_9 _UpperCAmelCase = 0X9_8_B_A_D_C_F_E _UpperCAmelCase = 0X1_0_3_2_5_4_7_6 _UpperCAmelCase = [ 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, ] # Process bit string in chunks, each with 16 32-char words for block_words in get_block_words(_UpperCAmelCase ): _UpperCAmelCase = aa _UpperCAmelCase = ba _UpperCAmelCase = ca _UpperCAmelCase = da # Hash current chunk for i in range(64 ): if i <= 15: # f = (b & c) | (not_32(b) & d) # Alternate definition for f _UpperCAmelCase = d ^ (b & (c ^ d)) _UpperCAmelCase = i elif i <= 31: # f = (d & b) | (not_32(d) & c) # Alternate definition for f _UpperCAmelCase = c ^ (d & (b ^ c)) _UpperCAmelCase = (5 * i + 1) % 16 elif i <= 47: _UpperCAmelCase = b ^ c ^ d _UpperCAmelCase = (3 * i + 5) % 16 else: _UpperCAmelCase = c ^ (b | not_aa(_UpperCAmelCase )) _UpperCAmelCase = (7 * i) % 16 _UpperCAmelCase = (f + a + added_consts[i] + block_words[g]) % 2**32 _UpperCAmelCase = d _UpperCAmelCase = c _UpperCAmelCase = b _UpperCAmelCase = sum_aa(_UpperCAmelCase , left_rotate_aa(_UpperCAmelCase , shift_amounts[i] ) ) # Add hashed chunk to running total _UpperCAmelCase = sum_aa(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase = sum_aa(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase = sum_aa(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase = sum_aa(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase = reformat_hex(_UpperCAmelCase ) + reformat_hex(_UpperCAmelCase ) + reformat_hex(_UpperCAmelCase ) + reformat_hex(_UpperCAmelCase ) return digest if __name__ == "__main__": import doctest doctest.testmod()
39
from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. __snake_case :Optional[int] = 200 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. __snake_case :List[str] = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. __snake_case :List[Any] = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 1000)) def __snake_case ( _UpperCAmelCase , _UpperCAmelCase ): __a = len([g for position, g in enumerate(_UpperCAmelCase ) if g == main_target[position]] ) return (item, float(_UpperCAmelCase )) def __snake_case ( _UpperCAmelCase , _UpperCAmelCase ): __a = random.randint(0 , len(_UpperCAmelCase ) - 1 ) __a = parent_a[:random_slice] + parent_a[random_slice:] __a = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def __snake_case ( _UpperCAmelCase , _UpperCAmelCase ): __a = list(_UpperCAmelCase ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: __a = random.choice(_UpperCAmelCase ) return "".join(_UpperCAmelCase ) def __snake_case ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , ): __a = [] # Generate more children proportionally to the fitness score. __a = int(parent_a[1] * 100 ) + 1 __a = 10 if child_n >= 10 else child_n for _ in range(_UpperCAmelCase ): __a = population_score[random.randint(0 , _UpperCAmelCase )][0] __a , __a = crossover(parent_a[0] , _UpperCAmelCase ) # Append new string to the population list. pop.append(mutate(_UpperCAmelCase , _UpperCAmelCase ) ) pop.append(mutate(_UpperCAmelCase , _UpperCAmelCase ) ) return pop def __snake_case ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = True ): # Verify if N_POPULATION is bigger than N_SELECTED if N_POPULATION < N_SELECTED: __a = f'{N_POPULATION} must be bigger than {N_SELECTED}' raise ValueError(_UpperCAmelCase ) # Verify that the target contains no genes besides the ones inside genes variable. __a = sorted({c for c in target if c not in genes} ) if not_in_genes_list: __a = f'{not_in_genes_list} is not in genes list, evolution cannot converge' raise ValueError(_UpperCAmelCase ) # Generate random starting population. __a = [] for _ in range(_UpperCAmelCase ): population.append(''''''.join([random.choice(_UpperCAmelCase ) for i in range(len(_UpperCAmelCase ) )] ) ) # Just some logs to know what the algorithms is doing. __a , __a = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(_UpperCAmelCase ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. __a = [evaluate(_UpperCAmelCase , _UpperCAmelCase ) for item in population] # Check if there is a matching evolution. __a = sorted(_UpperCAmelCase , key=lambda _UpperCAmelCase : x[1] , reverse=_UpperCAmelCase ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( f'\nGeneration: {generation}' f'\nTotal Population:{total_population}' f'\nBest score: {population_score[0][1]}' f'\nBest string: {population_score[0][0]}' ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. __a = population[: int(N_POPULATION / 3 )] population.clear() population.extend(_UpperCAmelCase ) # Normalize population score to be between 0 and 1. __a = [ (item, score / len(_UpperCAmelCase )) for item, score in population_score ] # This is selection for i in range(_UpperCAmelCase ): population.extend(select(population_score[int(_UpperCAmelCase )] , _UpperCAmelCase , _UpperCAmelCase ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(_UpperCAmelCase ) > N_POPULATION: break if __name__ == "__main__": __snake_case :Optional[int] = ( '''This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!''' ) __snake_case :List[Any] = list( ''' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm''' '''nopqrstuvwxyz.,;!?+-*#@^\'èéòà€ù=)(&%$£/\\''' ) __snake_case ,__snake_case ,__snake_case :Dict = basic(target_str, genes_list) print( f'\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}' )
49
0
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_mobilebert import MobileBertTokenizer UpperCAmelCase_ : Optional[Any] = logging.get_logger(__name__) UpperCAmelCase_ : List[str] = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} UpperCAmelCase_ : Tuple = { """vocab_file""": {"""mobilebert-uncased""": """https://huggingface.co/google/mobilebert-uncased/resolve/main/vocab.txt"""}, """tokenizer_file""": { """mobilebert-uncased""": """https://huggingface.co/google/mobilebert-uncased/resolve/main/tokenizer.json""" }, } UpperCAmelCase_ : Any = {"""mobilebert-uncased""": 512} UpperCAmelCase_ : int = {} class _a ( lowerCAmelCase_ ): snake_case__ : Dict = VOCAB_FILES_NAMES snake_case__ : List[Any] = PRETRAINED_VOCAB_FILES_MAP snake_case__ : Tuple = PRETRAINED_INIT_CONFIGURATION snake_case__ : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ : Any = MobileBertTokenizer def __init__( self : List[Any] , SCREAMING_SNAKE_CASE__ : Optional[Any]=None , SCREAMING_SNAKE_CASE__ : int=None , SCREAMING_SNAKE_CASE__ : Dict=True , SCREAMING_SNAKE_CASE__ : int="[UNK]" , SCREAMING_SNAKE_CASE__ : Dict="[SEP]" , SCREAMING_SNAKE_CASE__ : Dict="[PAD]" , SCREAMING_SNAKE_CASE__ : Dict="[CLS]" , SCREAMING_SNAKE_CASE__ : Optional[Any]="[MASK]" , SCREAMING_SNAKE_CASE__ : Optional[Any]=True , SCREAMING_SNAKE_CASE__ : Tuple=None , **SCREAMING_SNAKE_CASE__ : Union[str, Any] , ) -> Dict: super().__init__( __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , do_lower_case=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , tokenize_chinese_chars=__SCREAMING_SNAKE_CASE , strip_accents=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) a_ : Any = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' , __SCREAMING_SNAKE_CASE ) != do_lower_case or normalizer_state.get('strip_accents' , __SCREAMING_SNAKE_CASE ) != strip_accents or normalizer_state.get('handle_chinese_chars' , __SCREAMING_SNAKE_CASE ) != tokenize_chinese_chars ): a_ : Dict = getattr(__SCREAMING_SNAKE_CASE , normalizer_state.pop('type' ) ) a_ : Dict = do_lower_case a_ : Any = strip_accents a_ : Tuple = tokenize_chinese_chars a_ : Optional[int] = normalizer_class(**__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = do_lower_case def SCREAMING_SNAKE_CASE ( self : List[str] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[Any]=None ) -> Optional[Any]: a_ : List[Any] = [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 SCREAMING_SNAKE_CASE ( self : Any , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Optional[int] = None ) -> Optional[Any]: a_ : List[Any] = [self.sep_token_id] a_ : int = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def SCREAMING_SNAKE_CASE ( self : Tuple , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : List[str] = None ) -> Tuple: a_ : List[Any] = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE )
352
import os from collections import deque import torch from torch.utils.data import Dataset class SCREAMING_SNAKE_CASE__ ( lowercase__ ): def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : List[Any]="" , SCREAMING_SNAKE_CASE__ : Union[str, Any]="train" ) -> Tuple: assert os.path.isdir(SCREAMING_SNAKE_CASE__ ) a_ : int = [] a_ : Optional[int] = os.listdir(SCREAMING_SNAKE_CASE__ ) for story_filename in story_filenames_list: if "summary" in story_filename: continue a_ : List[str] = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if not os.path.isfile(SCREAMING_SNAKE_CASE__ ): continue self.documents.append(SCREAMING_SNAKE_CASE__ ) def __len__( self : Dict ) -> str: return len(self.documents ) def __getitem__( self : Dict , SCREAMING_SNAKE_CASE__ : str ) -> str: a_ : int = self.documents[idx] a_ : Tuple = document_path.split('/' )[-1] with open(SCREAMING_SNAKE_CASE__ , encoding='utf-8' ) as source: a_ : Dict = source.read() a_ , a_ : Optional[Any] = process_story(SCREAMING_SNAKE_CASE__ ) return document_name, story_lines, summary_lines def SCREAMING_SNAKE_CASE_ ( __A : Optional[int] ) -> Any: """simple docstring""" a_ : Optional[Any] = list(filter(lambda __A : len(__A ) != 0 , [line.strip() for line in raw_story.split('\n' )] ) ) # for some unknown reason some lines miss a period, add it a_ : List[Any] = [_add_missing_period(__A ) for line in nonempty_lines] # gather article lines a_ : int = [] a_ : List[Any] = deque(__A ) while True: try: a_ : Dict = lines.popleft() if element.startswith('@highlight' ): break story_lines.append(__A ) except IndexError: # if "@highlight" is absent from the file we pop # all elements until there is None, raising an exception. return story_lines, [] # gather summary lines a_ : List[str] = list(filter(lambda __A : not t.startswith('@highlight' ) , __A ) ) return story_lines, summary_lines def SCREAMING_SNAKE_CASE_ ( __A : Union[str, Any] ) -> Any: """simple docstring""" a_ : Any = ['.', '!', '?', '...', '\'', '`', '"', '\u2019', '\u2019', ')'] if line.startswith('@highlight' ): return line if line[-1] in END_TOKENS: return line return line + "." def SCREAMING_SNAKE_CASE_ ( __A : Optional[int] , __A : Union[str, Any] , __A : List[str] ) -> Union[str, Any]: """simple docstring""" if len(__A ) > block_size: return sequence[:block_size] else: sequence.extend([pad_token_id] * (block_size - len(__A )) ) return sequence def SCREAMING_SNAKE_CASE_ ( __A : Tuple , __A : str ) -> Any: """simple docstring""" a_ : Optional[int] = torch.ones_like(__A ) a_ : List[str] = sequence == pad_token_id a_ : str = 0 return mask def SCREAMING_SNAKE_CASE_ ( __A : Union[str, Any] , __A : Optional[Any] , __A : Dict ) -> List[str]: """simple docstring""" a_ : Optional[int] = [tokenizer.encode(__A ) for line in story_lines] a_ : int = [token for sentence in story_lines_token_ids for token in sentence] a_ : Dict = [tokenizer.encode(__A ) for line in summary_lines] a_ : int = [token for sentence in summary_lines_token_ids for token in sentence] return story_token_ids, summary_token_ids def SCREAMING_SNAKE_CASE_ ( __A : Tuple , __A : List[str] ) -> Optional[Any]: """simple docstring""" a_ : int = [] for sequence in batch: a_ : int = -1 a_ : Dict = [] for s in sequence: if s == separator_token_id: sentence_num += 1 embeddings.append(sentence_num % 2 ) batch_embeddings.append(__A ) return torch.tensor(__A )
120
0
"""simple docstring""" def _snake_case ( lowercase__ : dict ) -> set: '''simple docstring''' lowerCAmelCase_ :Any = set() # edges = list of graph's edges lowerCAmelCase_ :List[Any] = get_edges(lowercase__ ) # While there are still elements in edges list, take an arbitrary edge # (from_node, to_node) and add his extremity to chosen_vertices and then # remove all arcs adjacent to the from_node and to_node while edges: lowerCAmelCase_ , lowerCAmelCase_ :Optional[Any] = edges.pop() chosen_vertices.add(lowercase__ ) chosen_vertices.add(lowercase__ ) for edge in edges.copy(): if from_node in edge or to_node in edge: edges.discard(lowercase__ ) return chosen_vertices def _snake_case ( lowercase__ : dict ) -> set: '''simple docstring''' lowerCAmelCase_ :Dict = set() for from_node, to_nodes in graph.items(): for to_node in to_nodes: edges.add((from_node, to_node) ) return edges if __name__ == "__main__": import doctest doctest.testmod() # graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} # print(f"Matching vertex cover:\n{matching_min_vertex_cover(graph)}")
84
"""simple docstring""" from __future__ import annotations class snake_case : def __init__( self , __UpperCAmelCase) ->Any: a_ = TypeError( "Matrices must be formed from a list of zero or more lists containing at " "least one and the same number of values, each of which must be of type " "int or float.") if len(__UpperCAmelCase) != 0: a_ = len(rows[0]) if cols == 0: raise error for row in rows: if len(__UpperCAmelCase) != cols: raise error for value in row: if not isinstance(__UpperCAmelCase , (int, float)): raise error a_ = rows else: a_ = [] def UpperCAmelCase__ ( self) ->list[list[int]]: return [[row[i] for row in self.rows] for i in range(len(self.rows[0]))] @property def UpperCAmelCase__ ( self) ->int: return len(self.rows) @property def UpperCAmelCase__ ( self) ->int: return len(self.rows[0]) @property def UpperCAmelCase__ ( self) ->tuple[int, int]: return (self.num_rows, self.num_columns) @property def UpperCAmelCase__ ( self) ->bool: return self.order[0] == self.order[1] def UpperCAmelCase__ ( self) ->Matrix: a_ = [ [0 if column_num != row_num else 1 for column_num in range(self.num_rows)] for row_num in range(self.num_rows) ] return Matrix(__UpperCAmelCase) def UpperCAmelCase__ ( self) ->int: if not self.is_square: return 0 if self.order == (0, 0): return 1 if self.order == (1, 1): return int(self.rows[0][0]) if self.order == (2, 2): return int( (self.rows[0][0] * self.rows[1][1]) - (self.rows[0][1] * self.rows[1][0])) else: return sum( self.rows[0][column] * self.cofactors().rows[0][column] for column in range(self.num_columns)) def UpperCAmelCase__ ( self) ->bool: return bool(self.determinant()) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase) ->int: a_ = [ [ self.rows[other_row][other_column] for other_column in range(self.num_columns) if other_column != column ] for other_row in range(self.num_rows) if other_row != row ] return Matrix(__UpperCAmelCase).determinant() def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase) ->int: if (row + column) % 2 == 0: return self.get_minor(__UpperCAmelCase , __UpperCAmelCase) return -1 * self.get_minor(__UpperCAmelCase , __UpperCAmelCase) def UpperCAmelCase__ ( self) ->Matrix: return Matrix( [ [self.get_minor(__UpperCAmelCase , __UpperCAmelCase) for column in range(self.num_columns)] for row in range(self.num_rows) ]) def UpperCAmelCase__ ( self) ->Matrix: return Matrix( [ [ self.minors().rows[row][column] if (row + column) % 2 == 0 else self.minors().rows[row][column] * -1 for column in range(self.minors().num_columns) ] for row in range(self.minors().num_rows) ]) def UpperCAmelCase__ ( self) ->Matrix: a_ = [ [self.cofactors().rows[column][row] for column in range(self.num_columns)] for row in range(self.num_rows) ] return Matrix(__UpperCAmelCase) def UpperCAmelCase__ ( self) ->Matrix: a_ = self.determinant() if not determinant: raise TypeError("Only matrices with a non-zero determinant have an inverse") return self.adjugate() * (1 / determinant) def __repr__( self) ->str: return str(self.rows) def __str__( self) ->str: if self.num_rows == 0: return "[]" if self.num_rows == 1: return "[[" + ". ".join(str(self.rows[0])) + "]]" return ( "[" + "\n ".join( [ "[" + ". ".join([str(__UpperCAmelCase) for value in row]) + ".]" for row in self.rows ]) + "]" ) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase = None) ->None: a_ = TypeError("Row must be a list containing all ints and/or floats") if not isinstance(__UpperCAmelCase , __UpperCAmelCase): raise type_error for value in row: if not isinstance(__UpperCAmelCase , (int, float)): raise type_error if len(__UpperCAmelCase) != self.num_columns: raise ValueError( "Row must be equal in length to the other rows in the matrix") if position is None: self.rows.append(__UpperCAmelCase) else: a_ = self.rows[0:position] + [row] + self.rows[position:] def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase = None) ->None: a_ = TypeError( "Column must be a list containing all ints and/or floats") if not isinstance(__UpperCAmelCase , __UpperCAmelCase): raise type_error for value in column: if not isinstance(__UpperCAmelCase , (int, float)): raise type_error if len(__UpperCAmelCase) != self.num_rows: raise ValueError( "Column must be equal in length to the other columns in the matrix") if position is None: a_ = [self.rows[i] + [column[i]] for i in range(self.num_rows)] else: a_ = [ self.rows[i][0:position] + [column[i]] + self.rows[i][position:] for i in range(self.num_rows) ] def __eq__( self , __UpperCAmelCase) ->bool: if not isinstance(__UpperCAmelCase , __UpperCAmelCase): return NotImplemented return self.rows == other.rows def __ne__( self , __UpperCAmelCase) ->bool: return not self == other def __neg__( self) ->Matrix: return self * -1 def __add__( self , __UpperCAmelCase) ->Matrix: if self.order != other.order: raise ValueError("Addition requires matrices of the same order") return Matrix( [ [self.rows[i][j] + other.rows[i][j] for j in range(self.num_columns)] for i in range(self.num_rows) ]) def __sub__( self , __UpperCAmelCase) ->Matrix: if self.order != other.order: raise ValueError("Subtraction requires matrices of the same order") return Matrix( [ [self.rows[i][j] - other.rows[i][j] for j in range(self.num_columns)] for i in range(self.num_rows) ]) def __mul__( self , __UpperCAmelCase) ->Matrix: if isinstance(__UpperCAmelCase , (int, float)): return Matrix( [[int(element * other) for element in row] for row in self.rows]) elif isinstance(__UpperCAmelCase , __UpperCAmelCase): if self.num_columns != other.num_rows: raise ValueError( "The number of columns in the first matrix must " "be equal to the number of rows in the second") return Matrix( [ [Matrix.dot_product(__UpperCAmelCase , __UpperCAmelCase) for column in other.columns()] for row in self.rows ]) else: raise TypeError( "A Matrix can only be multiplied by an int, float, or another matrix") def __pow__( self , __UpperCAmelCase) ->Matrix: if not isinstance(__UpperCAmelCase , __UpperCAmelCase): raise TypeError("A Matrix can only be raised to the power of an int") if not self.is_square: raise ValueError("Only square matrices can be raised to a power") if other == 0: return self.identity() if other < 0: if self.is_invertable(): return self.inverse() ** (-other) raise ValueError( "Only invertable matrices can be raised to a negative power") a_ = self for _ in range(other - 1): result *= self return result @classmethod def UpperCAmelCase__ ( cls , __UpperCAmelCase , __UpperCAmelCase) ->int: return sum(row[i] * column[i] for i in range(len(__UpperCAmelCase))) if __name__ == "__main__": import doctest doctest.testmod()
243
0
import argparse import torch from transformers import FunnelBaseModel, FunnelConfig, FunnelModel, load_tf_weights_in_funnel from transformers.utils import logging logging.set_verbosity_info() def A ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase_ = FunnelConfig.from_json_file(__UpperCAmelCase ) print(f"Building PyTorch model from configuration: {config}" ) UpperCAmelCase_ = FunnelBaseModel(__UpperCAmelCase ) if base_model else FunnelModel(__UpperCAmelCase ) # Load weights from tf checkpoint load_tf_weights_in_funnel(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) # Save pytorch-model print(f"Save PyTorch model to {pytorch_dump_path}" ) torch.save(model.state_dict() , __UpperCAmelCase ) if __name__ == "__main__": UpperCamelCase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--config_file", default=None, type=str, required=True, help="The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.", ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument( "--base_model", action="store_true", help="Whether you want just the base model (no decoder) or not." ) UpperCamelCase_ = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path, args.base_model )
344
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCamelCase_ = logging.get_logger(__name__) UpperCamelCase_ = { "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 a_ ( _snake_case ): UpperCamelCase__ : Optional[int] ="levit" def __init__( self :List[str] , _lowercase :List[Any]=224 , _lowercase :str=3 , _lowercase :Optional[int]=3 , _lowercase :str=2 , _lowercase :List[Any]=1 , _lowercase :str=16 , _lowercase :Dict=[128, 256, 384] , _lowercase :Union[str, Any]=[4, 8, 12] , _lowercase :Tuple=[4, 4, 4] , _lowercase :Dict=[16, 16, 16] , _lowercase :Any=0 , _lowercase :Dict=[2, 2, 2] , _lowercase :Any=[2, 2, 2] , _lowercase :Tuple=0.02 , **_lowercase :Union[str, Any] , ) -> Optional[Any]: super().__init__(**_lowercase) UpperCAmelCase_ = image_size UpperCAmelCase_ = num_channels UpperCAmelCase_ = kernel_size UpperCAmelCase_ = stride UpperCAmelCase_ = padding UpperCAmelCase_ = hidden_sizes UpperCAmelCase_ = num_attention_heads UpperCAmelCase_ = depths UpperCAmelCase_ = key_dim UpperCAmelCase_ = drop_path_rate UpperCAmelCase_ = patch_size UpperCAmelCase_ = attention_ratio UpperCAmelCase_ = mlp_ratio UpperCAmelCase_ = initializer_range UpperCAmelCase_ = [ ['''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 a_ ( _snake_case ): UpperCamelCase__ : Union[str, Any] =version.parse("1.11" ) @property def __a ( self :Any) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ]) @property def __a ( self :List[Any]) -> float: return 1E-4
344
1
from typing import TYPE_CHECKING from ...utils import _LazyModule lowercase__ : Optional[int] = {"tokenization_wav2vec2_phoneme": ["Wav2Vec2PhonemeCTCTokenizer"]} if TYPE_CHECKING: from .tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizer else: import sys lowercase__ : int = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
187
import argparse import os.path as osp import re import torch from safetensors.torch import load_file, save_file # =================# # UNet Conversion # # =================# lowercase__ : Optional[int] = [ # (stable-diffusion, HF Diffusers) ("time_embed.0.weight", "time_embedding.linear_1.weight"), ("time_embed.0.bias", "time_embedding.linear_1.bias"), ("time_embed.2.weight", "time_embedding.linear_2.weight"), ("time_embed.2.bias", "time_embedding.linear_2.bias"), ("input_blocks.0.0.weight", "conv_in.weight"), ("input_blocks.0.0.bias", "conv_in.bias"), ("out.0.weight", "conv_norm_out.weight"), ("out.0.bias", "conv_norm_out.bias"), ("out.2.weight", "conv_out.weight"), ("out.2.bias", "conv_out.bias"), ] lowercase__ : List[Any] = [ # (stable-diffusion, HF Diffusers) ("in_layers.0", "norm1"), ("in_layers.2", "conv1"), ("out_layers.0", "norm2"), ("out_layers.3", "conv2"), ("emb_layers.1", "time_emb_proj"), ("skip_connection", "conv_shortcut"), ] lowercase__ : Optional[Any] = [] # hardcoded number of downblocks and resnets/attentions... # would need smarter logic for other networks. for i in range(4): # loop over downblocks/upblocks for j in range(2): # loop over resnets/attentions for downblocks lowercase__ : str = f'''down_blocks.{i}.resnets.{j}.''' lowercase__ : Union[str, Any] = f'''input_blocks.{3*i + j + 1}.0.''' unet_conversion_map_layer.append((sd_down_res_prefix, hf_down_res_prefix)) if i < 3: # no attention layers in down_blocks.3 lowercase__ : Tuple = f'''down_blocks.{i}.attentions.{j}.''' lowercase__ : Dict = f'''input_blocks.{3*i + j + 1}.1.''' unet_conversion_map_layer.append((sd_down_atn_prefix, hf_down_atn_prefix)) for j in range(3): # loop over resnets/attentions for upblocks lowercase__ : List[Any] = f'''up_blocks.{i}.resnets.{j}.''' lowercase__ : int = f'''output_blocks.{3*i + j}.0.''' unet_conversion_map_layer.append((sd_up_res_prefix, hf_up_res_prefix)) if i > 0: # no attention layers in up_blocks.0 lowercase__ : List[str] = f'''up_blocks.{i}.attentions.{j}.''' lowercase__ : Tuple = f'''output_blocks.{3*i + j}.1.''' unet_conversion_map_layer.append((sd_up_atn_prefix, hf_up_atn_prefix)) if i < 3: # no downsample in down_blocks.3 lowercase__ : List[str] = f'''down_blocks.{i}.downsamplers.0.conv.''' lowercase__ : Any = f'''input_blocks.{3*(i+1)}.0.op.''' unet_conversion_map_layer.append((sd_downsample_prefix, hf_downsample_prefix)) # no upsample in up_blocks.3 lowercase__ : Optional[int] = f'''up_blocks.{i}.upsamplers.0.''' lowercase__ : int = f'''output_blocks.{3*i + 2}.{1 if i == 0 else 2}.''' unet_conversion_map_layer.append((sd_upsample_prefix, hf_upsample_prefix)) lowercase__ : Union[str, Any] = "mid_block.attentions.0." lowercase__ : List[Any] = "middle_block.1." unet_conversion_map_layer.append((sd_mid_atn_prefix, hf_mid_atn_prefix)) for j in range(2): lowercase__ : Tuple = f'''mid_block.resnets.{j}.''' lowercase__ : List[str] = f'''middle_block.{2*j}.''' unet_conversion_map_layer.append((sd_mid_res_prefix, hf_mid_res_prefix)) def lowerCamelCase__ ( _A ): '''simple docstring''' snake_case_ = {k: k for k in unet_state_dict.keys()} for sd_name, hf_name in unet_conversion_map: snake_case_ = sd_name for k, v in mapping.items(): if "resnets" in k: for sd_part, hf_part in unet_conversion_map_resnet: snake_case_ = v.replace(_A , _A ) snake_case_ = v for k, v in mapping.items(): for sd_part, hf_part in unet_conversion_map_layer: snake_case_ = v.replace(_A , _A ) snake_case_ = v snake_case_ = {v: unet_state_dict[k] for k, v in mapping.items()} return new_state_dict # ================# # VAE Conversion # # ================# lowercase__ : Dict = [ # (stable-diffusion, HF Diffusers) ("nin_shortcut", "conv_shortcut"), ("norm_out", "conv_norm_out"), ("mid.attn_1.", "mid_block.attentions.0."), ] for i in range(4): # down_blocks have two resnets for j in range(2): lowercase__ : Any = f'''encoder.down_blocks.{i}.resnets.{j}.''' lowercase__ : List[Any] = f'''encoder.down.{i}.block.{j}.''' vae_conversion_map.append((sd_down_prefix, hf_down_prefix)) if i < 3: lowercase__ : Optional[int] = f'''down_blocks.{i}.downsamplers.0.''' lowercase__ : Tuple = f'''down.{i}.downsample.''' vae_conversion_map.append((sd_downsample_prefix, hf_downsample_prefix)) lowercase__ : List[str] = f'''up_blocks.{i}.upsamplers.0.''' lowercase__ : Optional[int] = f'''up.{3-i}.upsample.''' vae_conversion_map.append((sd_upsample_prefix, hf_upsample_prefix)) # up_blocks have three resnets # also, up blocks in hf are numbered in reverse from sd for j in range(3): lowercase__ : int = f'''decoder.up_blocks.{i}.resnets.{j}.''' lowercase__ : Union[str, Any] = f'''decoder.up.{3-i}.block.{j}.''' vae_conversion_map.append((sd_up_prefix, hf_up_prefix)) # this part accounts for mid blocks in both the encoder and the decoder for i in range(2): lowercase__ : Dict = f'''mid_block.resnets.{i}.''' lowercase__ : int = f'''mid.block_{i+1}.''' vae_conversion_map.append((sd_mid_res_prefix, hf_mid_res_prefix)) lowercase__ : Optional[Any] = [ # (stable-diffusion, HF Diffusers) ("norm.", "group_norm."), ("q.", "query."), ("k.", "key."), ("v.", "value."), ("proj_out.", "proj_attn."), ] def lowerCamelCase__ ( _A ): '''simple docstring''' return w.reshape(*w.shape , 1 , 1 ) def lowerCamelCase__ ( _A ): '''simple docstring''' snake_case_ = {k: k for k in vae_state_dict.keys()} for k, v in mapping.items(): for sd_part, hf_part in vae_conversion_map: snake_case_ = v.replace(_A , _A ) snake_case_ = v for k, v in mapping.items(): if "attentions" in k: for sd_part, hf_part in vae_conversion_map_attn: snake_case_ = v.replace(_A , _A ) snake_case_ = v snake_case_ = {v: vae_state_dict[k] for k, v in mapping.items()} snake_case_ = ["q", "k", "v", "proj_out"] for k, v in new_state_dict.items(): for weight_name in weights_to_convert: if f"mid.attn_1.{weight_name}.weight" in k: print(f"Reshaping {k} for SD format" ) snake_case_ = reshape_weight_for_sd(_A ) return new_state_dict # =========================# # Text Encoder Conversion # # =========================# lowercase__ : int = [ # (stable-diffusion, HF Diffusers) ("resblocks.", "text_model.encoder.layers."), ("ln_1", "layer_norm1"), ("ln_2", "layer_norm2"), (".c_fc.", ".fc1."), (".c_proj.", ".fc2."), (".attn", ".self_attn"), ("ln_final.", "transformer.text_model.final_layer_norm."), ("token_embedding.weight", "transformer.text_model.embeddings.token_embedding.weight"), ("positional_embedding", "transformer.text_model.embeddings.position_embedding.weight"), ] lowercase__ : Dict = {re.escape(x[1]): x[0] for x in textenc_conversion_lst} lowercase__ : Tuple = re.compile("|".join(protected.keys())) # Ordering is from https://github.com/pytorch/pytorch/blob/master/test/cpp/api/modules.cpp lowercase__ : Dict = {"q": 0, "k": 1, "v": 2} def lowerCamelCase__ ( _A ): '''simple docstring''' snake_case_ = {} snake_case_ = {} snake_case_ = {} for k, v in text_enc_dict.items(): if ( k.endswith(".self_attn.q_proj.weight" ) or k.endswith(".self_attn.k_proj.weight" ) or k.endswith(".self_attn.v_proj.weight" ) ): snake_case_ = k[: -len(".q_proj.weight" )] snake_case_ = k[-len("q_proj.weight" )] if k_pre not in capture_qkv_weight: snake_case_ = [None, None, None] snake_case_ = v continue if ( k.endswith(".self_attn.q_proj.bias" ) or k.endswith(".self_attn.k_proj.bias" ) or k.endswith(".self_attn.v_proj.bias" ) ): snake_case_ = k[: -len(".q_proj.bias" )] snake_case_ = k[-len("q_proj.bias" )] if k_pre not in capture_qkv_bias: snake_case_ = [None, None, None] snake_case_ = v continue snake_case_ = textenc_pattern.sub(lambda _A : protected[re.escape(m.group(0 ) )] , _A ) snake_case_ = v for k_pre, tensors in capture_qkv_weight.items(): if None in tensors: raise Exception("CORRUPTED MODEL: one of the q-k-v values for the text encoder was missing" ) snake_case_ = textenc_pattern.sub(lambda _A : protected[re.escape(m.group(0 ) )] , _A ) snake_case_ = torch.cat(_A ) for k_pre, tensors in capture_qkv_bias.items(): if None in tensors: raise Exception("CORRUPTED MODEL: one of the q-k-v values for the text encoder was missing" ) snake_case_ = textenc_pattern.sub(lambda _A : protected[re.escape(m.group(0 ) )] , _A ) snake_case_ = torch.cat(_A ) return new_state_dict def lowerCamelCase__ ( _A ): '''simple docstring''' return text_enc_dict if __name__ == "__main__": lowercase__ : int = argparse.ArgumentParser() parser.add_argument("--model_path", default=None, type=str, required=True, help="Path to the model to convert.") parser.add_argument("--checkpoint_path", default=None, type=str, required=True, help="Path to the output model.") parser.add_argument("--half", action="store_true", help="Save weights in half precision.") parser.add_argument( "--use_safetensors", action="store_true", help="Save weights use safetensors, default is ckpt." ) lowercase__ : Dict = parser.parse_args() assert args.model_path is not None, "Must provide a model path!" assert args.checkpoint_path is not None, "Must provide a checkpoint path!" # Path for safetensors lowercase__ : Tuple = osp.join(args.model_path, "unet", "diffusion_pytorch_model.safetensors") lowercase__ : int = osp.join(args.model_path, "vae", "diffusion_pytorch_model.safetensors") lowercase__ : Any = osp.join(args.model_path, "text_encoder", "model.safetensors") # Load models from safetensors if it exists, if it doesn't pytorch if osp.exists(unet_path): lowercase__ : str = load_file(unet_path, device="cpu") else: lowercase__ : Optional[Any] = osp.join(args.model_path, "unet", "diffusion_pytorch_model.bin") lowercase__ : Any = torch.load(unet_path, map_location="cpu") if osp.exists(vae_path): lowercase__ : Tuple = load_file(vae_path, device="cpu") else: lowercase__ : Any = osp.join(args.model_path, "vae", "diffusion_pytorch_model.bin") lowercase__ : Dict = torch.load(vae_path, map_location="cpu") if osp.exists(text_enc_path): lowercase__ : Union[str, Any] = load_file(text_enc_path, device="cpu") else: lowercase__ : Union[str, Any] = osp.join(args.model_path, "text_encoder", "pytorch_model.bin") lowercase__ : Optional[int] = torch.load(text_enc_path, map_location="cpu") # Convert the UNet model lowercase__ : Dict = convert_unet_state_dict(unet_state_dict) lowercase__ : Any = {"model.diffusion_model." + k: v for k, v in unet_state_dict.items()} # Convert the VAE model lowercase__ : Dict = convert_vae_state_dict(vae_state_dict) lowercase__ : Union[str, Any] = {"first_stage_model." + k: v for k, v in vae_state_dict.items()} # Easiest way to identify v2.0 model seems to be that the text encoder (OpenCLIP) is deeper lowercase__ : Optional[Any] = "text_model.encoder.layers.22.layer_norm2.bias" in text_enc_dict if is_vaa_model: # Need to add the tag 'transformer' in advance so we can knock it out from the final layer-norm lowercase__ : Any = {"transformer." + k: v for k, v in text_enc_dict.items()} lowercase__ : List[Any] = convert_text_enc_state_dict_vaa(text_enc_dict) lowercase__ : List[str] = {"cond_stage_model.model." + k: v for k, v in text_enc_dict.items()} else: lowercase__ : Tuple = convert_text_enc_state_dict(text_enc_dict) lowercase__ : Any = {"cond_stage_model.transformer." + k: v for k, v in text_enc_dict.items()} # Put together new checkpoint lowercase__ : Tuple = {**unet_state_dict, **vae_state_dict, **text_enc_dict} if args.half: lowercase__ : Any = {k: v.half() for k, v in state_dict.items()} if args.use_safetensors: save_file(state_dict, args.checkpoint_path) else: lowercase__ : Union[str, Any] = {"state_dict": state_dict} torch.save(state_dict, args.checkpoint_path)
187
1
import time from contextlib import contextmanager from pathlib import Path import pytest import requests from huggingface_hub.hf_api import HfApi, HfFolder UpperCAmelCase = """__DUMMY_TRANSFORMERS_USER__""" UpperCAmelCase = """Dummy User""" UpperCAmelCase = """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""" UpperCAmelCase = """https://hub-ci.huggingface.co""" UpperCAmelCase = CI_HUB_ENDPOINT + """/datasets/{repo_id}/resolve/{revision}/{path}""" UpperCAmelCase = CI_HUB_ENDPOINT + """/{repo_id}/resolve/{revision}/{filename}""" UpperCAmelCase = Path("""~/.huggingface/hub_ci_token""").expanduser() @pytest.fixture def __lowerCAmelCase (SCREAMING_SNAKE_CASE )-> List[Any]: """simple docstring""" monkeypatch.setattr( '''huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE''' , SCREAMING_SNAKE_CASE ) @pytest.fixture def __lowerCAmelCase (SCREAMING_SNAKE_CASE )-> Any: """simple docstring""" monkeypatch.setattr('''datasets.config.HF_ENDPOINT''' , SCREAMING_SNAKE_CASE ) monkeypatch.setattr('''datasets.config.HUB_DATASETS_URL''' , SCREAMING_SNAKE_CASE ) @pytest.fixture def __lowerCAmelCase (SCREAMING_SNAKE_CASE )-> Optional[Any]: """simple docstring""" monkeypatch.setattr('''huggingface_hub.hf_api.HfFolder.path_token''' , SCREAMING_SNAKE_CASE ) @pytest.fixture def __lowerCAmelCase (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE )-> List[Any]: """simple docstring""" HfFolder.save_token(SCREAMING_SNAKE_CASE ) yield HfFolder.delete_token() @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ()-> Any: """simple docstring""" return HfApi(endpoint=SCREAMING_SNAKE_CASE ) @pytest.fixture(scope='''session''' ) def __lowerCAmelCase (SCREAMING_SNAKE_CASE )-> List[str]: """simple docstring""" snake_case_ = HfFolder.get_token() HfFolder.save_token(SCREAMING_SNAKE_CASE ) yield CI_HUB_USER_TOKEN if previous_token is not None: HfFolder.save_token(SCREAMING_SNAKE_CASE ) @pytest.fixture def __lowerCAmelCase (SCREAMING_SNAKE_CASE )-> Tuple: """simple docstring""" def _cleanup_repo(SCREAMING_SNAKE_CASE ): hf_api.delete_repo(SCREAMING_SNAKE_CASE , token=SCREAMING_SNAKE_CASE , repo_type='''dataset''' ) return _cleanup_repo @pytest.fixture def __lowerCAmelCase (SCREAMING_SNAKE_CASE )-> int: """simple docstring""" @contextmanager def _temporary_repo(SCREAMING_SNAKE_CASE ): try: yield repo_id finally: cleanup_repo(SCREAMING_SNAKE_CASE ) return _temporary_repo @pytest.fixture(scope='''session''' ) def __lowerCAmelCase (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE )-> Any: """simple docstring""" snake_case_ = f'''repo_txt_data-{int(time.time() * 10E3 )}''' snake_case_ = f'''{CI_HUB_USER}/{repo_name}''' hf_api.create_repo(SCREAMING_SNAKE_CASE , token=SCREAMING_SNAKE_CASE , repo_type='''dataset''' , private=SCREAMING_SNAKE_CASE ) hf_api.upload_file( token=SCREAMING_SNAKE_CASE , path_or_fileobj=str(SCREAMING_SNAKE_CASE ) , path_in_repo='''data/text_data.txt''' , repo_id=SCREAMING_SNAKE_CASE , repo_type='''dataset''' , ) yield repo_id try: hf_api.delete_repo(SCREAMING_SNAKE_CASE , token=SCREAMING_SNAKE_CASE , repo_type='''dataset''' ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __lowerCAmelCase (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE )-> List[Any]: """simple docstring""" return hf_private_dataset_repo_txt_data_ @pytest.fixture(scope='''session''' ) def __lowerCAmelCase (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE )-> Optional[int]: """simple docstring""" snake_case_ = f'''repo_zipped_txt_data-{int(time.time() * 10E3 )}''' snake_case_ = f'''{CI_HUB_USER}/{repo_name}''' hf_api.create_repo(SCREAMING_SNAKE_CASE , token=SCREAMING_SNAKE_CASE , repo_type='''dataset''' , private=SCREAMING_SNAKE_CASE ) hf_api.upload_file( token=SCREAMING_SNAKE_CASE , path_or_fileobj=str(SCREAMING_SNAKE_CASE ) , path_in_repo='''data.zip''' , repo_id=SCREAMING_SNAKE_CASE , repo_type='''dataset''' , ) yield repo_id try: hf_api.delete_repo(SCREAMING_SNAKE_CASE , token=SCREAMING_SNAKE_CASE , repo_type='''dataset''' ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __lowerCAmelCase (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE )-> Optional[Any]: """simple docstring""" return hf_private_dataset_repo_zipped_txt_data_ @pytest.fixture(scope='''session''' ) def __lowerCAmelCase (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE )-> Tuple: """simple docstring""" snake_case_ = f'''repo_zipped_img_data-{int(time.time() * 10E3 )}''' snake_case_ = f'''{CI_HUB_USER}/{repo_name}''' hf_api.create_repo(SCREAMING_SNAKE_CASE , token=SCREAMING_SNAKE_CASE , repo_type='''dataset''' , private=SCREAMING_SNAKE_CASE ) hf_api.upload_file( token=SCREAMING_SNAKE_CASE , path_or_fileobj=str(SCREAMING_SNAKE_CASE ) , path_in_repo='''data.zip''' , repo_id=SCREAMING_SNAKE_CASE , repo_type='''dataset''' , ) yield repo_id try: hf_api.delete_repo(SCREAMING_SNAKE_CASE , token=SCREAMING_SNAKE_CASE , repo_type='''dataset''' ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __lowerCAmelCase (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE )-> List[str]: """simple docstring""" return hf_private_dataset_repo_zipped_img_data_
370
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import TransformeraDModel, VQDiffusionPipeline, VQDiffusionScheduler, VQModel from diffusers.pipelines.vq_diffusion.pipeline_vq_diffusion import LearnedClassifierFreeSamplingEmbeddings from diffusers.utils import load_numpy, slow, torch_device from diffusers.utils.testing_utils import require_torch_gpu UpperCAmelCase = False class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def UpperCamelCase__ ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def UpperCamelCase__ ( self ): return 12 @property def UpperCamelCase__ ( self ): return 12 @property def UpperCamelCase__ ( self ): return 32 @property def UpperCamelCase__ ( self ): torch.manual_seed(0 ) snake_case_ = VQModel( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=3 , num_vq_embeddings=self.num_embed , vq_embed_dim=3 , ) return model @property def UpperCamelCase__ ( self ): snake_case_ = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) return tokenizer @property def UpperCamelCase__ ( self ): torch.manual_seed(0 ) snake_case_ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=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=10_00 , ) return CLIPTextModel(_UpperCAmelCase ) @property def UpperCamelCase__ ( self ): torch.manual_seed(0 ) snake_case_ = 12 snake_case_ = 12 snake_case_ = { '''attention_bias''': True, '''cross_attention_dim''': 32, '''attention_head_dim''': height * width, '''num_attention_heads''': 1, '''num_vector_embeds''': self.num_embed, '''num_embeds_ada_norm''': self.num_embeds_ada_norm, '''norm_num_groups''': 32, '''sample_size''': width, '''activation_fn''': '''geglu-approximate''', } snake_case_ = TransformeraDModel(**_UpperCAmelCase ) return model def UpperCamelCase__ ( self ): snake_case_ = '''cpu''' snake_case_ = self.dummy_vqvae snake_case_ = self.dummy_text_encoder snake_case_ = self.dummy_tokenizer snake_case_ = self.dummy_transformer snake_case_ = VQDiffusionScheduler(self.num_embed ) snake_case_ = LearnedClassifierFreeSamplingEmbeddings(learnable=_UpperCAmelCase ) snake_case_ = VQDiffusionPipeline( vqvae=_UpperCAmelCase , text_encoder=_UpperCAmelCase , tokenizer=_UpperCAmelCase , transformer=_UpperCAmelCase , scheduler=_UpperCAmelCase , learned_classifier_free_sampling_embeddings=_UpperCAmelCase , ) snake_case_ = pipe.to(_UpperCAmelCase ) pipe.set_progress_bar_config(disable=_UpperCAmelCase ) snake_case_ = '''teddy bear playing in the pool''' snake_case_ = torch.Generator(device=_UpperCAmelCase ).manual_seed(0 ) snake_case_ = pipe([prompt] , generator=_UpperCAmelCase , num_inference_steps=2 , output_type='''np''' ) snake_case_ = output.images snake_case_ = torch.Generator(device=_UpperCAmelCase ).manual_seed(0 ) snake_case_ = pipe( [prompt] , generator=_UpperCAmelCase , output_type='''np''' , return_dict=_UpperCAmelCase , num_inference_steps=2 )[0] snake_case_ = image[0, -3:, -3:, -1] snake_case_ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 24, 24, 3) snake_case_ = np.array([0.6_551, 0.6_168, 0.5_008, 0.5_676, 0.5_659, 0.4_295, 0.6_073, 0.5_599, 0.4_992] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCamelCase__ ( self ): snake_case_ = '''cpu''' snake_case_ = self.dummy_vqvae snake_case_ = self.dummy_text_encoder snake_case_ = self.dummy_tokenizer snake_case_ = self.dummy_transformer snake_case_ = VQDiffusionScheduler(self.num_embed ) snake_case_ = LearnedClassifierFreeSamplingEmbeddings( learnable=_UpperCAmelCase , hidden_size=self.text_embedder_hidden_size , length=tokenizer.model_max_length ) snake_case_ = VQDiffusionPipeline( vqvae=_UpperCAmelCase , text_encoder=_UpperCAmelCase , tokenizer=_UpperCAmelCase , transformer=_UpperCAmelCase , scheduler=_UpperCAmelCase , learned_classifier_free_sampling_embeddings=_UpperCAmelCase , ) snake_case_ = pipe.to(_UpperCAmelCase ) pipe.set_progress_bar_config(disable=_UpperCAmelCase ) snake_case_ = '''teddy bear playing in the pool''' snake_case_ = torch.Generator(device=_UpperCAmelCase ).manual_seed(0 ) snake_case_ = pipe([prompt] , generator=_UpperCAmelCase , num_inference_steps=2 , output_type='''np''' ) snake_case_ = output.images snake_case_ = torch.Generator(device=_UpperCAmelCase ).manual_seed(0 ) snake_case_ = pipe( [prompt] , generator=_UpperCAmelCase , output_type='''np''' , return_dict=_UpperCAmelCase , num_inference_steps=2 )[0] snake_case_ = image[0, -3:, -3:, -1] snake_case_ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 24, 24, 3) snake_case_ = np.array([0.6_693, 0.6_075, 0.4_959, 0.5_701, 0.5_583, 0.4_333, 0.6_171, 0.5_684, 0.4_988] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 2.0 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 @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 ): snake_case_ = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/vq_diffusion/teddy_bear_pool_classifier_free_sampling.npy''' ) snake_case_ = VQDiffusionPipeline.from_pretrained('''microsoft/vq-diffusion-ithq''' ) snake_case_ = pipeline.to(_UpperCAmelCase ) pipeline.set_progress_bar_config(disable=_UpperCAmelCase ) # requires GPU generator for gumbel softmax # don't use GPU generator in tests though snake_case_ = torch.Generator(device=_UpperCAmelCase ).manual_seed(0 ) snake_case_ = pipeline( '''teddy bear playing in the pool''' , num_images_per_prompt=1 , generator=_UpperCAmelCase , output_type='''np''' , ) snake_case_ = output.images[0] assert image.shape == (2_56, 2_56, 3) assert np.abs(expected_image - image ).max() < 2.0
267
0
"""simple docstring""" import requests from bsa import BeautifulSoup def __lowerCAmelCase (_UpperCamelCase = "https://www.worldometers.info/coronavirus" ): __lowerCAmelCase : Optional[int] = BeautifulSoup(requests.get(_UpperCamelCase ).text , 'html.parser' ) __lowerCAmelCase : str = soup.findAll('h1' ) __lowerCAmelCase : str = soup.findAll('div' , {'class': 'maincounter-number'} ) keys += soup.findAll('span' , {'class': 'panel-title'} ) values += soup.findAll('div' , {'class': 'number-table-main'} ) return {key.text.strip(): value.text.strip() for key, value in zip(_UpperCamelCase , _UpperCamelCase )} if __name__ == "__main__": print("""\033[1m""" + """COVID-19 Status of the World""" + """\033[0m\n""") for key, value in world_covidaa_stats().items(): print(f'{key}\n{value}\n')
86
import collections import os from typing import List, Optional, Tuple from transformers.utils import is_jieba_available, requires_backends if is_jieba_available(): import jieba from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _UpperCAmelCase : List[Any] = logging.get_logger(__name__) _UpperCAmelCase : int = {"vocab_file": "vocab.txt"} _UpperCAmelCase : str = { "vocab_file": { "openbmb/cpm-ant-10b": "https://huggingface.co/openbmb/cpm-ant-10b/blob/main/vocab.txt", }, } _UpperCAmelCase : Optional[Any] = { "openbmb/cpm-ant-10b": 1_024, } def A ( lowercase ) -> Dict: '''simple docstring''' UpperCamelCase = collections.OrderedDict() with open(lowercase , 'r' , encoding='utf-8' ) as reader: UpperCamelCase = reader.readlines() for index, token in enumerate(lowercase ): UpperCamelCase = token.rstrip('\n' ) UpperCamelCase = index return vocab class lowercase ( _SCREAMING_SNAKE_CASE ): def __init__( self , A_ , A_="<unk>" , A_=200 ) -> Dict: """simple docstring""" UpperCamelCase = vocab UpperCamelCase = unk_token UpperCamelCase = max_input_chars_per_word def __UpperCamelCase ( self , A_ ) -> List[Any]: """simple docstring""" UpperCamelCase = list(A_ ) if len(A_ ) > self.max_input_chars_per_word: return [self.unk_token] UpperCamelCase = 0 UpperCamelCase = [] while start < len(A_ ): UpperCamelCase = len(A_ ) UpperCamelCase = None while start < end: UpperCamelCase = ''.join(chars[start:end] ) if substr in self.vocab: UpperCamelCase = substr break end -= 1 if cur_substr is None: sub_tokens.append(self.unk_token ) start += 1 else: sub_tokens.append(A_ ) UpperCamelCase = end return sub_tokens class lowercase ( _SCREAMING_SNAKE_CASE ): __lowercase : List[str] = VOCAB_FILES_NAMES __lowercase : Dict = PRETRAINED_VOCAB_FILES_MAP __lowercase : int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Any = ["input_ids", "attention_mask"] __lowercase : Tuple = False def __init__( self , A_ , A_="<d>" , A_="</d>" , A_="<s>" , A_="</s>" , A_="<pad>" , A_="<unk>" , A_="</n>" , A_="</_>" , A_="left" , **A_ , ) -> Tuple: """simple docstring""" requires_backends(self , ['jieba'] ) super().__init__( bod_token=A_ , eod_token=A_ , bos_token=A_ , eos_token=A_ , pad_token=A_ , unk_token=A_ , line_token=A_ , space_token=A_ , padding_side=A_ , **A_ , ) UpperCamelCase = bod_token UpperCamelCase = eod_token UpperCamelCase = load_vocab(A_ ) UpperCamelCase = self.encoder[space_token] UpperCamelCase = self.encoder[line_token] del self.encoder[space_token] del self.encoder[line_token] UpperCamelCase = collections.OrderedDict(sorted(self.encoder.items() , key=lambda A_ : x[1] ) ) UpperCamelCase = {v: k for k, v in self.encoder.items()} UpperCamelCase = WordpieceTokenizer(vocab=self.encoder , unk_token=self.unk_token ) @property def __UpperCamelCase ( self ) -> Optional[int]: """simple docstring""" return self.encoder[self.bod_token] @property def __UpperCamelCase ( self ) -> Tuple: """simple docstring""" return self.encoder[self.eod_token] @property def __UpperCamelCase ( self ) -> Union[str, Any]: """simple docstring""" return self.encoder["\n"] @property def __UpperCamelCase ( self ) -> int: """simple docstring""" return len(self.encoder ) def __UpperCamelCase ( self ) -> List[Any]: """simple docstring""" return dict(self.encoder , **self.added_tokens_encoder ) def __UpperCamelCase ( self , A_ ) -> List[Any]: """simple docstring""" UpperCamelCase = [] for x in jieba.cut(A_ , cut_all=A_ ): output_tokens.extend(self.wordpiece_tokenizer.tokenize(A_ ) ) return output_tokens def __UpperCamelCase ( self , A_ , **A_ ) -> Dict: """simple docstring""" UpperCamelCase = [i for i in token_ids if i >= 0] UpperCamelCase = [ x for x in token_ids if x != self.pad_token_id and x != self.eos_token_id and x != self.bos_token_id ] return super()._decode(A_ , **A_ ) def __UpperCamelCase ( self , A_ ) -> int: """simple docstring""" return token in self.encoder def __UpperCamelCase ( self , A_ ) -> str: """simple docstring""" return "".join(A_ ) def __UpperCamelCase ( self , A_ ) -> Tuple: """simple docstring""" return self.encoder.get(A_ , self.encoder.get(self.unk_token ) ) def __UpperCamelCase ( self , A_ ) -> Optional[Any]: """simple docstring""" return self.decoder.get(A_ , self.unk_token ) def __UpperCamelCase ( self , A_ , A_ = None ) -> Tuple[str]: """simple docstring""" if os.path.isdir(A_ ): UpperCamelCase = os.path.join( A_ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) else: UpperCamelCase = (filename_prefix + '-' if filename_prefix else '') + save_directory UpperCamelCase = 0 if " " in self.encoder: UpperCamelCase = self.encoder[' '] del self.encoder[" "] if "\n" in self.encoder: UpperCamelCase = self.encoder['\n'] del self.encoder["\n"] UpperCamelCase = collections.OrderedDict(sorted(self.encoder.items() , key=lambda A_ : x[1] ) ) with open(A_ , 'w' , encoding='utf-8' ) as writer: for token, token_index in self.encoder.items(): if index != token_index: logger.warning( F'''Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.''' ' Please check that the vocabulary is not corrupted!' ) UpperCamelCase = token_index writer.write(token + '\n' ) index += 1 return (vocab_file,) def __UpperCamelCase ( self , A_ , A_ = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return [self.bos_token_id] + token_ids_a return [self.bos_token_id] + token_ids_a + [self.bos_token_id] + token_ids_a def __UpperCamelCase ( self , A_ , A_ = None , A_ = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A_ , token_ids_a=A_ , already_has_special_tokens=A_ ) if token_ids_a is not None: return [1] + ([0] * len(A_ )) + [1] + ([0] * len(A_ )) return [1] + ([0] * len(A_ ))
222
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) snake_case : Tuple = { "configuration_funnel": ["FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP", "FunnelConfig"], "convert_funnel_original_tf_checkpoint_to_pytorch": [], "tokenization_funnel": ["FunnelTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case : Union[str, Any] = ["FunnelTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case : Dict = [ "FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "FunnelBaseModel", "FunnelForMaskedLM", "FunnelForMultipleChoice", "FunnelForPreTraining", "FunnelForQuestionAnswering", "FunnelForSequenceClassification", "FunnelForTokenClassification", "FunnelModel", "FunnelPreTrainedModel", "load_tf_weights_in_funnel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case : Dict = [ "TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "TFFunnelBaseModel", "TFFunnelForMaskedLM", "TFFunnelForMultipleChoice", "TFFunnelForPreTraining", "TFFunnelForQuestionAnswering", "TFFunnelForSequenceClassification", "TFFunnelForTokenClassification", "TFFunnelModel", "TFFunnelPreTrainedModel", ] if TYPE_CHECKING: from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig from .tokenization_funnel import FunnelTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_funnel_fast import FunnelTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_funnel import ( FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, FunnelBaseModel, FunnelForMaskedLM, FunnelForMultipleChoice, FunnelForPreTraining, FunnelForQuestionAnswering, FunnelForSequenceClassification, FunnelForTokenClassification, FunnelModel, FunnelPreTrainedModel, load_tf_weights_in_funnel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_funnel import ( TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, TFFunnelPreTrainedModel, ) else: import sys snake_case : int = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
362
import re import string import numpy as np import datasets snake_case : Any = "\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n" snake_case : Optional[Any] = "\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric(\"exact_match\")\n >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results[\"exact_match\"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric(\"exact_match\")\n >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results[\"exact_match\"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric(\"exact_match\")\n >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results[\"exact_match\"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric(\"exact_match\")\n >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results[\"exact_match\"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric(\"exact_match\")\n >>> refs = [\"The cat sat on the mat.\", \"Theaters are great.\", \"It's like comparing oranges and apples.\"]\n >>> preds = [\"The cat sat on the mat?\", \"Theaters are great.\", \"It's like comparing apples and oranges.\"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results[\"exact_match\"], 1))\n 33.3\n\n" snake_case : Union[str, Any] = "\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _snake_case ( datasets.Metric ): def SCREAMING_SNAKE_CASE ( self ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , reference_urls=[] , ) def SCREAMING_SNAKE_CASE ( self , _a , _a , _a=None , _a=False , _a=False , _a=False , ): if regexes_to_ignore is not None: for s in regexes_to_ignore: __magic_name__ : Any = np.array([re.sub(_a , "" , _a ) for x in predictions] ) __magic_name__ : Tuple = np.array([re.sub(_a , "" , _a ) for x in references] ) else: __magic_name__ : Union[str, Any] = np.asarray(_a ) __magic_name__ : List[Any] = np.asarray(_a ) if ignore_case: __magic_name__ : List[Any] = np.char.lower(_a ) __magic_name__ : Optional[int] = np.char.lower(_a ) if ignore_punctuation: __magic_name__ : Optional[Any] = string.punctuation.maketrans("" , "" , string.punctuation ) __magic_name__ : int = np.char.translate(_a , table=_a ) __magic_name__ : Optional[Any] = np.char.translate(_a , table=_a ) if ignore_numbers: __magic_name__ : Optional[Any] = string.digits.maketrans("" , "" , string.digits ) __magic_name__ : Any = np.char.translate(_a , table=_a ) __magic_name__ : List[str] = np.char.translate(_a , table=_a ) __magic_name__ : Dict = predictions == references return {"exact_match": np.mean(_a ) * 100}
41
0
"""simple docstring""" from __future__ import annotations def A_ ( _lowercase ): '''simple docstring''' if len(_lowercase ) == 0: return [] snake_case_, snake_case_ :Tuple = min(_lowercase ), max(_lowercase ) snake_case_ :Tuple = int(max_value - min_value ) + 1 snake_case_ :list[list] = [[] for _ in range(_lowercase )] for i in my_list: buckets[int(i - min_value )].append(_lowercase ) return [v for bucket in buckets for v in sorted(_lowercase )] if __name__ == "__main__": from doctest import testmod testmod() assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]
66
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) __a = { "configuration_mobilebert": [ "MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MobileBertConfig", "MobileBertOnnxConfig", ], "tokenization_mobilebert": ["MobileBertTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ["MobileBertTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ "MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "MobileBertForMaskedLM", "MobileBertForMultipleChoice", "MobileBertForNextSentencePrediction", "MobileBertForPreTraining", "MobileBertForQuestionAnswering", "MobileBertForSequenceClassification", "MobileBertForTokenClassification", "MobileBertLayer", "MobileBertModel", "MobileBertPreTrainedModel", "load_tf_weights_in_mobilebert", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ "TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFMobileBertForMaskedLM", "TFMobileBertForMultipleChoice", "TFMobileBertForNextSentencePrediction", "TFMobileBertForPreTraining", "TFMobileBertForQuestionAnswering", "TFMobileBertForSequenceClassification", "TFMobileBertForTokenClassification", "TFMobileBertMainLayer", "TFMobileBertModel", "TFMobileBertPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mobilebert import ( MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileBertConfig, MobileBertOnnxConfig, ) from .tokenization_mobilebert import MobileBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mobilebert_fast import MobileBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilebert import ( MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, MobileBertForMaskedLM, MobileBertForMultipleChoice, MobileBertForNextSentencePrediction, MobileBertForPreTraining, MobileBertForQuestionAnswering, MobileBertForSequenceClassification, MobileBertForTokenClassification, MobileBertLayer, MobileBertModel, MobileBertPreTrainedModel, load_tf_weights_in_mobilebert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mobilebert import ( TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFMobileBertForMaskedLM, TFMobileBertForMultipleChoice, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertMainLayer, TFMobileBertModel, TFMobileBertPreTrainedModel, ) else: import sys __a = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
66
1
from __future__ import annotations def lowerCamelCase__ (__lowerCamelCase, __lowerCamelCase ): if len(__lowerCamelCase ) == 0: return False _SCREAMING_SNAKE_CASE : List[Any] = len(__lowerCamelCase ) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint], __lowerCamelCase ) else: return binary_search(a_list[midpoint + 1 :], __lowerCamelCase ) if __name__ == "__main__": UpperCamelCase__ =input('Enter numbers separated by comma:\n').strip() UpperCamelCase__ =[int(item.strip()) for item in user_input.split(',')] UpperCamelCase__ =int(input('Enter the number to be found in the list:\n').strip()) UpperCamelCase__ ='' if binary_search(sequence, target) else 'not ' print(f"{target} was {not_str}found in {sequence}")
371
import math import torch from torch import nn from ..configuration_utils import ConfigMixin, register_to_config from .attention_processor import Attention from .embeddings import get_timestep_embedding from .modeling_utils import ModelMixin class lowerCAmelCase__( __lowercase , __lowercase ): '''simple docstring''' @register_to_config def __init__( self , __lowerCamelCase = 1_2_8 , __lowerCamelCase = 2_5_6 , __lowerCamelCase = 2000.0 , __lowerCamelCase = 7_6_8 , __lowerCamelCase = 1_2 , __lowerCamelCase = 1_2 , __lowerCamelCase = 6_4 , __lowerCamelCase = 2_0_4_8 , __lowerCamelCase = 0.1 , ) -> int: super().__init__() _SCREAMING_SNAKE_CASE : Optional[int] = nn.Sequential( nn.Linear(__lowerCamelCase , d_model * 4 , bias=__lowerCamelCase ) , nn.SiLU() , nn.Linear(d_model * 4 , d_model * 4 , bias=__lowerCamelCase ) , nn.SiLU() , ) _SCREAMING_SNAKE_CASE : str = nn.Embedding(__lowerCamelCase , __lowerCamelCase ) _SCREAMING_SNAKE_CASE : Optional[Any] = False _SCREAMING_SNAKE_CASE : Optional[Any] = nn.Linear(__lowerCamelCase , __lowerCamelCase , bias=__lowerCamelCase ) _SCREAMING_SNAKE_CASE : Optional[Any] = nn.Dropout(p=__lowerCamelCase ) _SCREAMING_SNAKE_CASE : Optional[int] = nn.ModuleList() for lyr_num in range(__lowerCamelCase ): # FiLM conditional T5 decoder _SCREAMING_SNAKE_CASE : Optional[int] = DecoderLayer(d_model=__lowerCamelCase , d_kv=__lowerCamelCase , num_heads=__lowerCamelCase , d_ff=__lowerCamelCase , dropout_rate=__lowerCamelCase ) self.decoders.append(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : Tuple = TaLayerNorm(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : str = nn.Dropout(p=__lowerCamelCase ) _SCREAMING_SNAKE_CASE : List[Any] = nn.Linear(__lowerCamelCase , __lowerCamelCase , bias=__lowerCamelCase ) def UpperCamelCase_ ( self , __lowerCamelCase , __lowerCamelCase ) -> List[Any]: _SCREAMING_SNAKE_CASE : int = torch.mul(query_input.unsqueeze(-1 ) , key_input.unsqueeze(-2 ) ) return mask.unsqueeze(-3 ) def UpperCamelCase_ ( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) -> Dict: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : str = decoder_input_tokens.shape assert decoder_noise_time.shape == (batch,) # decoder_noise_time is in [0, 1), so rescale to expected timing range. _SCREAMING_SNAKE_CASE : Tuple = get_timestep_embedding( decoder_noise_time * self.config.max_decoder_noise_time , embedding_dim=self.config.d_model , max_period=self.config.max_decoder_noise_time , ).to(dtype=self.dtype ) _SCREAMING_SNAKE_CASE : str = self.conditioning_emb(__lowerCamelCase ).unsqueeze(1 ) assert conditioning_emb.shape == (batch, 1, self.config.d_model * 4) _SCREAMING_SNAKE_CASE : Tuple = decoder_input_tokens.shape[1] # If we want to use relative positions for audio context, we can just offset # this sequence by the length of encodings_and_masks. _SCREAMING_SNAKE_CASE : Optional[int] = torch.broadcast_to( torch.arange(__lowerCamelCase , device=decoder_input_tokens.device ) , (batch, seq_length) , ) _SCREAMING_SNAKE_CASE : Union[str, Any] = self.position_encoding(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : str = self.continuous_inputs_projection(__lowerCamelCase ) inputs += position_encodings _SCREAMING_SNAKE_CASE : Any = self.dropout(__lowerCamelCase ) # decoder: No padding present. _SCREAMING_SNAKE_CASE : Any = torch.ones( decoder_input_tokens.shape[:2] , device=decoder_input_tokens.device , dtype=inputs.dtype ) # Translate encoding masks to encoder-decoder masks. _SCREAMING_SNAKE_CASE : List[str] = [(x, self.encoder_decoder_mask(__lowerCamelCase , __lowerCamelCase )) for x, y in encodings_and_masks] # cross attend style: concat encodings _SCREAMING_SNAKE_CASE : Tuple = torch.cat([x[0] for x in encodings_and_encdec_masks] , dim=1 ) _SCREAMING_SNAKE_CASE : Union[str, Any] = torch.cat([x[1] for x in encodings_and_encdec_masks] , dim=-1 ) for lyr in self.decoders: _SCREAMING_SNAKE_CASE : Optional[Any] = lyr( __lowerCamelCase , conditioning_emb=__lowerCamelCase , encoder_hidden_states=__lowerCamelCase , encoder_attention_mask=__lowerCamelCase , )[0] _SCREAMING_SNAKE_CASE : int = self.decoder_norm(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : Dict = self.post_dropout(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : int = self.spec_out(__lowerCamelCase ) return spec_out class lowerCAmelCase__( nn.Module ): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase=1E-6 ) -> Dict: super().__init__() _SCREAMING_SNAKE_CASE : Optional[int] = nn.ModuleList() # cond self attention: layer 0 self.layer.append( TaLayerSelfAttentionCond(d_model=__lowerCamelCase , d_kv=__lowerCamelCase , num_heads=__lowerCamelCase , dropout_rate=__lowerCamelCase ) ) # cross attention: layer 1 self.layer.append( TaLayerCrossAttention( d_model=__lowerCamelCase , d_kv=__lowerCamelCase , num_heads=__lowerCamelCase , dropout_rate=__lowerCamelCase , layer_norm_epsilon=__lowerCamelCase , ) ) # Film Cond MLP + dropout: last layer self.layer.append( TaLayerFFCond(d_model=__lowerCamelCase , d_ff=__lowerCamelCase , dropout_rate=__lowerCamelCase , layer_norm_epsilon=__lowerCamelCase ) ) def UpperCamelCase_ ( self , __lowerCamelCase , __lowerCamelCase=None , __lowerCamelCase=None , __lowerCamelCase=None , __lowerCamelCase=None , __lowerCamelCase=None , ) -> Union[str, Any]: _SCREAMING_SNAKE_CASE : int = self.layer[0]( __lowerCamelCase , conditioning_emb=__lowerCamelCase , attention_mask=__lowerCamelCase , ) if encoder_hidden_states is not None: _SCREAMING_SNAKE_CASE : str = torch.where(encoder_attention_mask > 0 , 0 , -1E10 ).to( encoder_hidden_states.dtype ) _SCREAMING_SNAKE_CASE : Tuple = self.layer[1]( __lowerCamelCase , key_value_states=__lowerCamelCase , attention_mask=__lowerCamelCase , ) # Apply Film Conditional Feed Forward layer _SCREAMING_SNAKE_CASE : Optional[Any] = self.layer[-1](__lowerCamelCase , __lowerCamelCase ) return (hidden_states,) class lowerCAmelCase__( nn.Module ): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) -> Optional[Any]: super().__init__() _SCREAMING_SNAKE_CASE : List[str] = TaLayerNorm(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : Any = TaFiLMLayer(in_features=d_model * 4 , out_features=__lowerCamelCase ) _SCREAMING_SNAKE_CASE : str = Attention(query_dim=__lowerCamelCase , heads=__lowerCamelCase , dim_head=__lowerCamelCase , out_bias=__lowerCamelCase , scale_qk=__lowerCamelCase ) _SCREAMING_SNAKE_CASE : Optional[Any] = nn.Dropout(__lowerCamelCase ) def UpperCamelCase_ ( self , __lowerCamelCase , __lowerCamelCase=None , __lowerCamelCase=None , ) -> Union[str, Any]: # pre_self_attention_layer_norm _SCREAMING_SNAKE_CASE : int = self.layer_norm(__lowerCamelCase ) if conditioning_emb is not None: _SCREAMING_SNAKE_CASE : Any = self.FiLMLayer(__lowerCamelCase , __lowerCamelCase ) # Self-attention block _SCREAMING_SNAKE_CASE : Optional[int] = self.attention(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : Dict = hidden_states + self.dropout(__lowerCamelCase ) return hidden_states class lowerCAmelCase__( nn.Module ): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) -> List[str]: super().__init__() _SCREAMING_SNAKE_CASE : Optional[Any] = Attention(query_dim=__lowerCamelCase , heads=__lowerCamelCase , dim_head=__lowerCamelCase , out_bias=__lowerCamelCase , scale_qk=__lowerCamelCase ) _SCREAMING_SNAKE_CASE : List[str] = TaLayerNorm(__lowerCamelCase , eps=__lowerCamelCase ) _SCREAMING_SNAKE_CASE : Optional[Any] = nn.Dropout(__lowerCamelCase ) def UpperCamelCase_ ( self , __lowerCamelCase , __lowerCamelCase=None , __lowerCamelCase=None , ) -> List[Any]: _SCREAMING_SNAKE_CASE : Tuple = self.layer_norm(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : str = self.attention( __lowerCamelCase , encoder_hidden_states=__lowerCamelCase , attention_mask=attention_mask.squeeze(1 ) , ) _SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_states + self.dropout(__lowerCamelCase ) return layer_output class lowerCAmelCase__( nn.Module ): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) -> List[Any]: super().__init__() _SCREAMING_SNAKE_CASE : Tuple = TaDenseGatedActDense(d_model=__lowerCamelCase , d_ff=__lowerCamelCase , dropout_rate=__lowerCamelCase ) _SCREAMING_SNAKE_CASE : str = TaFiLMLayer(in_features=d_model * 4 , out_features=__lowerCamelCase ) _SCREAMING_SNAKE_CASE : str = TaLayerNorm(__lowerCamelCase , eps=__lowerCamelCase ) _SCREAMING_SNAKE_CASE : Optional[Any] = nn.Dropout(__lowerCamelCase ) def UpperCamelCase_ ( self , __lowerCamelCase , __lowerCamelCase=None ) -> List[str]: _SCREAMING_SNAKE_CASE : Optional[int] = self.layer_norm(__lowerCamelCase ) if conditioning_emb is not None: _SCREAMING_SNAKE_CASE : Union[str, Any] = self.film(__lowerCamelCase , __lowerCamelCase ) _SCREAMING_SNAKE_CASE : Any = self.DenseReluDense(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : List[Any] = hidden_states + self.dropout(__lowerCamelCase ) return hidden_states class lowerCAmelCase__( nn.Module ): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) -> Optional[Any]: super().__init__() _SCREAMING_SNAKE_CASE : Union[str, Any] = nn.Linear(__lowerCamelCase , __lowerCamelCase , bias=__lowerCamelCase ) _SCREAMING_SNAKE_CASE : List[str] = nn.Linear(__lowerCamelCase , __lowerCamelCase , bias=__lowerCamelCase ) _SCREAMING_SNAKE_CASE : Dict = nn.Linear(__lowerCamelCase , __lowerCamelCase , bias=__lowerCamelCase ) _SCREAMING_SNAKE_CASE : Union[str, Any] = nn.Dropout(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : Union[str, Any] = NewGELUActivation() def UpperCamelCase_ ( self , __lowerCamelCase ) -> Any: _SCREAMING_SNAKE_CASE : Dict = self.act(self.wi_a(__lowerCamelCase ) ) _SCREAMING_SNAKE_CASE : Dict = self.wi_a(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : int = hidden_gelu * hidden_linear _SCREAMING_SNAKE_CASE : Optional[int] = self.dropout(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : Optional[int] = self.wo(__lowerCamelCase ) return hidden_states class lowerCAmelCase__( nn.Module ): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase=1E-6 ) -> int: super().__init__() _SCREAMING_SNAKE_CASE : Dict = nn.Parameter(torch.ones(__lowerCamelCase ) ) _SCREAMING_SNAKE_CASE : str = eps def UpperCamelCase_ ( self , __lowerCamelCase ) -> Optional[Any]: # T5 uses a layer_norm which only scales and doesn't shift, which is also known as Root Mean # Square Layer Normalization https://arxiv.org/abs/1910.07467 thus variance is calculated # w/o mean and there is no bias. Additionally we want to make sure that the accumulation for # half-precision inputs is done in fp32 _SCREAMING_SNAKE_CASE : Tuple = hidden_states.to(torch.floataa ).pow(2 ).mean(-1 , keepdim=__lowerCamelCase ) _SCREAMING_SNAKE_CASE : List[Any] = hidden_states * torch.rsqrt(variance + self.variance_epsilon ) # convert into half-precision if necessary if self.weight.dtype in [torch.floataa, torch.bfloataa]: _SCREAMING_SNAKE_CASE : str = hidden_states.to(self.weight.dtype ) return self.weight * hidden_states class lowerCAmelCase__( nn.Module ): '''simple docstring''' def UpperCamelCase_ ( self , __lowerCamelCase ) -> torch.Tensor: return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi ) * (input + 0.04_4715 * torch.pow(__lowerCamelCase , 3.0 )) )) class lowerCAmelCase__( nn.Module ): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase ) -> Optional[Any]: super().__init__() _SCREAMING_SNAKE_CASE : Any = nn.Linear(__lowerCamelCase , out_features * 2 , bias=__lowerCamelCase ) def UpperCamelCase_ ( self , __lowerCamelCase , __lowerCamelCase ) -> Dict: _SCREAMING_SNAKE_CASE : List[Any] = self.scale_bias(__lowerCamelCase ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Dict = torch.chunk(__lowerCamelCase , 2 , -1 ) _SCREAMING_SNAKE_CASE : Optional[int] = x * (1 + scale) + shift return x
325
0
import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def _a ( UpperCamelCase_ : Dict ) -> Dict: """simple docstring""" lowerCAmelCase__ = {} lowerCAmelCase__ = tokenizer(example["content"] , truncation=UpperCamelCase_ )["input_ids"] lowerCAmelCase__ = len(example["content"] ) / len(output["input_ids"] ) return output a_ = HfArgumentParser(PretokenizationArguments) a_ = parser.parse_args() if args.num_workers is None: a_ = multiprocessing.cpu_count() a_ = AutoTokenizer.from_pretrained(args.tokenizer_dir) a_ = time.time() a_ = load_dataset(args.dataset_name, split='''train''') print(F"Dataset loaded in {time.time()-t_start:.2f}s") a_ = time.time() a_ = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ '''repo_name''', '''path''', '''copies''', '''size''', '''content''', '''license''', '''hash''', '''line_mean''', '''line_max''', '''alpha_frac''', '''autogenerated''', ], ) print(F"Dataset tokenized in {time.time()-t_start:.2f}s") a_ = time.time() ds.push_to_hub(args.tokenized_data_repo) print(F"Data pushed to the hub in {time.time()-t_start:.2f}s")
340
'''simple docstring''' # Copyright (c) 2021-, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #################################################################################################### # # Note: If when running this conversion script you're getting an exception: # ModuleNotFoundError: No module named 'megatron.model.enums' # you need to tell python where to find the clone of Megatron-LM, e.g.: # # cd /tmp # git clone https://github.com/NVIDIA/Megatron-LM # PYTHONPATH=/tmp/Megatron-LM python src/transformers/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py ... # # if you already have it cloned elsewhere, simply adjust the path to the existing path # # If the training was done using a Megatron-LM fork, e.g., # https://github.com/microsoft/Megatron-DeepSpeed/ then chances are that you need to have that one # in your path, i.e., /path/to/Megatron-DeepSpeed/ # import argparse import os import re import zipfile import torch from transformers import AutoTokenizer, GPTaConfig def a_ ( lowerCamelCase : int , lowerCamelCase : Optional[Any] , lowerCamelCase : Union[str, Any]=0 ): # Format the message. if name is None: lowerCAmelCase = None else: lowerCAmelCase = '.' * max(0 , spaces - 2 ) + '# {:' + str(50 - spaces ) + 's}' lowerCAmelCase = fmt.format(lowerCamelCase ) # Print and recurse (if needed). if isinstance(lowerCamelCase , lowerCamelCase ): if msg is not None: print(lowerCamelCase ) for k in val.keys(): recursive_print(lowerCamelCase , val[k] , spaces + 2 ) elif isinstance(lowerCamelCase , torch.Tensor ): print(lowerCamelCase , ':' , val.size() ) else: print(lowerCamelCase , ':' , lowerCamelCase ) def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : List[str] , lowerCamelCase : List[Any] , lowerCamelCase : Dict , lowerCamelCase : Tuple ): # Permutes layout of param tensor to [num_splits * num_heads * hidden_size, :] # for compatibility with later versions of NVIDIA Megatron-LM. # The inverse operation is performed inside Megatron-LM to read checkpoints: # https://github.com/NVIDIA/Megatron-LM/blob/v2.4/megatron/checkpointing.py#L209 # If param is the weight tensor of the self-attention block, the returned tensor # will have to be transposed one more time to be read by HuggingFace GPT2. lowerCAmelCase = param.size() if checkpoint_version == 1.0: # version 1.0 stores [num_heads * hidden_size * num_splits, :] lowerCAmelCase = (num_heads, hidden_size, num_splits) + input_shape[1:] lowerCAmelCase = param.view(*lowerCamelCase ) lowerCAmelCase = param.transpose(0 , 2 ) lowerCAmelCase = param.transpose(1 , 2 ).contiguous() elif checkpoint_version >= 2.0: # other versions store [num_heads * num_splits * hidden_size, :] lowerCAmelCase = (num_heads, num_splits, hidden_size) + input_shape[1:] lowerCAmelCase = param.view(*lowerCamelCase ) lowerCAmelCase = param.transpose(0 , 1 ).contiguous() lowerCAmelCase = param.view(*lowerCamelCase ) return param def a_ ( lowerCamelCase : Optional[int] , lowerCamelCase : Optional[int] , lowerCamelCase : str ): # The converted output model. lowerCAmelCase = {} # old versions did not store training args lowerCAmelCase = input_state_dict.get('args' , lowerCamelCase ) if ds_args is not None: # do not make the user write a config file when the exact dimensions/sizes are already in the checkpoint # from pprint import pprint # pprint(vars(ds_args)) lowerCAmelCase = ds_args.padded_vocab_size lowerCAmelCase = ds_args.max_position_embeddings lowerCAmelCase = ds_args.hidden_size lowerCAmelCase = ds_args.num_layers lowerCAmelCase = ds_args.num_attention_heads lowerCAmelCase = ds_args.ffn_hidden_size # pprint(config) # The number of heads. lowerCAmelCase = config.n_head # The hidden_size per head. lowerCAmelCase = config.n_embd // config.n_head # Megatron-LM checkpoint version if "checkpoint_version" in input_state_dict.keys(): lowerCAmelCase = input_state_dict['checkpoint_version'] else: lowerCAmelCase = 0.0 # The model. lowerCAmelCase = input_state_dict['model'] # The language model. lowerCAmelCase = model['language_model'] # The embeddings. lowerCAmelCase = lm['embedding'] # The word embeddings. lowerCAmelCase = embeddings['word_embeddings']['weight'] # Truncate the embedding table to vocab_size rows. lowerCAmelCase = word_embeddings[: config.vocab_size, :] lowerCAmelCase = word_embeddings # The position embeddings. lowerCAmelCase = embeddings['position_embeddings']['weight'] # Read the causal mask dimension (seqlen). [max_sequence_length, hidden_size] lowerCAmelCase = pos_embeddings.size(0 ) if n_positions != config.n_positions: raise ValueError( f'''pos_embeddings.max_sequence_length={n_positions} and config.n_positions={config.n_positions} don\'t match''' ) # Store the position embeddings. lowerCAmelCase = pos_embeddings # The transformer. lowerCAmelCase = lm['transformer'] if 'transformer' in lm.keys() else lm['encoder'] # The regex to extract layer names. lowerCAmelCase = re.compile(R'layers\.(\d+)\.([a-z0-9_.]+)\.([a-z]+)' ) # The simple map of names for "automated" rules. lowerCAmelCase = { 'attention.dense': '.attn.c_proj.', 'self_attention.dense': '.attn.c_proj.', 'mlp.dense_h_to_4h': '.mlp.c_fc.', 'mlp.dense_4h_to_h': '.mlp.c_proj.', } # Extract the layers. for key, val in transformer.items(): # Match the name. lowerCAmelCase = layer_re.match(lowerCamelCase ) # Stop if that's not a layer if m is None: break # The index of the layer. lowerCAmelCase = int(m.group(1 ) ) # The name of the operation. lowerCAmelCase = m.group(2 ) # Is it a weight or a bias? lowerCAmelCase = m.group(3 ) # The name of the layer. lowerCAmelCase = f'''transformer.h.{layer_idx}''' # For layernorm(s), simply store the layer norm. if op_name.endswith('layernorm' ): lowerCAmelCase = 'ln_1' if op_name.startswith('input' ) else 'ln_2' lowerCAmelCase = val # Transpose the QKV matrix. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "weight": # Insert a tensor of 1x1xDxD bias. lowerCAmelCase = torch.tril(torch.ones((n_positions, n_positions) , dtype=torch.floataa ) ).view( 1 , 1 , lowerCamelCase , lowerCamelCase ) lowerCAmelCase = causal_mask # Insert a "dummy" tensor for masked_bias. lowerCAmelCase = torch.tensor(-1e4 , dtype=torch.floataa ) lowerCAmelCase = masked_bias lowerCAmelCase = fix_query_key_value_ordering(lowerCamelCase , lowerCamelCase , 3 , lowerCamelCase , lowerCamelCase ) # Megatron stores (3*D) x D but transformers-GPT2 expects D x 3*D. lowerCAmelCase = out_val.transpose(0 , 1 ).contiguous() # Store. lowerCAmelCase = out_val # Transpose the bias. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "bias": lowerCAmelCase = fix_query_key_value_ordering(lowerCamelCase , lowerCamelCase , 3 , lowerCamelCase , lowerCamelCase ) # Store. No change of shape. lowerCAmelCase = out_val # Transpose the weights. elif weight_or_bias == "weight": lowerCAmelCase = megatron_to_transformers[op_name] lowerCAmelCase = val.transpose(0 , 1 ) # Copy the bias. elif weight_or_bias == "bias": lowerCAmelCase = megatron_to_transformers[op_name] lowerCAmelCase = val # DEBUG. assert config.n_layer == layer_idx + 1 # The final layernorm. lowerCAmelCase = transformer['final_layernorm.weight'] lowerCAmelCase = transformer['final_layernorm.bias'] # For LM head, transformers' wants the matrix to weight embeddings. lowerCAmelCase = word_embeddings # It should be done! return output_state_dict def a_ ( ): # Create the argument parser. lowerCAmelCase = argparse.ArgumentParser() parser.add_argument('--print-checkpoint-structure' , action='store_true' ) parser.add_argument( 'path_to_checkpoint' , type=lowerCamelCase , help='Path to the checkpoint file (.zip archive or direct .pt file)' , ) parser.add_argument( '--config_file' , default='' , type=lowerCamelCase , help='An optional config json file describing the pre-trained model.' , ) lowerCAmelCase = parser.parse_args() # Extract the basename. lowerCAmelCase = os.path.dirname(args.path_to_checkpoint ) # Load the model. # the .zip is very optional, let's keep it for backward compatibility print(f'''Extracting PyTorch state dictionary from {args.path_to_checkpoint}''' ) if args.path_to_checkpoint.endswith('.zip' ): with zipfile.ZipFile(args.path_to_checkpoint , 'r' ) as checkpoint: with checkpoint.open('release/mp_rank_00/model_optim_rng.pt' ) as pytorch_dict: lowerCAmelCase = torch.load(lowerCamelCase , map_location='cpu' ) else: lowerCAmelCase = torch.load(args.path_to_checkpoint , map_location='cpu' ) lowerCAmelCase = input_state_dict.get('args' , lowerCamelCase ) # Read the config, or default to the model released by NVIDIA. if args.config_file == "": if ds_args is not None: if ds_args.bias_gelu_fusion: lowerCAmelCase = 'gelu_fast' elif ds_args.openai_gelu: lowerCAmelCase = 'gelu_new' else: lowerCAmelCase = 'gelu' else: # in the very early days this used to be "gelu_new" lowerCAmelCase = 'gelu_new' # Spell out all parameters in case the defaults change. lowerCAmelCase = GPTaConfig( vocab_size=50257 , n_positions=1024 , n_embd=1024 , n_layer=24 , n_head=16 , n_inner=4096 , activation_function=lowerCamelCase , resid_pdrop=0.1 , embd_pdrop=0.1 , attn_pdrop=0.1 , layer_norm_epsilon=1e-5 , initializer_range=0.02 , summary_type='cls_index' , summary_use_proj=lowerCamelCase , summary_activation=lowerCamelCase , summary_proj_to_labels=lowerCamelCase , summary_first_dropout=0.1 , scale_attn_weights=lowerCamelCase , use_cache=lowerCamelCase , bos_token_id=50256 , eos_token_id=50256 , ) else: lowerCAmelCase = GPTaConfig.from_json_file(args.config_file ) lowerCAmelCase = ['GPT2LMHeadModel'] # Convert. print('Converting' ) lowerCAmelCase = convert_megatron_checkpoint(lowerCamelCase , lowerCamelCase , lowerCamelCase ) # Print the structure of converted state dict. if args.print_checkpoint_structure: recursive_print(lowerCamelCase , lowerCamelCase ) # Add tokenizer class info to config # see https://github.com/huggingface/transformers/issues/13906) if ds_args is not None: lowerCAmelCase = ds_args.tokenizer_type if tokenizer_type == "GPT2BPETokenizer": lowerCAmelCase = 'gpt2' elif tokenizer_type == "PretrainedFromHF": lowerCAmelCase = ds_args.tokenizer_name_or_path else: raise ValueError(f'''Unrecognized tokenizer_type {tokenizer_type}''' ) else: lowerCAmelCase = 'gpt2' lowerCAmelCase = AutoTokenizer.from_pretrained(lowerCamelCase ) lowerCAmelCase = type(lowerCamelCase ).__name__ lowerCAmelCase = tokenizer_class # Store the config to file. print('Saving config' ) config.save_pretrained(lowerCamelCase ) # Save tokenizer based on args print(f'''Adding {tokenizer_class} tokenizer files''' ) tokenizer.save_pretrained(lowerCamelCase ) # Store the state_dict to file. lowerCAmelCase = os.path.join(lowerCamelCase , 'pytorch_model.bin' ) print(f'''Saving checkpoint to "{output_checkpoint_file}"''' ) torch.save(lowerCamelCase , lowerCamelCase ) #################################################################################################### if __name__ == "__main__": main() ####################################################################################################
4
0
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _A = logging.get_logger(__name__) _A = { '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 UpperCAmelCase__ ( A_ ): """simple docstring""" UpperCAmelCase__ : 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_ , ) -> Union[str, Any]: super().__init__(**A_ ) __UpperCamelCase =image_size __UpperCamelCase =num_channels __UpperCamelCase =kernel_size __UpperCamelCase =stride __UpperCamelCase =padding __UpperCamelCase =hidden_sizes __UpperCamelCase =num_attention_heads __UpperCamelCase =depths __UpperCamelCase =key_dim __UpperCamelCase =drop_path_rate __UpperCamelCase =patch_size __UpperCamelCase =attention_ratio __UpperCamelCase =mlp_ratio __UpperCamelCase =initializer_range __UpperCamelCase =[ ['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 UpperCAmelCase__ ( A_ ): """simple docstring""" UpperCAmelCase__ : int = version.parse("1.11" ) @property def _a ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def _a ( self ) -> float: return 1E-4
117
from ..utils import DummyObject, requires_backends class UpperCAmelCase__ ( metaclass=A_ ): """simple docstring""" UpperCAmelCase__ : Any = ["speech"] def __init__( self , *A_ , **A_ ) -> Any: requires_backends(self , ['speech'] ) class UpperCAmelCase__ ( metaclass=A_ ): """simple docstring""" UpperCAmelCase__ : Union[str, Any] = ["speech"] def __init__( self , *A_ , **A_ ) -> Union[str, Any]: requires_backends(self , ['speech'] )
117
1
'''simple docstring''' def lowerCamelCase ( UpperCAmelCase__ : list ) -> list: if any(not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) or x < 0 for x in sequence ): raise TypeError("""Sequence must be list of non-negative integers""" ) for _ in range(len(UpperCAmelCase__ ) ): for i, (rod_upper, rod_lower) in enumerate(zip(UpperCAmelCase__ , sequence[1:] ) ): if rod_upper > rod_lower: sequence[i] -= rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower return sequence if __name__ == "__main__": assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9]
239
'''simple docstring''' def lowerCamelCase ( UpperCAmelCase__ : list ) -> list: if any(not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) or x < 0 for x in sequence ): raise TypeError("""Sequence must be list of non-negative integers""" ) for _ in range(len(UpperCAmelCase__ ) ): for i, (rod_upper, rod_lower) in enumerate(zip(UpperCAmelCase__ , sequence[1:] ) ): if rod_upper > rod_lower: sequence[i] -= rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower return sequence if __name__ == "__main__": assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9]
239
1
def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[Any] ): return number | (1 << position) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : int ): return number & ~(1 << position) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Any ): return number ^ (1 << position) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Any ): return ((number >> position) & 1) == 1 def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : int ): return int((number & (1 << position)) != 0 ) if __name__ == "__main__": import doctest doctest.testmod()
358
from __future__ import annotations def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int ): if b == 0: return (1, 0) ((__UpperCamelCase) , (__UpperCamelCase)) =extended_euclid(SCREAMING_SNAKE_CASE__ , a % b ) __UpperCamelCase =a // b return (y, x - k * y) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int ): ((__UpperCamelCase) , (__UpperCamelCase)) =extended_euclid(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =na * na __UpperCamelCase =ra * x * na + ra * y * na return (n % m + m) % m def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int ): ((__UpperCamelCase) , (__UpperCamelCase)) =extended_euclid(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if b < 0: __UpperCamelCase =(b % n + n) % n return b def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int ): __UpperCamelCase , __UpperCamelCase =invert_modulo(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ), invert_modulo(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =na * na __UpperCamelCase =ra * x * na + ra * y * na return (n % m + m) % m if __name__ == "__main__": from doctest import testmod testmod(name='chinese_remainder_theorem', verbose=True) testmod(name='chinese_remainder_theorem2', verbose=True) testmod(name='invert_modulo', verbose=True) testmod(name='extended_euclid', verbose=True)
117
0
def A_ ( snake_case : int = 1000 ) -> int: '''simple docstring''' __UpperCamelCase = 3 __UpperCamelCase = 0 while a < n: if a % 3 == 0 or a % 5 == 0: result += a elif a % 15 == 0: result -= a a += 1 return result if __name__ == "__main__": print(F"{solution() = }")
328
def A_ ( snake_case : list ) -> list: '''simple docstring''' __UpperCamelCase = len(snake_case ) for i in range(1 , snake_case ): __UpperCamelCase = collection[i] __UpperCamelCase = 0 __UpperCamelCase = i - 1 while low <= high: __UpperCamelCase = (low + high) // 2 if val < collection[mid]: __UpperCamelCase = mid - 1 else: __UpperCamelCase = mid + 1 for j in range(snake_case , snake_case , -1 ): __UpperCamelCase = collection[j - 1] __UpperCamelCase = val return collection if __name__ == "__main__": lowercase__ : List[Any] = input("Enter numbers separated by a comma:\n").strip() lowercase__ : str = [int(item) for item in user_input.split(",")] print(binary_insertion_sort(unsorted))
328
1
"""simple docstring""" import argparse import datetime import io import itertools import json import math import os import platform import re import shlex import subprocess import sys from pathlib import Path from statistics import fmean import pandas as pd import torch from tqdm import tqdm import transformers UpperCAmelCase_ : Union[str, Any] = float("""nan""") class lowerCAmelCase__ : '''simple docstring''' def __init__( self : Union[str, Any] , lowercase_ : List[Any]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[Any] = sys.stdout SCREAMING_SNAKE_CASE_ : str = open(lowercase_ , '''a''') def __getattr__( self : Union[str, Any] , lowercase_ : Dict): '''simple docstring''' return getattr(self.stdout , lowercase_) def _SCREAMING_SNAKE_CASE ( self : int , lowercase_ : Tuple): '''simple docstring''' self.stdout.write(lowercase_) # strip tqdm codes self.file.write(re.sub(r'''^.*\r''' , '''''' , lowercase_ , 0 , re.M)) def _A (__a=80 , __a=False ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = [] # deal with critical env vars SCREAMING_SNAKE_CASE_ : List[str] = ['''CUDA_VISIBLE_DEVICES'''] for key in env_keys: SCREAMING_SNAKE_CASE_ : int = os.environ.get(__a , __a ) if val is not None: cmd.append(f'{key}={val}' ) # python executable (not always needed if the script is executable) SCREAMING_SNAKE_CASE_ : Any = sys.executable if full_python_path else sys.executable.split('''/''' )[-1] cmd.append(__a ) # now the normal args cmd += list(map(shlex.quote , sys.argv ) ) # split up into up to MAX_WIDTH lines with shell multi-line escapes SCREAMING_SNAKE_CASE_ : int = [] SCREAMING_SNAKE_CASE_ : Tuple = '''''' while len(__a ) > 0: current_line += f'{cmd.pop(0 )} ' if len(__a ) == 0 or len(__a ) + len(cmd[0] ) + 1 > max_width - 1: lines.append(__a ) SCREAMING_SNAKE_CASE_ : Dict = '''''' return "\\\n".join(__a ) def _A (__a , __a ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = re.sub(R'''[\\\n]+''' , ''' ''' , args.base_cmd ) # remove --output_dir if any and set our own SCREAMING_SNAKE_CASE_ : List[Any] = re.sub('''--output_dir\s+[^\s]+''' , '''''' , args.base_cmd ) args.base_cmd += f' --output_dir {output_dir}' # ensure we have --overwrite_output_dir SCREAMING_SNAKE_CASE_ : List[str] = re.sub('''--overwrite_output_dir\s+''' , '''''' , args.base_cmd ) args.base_cmd += " --overwrite_output_dir" return [sys.executable] + shlex.split(args.base_cmd ) def _A (__a , __a , __a , __a , __a , __a , __a ) -> str: """simple docstring""" if 0: import random from time import sleep sleep(0 ) return dict( {k: random.uniform(0 , 1_00 ) for k in metric_keys} , **{target_metric_key: random.choice([nan, 10.31, 1_00.2, 55.66_66, 2_22.22_22_22_22] )} , ) SCREAMING_SNAKE_CASE_ : List[Any] = subprocess.run(__a , capture_output=__a , text=__a ) if verbose: print('''STDOUT''' , result.stdout ) print('''STDERR''' , result.stderr ) # save the streams SCREAMING_SNAKE_CASE_ : Optional[Any] = variation.replace(''' ''' , '''-''' ) with open(Path(__a ) / f'log.{prefix}.stdout.txt' , '''w''' ) as f: f.write(result.stdout ) with open(Path(__a ) / f'log.{prefix}.stderr.txt' , '''w''' ) as f: f.write(result.stderr ) if result.returncode != 0: if verbose: print('''failed''' ) return {target_metric_key: nan} with io.open(f'{output_dir}/all_results.json' , '''r''' , encoding='''utf-8''' ) as f: SCREAMING_SNAKE_CASE_ : Any = json.load(__a ) # filter out just the keys we want return {k: v for k, v in metrics.items() if k in metric_keys} def _A (__a , __a , __a , __a , __a , __a , __a , __a , __a , __a , ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = [] SCREAMING_SNAKE_CASE_ : Optional[int] = [] SCREAMING_SNAKE_CASE_ : List[Any] = f'{id}: {variation:<{longest_variation_len}}' SCREAMING_SNAKE_CASE_ : Any = f'{preamble}: ' SCREAMING_SNAKE_CASE_ : str = set(report_metric_keys + [target_metric_key] ) for i in tqdm(range(__a ) , desc=__a , leave=__a ): SCREAMING_SNAKE_CASE_ : List[Any] = process_run_single( __a , __a , __a , __a , __a , __a , __a ) SCREAMING_SNAKE_CASE_ : List[Any] = single_run_metrics[target_metric_key] if not math.isnan(__a ): metrics.append(__a ) results.append(__a ) outcome += "✓" else: outcome += "✘" SCREAMING_SNAKE_CASE_ : List[str] = f'\33[2K\r{outcome}' if len(__a ) > 0: SCREAMING_SNAKE_CASE_ : List[Any] = {k: fmean([x[k] for x in metrics] ) for k in metrics[0].keys()} SCREAMING_SNAKE_CASE_ : List[str] = round(mean_metrics[target_metric_key] , 2 ) SCREAMING_SNAKE_CASE_ : Tuple = f'{outcome} {mean_target}' if len(__a ) > 1: results_str += f' {tuple(round(__a , 2 ) for x in results )}' print(__a ) SCREAMING_SNAKE_CASE_ : Any = variation return mean_metrics else: print(__a ) return {variation_key: variation, target_metric_key: nan} def _A () -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = torch.cuda.get_device_properties(torch.device('''cuda''' ) ) return f'\nDatetime : {datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S" )}\n\nSoftware:\ntransformers: {transformers.__version__}\ntorch : {torch.__version__}\ncuda : {torch.version.cuda}\npython : {platform.python_version()}\n\nHardware:\n{torch.cuda.device_count()} GPUs : {properties.name}, {properties.total_memory/2**30:0.2f}GB\n' def _A (__a , __a , __a , __a , __a ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = pd.DataFrame(__a ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = '''variation''' SCREAMING_SNAKE_CASE_ : List[Any] = '''diff_%''' SCREAMING_SNAKE_CASE_ : Optional[Any] = nan if base_variation is not None and len(df[df[variation_key] == base_variation] ): # this may still return nan SCREAMING_SNAKE_CASE_ : Optional[int] = df.loc[df[variation_key] == base_variation][target_metric_key].item() if math.isnan(__a ): # as a fallback, use the minimal value as the sentinel SCREAMING_SNAKE_CASE_ : List[str] = df.loc[df[target_metric_key] != nan][target_metric_key].min() # create diff column if possible if not math.isnan(__a ): SCREAMING_SNAKE_CASE_ : Any = df.apply( lambda __a : round(1_00 * (r[target_metric_key] - sentinel_value) / sentinel_value ) if not math.isnan(r[target_metric_key] ) else 0 , axis='''columns''' , ) # re-order columns SCREAMING_SNAKE_CASE_ : Dict = [variation_key, target_metric_key, diff_key, *report_metric_keys] SCREAMING_SNAKE_CASE_ : Union[str, Any] = df.reindex(__a , axis='''columns''' ) # reorder cols # capitalize SCREAMING_SNAKE_CASE_ : Union[str, Any] = df.rename(str.capitalize , axis='''columns''' ) # make the cols as narrow as possible SCREAMING_SNAKE_CASE_ : Optional[Any] = df.rename(lambda __a : c.replace('''_''' , '''<br>''' ) , axis='''columns''' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = df.rename(lambda __a : c.replace('''_''' , '''\n''' ) , axis='''columns''' ) SCREAMING_SNAKE_CASE_ : int = ['''''', '''Copy between the cut-here-lines and paste as is to github or a forum'''] report += ["----------8<-----------------8<--------"] report += ["*** Results:", df_github.to_markdown(index=__a , floatfmt='''.2f''' )] report += ["```"] report += ["*** Setup:", get_versions()] report += ["*** The benchmark command line was:", get_original_command()] report += ["```"] report += ["----------8<-----------------8<--------"] report += ["*** Results (console):", df_console.to_markdown(index=__a , floatfmt='''.2f''' )] print('''\n\n'''.join(__a ) ) def _A () -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = argparse.ArgumentParser() parser.add_argument( '''--base-cmd''' , default=__a , type=__a , required=__a , help='''Base cmd''' , ) parser.add_argument( '''--variations''' , default=__a , type=__a , nargs='''+''' , required=__a , help='''Multi-dimensional variations, example: \'|--fp16|--bf16\' \'|--tf32\'''' , ) parser.add_argument( '''--base-variation''' , default=__a , type=__a , help='''Baseline variation to compare to. if None the minimal target value will be used to compare against''' , ) parser.add_argument( '''--target-metric-key''' , default=__a , type=__a , required=__a , help='''Target metric key in output_dir/all_results.json, e.g., train_samples_per_second''' , ) parser.add_argument( '''--report-metric-keys''' , default='''''' , type=__a , help='''Report metric keys - other metric keys from output_dir/all_results.json to report, e.g., train_loss. Use a single argument e.g., \'train_loss train_samples''' , ) parser.add_argument( '''--repeat-times''' , default=1 , type=__a , help='''How many times to re-run each variation - an average will be reported''' , ) parser.add_argument( '''--output_dir''' , default='''output_benchmark''' , type=__a , help='''The output directory where all the benchmark reports will go to and additionally this directory will be used to override --output_dir in the script that is being benchmarked''' , ) parser.add_argument( '''--verbose''' , default=__a , action='''store_true''' , help='''Whether to show the outputs of each run or just the benchmark progress''' , ) SCREAMING_SNAKE_CASE_ : Dict = parser.parse_args() SCREAMING_SNAKE_CASE_ : Tuple = args.output_dir Path(__a ).mkdir(exist_ok=__a ) SCREAMING_SNAKE_CASE_ : int = get_base_command(__a , __a ) # split each dimension into its --foo variations SCREAMING_SNAKE_CASE_ : Dict = [list(map(str.strip , re.split(R'''\|''' , __a ) ) ) for x in args.variations] # build a cartesian product of dimensions and convert those back into cmd-line arg strings, # while stripping white space for inputs that were empty SCREAMING_SNAKE_CASE_ : List[str] = list(map(str.strip , map(''' '''.join , itertools.product(*__a ) ) ) ) SCREAMING_SNAKE_CASE_ : Tuple = max(len(__a ) for x in variations ) # split wanted keys SCREAMING_SNAKE_CASE_ : Union[str, Any] = args.report_metric_keys.split() # capture prints into a log file for convenience SCREAMING_SNAKE_CASE_ : Union[str, Any] = f'benchmark-report-{datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S" )}.txt' print(f'\nNote: each run\'s output is also logged under {output_dir}/log.*.std*.txt' ) print(f'and this script\'s output is also piped into {report_fn}' ) SCREAMING_SNAKE_CASE_ : int = Tee(__a ) print(f'\n*** Running {len(__a )} benchmarks:' ) print(f'Base command: {" ".join(__a )}' ) SCREAMING_SNAKE_CASE_ : Optional[int] = '''variation''' SCREAMING_SNAKE_CASE_ : str = [] for id, variation in enumerate(tqdm(__a , desc='''Total completion: ''' , leave=__a ) ): SCREAMING_SNAKE_CASE_ : str = base_cmd + variation.split() results.append( process_run( id + 1 , __a , __a , __a , __a , args.target_metric_key , __a , args.repeat_times , __a , args.verbose , ) ) process_results(__a , args.target_metric_key , __a , args.base_variation , __a ) if __name__ == "__main__": main()
357
"""simple docstring""" from ...processing_utils import ProcessorMixin class lowerCAmelCase__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "SpeechT5FeatureExtractor" __UpperCamelCase = "SpeechT5Tokenizer" def __init__( self : Any , lowercase_ : Dict , lowercase_ : Optional[Any]): '''simple docstring''' super().__init__(lowercase_ , lowercase_) def __call__( self : List[Any] , *lowercase_ : List[Any] , **lowercase_ : int): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[str] = kwargs.pop('''audio''' , lowercase_) SCREAMING_SNAKE_CASE_ : Optional[int] = kwargs.pop('''text''' , lowercase_) SCREAMING_SNAKE_CASE_ : Any = kwargs.pop('''text_target''' , lowercase_) SCREAMING_SNAKE_CASE_ : Dict = kwargs.pop('''audio_target''' , lowercase_) SCREAMING_SNAKE_CASE_ : List[Any] = kwargs.pop('''sampling_rate''' , lowercase_) if audio is not None and text is not None: raise ValueError( '''Cannot process both `audio` and `text` inputs. Did you mean `audio_target` or `text_target`?''') if audio_target is not None and text_target is not None: raise ValueError( '''Cannot process both `audio_target` and `text_target` inputs. Did you mean `audio` or `text`?''') if audio is None and audio_target is None and text is None and text_target is None: raise ValueError( '''You need to specify either an `audio`, `audio_target`, `text`, or `text_target` input to process.''') if audio is not None: SCREAMING_SNAKE_CASE_ : Any = self.feature_extractor(lowercase_ , *lowercase_ , sampling_rate=lowercase_ , **lowercase_) elif text is not None: SCREAMING_SNAKE_CASE_ : Dict = self.tokenizer(lowercase_ , **lowercase_) else: SCREAMING_SNAKE_CASE_ : Any = None if audio_target is not None: SCREAMING_SNAKE_CASE_ : List[Any] = self.feature_extractor(audio_target=lowercase_ , *lowercase_ , sampling_rate=lowercase_ , **lowercase_) SCREAMING_SNAKE_CASE_ : Optional[int] = targets['''input_values'''] elif text_target is not None: SCREAMING_SNAKE_CASE_ : int = self.tokenizer(lowercase_ , **lowercase_) SCREAMING_SNAKE_CASE_ : List[Any] = targets['''input_ids'''] else: SCREAMING_SNAKE_CASE_ : int = None if inputs is None: return targets if targets is not None: SCREAMING_SNAKE_CASE_ : Union[str, Any] = labels SCREAMING_SNAKE_CASE_ : Optional[Any] = targets.get('''attention_mask''') if decoder_attention_mask is not None: SCREAMING_SNAKE_CASE_ : Any = decoder_attention_mask return inputs def _SCREAMING_SNAKE_CASE ( self : Tuple , *lowercase_ : Tuple , **lowercase_ : List[str]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[Any] = kwargs.pop('''input_values''' , lowercase_) SCREAMING_SNAKE_CASE_ : int = kwargs.pop('''input_ids''' , lowercase_) SCREAMING_SNAKE_CASE_ : Dict = kwargs.pop('''labels''' , lowercase_) if input_values is not None and input_ids is not None: raise ValueError('''Cannot process both `input_values` and `input_ids` inputs.''') if input_values is None and input_ids is None and labels is None: raise ValueError( '''You need to specify either an `input_values`, `input_ids`, or `labels` input to be padded.''') if input_values is not None: SCREAMING_SNAKE_CASE_ : Any = self.feature_extractor.pad(lowercase_ , *lowercase_ , **lowercase_) elif input_ids is not None: SCREAMING_SNAKE_CASE_ : Tuple = self.tokenizer.pad(lowercase_ , **lowercase_) else: SCREAMING_SNAKE_CASE_ : List[Any] = None if labels is not None: if "input_ids" in labels or (isinstance(lowercase_ , lowercase_) and "input_ids" in labels[0]): SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.tokenizer.pad(lowercase_ , **lowercase_) SCREAMING_SNAKE_CASE_ : Dict = targets['''input_ids'''] else: SCREAMING_SNAKE_CASE_ : Dict = self.feature_extractor.feature_size SCREAMING_SNAKE_CASE_ : Optional[int] = self.feature_extractor.num_mel_bins SCREAMING_SNAKE_CASE_ : str = self.feature_extractor.pad(lowercase_ , *lowercase_ , **lowercase_) SCREAMING_SNAKE_CASE_ : str = feature_size_hack SCREAMING_SNAKE_CASE_ : Dict = targets['''input_values'''] else: SCREAMING_SNAKE_CASE_ : List[Any] = None if inputs is None: return targets if targets is not None: SCREAMING_SNAKE_CASE_ : Dict = labels SCREAMING_SNAKE_CASE_ : List[str] = targets.get('''attention_mask''') if decoder_attention_mask is not None: SCREAMING_SNAKE_CASE_ : Optional[Any] = decoder_attention_mask return inputs def _SCREAMING_SNAKE_CASE ( self : Optional[Any] , *lowercase_ : Optional[int] , **lowercase_ : Tuple): '''simple docstring''' return self.tokenizer.batch_decode(*lowercase_ , **lowercase_) def _SCREAMING_SNAKE_CASE ( self : List[Any] , *lowercase_ : Dict , **lowercase_ : List[Any]): '''simple docstring''' return self.tokenizer.decode(*lowercase_ , **lowercase_)
318
0
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 A : """simple docstring""" def __init__( self : Optional[int],lowercase_ : Tuple = None,lowercase_ : List[str] = None,lowercase_ : Any=None,lowercase_ : str=None )-> Any: '''simple docstring''' 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 : Any,lowercase_ : Dict )-> List[Any]: '''simple docstring''' if not isinstance(lowercase_,lowercase_ ): 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 snake_case__ ( self : str,lowercase_ : str,lowercase_ : Tuple = False )-> List[Any]: '''simple docstring''' 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 snake_case__ ( self : int )-> Tuple: '''simple docstring''' if self.new_user_input: self.past_user_inputs.append(self.new_user_input ) A__ = None def snake_case__ ( self : List[Any],lowercase_ : str )-> Optional[Any]: '''simple docstring''' self.generated_responses.append(lowercase_ ) def snake_case__ ( self : Any )-> Tuple: '''simple docstring''' 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 : List[str] )-> Any: '''simple docstring''' 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( _UpperCAmelCase , 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 A ( _UpperCAmelCase ): """simple docstring""" def __init__( self : Any,*lowercase_ : List[str],**lowercase_ : str )-> str: '''simple docstring''' super().__init__(*lowercase_,**lowercase_ ) if self.tokenizer.pad_token_id is None: A__ = self.tokenizer.eos_token def snake_case__ ( self : Any,lowercase_ : Tuple=None,lowercase_ : Optional[Any]=None,lowercase_ : Optional[int]=None,**lowercase_ : Optional[int] )-> Tuple: '''simple docstring''' 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(lowercase_ ) return preprocess_params, forward_params, postprocess_params def __call__( self : Optional[Any],lowercase_ : List[str],lowercase_ : Optional[Any]=0,**lowercase_ : List[Any] )-> Optional[int]: '''simple docstring''' A__ = super().__call__(lowercase_,num_workers=lowercase_,**lowercase_ ) if isinstance(lowercase_,lowercase_ ) and len(lowercase_ ) == 1: return outputs[0] return outputs def snake_case__ ( self : List[Any],lowercase_ : Optional[Any],lowercase_ : Tuple=3_2 )-> Dict[str, Any]: '''simple docstring''' if not isinstance(lowercase_,lowercase_ ): 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(lowercase_ ) else: # If the tokenizer cannot handle conversations, we default to only the old version A__ = self._legacy_parse_and_tokenize(lowercase_ ) 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 snake_case__ ( self : Union[str, Any],lowercase_ : str,lowercase_ : List[str]=1_0,**lowercase_ : Any )-> int: '''simple docstring''' 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(**lowercase_,**lowercase_ ) if self.model.config.is_encoder_decoder: A__ = 1 else: A__ = n return {"output_ids": output_ids[:, start_position:], "conversation": conversation} def snake_case__ ( self : Tuple,lowercase_ : Union[str, Any],lowercase_ : Any=True )-> Union[str, Any]: '''simple docstring''' A__ = model_outputs['''output_ids'''] A__ = self.tokenizer.decode( output_ids[0],skip_special_tokens=lowercase_,clean_up_tokenization_spaces=lowercase_,) A__ = model_outputs['''conversation'''] conversation.mark_processed() conversation.append_response(lowercase_ ) return conversation def snake_case__ ( self : Tuple,lowercase_ : List[str] )-> Dict: '''simple docstring''' 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(lowercase_,add_special_tokens=lowercase_ ) + [eos_token_id] ) else: input_ids.extend(self.tokenizer.encode(lowercase_,add_special_tokens=lowercase_ ) ) if len(lowercase_ ) > self.tokenizer.model_max_length: A__ = input_ids[-self.tokenizer.model_max_length :] return input_ids
7
"""simple docstring""" def lowerCamelCase_ (UpperCamelCase__ : int , UpperCamelCase__ : int ): if a < 0 or b < 0: raise ValueError('''the value of both inputs must be positive''' ) _UpperCAmelCase : List[str] = str(bin(UpperCamelCase__ ) )[2:] # remove the leading "0b" _UpperCAmelCase : str = str(bin(UpperCamelCase__ ) )[2:] _UpperCAmelCase : List[str] = max(len(UpperCamelCase__ ) , len(UpperCamelCase__ ) ) return "0b" + "".join( str(int('''1''' in (char_a, char_b) ) ) for char_a, char_b in zip(a_binary.zfill(UpperCamelCase__ ) , b_binary.zfill(UpperCamelCase__ ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
263
0
'''simple docstring''' from __future__ import annotations def __lowerCamelCase ( A__ , A__ , A__ ) -> tuple[float, list[float]]: """simple docstring""" UpperCamelCase = list(range(len(A__ ) ) ) UpperCamelCase = [v / w for v, w in zip(A__ , A__ )] index.sort(key=lambda A__ : ratio[i] , reverse=A__ ) UpperCamelCase = 0 UpperCamelCase = [0] * len(A__ ) for i in index: if weight[i] <= capacity: UpperCamelCase = 1 max_value += value[i] capacity -= weight[i] else: UpperCamelCase = capacity / weight[i] max_value += value[i] * capacity / weight[i] break return max_value, fractions if __name__ == "__main__": import doctest doctest.testmod()
368
'''simple docstring''' import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class SCREAMING_SNAKE_CASE ( unittest.TestCase ): """simple docstring""" _SCREAMING_SNAKE_CASE = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING _SCREAMING_SNAKE_CASE = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def A ( self : List[Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[str] ): """simple docstring""" UpperCamelCase = TextaTextGenerationPipeline(model=UpperCamelCase__ , tokenizer=UpperCamelCase__ ) return generator, ["Something to write", "Something else"] def A ( self : str , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Any] ): """simple docstring""" UpperCamelCase = generator('Something there' ) self.assertEqual(UpperCamelCase__ , [{'generated_text': ANY(UpperCamelCase__ )}] ) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]['generated_text'].startswith('Something there' ) ) UpperCamelCase = generator(['This is great !', 'Something else'] , num_return_sequences=2 , do_sample=UpperCamelCase__ ) self.assertEqual( UpperCamelCase__ , [ [{'generated_text': ANY(UpperCamelCase__ )}, {'generated_text': ANY(UpperCamelCase__ )}], [{'generated_text': ANY(UpperCamelCase__ )}, {'generated_text': ANY(UpperCamelCase__ )}], ] , ) UpperCamelCase = generator( ['This is great !', 'Something else'] , num_return_sequences=2 , batch_size=2 , do_sample=UpperCamelCase__ ) self.assertEqual( UpperCamelCase__ , [ [{'generated_text': ANY(UpperCamelCase__ )}, {'generated_text': ANY(UpperCamelCase__ )}], [{'generated_text': ANY(UpperCamelCase__ )}, {'generated_text': ANY(UpperCamelCase__ )}], ] , ) with self.assertRaises(UpperCamelCase__ ): generator(4 ) @require_torch def A ( self : Dict ): """simple docstring""" UpperCamelCase = pipeline('text2text-generation' , model='patrickvonplaten/t5-tiny-random' , framework='pt' ) # do_sample=False necessary for reproducibility UpperCamelCase = generator('Something there' , do_sample=UpperCamelCase__ ) self.assertEqual(UpperCamelCase__ , [{'generated_text': ''}] ) UpperCamelCase = 3 UpperCamelCase = generator( 'Something there' , num_return_sequences=UpperCamelCase__ , num_beams=UpperCamelCase__ , ) UpperCamelCase = [ {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide Beide'}, {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide'}, {'generated_text': ''}, ] self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) UpperCamelCase = generator('This is a test' , do_sample=UpperCamelCase__ , num_return_sequences=2 , return_tensors=UpperCamelCase__ ) self.assertEqual( UpperCamelCase__ , [ {'generated_token_ids': ANY(torch.Tensor )}, {'generated_token_ids': ANY(torch.Tensor )}, ] , ) UpperCamelCase = generator.model.config.eos_token_id UpperCamelCase = '<pad>' UpperCamelCase = generator( ['This is a test', 'This is a second test'] , do_sample=UpperCamelCase__ , num_return_sequences=2 , batch_size=2 , return_tensors=UpperCamelCase__ , ) self.assertEqual( UpperCamelCase__ , [ [ {'generated_token_ids': ANY(torch.Tensor )}, {'generated_token_ids': ANY(torch.Tensor )}, ], [ {'generated_token_ids': ANY(torch.Tensor )}, {'generated_token_ids': ANY(torch.Tensor )}, ], ] , ) @require_tf def A ( self : str ): """simple docstring""" UpperCamelCase = pipeline('text2text-generation' , model='patrickvonplaten/t5-tiny-random' , framework='tf' ) # do_sample=False necessary for reproducibility UpperCamelCase = generator('Something there' , do_sample=UpperCamelCase__ ) self.assertEqual(UpperCamelCase__ , [{'generated_text': ''}] )
249
0
'''simple docstring''' from heapq import heappop, heappush import numpy as np def SCREAMING_SNAKE_CASE_ (UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , ) -> tuple[float | int, list[tuple[int, int]]]: lowerCamelCase__ , lowerCamelCase__ : Union[str, Any] = grid.shape lowerCamelCase__ : List[str] = [-1, 1, 0, 0] lowerCamelCase__ : Dict = [0, 0, -1, 1] if allow_diagonal: dx += [-1, -1, 1, 1] dy += [-1, 1, -1, 1] lowerCamelCase__ , lowerCamelCase__ : Any = [(0, source)], set() lowerCamelCase__ : Tuple = np.full((rows, cols) , np.inf ) lowerCamelCase__ : List[str] = 0 lowerCamelCase__ : Optional[int] = np.empty((rows, cols) , dtype=UpperCamelCase ) lowerCamelCase__ : str = None while queue: ((lowerCamelCase__) , (lowerCamelCase__)) : List[str] = heappop(UpperCamelCase ) if (x, y) in visited: continue visited.add((x, y) ) if (x, y) == destination: lowerCamelCase__ : Optional[int] = [] while (x, y) != source: path.append((x, y) ) lowerCamelCase__ , lowerCamelCase__ : List[Any] = predecessors[x, y] path.append(UpperCamelCase ) # add the source manually path.reverse() return matrix[destination], path for i in range(len(UpperCamelCase ) ): lowerCamelCase__ , lowerCamelCase__ : Union[str, Any] = x + dx[i], y + dy[i] if 0 <= nx < rows and 0 <= ny < cols: lowerCamelCase__ : Any = grid[nx][ny] if next_node == 1 and matrix[nx, ny] > dist + 1: heappush(UpperCamelCase , (dist + 1, (nx, ny)) ) lowerCamelCase__ : Union[str, Any] = dist + 1 lowerCamelCase__ : List[str] = (x, y) return np.inf, [] if __name__ == "__main__": import doctest doctest.testmod()
41
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import _LazyModule A_ : int = {"""tokenization_bertweet""": ["""BertweetTokenizer"""]} if TYPE_CHECKING: from .tokenization_bertweet import BertweetTokenizer else: import sys A_ : int = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
215
0
"""simple docstring""" import socket def lowercase__ ( ): __UpperCAmelCase = socket.socket(socket.AF_INET , socket.SOCK_STREAM ) __UpperCAmelCase = socket.gethostname() __UpperCAmelCase = 12_312 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: __UpperCAmelCase = sock.recv(1_024 ) if not data: break out_file.write(lowercase_ ) print('''Successfully received the file''' ) sock.close() print('''Connection closed''' ) if __name__ == "__main__": main()
357
"""simple docstring""" from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch _lowercase : Optional[int] = logging.get_logger(__name__) @add_end_docstrings( _lowerCAmelCase , r"\n top_k (`int`, defaults to 5):\n The number of predictions to return.\n targets (`str` or `List[str]`, *optional*):\n When passed, the model will limit the scores to the passed targets instead of looking up in the whole\n vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting\n token will be used (with a warning, and that might be slower).\n\n " , ) class _UpperCAmelCase ( _lowerCAmelCase ): def a ( self : List[Any] , _lowercase : GenericTensor ): if self.framework == "tf": __UpperCAmelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": __UpperCAmelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=_lowercase ) else: raise ValueError('''Unsupported framework''' ) return masked_index def a ( self : List[str] , _lowercase : GenericTensor ): __UpperCAmelCase = self.get_masked_index(_lowercase ) __UpperCAmelCase = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , F'''No mask_token ({self.tokenizer.mask_token}) found on the input''' , ) def a ( self : Optional[int] , _lowercase : GenericTensor ): if isinstance(_lowercase , _lowercase ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input['''input_ids'''][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(_lowercase ) def a ( self : List[str] , _lowercase : Optional[int] , _lowercase : Tuple=None , **_lowercase : Tuple ): if return_tensors is None: __UpperCAmelCase = self.framework __UpperCAmelCase = self.tokenizer(_lowercase , return_tensors=_lowercase ) self.ensure_exactly_one_mask_token(_lowercase ) return model_inputs def a ( self : Optional[int] , _lowercase : Tuple ): __UpperCAmelCase = self.model(**_lowercase ) __UpperCAmelCase = model_inputs['''input_ids'''] return model_outputs def a ( self : Optional[int] , _lowercase : List[str] , _lowercase : Optional[Any]=5 , _lowercase : Dict=None ): # Cap top_k if there are targets if target_ids is not None and target_ids.shape[0] < top_k: __UpperCAmelCase = target_ids.shape[0] __UpperCAmelCase = model_outputs['''input_ids'''][0] __UpperCAmelCase = model_outputs['''logits'''] if self.framework == "tf": __UpperCAmelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] __UpperCAmelCase = outputs.numpy() __UpperCAmelCase = outputs[0, masked_index, :] __UpperCAmelCase = stable_softmax(_lowercase , axis=-1 ) if target_ids is not None: __UpperCAmelCase = tf.gather_nd(tf.squeeze(_lowercase , 0 ) , target_ids.reshape(-1 , 1 ) ) __UpperCAmelCase = tf.expand_dims(_lowercase , 0 ) __UpperCAmelCase = tf.math.top_k(_lowercase , k=_lowercase ) __UpperCAmelCase , __UpperCAmelCase = topk.values.numpy(), topk.indices.numpy() else: __UpperCAmelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=_lowercase ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample __UpperCAmelCase = outputs[0, masked_index, :] __UpperCAmelCase = logits.softmax(dim=-1 ) if target_ids is not None: __UpperCAmelCase = probs[..., target_ids] __UpperCAmelCase , __UpperCAmelCase = probs.topk(_lowercase ) __UpperCAmelCase = [] __UpperCAmelCase = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() , predictions.tolist() ) ): __UpperCAmelCase = [] for v, p in zip(_values , _predictions ): # Copy is important since we're going to modify this array in place __UpperCAmelCase = input_ids.numpy().copy() if target_ids is not None: __UpperCAmelCase = target_ids[p].tolist() __UpperCAmelCase = p # Filter padding out: __UpperCAmelCase = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back __UpperCAmelCase = self.tokenizer.decode(_lowercase , skip_special_tokens=_lowercase ) __UpperCAmelCase = {'''score''': v, '''token''': p, '''token_str''': self.tokenizer.decode([p] ), '''sequence''': sequence} row.append(_lowercase ) result.append(_lowercase ) if single_mask: return result[0] return result def a ( self : str , _lowercase : List[Any] , _lowercase : List[Any]=None ): if isinstance(_lowercase , _lowercase ): __UpperCAmelCase = [targets] try: __UpperCAmelCase = self.tokenizer.get_vocab() except Exception: __UpperCAmelCase = {} __UpperCAmelCase = [] for target in targets: __UpperCAmelCase = vocab.get(_lowercase , _lowercase ) if id_ is None: __UpperCAmelCase = self.tokenizer( _lowercase , add_special_tokens=_lowercase , return_attention_mask=_lowercase , return_token_type_ids=_lowercase , max_length=1 , truncation=_lowercase , )['''input_ids'''] if len(_lowercase ) == 0: logger.warning( F'''The specified target token `{target}` does not exist in the model vocabulary. ''' '''We cannot replace it with anything meaningful, ignoring it''' ) continue __UpperCAmelCase = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( F'''The specified target token `{target}` does not exist in the model vocabulary. ''' F'''Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`.''' ) target_ids.append(id_ ) __UpperCAmelCase = list(set(_lowercase ) ) if len(_lowercase ) == 0: raise ValueError('''At least one target must be provided when passed.''' ) __UpperCAmelCase = np.array(_lowercase ) return target_ids def a ( self : int , _lowercase : Dict=None , _lowercase : Optional[Any]=None ): __UpperCAmelCase = {} if targets is not None: __UpperCAmelCase = self.get_target_ids(_lowercase , _lowercase ) __UpperCAmelCase = target_ids if top_k is not None: __UpperCAmelCase = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , '''The tokenizer does not define a `mask_token`.''' ) return {}, {}, postprocess_params def __call__( self : Union[str, Any] , _lowercase : Optional[Any] , *_lowercase : Union[str, Any] , **_lowercase : int ): __UpperCAmelCase = super().__call__(_lowercase , **_lowercase ) if isinstance(_lowercase , _lowercase ) and len(_lowercase ) == 1: return outputs[0] return outputs
86
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) __lowercase = { '''configuration_speech_to_text''': ['''SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Speech2TextConfig'''], '''processing_speech_to_text''': ['''Speech2TextProcessor'''], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase = ['''Speech2TextTokenizer'''] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase = ['''Speech2TextFeatureExtractor'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase = [ '''TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFSpeech2TextForConditionalGeneration''', '''TFSpeech2TextModel''', '''TFSpeech2TextPreTrainedModel''', ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase = [ '''SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Speech2TextForConditionalGeneration''', '''Speech2TextModel''', '''Speech2TextPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys __lowercase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
43
'''simple docstring''' import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 __A : str = get_tests_dir("fixtures") class __snake_case ( unittest.TestCase): """simple docstring""" def __lowercase ( self : int ) -> List[str]: # A mock response for an HTTP head request to emulate server down lowerCAmelCase_ : str = mock.Mock() lowerCAmelCase_ : Optional[Any] = 5_00 lowerCAmelCase_ : List[str] = {} lowerCAmelCase_ : str = HTTPError lowerCAmelCase_ : Any = {} # Download this model to make sure it's in the cache. lowerCAmelCase_ : Optional[Any] = WavaVecaFeatureExtractor.from_pretrained("""hf-internal-testing/tiny-random-wav2vec2""" ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch("""requests.Session.request""" , return_value=lowerCamelCase ) as mock_head: lowerCAmelCase_ : Union[str, Any] = WavaVecaFeatureExtractor.from_pretrained("""hf-internal-testing/tiny-random-wav2vec2""" ) # This check we did call the fake head request mock_head.assert_called() def __lowercase ( self : Dict ) -> Any: # This test is for deprecated behavior and can be removed in v5 lowerCAmelCase_ : List[str] = WavaVecaFeatureExtractor.from_pretrained( """https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json""" ) @is_staging_test class __snake_case ( unittest.TestCase): """simple docstring""" @classmethod def __lowercase ( cls : Tuple ) -> str: lowerCAmelCase_ : Dict = TOKEN HfFolder.save_token(lowerCamelCase ) @classmethod def __lowercase ( cls : Any ) -> Any: try: delete_repo(token=cls._token , repo_id="""test-feature-extractor""" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="""valid_org/test-feature-extractor-org""" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="""test-dynamic-feature-extractor""" ) except HTTPError: pass def __lowercase ( self : int ) -> str: lowerCAmelCase_ : Tuple = WavaVecaFeatureExtractor.from_pretrained(lowerCamelCase ) feature_extractor.push_to_hub("""test-feature-extractor""" , use_auth_token=self._token ) lowerCAmelCase_ : Optional[Any] = WavaVecaFeatureExtractor.from_pretrained(F'{USER}/test-feature-extractor' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCamelCase , getattr(lowerCamelCase , lowerCamelCase ) ) # Reset repo delete_repo(token=self._token , repo_id="""test-feature-extractor""" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCamelCase , repo_id="""test-feature-extractor""" , push_to_hub=lowerCamelCase , use_auth_token=self._token ) lowerCAmelCase_ : Any = WavaVecaFeatureExtractor.from_pretrained(F'{USER}/test-feature-extractor' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCamelCase , getattr(lowerCamelCase , lowerCamelCase ) ) def __lowercase ( self : Optional[Any] ) -> int: lowerCAmelCase_ : Union[str, Any] = WavaVecaFeatureExtractor.from_pretrained(lowerCamelCase ) feature_extractor.push_to_hub("""valid_org/test-feature-extractor""" , use_auth_token=self._token ) lowerCAmelCase_ : Optional[int] = WavaVecaFeatureExtractor.from_pretrained("""valid_org/test-feature-extractor""" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCamelCase , getattr(lowerCamelCase , lowerCamelCase ) ) # Reset repo delete_repo(token=self._token , repo_id="""valid_org/test-feature-extractor""" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCamelCase , repo_id="""valid_org/test-feature-extractor-org""" , push_to_hub=lowerCamelCase , use_auth_token=self._token ) lowerCAmelCase_ : Optional[int] = WavaVecaFeatureExtractor.from_pretrained("""valid_org/test-feature-extractor-org""" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCamelCase , getattr(lowerCamelCase , lowerCamelCase ) ) def __lowercase ( self : Optional[Any] ) -> Any: CustomFeatureExtractor.register_for_auto_class() lowerCAmelCase_ : Dict = CustomFeatureExtractor.from_pretrained(lowerCamelCase ) feature_extractor.push_to_hub("""test-dynamic-feature-extractor""" , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( feature_extractor.auto_map , {"""AutoFeatureExtractor""": """custom_feature_extraction.CustomFeatureExtractor"""} , ) lowerCAmelCase_ : Union[str, Any] = AutoFeatureExtractor.from_pretrained( F'{USER}/test-dynamic-feature-extractor' , trust_remote_code=lowerCamelCase ) # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module self.assertEqual(new_feature_extractor.__class__.__name__ , """CustomFeatureExtractor""" )
120
0
"""simple docstring""" from __future__ import annotations lowercase__ : Optional[int] = [ [-1, 0], # left [0, -1], # down [1, 0], # right [0, 1], # up ] def __lowercase ( _a , _a , _a , _a , _a , ): snake_case_ : Optional[int] = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_a ) ) ] # the reference grid snake_case_ : Optional[int] = 1 snake_case_ : Optional[int] = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_a ) ) ] # the action grid snake_case_ : Tuple = init[0] snake_case_ : Optional[int] = init[1] snake_case_ : Union[str, Any] = 0 snake_case_ : str = g + heuristic[x][y] # cost from starting cell to destination cell snake_case_ : str = [[f, g, x, y]] snake_case_ : Optional[Any] = False # flag that is set when search is complete snake_case_ : int = False # flag set if we can't find expand while not found and not resign: if len(_a ) == 0: raise ValueError('''Algorithm is unable to find solution''' ) else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() snake_case_ : Dict = cell.pop() snake_case_ : int = next_cell[2] snake_case_ : Optional[Any] = next_cell[3] snake_case_ : Optional[Any] = next_cell[1] if x == goal[0] and y == goal[1]: snake_case_ : Union[str, Any] = True else: for i in range(len(_a ) ): # to try out different valid actions snake_case_ : Optional[int] = x + DIRECTIONS[i][0] snake_case_ : int = y + DIRECTIONS[i][1] if xa >= 0 and xa < len(_a ) and ya >= 0 and ya < len(grid[0] ): if closed[xa][ya] == 0 and grid[xa][ya] == 0: snake_case_ : List[Any] = g + cost snake_case_ : List[Any] = ga + heuristic[xa][ya] cell.append([fa, ga, xa, ya] ) snake_case_ : str = 1 snake_case_ : int = i snake_case_ : Optional[int] = [] snake_case_ : List[str] = goal[0] snake_case_ : List[str] = goal[1] invpath.append([x, y] ) # we get the reverse path from here while x != init[0] or y != init[1]: snake_case_ : int = x - DIRECTIONS[action[x][y]][0] snake_case_ : int = y - DIRECTIONS[action[x][y]][1] snake_case_ : List[str] = xa snake_case_ : Any = ya invpath.append([x, y] ) snake_case_ : Any = [] for i in range(len(_a ) ): path.append(invpath[len(_a ) - 1 - i] ) return path, action if __name__ == "__main__": lowercase__ : str = [ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], ] lowercase__ : Optional[Any] = [0, 0] # all coordinates are given in format [y,x] lowercase__ : Optional[int] = [len(grid) - 1, len(grid[0]) - 1] lowercase__ : Tuple = 1 # the cost map which pushes the path closer to the goal lowercase__ : Optional[int] = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): lowercase__ : Tuple = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: # added extra penalty in the heuristic map lowercase__ : int = 99 lowercase__ ,lowercase__ : Dict = search(grid, init, goal, cost, heuristic) print('''ACTION MAP''') for i in range(len(action)): print(action[i]) for i in range(len(path)): print(path[i])
155
"""simple docstring""" import gc import unittest from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline from diffusers.utils import is_flax_available, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class _UpperCAmelCase ( unittest.TestCase): def _snake_case ( self : List[str] ): # clean up the VRAM after each test super().tearDown() gc.collect() def _snake_case ( self : List[str] ): snake_case_, snake_case_ : List[str] = FlaxStableDiffusionPipeline.from_pretrained( '''stabilityai/stable-diffusion-2''' , revision='''bf16''' , dtype=jnp.bfloataa , ) snake_case_ : Union[str, Any] = '''A painting of a squirrel eating a burger''' snake_case_ : Tuple = jax.device_count() snake_case_ : Dict = num_samples * [prompt] snake_case_ : Tuple = sd_pipe.prepare_inputs(lowercase_ ) snake_case_ : str = replicate(lowercase_ ) snake_case_ : Any = shard(lowercase_ ) snake_case_ : Optional[int] = jax.random.PRNGKey(0 ) snake_case_ : Union[str, Any] = jax.random.split(lowercase_ , jax.device_count() ) snake_case_ : Optional[Any] = sd_pipe(lowercase_ , lowercase_ , lowercase_ , num_inference_steps=25 , jit=lowercase_ )[0] assert images.shape == (jax.device_count(), 1, 768, 768, 3) snake_case_ : str = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) snake_case_ : str = images[0, 253:256, 253:256, -1] snake_case_ : Dict = jnp.asarray(jax.device_get(image_slice.flatten() ) ) snake_case_ : int = jnp.array([0.42_38, 0.44_14, 0.43_95, 0.44_53, 0.46_29, 0.45_90, 0.45_31, 0.4_55_08, 0.45_12] ) print(f"output_slice: {output_slice}" ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2 def _snake_case ( self : str ): snake_case_ : Optional[Any] = '''stabilityai/stable-diffusion-2''' snake_case_, snake_case_ : Union[str, Any] = FlaxDPMSolverMultistepScheduler.from_pretrained(lowercase_ , subfolder='''scheduler''' ) snake_case_, snake_case_ : Optional[int] = FlaxStableDiffusionPipeline.from_pretrained( lowercase_ , scheduler=lowercase_ , revision='''bf16''' , dtype=jnp.bfloataa , ) snake_case_ : List[Any] = scheduler_params snake_case_ : int = '''A painting of a squirrel eating a burger''' snake_case_ : str = jax.device_count() snake_case_ : Union[str, Any] = num_samples * [prompt] snake_case_ : int = sd_pipe.prepare_inputs(lowercase_ ) snake_case_ : List[str] = replicate(lowercase_ ) snake_case_ : List[Any] = shard(lowercase_ ) snake_case_ : int = jax.random.PRNGKey(0 ) snake_case_ : Tuple = jax.random.split(lowercase_ , jax.device_count() ) snake_case_ : int = sd_pipe(lowercase_ , lowercase_ , lowercase_ , num_inference_steps=25 , jit=lowercase_ )[0] assert images.shape == (jax.device_count(), 1, 768, 768, 3) snake_case_ : Optional[int] = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) snake_case_ : List[str] = images[0, 253:256, 253:256, -1] snake_case_ : Optional[int] = jnp.asarray(jax.device_get(image_slice.flatten() ) ) snake_case_ : Optional[int] = jnp.array([0.43_36, 0.4_29_69, 0.44_53, 0.41_99, 0.42_97, 0.45_31, 0.44_34, 0.44_34, 0.42_97] ) print(f"output_slice: {output_slice}" ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2
155
1
'''simple docstring''' import argparse import torch from transformers import FunnelBaseModel, FunnelConfig, FunnelModel, load_tf_weights_in_funnel from transformers.utils import logging logging.set_verbosity_info() def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> Any: """simple docstring""" A_ : Any = FunnelConfig.from_json_file(a_ ) print(F"Building PyTorch model from configuration: {config}" ) A_ : Union[str, Any] = FunnelBaseModel(a_ ) if base_model else FunnelModel(a_ ) # Load weights from tf checkpoint load_tf_weights_in_funnel(a_ , a_ , a_ ) # Save pytorch-model print(F"Save PyTorch model to {pytorch_dump_path}" ) torch.save(model.state_dict() , a_ ) if __name__ == "__main__": UpperCamelCase__ : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help='The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.', ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) parser.add_argument( '--base_model', action='store_true', help='Whether you want just the base model (no decoder) or not.' ) UpperCamelCase__ : Tuple = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path, args.base_model )
344
'''simple docstring''' from typing import Dict import numpy as np import torch from . import residue_constants as rc from .tensor_utils import tensor_tree_map, tree_map def UpperCAmelCase ( a_ ) -> Dict[str, torch.Tensor]: """simple docstring""" A_ : List[str] = [] A_ : Dict = [] A_ : List[Any] = [] for rt in rc.restypes: A_ : Tuple = rc.restype_name_to_atomaa_names[rc.restype_atoa[rt]] restype_atomaa_to_atomaa_list.append([(rc.atom_order[name] if name else 0) for name in atom_names] ) A_ : Union[str, Any] = {name: i for i, name in enumerate(a_ )} restype_atomaa_to_atomaa_list.append( [(atom_name_to_idxaa[name] if name in atom_name_to_idxaa else 0) for name in rc.atom_types] ) restype_atomaa_mask_list.append([(1.0 if name else 0.0) for name in atom_names] ) # Add dummy mapping for restype 'UNK' restype_atomaa_to_atomaa_list.append([0] * 1_4 ) restype_atomaa_to_atomaa_list.append([0] * 3_7 ) restype_atomaa_mask_list.append([0.0] * 1_4 ) A_ : Tuple = torch.tensor( a_ , dtype=torch.intaa , device=protein["""aatype"""].device , ) A_ : Optional[int] = torch.tensor( a_ , dtype=torch.intaa , device=protein["""aatype"""].device , ) A_ : List[Any] = torch.tensor( a_ , dtype=torch.floataa , device=protein["""aatype"""].device , ) A_ : Optional[int] = protein["""aatype"""].to(torch.long ) # create the mapping for (residx, atom14) --> atom37, i.e. an array # with shape (num_res, 14) containing the atom37 indices for this protein A_ : Dict = restype_atomaa_to_atomaa[protein_aatype] A_ : Optional[Any] = restype_atomaa_mask[protein_aatype] A_ : Any = residx_atomaa_mask A_ : List[str] = residx_atomaa_to_atomaa.long() # create the gather indices for mapping back A_ : Tuple = restype_atomaa_to_atomaa[protein_aatype] A_ : Tuple = residx_atomaa_to_atomaa.long() # create the corresponding mask A_ : Optional[Any] = torch.zeros([2_1, 3_7] , dtype=torch.floataa , device=protein["""aatype"""].device ) for restype, restype_letter in enumerate(rc.restypes ): A_ : Optional[Any] = rc.restype_atoa[restype_letter] A_ : Any = rc.residue_atoms[restype_name] for atom_name in atom_names: A_ : Any = rc.atom_order[atom_name] A_ : Optional[int] = 1 A_ : Optional[int] = restype_atomaa_mask[protein_aatype] A_ : Dict = residx_atomaa_mask return protein def UpperCAmelCase ( a_ ) -> Dict[str, np.ndarray]: """simple docstring""" A_ : Union[str, Any] = tree_map(lambda a_ : torch.tensor(a_ , device=batch["""aatype"""].device ) , a_ , np.ndarray ) A_ : Optional[int] = tensor_tree_map(lambda a_ : np.array(a_ ) , make_atomaa_masks(a_ ) ) return out
344
1
"""simple docstring""" def _A ( UpperCamelCase_ : int) -> None: '''simple docstring''' __lowercase = generate_pascal_triangle(UpperCamelCase_) for row_idx in range(UpperCamelCase_): # Print left spaces for _ in range(num_rows - row_idx - 1): print(end=" ") # Print row values for col_idx in range(row_idx + 1): if col_idx != row_idx: print(triangle[row_idx][col_idx], end=" ") else: print(triangle[row_idx][col_idx], end="") print() def _A ( UpperCamelCase_ : int) -> list[list[int]]: '''simple docstring''' if not isinstance(UpperCamelCase_, UpperCamelCase_): raise TypeError("The input value of 'num_rows' should be 'int'") if num_rows == 0: return [] elif num_rows < 0: raise ValueError( "The input value of 'num_rows' should be greater than or equal to 0") __lowercase = [] for current_row_idx in range(UpperCamelCase_): __lowercase = populate_current_row(UpperCamelCase_, UpperCamelCase_) triangle.append(UpperCamelCase_) return triangle def _A ( UpperCamelCase_ : list[list[int]], UpperCamelCase_ : int) -> list[int]: '''simple docstring''' __lowercase = [-1] * (current_row_idx + 1) # first and last elements of current row are equal to 1 __lowercase ,__lowercase = 1, 1 for current_col_idx in range(1, UpperCamelCase_): calculate_current_element( UpperCamelCase_, UpperCamelCase_, UpperCamelCase_, UpperCamelCase_) return current_row def _A ( UpperCamelCase_ : list[list[int]], UpperCamelCase_ : list[int], UpperCamelCase_ : int, UpperCamelCase_ : int, ) -> None: '''simple docstring''' __lowercase = triangle[current_row_idx - 1][current_col_idx - 1] __lowercase = triangle[current_row_idx - 1][current_col_idx] __lowercase = above_to_left_elt + above_to_right_elt def _A ( UpperCamelCase_ : int) -> list[list[int]]: '''simple docstring''' if not isinstance(UpperCamelCase_, UpperCamelCase_): raise TypeError("The input value of 'num_rows' should be 'int'") if num_rows == 0: return [] elif num_rows < 0: raise ValueError( "The input value of 'num_rows' should be greater than or equal to 0") __lowercase = [[1]] for row_index in range(1, UpperCamelCase_): __lowercase = [0] + result[-1] + [0] __lowercase = row_index + 1 # Calculate the number of distinct elements in a row __lowercase = sum(divmod(UpperCamelCase_, 2)) __lowercase = [ temp_row[i - 1] + temp_row[i] for i in range(1, distinct_elements + 1) ] __lowercase = row_first_half[: (row_index + 1) // 2] row_second_half.reverse() __lowercase = row_first_half + row_second_half result.append(UpperCamelCase_) return result def _A ( ) -> None: '''simple docstring''' from collections.abc import Callable from timeit import timeit def benchmark_a_function(UpperCamelCase_ : Callable, UpperCamelCase_ : int) -> None: __lowercase = F"""{func.__name__}({value})""" __lowercase = timeit(F"""__main__.{call}""", setup="import __main__") # print(f"{call:38} = {func(value)} -- {timing:.4f} seconds") print(F"""{call:38} -- {timing:.4f} seconds""") for value in range(15): # (1, 7, 14): for func in (generate_pascal_triangle, generate_pascal_triangle_optimized): benchmark_a_function(UpperCamelCase_, UpperCamelCase_) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
144
"""simple docstring""" import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class _lowerCAmelCase ( lowercase ): """simple docstring""" def __init__( self : List[str], UpperCAmelCase__ : Union[str, Any], UpperCAmelCase__ : Any, UpperCAmelCase__ : Dict ): __lowercase = dataset __lowercase = process __lowercase = params def __len__( self : str ): return len(self.dataset ) def __getitem__( self : List[Any], UpperCAmelCase__ : int ): __lowercase = self.dataset[i] __lowercase = self.process(UpperCAmelCase__, **self.params ) return processed class _lowerCAmelCase ( lowercase ): """simple docstring""" def __init__( self : Optional[Any], UpperCAmelCase__ : Dict, UpperCAmelCase__ : Any, UpperCAmelCase__ : Union[str, Any], UpperCAmelCase__ : List[Any]=None ): __lowercase = loader __lowercase = infer __lowercase = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether __lowercase = None __lowercase = loader_batch_size # Internal bookkeeping __lowercase = None __lowercase = None def __len__( self : str ): return len(self.loader ) def __iter__( self : List[str] ): __lowercase = iter(self.loader ) return self def _lowercase ( self : Union[str, Any] ): if isinstance(self._loader_batch_data, torch.Tensor ): # Batch data is simple tensor, just fetch the slice __lowercase = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) __lowercase = {} for k, element in self._loader_batch_data.items(): if isinstance(UpperCAmelCase__, UpperCAmelCase__ ): # Convert ModelOutput to tuple first __lowercase = element.to_tuple() if isinstance(element[0], torch.Tensor ): __lowercase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0], np.ndarray ): __lowercase = tuple(np.expand_dims(el[self._loader_batch_index], 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(UpperCAmelCase__, UpperCAmelCase__ ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0], torch.Tensor ): __lowercase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0], np.ndarray ): __lowercase = tuple(np.expand_dims(el[self._loader_batch_index], 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around __lowercase = None elif isinstance(element[self._loader_batch_index], torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers __lowercase = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index], np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers __lowercase = np.expand_dims(element[self._loader_batch_index], 0 ) else: # This is typically a list, so no need to `unsqueeze`. __lowercase = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 __lowercase = self._loader_batch_data.__class__(UpperCAmelCase__ ) self._loader_batch_index += 1 return result def _lowercase ( self : Tuple ): if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch __lowercase = next(self.iterator ) __lowercase = self.infer(UpperCAmelCase__, **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(UpperCAmelCase__, torch.Tensor ): __lowercase = processed else: __lowercase = list(processed.keys() )[0] __lowercase = processed[key] if isinstance(UpperCAmelCase__, UpperCAmelCase__ ): __lowercase = len(UpperCAmelCase__ ) else: __lowercase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. __lowercase = observed_batch_size # Setting internal index to unwrap the batch __lowercase = processed __lowercase = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class _lowerCAmelCase ( lowercase ): """simple docstring""" def __init__( self : Union[str, Any], UpperCAmelCase__ : Any, UpperCAmelCase__ : List[str], UpperCAmelCase__ : int, UpperCAmelCase__ : str=None ): super().__init__(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) def __iter__( self : str ): __lowercase = iter(self.loader ) __lowercase = None return self def _lowercase ( self : int ): if self.subiterator is None: __lowercase = self.infer(next(self.iterator ), **self.params ) try: # Try to return next item __lowercase = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators __lowercase = self.infer(next(self.iterator ), **self.params ) __lowercase = next(self.subiterator ) return processed class _lowerCAmelCase ( lowercase ): """simple docstring""" def __iter__( self : int ): __lowercase = iter(self.loader ) return self def _lowercase ( self : List[str] ): # Extremely similar to PipelineIterator in its unpacking mechanism # BUT, we have an extra required item which is the presence of `is_last` # That is because everything is flattened by `PipelineChunkIterator` we # need to keep track of how to regroup here in the original `process` # boundaries so that `process` and `postprocess` see the same data. # This iterator accumulates items (possibly while unbatching) until it # its a `is_last` and then just passes it on to the caller. __lowercase = False __lowercase = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: __lowercase = self.loader_batch_item() __lowercase = item.pop("is_last" ) accumulator.append(UpperCAmelCase__ ) if is_last: return accumulator while not is_last: __lowercase = self.infer(next(self.iterator ), **self.params ) if self.loader_batch_size is not None: if isinstance(UpperCAmelCase__, torch.Tensor ): __lowercase = processed else: __lowercase = list(processed.keys() )[0] __lowercase = processed[key] if isinstance(UpperCAmelCase__, UpperCAmelCase__ ): __lowercase = len(UpperCAmelCase__ ) else: __lowercase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. __lowercase = observed_batch_size __lowercase = processed __lowercase = 0 while self._loader_batch_index < self.loader_batch_size: __lowercase = self.loader_batch_item() __lowercase = item.pop("is_last" ) accumulator.append(UpperCAmelCase__ ) if is_last: return accumulator else: __lowercase = processed __lowercase = item.pop("is_last" ) accumulator.append(UpperCAmelCase__ ) return accumulator class _lowerCAmelCase ( lowercase ): """simple docstring""" def __init__( self : List[Any], UpperCAmelCase__ : Dataset, UpperCAmelCase__ : str ): __lowercase = dataset __lowercase = key def __len__( self : Optional[Any] ): return len(self.dataset ) def __getitem__( self : Union[str, Any], UpperCAmelCase__ : Any ): return self.dataset[i][self.key] class _lowerCAmelCase ( lowercase ): """simple docstring""" def __init__( self : str, UpperCAmelCase__ : Dataset, UpperCAmelCase__ : str, UpperCAmelCase__ : str ): __lowercase = dataset __lowercase = keya __lowercase = keya def __len__( self : Optional[int] ): return len(self.dataset ) def __getitem__( self : Dict, UpperCAmelCase__ : Tuple ): return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
144
1
'''simple docstring''' # NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from ...utils import deprecate from ..controlnet.multicontrolnet import MultiControlNetModel # noqa: F401 from ..controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline # noqa: F401 deprecate( "stable diffusion controlnet", "0.22.0", "Importing `StableDiffusionControlNetPipeline` or `MultiControlNetModel` from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import StableDiffusionControlNetPipeline` instead.", standard_warn=False, stacklevel=3, )
35
'''simple docstring''' import itertools import os import random import tempfile import unittest import numpy as np from datasets import load_dataset from transformers import is_speech_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_speech_available(): from transformers import WhisperFeatureExtractor if is_torch_available(): import torch UpperCAmelCase : int = random.Random() def a__ ( a__ , a__=1.0 , a__=None , a__=None ): """simple docstring""" if rng is None: __SCREAMING_SNAKE_CASE = global_rng __SCREAMING_SNAKE_CASE = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch @require_torchaudio class lowerCAmelCase__ ( unittest.TestCase ): """simple docstring""" def __init__( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : str=7 , __SCREAMING_SNAKE_CASE : List[str]=400 , __SCREAMING_SNAKE_CASE : Any=2_000 , __SCREAMING_SNAKE_CASE : List[str]=10 , __SCREAMING_SNAKE_CASE : Optional[int]=160 , __SCREAMING_SNAKE_CASE : List[str]=8 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.0 , __SCREAMING_SNAKE_CASE : Dict=4_000 , __SCREAMING_SNAKE_CASE : Optional[int]=False , __SCREAMING_SNAKE_CASE : List[Any]=True , ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = min_seq_length __SCREAMING_SNAKE_CASE = max_seq_length __SCREAMING_SNAKE_CASE = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) __SCREAMING_SNAKE_CASE = padding_value __SCREAMING_SNAKE_CASE = sampling_rate __SCREAMING_SNAKE_CASE = return_attention_mask __SCREAMING_SNAKE_CASE = do_normalize __SCREAMING_SNAKE_CASE = feature_size __SCREAMING_SNAKE_CASE = chunk_length __SCREAMING_SNAKE_CASE = hop_length def UpperCAmelCase__ ( self : Dict ) -> Dict: """simple docstring""" return { "feature_size": self.feature_size, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def UpperCAmelCase__ ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Union[str, Any]=False , __SCREAMING_SNAKE_CASE : Optional[Any]=False ) -> Union[str, Any]: """simple docstring""" def _flatten(__SCREAMING_SNAKE_CASE : Dict ): return list(itertools.chain(*__SCREAMING_SNAKE_CASE ) ) if equal_length: __SCREAMING_SNAKE_CASE = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size __SCREAMING_SNAKE_CASE = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: __SCREAMING_SNAKE_CASE = [np.asarray(__SCREAMING_SNAKE_CASE ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowerCAmelCase__ ( a , unittest.TestCase ): """simple docstring""" lowerCAmelCase__ = WhisperFeatureExtractor if is_speech_available() else None def UpperCAmelCase__ ( self : str ) -> Union[str, Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = WhisperFeatureExtractionTester(self ) def UpperCAmelCase__ ( self : str ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __SCREAMING_SNAKE_CASE = feat_extract_first.save_pretrained(__SCREAMING_SNAKE_CASE )[0] check_json_file_has_correct_format(__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = self.feature_extraction_class.from_pretrained(__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = feat_extract_first.to_dict() __SCREAMING_SNAKE_CASE = feat_extract_second.to_dict() __SCREAMING_SNAKE_CASE = feat_extract_first.mel_filters __SCREAMING_SNAKE_CASE = feat_extract_second.mel_filters self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def UpperCAmelCase__ ( self : List[str] ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: __SCREAMING_SNAKE_CASE = os.path.join(__SCREAMING_SNAKE_CASE , """feat_extract.json""" ) feat_extract_first.to_json_file(__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = self.feature_extraction_class.from_json_file(__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = feat_extract_first.to_dict() __SCREAMING_SNAKE_CASE = feat_extract_second.to_dict() __SCREAMING_SNAKE_CASE = feat_extract_first.mel_filters __SCREAMING_SNAKE_CASE = feat_extract_second.mel_filters self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def UpperCAmelCase__ ( self : List[Any] ) -> Union[str, Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 __SCREAMING_SNAKE_CASE = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] __SCREAMING_SNAKE_CASE = [np.asarray(__SCREAMING_SNAKE_CASE ) for speech_input in speech_inputs] # Test feature size __SCREAMING_SNAKE_CASE = feature_extractor(__SCREAMING_SNAKE_CASE , padding="""max_length""" , return_tensors="""np""" ).input_features self.assertTrue(input_features.ndim == 3 ) self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames ) self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size ) # Test not batched input __SCREAMING_SNAKE_CASE = feature_extractor(speech_inputs[0] , return_tensors="""np""" ).input_features __SCREAMING_SNAKE_CASE = feature_extractor(np_speech_inputs[0] , return_tensors="""np""" ).input_features self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , atol=1E-3 ) ) # Test batched __SCREAMING_SNAKE_CASE = feature_extractor(__SCREAMING_SNAKE_CASE , return_tensors="""np""" ).input_features __SCREAMING_SNAKE_CASE = feature_extractor(__SCREAMING_SNAKE_CASE , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. __SCREAMING_SNAKE_CASE = [floats_list((1, x) )[0] for x in (800, 800, 800)] __SCREAMING_SNAKE_CASE = np.asarray(__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = feature_extractor(__SCREAMING_SNAKE_CASE , return_tensors="""np""" ).input_features __SCREAMING_SNAKE_CASE = feature_extractor(__SCREAMING_SNAKE_CASE , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , atol=1E-3 ) ) # Test truncation required __SCREAMING_SNAKE_CASE = [floats_list((1, x) )[0] for x in range(200 , (feature_extractor.n_samples + 500) , 200 )] __SCREAMING_SNAKE_CASE = [np.asarray(__SCREAMING_SNAKE_CASE ) for speech_input in speech_inputs] __SCREAMING_SNAKE_CASE = [x[: feature_extractor.n_samples] for x in speech_inputs] __SCREAMING_SNAKE_CASE = [np.asarray(__SCREAMING_SNAKE_CASE ) for speech_input in speech_inputs_truncated] __SCREAMING_SNAKE_CASE = feature_extractor(__SCREAMING_SNAKE_CASE , return_tensors="""np""" ).input_features __SCREAMING_SNAKE_CASE = feature_extractor(__SCREAMING_SNAKE_CASE , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , atol=1E-3 ) ) def UpperCAmelCase__ ( self : Dict ) -> Optional[int]: """simple docstring""" import torch __SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __SCREAMING_SNAKE_CASE = np.random.rand(100 , 32 ).astype(np.floataa ) __SCREAMING_SNAKE_CASE = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: __SCREAMING_SNAKE_CASE = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""np""" ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) __SCREAMING_SNAKE_CASE = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""pt""" ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def UpperCAmelCase__ ( self : str , __SCREAMING_SNAKE_CASE : Tuple ) -> Any: """simple docstring""" __SCREAMING_SNAKE_CASE = load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) # automatic decoding with librispeech __SCREAMING_SNAKE_CASE = ds.sort("""id""" ).select(range(__SCREAMING_SNAKE_CASE ) )[:num_samples]["""audio"""] return [x["array"] for x in speech_samples] def UpperCAmelCase__ ( self : Tuple ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = torch.tensor( [ 0.1193, -0.0946, -0.1098, -0.0196, 0.0225, -0.0690, -0.1736, 0.0951, 0.0971, -0.0817, -0.0702, 0.0162, 0.0260, 0.0017, -0.0192, -0.1678, 0.0709, -0.1867, -0.0655, -0.0274, -0.0234, -0.1884, -0.0516, -0.0554, -0.0274, -0.1425, -0.1423, 0.0837, 0.0377, -0.0854 ] ) # fmt: on __SCREAMING_SNAKE_CASE = self._load_datasamples(1 ) __SCREAMING_SNAKE_CASE = WhisperFeatureExtractor() __SCREAMING_SNAKE_CASE = feature_extractor(__SCREAMING_SNAKE_CASE , return_tensors="""pt""" ).input_features self.assertEqual(input_features.shape , (1, 80, 3_000) ) self.assertTrue(torch.allclose(input_features[0, 0, :30] , __SCREAMING_SNAKE_CASE , atol=1E-4 ) ) def UpperCAmelCase__ ( self : List[Any] ) -> Tuple: """simple docstring""" __SCREAMING_SNAKE_CASE = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __SCREAMING_SNAKE_CASE = self._load_datasamples(1 )[0] __SCREAMING_SNAKE_CASE = ((audio - audio.min()) / (audio.max() - audio.min())) * 65_535 # Rescale to [0, 65535] to show issue __SCREAMING_SNAKE_CASE = feat_extract.zero_mean_unit_var_norm([audio] , attention_mask=__SCREAMING_SNAKE_CASE )[0] self.assertTrue(np.all(np.mean(__SCREAMING_SNAKE_CASE ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(__SCREAMING_SNAKE_CASE ) - 1 ) < 1E-3 ) )
267
0
from math import ceil def _UpperCamelCase (a__ :Any = 1001 ): """simple docstring""" UpperCamelCase__ = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): UpperCamelCase__ = 2 * i + 1 UpperCamelCase__ = 2 * i UpperCamelCase__ = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: UpperCamelCase__ = int(sys.argv[1]) print(solution(n)) except ValueError: print("Invalid entry - please enter a number")
371
from datetime import datetime as dt import os from github import Github UpperCamelCase__ = [ "good first issue", "good second issue", "good difficult issue", "feature request", "new model", "wip", ] def _UpperCamelCase (): """simple docstring""" UpperCamelCase__ = Github(os.environ["""GITHUB_TOKEN"""] ) UpperCamelCase__ = g.get_repo("""huggingface/transformers""" ) UpperCamelCase__ = repo.get_issues(state="""open""" ) for issue in open_issues: UpperCamelCase__ = sorted([comment for comment in issue.get_comments()] , key=lambda a__ : i.created_at , reverse=a__ ) UpperCamelCase__ = comments[0] if len(a__ ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.") issue.edit(state="""closed""" ) elif ( (dt.utcnow() - issue.updated_at).days > 23 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would add stale comment to {issue.number}") issue.create_comment( """This issue has been automatically marked as stale because it has not had """ """recent activity. If you think this still needs to be addressed """ """please comment on this thread.\n\nPlease note that issues that do not follow the """ """[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) """ """are likely to be ignored.""" ) if __name__ == "__main__": main()
87
0
"""simple docstring""" from maths.prime_check import is_prime def _lowerCAmelCase ( UpperCamelCase_ ): if not isinstance(UpperCamelCase_ , UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = f"Input value of [number={number}] must be an integer" raise TypeError(UpperCamelCase_ ) if is_prime(UpperCamelCase_ ) and is_prime(number + 2 ): return number + 2 else: return -1 if __name__ == "__main__": import doctest doctest.testmod()
100
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _A : Dict ={ '''configuration_swinv2''': ['''SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Swinv2Config'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A : List[Any] =[ '''SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Swinv2ForImageClassification''', '''Swinv2ForMaskedImageModeling''', '''Swinv2Model''', '''Swinv2PreTrainedModel''', ] if TYPE_CHECKING: from .configuration_swinva import SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinvaConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swinva import ( SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST, SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel, SwinvaPreTrainedModel, ) else: import sys _A : Optional[Any] =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
41
0
from collections import defaultdict class a : """simple docstring""" def __init__( self , lowerCAmelCase_ , lowerCAmelCase_ ) -> Optional[Any]: _A = total # total no of tasks (N) # DP table will have a dimension of (2^M)*N # initially all values are set to -1 _A = [ [-1 for i in range(total + 1 )] for j in range(2 ** len(lowerCAmelCase_ ) ) ] _A = defaultdict(lowerCAmelCase_ ) # stores the list of persons for each task # final_mask is used to check if all persons are included by setting all bits # to 1 _A = (1 << len(lowerCAmelCase_ )) - 1 def UpperCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ ) -> str: # if mask == self.finalmask all persons are distributed tasks, return 1 if mask == self.final_mask: return 1 # if not everyone gets the task and no more tasks are available, return 0 if task_no > self.total_tasks: return 0 # if case already considered if self.dp[mask][task_no] != -1: return self.dp[mask][task_no] # Number of ways when we don't this task in the arrangement _A = self.count_ways_until(lowerCAmelCase_ , task_no + 1 ) # now assign the tasks one by one to all possible persons and recursively # assign for the remaining tasks. if task_no in self.task: for p in self.task[task_no]: # if p is already given a task if mask & (1 << p): continue # assign this task to p and change the mask value. And recursively # assign tasks with the new mask value. total_ways_util += self.count_ways_until(mask | (1 << p) , task_no + 1 ) # save the value. _A = total_ways_util return self.dp[mask][task_no] def UpperCAmelCase ( self , lowerCAmelCase_ ) -> Optional[int]: # Store the list of persons for each task for i in range(len(lowerCAmelCase_ ) ): for j in task_performed[i]: self.task[j].append(lowerCAmelCase_ ) # call the function to fill the DP table, final answer is stored in dp[0][1] return self.count_ways_until(0 , 1 ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE = 5 # total no of tasks (the value of N) # the list of tasks that can be done by M persons. _SCREAMING_SNAKE_CASE = [[1, 3, 4], [1, 2, 5], [3, 4]] print( AssignmentUsingBitmask(task_performed, total_tasks).count_no_of_ways( task_performed ) )
81
# 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. import re from ..models.auto import AutoProcessor from ..models.vision_encoder_decoder import VisionEncoderDecoderModel from ..utils import is_vision_available from .base import PipelineTool if is_vision_available(): from PIL import Image class a ( __lowerCAmelCase ): """simple docstring""" lowerCamelCase :Tuple = '''naver-clova-ix/donut-base-finetuned-docvqa''' lowerCamelCase :Union[str, Any] = ( '''This is a tool that answers a question about an document (pdf). It takes an input named `document` which ''' '''should be the document containing the information, as well as a `question` that is the question about the ''' '''document. It returns a text that contains the answer to the question.''' ) lowerCamelCase :List[str] = '''document_qa''' lowerCamelCase :Union[str, Any] = AutoProcessor lowerCamelCase :str = VisionEncoderDecoderModel lowerCamelCase :str = ['''image''', '''text'''] lowerCamelCase :List[str] = ['''text'''] def __init__( self , *lowerCAmelCase_ , **lowerCAmelCase_ ) -> Optional[Any]: if not is_vision_available(): raise ValueError("""Pillow must be installed to use the DocumentQuestionAnsweringTool.""" ) super().__init__(*lowerCAmelCase_ , **lowerCAmelCase_ ) def UpperCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ ) -> Any: _A = """<s_docvqa><s_question>{user_input}</s_question><s_answer>""" _A = task_prompt.replace("""{user_input}""" , lowerCAmelCase_ ) _A = self.pre_processor.tokenizer( lowerCAmelCase_ , add_special_tokens=lowerCAmelCase_ , return_tensors="""pt""" ).input_ids _A = self.pre_processor(lowerCAmelCase_ , return_tensors="""pt""" ).pixel_values return {"decoder_input_ids": decoder_input_ids, "pixel_values": pixel_values} def UpperCAmelCase ( self , lowerCAmelCase_ ) -> Union[str, Any]: return self.model.generate( inputs["""pixel_values"""].to(self.device ) , decoder_input_ids=inputs["""decoder_input_ids"""].to(self.device ) , max_length=self.model.decoder.config.max_position_embeddings , early_stopping=lowerCAmelCase_ , pad_token_id=self.pre_processor.tokenizer.pad_token_id , eos_token_id=self.pre_processor.tokenizer.eos_token_id , use_cache=lowerCAmelCase_ , num_beams=1 , bad_words_ids=[[self.pre_processor.tokenizer.unk_token_id]] , return_dict_in_generate=lowerCAmelCase_ , ).sequences def UpperCAmelCase ( self , lowerCAmelCase_ ) -> Dict: _A = self.pre_processor.batch_decode(lowerCAmelCase_ )[0] _A = sequence.replace(self.pre_processor.tokenizer.eos_token , """""" ) _A = sequence.replace(self.pre_processor.tokenizer.pad_token , """""" ) _A = re.sub(r"""<.*?>""" , """""" , lowerCAmelCase_ , count=1 ).strip() # remove first task start token _A = self.pre_processor.tokenajson(lowerCAmelCase_ ) return sequence["answer"]
81
1
import os import pytest from transformers.dynamic_module_utils import get_imports _SCREAMING_SNAKE_CASE = """ import os """ _SCREAMING_SNAKE_CASE = """ def foo(): import os return False """ _SCREAMING_SNAKE_CASE = """ def foo(): def bar(): if True: import os return False return bar() """ _SCREAMING_SNAKE_CASE = """ import os try: import bar except ImportError: raise ValueError() """ _SCREAMING_SNAKE_CASE = """ import os def foo(): try: import bar except ImportError: raise ValueError() """ _SCREAMING_SNAKE_CASE = """ import os try: import bar except (ImportError, AttributeError): raise ValueError() """ _SCREAMING_SNAKE_CASE = """ import os try: import bar except ImportError as e: raise ValueError() """ _SCREAMING_SNAKE_CASE = """ import os try: import bar except: raise ValueError() """ _SCREAMING_SNAKE_CASE = """ import os try: import bar import baz except ImportError: raise ValueError() """ _SCREAMING_SNAKE_CASE = """ import os try: import bar import baz except ImportError: x = 1 raise ValueError() """ _SCREAMING_SNAKE_CASE = [ TOP_LEVEL_IMPORT, IMPORT_IN_FUNCTION, DEEPLY_NESTED_IMPORT, TOP_LEVEL_TRY_IMPORT, GENERIC_EXCEPT_IMPORT, MULTILINE_TRY_IMPORT, MULTILINE_BOTH_IMPORT, MULTIPLE_EXCEPTS_IMPORT, EXCEPT_AS_IMPORT, TRY_IMPORT_IN_FUNCTION, ] @pytest.mark.parametrize('case' , __a ) def SCREAMING_SNAKE_CASE__ ( __a , __a ): snake_case_ : Optional[int] = os.path.join(__a , 'test_file.py' ) with open(__a , 'w' ) as _tmp_file: _tmp_file.write(__a ) snake_case_ : Optional[int] = get_imports(__a ) assert parsed_imports == ["os"]
327
import copy from typing import Dict, List, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING SCREAMING_SNAKE_CASE__ = { """facebook/mask2former-swin-small-coco-instance""": ( """https://huggingface.co/facebook/mask2former-swin-small-coco-instance/blob/main/config.json""" ) # See all Mask2Former models at https://huggingface.co/models?filter=mask2former } SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) class A__ ( lowerCAmelCase__ ): lowerCAmelCase__ : Tuple = "mask2former" lowerCAmelCase__ : List[Any] = ["swin"] lowerCAmelCase__ : str = {"hidden_size": "hidden_dim"} def __init__( self : Optional[int] , _UpperCAmelCase : Optional[Dict] = None , _UpperCAmelCase : int = 2_56 , _UpperCAmelCase : int = 2_56 , _UpperCAmelCase : int = 2_56 , _UpperCAmelCase : int = 10_24 , _UpperCAmelCase : str = "relu" , _UpperCAmelCase : int = 6 , _UpperCAmelCase : int = 10 , _UpperCAmelCase : int = 8 , _UpperCAmelCase : float = 0.0 , _UpperCAmelCase : int = 20_48 , _UpperCAmelCase : bool = False , _UpperCAmelCase : bool = False , _UpperCAmelCase : int = 4 , _UpperCAmelCase : int = 2_55 , _UpperCAmelCase : int = 1_00 , _UpperCAmelCase : float = 0.1 , _UpperCAmelCase : float = 2.0 , _UpperCAmelCase : float = 5.0 , _UpperCAmelCase : float = 5.0 , _UpperCAmelCase : int = 1_25_44 , _UpperCAmelCase : float = 3.0 , _UpperCAmelCase : float = 0.75 , _UpperCAmelCase : float = 0.02 , _UpperCAmelCase : float = 1.0 , _UpperCAmelCase : bool = True , _UpperCAmelCase : List[int] = [4, 8, 16, 32] , _UpperCAmelCase : bool = None , **_UpperCAmelCase : List[str] , ) -> int: """simple docstring""" if backbone_config is None: logger.info('`backbone_config` is `None`. Initializing the config with the default `Swin` backbone.' ) __lowercase = CONFIG_MAPPING['swin']( image_size=2_24 , in_channels=3 , patch_size=4 , embed_dim=96 , depths=[2, 2, 18, 2] , num_heads=[3, 6, 12, 24] , window_size=7 , drop_path_rate=0.3 , use_absolute_embeddings=_UpperCAmelCase , out_features=['stage1', 'stage2', 'stage3', 'stage4'] , ) if isinstance(_UpperCAmelCase , _UpperCAmelCase ): __lowercase = backbone_config.pop('model_type' ) __lowercase = CONFIG_MAPPING[backbone_model_type] __lowercase = config_class.from_dict(_UpperCAmelCase ) # verify that the backbone is supported if backbone_config.model_type not in self.backbones_supported: logger.warning_once( f"""Backbone {backbone_config.model_type} is not a supported model and may not be compatible with Mask2Former. """ f"""Supported model types: {",".join(self.backbones_supported )}""" ) __lowercase = backbone_config __lowercase = feature_size __lowercase = mask_feature_size __lowercase = hidden_dim __lowercase = encoder_feedforward_dim __lowercase = activation_function __lowercase = encoder_layers __lowercase = decoder_layers __lowercase = num_attention_heads __lowercase = dropout __lowercase = dim_feedforward __lowercase = pre_norm __lowercase = enforce_input_projection __lowercase = common_stride __lowercase = ignore_value __lowercase = num_queries __lowercase = no_object_weight __lowercase = class_weight __lowercase = mask_weight __lowercase = dice_weight __lowercase = train_num_points __lowercase = oversample_ratio __lowercase = importance_sample_ratio __lowercase = init_std __lowercase = init_xavier_std __lowercase = use_auxiliary_loss __lowercase = feature_strides __lowercase = output_auxiliary_logits __lowercase = decoder_layers super().__init__(**_UpperCAmelCase ) @classmethod def a__ ( cls : Union[str, Any] , _UpperCAmelCase : PretrainedConfig , **_UpperCAmelCase : Optional[int] ) -> Dict: """simple docstring""" return cls( backbone_config=_UpperCAmelCase , **_UpperCAmelCase , ) def a__ ( self : str ) -> Dict[str, any]: """simple docstring""" __lowercase = copy.deepcopy(self.__dict__ ) __lowercase = self.backbone_config.to_dict() __lowercase = self.__class__.model_type return output
325
0
import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipaConfig, BlipaForConditionalGeneration, BlipaProcessor, BlipaVisionConfig, BlipImageProcessor, OPTConfig, TaConfig, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def UpperCAmelCase__ (): """simple docstring""" snake_case = '''https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png''' snake_case = Image.open(requests.get(UpperCamelCase_ ,stream=UpperCamelCase_ ).raw ).convert('''RGB''' ) return image def UpperCAmelCase__ (UpperCamelCase_ ): """simple docstring""" snake_case = [] # fmt: off # vision encoder rename_keys.append(('''visual_encoder.cls_token''', '''vision_model.embeddings.class_embedding''') ) rename_keys.append(('''visual_encoder.pos_embed''', '''vision_model.embeddings.position_embedding''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.weight''', '''vision_model.embeddings.patch_embedding.weight''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.bias''', '''vision_model.embeddings.patch_embedding.bias''') ) rename_keys.append(('''ln_vision.weight''', '''vision_model.post_layernorm.weight''') ) rename_keys.append(('''ln_vision.bias''', '''vision_model.post_layernorm.bias''') ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((F'''visual_encoder.blocks.{i}.norm1.weight''', F'''vision_model.encoder.layers.{i}.layer_norm1.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.norm1.bias''', F'''vision_model.encoder.layers.{i}.layer_norm1.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.norm2.weight''', F'''vision_model.encoder.layers.{i}.layer_norm2.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.norm2.bias''', F'''vision_model.encoder.layers.{i}.layer_norm2.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.qkv.weight''', F'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.proj.weight''', F'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',) ) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.proj.bias''', F'''vision_model.encoder.layers.{i}.self_attn.projection.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc1.weight''', F'''vision_model.encoder.layers.{i}.mlp.fc1.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc1.bias''', F'''vision_model.encoder.layers.{i}.mlp.fc1.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc2.weight''', F'''vision_model.encoder.layers.{i}.mlp.fc2.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc2.bias''', F'''vision_model.encoder.layers.{i}.mlp.fc2.bias''') ) # QFormer rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.weight''', '''qformer.layernorm.weight''') ) rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.bias''', '''qformer.layernorm.bias''') ) # fmt: on return rename_keys def UpperCAmelCase__ (UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_ ): """simple docstring""" snake_case = dct.pop(UpperCamelCase_ ) snake_case = val def UpperCAmelCase__ (UpperCamelCase_ ,UpperCamelCase_ ): """simple docstring""" for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases snake_case = state_dict.pop(F'''visual_encoder.blocks.{i}.attn.q_bias''' ) snake_case = state_dict.pop(F'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict snake_case = torch.cat((q_bias, torch.zeros_like(UpperCamelCase_ ,requires_grad=UpperCamelCase_ ), v_bias) ) snake_case = qkv_bias def UpperCAmelCase__ (UpperCamelCase_ ,UpperCamelCase_ ): """simple docstring""" snake_case = 3_64 if '''coco''' in model_name else 2_24 snake_case = BlipaVisionConfig(image_size=UpperCamelCase_ ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: snake_case = OPTConfig.from_pretrained('''facebook/opt-2.7b''' ,eos_token_id=UpperCamelCase_ ).to_dict() elif "opt-6.7b" in model_name: snake_case = OPTConfig.from_pretrained('''facebook/opt-6.7b''' ,eos_token_id=UpperCamelCase_ ).to_dict() elif "t5-xl" in model_name: snake_case = TaConfig.from_pretrained('''google/flan-t5-xl''' ,dense_act_fn='''gelu''' ,bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: snake_case = TaConfig.from_pretrained('''google/flan-t5-xxl''' ,dense_act_fn='''gelu''' ,bos_token_id=1 ).to_dict() snake_case = BlipaConfig(vision_config=UpperCamelCase_ ,text_config=UpperCamelCase_ ) return config, image_size @torch.no_grad() def UpperCAmelCase__ (UpperCamelCase_ ,UpperCamelCase_=None ,UpperCamelCase_=False ): """simple docstring""" snake_case = ( AutoTokenizer.from_pretrained('''facebook/opt-2.7b''' ) if '''opt''' in model_name else AutoTokenizer.from_pretrained('''google/flan-t5-xl''' ) ) snake_case = tokenizer('''\n''' ,add_special_tokens=UpperCamelCase_ ).input_ids[0] snake_case , snake_case = get_blipa_config(UpperCamelCase_ ,eos_token_id=UpperCamelCase_ ) snake_case = BlipaForConditionalGeneration(UpperCamelCase_ ).eval() snake_case = { '''blip2-opt-2.7b''': ('''blip2_opt''', '''pretrain_opt2.7b'''), '''blip2-opt-6.7b''': ('''blip2_opt''', '''pretrain_opt6.7b'''), '''blip2-opt-2.7b-coco''': ('''blip2_opt''', '''caption_coco_opt2.7b'''), '''blip2-opt-6.7b-coco''': ('''blip2_opt''', '''caption_coco_opt6.7b'''), '''blip2-flan-t5-xl''': ('''blip2_t5''', '''pretrain_flant5xl'''), '''blip2-flan-t5-xl-coco''': ('''blip2_t5''', '''caption_coco_flant5xl'''), '''blip2-flan-t5-xxl''': ('''blip2_t5''', '''pretrain_flant5xxl'''), } snake_case , snake_case = model_name_to_original[model_name] # load original model print('''Loading original model...''' ) snake_case = '''cuda''' if torch.cuda.is_available() else '''cpu''' snake_case , snake_case , snake_case = load_model_and_preprocess( name=UpperCamelCase_ ,model_type=UpperCamelCase_ ,is_eval=UpperCamelCase_ ,device=UpperCamelCase_ ) original_model.eval() print('''Done!''' ) # update state dict keys snake_case = original_model.state_dict() snake_case = create_rename_keys(UpperCamelCase_ ) for src, dest in rename_keys: rename_key(UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_ ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): snake_case = state_dict.pop(UpperCamelCase_ ) if key.startswith('''Qformer.bert''' ): snake_case = key.replace('''Qformer.bert''' ,'''qformer''' ) if "attention.self" in key: snake_case = key.replace('''self''' ,'''attention''' ) if "opt_proj" in key: snake_case = key.replace('''opt_proj''' ,'''language_projection''' ) if "t5_proj" in key: snake_case = key.replace('''t5_proj''' ,'''language_projection''' ) if key.startswith('''opt''' ): snake_case = key.replace('''opt''' ,'''language''' ) if key.startswith('''t5''' ): snake_case = key.replace('''t5''' ,'''language''' ) snake_case = val # read in qv biases read_in_q_v_bias(UpperCamelCase_ ,UpperCamelCase_ ) snake_case , snake_case = hf_model.load_state_dict(UpperCamelCase_ ,strict=UpperCamelCase_ ) assert len(UpperCamelCase_ ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] snake_case = load_demo_image() snake_case = vis_processors['''eval'''](UpperCamelCase_ ).unsqueeze(0 ).to(UpperCamelCase_ ) snake_case = tokenizer(['''\n'''] ,return_tensors='''pt''' ).input_ids.to(UpperCamelCase_ ) # create processor snake_case = BlipImageProcessor( size={'''height''': image_size, '''width''': image_size} ,image_mean=UpperCamelCase_ ,image_std=UpperCamelCase_ ) snake_case = BlipaProcessor(image_processor=UpperCamelCase_ ,tokenizer=UpperCamelCase_ ) snake_case = processor(images=UpperCamelCase_ ,return_tensors='''pt''' ).pixel_values.to(UpperCamelCase_ ) # make sure processor creates exact same pixel values assert torch.allclose(UpperCamelCase_ ,UpperCamelCase_ ) original_model.to(UpperCamelCase_ ) hf_model.to(UpperCamelCase_ ) with torch.no_grad(): if "opt" in model_name: snake_case = original_model({'''image''': original_pixel_values, '''text_input''': ['''''']} ).logits snake_case = hf_model(UpperCamelCase_ ,UpperCamelCase_ ).logits else: snake_case = original_model( {'''image''': original_pixel_values, '''text_input''': ['''\n'''], '''text_output''': ['''\n''']} ).logits snake_case = input_ids.masked_fill(input_ids == tokenizer.pad_token_id ,-1_00 ) snake_case = hf_model(UpperCamelCase_ ,UpperCamelCase_ ,labels=UpperCamelCase_ ).logits assert original_logits.shape == logits.shape print('''First values of original logits:''' ,original_logits[0, :3, :3] ) print('''First values of HF logits:''' ,logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": snake_case = torch.tensor( [[-41.5850, -4.4440, -8.9922], [-47.4322, -5.9143, -1.7340]] ,device=UpperCamelCase_ ) assert torch.allclose(logits[0, :3, :3] ,UpperCamelCase_ ,atol=1e-4 ) elif model_name == "blip2-flan-t5-xl-coco": snake_case = torch.tensor( [[-57.0109, -9.8967, -12.6280], [-68.6578, -12.7191, -10.5065]] ,device=UpperCamelCase_ ) else: # cast to same type snake_case = logits.dtype assert torch.allclose(original_logits.to(UpperCamelCase_ ) ,UpperCamelCase_ ,atol=1e-2 ) print('''Looks ok!''' ) print('''Generating a caption...''' ) snake_case = '''''' snake_case = tokenizer(UpperCamelCase_ ,return_tensors='''pt''' ).input_ids.to(UpperCamelCase_ ) snake_case = original_model.generate({'''image''': original_pixel_values} ) snake_case = hf_model.generate( UpperCamelCase_ ,UpperCamelCase_ ,do_sample=UpperCamelCase_ ,num_beams=5 ,max_length=30 ,min_length=1 ,top_p=0.9 ,repetition_penalty=1.0 ,length_penalty=1.0 ,temperature=1 ,) print('''Original generation:''' ,UpperCamelCase_ ) snake_case = input_ids.shape[1] snake_case = processor.batch_decode(outputs[:, prompt_length:] ,skip_special_tokens=UpperCamelCase_ ) snake_case = [text.strip() for text in output_text] print('''HF generation:''' ,UpperCamelCase_ ) if pytorch_dump_folder_path is not None: processor.save_pretrained(UpperCamelCase_ ) hf_model.save_pretrained(UpperCamelCase_ ) if push_to_hub: processor.push_to_hub(F'''nielsr/{model_name}''' ) hf_model.push_to_hub(F'''nielsr/{model_name}''' ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE : Dict = argparse.ArgumentParser() _SCREAMING_SNAKE_CASE : str = [ "blip2-opt-2.7b", "blip2-opt-6.7b", "blip2-opt-2.7b-coco", "blip2-opt-6.7b-coco", "blip2-flan-t5-xl", "blip2-flan-t5-xl-coco", "blip2-flan-t5-xxl", ] parser.add_argument( "--model_name", default="blip2-opt-2.7b", choices=choices, type=str, help="Path to hf config.json of model to convert", ) parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument( "--push_to_hub", action="store_true", help="Whether to push the model and processor to the hub after converting", ) _SCREAMING_SNAKE_CASE : Optional[int] = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
366
# tests directory-specific settings - this file is run automatically # by pytest before any tests are run import sys import warnings from os.path import abspath, dirname, join # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. _SCREAMING_SNAKE_CASE : Union[str, Any] = abspath(join(dirname(dirname(dirname(__file__))), "src")) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action="ignore", category=FutureWarning) def UpperCAmelCase__ (UpperCamelCase_ ): """simple docstring""" from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(UpperCamelCase_ ) def UpperCAmelCase__ (UpperCamelCase_ ): """simple docstring""" from transformers.testing_utils import pytest_terminal_summary_main snake_case = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(UpperCamelCase_ ,id=UpperCamelCase_ )
213
0
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, BatchEncoding, PreTrainedTokenizer from ...utils import logging snake_case__ : Optional[Any] = logging.get_logger(__name__) snake_case__ : int = '▁' snake_case__ : Dict = {'vocab_file': 'sentencepiece.bpe.model'} snake_case__ : List[str] = { 'vocab_file': { 'facebook/nllb-200-distilled-600M': ( 'https://huggingface.co/facebook/nllb-200-distilled-600M/blob/main/sentencepiece.bpe.model' ), } } snake_case__ : Union[str, Any] = { 'facebook/nllb-200-distilled-600M': 1024, } # fmt: off snake_case__ : Union[str, Any] = ['ace_Arab', 'ace_Latn', 'acm_Arab', 'acq_Arab', 'aeb_Arab', 'afr_Latn', 'ajp_Arab', 'aka_Latn', 'amh_Ethi', 'apc_Arab', 'arb_Arab', 'ars_Arab', 'ary_Arab', 'arz_Arab', 'asm_Beng', 'ast_Latn', 'awa_Deva', 'ayr_Latn', 'azb_Arab', 'azj_Latn', 'bak_Cyrl', 'bam_Latn', 'ban_Latn', 'bel_Cyrl', 'bem_Latn', 'ben_Beng', 'bho_Deva', 'bjn_Arab', 'bjn_Latn', 'bod_Tibt', 'bos_Latn', 'bug_Latn', 'bul_Cyrl', 'cat_Latn', 'ceb_Latn', 'ces_Latn', 'cjk_Latn', 'ckb_Arab', 'crh_Latn', 'cym_Latn', 'dan_Latn', 'deu_Latn', 'dik_Latn', 'dyu_Latn', 'dzo_Tibt', 'ell_Grek', 'eng_Latn', 'epo_Latn', 'est_Latn', 'eus_Latn', 'ewe_Latn', 'fao_Latn', 'pes_Arab', 'fij_Latn', 'fin_Latn', 'fon_Latn', 'fra_Latn', 'fur_Latn', 'fuv_Latn', 'gla_Latn', 'gle_Latn', 'glg_Latn', 'grn_Latn', 'guj_Gujr', 'hat_Latn', 'hau_Latn', 'heb_Hebr', 'hin_Deva', 'hne_Deva', 'hrv_Latn', 'hun_Latn', 'hye_Armn', 'ibo_Latn', 'ilo_Latn', 'ind_Latn', 'isl_Latn', 'ita_Latn', 'jav_Latn', 'jpn_Jpan', 'kab_Latn', 'kac_Latn', 'kam_Latn', 'kan_Knda', 'kas_Arab', 'kas_Deva', 'kat_Geor', 'knc_Arab', 'knc_Latn', 'kaz_Cyrl', 'kbp_Latn', 'kea_Latn', 'khm_Khmr', 'kik_Latn', 'kin_Latn', 'kir_Cyrl', 'kmb_Latn', 'kon_Latn', 'kor_Hang', 'kmr_Latn', 'lao_Laoo', 'lvs_Latn', 'lij_Latn', 'lim_Latn', 'lin_Latn', 'lit_Latn', 'lmo_Latn', 'ltg_Latn', 'ltz_Latn', 'lua_Latn', 'lug_Latn', 'luo_Latn', 'lus_Latn', 'mag_Deva', 'mai_Deva', 'mal_Mlym', 'mar_Deva', 'min_Latn', 'mkd_Cyrl', 'plt_Latn', 'mlt_Latn', 'mni_Beng', 'khk_Cyrl', 'mos_Latn', 'mri_Latn', 'zsm_Latn', 'mya_Mymr', 'nld_Latn', 'nno_Latn', 'nob_Latn', 'npi_Deva', 'nso_Latn', 'nus_Latn', 'nya_Latn', 'oci_Latn', 'gaz_Latn', 'ory_Orya', 'pag_Latn', 'pan_Guru', 'pap_Latn', 'pol_Latn', 'por_Latn', 'prs_Arab', 'pbt_Arab', 'quy_Latn', 'ron_Latn', 'run_Latn', 'rus_Cyrl', 'sag_Latn', 'san_Deva', 'sat_Beng', 'scn_Latn', 'shn_Mymr', 'sin_Sinh', 'slk_Latn', 'slv_Latn', 'smo_Latn', 'sna_Latn', 'snd_Arab', 'som_Latn', 'sot_Latn', 'spa_Latn', 'als_Latn', 'srd_Latn', 'srp_Cyrl', 'ssw_Latn', 'sun_Latn', 'swe_Latn', 'swh_Latn', 'szl_Latn', 'tam_Taml', 'tat_Cyrl', 'tel_Telu', 'tgk_Cyrl', 'tgl_Latn', 'tha_Thai', 'tir_Ethi', 'taq_Latn', 'taq_Tfng', 'tpi_Latn', 'tsn_Latn', 'tso_Latn', 'tuk_Latn', 'tum_Latn', 'tur_Latn', 'twi_Latn', 'tzm_Tfng', 'uig_Arab', 'ukr_Cyrl', 'umb_Latn', 'urd_Arab', 'uzn_Latn', 'vec_Latn', 'vie_Latn', 'war_Latn', 'wol_Latn', 'xho_Latn', 'ydd_Hebr', 'yor_Latn', 'yue_Hant', 'zho_Hans', 'zho_Hant', 'zul_Latn'] class A_ ( _lowerCamelCase ): lowerCAmelCase__ = VOCAB_FILES_NAMES lowerCAmelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase__ = PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase__ = ["""input_ids""", """attention_mask"""] lowerCAmelCase__ = [] lowerCAmelCase__ = [] def __init__(self :Any , _UpperCamelCase :List[str] , _UpperCamelCase :Optional[Any]="<s>" , _UpperCamelCase :int="</s>" , _UpperCamelCase :int="</s>" , _UpperCamelCase :Optional[int]="<s>" , _UpperCamelCase :List[str]="<unk>" , _UpperCamelCase :List[str]="<pad>" , _UpperCamelCase :List[Any]="<mask>" , _UpperCamelCase :Optional[int]=None , _UpperCamelCase :Optional[int]=None , _UpperCamelCase :str=None , _UpperCamelCase :Optional[Dict[str, Any]] = None , _UpperCamelCase :Tuple=None , _UpperCamelCase :str=False , **_UpperCamelCase :int , )-> Optional[int]: # Mask token behave like a normal word, i.e. include the space before it __A = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else mask_token __A = {} if sp_model_kwargs is None else sp_model_kwargs __A = legacy_behaviour super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , unk_token=_UpperCamelCase , sep_token=_UpperCamelCase , cls_token=_UpperCamelCase , pad_token=_UpperCamelCase , mask_token=_UpperCamelCase , tokenizer_file=_UpperCamelCase , src_lang=_UpperCamelCase , tgt_lang=_UpperCamelCase , additional_special_tokens=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , legacy_behaviour=_UpperCamelCase , **_UpperCamelCase , ) __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_UpperCamelCase ) ) __A = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | ---- | ---- | ---- | ---- | ---- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | 'an' | '▁n' | '▁m' | '▁t' | '▁k' | '▁a' # spm | '<unk>' | '<s>' | '</s>' | 'an' | '▁n' | '▁m' | '▁t' | '▁k' | '▁a' | '▁s' # Mimic fairseq token-to-id alignment for the first 4 token __A = {'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3} # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab __A = 1 __A = len(self.sp_model ) __A = { code: self.sp_model_size + i + self.fairseq_offset for i, code in enumerate(_UpperCamelCase ) } __A = {v: k for k, v in self.lang_code_to_id.items()} __A = len(self.sp_model ) + len(self.lang_code_to_id ) + self.fairseq_offset self.fairseq_tokens_to_ids.update(self.lang_code_to_id ) __A = {v: k for k, v in self.fairseq_tokens_to_ids.items()} __A = list(self.lang_code_to_id.keys() ) if additional_special_tokens is not None: # Only add those special tokens if they are not already there. self._additional_special_tokens.extend( [t for t in additional_special_tokens if t not in self._additional_special_tokens] ) __A = src_lang if src_lang is not None else '''eng_Latn''' __A = self.lang_code_to_id[self._src_lang] __A = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) def __getstate__(self :List[str] )-> Tuple: __A = self.__dict__.copy() __A = None __A = self.sp_model.serialized_model_proto() return state def __setstate__(self :List[str] , _UpperCamelCase :Any )-> str: __A = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): __A = {} __A = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) @property def _lowerCAmelCase (self :Union[str, Any] )-> Dict: return len(self.sp_model ) + len(self.lang_code_to_id ) + self.fairseq_offset + 1 # Plus 1 for the mask token @property def _lowerCAmelCase (self :List[str] )-> str: return self._src_lang @src_lang.setter def _lowerCAmelCase (self :int , _UpperCamelCase :str )-> None: __A = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def _lowerCAmelCase (self :List[str] , _UpperCamelCase :List[int] , _UpperCamelCase :Optional[List[int]] = None , _UpperCamelCase :bool = False )-> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) __A = [1] * len(self.prefix_tokens ) __A = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(_UpperCamelCase )) + suffix_ones return prefix_ones + ([0] * len(_UpperCamelCase )) + ([0] * len(_UpperCamelCase )) + suffix_ones def _lowerCAmelCase (self :Union[str, Any] , _UpperCamelCase :List[int] , _UpperCamelCase :Optional[List[int]] = None )-> List[int]: if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def _lowerCAmelCase (self :Any , _UpperCamelCase :List[int] , _UpperCamelCase :Optional[List[int]] = None )-> List[int]: __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 + sep + token_ids_a + sep ) * [0] def _lowerCAmelCase (self :List[str] , _UpperCamelCase :Union[str, Any] , _UpperCamelCase :str , _UpperCamelCase :Optional[str] , _UpperCamelCase :Optional[str] , **_UpperCamelCase :Optional[int] )-> Union[str, Any]: if src_lang is None or tgt_lang is None: raise ValueError('''Translation requires a `src_lang` and a `tgt_lang` for this model''' ) __A = src_lang __A = self(_UpperCamelCase , add_special_tokens=_UpperCamelCase , return_tensors=_UpperCamelCase , **_UpperCamelCase ) __A = self.convert_tokens_to_ids(_UpperCamelCase ) __A = tgt_lang_id return inputs def _lowerCAmelCase (self :Optional[int] )-> Dict: __A = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def _lowerCAmelCase (self :Optional[Any] , _UpperCamelCase :str )-> List[str]: return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def _lowerCAmelCase (self :Union[str, Any] , _UpperCamelCase :List[str] )-> Optional[int]: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] __A = self.sp_model.PieceToId(_UpperCamelCase ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def _lowerCAmelCase (self :Dict , _UpperCamelCase :Optional[int] )-> str: if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def _lowerCAmelCase (self :Optional[int] , _UpperCamelCase :Any )-> int: __A = ''''''.join(_UpperCamelCase ).replace(_UpperCamelCase , ''' ''' ).strip() return out_string def _lowerCAmelCase (self :Union[str, Any] , _UpperCamelCase :str , _UpperCamelCase :Optional[str] = None )-> Tuple[str]: if not os.path.isdir(_UpperCamelCase ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return __A = os.path.join( _UpperCamelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase , '''wb''' ) as fi: __A = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,) def _lowerCAmelCase (self :Union[str, Any] , _UpperCamelCase :List[str] , _UpperCamelCase :str = "eng_Latn" , _UpperCamelCase :Optional[List[str]] = None , _UpperCamelCase :str = "fra_Latn" , **_UpperCamelCase :Optional[Any] , )-> BatchEncoding: __A = src_lang __A = tgt_lang return super().prepare_seqaseq_batch(_UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ) def _lowerCAmelCase (self :Optional[Any] )-> str: return self.set_src_lang_special_tokens(self.src_lang ) def _lowerCAmelCase (self :Optional[int] )-> Optional[int]: return self.set_tgt_lang_special_tokens(self.tgt_lang ) def _lowerCAmelCase (self :Dict , _UpperCamelCase :List[Any] )-> None: __A = self.lang_code_to_id[src_lang] if self.legacy_behaviour: __A = [] __A = [self.eos_token_id, self.cur_lang_code] else: __A = [self.cur_lang_code] __A = [self.eos_token_id] def _lowerCAmelCase (self :int , _UpperCamelCase :str )-> None: __A = self.lang_code_to_id[lang] if self.legacy_behaviour: __A = [] __A = [self.eos_token_id, self.cur_lang_code] else: __A = [self.cur_lang_code] __A = [self.eos_token_id]
117
def _a ( lowerCamelCase: int = 2_00 ) -> int: '''simple docstring''' __A = [1, 2, 5, 10, 20, 50, 1_00, 2_00] __A = [0] * (pence + 1) __A = 1 # base case: 1 way to make 0 pence for coin in coins: for i in range(lowerCamelCase , pence + 1 , 1 ): number_of_ways[i] += number_of_ways[i - coin] return number_of_ways[pence] if __name__ == "__main__": assert solution(200) == 73682
117
1
'''simple docstring''' from __future__ import annotations import math import numpy as np from numpy.linalg import norm def lowerCamelCase__ ( __lowerCamelCase : np.ndarray , __lowerCamelCase : np.ndarray ): '''simple docstring''' return math.sqrt(sum(pow(a - b , 2 ) for a, b in zip(__lowerCamelCase , __lowerCamelCase ) ) ) def lowerCamelCase__ ( __lowerCamelCase : np.ndarray , __lowerCamelCase : np.ndarray ): '''simple docstring''' if dataset.ndim != value_array.ndim: _UpperCAmelCase : Dict =( 'Wrong input data\'s dimensions... ' f"dataset : {dataset.ndim}, value_array : {value_array.ndim}" ) raise ValueError(__lowerCamelCase ) try: if dataset.shape[1] != value_array.shape[1]: _UpperCAmelCase : Any =( 'Wrong input data\'s shape... ' f"dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}" ) raise ValueError(__lowerCamelCase ) except IndexError: if dataset.ndim != value_array.ndim: raise TypeError('Wrong shape' ) if dataset.dtype != value_array.dtype: _UpperCAmelCase : Any =( 'Input data have different datatype... ' f"dataset : {dataset.dtype}, value_array : {value_array.dtype}" ) raise TypeError(__lowerCamelCase ) _UpperCAmelCase : List[Any] =[] for value in value_array: _UpperCAmelCase : Tuple =euclidean(__lowerCamelCase , dataset[0] ) _UpperCAmelCase : Optional[Any] =dataset[0].tolist() for dataset_value in dataset[1:]: _UpperCAmelCase : Dict =euclidean(__lowerCamelCase , __lowerCamelCase ) if dist > temp_dist: _UpperCAmelCase : Dict =temp_dist _UpperCAmelCase : Optional[int] =dataset_value.tolist() answer.append([vector, dist] ) return answer def lowerCamelCase__ ( __lowerCamelCase : np.ndarray , __lowerCamelCase : np.ndarray ): '''simple docstring''' return np.dot(__lowerCamelCase , __lowerCamelCase ) / (norm(__lowerCamelCase ) * norm(__lowerCamelCase )) if __name__ == "__main__": import doctest doctest.testmod()
360
'''simple docstring''' lowercase =[0, 2, 4, 6, 8] lowercase =[1, 3, 5, 7, 9] def lowerCamelCase__ ( __lowerCamelCase : int , __lowerCamelCase : int , __lowerCamelCase : list[int] , __lowerCamelCase : int ): '''simple docstring''' if remaining_length == 0: if digits[0] == 0 or digits[-1] == 0: return 0 for i in range(length // 2 - 1 , -1 , -1 ): remainder += digits[i] + digits[length - i - 1] if remainder % 2 == 0: return 0 remainder //= 1_0 return 1 if remaining_length == 1: if remainder % 2 == 0: return 0 _UpperCAmelCase : Union[str, Any] =0 for digit in range(1_0 ): _UpperCAmelCase : str =digit result += reversible_numbers( 0 , (remainder + 2 * digit) // 1_0 , __lowerCamelCase , __lowerCamelCase ) return result _UpperCAmelCase : Optional[Any] =0 for digita in range(1_0 ): _UpperCAmelCase : Any =digita if (remainder + digita) % 2 == 0: _UpperCAmelCase : Optional[int] =ODD_DIGITS else: _UpperCAmelCase : Union[str, Any] =EVEN_DIGITS for digita in other_parity_digits: _UpperCAmelCase : int =digita result += reversible_numbers( remaining_length - 2 , (remainder + digita + digita) // 1_0 , __lowerCamelCase , __lowerCamelCase , ) return result def lowerCamelCase__ ( __lowerCamelCase : int = 9 ): '''simple docstring''' _UpperCAmelCase : Optional[int] =0 for length in range(1 , max_power + 1 ): result += reversible_numbers(__lowerCamelCase , 0 , [0] * length , __lowerCamelCase ) return result if __name__ == "__main__": print(F"""{solution() = }""")
242
0
import copy import random from transformers import CLIPTokenizer class __A ( a ): """simple docstring""" def __init__( self , *lowerCamelCase__ , **lowerCamelCase__ ): """simple docstring""" super().__init__(*lowerCamelCase__ , **lowerCamelCase__ ) __UpperCamelCase : str ={} def __lowercase ( self , lowerCamelCase__ , *lowerCamelCase__ , **lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : Any =super().add_tokens(lowerCamelCase__ , *lowerCamelCase__ , **lowerCamelCase__ ) if num_added_tokens == 0: raise ValueError( f'The tokenizer already contains the token {placeholder_token}. Please pass a different' ' `placeholder_token` that is not already in the tokenizer.' ) def __lowercase ( self , lowerCamelCase__ , *lowerCamelCase__ , lowerCamelCase__=1 , **lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : Optional[Any] =[] if num_vec_per_token == 1: self.try_adding_tokens(lowerCamelCase__ , *lowerCamelCase__ , **lowerCamelCase__ ) output.append(lowerCamelCase__ ) else: __UpperCamelCase : List[str] =[] for i in range(lowerCamelCase__ ): __UpperCamelCase : Dict =placeholder_token + f'_{i}' self.try_adding_tokens(lowerCamelCase__ , *lowerCamelCase__ , **lowerCamelCase__ ) output.append(lowerCamelCase__ ) # handle cases where there is a new placeholder token that contains the current placeholder token but is larger for token in self.token_map: if token in placeholder_token: raise ValueError( f'The tokenizer already has placeholder token {token} that can get confused with' f' {placeholder_token}keep placeholder tokens independent' ) __UpperCamelCase : List[Any] =output def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__=False , lowerCamelCase__=1.0 ): """simple docstring""" if isinstance(lowerCamelCase__ , lowerCamelCase__ ): __UpperCamelCase : Dict =[] for i in range(len(lowerCamelCase__ ) ): output.append(self.replace_placeholder_tokens_in_text(text[i] , vector_shuffle=lowerCamelCase__ ) ) return output for placeholder_token in self.token_map: if placeholder_token in text: __UpperCamelCase : str =self.token_map[placeholder_token] __UpperCamelCase : Dict =tokens[: 1 + int(len(lowerCamelCase__ ) * prop_tokens_to_load )] if vector_shuffle: __UpperCamelCase : int =copy.copy(lowerCamelCase__ ) random.shuffle(lowerCamelCase__ ) __UpperCamelCase : Dict =text.replace(lowerCamelCase__ , ' '.join(lowerCamelCase__ ) ) return text def __call__( self , lowerCamelCase__ , *lowerCamelCase__ , lowerCamelCase__=False , lowerCamelCase__=1.0 , **lowerCamelCase__ ): """simple docstring""" return super().__call__( self.replace_placeholder_tokens_in_text( lowerCamelCase__ , vector_shuffle=lowerCamelCase__ , prop_tokens_to_load=lowerCamelCase__ ) , *lowerCamelCase__ , **lowerCamelCase__ , ) def __lowercase ( self , lowerCamelCase__ , *lowerCamelCase__ , lowerCamelCase__=False , lowerCamelCase__=1.0 , **lowerCamelCase__ ): """simple docstring""" return super().encode( self.replace_placeholder_tokens_in_text( lowerCamelCase__ , vector_shuffle=lowerCamelCase__ , prop_tokens_to_load=lowerCamelCase__ ) , *lowerCamelCase__ , **lowerCamelCase__ , )
71
import argparse import fairseq import torch from transformers import UniSpeechSatConfig, UniSpeechSatForCTC, UniSpeechSatForPreTraining, logging logging.set_verbosity_info() snake_case__ : Dict = logging.get_logger(__name__) snake_case__ : Optional[Any] = { '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', 'encoder.layer_norm_for_extract': 'layer_norm_for_extract', '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', 'label_embs_concat': 'label_embeddings_concat', 'mask_emb': 'masked_spec_embed', 'spk_proj': 'speaker_proj', } snake_case__ : Optional[int] = [ 'lm_head', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', 'label_embeddings_concat', 'speaker_proj', 'layer_norm_for_extract', ] def _a ( lowerCamelCase: List[Any] , lowerCamelCase: Any , lowerCamelCase: Union[str, Any] , lowerCamelCase: Any , lowerCamelCase: int ) -> List[str]: '''simple docstring''' 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 else: __A = value logger.info(F"""{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.""" ) def _a ( lowerCamelCase: List[str] , lowerCamelCase: Optional[int] ) -> Tuple: '''simple docstring''' __A = [] __A = fairseq_model.state_dict() __A = hf_model.unispeech_sat.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_sat.''' + 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]: if "layer_norm_for_extract" in name and (".".join(name.split('''.''' )[:-1] ) != key): # special case since naming is very similar continue __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 ) continue if not is_used: unused_weights.append(lowerCamelCase ) logger.warning(F"""Unused weights: {unused_weights}""" ) def _a ( lowerCamelCase: int , lowerCamelCase: Any , lowerCamelCase: int , lowerCamelCase: int , lowerCamelCase: List[str] ) -> Union[str, Any]: '''simple docstring''' __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[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[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 _a ( lowerCamelCase: Tuple , lowerCamelCase: int , lowerCamelCase: Optional[Any]=None , lowerCamelCase: Optional[Any]=None , lowerCamelCase: Optional[int]=True ) -> List[Any]: '''simple docstring''' if config_path is not None: __A = UniSpeechSatConfig.from_pretrained(lowerCamelCase ) else: __A = UniSpeechSatConfig() __A = '''''' if is_finetuned: __A = UniSpeechSatForCTC(lowerCamelCase ) else: __A = UniSpeechSatForPreTraining(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() recursively_load_weights(lowerCamelCase , lowerCamelCase ) hf_wavavec.save_pretrained(lowerCamelCase ) if __name__ == "__main__": snake_case__ : Tuple = 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' ) snake_case__ : Any = parser.parse_args() convert_unispeech_sat_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
117
0
'''simple docstring''' import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( '''kwargs, expected''' , [ ({'''num_shards''': 0, '''max_num_jobs''': 1}, []), ({'''num_shards''': 10, '''max_num_jobs''': 1}, [range(10 )]), ({'''num_shards''': 10, '''max_num_jobs''': 10}, [range(a , i + 1 ) for i in range(10 )]), ({'''num_shards''': 1, '''max_num_jobs''': 10}, [range(1 )]), ({'''num_shards''': 10, '''max_num_jobs''': 3}, [range(0 , 4 ), range(4 , 7 ), range(7 , 10 )]), ({'''num_shards''': 3, '''max_num_jobs''': 10}, [range(0 , 1 ), range(1 , 2 ), range(2 , 3 )]), ] , ) def UpperCamelCase ( a , a ) -> Tuple: '''simple docstring''' __magic_name__ = _distribute_shards(**a ) assert out == expected @pytest.mark.parametrize( '''gen_kwargs, max_num_jobs, expected''' , [ ({'''foo''': 0}, 10, [{'''foo''': 0}]), ({'''shards''': [0, 1, 2, 3]}, 1, [{'''shards''': [0, 1, 2, 3]}]), ({'''shards''': [0, 1, 2, 3]}, 4, [{'''shards''': [0]}, {'''shards''': [1]}, {'''shards''': [2]}, {'''shards''': [3]}]), ({'''shards''': [0, 1]}, 4, [{'''shards''': [0]}, {'''shards''': [1]}]), ({'''shards''': [0, 1, 2, 3]}, 2, [{'''shards''': [0, 1]}, {'''shards''': [2, 3]}]), ] , ) def UpperCamelCase ( a , a , a ) -> Optional[int]: '''simple docstring''' __magic_name__ = _split_gen_kwargs(a , a ) assert out == expected @pytest.mark.parametrize( '''gen_kwargs, expected''' , [ ({'''foo''': 0}, 1), ({'''shards''': [0]}, 1), ({'''shards''': [0, 1, 2, 3]}, 4), ({'''shards''': [0, 1, 2, 3], '''foo''': 0}, 4), ({'''shards''': [0, 1, 2, 3], '''other''': (0, 1)}, 4), ({'''shards''': [0, 1, 2, 3], '''shards2''': [0, 1]}, RuntimeError), ] , ) def UpperCamelCase ( a , a ) -> Union[str, Any]: '''simple docstring''' if expected is RuntimeError: with pytest.raises(a ): _number_of_shards_in_gen_kwargs(a ) else: __magic_name__ = _number_of_shards_in_gen_kwargs(a ) assert out == expected
98
'''simple docstring''' import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): __SCREAMING_SNAKE_CASE :List[Any] = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING __SCREAMING_SNAKE_CASE :Any = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def snake_case__ ( self : Tuple , a__ : Tuple , a__ : int , a__ : int ): __magic_name__ = TextaTextGenerationPipeline(model=a__ , tokenizer=a__ ) return generator, ["Something to write", "Something else"] def snake_case__ ( self : List[str] , a__ : List[Any] , a__ : List[str] ): __magic_name__ = generator('''Something there''' ) self.assertEqual(a__ , [{'''generated_text''': ANY(a__ )}] ) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]['''generated_text'''].startswith('''Something there''' ) ) __magic_name__ = generator(['''This is great !''', '''Something else'''] , num_return_sequences=2 , do_sample=a__ ) self.assertEqual( a__ , [ [{'''generated_text''': ANY(a__ )}, {'''generated_text''': ANY(a__ )}], [{'''generated_text''': ANY(a__ )}, {'''generated_text''': ANY(a__ )}], ] , ) __magic_name__ = generator( ['''This is great !''', '''Something else'''] , num_return_sequences=2 , batch_size=2 , do_sample=a__ ) self.assertEqual( a__ , [ [{'''generated_text''': ANY(a__ )}, {'''generated_text''': ANY(a__ )}], [{'''generated_text''': ANY(a__ )}, {'''generated_text''': ANY(a__ )}], ] , ) with self.assertRaises(a__ ): generator(4 ) @require_torch def snake_case__ ( self : Any ): __magic_name__ = pipeline('''text2text-generation''' , model='''patrickvonplaten/t5-tiny-random''' , framework='''pt''' ) # do_sample=False necessary for reproducibility __magic_name__ = generator('''Something there''' , do_sample=a__ ) self.assertEqual(a__ , [{'''generated_text''': ''''''}] ) __magic_name__ = 3 __magic_name__ = generator( '''Something there''' , num_return_sequences=a__ , num_beams=a__ , ) __magic_name__ = [ {'''generated_text''': '''Beide Beide Beide Beide Beide Beide Beide Beide Beide'''}, {'''generated_text''': '''Beide Beide Beide Beide Beide Beide Beide Beide'''}, {'''generated_text''': ''''''}, ] self.assertEqual(a__ , a__ ) __magic_name__ = generator('''This is a test''' , do_sample=a__ , num_return_sequences=2 , return_tensors=a__ ) self.assertEqual( a__ , [ {'''generated_token_ids''': ANY(torch.Tensor )}, {'''generated_token_ids''': ANY(torch.Tensor )}, ] , ) __magic_name__ = generator.model.config.eos_token_id __magic_name__ = '''<pad>''' __magic_name__ = generator( ['''This is a test''', '''This is a second test'''] , do_sample=a__ , num_return_sequences=2 , batch_size=2 , return_tensors=a__ , ) self.assertEqual( a__ , [ [ {'''generated_token_ids''': ANY(torch.Tensor )}, {'''generated_token_ids''': ANY(torch.Tensor )}, ], [ {'''generated_token_ids''': ANY(torch.Tensor )}, {'''generated_token_ids''': ANY(torch.Tensor )}, ], ] , ) @require_tf def snake_case__ ( self : int ): __magic_name__ = pipeline('''text2text-generation''' , model='''patrickvonplaten/t5-tiny-random''' , framework='''tf''' ) # do_sample=False necessary for reproducibility __magic_name__ = generator('''Something there''' , do_sample=a__ ) self.assertEqual(a__ , [{'''generated_text''': ''''''}] )
98
1
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_camembert import CamembertTokenizer else: lowerCamelCase = None lowerCamelCase = logging.get_logger(__name__) lowerCamelCase = {'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''} lowerCamelCase = { '''vocab_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/tokenizer.json''', }, } lowerCamelCase = { '''camembert-base''': 512, } lowerCamelCase = '''▁''' class __magic_name__ ( _lowercase ): '''simple docstring''' lowerCamelCase__ : str = VOCAB_FILES_NAMES lowerCamelCase__ : List[Any] = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase__ : int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase__ : str = ["input_ids", "attention_mask"] lowerCamelCase__ : List[str] = CamembertTokenizer def __init__( self, lowercase_=None, lowercase_=None, lowercase_="<s>", lowercase_="</s>", lowercase_="</s>", lowercase_="<s>", lowercase_="<unk>", lowercase_="<pad>", lowercase_="<mask>", lowercase_=["<s>NOTUSED", "</s>NOTUSED"], **lowercase_, ) -> str: """simple docstring""" a__ =AddedToken(lowercase_, lstrip=lowercase_, rstrip=lowercase_ ) if isinstance(lowercase_, lowercase_ ) else mask_token super().__init__( lowercase_, tokenizer_file=lowercase_, bos_token=lowercase_, eos_token=lowercase_, sep_token=lowercase_, cls_token=lowercase_, unk_token=lowercase_, pad_token=lowercase_, mask_token=lowercase_, additional_special_tokens=lowercase_, **lowercase_, ) a__ =vocab_file a__ =False if not self.vocab_file else True def _UpperCAmelCase ( self, lowercase_, lowercase_ = None ) -> Tuple: """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] a__ =[self.cls_token_id] a__ =[self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _UpperCAmelCase ( self, lowercase_, lowercase_ = None ) -> List[str]: """simple docstring""" a__ =[self.sep_token_id] a__ =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def _UpperCAmelCase ( self, lowercase_, lowercase_ = None ) -> Optional[Any]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(lowercase_ ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return a__ =os.path.join( lowercase_, (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowercase_ ): copyfile(self.vocab_file, lowercase_ ) return (out_vocab_file,)
188
'''simple docstring''' import os import unittest from transformers import LayoutLMTokenizer, LayoutLMTokenizerFast from transformers.models.layoutlm.tokenization_layoutlm import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __lowercase ( _lowercase , unittest.TestCase ): lowerCamelCase : Any = LayoutLMTokenizer lowerCamelCase : Union[str, Any] = LayoutLMTokenizerFast lowerCamelCase : Optional[int] = True lowerCamelCase : int = True def UpperCAmelCase__ (self ): super().setUp() lowerCamelCase_ : Dict = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] lowerCamelCase_ : str = 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 LayoutLMTokenizer.from_pretrained(self.tmpdirname , **A ) def UpperCAmelCase__ (self , A ): lowerCamelCase_ : Any = '''UNwant\u00E9d,running''' lowerCamelCase_ : List[Any] = '''unwanted, running''' return input_text, output_text def UpperCAmelCase__ (self ): lowerCamelCase_ : List[str] = self.tokenizer_class(self.vocab_file ) lowerCamelCase_ : Optional[Any] = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(A , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) , [7, 4, 5, 1_0, 8, 9] ) def UpperCAmelCase__ (self ): pass
318
0
"""simple docstring""" import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_url from PIL import Image from transformers import DPTConfig, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTImageProcessor from transformers.utils import logging logging.set_verbosity_info() _a = logging.get_logger(__name__) def __a ( __lowerCamelCase ): UpperCAmelCase_ : int = DPTConfig(embedding_type="hybrid" ) if "large" in checkpoint_url: UpperCAmelCase_ : Dict = 1024 UpperCAmelCase_ : List[str] = 4096 UpperCAmelCase_ : Union[str, Any] = 24 UpperCAmelCase_ : str = 16 UpperCAmelCase_ : Tuple = [5, 11, 17, 23] UpperCAmelCase_ : int = [256, 512, 1024, 1024] UpperCAmelCase_ : Any = (1, 384, 384) if "nyu" or "midas" in checkpoint_url: UpperCAmelCase_ : Optional[int] = 768 UpperCAmelCase_ : List[str] = [1, 1, 1, 0.5] UpperCAmelCase_ : str = [256, 512, 768, 768] UpperCAmelCase_ : str = 150 UpperCAmelCase_ : List[Any] = 16 UpperCAmelCase_ : Optional[Any] = (1, 384, 384) UpperCAmelCase_ : Optional[Any] = False UpperCAmelCase_ : List[str] = "project" if "ade" in checkpoint_url: UpperCAmelCase_ : Any = True UpperCAmelCase_ : Dict = 768 UpperCAmelCase_ : Any = [1, 1, 1, 0.5] UpperCAmelCase_ : Dict = 150 UpperCAmelCase_ : str = 16 UpperCAmelCase_ : Optional[int] = "huggingface/label-files" UpperCAmelCase_ : Dict = "ade20k-id2label.json" UpperCAmelCase_ : List[Any] = json.load(open(cached_download(hf_hub_url(__lowerCamelCase, __lowerCamelCase, repo_type="dataset" ) ), "r" ) ) UpperCAmelCase_ : Union[str, Any] = {int(__lowerCamelCase ): v for k, v in idalabel.items()} UpperCAmelCase_ : List[Any] = idalabel UpperCAmelCase_ : int = {v: k for k, v in idalabel.items()} UpperCAmelCase_ : List[Any] = [1, 150, 480, 480] return config, expected_shape def __a ( __lowerCamelCase ): UpperCAmelCase_ : Dict = ["pretrained.model.head.weight", "pretrained.model.head.bias"] for k in ignore_keys: state_dict.pop(__lowerCamelCase, __lowerCamelCase ) def __a ( __lowerCamelCase ): if ( "pretrained.model" in name and "cls_token" not in name and "pos_embed" not in name and "patch_embed" not in name ): UpperCAmelCase_ : int = name.replace("pretrained.model", "dpt.encoder" ) if "pretrained.model" in name: UpperCAmelCase_ : int = name.replace("pretrained.model", "dpt.embeddings" ) if "patch_embed" in name: UpperCAmelCase_ : List[Any] = name.replace("patch_embed", "" ) if "pos_embed" in name: UpperCAmelCase_ : int = name.replace("pos_embed", "position_embeddings" ) if "attn.proj" in name: UpperCAmelCase_ : Optional[int] = name.replace("attn.proj", "attention.output.dense" ) if "proj" in name and "project" not in name: UpperCAmelCase_ : Tuple = name.replace("proj", "projection" ) if "blocks" in name: UpperCAmelCase_ : Union[str, Any] = name.replace("blocks", "layer" ) if "mlp.fc1" in name: UpperCAmelCase_ : Any = name.replace("mlp.fc1", "intermediate.dense" ) if "mlp.fc2" in name: UpperCAmelCase_ : Optional[int] = name.replace("mlp.fc2", "output.dense" ) if "norm1" in name and "backbone" not in name: UpperCAmelCase_ : Union[str, Any] = name.replace("norm1", "layernorm_before" ) if "norm2" in name and "backbone" not in name: UpperCAmelCase_ : Tuple = name.replace("norm2", "layernorm_after" ) if "scratch.output_conv" in name: UpperCAmelCase_ : List[Any] = name.replace("scratch.output_conv", "head" ) if "scratch" in name: UpperCAmelCase_ : int = name.replace("scratch", "neck" ) if "layer1_rn" in name: UpperCAmelCase_ : Dict = name.replace("layer1_rn", "convs.0" ) if "layer2_rn" in name: UpperCAmelCase_ : Dict = name.replace("layer2_rn", "convs.1" ) if "layer3_rn" in name: UpperCAmelCase_ : List[Any] = name.replace("layer3_rn", "convs.2" ) if "layer4_rn" in name: UpperCAmelCase_ : Tuple = name.replace("layer4_rn", "convs.3" ) if "refinenet" in name: UpperCAmelCase_ : Optional[Any] = int(name[len("neck.refinenet" ) : len("neck.refinenet" ) + 1] ) # tricky here: we need to map 4 to 0, 3 to 1, 2 to 2 and 1 to 3 UpperCAmelCase_ : List[Any] = name.replace(f"""refinenet{layer_idx}""", f"""fusion_stage.layers.{abs(layer_idx-4 )}""" ) if "out_conv" in name: UpperCAmelCase_ : List[str] = name.replace("out_conv", "projection" ) if "resConfUnit1" in name: UpperCAmelCase_ : Optional[int] = name.replace("resConfUnit1", "residual_layer1" ) if "resConfUnit2" in name: UpperCAmelCase_ : int = name.replace("resConfUnit2", "residual_layer2" ) if "conv1" in name: UpperCAmelCase_ : Union[str, Any] = name.replace("conv1", "convolution1" ) if "conv2" in name: UpperCAmelCase_ : int = name.replace("conv2", "convolution2" ) # readout blocks if "pretrained.act_postprocess1.0.project.0" in name: UpperCAmelCase_ : List[Any] = name.replace("pretrained.act_postprocess1.0.project.0", "neck.reassemble_stage.readout_projects.0.0" ) if "pretrained.act_postprocess2.0.project.0" in name: UpperCAmelCase_ : str = name.replace("pretrained.act_postprocess2.0.project.0", "neck.reassemble_stage.readout_projects.1.0" ) if "pretrained.act_postprocess3.0.project.0" in name: UpperCAmelCase_ : Dict = name.replace("pretrained.act_postprocess3.0.project.0", "neck.reassemble_stage.readout_projects.2.0" ) if "pretrained.act_postprocess4.0.project.0" in name: UpperCAmelCase_ : str = name.replace("pretrained.act_postprocess4.0.project.0", "neck.reassemble_stage.readout_projects.3.0" ) # resize blocks if "pretrained.act_postprocess1.3" in name: UpperCAmelCase_ : Dict = name.replace("pretrained.act_postprocess1.3", "neck.reassemble_stage.layers.0.projection" ) if "pretrained.act_postprocess1.4" in name: UpperCAmelCase_ : List[Any] = name.replace("pretrained.act_postprocess1.4", "neck.reassemble_stage.layers.0.resize" ) if "pretrained.act_postprocess2.3" in name: UpperCAmelCase_ : Any = name.replace("pretrained.act_postprocess2.3", "neck.reassemble_stage.layers.1.projection" ) if "pretrained.act_postprocess2.4" in name: UpperCAmelCase_ : Union[str, Any] = name.replace("pretrained.act_postprocess2.4", "neck.reassemble_stage.layers.1.resize" ) if "pretrained.act_postprocess3.3" in name: UpperCAmelCase_ : str = name.replace("pretrained.act_postprocess3.3", "neck.reassemble_stage.layers.2.projection" ) if "pretrained.act_postprocess4.3" in name: UpperCAmelCase_ : Any = name.replace("pretrained.act_postprocess4.3", "neck.reassemble_stage.layers.3.projection" ) if "pretrained.act_postprocess4.4" in name: UpperCAmelCase_ : Dict = name.replace("pretrained.act_postprocess4.4", "neck.reassemble_stage.layers.3.resize" ) if "pretrained" in name: UpperCAmelCase_ : Any = name.replace("pretrained", "dpt" ) if "bn" in name: UpperCAmelCase_ : List[str] = name.replace("bn", "batch_norm" ) if "head" in name: UpperCAmelCase_ : List[str] = name.replace("head", "head.head" ) if "encoder.norm" in name: UpperCAmelCase_ : int = name.replace("encoder.norm", "layernorm" ) if "auxlayer" in name: UpperCAmelCase_ : Any = name.replace("auxlayer", "auxiliary_head.head" ) if "backbone" in name: UpperCAmelCase_ : List[Any] = name.replace("backbone", "backbone.bit.encoder" ) if ".." in name: UpperCAmelCase_ : Union[str, Any] = name.replace("..", "." ) if "stem.conv" in name: UpperCAmelCase_ : str = name.replace("stem.conv", "bit.embedder.convolution" ) if "blocks" in name: UpperCAmelCase_ : Dict = name.replace("blocks", "layers" ) if "convolution" in name and "backbone" in name: UpperCAmelCase_ : str = name.replace("convolution", "conv" ) if "layer" in name and "backbone" in name: UpperCAmelCase_ : Dict = name.replace("layer", "layers" ) if "backbone.bit.encoder.bit" in name: UpperCAmelCase_ : List[str] = name.replace("backbone.bit.encoder.bit", "backbone.bit" ) if "embedder.conv" in name: UpperCAmelCase_ : str = name.replace("embedder.conv", "embedder.convolution" ) if "backbone.bit.encoder.stem.norm" in name: UpperCAmelCase_ : Union[str, Any] = name.replace("backbone.bit.encoder.stem.norm", "backbone.bit.embedder.norm" ) return name def __a ( __lowerCamelCase, __lowerCamelCase ): for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) UpperCAmelCase_ : Optional[int] = state_dict.pop(f"""dpt.encoder.layer.{i}.attn.qkv.weight""" ) UpperCAmelCase_ : List[Any] = state_dict.pop(f"""dpt.encoder.layer.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict UpperCAmelCase_ : List[str] = in_proj_weight[: config.hidden_size, :] UpperCAmelCase_ : Optional[Any] = in_proj_bias[: config.hidden_size] UpperCAmelCase_ : Dict = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] UpperCAmelCase_ : Optional[int] = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] UpperCAmelCase_ : Union[str, Any] = in_proj_weight[ -config.hidden_size :, : ] UpperCAmelCase_ : List[Any] = in_proj_bias[-config.hidden_size :] def __a ( ): UpperCAmelCase_ : Dict = "http://images.cocodataset.org/val2017/000000039769.jpg" UpperCAmelCase_ : Union[str, Any] = Image.open(requests.get(__lowerCamelCase, stream=__lowerCamelCase ).raw ) return im @torch.no_grad() def __a ( __lowerCamelCase, __lowerCamelCase, __lowerCamelCase, __lowerCamelCase, __lowerCamelCase ): UpperCAmelCase_ : int = get_dpt_config(__lowerCamelCase ) # load original state_dict from URL # state_dict = torch.hub.load_state_dict_from_url(checkpoint_url, map_location="cpu") UpperCAmelCase_ : Union[str, Any] = torch.load(__lowerCamelCase, map_location="cpu" ) # remove certain keys remove_ignore_keys_(__lowerCamelCase ) # rename keys for key in state_dict.copy().keys(): UpperCAmelCase_ : Optional[Any] = state_dict.pop(__lowerCamelCase ) UpperCAmelCase_ : Optional[Any] = val # read in qkv matrices read_in_q_k_v(__lowerCamelCase, __lowerCamelCase ) # load HuggingFace model UpperCAmelCase_ : str = DPTForSemanticSegmentation(__lowerCamelCase ) if "ade" in checkpoint_url else DPTForDepthEstimation(__lowerCamelCase ) model.load_state_dict(__lowerCamelCase ) model.eval() # Check outputs on an image UpperCAmelCase_ : List[Any] = 480 if "ade" in checkpoint_url else 384 UpperCAmelCase_ : List[str] = DPTImageProcessor(size=__lowerCamelCase ) UpperCAmelCase_ : Any = prepare_img() UpperCAmelCase_ : int = image_processor(__lowerCamelCase, return_tensors="pt" ) # forward pass UpperCAmelCase_ : int = model(**__lowerCamelCase ).logits if "ade" in checkpoint_url else model(**__lowerCamelCase ).predicted_depth if show_prediction: UpperCAmelCase_ : Any = ( torch.nn.functional.interpolate( outputs.unsqueeze(1 ), size=(image.size[1], image.size[0]), mode="bicubic", align_corners=__lowerCamelCase, ) .squeeze() .cpu() .numpy() ) Image.fromarray((prediction / prediction.max()) * 255 ).show() if pytorch_dump_folder_path is not None: Path(__lowerCamelCase ).mkdir(exist_ok=__lowerCamelCase ) print(f"""Saving model to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowerCamelCase ) print(f"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(__lowerCamelCase ) if push_to_hub: model.push_to_hub("ybelkada/dpt-hybrid-midas" ) image_processor.push_to_hub("ybelkada/dpt-hybrid-midas" ) if __name__ == "__main__": _a = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint_url', default='https://github.com/intel-isl/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt', type=str, help='URL of the original DPT checkpoint you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=False, help='Path to the output PyTorch model directory.', ) parser.add_argument( '--push_to_hub', action='store_true', ) parser.add_argument( '--model_name', default='dpt-large', type=str, help='Name of the model, in case you\'re pushing to the hub.', ) parser.add_argument( '--show_prediction', action='store_true', ) _a = parser.parse_args() convert_dpt_checkpoint( args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name, args.show_prediction )
360
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _a = logging.get_logger(__name__) _a = {'ctrl': 'https://huggingface.co/ctrl/resolve/main/config.json'} class A_ (lowercase__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Dict = """ctrl""" SCREAMING_SNAKE_CASE__ : Optional[int] = ["""past_key_values"""] SCREAMING_SNAKE_CASE__ : List[str] = { """max_position_embeddings""": """n_positions""", """hidden_size""": """n_embd""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self , lowercase_=24_6534 , lowercase_=256 , lowercase_=1280 , lowercase_=8192 , lowercase_=48 , lowercase_=16 , lowercase_=0.1 , lowercase_=0.1 , lowercase_=1E-6 , lowercase_=0.02 , lowercase_=True , **lowercase_ , ): """simple docstring""" UpperCAmelCase_ : Tuple = vocab_size UpperCAmelCase_ : Union[str, Any] = n_positions UpperCAmelCase_ : List[str] = n_embd UpperCAmelCase_ : Dict = n_layer UpperCAmelCase_ : Optional[int] = n_head UpperCAmelCase_ : List[str] = dff UpperCAmelCase_ : Tuple = resid_pdrop UpperCAmelCase_ : Optional[Any] = embd_pdrop UpperCAmelCase_ : str = layer_norm_epsilon UpperCAmelCase_ : List[str] = initializer_range UpperCAmelCase_ : List[str] = use_cache super().__init__(**lowercase_ )
23
0
'''simple docstring''' import io import json import unittest from parameterized import parameterized from transformers import FSMTForConditionalGeneration, FSMTTokenizer from transformers.testing_utils import get_tests_dir, require_torch, slow, torch_device from utils import calculate_bleu a : Any = get_tests_dir() + '/test_data/fsmt/fsmt_val_data.json' with io.open(filename, 'r', encoding='utf-8') as f: a : Dict = json.load(f) @require_torch class a ( unittest.TestCase ): def A_ ( self : int , lowercase_ : int ): return FSMTTokenizer.from_pretrained(lowercase_ ) def A_ ( self : Tuple , lowercase_ : List[Any] ): snake_case_ = FSMTForConditionalGeneration.from_pretrained(lowercase_ ).to(lowercase_ ) if torch_device == "cuda": model.half() return model @parameterized.expand( [ ['''en-ru''', 26.0], ['''ru-en''', 22.0], ['''en-de''', 22.0], ['''de-en''', 29.0], ] ) @slow def A_ ( self : Optional[Any] , lowercase_ : List[Any] , lowercase_ : List[str] ): # note: this test is not testing the best performance since it only evals a small batch # but it should be enough to detect a regression in the output quality snake_case_ = F"facebook/wmt19-{pair}" snake_case_ = self.get_tokenizer(lowercase_ ) snake_case_ = self.get_model(lowercase_ ) snake_case_ = bleu_data[pair]['''src'''] snake_case_ = bleu_data[pair]['''tgt'''] snake_case_ = tokenizer(lowercase_ , return_tensors='''pt''' , truncation=lowercase_ , padding='''longest''' ).to(lowercase_ ) snake_case_ = model.generate( input_ids=batch.input_ids , num_beams=8 , ) snake_case_ = tokenizer.batch_decode( lowercase_ , skip_special_tokens=lowercase_ , clean_up_tokenization_spaces=lowercase_ ) snake_case_ = calculate_bleu(lowercase_ , lowercase_ ) print(lowercase_ ) self.assertGreaterEqual(scores['''bleu'''] , lowercase_ )
56
"""simple docstring""" def __UpperCAmelCase ( __UpperCamelCase ): __lowercase : str = [1] __lowercase ,__lowercase ,__lowercase : List[str] = 0, 0, 0 __lowercase : List[str] = ugly_nums[ia] * 2 __lowercase : Any = ugly_nums[ia] * 3 __lowercase : str = ugly_nums[ia] * 5 for _ in range(1 , __UpperCamelCase ): __lowercase : Union[str, Any] = min(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) ugly_nums.append(__UpperCamelCase ) if next_num == next_a: ia += 1 __lowercase : List[str] = ugly_nums[ia] * 2 if next_num == next_a: ia += 1 __lowercase : int = ugly_nums[ia] * 3 if next_num == next_a: ia += 1 __lowercase : Optional[int] = ugly_nums[ia] * 5 return ugly_nums[-1] if __name__ == "__main__": from doctest import testmod testmod(verbose=True) print(F"{ugly_numbers(2_0_0) = }")
249
0
'''simple docstring''' def A_( A : list): if any(not isinstance(__lowerCamelCase , __lowerCamelCase) or x < 0 for x in sequence): raise TypeError('Sequence must be list of non-negative integers') for _ in range(len(__lowerCamelCase)): for i, (rod_upper, rod_lower) in enumerate(zip(__lowerCamelCase , sequence[1:])): if rod_upper > rod_lower: sequence[i] -= rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower return sequence if __name__ == "__main__": assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9]
360
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : Optional[int] = logging.get_logger(__name__) lowerCAmelCase : Union[str, Any] = {'openai-gpt': 'https://huggingface.co/openai-gpt/resolve/main/config.json'} class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """openai-gpt""" lowerCAmelCase_ = { """max_position_embeddings""": """n_positions""", """hidden_size""": """n_embd""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self , A_=40478 , A_=512 , A_=768 , A_=12 , A_=12 , A_="gelu" , A_=0.1 , A_=0.1 , A_=0.1 , A_=1e-5 , A_=0.02 , A_="cls_index" , A_=True , A_=None , A_=True , A_=0.1 , **A_ , )-> List[str]: '''simple docstring''' UpperCamelCase = vocab_size UpperCamelCase = n_positions UpperCamelCase = n_embd UpperCamelCase = n_layer UpperCamelCase = n_head UpperCamelCase = afn UpperCamelCase = resid_pdrop UpperCamelCase = embd_pdrop UpperCamelCase = attn_pdrop UpperCamelCase = layer_norm_epsilon UpperCamelCase = initializer_range UpperCamelCase = summary_type UpperCamelCase = summary_use_proj UpperCamelCase = summary_activation UpperCamelCase = summary_first_dropout UpperCamelCase = summary_proj_to_labels super().__init__(**A_ )
251
0
import re from filelock import FileLock try: import nltk A_ :Any = True except (ImportError, ModuleNotFoundError): A_ :Union[str, Any] = False if NLTK_AVAILABLE: with FileLock('''.lock''') as lock: nltk.download('''punkt''', quiet=True) def A ( a_ ) -> str: re.sub('<n>' ,'' ,a_ ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(a_ ) )
71
"""simple docstring""" import argparse import torch # Step 1. clone https://github.com/microsoft/unilm # Step 2. git checkout to https://github.com/microsoft/unilm/commit/b94ec76c36f02fb2b0bf0dcb0b8554a2185173cd # Step 3. cd unilm # Step 4. ln -s $(realpath wavlm/modules.py) ./ # create simlink # import classes from unilm.wavlm.WavLM import WavLM as WavLMOrig from unilm.wavlm.WavLM import WavLMConfig as WavLMConfigOrig from transformers import WavLMConfig, WavLMModel, logging logging.set_verbosity_info() lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """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.grep_linear""": """encoder.layers.*.attention.gru_rel_pos_linear""", """self_attn.relative_attention_bias""": """encoder.layers.*.attention.rel_attn_embed""", """self_attn.grep_a""": """encoder.layers.*.attention.gru_rel_pos_const""", """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""", } lowerCamelCase__ = [ """ctc_proj""", """quantizer.weight_proj""", """quantizer.codevectors""", """project_q""", """project_hid""", ] def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): for attribute in key.split('.' ): __lowerCAmelCase : str = getattr(_UpperCamelCase , _UpperCamelCase ) if weight_type is not None: __lowerCAmelCase : Tuple = getattr(_UpperCamelCase , _UpperCamelCase ).shape else: __lowerCAmelCase : Dict = 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": __lowerCAmelCase : Union[str, Any] = value elif weight_type == "weight_g": __lowerCAmelCase : List[Any] = value elif weight_type == "weight_v": __lowerCAmelCase : Any = value elif weight_type == "bias": __lowerCAmelCase : List[str] = value else: __lowerCAmelCase : List[Any] = value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase ): __lowerCAmelCase : Any = [] __lowerCAmelCase : Optional[int] = fairseq_model.state_dict() __lowerCAmelCase : Union[str, Any] = hf_model.feature_extractor for name, value in fairseq_dict.items(): __lowerCAmelCase : Union[str, Any] = False if "conv_layers" in name: load_conv_layer( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , hf_model.config.feat_extract_norm == 'group' , ) __lowerCAmelCase : str = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split('w2v_model.' )[-1] == name.split('.' )[0]: __lowerCAmelCase : int = True if "*" in mapped_key: __lowerCAmelCase : List[str] = name.split(_UpperCamelCase )[0].split('.' )[-2] __lowerCAmelCase : Optional[Any] = mapped_key.replace('*' , _UpperCamelCase ) if "weight_g" in name: __lowerCAmelCase : Union[str, Any] = 'weight_g' elif "weight_v" in name: __lowerCAmelCase : int = 'weight_v' elif "bias" in name and "relative_attention_bias" not in name: __lowerCAmelCase : Optional[Any] = 'bias' elif "weight" in name: # TODO: don't match quantizer.weight_proj __lowerCAmelCase : List[str] = 'weight' else: __lowerCAmelCase : Optional[Any] = None set_recursively(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) continue if not is_used: unused_weights.append(_UpperCamelCase ) logger.warning(F"Unused weights: {unused_weights}" ) def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): __lowerCAmelCase : List[Any] = full_name.split('conv_layers.' )[-1] __lowerCAmelCase : Any = name.split('.' ) __lowerCAmelCase : List[Any] = int(items[0] ) __lowerCAmelCase : Tuple = 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." ) __lowerCAmelCase : Tuple = 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." ) __lowerCAmelCase : int = 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." ) __lowerCAmelCase : Optional[Any] = 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." ) __lowerCAmelCase : Any = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(_UpperCamelCase ) @torch.no_grad() def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase=None ): # load the pre-trained checkpoints __lowerCAmelCase : Any = torch.load(_UpperCamelCase ) __lowerCAmelCase : List[str] = WavLMConfigOrig(checkpoint['cfg'] ) __lowerCAmelCase : Optional[Any] = WavLMOrig(_UpperCamelCase ) model.load_state_dict(checkpoint['model'] ) model.eval() if config_path is not None: __lowerCAmelCase : Dict = WavLMConfig.from_pretrained(_UpperCamelCase ) else: __lowerCAmelCase : List[str] = WavLMConfig() __lowerCAmelCase : List[str] = WavLMModel(_UpperCamelCase ) recursively_load_weights(_UpperCamelCase , _UpperCamelCase ) hf_wavlm.save_pretrained(_UpperCamelCase ) if __name__ == "__main__": lowerCamelCase__ = 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("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") lowerCamelCase__ = parser.parse_args() convert_wavlm_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
86
0
"""simple docstring""" from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class A_ (UpperCamelCase__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[str] = ["""image_processor""", """tokenizer"""] SCREAMING_SNAKE_CASE__ : List[Any] = """AutoImageProcessor""" SCREAMING_SNAKE_CASE__ : Any = """AutoTokenizer""" def __init__( self , lowercase_ , lowercase_ ): """simple docstring""" super().__init__(__a , __a ) UpperCAmelCase_ : List[str] = self.image_processor def __call__( self , lowercase_=None , lowercase_=None , lowercase_=None , **lowercase_ ): """simple docstring""" if text is None and images is None: raise ValueError("You have to specify either text or images. Both cannot be none." ) if text is not None: UpperCAmelCase_ : Tuple = self.tokenizer(__a , return_tensors=__a , **__a ) if images is not None: UpperCAmelCase_ : Optional[int] = self.image_processor(__a , return_tensors=__a , **__a ) if text is not None and images is not None: UpperCAmelCase_ : List[str] = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__a ) , tensor_type=__a ) def UpperCamelCase__ ( self , *lowercase_ , **lowercase_ ): """simple docstring""" return self.tokenizer.batch_decode(*__a , **__a ) def UpperCamelCase__ ( self , *lowercase_ , **lowercase_ ): """simple docstring""" return self.tokenizer.decode(*__a , **__a ) @property def UpperCamelCase__ ( self ): """simple docstring""" return ["input_ids", "attention_mask", "pixel_values"]
367
"""simple docstring""" import tempfile import torch from diffusers import PNDMScheduler from .test_schedulers import SchedulerCommonTest class A_ (lowercase__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : str = (PNDMScheduler,) SCREAMING_SNAKE_CASE__ : str = (("""num_inference_steps""", 50),) def UpperCamelCase__ ( self , **lowercase_ ): """simple docstring""" UpperCAmelCase_ : int = { "num_train_timesteps": 1000, "beta_start": 0.00_01, "beta_end": 0.02, "beta_schedule": "linear", } config.update(**lowercase_ ) return config def UpperCamelCase__ ( self , lowercase_=0 , **lowercase_ ): """simple docstring""" UpperCAmelCase_ : str = dict(self.forward_default_kwargs ) UpperCAmelCase_ : List[str] = kwargs.pop("num_inference_steps" , lowercase_ ) UpperCAmelCase_ : Union[str, Any] = self.dummy_sample UpperCAmelCase_ : Dict = 0.1 * sample UpperCAmelCase_ : Dict = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: UpperCAmelCase_ : List[Any] = self.get_scheduler_config(**lowercase_ ) UpperCAmelCase_ : Dict = scheduler_class(**lowercase_ ) scheduler.set_timesteps(lowercase_ ) # copy over dummy past residuals UpperCAmelCase_ : List[Any] = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowercase_ ) UpperCAmelCase_ : Optional[int] = scheduler_class.from_pretrained(lowercase_ ) new_scheduler.set_timesteps(lowercase_ ) # copy over dummy past residuals UpperCAmelCase_ : int = dummy_past_residuals[:] UpperCAmelCase_ : List[str] = scheduler.step_prk(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample UpperCAmelCase_ : str = new_scheduler.step_prk(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" UpperCAmelCase_ : Optional[int] = scheduler.step_plms(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample UpperCAmelCase_ : Dict = new_scheduler.step_plms(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def UpperCamelCase__ ( self ): """simple docstring""" pass def UpperCamelCase__ ( self , lowercase_=0 , **lowercase_ ): """simple docstring""" UpperCAmelCase_ : List[Any] = dict(self.forward_default_kwargs ) UpperCAmelCase_ : str = kwargs.pop("num_inference_steps" , lowercase_ ) UpperCAmelCase_ : Optional[int] = self.dummy_sample UpperCAmelCase_ : List[str] = 0.1 * sample UpperCAmelCase_ : Tuple = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: UpperCAmelCase_ : str = self.get_scheduler_config() UpperCAmelCase_ : Dict = scheduler_class(**lowercase_ ) scheduler.set_timesteps(lowercase_ ) # copy over dummy past residuals (must be after setting timesteps) UpperCAmelCase_ : List[Any] = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowercase_ ) UpperCAmelCase_ : Dict = scheduler_class.from_pretrained(lowercase_ ) # copy over dummy past residuals new_scheduler.set_timesteps(lowercase_ ) # copy over dummy past residual (must be after setting timesteps) UpperCAmelCase_ : Optional[Any] = dummy_past_residuals[:] UpperCAmelCase_ : Union[str, Any] = scheduler.step_prk(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample UpperCAmelCase_ : Dict = new_scheduler.step_prk(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" UpperCAmelCase_ : List[str] = scheduler.step_plms(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample UpperCAmelCase_ : int = new_scheduler.step_plms(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def UpperCamelCase__ ( self , **lowercase_ ): """simple docstring""" UpperCAmelCase_ : str = self.scheduler_classes[0] UpperCAmelCase_ : Union[str, Any] = self.get_scheduler_config(**lowercase_ ) UpperCAmelCase_ : List[Any] = scheduler_class(**lowercase_ ) UpperCAmelCase_ : Tuple = 10 UpperCAmelCase_ : List[str] = self.dummy_model() UpperCAmelCase_ : str = self.dummy_sample_deter scheduler.set_timesteps(lowercase_ ) for i, t in enumerate(scheduler.prk_timesteps ): UpperCAmelCase_ : Tuple = model(lowercase_ , lowercase_ ) UpperCAmelCase_ : Optional[int] = scheduler.step_prk(lowercase_ , lowercase_ , lowercase_ ).prev_sample for i, t in enumerate(scheduler.plms_timesteps ): UpperCAmelCase_ : Any = model(lowercase_ , lowercase_ ) UpperCAmelCase_ : Optional[Any] = scheduler.step_plms(lowercase_ , lowercase_ , lowercase_ ).prev_sample return sample def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : str = dict(self.forward_default_kwargs ) UpperCAmelCase_ : Optional[Any] = kwargs.pop("num_inference_steps" , lowercase_ ) for scheduler_class in self.scheduler_classes: UpperCAmelCase_ : Any = self.get_scheduler_config() UpperCAmelCase_ : Tuple = scheduler_class(**lowercase_ ) UpperCAmelCase_ : str = self.dummy_sample UpperCAmelCase_ : List[Any] = 0.1 * sample if num_inference_steps is not None and hasattr(lowercase_ , "set_timesteps" ): scheduler.set_timesteps(lowercase_ ) elif num_inference_steps is not None and not hasattr(lowercase_ , "set_timesteps" ): UpperCAmelCase_ : List[Any] = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) UpperCAmelCase_ : List[str] = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] UpperCAmelCase_ : List[str] = dummy_past_residuals[:] UpperCAmelCase_ : str = scheduler.step_prk(lowercase_ , 0 , lowercase_ , **lowercase_ ).prev_sample UpperCAmelCase_ : Any = scheduler.step_prk(lowercase_ , 1 , lowercase_ , **lowercase_ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) UpperCAmelCase_ : Optional[Any] = scheduler.step_plms(lowercase_ , 0 , lowercase_ , **lowercase_ ).prev_sample UpperCAmelCase_ : Optional[Any] = scheduler.step_plms(lowercase_ , 1 , lowercase_ , **lowercase_ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def UpperCamelCase__ ( self ): """simple docstring""" for timesteps in [100, 1000]: self.check_over_configs(num_train_timesteps=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" for steps_offset in [0, 1]: self.check_over_configs(steps_offset=lowercase_ ) UpperCAmelCase_ : Optional[int] = self.scheduler_classes[0] UpperCAmelCase_ : int = self.get_scheduler_config(steps_offset=1 ) UpperCAmelCase_ : Optional[Any] = scheduler_class(**lowercase_ ) scheduler.set_timesteps(10 ) assert torch.equal( scheduler.timesteps , torch.LongTensor( [901, 851, 851, 801, 801, 751, 751, 701, 701, 651, 651, 601, 601, 501, 401, 301, 201, 101, 1] ) , ) def UpperCamelCase__ ( self ): """simple docstring""" for beta_start, beta_end in zip([0.00_01, 0.0_01] , [0.0_02, 0.02] ): self.check_over_configs(beta_start=lowercase_ , beta_end=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" for t in [1, 5, 10]: self.check_over_forward(time_step=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" for t, num_inference_steps in zip([1, 5, 10] , [10, 50, 100] ): self.check_over_forward(num_inference_steps=lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" # earlier version of set_timesteps() caused an error indexing alpha's with inference steps as power of 3 UpperCAmelCase_ : List[Any] = 27 for scheduler_class in self.scheduler_classes: UpperCAmelCase_ : List[Any] = self.dummy_sample UpperCAmelCase_ : Optional[int] = 0.1 * sample UpperCAmelCase_ : List[str] = self.get_scheduler_config() UpperCAmelCase_ : List[str] = scheduler_class(**lowercase_ ) scheduler.set_timesteps(lowercase_ ) # before power of 3 fix, would error on first step, so we only need to do two for i, t in enumerate(scheduler.prk_timesteps[:2] ): UpperCAmelCase_ : List[str] = scheduler.step_prk(lowercase_ , lowercase_ , lowercase_ ).prev_sample def UpperCamelCase__ ( self ): """simple docstring""" with self.assertRaises(lowercase_ ): UpperCAmelCase_ : List[str] = self.scheduler_classes[0] UpperCAmelCase_ : str = self.get_scheduler_config() UpperCAmelCase_ : Tuple = scheduler_class(**lowercase_ ) scheduler.step_plms(self.dummy_sample , 1 , self.dummy_sample ).prev_sample def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[Any] = self.full_loop() UpperCAmelCase_ : Any = torch.sum(torch.abs(lowercase_ ) ) UpperCAmelCase_ : Dict = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_sum.item() - 1_98.13_18 ) < 1E-2 assert abs(result_mean.item() - 0.25_80 ) < 1E-3 def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] = self.full_loop(prediction_type="v_prediction" ) UpperCAmelCase_ : str = torch.sum(torch.abs(lowercase_ ) ) UpperCAmelCase_ : Tuple = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_sum.item() - 67.39_86 ) < 1E-2 assert abs(result_mean.item() - 0.08_78 ) < 1E-3 def UpperCamelCase__ ( self ): """simple docstring""" # We specify different beta, so that the first alpha is 0.99 UpperCAmelCase_ : Union[str, Any] = self.full_loop(set_alpha_to_one=lowercase_ , beta_start=0.01 ) UpperCAmelCase_ : List[Any] = torch.sum(torch.abs(lowercase_ ) ) UpperCAmelCase_ : int = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_sum.item() - 2_30.03_99 ) < 1E-2 assert abs(result_mean.item() - 0.29_95 ) < 1E-3 def UpperCamelCase__ ( self ): """simple docstring""" # We specify different beta, so that the first alpha is 0.99 UpperCAmelCase_ : Tuple = self.full_loop(set_alpha_to_one=lowercase_ , beta_start=0.01 ) UpperCAmelCase_ : int = torch.sum(torch.abs(lowercase_ ) ) UpperCAmelCase_ : Tuple = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_sum.item() - 1_86.94_82 ) < 1E-2 assert abs(result_mean.item() - 0.24_34 ) < 1E-3
23
0
"""simple docstring""" def lowercase (snake_case__ : int = 1_000 ) -> int: '''simple docstring''' lowerCAmelCase = 3 lowerCAmelCase = 0 while a < n: if a % 3 == 0 or a % 5 == 0: result += a elif a % 15 == 0: result -= a a += 1 return result if __name__ == "__main__": print(f"""{solution() = }""")
155
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType a = logging.get_logger(__name__) a = { 'microsoft/deberta-v2-xlarge': 'https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json', 'microsoft/deberta-v2-xxlarge': 'https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json', 'microsoft/deberta-v2-xlarge-mnli': ( 'https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json' ), 'microsoft/deberta-v2-xxlarge-mnli': ( 'https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json' ), } class SCREAMING_SNAKE_CASE__ ( _a ): _a = 'deberta-v2' def __init__( self : Optional[Any] , lowerCAmelCase : Optional[Any]=12_8100 , lowerCAmelCase : Any=1536 , lowerCAmelCase : str=24 , lowerCAmelCase : str=24 , lowerCAmelCase : Optional[Any]=6144 , lowerCAmelCase : List[str]="gelu" , lowerCAmelCase : str=0.1 , lowerCAmelCase : Dict=0.1 , lowerCAmelCase : Optional[int]=512 , lowerCAmelCase : Optional[Any]=0 , lowerCAmelCase : Optional[int]=0.02 , lowerCAmelCase : Optional[int]=1e-7 , lowerCAmelCase : Union[str, Any]=False , lowerCAmelCase : Dict=-1 , lowerCAmelCase : int=0 , lowerCAmelCase : Union[str, Any]=True , lowerCAmelCase : int=None , lowerCAmelCase : Any=0 , lowerCAmelCase : Dict="gelu" , **lowerCAmelCase : Any , ): super().__init__(**lowerCAmelCase ) lowerCAmelCase = hidden_size lowerCAmelCase = num_hidden_layers lowerCAmelCase = num_attention_heads lowerCAmelCase = intermediate_size lowerCAmelCase = hidden_act lowerCAmelCase = hidden_dropout_prob lowerCAmelCase = attention_probs_dropout_prob lowerCAmelCase = max_position_embeddings lowerCAmelCase = type_vocab_size lowerCAmelCase = initializer_range lowerCAmelCase = relative_attention lowerCAmelCase = max_relative_positions lowerCAmelCase = pad_token_id lowerCAmelCase = position_biased_input # Backwards compatibility if type(lowerCAmelCase ) == str: lowerCAmelCase = [x.strip() for x in pos_att_type.lower().split("""|""" )] lowerCAmelCase = pos_att_type lowerCAmelCase = vocab_size lowerCAmelCase = layer_norm_eps lowerCAmelCase = kwargs.get("""pooler_hidden_size""" , lowerCAmelCase ) lowerCAmelCase = pooler_dropout lowerCAmelCase = pooler_hidden_act class SCREAMING_SNAKE_CASE__ ( _a ): @property def __lowercase ( self : Optional[int] ): if self.task == "multiple-choice": lowerCAmelCase = {0: """batch""", 1: """choice""", 2: """sequence"""} else: lowerCAmelCase = {0: """batch""", 1: """sequence"""} if self._config.type_vocab_size > 0: return OrderedDict( [("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis)] ) else: return OrderedDict([("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis)] ) @property def __lowercase ( self : List[str] ): return 12 def __lowercase ( self : int , lowerCAmelCase : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"] , lowerCAmelCase : int = -1 , lowerCAmelCase : int = -1 , lowerCAmelCase : int = -1 , lowerCAmelCase : bool = False , lowerCAmelCase : Optional["TensorType"] = None , lowerCAmelCase : int = 3 , lowerCAmelCase : int = 40 , lowerCAmelCase : int = 40 , lowerCAmelCase : "PreTrainedTokenizerBase" = None , ): lowerCAmelCase = super().generate_dummy_inputs(preprocessor=lowerCAmelCase , framework=lowerCAmelCase ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
155
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a_ = { 'configuration_time_series_transformer': [ 'TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TimeSeriesTransformerConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ 'TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TimeSeriesTransformerForPrediction', 'TimeSeriesTransformerModel', 'TimeSeriesTransformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys a_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
50
from timeit import timeit a_ = { 'MALAYALAM': True, 'String': False, 'rotor': True, 'level': True, 'A': True, 'BB': True, 'ABC': False, 'amanaplanacanalpanama': True, # "a man a plan a canal panama" } # Ensure our test data is valid assert all((key == key[::-1]) is value for key, value in test_data.items()) def __lowercase ( lowerCamelCase : str ): UpperCamelCase_ : Union[str, Any] = 0 UpperCamelCase_ : Optional[Any] = len(lowerCamelCase ) - 1 while start_i < end_i: if s[start_i] == s[end_i]: start_i += 1 end_i -= 1 else: return False return True def __lowercase ( lowerCamelCase : str ): UpperCamelCase_ : Union[str, Any] = len(lowerCamelCase ) // 2 UpperCamelCase_ : Tuple = len(lowerCamelCase ) # We need to traverse till half of the length of string # as we can get access of the i'th last element from # i'th index. # eg: [0,1,2,3,4,5] => 4th index can be accessed # with the help of 1st index (i==n-i-1) # where n is length of string return all(s[i] == s[n - i - 1] for i in range(lowerCamelCase ) ) def __lowercase ( lowerCamelCase : str ): if len(lowerCamelCase ) <= 2: return True if s[0] == s[len(lowerCamelCase ) - 1]: return is_palindrome_recursive(s[1:-1] ) else: return False def __lowercase ( lowerCamelCase : str ): return s == s[::-1] def __lowercase ( lowerCamelCase : str ): UpperCamelCase_ : Optional[Any] = F"all({name}(key) is value for key, value in test_data.items())" UpperCamelCase_ : Tuple = F"from __main__ import test_data, {name}" UpperCamelCase_ : List[str] = 500000 UpperCamelCase_ : int = timeit(stmt=lowerCamelCase , setup=lowerCamelCase , number=lowerCamelCase ) print(F"{name:<35} finished {number:,} runs in {result:.5f} seconds" ) if __name__ == "__main__": for key, value in test_data.items(): assert is_palindrome(key) is is_palindrome_recursive(key) assert is_palindrome(key) is is_palindrome_slice(key) print(F"""{key:21} {value}""") print('a man a plan a canal panama') # finished 500,000 runs in 0.46793 seconds benchmark_function('is_palindrome_slice') # finished 500,000 runs in 0.85234 seconds benchmark_function('is_palindrome') # finished 500,000 runs in 1.32028 seconds benchmark_function('is_palindrome_recursive') # finished 500,000 runs in 2.08679 seconds benchmark_function('is_palindrome_traversal')
50
1
"""simple docstring""" def _snake_case ( lowerCamelCase__ : int , lowerCamelCase__ : int ) -> str: if a < 0 or b < 0: raise ValueError("the value of both inputs must be positive" ) lowerCamelCase_ : Dict =str(bin(lowerCamelCase__ ) )[2:] # remove the leading "0b" lowerCamelCase_ : int =str(bin(lowerCamelCase__ ) )[2:] # remove the leading "0b" lowerCamelCase_ : Any =max(len(lowerCamelCase__ ) , len(lowerCamelCase__ ) ) return "0b" + "".join( str(int(char_a != char_b ) ) for char_a, char_b in zip(a_binary.zfill(lowerCamelCase__ ) , b_binary.zfill(lowerCamelCase__ ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
144
"""simple docstring""" import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ConvNextConfig, SegformerImageProcessor, UperNetConfig, UperNetForSemanticSegmentation def _snake_case ( lowerCamelCase__ : Tuple ) -> List[Any]: lowerCamelCase_ : Union[str, Any] =384 if "tiny" in model_name: lowerCamelCase_ : str =[3, 3, 9, 3] lowerCamelCase_ : Union[str, Any] =[96, 192, 384, 768] if "small" in model_name: lowerCamelCase_ : Tuple =[3, 3, 27, 3] lowerCamelCase_ : List[str] =[96, 192, 384, 768] if "base" in model_name: lowerCamelCase_ : Tuple =[3, 3, 27, 3] lowerCamelCase_ : Tuple =[128, 256, 512, 1_024] lowerCamelCase_ : str =512 if "large" in model_name: lowerCamelCase_ : Optional[int] =[3, 3, 27, 3] lowerCamelCase_ : Optional[int] =[192, 384, 768, 1_536] lowerCamelCase_ : Optional[Any] =768 if "xlarge" in model_name: lowerCamelCase_ : str =[3, 3, 27, 3] lowerCamelCase_ : Optional[Any] =[256, 512, 1_024, 2_048] lowerCamelCase_ : Any =1_024 # set label information lowerCamelCase_ : Dict =150 lowerCamelCase_ : Union[str, Any] ="huggingface/label-files" lowerCamelCase_ : Optional[int] ="ade20k-id2label.json" lowerCamelCase_ : str =json.load(open(hf_hub_download(lowerCamelCase__ , lowerCamelCase__ , repo_type="dataset" ) , "r" ) ) lowerCamelCase_ : Dict ={int(lowerCamelCase__ ): v for k, v in idalabel.items()} lowerCamelCase_ : Optional[Any] ={v: k for k, v in idalabel.items()} lowerCamelCase_ : Optional[int] =ConvNextConfig( depths=lowerCamelCase__ , hidden_sizes=lowerCamelCase__ , out_features=["stage1", "stage2", "stage3", "stage4"] ) lowerCamelCase_ : Any =UperNetConfig( backbone_config=lowerCamelCase__ , auxiliary_in_channels=lowerCamelCase__ , num_labels=lowerCamelCase__ , idalabel=lowerCamelCase__ , labelaid=lowerCamelCase__ , ) return config def _snake_case ( lowerCamelCase__ : str ) -> str: lowerCamelCase_ : List[str] =[] # fmt: off # stem rename_keys.append(("backbone.downsample_layers.0.0.weight", "backbone.embeddings.patch_embeddings.weight") ) rename_keys.append(("backbone.downsample_layers.0.0.bias", "backbone.embeddings.patch_embeddings.bias") ) rename_keys.append(("backbone.downsample_layers.0.1.weight", "backbone.embeddings.layernorm.weight") ) rename_keys.append(("backbone.downsample_layers.0.1.bias", "backbone.embeddings.layernorm.bias") ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F"""backbone.stages.{i}.{j}.gamma""", F"""backbone.encoder.stages.{i}.layers.{j}.layer_scale_parameter""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.depthwise_conv.weight""", F"""backbone.encoder.stages.{i}.layers.{j}.dwconv.weight""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.depthwise_conv.bias""", F"""backbone.encoder.stages.{i}.layers.{j}.dwconv.bias""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.norm.weight""", F"""backbone.encoder.stages.{i}.layers.{j}.layernorm.weight""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.norm.bias""", F"""backbone.encoder.stages.{i}.layers.{j}.layernorm.bias""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.pointwise_conv1.weight""", F"""backbone.encoder.stages.{i}.layers.{j}.pwconv1.weight""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.pointwise_conv1.bias""", F"""backbone.encoder.stages.{i}.layers.{j}.pwconv1.bias""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.pointwise_conv2.weight""", F"""backbone.encoder.stages.{i}.layers.{j}.pwconv2.weight""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.pointwise_conv2.bias""", F"""backbone.encoder.stages.{i}.layers.{j}.pwconv2.bias""") ) if i > 0: rename_keys.append((F"""backbone.downsample_layers.{i}.0.weight""", F"""backbone.encoder.stages.{i}.downsampling_layer.0.weight""") ) rename_keys.append((F"""backbone.downsample_layers.{i}.0.bias""", F"""backbone.encoder.stages.{i}.downsampling_layer.0.bias""") ) rename_keys.append((F"""backbone.downsample_layers.{i}.1.weight""", F"""backbone.encoder.stages.{i}.downsampling_layer.1.weight""") ) rename_keys.append((F"""backbone.downsample_layers.{i}.1.bias""", F"""backbone.encoder.stages.{i}.downsampling_layer.1.bias""") ) rename_keys.append((F"""backbone.norm{i}.weight""", F"""backbone.hidden_states_norms.stage{i+1}.weight""") ) rename_keys.append((F"""backbone.norm{i}.bias""", F"""backbone.hidden_states_norms.stage{i+1}.bias""") ) # decode head rename_keys.extend( [ ("decode_head.conv_seg.weight", "decode_head.classifier.weight"), ("decode_head.conv_seg.bias", "decode_head.classifier.bias"), ("auxiliary_head.conv_seg.weight", "auxiliary_head.classifier.weight"), ("auxiliary_head.conv_seg.bias", "auxiliary_head.classifier.bias"), ] ) # fmt: on return rename_keys def _snake_case ( lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : List[str] , lowerCamelCase__ : Any ) -> Dict: lowerCamelCase_ : List[str] =dct.pop(lowerCamelCase__ ) lowerCamelCase_ : Union[str, Any] =val def _snake_case ( lowerCamelCase__ : List[Any] , lowerCamelCase__ : List[str] , lowerCamelCase__ : Union[str, Any] ) -> Dict: lowerCamelCase_ : Union[str, Any] ={ "upernet-convnext-tiny": "https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_tiny_fp16_512x512_160k_ade20k/upernet_convnext_tiny_fp16_512x512_160k_ade20k_20220227_124553-cad485de.pth", "upernet-convnext-small": "https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_small_fp16_512x512_160k_ade20k/upernet_convnext_small_fp16_512x512_160k_ade20k_20220227_131208-1b1e394f.pth", "upernet-convnext-base": "https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_base_fp16_512x512_160k_ade20k/upernet_convnext_base_fp16_512x512_160k_ade20k_20220227_181227-02a24fc6.pth", "upernet-convnext-large": "https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_large_fp16_640x640_160k_ade20k/upernet_convnext_large_fp16_640x640_160k_ade20k_20220226_040532-e57aa54d.pth", "upernet-convnext-xlarge": "https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_xlarge_fp16_640x640_160k_ade20k/upernet_convnext_xlarge_fp16_640x640_160k_ade20k_20220226_080344-95fc38c2.pth", } lowerCamelCase_ : Optional[int] =model_name_to_url[model_name] lowerCamelCase_ : Optional[Any] =torch.hub.load_state_dict_from_url(lowerCamelCase__ , map_location="cpu" )["state_dict"] lowerCamelCase_ : List[Any] =get_upernet_config(lowerCamelCase__ ) lowerCamelCase_ : Tuple =UperNetForSemanticSegmentation(lowerCamelCase__ ) model.eval() # replace "bn" => "batch_norm" for key in state_dict.copy().keys(): lowerCamelCase_ : Optional[Any] =state_dict.pop(lowerCamelCase__ ) if "bn" in key: lowerCamelCase_ : str =key.replace("bn" , "batch_norm" ) lowerCamelCase_ : Union[str, Any] =val # rename keys lowerCamelCase_ : Tuple =create_rename_keys(lowerCamelCase__ ) for src, dest in rename_keys: rename_key(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) model.load_state_dict(lowerCamelCase__ ) # verify on image lowerCamelCase_ : List[str] ="https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg" lowerCamelCase_ : Union[str, Any] =Image.open(requests.get(lowerCamelCase__ , stream=lowerCamelCase__ ).raw ).convert("RGB" ) lowerCamelCase_ : List[str] =SegformerImageProcessor() lowerCamelCase_ : int =processor(lowerCamelCase__ , return_tensors="pt" ).pixel_values with torch.no_grad(): lowerCamelCase_ : Tuple =model(lowerCamelCase__ ) if model_name == "upernet-convnext-tiny": lowerCamelCase_ : List[Any] =torch.tensor( [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]] ) elif model_name == "upernet-convnext-small": lowerCamelCase_ : Dict =torch.tensor( [[-8.8236, -8.8236, -8.6771], [-8.8236, -8.8236, -8.6771], [-8.7638, -8.7638, -8.6240]] ) elif model_name == "upernet-convnext-base": lowerCamelCase_ : Tuple =torch.tensor( [[-8.8558, -8.8558, -8.6905], [-8.8558, -8.8558, -8.6905], [-8.7669, -8.7669, -8.6021]] ) elif model_name == "upernet-convnext-large": lowerCamelCase_ : Dict =torch.tensor( [[-8.6660, -8.6660, -8.6210], [-8.6660, -8.6660, -8.6210], [-8.6310, -8.6310, -8.5964]] ) elif model_name == "upernet-convnext-xlarge": lowerCamelCase_ : List[Any] =torch.tensor( [[-8.4980, -8.4980, -8.3977], [-8.4980, -8.4980, -8.3977], [-8.4379, -8.4379, -8.3412]] ) print("Logits:" , outputs.logits[0, 0, :3, :3] ) assert torch.allclose(outputs.logits[0, 0, :3, :3] , lowerCamelCase__ , atol=1e-4 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(lowerCamelCase__ ) print(F"""Saving processor to {pytorch_dump_folder_path}""" ) processor.save_pretrained(lowerCamelCase__ ) if push_to_hub: print(F"""Pushing model and processor for {model_name} to hub""" ) model.push_to_hub(F"""openmmlab/{model_name}""" ) processor.push_to_hub(F"""openmmlab/{model_name}""" ) if __name__ == "__main__": A__ : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='upernet-convnext-tiny', type=str, choices=[f'upernet-convnext-{size}' for size in ['tiny', 'small', 'base', 'large', 'xlarge']], help='Name of the ConvNext UperNet model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) A__ : str = parser.parse_args() convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
144
1
"""simple docstring""" from ...processing_utils import ProcessorMixin class __snake_case ( SCREAMING_SNAKE_CASE__ ): """simple docstring""" _lowerCamelCase = """WhisperFeatureExtractor""" _lowerCamelCase = """WhisperTokenizer""" def __init__( self , __lowerCamelCase , __lowerCamelCase ): '''simple docstring''' super().__init__(__lowerCamelCase , __lowerCamelCase ) __A : Dict = self.feature_extractor __A : Dict = False def UpperCamelCase__( self , __lowerCamelCase=None , __lowerCamelCase=None , __lowerCamelCase=True ): '''simple docstring''' return self.tokenizer.get_decoder_prompt_ids(task=__lowerCamelCase , language=__lowerCamelCase , no_timestamps=__lowerCamelCase ) def __call__( self , *__lowerCamelCase , **__lowerCamelCase ): '''simple docstring''' if self._in_target_context_manager: return self.current_processor(*__lowerCamelCase , **__lowerCamelCase ) __A : Optional[Any] = kwargs.pop('''audio''' , __lowerCamelCase ) __A : Optional[Any] = kwargs.pop('''sampling_rate''' , __lowerCamelCase ) __A : List[Any] = kwargs.pop('''text''' , __lowerCamelCase ) if len(__lowerCamelCase ) > 0: __A : Tuple = args[0] __A : Optional[Any] = 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 : str = self.feature_extractor(__lowerCamelCase , *__lowerCamelCase , sampling_rate=__lowerCamelCase , **__lowerCamelCase ) if text is not None: __A : List[str] = self.tokenizer(__lowerCamelCase , **__lowerCamelCase ) if text is None: return inputs elif audio is None: return encodings else: __A : int = encodings['''input_ids'''] return inputs def UpperCamelCase__( self , *__lowerCamelCase , **__lowerCamelCase ): '''simple docstring''' return self.tokenizer.batch_decode(*__lowerCamelCase , **__lowerCamelCase ) def UpperCamelCase__( self , *__lowerCamelCase , **__lowerCamelCase ): '''simple docstring''' return self.tokenizer.decode(*__lowerCamelCase , **__lowerCamelCase ) def UpperCamelCase__( self , __lowerCamelCase , __lowerCamelCase="np" ): '''simple docstring''' return self.tokenizer.get_prompt_ids(__lowerCamelCase , return_tensors=__lowerCamelCase )
364
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available a_ = { """configuration_bloom""": ["""BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BloomConfig""", """BloomOnnxConfig"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = ["""BloomTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ """BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST""", """BloomForCausalLM""", """BloomModel""", """BloomPreTrainedModel""", """BloomForSequenceClassification""", """BloomForTokenClassification""", """BloomForQuestionAnswering""", ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys a_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
291
0
'''simple docstring''' def _lowerCamelCase ( lowercase : str ) -> list: if n_term == "": return [] _a = [] for temp in range(int(lowercase ) ): series.append(F'1/{temp + 1}' if series else "1" ) return series if __name__ == "__main__": lowerCAmelCase_ : Union[str, Any] = 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))
63
import operator def lowercase_ ( _lowerCamelCase : list , _lowerCamelCase : bool = False , _lowerCamelCase : list | None = None): lowercase__ : int = operator.lt if reverse else operator.gt lowercase__ : str = solution or [] if not arr: return solution lowercase__ : List[str] = [arr.pop(0)] for i, item in enumerate(_lowerCamelCase): if _operator(_lowerCamelCase , sublist[-1]): sublist.append(_lowerCamelCase) arr.pop(_lowerCamelCase) # merging sublist into solution list if not solution: solution.extend(_lowerCamelCase) else: while sublist: lowercase__ : str = sublist.pop(0) for i, xx in enumerate(_lowerCamelCase): if not _operator(_lowerCamelCase , _lowerCamelCase): solution.insert(_lowerCamelCase , _lowerCamelCase) break else: solution.append(_lowerCamelCase) strand_sort(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase) return solution if __name__ == "__main__": assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5] assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]
87
0
import argparse import os from . import ( ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BART_PRETRAINED_MODEL_ARCHIVE_LIST, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST, ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, T5_PRETRAINED_CONFIG_ARCHIVE_MAP, TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP, XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, AlbertConfig, BartConfig, BertConfig, CamembertConfig, CTRLConfig, DistilBertConfig, DPRConfig, ElectraConfig, FlaubertConfig, GPTaConfig, LayoutLMConfig, LxmertConfig, OpenAIGPTConfig, RobertaConfig, TaConfig, TFAlbertForPreTraining, TFBartForConditionalGeneration, TFBartForSequenceClassification, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFCamembertForMaskedLM, TFCTRLLMHeadModel, TFDistilBertForMaskedLM, TFDistilBertForQuestionAnswering, TFDPRContextEncoder, TFDPRQuestionEncoder, TFDPRReader, TFElectraForPreTraining, TFFlaubertWithLMHeadModel, TFGPTaLMHeadModel, TFLayoutLMForMaskedLM, TFLxmertForPreTraining, TFLxmertVisualFeatureEncoder, TFOpenAIGPTLMHeadModel, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForSequenceClassification, TFTaForConditionalGeneration, TFTransfoXLLMHeadModel, TFWavaVecaModel, TFXLMRobertaForMaskedLM, TFXLMWithLMHeadModel, TFXLNetLMHeadModel, TransfoXLConfig, WavaVecaConfig, WavaVecaModel, XLMConfig, XLMRobertaConfig, XLNetConfig, is_torch_available, load_pytorch_checkpoint_in_tfa_model, ) from .utils import CONFIG_NAME, WEIGHTS_NAME, cached_file, logging if is_torch_available(): import numpy as np import torch from . import ( AlbertForPreTraining, BartForConditionalGeneration, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, CamembertForMaskedLM, CTRLLMHeadModel, DistilBertForMaskedLM, DistilBertForQuestionAnswering, DPRContextEncoder, DPRQuestionEncoder, DPRReader, ElectraForPreTraining, FlaubertWithLMHeadModel, GPTaLMHeadModel, LayoutLMForMaskedLM, LxmertForPreTraining, LxmertVisualFeatureEncoder, OpenAIGPTLMHeadModel, RobertaForMaskedLM, RobertaForSequenceClassification, TaForConditionalGeneration, TransfoXLLMHeadModel, XLMRobertaForMaskedLM, XLMWithLMHeadModel, XLNetLMHeadModel, ) logging.set_verbosity_info() a__: Dict = { 'bart': ( BartConfig, TFBartForConditionalGeneration, TFBartForSequenceClassification, BartForConditionalGeneration, BART_PRETRAINED_MODEL_ARCHIVE_LIST, ), 'bert': ( BertConfig, TFBertForPreTraining, BertForPreTraining, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'bert-large-uncased-whole-word-masking-finetuned-squad': ( BertConfig, TFBertForQuestionAnswering, BertForQuestionAnswering, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'bert-large-cased-whole-word-masking-finetuned-squad': ( BertConfig, TFBertForQuestionAnswering, BertForQuestionAnswering, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'bert-base-cased-finetuned-mrpc': ( BertConfig, TFBertForSequenceClassification, BertForSequenceClassification, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'dpr': ( DPRConfig, TFDPRQuestionEncoder, TFDPRContextEncoder, TFDPRReader, DPRQuestionEncoder, DPRContextEncoder, DPRReader, DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST, ), 'gpt2': ( GPTaConfig, TFGPTaLMHeadModel, GPTaLMHeadModel, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'xlnet': ( XLNetConfig, TFXLNetLMHeadModel, XLNetLMHeadModel, XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'xlm': ( XLMConfig, TFXLMWithLMHeadModel, XLMWithLMHeadModel, XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'xlm-roberta': ( XLMRobertaConfig, TFXLMRobertaForMaskedLM, XLMRobertaForMaskedLM, XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'transfo-xl': ( TransfoXLConfig, TFTransfoXLLMHeadModel, TransfoXLLMHeadModel, TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'openai-gpt': ( OpenAIGPTConfig, TFOpenAIGPTLMHeadModel, OpenAIGPTLMHeadModel, OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'roberta': ( RobertaConfig, TFRobertaForCausalLM, TFRobertaForMaskedLM, RobertaForMaskedLM, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'layoutlm': ( LayoutLMConfig, TFLayoutLMForMaskedLM, LayoutLMForMaskedLM, LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, ), 'roberta-large-mnli': ( RobertaConfig, TFRobertaForSequenceClassification, RobertaForSequenceClassification, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'camembert': ( CamembertConfig, TFCamembertForMaskedLM, CamembertForMaskedLM, CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'flaubert': ( FlaubertConfig, TFFlaubertWithLMHeadModel, FlaubertWithLMHeadModel, FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'distilbert': ( DistilBertConfig, TFDistilBertForMaskedLM, DistilBertForMaskedLM, DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'distilbert-base-distilled-squad': ( DistilBertConfig, TFDistilBertForQuestionAnswering, DistilBertForQuestionAnswering, DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'lxmert': ( LxmertConfig, TFLxmertForPreTraining, LxmertForPreTraining, LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'lxmert-visual-feature-encoder': ( LxmertConfig, TFLxmertVisualFeatureEncoder, LxmertVisualFeatureEncoder, LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'ctrl': ( CTRLConfig, TFCTRLLMHeadModel, CTRLLMHeadModel, CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'albert': ( AlbertConfig, TFAlbertForPreTraining, AlbertForPreTraining, ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 't5': ( TaConfig, TFTaForConditionalGeneration, TaForConditionalGeneration, T5_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'electra': ( ElectraConfig, TFElectraForPreTraining, ElectraForPreTraining, ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'wav2vec2': ( WavaVecaConfig, TFWavaVecaModel, WavaVecaModel, WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP, ), } def UpperCamelCase__( UpperCamelCase__ : int , UpperCamelCase__ : List[str] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Any , UpperCamelCase__ : Dict=False , UpperCamelCase__ : Dict=True )->Dict: if model_type not in MODEL_CLASSES: raise ValueError(f"Unrecognized model type, should be one of {list(MODEL_CLASSES.keys() )}." ) A__ , A__ , A__ , A__ = MODEL_CLASSES[model_type] # Initialise TF model if config_file in aws_config_map: A__ = cached_file(UpperCamelCase__ , UpperCamelCase__ , force_download=not use_cached_models ) A__ = config_class.from_json_file(UpperCamelCase__ ) A__ = True A__ = True print(f"Building TensorFlow model from configuration: {config}" ) A__ = model_class(UpperCamelCase__ ) # Load weights from tf checkpoint if pytorch_checkpoint_path in aws_config_map.keys(): A__ = cached_file( UpperCamelCase__ , UpperCamelCase__ , force_download=not use_cached_models ) # Load PyTorch checkpoint in tf2 model: A__ = load_pytorch_checkpoint_in_tfa_model(UpperCamelCase__ , UpperCamelCase__ ) if compare_with_pt_model: A__ = tf_model(tf_model.dummy_inputs , training=UpperCamelCase__ ) # build the network A__ = torch.load(UpperCamelCase__ , map_location='''cpu''' ) A__ = pt_model_class.from_pretrained( pretrained_model_name_or_path=UpperCamelCase__ , config=UpperCamelCase__ , state_dict=UpperCamelCase__ ) with torch.no_grad(): A__ = pt_model(**pt_model.dummy_inputs ) A__ = pto[0].numpy() A__ = tfo[0].numpy() A__ = np.amax(np.abs(np_pt - np_tf ) ) print(f"Max absolute difference between models outputs {diff}" ) assert diff <= 2e-2, f"Error, model absolute difference is >2e-2: {diff}" # Save pytorch-model print(f"Save TensorFlow model to {tf_dump_path}" ) tf_model.save_weights(UpperCamelCase__ , save_format='''h5''' ) def UpperCamelCase__( UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Union[str, Any]=None , UpperCamelCase__ : List[str]=None , UpperCamelCase__ : List[str]=False , UpperCamelCase__ : str=False , UpperCamelCase__ : Union[str, Any]=False , UpperCamelCase__ : str=False , )->str: if args_model_type is None: A__ = list(MODEL_CLASSES.keys() ) else: A__ = [args_model_type] for j, model_type in enumerate(UpperCamelCase__ , start=1 ): print('''=''' * 1_00 ) print(f" Converting model type {j}/{len(UpperCamelCase__ )}: {model_type}" ) print('''=''' * 1_00 ) if model_type not in MODEL_CLASSES: raise ValueError(f"Unrecognized model type {model_type}, should be one of {list(MODEL_CLASSES.keys() )}." ) A__ , A__ , A__ , A__ , A__ = MODEL_CLASSES[model_type] if model_shortcut_names_or_path is None: A__ = list(aws_model_maps.keys() ) if config_shortcut_names_or_path is None: A__ = model_shortcut_names_or_path for i, (model_shortcut_name, config_shortcut_name) in enumerate( zip(UpperCamelCase__ , UpperCamelCase__ ) , start=1 ): print('''-''' * 1_00 ) if "-squad" in model_shortcut_name or "-mrpc" in model_shortcut_name or "-mnli" in model_shortcut_name: if not only_convert_finetuned_models: print(f" Skipping finetuned checkpoint {model_shortcut_name}" ) continue A__ = model_shortcut_name elif only_convert_finetuned_models: print(f" Skipping not finetuned checkpoint {model_shortcut_name}" ) continue print( f" Converting checkpoint {i}/{len(UpperCamelCase__ )}: {model_shortcut_name} - model_type {model_type}" ) print('''-''' * 1_00 ) if config_shortcut_name in aws_config_map: A__ = cached_file(UpperCamelCase__ , UpperCamelCase__ , force_download=not use_cached_models ) else: A__ = config_shortcut_name if model_shortcut_name in aws_model_maps: A__ = cached_file(UpperCamelCase__ , UpperCamelCase__ , force_download=not use_cached_models ) else: A__ = model_shortcut_name if os.path.isfile(UpperCamelCase__ ): A__ = '''converted_model''' convert_pt_checkpoint_to_tf( model_type=UpperCamelCase__ , pytorch_checkpoint_path=UpperCamelCase__ , config_file=UpperCamelCase__ , tf_dump_path=os.path.join(UpperCamelCase__ , model_shortcut_name + '''-tf_model.h5''' ) , compare_with_pt_model=UpperCamelCase__ , ) if remove_cached_files: os.remove(UpperCamelCase__ ) os.remove(UpperCamelCase__ ) if __name__ == "__main__": a__: Any = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_dump_path', default=None, type=str, required=True, help='Path to the output Tensorflow dump file.' ) parser.add_argument( '--model_type', default=None, type=str, help=( F"Model type selected in the list of {list(MODEL_CLASSES.keys())}. If not given, will download and " 'convert all the models from AWS.' ), ) parser.add_argument( '--pytorch_checkpoint_path', default=None, type=str, help=( 'Path to the PyTorch checkpoint path or shortcut name to download from AWS. ' 'If not given, will download and convert all the checkpoints from AWS.' ), ) parser.add_argument( '--config_file', default=None, type=str, help=( 'The config json file corresponding to the pre-trained model. \n' 'This specifies the model architecture. If not given and ' '--pytorch_checkpoint_path is not given or is a shortcut name ' 'use the configuration associated to the shortcut name on the AWS' ), ) parser.add_argument( '--compare_with_pt_model', action='store_true', help='Compare Tensorflow and PyTorch model predictions.' ) parser.add_argument( '--use_cached_models', action='store_true', help='Use cached models if possible instead of updating to latest checkpoint versions.', ) parser.add_argument( '--remove_cached_files', action='store_true', help='Remove pytorch models after conversion (save memory when converting in batches).', ) parser.add_argument('--only_convert_finetuned_models', action='store_true', help='Only convert finetuned models.') a__: Optional[Any] = parser.parse_args() # if args.pytorch_checkpoint_path is not None: # convert_pt_checkpoint_to_tf(args.model_type.lower(), # args.pytorch_checkpoint_path, # args.config_file if args.config_file is not None else args.pytorch_checkpoint_path, # args.tf_dump_path, # compare_with_pt_model=args.compare_with_pt_model, # use_cached_models=args.use_cached_models) # else: convert_all_pt_checkpoints_to_tf( args.model_type.lower() if args.model_type is not None else None, args.tf_dump_path, model_shortcut_names_or_path=[args.pytorch_checkpoint_path] if args.pytorch_checkpoint_path is not None else None, config_shortcut_names_or_path=[args.config_file] if args.config_file is not None else None, compare_with_pt_model=args.compare_with_pt_model, use_cached_models=args.use_cached_models, remove_cached_files=args.remove_cached_files, only_convert_finetuned_models=args.only_convert_finetuned_models, )
358
# DISCLAIMER: This code is strongly influenced by https://github.com/pesser/pytorch_diffusion # and https://github.com/hojonathanho/diffusion import math from dataclasses import dataclass from typing import List, Optional, Tuple, Union import numpy as np import torch from diffusers.configuration_utils import ConfigMixin, register_to_config from diffusers.schedulers.scheduling_utils import SchedulerMixin from diffusers.utils import BaseOutput, deprecate @dataclass # Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->DDIM class SCREAMING_SNAKE_CASE__ ( UpperCamelCase__ ): __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = None def UpperCamelCase__( UpperCamelCase__ : List[Any] , UpperCamelCase__ : Tuple=0.999 , UpperCamelCase__ : Optional[int]="cosine" , )->Optional[Any]: if alpha_transform_type == "cosine": def alpha_bar_fn(UpperCamelCase__ : List[str] ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(UpperCamelCase__ : Union[str, Any] ): return math.exp(t * -12.0 ) else: raise ValueError(f"Unsupported alpha_tranform_type: {alpha_transform_type}" ) A__ = [] for i in range(UpperCamelCase__ ): A__ = i / num_diffusion_timesteps A__ = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(UpperCamelCase__ ) / alpha_bar_fn(UpperCamelCase__ ) , UpperCamelCase__ ) ) return torch.tensor(UpperCamelCase__ , dtype=torch.floataa ) class SCREAMING_SNAKE_CASE__ ( UpperCamelCase__ , UpperCamelCase__ ): __SCREAMING_SNAKE_CASE = 1 @register_to_config def __init__( self,__lowerCamelCase = 1000,__lowerCamelCase = 0.0001,__lowerCamelCase = 0.02,__lowerCamelCase = "linear",__lowerCamelCase = None,__lowerCamelCase = True,__lowerCamelCase = True,__lowerCamelCase = 0,__lowerCamelCase = "epsilon",__lowerCamelCase = 1.0,**__lowerCamelCase,): if kwargs.get('''set_alpha_to_one''',__lowerCamelCase ) is not None: A__ = ( '''The `set_alpha_to_one` argument is deprecated. Please use `set_alpha_to_zero` instead.''' ) deprecate('''set_alpha_to_one''','''1.0.0''',__lowerCamelCase,standard_warn=__lowerCamelCase ) A__ = kwargs['''set_alpha_to_one'''] if trained_betas is not None: A__ = torch.tensor(__lowerCamelCase,dtype=torch.floataa ) elif beta_schedule == "linear": A__ = torch.linspace(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. A__ = ( torch.linspace(beta_start**0.5,beta_end**0.5,__lowerCamelCase,dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule A__ = betas_for_alpha_bar(__lowerCamelCase ) else: raise NotImplementedError(f"{beta_schedule} does is not implemented for {self.__class__}" ) A__ = 1.0 - self.betas A__ = torch.cumprod(self.alphas,dim=0 ) # At every step in inverted ddim, we are looking into the next alphas_cumprod # For the final step, there is no next alphas_cumprod, and the index is out of bounds # `set_alpha_to_zero` decides whether we set this parameter simply to zero # in this case, self.step() just output the predicted noise # or whether we use the final alpha of the "non-previous" one. A__ = torch.tensor(0.0 ) if set_alpha_to_zero else self.alphas_cumprod[-1] # standard deviation of the initial noise distribution A__ = 1.0 # setable values A__ = None A__ = torch.from_numpy(np.arange(0,__lowerCamelCase ).copy().astype(np.intaa ) ) def UpperCamelCase ( self,__lowerCamelCase,__lowerCamelCase = None ): return sample def UpperCamelCase ( self,__lowerCamelCase,__lowerCamelCase = None ): if num_inference_steps > self.config.num_train_timesteps: raise ValueError( f"`num_inference_steps`: {num_inference_steps} cannot be larger than `self.config.train_timesteps`:" f" {self.config.num_train_timesteps} as the unet model trained with this scheduler can only handle" f" maximal {self.config.num_train_timesteps} timesteps." ) A__ = num_inference_steps A__ = self.config.num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 A__ = (np.arange(0,__lowerCamelCase ) * step_ratio).round().copy().astype(np.intaa ) A__ = torch.from_numpy(__lowerCamelCase ).to(__lowerCamelCase ) self.timesteps += self.config.steps_offset def UpperCamelCase ( self,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase = 0.0,__lowerCamelCase = False,__lowerCamelCase = None,__lowerCamelCase = True,): # 1. get previous step value (=t+1) A__ = timestep + self.config.num_train_timesteps // self.num_inference_steps # 2. compute alphas, betas # change original implementation to exactly match noise levels for analogous forward process A__ = self.alphas_cumprod[timestep] A__ = ( self.alphas_cumprod[prev_timestep] if prev_timestep < self.config.num_train_timesteps else self.final_alpha_cumprod ) A__ = 1 - alpha_prod_t # 3. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf if self.config.prediction_type == "epsilon": A__ = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 A__ = model_output elif self.config.prediction_type == "sample": A__ = model_output A__ = (sample - alpha_prod_t ** 0.5 * pred_original_sample) / beta_prod_t ** 0.5 elif self.config.prediction_type == "v_prediction": A__ = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output A__ = (alpha_prod_t**0.5) * model_output + (beta_prod_t**0.5) * sample else: raise ValueError( f"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample`, or" ''' `v_prediction`''' ) # 4. Clip or threshold "predicted x_0" if self.config.clip_sample: A__ = pred_original_sample.clamp( -self.config.clip_sample_range,self.config.clip_sample_range ) # 5. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf A__ = (1 - alpha_prod_t_prev) ** 0.5 * pred_epsilon # 6. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf A__ = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction if not return_dict: return (prev_sample, pred_original_sample) return DDIMSchedulerOutput(prev_sample=__lowerCamelCase,pred_original_sample=__lowerCamelCase ) def __len__( self ): return self.config.num_train_timesteps
39
0
"""simple docstring""" # DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from dataclasses import dataclass from typing import Optional, Tuple, Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin, SchedulerOutput @dataclass class __A ( _SCREAMING_SNAKE_CASE ): """simple docstring""" __lowerCAmelCase = 42 __lowerCAmelCase = 42 class __A ( _SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE ): """simple docstring""" __lowerCAmelCase = 1 @register_to_config def __init__( self , __A = 2000 , __A = 0.15 , __A = 0.01 , __A = 1_348.0 , __A = 1E-5 , __A = 1 , ) -> str: # standard deviation of the initial noise distribution a =sigma_max # setable values a =None self.set_sigmas(__A , __A , __A , __A ) def SCREAMING_SNAKE_CASE ( self , __A , __A = None ) -> torch.FloatTensor: return sample def SCREAMING_SNAKE_CASE ( self , __A , __A = None , __A = None ) -> Union[str, Any]: a =sampling_eps if sampling_eps is not None else self.config.sampling_eps a =torch.linspace(1 , __A , __A , device=__A ) def SCREAMING_SNAKE_CASE ( self , __A , __A = None , __A = None , __A = None ) -> Union[str, Any]: a =sigma_min if sigma_min is not None else self.config.sigma_min a =sigma_max if sigma_max is not None else self.config.sigma_max a =sampling_eps if sampling_eps is not None else self.config.sampling_eps if self.timesteps is None: self.set_timesteps(__A , __A ) a =sigma_min * (sigma_max / sigma_min) ** (self.timesteps / sampling_eps) a =torch.exp(torch.linspace(math.log(__A ) , math.log(__A ) , __A ) ) a =torch.tensor([sigma_min * (sigma_max / sigma_min) ** t for t in self.timesteps] ) def SCREAMING_SNAKE_CASE ( self , __A , __A ) -> Any: return torch.where( timesteps == 0 , torch.zeros_like(t.to(timesteps.device ) ) , self.discrete_sigmas[timesteps - 1].to(timesteps.device ) , ) def SCREAMING_SNAKE_CASE ( self , __A , __A , __A , __A = None , __A = True , ) -> Union[SdeVeOutput, Tuple]: if self.timesteps is None: raise ValueError( '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler''' ) a =timestep * torch.ones( sample.shape[0] , device=sample.device ) # torch.repeat_interleave(timestep, sample.shape[0]) a =(timestep * (len(self.timesteps ) - 1)).long() # mps requires indices to be in the same device, so we use cpu as is the default with cuda a =timesteps.to(self.discrete_sigmas.device ) a =self.discrete_sigmas[timesteps].to(sample.device ) a =self.get_adjacent_sigma(__A , __A ).to(sample.device ) a =torch.zeros_like(__A ) a =(sigma**2 - adjacent_sigma**2) ** 0.5 # equation 6 in the paper: the model_output modeled by the network is grad_x log pt(x) # also equation 47 shows the analog from SDE models to ancestral sampling methods a =diffusion.flatten() while len(diffusion.shape ) < len(sample.shape ): a =diffusion.unsqueeze(-1 ) a =drift - diffusion**2 * model_output # equation 6: sample noise for the diffusion term of a =randn_tensor( sample.shape , layout=sample.layout , generator=__A , device=sample.device , dtype=sample.dtype ) a =sample - drift # subtract because `dt` is a small negative timestep # TODO is the variable diffusion the correct scaling term for the noise? a =prev_sample_mean + diffusion * noise # add impact of diffusion field g if not return_dict: return (prev_sample, prev_sample_mean) return SdeVeOutput(prev_sample=__A , prev_sample_mean=__A ) def SCREAMING_SNAKE_CASE ( self , __A , __A , __A = None , __A = True , ) -> Union[SchedulerOutput, Tuple]: if self.timesteps is None: raise ValueError( '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler''' ) # For small batch sizes, the paper "suggest replacing norm(z) with sqrt(d), where d is the dim. of z" # sample noise for correction a =randn_tensor(sample.shape , layout=sample.layout , generator=__A ).to(sample.device ) # compute step size from the model_output, the noise, and the snr a =torch.norm(model_output.reshape(model_output.shape[0] , -1 ) , dim=-1 ).mean() a =torch.norm(noise.reshape(noise.shape[0] , -1 ) , dim=-1 ).mean() a =(self.config.snr * noise_norm / grad_norm) ** 2 * 2 a =step_size * torch.ones(sample.shape[0] ).to(sample.device ) # self.repeat_scalar(step_size, sample.shape[0]) # compute corrected sample: model_output term and noise term a =step_size.flatten() while len(step_size.shape ) < len(sample.shape ): a =step_size.unsqueeze(-1 ) a =sample + step_size * model_output a =prev_sample_mean + ((step_size * 2) ** 0.5) * noise if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=__A ) def SCREAMING_SNAKE_CASE ( self , __A , __A , __A , ) -> torch.FloatTensor: # Make sure sigmas and timesteps have the same device and dtype as original_samples a =timesteps.to(original_samples.device ) a =self.discrete_sigmas.to(original_samples.device )[timesteps] a =( noise * sigmas[:, None, None, None] if noise is not None else torch.randn_like(__A ) * sigmas[:, None, None, None] ) a =noise + original_samples return noisy_samples def __len__( self ) -> Optional[int]: return self.config.num_train_timesteps
81
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase_ : Union[str, Any] = { """configuration_table_transformer""": [ """TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TableTransformerConfig""", """TableTransformerOnnxConfig""", ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ : List[Any] = [ """TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """TableTransformerForObjectDetection""", """TableTransformerModel""", """TableTransformerPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TableTransformerConfig, TableTransformerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TableTransformerForObjectDetection, TableTransformerModel, TableTransformerPreTrainedModel, ) else: import sys lowerCamelCase_ : Optional[Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
81
1
import warnings from ...configuration_utils import PretrainedConfig from ...utils import logging __A = logging.get_logger(__name__) __A = { '''RUCAIBox/mvp''': '''https://huggingface.co/RUCAIBox/mvp/resolve/main/config.json''', } class lowercase_ ( __lowercase ): UpperCamelCase_ : List[Any] = "mvp" UpperCamelCase_ : Union[str, Any] = ["past_key_values"] UpperCamelCase_ : Union[str, Any] = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"} def __init__( self : Optional[Any] , A__ : Union[str, Any]=50267 , A__ : Union[str, Any]=1024 , A__ : Optional[Any]=12 , A__ : str=4096 , A__ : Optional[Any]=16 , A__ : Any=12 , A__ : int=4096 , A__ : List[str]=16 , A__ : Dict=0.0 , A__ : Optional[Any]=0.0 , A__ : Tuple="gelu" , A__ : Tuple=1024 , A__ : Optional[Any]=0.1 , A__ : Tuple=0.0 , A__ : str=0.0 , A__ : Optional[Any]=0.02 , A__ : Tuple=0.0 , A__ : Any=False , A__ : int=True , A__ : Tuple=1 , A__ : List[str]=0 , A__ : Union[str, Any]=2 , A__ : List[Any]=True , A__ : Any=2 , A__ : Optional[int]=2 , A__ : Any=False , A__ : str=100 , A__ : Optional[int]=800 , **A__ : Dict , ) -> List[str]: _snake_case = vocab_size _snake_case = max_position_embeddings _snake_case = d_model _snake_case = encoder_ffn_dim _snake_case = encoder_layers _snake_case = encoder_attention_heads _snake_case = decoder_ffn_dim _snake_case = decoder_layers _snake_case = decoder_attention_heads _snake_case = dropout _snake_case = attention_dropout _snake_case = activation_dropout _snake_case = activation_function _snake_case = init_std _snake_case = encoder_layerdrop _snake_case = decoder_layerdrop _snake_case = classifier_dropout _snake_case = use_cache _snake_case = encoder_layers _snake_case = scale_embedding # scale factor will be sqrt(d_model) if True _snake_case = use_prompt _snake_case = prompt_length _snake_case = prompt_mid_dim super().__init__( pad_token_id=A__ , bos_token_id=A__ , eos_token_id=A__ , is_encoder_decoder=A__ , decoder_start_token_id=A__ , forced_eos_token_id=A__ , **A__ , ) if self.forced_bos_token_id is None and kwargs.get('''force_bos_token_to_be_generated''' , A__ ): _snake_case = self.bos_token_id warnings.warn( f"""Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. """ '''The config can simply be saved and uploaded again to be fixed.''' )
278
from math import factorial def snake_case_(_UpperCamelCase , _UpperCamelCase ) -> int: """simple docstring""" if n < k or k < 0: raise ValueError('''Please enter positive integers for n and k where n >= k''' ) return factorial(_UpperCamelCase ) // (factorial(_UpperCamelCase ) * factorial(n - k )) if __name__ == "__main__": print( '''The number of five-card hands possible from a standard''', f'''fifty-two card deck is: {combinations(52, 5)}\n''', ) print( '''If a class of 40 students must be arranged into groups of''', f'''4 for group projects, there are {combinations(40, 4)} ways''', '''to arrange them.\n''', ) print( '''If 10 teams are competing in a Formula One race, there''', f'''are {combinations(10, 3)} ways that first, second and''', '''third place can be awarded.''', )
278
1
"""simple docstring""" from manim import * class lowerCAmelCase_ ( lowercase_ ): """simple docstring""" def snake_case ( self ): """simple docstring""" snake_case = Rectangle(height=0.5 , width=0.5 ) snake_case = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) snake_case = [mem.copy() for i in range(6 )] snake_case = [mem.copy() for i in range(6 )] snake_case = VGroup(*__UpperCamelCase ).arrange(__UpperCamelCase , buff=0 ) snake_case = VGroup(*__UpperCamelCase ).arrange(__UpperCamelCase , buff=0 ) snake_case = VGroup(__UpperCamelCase , __UpperCamelCase ).arrange(__UpperCamelCase , buff=0 ) snake_case = Text('CPU' , font_size=24 ) snake_case = Group(__UpperCamelCase , __UpperCamelCase ).arrange(__UpperCamelCase , buff=0.5 , aligned_edge=__UpperCamelCase ) cpu.move_to([-2.5, -0.5, 0] ) self.add(__UpperCamelCase ) snake_case = [mem.copy() for i in range(1 )] snake_case = VGroup(*__UpperCamelCase ).arrange(__UpperCamelCase , buff=0 ) snake_case = Text('GPU' , font_size=24 ) snake_case = Group(__UpperCamelCase , __UpperCamelCase ).arrange(__UpperCamelCase , buff=0.5 , aligned_edge=__UpperCamelCase ) gpu.align_to(__UpperCamelCase , __UpperCamelCase ) gpu.set_x(gpu.get_x() - 1 ) self.add(__UpperCamelCase ) snake_case = [mem.copy() for i in range(6 )] snake_case = VGroup(*__UpperCamelCase ).arrange(__UpperCamelCase , buff=0 ) snake_case = Text('Model' , font_size=24 ) snake_case = Group(__UpperCamelCase , __UpperCamelCase ).arrange(__UpperCamelCase , buff=0.5 , aligned_edge=__UpperCamelCase ) model.move_to([3, -1.0, 0] ) self.play( Create(__UpperCamelCase , run_time=1 ) , Create(__UpperCamelCase , run_time=1 ) , Create(__UpperCamelCase , run_time=1 ) , ) snake_case = MarkupText( F"""First, an empty model skeleton is loaded\ninto <span fgcolor=\'{YELLOW}\'>memory</span> without using much RAM.""" , font_size=24 , ) snake_case = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) snake_case = MarkupText( F"""<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model""" , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) step_a.move_to([2, 2, 0] ) self.play(Write(__UpperCamelCase , run_time=2.5 ) , Write(__UpperCamelCase ) , Write(__UpperCamelCase ) ) self.add(__UpperCamelCase ) snake_case = [] snake_case = [] snake_case = [] for i, rect in enumerate(__UpperCamelCase ): snake_case = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0.0 ).set_fill(__UpperCamelCase , opacity=0.7 ) cpu_target.move_to(__UpperCamelCase ) cpu_target.generate_target() snake_case = 0.46 / 4 snake_case = 0.46 / 3 if i == 0: cpu_target.target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.02 , direction=__UpperCamelCase ) cpu_target.target.set_x(cpu_target.target.get_x() + 0.1 ) elif i == 3: cpu_target.target.next_to(cpu_targs[0].target , direction=__UpperCamelCase , buff=0.0 ) else: cpu_target.target.next_to(cpu_targs[i - 1].target , direction=__UpperCamelCase , buff=0.0 ) cpu_targs.append(__UpperCamelCase ) first_animations.append(rect.animate(run_time=0.5 ).set_stroke(__UpperCamelCase ) ) second_animations.append(MoveToTarget(__UpperCamelCase , run_time=1.5 ) ) self.play(*__UpperCamelCase ) self.play(*__UpperCamelCase ) self.wait()
150
"""simple docstring""" from collections import Counter from pathlib import Path from typing import Optional, Tuple import yaml class UpperCamelCase ( yaml.SafeLoader ): def _UpperCAmelCase ( self ,__UpperCamelCase ) -> Optional[int]: '''simple docstring''' lowercase_ : str = [self.constructed_objects[key_node] for key_node, _ in node.value] lowercase_ : str = [tuple(__UpperCamelCase ) if isinstance(__UpperCamelCase ,__UpperCamelCase ) else key for key in keys] lowercase_ : List[Any] = Counter(__UpperCamelCase ) lowercase_ : str = [key for key in counter if counter[key] > 1] if duplicate_keys: raise TypeError(f'''Got duplicate yaml keys: {duplicate_keys}''' ) def _UpperCAmelCase ( self ,__UpperCamelCase ,__UpperCamelCase=False ) -> List[Any]: '''simple docstring''' lowercase_ : Optional[int] = super().construct_mapping(__UpperCamelCase ,deep=__UpperCamelCase ) self._check_no_duplicates_on_constructed_node(__UpperCamelCase ) return mapping def lowercase__( __SCREAMING_SNAKE_CASE : str ): lowercase_ : Tuple = list(readme_content.splitlines() ) if full_content and full_content[0] == "---" and "---" in full_content[1:]: lowercase_ : Dict = full_content[1:].index('---' ) + 1 lowercase_ : Optional[int] = '\n'.join(full_content[1:sep_idx] ) return yamlblock, "\n".join(full_content[sep_idx + 1 :] ) return None, "\n".join(__SCREAMING_SNAKE_CASE ) class UpperCamelCase ( lowercase_ ): # class attributes lowercase = {'train_eval_index'} # train-eval-index in the YAML metadata @classmethod def _UpperCAmelCase ( cls ,__UpperCamelCase ) -> "DatasetMetadata": '''simple docstring''' with open(__UpperCamelCase ,encoding='utf-8' ) as readme_file: lowercase_ , lowercase_ : Optional[int] = _split_yaml_from_readme(readme_file.read() ) if yaml_string is not None: return cls.from_yaml_string(__UpperCamelCase ) else: return cls() def _UpperCAmelCase ( self ,__UpperCamelCase ) -> List[Any]: '''simple docstring''' if path.exists(): with open(__UpperCamelCase ,encoding='utf-8' ) as readme_file: lowercase_ : Dict = readme_file.read() else: lowercase_ : int = None lowercase_ : Any = self._to_readme(__UpperCamelCase ) with open(__UpperCamelCase ,'w' ,encoding='utf-8' ) as readme_file: readme_file.write(__UpperCamelCase ) def _UpperCAmelCase ( self ,__UpperCamelCase = None ) -> str: '''simple docstring''' if readme_content is not None: lowercase_ , lowercase_ : Optional[Any] = _split_yaml_from_readme(__UpperCamelCase ) lowercase_ : Optional[Any] = '---\n' + self.to_yaml_string() + '---\n' + content else: lowercase_ : Tuple = '---\n' + self.to_yaml_string() + '---\n' return full_content @classmethod def _UpperCAmelCase ( cls ,__UpperCamelCase ) -> "DatasetMetadata": '''simple docstring''' lowercase_ : List[str] = yaml.load(__UpperCamelCase ,Loader=_NoDuplicateSafeLoader ) or {} # Convert the YAML keys to DatasetMetadata fields lowercase_ : Dict = { (key.replace('-' ,'_' ) if key.replace('-' ,'_' ) in cls._FIELDS_WITH_DASHES else key): value for key, value in metadata_dict.items() } return cls(**__UpperCamelCase ) def _UpperCAmelCase ( self ) -> str: '''simple docstring''' return yaml.safe_dump( { (key.replace('_' ,'-' ) if key in self._FIELDS_WITH_DASHES else key): value for key, value in self.items() } ,sort_keys=__UpperCamelCase ,allow_unicode=__UpperCamelCase ,encoding='utf-8' ,).decode('utf-8' ) __SCREAMING_SNAKE_CASE ={ "image-classification": [], "translation": [], "image-segmentation": [], "fill-mask": [], "automatic-speech-recognition": [], "token-classification": [], "sentence-similarity": [], "audio-classification": [], "question-answering": [], "summarization": [], "zero-shot-classification": [], "table-to-text": [], "feature-extraction": [], "other": [], "multiple-choice": [], "text-classification": [], "text-to-image": [], "text2text-generation": [], "zero-shot-image-classification": [], "tabular-classification": [], "tabular-regression": [], "image-to-image": [], "tabular-to-text": [], "unconditional-image-generation": [], "text-retrieval": [], "text-to-speech": [], "object-detection": [], "audio-to-audio": [], "text-generation": [], "conversational": [], "table-question-answering": [], "visual-question-answering": [], "image-to-text": [], "reinforcement-learning": [], "voice-activity-detection": [], "time-series-forecasting": [], "document-question-answering": [], } if __name__ == "__main__": from argparse import ArgumentParser __SCREAMING_SNAKE_CASE =ArgumentParser(usage="Validate the yaml metadata block of a README.md file.") ap.add_argument("readme_filepath") __SCREAMING_SNAKE_CASE =ap.parse_args() __SCREAMING_SNAKE_CASE =Path(args.readme_filepath) __SCREAMING_SNAKE_CASE =DatasetMetadata.from_readme(readme_filepath) print(dataset_metadata) dataset_metadata.to_readme(readme_filepath)
213
0
'''simple docstring''' from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax lowerCamelCase = logging.get_logger(__name__) @add_end_docstrings(A ) class _UpperCamelCase ( A ): '''simple docstring''' def __init__( self : int , **_lowerCAmelCase : List[Any]): '''simple docstring''' super().__init__(**_lowerCAmelCase) requires_backends(self , 'vision') self.check_model_type( TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if self.framework == 'tf' else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING) def __call__( self : List[str] , _lowerCAmelCase : Union[str, List[str], "Image", List["Image"]] , **_lowerCAmelCase : Any): '''simple docstring''' return super().__call__(_lowerCAmelCase , **_lowerCAmelCase) def __lowerCamelCase ( self : List[str] , **_lowerCAmelCase : List[str]): '''simple docstring''' __lowercase ={} if "candidate_labels" in kwargs: __lowercase =kwargs['candidate_labels'] if "hypothesis_template" in kwargs: __lowercase =kwargs['hypothesis_template'] return preprocess_params, {}, {} def __lowerCamelCase ( self : Any , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : str=None , _lowerCAmelCase : Optional[Any]="This is a photo of {}."): '''simple docstring''' __lowercase =load_image(_lowerCAmelCase) __lowercase =self.image_processor(images=[image] , return_tensors=self.framework) __lowercase =candidate_labels __lowercase =[hypothesis_template.format(_lowerCAmelCase) for x in candidate_labels] __lowercase =self.tokenizer(_lowerCAmelCase , return_tensors=self.framework , padding=_lowerCAmelCase) __lowercase =[text_inputs] return inputs def __lowerCamelCase ( self : List[str] , _lowerCAmelCase : str): '''simple docstring''' __lowercase =model_inputs.pop('candidate_labels') __lowercase =model_inputs.pop('text_inputs') if isinstance(text_inputs[0] , _lowerCAmelCase): __lowercase =text_inputs[0] else: # Batching case. __lowercase =text_inputs[0][0] __lowercase =self.model(**_lowerCAmelCase , **_lowerCAmelCase) __lowercase ={ 'candidate_labels': candidate_labels, 'logits': outputs.logits_per_image, } return model_outputs def __lowerCamelCase ( self : List[str] , _lowerCAmelCase : Optional[int]): '''simple docstring''' __lowercase =model_outputs.pop('candidate_labels') __lowercase =model_outputs['logits'][0] if self.framework == "pt": __lowercase =logits.softmax(dim=-1).squeeze(-1) __lowercase =probs.tolist() if not isinstance(_lowerCAmelCase , _lowerCAmelCase): __lowercase =[scores] elif self.framework == "tf": __lowercase =stable_softmax(_lowerCAmelCase , axis=-1) __lowercase =probs.numpy().tolist() else: raise ValueError(f"""Unsupported framework: {self.framework}""") __lowercase =[ {'score': score, 'label': candidate_label} for score, candidate_label in sorted(zip(_lowerCAmelCase , _lowerCAmelCase) , key=lambda _lowerCAmelCase: -x[0]) ] return result
48
'''simple docstring''' from __future__ import annotations import math import random from typing import Any class _UpperCamelCase : '''simple docstring''' def __init__( self : List[Any]): '''simple docstring''' __lowercase =[] __lowercase =0 __lowercase =0 def __lowerCamelCase ( self : List[Any]): '''simple docstring''' return self.head == self.tail def __lowerCamelCase ( self : str , _lowerCAmelCase : Any): '''simple docstring''' self.data.append(_lowerCAmelCase) __lowercase =self.tail + 1 def __lowerCamelCase ( self : Dict): '''simple docstring''' __lowercase =self.data[self.head] __lowercase =self.head + 1 return ret def __lowerCamelCase ( self : Optional[Any]): '''simple docstring''' return self.tail - self.head def __lowerCamelCase ( self : Optional[int]): '''simple docstring''' print(self.data) print('**************') print(self.data[self.head : self.tail]) class _UpperCamelCase : '''simple docstring''' def __init__( self : Optional[int] , _lowerCAmelCase : Any): '''simple docstring''' __lowercase =data __lowercase =None __lowercase =None __lowercase =1 def __lowerCamelCase ( self : Any): '''simple docstring''' return self.data def __lowerCamelCase ( self : Optional[int]): '''simple docstring''' return self.left def __lowerCamelCase ( self : Tuple): '''simple docstring''' return self.right def __lowerCamelCase ( self : Dict): '''simple docstring''' return self.height def __lowerCamelCase ( self : int , _lowerCAmelCase : Any): '''simple docstring''' __lowercase =data def __lowerCamelCase ( self : Optional[Any] , _lowerCAmelCase : MyNode | None): '''simple docstring''' __lowercase =node def __lowerCamelCase ( self : List[Any] , _lowerCAmelCase : MyNode | None): '''simple docstring''' __lowercase =node def __lowerCamelCase ( self : Optional[int] , _lowerCAmelCase : int): '''simple docstring''' __lowercase =height def _A ( _lowerCAmelCase ): """simple docstring""" if node is None: return 0 return node.get_height() def _A ( _lowerCAmelCase , _lowerCAmelCase ): """simple docstring""" if a > b: return a return b def _A ( _lowerCAmelCase ): """simple docstring""" print('left rotation node:' , node.get_data() ) __lowercase =node.get_left() assert ret is not None node.set_left(ret.get_right() ) ret.set_right(_lowerCAmelCase ) __lowercase =my_max(get_height(node.get_right() ) , get_height(node.get_left() ) ) + 1 node.set_height(_lowerCAmelCase ) __lowercase =my_max(get_height(ret.get_right() ) , get_height(ret.get_left() ) ) + 1 ret.set_height(_lowerCAmelCase ) return ret def _A ( _lowerCAmelCase ): """simple docstring""" print('right rotation node:' , node.get_data() ) __lowercase =node.get_right() assert ret is not None node.set_right(ret.get_left() ) ret.set_left(_lowerCAmelCase ) __lowercase =my_max(get_height(node.get_right() ) , get_height(node.get_left() ) ) + 1 node.set_height(_lowerCAmelCase ) __lowercase =my_max(get_height(ret.get_right() ) , get_height(ret.get_left() ) ) + 1 ret.set_height(_lowerCAmelCase ) return ret def _A ( _lowerCAmelCase ): """simple docstring""" __lowercase =node.get_left() assert left_child is not None node.set_left(left_rotation(_lowerCAmelCase ) ) return right_rotation(_lowerCAmelCase ) def _A ( _lowerCAmelCase ): """simple docstring""" __lowercase =node.get_right() assert right_child is not None node.set_right(right_rotation(_lowerCAmelCase ) ) return left_rotation(_lowerCAmelCase ) def _A ( _lowerCAmelCase , _lowerCAmelCase ): """simple docstring""" if node is None: return MyNode(_lowerCAmelCase ) if data < node.get_data(): node.set_left(insert_node(node.get_left() , _lowerCAmelCase ) ) if ( get_height(node.get_left() ) - get_height(node.get_right() ) == 2 ): # an unbalance detected __lowercase =node.get_left() assert left_child is not None if ( data < left_child.get_data() ): # new node is the left child of the left child __lowercase =right_rotation(_lowerCAmelCase ) else: __lowercase =lr_rotation(_lowerCAmelCase ) else: node.set_right(insert_node(node.get_right() , _lowerCAmelCase ) ) if get_height(node.get_right() ) - get_height(node.get_left() ) == 2: __lowercase =node.get_right() assert right_child is not None if data < right_child.get_data(): __lowercase =rl_rotation(_lowerCAmelCase ) else: __lowercase =left_rotation(_lowerCAmelCase ) __lowercase =my_max(get_height(node.get_right() ) , get_height(node.get_left() ) ) + 1 node.set_height(_lowerCAmelCase ) return node def _A ( _lowerCAmelCase ): """simple docstring""" while True: __lowercase =root.get_right() if right_child is None: break __lowercase =right_child return root.get_data() def _A ( _lowerCAmelCase ): """simple docstring""" while True: __lowercase =root.get_left() if left_child is None: break __lowercase =left_child return root.get_data() def _A ( _lowerCAmelCase , _lowerCAmelCase ): """simple docstring""" __lowercase =root.get_left() __lowercase =root.get_right() if root.get_data() == data: if left_child is not None and right_child is not None: __lowercase =get_left_most(_lowerCAmelCase ) root.set_data(_lowerCAmelCase ) root.set_right(del_node(_lowerCAmelCase , _lowerCAmelCase ) ) elif left_child is not None: __lowercase =left_child elif right_child is not None: __lowercase =right_child else: return None elif root.get_data() > data: if left_child is None: print('No such data' ) return root else: root.set_left(del_node(_lowerCAmelCase , _lowerCAmelCase ) ) else: # root.get_data() < data if right_child is None: return root else: root.set_right(del_node(_lowerCAmelCase , _lowerCAmelCase ) ) if get_height(_lowerCAmelCase ) - get_height(_lowerCAmelCase ) == 2: assert right_child is not None if get_height(right_child.get_right() ) > get_height(right_child.get_left() ): __lowercase =left_rotation(_lowerCAmelCase ) else: __lowercase =rl_rotation(_lowerCAmelCase ) elif get_height(_lowerCAmelCase ) - get_height(_lowerCAmelCase ) == -2: assert left_child is not None if get_height(left_child.get_left() ) > get_height(left_child.get_right() ): __lowercase =right_rotation(_lowerCAmelCase ) else: __lowercase =lr_rotation(_lowerCAmelCase ) __lowercase =my_max(get_height(root.get_right() ) , get_height(root.get_left() ) ) + 1 root.set_height(_lowerCAmelCase ) return root class _UpperCamelCase : '''simple docstring''' def __init__( self : Tuple): '''simple docstring''' __lowercase =None def __lowerCamelCase ( self : Optional[int]): '''simple docstring''' return get_height(self.root) def __lowerCamelCase ( self : List[str] , _lowerCAmelCase : Any): '''simple docstring''' print('insert:' + str(_lowerCAmelCase)) __lowercase =insert_node(self.root , _lowerCAmelCase) def __lowerCamelCase ( self : List[Any] , _lowerCAmelCase : Any): '''simple docstring''' print('delete:' + str(_lowerCAmelCase)) if self.root is None: print('Tree is empty!') return __lowercase =del_node(self.root , _lowerCAmelCase) def __str__( self : int , ): # a level traversale, gives a more intuitive look on the tree '''simple docstring''' __lowercase ='' __lowercase =MyQueue() q.push(self.root) __lowercase =self.get_height() if layer == 0: return output __lowercase =0 while not q.is_empty(): __lowercase =q.pop() __lowercase =' ' * int(math.pow(2 , layer - 1)) output += space if node is None: output += "*" q.push(_lowerCAmelCase) q.push(_lowerCAmelCase) else: output += str(node.get_data()) q.push(node.get_left()) q.push(node.get_right()) output += space __lowercase =cnt + 1 for i in range(1_0_0): if cnt == math.pow(2 , _lowerCAmelCase) - 1: __lowercase =layer - 1 if layer == 0: output += "\n*************************************" return output output += "\n" break output += "\n*************************************" return output def _A ( ): """simple docstring""" import doctest doctest.testmod() if __name__ == "__main__": _test() lowerCamelCase = AVLtree() lowerCamelCase = list(range(10)) random.shuffle(lst) for i in lst: t.insert(i) print(str(t)) random.shuffle(lst) for i in lst: t.del_node(i) print(str(t))
48
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) _a = { "configuration_clip": [ "CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "CLIPConfig", "CLIPOnnxConfig", "CLIPTextConfig", "CLIPVisionConfig", ], "processing_clip": ["CLIPProcessor"], "tokenization_clip": ["CLIPTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a = ["CLIPTokenizerFast"] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a = ["CLIPFeatureExtractor"] _a = ["CLIPImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a = [ "CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "CLIPModel", "CLIPPreTrainedModel", "CLIPTextModel", "CLIPTextModelWithProjection", "CLIPVisionModel", "CLIPVisionModelWithProjection", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a = [ "TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "TFCLIPModel", "TFCLIPPreTrainedModel", "TFCLIPTextModel", "TFCLIPVisionModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a = [ "FlaxCLIPModel", "FlaxCLIPPreTrainedModel", "FlaxCLIPTextModel", "FlaxCLIPTextPreTrainedModel", "FlaxCLIPVisionModel", "FlaxCLIPVisionPreTrainedModel", ] if TYPE_CHECKING: from .configuration_clip import ( CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPConfig, CLIPOnnxConfig, CLIPTextConfig, CLIPVisionConfig, ) from .processing_clip import CLIPProcessor from .tokenization_clip import CLIPTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_clip_fast import CLIPTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clip import CLIPFeatureExtractor from .image_processing_clip import CLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clip import ( CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPModel, CLIPPreTrainedModel, CLIPTextModel, CLIPTextModelWithProjection, CLIPVisionModel, CLIPVisionModelWithProjection, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_clip import ( TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFCLIPModel, TFCLIPPreTrainedModel, TFCLIPTextModel, TFCLIPVisionModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_clip import ( FlaxCLIPModel, FlaxCLIPPreTrainedModel, FlaxCLIPTextModel, FlaxCLIPTextPreTrainedModel, FlaxCLIPVisionModel, FlaxCLIPVisionPreTrainedModel, ) else: import sys _a = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
209
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from transformers.utils import is_vision_available from transformers.utils.generic import TensorType from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import logging if is_vision_available(): import PIL _A = logging.get_logger(__name__) def lowercase_ ( __UpperCAmelCase ) -> List[List[ImageInput]]: if isinstance(__UpperCAmelCase , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(__UpperCAmelCase , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(__UpperCAmelCase ): return [[videos]] raise ValueError(f"""Could not make batched video from {videos}""" ) class _lowerCamelCase ( a_ ): _lowerCamelCase :Optional[int] = ["pixel_values"] def __init__( self : Dict , UpperCamelCase : bool = True , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase : bool = True , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : bool = True , UpperCamelCase : Union[int, float] = 1 / 2_55 , UpperCamelCase : bool = True , UpperCamelCase : bool = True , UpperCamelCase : Optional[Union[float, List[float]]] = None , UpperCamelCase : Optional[Union[float, List[float]]] = None , **UpperCamelCase : Dict , ) -> None: """simple docstring""" super().__init__(**UpperCamelCase ) lowerCAmelCase__ : int = size if size is not None else {"""shortest_edge""": 2_56} lowerCAmelCase__ : Union[str, Any] = get_size_dict(UpperCamelCase , default_to_square=UpperCamelCase ) lowerCAmelCase__ : Union[str, Any] = crop_size if crop_size is not None else {"""height""": 2_24, """width""": 2_24} lowerCAmelCase__ : List[str] = get_size_dict(UpperCamelCase , param_name="""crop_size""" ) lowerCAmelCase__ : Union[str, Any] = do_resize lowerCAmelCase__ : str = size lowerCAmelCase__ : str = do_center_crop lowerCAmelCase__ : Tuple = crop_size lowerCAmelCase__ : Union[str, Any] = resample lowerCAmelCase__ : Any = do_rescale lowerCAmelCase__ : Union[str, Any] = rescale_factor lowerCAmelCase__ : Dict = offset lowerCAmelCase__ : Optional[int] = do_normalize lowerCAmelCase__ : Optional[int] = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN lowerCAmelCase__ : Tuple = image_std if image_std is not None else IMAGENET_STANDARD_STD def _lowerCAmelCase ( self : List[str] , UpperCamelCase : np.ndarray , UpperCamelCase : Dict[str, int] , UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase : Optional[Any] , ) -> np.ndarray: """simple docstring""" lowerCAmelCase__ : List[Any] = get_size_dict(UpperCamelCase , default_to_square=UpperCamelCase ) if "shortest_edge" in size: lowerCAmelCase__ : int = get_resize_output_image_size(UpperCamelCase , size["""shortest_edge"""] , default_to_square=UpperCamelCase ) elif "height" in size and "width" in size: lowerCAmelCase__ : Union[str, Any] = (size["""height"""], size["""width"""]) else: raise ValueError(f"""Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}""" ) return resize(UpperCamelCase , size=UpperCamelCase , resample=UpperCamelCase , data_format=UpperCamelCase , **UpperCamelCase ) def _lowerCAmelCase ( self : Tuple , UpperCamelCase : np.ndarray , UpperCamelCase : Dict[str, int] , UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase : Optional[int] , ) -> np.ndarray: """simple docstring""" lowerCAmelCase__ : int = get_size_dict(UpperCamelCase ) if "height" not in size or "width" not in size: raise ValueError(f"""Size must have 'height' and 'width' as keys. Got {size.keys()}""" ) return center_crop(UpperCamelCase , size=(size["""height"""], size["""width"""]) , data_format=UpperCamelCase , **UpperCamelCase ) def _lowerCAmelCase ( self : Dict , UpperCamelCase : np.ndarray , UpperCamelCase : Union[int, float] , UpperCamelCase : bool = True , UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase : Union[str, Any] , ) -> List[Any]: """simple docstring""" lowerCAmelCase__ : Union[str, Any] = image.astype(np.floataa ) if offset: lowerCAmelCase__ : Tuple = image - (scale / 2) return rescale(UpperCamelCase , scale=UpperCamelCase , data_format=UpperCamelCase , **UpperCamelCase ) def _lowerCAmelCase ( self : Dict , UpperCamelCase : np.ndarray , UpperCamelCase : Union[float, List[float]] , UpperCamelCase : Union[float, List[float]] , UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase : Optional[int] , ) -> np.ndarray: """simple docstring""" return normalize(UpperCamelCase , mean=UpperCamelCase , std=UpperCamelCase , data_format=UpperCamelCase , **UpperCamelCase ) def _lowerCAmelCase ( self : Optional[int] , UpperCamelCase : ImageInput , UpperCamelCase : bool = None , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : PILImageResampling = None , UpperCamelCase : bool = None , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : bool = None , UpperCamelCase : float = None , UpperCamelCase : bool = None , UpperCamelCase : bool = None , UpperCamelCase : Optional[Union[float, List[float]]] = None , UpperCamelCase : Optional[Union[float, List[float]]] = None , UpperCamelCase : Optional[ChannelDimension] = ChannelDimension.FIRST , ) -> np.ndarray: """simple docstring""" if do_resize and size is None or resample is None: raise ValueError("""Size and resample must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) if offset and not do_rescale: raise ValueError("""For offset, do_rescale must also be set to True.""" ) # All transformations expect numpy arrays. lowerCAmelCase__ : Optional[Any] = to_numpy_array(UpperCamelCase ) if do_resize: lowerCAmelCase__ : List[str] = self.resize(image=UpperCamelCase , size=UpperCamelCase , resample=UpperCamelCase ) if do_center_crop: lowerCAmelCase__ : List[str] = self.center_crop(UpperCamelCase , size=UpperCamelCase ) if do_rescale: lowerCAmelCase__ : Optional[int] = self.rescale(image=UpperCamelCase , scale=UpperCamelCase , offset=UpperCamelCase ) if do_normalize: lowerCAmelCase__ : Tuple = self.normalize(image=UpperCamelCase , mean=UpperCamelCase , std=UpperCamelCase ) lowerCAmelCase__ : List[str] = to_channel_dimension_format(UpperCamelCase , UpperCamelCase ) return image def _lowerCAmelCase ( self : Optional[int] , UpperCamelCase : ImageInput , UpperCamelCase : bool = None , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : PILImageResampling = None , UpperCamelCase : bool = None , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : bool = None , UpperCamelCase : float = None , UpperCamelCase : bool = None , UpperCamelCase : bool = None , UpperCamelCase : Optional[Union[float, List[float]]] = None , UpperCamelCase : Optional[Union[float, List[float]]] = None , UpperCamelCase : Optional[Union[str, TensorType]] = None , UpperCamelCase : ChannelDimension = ChannelDimension.FIRST , **UpperCamelCase : Dict , ) -> PIL.Image.Image: """simple docstring""" lowerCAmelCase__ : List[str] = do_resize if do_resize is not None else self.do_resize lowerCAmelCase__ : Union[str, Any] = resample if resample is not None else self.resample lowerCAmelCase__ : Union[str, Any] = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCAmelCase__ : int = do_rescale if do_rescale is not None else self.do_rescale lowerCAmelCase__ : Dict = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCAmelCase__ : Dict = offset if offset is not None else self.offset lowerCAmelCase__ : str = do_normalize if do_normalize is not None else self.do_normalize lowerCAmelCase__ : List[Any] = image_mean if image_mean is not None else self.image_mean lowerCAmelCase__ : Union[str, Any] = image_std if image_std is not None else self.image_std lowerCAmelCase__ : List[Any] = size if size is not None else self.size lowerCAmelCase__ : Tuple = get_size_dict(UpperCamelCase , default_to_square=UpperCamelCase ) lowerCAmelCase__ : Optional[int] = crop_size if crop_size is not None else self.crop_size lowerCAmelCase__ : Optional[Any] = get_size_dict(UpperCamelCase , param_name="""crop_size""" ) if not valid_images(UpperCamelCase ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) lowerCAmelCase__ : int = make_batched(UpperCamelCase ) lowerCAmelCase__ : str = [ [ self._preprocess_image( image=UpperCamelCase , do_resize=UpperCamelCase , size=UpperCamelCase , resample=UpperCamelCase , do_center_crop=UpperCamelCase , crop_size=UpperCamelCase , do_rescale=UpperCamelCase , rescale_factor=UpperCamelCase , offset=UpperCamelCase , do_normalize=UpperCamelCase , image_mean=UpperCamelCase , image_std=UpperCamelCase , data_format=UpperCamelCase , ) for img in video ] for video in videos ] lowerCAmelCase__ : Dict = {"""pixel_values""": videos} return BatchFeature(data=UpperCamelCase , tensor_type=UpperCamelCase )
242
0
import json import logging import os import re import sys from dataclasses import dataclass, field from typing import Any, Dict, List, Optional, Union import datasets import numpy as np import torch import torchaudio from packaging import version from torch import nn import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaForCTC, WavaVecaProcessor, is_apex_available, set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process if is_apex_available(): from apex import amp if version.parse(version.parse(torch.__version__).base_version) >= version.parse('1.6'): UpperCAmelCase_ = True from torch.cuda.amp import autocast UpperCAmelCase_ = logging.getLogger(__name__) def lowerCamelCase__ ( A__ : str=None , A__ : Tuple=None ): '''simple docstring''' return field(default_factory=lambda: default , metadata=A__ ) @dataclass class lowerCamelCase__: UpperCAmelCase__ : str = field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'}) UpperCAmelCase__ : Optional[str] = field( default=__lowerCamelCase , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) UpperCAmelCase__ : Optional[bool] = field( default=__lowerCamelCase , metadata={'help': 'Whether to freeze the feature extractor layers of the model.'}) UpperCAmelCase__ : Optional[float] = field( default=0.1 , metadata={'help': 'The dropout ratio for the attention probabilities.'}) UpperCAmelCase__ : Optional[float] = field( default=0.1 , metadata={'help': 'The dropout ratio for activations inside the fully connected layer.'}) UpperCAmelCase__ : Optional[float] = field( default=0.1 , metadata={ 'help': 'The dropout probabilitiy for all fully connected layers in the embeddings, encoder, and pooler.' } , ) UpperCAmelCase__ : Optional[float] = field( default=0.1 , metadata={'help': 'The dropout probabilitiy for all 1D convolutional layers in feature extractor.'} , ) UpperCAmelCase__ : Optional[float] = field( default=0.05 , metadata={ 'help': ( 'Propability of each feature vector along the time axis to be chosen as the start of the vector' 'span to be masked. Approximately ``mask_time_prob * sequence_length // mask_time_length`` feature' 'vectors will be masked along the time axis. This is only relevant if ``apply_spec_augment is True``.' ) } , ) UpperCAmelCase__ : Optional[float] = field(default=0.0 , metadata={'help': 'The LayerDrop probability.'}) @dataclass class lowerCamelCase__: UpperCAmelCase__ : Optional[str] = field( default=__lowerCamelCase , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'}) UpperCAmelCase__ : Optional[str] = field( default='train+validation' , metadata={ 'help': 'The name of the training data set split to use (via the datasets library). Defaults to \'train\'' } , ) UpperCAmelCase__ : bool = field( default=__lowerCamelCase , metadata={'help': 'Overwrite the cached preprocessed datasets or not.'}) UpperCAmelCase__ : Optional[int] = field( default=__lowerCamelCase , metadata={'help': 'The number of processes to use for the preprocessing.'} , ) UpperCAmelCase__ : Optional[int] = field( default=__lowerCamelCase , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) UpperCAmelCase__ : Optional[int] = field( default=__lowerCamelCase , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of validation examples to this ' 'value if set.' ) } , ) UpperCAmelCase__ : List[str] = list_field( default=[',', '?', '.', '!', '-', ';', ':', '""', '%', '\'', '"', '�'] , metadata={'help': 'A list of characters to remove from the transcripts.'} , ) @dataclass class lowerCamelCase__: UpperCAmelCase__ : WavaVecaProcessor UpperCAmelCase__ : Union[bool, str] = True UpperCAmelCase__ : Optional[int] = None UpperCAmelCase__ : Optional[int] = None UpperCAmelCase__ : Optional[int] = None UpperCAmelCase__ : Optional[int] = None def __call__( self: Optional[int] , UpperCamelCase_: List[Dict[str, Union[List[int], torch.Tensor]]] ): # split inputs and labels since they have to be of different lenghts and need # different padding methods __lowerCamelCase = [{"""input_values""": feature["""input_values"""]} for feature in features] __lowerCamelCase = [{"""input_ids""": feature["""labels"""]} for feature in features] __lowerCamelCase = self.processor.pad( UpperCamelCase_ , padding=self.padding , max_length=self.max_length , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors="""pt""" , ) __lowerCamelCase = self.processor.pad( labels=UpperCamelCase_ , padding=self.padding , max_length=self.max_length_labels , pad_to_multiple_of=self.pad_to_multiple_of_labels , return_tensors="""pt""" , ) # replace padding with -100 to ignore loss correctly __lowerCamelCase = labels_batch["""input_ids"""].masked_fill(labels_batch.attention_mask.ne(1 ) , -1_00 ) __lowerCamelCase = labels return batch class lowerCamelCase__( __lowerCamelCase): def lowerCAmelCase__ ( self: int , UpperCamelCase_: nn.Module , UpperCamelCase_: Dict[str, Union[torch.Tensor, Any]] ): model.train() __lowerCamelCase = self._prepare_inputs(UpperCamelCase_ ) if self.use_amp: with autocast(): __lowerCamelCase = self.compute_loss(UpperCamelCase_ , UpperCamelCase_ ) else: __lowerCamelCase = self.compute_loss(UpperCamelCase_ , UpperCamelCase_ ) if self.args.n_gpu > 1: if model.module.config.ctc_loss_reduction == "mean": __lowerCamelCase = loss.mean() elif model.module.config.ctc_loss_reduction == "sum": __lowerCamelCase = loss.sum() / (inputs["""labels"""] >= 0).sum() else: raise ValueError(F'{model.config.ctc_loss_reduction} is not valid. Choose one of [\'mean\', \'sum\']' ) if self.args.gradient_accumulation_steps > 1: __lowerCamelCase = loss / self.args.gradient_accumulation_steps if self.use_amp: self.scaler.scale(UpperCamelCase_ ).backward() elif self.use_apex: with amp.scale_loss(UpperCamelCase_ , self.optimizer ) as scaled_loss: scaled_loss.backward() elif self.deepspeed: self.deepspeed.backward(UpperCamelCase_ ) else: loss.backward() return loss.detach() def lowerCamelCase__ ( ): '''simple docstring''' __lowerCamelCase = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(""".json""" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. __lowerCamelCase, __lowerCamelCase, __lowerCamelCase = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: __lowerCamelCase, __lowerCamelCase, __lowerCamelCase = parser.parse_args_into_dataclasses() # Detecting last checkpoint. __lowerCamelCase = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: __lowerCamelCase = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f'Output directory ({training_args.output_dir}) already exists and is not empty. ' """Use --overwrite_output_dir to overcome.""" ) elif last_checkpoint is not None: logger.info( f'Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ' """the `--output_dir` or add `--overwrite_output_dir` to train from scratch.""" ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , handlers=[logging.StreamHandler(sys.stdout )] , ) logger.setLevel(logging.INFO if is_main_process(training_args.local_rank ) else logging.WARN ) # Log on each process the small summary: logger.warning( f'Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}' + f'distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}' ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info("""Training/evaluation parameters %s""" , A__ ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: __lowerCamelCase = datasets.load_dataset( """common_voice""" , data_args.dataset_config_name , split=data_args.train_split_name ) __lowerCamelCase = datasets.load_dataset("""common_voice""" , data_args.dataset_config_name , split="""test""" ) # Create and save tokenizer __lowerCamelCase = f'[{"".join(data_args.chars_to_ignore )}]' def remove_special_characters(A__ : Any ): __lowerCamelCase = re.sub(A__ , """""" , batch["""sentence"""] ).lower() + """ """ return batch __lowerCamelCase = train_dataset.map(A__ , remove_columns=["""sentence"""] ) __lowerCamelCase = eval_dataset.map(A__ , remove_columns=["""sentence"""] ) def extract_all_chars(A__ : Dict ): __lowerCamelCase = """ """.join(batch["""text"""] ) __lowerCamelCase = list(set(A__ ) ) return {"vocab": [vocab], "all_text": [all_text]} __lowerCamelCase = train_dataset.map( A__ , batched=A__ , batch_size=-1 , keep_in_memory=A__ , remove_columns=train_dataset.column_names , ) __lowerCamelCase = train_dataset.map( A__ , batched=A__ , batch_size=-1 , keep_in_memory=A__ , remove_columns=eval_dataset.column_names , ) __lowerCamelCase = list(set(vocab_train["""vocab"""][0] ) | set(vocab_test["""vocab"""][0] ) ) __lowerCamelCase = {v: k for k, v in enumerate(A__ )} __lowerCamelCase = vocab_dict[""" """] del vocab_dict[" "] __lowerCamelCase = len(A__ ) __lowerCamelCase = len(A__ ) with open("""vocab.json""" , """w""" ) as vocab_file: json.dump(A__ , A__ ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. __lowerCamelCase = WavaVecaCTCTokenizer( """vocab.json""" , unk_token="""[UNK]""" , pad_token="""[PAD]""" , word_delimiter_token="""|""" , ) __lowerCamelCase = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0.0 , do_normalize=A__ , return_attention_mask=A__ ) __lowerCamelCase = WavaVecaProcessor(feature_extractor=A__ , tokenizer=A__ ) __lowerCamelCase = WavaVecaForCTC.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , activation_dropout=model_args.activation_dropout , attention_dropout=model_args.attention_dropout , hidden_dropout=model_args.hidden_dropout , feat_proj_dropout=model_args.feat_proj_dropout , mask_time_prob=model_args.mask_time_prob , gradient_checkpointing=training_args.gradient_checkpointing , layerdrop=model_args.layerdrop , ctc_loss_reduction="""mean""" , pad_token_id=processor.tokenizer.pad_token_id , vocab_size=len(processor.tokenizer ) , ) if data_args.max_train_samples is not None: __lowerCamelCase = min(len(A__ ) , data_args.max_train_samples ) __lowerCamelCase = train_dataset.select(range(A__ ) ) if data_args.max_val_samples is not None: __lowerCamelCase = eval_dataset.select(range(data_args.max_val_samples ) ) __lowerCamelCase = torchaudio.transforms.Resample(48000 , 16000 ) # Preprocessing the datasets. # We need to read the aduio files as arrays and tokenize the targets. def speech_file_to_array_fn(A__ : Any ): __lowerCamelCase, __lowerCamelCase = torchaudio.load(batch["""path"""] ) __lowerCamelCase = resampler(A__ ).squeeze().numpy() __lowerCamelCase = 16000 __lowerCamelCase = batch["""text"""] return batch __lowerCamelCase = train_dataset.map( A__ , remove_columns=train_dataset.column_names , num_proc=data_args.preprocessing_num_workers , ) __lowerCamelCase = eval_dataset.map( A__ , remove_columns=eval_dataset.column_names , num_proc=data_args.preprocessing_num_workers , ) def prepare_dataset(A__ : Dict ): # check that all files have the correct sampling rate assert ( len(set(batch["""sampling_rate"""] ) ) == 1 ), f'Make sure all inputs have the same sampling rate of {processor.feature_extractor.sampling_rate}.' __lowerCamelCase = processor( audio=batch["""speech"""] , text=batch["""target_text"""] , sampling_rate=batch["""sampling_rate"""][0] ) batch.update(A__ ) return batch __lowerCamelCase = train_dataset.map( A__ , remove_columns=train_dataset.column_names , batch_size=training_args.per_device_train_batch_size , batched=A__ , num_proc=data_args.preprocessing_num_workers , ) __lowerCamelCase = eval_dataset.map( A__ , remove_columns=eval_dataset.column_names , batch_size=training_args.per_device_train_batch_size , batched=A__ , num_proc=data_args.preprocessing_num_workers , ) # Metric __lowerCamelCase = datasets.load_metric("""wer""" ) def compute_metrics(A__ : Optional[int] ): __lowerCamelCase = pred.predictions __lowerCamelCase = np.argmax(A__ , axis=-1 ) __lowerCamelCase = processor.tokenizer.pad_token_id __lowerCamelCase = processor.batch_decode(A__ ) # we do not want to group tokens when computing the metrics __lowerCamelCase = processor.batch_decode(pred.label_ids , group_tokens=A__ ) __lowerCamelCase = wer_metric.compute(predictions=A__ , references=A__ ) return {"wer": wer} if model_args.freeze_feature_extractor: model.freeze_feature_extractor() # Data collator __lowerCamelCase = DataCollatorCTCWithPadding(processor=A__ , padding=A__ ) # Initialize our Trainer __lowerCamelCase = CTCTrainer( model=A__ , data_collator=A__ , args=A__ , compute_metrics=A__ , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , tokenizer=processor.feature_extractor , ) # Training if training_args.do_train: if last_checkpoint is not None: __lowerCamelCase = last_checkpoint elif os.path.isdir(model_args.model_name_or_path ): __lowerCamelCase = model_args.model_name_or_path else: __lowerCamelCase = None # Save the feature_extractor and the tokenizer if is_main_process(training_args.local_rank ): processor.save_pretrained(training_args.output_dir ) __lowerCamelCase = trainer.train(resume_from_checkpoint=A__ ) trainer.save_model() __lowerCamelCase = train_result.metrics __lowerCamelCase = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(A__ ) ) __lowerCamelCase = min(A__ , len(A__ ) ) trainer.log_metrics("""train""" , A__ ) trainer.save_metrics("""train""" , A__ ) trainer.save_state() # Evaluation __lowerCamelCase = {} if training_args.do_eval: logger.info("""*** Evaluate ***""" ) __lowerCamelCase = trainer.evaluate() __lowerCamelCase = data_args.max_val_samples if data_args.max_val_samples is not None else len(A__ ) __lowerCamelCase = min(A__ , len(A__ ) ) trainer.log_metrics("""eval""" , A__ ) trainer.save_metrics("""eval""" , A__ ) return results if __name__ == "__main__": main()
29
def lowerCamelCase__ ( A__ : list ): '''simple docstring''' for i in range(len(A__ ) - 1 , 0 , -1 ): __lowerCamelCase = False for j in range(A__ , 0 , -1 ): if unsorted[j] < unsorted[j - 1]: __lowerCamelCase, __lowerCamelCase = unsorted[j - 1], unsorted[j] __lowerCamelCase = True for j in range(A__ ): if unsorted[j] > unsorted[j + 1]: __lowerCamelCase, __lowerCamelCase = unsorted[j + 1], unsorted[j] __lowerCamelCase = True if not swapped: break return unsorted if __name__ == "__main__": import doctest doctest.testmod() UpperCAmelCase_ = input('Enter numbers separated by a comma:\n').strip() UpperCAmelCase_ = [int(item) for item in user_input.split(',')] print(f"""{cocktail_shaker_sort(unsorted) = }""")
29
1
"""simple docstring""" import json import os import unittest from transformers import BatchEncoding, MvpTokenizer, MvpTokenizerFast from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, require_torch from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin, filter_roberta_detectors @require_tokenizers class snake_case ( __UpperCAmelCase , unittest.TestCase ): """simple docstring""" snake_case__ = MvpTokenizer snake_case__ = MvpTokenizerFast snake_case__ = True snake_case__ = filter_roberta_detectors def __lowerCAmelCase ( self : Dict ): super().setUp() UpperCAmelCase__ = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] UpperCAmelCase__ = dict(zip(lowerCamelCase__ ,range(len(lowerCamelCase__ ) ) ) ) UpperCAmelCase__ = ['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] UpperCAmelCase__ = {'unk_token': '<unk>'} UpperCAmelCase__ = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['vocab_file'] ) UpperCAmelCase__ = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file ,'w' ,encoding='utf-8' ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + '\n' ) with open(self.merges_file ,'w' ,encoding='utf-8' ) as fp: fp.write('\n'.join(lowerCamelCase__ ) ) def __lowerCAmelCase ( self : Union[str, Any] ,**lowerCamelCase__ : List[Any] ): kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname ,**lowerCamelCase__ ) def __lowerCAmelCase ( self : str ,**lowerCamelCase__ : Optional[Any] ): kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname ,**lowerCamelCase__ ) def __lowerCAmelCase ( self : Dict ,lowerCamelCase__ : List[Any] ): return "lower newer", "lower newer" @cached_property def __lowerCAmelCase ( self : Optional[int] ): return MvpTokenizer.from_pretrained('RUCAIBox/mvp' ) @cached_property def __lowerCAmelCase ( self : Dict ): return MvpTokenizerFast.from_pretrained('RUCAIBox/mvp' ) @require_torch def __lowerCAmelCase ( self : Union[str, Any] ): UpperCAmelCase__ = ['A long paragraph for summarization.', 'Another paragraph for summarization.'] UpperCAmelCase__ = [0, 250, 251, 17_818, 13, 39_186, 1_938, 4, 2] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase__ = tokenizer(lowerCamelCase__ ,max_length=len(lowerCamelCase__ ) ,padding=lowerCamelCase__ ,return_tensors='pt' ) self.assertIsInstance(lowerCamelCase__ ,lowerCamelCase__ ) self.assertEqual((2, 9) ,batch.input_ids.shape ) self.assertEqual((2, 9) ,batch.attention_mask.shape ) UpperCAmelCase__ = batch.input_ids.tolist()[0] self.assertListEqual(lowerCamelCase__ ,lowerCamelCase__ ) # Test that special tokens are reset @require_torch def __lowerCAmelCase ( self : Tuple ): UpperCAmelCase__ = ['A long paragraph for summarization.', 'Another paragraph for summarization.'] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase__ = tokenizer(lowerCamelCase__ ,padding=lowerCamelCase__ ,return_tensors='pt' ) # check if input_ids are returned and no labels self.assertIn('input_ids' ,lowerCamelCase__ ) self.assertIn('attention_mask' ,lowerCamelCase__ ) self.assertNotIn('labels' ,lowerCamelCase__ ) self.assertNotIn('decoder_attention_mask' ,lowerCamelCase__ ) @require_torch def __lowerCAmelCase ( self : Union[str, Any] ): UpperCAmelCase__ = [ 'Summary of the text.', 'Another summary.', ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase__ = tokenizer(text_target=lowerCamelCase__ ,max_length=32 ,padding='max_length' ,return_tensors='pt' ) self.assertEqual(32 ,targets['input_ids'].shape[1] ) @require_torch def __lowerCAmelCase ( self : List[str] ): for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase__ = tokenizer( ['I am a small frog' * 1_024, 'I am a small frog'] ,padding=lowerCamelCase__ ,truncation=lowerCamelCase__ ,return_tensors='pt' ) self.assertIsInstance(lowerCamelCase__ ,lowerCamelCase__ ) self.assertEqual(batch.input_ids.shape ,(2, 1_024) ) @require_torch def __lowerCAmelCase ( self : Dict ): UpperCAmelCase__ = ['A long paragraph for summarization.'] UpperCAmelCase__ = [ 'Summary of the text.', ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase__ = tokenizer(lowerCamelCase__ ,text_target=lowerCamelCase__ ,return_tensors='pt' ) UpperCAmelCase__ = inputs['input_ids'] UpperCAmelCase__ = inputs['labels'] self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item() ) self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item() ) def __lowerCAmelCase ( self : Any ): pass def __lowerCAmelCase ( self : Tuple ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): UpperCAmelCase__ = self.rust_tokenizer_class.from_pretrained(lowerCamelCase__ ,**lowerCamelCase__ ) UpperCAmelCase__ = self.tokenizer_class.from_pretrained(lowerCamelCase__ ,**lowerCamelCase__ ) UpperCAmelCase__ = 'A, <mask> AllenNLP sentence.' UpperCAmelCase__ = tokenizer_r.encode_plus(lowerCamelCase__ ,add_special_tokens=lowerCamelCase__ ,return_token_type_ids=lowerCamelCase__ ) UpperCAmelCase__ = tokenizer_p.encode_plus(lowerCamelCase__ ,add_special_tokens=lowerCamelCase__ ,return_token_type_ids=lowerCamelCase__ ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['token_type_ids'] ) ,sum(tokens_p['token_type_ids'] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['attention_mask'] ) / len(tokens_r['attention_mask'] ) ,sum(tokens_p['attention_mask'] ) / len(tokens_p['attention_mask'] ) ,) UpperCAmelCase__ = tokenizer_r.convert_ids_to_tokens(tokens_r['input_ids'] ) UpperCAmelCase__ = tokenizer_p.convert_ids_to_tokens(tokens_p['input_ids'] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['input_ids'] ,[0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2] ) self.assertSequenceEqual(tokens_r['input_ids'] ,[0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2] ) self.assertSequenceEqual( lowerCamelCase__ ,['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) self.assertSequenceEqual( lowerCamelCase__ ,['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] )
98
"""simple docstring""" import argparse import logging import sys from unittest.mock import patch import run_glue_deebert from transformers.testing_utils import TestCasePlus, get_gpu_count, require_torch_non_multi_gpu, slow logging.basicConfig(level=logging.DEBUG) lowerCAmelCase__ : Optional[Any] = logging.getLogger() def a_ ( ): UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument('-f' ) UpperCAmelCase__ = parser.parse_args() return args.f class snake_case ( __UpperCAmelCase ): """simple docstring""" def __lowerCAmelCase ( self : int ): UpperCAmelCase__ = logging.StreamHandler(sys.stdout ) logger.addHandler(lowerCamelCase__ ) def __lowerCAmelCase ( self : Optional[Any] ,lowerCamelCase__ : Union[str, Any] ): UpperCAmelCase__ = get_gpu_count() if n_gpu > 1: pass # XXX: doesn't quite work with n_gpu > 1 https://github.com/huggingface/transformers/issues/10560 # script = f"{self.examples_dir_str}/research_projects/deebert/run_glue_deebert.py" # distributed_args = f"-m torch.distributed.launch --nproc_per_node={n_gpu} {script}".split() # cmd = [sys.executable] + distributed_args + args # execute_subprocess_async(cmd, env=self.get_env()) # XXX: test the results - need to save them first into .json file else: args.insert(0 ,'run_glue_deebert.py' ) with patch.object(lowerCamelCase__ ,'argv' ,lowerCamelCase__ ): UpperCAmelCase__ = run_glue_deebert.main() for value in result.values(): self.assertGreaterEqual(lowerCamelCase__ ,0.6_6_6 ) @slow @require_torch_non_multi_gpu def __lowerCAmelCase ( self : List[str] ): UpperCAmelCase__ = '\n --model_type roberta\n --model_name_or_path roberta-base\n --task_name MRPC\n --do_train\n --do_eval\n --do_lower_case\n --data_dir ./tests/fixtures/tests_samples/MRPC/\n --max_seq_length 128\n --per_gpu_eval_batch_size=1\n --per_gpu_train_batch_size=8\n --learning_rate 2e-4\n --num_train_epochs 3\n --overwrite_output_dir\n --seed 42\n --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --plot_data_dir ./examples/deebert/results/\n --save_steps 0\n --overwrite_cache\n --eval_after_first_stage\n '.split() self.run_and_check(lowerCamelCase__ ) UpperCAmelCase__ = '\n --model_type roberta\n --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --task_name MRPC\n --do_eval\n --do_lower_case\n --data_dir ./tests/fixtures/tests_samples/MRPC/\n --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --plot_data_dir ./examples/deebert/results/\n --max_seq_length 128\n --eval_each_highway\n --eval_highway\n --overwrite_cache\n --per_gpu_eval_batch_size=1\n '.split() self.run_and_check(lowerCamelCase__ ) UpperCAmelCase__ = '\n --model_type roberta\n --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --task_name MRPC\n --do_eval\n --do_lower_case\n --data_dir ./tests/fixtures/tests_samples/MRPC/\n --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --plot_data_dir ./examples/deebert/results/\n --max_seq_length 128\n --early_exit_entropy 0.1\n --eval_highway\n --overwrite_cache\n --per_gpu_eval_batch_size=1\n '.split() self.run_and_check(lowerCamelCase__ )
98
1
"""simple docstring""" import unittest from transformers import DebertaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device 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 ( DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, ) from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST class A_ (_SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , lowercase_ , lowercase_=13 , lowercase_=7 , lowercase_=True , lowercase_=True , lowercase_=True , lowercase_=True , lowercase_=99 , lowercase_=32 , lowercase_=5 , lowercase_=4 , lowercase_=37 , lowercase_="gelu" , lowercase_=0.1 , lowercase_=0.1 , lowercase_=512 , lowercase_=16 , lowercase_=2 , lowercase_=0.02 , lowercase_=False , lowercase_=True , lowercase_="None" , lowercase_=3 , lowercase_=4 , lowercase_=None , ): """simple docstring""" UpperCAmelCase_ : int = parent UpperCAmelCase_ : Optional[int] = batch_size UpperCAmelCase_ : Optional[int] = seq_length UpperCAmelCase_ : List[str] = is_training UpperCAmelCase_ : Any = use_input_mask UpperCAmelCase_ : List[Any] = use_token_type_ids UpperCAmelCase_ : List[Any] = use_labels UpperCAmelCase_ : List[Any] = vocab_size UpperCAmelCase_ : List[Any] = hidden_size UpperCAmelCase_ : Any = num_hidden_layers UpperCAmelCase_ : Optional[Any] = num_attention_heads UpperCAmelCase_ : Tuple = intermediate_size UpperCAmelCase_ : Tuple = hidden_act UpperCAmelCase_ : Optional[int] = hidden_dropout_prob UpperCAmelCase_ : Any = attention_probs_dropout_prob UpperCAmelCase_ : Dict = max_position_embeddings UpperCAmelCase_ : Optional[Any] = type_vocab_size UpperCAmelCase_ : Any = type_sequence_label_size UpperCAmelCase_ : List[str] = initializer_range UpperCAmelCase_ : List[str] = num_labels UpperCAmelCase_ : List[str] = num_choices UpperCAmelCase_ : List[Any] = relative_attention UpperCAmelCase_ : Tuple = position_biased_input UpperCAmelCase_ : str = pos_att_type UpperCAmelCase_ : Optional[int] = scope def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase_ : Any = None if self.use_input_mask: UpperCAmelCase_ : Any = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) UpperCAmelCase_ : Union[str, Any] = None if self.use_token_type_ids: UpperCAmelCase_ : int = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCAmelCase_ : Any = None UpperCAmelCase_ : str = None UpperCAmelCase_ : int = None if self.use_labels: UpperCAmelCase_ : Tuple = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase_ : int = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCAmelCase_ : Dict = ids_tensor([self.batch_size] , self.num_choices ) UpperCAmelCase_ : Optional[Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase__ ( self ): """simple docstring""" return DebertaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , relative_attention=self.relative_attention , position_biased_input=self.position_biased_input , pos_att_type=self.pos_att_type , ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Any = self.get_config() UpperCAmelCase_ : List[str] = 300 return config def UpperCamelCase__ ( self , lowercase_ ): """simple docstring""" self.parent.assertListEqual(list(result.loss.size() ) , [] ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ ): """simple docstring""" UpperCAmelCase_ : Any = DebertaModel(config=lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase_ : Union[str, Any] = model(lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ )[0] UpperCAmelCase_ : Any = model(lowercase_ , token_type_ids=lowercase_ )[0] UpperCAmelCase_ : Optional[int] = model(lowercase_ )[0] self.parent.assertListEqual(list(sequence_output.size() ) , [self.batch_size, self.seq_length, self.hidden_size] ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] = DebertaForMaskedLM(config=lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase_ : str = model(lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , labels=lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = self.num_labels UpperCAmelCase_ : Tuple = DebertaForSequenceClassification(lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase_ : Any = model(lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , labels=lowercase_ ) self.parent.assertListEqual(list(result.logits.size() ) , [self.batch_size, self.num_labels] ) self.check_loss_output(lowercase_ ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ ): """simple docstring""" UpperCAmelCase_ : List[Any] = self.num_labels UpperCAmelCase_ : Optional[int] = DebertaForTokenClassification(config=lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase_ : List[Any] = model(lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , labels=lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = DebertaForQuestionAnswering(config=lowercase_ ) model.to(lowercase_ ) model.eval() UpperCAmelCase_ : Tuple = model( lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , start_positions=lowercase_ , end_positions=lowercase_ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = self.prepare_config_and_inputs() ( ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ) : Any = config_and_inputs UpperCAmelCase_ : Any = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class A_ (_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Union[str, Any] = ( ( DebertaModel, DebertaForMaskedLM, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaForQuestionAnswering, ) if is_torch_available() else () ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = ( { """feature-extraction""": DebertaModel, """fill-mask""": DebertaForMaskedLM, """question-answering""": DebertaForQuestionAnswering, """text-classification""": DebertaForSequenceClassification, """token-classification""": DebertaForTokenClassification, """zero-shot""": DebertaForSequenceClassification, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE__ : Optional[int] = True SCREAMING_SNAKE_CASE__ : Any = False SCREAMING_SNAKE_CASE__ : Union[str, Any] = False SCREAMING_SNAKE_CASE__ : Dict = False SCREAMING_SNAKE_CASE__ : Union[str, Any] = False def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] = DebertaModelTester(self ) UpperCAmelCase_ : Optional[Any] = ConfigTester(self , config_class=lowercase_ , hidden_size=37 ) def UpperCamelCase__ ( self ): """simple docstring""" self.config_tester.run_common_tests() def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_model(*lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_sequence_classification(*lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_masked_lm(*lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_question_answering(*lowercase_ ) def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_token_classification(*lowercase_ ) @slow def UpperCamelCase__ ( self ): """simple docstring""" for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : List[str] = DebertaModel.from_pretrained(lowercase_ ) self.assertIsNotNone(lowercase_ ) @require_torch @require_sentencepiece @require_tokenizers class A_ (unittest.TestCase ): '''simple docstring''' @unittest.skip(reason="Model not available yet" ) def UpperCamelCase__ ( self ): """simple docstring""" pass @slow def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] = DebertaModel.from_pretrained("microsoft/deberta-base" ) UpperCAmelCase_ : Dict = torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]] ) UpperCAmelCase_ : Optional[Any] = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): UpperCAmelCase_ : Union[str, Any] = model(lowercase_ , attention_mask=lowercase_ )[0] # compare the actual values for a slice. UpperCAmelCase_ : Optional[int] = torch.tensor( [[[-0.59_86, -0.80_55, -0.84_62], [1.44_84, -0.93_48, -0.80_59], [0.31_23, 0.00_32, -1.41_31]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , lowercase_ , atol=1E-4 ) , F"""{output[:, 1:4, 1:4]}""" )
365
"""simple docstring""" import os import tempfile import unittest import numpy as np from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import require_flax, slow if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard from diffusers import FlaxDDIMScheduler, FlaxDiffusionPipeline, FlaxStableDiffusionPipeline @require_flax class A_ (unittest.TestCase ): '''simple docstring''' def UpperCamelCase__ ( self ): """simple docstring""" with tempfile.TemporaryDirectory() as tmpdirname: # pipeline has Flax weights UpperCAmelCase_ : Optional[Any] = FlaxDiffusionPipeline.from_pretrained( "hf-internal-testing/tiny-stable-diffusion-pipe" , safety_checker=lowercase_ , cache_dir=lowercase_ ) UpperCAmelCase_ : List[Any] = [t[-1] for t in os.walk(os.path.join(lowercase_ , os.listdir(lowercase_ )[0] , "snapshots" ) )] UpperCAmelCase_ : Dict = [item for sublist in all_root_files for item in sublist] # None of the downloaded files should be a PyTorch file even if we have some here: # https://huggingface.co/hf-internal-testing/tiny-stable-diffusion-pipe/blob/main/unet/diffusion_pytorch_model.bin assert not any(f.endswith(".bin" ) for f in files ) @slow @require_flax class A_ (unittest.TestCase ): '''simple docstring''' def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ : Union[str, Any] = FlaxStableDiffusionPipeline.from_pretrained( "hf-internal-testing/tiny-stable-diffusion-pipe" , safety_checker=lowercase_ ) UpperCAmelCase_ : Tuple = ( "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of" " field, close up, split lighting, cinematic" ) UpperCAmelCase_ : List[Any] = jax.random.PRNGKey(0 ) UpperCAmelCase_ : List[str] = 4 UpperCAmelCase_ : Tuple = jax.device_count() UpperCAmelCase_ : Optional[int] = num_samples * [prompt] UpperCAmelCase_ : List[Any] = pipeline.prepare_inputs(lowercase_ ) # shard inputs and rng UpperCAmelCase_ : int = replicate(lowercase_ ) UpperCAmelCase_ : str = jax.random.split(lowercase_ , lowercase_ ) UpperCAmelCase_ : List[str] = shard(lowercase_ ) UpperCAmelCase_ : Dict = pipeline(lowercase_ , lowercase_ , lowercase_ , lowercase_ , jit=lowercase_ ).images assert images.shape == (num_samples, 1, 64, 64, 3) if jax.device_count() == 8: assert np.abs(np.abs(images[0, 0, :2, :2, -2:] , dtype=np.floataa ).sum() - 4.1_51_47_45 ) < 1E-3 assert np.abs(np.abs(lowercase_ , dtype=np.floataa ).sum() - 4_99_47.8_75 ) < 5E-1 UpperCAmelCase_ : List[Any] = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:] ) ) ) assert len(lowercase_ ) == num_samples def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = FlaxStableDiffusionPipeline.from_pretrained( "CompVis/stable-diffusion-v1-4" , revision="flax" , safety_checker=lowercase_ ) UpperCAmelCase_ : Optional[int] = ( "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of" " field, close up, split lighting, cinematic" ) UpperCAmelCase_ : str = jax.random.PRNGKey(0 ) UpperCAmelCase_ : Union[str, Any] = 50 UpperCAmelCase_ : List[str] = jax.device_count() UpperCAmelCase_ : List[str] = num_samples * [prompt] UpperCAmelCase_ : Union[str, Any] = pipeline.prepare_inputs(lowercase_ ) # shard inputs and rng UpperCAmelCase_ : Any = replicate(lowercase_ ) UpperCAmelCase_ : List[str] = jax.random.split(lowercase_ , lowercase_ ) UpperCAmelCase_ : List[str] = shard(lowercase_ ) UpperCAmelCase_ : int = pipeline(lowercase_ , lowercase_ , lowercase_ , lowercase_ , jit=lowercase_ ).images assert images.shape == (num_samples, 1, 512, 512, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:] , dtype=np.floataa ).sum() - 0.05_65_24_01) ) < 1E-3 assert np.abs((np.abs(lowercase_ , dtype=np.floataa ).sum() - 2_38_38_08.2) ) < 5E-1 def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ : int = FlaxStableDiffusionPipeline.from_pretrained( "CompVis/stable-diffusion-v1-4" , revision="bf16" , dtype=jnp.bfloataa , safety_checker=lowercase_ ) UpperCAmelCase_ : Any = ( "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of" " field, close up, split lighting, cinematic" ) UpperCAmelCase_ : str = jax.random.PRNGKey(0 ) UpperCAmelCase_ : str = 50 UpperCAmelCase_ : List[str] = jax.device_count() UpperCAmelCase_ : List[Any] = num_samples * [prompt] UpperCAmelCase_ : Any = pipeline.prepare_inputs(lowercase_ ) # shard inputs and rng UpperCAmelCase_ : Dict = replicate(lowercase_ ) UpperCAmelCase_ : str = jax.random.split(lowercase_ , lowercase_ ) UpperCAmelCase_ : Union[str, Any] = shard(lowercase_ ) UpperCAmelCase_ : List[Any] = pipeline(lowercase_ , lowercase_ , lowercase_ , lowercase_ , jit=lowercase_ ).images assert images.shape == (num_samples, 1, 512, 512, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:] , dtype=np.floataa ).sum() - 0.04_00_39_06) ) < 1E-3 assert np.abs((np.abs(lowercase_ , dtype=np.floataa ).sum() - 2_37_35_16.75) ) < 5E-1 def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ : str = FlaxStableDiffusionPipeline.from_pretrained( "CompVis/stable-diffusion-v1-4" , revision="bf16" , dtype=jnp.bfloataa ) UpperCAmelCase_ : List[Any] = ( "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of" " field, close up, split lighting, cinematic" ) UpperCAmelCase_ : Dict = jax.random.PRNGKey(0 ) UpperCAmelCase_ : Optional[int] = 50 UpperCAmelCase_ : Optional[int] = jax.device_count() UpperCAmelCase_ : str = num_samples * [prompt] UpperCAmelCase_ : int = pipeline.prepare_inputs(lowercase_ ) # shard inputs and rng UpperCAmelCase_ : Union[str, Any] = replicate(lowercase_ ) UpperCAmelCase_ : Union[str, Any] = jax.random.split(lowercase_ , lowercase_ ) UpperCAmelCase_ : Optional[int] = shard(lowercase_ ) UpperCAmelCase_ : Any = pipeline(lowercase_ , lowercase_ , lowercase_ , lowercase_ , jit=lowercase_ ).images assert images.shape == (num_samples, 1, 512, 512, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:] , dtype=np.floataa ).sum() - 0.04_00_39_06) ) < 1E-3 assert np.abs((np.abs(lowercase_ , dtype=np.floataa ).sum() - 2_37_35_16.75) ) < 5E-1 def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : List[Any] = FlaxDDIMScheduler( beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule="scaled_linear" , set_alpha_to_one=lowercase_ , steps_offset=1 , ) UpperCAmelCase_ , UpperCAmelCase_ : int = FlaxStableDiffusionPipeline.from_pretrained( "CompVis/stable-diffusion-v1-4" , revision="bf16" , dtype=jnp.bfloataa , scheduler=lowercase_ , safety_checker=lowercase_ , ) UpperCAmelCase_ : List[Any] = scheduler.create_state() UpperCAmelCase_ : int = scheduler_state UpperCAmelCase_ : Union[str, Any] = ( "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of" " field, close up, split lighting, cinematic" ) UpperCAmelCase_ : Optional[Any] = jax.random.PRNGKey(0 ) UpperCAmelCase_ : int = 50 UpperCAmelCase_ : str = jax.device_count() UpperCAmelCase_ : List[Any] = num_samples * [prompt] UpperCAmelCase_ : int = pipeline.prepare_inputs(lowercase_ ) # shard inputs and rng UpperCAmelCase_ : int = replicate(lowercase_ ) UpperCAmelCase_ : List[str] = jax.random.split(lowercase_ , lowercase_ ) UpperCAmelCase_ : Optional[Any] = shard(lowercase_ ) UpperCAmelCase_ : Any = pipeline(lowercase_ , lowercase_ , lowercase_ , lowercase_ , jit=lowercase_ ).images assert images.shape == (num_samples, 1, 512, 512, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:] , dtype=np.floataa ).sum() - 0.0_45_04_39_45) ) < 1E-3 assert np.abs((np.abs(lowercase_ , dtype=np.floataa ).sum() - 2_34_76_93.5) ) < 5E-1 def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : int = ( "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of" " field, close up, split lighting, cinematic" ) UpperCAmelCase_ : List[str] = jax.device_count() UpperCAmelCase_ : List[Any] = num_samples * [prompt] UpperCAmelCase_ : Union[str, Any] = jax.random.split(jax.random.PRNGKey(0 ) , lowercase_ ) UpperCAmelCase_ , UpperCAmelCase_ : str = FlaxStableDiffusionPipeline.from_pretrained( "CompVis/stable-diffusion-v1-4" , revision="bf16" , dtype=jnp.bfloataa , safety_checker=lowercase_ , ) UpperCAmelCase_ : Any = replicate(lowercase_ ) UpperCAmelCase_ : List[str] = pipeline.prepare_inputs(lowercase_ ) UpperCAmelCase_ : List[str] = shard(lowercase_ ) UpperCAmelCase_ : List[Any] = pipeline(lowercase_ , lowercase_ , lowercase_ , jit=lowercase_ ).images assert images.shape == (num_samples, 1, 512, 512, 3) UpperCAmelCase_ : int = images[2, 0, 256, 10:17, 1] # With memory efficient attention UpperCAmelCase_ , UpperCAmelCase_ : int = FlaxStableDiffusionPipeline.from_pretrained( "CompVis/stable-diffusion-v1-4" , revision="bf16" , dtype=jnp.bfloataa , safety_checker=lowercase_ , use_memory_efficient_attention=lowercase_ , ) UpperCAmelCase_ : str = replicate(lowercase_ ) UpperCAmelCase_ : str = pipeline.prepare_inputs(lowercase_ ) UpperCAmelCase_ : Optional[int] = shard(lowercase_ ) UpperCAmelCase_ : str = pipeline(lowercase_ , lowercase_ , lowercase_ , jit=lowercase_ ).images assert images_eff.shape == (num_samples, 1, 512, 512, 3) UpperCAmelCase_ : Optional[int] = images[2, 0, 256, 10:17, 1] # I checked the results visually and they are very similar. However, I saw that the max diff is `1` and the `sum` # over the 8 images is exactly `256`, which is very suspicious. Testing a random slice for now. assert abs(slice_eff - slice ).max() < 1E-2
23
0
from typing import Optional from urllib.parse import quote import huggingface_hub as hfh from packaging import version def lowercase( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ = None ) -> str: '''simple docstring''' if version.parse(hfh.__version__ ).release < version.parse("""0.11.0""" ).release: # old versions of hfh don't url-encode the file path UpperCamelCase = quote(_lowerCAmelCase ) return hfh.hf_hub_url(_lowerCAmelCase , _lowerCAmelCase , repo_type="""dataset""" , revision=_lowerCAmelCase )
343
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_flax, require_tf, require_torch from transformers.utils import ( expand_dims, flatten_dict, is_flax_available, is_tf_available, is_torch_available, reshape, squeeze, transpose, ) if is_flax_available(): import jax.numpy as jnp if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch class SCREAMING_SNAKE_CASE( unittest.TestCase ): """simple docstring""" def A ( self : Any ) -> str: UpperCAmelCase : Any = { '''task_specific_params''': { '''summarization''': {'''length_penalty''': 1.0, '''max_length''': 128, '''min_length''': 12, '''num_beams''': 4}, '''summarization_cnn''': {'''length_penalty''': 2.0, '''max_length''': 142, '''min_length''': 56, '''num_beams''': 4}, '''summarization_xsum''': {'''length_penalty''': 1.0, '''max_length''': 62, '''min_length''': 11, '''num_beams''': 6}, } } UpperCAmelCase : int = { '''task_specific_params.summarization.length_penalty''': 1.0, '''task_specific_params.summarization.max_length''': 128, '''task_specific_params.summarization.min_length''': 12, '''task_specific_params.summarization.num_beams''': 4, '''task_specific_params.summarization_cnn.length_penalty''': 2.0, '''task_specific_params.summarization_cnn.max_length''': 142, '''task_specific_params.summarization_cnn.min_length''': 56, '''task_specific_params.summarization_cnn.num_beams''': 4, '''task_specific_params.summarization_xsum.length_penalty''': 1.0, '''task_specific_params.summarization_xsum.max_length''': 62, '''task_specific_params.summarization_xsum.min_length''': 11, '''task_specific_params.summarization_xsum.num_beams''': 6, } self.assertEqual(flatten_dict(__snake_case ) , __snake_case ) def A ( self : int ) -> str: UpperCAmelCase : Any = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(transpose(__snake_case ) , x.transpose() ) ) UpperCAmelCase : str = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(transpose(__snake_case , axes=(1, 2, 0) ) , x.transpose((1, 2, 0) ) ) ) @require_torch def A ( self : str ) -> Union[str, Any]: UpperCAmelCase : Any = np.random.randn(3 , 4 ) UpperCAmelCase : List[Any] = torch.tensor(__snake_case ) self.assertTrue(np.allclose(transpose(__snake_case ) , transpose(__snake_case ).numpy() ) ) UpperCAmelCase : Tuple = np.random.randn(3 , 4 , 5 ) UpperCAmelCase : Any = torch.tensor(__snake_case ) self.assertTrue(np.allclose(transpose(__snake_case , axes=(1, 2, 0) ) , transpose(__snake_case , axes=(1, 2, 0) ).numpy() ) ) @require_tf def A ( self : List[str] ) -> Optional[Any]: UpperCAmelCase : int = np.random.randn(3 , 4 ) UpperCAmelCase : Optional[int] = tf.constant(__snake_case ) self.assertTrue(np.allclose(transpose(__snake_case ) , transpose(__snake_case ).numpy() ) ) UpperCAmelCase : Union[str, Any] = np.random.randn(3 , 4 , 5 ) UpperCAmelCase : str = tf.constant(__snake_case ) self.assertTrue(np.allclose(transpose(__snake_case , axes=(1, 2, 0) ) , transpose(__snake_case , axes=(1, 2, 0) ).numpy() ) ) @require_flax def A ( self : Tuple ) -> Any: UpperCAmelCase : List[Any] = np.random.randn(3 , 4 ) UpperCAmelCase : List[str] = jnp.array(__snake_case ) self.assertTrue(np.allclose(transpose(__snake_case ) , np.asarray(transpose(__snake_case ) ) ) ) UpperCAmelCase : Dict = np.random.randn(3 , 4 , 5 ) UpperCAmelCase : int = jnp.array(__snake_case ) self.assertTrue(np.allclose(transpose(__snake_case , axes=(1, 2, 0) ) , np.asarray(transpose(__snake_case , axes=(1, 2, 0) ) ) ) ) def A ( self : Optional[Any] ) -> Any: UpperCAmelCase : Union[str, Any] = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(reshape(__snake_case , (4, 3) ) , np.reshape(__snake_case , (4, 3) ) ) ) UpperCAmelCase : Union[str, Any] = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(reshape(__snake_case , (12, 5) ) , np.reshape(__snake_case , (12, 5) ) ) ) @require_torch def A ( self : Union[str, Any] ) -> int: UpperCAmelCase : Dict = np.random.randn(3 , 4 ) UpperCAmelCase : Optional[Any] = torch.tensor(__snake_case ) self.assertTrue(np.allclose(reshape(__snake_case , (4, 3) ) , reshape(__snake_case , (4, 3) ).numpy() ) ) UpperCAmelCase : List[str] = np.random.randn(3 , 4 , 5 ) UpperCAmelCase : List[Any] = torch.tensor(__snake_case ) self.assertTrue(np.allclose(reshape(__snake_case , (12, 5) ) , reshape(__snake_case , (12, 5) ).numpy() ) ) @require_tf def A ( self : int ) -> List[str]: UpperCAmelCase : List[Any] = np.random.randn(3 , 4 ) UpperCAmelCase : List[str] = tf.constant(__snake_case ) self.assertTrue(np.allclose(reshape(__snake_case , (4, 3) ) , reshape(__snake_case , (4, 3) ).numpy() ) ) UpperCAmelCase : List[Any] = np.random.randn(3 , 4 , 5 ) UpperCAmelCase : Optional[Any] = tf.constant(__snake_case ) self.assertTrue(np.allclose(reshape(__snake_case , (12, 5) ) , reshape(__snake_case , (12, 5) ).numpy() ) ) @require_flax def A ( self : Any ) -> Dict: UpperCAmelCase : Tuple = np.random.randn(3 , 4 ) UpperCAmelCase : Union[str, Any] = jnp.array(__snake_case ) self.assertTrue(np.allclose(reshape(__snake_case , (4, 3) ) , np.asarray(reshape(__snake_case , (4, 3) ) ) ) ) UpperCAmelCase : Any = np.random.randn(3 , 4 , 5 ) UpperCAmelCase : Optional[Any] = jnp.array(__snake_case ) self.assertTrue(np.allclose(reshape(__snake_case , (12, 5) ) , np.asarray(reshape(__snake_case , (12, 5) ) ) ) ) def A ( self : List[Any] ) -> List[Any]: UpperCAmelCase : Union[str, Any] = np.random.randn(1 , 3 , 4 ) self.assertTrue(np.allclose(squeeze(__snake_case ) , np.squeeze(__snake_case ) ) ) UpperCAmelCase : str = np.random.randn(1 , 4 , 1 , 5 ) self.assertTrue(np.allclose(squeeze(__snake_case , axis=2 ) , np.squeeze(__snake_case , axis=2 ) ) ) @require_torch def A ( self : Optional[int] ) -> Union[str, Any]: UpperCAmelCase : Optional[int] = np.random.randn(1 , 3 , 4 ) UpperCAmelCase : List[str] = torch.tensor(__snake_case ) self.assertTrue(np.allclose(squeeze(__snake_case ) , squeeze(__snake_case ).numpy() ) ) UpperCAmelCase : Any = np.random.randn(1 , 4 , 1 , 5 ) UpperCAmelCase : str = torch.tensor(__snake_case ) self.assertTrue(np.allclose(squeeze(__snake_case , axis=2 ) , squeeze(__snake_case , axis=2 ).numpy() ) ) @require_tf def A ( self : Optional[Any] ) -> Dict: UpperCAmelCase : int = np.random.randn(1 , 3 , 4 ) UpperCAmelCase : Optional[int] = tf.constant(__snake_case ) self.assertTrue(np.allclose(squeeze(__snake_case ) , squeeze(__snake_case ).numpy() ) ) UpperCAmelCase : List[str] = np.random.randn(1 , 4 , 1 , 5 ) UpperCAmelCase : Optional[int] = tf.constant(__snake_case ) self.assertTrue(np.allclose(squeeze(__snake_case , axis=2 ) , squeeze(__snake_case , axis=2 ).numpy() ) ) @require_flax def A ( self : List[Any] ) -> Dict: UpperCAmelCase : Optional[int] = np.random.randn(1 , 3 , 4 ) UpperCAmelCase : int = jnp.array(__snake_case ) self.assertTrue(np.allclose(squeeze(__snake_case ) , np.asarray(squeeze(__snake_case ) ) ) ) UpperCAmelCase : str = np.random.randn(1 , 4 , 1 , 5 ) UpperCAmelCase : int = jnp.array(__snake_case ) self.assertTrue(np.allclose(squeeze(__snake_case , axis=2 ) , np.asarray(squeeze(__snake_case , axis=2 ) ) ) ) def A ( self : Optional[Any] ) -> int: UpperCAmelCase : Optional[Any] = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(expand_dims(__snake_case , axis=1 ) , np.expand_dims(__snake_case , axis=1 ) ) ) @require_torch def A ( self : List[str] ) -> Tuple: UpperCAmelCase : Tuple = np.random.randn(3 , 4 ) UpperCAmelCase : Tuple = torch.tensor(__snake_case ) self.assertTrue(np.allclose(expand_dims(__snake_case , axis=1 ) , expand_dims(__snake_case , axis=1 ).numpy() ) ) @require_tf def A ( self : List[str] ) -> Union[str, Any]: UpperCAmelCase : Union[str, Any] = np.random.randn(3 , 4 ) UpperCAmelCase : Any = tf.constant(__snake_case ) self.assertTrue(np.allclose(expand_dims(__snake_case , axis=1 ) , expand_dims(__snake_case , axis=1 ).numpy() ) ) @require_flax def A ( self : Any ) -> List[Any]: UpperCAmelCase : List[str] = np.random.randn(3 , 4 ) UpperCAmelCase : str = jnp.array(__snake_case ) self.assertTrue(np.allclose(expand_dims(__snake_case , axis=1 ) , np.asarray(expand_dims(__snake_case , axis=1 ) ) ) )
23
0
from __future__ import annotations from collections import namedtuple from dataclasses import dataclass @dataclass class SCREAMING_SNAKE_CASE__ : __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None a__: Dict = namedtuple('CoinsDistribResult', 'moves excess') def UpperCamelCase__( UpperCamelCase__ : TreeNode | None )->int: if root is None: return 0 # Validation def count_nodes(UpperCamelCase__ : TreeNode | None ) -> int: if node is None: return 0 return count_nodes(node.left ) + count_nodes(node.right ) + 1 def count_coins(UpperCamelCase__ : TreeNode | None ) -> int: if node is None: return 0 return count_coins(node.left ) + count_coins(node.right ) + node.data if count_nodes(UpperCamelCase__ ) != count_coins(UpperCamelCase__ ): raise ValueError('''The nodes number should be same as the number of coins''' ) # Main calculation def get_distrib(UpperCamelCase__ : TreeNode | None ) -> CoinsDistribResult: if node is None: return CoinsDistribResult(0 , 1 ) A__ , A__ = get_distrib(node.left ) A__ , A__ = get_distrib(node.right ) A__ = 1 - left_distrib_excess A__ = 1 - right_distrib_excess A__ = ( left_distrib_moves + right_distrib_moves + abs(UpperCamelCase__ ) + abs(UpperCamelCase__ ) ) A__ = node.data - coins_to_left - coins_to_right return CoinsDistribResult(UpperCamelCase__ , UpperCamelCase__ ) return get_distrib(UpperCamelCase__ )[0] if __name__ == "__main__": import doctest doctest.testmod()
39
import warnings from contextlib import contextmanager from ....processing_utils import ProcessorMixin class SCREAMING_SNAKE_CASE__ ( UpperCamelCase__ ): __SCREAMING_SNAKE_CASE = '''MCTCTFeatureExtractor''' __SCREAMING_SNAKE_CASE = '''AutoTokenizer''' def __init__( self,__lowerCamelCase,__lowerCamelCase ): super().__init__(__lowerCamelCase,__lowerCamelCase ) A__ = self.feature_extractor A__ = False def __call__( self,*__lowerCamelCase,**__lowerCamelCase ): # For backward compatibility if self._in_target_context_manager: return self.current_processor(*__lowerCamelCase,**__lowerCamelCase ) 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''',__lowerCamelCase ) A__ = kwargs.pop('''sampling_rate''',__lowerCamelCase ) A__ = kwargs.pop('''text''',__lowerCamelCase ) if len(__lowerCamelCase ) > 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(__lowerCamelCase,*__lowerCamelCase,sampling_rate=__lowerCamelCase,**__lowerCamelCase ) if text is not None: A__ = self.tokenizer(__lowerCamelCase,**__lowerCamelCase ) if text is None: return inputs elif audio is None: return encodings else: A__ = encodings['''input_ids'''] return inputs def UpperCamelCase ( self,*__lowerCamelCase,**__lowerCamelCase ): return self.tokenizer.batch_decode(*__lowerCamelCase,**__lowerCamelCase ) def UpperCamelCase ( self,*__lowerCamelCase,**__lowerCamelCase ): # For backward compatibility if self._in_target_context_manager: return self.current_processor.pad(*__lowerCamelCase,**__lowerCamelCase ) A__ = kwargs.pop('''input_features''',__lowerCamelCase ) A__ = kwargs.pop('''labels''',__lowerCamelCase ) if len(__lowerCamelCase ) > 0: A__ = args[0] A__ = args[1:] if input_features is not None: A__ = self.feature_extractor.pad(__lowerCamelCase,*__lowerCamelCase,**__lowerCamelCase ) if labels is not None: A__ = self.tokenizer.pad(__lowerCamelCase,**__lowerCamelCase ) if labels is None: return input_features elif input_features is None: return labels else: A__ = labels['''input_ids'''] return input_features def UpperCamelCase ( self,*__lowerCamelCase,**__lowerCamelCase ): return self.tokenizer.decode(*__lowerCamelCase,**__lowerCamelCase ) @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
39
1
'''simple docstring''' def a_ ( lowerCamelCase : list[int] , lowerCamelCase : list[int] , lowerCamelCase : int ): return not any( neighbour == 1 and colored_vertices[i] == color for i, neighbour in enumerate(lowerCamelCase ) ) def a_ ( lowerCamelCase : list[list[int]] , lowerCamelCase : int , lowerCamelCase : list[int] , lowerCamelCase : int ): # Base Case if index == len(lowerCamelCase ): return True # Recursive Step for i in range(lowerCamelCase ): if valid_coloring(graph[index] , lowerCamelCase , lowerCamelCase ): # Color current vertex lowerCAmelCase = i # Validate coloring if util_color(lowerCamelCase , lowerCamelCase , lowerCamelCase , index + 1 ): return True # Backtrack lowerCAmelCase = -1 return False def a_ ( lowerCamelCase : list[list[int]] , lowerCamelCase : int ): lowerCAmelCase = [-1] * len(lowerCamelCase ) if util_color(lowerCamelCase , lowerCamelCase , lowerCamelCase , 0 ): return colored_vertices return []
4
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCamelCase_ = { "configuration_xlm": ["XLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLMConfig", "XLMOnnxConfig"], "tokenization_xlm": ["XLMTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ "XLM_PRETRAINED_MODEL_ARCHIVE_LIST", "XLMForMultipleChoice", "XLMForQuestionAnswering", "XLMForQuestionAnsweringSimple", "XLMForSequenceClassification", "XLMForTokenClassification", "XLMModel", "XLMPreTrainedModel", "XLMWithLMHeadModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ "TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST", "TFXLMForMultipleChoice", "TFXLMForQuestionAnsweringSimple", "TFXLMForSequenceClassification", "TFXLMForTokenClassification", "TFXLMMainLayer", "TFXLMModel", "TFXLMPreTrainedModel", "TFXLMWithLMHeadModel", ] if TYPE_CHECKING: from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig from .tokenization_xlm import XLMTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm import ( XLM_PRETRAINED_MODEL_ARCHIVE_LIST, XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMPreTrainedModel, XLMWithLMHeadModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm import ( TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMForMultipleChoice, TFXLMForQuestionAnsweringSimple, TFXLMForSequenceClassification, TFXLMForTokenClassification, TFXLMMainLayer, TFXLMModel, TFXLMPreTrainedModel, TFXLMWithLMHeadModel, ) else: import sys UpperCamelCase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
251
0
import argparse import os import re import torch from flax.traverse_util import flatten_dict from tax import checkpoints from transformers import ( AutoTokenizer, PixaStructConfig, PixaStructForConditionalGeneration, PixaStructImageProcessor, PixaStructProcessor, PixaStructTextConfig, PixaStructVisionConfig, ) def SCREAMING_SNAKE_CASE ( snake_case_ : Optional[int] ): snake_case__ : str = checkpoints.load_tax_checkpoint(snake_case_ ) snake_case__ : Any = flatten_dict(snake_case_ ) return flax_params def SCREAMING_SNAKE_CASE ( snake_case_ : int ): snake_case__ : Dict = {} snake_case__ : Optional[Any] = { "token_embedder": "embeddings", "encoder_norm": "layernorm", "kernel": "weight", ".out": ".output", "scale": "weight", "embedders_0.pos_embedding": "row_embedder.weight", "embedders_1.pos_embedding": "column_embedder.weight", } snake_case__ : List[Any] = { "query": "attention.query", "key": "attention.key", "value": "attention.value", "output.dense": "output", "encoder_decoder_attention.o": "encoder_decoder_attention.attention.o", "pre_self_attention_layer_norm": "self_attention.layer_norm", "pre_cross_attention_layer_norm": "encoder_decoder_attention.layer_norm", "mlp.": "mlp.DenseReluDense.", "pre_mlp_layer_norm": "mlp.layer_norm", "self_attention.o": "self_attention.attention.o", "decoder.embeddings.embedding": "decoder.embed_tokens.weight", "decoder.relpos_bias.rel_embedding": "decoder.layer.0.self_attention.attention.relative_attention_bias.weight", "decoder.decoder_norm.weight": "decoder.final_layer_norm.weight", "decoder.logits_dense.weight": "decoder.lm_head.weight", } for key in flax_dict.keys(): if "target" in key: # remove the first prefix from the key snake_case__ : Dict = ".".join(key[1:] ) # rename the key for old, new in CONVERSION_MAPPING.items(): snake_case__ : int = new_key.replace(snake_case_ , snake_case_ ) if "decoder" in new_key: for old, new in DECODER_CONVERSION_MAPPING.items(): snake_case__ : Tuple = new_key.replace(snake_case_ , snake_case_ ) if "layers" in new_key and "decoder" not in new_key: # use regex to replace the layer number snake_case__ : Any = re.sub(R"layers_(\d+)" , R"layer.\1" , snake_case_ ) snake_case__ : Optional[Any] = new_key.replace("encoder" , "encoder.encoder" ) elif "layers" in new_key and "decoder" in new_key: # use regex to replace the layer number snake_case__ : Optional[int] = re.sub(R"layers_(\d+)" , R"layer.\1" , snake_case_ ) snake_case__ : List[str] = flax_dict[key] snake_case__ : Union[str, Any] = {} # convert converted_dict into torch format for key in converted_dict.keys(): if ("embed_tokens" not in key) and ("embedder" not in key): snake_case__ : List[str] = torch.from_numpy(converted_dict[key].T ) else: snake_case__ : Dict = torch.from_numpy(converted_dict[key] ) return converted_torch_dict def SCREAMING_SNAKE_CASE ( snake_case_ : Optional[Any] , snake_case_ : List[str] , snake_case_ : Dict=False , snake_case_ : int=False ): snake_case__ : Union[str, Any] = get_flax_param(snake_case_ ) if not use_large: snake_case__ : Any = PixaStructVisionConfig() snake_case__ : List[Any] = PixaStructTextConfig() else: snake_case__ : int = PixaStructVisionConfig( hidden_size=1536 , d_ff=3968 , num_attention_heads=24 , num_hidden_layers=18 ) snake_case__ : Any = PixaStructTextConfig(hidden_size=1536 , d_ff=3968 , num_heads=24 , num_layers=18 ) snake_case__ : Union[str, Any] = PixaStructConfig( vision_config=encoder_config.to_dict() , text_config=decoder_config.to_dict() , is_vqa=snake_case_ ) snake_case__ : int = PixaStructForConditionalGeneration(snake_case_ ) snake_case__ : List[str] = rename_and_convert_flax_params(snake_case_ ) model.load_state_dict(snake_case_ ) snake_case__ : Union[str, Any] = AutoTokenizer.from_pretrained("ybelkada/test-pix2struct-tokenizer" ) snake_case__ : List[str] = PixaStructImageProcessor() snake_case__ : List[str] = PixaStructProcessor(image_processor=snake_case_ , tokenizer=snake_case_ ) if use_large: snake_case__ : Optional[int] = 4096 snake_case__ : Dict = True # mkdir if needed os.makedirs(snake_case_ , exist_ok=snake_case_ ) model.save_pretrained(snake_case_ ) processor.save_pretrained(snake_case_ ) print("Model saved in {}".format(snake_case_ ) ) if __name__ == "__main__": __lowerCamelCase : Dict = argparse.ArgumentParser() parser.add_argument("""--t5x_checkpoint_path""", default=None, type=str, help="""Path to the original T5x checkpoint.""") parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--use_large""", action="""store_true""", help="""Use large model.""") parser.add_argument("""--is_vqa""", action="""store_true""", help="""Use large model.""") __lowerCamelCase : Optional[Any] = parser.parse_args() convert_pixastruct_original_pytorch_checkpoint_to_hf( args.tax_checkpoint_path, args.pytorch_dump_folder_path, args.use_large )
286
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 SCREAMING_SNAKE_CASE__ ( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase ): """simple docstring""" a_ = StableDiffusionInstructPixaPixPipeline a_ = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"height", "width", "cross_attention_kwargs"} a_ = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS a_ = IMAGE_TO_IMAGE_IMAGE_PARAMS a_ = IMAGE_TO_IMAGE_IMAGE_PARAMS def _lowercase ( self : List[str] ): torch.manual_seed(0 ) snake_case__ : Any = UNetaDConditionModel( block_out_channels=(3_2, 6_4) , layers_per_block=2 , sample_size=3_2 , in_channels=8 , out_channels=4 , down_block_types=("DownBlock2D", "CrossAttnDownBlock2D") , up_block_types=("CrossAttnUpBlock2D", "UpBlock2D") , cross_attention_dim=3_2 , ) snake_case__ : int = PNDMScheduler(skip_prk_steps=__A ) torch.manual_seed(0 ) snake_case__ : Union[str, Any] = AutoencoderKL( block_out_channels=[3_2, 6_4] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , ) torch.manual_seed(0 ) snake_case__ : int = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=3_2 , intermediate_size=3_7 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_0_0_0 , ) snake_case__ : Union[str, Any] = CLIPTextModel(__A ) snake_case__ : List[Any] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) snake_case__ : str = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def _lowercase ( self : List[Any] , __A : int , __A : Any=0 ): snake_case__ : Optional[int] = floats_tensor((1, 3, 3_2, 3_2) , rng=random.Random(__A ) ).to(__A ) snake_case__ : int = image.cpu().permute(0 , 2 , 3 , 1 )[0] snake_case__ : Union[str, Any] = Image.fromarray(np.uinta(__A ) ).convert("RGB" ) if str(__A ).startswith("mps" ): snake_case__ : List[Any] = torch.manual_seed(__A ) else: snake_case__ : Optional[int] = torch.Generator(device=__A ).manual_seed(__A ) snake_case__ : Optional[int] = { "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 _lowercase ( self : int ): snake_case__ : Tuple = "cpu" # ensure determinism for the device-dependent torch.Generator snake_case__ : int = self.get_dummy_components() snake_case__ : List[Any] = StableDiffusionInstructPixaPixPipeline(**__A ) snake_case__ : List[Any] = sd_pipe.to(__A ) sd_pipe.set_progress_bar_config(disable=__A ) snake_case__ : Tuple = self.get_dummy_inputs(__A ) snake_case__ : List[str] = sd_pipe(**__A ).images snake_case__ : str = image[0, -3:, -3:, -1] assert image.shape == (1, 3_2, 3_2, 3) snake_case__ : List[Any] = np.array([0.7_5_2_6, 0.3_7_5_0, 0.4_5_4_7, 0.6_1_1_7, 0.5_8_6_6, 0.5_0_1_6, 0.4_3_2_7, 0.5_6_4_2, 0.4_8_1_5] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def _lowercase ( self : Union[str, Any] ): snake_case__ : Dict = "cpu" # ensure determinism for the device-dependent torch.Generator snake_case__ : List[Any] = self.get_dummy_components() snake_case__ : List[Any] = StableDiffusionInstructPixaPixPipeline(**__A ) snake_case__ : str = sd_pipe.to(__A ) sd_pipe.set_progress_bar_config(disable=__A ) snake_case__ : str = self.get_dummy_inputs(__A ) snake_case__ : List[Any] = "french fries" snake_case__ : str = sd_pipe(**__A , negative_prompt=__A ) snake_case__ : Any = output.images snake_case__ : Any = image[0, -3:, -3:, -1] assert image.shape == (1, 3_2, 3_2, 3) snake_case__ : Union[str, Any] = np.array([0.7_5_1_1, 0.3_6_4_2, 0.4_5_5_3, 0.6_2_3_6, 0.5_7_9_7, 0.5_0_1_3, 0.4_3_4_3, 0.5_6_1_1, 0.4_8_3_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def _lowercase ( self : Optional[int] ): snake_case__ : Tuple = "cpu" # ensure determinism for the device-dependent torch.Generator snake_case__ : List[Any] = self.get_dummy_components() snake_case__ : str = StableDiffusionInstructPixaPixPipeline(**__A ) snake_case__ : List[str] = sd_pipe.to(__A ) sd_pipe.set_progress_bar_config(disable=__A ) snake_case__ : Any = self.get_dummy_inputs(__A ) snake_case__ : Tuple = [inputs["prompt"]] * 2 snake_case__ : Any = np.array(inputs["image"] ).astype(np.floataa ) / 2_5_5.0 snake_case__ : List[str] = torch.from_numpy(__A ).unsqueeze(0 ).to(__A ) snake_case__ : Union[str, Any] = image / 2 + 0.5 snake_case__ : str = image.permute(0 , 3 , 1 , 2 ) snake_case__ : int = image.repeat(2 , 1 , 1 , 1 ) snake_case__ : str = sd_pipe(**__A ).images snake_case__ : Any = image[-1, -3:, -3:, -1] assert image.shape == (2, 3_2, 3_2, 3) snake_case__ : int = np.array([0.5_8_1_2, 0.5_7_4_8, 0.5_2_2_2, 0.5_9_0_8, 0.5_6_9_5, 0.7_1_7_4, 0.6_8_0_4, 0.5_5_2_3, 0.5_5_7_9] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def _lowercase ( self : Union[str, Any] ): snake_case__ : Union[str, Any] = "cpu" # ensure determinism for the device-dependent torch.Generator snake_case__ : int = self.get_dummy_components() snake_case__ : Dict = EulerAncestralDiscreteScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule="scaled_linear" ) snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline(**__A ) snake_case__ : str = sd_pipe.to(__A ) sd_pipe.set_progress_bar_config(disable=__A ) snake_case__ : str = self.get_dummy_inputs(__A ) snake_case__ : Optional[Any] = sd_pipe(**__A ).images snake_case__ : Dict = image[0, -3:, -3:, -1] snake_case__ : Union[str, Any] = [round(__A , 4 ) for x in image_slice.flatten().tolist()] print(",".join([str(__A ) for x in slice] ) ) assert image.shape == (1, 3_2, 3_2, 3) snake_case__ : str = np.array([0.7_4_1_7, 0.3_8_4_2, 0.4_7_3_2, 0.5_7_7_6, 0.5_8_9_1, 0.5_1_3_9, 0.4_0_5_2, 0.5_6_7_3, 0.4_9_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def _lowercase ( self : List[str] ): super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) def _lowercase ( self : List[Any] ): snake_case__ : Tuple = self.get_dummy_components() snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline(**__A ) snake_case__ : int = VaeImageProcessor(do_resize=__A , do_normalize=__A ) snake_case__ : Any = pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) snake_case__ : Dict = pipe(**self.get_dummy_inputs_by_type(__A , input_image_type="pt" ) )[0] snake_case__ : int = components["vae"] snake_case__ : Union[str, Any] = 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(): snake_case__ : Optional[int] = vae.encode(inputs[image_param] ).latent_dist.mode() snake_case__ : str = pipe(**__A )[0] snake_case__ : Dict = 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 SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def _lowercase ( self : Optional[int] ): super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowercase ( self : str , __A : Dict=0 ): snake_case__ : Optional[int] = torch.manual_seed(__A ) snake_case__ : Tuple = load_image( "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg" ) snake_case__ : Optional[Any] = { "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 _lowercase ( self : int ): snake_case__ : Union[str, Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=__A ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing() snake_case__ : Union[str, Any] = self.get_inputs() snake_case__ : Union[str, Any] = pipe(**__A ).images snake_case__ : Dict = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_1_2, 5_1_2, 3) snake_case__ : Any = np.array([0.5_9_0_2, 0.6_0_1_5, 0.6_0_2_7, 0.5_9_8_3, 0.6_0_9_2, 0.6_0_6_1, 0.5_7_6_5, 0.5_7_8_5, 0.5_5_5_5] ) assert np.abs(expected_slice - image_slice ).max() < 1e-3 def _lowercase ( self : str ): snake_case__ : int = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=__A ) snake_case__ : Dict = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing() snake_case__ : List[str] = self.get_inputs() snake_case__ : Any = pipe(**__A ).images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_1_2, 5_1_2, 3) snake_case__ : Optional[Any] = np.array([0.6_5_7_8, 0.6_8_1_7, 0.6_9_7_2, 0.6_7_6_1, 0.6_8_5_6, 0.6_9_1_6, 0.6_4_2_8, 0.6_5_1_6, 0.6_3_0_1] ) assert np.abs(expected_slice - image_slice ).max() < 1e-3 def _lowercase ( self : Dict ): snake_case__ : List[str] = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=__A ) snake_case__ : List[str] = DDIMScheduler.from_config(pipe.scheduler.config ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing() snake_case__ : int = self.get_inputs() snake_case__ : Union[str, Any] = pipe(**__A ).images snake_case__ : Tuple = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_1_2, 5_1_2, 3) snake_case__ : Union[str, Any] = np.array([0.3_8_2_8, 0.3_8_3_4, 0.3_8_1_8, 0.3_7_9_2, 0.3_8_6_5, 0.3_7_5_2, 0.3_7_9_2, 0.3_8_4_7, 0.3_7_5_3] ) assert np.abs(expected_slice - image_slice ).max() < 1e-3 def _lowercase ( self : List[Any] ): snake_case__ : Optional[Any] = 0 def callback_fn(__A : int , __A : int , __A : torch.FloatTensor ) -> None: snake_case__ : Union[str, Any] = True nonlocal number_of_steps number_of_steps += 1 if step == 1: snake_case__ : Optional[Any] = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 6_4, 6_4) snake_case__ : int = latents[0, -3:, -3:, -1] snake_case__ : Optional[int] = np.array([-0.2_4_6_3, -0.4_6_4_4, -0.9_7_5_6, 1.5_1_7_6, 1.4_4_1_4, 0.7_8_6_6, 0.9_8_9_7, 0.8_5_2_1, 0.7_9_8_3] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 elif step == 2: snake_case__ : int = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 6_4, 6_4) snake_case__ : Any = latents[0, -3:, -3:, -1] snake_case__ : Dict = np.array([-0.2_6_4_4, -0.4_6_2_6, -0.9_6_5_3, 1.5_1_7_6, 1.4_5_5_1, 0.7_6_8_6, 0.9_8_0_5, 0.8_4_5_2, 0.8_1_1_5] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 snake_case__ : Any = False snake_case__ : Union[str, Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=__A , torch_dtype=torch.floataa ) snake_case__ : int = pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing() snake_case__ : Optional[Any] = self.get_inputs() pipe(**__A , callback=__A , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def _lowercase ( self : List[Any] ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() snake_case__ : Dict = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=__A , torch_dtype=torch.floataa ) snake_case__ : Tuple = pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() snake_case__ : Dict = self.get_inputs() snake_case__ : List[Any] = pipe(**__A ) snake_case__ : List[str] = torch.cuda.max_memory_allocated() # make sure that less than 2.2 GB is allocated assert mem_bytes < 2.2 * 1_0**9 def _lowercase ( self : Tuple ): snake_case__ : int = self.get_inputs() # resize to resolution that is divisible by 8 but not 16 or 32 snake_case__ : Union[str, Any] = inputs["image"].resize((5_0_4, 5_0_4) ) snake_case__ : Optional[Any] = "timbrooks/instruct-pix2pix" snake_case__ : Union[str, Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( __A , safety_checker=__A , ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing() snake_case__ : Union[str, Any] = pipe(**__A ) snake_case__ : Tuple = output.images[0] snake_case__ : List[Any] = image[2_5_5:2_5_8, 3_8_3:3_8_6, -1] assert image.shape == (5_0_4, 5_0_4, 3) snake_case__ : int = np.array([0.2_7_2_6, 0.2_5_2_9, 0.2_6_6_4, 0.2_6_5_5, 0.2_6_4_1, 0.2_6_4_2, 0.2_5_9_1, 0.2_6_4_9, 0.2_5_9_0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-3
286
1
"""simple docstring""" from math import factorial def _SCREAMING_SNAKE_CASE ( _lowercase : int = 100 ) ->int: '''simple docstring''' return sum(map(_lowercase , str(factorial(_lowercase ) ) ) ) if __name__ == "__main__": print(solution(int(input('''Enter the Number: ''').strip())))
105
'''simple docstring''' import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( WavaVecaConfig, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaForCTC, WavaVecaForPreTraining, WavaVecaProcessor, logging, ) from transformers.models.wavaveca.modeling_wavaveca import WavaVecaForSequenceClassification logging.set_verbosity_info() UpperCamelCase__: int = logging.get_logger(__name__) UpperCamelCase__: Dict = { "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", "adapter_layer": "encoder.layers.*.adapter_layer", "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", "pooling_layer.linear": "projector", "pooling_layer.projection": "classifier", } UpperCamelCase__: Optional[Any] = [ "lm_head", "quantizer.weight_proj", "quantizer.codevectors", "project_q", "project_hid", "projector", "classifier", ] def snake_case_ ( _lowerCAmelCase : str ) -> Union[str, Any]: UpperCAmelCase : Optional[int] = {} with open(_lowerCAmelCase , '''r''' ) as file: for line_number, line in enumerate(_lowerCAmelCase ): UpperCAmelCase : List[str] = line.strip() if line: UpperCAmelCase : str = line.split() UpperCAmelCase : Union[str, Any] = line_number UpperCAmelCase : List[Any] = words[0] UpperCAmelCase : Union[str, Any] = value return result def snake_case_ ( _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Tuple , _lowerCAmelCase : str ) -> int: for attribute in key.split('''.''' ): UpperCAmelCase : Dict = getattr(_lowerCAmelCase , _lowerCAmelCase ) UpperCAmelCase : Dict = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(_lowerCAmelCase ): UpperCAmelCase : Any = PARAM_MAPPING[full_name.split('''.''' )[-1]] UpperCAmelCase : Dict = '''param''' if weight_type is not None and weight_type != "param": UpperCAmelCase : Optional[int] = getattr(_lowerCAmelCase , _lowerCAmelCase ).shape elif weight_type is not None and weight_type == "param": UpperCAmelCase : List[Any] = hf_pointer for attribute in hf_param_name.split('''.''' ): UpperCAmelCase : Optional[Any] = getattr(_lowerCAmelCase , _lowerCAmelCase ) UpperCAmelCase : int = shape_pointer.shape # let's reduce dimension UpperCAmelCase : Union[str, Any] = value[0] else: UpperCAmelCase : List[Any] = 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": UpperCAmelCase : int = value elif weight_type == "weight_g": UpperCAmelCase : str = value elif weight_type == "weight_v": UpperCAmelCase : Dict = value elif weight_type == "bias": UpperCAmelCase : str = value elif weight_type == "param": for attribute in hf_param_name.split('''.''' ): UpperCAmelCase : int = getattr(_lowerCAmelCase , _lowerCAmelCase ) UpperCAmelCase : Optional[int] = value else: UpperCAmelCase : Tuple = value logger.info(f"""{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.""" ) def snake_case_ ( _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Any , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict , _lowerCAmelCase : List[Any] ) -> List[Any]: UpperCAmelCase : List[str] = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(_lowerCAmelCase ): UpperCAmelCase : List[str] = PARAM_MAPPING[full_name.split('''.''' )[-1]] UpperCAmelCase : Any = '''param''' if weight_type is not None and weight_type != "param": UpperCAmelCase : Optional[int] = '''.'''.join([key, weight_type] ) elif weight_type is not None and weight_type == "param": UpperCAmelCase : Optional[int] = '''.'''.join([key, hf_param_name] ) else: UpperCAmelCase : List[Any] = key UpperCAmelCase : Tuple = value if '''lm_head''' in full_key else value[0] UpperCamelCase__: Tuple = { "W_a": "linear_1.weight", "W_b": "linear_2.weight", "b_a": "linear_1.bias", "b_b": "linear_2.bias", "ln_W": "norm.weight", "ln_b": "norm.bias", } def snake_case_ ( _lowerCAmelCase : str , _lowerCAmelCase : List[str] , _lowerCAmelCase : Any=None , _lowerCAmelCase : Optional[Any]=None ) -> int: UpperCAmelCase : List[Any] = False for key, mapped_key in MAPPING.items(): UpperCAmelCase : int = '''wav2vec2.''' + 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]: UpperCAmelCase : Optional[Any] = True if "*" in mapped_key: UpperCAmelCase : Tuple = name.split(_lowerCAmelCase )[0].split('''.''' )[-2] UpperCAmelCase : List[Any] = mapped_key.replace('''*''' , _lowerCAmelCase ) if "weight_g" in name: UpperCAmelCase : str = '''weight_g''' elif "weight_v" in name: UpperCAmelCase : int = '''weight_v''' elif "bias" in name: UpperCAmelCase : int = '''bias''' elif "weight" in name: # TODO: don't match quantizer.weight_proj UpperCAmelCase : List[str] = '''weight''' else: UpperCAmelCase : Dict = None if hf_dict is not None: rename_dict(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) else: set_recursively(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) return is_used return is_used def snake_case_ ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Any ) -> Any: UpperCAmelCase : Dict = [] UpperCAmelCase : Dict = fairseq_model.state_dict() UpperCAmelCase : Union[str, Any] = hf_model.wavaveca.feature_extractor for name, value in fairseq_dict.items(): UpperCAmelCase : Dict = False if "conv_layers" in name: load_conv_layer( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , hf_model.config.feat_extract_norm == '''group''' , ) UpperCAmelCase : Any = True else: UpperCAmelCase : Optional[Any] = load_wavaveca_layer(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) if not is_used: unused_weights.append(_lowerCAmelCase ) logger.warning(f"""Unused weights: {unused_weights}""" ) def snake_case_ ( _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Tuple , _lowerCAmelCase : Any , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Optional[int] ) -> Union[str, Any]: UpperCAmelCase : Any = full_name.split('''conv_layers.''' )[-1] UpperCAmelCase : Optional[int] = name.split('''.''' ) UpperCAmelCase : Tuple = int(items[0] ) UpperCAmelCase : Tuple = 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.""" ) UpperCAmelCase : Tuple = 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.""" ) UpperCAmelCase : Union[str, Any] = 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.""" ) UpperCAmelCase : Union[str, Any] = 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.""" ) UpperCAmelCase : List[str] = 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 snake_case_ ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict=None , _lowerCAmelCase : List[Any]=None , _lowerCAmelCase : int=True , _lowerCAmelCase : Optional[int]=False ) -> Dict: if config_path is not None: UpperCAmelCase : List[str] = WavaVecaConfig.from_pretrained(_lowerCAmelCase ) else: UpperCAmelCase : List[Any] = WavaVecaConfig() if is_seq_class: UpperCAmelCase : Optional[Any] = read_txt_into_dict(_lowerCAmelCase ) UpperCAmelCase : Optional[int] = idalabel UpperCAmelCase : Optional[Any] = WavaVecaForSequenceClassification(_lowerCAmelCase ) UpperCAmelCase : Dict = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=_lowerCAmelCase , return_attention_mask=_lowerCAmelCase , ) feature_extractor.save_pretrained(_lowerCAmelCase ) elif is_finetuned: if dict_path: UpperCAmelCase : Dict = Dictionary.load(_lowerCAmelCase ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq UpperCAmelCase : Any = target_dict.pad_index UpperCAmelCase : Tuple = target_dict.bos_index UpperCAmelCase : Optional[int] = target_dict.eos_index UpperCAmelCase : Union[str, Any] = len(target_dict.symbols ) UpperCAmelCase : Dict = 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 ) UpperCAmelCase : List[Any] = target_dict.indices # fairseq has the <pad> and <s> switched UpperCAmelCase : List[str] = 0 UpperCAmelCase : List[str] = 1 with open(_lowerCAmelCase , '''w''' , encoding='''utf-8''' ) as vocab_handle: json.dump(_lowerCAmelCase , _lowerCAmelCase ) UpperCAmelCase : Optional[int] = WavaVecaCTCTokenizer( _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 , ) UpperCAmelCase : int = True if config.feat_extract_norm == '''layer''' else False UpperCAmelCase : int = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=_lowerCAmelCase , return_attention_mask=_lowerCAmelCase , ) UpperCAmelCase : str = WavaVecaProcessor(feature_extractor=_lowerCAmelCase , tokenizer=_lowerCAmelCase ) processor.save_pretrained(_lowerCAmelCase ) UpperCAmelCase : Union[str, Any] = WavaVecaForCTC(_lowerCAmelCase ) else: UpperCAmelCase : Dict = WavaVecaForPreTraining(_lowerCAmelCase ) if is_finetuned or is_seq_class: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[str] = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'''data''': '''/'''.join(dict_path.split('''/''' )[:-1] )} ) else: UpperCAmelCase : Optional[Any] = argparse.Namespace(task='''audio_pretraining''' ) UpperCAmelCase : List[Any] = fairseq.tasks.setup_task(_lowerCAmelCase ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : int = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] , task=_lowerCAmelCase ) UpperCAmelCase : Optional[int] = model[0].eval() recursively_load_weights(_lowerCAmelCase , _lowerCAmelCase , not is_finetuned ) hf_wavavec.save_pretrained(_lowerCAmelCase ) if __name__ == "__main__": UpperCamelCase__: Dict = 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" ) parser.add_argument( "--is_seq_class", action="store_true", help="Whether the model to convert is a fine-tuned sequence classification model or not", ) UpperCamelCase__: Any = parser.parse_args() UpperCamelCase__: int = not args.not_finetuned and not args.is_seq_class convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, is_finetuned, args.is_seq_class, )
23
0
"""simple docstring""" from typing import List, Optional, Union import torch from transformers import ( XLMRobertaTokenizer, ) from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) from .text_encoder import MultilingualCLIP snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name snake_case = """ Examples: ```py >>> from diffusers import KandinskyPipeline, KandinskyPriorPipeline >>> import torch >>> pipe_prior = KandinskyPriorPipeline.from_pretrained(\"kandinsky-community/Kandinsky-2-1-prior\") >>> pipe_prior.to(\"cuda\") >>> prompt = \"red cat, 4k photo\" >>> out = pipe_prior(prompt) >>> image_emb = out.image_embeds >>> negative_image_emb = out.negative_image_embeds >>> pipe = KandinskyPipeline.from_pretrained(\"kandinsky-community/kandinsky-2-1\") >>> pipe.to(\"cuda\") >>> image = pipe( ... prompt, ... image_embeds=image_emb, ... negative_image_embeds=negative_image_emb, ... height=768, ... width=768, ... num_inference_steps=100, ... ).images >>> image[0].save(\"cat.png\") ``` """ def lowerCamelCase__ ( lowercase , lowercase , lowercase=8 ): """simple docstring""" SCREAMING_SNAKE_CASE : Optional[int] = h // scale_factor**2 if h % scale_factor**2 != 0: new_h += 1 SCREAMING_SNAKE_CASE : List[Any] = w // scale_factor**2 if w % scale_factor**2 != 0: new_w += 1 return new_h * scale_factor, new_w * scale_factor class SCREAMING_SNAKE_CASE ( lowerCAmelCase ): '''simple docstring''' def __init__( self : Optional[Any] , UpperCAmelCase_ : MultilingualCLIP , UpperCAmelCase_ : XLMRobertaTokenizer , UpperCAmelCase_ : UNetaDConditionModel , UpperCAmelCase_ : Union[DDIMScheduler, DDPMScheduler] , UpperCAmelCase_ : VQModel , ): super().__init__() self.register_modules( text_encoder=UpperCAmelCase_ , tokenizer=UpperCAmelCase_ , unet=UpperCAmelCase_ , scheduler=UpperCAmelCase_ , movq=UpperCAmelCase_ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = 2 ** (len(self.movq.config.block_out_channels ) - 1) def _A ( self : Optional[int] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : str , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Dict ): if latents is None: SCREAMING_SNAKE_CASE : str = randn_tensor(UpperCAmelCase_ , generator=UpperCAmelCase_ , device=UpperCAmelCase_ , dtype=UpperCAmelCase_ ) else: if latents.shape != shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {shape}''' ) SCREAMING_SNAKE_CASE : Tuple = latents.to(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = latents * scheduler.init_noise_sigma return latents def _A ( self : List[Any] , UpperCAmelCase_ : int , UpperCAmelCase_ : str , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Any=None , ): SCREAMING_SNAKE_CASE : Union[str, Any] = len(UpperCAmelCase_ ) if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) else 1 # get prompt text embeddings SCREAMING_SNAKE_CASE : Optional[Any] = self.tokenizer( UpperCAmelCase_ , padding="max_length" , truncation=UpperCAmelCase_ , max_length=77 , return_attention_mask=UpperCAmelCase_ , add_special_tokens=UpperCAmelCase_ , return_tensors="pt" , ) SCREAMING_SNAKE_CASE : Optional[int] = text_inputs.input_ids SCREAMING_SNAKE_CASE : Tuple = self.tokenizer(UpperCAmelCase_ , padding="longest" , return_tensors="pt" ).input_ids if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(UpperCAmelCase_ , UpperCAmelCase_ ): SCREAMING_SNAKE_CASE : Dict = self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1] ) logger.warning( "The following part of your input was truncated because CLIP can only handle sequences up to" f''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) SCREAMING_SNAKE_CASE : Optional[Any] = text_input_ids.to(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Dict = text_inputs.attention_mask.to(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Any = self.text_encoder( input_ids=UpperCAmelCase_ , attention_mask=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : int = prompt_embeds.repeat_interleave(UpperCAmelCase_ , dim=0 ) SCREAMING_SNAKE_CASE : Optional[int] = text_encoder_hidden_states.repeat_interleave(UpperCAmelCase_ , dim=0 ) SCREAMING_SNAKE_CASE : int = text_mask.repeat_interleave(UpperCAmelCase_ , dim=0 ) if do_classifier_free_guidance: SCREAMING_SNAKE_CASE : List[str] if negative_prompt is None: SCREAMING_SNAKE_CASE : str = [""] * batch_size elif type(UpperCAmelCase_ ) is not type(UpperCAmelCase_ ): raise TypeError( f'''`negative_prompt` should be the same type to `prompt`, but got {type(UpperCAmelCase_ )} !=''' f''' {type(UpperCAmelCase_ )}.''' ) elif isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): SCREAMING_SNAKE_CASE : int = [negative_prompt] elif batch_size != len(UpperCAmelCase_ ): raise ValueError( f'''`negative_prompt`: {negative_prompt} has batch size {len(UpperCAmelCase_ )}, but `prompt`:''' f''' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches''' " the batch size of `prompt`." ) else: SCREAMING_SNAKE_CASE : int = negative_prompt SCREAMING_SNAKE_CASE : Union[str, Any] = self.tokenizer( UpperCAmelCase_ , padding="max_length" , max_length=77 , truncation=UpperCAmelCase_ , return_attention_mask=UpperCAmelCase_ , add_special_tokens=UpperCAmelCase_ , return_tensors="pt" , ) SCREAMING_SNAKE_CASE : List[Any] = uncond_input.input_ids.to(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : List[str] = uncond_input.attention_mask.to(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Any = self.text_encoder( input_ids=UpperCAmelCase_ , attention_mask=UpperCAmelCase_ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method SCREAMING_SNAKE_CASE : Tuple = negative_prompt_embeds.shape[1] SCREAMING_SNAKE_CASE : Union[str, Any] = negative_prompt_embeds.repeat(1 , UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = negative_prompt_embeds.view(batch_size * num_images_per_prompt , UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Any = uncond_text_encoder_hidden_states.shape[1] SCREAMING_SNAKE_CASE : Tuple = uncond_text_encoder_hidden_states.repeat(1 , UpperCAmelCase_ , 1 ) SCREAMING_SNAKE_CASE : int = uncond_text_encoder_hidden_states.view( batch_size * num_images_per_prompt , UpperCAmelCase_ , -1 ) SCREAMING_SNAKE_CASE : str = uncond_text_mask.repeat_interleave(UpperCAmelCase_ , dim=0 ) # done duplicates # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes SCREAMING_SNAKE_CASE : str = torch.cat([negative_prompt_embeds, prompt_embeds] ) SCREAMING_SNAKE_CASE : List[Any] = torch.cat([uncond_text_encoder_hidden_states, text_encoder_hidden_states] ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.cat([uncond_text_mask, text_mask] ) return prompt_embeds, text_encoder_hidden_states, text_mask def _A ( self : List[str] , UpperCAmelCase_ : int=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("Please install accelerate via `pip install accelerate`" ) SCREAMING_SNAKE_CASE : str = torch.device(f'''cuda:{gpu_id}''' ) SCREAMING_SNAKE_CASE : List[str] = [ self.unet, self.text_encoder, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(UpperCAmelCase_ , UpperCAmelCase_ ) def _A ( self : Tuple , UpperCAmelCase_ : Optional[int]=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." ) SCREAMING_SNAKE_CASE : Tuple = torch.device(f'''cuda:{gpu_id}''' ) if self.device.type != "cpu": self.to("cpu" , silence_dtype_warnings=UpperCAmelCase_ ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) SCREAMING_SNAKE_CASE : Optional[Any] = None for cpu_offloaded_model in [self.text_encoder, self.unet, self.movq]: SCREAMING_SNAKE_CASE : Union[str, Any] = cpu_offload_with_hook(UpperCAmelCase_ , UpperCAmelCase_ , prev_module_hook=UpperCAmelCase_ ) if self.safety_checker is not None: SCREAMING_SNAKE_CASE : str = cpu_offload_with_hook(self.safety_checker , UpperCAmelCase_ , prev_module_hook=UpperCAmelCase_ ) # We'll offload the last model manually. SCREAMING_SNAKE_CASE : int = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def _A ( self : str ): if not hasattr(self.unet , "_hf_hook" ): return self.device for module in self.unet.modules(): if ( hasattr(UpperCAmelCase_ , "_hf_hook" ) and hasattr(module._hf_hook , "execution_device" ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(UpperCAmelCase_ ) def __call__( self : Any , UpperCAmelCase_ : Union[str, List[str]] , UpperCAmelCase_ : Union[torch.FloatTensor, List[torch.FloatTensor]] , UpperCAmelCase_ : Union[torch.FloatTensor, List[torch.FloatTensor]] , UpperCAmelCase_ : Optional[Union[str, List[str]]] = None , UpperCAmelCase_ : int = 512 , UpperCAmelCase_ : int = 512 , UpperCAmelCase_ : int = 100 , UpperCAmelCase_ : float = 4.0 , UpperCAmelCase_ : int = 1 , UpperCAmelCase_ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , UpperCAmelCase_ : Optional[torch.FloatTensor] = None , UpperCAmelCase_ : Optional[str] = "pil" , UpperCAmelCase_ : bool = True , ): if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): SCREAMING_SNAKE_CASE : Any = 1 elif isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = len(UpperCAmelCase_ ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(UpperCAmelCase_ )}''' ) SCREAMING_SNAKE_CASE : str = self._execution_device SCREAMING_SNAKE_CASE : Tuple = batch_size * num_images_per_prompt SCREAMING_SNAKE_CASE : Dict = guidance_scale > 1.0 SCREAMING_SNAKE_CASE : Optional[int] = self._encode_prompt( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): SCREAMING_SNAKE_CASE : Dict = torch.cat(UpperCAmelCase_ , dim=0 ) if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): SCREAMING_SNAKE_CASE : List[str] = torch.cat(UpperCAmelCase_ , dim=0 ) if do_classifier_free_guidance: SCREAMING_SNAKE_CASE : Tuple = image_embeds.repeat_interleave(UpperCAmelCase_ , dim=0 ) SCREAMING_SNAKE_CASE : int = negative_image_embeds.repeat_interleave(UpperCAmelCase_ , dim=0 ) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to( dtype=prompt_embeds.dtype , device=UpperCAmelCase_ ) self.scheduler.set_timesteps(UpperCAmelCase_ , device=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = self.scheduler.timesteps SCREAMING_SNAKE_CASE : Tuple = self.unet.config.in_channels SCREAMING_SNAKE_CASE : int = get_new_h_w(UpperCAmelCase_ , UpperCAmelCase_ , self.movq_scale_factor ) # create initial latent SCREAMING_SNAKE_CASE : List[str] = self.prepare_latents( (batch_size, num_channels_latents, height, width) , text_encoder_hidden_states.dtype , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , self.scheduler , ) for i, t in enumerate(self.progress_bar(UpperCAmelCase_ ) ): # expand the latents if we are doing classifier free guidance SCREAMING_SNAKE_CASE : str = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents SCREAMING_SNAKE_CASE : Optional[int] = {"text_embeds": prompt_embeds, "image_embeds": image_embeds} SCREAMING_SNAKE_CASE : List[str] = self.unet( sample=UpperCAmelCase_ , timestep=UpperCAmelCase_ , encoder_hidden_states=UpperCAmelCase_ , added_cond_kwargs=UpperCAmelCase_ , return_dict=UpperCAmelCase_ , )[0] if do_classifier_free_guidance: SCREAMING_SNAKE_CASE : int = noise_pred.split(latents.shape[1] , dim=1 ) SCREAMING_SNAKE_CASE : str = noise_pred.chunk(2 ) SCREAMING_SNAKE_CASE : List[str] = variance_pred.chunk(2 ) SCREAMING_SNAKE_CASE : int = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) SCREAMING_SNAKE_CASE : Tuple = 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"] ): SCREAMING_SNAKE_CASE : Union[str, Any] = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 SCREAMING_SNAKE_CASE : Dict = self.scheduler.step( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , generator=UpperCAmelCase_ , ).prev_sample # post-processing SCREAMING_SNAKE_CASE : Any = self.movq.decode(UpperCAmelCase_ , force_not_quantize=UpperCAmelCase_ )["sample"] if output_type not in ["pt", "np", "pil"]: raise ValueError(f'''Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}''' ) if output_type in ["np", "pil"]: SCREAMING_SNAKE_CASE : str = image * 0.5 + 0.5 SCREAMING_SNAKE_CASE : Tuple = image.clamp(0 , 1 ) SCREAMING_SNAKE_CASE : Optional[int] = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": SCREAMING_SNAKE_CASE : Any = self.numpy_to_pil(UpperCAmelCase_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=UpperCAmelCase_ )
352
def lowerCamelCase__ ( lowercase ): """simple docstring""" SCREAMING_SNAKE_CASE : Dict = n ** (1 / 3) return (val * val * val) == n if __name__ == "__main__": print(perfect_cube(27)) print(perfect_cube(4))
319
0
import numpy # List of input, output pairs _UpperCAmelCase : Union[str, Any] = ( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) _UpperCAmelCase : Dict = (((5_15, 22, 13), 5_55), ((61, 35, 49), 1_50)) _UpperCAmelCase : Tuple = [2, 4, 1, 5] _UpperCAmelCase : Optional[Any] = len(train_data) _UpperCAmelCase : Optional[Any] = 0.009 def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase="train" ) -> List[str]: return calculate_hypothesis_value(_UpperCAmelCase , _UpperCAmelCase ) - output( _UpperCAmelCase , _UpperCAmelCase ) def SCREAMING_SNAKE_CASE ( _UpperCAmelCase ) -> int: lowerCamelCase__ : str = 0 for i in range(len(_UpperCAmelCase ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase ) -> int: if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase ) -> Optional[int]: if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase=m ) -> Dict: lowerCamelCase__ : Union[str, Any] = 0 for i in range(_UpperCAmelCase ): if index == -1: summation_value += _error(_UpperCAmelCase ) else: summation_value += _error(_UpperCAmelCase ) * train_data[i][0][index] return summation_value def SCREAMING_SNAKE_CASE ( _UpperCAmelCase ) -> str: lowerCamelCase__ : List[Any] = summation_of_cost_derivative(_UpperCAmelCase , _UpperCAmelCase ) / m return cost_derivative_value def SCREAMING_SNAKE_CASE ( ) -> Optional[Any]: global parameter_vector # Tune these values to set a tolerance value for predicted output lowerCamelCase__ : List[str] = 0.000_002 lowerCamelCase__ : Any = 0 lowerCamelCase__ : Tuple = 0 while True: j += 1 lowerCamelCase__ : str = [0, 0, 0, 0] for i in range(0 , len(_UpperCAmelCase ) ): lowerCamelCase__ : Optional[Any] = get_cost_derivative(i - 1 ) lowerCamelCase__ : Tuple = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( _UpperCAmelCase , _UpperCAmelCase , atol=_UpperCAmelCase , rtol=_UpperCAmelCase , ): break lowerCamelCase__ : str = temp_parameter_vector print(('Number of iterations:', j) ) def SCREAMING_SNAKE_CASE ( ) -> Optional[int]: for i in range(len(_UpperCAmelCase ) ): print(('Actual output value:', output(_UpperCAmelCase , 'test' )) ) print(('Hypothesis output:', calculate_hypothesis_value(_UpperCAmelCase , 'test' )) ) if __name__ == "__main__": run_gradient_descent() print("""\nTesting gradient descent for a linear hypothesis function.\n""") test_gradient_descent()
50
import argparse import json from collections import OrderedDict import torch from huggingface_hub import cached_download, hf_hub_url from transformers import AutoImageProcessor, CvtConfig, CvtForImageClassification def SCREAMING_SNAKE_CASE ( _UpperCAmelCase ) -> int: lowerCamelCase__ : Optional[int] = [] embed.append( ( F"""cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.weight""", F"""stage{idx}.patch_embed.proj.weight""", ) ) embed.append( ( F"""cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.bias""", F"""stage{idx}.patch_embed.proj.bias""", ) ) embed.append( ( F"""cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.weight""", F"""stage{idx}.patch_embed.norm.weight""", ) ) embed.append( ( F"""cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.bias""", F"""stage{idx}.patch_embed.norm.bias""", ) ) return embed def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase ) -> Tuple: lowerCamelCase__ : Tuple = [] attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.convolution.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.conv.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.bias""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_mean""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_mean""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_var""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_var""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.num_batches_tracked""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.num_batches_tracked""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.convolution.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.conv.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.bias""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_mean""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_mean""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_var""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_var""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.num_batches_tracked""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.num_batches_tracked""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.convolution.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.conv.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.bias""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_mean""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_mean""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_var""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_var""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.num_batches_tracked""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.num_batches_tracked""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.weight""", F"""stage{idx}.blocks.{cnt}.attn.proj_q.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.bias""", F"""stage{idx}.blocks.{cnt}.attn.proj_q.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.weight""", F"""stage{idx}.blocks.{cnt}.attn.proj_k.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.bias""", F"""stage{idx}.blocks.{cnt}.attn.proj_k.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.weight""", F"""stage{idx}.blocks.{cnt}.attn.proj_v.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.bias""", F"""stage{idx}.blocks.{cnt}.attn.proj_v.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.weight""", F"""stage{idx}.blocks.{cnt}.attn.proj.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.bias""", F"""stage{idx}.blocks.{cnt}.attn.proj.bias""", ) ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.weight""", F"""stage{idx}.blocks.{cnt}.mlp.fc1.weight""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.bias""", F"""stage{idx}.blocks.{cnt}.mlp.fc1.bias""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.weight""", F"""stage{idx}.blocks.{cnt}.mlp.fc2.weight""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.bias""", F"""stage{idx}.blocks.{cnt}.mlp.fc2.bias""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.weight""", F"""stage{idx}.blocks.{cnt}.norm1.weight""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.bias""", F"""stage{idx}.blocks.{cnt}.norm1.bias""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.weight""", F"""stage{idx}.blocks.{cnt}.norm2.weight""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.bias""", F"""stage{idx}.blocks.{cnt}.norm2.bias""") ) return attention_weights def SCREAMING_SNAKE_CASE ( _UpperCAmelCase ) -> Tuple: lowerCamelCase__ : Union[str, Any] = [] token.append((F"""cvt.encoder.stages.{idx}.cls_token""", 'stage2.cls_token') ) return token def SCREAMING_SNAKE_CASE ( ) -> str: lowerCamelCase__ : str = [] head.append(('layernorm.weight', 'norm.weight') ) head.append(('layernorm.bias', 'norm.bias') ) head.append(('classifier.weight', 'head.weight') ) head.append(('classifier.bias', 'head.bias') ) return head def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) -> Optional[int]: lowerCamelCase__ : Tuple = 'imagenet-1k-id2label.json' lowerCamelCase__ : Union[str, Any] = 1000 lowerCamelCase__ : Optional[Any] = 'huggingface/label-files' lowerCamelCase__ : Any = num_labels lowerCamelCase__ : Dict = json.load(open(cached_download(hf_hub_url(_UpperCAmelCase , _UpperCAmelCase , repo_type='dataset' ) ) , 'r' ) ) lowerCamelCase__ : int = {int(_UpperCAmelCase ): v for k, v in idalabel.items()} lowerCamelCase__ : Tuple = idalabel lowerCamelCase__ : List[Any] = {v: k for k, v in idalabel.items()} lowerCamelCase__ : List[str] = CvtConfig(num_labels=_UpperCAmelCase , idalabel=_UpperCAmelCase , labelaid=_UpperCAmelCase ) # For depth size 13 (13 = 1+2+10) if cvt_model.rsplit('/' , 1 )[-1][4:6] == "13": lowerCamelCase__ : List[Any] = [1, 2, 10] # For depth size 21 (21 = 1+4+16) elif cvt_model.rsplit('/' , 1 )[-1][4:6] == "21": lowerCamelCase__ : Dict = [1, 4, 16] # For wide cvt (similar to wide-resnet) depth size 24 (w24 = 2 + 2 20) else: lowerCamelCase__ : Optional[Any] = [2, 2, 20] lowerCamelCase__ : Optional[int] = [3, 12, 16] lowerCamelCase__ : str = [192, 768, 1024] lowerCamelCase__ : Any = CvtForImageClassification(_UpperCAmelCase ) lowerCamelCase__ : Optional[Any] = AutoImageProcessor.from_pretrained('facebook/convnext-base-224-22k-1k' ) lowerCamelCase__ : Tuple = image_size lowerCamelCase__ : List[str] = torch.load(_UpperCAmelCase , map_location=torch.device('cpu' ) ) lowerCamelCase__ : Optional[int] = OrderedDict() lowerCamelCase__ : Tuple = [] for idx in range(len(config.depth ) ): if config.cls_token[idx]: lowerCamelCase__ : Optional[Any] = list_of_state_dict + cls_token(_UpperCAmelCase ) lowerCamelCase__ : str = list_of_state_dict + embeddings(_UpperCAmelCase ) for cnt in range(config.depth[idx] ): lowerCamelCase__ : str = list_of_state_dict + attention(_UpperCAmelCase , _UpperCAmelCase ) lowerCamelCase__ : int = list_of_state_dict + final() for gg in list_of_state_dict: print(_UpperCAmelCase ) for i in range(len(_UpperCAmelCase ) ): lowerCamelCase__ : str = original_weights[list_of_state_dict[i][1]] model.load_state_dict(_UpperCAmelCase ) model.save_pretrained(_UpperCAmelCase ) image_processor.save_pretrained(_UpperCAmelCase ) # Download the weights from zoo: https://1drv.ms/u/s!AhIXJn_J-blW9RzF3rMW7SsLHa8h?e=blQ0Al if __name__ == "__main__": _UpperCAmelCase : List[str] = argparse.ArgumentParser() parser.add_argument( """--cvt_model""", default="""cvt-w24""", type=str, help="""Name of the cvt model you'd like to convert.""", ) parser.add_argument( """--image_size""", default=3_84, type=int, help="""Input Image Size""", ) parser.add_argument( """--cvt_file_name""", default=R"""cvtmodels\CvT-w24-384x384-IN-22k.pth""", type=str, help="""Input Image Size""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) _UpperCAmelCase : List[str] = parser.parse_args() convert_cvt_checkpoint(args.cvt_model, args.image_size, args.cvt_file_name, args.pytorch_dump_folder_path)
50
1
"""simple docstring""" import numpy as np lowercase__ = [ ["""a""", """b""", """c""", """d""", """e"""], ["""f""", """g""", """h""", """i""", """k"""], ["""l""", """m""", """n""", """o""", """p"""], ["""q""", """r""", """s""", """t""", """u"""], ["""v""", """w""", """x""", """y""", """z"""], ] class __lowerCamelCase : '''simple docstring''' def __init__( self : List[str] ): lowerCAmelCase_ : Any = np.array(a_ ) def lowerCamelCase ( self : Optional[Any] , a_ : str ): lowerCAmelCase_ , lowerCAmelCase_ : Dict = np.where(letter == self.SQUARE ) lowerCAmelCase_ : List[Any] = np.concatenate([indexa + 1, indexa + 1] ) return indexes def lowerCamelCase ( self : Optional[int] , a_ : int , a_ : int ): lowerCAmelCase_ : Tuple = self.SQUARE[indexa - 1, indexa - 1] return letter def lowerCamelCase ( self : List[str] , a_ : str ): lowerCAmelCase_ : Optional[Any] = message.lower() lowerCAmelCase_ : int = message.replace(" " , "" ) lowerCAmelCase_ : int = message.replace("j" , "i" ) lowerCAmelCase_ : Tuple = np.empty((2, len(a_ )) ) for letter_index in range(len(a_ ) ): lowerCAmelCase_ : Union[str, Any] = self.letter_to_numbers(message[letter_index] ) lowerCAmelCase_ : List[Any] = numbers[0] lowerCAmelCase_ : Any = numbers[1] lowerCAmelCase_ : Dict = first_step.reshape(2 * len(a_ ) ) lowerCAmelCase_ : List[Any] = "" for numbers_index in range(len(a_ ) ): lowerCAmelCase_ : str = int(second_step[numbers_index * 2] ) lowerCAmelCase_ : Union[str, Any] = int(second_step[(numbers_index * 2) + 1] ) lowerCAmelCase_ : Optional[Any] = self.numbers_to_letter(a_ , a_ ) lowerCAmelCase_ : str = encoded_message + letter return encoded_message def lowerCamelCase ( self : str , a_ : str ): lowerCAmelCase_ : Tuple = message.lower() message.replace(" " , "" ) lowerCAmelCase_ : Any = np.empty(2 * len(a_ ) ) for letter_index in range(len(a_ ) ): lowerCAmelCase_ : int = self.letter_to_numbers(message[letter_index] ) lowerCAmelCase_ : Optional[Any] = numbers[0] lowerCAmelCase_ : Dict = numbers[1] lowerCAmelCase_ : List[Any] = first_step.reshape((2, len(a_ )) ) lowerCAmelCase_ : Optional[int] = "" for numbers_index in range(len(a_ ) ): lowerCAmelCase_ : int = int(second_step[0, numbers_index] ) lowerCAmelCase_ : int = int(second_step[1, numbers_index] ) lowerCAmelCase_ : List[str] = self.numbers_to_letter(a_ , a_ ) lowerCAmelCase_ : Any = decoded_message + letter return decoded_message
161
"""simple docstring""" import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation lowercase__ = logging.get_logger(__name__) lowercase__ = { """vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_config_file""": """tokenizer_config.json""", } lowercase__ = { """vocab_file""": {"""facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json"""}, """merges_file""": {"""facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt"""}, """tokenizer_config_file""": { """facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json""" }, } lowercase__ = {"""facebook/blenderbot-3B""": 128} class __lowerCamelCase ( A__ ): '''simple docstring''' a_ : Dict = VOCAB_FILES_NAMES a_ : Tuple = PRETRAINED_VOCAB_FILES_MAP a_ : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : Optional[int] = ["""input_ids""", """attention_mask"""] a_ : int = BlenderbotTokenizer def __init__( self : Optional[Any] , a_ : Union[str, Any]=None , a_ : Any=None , a_ : int=None , a_ : str="replace" , a_ : Tuple="<s>" , a_ : Optional[int]="</s>" , a_ : Union[str, Any]="</s>" , a_ : Union[str, Any]="<s>" , a_ : Optional[Any]="<unk>" , a_ : str="<pad>" , a_ : List[Any]="<mask>" , a_ : Tuple=False , a_ : Dict=True , **a_ : str , ): super().__init__( a_ , a_ , tokenizer_file=a_ , errors=a_ , bos_token=a_ , eos_token=a_ , sep_token=a_ , cls_token=a_ , unk_token=a_ , pad_token=a_ , mask_token=a_ , add_prefix_space=a_ , trim_offsets=a_ , **a_ , ) lowerCAmelCase_ : Any = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , a_ ) != add_prefix_space: lowerCAmelCase_ : str = getattr(a_ , pre_tok_state.pop("type" ) ) lowerCAmelCase_ : int = add_prefix_space lowerCAmelCase_ : List[Any] = pre_tok_class(**a_ ) lowerCAmelCase_ : Any = add_prefix_space lowerCAmelCase_ : str = "post_processor" lowerCAmelCase_ : str = getattr(self.backend_tokenizer , a_ , a_ ) if tokenizer_component_instance: lowerCAmelCase_ : str = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: lowerCAmelCase_ : Dict = tuple(state["sep"] ) if "cls" in state: lowerCAmelCase_ : Optional[int] = tuple(state["cls"] ) lowerCAmelCase_ : Optional[int] = False if state.get("add_prefix_space" , a_ ) != add_prefix_space: lowerCAmelCase_ : List[str] = add_prefix_space lowerCAmelCase_ : Any = True if state.get("trim_offsets" , a_ ) != trim_offsets: lowerCAmelCase_ : int = trim_offsets lowerCAmelCase_ : List[str] = True if changes_to_apply: lowerCAmelCase_ : Optional[Any] = getattr(a_ , state.pop("type" ) ) lowerCAmelCase_ : Tuple = component_class(**a_ ) setattr(self.backend_tokenizer , a_ , a_ ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def lowerCamelCase ( self : int ): if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def lowerCamelCase ( self : int , a_ : List[Any] ): lowerCAmelCase_ : Optional[Any] = AddedToken(a_ , lstrip=a_ , rstrip=a_ ) if isinstance(a_ , a_ ) else value lowerCAmelCase_ : Tuple = value def lowerCamelCase ( self : int , *a_ : List[str] , **a_ : Optional[int] ): lowerCAmelCase_ : Tuple = kwargs.get("is_split_into_words" , a_ ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*a_ , **a_ ) def lowerCamelCase ( self : str , *a_ : Union[str, Any] , **a_ : List[str] ): lowerCAmelCase_ : Tuple = kwargs.get("is_split_into_words" , a_ ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*a_ , **a_ ) def lowerCamelCase ( self : int , a_ : str , a_ : Optional[str] = None ): lowerCAmelCase_ : str = self._tokenizer.model.save(a_ , name=a_ ) return tuple(a_ ) def lowerCamelCase ( self : int , a_ : List[int] , a_ : Optional[List[int]] = None ): lowerCAmelCase_ : Optional[int] = [self.sep_token_id] lowerCAmelCase_ : Tuple = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def lowerCamelCase ( self : List[Any] , a_ : List[int] , a_ : Optional[List[int]] = None ): return token_ids_a + [self.eos_token_id] def lowerCamelCase ( self : Union[str, Any] , a_ : "Conversation" ): lowerCAmelCase_ : List[str] = [] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(" " + text ) else: # Generated responses should contain them already. inputs.append(a_ ) lowerCAmelCase_ : Tuple = " ".join(a_ ) lowerCAmelCase_ : Any = self.encode(a_ ) if len(a_ ) > self.model_max_length: lowerCAmelCase_ : Optional[Any] = input_ids[-self.model_max_length :] logger.warning(f'''Trimmed input from conversation as it was longer than {self.model_max_length} tokens.''' ) return input_ids
161
1
import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer UpperCAmelCase_ = logging.get_logger(__name__) UpperCAmelCase_ = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} UpperCAmelCase_ = { """vocab_file""": { """facebook/dpr-ctx_encoder-single-nq-base""": ( """https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt""" ), """facebook/dpr-ctx_encoder-multiset-base""": ( """https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt""" ), }, """tokenizer_file""": { """facebook/dpr-ctx_encoder-single-nq-base""": ( """https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json""" ), """facebook/dpr-ctx_encoder-multiset-base""": ( """https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json""" ), }, } UpperCAmelCase_ = { """vocab_file""": { """facebook/dpr-question_encoder-single-nq-base""": ( """https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt""" ), """facebook/dpr-question_encoder-multiset-base""": ( """https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt""" ), }, """tokenizer_file""": { """facebook/dpr-question_encoder-single-nq-base""": ( """https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json""" ), """facebook/dpr-question_encoder-multiset-base""": ( """https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json""" ), }, } UpperCAmelCase_ = { """vocab_file""": { """facebook/dpr-reader-single-nq-base""": ( """https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt""" ), """facebook/dpr-reader-multiset-base""": ( """https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt""" ), }, """tokenizer_file""": { """facebook/dpr-reader-single-nq-base""": ( """https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json""" ), """facebook/dpr-reader-multiset-base""": ( """https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json""" ), }, } UpperCAmelCase_ = { """facebook/dpr-ctx_encoder-single-nq-base""": 512, """facebook/dpr-ctx_encoder-multiset-base""": 512, } UpperCAmelCase_ = { """facebook/dpr-question_encoder-single-nq-base""": 512, """facebook/dpr-question_encoder-multiset-base""": 512, } UpperCAmelCase_ = { """facebook/dpr-reader-single-nq-base""": 512, """facebook/dpr-reader-multiset-base""": 512, } UpperCAmelCase_ = { """facebook/dpr-ctx_encoder-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-ctx_encoder-multiset-base""": {"""do_lower_case""": True}, } UpperCAmelCase_ = { """facebook/dpr-question_encoder-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-question_encoder-multiset-base""": {"""do_lower_case""": True}, } UpperCAmelCase_ = { """facebook/dpr-reader-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-reader-multiset-base""": {"""do_lower_case""": True}, } class lowerCamelCase__( UpperCAmelCase__): UpperCAmelCase__ : Any = VOCAB_FILES_NAMES UpperCAmelCase__ : Any = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ : int = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ : Union[str, Any] = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCAmelCase__ : int = DPRContextEncoderTokenizer class lowerCamelCase__( UpperCAmelCase__): UpperCAmelCase__ : Any = VOCAB_FILES_NAMES UpperCAmelCase__ : Any = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ : Union[str, Any] = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ : List[str] = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCAmelCase__ : Optional[Any] = DPRQuestionEncoderTokenizer UpperCAmelCase_ = collections.namedtuple( 'DPRSpanPrediction', ['span_score', 'relevance_score', 'doc_id', 'start_index', 'end_index', 'text'] ) UpperCAmelCase_ = collections.namedtuple('DPRReaderOutput', ['start_logits', 'end_logits', 'relevance_logits']) UpperCAmelCase_ = r""" Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`. It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers), using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)` with the format: [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids> Args: questions (`str` or `List[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. titles (`str` or `List[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. texts (`str` or `List[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different lengths). truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`): Activates and controls truncation. Accepts the following values: - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). max_length (`int`, *optional*): Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors instead of list of python integers. Acceptable values are: - `'tf'`: Return TensorFlow `tf.constant` objects. - `'pt'`: Return PyTorch `torch.Tensor` objects. - `'np'`: Return Numpy `np.ndarray` objects. return_attention_mask (`bool`, *optional*): Whether or not to return the attention mask. If not set, will return the attention mask according to the specific tokenizer's default, defined by the `return_outputs` attribute. [What are attention masks?](../glossary#attention-mask) Return: `Dict[str, List[List[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. """ @add_start_docstrings(UpperCAmelCase__) class lowerCamelCase__: def __call__( self: Optional[Any] , UpperCamelCase_: Optional[int] , UpperCamelCase_: Optional[int] = None , UpperCamelCase_: List[str] = None , UpperCamelCase_: Optional[Any] = False , UpperCamelCase_: str = False , UpperCamelCase_: Optional[Any] = None , UpperCamelCase_: List[str] = None , UpperCamelCase_: Union[str, Any] = None , **UpperCamelCase_: Dict , ): if titles is None and texts is None: return super().__call__( _a , padding=_a , truncation=_a , max_length=_a , return_tensors=_a , return_attention_mask=_a , **_a , ) elif titles is None or texts is None: __lowerCamelCase = titles if texts is None else texts return super().__call__( _a , _a , padding=_a , truncation=_a , max_length=_a , return_tensors=_a , return_attention_mask=_a , **_a , ) __lowerCamelCase = titles if not isinstance(_a , _a ) else [titles] __lowerCamelCase = texts if not isinstance(_a , _a ) else [texts] __lowerCamelCase = len(_a ) __lowerCamelCase = questions if not isinstance(_a , _a ) else [questions] * n_passages assert len(_a ) == len( _a ), F'There should be as many titles than texts but got {len(_a )} titles and {len(_a )} texts.' __lowerCamelCase = super().__call__(_a , _a , padding=_a , truncation=_a )["""input_ids"""] __lowerCamelCase = super().__call__(_a , add_special_tokens=_a , padding=_a , truncation=_a )["""input_ids"""] __lowerCamelCase = { """input_ids""": [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(_a , _a ) ] } if return_attention_mask is not False: __lowerCamelCase = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) __lowerCamelCase = attention_mask return self.pad(_a , padding=_a , max_length=_a , return_tensors=_a ) def lowerCAmelCase__ ( self: Any , UpperCamelCase_: int , UpperCamelCase_: List[Any] , UpperCamelCase_: List[str] = 16 , UpperCamelCase_: int = 64 , UpperCamelCase_: Optional[int] = 4 , ): __lowerCamelCase = reader_input["""input_ids"""] __lowerCamelCase, __lowerCamelCase, __lowerCamelCase = reader_output[:3] __lowerCamelCase = len(_a ) __lowerCamelCase = sorted(range(_a ) , reverse=_a , key=relevance_logits.__getitem__ ) __lowerCamelCase = [] for doc_id in sorted_docs: __lowerCamelCase = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence __lowerCamelCase = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: __lowerCamelCase = sequence_ids.index(self.pad_token_id ) else: __lowerCamelCase = len(_a ) __lowerCamelCase = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=_a , top_spans=_a , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=_a , start_index=_a , end_index=_a , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(_a ) >= num_spans: break return nbest_spans_predictions[:num_spans] def lowerCAmelCase__ ( self: Dict , UpperCamelCase_: Optional[Any] , UpperCamelCase_: Optional[int] , UpperCamelCase_: Optional[int] , UpperCamelCase_: List[str] , ): __lowerCamelCase = [] for start_index, start_score in enumerate(_a ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) __lowerCamelCase = sorted(_a , key=lambda UpperCamelCase_ : x[1] , reverse=_a ) __lowerCamelCase = [] for (start_index, end_index), score in scores: assert start_index <= end_index, F'Wrong span indices: [{start_index}:{end_index}]' __lowerCamelCase = end_index - start_index + 1 assert length <= max_answer_length, F'Span is too long: {length} > {max_answer_length}' if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(_a ) == top_spans: break return chosen_span_intervals @add_end_docstrings(UpperCAmelCase__) class lowerCamelCase__( UpperCAmelCase__ , UpperCAmelCase__): UpperCAmelCase__ : Optional[Any] = VOCAB_FILES_NAMES UpperCAmelCase__ : str = READER_PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ : List[str] = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ : List[str] = READER_PRETRAINED_INIT_CONFIGURATION UpperCAmelCase__ : Optional[int] = ['input_ids', 'attention_mask'] UpperCAmelCase__ : Union[str, Any] = DPRReaderTokenizer
12
"""simple docstring""" import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = process lowerCamelCase = params def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" lowerCamelCase = self.dataset[i] lowerCamelCase = self.process(_a , **self.params ) return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a , _a=None ): """simple docstring""" lowerCamelCase = loader lowerCamelCase = infer lowerCamelCase = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether lowerCamelCase = None lowerCamelCase = loader_batch_size # Internal bookkeeping lowerCamelCase = None lowerCamelCase = None def __len__( self ): """simple docstring""" return len(self.loader ) def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) return self def _lowerCAmelCase ( self ): """simple docstring""" if isinstance(self._loader_batch_data , torch.Tensor ): # Batch data is simple tensor, just fetch the slice lowerCamelCase = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) lowerCamelCase = {} for k, element in self._loader_batch_data.items(): if isinstance(_a , _a ): # Convert ModelOutput to tuple first lowerCamelCase = element.to_tuple() if isinstance(element[0] , torch.Tensor ): lowerCamelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowerCamelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(_a , _a ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0] , torch.Tensor ): lowerCamelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowerCamelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around lowerCamelCase = None elif isinstance(element[self._loader_batch_index] , torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowerCamelCase = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index] , np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowerCamelCase = np.expand_dims(element[self._loader_batch_index] , 0 ) else: # This is typically a list, so no need to `unsqueeze`. lowerCamelCase = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 lowerCamelCase = self._loader_batch_data.__class__(_a ) self._loader_batch_index += 1 return result def _lowerCAmelCase ( self ): """simple docstring""" if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch lowerCamelCase = next(self.iterator ) lowerCamelCase = self.infer(_a , **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(_a , torch.Tensor ): lowerCamelCase = processed else: lowerCamelCase = list(processed.keys() )[0] lowerCamelCase = processed[key] if isinstance(_a , _a ): lowerCamelCase = len(_a ) else: lowerCamelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowerCamelCase = observed_batch_size # Setting internal index to unwrap the batch lowerCamelCase = processed lowerCamelCase = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a , _a=None ): """simple docstring""" super().__init__(_a , _a , _a ) def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) lowerCamelCase = None return self def _lowerCAmelCase ( self ): """simple docstring""" if self.subiterator is None: lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) try: # Try to return next item lowerCamelCase = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) lowerCamelCase = next(self.subiterator ) return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) return self def _lowerCAmelCase ( self ): """simple docstring""" # Extremely similar to PipelineIterator in its unpacking mechanism # BUT, we have an extra required item which is the presence of `is_last` # That is because everything is flattened by `PipelineChunkIterator` we # need to keep track of how to regroup here in the original `process` # boundaries so that `process` and `postprocess` see the same data. # This iterator accumulates items (possibly while unbatching) until it # its a `is_last` and then just passes it on to the caller. lowerCamelCase = False lowerCamelCase = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: lowerCamelCase = self.loader_batch_item() lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) if is_last: return accumulator while not is_last: lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) if self.loader_batch_size is not None: if isinstance(_a , torch.Tensor ): lowerCamelCase = processed else: lowerCamelCase = list(processed.keys() )[0] lowerCamelCase = processed[key] if isinstance(_a , _a ): lowerCamelCase = len(_a ) else: lowerCamelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowerCamelCase = observed_batch_size lowerCamelCase = processed lowerCamelCase = 0 while self._loader_batch_index < self.loader_batch_size: lowerCamelCase = self.loader_batch_item() lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) if is_last: return accumulator else: lowerCamelCase = processed lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) return accumulator class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = key def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" return self.dataset[i][self.key] class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = keya lowerCamelCase = keya def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
291
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCamelCase : Dict = logging.get_logger(__name__) __UpperCamelCase : Optional[Any] = { '''EleutherAI/gpt-neox-20b''': '''https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/config.json''', # See all GPTNeoX models at https://huggingface.co/models?filter=gpt_neox } class a ( a__ ): snake_case__ = '''gpt_neox''' def __init__( self , _snake_case=5_04_32 , _snake_case=61_44 , _snake_case=44 , _snake_case=64 , _snake_case=2_45_76 , _snake_case="gelu" , _snake_case=0.25 , _snake_case=1_00_00 , _snake_case=0.0 , _snake_case=0.0 , _snake_case=0.1 , _snake_case=20_48 , _snake_case=0.02 , _snake_case=1E-5 , _snake_case=True , _snake_case=0 , _snake_case=2 , _snake_case=False , _snake_case=True , _snake_case=None , **_snake_case , ): """simple docstring""" super().__init__(bos_token_id=_snake_case , eos_token_id=_snake_case , **_snake_case ) lowerCAmelCase = vocab_size lowerCAmelCase = max_position_embeddings lowerCAmelCase = hidden_size lowerCAmelCase = num_hidden_layers lowerCAmelCase = num_attention_heads lowerCAmelCase = intermediate_size lowerCAmelCase = hidden_act lowerCAmelCase = rotary_pct lowerCAmelCase = rotary_emb_base lowerCAmelCase = attention_dropout lowerCAmelCase = hidden_dropout lowerCAmelCase = classifier_dropout lowerCAmelCase = initializer_range lowerCAmelCase = layer_norm_eps lowerCAmelCase = use_cache lowerCAmelCase = tie_word_embeddings lowerCAmelCase = use_parallel_residual lowerCAmelCase = rope_scaling self._rope_scaling_validation() if self.hidden_size % self.num_attention_heads != 0: raise ValueError( 'The hidden size is not divisble by the number of attention heads! Make sure to update them!' ) def UpperCamelCase__ ( self ): """simple docstring""" if self.rope_scaling is None: return if not isinstance(self.rope_scaling , _snake_case ) or len(self.rope_scaling ) != 2: raise ValueError( '`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, ' F'got {self.rope_scaling}' ) lowerCAmelCase = self.rope_scaling.get('type' , _snake_case ) lowerCAmelCase = self.rope_scaling.get('factor' , _snake_case ) if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: raise ValueError( F'`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}' ) if rope_scaling_factor is None or not isinstance(_snake_case , _snake_case ) or rope_scaling_factor <= 1.0: raise ValueError(F'`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}' )
309
"""simple docstring""" import numpy as np import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () __UpperCamelCase : List[Any] = np.linspace(start=0, stop=75, num=75, endpoint=True, retstep=False) # Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). __UpperCamelCase : str = [0, 25, 50] __UpperCamelCase : int = [25, 50, 75] __UpperCamelCase : str = fuzz.membership.trimf(X, abca) __UpperCamelCase : Tuple = fuzz.membership.trimf(X, abca) # Compute the different operations using inbuilt functions. __UpperCamelCase : Dict = np.ones(75) __UpperCamelCase : str = np.zeros((75,)) # 1. Union = max(µA(x), µB(x)) __UpperCamelCase : Optional[Any] = fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) __UpperCamelCase : Dict = fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) __UpperCamelCase : Dict = fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) __UpperCamelCase : List[str] = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] __UpperCamelCase : List[str] = young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) __UpperCamelCase : Tuple = young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] __UpperCamelCase : Union[str, Any] = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] __UpperCamelCase : Dict = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot(). from matplotlib import pyplot as plt plt.figure() plt.subplot(4, 3, 1) plt.plot(X, young) plt.title('''Young''') plt.grid(True) plt.subplot(4, 3, 2) plt.plot(X, middle_aged) plt.title('''Middle aged''') plt.grid(True) plt.subplot(4, 3, 3) plt.plot(X, union) plt.title('''union''') plt.grid(True) plt.subplot(4, 3, 4) plt.plot(X, intersection) plt.title('''intersection''') plt.grid(True) plt.subplot(4, 3, 5) plt.plot(X, complement_a) plt.title('''complement_a''') plt.grid(True) plt.subplot(4, 3, 6) plt.plot(X, difference) plt.title('''difference a/b''') plt.grid(True) plt.subplot(4, 3, 7) plt.plot(X, alg_sum) plt.title('''alg_sum''') plt.grid(True) plt.subplot(4, 3, 8) plt.plot(X, alg_product) plt.title('''alg_product''') plt.grid(True) plt.subplot(4, 3, 9) plt.plot(X, bdd_sum) plt.title('''bdd_sum''') plt.grid(True) plt.subplot(4, 3, 10) plt.plot(X, bdd_difference) plt.title('''bdd_difference''') plt.grid(True) plt.subplots_adjust(hspace=0.5) plt.show()
309
1
'''simple docstring''' from __future__ import annotations from fractions import Fraction def __a(SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[int] ): '''simple docstring''' return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def __a(SCREAMING_SNAKE_CASE_ : int ): '''simple docstring''' _lowerCAmelCase = [] _lowerCAmelCase = 11 _lowerCAmelCase = int("1" + "0" * digit_len ) for num in range(__lowerCAmelCase , __lowerCAmelCase ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(__lowerCAmelCase , __lowerCAmelCase ): solutions.append(F'''{num}/{den}''' ) den += 1 num += 1 _lowerCAmelCase = 10 return solutions def __a(SCREAMING_SNAKE_CASE_ : Tuple = 2 ): '''simple docstring''' _lowerCAmelCase = 1.0 for fraction in fraction_list(__lowerCAmelCase ): _lowerCAmelCase = Fraction(__lowerCAmelCase ) result *= frac.denominator / frac.numerator return int(__lowerCAmelCase ) if __name__ == "__main__": print(solution())
158
import json import logging import os import sys from pathlib import Path import finetune_rag from transformers.file_utils import is_apex_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, require_ray, require_torch_gpu, require_torch_multi_gpu, ) logging.basicConfig(level=logging.DEBUG) _a = logging.getLogger() _a = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class __lowerCamelCase ( snake_case__): """simple docstring""" def UpperCamelCase ( self , UpperCAmelCase ): """simple docstring""" os.makedirs(UpperCAmelCase , exist_ok=UpperCAmelCase ) _UpperCAmelCase = {'source': 'What is love ?', 'target': 'life'} _UpperCAmelCase = {'train': 12, 'val': 2, 'test': 2} for split in ["train", "test", "val"]: for field in ["source", "target"]: _UpperCAmelCase = '\n'.join([contents[field]] * n_lines[split] ) with open(os.path.join(UpperCAmelCase , F"""{split}.{field}""" ) , 'w' ) as f: f.write(UpperCAmelCase ) def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase = "pytorch" ): """simple docstring""" _UpperCAmelCase = self.get_auto_remove_tmp_dir() _UpperCAmelCase = os.path.join(UpperCAmelCase , 'output' ) _UpperCAmelCase = os.path.join(UpperCAmelCase , 'data' ) self._create_dummy_data(data_dir=UpperCAmelCase ) _UpperCAmelCase = F""" --data_dir {data_dir} \ --output_dir {output_dir} \ --model_name_or_path facebook/rag-sequence-base \ --model_type rag_sequence \ --do_train \ --do_predict \ --n_val -1 \ --val_check_interval 1.0 \ --train_batch_size 2 \ --eval_batch_size 1 \ --max_source_length 25 \ --max_target_length 25 \ --val_max_target_length 25 \ --test_max_target_length 25 \ --label_smoothing 0.1 \ --dropout 0.1 \ --attention_dropout 0.1 \ --weight_decay 0.001 \ --adam_epsilon 1e-08 \ --max_grad_norm 0.1 \ --lr_scheduler polynomial \ --learning_rate 3e-04 \ --num_train_epochs 1 \ --warmup_steps 4 \ --gradient_accumulation_steps 1 \ --distributed-port 8787 \ --use_dummy_dataset 1 \ --distributed_retriever {distributed_retriever} \ """.split() if gpus > 0: testargs.append(F"""--gpus={gpus}""" ) if is_apex_available(): testargs.append('--fp16' ) else: testargs.append('--gpus=0' ) testargs.append('--distributed_backend=ddp_cpu' ) testargs.append('--num_processes=2' ) _UpperCAmelCase = [sys.executable, str(Path(finetune_rag.__file__ ).resolve() )] + testargs execute_subprocess_async(UpperCAmelCase , env=self.get_env() ) _UpperCAmelCase = os.path.join(UpperCAmelCase , 'metrics.json' ) with open(UpperCAmelCase ) as f: _UpperCAmelCase = json.load(UpperCAmelCase ) return result @require_torch_gpu def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self._run_finetune(gpus=1 ) self.assertGreaterEqual(result['test'][0]['test_avg_em'] , 0.2 ) @require_torch_multi_gpu def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self._run_finetune(gpus=2 ) self.assertGreaterEqual(result['test'][0]['test_avg_em'] , 0.2 ) @require_torch_gpu @require_ray def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self._run_finetune(gpus=1 , distributed_retriever='ray' ) self.assertGreaterEqual(result['test'][0]['test_avg_em'] , 0.2 ) @require_torch_multi_gpu @require_ray def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self._run_finetune(gpus=1 , distributed_retriever='ray' ) self.assertGreaterEqual(result['test'][0]['test_avg_em'] , 0.2 )
39
0
def lowerCAmelCase( __lowerCamelCase , __lowerCamelCase ): __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()
197
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available lowerCamelCase_ : Dict = {"""configuration_speech_encoder_decoder""": ["""SpeechEncoderDecoderConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ : Optional[Any] = ["""SpeechEncoderDecoderModel"""] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ : Dict = ["""FlaxSpeechEncoderDecoderModel"""] if TYPE_CHECKING: from .configuration_speech_encoder_decoder import SpeechEncoderDecoderConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_encoder_decoder import SpeechEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_speech_encoder_decoder import FlaxSpeechEncoderDecoderModel else: import sys lowerCamelCase_ : Optional[int] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
197
1
from math import asin, atan, cos, radians, sin, sqrt, tan _A = 6_378_137.0 _A = 6_356_752.314_245 _A = 6_378_137 def __UpperCamelCase ( _A , _A , _A , _A ): lowerCAmelCase_ = (AXIS_A - AXIS_B) / AXIS_A lowerCAmelCase_ = atan((1 - flattening) * tan(radians(_A ) ) ) lowerCAmelCase_ = atan((1 - flattening) * tan(radians(_A ) ) ) lowerCAmelCase_ = radians(_A ) lowerCAmelCase_ = radians(_A ) # Equation lowerCAmelCase_ = sin((phi_a - phi_a) / 2 ) lowerCAmelCase_ = sin((lambda_a - lambda_a) / 2 ) # Square both values sin_sq_phi *= sin_sq_phi sin_sq_lambda *= sin_sq_lambda lowerCAmelCase_ = sqrt(sin_sq_phi + (cos(_A ) * cos(_A ) * sin_sq_lambda) ) return 2 * RADIUS * asin(_A ) if __name__ == "__main__": import doctest doctest.testmod()
278
def __UpperCamelCase ( _A = 1000000 ): lowerCAmelCase_ = 1 lowerCAmelCase_ = 1 lowerCAmelCase_ = {1: 1} for inputa in range(2 , _A ): lowerCAmelCase_ = 0 lowerCAmelCase_ = inputa while True: if number in counters: counter += counters[number] break if number % 2 == 0: number //= 2 counter += 1 else: lowerCAmelCase_ = (3 * number) + 1 counter += 1 if inputa not in counters: lowerCAmelCase_ = counter if counter > pre_counter: lowerCAmelCase_ = inputa lowerCAmelCase_ = counter return largest_number if __name__ == "__main__": print(solution(int(input().strip())))
278
1
import argparse import os import shutil from pathlib import Path import onnx import torch from packaging import version from torch.onnx import export from diffusers import OnnxRuntimeModel, OnnxStableDiffusionPipeline, StableDiffusionPipeline __A = version.parse(version.parse(torch.__version__).base_version) < version.parse('1.11') def __A ( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase=False , ): '''simple docstring''' output_path.parent.mkdir(parents=_lowercase , exist_ok=_lowercase ) # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11, # so we check the torch version for backwards compatibility if is_torch_less_than_1_11: export( _lowercase , _lowercase , f=output_path.as_posix() , input_names=_lowercase , output_names=_lowercase , dynamic_axes=_lowercase , do_constant_folding=_lowercase , use_external_data_format=_lowercase , enable_onnx_checker=_lowercase , opset_version=_lowercase , ) else: export( _lowercase , _lowercase , f=output_path.as_posix() , input_names=_lowercase , output_names=_lowercase , dynamic_axes=_lowercase , do_constant_folding=_lowercase , opset_version=_lowercase , ) @torch.no_grad() def __A ( _lowercase , _lowercase , _lowercase , _lowercase = False ): '''simple docstring''' _A = torch.floataa if fpaa else torch.floataa if fpaa and torch.cuda.is_available(): _A = '''cuda''' elif fpaa and not torch.cuda.is_available(): raise ValueError('''`float16` model export is only supported on GPUs with CUDA''' ) else: _A = '''cpu''' _A = StableDiffusionPipeline.from_pretrained(_lowercase , torch_dtype=_lowercase ).to(_lowercase ) _A = Path(_lowercase ) # TEXT ENCODER _A = pipeline.text_encoder.config.max_position_embeddings _A = pipeline.text_encoder.config.hidden_size _A = pipeline.tokenizer( '''A sample prompt''' , padding='''max_length''' , max_length=pipeline.tokenizer.model_max_length , truncation=_lowercase , return_tensors='''pt''' , ) onnx_export( pipeline.text_encoder , model_args=(text_input.input_ids.to(device=_lowercase , dtype=torch.intaa )) , output_path=output_path / '''text_encoder''' / '''model.onnx''' , ordered_input_names=['''input_ids'''] , output_names=['''last_hidden_state''', '''pooler_output'''] , dynamic_axes={ '''input_ids''': {0: '''batch''', 1: '''sequence'''}, } , opset=_lowercase , ) del pipeline.text_encoder # UNET _A = pipeline.unet.config.in_channels _A = pipeline.unet.config.sample_size _A = output_path / '''unet''' / '''model.onnx''' onnx_export( pipeline.unet , model_args=( torch.randn(2 , _lowercase , _lowercase , _lowercase ).to(device=_lowercase , dtype=_lowercase ), torch.randn(2 ).to(device=_lowercase , dtype=_lowercase ), torch.randn(2 , _lowercase , _lowercase ).to(device=_lowercase , dtype=_lowercase ), False, ) , output_path=_lowercase , ordered_input_names=['''sample''', '''timestep''', '''encoder_hidden_states''', '''return_dict'''] , output_names=['''out_sample'''] , dynamic_axes={ '''sample''': {0: '''batch''', 1: '''channels''', 2: '''height''', 3: '''width'''}, '''timestep''': {0: '''batch'''}, '''encoder_hidden_states''': {0: '''batch''', 1: '''sequence'''}, } , opset=_lowercase , use_external_data_format=_lowercase , ) _A = str(unet_path.absolute().as_posix() ) _A = os.path.dirname(_lowercase ) _A = onnx.load(_lowercase ) # clean up existing tensor files shutil.rmtree(_lowercase ) os.mkdir(_lowercase ) # collate external tensor files into one onnx.save_model( _lowercase , _lowercase , save_as_external_data=_lowercase , all_tensors_to_one_file=_lowercase , location='''weights.pb''' , convert_attribute=_lowercase , ) del pipeline.unet # VAE ENCODER _A = pipeline.vae _A = vae_encoder.config.in_channels _A = vae_encoder.config.sample_size # need to get the raw tensor output (sample) from the encoder _A = lambda _lowercase , _lowercase : vae_encoder.encode(_lowercase , _lowercase )[0].sample() onnx_export( _lowercase , model_args=( torch.randn(1 , _lowercase , _lowercase , _lowercase ).to(device=_lowercase , dtype=_lowercase ), False, ) , output_path=output_path / '''vae_encoder''' / '''model.onnx''' , ordered_input_names=['''sample''', '''return_dict'''] , output_names=['''latent_sample'''] , dynamic_axes={ '''sample''': {0: '''batch''', 1: '''channels''', 2: '''height''', 3: '''width'''}, } , opset=_lowercase , ) # VAE DECODER _A = pipeline.vae _A = vae_decoder.config.latent_channels _A = vae_decoder.config.out_channels # forward only through the decoder part _A = vae_encoder.decode onnx_export( _lowercase , model_args=( torch.randn(1 , _lowercase , _lowercase , _lowercase ).to(device=_lowercase , dtype=_lowercase ), False, ) , output_path=output_path / '''vae_decoder''' / '''model.onnx''' , ordered_input_names=['''latent_sample''', '''return_dict'''] , output_names=['''sample'''] , dynamic_axes={ '''latent_sample''': {0: '''batch''', 1: '''channels''', 2: '''height''', 3: '''width'''}, } , opset=_lowercase , ) del pipeline.vae # SAFETY CHECKER if pipeline.safety_checker is not None: _A = pipeline.safety_checker _A = safety_checker.config.vision_config.num_channels _A = safety_checker.config.vision_config.image_size _A = safety_checker.forward_onnx onnx_export( pipeline.safety_checker , model_args=( torch.randn( 1 , _lowercase , _lowercase , _lowercase , ).to(device=_lowercase , dtype=_lowercase ), torch.randn(1 , _lowercase , _lowercase , _lowercase ).to(device=_lowercase , dtype=_lowercase ), ) , output_path=output_path / '''safety_checker''' / '''model.onnx''' , ordered_input_names=['''clip_input''', '''images'''] , output_names=['''out_images''', '''has_nsfw_concepts'''] , dynamic_axes={ '''clip_input''': {0: '''batch''', 1: '''channels''', 2: '''height''', 3: '''width'''}, '''images''': {0: '''batch''', 1: '''height''', 2: '''width''', 3: '''channels'''}, } , opset=_lowercase , ) del pipeline.safety_checker _A = OnnxRuntimeModel.from_pretrained(output_path / '''safety_checker''' ) _A = pipeline.feature_extractor else: _A = None _A = None _A = OnnxStableDiffusionPipeline( vae_encoder=OnnxRuntimeModel.from_pretrained(output_path / '''vae_encoder''' ) , vae_decoder=OnnxRuntimeModel.from_pretrained(output_path / '''vae_decoder''' ) , text_encoder=OnnxRuntimeModel.from_pretrained(output_path / '''text_encoder''' ) , tokenizer=pipeline.tokenizer , unet=OnnxRuntimeModel.from_pretrained(output_path / '''unet''' ) , scheduler=pipeline.scheduler , safety_checker=_lowercase , feature_extractor=_lowercase , requires_safety_checker=safety_checker is not None , ) onnx_pipeline.save_pretrained(_lowercase ) print('''ONNX pipeline saved to''' , _lowercase ) del pipeline del onnx_pipeline _A = OnnxStableDiffusionPipeline.from_pretrained(_lowercase , provider='''CPUExecutionProvider''' ) print('''ONNX pipeline is loadable''' ) if __name__ == "__main__": __A = argparse.ArgumentParser() parser.add_argument( '--model_path', type=str, required=True, help='Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).', ) parser.add_argument('--output_path', type=str, required=True, help='Path to the output model.') parser.add_argument( '--opset', default=14, type=int, help='The version of the ONNX operator set to use.', ) parser.add_argument('--fp16', action='store_true', default=False, help='Export the models in `float16` mode') __A = parser.parse_args() convert_models(args.model_path, args.output_path, args.opset, args.fpaa)
363
import argparse import os import numpy as np import tensorflow as tf import torch from transformers import BertModel def __A ( _lowercase , _lowercase , _lowercase ): '''simple docstring''' _A = ('''dense.weight''', '''attention.self.query''', '''attention.self.key''', '''attention.self.value''') _A = ( ('''layer.''', '''layer_'''), ('''word_embeddings.weight''', '''word_embeddings'''), ('''position_embeddings.weight''', '''position_embeddings'''), ('''token_type_embeddings.weight''', '''token_type_embeddings'''), ('''.''', '''/'''), ('''LayerNorm/weight''', '''LayerNorm/gamma'''), ('''LayerNorm/bias''', '''LayerNorm/beta'''), ('''weight''', '''kernel'''), ) if not os.path.isdir(_lowercase ): os.makedirs(_lowercase ) _A = model.state_dict() def to_tf_var_name(_lowercase ): for patt, repl in iter(_lowercase ): _A = name.replace(_lowercase , _lowercase ) return f"""bert/{name}""" def create_tf_var(_lowercase , _lowercase , _lowercase ): _A = tf.dtypes.as_dtype(tensor.dtype ) _A = tf.get_variable(dtype=_lowercase , shape=tensor.shape , name=_lowercase , initializer=tf.zeros_initializer() ) session.run(tf.variables_initializer([tf_var] ) ) session.run(_lowercase ) return tf_var tf.reset_default_graph() with tf.Session() as session: for var_name in state_dict: _A = to_tf_var_name(_lowercase ) _A = state_dict[var_name].numpy() if any(x in var_name for x in tensors_to_transpose ): _A = torch_tensor.T _A = create_tf_var(tensor=_lowercase , name=_lowercase , session=_lowercase ) tf.keras.backend.set_value(_lowercase , _lowercase ) _A = session.run(_lowercase ) print(f"""Successfully created {tf_name}: {np.allclose(_lowercase , _lowercase )}""" ) _A = tf.train.Saver(tf.trainable_variables() ) saver.save(_lowercase , os.path.join(_lowercase , model_name.replace('''-''' , '''_''' ) + '''.ckpt''' ) ) def __A ( _lowercase=None ): '''simple docstring''' _A = argparse.ArgumentParser() parser.add_argument('''--model_name''' , type=_lowercase , required=_lowercase , help='''model name e.g. bert-base-uncased''' ) parser.add_argument( '''--cache_dir''' , type=_lowercase , default=_lowercase , required=_lowercase , help='''Directory containing pytorch model''' ) parser.add_argument('''--pytorch_model_path''' , type=_lowercase , required=_lowercase , help='''/path/to/<pytorch-model-name>.bin''' ) parser.add_argument('''--tf_cache_dir''' , type=_lowercase , required=_lowercase , help='''Directory in which to save tensorflow model''' ) _A = parser.parse_args(_lowercase ) _A = BertModel.from_pretrained( pretrained_model_name_or_path=args.model_name , state_dict=torch.load(args.pytorch_model_path ) , cache_dir=args.cache_dir , ) convert_pytorch_checkpoint_to_tf(model=_lowercase , ckpt_dir=args.tf_cache_dir , model_name=args.model_name ) if __name__ == "__main__": main()
75
0
import operator as op def A ( _SCREAMING_SNAKE_CASE ) -> Union[str, Any]: lowerCamelCase : Any = [] lowerCamelCase : List[str] = lambda _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE : int(x / y ) # noqa: E731 integer division operation lowerCamelCase : Tuple = { "^": op.pow, "*": op.mul, "/": div, "+": op.add, "-": op.sub, } # operators & their respective operation # print table header print("Symbol".center(8 ) ,"Action".center(12 ) ,"Stack" ,sep=" | " ) print("-" * (30 + len(_SCREAMING_SNAKE_CASE )) ) for x in post_fix: if x.isdigit(): # if x in digit stack.append(_SCREAMING_SNAKE_CASE ) # append x to stack # output in tabular format print(x.rjust(8 ) ,("push(" + x + ")").ljust(12 ) ,",".join(_SCREAMING_SNAKE_CASE ) ,sep=" | " ) else: lowerCamelCase : Optional[int] = stack.pop() # pop stack # output in tabular format print("".rjust(8 ) ,("pop(" + b + ")").ljust(12 ) ,",".join(_SCREAMING_SNAKE_CASE ) ,sep=" | " ) lowerCamelCase : int = stack.pop() # pop stack # output in tabular format print("".rjust(8 ) ,("pop(" + a + ")").ljust(12 ) ,",".join(_SCREAMING_SNAKE_CASE ) ,sep=" | " ) stack.append( str(opr[x](int(_SCREAMING_SNAKE_CASE ) ,int(_SCREAMING_SNAKE_CASE ) ) ) ) # evaluate the 2 values popped from stack & push result to stack # output in tabular format print( x.rjust(8 ) ,("push(" + a + x + b + ")").ljust(12 ) ,",".join(_SCREAMING_SNAKE_CASE ) ,sep=" | " ,) return int(stack[0] ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ : Tuple = input('\n\nEnter a Postfix Equation (space separated) = ').split(' ') print('\n\tResult = ', solve(Postfix))
48
import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(lowerCAmelCase__ ) , """Tatoeba directory does not exist.""" ) class UpperCamelCase__ (unittest.TestCase ): '''simple docstring''' @cached_property def _lowercase ( self ) -> int: lowerCamelCase : str = tempfile.mkdtemp() return TatoebaConverter(save_dir=UpperCamelCase__ ) @slow def _lowercase ( self ) -> List[Any]: self.resolver.convert_models(["heb-eng"] ) @slow def _lowercase ( self ) -> Tuple: lowerCamelCase , lowerCamelCase : Dict = self.resolver.write_model_card("opus-mt-he-en" , dry_run=UpperCamelCase__ ) assert mmeta["long_pair"] == "heb-eng"
48
1
from dataclasses import dataclass, field from typing import Optional @dataclass class SCREAMING_SNAKE_CASE_ : __lowerCAmelCase = field( default="""codeparrot/codeparrot""" , metadata={"""help""": """Model name or path of model to be trained."""} ) __lowerCAmelCase = field( default="""./""" , metadata={"""help""": """Save dir where model repo is cloned and models updates are saved to."""} ) __lowerCAmelCase = field( default="""codeparrot/codeparrot-clean-train""" , metadata={"""help""": """Name or path of training dataset."""} ) __lowerCAmelCase = field( default="""codeparrot/codeparrot-clean-valid""" , metadata={"""help""": """Name or path of validation dataset."""} ) __lowerCAmelCase = field(default=2 , metadata={"""help""": """Batch size for training."""} ) __lowerCAmelCase = field(default=2 , metadata={"""help""": """Batch size for evaluation."""} ) __lowerCAmelCase = field(default=0.1 , metadata={"""help""": """Value of weight decay."""} ) __lowerCAmelCase = field( default=10_000 , metadata={"""help""": """Size of buffer used to shuffle streaming dataset."""} ) __lowerCAmelCase = field(default=2e-4 , metadata={"""help""": """Learning rate fo training."""} ) __lowerCAmelCase = field(default="""cosine""" , metadata={"""help""": """Learning rate."""} ) __lowerCAmelCase = field( default=750 , metadata={"""help""": """Number of warmup steps in the learning rate schedule."""} ) __lowerCAmelCase = field( default=16 , metadata={"""help""": """Number of gradient accumulation steps."""} ) __lowerCAmelCase = field( default=__lowerCAmelCase , metadata={"""help""": """Use gradient checkpointing to reduce memory footprint."""} ) __lowerCAmelCase = field(default=50_000 , metadata={"""help""": """Maximum number of training steps."""} ) __lowerCAmelCase = field( default=-1 , metadata={"""help""": """Maximum number of evaluation steps. If -1 the full dataset is evaluated."""} ) __lowerCAmelCase = field(default=1_024 , metadata={"""help""": """Sequence lengths used for training."""} ) __lowerCAmelCase = field(default=1 , metadata={"""help""": """Training seed."""} ) __lowerCAmelCase = field( default=1_024 , metadata={"""help""": """Interval to save checkpoints. Measured as number of forward passes not training steps."""} , ) __lowerCAmelCase = field( default=__lowerCAmelCase , metadata={"""help""": """States path if the training should continue from a checkpoint folder."""} ) __lowerCAmelCase = field(default=__lowerCAmelCase , metadata={"""help""": """If True the data is pretokenized."""} ) @dataclass class SCREAMING_SNAKE_CASE_ : __lowerCAmelCase = field( default="""codeparrot/codeparrot""" , metadata={"""help""": """Model name or path of model to be evaluated."""} ) __lowerCAmelCase = field( default="""codeparrot/codeparrot-clean-valid""" , metadata={"""help""": """Name or path of validation dataset."""} ) __lowerCAmelCase = field(default=2 , metadata={"""help""": """Batch size used for evaluation."""} ) __lowerCAmelCase = field( default=-1 , metadata={"""help""": """Maximum number of evaluation steps. If -1 the full dataset is evaluated."""} ) __lowerCAmelCase = field(default=1_024 , metadata={"""help""": """Length of sequences to be evaluated."""} ) __lowerCAmelCase = field(default=1 , metadata={"""help""": """Random seed used for evaluation."""} ) @dataclass class SCREAMING_SNAKE_CASE_ : __lowerCAmelCase = field( default="""codeparrot/codeparrot""" , metadata={"""help""": """Model name or path of model to be evaluated."""} ) __lowerCAmelCase = field(default=__lowerCAmelCase , metadata={"""help""": """Number of workers used for code evaluation."""} ) __lowerCAmelCase = field( default=__lowerCAmelCase , metadata={"""help""": """The number of human-eval tasks to run. If not included all tasks are evaluated."""} , ) __lowerCAmelCase = field( default=__lowerCAmelCase , metadata={"""help""": """Sample from the language model's output distribution."""} ) __lowerCAmelCase = field(default=0.2 , metadata={"""help""": """Sampling temperature used for generation."""} ) __lowerCAmelCase = field(default=256 , metadata={"""help""": """Maximum number of newly generated tokens."""} ) __lowerCAmelCase = field(default=0 , metadata={"""help""": """Top-k parameter used for generation."""} ) __lowerCAmelCase = field(default=0.95 , metadata={"""help""": """Top-p parameter used for nucleus sampling."""} ) __lowerCAmelCase = field(default=10 , metadata={"""help""": """Number of generations to run in parallel."""} ) __lowerCAmelCase = field( default=200 , metadata={"""help""": """Number of completions to generate for each sample."""} ) __lowerCAmelCase = field(default=1 , metadata={"""help""": """Random seed used for evaluation."""} ) __lowerCAmelCase = field( default="""eval_results.json""" , metadata={"""help""": """Random seed used for evaluation."""} ) __lowerCAmelCase = field( default="""0""" , metadata={"""help""": """Allow `code_eval` to execute Python code on machine"""} ) __lowerCAmelCase = field( default=-1 , metadata={ """help""": ( """Determine which device to run the `text-generation` Pipeline on. -1 is CPU and any zero or positive""" """ number corresponds to which GPU device id to run on.""" ) } , ) @dataclass class SCREAMING_SNAKE_CASE_ : __lowerCAmelCase = field( default=__lowerCAmelCase , metadata={ """help""": """The number of CPU cores to use for parallel preprocessing. Default uses the maximum available.""" } , ) __lowerCAmelCase = field( default="""transformersbook/codeparrot""" , metadata={"""help""": """Folder or name of dataset to process."""} ) __lowerCAmelCase = field( default="""codeparrot-clean""" , metadata={"""help""": """Folder to save processed processed dataset."""} ) __lowerCAmelCase = field( default=100_000 , metadata={"""help""": """Number of files to save per JSON output file."""} ) __lowerCAmelCase = field(default="""content""" , metadata={"""help""": """Column containing text data to process."""} ) __lowerCAmelCase = field( default=1_000 , metadata={"""help""": """Maximum line length in file, otherwise file is filtered."""} ) __lowerCAmelCase = field( default=100 , metadata={"""help""": """Maximum mean line length in file, otherwise file is filtered."""} ) __lowerCAmelCase = field( default=0.25 , metadata={"""help""": """Maximum fraction of non-alphanumeric characters, otherwise file is filtered."""} ) __lowerCAmelCase = field( default=1.5 , metadata={"""help""": """Minimum character token ratio for the file, otherwise file is filtered."""} ) __lowerCAmelCase = field( default=0.7 , metadata={"""help""": """Probability for filtering config, test and uncommon files."""} ) __lowerCAmelCase = field( default="""codeparrot/codeparrot""" , metadata={"""help""": """Name or path to the tokenizer."""} , ) __lowerCAmelCase = field( default=__lowerCAmelCase , metadata={"""help""": """If True, near-duplicate samples are removed."""} ) __lowerCAmelCase = field( default=0.85 , metadata={"""help""": """Jaccard threshold for near-duplicate samples."""} ) @dataclass class SCREAMING_SNAKE_CASE_ : __lowerCAmelCase = field( default="""gpt2""" , metadata={"""help""": """Base tokenizer to build new tokenizer from."""} ) __lowerCAmelCase = field( default="""transformersbook/codeparrot-train""" , metadata={"""help""": """Dataset to train tokenizer on."""} ) __lowerCAmelCase = field(default="""content""" , metadata={"""help""": """Column containing text data to process."""} ) __lowerCAmelCase = field(default=200_000 , metadata={"""help""": """Number of examples to train tokenizer on."""} ) __lowerCAmelCase = field( default=32_768 , metadata={"""help""": """Number of examples to train the tokenizer on."""} ) __lowerCAmelCase = field(default="""codeparrot""" , metadata={"""help""": """Name of new tokenizer."""} ) __lowerCAmelCase = field(default=__lowerCAmelCase , metadata={"""help""": """Push saved tokenizer to the hub."""} ) @dataclass class SCREAMING_SNAKE_CASE_ : __lowerCAmelCase = field( default="""codeparrot/codeparrot""" , metadata={"""help""": """Name or path to the tokenizer."""} ) __lowerCAmelCase = field( default="""codeparrot/codeparrot-clean-train""" , metadata={"""help""": """Name or path to the dataset to pretokenize."""} ) __lowerCAmelCase = field( default="""tokenized-codeparrot-train""" , metadata={"""help""": """Repo name of the pretokenized data."""} ) __lowerCAmelCase = field(default=__lowerCAmelCase , metadata={"""help""": """Number of workers used for code evaluation."""} ) @dataclass class SCREAMING_SNAKE_CASE_ : __lowerCAmelCase = field( default="""gpt2-large""" , metadata={"""help""": """Configuration to use for model initialization."""} ) __lowerCAmelCase = field( default="""codeparrot/codeparrot""" , metadata={"""help""": """Tokenizer attached to model."""} ) __lowerCAmelCase = field(default="""codeparrot""" , metadata={"""help""": """Name of the created model."""} ) __lowerCAmelCase = field(default=__lowerCAmelCase , metadata={"""help""": """Push saved tokenizer to the hub."""} )
165
import argparse import re from flax.traverse_util import flatten_dict, unflatten_dict from tax import checkpoints from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model from transformers.utils import logging logging.set_verbosity_info() # should not include what is already done by the `from_pt` argument _SCREAMING_SNAKE_CASE = { """/attention/""": """/0/SelfAttention/""", """/self_attention/""": """/0/SelfAttention/""", """/encoder_decoder_attention/""": """/1/EncDecAttention/""", """value""": """v""", """query""": """q""", """key""": """k""", """out""": """o""", """pre_self_attention_layer_norm""": """0/layer_norm""", """pre_cross_attention_layer_norm""": """1/layer_norm""", """pre_attention_layer_norm""": """0/layer_norm""", # previously 1, but seems wrong """token_embedder""": """shared""", """encoder_norm""": """final_layer_norm""", """decoder_norm""": """final_layer_norm""", """relpos_bias/rel_embedding""": """block/0/layer/0/SelfAttention/relative_attention_bias/weight""", """router/router_weights/w/""": """router/classifier/""", """roer/roer_weights/w/""": """router/classifier/""", """logits_dense""": """lm_head""", } def lowercase( UpperCamelCase_ ) -> str: '''simple docstring''' # 1. in HF T5, we have block.{x}.layer.{y}. which corresponds to layer.{x} in # the original model UpperCamelCase = list(s_dict.keys() ) for key in keys: UpperCamelCase = R""".*/layers_(\d+)""" UpperCamelCase = key if re.match(UpperCamelCase_ , UpperCamelCase_ ): UpperCamelCase = re.sub(R"""layers_(\d+)""" , R"""block/\1/layer""" , UpperCamelCase_ ) UpperCamelCase = R"""(encoder|decoder)\/""" if re.match(UpperCamelCase_ , UpperCamelCase_ ): UpperCamelCase = re.match(UpperCamelCase_ , UpperCamelCase_ ).groups() if groups[0] == "encoder": UpperCamelCase = re.sub(R"""/mlp/""" , R"""/1/mlp/""" , UpperCamelCase_ ) UpperCamelCase = re.sub(R"""/pre_mlp_layer_norm/""" , R"""/1/layer_norm/""" , UpperCamelCase_ ) elif groups[0] == "decoder": UpperCamelCase = re.sub(R"""/mlp/""" , R"""/2/mlp/""" , UpperCamelCase_ ) UpperCamelCase = re.sub(R"""/pre_mlp_layer_norm/""" , R"""/2/layer_norm/""" , UpperCamelCase_ ) # 2. Convert other classic mappings for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items(): if old_key in new_key: UpperCamelCase = new_key.replace(UpperCamelCase_ , UpperCamelCase_ ) print(f"""{key} -> {new_key}""" ) UpperCamelCase = s_dict.pop(UpperCamelCase_ ) if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: UpperCamelCase = s_dict[ """encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight""" ].T if "decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: UpperCamelCase = s_dict[ """decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight""" ].T # 3. Take extra care of the EXPERTS layer for key in list(s_dict.keys() ): if "expert" in key: UpperCamelCase = s_dict[key].shape[0] UpperCamelCase = s_dict[key] for idx in range(UpperCamelCase_ ): UpperCamelCase = expert_weihts[idx] print(f"""{key} -> {key.replace("expert/" , "nested fstring" )}""" ) s_dict.pop(UpperCamelCase_ ) return s_dict _SCREAMING_SNAKE_CASE = { """NUM_ENCODER_LAYERS""": """num_layers""", """NUM_DECODER_LAYERS""": """num_decoder_layers""", """NUM_HEADS""": """num_heads""", """HEAD_DIM""": """d_kv""", """EMBED_DIM""": """d_model""", """MLP_DIM""": """d_ff""", """NUM_SELECTED_EXPERTS""": """num_selected_experts""", """NUM_ENCODER_SPARSE_LAYERS""": """num_sparse_encoder_layers""", """NUM_DECODER_SPARSE_LAYERS""": """num_sparse_decoder_layers""", """dense.MlpBlock.activations""": """feed_forward_proj""", } def lowercase( UpperCamelCase_ , UpperCamelCase_ ) -> Any: '''simple docstring''' # Convert a google style config to the hugging face fromat import regex as re with open(UpperCamelCase_ , """r""" ) as f: UpperCamelCase = f.read() UpperCamelCase = re.findall(R"""(.*) = ([0-9.]*)""" , UpperCamelCase_ ) UpperCamelCase = {} for param, value in regex_match: if param in GIN_TO_CONFIG_MAPPING and value != "": UpperCamelCase = float(UpperCamelCase_ ) if """.""" in value else int(UpperCamelCase_ ) UpperCamelCase = re.findall(R"""(.*activations) = \(\'(.*)\',\)""" , UpperCamelCase_ )[0] UpperCamelCase = str(activation[1] ) UpperCamelCase = num_experts UpperCamelCase = SwitchTransformersConfig(**UpperCamelCase_ ) return config def lowercase( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_=None , UpperCamelCase_="./" , UpperCamelCase_=8 ) -> Optional[int]: '''simple docstring''' # Initialise PyTorch model print(f"""Loading flax weights from : {flax_checkpoint_path}""" ) UpperCamelCase = checkpoints.load_tax_checkpoint(UpperCamelCase_ ) if gin_file is not None: UpperCamelCase = convert_gin_to_config(UpperCamelCase_ , UpperCamelCase_ ) else: UpperCamelCase = SwitchTransformersConfig.from_pretrained(UpperCamelCase_ ) UpperCamelCase = SwitchTransformersForConditionalGeneration(UpperCamelCase_ ) UpperCamelCase = flax_params["""target"""] UpperCamelCase = flatten_dict(UpperCamelCase_ , sep="""/""" ) UpperCamelCase = rename_keys(UpperCamelCase_ ) UpperCamelCase = unflatten_dict(UpperCamelCase_ , sep="""/""" ) # Load the flax params in the PT model load_flax_weights_in_pytorch_model(UpperCamelCase_ , UpperCamelCase_ ) print(f"""Save PyTorch model to {pytorch_dump_path}""" ) pt_model.save_pretrained(UpperCamelCase_ ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE = argparse.ArgumentParser() # Required parameters parser.add_argument( """--switch_t5x_checkpoint_path""", default=None, type=str, required=True, help=( """The config json file corresponding to the pre-trained SwitchTransformers model. \nThis specifies the""" """ model architecture. If not provided, a `gin_file` has to be provided.""" ), ) parser.add_argument( """--gin_file""", default=None, type=str, required=False, help="""Path to the gin config file. If not provided, a `config_file` has to be passed """, ) parser.add_argument( """--config_name""", default=None, type=str, required=False, help="""Config name of SwitchTransformers model.""" ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output pytorch model.""" ) parser.add_argument("""--num_experts""", default=8, type=int, required=False, help="""Number of experts""") _SCREAMING_SNAKE_CASE = parser.parse_args() convert_flax_checkpoint_to_pytorch( args.switch_tax_checkpoint_path, args.config_name, args.gin_file, args.pytorch_dump_folder_path, args.num_experts, )
165
1
from typing import List from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase = logging.get_logger(__name__) __UpperCAmelCase = { 'snap-research/efficientformer-l1-300': ( 'https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json' ), } class lowerCamelCase (_snake_case ): '''simple docstring''' _snake_case : Optional[int] = '''efficientformer''' def __init__( self , _UpperCamelCase = [3, 2, 6, 4] , _UpperCamelCase = [4_8, 9_6, 2_2_4, 4_4_8] , _UpperCamelCase = [True, True, True, True] , _UpperCamelCase = 4_4_8 , _UpperCamelCase = 3_2 , _UpperCamelCase = 4 , _UpperCamelCase = 7 , _UpperCamelCase = 5 , _UpperCamelCase = 8 , _UpperCamelCase = 4 , _UpperCamelCase = 0.0 , _UpperCamelCase = 1_6 , _UpperCamelCase = 3 , _UpperCamelCase = 3 , _UpperCamelCase = 3 , _UpperCamelCase = 2 , _UpperCamelCase = 1 , _UpperCamelCase = 0.0 , _UpperCamelCase = 1 , _UpperCamelCase = True , _UpperCamelCase = True , _UpperCamelCase = 1E-5 , _UpperCamelCase = "gelu" , _UpperCamelCase = 0.02 , _UpperCamelCase = 1E-12 , _UpperCamelCase = 2_2_4 , _UpperCamelCase = 1E-05 , **_UpperCamelCase , ) -> None: super().__init__(**_UpperCamelCase ) UpperCAmelCase_ : int = hidden_act UpperCAmelCase_ : Union[str, Any] = hidden_dropout_prob UpperCAmelCase_ : Tuple = hidden_sizes UpperCAmelCase_ : Union[str, Any] = num_hidden_layers UpperCAmelCase_ : List[str] = num_attention_heads UpperCAmelCase_ : List[Any] = initializer_range UpperCAmelCase_ : int = layer_norm_eps UpperCAmelCase_ : List[str] = patch_size UpperCAmelCase_ : Union[str, Any] = num_channels UpperCAmelCase_ : Optional[Any] = depths UpperCAmelCase_ : List[Any] = mlp_expansion_ratio UpperCAmelCase_ : List[str] = downsamples UpperCAmelCase_ : List[Any] = dim UpperCAmelCase_ : Tuple = key_dim UpperCAmelCase_ : Optional[int] = attention_ratio UpperCAmelCase_ : str = resolution UpperCAmelCase_ : Dict = pool_size UpperCAmelCase_ : Union[str, Any] = downsample_patch_size UpperCAmelCase_ : List[str] = downsample_stride UpperCAmelCase_ : List[str] = downsample_pad UpperCAmelCase_ : Any = drop_path_rate UpperCAmelCase_ : Dict = num_metaad_blocks UpperCAmelCase_ : Dict = distillation UpperCAmelCase_ : int = use_layer_scale UpperCAmelCase_ : Any = layer_scale_init_value UpperCAmelCase_ : Any = image_size UpperCAmelCase_ : Dict = batch_norm_eps
29
import contextlib import csv import json import os import sqlitea import tarfile import textwrap import zipfile import pyarrow as pa import pyarrow.parquet as pq import pytest import datasets import datasets.config @pytest.fixture(scope='session' ) def lowercase__ ( ): '''simple docstring''' UpperCAmelCase_ : Tuple = 10 UpperCAmelCase_ : Tuple = datasets.Features( { 'tokens': datasets.Sequence(datasets.Value('string' ) ), 'labels': datasets.Sequence(datasets.ClassLabel(names=['negative', 'positive'] ) ), 'answers': datasets.Sequence( { 'text': datasets.Value('string' ), 'answer_start': datasets.Value('int32' ), } ), 'id': datasets.Value('int64' ), } ) UpperCAmelCase_ : Tuple = datasets.Dataset.from_dict( { 'tokens': [['foo'] * 5] * n, 'labels': [[1] * 5] * n, 'answers': [{'answer_start': [97], 'text': ['1976']}] * 10, 'id': list(range(__snake_case ) ), } , features=__snake_case , ) return dataset @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Optional[Any] , __snake_case : List[str] ): '''simple docstring''' UpperCAmelCase_ : str = str(tmp_path_factory.mktemp('data' ) / 'file.arrow' ) dataset.map(cache_file_name=__snake_case ) return filename # FILE_CONTENT + files __UpperCAmelCase = '\\n Text data.\n Second line of data.' @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Optional[Any] ): '''simple docstring''' UpperCAmelCase_ : Optional[int] = tmp_path_factory.mktemp('data' ) / 'file.txt' UpperCAmelCase_ : Tuple = FILE_CONTENT with open(__snake_case , 'w' ) as f: f.write(__snake_case ) return filename @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : List[str] ): '''simple docstring''' import bza UpperCAmelCase_ : Optional[int] = tmp_path_factory.mktemp('data' ) / 'file.txt.bz2' UpperCAmelCase_ : str = bytes(__snake_case , 'utf-8' ) with bza.open(__snake_case , 'wb' ) as f: f.write(__snake_case ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Any ): '''simple docstring''' import gzip UpperCAmelCase_ : Optional[Any] = str(tmp_path_factory.mktemp('data' ) / 'file.txt.gz' ) UpperCAmelCase_ : Dict = bytes(__snake_case , 'utf-8' ) with gzip.open(__snake_case , 'wb' ) as f: f.write(__snake_case ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Dict ): '''simple docstring''' if datasets.config.LZ4_AVAILABLE: import lza.frame UpperCAmelCase_ : Any = tmp_path_factory.mktemp('data' ) / 'file.txt.lz4' UpperCAmelCase_ : Any = bytes(__snake_case , 'utf-8' ) with lza.frame.open(__snake_case , 'wb' ) as f: f.write(__snake_case ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Tuple , __snake_case : List[Any] ): '''simple docstring''' if datasets.config.PY7ZR_AVAILABLE: import pyazr UpperCAmelCase_ : Optional[Any] = tmp_path_factory.mktemp('data' ) / 'file.txt.7z' with pyazr.SevenZipFile(__snake_case , 'w' ) as archive: archive.write(__snake_case , arcname=os.path.basename(__snake_case ) ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : List[str] , __snake_case : List[Any] ): '''simple docstring''' import tarfile UpperCAmelCase_ : Any = tmp_path_factory.mktemp('data' ) / 'file.txt.tar' with tarfile.TarFile(__snake_case , 'w' ) as f: f.add(__snake_case , arcname=os.path.basename(__snake_case ) ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : str ): '''simple docstring''' import lzma UpperCAmelCase_ : Union[str, Any] = tmp_path_factory.mktemp('data' ) / 'file.txt.xz' UpperCAmelCase_ : Any = bytes(__snake_case , 'utf-8' ) with lzma.open(__snake_case , 'wb' ) as f: f.write(__snake_case ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Optional[int] , __snake_case : Optional[Any] ): '''simple docstring''' import zipfile UpperCAmelCase_ : int = tmp_path_factory.mktemp('data' ) / 'file.txt.zip' with zipfile.ZipFile(__snake_case , 'w' ) as f: f.write(__snake_case , arcname=os.path.basename(__snake_case ) ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Optional[Any] ): '''simple docstring''' if datasets.config.ZSTANDARD_AVAILABLE: import zstandard as zstd UpperCAmelCase_ : Tuple = tmp_path_factory.mktemp('data' ) / 'file.txt.zst' UpperCAmelCase_ : List[str] = bytes(__snake_case , 'utf-8' ) with zstd.open(__snake_case , 'wb' ) as f: f.write(__snake_case ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Any ): '''simple docstring''' UpperCAmelCase_ : Union[str, Any] = tmp_path_factory.mktemp('data' ) / 'file.xml' UpperCAmelCase_ : List[Any] = textwrap.dedent( '\\n <?xml version="1.0" encoding="UTF-8" ?>\n <tmx version="1.4">\n <header segtype="sentence" srclang="ca" />\n <body>\n <tu>\n <tuv xml:lang="ca"><seg>Contingut 1</seg></tuv>\n <tuv xml:lang="en"><seg>Content 1</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang="ca"><seg>Contingut 2</seg></tuv>\n <tuv xml:lang="en"><seg>Content 2</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang="ca"><seg>Contingut 3</seg></tuv>\n <tuv xml:lang="en"><seg>Content 3</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang="ca"><seg>Contingut 4</seg></tuv>\n <tuv xml:lang="en"><seg>Content 4</seg></tuv>\n </tu>\n <tu>\n <tuv xml:lang="ca"><seg>Contingut 5</seg></tuv>\n <tuv xml:lang="en"><seg>Content 5</seg></tuv>\n </tu>\n </body>\n </tmx>' ) with open(__snake_case , 'w' ) as f: f.write(__snake_case ) return filename __UpperCAmelCase = [ {'col_1': '0', 'col_2': 0, 'col_3': 0.0}, {'col_1': '1', 'col_2': 1, 'col_3': 1.0}, {'col_1': '2', 'col_2': 2, 'col_3': 2.0}, {'col_1': '3', 'col_2': 3, 'col_3': 3.0}, ] __UpperCAmelCase = [ {'col_1': '4', 'col_2': 4, 'col_3': 4.0}, {'col_1': '5', 'col_2': 5, 'col_3': 5.0}, ] __UpperCAmelCase = { 'col_1': ['0', '1', '2', '3'], 'col_2': [0, 1, 2, 3], 'col_3': [0.0, 1.0, 2.0, 3.0], } __UpperCAmelCase = [ {'col_3': 0.0, 'col_1': '0', 'col_2': 0}, {'col_3': 1.0, 'col_1': '1', 'col_2': 1}, ] __UpperCAmelCase = [ {'col_1': 's0', 'col_2': 0, 'col_3': 0.0}, {'col_1': 's1', 'col_2': 1, 'col_3': 1.0}, {'col_1': 's2', 'col_2': 2, 'col_3': 2.0}, {'col_1': 's3', 'col_2': 3, 'col_3': 3.0}, ] @pytest.fixture(scope='session' ) def lowercase__ ( ): '''simple docstring''' return DATA_DICT_OF_LISTS @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : int ): '''simple docstring''' UpperCAmelCase_ : List[Any] = datasets.Dataset.from_dict(__snake_case ) UpperCAmelCase_ : Optional[Any] = str(tmp_path_factory.mktemp('data' ) / 'dataset.arrow' ) dataset.map(cache_file_name=__snake_case ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : List[Any] ): '''simple docstring''' UpperCAmelCase_ : Optional[int] = str(tmp_path_factory.mktemp('data' ) / 'dataset.sqlite' ) with contextlib.closing(sqlitea.connect(__snake_case ) ) as con: UpperCAmelCase_ : List[Any] = con.cursor() cur.execute('CREATE TABLE dataset(col_1 text, col_2 int, col_3 real)' ) for item in DATA: cur.execute('INSERT INTO dataset(col_1, col_2, col_3) VALUES (?, ?, ?)' , tuple(item.values() ) ) con.commit() return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Union[str, Any] ): '''simple docstring''' UpperCAmelCase_ : Optional[Any] = str(tmp_path_factory.mktemp('data' ) / 'dataset.csv' ) with open(__snake_case , 'w' , newline='' ) as f: UpperCAmelCase_ : Tuple = csv.DictWriter(__snake_case , fieldnames=['col_1', 'col_2', 'col_3'] ) writer.writeheader() for item in DATA: writer.writerow(__snake_case ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Optional[Any] ): '''simple docstring''' UpperCAmelCase_ : Tuple = str(tmp_path_factory.mktemp('data' ) / 'dataset2.csv' ) with open(__snake_case , 'w' , newline='' ) as f: UpperCAmelCase_ : Optional[Any] = csv.DictWriter(__snake_case , fieldnames=['col_1', 'col_2', 'col_3'] ) writer.writeheader() for item in DATA: writer.writerow(__snake_case ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : str , __snake_case : Any ): '''simple docstring''' import bza UpperCAmelCase_ : int = tmp_path_factory.mktemp('data' ) / 'dataset.csv.bz2' with open(__snake_case , 'rb' ) as f: UpperCAmelCase_ : int = f.read() # data = bytes(FILE_CONTENT, "utf-8") with bza.open(__snake_case , 'wb' ) as f: f.write(__snake_case ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : List[str] , __snake_case : Tuple , __snake_case : Optional[Any] ): '''simple docstring''' UpperCAmelCase_ : List[Any] = tmp_path_factory.mktemp('data' ) / 'dataset.csv.zip' with zipfile.ZipFile(__snake_case , 'w' ) as f: f.write(__snake_case , arcname=os.path.basename(__snake_case ) ) f.write(__snake_case , arcname=os.path.basename(__snake_case ) ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : str , __snake_case : Optional[int] , __snake_case : Tuple ): '''simple docstring''' UpperCAmelCase_ : List[Any] = tmp_path_factory.mktemp('data' ) / 'dataset.csv.zip' with zipfile.ZipFile(__snake_case , 'w' ) as f: f.write(__snake_case , arcname=os.path.basename(csv_path.replace('.csv' , '.CSV' ) ) ) f.write(__snake_case , arcname=os.path.basename(csva_path.replace('.csv' , '.CSV' ) ) ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Tuple , __snake_case : int , __snake_case : str ): '''simple docstring''' UpperCAmelCase_ : Optional[Any] = tmp_path_factory.mktemp('data' ) / 'dataset_with_dir.csv.zip' with zipfile.ZipFile(__snake_case , 'w' ) as f: f.write(__snake_case , arcname=os.path.join('main_dir' , os.path.basename(__snake_case ) ) ) f.write(__snake_case , arcname=os.path.join('main_dir' , os.path.basename(__snake_case ) ) ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Union[str, Any] ): '''simple docstring''' UpperCAmelCase_ : int = str(tmp_path_factory.mktemp('data' ) / 'dataset.parquet' ) UpperCAmelCase_ : Dict = pa.schema( { 'col_1': pa.string(), 'col_2': pa.intaa(), 'col_3': pa.floataa(), } ) with open(__snake_case , 'wb' ) as f: UpperCAmelCase_ : List[Any] = pq.ParquetWriter(__snake_case , schema=__snake_case ) UpperCAmelCase_ : Any = pa.Table.from_pydict({k: [DATA[i][k] for i in range(len(__snake_case ) )] for k in DATA[0]} , schema=__snake_case ) writer.write_table(__snake_case ) writer.close() return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Union[str, Any] ): '''simple docstring''' UpperCAmelCase_ : Tuple = str(tmp_path_factory.mktemp('data' ) / 'dataset.json' ) UpperCAmelCase_ : Optional[int] = {'data': DATA} with open(__snake_case , 'w' ) as f: json.dump(__snake_case , __snake_case ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : List[Any] ): '''simple docstring''' UpperCAmelCase_ : Tuple = str(tmp_path_factory.mktemp('data' ) / 'dataset.json' ) UpperCAmelCase_ : Tuple = {'data': DATA_DICT_OF_LISTS} with open(__snake_case , 'w' ) as f: json.dump(__snake_case , __snake_case ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Optional[Any] ): '''simple docstring''' UpperCAmelCase_ : Dict = str(tmp_path_factory.mktemp('data' ) / 'dataset.jsonl' ) with open(__snake_case , 'w' ) as f: for item in DATA: f.write(json.dumps(__snake_case ) + '\n' ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : str ): '''simple docstring''' UpperCAmelCase_ : Union[str, Any] = str(tmp_path_factory.mktemp('data' ) / 'dataset2.jsonl' ) with open(__snake_case , 'w' ) as f: for item in DATA: f.write(json.dumps(__snake_case ) + '\n' ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : int ): '''simple docstring''' UpperCAmelCase_ : int = str(tmp_path_factory.mktemp('data' ) / 'dataset_312.jsonl' ) with open(__snake_case , 'w' ) as f: for item in DATA_312: f.write(json.dumps(__snake_case ) + '\n' ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : str ): '''simple docstring''' UpperCAmelCase_ : Optional[int] = str(tmp_path_factory.mktemp('data' ) / 'dataset-str.jsonl' ) with open(__snake_case , 'w' ) as f: for item in DATA_STR: f.write(json.dumps(__snake_case ) + '\n' ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Dict , __snake_case : Dict ): '''simple docstring''' import gzip UpperCAmelCase_ : Union[str, Any] = str(tmp_path_factory.mktemp('data' ) / 'dataset.txt.gz' ) with open(__snake_case , 'rb' ) as orig_file: with gzip.open(__snake_case , 'wb' ) as zipped_file: zipped_file.writelines(__snake_case ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : int , __snake_case : Any ): '''simple docstring''' import gzip UpperCAmelCase_ : Dict = str(tmp_path_factory.mktemp('data' ) / 'dataset.jsonl.gz' ) with open(__snake_case , 'rb' ) as orig_file: with gzip.open(__snake_case , 'wb' ) as zipped_file: zipped_file.writelines(__snake_case ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Optional[Any] , __snake_case : Dict , __snake_case : Optional[int] ): '''simple docstring''' UpperCAmelCase_ : int = tmp_path_factory.mktemp('data' ) / 'dataset.jsonl.zip' with zipfile.ZipFile(__snake_case , 'w' ) as f: f.write(__snake_case , arcname=os.path.basename(__snake_case ) ) f.write(__snake_case , arcname=os.path.basename(__snake_case ) ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Optional[Any] , __snake_case : str , __snake_case : Dict , __snake_case : Union[str, Any] ): '''simple docstring''' UpperCAmelCase_ : str = tmp_path_factory.mktemp('data' ) / 'dataset_nested.jsonl.zip' with zipfile.ZipFile(__snake_case , 'w' ) as f: f.write(__snake_case , arcname=os.path.join('nested' , os.path.basename(__snake_case ) ) ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Dict , __snake_case : Union[str, Any] , __snake_case : Tuple ): '''simple docstring''' UpperCAmelCase_ : Optional[int] = tmp_path_factory.mktemp('data' ) / 'dataset_with_dir.jsonl.zip' with zipfile.ZipFile(__snake_case , 'w' ) as f: f.write(__snake_case , arcname=os.path.join('main_dir' , os.path.basename(__snake_case ) ) ) f.write(__snake_case , arcname=os.path.join('main_dir' , os.path.basename(__snake_case ) ) ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Tuple , __snake_case : str , __snake_case : Union[str, Any] ): '''simple docstring''' UpperCAmelCase_ : List[Any] = tmp_path_factory.mktemp('data' ) / 'dataset.jsonl.tar' with tarfile.TarFile(__snake_case , 'w' ) as f: f.add(__snake_case , arcname=os.path.basename(__snake_case ) ) f.add(__snake_case , arcname=os.path.basename(__snake_case ) ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : str , __snake_case : Any , __snake_case : Any , __snake_case : List[Any] ): '''simple docstring''' UpperCAmelCase_ : List[Any] = tmp_path_factory.mktemp('data' ) / 'dataset_nested.jsonl.tar' with tarfile.TarFile(__snake_case , 'w' ) as f: f.add(__snake_case , arcname=os.path.join('nested' , os.path.basename(__snake_case ) ) ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : List[Any] ): '''simple docstring''' UpperCAmelCase_ : Any = ['0', '1', '2', '3'] UpperCAmelCase_ : Dict = str(tmp_path_factory.mktemp('data' ) / 'dataset.txt' ) with open(__snake_case , 'w' ) as f: for item in data: f.write(item + '\n' ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : List[Any] ): '''simple docstring''' UpperCAmelCase_ : Optional[Any] = ['0', '1', '2', '3'] UpperCAmelCase_ : Optional[int] = str(tmp_path_factory.mktemp('data' ) / 'dataset2.txt' ) with open(__snake_case , 'w' ) as f: for item in data: f.write(item + '\n' ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Tuple ): '''simple docstring''' UpperCAmelCase_ : Dict = ['0', '1', '2', '3'] UpperCAmelCase_ : List[str] = tmp_path_factory.mktemp('data' ) / 'dataset.abc' with open(__snake_case , 'w' ) as f: for item in data: f.write(item + '\n' ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Any , __snake_case : Union[str, Any] , __snake_case : List[Any] ): '''simple docstring''' UpperCAmelCase_ : List[Any] = tmp_path_factory.mktemp('data' ) / 'dataset.text.zip' with zipfile.ZipFile(__snake_case , 'w' ) as f: f.write(__snake_case , arcname=os.path.basename(__snake_case ) ) f.write(__snake_case , arcname=os.path.basename(__snake_case ) ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Dict , __snake_case : str , __snake_case : Any ): '''simple docstring''' UpperCAmelCase_ : Union[str, Any] = tmp_path_factory.mktemp('data' ) / 'dataset_with_dir.text.zip' with zipfile.ZipFile(__snake_case , 'w' ) as f: f.write(__snake_case , arcname=os.path.join('main_dir' , os.path.basename(__snake_case ) ) ) f.write(__snake_case , arcname=os.path.join('main_dir' , os.path.basename(__snake_case ) ) ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Union[str, Any] , __snake_case : str , __snake_case : Optional[int] ): '''simple docstring''' UpperCAmelCase_ : Union[str, Any] = tmp_path_factory.mktemp('data' ) / 'dataset.ext.zip' with zipfile.ZipFile(__snake_case , 'w' ) as f: f.write(__snake_case , arcname=os.path.basename('unsupported.ext' ) ) f.write(__snake_case , arcname=os.path.basename('unsupported_2.ext' ) ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Dict ): '''simple docstring''' UpperCAmelCase_ : Tuple = '\n'.join(['First', 'Second\u2029with Unicode new line', 'Third'] ) UpperCAmelCase_ : Dict = str(tmp_path_factory.mktemp('data' ) / 'dataset_with_unicode_new_lines.txt' ) with open(__snake_case , 'w' , encoding='utf-8' ) as f: f.write(__snake_case ) return path @pytest.fixture(scope='session' ) def lowercase__ ( ): '''simple docstring''' return os.path.join('tests' , 'features' , 'data' , 'test_image_rgb.jpg' ) @pytest.fixture(scope='session' ) def lowercase__ ( ): '''simple docstring''' return os.path.join('tests' , 'features' , 'data' , 'test_audio_44100.wav' ) @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : str , __snake_case : List[str] ): '''simple docstring''' UpperCAmelCase_ : Optional[Any] = tmp_path_factory.mktemp('data' ) / 'dataset.img.zip' with zipfile.ZipFile(__snake_case , 'w' ) as f: f.write(__snake_case , arcname=os.path.basename(__snake_case ) ) f.write(__snake_case , arcname=os.path.basename(__snake_case ).replace('.jpg' , '2.jpg' ) ) return path @pytest.fixture(scope='session' ) def lowercase__ ( __snake_case : Any ): '''simple docstring''' UpperCAmelCase_ : Optional[Any] = tmp_path_factory.mktemp('data_dir' ) (data_dir / "subdir").mkdir() with open(data_dir / 'subdir' / 'train.txt' , 'w' ) as f: f.write('foo\n' * 10 ) with open(data_dir / 'subdir' / 'test.txt' , 'w' ) as f: f.write('bar\n' * 10 ) # hidden file with open(data_dir / 'subdir' / '.test.txt' , 'w' ) as f: f.write('bar\n' * 10 ) # hidden directory (data_dir / ".subdir").mkdir() with open(data_dir / '.subdir' / 'train.txt' , 'w' ) as f: f.write('foo\n' * 10 ) with open(data_dir / '.subdir' / 'test.txt' , 'w' ) as f: f.write('bar\n' * 10 ) return data_dir
29
1
from __future__ import annotations import requests UpperCamelCase__ =set( 'approved_at_utc approved_by author_flair_background_color\nauthor_flair_css_class author_flair_richtext author_flair_template_id author_fullname\nauthor_premium can_mod_post category clicked content_categories created_utc downs\nedited gilded gildings hidden hide_score is_created_from_ads_ui is_meta\nis_original_content is_reddit_media_domain is_video link_flair_css_class\nlink_flair_richtext link_flair_text link_flair_text_color media_embed mod_reason_title\nname permalink pwls quarantine saved score secure_media secure_media_embed selftext\nsubreddit subreddit_name_prefixed subreddit_type thumbnail title top_awarded_type\ntotal_awards_received ups upvote_ratio url user_reports'.split() ) def lowerCamelCase__ (__lowerCamelCase, __lowerCamelCase = 1, __lowerCamelCase = "new", __lowerCamelCase = None ): _SCREAMING_SNAKE_CASE : Dict = wanted_data or [] if invalid_search_terms := ", ".join(sorted(set(_SCREAMING_SNAKE_CASE ) - valid_terms ) ): _SCREAMING_SNAKE_CASE : Optional[int] = f"""Invalid search term: {invalid_search_terms}""" raise ValueError(_SCREAMING_SNAKE_CASE ) _SCREAMING_SNAKE_CASE : Dict = requests.get( f"""https://reddit.com/r/{subreddit}/{age}.json?limit={limit}""", headers={"User-agent": "A random string"}, ) if response.status_code == 429: raise requests.HTTPError _SCREAMING_SNAKE_CASE : int = response.json() if not wanted_data: return {id_: data["data"]["children"][id_] for id_ in range(_SCREAMING_SNAKE_CASE )} _SCREAMING_SNAKE_CASE : Union[str, Any] = {} for id_ in range(_SCREAMING_SNAKE_CASE ): _SCREAMING_SNAKE_CASE : int = { item: data["data"]["children"][id_]["data"][item] for item in wanted_data } return data_dict if __name__ == "__main__": # If you get Error 429, that means you are rate limited.Try after some time print(get_subreddit_data('learnpython', wanted_data=['title', 'url', 'selftext']))
351
import logging import os from typing import List, Tuple import numpy as np import psutil import torch import torch.distributed as dist from transformers import RagRetriever UpperCamelCase__ =logging.getLogger(__name__) class lowerCAmelCase__( __lowercase ): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase=None ) -> Optional[Any]: super().__init__( __lowerCamelCase , question_encoder_tokenizer=__lowerCamelCase , generator_tokenizer=__lowerCamelCase , index=__lowerCamelCase , init_retrieval=__lowerCamelCase , ) _SCREAMING_SNAKE_CASE : List[Any] = None def UpperCamelCase_ ( self , __lowerCamelCase ) -> Any: logger.info("initializing retrieval" ) # initializing a separate process group for retrieval as the default # nccl backend doesn't support gather/scatter operations while gloo # is too slow to replace nccl for the core gpu communication if dist.is_initialized(): logger.info("dist initialized" ) # needs to be set manually _SCREAMING_SNAKE_CASE : List[str] = self._infer_socket_ifname() # avoid clash with the NCCL port _SCREAMING_SNAKE_CASE : List[Any] = str(distributed_port + 1 ) _SCREAMING_SNAKE_CASE : int = dist.new_group(ranks=__lowerCamelCase , backend="gloo" ) # initialize retriever only on the main worker if not dist.is_initialized() or self._is_main(): logger.info("dist not initialized / main" ) self.index.init_index() # all processes wait untill the retriever is initialized by the main process if dist.is_initialized(): torch.distributed.barrier(group=self.process_group ) def UpperCamelCase_ ( self ) -> Optional[Any]: return dist.get_rank(group=self.process_group ) == 0 def UpperCamelCase_ ( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase=torch.floataa ) -> Optional[Any]: _SCREAMING_SNAKE_CASE : Optional[int] = torch.empty(__lowerCamelCase , dtype=__lowerCamelCase ) dist.scatter(__lowerCamelCase , src=0 , scatter_list=__lowerCamelCase , group=self.process_group ) return target_tensor def UpperCamelCase_ ( self ) -> Tuple: _SCREAMING_SNAKE_CASE : int = psutil.net_if_addrs() # a hacky way to deal with varying network interface names _SCREAMING_SNAKE_CASE : Any = next((addr for addr in addrs if addr.startswith("e" )) , __lowerCamelCase ) return ifname def UpperCamelCase_ ( self , __lowerCamelCase , __lowerCamelCase ) -> Tuple[np.ndarray, List[dict]]: # single GPU training if not dist.is_initialized(): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Dict = self._main_retrieve(__lowerCamelCase , __lowerCamelCase ) return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(__lowerCamelCase ) # distributed training _SCREAMING_SNAKE_CASE : Union[str, Any] = dist.get_world_size(group=self.process_group ) # gather logic _SCREAMING_SNAKE_CASE : Any = None if self._is_main(): _SCREAMING_SNAKE_CASE : Optional[Any] = [torch.empty(question_hidden_states.shape , dtype=torch.floataa ) for _ in range(__lowerCamelCase )] dist.gather(torch.tensor(__lowerCamelCase ) , dst=0 , gather_list=__lowerCamelCase , group=self.process_group ) # scatter logic _SCREAMING_SNAKE_CASE : Optional[int] = question_hidden_states.shape[0] _SCREAMING_SNAKE_CASE : Optional[Any] = [] _SCREAMING_SNAKE_CASE : Optional[int] = [] if self._is_main(): assert len(__lowerCamelCase ) == world_size _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : int = self._main_retrieve(torch.cat(__lowerCamelCase ).numpy() , __lowerCamelCase ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : List[str] = torch.tensor(__lowerCamelCase ), torch.tensor(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : List[str] = self._chunk_tensor(__lowerCamelCase , __lowerCamelCase ) _SCREAMING_SNAKE_CASE : Tuple = self._chunk_tensor(__lowerCamelCase , __lowerCamelCase ) _SCREAMING_SNAKE_CASE : Dict = self._scattered(__lowerCamelCase , [n_queries, n_docs] , target_type=torch.intaa ) _SCREAMING_SNAKE_CASE : Optional[Any] = self._scattered(__lowerCamelCase , [n_queries, n_docs, question_hidden_states.shape[1]] ) return retrieved_doc_embeds.numpy(), doc_ids.numpy(), self.index.get_doc_dicts(__lowerCamelCase )
325
0