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""" # Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def _snake_case ( snake_case__ : Optional[Any] , snake_case__ : Any , snake_case__ : Dict ): A = { 'en': 'Machine learning is great, isn\'t it?', 'ru': 'Машинное обучение - это здорово, не так ли?', 'de': 'Maschinelles Lernen ist großartig, oder?', } # BLUE scores as follows: # "pair": [fairseq, transformers] A = { 'ru-en': ['[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)', '39.20'], 'en-ru': ['[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)', '33.47'], 'en-de': ['[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)', '42.83'], 'de-en': ['[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)', '41.35'], } A = F'{src_lang}-{tgt_lang}' A = F'\n---\nlanguage: \n- {src_lang}\n- {tgt_lang}\nthumbnail:\ntags:\n- translation\n- wmt19\n- facebook\nlicense: apache-2.0\ndatasets:\n- wmt19\nmetrics:\n- bleu\n---\n\n# FSMT\n\n## Model description\n\nThis is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}.\n\nFor more details, please see, [Facebook FAIR\'s WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616).\n\nThe abbreviation FSMT stands for FairSeqMachineTranslation\n\nAll four models are available:\n\n* [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru)\n* [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en)\n* [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de)\n* [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en)\n\n## Intended uses & limitations\n\n#### How to use\n\n```python\nfrom transformers import FSMTForConditionalGeneration, FSMTTokenizer\nmname = "facebook/wmt19-{src_lang}-{tgt_lang}"\ntokenizer = FSMTTokenizer.from_pretrained(mname)\nmodel = FSMTForConditionalGeneration.from_pretrained(mname)\n\ninput = "{texts[src_lang]}"\ninput_ids = tokenizer.encode(input, return_tensors="pt")\noutputs = model.generate(input_ids)\ndecoded = tokenizer.decode(outputs[0], skip_special_tokens=True)\nprint(decoded) # {texts[tgt_lang]}\n\n```\n\n#### Limitations and bias\n\n- The original (and this ported model) doesn\'t seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981)\n\n## Training data\n\nPretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616).\n\n## Eval results\n\npair | fairseq | transformers\n-------|---------|----------\n{pair} | {scores[pair][0]} | {scores[pair][1]}\n\nThe score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn\'t support:\n- model ensemble, therefore the best performing checkpoint was ported (``model4.pt``).\n- re-ranking\n\nThe score was calculated using this code:\n\n```bash\ngit clone https://github.com/huggingface/transformers\ncd transformers\nexport PAIR={pair}\nexport DATA_DIR=data/$PAIR\nexport SAVE_DIR=data/$PAIR\nexport BS=8\nexport NUM_BEAMS=15\nmkdir -p $DATA_DIR\nsacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source\nsacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target\necho $PAIR\nPYTHONPATH="src:examples/seq2seq" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS\n```\nnote: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`.\n\n## Data Sources\n\n- [training, etc.](http://www.statmt.org/wmt19/)\n- [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561)\n\n\n### BibTeX entry and citation info\n\n```bibtex\n@inproceedings{{...,\n year={{2020}},\n title={{Facebook FAIR\'s WMT19 News Translation Task Submission}},\n author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}},\n booktitle={{Proc. of WMT}},\n}}\n```\n\n\n## TODO\n\n- port model ensemble (fairseq uses 4 model checkpoints)\n\n' os.makedirs(snake_case__ , exist_ok=snake_case__ ) A = os.path.join(snake_case__ , 'README.md' ) print(F'Generating {path}' ) with open(snake_case__ , 'w' , encoding='utf-8' ) as f: f.write(snake_case__ ) # make sure we are under the root of the project _lowercase = Path(__file__).resolve().parent.parent.parent _lowercase = repo_dir / '''model_cards''' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: _lowercase , _lowercase , _lowercase = model_name.split('''-''') _lowercase = model_cards_dir / '''facebook''' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
74
"""simple docstring""" import argparse import torch from torch import nn from transformers import MBartConfig, MBartForConditionalGeneration def _snake_case ( snake_case__ : Dict ): A = [ 'encoder.version', 'decoder.version', 'model.encoder.version', 'model.decoder.version', '_float_tensor', 'decoder.output_projection.weight', ] for k in ignore_keys: state_dict.pop(snake_case__ , snake_case__ ) def _snake_case ( snake_case__ : int ): A , A = emb.weight.shape A = nn.Linear(snake_case__ , snake_case__ , bias=snake_case__ ) A = emb.weight.data return lin_layer def _snake_case ( snake_case__ : List[str] , snake_case__ : Any="facebook/mbart-large-en-ro" , snake_case__ : Optional[int]=False , snake_case__ : List[str]=False ): A = torch.load(snake_case__ , map_location='cpu' )['model'] remove_ignore_keys_(snake_case__ ) A = state_dict['encoder.embed_tokens.weight'].shape[0] A = MBartConfig.from_pretrained(snake_case__ , vocab_size=snake_case__ ) if mbart_aa and finetuned: A = 'relu' A = state_dict['decoder.embed_tokens.weight'] A = MBartForConditionalGeneration(snake_case__ ) model.model.load_state_dict(snake_case__ ) if finetuned: A = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": _lowercase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''fairseq_path''', type=str, help='''bart.large, bart.large.cnn or a path to a model.pt on local filesystem.''' ) parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument( '''--hf_config''', default='''facebook/mbart-large-cc25''', type=str, help='''Which huggingface architecture to use: mbart-large''', ) parser.add_argument('''--mbart_50''', action='''store_true''', help='''whether the model is mMART-50 checkpoint''') parser.add_argument('''--finetuned''', action='''store_true''', help='''whether the model is a fine-tuned checkpoint''') _lowercase = parser.parse_args() _lowercase = convert_fairseq_mbart_checkpoint_from_disk( args.fairseq_path, hf_config_path=args.hf_config, finetuned=args.finetuned, mbart_aa=args.mbart_aa ) model.save_pretrained(args.pytorch_dump_folder_path)
74
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_albert import AlbertTokenizer else: lowerCamelCase__ = None lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} lowerCamelCase__ = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json''', }, } lowerCamelCase__ = { '''albert-base-v1''': 5_12, '''albert-large-v1''': 5_12, '''albert-xlarge-v1''': 5_12, '''albert-xxlarge-v1''': 5_12, '''albert-base-v2''': 5_12, '''albert-large-v2''': 5_12, '''albert-xlarge-v2''': 5_12, '''albert-xxlarge-v2''': 5_12, } lowerCamelCase__ = '''▁''' class __magic_name__ (__lowercase ): lowerCamelCase__ = VOCAB_FILES_NAMES lowerCamelCase__ = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase__ = AlbertTokenizer def __init__( self , _a=None , _a=None , _a=True , _a=True , _a=False , _a="[CLS]" , _a="[SEP]" , _a="<unk>" , _a="[SEP]" , _a="<pad>" , _a="[CLS]" , _a="[MASK]" , **_a , ) -> Tuple: # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. lowerCAmelCase_ = ( AddedToken(_a , lstrip=_a , rstrip=_a , normalized=_a ) if isinstance(_a , _a ) else mask_token ) super().__init__( _a , tokenizer_file=_a , do_lower_case=_a , remove_space=_a , keep_accents=_a , bos_token=_a , eos_token=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , **_a , ) lowerCAmelCase_ = do_lower_case lowerCAmelCase_ = remove_space lowerCAmelCase_ = keep_accents lowerCAmelCase_ = vocab_file lowerCAmelCase_ = False if not self.vocab_file else True def __a ( self , _a , _a = None ) -> List[int]: lowerCAmelCase_ = [self.sep_token_id] lowerCAmelCase_ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def __a ( self , _a , _a = None ) -> List[int]: lowerCAmelCase_ = [self.sep_token_id] lowerCAmelCase_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __a ( self , _a , _a = None ) -> Tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " "tokenizer." ) if not os.path.isdir(_a ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return lowerCAmelCase_ = os.path.join( _a , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ): copyfile(self.vocab_file , _a ) return (out_vocab_file,)
22
# Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def A(__a: Any , __a: Union[str, Any] , __a: List[str] ): lowerCAmelCase_ = { "en": "Machine learning is great, isn't it?", "ru": "Машинное обучение - это здорово, не так ли?", "de": "Maschinelles Lernen ist großartig, oder?", } # BLUE scores as follows: # "pair": [fairseq, transformers] lowerCAmelCase_ = { "ru-en": ["[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)", "39.20"], "en-ru": ["[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)", "33.47"], "en-de": ["[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)", "42.83"], "de-en": ["[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)", "41.35"], } lowerCAmelCase_ = F"{src_lang}-{tgt_lang}" lowerCAmelCase_ = F"\n---\nlanguage: \n- {src_lang}\n- {tgt_lang}\nthumbnail:\ntags:\n- translation\n- wmt19\n- facebook\nlicense: apache-2.0\ndatasets:\n- wmt19\nmetrics:\n- bleu\n---\n\n# FSMT\n\n## Model description\n\nThis is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}.\n\nFor more details, please see, [Facebook FAIR's WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616).\n\nThe abbreviation FSMT stands for FairSeqMachineTranslation\n\nAll four models are available:\n\n* [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru)\n* [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en)\n* [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de)\n* [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en)\n\n## Intended uses & limitations\n\n#### How to use\n\n```python\nfrom transformers import FSMTForConditionalGeneration, FSMTTokenizer\nmname = \"facebook/wmt19-{src_lang}-{tgt_lang}\"\ntokenizer = FSMTTokenizer.from_pretrained(mname)\nmodel = FSMTForConditionalGeneration.from_pretrained(mname)\n\ninput = \"{texts[src_lang]}\"\ninput_ids = tokenizer.encode(input, return_tensors=\"pt\")\noutputs = model.generate(input_ids)\ndecoded = tokenizer.decode(outputs[0], skip_special_tokens=True)\nprint(decoded) # {texts[tgt_lang]}\n\n```\n\n#### Limitations and bias\n\n- The original (and this ported model) doesn't seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981)\n\n## Training data\n\nPretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616).\n\n## Eval results\n\npair | fairseq | transformers\n-------|---------|----------\n{pair} | {scores[pair][0]} | {scores[pair][1]}\n\nThe score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn't support:\n- model ensemble, therefore the best performing checkpoint was ported (``model4.pt``).\n- re-ranking\n\nThe score was calculated using this code:\n\n```bash\ngit clone https://github.com/huggingface/transformers\ncd transformers\nexport PAIR={pair}\nexport DATA_DIR=data/$PAIR\nexport SAVE_DIR=data/$PAIR\nexport BS=8\nexport NUM_BEAMS=15\nmkdir -p $DATA_DIR\nsacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source\nsacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target\necho $PAIR\nPYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS\n```\nnote: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`.\n\n## Data Sources\n\n- [training, etc.](http://www.statmt.org/wmt19/)\n- [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561)\n\n\n### BibTeX entry and citation info\n\n```bibtex\n@inproceedings{{...,\n year={{2020}},\n title={{Facebook FAIR's WMT19 News Translation Task Submission}},\n author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}},\n booktitle={{Proc. of WMT}},\n}}\n```\n\n\n## TODO\n\n- port model ensemble (fairseq uses 4 model checkpoints)\n\n" os.makedirs(__a , exist_ok=__a ) lowerCAmelCase_ = os.path.join(__a , "README.md" ) print(F"Generating {path}" ) with open(__a , "w" , encoding="utf-8" ) as f: f.write(__a ) # make sure we are under the root of the project lowerCamelCase__ = Path(__file__).resolve().parent.parent.parent lowerCamelCase__ = repo_dir / '''model_cards''' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = model_name.split('''-''') lowerCamelCase__ = model_cards_dir / '''facebook''' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
22
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) UpperCAmelCase_ = { 'configuration_blip': [ 'BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BlipConfig', 'BlipTextConfig', 'BlipVisionConfig', ], 'processing_blip': ['BlipProcessor'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ = ['BlipImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ = [ 'BLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'BlipModel', 'BlipPreTrainedModel', 'BlipForConditionalGeneration', 'BlipForQuestionAnswering', 'BlipVisionModel', 'BlipTextModel', 'BlipForImageTextRetrieval', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ = [ 'TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFBlipModel', 'TFBlipPreTrainedModel', 'TFBlipForConditionalGeneration', 'TFBlipForQuestionAnswering', 'TFBlipVisionModel', 'TFBlipTextModel', 'TFBlipForImageTextRetrieval', ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys UpperCAmelCase_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
12
from pickle import UnpicklingError import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict from ..utils import logging lowerCAmelCase = logging.get_logger(__name__) def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" try: with open(SCREAMING_SNAKE_CASE , '''rb''' ) as flax_state_f: lowercase__ = from_bytes(SCREAMING_SNAKE_CASE , flax_state_f.read() ) except UnpicklingError as e: try: with open(SCREAMING_SNAKE_CASE ) as f: if f.read().startswith('''version''' ): raise OSError( '''You seem to have cloned a repository without having git-lfs installed. Please''' ''' install git-lfs and run `git lfs install` followed by `git lfs pull` in the''' ''' folder you cloned.''' ) else: raise ValueError from e except (UnicodeDecodeError, ValueError): raise EnvironmentError(f'Unable to convert {model_file} to Flax deserializable object. ' ) return load_flax_weights_in_pytorch_model(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" try: import torch # noqa: F401 except ImportError: logger.error( '''Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see''' ''' https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation''' ''' instructions.''' ) raise # check if we have bf16 weights lowercase__ = flatten_dict(jax.tree_util.tree_map(lambda SCREAMING_SNAKE_CASE : x.dtype == jnp.bfloataa , SCREAMING_SNAKE_CASE ) ).values() if any(SCREAMING_SNAKE_CASE ): # convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( '''Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` ''' '''before loading those in PyTorch model.''' ) lowercase__ = jax.tree_util.tree_map( lambda SCREAMING_SNAKE_CASE : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , SCREAMING_SNAKE_CASE ) lowercase__ = '''''' lowercase__ = flatten_dict(SCREAMING_SNAKE_CASE , sep='''.''' ) lowercase__ = pt_model.state_dict() # keep track of unexpected & missing keys lowercase__ = [] lowercase__ = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): lowercase__ = flax_key_tuple.split('''.''' ) if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4: lowercase__ = flax_key_tuple_array[:-1] + ['''weight'''] lowercase__ = jnp.transpose(SCREAMING_SNAKE_CASE , (3, 2, 0, 1) ) elif flax_key_tuple_array[-1] == "kernel": lowercase__ = flax_key_tuple_array[:-1] + ['''weight'''] lowercase__ = flax_tensor.T elif flax_key_tuple_array[-1] == "scale": lowercase__ = flax_key_tuple_array[:-1] + ['''weight'''] if "time_embedding" not in flax_key_tuple_array: for i, flax_key_tuple_string in enumerate(SCREAMING_SNAKE_CASE ): lowercase__ = ( flax_key_tuple_string.replace('''_0''' , '''.0''' ) .replace('''_1''' , '''.1''' ) .replace('''_2''' , '''.2''' ) .replace('''_3''' , '''.3''' ) .replace('''_4''' , '''.4''' ) .replace('''_5''' , '''.5''' ) .replace('''_6''' , '''.6''' ) .replace('''_7''' , '''.7''' ) .replace('''_8''' , '''.8''' ) .replace('''_9''' , '''.9''' ) ) lowercase__ = '''.'''.join(SCREAMING_SNAKE_CASE ) if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( f'Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected ' f'to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) else: # add weight to pytorch dict lowercase__ = np.asarray(SCREAMING_SNAKE_CASE ) if not isinstance(SCREAMING_SNAKE_CASE , np.ndarray ) else flax_tensor lowercase__ = torch.from_numpy(SCREAMING_SNAKE_CASE ) # remove from missing keys missing_keys.remove(SCREAMING_SNAKE_CASE ) else: # weight is not expected by PyTorch model unexpected_keys.append(SCREAMING_SNAKE_CASE ) pt_model.load_state_dict(SCREAMING_SNAKE_CASE ) # re-transform missing_keys to list lowercase__ = list(SCREAMING_SNAKE_CASE ) if len(SCREAMING_SNAKE_CASE ) > 0: logger.warning( '''Some weights of the Flax model were not used when initializing the PyTorch model''' f' {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing' f' {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture' ''' (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This''' f' IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect' ''' to be exactly identical (e.g. initializing a BertForSequenceClassification model from a''' ''' FlaxBertForSequenceClassification model).''' ) if len(SCREAMING_SNAKE_CASE ) > 0: logger.warning( f'Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly' f' initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to' ''' use it for predictions and inference.''' ) return pt_model
110
0
class snake_case__ : """simple docstring""" def __init__( self , __lowercase ) -> None: """simple docstring""" a__ : Union[str, Any] = set_counts a__ : Union[str, Any] = max(__lowercase ) a__ : List[str] = len(__lowercase ) a__ : Tuple = [1] * num_sets a__ : int = list(range(__lowercase ) ) def SCREAMING_SNAKE_CASE__( self , __lowercase , __lowercase ) -> bool: """simple docstring""" a__ : Optional[int] = self.get_parent(__lowercase ) a__ : List[Any] = self.get_parent(__lowercase ) if src_parent == dst_parent: return False if self.ranks[dst_parent] >= self.ranks[src_parent]: self.set_counts[dst_parent] += self.set_counts[src_parent] a__ : List[str] = 0 a__ : Tuple = dst_parent if self.ranks[dst_parent] == self.ranks[src_parent]: self.ranks[dst_parent] += 1 a__ : List[str] = self.set_counts[dst_parent] else: self.set_counts[src_parent] += self.set_counts[dst_parent] a__ : Dict = 0 a__ : str = src_parent a__ : Dict = self.set_counts[src_parent] a__ : Optional[int] = max(self.max_set , __lowercase ) return True def SCREAMING_SNAKE_CASE__( self , __lowercase ) -> int: """simple docstring""" if self.parents[disj_set] == disj_set: return disj_set a__ : Tuple = self.get_parent(self.parents[disj_set] ) return self.parents[disj_set]
266
import os import unittest from transformers import BatchEncoding from transformers.models.bert.tokenization_bert import ( BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.models.prophetnet.tokenization_prophetnet import VOCAB_FILES_NAMES, ProphetNetTokenizer from transformers.testing_utils import require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin class snake_case__ (A__ , unittest.TestCase ): """simple docstring""" __lowerCAmelCase :Union[str, Any] = ProphetNetTokenizer __lowerCAmelCase :Any = False def SCREAMING_SNAKE_CASE__( self ) -> Union[str, Any]: """simple docstring""" super().setUp() a__ : Optional[Any] = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest""", ] a__ : List[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 SCREAMING_SNAKE_CASE__( self , __lowercase ) -> str: """simple docstring""" a__ : Any = """UNwant\u00E9d,running""" a__ : Dict = """unwanted, running""" return input_text, output_text def SCREAMING_SNAKE_CASE__( self ) -> Dict: """simple docstring""" a__ : Tuple = self.tokenizer_class(self.vocab_file ) a__ : int = tokenizer.tokenize("""UNwant\u00E9d,running""" ) self.assertListEqual(__lowercase , ["""un""", """##want""", """##ed""", """,""", """runn""", """##ing"""] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowercase ) , [9, 6, 7, 1_2, 1_0, 1_1] ) def SCREAMING_SNAKE_CASE__( self ) -> Union[str, Any]: """simple docstring""" a__ : str = BasicTokenizer() self.assertListEqual(tokenizer.tokenize("""ah\u535A\u63A8zz""" ) , ["""ah""", """\u535A""", """\u63A8""", """zz"""] ) def SCREAMING_SNAKE_CASE__( self ) -> int: """simple docstring""" a__ : int = BasicTokenizer(do_lower_case=__lowercase ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? """ ) , ["""hello""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def SCREAMING_SNAKE_CASE__( self ) -> List[Any]: """simple docstring""" a__ : str = BasicTokenizer(do_lower_case=__lowercase , strip_accents=__lowercase ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hällo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""h\u00E9llo"""] ) def SCREAMING_SNAKE_CASE__( self ) -> str: """simple docstring""" a__ : List[str] = BasicTokenizer(do_lower_case=__lowercase , strip_accents=__lowercase ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hallo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def SCREAMING_SNAKE_CASE__( self ) -> List[Any]: """simple docstring""" a__ : Optional[Any] = BasicTokenizer(do_lower_case=__lowercase ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""hallo""", """!""", """how""", """are""", """you""", """?"""] ) self.assertListEqual(tokenizer.tokenize("""H\u00E9llo""" ) , ["""hello"""] ) def SCREAMING_SNAKE_CASE__( self ) -> Optional[int]: """simple docstring""" a__ : List[str] = BasicTokenizer(do_lower_case=__lowercase ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? """ ) , ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def SCREAMING_SNAKE_CASE__( self ) -> List[str]: """simple docstring""" a__ : str = BasicTokenizer(do_lower_case=__lowercase , strip_accents=__lowercase ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""HäLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def SCREAMING_SNAKE_CASE__( self ) -> Optional[Any]: """simple docstring""" a__ : List[str] = BasicTokenizer(do_lower_case=__lowercase , strip_accents=__lowercase ) self.assertListEqual( tokenizer.tokenize(""" \tHäLLo!how \n Are yoU? """ ) , ["""HaLLo""", """!""", """how""", """Are""", """yoU""", """?"""] ) def SCREAMING_SNAKE_CASE__( self ) -> List[Any]: """simple docstring""" a__ : Union[str, Any] = BasicTokenizer(do_lower_case=__lowercase , never_split=["""[UNK]"""] ) self.assertListEqual( tokenizer.tokenize(""" \tHeLLo!how \n Are yoU? [UNK]""" ) , ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?""", """[UNK]"""] ) def SCREAMING_SNAKE_CASE__( self ) -> Optional[Any]: """simple docstring""" a__ : Optional[Any] = ["""[UNK]""", """[CLS]""", """[SEP]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing"""] a__ : Dict = {} for i, token in enumerate(__lowercase ): a__ : Optional[Any] = i a__ : str = WordpieceTokenizer(vocab=__lowercase , unk_token="""[UNK]""" ) self.assertListEqual(tokenizer.tokenize("""""" ) , [] ) self.assertListEqual(tokenizer.tokenize("""unwanted running""" ) , ["""un""", """##want""", """##ed""", """runn""", """##ing"""] ) self.assertListEqual(tokenizer.tokenize("""unwantedX running""" ) , ["""[UNK]""", """runn""", """##ing"""] ) @require_torch def SCREAMING_SNAKE_CASE__( self ) -> List[Any]: """simple docstring""" a__ : List[Any] = self.tokenizer_class.from_pretrained("""microsoft/prophetnet-large-uncased""" ) a__ : List[Any] = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] a__ : Optional[Any] = [1_0_3_7, 2_1_4_6, 2_0_4_2_3, 2_0_0_5, 7_6_8_0, 7_8_4_9, 3_9_8_9, 1_0_1_2, 1_0_2] a__ : List[Any] = tokenizer(__lowercase , padding=__lowercase , return_tensors="""pt""" ) self.assertIsInstance(__lowercase , __lowercase ) a__ : Optional[int] = list(batch.input_ids.numpy()[0] ) self.assertListEqual(__lowercase , __lowercase ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) def SCREAMING_SNAKE_CASE__( self ) -> List[str]: """simple docstring""" self.assertTrue(_is_whitespace(""" """ ) ) self.assertTrue(_is_whitespace("""\t""" ) ) self.assertTrue(_is_whitespace("""\r""" ) ) self.assertTrue(_is_whitespace("""\n""" ) ) self.assertTrue(_is_whitespace("""\u00A0""" ) ) self.assertFalse(_is_whitespace("""A""" ) ) self.assertFalse(_is_whitespace("""-""" ) ) def SCREAMING_SNAKE_CASE__( self ) -> Optional[Any]: """simple docstring""" self.assertTrue(_is_control("""\u0005""" ) ) self.assertFalse(_is_control("""A""" ) ) self.assertFalse(_is_control(""" """ ) ) self.assertFalse(_is_control("""\t""" ) ) self.assertFalse(_is_control("""\r""" ) ) def SCREAMING_SNAKE_CASE__( self ) -> Optional[Any]: """simple docstring""" self.assertTrue(_is_punctuation("""-""" ) ) self.assertTrue(_is_punctuation("""$""" ) ) self.assertTrue(_is_punctuation("""`""" ) ) self.assertTrue(_is_punctuation(""".""" ) ) self.assertFalse(_is_punctuation("""A""" ) ) self.assertFalse(_is_punctuation(""" """ ) ) @slow def SCREAMING_SNAKE_CASE__( self ) -> Optional[int]: """simple docstring""" a__ : Optional[Any] = self.tokenizer_class.from_pretrained("""microsoft/prophetnet-large-uncased""" ) a__ : Dict = tokenizer.encode("""sequence builders""" , add_special_tokens=__lowercase ) a__ : str = tokenizer.encode("""multi-sequence build""" , add_special_tokens=__lowercase ) a__ : Optional[Any] = tokenizer.build_inputs_with_special_tokens(__lowercase ) a__ : Optional[int] = tokenizer.build_inputs_with_special_tokens(__lowercase , __lowercase ) assert encoded_sentence == text + [1_0_2] assert encoded_pair == text + [1_0_2] + text_a + [1_0_2]
266
1
"""simple docstring""" from __future__ import annotations from collections.abc import Iterator from typing import Any class _lowerCAmelCase : """simple docstring""" def __init__( self : List[str], UpperCAmelCase__ : Any ): __lowercase = data __lowercase = None class _lowerCAmelCase : """simple docstring""" def __init__( self : Union[str, Any] ): __lowercase = None __lowercase = None def __iter__( self : List[Any] ): __lowercase = self.head while self.head: yield node.data __lowercase = node.next if node == self.head: break def __len__( self : Union[str, Any] ): return sum(1 for _ in self ) def __repr__( self : List[str] ): return "->".join(str(UpperCAmelCase__ ) for item in iter(self ) ) def _lowercase ( self : Optional[int], UpperCAmelCase__ : Any ): self.insert_nth(len(self ), UpperCAmelCase__ ) def _lowercase ( self : Union[str, Any], UpperCAmelCase__ : Any ): self.insert_nth(0, UpperCAmelCase__ ) def _lowercase ( self : str, UpperCAmelCase__ : int, UpperCAmelCase__ : Any ): if index < 0 or index > len(self ): raise IndexError("list index out of range." ) __lowercase = Node(UpperCAmelCase__ ) if self.head is None: __lowercase = new_node # first node points itself __lowercase = __lowercase = new_node elif index == 0: # insert at head __lowercase = self.head __lowercase = __lowercase = new_node else: __lowercase = self.head for _ in range(index - 1 ): __lowercase = temp.next __lowercase = temp.next __lowercase = new_node if index == len(self ) - 1: # insert at tail __lowercase = new_node def _lowercase ( self : List[Any] ): return self.delete_nth(0 ) def _lowercase ( self : List[Any] ): return self.delete_nth(len(self ) - 1 ) def _lowercase ( self : List[str], UpperCAmelCase__ : int = 0 ): if not 0 <= index < len(self ): raise IndexError("list index out of range." ) __lowercase = self.head if self.head == self.tail: # just one node __lowercase = __lowercase = None elif index == 0: # delete head node __lowercase = self.tail.next.next __lowercase = self.head.next else: __lowercase = self.head for _ in range(index - 1 ): __lowercase = temp.next __lowercase = temp.next __lowercase = temp.next.next if index == len(self ) - 1: # delete at tail __lowercase = temp return delete_node.data def _lowercase ( self : Optional[Any] ): return len(self ) == 0 def _A ( ) -> None: '''simple docstring''' __lowercase = CircularLinkedList() assert len(UpperCamelCase_) == 0 assert circular_linked_list.is_empty() is True assert str(UpperCamelCase_) == "" 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(UpperCamelCase_) == i circular_linked_list.insert_nth(UpperCamelCase_, i + 1) assert str(UpperCamelCase_) == "->".join(str(UpperCamelCase_) for i in range(1, 6)) circular_linked_list.insert_tail(6) assert str(UpperCamelCase_) == "->".join(str(UpperCamelCase_) for i in range(1, 7)) circular_linked_list.insert_head(0) assert str(UpperCamelCase_) == "->".join(str(UpperCamelCase_) for i in range(0, 7)) assert circular_linked_list.delete_front() == 0 assert circular_linked_list.delete_tail() == 6 assert str(UpperCamelCase_) == "->".join(str(UpperCamelCase_) for i in range(1, 6)) assert circular_linked_list.delete_nth(2) == 3 circular_linked_list.insert_nth(2, 3) assert str(UpperCamelCase_) == "->".join(str(UpperCamelCase_) for i in range(1, 6)) assert circular_linked_list.is_empty() is False if __name__ == "__main__": import doctest doctest.testmod()
17
"""simple docstring""" def _A ( UpperCamelCase_ : list[int]) -> float: '''simple docstring''' if not nums: # Makes sure that the list is not empty raise ValueError("List is empty") __lowercase = sum(UpperCamelCase_) / len(UpperCamelCase_) # Calculate the average return sum(abs(x - average) for x in nums) / len(UpperCamelCase_) if __name__ == "__main__": import doctest doctest.testmod()
17
1
from __future__ import annotations def UpperCamelCase_( _snake_case : int = 4 ): """simple docstring""" __a =abs(_snake_case ) or 4 return [[1 + x + y * row_size for x in range(_snake_case )] for y in range(_snake_case )] def UpperCamelCase_( _snake_case : list[list[int]] ): """simple docstring""" return reverse_row(transpose(_snake_case ) ) # OR.. transpose(reverse_column(matrix)) def UpperCamelCase_( _snake_case : list[list[int]] ): """simple docstring""" return reverse_row(reverse_column(_snake_case ) ) # OR.. reverse_column(reverse_row(matrix)) def UpperCamelCase_( _snake_case : list[list[int]] ): """simple docstring""" return reverse_column(transpose(_snake_case ) ) # OR.. transpose(reverse_row(matrix)) def UpperCamelCase_( _snake_case : list[list[int]] ): """simple docstring""" __a =[list(_snake_case ) for x in zip(*_snake_case )] return matrix def UpperCamelCase_( _snake_case : list[list[int]] ): """simple docstring""" __a =matrix[::-1] return matrix def UpperCamelCase_( _snake_case : list[list[int]] ): """simple docstring""" __a =[x[::-1] for x in matrix] return matrix def UpperCamelCase_( _snake_case : list[list[int]] ): """simple docstring""" for i in matrix: print(*_snake_case ) if __name__ == "__main__": _lowerCAmelCase : Any = make_matrix() print("\norigin:\n") print_matrix(matrix) print("\nrotate 90 counterclockwise:\n") print_matrix(rotate_aa(matrix)) _lowerCAmelCase : Union[str, Any] = make_matrix() print("\norigin:\n") print_matrix(matrix) print("\nrotate 180:\n") print_matrix(rotate_aaa(matrix)) _lowerCAmelCase : Any = make_matrix() print("\norigin:\n") print_matrix(matrix) print("\nrotate 270 counterclockwise:\n") print_matrix(rotate_aaa(matrix))
308
import string # frequency taken from https://en.wikipedia.org/wiki/Letter_frequency _lowerCAmelCase : Tuple = { "E": 12.70, "T": 9.06, "A": 8.17, "O": 7.51, "I": 6.97, "N": 6.75, "S": 6.33, "H": 6.09, "R": 5.99, "D": 4.25, "L": 4.03, "C": 2.78, "U": 2.76, "M": 2.41, "W": 2.36, "F": 2.23, "G": 2.02, "Y": 1.97, "P": 1.93, "B": 1.29, "V": 0.98, "K": 0.77, "J": 0.15, "X": 0.15, "Q": 0.10, "Z": 0.07, } _lowerCAmelCase : Optional[int] = "ETAOINSHRDLCUMWFGYPBVKJXQZ" _lowerCAmelCase : Any = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" def UpperCamelCase_( _snake_case : str ): """simple docstring""" __a ={letter: 0 for letter in string.ascii_uppercase} for letter in message.upper(): if letter in LETTERS: letter_count[letter] += 1 return letter_count def UpperCamelCase_( _snake_case : tuple ): """simple docstring""" return x[0] def UpperCamelCase_( _snake_case : str ): """simple docstring""" __a =get_letter_count(_snake_case ) __a ={ freq: [] for letter, freq in letter_to_freq.items() } for letter in LETTERS: freq_to_letter[letter_to_freq[letter]].append(_snake_case ) __a ={} for freq in freq_to_letter: freq_to_letter[freq].sort(key=ETAOIN.find , reverse=_snake_case ) __a =''.join(freq_to_letter[freq] ) __a =list(freq_to_letter_str.items() ) freq_pairs.sort(key=_snake_case , reverse=_snake_case ) __a =[freq_pair[1] for freq_pair in freq_pairs] return "".join(_snake_case ) def UpperCamelCase_( _snake_case : str ): """simple docstring""" __a =get_frequency_order(_snake_case ) __a =0 for common_letter in ETAOIN[:6]: if common_letter in freq_order[:6]: match_score += 1 for uncommon_letter in ETAOIN[-6:]: if uncommon_letter in freq_order[-6:]: match_score += 1 return match_score if __name__ == "__main__": import doctest doctest.testmod()
308
1
from __future__ import annotations import inspect import unittest import numpy as np from transformers import DeiTConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, ) from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DeiTImageProcessor class __lowerCAmelCase : """simple docstring""" def __init__( self : int , _snake_case : int , _snake_case : Tuple=13 , _snake_case : Any=30 , _snake_case : Optional[int]=2 , _snake_case : Optional[int]=3 , _snake_case : Dict=True , _snake_case : int=True , _snake_case : Union[str, Any]=32 , _snake_case : Tuple=2 , _snake_case : Tuple=4 , _snake_case : Union[str, Any]=37 , _snake_case : Any="gelu" , _snake_case : Dict=0.1 , _snake_case : Tuple=0.1 , _snake_case : Union[str, Any]=10 , _snake_case : Any=0.02 , _snake_case : Any=3 , _snake_case : Any=None , _snake_case : Any=2 , ): __lowercase : Union[str, Any] = parent __lowercase : List[str] = batch_size __lowercase : int = image_size __lowercase : List[Any] = patch_size __lowercase : Optional[int] = num_channels __lowercase : Union[str, Any] = is_training __lowercase : Any = use_labels __lowercase : Union[str, Any] = hidden_size __lowercase : Dict = num_hidden_layers __lowercase : List[Any] = num_attention_heads __lowercase : Optional[Any] = intermediate_size __lowercase : Union[str, Any] = hidden_act __lowercase : List[str] = hidden_dropout_prob __lowercase : Dict = attention_probs_dropout_prob __lowercase : Dict = type_sequence_label_size __lowercase : Tuple = initializer_range __lowercase : Union[str, Any] = scope __lowercase : Optional[int] = encoder_stride # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens) __lowercase : str = (image_size // patch_size) ** 2 __lowercase : Union[str, Any] = num_patches + 2 def snake_case_ ( self : List[str] ): __lowercase : int = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __lowercase : Tuple = None if self.use_labels: __lowercase : List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowercase : Dict = self.get_config() return config, pixel_values, labels def snake_case_ ( self : Optional[Any] ): return DeiTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_snake_case , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def snake_case_ ( self : str , _snake_case : int , _snake_case : Tuple , _snake_case : Any ): __lowercase : Union[str, Any] = TFDeiTModel(config=_snake_case ) __lowercase : Optional[Any] = model(_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def snake_case_ ( self : Tuple , _snake_case : Dict , _snake_case : Optional[Any] , _snake_case : Dict ): __lowercase : Any = TFDeiTForMaskedImageModeling(config=_snake_case ) __lowercase : Dict = model(_snake_case ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images __lowercase : Tuple = 1 __lowercase : List[str] = TFDeiTForMaskedImageModeling(_snake_case ) __lowercase : List[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) __lowercase : Tuple = model(_snake_case ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def snake_case_ ( self : str , _snake_case : List[str] , _snake_case : List[str] , _snake_case : List[Any] ): __lowercase : Any = self.type_sequence_label_size __lowercase : List[Any] = TFDeiTForImageClassification(_snake_case ) __lowercase : str = model(_snake_case , labels=_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images __lowercase : Dict = 1 __lowercase : List[Any] = TFDeiTForImageClassification(_snake_case ) __lowercase : Tuple = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) __lowercase : List[str] = model(_snake_case , labels=_snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def snake_case_ ( self : Any ): __lowercase : List[Any] = self.prepare_config_and_inputs() __lowercase , __lowercase , __lowercase : Tuple = config_and_inputs __lowercase : List[str] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class __lowerCAmelCase ( lowerCAmelCase_ , lowerCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Any = ( ( TFDeiTModel, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, ) if is_tf_available() else () ) A__ : Optional[Any] = ( { '''feature-extraction''': TFDeiTModel, '''image-classification''': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher), } if is_tf_available() else {} ) A__ : Optional[int] = False A__ : Union[str, Any] = False A__ : List[Any] = False A__ : Optional[int] = False def snake_case_ ( self : str ): __lowercase : Tuple = TFDeiTModelTester(self ) __lowercase : Optional[int] = ConfigTester(self , config_class=_snake_case , has_text_modality=_snake_case , hidden_size=37 ) def snake_case_ ( self : Tuple ): self.config_tester.run_common_tests() @unittest.skip(reason='''DeiT does not use inputs_embeds''' ) def snake_case_ ( self : List[str] ): pass def snake_case_ ( self : Tuple ): __lowercase , __lowercase : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowercase : str = model_class(_snake_case ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) __lowercase : int = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_snake_case , tf.keras.layers.Dense ) ) def snake_case_ ( self : Any ): __lowercase , __lowercase : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowercase : Any = model_class(_snake_case ) __lowercase : Optional[Any] = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __lowercase : Dict = [*signature.parameters.keys()] __lowercase : str = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _snake_case ) def snake_case_ ( self : List[Any] ): __lowercase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def snake_case_ ( self : Union[str, Any] ): __lowercase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*_snake_case ) def snake_case_ ( self : Union[str, Any] ): __lowercase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_snake_case ) def snake_case_ ( self : Tuple , _snake_case : Tuple , _snake_case : Optional[int] , _snake_case : List[Any]=False ): __lowercase : List[Any] = super()._prepare_for_class(_snake_case , _snake_case , return_labels=_snake_case ) if return_labels: if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call ).parameters: del inputs_dict["labels"] return inputs_dict @slow def snake_case_ ( self : str ): for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __lowercase : Any = TFDeiTModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def UpperCAmelCase_ ( ) -> Any: __lowercase : Tuple = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf @require_vision class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def snake_case_ ( self : int ): return ( DeiTImageProcessor.from_pretrained('''facebook/deit-base-distilled-patch16-224''' ) if is_vision_available() else None ) @slow def snake_case_ ( self : int ): __lowercase : Optional[int] = TFDeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224''' ) __lowercase : List[str] = self.default_image_processor __lowercase : Optional[Any] = prepare_img() __lowercase : int = image_processor(images=_snake_case , return_tensors='''tf''' ) # forward pass __lowercase : List[str] = model(**_snake_case ) # verify the logits __lowercase : int = tf.TensorShape((1, 1000) ) self.assertEqual(outputs.logits.shape , _snake_case ) __lowercase : List[Any] = tf.constant([-1.02_66, 0.19_12, -1.28_61] ) self.assertTrue(np.allclose(outputs.logits[0, :3] , _snake_case , atol=1E-4 ) )
156
import unittest from pathlib import Path from tempfile import TemporaryDirectory from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available from transformers.models.bert.tokenization_bert import BertTokenizer from transformers.testing_utils import require_tensorflow_text, require_tf, slow if is_tf_available(): import tensorflow as tf if is_tensorflow_text_available(): from transformers.models.bert import TFBertTokenizer __lowerCAmelCase : Optional[int] = ["bert-base-uncased", "bert-base-cased"] __lowerCAmelCase : List[str] = "hf-internal-testing/tiny-bert-tf-only" if is_tf_available(): class __lowerCAmelCase ( tf.keras.Model ): """simple docstring""" def __init__( self : Any , _snake_case : str ): super().__init__() __lowercase : str = tokenizer __lowercase : Any = AutoConfig.from_pretrained(_snake_case ) __lowercase : Union[str, Any] = TFAutoModel.from_config(_snake_case ) def snake_case_ ( self : str , _snake_case : int ): __lowercase : Optional[Any] = self.tokenizer(_snake_case ) __lowercase : int = self.bert(**_snake_case ) return out["pooler_output"] @require_tf @require_tensorflow_text class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def snake_case_ ( self : int ): super().setUp() __lowercase : Optional[int] = [ BertTokenizer.from_pretrained(_snake_case ) for checkpoint in (TOKENIZER_CHECKPOINTS * 2) ] # repeat for when fast_bert_tokenizer=false __lowercase : Optional[Any] = [TFBertTokenizer.from_pretrained(_snake_case ) for checkpoint in TOKENIZER_CHECKPOINTS] + [ TFBertTokenizer.from_pretrained(_snake_case , use_fast_bert_tokenizer=_snake_case ) for checkpoint in TOKENIZER_CHECKPOINTS ] assert len(self.tokenizers ) == len(self.tf_tokenizers ) __lowercase : Optional[int] = [ '''This is a straightforward English test sentence.''', '''This one has some weird characters\rto\nsee\r\nif those\u00E9break things.''', '''Now we\'re going to add some Chinese: 一 二 三 一二三''', '''And some much more rare Chinese: 齉 堃 齉堃''', '''Je vais aussi écrire en français pour tester les accents''', '''Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ''', ] __lowercase : Tuple = list(zip(self.test_sentences , self.test_sentences[::-1] ) ) def snake_case_ ( self : List[str] ): for tokenizer, tf_tokenizer in zip(self.tokenizers , self.tf_tokenizers ): for test_inputs in (self.test_sentences, self.paired_sentences): __lowercase : Dict = tokenizer(_snake_case , return_tensors='''tf''' , padding='''longest''' ) __lowercase : int = tf_tokenizer(_snake_case ) for key in python_outputs.keys(): self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape ) ) self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key] , tf.intaa ) == tf_outputs[key] ) ) @slow def snake_case_ ( self : Union[str, Any] ): for tf_tokenizer in self.tf_tokenizers: __lowercase : Union[str, Any] = tf_tokenizer(self.paired_sentences ) __lowercase : List[str] = tf_tokenizer( text=[sentence[0] for sentence in self.paired_sentences] , text_pair=[sentence[1] for sentence in self.paired_sentences] , ) for key in merged_outputs.keys(): self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key] , tf.intaa ) == separated_outputs[key] ) ) @slow def snake_case_ ( self : Optional[Any] ): for tf_tokenizer in self.tf_tokenizers: __lowercase : Any = tf.function(_snake_case ) for test_inputs in (self.test_sentences, self.paired_sentences): __lowercase : List[Any] = tf.constant(_snake_case ) __lowercase : Any = compiled_tokenizer(_snake_case ) __lowercase : Union[str, Any] = tf_tokenizer(_snake_case ) for key in eager_outputs.keys(): self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key] ) ) @slow def snake_case_ ( self : Tuple ): for tf_tokenizer in self.tf_tokenizers: __lowercase : Any = ModelToSave(tokenizer=_snake_case ) __lowercase : str = tf.convert_to_tensor(self.test_sentences ) __lowercase : Union[str, Any] = model(_snake_case ) # Build model with some sample inputs with TemporaryDirectory() as tempdir: __lowercase : Union[str, Any] = Path(_snake_case ) / '''saved.model''' model.save(_snake_case ) __lowercase : List[str] = tf.keras.models.load_model(_snake_case ) __lowercase : Tuple = loaded_model(_snake_case ) # We may see small differences because the loaded model is compiled, so we need an epsilon for the test self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output ) ) , 1E-5 )
156
1
import copy import unittest from transformers.models.auto import get_values from transformers.testing_utils import require_torch, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_MULTIPLE_CHOICE_MAPPING, MODEL_FOR_QUESTION_ANSWERING_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaModel, ) from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class __a : def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=7 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=99 , _SCREAMING_SNAKE_CASE=36 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=37 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=512 , _SCREAMING_SNAKE_CASE=16 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=6 , _SCREAMING_SNAKE_CASE=6 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=1000 , ) -> Optional[int]: """simple docstring""" _UpperCAmelCase = parent _UpperCAmelCase = batch_size _UpperCAmelCase = num_channels _UpperCAmelCase = image_size _UpperCAmelCase = patch_size _UpperCAmelCase = text_seq_length _UpperCAmelCase = is_training _UpperCAmelCase = use_input_mask _UpperCAmelCase = use_token_type_ids _UpperCAmelCase = use_labels _UpperCAmelCase = vocab_size _UpperCAmelCase = hidden_size _UpperCAmelCase = num_hidden_layers _UpperCAmelCase = num_attention_heads _UpperCAmelCase = intermediate_size _UpperCAmelCase = hidden_act _UpperCAmelCase = hidden_dropout_prob _UpperCAmelCase = attention_probs_dropout_prob _UpperCAmelCase = max_position_embeddings _UpperCAmelCase = type_vocab_size _UpperCAmelCase = type_sequence_label_size _UpperCAmelCase = initializer_range _UpperCAmelCase = coordinate_size _UpperCAmelCase = shape_size _UpperCAmelCase = num_labels _UpperCAmelCase = num_choices _UpperCAmelCase = scope _UpperCAmelCase = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) _UpperCAmelCase = text_seq_length _UpperCAmelCase = (image_size // patch_size) ** 2 + 1 _UpperCAmelCase = self.text_seq_length + self.image_seq_length def UpperCAmelCase__ ( self ) -> Dict: """simple docstring""" _UpperCAmelCase = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) _UpperCAmelCase = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: _UpperCAmelCase = bbox[i, j, 3] _UpperCAmelCase = bbox[i, j, 1] _UpperCAmelCase = t if bbox[i, j, 2] < bbox[i, j, 0]: _UpperCAmelCase = bbox[i, j, 2] _UpperCAmelCase = bbox[i, j, 0] _UpperCAmelCase = t _UpperCAmelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _UpperCAmelCase = None if self.use_input_mask: _UpperCAmelCase = random_attention_mask([self.batch_size, self.text_seq_length] ) _UpperCAmelCase = None if self.use_token_type_ids: _UpperCAmelCase = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) _UpperCAmelCase = None _UpperCAmelCase = None if self.use_labels: _UpperCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCAmelCase = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) _UpperCAmelCase = LayoutLMvaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def UpperCAmelCase__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" _UpperCAmelCase = LayoutLMvaModel(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() # text + image _UpperCAmelCase = model(SCREAMING_SNAKE_CASE_ , pixel_values=SCREAMING_SNAKE_CASE_ ) _UpperCAmelCase = model( SCREAMING_SNAKE_CASE_ , bbox=SCREAMING_SNAKE_CASE_ , pixel_values=SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , token_type_ids=SCREAMING_SNAKE_CASE_ ) _UpperCAmelCase = model(SCREAMING_SNAKE_CASE_ , bbox=SCREAMING_SNAKE_CASE_ , pixel_values=SCREAMING_SNAKE_CASE_ , token_type_ids=SCREAMING_SNAKE_CASE_ ) _UpperCAmelCase = model(SCREAMING_SNAKE_CASE_ , bbox=SCREAMING_SNAKE_CASE_ , pixel_values=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only _UpperCAmelCase = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only _UpperCAmelCase = model(pixel_values=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def UpperCAmelCase__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: """simple docstring""" _UpperCAmelCase = self.num_labels _UpperCAmelCase = LayoutLMvaForSequenceClassification(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() _UpperCAmelCase = model( SCREAMING_SNAKE_CASE_ , bbox=SCREAMING_SNAKE_CASE_ , pixel_values=SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , token_type_ids=SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCAmelCase__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[str]: """simple docstring""" _UpperCAmelCase = self.num_labels _UpperCAmelCase = LayoutLMvaForTokenClassification(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() _UpperCAmelCase = model( SCREAMING_SNAKE_CASE_ , bbox=SCREAMING_SNAKE_CASE_ , pixel_values=SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , token_type_ids=SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def UpperCAmelCase__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" _UpperCAmelCase = LayoutLMvaForQuestionAnswering(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() _UpperCAmelCase = model( SCREAMING_SNAKE_CASE_ , bbox=SCREAMING_SNAKE_CASE_ , pixel_values=SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , token_type_ids=SCREAMING_SNAKE_CASE_ , start_positions=SCREAMING_SNAKE_CASE_ , end_positions=SCREAMING_SNAKE_CASE_ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCAmelCase__ ( self ) -> List[str]: """simple docstring""" _UpperCAmelCase = self.prepare_config_and_inputs() ( _UpperCAmelCase ) = config_and_inputs _UpperCAmelCase = { 'input_ids': input_ids, 'bbox': bbox, 'pixel_values': pixel_values, 'token_type_ids': token_type_ids, 'attention_mask': input_mask, } return config, inputs_dict @require_torch class __a ( a__ , a__ , unittest.TestCase ): _a : Any = False _a : List[str] = False _a : Union[str, Any] = False _a : str = ( ( LayoutLMvaModel, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaForQuestionAnswering, ) if is_torch_available() else () ) _a : Tuple = ( {"document-question-answering": LayoutLMvaForQuestionAnswering, "feature-extraction": LayoutLMvaModel} if is_torch_available() else {} ) def UpperCAmelCase__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[Any]: """simple docstring""" return True def UpperCAmelCase__ ( self ) -> Optional[Any]: """simple docstring""" _UpperCAmelCase = LayoutLMvaModelTester(self ) _UpperCAmelCase = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ , hidden_size=37 ) def UpperCAmelCase__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False ) -> str: """simple docstring""" _UpperCAmelCase = copy.deepcopy(SCREAMING_SNAKE_CASE_ ) if model_class in get_values(SCREAMING_SNAKE_CASE_ ): _UpperCAmelCase = { k: v.unsqueeze(1 ).expand(-1 , self.model_tester.num_choices , -1 ).contiguous() if isinstance(SCREAMING_SNAKE_CASE_ , torch.Tensor ) and v.ndim > 1 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(SCREAMING_SNAKE_CASE_ ): _UpperCAmelCase = torch.ones(self.model_tester.batch_size , dtype=torch.long , device=SCREAMING_SNAKE_CASE_ ) elif model_class in get_values(SCREAMING_SNAKE_CASE_ ): _UpperCAmelCase = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=SCREAMING_SNAKE_CASE_ ) _UpperCAmelCase = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=SCREAMING_SNAKE_CASE_ ) elif model_class in [ *get_values(SCREAMING_SNAKE_CASE_ ), ]: _UpperCAmelCase = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=SCREAMING_SNAKE_CASE_ ) elif model_class in [ *get_values(SCREAMING_SNAKE_CASE_ ), ]: _UpperCAmelCase = torch.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=torch.long , device=SCREAMING_SNAKE_CASE_ , ) return inputs_dict def UpperCAmelCase__ ( self ) -> Tuple: """simple docstring""" self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: """simple docstring""" _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase__ ( self ) -> Optional[int]: """simple docstring""" _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _UpperCAmelCase = type self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase__ ( self ) -> Tuple: """simple docstring""" _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase__ ( self ) -> Optional[Any]: """simple docstring""" _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase__ ( self ) -> Any: """simple docstring""" _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE_ ) @slow def UpperCAmelCase__ ( self ) -> Optional[Any]: """simple docstring""" for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCAmelCase = LayoutLMvaModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase__ ( ) -> Dict: '''simple docstring''' _UpperCAmelCase = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch class __a ( unittest.TestCase ): @cached_property def UpperCAmelCase__ ( self ) -> str: """simple docstring""" return LayoutLMvaImageProcessor(apply_ocr=SCREAMING_SNAKE_CASE_ ) if is_vision_available() else None @slow def UpperCAmelCase__ ( self ) -> Optional[int]: """simple docstring""" _UpperCAmelCase = LayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base' ).to(SCREAMING_SNAKE_CASE_ ) _UpperCAmelCase = self.default_image_processor _UpperCAmelCase = prepare_img() _UpperCAmelCase = image_processor(images=SCREAMING_SNAKE_CASE_ , return_tensors='pt' ).pixel_values.to(SCREAMING_SNAKE_CASE_ ) _UpperCAmelCase = torch.tensor([[1, 2]] ) _UpperCAmelCase = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]] ).unsqueeze(0 ) # forward pass _UpperCAmelCase = model( input_ids=input_ids.to(SCREAMING_SNAKE_CASE_ ) , bbox=bbox.to(SCREAMING_SNAKE_CASE_ ) , pixel_values=pixel_values.to(SCREAMING_SNAKE_CASE_ ) , ) # verify the logits _UpperCAmelCase = torch.Size((1, 199, 768) ) self.assertEqual(outputs.last_hidden_state.shape , SCREAMING_SNAKE_CASE_ ) _UpperCAmelCase = torch.tensor( [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]] ).to(SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=1e-4 ) )
365
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase__ :List[str] = { '''configuration_x_clip''': [ '''XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XCLIPConfig''', '''XCLIPTextConfig''', '''XCLIPVisionConfig''', ], '''processing_x_clip''': ['''XCLIPProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ :Any = [ '''XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XCLIPModel''', '''XCLIPPreTrainedModel''', '''XCLIPTextModel''', '''XCLIPVisionModel''', ] if TYPE_CHECKING: from .configuration_x_clip import ( XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, XCLIPConfig, XCLIPTextConfig, XCLIPVisionConfig, ) from .processing_x_clip import XCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_x_clip import ( XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, XCLIPModel, XCLIPPreTrainedModel, XCLIPTextModel, XCLIPVisionModel, ) else: import sys lowerCAmelCase__ :str = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
185
0
import unittest from transformers import load_tool from .test_tools_common import ToolTesterMixin lowerCAmelCase__ = '\nHugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.\n\nIn March 2021, Hugging Face raised $40 million in a Series B funding round.[3]\n\nOn April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]\n' class lowerCAmelCase__ ( unittest.TestCase , a): '''simple docstring''' def _lowerCamelCase ( self) -> Union[str, Any]: _A : List[str] = load_tool("text-question-answering") self.tool.setup() _A : List[Any] = load_tool("text-question-answering" , remote=__lowerCamelCase) def _lowerCamelCase ( self) -> int: _A : Union[str, Any] = self.tool(__lowerCamelCase , "What did Hugging Face do in April 2021?") self.assertEqual(__lowerCamelCase , "launched the BigScience Research Workshop") def _lowerCamelCase ( self) -> Dict: _A : List[Any] = self.remote_tool(__lowerCamelCase , "What did Hugging Face do in April 2021?") self.assertEqual(__lowerCamelCase , "launched the BigScience Research Workshop") def _lowerCamelCase ( self) -> str: _A : int = self.tool(text=__lowerCamelCase , question="What did Hugging Face do in April 2021?") self.assertEqual(__lowerCamelCase , "launched the BigScience Research Workshop") def _lowerCamelCase ( self) -> List[Any]: _A : Union[str, Any] = self.remote_tool(text=__lowerCamelCase , question="What did Hugging Face do in April 2021?") self.assertEqual(__lowerCamelCase , "launched the BigScience Research Workshop")
11
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) __lowerCAmelCase = { '''configuration_encodec''': [ '''ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EncodecConfig''', ], '''feature_extraction_encodec''': ['''EncodecFeatureExtractor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ '''ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EncodecModel''', '''EncodecPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_encodec import ( ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP, EncodecConfig, ) from .feature_extraction_encodec import EncodecFeatureExtractor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_encodec import ( ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST, EncodecModel, EncodecPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
196
0
'''simple docstring''' def UpperCamelCase_( snake_case : str , snake_case : List[str] ): '''simple docstring''' return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2 def UpperCamelCase_( snake_case : Optional[Any] , snake_case : Tuple=0 ): '''simple docstring''' return sorted(snake_case , key=lambda snake_case : x[column] ) def UpperCamelCase_( snake_case : str , snake_case : Union[str, Any] , snake_case : Dict=float("inf" ) ): '''simple docstring''' for i in range(points_counts - 1 ): for j in range(i + 1 , snake_case ): snake_case_ = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: snake_case_ = current_dis return min_dis def UpperCamelCase_( snake_case : List[Any] , snake_case : int , snake_case : List[Any]=float("inf" ) ): '''simple docstring''' for i in range(min(6 , points_counts - 1 ) , snake_case ): for j in range(max(0 , i - 6 ) , snake_case ): snake_case_ = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: snake_case_ = current_dis return min_dis def UpperCamelCase_( snake_case : Optional[Any] , snake_case : str , snake_case : List[Any] ): '''simple docstring''' if points_counts <= 3: return dis_between_closest_pair(snake_case , snake_case ) # recursion snake_case_ = points_counts // 2 snake_case_ = closest_pair_of_points_sqr( snake_case , points_sorted_on_y[:mid] , snake_case ) snake_case_ = closest_pair_of_points_sqr( snake_case , points_sorted_on_y[mid:] , points_counts - mid ) snake_case_ = min(snake_case , snake_case ) snake_case_ = [] for point in points_sorted_on_x: if abs(point[0] - points_sorted_on_x[mid][0] ) < closest_pair_dis: cross_strip.append(snake_case ) snake_case_ = dis_between_closest_in_strip( snake_case , len(snake_case ) , snake_case ) return min(snake_case , snake_case ) def UpperCamelCase_( snake_case : Dict , snake_case : List[str] ): '''simple docstring''' snake_case_ = column_based_sort(snake_case , column=0 ) snake_case_ = column_based_sort(snake_case , column=1 ) return ( closest_pair_of_points_sqr( snake_case , snake_case , snake_case ) ) ** 0.5 if __name__ == "__main__": _SCREAMING_SNAKE_CASE : int = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] print("Distance:", closest_pair_of_points(points, len(points)))
92
'''simple docstring''' _SCREAMING_SNAKE_CASE : Optional[int] = "Alexander Joslin" import operator as op from .stack import Stack def UpperCamelCase_( snake_case : str ): '''simple docstring''' snake_case_ = {"*": op.mul, "/": op.truediv, "+": op.add, "-": op.sub} snake_case_ = Stack() snake_case_ = Stack() for i in equation: if i.isdigit(): # RULE 1 operand_stack.push(int(snake_case ) ) elif i in operators: # RULE 2 operator_stack.push(snake_case ) elif i == ")": # RULE 4 snake_case_ = operator_stack.peek() operator_stack.pop() snake_case_ = operand_stack.peek() operand_stack.pop() snake_case_ = operand_stack.peek() operand_stack.pop() snake_case_ = operators[opr](snake_case , snake_case ) operand_stack.push(snake_case ) # RULE 5 return operand_stack.peek() if __name__ == "__main__": _SCREAMING_SNAKE_CASE : List[Any] = "(5 + ((4 * 2) * (2 + 3)))" # answer = 45 print(F"{equation} = {dijkstras_two_stack_algorithm(equation)}")
92
1
import pandas as pd from matplotlib import pyplot as plt from sklearn.linear_model import LinearRegression # Splitting the dataset into the Training set and Test set from sklearn.model_selection import train_test_split # Fitting Polynomial Regression to the dataset from sklearn.preprocessing import PolynomialFeatures # Importing the dataset _UpperCamelCase = pd.read_csv( '''https://s3.us-west-2.amazonaws.com/public.gamelab.fun/dataset/''' '''position_salaries.csv''' ) _UpperCamelCase = dataset.iloc[:, 1:2].values _UpperCamelCase = dataset.iloc[:, 2].values _UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase = train_test_split(X, y, test_size=0.2, random_state=0) _UpperCamelCase = PolynomialFeatures(degree=4) _UpperCamelCase = poly_reg.fit_transform(X) _UpperCamelCase = LinearRegression() pol_reg.fit(X_poly, y) def lowerCAmelCase__( ) -> Any: plt.scatter(lowerCAmelCase__ , lowerCAmelCase__ , color="red" ) plt.plot(lowerCAmelCase__ , pol_reg.predict(poly_reg.fit_transform(lowerCAmelCase__ ) ) , color="blue" ) plt.title("Truth or Bluff (Linear Regression)" ) plt.xlabel("Position level" ) plt.ylabel("Salary" ) plt.show() if __name__ == "__main__": viz_polymonial() # Predicting a new result with Polymonial Regression pol_reg.predict(poly_reg.fit_transform([[5.5]])) # output should be 132148.43750003
326
'''simple docstring''' from typing import Optional from urllib.parse import quote import huggingface_hub as hfh from packaging import version def lowercase_ ( lowerCAmelCase__ : str , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None ): """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 : Optional[Any] = quote(lowerCAmelCase__ ) return hfh.hf_hub_url(lowerCAmelCase__ , lowerCAmelCase__ , repo_type="""dataset""" , revision=lowerCAmelCase__ )
254
0
from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, apply_forward_hook from .modeling_utils import ModelMixin from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer @dataclass class a ( UpperCAmelCase ): _lowercase = 42 class a ( UpperCAmelCase , UpperCAmelCase ): @register_to_config def __init__( self , A_ = 3 , A_ = 3 , A_ = ("DownEncoderBlock2D",) , A_ = ("UpDecoderBlock2D",) , A_ = (64,) , A_ = 1 , A_ = "silu" , A_ = 3 , A_ = 32 , A_ = 256 , A_ = 32 , A_ = None , A_ = 0.1_82_15 , A_ = "group" , ): '''simple docstring''' super().__init__() # pass init params to Encoder _UpperCAmelCase : Optional[Any] = Encoder( in_channels=A_ , out_channels=A_ , down_block_types=A_ , block_out_channels=A_ , layers_per_block=A_ , act_fn=A_ , norm_num_groups=A_ , double_z=A_ , ) _UpperCAmelCase : Union[str, Any] = vq_embed_dim if vq_embed_dim is not None else latent_channels _UpperCAmelCase : Optional[Any] = nn.Convad(A_ , A_ , 1 ) _UpperCAmelCase : Any = VectorQuantizer(A_ , A_ , beta=0.25 , remap=A_ , sane_index_shape=A_ ) _UpperCAmelCase : Any = nn.Convad(A_ , A_ , 1 ) # pass init params to Decoder _UpperCAmelCase : Tuple = Decoder( in_channels=A_ , out_channels=A_ , up_block_types=A_ , block_out_channels=A_ , layers_per_block=A_ , act_fn=A_ , norm_num_groups=A_ , norm_type=A_ , ) @apply_forward_hook def _UpperCAmelCase ( self , A_ , A_ = True ): '''simple docstring''' _UpperCAmelCase : Union[str, Any] = self.encoder(A_ ) _UpperCAmelCase : List[str] = self.quant_conv(A_ ) if not return_dict: return (h,) return VQEncoderOutput(latents=A_ ) @apply_forward_hook def _UpperCAmelCase ( self , A_ , A_ = False , A_ = True ): '''simple docstring''' if not force_not_quantize: _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : Optional[int] = self.quantize(A_ ) else: _UpperCAmelCase : List[str] = h _UpperCAmelCase : List[Any] = self.post_quant_conv(A_ ) _UpperCAmelCase : Optional[int] = self.decoder(A_ , quant if self.config.norm_type == "spatial" else None ) if not return_dict: return (dec,) return DecoderOutput(sample=A_ ) def _UpperCAmelCase ( self , A_ , A_ = True ): '''simple docstring''' _UpperCAmelCase : Union[str, Any] = sample _UpperCAmelCase : Any = self.encode(A_ ).latents _UpperCAmelCase : Optional[int] = self.decode(A_ ).sample if not return_dict: return (dec,) return DecoderOutput(sample=A_ )
189
from typing import Optional import torch import torch.utils.checkpoint from torch import Tensor, nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import ( BackboneOutput, BaseModelOutputWithNoAttention, BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention, ) from ...modeling_utils import PreTrainedModel from ...utils import ( add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings, ) from ...utils.backbone_utils import BackboneMixin from .configuration_resnet import ResNetConfig SCREAMING_SNAKE_CASE_ = logging.get_logger(__name__) # General docstring SCREAMING_SNAKE_CASE_ = 'ResNetConfig' # Base docstring SCREAMING_SNAKE_CASE_ = 'microsoft/resnet-50' SCREAMING_SNAKE_CASE_ = [1, 2048, 7, 7] # Image classification docstring SCREAMING_SNAKE_CASE_ = 'microsoft/resnet-50' SCREAMING_SNAKE_CASE_ = 'tiger cat' SCREAMING_SNAKE_CASE_ = [ 'microsoft/resnet-50', # See all resnet models at https://huggingface.co/models?filter=resnet ] class a ( nn.Module ): def __init__( self , A_ , A_ , A_ = 3 , A_ = 1 , A_ = "relu" ): '''simple docstring''' super().__init__() _UpperCAmelCase : Union[str, Any] = nn.Convad( A_ , A_ , kernel_size=A_ , stride=A_ , padding=kernel_size // 2 , bias=A_ ) _UpperCAmelCase : List[Any] = nn.BatchNormad(A_ ) _UpperCAmelCase : Union[str, Any] = ACTaFN[activation] if activation is not None else nn.Identity() def _UpperCAmelCase ( self , A_ ): '''simple docstring''' _UpperCAmelCase : List[Any] = self.convolution(A_ ) _UpperCAmelCase : Optional[int] = self.normalization(A_ ) _UpperCAmelCase : Optional[Any] = self.activation(A_ ) return hidden_state class a ( nn.Module ): def __init__( self , A_ ): '''simple docstring''' super().__init__() _UpperCAmelCase : Any = ResNetConvLayer( config.num_channels , config.embedding_size , kernel_size=7 , stride=2 , activation=config.hidden_act ) _UpperCAmelCase : List[str] = nn.MaxPoolad(kernel_size=3 , stride=2 , padding=1 ) _UpperCAmelCase : List[Any] = config.num_channels def _UpperCAmelCase ( self , A_ ): '''simple docstring''' _UpperCAmelCase : int = pixel_values.shape[1] if num_channels != self.num_channels: raise ValueError( "Make sure that the channel dimension of the pixel values match with the one set in the configuration." ) _UpperCAmelCase : int = self.embedder(A_ ) _UpperCAmelCase : int = self.pooler(A_ ) return embedding class a ( nn.Module ): def __init__( self , A_ , A_ , A_ = 2 ): '''simple docstring''' super().__init__() _UpperCAmelCase : Union[str, Any] = nn.Convad(A_ , A_ , kernel_size=1 , stride=A_ , bias=A_ ) _UpperCAmelCase : Optional[int] = nn.BatchNormad(A_ ) def _UpperCAmelCase ( self , A_ ): '''simple docstring''' _UpperCAmelCase : str = self.convolution(A_ ) _UpperCAmelCase : List[str] = self.normalization(A_ ) return hidden_state class a ( nn.Module ): def __init__( self , A_ , A_ , A_ = 1 , A_ = "relu" ): '''simple docstring''' super().__init__() _UpperCAmelCase : Optional[int] = in_channels != out_channels or stride != 1 _UpperCAmelCase : Dict = ( ResNetShortCut(A_ , A_ , stride=A_ ) if should_apply_shortcut else nn.Identity() ) _UpperCAmelCase : int = nn.Sequential( ResNetConvLayer(A_ , A_ , stride=A_ ) , ResNetConvLayer(A_ , A_ , activation=A_ ) , ) _UpperCAmelCase : Dict = ACTaFN[activation] def _UpperCAmelCase ( self , A_ ): '''simple docstring''' _UpperCAmelCase : Optional[Any] = hidden_state _UpperCAmelCase : Any = self.layer(A_ ) _UpperCAmelCase : Optional[int] = self.shortcut(A_ ) hidden_state += residual _UpperCAmelCase : Optional[int] = self.activation(A_ ) return hidden_state class a ( nn.Module ): def __init__( self , A_ , A_ , A_ = 1 , A_ = "relu" , A_ = 4 ): '''simple docstring''' super().__init__() _UpperCAmelCase : Optional[Any] = in_channels != out_channels or stride != 1 _UpperCAmelCase : Optional[int] = out_channels // reduction _UpperCAmelCase : List[str] = ( ResNetShortCut(A_ , A_ , stride=A_ ) if should_apply_shortcut else nn.Identity() ) _UpperCAmelCase : Dict = nn.Sequential( ResNetConvLayer(A_ , A_ , kernel_size=1 ) , ResNetConvLayer(A_ , A_ , stride=A_ ) , ResNetConvLayer(A_ , A_ , kernel_size=1 , activation=A_ ) , ) _UpperCAmelCase : List[str] = ACTaFN[activation] def _UpperCAmelCase ( self , A_ ): '''simple docstring''' _UpperCAmelCase : List[Any] = hidden_state _UpperCAmelCase : List[str] = self.layer(A_ ) _UpperCAmelCase : List[str] = self.shortcut(A_ ) hidden_state += residual _UpperCAmelCase : Dict = self.activation(A_ ) return hidden_state class a ( nn.Module ): def __init__( self , A_ , A_ , A_ , A_ = 2 , A_ = 2 , ): '''simple docstring''' super().__init__() _UpperCAmelCase : Any = ResNetBottleNeckLayer if config.layer_type == "bottleneck" else ResNetBasicLayer _UpperCAmelCase : Optional[Any] = nn.Sequential( # downsampling is done in the first layer with stride of 2 layer(A_ , A_ , stride=A_ , activation=config.hidden_act ) , *[layer(A_ , A_ , activation=config.hidden_act ) for _ in range(depth - 1 )] , ) def _UpperCAmelCase ( self , A_ ): '''simple docstring''' _UpperCAmelCase : List[Any] = input for layer in self.layers: _UpperCAmelCase : Optional[Any] = layer(A_ ) return hidden_state class a ( nn.Module ): def __init__( self , A_ ): '''simple docstring''' super().__init__() _UpperCAmelCase : Any = nn.ModuleList([] ) # based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input self.stages.append( ResNetStage( A_ , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , ) ) _UpperCAmelCase : Union[str, Any] = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for (in_channels, out_channels), depth in zip(A_ , config.depths[1:] ): self.stages.append(ResNetStage(A_ , A_ , A_ , depth=A_ ) ) def _UpperCAmelCase ( self , A_ , A_ = False , A_ = True ): '''simple docstring''' _UpperCAmelCase : List[Any] = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: _UpperCAmelCase : Dict = hidden_states + (hidden_state,) _UpperCAmelCase : str = stage_module(A_ ) if output_hidden_states: _UpperCAmelCase : int = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return BaseModelOutputWithNoAttention( last_hidden_state=A_ , hidden_states=A_ , ) class a ( UpperCAmelCase ): _lowercase = ResNetConfig _lowercase = "resnet" _lowercase = "pixel_values" _lowercase = True def _UpperCAmelCase ( self , A_ ): '''simple docstring''' if isinstance(A_ , nn.Convad ): nn.init.kaiming_normal_(module.weight , mode="fan_out" , nonlinearity="relu" ) elif isinstance(A_ , (nn.BatchNormad, nn.GroupNorm) ): nn.init.constant_(module.weight , 1 ) nn.init.constant_(module.bias , 0 ) def _UpperCAmelCase ( self , A_ , A_=False ): '''simple docstring''' if isinstance(A_ , A_ ): _UpperCAmelCase : Optional[Any] = value SCREAMING_SNAKE_CASE_ = R'\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n\n Parameters:\n config ([`ResNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n' SCREAMING_SNAKE_CASE_ = R'\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConvNextImageProcessor.__call__`] for details.\n\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( "The bare ResNet model outputting raw features without any specific head on top." , UpperCAmelCase , ) class a ( UpperCAmelCase ): def __init__( self , A_ ): '''simple docstring''' super().__init__(A_ ) _UpperCAmelCase : List[str] = config _UpperCAmelCase : Any = ResNetEmbeddings(A_ ) _UpperCAmelCase : str = ResNetEncoder(A_ ) _UpperCAmelCase : Any = nn.AdaptiveAvgPoolad((1, 1) ) # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(A_ ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=A_ , config_class=_CONFIG_FOR_DOC , modality="vision" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def _UpperCAmelCase ( self , A_ , A_ = None , A_ = None ): '''simple docstring''' _UpperCAmelCase : Optional[int] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _UpperCAmelCase : List[str] = return_dict if return_dict is not None else self.config.use_return_dict _UpperCAmelCase : List[Any] = self.embedder(A_ ) _UpperCAmelCase : str = self.encoder( A_ , output_hidden_states=A_ , return_dict=A_ ) _UpperCAmelCase : List[Any] = encoder_outputs[0] _UpperCAmelCase : int = self.pooler(A_ ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=A_ , pooler_output=A_ , hidden_states=encoder_outputs.hidden_states , ) @add_start_docstrings( "\n ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n " , UpperCAmelCase , ) class a ( UpperCAmelCase ): def __init__( self , A_ ): '''simple docstring''' super().__init__(A_ ) _UpperCAmelCase : Optional[int] = config.num_labels _UpperCAmelCase : str = ResNetModel(A_ ) # classification head _UpperCAmelCase : int = nn.Sequential( nn.Flatten() , nn.Linear(config.hidden_sizes[-1] , config.num_labels ) if config.num_labels > 0 else nn.Identity() , ) # initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(A_ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=A_ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def _UpperCAmelCase ( self , A_ = None , A_ = None , A_ = None , A_ = None , ): '''simple docstring''' _UpperCAmelCase : Optional[int] = return_dict if return_dict is not None else self.config.use_return_dict _UpperCAmelCase : Tuple = self.resnet(A_ , output_hidden_states=A_ , return_dict=A_ ) _UpperCAmelCase : Optional[int] = outputs.pooler_output if return_dict else outputs[1] _UpperCAmelCase : int = self.classifier(A_ ) _UpperCAmelCase : Dict = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: _UpperCAmelCase : Optional[Any] = "regression" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): _UpperCAmelCase : Optional[Any] = "single_label_classification" else: _UpperCAmelCase : Any = "multi_label_classification" if self.config.problem_type == "regression": _UpperCAmelCase : str = MSELoss() if self.num_labels == 1: _UpperCAmelCase : Any = loss_fct(logits.squeeze() , labels.squeeze() ) else: _UpperCAmelCase : Optional[int] = loss_fct(A_ , A_ ) elif self.config.problem_type == "single_label_classification": _UpperCAmelCase : Any = CrossEntropyLoss() _UpperCAmelCase : Any = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": _UpperCAmelCase : Any = BCEWithLogitsLoss() _UpperCAmelCase : Tuple = loss_fct(A_ , A_ ) if not return_dict: _UpperCAmelCase : Any = (logits,) + outputs[2:] return (loss,) + output if loss is not None else output return ImageClassifierOutputWithNoAttention(loss=A_ , logits=A_ , hidden_states=outputs.hidden_states ) @add_start_docstrings( "\n ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n " , UpperCAmelCase , ) class a ( UpperCAmelCase , UpperCAmelCase ): def __init__( self , A_ ): '''simple docstring''' super().__init__(A_ ) super()._init_backbone(A_ ) _UpperCAmelCase : Optional[int] = [config.embedding_size] + config.hidden_sizes _UpperCAmelCase : str = ResNetEmbeddings(A_ ) _UpperCAmelCase : List[Any] = ResNetEncoder(A_ ) # initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(A_ ) @replace_return_docstrings(output_type=A_ , config_class=_CONFIG_FOR_DOC ) def _UpperCAmelCase ( self , A_ , A_ = None , A_ = None ): '''simple docstring''' _UpperCAmelCase : Tuple = return_dict if return_dict is not None else self.config.use_return_dict _UpperCAmelCase : Tuple = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _UpperCAmelCase : Tuple = self.embedder(A_ ) _UpperCAmelCase : Optional[int] = self.encoder(A_ , output_hidden_states=A_ , return_dict=A_ ) _UpperCAmelCase : Optional[int] = outputs.hidden_states _UpperCAmelCase : Any = () for idx, stage in enumerate(self.stage_names ): if stage in self.out_features: feature_maps += (hidden_states[idx],) if not return_dict: _UpperCAmelCase : Union[str, Any] = (feature_maps,) if output_hidden_states: output += (outputs.hidden_states,) return output return BackboneOutput( feature_maps=A_ , hidden_states=outputs.hidden_states if output_hidden_states else None , attentions=A_ , )
189
1
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available from ...utils import OptionalDependencyNotAvailable UpperCamelCase = {'''configuration_dpt''': ['''DPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DPTConfig''']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''DPTFeatureExtractor'''] UpperCamelCase = ['''DPTImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''DPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''DPTForDepthEstimation''', '''DPTForSemanticSegmentation''', '''DPTModel''', '''DPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_dpt import DPT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPTConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_dpt import DPTFeatureExtractor from .image_processing_dpt import DPTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dpt import ( DPT_PRETRAINED_MODEL_ARCHIVE_LIST, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel, DPTPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
87
import math def snake_case_ ( snake_case , snake_case ) -> float: return math.pow(snake_case , 2 ) - a def snake_case_ ( snake_case ) -> float: return 2 * x def snake_case_ ( snake_case ) -> float: lowercase__: Dict = 2.0 while start <= a: lowercase__: str = math.pow(snake_case , 2 ) return start def snake_case_ ( snake_case , snake_case = 99_99 , snake_case = 0.0_0_0_0_0_0_0_0_0_0_0_0_0_1 ) -> float: if a < 0: raise ValueError('math domain error' ) lowercase__: Tuple = get_initial_point(snake_case ) for _ in range(snake_case ): lowercase__: List[Any] = value lowercase__: Any = value - fx(snake_case , snake_case ) / fx_derivative(snake_case ) if abs(prev_value - value ) < tolerance: return value return value if __name__ == "__main__": from doctest import testmod testmod()
196
0
import argparse import json import os import torch from torch import nn from transformers import NllbMoeConfig, NllbMoeModel from transformers.modeling_utils import dtype_byte_size from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE :str ) -> Optional[int]: __lowerCAmelCase : Optional[int] = [ """encoder.version""", """decoder.version""", """model.encoder.version""", """model.decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """encoder.embed_positions._float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE :Tuple ) -> Optional[Any]: __lowerCAmelCase : int = emb.weight.shape __lowerCAmelCase : str = nn.Linear(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , bias=SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[Any] = emb.weight.data return lin_layer def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE :Optional[int] , SCREAMING_SNAKE_CASE :str=None ) -> List[str]: __lowerCAmelCase : Optional[Any] = {} for old_key in state_dict.keys(): __lowerCAmelCase : List[str] = old_key if "moe_layer.experts." in key: if expert_idx is not None: __lowerCAmelCase : Tuple = key.replace("""moe_layer.experts.0""" , F'''ffn.experts.expert_{expert_idx}''' ) else: __lowerCAmelCase : Optional[Any] = key.replace("""moe_layer.experts.""" , """ffn.experts.expert_""" ) if "gate" in key: __lowerCAmelCase : int = key.replace(""".moe_layer.gate.wg""" , """.ffn.router.classifier""" ) if "fc2" and "experts" not in key: __lowerCAmelCase : str = key.replace(""".fc2.""" , """.ffn.fc2.""" ) if "fc1" and "experts" not in key: __lowerCAmelCase : List[Any] = key.replace(""".fc1.""" , """.ffn.fc1.""" ) if ".encoder_attn." in key: __lowerCAmelCase : List[Any] = key.replace(""".encoder_attn.""" , """.cross_attention.""" ) if "encoder_attn_layer_norm" in key: __lowerCAmelCase : Optional[int] = key.replace("""encoder_attn_layer_norm""" , """cross_attention_layer_norm""" ) if "final_layer_norm" in key: __lowerCAmelCase : Optional[int] = key.replace("""final_layer_norm""" , """ff_layer_norm""" ) __lowerCAmelCase : Any = state_dict[old_key] return new_dict def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE :Optional[int] , SCREAMING_SNAKE_CASE :Any , SCREAMING_SNAKE_CASE :Tuple , SCREAMING_SNAKE_CASE :Any , SCREAMING_SNAKE_CASE :str = WEIGHTS_NAME ) -> Tuple: __lowerCAmelCase : Union[str, Any] = [] __lowerCAmelCase : Optional[Any] = 0 os.makedirs(SCREAMING_SNAKE_CASE , exist_ok=SCREAMING_SNAKE_CASE ) for expert in range(SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Dict = switch_checkpoint_path + F'''-rank-{expert}.pt''' if os.path.isfile(SCREAMING_SNAKE_CASE ): __lowerCAmelCase : List[str] = torch.load(SCREAMING_SNAKE_CASE )["""model"""] remove_ignore_keys_(SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Union[str, Any] = rename_fairseq_keys(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) __lowerCAmelCase : List[str] = os.path.join( SCREAMING_SNAKE_CASE , weights_name.replace(""".bin""" , F'''-{len(SCREAMING_SNAKE_CASE )+1:05d}-of-???.bin''' ) ) torch.save(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) sharded_state_dicts.append(expert_state.keys() ) total_size += sum([value.numel() for key, value in expert_state.items()] ) * dtype_byte_size( expert_state[list(SCREAMING_SNAKE_CASE )[0]].dtype ) # Add the last block __lowerCAmelCase : Union[str, Any] = os.path.join(SCREAMING_SNAKE_CASE , weights_name.replace(""".bin""" , F'''-{len(SCREAMING_SNAKE_CASE )+1:05d}-of-???.bin''' ) ) __lowerCAmelCase : Dict = torch.load(switch_checkpoint_path + """-shared.pt""" )["""model"""] remove_ignore_keys_(SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[Any] = rename_fairseq_keys(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Any = shared_weights["""decoder.embed_tokens.weight"""] sharded_state_dicts.append(shared_weights.keys() ) # If we only have the shared weights (dummy model/experts saved on the same file) if len(SCREAMING_SNAKE_CASE ) == 1: __lowerCAmelCase : Any = os.path.join(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) torch.save(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) return {weights_name: sharded_state_dicts[0]}, None else: torch.save(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # Otherwise, let's build the index __lowerCAmelCase : List[Any] = {} for idx, shard in enumerate(SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Union[str, Any] = weights_name.replace(""".bin""" , F'''-{idx+1:05d}-of-{len(SCREAMING_SNAKE_CASE ):05d}.bin''' ) __lowerCAmelCase : List[Any] = os.path.join(SCREAMING_SNAKE_CASE , weights_name.replace(""".bin""" , F'''-{idx+1:05d}-of-???.bin''' ) ) os.rename(SCREAMING_SNAKE_CASE , os.path.join(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) for key in shard: __lowerCAmelCase : Any = shard_file # Add the metadata __lowerCAmelCase : Optional[Any] = {"""total_size""": total_size} __lowerCAmelCase : Dict = {"""metadata""": metadata, """weight_map""": weight_map} with open(os.path.join(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) , """w""" , encoding="""utf-8""" ) as f: __lowerCAmelCase : Optional[Any] = json.dumps(SCREAMING_SNAKE_CASE , indent=2 , sort_keys=SCREAMING_SNAKE_CASE ) + """\n""" f.write(SCREAMING_SNAKE_CASE ) return metadata, index if __name__ == "__main__": _UpperCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--nllb_moe_checkpoint_path', default='/home/arthur_huggingface_co/fairseq/weights/checkpoints/model_moe_54b/checkpoint_2_300000', type=str, required=False, help='Path to a directory containing a folder per layer. Follows the original Google format.', ) parser.add_argument('--dtype', default='float32', type=str, required=False, help='dtype of the saved model') parser.add_argument( '--pytorch_dump_folder_path', default='/home/arthur_huggingface_co/fairseq/weights/checkpoints/hf-converted-moe-54b', type=str, required=False, help='Path to the output pytorch model.', ) _UpperCAmelCase = parser.parse_args() _UpperCAmelCase , _UpperCAmelCase = shard_on_the_fly( args.nllb_moe_checkpoint_path, args.pytorch_dump_folder_path, 128, args.dtype, ) _UpperCAmelCase = NllbMoeConfig.from_pretrained( 'facebook/nllb-200-3.3B', encoder_sparse_step=4, decoder_sparse_step=4, num_experts=128 ) config.save_pretrained(args.pytorch_dump_folder_path) _UpperCAmelCase = NllbMoeModel.from_pretrained(args.pytorch_dump_folder_path) print('Done') model.save_pretrained(args.pytorch_dump_folder_path)
355
_UpperCAmelCase = {0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []} _UpperCAmelCase = {0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]} def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE :dict[int, list[int]] , SCREAMING_SNAKE_CASE :int , SCREAMING_SNAKE_CASE :list[bool] ) -> list[int]: __lowerCAmelCase : str = True __lowerCAmelCase : str = [] for neighbour in graph[vert]: if not visited[neighbour]: order += topology_sort(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) order.append(SCREAMING_SNAKE_CASE ) return order def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE :dict[int, list[int]] , SCREAMING_SNAKE_CASE :int , SCREAMING_SNAKE_CASE :list[bool] ) -> list[int]: __lowerCAmelCase : Optional[Any] = True __lowerCAmelCase : Union[str, Any] = [vert] for neighbour in reversed_graph[vert]: if not visited[neighbour]: component += find_components(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) return component def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE :dict[int, list[int]] ) -> list[list[int]]: __lowerCAmelCase : Optional[Any] = len(SCREAMING_SNAKE_CASE ) * [False] __lowerCAmelCase : dict[int, list[int]] = {vert: [] for vert in range(len(SCREAMING_SNAKE_CASE ) )} for vert, neighbours in graph.items(): for neighbour in neighbours: reversed_graph[neighbour].append(SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Tuple = [] for i, was_visited in enumerate(SCREAMING_SNAKE_CASE ): if not was_visited: order += topology_sort(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[Any] = [] __lowerCAmelCase : Any = len(SCREAMING_SNAKE_CASE ) * [False] for i in range(len(SCREAMING_SNAKE_CASE ) ): __lowerCAmelCase : Optional[int] = order[len(SCREAMING_SNAKE_CASE ) - i - 1] if not visited[vert]: __lowerCAmelCase : Any = find_components(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) components_list.append(SCREAMING_SNAKE_CASE ) return components_list
232
0
from ..utils import DummyObject, requires_backends class _a ( metaclass=snake_case_ ): """simple docstring""" _lowerCamelCase : str = ['torch', 'transformers', 'onnx'] def __init__( self : int , *UpperCAmelCase : str , **UpperCAmelCase : Any ): requires_backends(self , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Optional[int] , *UpperCAmelCase : int , **UpperCAmelCase : Union[str, Any] ): requires_backends(cls , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Optional[Any] , *UpperCAmelCase : Optional[int] , **UpperCAmelCase : Tuple ): requires_backends(cls , ["torch", "transformers", "onnx"] ) class _a ( metaclass=snake_case_ ): """simple docstring""" _lowerCamelCase : Tuple = ['torch', 'transformers', 'onnx'] def __init__( self : Tuple , *UpperCAmelCase : Dict , **UpperCAmelCase : Optional[Any] ): requires_backends(self , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Dict , *UpperCAmelCase : List[str] , **UpperCAmelCase : Optional[Any] ): requires_backends(cls , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Tuple , *UpperCAmelCase : Any , **UpperCAmelCase : Tuple ): requires_backends(cls , ["torch", "transformers", "onnx"] ) class _a ( metaclass=snake_case_ ): """simple docstring""" _lowerCamelCase : Any = ['torch', 'transformers', 'onnx'] def __init__( self : int , *UpperCAmelCase : Any , **UpperCAmelCase : str ): requires_backends(self , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Tuple , *UpperCAmelCase : Optional[Any] , **UpperCAmelCase : int ): requires_backends(cls , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : List[str] , *UpperCAmelCase : Any , **UpperCAmelCase : Tuple ): requires_backends(cls , ["torch", "transformers", "onnx"] ) class _a ( metaclass=snake_case_ ): """simple docstring""" _lowerCamelCase : List[Any] = ['torch', 'transformers', 'onnx'] def __init__( self : Optional[int] , *UpperCAmelCase : str , **UpperCAmelCase : Tuple ): requires_backends(self , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Optional[int] , *UpperCAmelCase : Optional[int] , **UpperCAmelCase : Union[str, Any] ): requires_backends(cls , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Union[str, Any] , *UpperCAmelCase : int , **UpperCAmelCase : Optional[Any] ): requires_backends(cls , ["torch", "transformers", "onnx"] ) class _a ( metaclass=snake_case_ ): """simple docstring""" _lowerCamelCase : List[Any] = ['torch', 'transformers', 'onnx'] def __init__( self : Any , *UpperCAmelCase : Union[str, Any] , **UpperCAmelCase : List[Any] ): requires_backends(self , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Dict , *UpperCAmelCase : Tuple , **UpperCAmelCase : List[Any] ): requires_backends(cls , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Any , *UpperCAmelCase : Dict , **UpperCAmelCase : int ): requires_backends(cls , ["torch", "transformers", "onnx"] ) class _a ( metaclass=snake_case_ ): """simple docstring""" _lowerCamelCase : int = ['torch', 'transformers', 'onnx'] def __init__( self : str , *UpperCAmelCase : int , **UpperCAmelCase : Optional[int] ): requires_backends(self , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : int , *UpperCAmelCase : Optional[int] , **UpperCAmelCase : Optional[Any] ): requires_backends(cls , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : str , *UpperCAmelCase : Any , **UpperCAmelCase : Optional[Any] ): requires_backends(cls , ["torch", "transformers", "onnx"] )
312
# flake8: noqa # Lint as: python3 from typing import Dict, List, Optional, Type from .. import config from ..utils import logging from .formatting import ( ArrowFormatter, CustomFormatter, Formatter, PandasFormatter, PythonFormatter, TensorFormatter, format_table, query_table, ) from .np_formatter import NumpyFormatter __a :Optional[Any] = logging.get_logger(__name__) __a :Dict[Optional[str], Type[Formatter]] = {} __a :Dict[Optional[str], str] = {} __a :Dict[Optional[str], Exception] = {} def __snake_case ( __UpperCamelCase : type ,__UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[List[str]] = None ,): """simple docstring""" A_ = aliases if aliases is not None else [] if format_type in _FORMAT_TYPES: logger.warning( f'''Overwriting format type \'{format_type}\' ({_FORMAT_TYPES[format_type].__name__} -> {formatter_cls.__name__})''' ) A_ = formatter_cls for alias in set(aliases + [format_type] ): if alias in _FORMAT_TYPES_ALIASES: logger.warning( f'''Overwriting format type alias \'{alias}\' ({_FORMAT_TYPES_ALIASES[alias]} -> {format_type})''' ) A_ = format_type def __snake_case ( __UpperCamelCase : Exception ,__UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[List[str]] = None ): """simple docstring""" A_ = aliases if aliases is not None else [] for alias in set(aliases + [format_type] ): A_ = unavailable_error # Here we define all the available formatting functions that can be used by `Dataset.set_format` _register_formatter(PythonFormatter, None, aliases=['python']) _register_formatter(ArrowFormatter, 'arrow', aliases=['pa', 'pyarrow']) _register_formatter(NumpyFormatter, 'numpy', aliases=['np']) _register_formatter(PandasFormatter, 'pandas', aliases=['pd']) _register_formatter(CustomFormatter, 'custom') if config.TORCH_AVAILABLE: from .torch_formatter import TorchFormatter _register_formatter(TorchFormatter, 'torch', aliases=['pt', 'pytorch']) else: __a :List[Any] = ValueError('PyTorch needs to be installed to be able to return PyTorch tensors.') _register_unavailable_formatter(_torch_error, 'torch', aliases=['pt', 'pytorch']) if config.TF_AVAILABLE: from .tf_formatter import TFFormatter _register_formatter(TFFormatter, 'tensorflow', aliases=['tf']) else: __a :List[str] = ValueError('Tensorflow needs to be installed to be able to return Tensorflow tensors.') _register_unavailable_formatter(_tf_error, 'tensorflow', aliases=['tf']) if config.JAX_AVAILABLE: from .jax_formatter import JaxFormatter _register_formatter(JaxFormatter, 'jax', aliases=[]) else: __a :Tuple = ValueError('JAX needs to be installed to be able to return JAX arrays.') _register_unavailable_formatter(_jax_error, 'jax', aliases=[]) def __snake_case ( __UpperCamelCase : Optional[str] ): """simple docstring""" if format_type in _FORMAT_TYPES_ALIASES: return _FORMAT_TYPES_ALIASES[format_type] else: return format_type def __snake_case ( __UpperCamelCase : Optional[str] ,**__UpperCamelCase : List[Any] ): """simple docstring""" A_ = get_format_type_from_alias(__UpperCamelCase ) if format_type in _FORMAT_TYPES: return _FORMAT_TYPES[format_type](**__UpperCamelCase ) if format_type in _FORMAT_TYPES_ALIASES_UNAVAILABLE: raise _FORMAT_TYPES_ALIASES_UNAVAILABLE[format_type] else: raise ValueError( f'''Return type should be None or selected in {list(type for type in _FORMAT_TYPES.keys() if type != None )}, but got \'{format_type}\'''' )
312
1
'''simple docstring''' from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer _A : Tuple =logging.get_logger(__name__) _A : List[Any] ={ 'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_config_file': 'tokenizer_config.json', } _A : str ={ 'vocab_file': { 'facebook/blenderbot_small-90M': 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json' }, 'merges_file': { 'facebook/blenderbot_small-90M': 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt' }, 'tokenizer_config_file': { 'facebook/blenderbot_small-90M': ( 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json' ) }, } _A : Optional[Any] ={ 'facebook/blenderbot_small-90M': 512, } class _lowercase ( lowercase__ ): a = VOCAB_FILES_NAMES a = PRETRAINED_VOCAB_FILES_MAP a = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a = BlenderbotSmallTokenizer def __init__( self: str , UpperCamelCase__: Optional[int]=None , UpperCamelCase__: Union[str, Any]=None , UpperCamelCase__: Tuple="<|endoftext|>" , UpperCamelCase__: int="<|endoftext|>" , UpperCamelCase__: Dict="<|endoftext|>" , UpperCamelCase__: Optional[Any]=False , UpperCamelCase__: List[Any]=True , **UpperCamelCase__: Any , ): super().__init__( ByteLevelBPETokenizer( vocab=_UpperCamelCase , merges=_UpperCamelCase , add_prefix_space=_UpperCamelCase , trim_offsets=_UpperCamelCase , ) , bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , unk_token=_UpperCamelCase , **_UpperCamelCase , ) lowerCamelCase__ : Any = add_prefix_space def lowerCamelCase_ ( self: Optional[Any] , UpperCamelCase__: int , UpperCamelCase__: Optional[Any]=None ): lowerCamelCase__ : List[Any] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def lowerCamelCase_ ( self: int , UpperCamelCase__: List[int] , UpperCamelCase__: Optional[List[int]] = None ): lowerCamelCase__ : 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]
358
'''simple docstring''' from typing import List, Optional, Union import numpy as np from ....audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function from ....feature_extraction_sequence_utils import SequenceFeatureExtractor from ....feature_extraction_utils import BatchFeature from ....file_utils import PaddingStrategy, TensorType from ....utils import logging _A : List[str] =logging.get_logger(__name__) class _lowercase ( _lowercase ): a = ["""input_features""", """attention_mask"""] def __init__( self: Optional[int] , UpperCamelCase__: str=80 , UpperCamelCase__: Union[str, Any]=16_000 , UpperCamelCase__: str=0.0 , UpperCamelCase__: Tuple=10 , UpperCamelCase__: Dict=25 , UpperCamelCase__: List[Any]="hamming_window" , UpperCamelCase__: Tuple=32_768.0 , UpperCamelCase__: List[Any]=0.97 , UpperCamelCase__: Optional[int]=1.0 , UpperCamelCase__: str=True , UpperCamelCase__: Optional[Any]=True , UpperCamelCase__: Optional[int]=False , **UpperCamelCase__: Optional[int] , ): super().__init__(feature_size=UpperCamelCase__ , sampling_rate=UpperCamelCase__ , padding_value=UpperCamelCase__ , **UpperCamelCase__ ) lowerCamelCase__ : Dict = feature_size lowerCamelCase__ : Tuple = sampling_rate lowerCamelCase__ : Union[str, Any] = padding_value lowerCamelCase__ : Union[str, Any] = hop_length lowerCamelCase__ : Any = win_length lowerCamelCase__ : Tuple = frame_signal_scale lowerCamelCase__ : Dict = preemphasis_coeff lowerCamelCase__ : Optional[Any] = mel_floor lowerCamelCase__ : Any = normalize_means lowerCamelCase__ : Optional[int] = normalize_vars lowerCamelCase__ : Dict = win_function lowerCamelCase__ : Dict = return_attention_mask lowerCamelCase__ : Optional[Any] = win_length * sampling_rate // 1_000 lowerCamelCase__ : Tuple = hop_length * sampling_rate // 1_000 lowerCamelCase__ : List[Any] = optimal_fft_length(self.sample_size ) lowerCamelCase__ : List[str] = (self.n_fft // 2) + 1 def lowerCamelCase_ ( self: Union[str, Any] , UpperCamelCase__: np.array ): if self.win_function == "hamming_window": lowerCamelCase__ : Dict = window_function(window_length=self.sample_size , name=self.win_function , periodic=UpperCamelCase__ ) else: lowerCamelCase__ : Union[str, Any] = window_function(window_length=self.sample_size , name=self.win_function ) lowerCamelCase__ : List[Any] = mel_filter_bank( num_frequency_bins=self.n_freqs , num_mel_filters=self.feature_size , min_frequency=0.0 , max_frequency=self.sampling_rate / 2.0 , sampling_rate=self.sampling_rate , ) lowerCamelCase__ : Tuple = spectrogram( one_waveform * self.frame_signal_scale , window=UpperCamelCase__ , frame_length=self.sample_size , hop_length=self.sample_stride , fft_length=self.n_fft , center=UpperCamelCase__ , preemphasis=self.preemphasis_coeff , mel_filters=UpperCamelCase__ , mel_floor=self.mel_floor , log_mel="""log""" , ) return msfc_features.T def lowerCamelCase_ ( self: Optional[int] , UpperCamelCase__: List[str] , UpperCamelCase__: List[Any] , UpperCamelCase__: Union[str, Any] ): # make sure we normalize float32 arrays if self.normalize_means: lowerCamelCase__ : List[str] = x[:input_length].mean(axis=0 ) lowerCamelCase__ : List[str] = np.subtract(UpperCamelCase__ , UpperCamelCase__ ) if self.normalize_vars: lowerCamelCase__ : Union[str, Any] = x[:input_length].std(axis=0 ) lowerCamelCase__ : str = np.divide(UpperCamelCase__ , UpperCamelCase__ ) if input_length < x.shape[0]: lowerCamelCase__ : List[Any] = padding_value # make sure array is in float32 lowerCamelCase__ : str = x.astype(np.floataa ) return x def lowerCamelCase_ ( self: str , UpperCamelCase__: List[np.ndarray] , UpperCamelCase__: Optional[np.ndarray] = None ): lowerCamelCase__ : Optional[int] = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [self._normalize_one(UpperCamelCase__ , UpperCamelCase__ , self.padding_value ) for x, n in zip(UpperCamelCase__ , UpperCamelCase__ )] def __call__( self: List[str] , UpperCamelCase__: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , UpperCamelCase__: Union[bool, str, PaddingStrategy] = False , UpperCamelCase__: Optional[int] = None , UpperCamelCase__: bool = False , UpperCamelCase__: Optional[int] = None , UpperCamelCase__: Optional[bool] = None , UpperCamelCase__: Optional[Union[str, TensorType]] = None , UpperCamelCase__: Optional[int] = None , **UpperCamelCase__: Tuple , ): 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.""" ) lowerCamelCase__ : List[str] = isinstance(UpperCamelCase__ , 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}''' ) lowerCamelCase__ : List[str] = is_batched_numpy or ( isinstance(UpperCamelCase__ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: lowerCamelCase__ : str = [np.asarray(UpperCamelCase__ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(UpperCamelCase__ , np.ndarray ): lowerCamelCase__ : List[Any] = np.asarray(UpperCamelCase__ , dtype=np.floataa ) elif isinstance(UpperCamelCase__ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): lowerCamelCase__ : Optional[Any] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: lowerCamelCase__ : Optional[Any] = [raw_speech] # extract fbank features lowerCamelCase__ : str = [self._extract_mfsc_features(UpperCamelCase__ ) for one_waveform in raw_speech] # convert into correct format for padding lowerCamelCase__ : str = BatchFeature({"""input_features""": features} ) lowerCamelCase__ : str = self.pad( UpperCamelCase__ , padding=UpperCamelCase__ , max_length=UpperCamelCase__ , truncation=UpperCamelCase__ , pad_to_multiple_of=UpperCamelCase__ , return_attention_mask=UpperCamelCase__ , **UpperCamelCase__ , ) # make sure list is in array format lowerCamelCase__ : str = padded_inputs.get("""input_features""" ) if isinstance(input_features[0] , UpperCamelCase__ ): lowerCamelCase__ : Dict = [np.asarray(UpperCamelCase__ , dtype=np.floataa ) for feature in input_features] lowerCamelCase__ : List[str] = padded_inputs.get("""attention_mask""" ) if attention_mask is not None: lowerCamelCase__ : Any = [np.asarray(UpperCamelCase__ , dtype=np.intaa ) for array in attention_mask] if self.normalize_means or self.normalize_vars: lowerCamelCase__ : str = ( np.array(UpperCamelCase__ , dtype=np.intaa ) if self._get_padding_strategies(UpperCamelCase__ , max_length=UpperCamelCase__ ) is not PaddingStrategy.DO_NOT_PAD and padding else None ) lowerCamelCase__ : Union[str, Any] = self.normalize( padded_inputs["""input_features"""] , attention_mask=UpperCamelCase__ ) if return_tensors is not None: lowerCamelCase__ : Dict = padded_inputs.convert_to_tensors(UpperCamelCase__ ) return padded_inputs
129
0
"""simple docstring""" import comet # From: unbabel-comet import torch import datasets _SCREAMING_SNAKE_CASE : int = datasets.logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Tuple = '''\ @inproceedings{rei-EtAl:2020:WMT, author = {Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon}, title = {Unbabel\'s Participation in the WMT20 Metrics Shared Task}, booktitle = {Proceedings of the Fifth Conference on Machine Translation}, month = {November}, year = {2020}, address = {Online}, publisher = {Association for Computational Linguistics}, pages = {909--918}, } @inproceedings{rei-etal-2020-comet, title = "{COMET}: A Neural Framework for {MT} Evaluation", author = "Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon", booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)", month = nov, year = "2020", address = "Online", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/2020.emnlp-main.213", pages = "2685--2702", } ''' _SCREAMING_SNAKE_CASE : Optional[int] = '''\ Crosslingual Optimized Metric for Evaluation of Translation (COMET) is an open-source framework used to train Machine Translation metrics that achieve high levels of correlation with different types of human judgments (HTER, DA\'s or MQM). With the release of the framework the authors also released fully trained models that were used to compete in the WMT20 Metrics Shared Task achieving SOTA in that years competition. See the [README.md] file at https://unbabel.github.io/COMET/html/models.html for more information. ''' _SCREAMING_SNAKE_CASE : List[Any] = ''' COMET score. Args: `sources` (list of str): Source sentences `predictions` (list of str): candidate translations `references` (list of str): reference translations `cuda` (bool): If set to True, runs COMET using GPU `show_progress` (bool): Shows progress `model`: COMET model to be used. Will default to `wmt-large-da-estimator-1719` if None. Returns: `samples`: List of dictionaries with `src`, `mt`, `ref` and `score`. `scores`: List of scores. Examples: >>> comet_metric = datasets.load_metric(\'comet\') >>> # comet_metric = load_metric(\'comet\', \'wmt20-comet-da\') # you can also choose which model to use >>> source = ["Dem Feuer konnte Einhalt geboten werden", "Schulen und Kindergärten wurden eröffnet."] >>> hypothesis = ["The fire could be stopped", "Schools and kindergartens were open"] >>> reference = ["They were able to control the fire.", "Schools and kindergartens opened"] >>> results = comet_metric.compute(predictions=hypothesis, references=reference, sources=source) >>> print([round(v, 2) for v in results["scores"]]) [0.19, 0.92] ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a ( datasets.Metric ): def UpperCamelCase ( self : int ) -> Union[str, Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='https://unbabel.github.io/COMET/html/index.html' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'sources': datasets.Value('string' , id='sequence' ), 'predictions': datasets.Value('string' , id='sequence' ), 'references': datasets.Value('string' , id='sequence' ), } ) , codebase_urls=['https://github.com/Unbabel/COMET'] , reference_urls=[ 'https://github.com/Unbabel/COMET', 'https://www.aclweb.org/anthology/2020.emnlp-main.213/', 'http://www.statmt.org/wmt20/pdf/2020.wmt-1.101.pdf6', ] , ) def UpperCamelCase ( self : Dict , __SCREAMING_SNAKE_CASE : str ) -> Tuple: if self.config_name == "default": lowerCamelCase_ = comet.load_from_checkpoint(comet.download_model('wmt20-comet-da' ) ) else: lowerCamelCase_ = comet.load_from_checkpoint(comet.download_model(self.config_name ) ) def UpperCamelCase ( self : str , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[int]=None , __SCREAMING_SNAKE_CASE : str=False ) -> Union[str, Any]: if gpus is None: lowerCamelCase_ = 1 if torch.cuda.is_available() else 0 lowerCamelCase_ = {'src': sources, 'mt': predictions, 'ref': references} lowerCamelCase_ = [dict(zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) for t in zip(*data.values() )] lowerCamelCase_ , lowerCamelCase_ = self.scorer.predict(__SCREAMING_SNAKE_CASE , gpus=__SCREAMING_SNAKE_CASE , progress_bar=__SCREAMING_SNAKE_CASE ) return {"mean_score": mean_score, "scores": scores}
183
"""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 a ( __snake_case ): def __init__( self : List[Any] , __SCREAMING_SNAKE_CASE : str = "▁" , __SCREAMING_SNAKE_CASE : bool = True , __SCREAMING_SNAKE_CASE : Union[str, AddedToken] = "<unk>" , __SCREAMING_SNAKE_CASE : Union[str, AddedToken] = "</s>" , __SCREAMING_SNAKE_CASE : Union[str, AddedToken] = "<pad>" , ) -> Optional[int]: lowerCamelCase_ = { 'pad': {'id': 0, 'token': pad_token}, 'eos': {'id': 1, 'token': eos_token}, 'unk': {'id': 2, 'token': unk_token}, } lowerCamelCase_ = [None] * len(self.special_tokens ) for token_dict in self.special_tokens.values(): lowerCamelCase_ = token_dict['token'] lowerCamelCase_ = Tokenizer(Unigram() ) lowerCamelCase_ = normalizers.Sequence( [ normalizers.Nmt(), normalizers.NFKC(), normalizers.Replace(Regex(' {2,}' ) , ' ' ), normalizers.Lowercase(), ] ) lowerCamelCase_ = pre_tokenizers.Sequence( [ pre_tokenizers.Metaspace(replacement=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ), pre_tokenizers.Digits(individual_digits=__SCREAMING_SNAKE_CASE ), pre_tokenizers.Punctuation(), ] ) lowerCamelCase_ = decoders.Metaspace(replacement=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = TemplateProcessing( single=F'''$A {self.special_tokens["eos"]["token"]}''' , special_tokens=[(self.special_tokens['eos']['token'], self.special_tokens['eos']['id'])] , ) lowerCamelCase_ = { 'model': 'SentencePieceUnigram', 'replacement': replacement, 'add_prefix_space': add_prefix_space, } super().__init__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def UpperCamelCase ( self : Tuple , __SCREAMING_SNAKE_CASE : Union[str, List[str]] , __SCREAMING_SNAKE_CASE : int = 8000 , __SCREAMING_SNAKE_CASE : bool = True , ) -> Tuple: lowerCamelCase_ = trainers.UnigramTrainer( vocab_size=__SCREAMING_SNAKE_CASE , special_tokens=self.special_tokens_list , show_progress=__SCREAMING_SNAKE_CASE , ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): lowerCamelCase_ = [files] self._tokenizer.train(__SCREAMING_SNAKE_CASE , trainer=__SCREAMING_SNAKE_CASE ) self.add_unk_id() def UpperCamelCase ( self : Tuple , __SCREAMING_SNAKE_CASE : Union[Iterator[str], Iterator[Iterator[str]]] , __SCREAMING_SNAKE_CASE : int = 8000 , __SCREAMING_SNAKE_CASE : bool = True , ) -> str: lowerCamelCase_ = trainers.UnigramTrainer( vocab_size=__SCREAMING_SNAKE_CASE , special_tokens=self.special_tokens_list , show_progress=__SCREAMING_SNAKE_CASE , ) self._tokenizer.train_from_iterator(__SCREAMING_SNAKE_CASE , trainer=__SCREAMING_SNAKE_CASE ) self.add_unk_id() def UpperCamelCase ( self : List[Any] ) -> Dict: lowerCamelCase_ = json.loads(self._tokenizer.to_str() ) lowerCamelCase_ = self.special_tokens['unk']['id'] lowerCamelCase_ = Tokenizer.from_str(json.dumps(__SCREAMING_SNAKE_CASE ) )
183
1
'''simple docstring''' def A_( A : float , A : float): return price * (1 + tax_rate) if __name__ == "__main__": print(f"""{price_plus_tax(1_00, 0.25) = }""") print(f"""{price_plus_tax(125.50, 0.05) = }""")
251
'''simple docstring''' from __future__ import annotations from statistics import mean def A_( A : list[int] , A : list[int] , A : int): UpperCamelCase = [0] * no_of_processes UpperCamelCase = [0] * no_of_processes # Initialize remaining_time to waiting_time. for i in range(A): UpperCamelCase = burst_time[i] UpperCamelCase = [] UpperCamelCase = 0 UpperCamelCase = 0 # When processes are not completed, # A process whose arrival time has passed \ # and has remaining execution time is put into the ready_process. # The shortest process in the ready_process, target_process is executed. while completed != no_of_processes: UpperCamelCase = [] UpperCamelCase = -1 for i in range(A): if (arrival_time[i] <= total_time) and (remaining_time[i] > 0): ready_process.append(A) if len(A) > 0: UpperCamelCase = ready_process[0] for i in ready_process: if remaining_time[i] < remaining_time[target_process]: UpperCamelCase = i total_time += burst_time[target_process] completed += 1 UpperCamelCase = 0 UpperCamelCase = ( total_time - arrival_time[target_process] - burst_time[target_process] ) else: total_time += 1 return waiting_time def A_( A : list[int] , A : int , A : list[int]): UpperCamelCase = [0] * no_of_processes for i in range(A): UpperCamelCase = burst_time[i] + waiting_time[i] return turn_around_time if __name__ == "__main__": print('[TEST CASE 01]') lowerCAmelCase : int = 4 lowerCAmelCase : Any = [2, 5, 3, 7] lowerCAmelCase : int = [0, 0, 0, 0] lowerCAmelCase : Dict = calculate_waitingtime(arrival_time, burst_time, no_of_processes) lowerCAmelCase : Optional[Any] = calculate_turnaroundtime( burst_time, no_of_processes, waiting_time ) # Printing the Result print('PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time') for i, process_id in enumerate(list(range(1, 5))): print( f"""{process_id}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t""" f"""{waiting_time[i]}\t\t\t\t{turn_around_time[i]}""" ) print(f"""\nAverage waiting time = {mean(waiting_time):.5f}""") print(f"""Average turnaround time = {mean(turn_around_time):.5f}""")
251
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase :int = { '''configuration_clap''': [ '''CLAP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ClapAudioConfig''', '''ClapConfig''', '''ClapTextConfig''', ], '''processing_clap''': ['''ClapProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase :Dict = [ '''CLAP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ClapModel''', '''ClapPreTrainedModel''', '''ClapTextModel''', '''ClapTextModelWithProjection''', '''ClapAudioModel''', '''ClapAudioModelWithProjection''', ] lowerCAmelCase :str = ['''ClapFeatureExtractor'''] if TYPE_CHECKING: from .configuration_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioConfig, ClapConfig, ClapTextConfig, ) from .processing_clap import ClapProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clap import ClapFeatureExtractor from .modeling_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioModel, ClapAudioModelWithProjection, ClapModel, ClapPreTrainedModel, ClapTextModel, ClapTextModelWithProjection, ) else: import sys lowerCAmelCase :int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
331
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', 'transformers') lowerCamelCase = '\n{0} = None\n' lowerCamelCase = '\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n' lowerCamelCase = '\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n' class A ( unittest.TestCase ): def lowerCamelCase ( self : Tuple ) -> Union[str, Any]: """simple docstring""" _lowerCamelCase : int =find_backend(' _import_structure["models.albert"].append("AlbertTokenizerFast")' ) self.assertIsNone(lowercase_ ) _lowerCamelCase : List[str] =find_backend(' if not is_tokenizers_available():' ) self.assertEqual(lowercase_ , 'tokenizers' ) _lowerCamelCase : List[Any] =find_backend(' if not is_tensorflow_text_available():' ) self.assertEqual(lowercase_ , 'tensorflow_text' ) _lowerCamelCase : int =find_backend(' if not (is_sentencepiece_available() and is_tokenizers_available()):' ) self.assertEqual(lowercase_ , 'sentencepiece_and_tokenizers' ) _lowerCamelCase : Dict =find_backend( ' if not (is_sentencepiece_available() and is_tensorflow_text_available()):' ) self.assertEqual(lowercase_ , 'sentencepiece_and_tensorflow_text' ) _lowerCamelCase : List[Any] =find_backend( ' if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):' ) self.assertEqual(lowercase_ , 'sentencepiece_and_tokenizers_and_vision' ) def lowerCamelCase ( self : Optional[Any] ) -> List[str]: """simple docstring""" _lowerCamelCase : Union[str, 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' , lowercase_ ) self.assertIn('tensorflow_text' , lowercase_ ) self.assertIn('sentencepiece_and_tokenizers' , lowercase_ ) # Likewise, we can't assert on the exact content of a key self.assertIn('BertModel' , objects['torch'] ) self.assertIn('TFBertModel' , objects['tf'] ) self.assertIn('FlaxBertModel' , objects['flax'] ) self.assertIn('BertModel' , objects['torch'] ) self.assertIn('TFBertTokenizer' , objects['tensorflow_text'] ) self.assertIn('convert_slow_tokenizer' , objects['sentencepiece_and_tokenizers'] ) def lowerCamelCase ( self : int ) -> Optional[Any]: """simple docstring""" _lowerCamelCase : Optional[Any] =create_dummy_object('CONSTANT' , '\'torch\'' ) self.assertEqual(lowercase_ , '\nCONSTANT = None\n' ) _lowerCamelCase : Dict =create_dummy_object('function' , '\'torch\'' ) self.assertEqual( lowercase_ , '\ndef function(*args, **kwargs):\n requires_backends(function, \'torch\')\n' ) _lowerCamelCase : Union[str, Any] ='\nclass FakeClass(metaclass=DummyObject):\n _backends = \'torch\'\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, \'torch\')\n' _lowerCamelCase : Tuple =create_dummy_object('FakeClass' , '\'torch\'' ) self.assertEqual(lowercase_ , lowercase_ ) def lowerCamelCase ( self : List[Any] ) -> Dict: """simple docstring""" _lowerCamelCase : Dict ='# This file is autogenerated by the command `make fix-copies`, do not edit.\nfrom ..utils import DummyObject, requires_backends\n\n\nCONSTANT = None\n\n\ndef function(*args, **kwargs):\n requires_backends(function, ["torch"])\n\n\nclass FakeClass(metaclass=DummyObject):\n _backends = ["torch"]\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, ["torch"])\n' _lowerCamelCase : Optional[int] =create_dummy_files({'torch': ['CONSTANT', 'function', 'FakeClass']} ) self.assertEqual(dummy_files['torch'] , lowercase_ )
199
0
"""simple docstring""" from __future__ import annotations import random import unittest from transformers import TransfoXLConfig, 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 from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, TFTransfoXLForSequenceClassification, TFTransfoXLLMHeadModel, TFTransfoXLModel, ) class _UpperCAmelCase : '''simple docstring''' def __init__( self , A , ) -> str: _UpperCAmelCase : Dict = parent _UpperCAmelCase : Any = 1_3 _UpperCAmelCase : List[Any] = 7 _UpperCAmelCase : List[Any] = 3_0 _UpperCAmelCase : Optional[int] = self.seq_length + self.mem_len _UpperCAmelCase : List[str] = 1_5 _UpperCAmelCase : Union[str, Any] = True _UpperCAmelCase : List[str] = True _UpperCAmelCase : str = 9_9 _UpperCAmelCase : Any = [1_0, 5_0, 8_0] _UpperCAmelCase : Tuple = 3_2 _UpperCAmelCase : Optional[Any] = 3_2 _UpperCAmelCase : List[Any] = 4 _UpperCAmelCase : int = 8 _UpperCAmelCase : List[str] = 1_2_8 _UpperCAmelCase : str = 2 _UpperCAmelCase : Dict = 2 _UpperCAmelCase : Union[str, Any] = None _UpperCAmelCase : Tuple = 1 _UpperCAmelCase : Tuple = 0 _UpperCAmelCase : List[str] = 3 _UpperCAmelCase : List[Any] = self.vocab_size - 1 _UpperCAmelCase : int = 0.01 def __lowerCAmelCase ( self ) -> Dict: _UpperCAmelCase : str = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCAmelCase : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCAmelCase : str = None if self.use_labels: _UpperCAmelCase : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCAmelCase : Tuple = TransfoXLConfig( vocab_size=self.vocab_size , mem_len=self.mem_len , clamp_len=self.clamp_len , cutoffs=self.cutoffs , d_model=self.hidden_size , d_embed=self.d_embed , n_head=self.num_attention_heads , d_head=self.d_head , d_inner=self.d_inner , div_val=self.div_val , n_layer=self.num_hidden_layers , eos_token_id=self.eos_token_id , pad_token_id=self.vocab_size - 1 , init_range=self.init_range , num_labels=self.num_labels , ) return (config, input_ids_a, input_ids_a, lm_labels) def __lowerCAmelCase ( self ) -> Dict: random.seed(self.seed ) tf.random.set_seed(self.seed ) def __lowerCAmelCase ( self , A , A , A , A ) -> Tuple: _UpperCAmelCase : int = TFTransfoXLModel(A ) _UpperCAmelCase : Any = model(A ).to_tuple() _UpperCAmelCase : Union[str, Any] = {'''input_ids''': input_ids_a, '''mems''': mems_a} _UpperCAmelCase : Union[str, Any] = model(A ).to_tuple() self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __lowerCAmelCase ( self , A , A , A , A ) -> Tuple: _UpperCAmelCase : Optional[int] = TFTransfoXLLMHeadModel(A ) _UpperCAmelCase : Union[str, Any] = model(A ).to_tuple() _UpperCAmelCase : Union[str, Any] = {'''input_ids''': input_ids_a, '''labels''': lm_labels} _UpperCAmelCase : Any = model(A ).to_tuple() _UpperCAmelCase : Any = model([input_ids_a, mems_a] ).to_tuple() _UpperCAmelCase : str = {'''input_ids''': input_ids_a, '''mems''': mems_a, '''labels''': lm_labels} _UpperCAmelCase : Tuple = model(A ).to_tuple() self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __lowerCAmelCase ( self , A , A , A , A ) -> List[Any]: _UpperCAmelCase : Union[str, Any] = TFTransfoXLForSequenceClassification(A ) _UpperCAmelCase : Optional[Any] = model(A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self ) -> Optional[Any]: _UpperCAmelCase : Optional[int] = self.prepare_config_and_inputs() (_UpperCAmelCase) : Dict = config_and_inputs _UpperCAmelCase : str = {'''input_ids''': input_ids_a} return config, inputs_dict @require_tf class _UpperCAmelCase ( a ,a ,unittest.TestCase ): '''simple docstring''' a__ =( (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else () ) a__ =() if is_tf_available() else () a__ =( { '''feature-extraction''': TFTransfoXLModel, '''text-classification''': TFTransfoXLForSequenceClassification, '''text-generation''': TFTransfoXLLMHeadModel, '''zero-shot''': TFTransfoXLForSequenceClassification, } if is_tf_available() else {} ) # TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented a__ =False a__ =False a__ =False a__ =False def __lowerCAmelCase ( self , A , A , A , A , A ) -> List[str]: if pipeline_test_casse_name == "TextGenerationPipelineTests": # Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`. # `TransfoXLConfig` was never used in pipeline tests: cannot create a simple # tokenizer. return True return False def __lowerCAmelCase ( self ) -> Tuple: _UpperCAmelCase : Dict = TFTransfoXLModelTester(self ) _UpperCAmelCase : Optional[int] = ConfigTester(self , config_class=A , d_embed=3_7 ) def __lowerCAmelCase ( self ) -> int: self.config_tester.run_common_tests() def __lowerCAmelCase ( self ) -> Optional[int]: self.model_tester.set_seed() _UpperCAmelCase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_model(*A ) def __lowerCAmelCase ( self ) -> List[Any]: self.model_tester.set_seed() _UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_lm_head(*A ) def __lowerCAmelCase ( self ) -> Optional[int]: _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*A ) def __lowerCAmelCase ( self ) -> List[Any]: _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase : List[str] = [TFTransfoXLForSequenceClassification] for model_class in self.all_model_classes: _UpperCAmelCase : Union[str, Any] = model_class(A ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class in list_other_models_with_output_ebd: _UpperCAmelCase : Dict = model.get_output_embeddings() assert isinstance(A , tf.keras.layers.Layer ) _UpperCAmelCase : Tuple = model.get_bias() assert name is None else: _UpperCAmelCase : str = model.get_output_embeddings() assert x is None _UpperCAmelCase : Optional[int] = model.get_bias() assert name is None def __lowerCAmelCase ( self ) -> Optional[Any]: # TODO JP: Make TransfoXL XLA compliant pass @slow def __lowerCAmelCase ( self ) -> Optional[int]: for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCAmelCase : int = TFTransfoXLModel.from_pretrained(A ) self.assertIsNotNone(A ) @unittest.skip(reason='''This model doesn\'t play well with fit() due to not returning a single loss.''' ) def __lowerCAmelCase ( self ) -> Optional[Any]: pass @require_tf class _UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @unittest.skip('''Skip test until #12651 is resolved.''' ) @slow def __lowerCAmelCase ( self ) -> Dict: _UpperCAmelCase : str = TFTransfoXLLMHeadModel.from_pretrained('''transfo-xl-wt103''' ) # fmt: off _UpperCAmelCase : Any = tf.convert_to_tensor([[3_3,1_2_9_7,2,1,1_0_0_9,4,1_1_0_9,1_1_7_3_9,4_7_6_2,3_5_8,5,2_5,2_4_5,2_2,1_7_0_6,1_7,2_0_0_9_8,5,3_2_1_5,2_1,3_7,1_1_1_0,3,1_3,1_0_4_1,4,2_4,6_0_3,4_9_0,2,7_1_4_7_7,2_0_0_9_8,1_0_4_4_4_7,2,2_0_9_6_1,1,2_6_0_4,4,1,3_2_9,3,6_2_2_4,8_3_1,1_6_0_0_2,2,8,6_0_3,7_8_9_6_7,2_9_5_4_6,2_3,8_0_3,2_0,2_5,4_1_6,5,8,2_3_2,4,2_7_7,6,1_8_5_5,4_6_0_1,3,2_9_5_4_6,5_4,8,3_6_0_9,5,5_7_2_1_1,4_9,4,1,2_7_7,1_8,8,1_7_5_5,1_5_6_9_1,3,3_4_1,2_5,4_1_6,6_9_3,4_2_5_7_3,7_1,1_7,4_0_1,9_4,3_1,1_7_9_1_9,2,2_9_5_4_6,7_8_7_3,1_8,1,4_3_5,2_3,1_1_0_1_1,7_5_5,5,5_1_6_7,3,7_9_8_3,9_8,8_4,2,2_9_5_4_6,3_2_6_7,8,3_6_0_9,4,1,4_8_6_5,1_0_7_5,2,6_0_8_7,7_1,6,3_4_6,8,5_8_5_4,3,2_9_5_4_6,8_2_4,1_4_0_0,1_8_6_8,2,1_9,1_6_0,2,3_1_1,8,5_4_9_6,2,2_0_9_2_0,1_7,2_5,1_5_0_9_7,3,2_4,2_4,0]] , dtype=tf.intaa ) # noqa: E231 # fmt: on # In 1991 , the remains of Russian Tsar Nicholas II and his family # ( except for Alexei and Maria ) are discovered . # The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the # remainder of the story . 1883 Western Siberia , # a young Grigori Rasputin is asked by his father and a group of men to perform magic . # Rasputin has a vision and denounces one of the men as a horse thief . Although his # father initially slaps him for making such an accusation , Rasputin watches as the # man is chased outside and beaten . Twenty years later , Rasputin sees a vision of # the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous , # with people , even a bishop , begging for his blessing . <eod> </s> <eos> # fmt: off _UpperCAmelCase : int = [3_3,1_2_9_7,2,1,1_0_0_9,4,1_1_0_9,1_1_7_3_9,4_7_6_2,3_5_8,5,2_5,2_4_5,2_2,1_7_0_6,1_7,2_0_0_9_8,5,3_2_1_5,2_1,3_7,1_1_1_0,3,1_3,1_0_4_1,4,2_4,6_0_3,4_9_0,2,7_1_4_7_7,2_0_0_9_8,1_0_4_4_4_7,2,2_0_9_6_1,1,2_6_0_4,4,1,3_2_9,3,6_2_2_4,8_3_1,1_6_0_0_2,2,8,6_0_3,7_8_9_6_7,2_9_5_4_6,2_3,8_0_3,2_0,2_5,4_1_6,5,8,2_3_2,4,2_7_7,6,1_8_5_5,4_6_0_1,3,2_9_5_4_6,5_4,8,3_6_0_9,5,5_7_2_1_1,4_9,4,1,2_7_7,1_8,8,1_7_5_5,1_5_6_9_1,3,3_4_1,2_5,4_1_6,6_9_3,4_2_5_7_3,7_1,1_7,4_0_1,9_4,3_1,1_7_9_1_9,2,2_9_5_4_6,7_8_7_3,1_8,1,4_3_5,2_3,1_1_0_1_1,7_5_5,5,5_1_6_7,3,7_9_8_3,9_8,8_4,2,2_9_5_4_6,3_2_6_7,8,3_6_0_9,4,1,4_8_6_5,1_0_7_5,2,6_0_8_7,7_1,6,3_4_6,8,5_8_5_4,3,2_9_5_4_6,8_2_4,1_4_0_0,1_8_6_8,2,1_9,1_6_0,2,3_1_1,8,5_4_9_6,2,2_0_9_2_0,1_7,2_5,1_5_0_9_7,3,2_4,2_4,0,3_3,1,1_8_5_7,2,1,1_0_0_9,4,1_1_0_9,1_1_7_3_9,4_7_6_2,3_5_8,5,2_5,2_4_5,2_8,1_1_1_0,3,1_3,1_0_4_1,4,2_4,6_0_3,4_9_0,2,7_1_4_7_7,2_0_0_9_8,1_0_4_4_4_7,2,2_0_9_6_1,1,2_6_0_4,4,1,3_2_9,3,0] # noqa: E231 # fmt: on # In 1991, the remains of Russian Tsar Nicholas II and his family ( # except for Alexei and Maria ) are discovered. The voice of young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story. # 1883 Western Siberia, a young Grigori Rasputin is asked by his father # and a group of men to perform magic. Rasputin has a vision and # denounces one of the men as a horse thief. Although his father initially # slaps him for making such an accusation, Rasputin watches as the man # is chased outside and beaten. Twenty years later, Rasputin sees a vision # of the Virgin Mary, prompting him to become a priest. # Rasputin quickly becomes famous, with people, even a bishop, begging for # his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar # Nicholas II and his family were discovered. The voice of <unk> young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos> _UpperCAmelCase : Any = model.generate(A , max_length=2_0_0 , do_sample=A ) self.assertListEqual(output_ids[0].numpy().tolist() , A )
352
"""simple docstring""" from ...processing_utils import ProcessorMixin class _UpperCAmelCase ( a ): '''simple docstring''' a__ ='''WhisperFeatureExtractor''' a__ ='''WhisperTokenizer''' def __init__( self , A , A ) -> Any: super().__init__(A , A ) _UpperCAmelCase : int = self.feature_extractor _UpperCAmelCase : List[str] = False def __lowerCAmelCase ( self , A=None , A=None , A=True ) -> Optional[int]: return self.tokenizer.get_decoder_prompt_ids(task=A , language=A , no_timestamps=A ) def __call__( self , *A , **A ) -> Tuple: # For backward compatibility if self._in_target_context_manager: return self.current_processor(*A , **A ) _UpperCAmelCase : str = kwargs.pop('''audio''' , A ) _UpperCAmelCase : Dict = kwargs.pop('''sampling_rate''' , A ) _UpperCAmelCase : Dict = kwargs.pop('''text''' , A ) if len(A ) > 0: _UpperCAmelCase : List[Any] = args[0] _UpperCAmelCase : Union[str, 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: _UpperCAmelCase : Optional[Any] = self.feature_extractor(A , *A , sampling_rate=A , **A ) if text is not None: _UpperCAmelCase : Any = self.tokenizer(A , **A ) if text is None: return inputs elif audio is None: return encodings else: _UpperCAmelCase : int = encodings['''input_ids'''] return inputs def __lowerCAmelCase ( self , *A , **A ) -> Optional[Any]: return self.tokenizer.batch_decode(*A , **A ) def __lowerCAmelCase ( self , *A , **A ) -> Any: return self.tokenizer.decode(*A , **A ) def __lowerCAmelCase ( self , A , A="np" ) -> Any: return self.tokenizer.get_prompt_ids(A , return_tensors=A )
68
0
from math import sqrt def _A ( _lowercase = 1_00_00_00 ) -> int: """simple docstring""" __UpperCamelCase = 0 __UpperCamelCase = 0 __UpperCamelCase = 42 while num_cuboids <= limit: max_cuboid_size += 1 for sum_shortest_sides in range(2 , 2 * max_cuboid_size + 1 ): if sqrt(sum_shortest_sides**2 + max_cuboid_size**2 ).is_integer(): num_cuboids += ( min(_lowercase , sum_shortest_sides // 2 ) - max(1 , sum_shortest_sides - max_cuboid_size ) + 1 ) return max_cuboid_size if __name__ == "__main__": print(f"""{solution() = }""")
310
import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class __lowerCamelCase (_a ): _lowercase = ["""image_processor""", """tokenizer"""] _lowercase = """OwlViTImageProcessor""" _lowercase = ("""CLIPTokenizer""", """CLIPTokenizerFast""") def __init__( self: int,A_: Tuple=None,A_: int=None,**A_: int ): '''simple docstring''' __UpperCamelCase = None if "feature_extractor" in kwargs: warnings.warn( 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`' ' instead.',A_,) __UpperCamelCase = kwargs.pop('feature_extractor' ) __UpperCamelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('You need to specify an `image_processor`.' ) if tokenizer is None: raise ValueError('You need to specify a `tokenizer`.' ) super().__init__(A_,A_ ) def __call__( self: str,A_: Dict=None,A_: Optional[int]=None,A_: Any=None,A_: Tuple="max_length",A_: int="np",**A_: Optional[Any] ): '''simple docstring''' if text is None and query_images is None and images is None: raise ValueError( 'You have to specify at least one text or query image or image. All three cannot be none.' ) if text is not None: if isinstance(A_,A_ ) or (isinstance(A_,A_ ) and not isinstance(text[0],A_ )): __UpperCamelCase = [self.tokenizer(A_,padding=A_,return_tensors=A_,**A_ )] elif isinstance(A_,A_ ) and isinstance(text[0],A_ ): __UpperCamelCase = [] # Maximum number of queries across batch __UpperCamelCase = max([len(A_ ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(A_ ) != max_num_queries: __UpperCamelCase = t + [' '] * (max_num_queries - len(A_ )) __UpperCamelCase = self.tokenizer(A_,padding=A_,return_tensors=A_,**A_ ) encodings.append(A_ ) else: raise TypeError('Input text should be a string, a list of strings or a nested list of strings' ) if return_tensors == "np": __UpperCamelCase = np.concatenate([encoding['input_ids'] for encoding in encodings],axis=0 ) __UpperCamelCase = np.concatenate([encoding['attention_mask'] for encoding in encodings],axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp __UpperCamelCase = jnp.concatenate([encoding['input_ids'] for encoding in encodings],axis=0 ) __UpperCamelCase = jnp.concatenate([encoding['attention_mask'] for encoding in encodings],axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch __UpperCamelCase = torch.cat([encoding['input_ids'] for encoding in encodings],dim=0 ) __UpperCamelCase = torch.cat([encoding['attention_mask'] for encoding in encodings],dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf __UpperCamelCase = tf.stack([encoding['input_ids'] for encoding in encodings],axis=0 ) __UpperCamelCase = tf.stack([encoding['attention_mask'] for encoding in encodings],axis=0 ) else: raise ValueError('Target return tensor type could not be returned' ) __UpperCamelCase = BatchEncoding() __UpperCamelCase = input_ids __UpperCamelCase = attention_mask if query_images is not None: __UpperCamelCase = BatchEncoding() __UpperCamelCase = self.image_processor( A_,return_tensors=A_,**A_ ).pixel_values __UpperCamelCase = query_pixel_values if images is not None: __UpperCamelCase = self.image_processor(A_,return_tensors=A_,**A_ ) if text is not None and images is not None: __UpperCamelCase = image_features.pixel_values return encoding elif query_images is not None and images is not None: __UpperCamelCase = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**A_ ),tensor_type=A_ ) def snake_case_ ( self: Optional[int],*A_: int,**A_: List[Any] ): '''simple docstring''' return self.image_processor.post_process(*A_,**A_ ) def snake_case_ ( self: str,*A_: Optional[int],**A_: List[Any] ): '''simple docstring''' return self.image_processor.post_process_object_detection(*A_,**A_ ) def snake_case_ ( self: str,*A_: Tuple,**A_: int ): '''simple docstring''' return self.image_processor.post_process_image_guided_detection(*A_,**A_ ) def snake_case_ ( self: List[str],*A_: str,**A_: List[Any] ): '''simple docstring''' return self.tokenizer.batch_decode(*A_,**A_ ) def snake_case_ ( self: int,*A_: Any,**A_: Tuple ): '''simple docstring''' return self.tokenizer.decode(*A_,**A_ ) @property def snake_case_ ( self: Optional[Any] ): '''simple docstring''' warnings.warn( '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.',A_,) return self.image_processor_class @property def snake_case_ ( self: Union[str, Any] ): '''simple docstring''' warnings.warn( '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.',A_,) return self.image_processor
310
1
'''simple docstring''' from math import acos, sin from typing import List, Tuple, Union import numpy as np import torch from PIL import Image from ...models import AutoencoderKL, UNetaDConditionModel from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import randn_tensor from ..pipeline_utils import AudioPipelineOutput, BaseOutput, DiffusionPipeline, ImagePipelineOutput from .mel import Mel class __UpperCAmelCase ( a__ ): __lowercase = ["vqvae"] def __init__( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , ): """simple docstring""" super().__init__() self.register_modules(unet=SCREAMING_SNAKE_CASE_ , scheduler=SCREAMING_SNAKE_CASE_ , mel=SCREAMING_SNAKE_CASE_ , vqvae=SCREAMING_SNAKE_CASE_ ) def lowerCamelCase ( self ): """simple docstring""" return 50 if isinstance(self.scheduler , SCREAMING_SNAKE_CASE_ ) else 10_00 @torch.no_grad() def __call__( self , lowerCAmelCase_ = 1 , lowerCAmelCase_ = None , lowerCAmelCase_ = None , lowerCAmelCase_ = 0 , lowerCAmelCase_ = 0 , lowerCAmelCase_ = None , lowerCAmelCase_ = None , lowerCAmelCase_ = 0 , lowerCAmelCase_ = 0 , lowerCAmelCase_ = None , lowerCAmelCase_ = 0 , lowerCAmelCase_ = None , lowerCAmelCase_ = None , lowerCAmelCase_=True , ): """simple docstring""" _snake_case = steps or self.get_default_steps() self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE_ ) _snake_case = step_generator or generator # For backwards compatibility if type(self.unet.config.sample_size ) == int: _snake_case = (self.unet.config.sample_size, self.unet.config.sample_size) if noise is None: _snake_case = randn_tensor( ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size[0], self.unet.config.sample_size[1], ) , generator=SCREAMING_SNAKE_CASE_ , device=self.device , ) _snake_case = noise _snake_case = None if audio_file is not None or raw_audio is not None: self.mel.load_audio(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) _snake_case = self.mel.audio_slice_to_image(SCREAMING_SNAKE_CASE_ ) _snake_case = np.frombuffer(input_image.tobytes() , dtype='uint8' ).reshape( (input_image.height, input_image.width) ) _snake_case = (input_image / 2_55) * 2 - 1 _snake_case = torch.tensor(input_image[np.newaxis, :, :] , dtype=torch.float ).to(self.device ) if self.vqvae is not None: _snake_case = self.vqvae.encode(torch.unsqueeze(SCREAMING_SNAKE_CASE_ , 0 ) ).latent_dist.sample( generator=SCREAMING_SNAKE_CASE_ )[0] _snake_case = self.vqvae.config.scaling_factor * input_images if start_step > 0: _snake_case = self.scheduler.add_noise(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.scheduler.timesteps[start_step - 1] ) _snake_case = ( self.unet.config.sample_size[1] * self.mel.get_sample_rate() / self.mel.x_res / self.mel.hop_length ) _snake_case = int(mask_start_secs * pixels_per_second ) _snake_case = int(mask_end_secs * pixels_per_second ) _snake_case = self.scheduler.add_noise(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , torch.tensor(self.scheduler.timesteps[start_step:] ) ) for step, t in enumerate(self.progress_bar(self.scheduler.timesteps[start_step:] ) ): if isinstance(self.unet , SCREAMING_SNAKE_CASE_ ): _snake_case = self.unet(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )['sample'] else: _snake_case = self.unet(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )['sample'] if isinstance(self.scheduler , SCREAMING_SNAKE_CASE_ ): _snake_case = self.scheduler.step( model_output=SCREAMING_SNAKE_CASE_ , timestep=SCREAMING_SNAKE_CASE_ , sample=SCREAMING_SNAKE_CASE_ , eta=SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , )['prev_sample'] else: _snake_case = self.scheduler.step( model_output=SCREAMING_SNAKE_CASE_ , timestep=SCREAMING_SNAKE_CASE_ , sample=SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , )['prev_sample'] if mask is not None: if mask_start > 0: _snake_case = mask[:, step, :, :mask_start] if mask_end > 0: _snake_case = mask[:, step, :, -mask_end:] if self.vqvae is not None: # 0.18215 was scaling factor used in training to ensure unit variance _snake_case = 1 / self.vqvae.config.scaling_factor * images _snake_case = self.vqvae.decode(SCREAMING_SNAKE_CASE_ )['sample'] _snake_case = (images / 2 + 0.5).clamp(0 , 1 ) _snake_case = images.cpu().permute(0 , 2 , 3 , 1 ).numpy() _snake_case = (images * 2_55).round().astype('uint8' ) _snake_case = list( (Image.fromarray(_[:, :, 0] ) for _ in images) if images.shape[3] == 1 else (Image.fromarray(SCREAMING_SNAKE_CASE_ , mode='RGB' ).convert('L' ) for _ in images) ) _snake_case = [self.mel.image_to_audio(SCREAMING_SNAKE_CASE_ ) for _ in images] if not return_dict: return images, (self.mel.get_sample_rate(), audios) return BaseOutput(**AudioPipelineOutput(np.array(SCREAMING_SNAKE_CASE_ )[:, np.newaxis, :] ) , **ImagePipelineOutput(SCREAMING_SNAKE_CASE_ ) ) @torch.no_grad() def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ = 50 ): """simple docstring""" assert isinstance(self.scheduler , SCREAMING_SNAKE_CASE_ ) self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE_ ) _snake_case = np.array( [np.frombuffer(image.tobytes() , dtype='uint8' ).reshape((1, image.height, image.width) ) for image in images] ) _snake_case = (sample / 2_55) * 2 - 1 _snake_case = torch.Tensor(SCREAMING_SNAKE_CASE_ ).to(self.device ) for t in self.progress_bar(torch.flip(self.scheduler.timesteps , (0,) ) ): _snake_case = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps _snake_case = self.scheduler.alphas_cumprod[t] _snake_case = ( self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod ) _snake_case = 1 - alpha_prod_t _snake_case = self.unet(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )['sample'] _snake_case = (1 - alpha_prod_t_prev) ** 0.5 * model_output _snake_case = (sample - pred_sample_direction) * alpha_prod_t_prev ** (-0.5) _snake_case = sample * alpha_prod_t ** 0.5 + beta_prod_t ** 0.5 * model_output return sample @staticmethod def lowerCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ): """simple docstring""" _snake_case = acos(torch.dot(torch.flatten(SCREAMING_SNAKE_CASE_ ) , torch.flatten(SCREAMING_SNAKE_CASE_ ) ) / torch.norm(SCREAMING_SNAKE_CASE_ ) / torch.norm(SCREAMING_SNAKE_CASE_ ) ) return sin((1 - alpha) * theta ) * xa / sin(SCREAMING_SNAKE_CASE_ ) + sin(alpha * theta ) * xa / sin(SCREAMING_SNAKE_CASE_ )
365
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( ) -> int: return [ a * b * (1_000 - a - b) for a in range(1 , 999 ) for b in range(__A , 999 ) if (a * a + b * b == (1_000 - a - b) ** 2) ][0] if __name__ == "__main__": print(F'''{solution() = }''')
160
0
def _lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase ) -> int: """simple docstring""" return number | (1 << position) def _lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase ) -> int: """simple docstring""" return number & ~(1 << position) def _lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase ) -> int: """simple docstring""" return number ^ (1 << position) def _lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase ) -> bool: """simple docstring""" return ((number >> position) & 1) == 1 def _lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase ) -> int: """simple docstring""" return int((number & (1 << position)) != 0 ) if __name__ == "__main__": import doctest doctest.testmod()
230
import coval # From: git+https://github.com/ns-moosavi/coval.git # noqa: F401 from coval.conll import reader, util from coval.eval import evaluator import datasets A__ = datasets.logging.get_logger(__name__) A__ = '''\ @InProceedings{moosavi2019minimum, author = { Nafise Sadat Moosavi, Leo Born, Massimo Poesio and Michael Strube}, title = {Using Automatically Extracted Minimum Spans to Disentangle Coreference Evaluation from Boundary Detection}, year = {2019}, booktitle = {Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)}, publisher = {Association for Computational Linguistics}, address = {Florence, Italy}, } @inproceedings{10.3115/1072399.1072405, author = {Vilain, Marc and Burger, John and Aberdeen, John and Connolly, Dennis and Hirschman, Lynette}, title = {A Model-Theoretic Coreference Scoring Scheme}, year = {1995}, isbn = {1558604022}, publisher = {Association for Computational Linguistics}, address = {USA}, url = {https://doi.org/10.3115/1072399.1072405}, doi = {10.3115/1072399.1072405}, booktitle = {Proceedings of the 6th Conference on Message Understanding}, pages = {45–52}, numpages = {8}, location = {Columbia, Maryland}, series = {MUC6 ’95} } @INPROCEEDINGS{Bagga98algorithmsfor, author = {Amit Bagga and Breck Baldwin}, title = {Algorithms for Scoring Coreference Chains}, booktitle = {In The First International Conference on Language Resources and Evaluation Workshop on Linguistics Coreference}, year = {1998}, pages = {563--566} } @INPROCEEDINGS{Luo05oncoreference, author = {Xiaoqiang Luo}, title = {On coreference resolution performance metrics}, booktitle = {In Proc. of HLT/EMNLP}, year = {2005}, pages = {25--32}, publisher = {URL} } @inproceedings{moosavi-strube-2016-coreference, title = "Which Coreference Evaluation Metric Do You Trust? A Proposal for a Link-based Entity Aware Metric", author = "Moosavi, Nafise Sadat and Strube, Michael", booktitle = "Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)", month = aug, year = "2016", address = "Berlin, Germany", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/P16-1060", doi = "10.18653/v1/P16-1060", pages = "632--642", } ''' A__ = '''\ CoVal is a coreference evaluation tool for the CoNLL and ARRAU datasets which implements of the common evaluation metrics including MUC [Vilain et al, 1995], B-cubed [Bagga and Baldwin, 1998], CEAFe [Luo et al., 2005], LEA [Moosavi and Strube, 2016] and the averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) [Denis and Baldridge, 2009a; Pradhan et al., 2011]. This wrapper of CoVal currently only work with CoNLL line format: The CoNLL format has one word per line with all the annotation for this word in column separated by spaces: Column Type Description 1 Document ID This is a variation on the document filename 2 Part number Some files are divided into multiple parts numbered as 000, 001, 002, ... etc. 3 Word number 4 Word itself This is the token as segmented/tokenized in the Treebank. Initially the *_skel file contain the placeholder [WORD] which gets replaced by the actual token from the Treebank which is part of the OntoNotes release. 5 Part-of-Speech 6 Parse bit This is the bracketed structure broken before the first open parenthesis in the parse, and the word/part-of-speech leaf replaced with a *. The full parse can be created by substituting the asterix with the "([pos] [word])" string (or leaf) and concatenating the items in the rows of that column. 7 Predicate lemma The predicate lemma is mentioned for the rows for which we have semantic role information. All other rows are marked with a "-" 8 Predicate Frameset ID This is the PropBank frameset ID of the predicate in Column 7. 9 Word sense This is the word sense of the word in Column 3. 10 Speaker/Author This is the speaker or author name where available. Mostly in Broadcast Conversation and Web Log data. 11 Named Entities These columns identifies the spans representing various named entities. 12:N Predicate Arguments There is one column each of predicate argument structure information for the predicate mentioned in Column 7. N Coreference Coreference chain information encoded in a parenthesis structure. More informations on the format can be found here (section "*_conll File Format"): http://www.conll.cemantix.org/2012/data.html Details on the evaluation on CoNLL can be found here: https://github.com/ns-moosavi/coval/blob/master/conll/README.md CoVal code was written by @ns-moosavi. Some parts are borrowed from https://github.com/clarkkev/deep-coref/blob/master/evaluation.py The test suite is taken from https://github.com/conll/reference-coreference-scorers/ Mention evaluation and the test suite are added by @andreasvc. Parsing CoNLL files is developed by Leo Born. ''' A__ = ''' Calculates coreference evaluation metrics. Args: predictions: list of sentences. Each sentence is a list of word predictions to score in the CoNLL format. Each prediction is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. references: list of sentences. Each sentence is a list of word reference to score in the CoNLL format. Each reference is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. keep_singletons: After extracting all mentions of key or system files, mentions whose corresponding coreference chain is of size one, are considered as singletons. The default evaluation mode will include singletons in evaluations if they are included in the key or the system files. By setting \'keep_singletons=False\', all singletons in the key and system files will be excluded from the evaluation. NP_only: Most of the recent coreference resolvers only resolve NP mentions and leave out the resolution of VPs. By setting the \'NP_only\' option, the scorer will only evaluate the resolution of NPs. min_span: By setting \'min_span\', the scorer reports the results based on automatically detected minimum spans. Minimum spans are determined using the MINA algorithm. Returns: \'mentions\': mentions \'muc\': MUC metric [Vilain et al, 1995] \'bcub\': B-cubed [Bagga and Baldwin, 1998] \'ceafe\': CEAFe [Luo et al., 2005] \'lea\': LEA [Moosavi and Strube, 2016] \'conll_score\': averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) Examples: >>> coval = datasets.load_metric(\'coval\') >>> words = [\'bc/cctv/00/cctv_0005 0 0 Thank VBP (TOP(S(VP* thank 01 1 Xu_li * (V*) * -\', ... \'bc/cctv/00/cctv_0005 0 1 you PRP (NP*) - - - Xu_li * (ARG1*) (ARG0*) (116)\', ... \'bc/cctv/00/cctv_0005 0 2 everyone NN (NP*) - - - Xu_li * (ARGM-DIS*) * (116)\', ... \'bc/cctv/00/cctv_0005 0 3 for IN (PP* - - - Xu_li * (ARG2* * -\', ... \'bc/cctv/00/cctv_0005 0 4 watching VBG (S(VP*)))) watch 01 1 Xu_li * *) (V*) -\', ... \'bc/cctv/00/cctv_0005 0 5 . . *)) - - - Xu_li * * * -\'] >>> references = [words] >>> predictions = [words] >>> results = coval.compute(predictions=predictions, references=references) >>> print(results) # doctest:+ELLIPSIS {\'mentions/recall\': 1.0,[...] \'conll_score\': 100.0} ''' def _lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=False , __lowerCAmelCase=False , __lowerCAmelCase=True , __lowerCAmelCase=False , __lowerCAmelCase="dummy_doc" ) -> int: """simple docstring""" snake_case__ : Dict = {doc: key_lines} snake_case__ : Any = {doc: sys_lines} snake_case__ : Dict = {} snake_case__ : List[str] = 0 snake_case__ : Optional[Any] = 0 snake_case__ : Optional[Any] = 0 snake_case__ : Dict = 0 snake_case__ : List[Any] = 0 snake_case__ : List[Any] = 0 snake_case__ , snake_case__ : Tuple = reader.get_doc_mentions(__lowerCAmelCase , key_doc_lines[doc] , __lowerCAmelCase ) key_singletons_num += singletons_num if NP_only or min_span: snake_case__ : str = reader.set_annotated_parse_trees(__lowerCAmelCase , key_doc_lines[doc] , __lowerCAmelCase , __lowerCAmelCase ) snake_case__ , snake_case__ : int = reader.get_doc_mentions(__lowerCAmelCase , sys_doc_lines[doc] , __lowerCAmelCase ) sys_singletons_num += singletons_num if NP_only or min_span: snake_case__ : Union[str, Any] = reader.set_annotated_parse_trees(__lowerCAmelCase , key_doc_lines[doc] , __lowerCAmelCase , __lowerCAmelCase ) if remove_nested: snake_case__ , snake_case__ : Dict = reader.remove_nested_coref_mentions(__lowerCAmelCase , __lowerCAmelCase ) key_nested_coref_num += nested_mentions key_removed_nested_clusters += removed_clusters snake_case__ , snake_case__ : Optional[int] = reader.remove_nested_coref_mentions(__lowerCAmelCase , __lowerCAmelCase ) sys_nested_coref_num += nested_mentions sys_removed_nested_clusters += removed_clusters snake_case__ : Any = reader.get_mention_assignments(__lowerCAmelCase , __lowerCAmelCase ) snake_case__ : Optional[int] = reader.get_mention_assignments(__lowerCAmelCase , __lowerCAmelCase ) snake_case__ : List[Any] = (key_clusters, sys_clusters, key_mention_sys_cluster, sys_mention_key_cluster) if remove_nested: logger.info( '''Number of removed nested coreferring mentions in the key ''' f"""annotation: {key_nested_coref_num}; and system annotation: {sys_nested_coref_num}""" ) logger.info( '''Number of resulting singleton clusters in the key ''' f"""annotation: {key_removed_nested_clusters}; and system annotation: {sys_removed_nested_clusters}""" ) if not keep_singletons: logger.info( f"""{key_singletons_num:d} and {sys_singletons_num:d} singletons are removed from the key and system """ '''files, respectively''' ) return doc_coref_infos def _lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> str: """simple docstring""" snake_case__ : Optional[Any] = get_coref_infos(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) snake_case__ : str = {} snake_case__ : Optional[int] = 0 snake_case__ : List[Any] = 0 for name, metric in metrics: snake_case__ , snake_case__ , snake_case__ : Any = evaluator.evaluate_documents(__lowerCAmelCase , __lowerCAmelCase , beta=1 ) if name in ["muc", "bcub", "ceafe"]: conll += fa conll_subparts_num += 1 output_scores.update({f"""{name}/recall""": recall, f"""{name}/precision""": precision, f"""{name}/f1""": fa} ) logger.info( name.ljust(10 ) , f"""Recall: {recall * 100:.2f}""" , f""" Precision: {precision * 100:.2f}""" , f""" F1: {fa * 100:.2f}""" , ) if conll_subparts_num == 3: snake_case__ : int = (conll / 3) * 100 logger.info(f"""CoNLL score: {conll:.2f}""" ) output_scores.update({'''conll_score''': conll} ) return output_scores def _lowerCAmelCase ( __lowerCAmelCase ) -> List[str]: """simple docstring""" snake_case__ : str = False for line in key_lines: if not line.startswith('''#''' ): if len(line.split() ) > 6: snake_case__ : List[Any] = line.split()[5] if not parse_col == "-": snake_case__ : Optional[Any] = True break else: break return has_gold_parse @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a ( datasets.Metric ): def __lowerCamelCase ( self :Dict ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { '''predictions''': datasets.Sequence(datasets.Value('''string''' ) ), '''references''': datasets.Sequence(datasets.Value('''string''' ) ), } ) ,codebase_urls=['''https://github.com/ns-moosavi/coval'''] ,reference_urls=[ '''https://github.com/ns-moosavi/coval''', '''https://www.aclweb.org/anthology/P16-1060''', '''http://www.conll.cemantix.org/2012/data.html''', ] ,) def __lowerCamelCase ( self :Any ,__lowercase :List[Any] ,__lowercase :int ,__lowercase :str=True ,__lowercase :Optional[int]=False ,__lowercase :Optional[Any]=False ,__lowercase :Tuple=False ): snake_case__ : Optional[Any] = [ ('''mentions''', evaluator.mentions), ('''muc''', evaluator.muc), ('''bcub''', evaluator.b_cubed), ('''ceafe''', evaluator.ceafe), ('''lea''', evaluator.lea), ] if min_span: snake_case__ : Optional[int] = util.check_gold_parse_annotation(__lowercase ) if not has_gold_parse: raise NotImplementedError('''References should have gold parse annotation to use \'min_span\'.''' ) # util.parse_key_file(key_file) # key_file = key_file + ".parsed" snake_case__ : Any = evaluate( key_lines=__lowercase ,sys_lines=__lowercase ,metrics=__lowercase ,NP_only=__lowercase ,remove_nested=__lowercase ,keep_singletons=__lowercase ,min_span=__lowercase ,) return score
230
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : List[Any] = logging.get_logger(__name__) __snake_case : List[str] = {} class A__ ( lowerCamelCase__ ): '''simple docstring''' SCREAMING_SNAKE_CASE = """llama""" SCREAMING_SNAKE_CASE = ["""past_key_values"""] def __init__( self: str , _SCREAMING_SNAKE_CASE: str=3_2000 , _SCREAMING_SNAKE_CASE: str=4096 , _SCREAMING_SNAKE_CASE: Optional[Any]=1_1008 , _SCREAMING_SNAKE_CASE: Any=32 , _SCREAMING_SNAKE_CASE: Dict=32 , _SCREAMING_SNAKE_CASE: int=None , _SCREAMING_SNAKE_CASE: Optional[Any]="silu" , _SCREAMING_SNAKE_CASE: Optional[int]=2048 , _SCREAMING_SNAKE_CASE: Dict=0.02 , _SCREAMING_SNAKE_CASE: str=1e-6 , _SCREAMING_SNAKE_CASE: int=True , _SCREAMING_SNAKE_CASE: List[Any]=0 , _SCREAMING_SNAKE_CASE: Any=1 , _SCREAMING_SNAKE_CASE: Optional[Any]=2 , _SCREAMING_SNAKE_CASE: int=1 , _SCREAMING_SNAKE_CASE: Optional[int]=False , _SCREAMING_SNAKE_CASE: int=None , **_SCREAMING_SNAKE_CASE: List[str] , ) -> List[str]: """simple docstring""" __lowerCAmelCase : List[str] = vocab_size __lowerCAmelCase : Dict = max_position_embeddings __lowerCAmelCase : Tuple = hidden_size __lowerCAmelCase : Tuple = intermediate_size __lowerCAmelCase : str = num_hidden_layers __lowerCAmelCase : Optional[int] = num_attention_heads # for backward compatibility if num_key_value_heads is None: __lowerCAmelCase : Any = num_attention_heads __lowerCAmelCase : Dict = num_key_value_heads __lowerCAmelCase : Union[str, Any] = hidden_act __lowerCAmelCase : Any = initializer_range __lowerCAmelCase : List[str] = rms_norm_eps __lowerCAmelCase : int = pretraining_tp __lowerCAmelCase : str = use_cache __lowerCAmelCase : Optional[int] = rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ , tie_word_embeddings=snake_case__ , **snake_case__ , ) def _SCREAMING_SNAKE_CASE ( self: Dict) -> Optional[Any]: """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 : Optional[Any] = self.rope_scaling.get("type" , snake_case__) __lowerCAmelCase : Union[str, Any] = 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}""")
361
"""simple docstring""" import unittest import numpy as np from diffusers import OnnxStableDiffusionInpaintPipelineLegacy from diffusers.utils.testing_utils import ( is_onnx_available, load_image, load_numpy, nightly, require_onnxruntime, require_torch_gpu, ) if is_onnx_available(): import onnxruntime as ort @nightly @require_onnxruntime @require_torch_gpu class A__ ( unittest.TestCase ): '''simple docstring''' @property def _SCREAMING_SNAKE_CASE ( self: Optional[int]) -> Optional[int]: """simple docstring""" return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def _SCREAMING_SNAKE_CASE ( self: Tuple) -> Union[str, Any]: """simple docstring""" __lowerCAmelCase : Any = ort.SessionOptions() __lowerCAmelCase : Dict = False return options def _SCREAMING_SNAKE_CASE ( self: Tuple) -> Optional[Any]: """simple docstring""" __lowerCAmelCase : int = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/in_paint/overture-creations-5sI6fQgYIuo.png") __lowerCAmelCase : int = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/in_paint/overture-creations-5sI6fQgYIuo_mask.png") __lowerCAmelCase : List[Any] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/in_paint/red_cat_sitting_on_a_park_bench_onnx.npy") # using the PNDM scheduler by default __lowerCAmelCase : int = OnnxStableDiffusionInpaintPipelineLegacy.from_pretrained( "CompVis/stable-diffusion-v1-4" , revision="onnx" , safety_checker=_SCREAMING_SNAKE_CASE , feature_extractor=_SCREAMING_SNAKE_CASE , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Union[str, Any] = "A red cat sitting on a park bench" __lowerCAmelCase : Optional[int] = np.random.RandomState(0) __lowerCAmelCase : Optional[int] = pipe( prompt=_SCREAMING_SNAKE_CASE , image=_SCREAMING_SNAKE_CASE , mask_image=_SCREAMING_SNAKE_CASE , strength=0.75 , guidance_scale=7.5 , num_inference_steps=15 , generator=_SCREAMING_SNAKE_CASE , output_type="np" , ) __lowerCAmelCase : int = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image).max() < 1e-2
58
0
import unittest import numpy as np from transformers import RobertaPreLayerNormConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roberta_prelayernorm.modeling_flax_roberta_prelayernorm import ( FlaxRobertaPreLayerNormForCausalLM, FlaxRobertaPreLayerNormForMaskedLM, FlaxRobertaPreLayerNormForMultipleChoice, FlaxRobertaPreLayerNormForQuestionAnswering, FlaxRobertaPreLayerNormForSequenceClassification, FlaxRobertaPreLayerNormForTokenClassification, FlaxRobertaPreLayerNormModel, ) class __snake_case ( unittest.TestCase ): def __init__( self , snake_case__ , snake_case__=13 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=99 , snake_case__=32 , snake_case__=5 , snake_case__=4 , snake_case__=37 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=16 , snake_case__=2 , snake_case__=0.02 , snake_case__=4 , ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Dict =parent UpperCAmelCase : int =batch_size UpperCAmelCase : str =seq_length UpperCAmelCase : Optional[int] =is_training UpperCAmelCase : str =use_attention_mask UpperCAmelCase : List[Any] =use_token_type_ids UpperCAmelCase : Any =use_labels UpperCAmelCase : Union[str, Any] =vocab_size UpperCAmelCase : int =hidden_size UpperCAmelCase : str =num_hidden_layers UpperCAmelCase : int =num_attention_heads UpperCAmelCase : List[str] =intermediate_size UpperCAmelCase : List[str] =hidden_act UpperCAmelCase : int =hidden_dropout_prob UpperCAmelCase : Dict =attention_probs_dropout_prob UpperCAmelCase : Optional[Any] =max_position_embeddings UpperCAmelCase : int =type_vocab_size UpperCAmelCase : str =type_sequence_label_size UpperCAmelCase : Union[str, Any] =initializer_range UpperCAmelCase : List[str] =num_choices def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : str =None if self.use_attention_mask: UpperCAmelCase : Tuple =random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : Optional[Any] =None if self.use_token_type_ids: UpperCAmelCase : str =ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCAmelCase : Dict =RobertaPreLayerNormConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=snake_case__ , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Any =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Optional[Any] =config_and_inputs UpperCAmelCase : List[str] ={'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : Union[str, Any] =self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Tuple =config_and_inputs UpperCAmelCase : List[Any] =True UpperCAmelCase : Tuple =floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) UpperCAmelCase : Tuple =ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax # Copied from tests.models.roberta.test_modelling_flax_roberta.FlaxRobertaPreLayerNormModelTest with ROBERTA->ROBERTA_PRELAYERNORM,Roberta->RobertaPreLayerNorm,roberta-base->andreasmadsen/efficient_mlm_m0.40 class __snake_case ( lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : Optional[Any] = True __lowerCamelCase : List[str] = ( ( FlaxRobertaPreLayerNormModel, FlaxRobertaPreLayerNormForCausalLM, FlaxRobertaPreLayerNormForMaskedLM, FlaxRobertaPreLayerNormForSequenceClassification, FlaxRobertaPreLayerNormForTokenClassification, FlaxRobertaPreLayerNormForMultipleChoice, FlaxRobertaPreLayerNormForQuestionAnswering, ) if is_flax_available() else () ) def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : List[Any] =FlaxRobertaPreLayerNormModelTester(self ) @slow def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: UpperCAmelCase : Optional[int] =model_class_name.from_pretrained('''andreasmadsen/efficient_mlm_m0.40''' , from_pt=snake_case__ ) UpperCAmelCase : int =model(np.ones((1, 1) ) ) self.assertIsNotNone(snake_case__ ) @require_flax class __snake_case ( unittest.TestCase ): @slow def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Any =FlaxRobertaPreLayerNormForMaskedLM.from_pretrained('''andreasmadsen/efficient_mlm_m0.40''' , from_pt=snake_case__ ) UpperCAmelCase : Tuple =np.array([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]] , dtype=jnp.intaa ) UpperCAmelCase : int =model(snake_case__ )[0] UpperCAmelCase : List[Any] =[1, 11, 5_0265] self.assertEqual(list(output.shape ) , snake_case__ ) # compare the actual values for a slice. UpperCAmelCase : List[str] =np.array( [[[40.4880, 18.0199, -5.2367], [-1.8877, -4.0885, 10.7085], [-2.2613, -5.6110, 7.2665]]] , dtype=np.floataa ) self.assertTrue(np.allclose(output[:, :3, :3] , snake_case__ , atol=1e-4 ) ) @slow def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =FlaxRobertaPreLayerNormModel.from_pretrained('''andreasmadsen/efficient_mlm_m0.40''' , from_pt=snake_case__ ) UpperCAmelCase : Union[str, Any] =np.array([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]] , dtype=jnp.intaa ) UpperCAmelCase : int =model(snake_case__ )[0] # compare the actual values for a slice. UpperCAmelCase : Dict =np.array( [[[0.0208, -0.0356, 0.0237], [-0.1569, -0.0411, -0.2626], [0.1879, 0.0125, -0.0089]]] , dtype=np.floataa ) self.assertTrue(np.allclose(output[:, :3, :3] , snake_case__ , atol=1e-4 ) )
348
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __snake_case = { '''configuration_bloom''': ['''BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BloomConfig''', '''BloomOnnxConfig'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''BloomTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __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 __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
348
1
"""simple docstring""" from __future__ import annotations import os from collections.abc import Mapping SCREAMING_SNAKE_CASE_ : Optional[Any] = tuple[int, int] class a : """simple docstring""" def __init__( self: int , UpperCamelCase: set[int] , UpperCamelCase: Mapping[EdgeT, int] ): """simple docstring""" A__ = vertices A__ = { (min(UpperCamelCase ), max(UpperCamelCase )): weight for edge, weight in edges.items() } def UpperCamelCase ( self: int , UpperCamelCase: EdgeT , UpperCamelCase: int ): """simple docstring""" self.vertices.add(edge[0] ) self.vertices.add(edge[1] ) A__ = weight def UpperCamelCase ( self: int ): """simple docstring""" A__ = Graph({min(self.vertices )} , {} ) A__ = 42 A__ = 42 A__ = 42 A__ = 42 while len(subgraph.vertices ) < len(self.vertices ): A__ = max(self.edges.values() ) + 1 for edge, weight in self.edges.items(): if (edge[0] in subgraph.vertices) ^ (edge[1] in subgraph.vertices): if weight < min_weight: A__ = edge A__ = weight subgraph.add_edge(UpperCamelCase , UpperCamelCase ) return subgraph def _snake_case ( UpperCAmelCase_ : str = "p107_network.txt" ): A__ = os.path.abspath(os.path.dirname(UpperCAmelCase_ ) ) A__ = os.path.join(UpperCAmelCase_ , UpperCAmelCase_ ) A__ = {} A__ = 42 A__ = 42 A__ = 42 with open(UpperCAmelCase_ ) as f: A__ = f.read().strip().split("""\n""" ) A__ = [line.split(""",""" ) for line in data] for edgea in range(1 , len(UpperCAmelCase_ ) ): for edgea in range(UpperCAmelCase_ ): if adjaceny_matrix[edgea][edgea] != "-": A__ = int(adjaceny_matrix[edgea][edgea] ) A__ = Graph(set(range(len(UpperCAmelCase_ ) ) ) , UpperCAmelCase_ ) A__ = graph.prims_algorithm() A__ = sum(graph.edges.values() ) A__ = sum(subgraph.edges.values() ) return initial_total - optimal_total if __name__ == "__main__": print(f"""{solution() = }""")
366
"""simple docstring""" from pathlib import Path import fire from tqdm import tqdm def _snake_case ( UpperCAmelCase_ : int="ro" , UpperCAmelCase_ : Optional[int]="en" , UpperCAmelCase_ : List[Any]="wmt16" , UpperCAmelCase_ : str=None ): try: import datasets except (ModuleNotFoundError, ImportError): raise ImportError("""run pip install datasets""" ) A__ = F"""{src_lang}-{tgt_lang}""" print(F"""Converting {dataset}-{pair}""" ) A__ = datasets.load_dataset(UpperCAmelCase_ , UpperCAmelCase_ ) if save_dir is None: A__ = F"""{dataset}-{pair}""" A__ = Path(UpperCAmelCase_ ) save_dir.mkdir(exist_ok=UpperCAmelCase_ ) for split in ds.keys(): print(F"""Splitting {split} with {ds[split].num_rows} records""" ) # to save to val.source, val.target like summary datasets A__ = """val""" if split == """validation""" else split A__ = save_dir.joinpath(F"""{fn}.source""" ) A__ = save_dir.joinpath(F"""{fn}.target""" ) A__ = src_path.open("""w+""" ) A__ = tgt_path.open("""w+""" ) # reader is the bottleneck so writing one record at a time doesn't slow things down for x in tqdm(ds[split] ): A__ = x["""translation"""] src_fp.write(ex[src_lang] + """\n""" ) tgt_fp.write(ex[tgt_lang] + """\n""" ) print(F"""Saved {dataset} dataset to {save_dir}""" ) if __name__ == "__main__": fire.Fire(download_wmt_dataset)
69
0
'''simple docstring''' import unittest from transformers import load_tool from .test_tools_common import ToolTesterMixin _SCREAMING_SNAKE_CASE : List[Any] = "\nHugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.\n\nIn March 2021, Hugging Face raised $40 million in a Series B funding round.[3]\n\nOn April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]\n" class _snake_case ( unittest.TestCase , lowercase_ ): def lowerCAmelCase__ ( self ) -> Union[str, Any]: '''simple docstring''' snake_case_ = load_tool("text-question-answering" ) self.tool.setup() snake_case_ = load_tool("text-question-answering" , remote=a__ ) def lowerCAmelCase__ ( self ) -> int: '''simple docstring''' snake_case_ = self.tool(a__ , "What did Hugging Face do in April 2021?" ) self.assertEqual(a__ , "launched the BigScience Research Workshop" ) def lowerCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' snake_case_ = self.remote_tool(a__ , "What did Hugging Face do in April 2021?" ) self.assertEqual(a__ , "launched the BigScience Research Workshop" ) def lowerCAmelCase__ ( self ) -> str: '''simple docstring''' snake_case_ = self.tool(text=a__ , question="What did Hugging Face do in April 2021?" ) self.assertEqual(a__ , "launched the BigScience Research Workshop" ) def lowerCAmelCase__ ( self ) -> List[str]: '''simple docstring''' snake_case_ = self.remote_tool(text=a__ , question="What did Hugging Face do in April 2021?" ) self.assertEqual(a__ , "launched the BigScience Research Workshop" )
85
"""simple docstring""" import flax.linen as nn import jax.numpy as jnp from .attention_flax import FlaxTransformeraDModel from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD class lowerCamelCase (nn.Module ): lowerCamelCase__ : int lowerCamelCase__ : int lowerCamelCase__ : float = 0.0 lowerCamelCase__ : int = 1 lowerCamelCase__ : int = 1 lowerCamelCase__ : bool = True lowerCamelCase__ : bool = False lowerCamelCase__ : bool = False lowerCamelCase__ : bool = False lowerCamelCase__ : jnp.dtype = jnp.floataa def SCREAMING_SNAKE_CASE ( self : int ) -> int: SCREAMING_SNAKE_CASE__ = [] SCREAMING_SNAKE_CASE__ = [] for i in range(self.num_layers ): SCREAMING_SNAKE_CASE__ = self.in_channels if i == 0 else self.out_channels SCREAMING_SNAKE_CASE__ = FlaxResnetBlockaD( in_channels=__UpperCAmelCase , out_channels=self.out_channels , dropout_prob=self.dropout , dtype=self.dtype , ) resnets.append(__UpperCAmelCase ) SCREAMING_SNAKE_CASE__ = FlaxTransformeraDModel( in_channels=self.out_channels , n_heads=self.num_attention_heads , d_head=self.out_channels // self.num_attention_heads , depth=1 , use_linear_projection=self.use_linear_projection , only_cross_attention=self.only_cross_attention , use_memory_efficient_attention=self.use_memory_efficient_attention , dtype=self.dtype , ) attentions.append(__UpperCAmelCase ) SCREAMING_SNAKE_CASE__ = resnets SCREAMING_SNAKE_CASE__ = attentions if self.add_downsample: SCREAMING_SNAKE_CASE__ = FlaxDownsampleaD(self.out_channels , dtype=self.dtype ) def __call__( self : Union[str, Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Dict , __UpperCAmelCase : Dict=True ) -> Any: SCREAMING_SNAKE_CASE__ = () for resnet, attn in zip(self.resnets , self.attentions ): SCREAMING_SNAKE_CASE__ = resnet(__UpperCAmelCase , __UpperCAmelCase , deterministic=__UpperCAmelCase ) SCREAMING_SNAKE_CASE__ = attn(__UpperCAmelCase , __UpperCAmelCase , deterministic=__UpperCAmelCase ) output_states += (hidden_states,) if self.add_downsample: SCREAMING_SNAKE_CASE__ = self.downsamplers_a(__UpperCAmelCase ) output_states += (hidden_states,) return hidden_states, output_states class lowerCamelCase (nn.Module ): lowerCamelCase__ : int lowerCamelCase__ : int lowerCamelCase__ : float = 0.0 lowerCamelCase__ : int = 1 lowerCamelCase__ : bool = True lowerCamelCase__ : jnp.dtype = jnp.floataa def SCREAMING_SNAKE_CASE ( self : Dict ) -> Any: SCREAMING_SNAKE_CASE__ = [] for i in range(self.num_layers ): SCREAMING_SNAKE_CASE__ = self.in_channels if i == 0 else self.out_channels SCREAMING_SNAKE_CASE__ = FlaxResnetBlockaD( in_channels=__UpperCAmelCase , out_channels=self.out_channels , dropout_prob=self.dropout , dtype=self.dtype , ) resnets.append(__UpperCAmelCase ) SCREAMING_SNAKE_CASE__ = resnets if self.add_downsample: SCREAMING_SNAKE_CASE__ = FlaxDownsampleaD(self.out_channels , dtype=self.dtype ) def __call__( self : Any , __UpperCAmelCase : int , __UpperCAmelCase : str , __UpperCAmelCase : Union[str, Any]=True ) -> List[Any]: SCREAMING_SNAKE_CASE__ = () for resnet in self.resnets: SCREAMING_SNAKE_CASE__ = resnet(__UpperCAmelCase , __UpperCAmelCase , deterministic=__UpperCAmelCase ) output_states += (hidden_states,) if self.add_downsample: SCREAMING_SNAKE_CASE__ = self.downsamplers_a(__UpperCAmelCase ) output_states += (hidden_states,) return hidden_states, output_states class lowerCamelCase (nn.Module ): lowerCamelCase__ : int lowerCamelCase__ : int lowerCamelCase__ : int lowerCamelCase__ : float = 0.0 lowerCamelCase__ : int = 1 lowerCamelCase__ : int = 1 lowerCamelCase__ : bool = True lowerCamelCase__ : bool = False lowerCamelCase__ : bool = False lowerCamelCase__ : bool = False lowerCamelCase__ : jnp.dtype = jnp.floataa def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Optional[int]: SCREAMING_SNAKE_CASE__ = [] SCREAMING_SNAKE_CASE__ = [] for i in range(self.num_layers ): SCREAMING_SNAKE_CASE__ = self.in_channels if (i == self.num_layers - 1) else self.out_channels SCREAMING_SNAKE_CASE__ = self.prev_output_channel if i == 0 else self.out_channels SCREAMING_SNAKE_CASE__ = FlaxResnetBlockaD( in_channels=resnet_in_channels + res_skip_channels , out_channels=self.out_channels , dropout_prob=self.dropout , dtype=self.dtype , ) resnets.append(__UpperCAmelCase ) SCREAMING_SNAKE_CASE__ = FlaxTransformeraDModel( in_channels=self.out_channels , n_heads=self.num_attention_heads , d_head=self.out_channels // self.num_attention_heads , depth=1 , use_linear_projection=self.use_linear_projection , only_cross_attention=self.only_cross_attention , use_memory_efficient_attention=self.use_memory_efficient_attention , dtype=self.dtype , ) attentions.append(__UpperCAmelCase ) SCREAMING_SNAKE_CASE__ = resnets SCREAMING_SNAKE_CASE__ = attentions if self.add_upsample: SCREAMING_SNAKE_CASE__ = FlaxUpsampleaD(self.out_channels , dtype=self.dtype ) def __call__( self : List[str] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : str , __UpperCAmelCase : Any=True ) -> Union[str, Any]: for resnet, attn in zip(self.resnets , self.attentions ): # pop res hidden states SCREAMING_SNAKE_CASE__ = res_hidden_states_tuple[-1] SCREAMING_SNAKE_CASE__ = res_hidden_states_tuple[:-1] SCREAMING_SNAKE_CASE__ = jnp.concatenate((hidden_states, res_hidden_states) , axis=-1 ) SCREAMING_SNAKE_CASE__ = resnet(__UpperCAmelCase , __UpperCAmelCase , deterministic=__UpperCAmelCase ) SCREAMING_SNAKE_CASE__ = attn(__UpperCAmelCase , __UpperCAmelCase , deterministic=__UpperCAmelCase ) if self.add_upsample: SCREAMING_SNAKE_CASE__ = self.upsamplers_a(__UpperCAmelCase ) return hidden_states class lowerCamelCase (nn.Module ): lowerCamelCase__ : int lowerCamelCase__ : int lowerCamelCase__ : int lowerCamelCase__ : float = 0.0 lowerCamelCase__ : int = 1 lowerCamelCase__ : bool = True lowerCamelCase__ : jnp.dtype = jnp.floataa def SCREAMING_SNAKE_CASE ( self : str ) -> List[Any]: SCREAMING_SNAKE_CASE__ = [] for i in range(self.num_layers ): SCREAMING_SNAKE_CASE__ = self.in_channels if (i == self.num_layers - 1) else self.out_channels SCREAMING_SNAKE_CASE__ = self.prev_output_channel if i == 0 else self.out_channels SCREAMING_SNAKE_CASE__ = FlaxResnetBlockaD( in_channels=resnet_in_channels + res_skip_channels , out_channels=self.out_channels , dropout_prob=self.dropout , dtype=self.dtype , ) resnets.append(__UpperCAmelCase ) SCREAMING_SNAKE_CASE__ = resnets if self.add_upsample: SCREAMING_SNAKE_CASE__ = FlaxUpsampleaD(self.out_channels , dtype=self.dtype ) def __call__( self : Optional[int] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Tuple , __UpperCAmelCase : List[str]=True ) -> Dict: for resnet in self.resnets: # pop res hidden states SCREAMING_SNAKE_CASE__ = res_hidden_states_tuple[-1] SCREAMING_SNAKE_CASE__ = res_hidden_states_tuple[:-1] SCREAMING_SNAKE_CASE__ = jnp.concatenate((hidden_states, res_hidden_states) , axis=-1 ) SCREAMING_SNAKE_CASE__ = resnet(__UpperCAmelCase , __UpperCAmelCase , deterministic=__UpperCAmelCase ) if self.add_upsample: SCREAMING_SNAKE_CASE__ = self.upsamplers_a(__UpperCAmelCase ) return hidden_states class lowerCamelCase (nn.Module ): lowerCamelCase__ : int lowerCamelCase__ : float = 0.0 lowerCamelCase__ : int = 1 lowerCamelCase__ : int = 1 lowerCamelCase__ : bool = False lowerCamelCase__ : bool = False lowerCamelCase__ : jnp.dtype = jnp.floataa def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Union[str, Any]: # there is always at least one resnet SCREAMING_SNAKE_CASE__ = [ FlaxResnetBlockaD( in_channels=self.in_channels , out_channels=self.in_channels , dropout_prob=self.dropout , dtype=self.dtype , ) ] SCREAMING_SNAKE_CASE__ = [] for _ in range(self.num_layers ): SCREAMING_SNAKE_CASE__ = FlaxTransformeraDModel( in_channels=self.in_channels , n_heads=self.num_attention_heads , d_head=self.in_channels // self.num_attention_heads , depth=1 , use_linear_projection=self.use_linear_projection , use_memory_efficient_attention=self.use_memory_efficient_attention , dtype=self.dtype , ) attentions.append(__UpperCAmelCase ) SCREAMING_SNAKE_CASE__ = FlaxResnetBlockaD( in_channels=self.in_channels , out_channels=self.in_channels , dropout_prob=self.dropout , dtype=self.dtype , ) resnets.append(__UpperCAmelCase ) SCREAMING_SNAKE_CASE__ = resnets SCREAMING_SNAKE_CASE__ = attentions def __call__( self : Tuple , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : int , __UpperCAmelCase : List[str]=True ) -> Tuple: SCREAMING_SNAKE_CASE__ = self.resnets[0](__UpperCAmelCase , __UpperCAmelCase ) for attn, resnet in zip(self.attentions , self.resnets[1:] ): SCREAMING_SNAKE_CASE__ = attn(__UpperCAmelCase , __UpperCAmelCase , deterministic=__UpperCAmelCase ) SCREAMING_SNAKE_CASE__ = resnet(__UpperCAmelCase , __UpperCAmelCase , deterministic=__UpperCAmelCase ) return hidden_states
165
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase = { '''configuration_blenderbot_small''': [ '''BLENDERBOT_SMALL_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlenderbotSmallConfig''', '''BlenderbotSmallOnnxConfig''', ], '''tokenization_blenderbot_small''': ['''BlenderbotSmallTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase = ['''BlenderbotSmallTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase = [ '''BLENDERBOT_SMALL_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlenderbotSmallForCausalLM''', '''BlenderbotSmallForConditionalGeneration''', '''BlenderbotSmallModel''', '''BlenderbotSmallPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase = [ '''TFBlenderbotSmallForConditionalGeneration''', '''TFBlenderbotSmallModel''', '''TFBlenderbotSmallPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase = [ '''FlaxBlenderbotSmallForConditionalGeneration''', '''FlaxBlenderbotSmallModel''', '''FlaxBlenderbotSmallPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_blenderbot_small import ( BLENDERBOT_SMALL_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotSmallConfig, BlenderbotSmallOnnxConfig, ) from .tokenization_blenderbot_small import BlenderbotSmallTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_small_fast import BlenderbotSmallTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot_small import ( BLENDERBOT_SMALL_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotSmallForCausalLM, BlenderbotSmallForConditionalGeneration, BlenderbotSmallModel, BlenderbotSmallPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot_small import ( TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel, TFBlenderbotSmallPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot_small import ( FlaxBlenderbotSmallForConditionalGeneration, FlaxBlenderbotSmallModel, FlaxBlenderbotSmallPreTrainedModel, ) else: import sys lowerCAmelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
304
import unittest from transformers import XLMConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMWithLMHeadModel, ) from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST class A : def __init__(self , lowerCAmelCase , lowerCAmelCase=1_3 , lowerCAmelCase=7 , lowerCAmelCase=True , lowerCAmelCase=True , lowerCAmelCase=True , lowerCAmelCase=True , lowerCAmelCase=True , lowerCAmelCase=False , lowerCAmelCase=False , lowerCAmelCase=False , lowerCAmelCase=2 , lowerCAmelCase=9_9 , lowerCAmelCase=0 , lowerCAmelCase=3_2 , lowerCAmelCase=5 , lowerCAmelCase=4 , lowerCAmelCase=0.1 , lowerCAmelCase=0.1 , lowerCAmelCase=5_1_2 , lowerCAmelCase=2 , lowerCAmelCase=0.02 , lowerCAmelCase=2 , lowerCAmelCase=4 , lowerCAmelCase="last" , lowerCAmelCase=True , lowerCAmelCase=None , lowerCAmelCase=0 , ): __lowercase= parent __lowercase= batch_size __lowercase= seq_length __lowercase= is_training __lowercase= use_input_lengths __lowercase= use_token_type_ids __lowercase= use_labels __lowercase= gelu_activation __lowercase= sinusoidal_embeddings __lowercase= causal __lowercase= asm __lowercase= n_langs __lowercase= vocab_size __lowercase= n_special __lowercase= hidden_size __lowercase= num_hidden_layers __lowercase= num_attention_heads __lowercase= hidden_dropout_prob __lowercase= attention_probs_dropout_prob __lowercase= max_position_embeddings __lowercase= type_sequence_label_size __lowercase= initializer_range __lowercase= num_labels __lowercase= num_choices __lowercase= summary_type __lowercase= use_proj __lowercase= scope __lowercase= bos_token_id def _A (self ): __lowercase= ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowercase= random_attention_mask([self.batch_size, self.seq_length] ) __lowercase= None if self.use_input_lengths: __lowercase= ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length __lowercase= None if self.use_token_type_ids: __lowercase= ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) __lowercase= None __lowercase= None __lowercase= None if self.use_labels: __lowercase= ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowercase= ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __lowercase= ids_tensor([self.batch_size] , 2 ).float() __lowercase= ids_tensor([self.batch_size] , self.num_choices ) __lowercase= self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def _A (self ): return XLMConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , num_labels=self.num_labels , bos_token_id=self.bos_token_id , ) def _A (self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , ): __lowercase= XLMModel(config=lowerCAmelCase ) model.to(lowerCAmelCase ) model.eval() __lowercase= model(lowerCAmelCase , lengths=lowerCAmelCase , langs=lowerCAmelCase ) __lowercase= model(lowerCAmelCase , langs=lowerCAmelCase ) __lowercase= model(lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _A (self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , ): __lowercase= XLMWithLMHeadModel(lowerCAmelCase ) model.to(lowerCAmelCase ) model.eval() __lowercase= model(lowerCAmelCase , token_type_ids=lowerCAmelCase , labels=lowerCAmelCase ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _A (self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , ): __lowercase= XLMForQuestionAnsweringSimple(lowerCAmelCase ) model.to(lowerCAmelCase ) model.eval() __lowercase= model(lowerCAmelCase ) __lowercase= model(lowerCAmelCase , start_positions=lowerCAmelCase , end_positions=lowerCAmelCase ) __lowercase= outputs self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def _A (self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , ): __lowercase= XLMForQuestionAnswering(lowerCAmelCase ) model.to(lowerCAmelCase ) model.eval() __lowercase= model(lowerCAmelCase ) __lowercase= model( lowerCAmelCase , start_positions=lowerCAmelCase , end_positions=lowerCAmelCase , cls_index=lowerCAmelCase , is_impossible=lowerCAmelCase , p_mask=lowerCAmelCase , ) __lowercase= model( lowerCAmelCase , start_positions=lowerCAmelCase , end_positions=lowerCAmelCase , cls_index=lowerCAmelCase , is_impossible=lowerCAmelCase , ) ((__lowercase), )= result_with_labels.to_tuple() __lowercase= model(lowerCAmelCase , start_positions=lowerCAmelCase , end_positions=lowerCAmelCase ) ((__lowercase), )= result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape , () ) self.parent.assertEqual(result.start_top_log_probs.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual(result.start_top_index.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual( result.end_top_log_probs.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual( result.end_top_index.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual(result.cls_logits.shape , (self.batch_size,) ) def _A (self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , ): __lowercase= XLMForSequenceClassification(lowerCAmelCase ) model.to(lowerCAmelCase ) model.eval() __lowercase= model(lowerCAmelCase ) __lowercase= model(lowerCAmelCase , labels=lowerCAmelCase ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _A (self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , ): __lowercase= self.num_labels __lowercase= XLMForTokenClassification(lowerCAmelCase ) model.to(lowerCAmelCase ) model.eval() __lowercase= model(lowerCAmelCase , attention_mask=lowerCAmelCase , labels=lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _A (self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , ): __lowercase= self.num_choices __lowercase= XLMForMultipleChoice(config=lowerCAmelCase ) model.to(lowerCAmelCase ) model.eval() __lowercase= input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __lowercase= token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __lowercase= input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __lowercase= model( lowerCAmelCase , attention_mask=lowerCAmelCase , token_type_ids=lowerCAmelCase , labels=lowerCAmelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def _A (self ): __lowercase= self.prepare_config_and_inputs() ( ( __lowercase ), ( __lowercase ), ( __lowercase ), ( __lowercase ), ( __lowercase ), ( __lowercase ), ( __lowercase ), ( __lowercase ), ( __lowercase ), )= config_and_inputs __lowercase= {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'lengths': input_lengths} return config, inputs_dict @require_torch class A ( A_ , A_ , A_ , unittest.TestCase ): UpperCamelCase_ : int =( ( XLMModel, XLMWithLMHeadModel, XLMForQuestionAnswering, XLMForSequenceClassification, XLMForQuestionAnsweringSimple, XLMForTokenClassification, XLMForMultipleChoice, ) if is_torch_available() else () ) UpperCamelCase_ : Dict =( (XLMWithLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable UpperCamelCase_ : str =( { '''feature-extraction''': XLMModel, '''fill-mask''': XLMWithLMHeadModel, '''question-answering''': XLMForQuestionAnsweringSimple, '''text-classification''': XLMForSequenceClassification, '''text-generation''': XLMWithLMHeadModel, '''token-classification''': XLMForTokenClassification, '''zero-shot''': XLMForSequenceClassification, } if is_torch_available() else {} ) def _A (self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ): if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('Fast' ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def _A (self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase=False ): __lowercase= super()._prepare_for_class(lowerCAmelCase , lowerCAmelCase , return_labels=lowerCAmelCase ) if return_labels: if model_class.__name__ == "XLMForQuestionAnswering": __lowercase= torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCAmelCase ) __lowercase= torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCAmelCase ) return inputs_dict def _A (self ): __lowercase= XLMModelTester(self ) __lowercase= ConfigTester(self , config_class=lowerCAmelCase , emb_dim=3_7 ) def _A (self ): self.config_tester.run_common_tests() def _A (self ): __lowercase= self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_model(*lowerCAmelCase ) def _A (self ): __lowercase= self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_lm_head(*lowerCAmelCase ) def _A (self ): __lowercase= self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_simple_qa(*lowerCAmelCase ) def _A (self ): __lowercase= self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_qa(*lowerCAmelCase ) def _A (self ): __lowercase= self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_sequence_classif(*lowerCAmelCase ) def _A (self ): __lowercase= self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_token_classif(*lowerCAmelCase ) def _A (self ): __lowercase= self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_for_multiple_choice(*lowerCAmelCase ) def _A (self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase=False , lowerCAmelCase=1 ): self.assertIsInstance(lowerCAmelCase , lowerCAmelCase ) self.assertListEqual( [isinstance(lowerCAmelCase , lowerCAmelCase ) for iter_attentions in attentions] , [True] * len(lowerCAmelCase ) ) self.assertEqual(len(lowerCAmelCase ) , (max_length - min_length) * num_beam_groups ) for idx, iter_attentions in enumerate(lowerCAmelCase ): # adds PAD dummy token __lowercase= min_length + idx + 1 __lowercase= min_length + idx + 1 __lowercase= ( batch_size * num_beam_groups, config.num_attention_heads, tgt_len, src_len, ) # check attn size self.assertListEqual( [layer_attention.shape for layer_attention in iter_attentions] , [expected_shape] * len(lowerCAmelCase ) ) def _A (self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase=False , lowerCAmelCase=1 ): self.assertIsInstance(lowerCAmelCase , lowerCAmelCase ) self.assertListEqual( [isinstance(lowerCAmelCase , lowerCAmelCase ) for iter_hidden_states in hidden_states] , [True] * len(lowerCAmelCase ) , ) self.assertEqual(len(lowerCAmelCase ) , (max_length - min_length) * num_beam_groups ) for idx, iter_hidden_states in enumerate(lowerCAmelCase ): # adds PAD dummy token __lowercase= min_length + idx + 1 __lowercase= (batch_size * num_beam_groups, seq_len, config.hidden_size) # check hidden size self.assertListEqual( [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states] , [expected_shape] * len(lowerCAmelCase ) , ) pass @slow def _A (self ): for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __lowercase= XLMModel.from_pretrained(lowerCAmelCase ) self.assertIsNotNone(lowerCAmelCase ) @require_torch class A ( unittest.TestCase ): @slow def _A (self ): __lowercase= XLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048' ) model.to(lowerCAmelCase ) __lowercase= torch.tensor([[1_4, 4_4_7]] , dtype=torch.long , device=lowerCAmelCase ) # the president __lowercase= [ 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, 1_4, 4_4_7, ] # the president the president the president the president the president the president the president the president the president the president # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference __lowercase= model.generate(lowerCAmelCase , do_sample=lowerCAmelCase ) self.assertListEqual(output_ids[0].cpu().numpy().tolist() , lowerCAmelCase )
304
1
'''simple docstring''' from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def UpperCamelCase__ ( lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , ): """simple docstring""" _lowerCAmelCase = coefficient_matrix.shape _lowerCAmelCase = constant_matrix.shape if rowsa != colsa: _lowerCAmelCase = f"Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}" raise ValueError(_a ) if colsa != 1: _lowerCAmelCase = f"Constant matrix must be nx1 but received {rowsa}x{colsa}" raise ValueError(_a ) if rowsa != rowsa: _lowerCAmelCase = ( '''Coefficient and constant matrices dimensions must be nxn and nx1 but ''' f"received {rowsa}x{colsa} and {rowsa}x{colsa}" ) raise ValueError(_a ) if len(_a ) != rowsa: _lowerCAmelCase = ( '''Number of initial values must be equal to number of rows in coefficient ''' f"matrix but received {len(_a )} and {rowsa}" ) raise ValueError(_a ) if iterations <= 0: raise ValueError("""Iterations must be at least 1""" ) _lowerCAmelCase = np.concatenate( (coefficient_matrix, constant_matrix) , axis=1 ) _lowerCAmelCase = table.shape strictly_diagonally_dominant(_a ) # Iterates the whole matrix for given number of times for _ in range(_a ): _lowerCAmelCase = [] for row in range(_a ): _lowerCAmelCase = 0 for col in range(_a ): if col == row: _lowerCAmelCase = table[row][col] elif col == cols - 1: _lowerCAmelCase = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] _lowerCAmelCase = (temp + val) / denom new_val.append(_a ) _lowerCAmelCase = new_val return [float(_a ) for i in new_val] def UpperCamelCase__ ( lowerCAmelCase ): """simple docstring""" _lowerCAmelCase = table.shape _lowerCAmelCase = True for i in range(0 , _a ): _lowerCAmelCase = 0 for j in range(0 , cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError("""Coefficient matrix is not strictly diagonally dominant""" ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
70
"""simple docstring""" import os from datetime import datetime as dt from github import Github lowercase__ : int = [ '''good first issue''', '''good second issue''', '''good difficult issue''', '''enhancement''', '''new pipeline/model''', '''new scheduler''', '''wip''', ] def __lowercase ( ): snake_case_ : Optional[Any] = Github(os.environ['''GITHUB_TOKEN'''] ) snake_case_ : Any = g.get_repo('''huggingface/diffusers''' ) snake_case_ : Any = repo.get_issues(state='''open''' ) for issue in open_issues: snake_case_ : str = sorted(issue.get_comments() , key=lambda _a : i.created_at , reverse=_a ) snake_case_ : Dict = 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() ) ): # Closes the issue after 7 days of inactivity since the Stalebot notification. issue.edit(state='''closed''' ) elif ( "stale" in issue.get_labels() and last_comment is not None and last_comment.user.login != "github-actions[bot]" ): # Opens the issue if someone other than Stalebot commented. issue.edit(state='''open''' ) issue.remove_from_labels('''stale''' ) 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() ) ): # Post a Stalebot notification after 23 days of inactivity. 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/diffusers/blob/main/CONTRIBUTING.md) ''' '''are likely to be ignored.''' ) issue.add_to_labels('''stale''' ) if __name__ == "__main__": main()
264
0
"""simple docstring""" import logging import os import sys from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import SeqaSeqTrainer from seqaseq_training_args import SeqaSeqTrainingArguments import transformers from transformers import ( AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer, HfArgumentParser, MBartTokenizer, MBartTokenizerFast, set_seed, ) from transformers.trainer_utils import EvaluationStrategy, is_main_process from transformers.training_args import ParallelMode from utils import ( SeqaSeqDataCollator, SeqaSeqDataset, assert_all_frozen, build_compute_metrics_fn, check_output_dir, freeze_embeds, freeze_params, lmap, save_json, use_task_specific_params, write_txt_file, ) SCREAMING_SNAKE_CASE = logging.getLogger(__name__) @dataclass class UpperCAmelCase_ : lowercase__ = field( metadata={'''help''': '''Path to pretrained model or model identifier from huggingface.co/models'''} ) lowercase__ = field( default=A_, metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) lowercase__ = field( default=A_, metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} ) lowercase__ = field( default=A_, metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''}, ) lowercase__ = field(default=A_, metadata={'''help''': '''Whether tp freeze the encoder.'''} ) lowercase__ = field(default=A_, metadata={'''help''': '''Whether to freeze the embeddings.'''} ) @dataclass class UpperCAmelCase_ : lowercase__ = field( metadata={'''help''': '''The input data dir. Should contain the .tsv files (or other data files) for the task.'''} ) lowercase__ = field( default='''summarization''', metadata={'''help''': '''Task name, summarization (or summarization_{dataset} for pegasus) or translation'''}, ) lowercase__ = field( default=10_24, metadata={ '''help''': ( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) }, ) lowercase__ = field( default=1_28, metadata={ '''help''': ( '''The maximum total sequence length for target text after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) }, ) lowercase__ = field( default=1_42, metadata={ '''help''': ( '''The maximum total sequence length for validation target text after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded. ''' '''This argument is also used to override the ``max_length`` param of ``model.generate``, which is used ''' '''during ``evaluate`` and ``predict``.''' ) }, ) lowercase__ = field( default=1_42, metadata={ '''help''': ( '''The maximum total sequence length for test target text after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) }, ) lowercase__ = field(default=-1, metadata={'''help''': '''# training examples. -1 means use all.'''} ) lowercase__ = field(default=-1, metadata={'''help''': '''# validation examples. -1 means use all.'''} ) lowercase__ = field(default=-1, metadata={'''help''': '''# test examples. -1 means use all.'''} ) lowercase__ = field(default=A_, metadata={'''help''': '''Source language id for translation.'''} ) lowercase__ = field(default=A_, metadata={'''help''': '''Target language id for translation.'''} ) lowercase__ = field(default=A_, metadata={'''help''': '''# num_beams to use for evaluation.'''} ) lowercase__ = field( default=A_, metadata={'''help''': '''If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined.'''}, ) def _SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ , lowercase_ ) -> List[str]: logger.info(f"""***** {split} metrics *****""" ) for key in sorted(metrics.keys() ): logger.info(f""" {key} = {metrics[key]}""" ) save_json(lowercase_ , os.path.join(lowercase_ , f"""{split}_results.json""" ) ) def _SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A__ = HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments) ) 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. A__, A__, A__ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: A__, A__, A__ = parser.parse_args_into_dataclasses() check_output_dir(lowercase_ ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s" , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.parallel_mode == ParallelMode.DISTRIBUTED ) , training_args.fpaa , ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # 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" , lowercase_ ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. A__ = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) A__ = ("encoder_layerdrop", "decoder_layerdrop", "dropout", "attention_dropout") for p in extra_model_params: if getattr(lowercase_ , lowercase_ , lowercase_ ): assert hasattr(lowercase_ , lowercase_ ), f"""({config.__class__.__name__}) doesn't have a `{p}` attribute""" setattr(lowercase_ , lowercase_ , getattr(lowercase_ , lowercase_ ) ) A__ = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) A__ = AutoModelForSeqaSeqLM.from_pretrained( model_args.model_name_or_path , from_tf=".ckpt" in model_args.model_name_or_path , config=lowercase_ , cache_dir=model_args.cache_dir , ) # use task specific params use_task_specific_params(lowercase_ , data_args.task ) # set num_beams for evaluation if data_args.eval_beams is None: A__ = model.config.num_beams # set decoder_start_token_id for MBart if model.config.decoder_start_token_id is None and isinstance(lowercase_ , (MBartTokenizer, MBartTokenizerFast) ): assert ( data_args.tgt_lang is not None and data_args.src_lang is not None ), "mBart requires --tgt_lang and --src_lang" if isinstance(lowercase_ , lowercase_ ): A__ = tokenizer.lang_code_to_id[data_args.tgt_lang] else: A__ = tokenizer.convert_tokens_to_ids(data_args.tgt_lang ) if model_args.freeze_embeds: freeze_embeds(lowercase_ ) if model_args.freeze_encoder: freeze_params(model.get_encoder() ) assert_all_frozen(model.get_encoder() ) A__ = SeqaSeqDataset # Get datasets A__ = ( dataset_class( lowercase_ , type_path="train" , data_dir=data_args.data_dir , n_obs=data_args.n_train , max_target_length=data_args.max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_train else None ) A__ = ( dataset_class( lowercase_ , type_path="val" , data_dir=data_args.data_dir , n_obs=data_args.n_val , max_target_length=data_args.val_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_eval or training_args.evaluation_strategy != EvaluationStrategy.NO else None ) A__ = ( dataset_class( lowercase_ , type_path="test" , data_dir=data_args.data_dir , n_obs=data_args.n_test , max_target_length=data_args.test_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or "" , ) if training_args.do_predict else None ) # Initialize our Trainer A__ = ( build_compute_metrics_fn(data_args.task , lowercase_ ) if training_args.predict_with_generate else None ) A__ = SeqaSeqTrainer( model=lowercase_ , args=lowercase_ , data_args=lowercase_ , train_dataset=lowercase_ , eval_dataset=lowercase_ , data_collator=SeqaSeqDataCollator( lowercase_ , lowercase_ , model.config.decoder_start_token_id , training_args.tpu_num_cores ) , compute_metrics=lowercase_ , tokenizer=lowercase_ , ) A__ = {} # Training if training_args.do_train: logger.info("*** Train ***" ) A__ = trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) A__ = train_result.metrics A__ = data_args.n_train trainer.save_model() # this also saves the tokenizer if trainer.is_world_process_zero(): handle_metrics("train" , lowercase_ , training_args.output_dir ) all_metrics.update(lowercase_ ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , "trainer_state.json" ) ) # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) tokenizer.save_pretrained(training_args.output_dir ) # Evaluation if training_args.do_eval: logger.info("*** Evaluate ***" ) A__ = trainer.evaluate(metric_key_prefix="val" ) A__ = data_args.n_val A__ = round(metrics["val_loss"] , 4 ) if trainer.is_world_process_zero(): handle_metrics("val" , lowercase_ , training_args.output_dir ) all_metrics.update(lowercase_ ) if training_args.do_predict: logger.info("*** Predict ***" ) A__ = trainer.predict(test_dataset=lowercase_ , metric_key_prefix="test" ) A__ = test_output.metrics A__ = data_args.n_test if trainer.is_world_process_zero(): A__ = round(metrics["test_loss"] , 4 ) handle_metrics("test" , lowercase_ , training_args.output_dir ) all_metrics.update(lowercase_ ) if training_args.predict_with_generate: A__ = tokenizer.batch_decode( test_output.predictions , skip_special_tokens=lowercase_ , clean_up_tokenization_spaces=lowercase_ ) A__ = lmap(str.strip , lowercase_ ) write_txt_file(lowercase_ , os.path.join(training_args.output_dir , "test_generations.txt" ) ) if trainer.is_world_process_zero(): save_json(lowercase_ , os.path.join(training_args.output_dir , "all_results.json" ) ) return all_metrics def _SCREAMING_SNAKE_CASE ( lowercase_ ) -> Optional[int]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
230
"""simple docstring""" from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class UpperCAmelCase_ ( A_ ): lowercase__ = ['''image_processor''', '''tokenizer'''] lowercase__ = '''AutoImageProcessor''' lowercase__ = '''AutoTokenizer''' def __init__( self : str , snake_case_ : Dict , snake_case_ : List[str] ) -> str: '''simple docstring''' super().__init__(snake_case_ , snake_case_ ) A__ = self.image_processor def __call__( self : int , snake_case_ : Any=None , snake_case_ : Any=None , snake_case_ : Union[str, Any]=None , **snake_case_ : Optional[int] ) -> Optional[int]: '''simple docstring''' if text is None and images is None: raise ValueError("You have to specify either text or images. Both cannot be none." ) if text is not None: A__ = self.tokenizer(snake_case_ , return_tensors=snake_case_ , **snake_case_ ) if images is not None: A__ = self.image_processor(snake_case_ , return_tensors=snake_case_ , **snake_case_ ) if text is not None and images is not None: A__ = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**snake_case_ ) , tensor_type=snake_case_ ) def __magic_name__ ( self : Optional[int] , *snake_case_ : Union[str, Any] , **snake_case_ : List[Any] ) -> int: '''simple docstring''' return self.tokenizer.batch_decode(*snake_case_ , **snake_case_ ) def __magic_name__ ( self : List[str] , *snake_case_ : List[str] , **snake_case_ : Optional[int] ) -> Tuple: '''simple docstring''' return self.tokenizer.decode(*snake_case_ , **snake_case_ ) @property def __magic_name__ ( self : List[Any] ) -> List[Any]: '''simple docstring''' return ["input_ids", "attention_mask", "pixel_values"]
230
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _A : Union[str, Any] =logging.get_logger(__name__) _A : List[str] ={ """google/vit-base-patch16-224""": """https://huggingface.co/vit-base-patch16-224/resolve/main/config.json""", # See all ViT models at https://huggingface.co/models?filter=vit } class _lowercase ( a_ ): a = '''vit''' def __init__( self: Tuple , UpperCamelCase__: str=768 , UpperCamelCase__: str=12 , UpperCamelCase__: Union[str, Any]=12 , UpperCamelCase__: Dict=3_072 , UpperCamelCase__: Optional[int]="gelu" , UpperCamelCase__: str=0.0 , UpperCamelCase__: List[str]=0.0 , UpperCamelCase__: Union[str, Any]=0.02 , UpperCamelCase__: str=1e-12 , UpperCamelCase__: Union[str, Any]=224 , UpperCamelCase__: Union[str, Any]=16 , UpperCamelCase__: Dict=3 , UpperCamelCase__: str=True , UpperCamelCase__: str=16 , **UpperCamelCase__: str , ): super().__init__(**_lowercase ) lowerCamelCase__ : Tuple = hidden_size lowerCamelCase__ : Dict = num_hidden_layers lowerCamelCase__ : Optional[Any] = num_attention_heads lowerCamelCase__ : Union[str, Any] = intermediate_size lowerCamelCase__ : Any = hidden_act lowerCamelCase__ : Union[str, Any] = hidden_dropout_prob lowerCamelCase__ : Tuple = attention_probs_dropout_prob lowerCamelCase__ : Optional[Any] = initializer_range lowerCamelCase__ : Optional[Any] = layer_norm_eps lowerCamelCase__ : Union[str, Any] = image_size lowerCamelCase__ : str = patch_size lowerCamelCase__ : List[Any] = num_channels lowerCamelCase__ : str = qkv_bias lowerCamelCase__ : Dict = encoder_stride class _lowercase ( a_ ): a = version.parse("""1.11""" ) @property def lowerCamelCase_ ( self: int ): return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def lowerCamelCase_ ( self: List[Any] ): return 1e-4
41
import os import re import shutil from argparse import ArgumentParser, Namespace from datasets.commands import BaseDatasetsCLICommand from datasets.utils.logging import get_logger __snake_case : Dict = """<<<<<<< This should probably be modified because it mentions: """ __snake_case : Any = """======= >>>>>>> """ __snake_case : Any = [ """TextEncoderConfig""", """ByteTextEncoder""", """SubwordTextEncoder""", """encoder_config""", """maybe_build_from_corpus""", """manual_dir""", ] __snake_case : Dict = [ # (pattern, replacement) # Order is important here for some replacements (r"""tfds\.core""", r"""datasets"""), (r"""tf\.io\.gfile\.GFile""", r"""open"""), (r"""tf\.([\w\d]+)""", r"""datasets.Value('\1')"""), (r"""tfds\.features\.Text\(\)""", r"""datasets.Value('string')"""), (r"""tfds\.features\.Text\(""", r"""datasets.Value('string'),"""), (r"""features\s*=\s*tfds.features.FeaturesDict\(""", r"""features=datasets.Features("""), (r"""tfds\.features\.FeaturesDict\(""", r"""dict("""), (r"""The TensorFlow Datasets Authors""", r"""The TensorFlow Datasets Authors and the HuggingFace Datasets Authors"""), (r"""tfds\.""", r"""datasets."""), (r"""dl_manager\.manual_dir""", r"""self.config.data_dir"""), (r"""self\.builder_config""", r"""self.config"""), ] def _UpperCAmelCase ( a__): '''simple docstring''' return ConvertCommand(args.tfds_path , args.datasets_directory) class A__(a_ ): """simple docstring""" @staticmethod def UpperCamelCase__ ( _lowercase ) -> Dict: a_ : Optional[Any] = parser.add_parser( """convert""" , help="""Convert a TensorFlow Datasets dataset to a HuggingFace Datasets dataset.""" , ) train_parser.add_argument( """--tfds_path""" , type=_lowercase , required=_lowercase , help="""Path to a TensorFlow Datasets folder to convert or a single tfds file to convert.""" , ) train_parser.add_argument( """--datasets_directory""" , type=_lowercase , required=_lowercase , help="""Path to the HuggingFace Datasets folder.""" ) train_parser.set_defaults(func=_lowercase ) def __init__( self , _lowercase , _lowercase , *_lowercase ) -> str: a_ : List[Any] = get_logger("""datasets-cli/converting""" ) a_ : Optional[Any] = tfds_path a_ : List[Any] = datasets_directory def UpperCamelCase__ ( self ) -> Dict: if os.path.isdir(self._tfds_path ): a_ : List[Any] = os.path.abspath(self._tfds_path ) elif os.path.isfile(self._tfds_path ): a_ : Dict = os.path.dirname(self._tfds_path ) else: raise ValueError("""--tfds_path is neither a directory nor a file. Please check path.""" ) a_ : List[Any] = os.path.abspath(self._datasets_directory ) self._logger.info(F'''Converting datasets from {abs_tfds_path} to {abs_datasets_path}''' ) a_ : Dict = [] a_ : Tuple = [] a_ : str = {} if os.path.isdir(self._tfds_path ): a_ : str = os.listdir(_lowercase ) else: a_ : int = [os.path.basename(self._tfds_path )] for f_name in file_names: self._logger.info(F'''Looking at file {f_name}''' ) a_ : List[str] = os.path.join(_lowercase , _lowercase ) a_ : Dict = os.path.join(_lowercase , _lowercase ) if not os.path.isfile(_lowercase ) or "__init__" in f_name or "_test" in f_name or ".py" not in f_name: self._logger.info("""Skipping file""" ) continue with open(_lowercase , encoding="""utf-8""" ) as f: a_ : Any = f.readlines() a_ : Any = [] a_ : str = False a_ : List[str] = False a_ : List[Any] = [] for line in lines: a_ : Union[str, Any] = line # Convert imports if "import tensorflow.compat.v2 as tf" in out_line: continue elif "@tfds.core" in out_line: continue elif "builder=self" in out_line: continue elif "import tensorflow_datasets.public_api as tfds" in out_line: a_ : List[Any] = """import datasets\n""" elif "import tensorflow" in out_line: # order is important here a_ : Optional[int] = """""" continue elif "from absl import logging" in out_line: a_ : List[str] = """from datasets import logging\n""" elif "getLogger" in out_line: a_ : List[str] = out_line.replace("""getLogger""" , """get_logger""" ) elif any(expression in out_line for expression in TO_HIGHLIGHT ): a_ : Dict = True a_ : Optional[Any] = list(filter(lambda _lowercase : e in out_line , _lowercase ) ) out_lines.append(HIGHLIGHT_MESSAGE_PRE + str(_lowercase ) + """\n""" ) out_lines.append(_lowercase ) out_lines.append(_lowercase ) continue else: for pattern, replacement in TO_CONVERT: a_ : List[str] = re.sub(_lowercase , _lowercase , _lowercase ) # Take care of saving utilities (to later move them together with main script) if "tensorflow_datasets" in out_line: a_ : Tuple = re.match(r"""from\stensorflow_datasets.*import\s([^\.\r\n]+)""" , _lowercase ) tfds_imports.extend(imp.strip() for imp in match.group(1 ).split(""",""" ) ) a_ : Optional[int] = """from . import """ + match.group(1 ) # Check we have not forget anything if "tf." in out_line or "tfds." in out_line or "tensorflow_datasets" in out_line: raise ValueError(F'''Error converting {out_line.strip()}''' ) if "GeneratorBasedBuilder" in out_line or "BeamBasedBuilder" in out_line: a_ : Optional[Any] = True out_lines.append(_lowercase ) if is_builder or "wmt" in f_name: # We create a new directory for each dataset a_ : List[str] = f_name.replace(""".py""" , """""" ) a_ : Optional[int] = os.path.join(_lowercase , _lowercase ) a_ : Dict = os.path.join(_lowercase , _lowercase ) os.makedirs(_lowercase , exist_ok=_lowercase ) self._logger.info(F'''Adding directory {output_dir}''' ) imports_to_builder_map.update({imp: output_dir for imp in tfds_imports} ) else: # Utilities will be moved at the end utils_files.append(_lowercase ) if needs_manual_update: with_manual_update.append(_lowercase ) with open(_lowercase , """w""" , encoding="""utf-8""" ) as f: f.writelines(_lowercase ) self._logger.info(F'''Converted in {output_file}''' ) for utils_file in utils_files: try: a_ : Optional[int] = os.path.basename(_lowercase ) a_ : List[Any] = imports_to_builder_map[f_name.replace(""".py""" , """""" )] self._logger.info(F'''Moving {dest_folder} to {utils_file}''' ) shutil.copy(_lowercase , _lowercase ) except KeyError: self._logger.error(F'''Cannot find destination folder for {utils_file}. Please copy manually.''' ) if with_manual_update: for file_path in with_manual_update: self._logger.warning( F'''You need to manually update file {file_path} to remove configurations using \'TextEncoderConfig\'.''' )
248
0
from __future__ import annotations def _A ( _a : int | float | str , _a : int | float | str ): """simple docstring""" if nth_term == "": return [""] A = int(_a ) A = int(_a ) A = [] for temp in range(int(_a ) ): series.append(f'1 / {pow(temp + 1 , int(_a ) )}' if series else """1""" ) return series if __name__ == "__main__": import doctest doctest.testmod() UpperCAmelCase =int(input("Enter the last number (nth term) of the P-Series")) UpperCAmelCase =int(input("Enter the power for P-Series")) print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p") print(p_series(nth_term, power))
370
"""simple docstring""" def _A ( _a : int ): """simple docstring""" A = abs(_a ) A = 0 while n > 0: res += n % 1_0 n //= 1_0 return res def _A ( _a : int ): """simple docstring""" A = abs(_a ) return n if n < 1_0 else n % 1_0 + sum_of_digits(n // 1_0 ) def _A ( _a : int ): """simple docstring""" return sum(int(_a ) for c in str(abs(_a ) ) ) def _A ( ): """simple docstring""" from collections.abc import Callable from timeit import timeit def benchmark_a_function(_a : Callable , _a : int ) -> None: A = f'{func.__name__}({value})' A = timeit(f'__main__.{call}' , setup="""import __main__""" ) print(f'{call:56} = {func(_a )} -- {timing:.4f} seconds' ) for value in (2_6_2_1_4_4, 1_1_2_5_8_9_9_9_0_6_8_4_2_6_2_4, 1_2_6_7_6_5_0_6_0_0_2_2_8_2_2_9_4_0_1_4_9_6_7_0_3_2_0_5_3_7_6): for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact): benchmark_a_function(_a , _a ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
77
0
"""simple docstring""" from typing import List, Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class SCREAMING_SNAKE_CASE__ ( __lowerCamelCase ): __lowerCAmelCase : Any = ["image_processor", "tokenizer"] __lowerCAmelCase : List[Any] = "BlipImageProcessor" __lowerCAmelCase : Optional[int] = ("BertTokenizer", "BertTokenizerFast") def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Optional[Any] = False super().__init__(__A , __A ) UpperCAmelCase : int = self.image_processor def __call__( self , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> Tuple: '''simple docstring''' if images is None and text is None: raise ValueError("""You have to specify either images or text.""" ) # Get only text if images is None: UpperCAmelCase : Any = self.tokenizer UpperCAmelCase : Optional[Any] = self.tokenizer( text=__A , add_special_tokens=__A , padding=__A , truncation=__A , max_length=__A , stride=__A , pad_to_multiple_of=__A , return_attention_mask=__A , return_overflowing_tokens=__A , return_special_tokens_mask=__A , return_offsets_mapping=__A , return_token_type_ids=__A , return_length=__A , verbose=__A , return_tensors=__A , **__A , ) return text_encoding # add pixel_values UpperCAmelCase : List[Any] = self.image_processor(__A , return_tensors=__A ) if text is not None: UpperCAmelCase : Dict = self.tokenizer( text=__A , add_special_tokens=__A , padding=__A , truncation=__A , max_length=__A , stride=__A , pad_to_multiple_of=__A , return_attention_mask=__A , return_overflowing_tokens=__A , return_special_tokens_mask=__A , return_offsets_mapping=__A , return_token_type_ids=__A , return_length=__A , verbose=__A , return_tensors=__A , **__A , ) else: UpperCAmelCase : Dict = None if text_encoding is not None: encoding_image_processor.update(__A ) return encoding_image_processor def SCREAMING_SNAKE_CASE ( self , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> List[str]: '''simple docstring''' return self.tokenizer.batch_decode(*__A , **__A ) def SCREAMING_SNAKE_CASE ( self , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> List[str]: '''simple docstring''' return self.tokenizer.decode(*__A , **__A ) @property def SCREAMING_SNAKE_CASE ( self ) -> int: '''simple docstring''' UpperCAmelCase : List[Any] = self.tokenizer.model_input_names UpperCAmelCase : Optional[int] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
109
'''simple docstring''' import os from typing import BinaryIO, Optional, Union import numpy as np import pyarrow.parquet as pq from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config from ..features.features import FeatureType, _visit from ..formatting import query_table from ..packaged_modules import _PACKAGED_DATASETS_MODULES from ..packaged_modules.parquet.parquet import Parquet from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader def lowercase__ ( __lowercase : Features ) -> Optional[int]: """simple docstring""" __UpperCamelCase = np.inf def set_batch_size(__lowercase : FeatureType ) -> None: nonlocal batch_size if isinstance(__lowercase , __lowercase ): __UpperCamelCase = min(__lowercase , config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS ) elif isinstance(__lowercase , __lowercase ): __UpperCamelCase = min(__lowercase , config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS ) elif isinstance(__lowercase , __lowercase ) and feature.dtype == "binary": __UpperCamelCase = min(__lowercase , config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS ) _visit(__lowercase , __lowercase ) return None if batch_size is np.inf else batch_size class snake_case ( __lowerCamelCase ): """simple docstring""" def __init__( self : List[str] , __A : NestedDataStructureLike[PathLike] , __A : Optional[NamedSplit] = None , __A : Optional[Features] = None , __A : str = None , __A : bool = False , __A : bool = False , __A : Optional[int] = None , **__A : Dict , ): super().__init__( __A , split=__A , features=__A , cache_dir=__A , keep_in_memory=__A , streaming=__A , num_proc=__A , **__A , ) __UpperCamelCase = path_or_paths if isinstance(__A , __A ) else {self.split: path_or_paths} __UpperCamelCase = _PACKAGED_DATASETS_MODULES['parquet'][1] __UpperCamelCase = Parquet( cache_dir=__A , data_files=__A , features=__A , hash=__A , **__A , ) def _lowerCamelCase ( self : Optional[int] ): # Build iterable dataset if self.streaming: __UpperCamelCase = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: __UpperCamelCase = None __UpperCamelCase = None __UpperCamelCase = None __UpperCamelCase = None self.builder.download_and_prepare( download_config=__A , download_mode=__A , verification_mode=__A , base_path=__A , num_proc=self.num_proc , ) __UpperCamelCase = self.builder.as_dataset( split=self.split , verification_mode=__A , in_memory=self.keep_in_memory ) return dataset class snake_case : """simple docstring""" def __init__( self : List[str] , __A : Dataset , __A : Union[PathLike, BinaryIO] , __A : Optional[int] = None , **__A : Dict , ): __UpperCamelCase = dataset __UpperCamelCase = path_or_buf __UpperCamelCase = batch_size or get_writer_batch_size(dataset.features ) __UpperCamelCase = parquet_writer_kwargs def _lowerCamelCase ( self : Optional[int] ): __UpperCamelCase = self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE if isinstance(self.path_or_buf , (str, bytes, os.PathLike) ): with open(self.path_or_buf , 'wb+' ) as buffer: __UpperCamelCase = self._write(file_obj=__A , batch_size=__A , **self.parquet_writer_kwargs ) else: __UpperCamelCase = self._write(file_obj=self.path_or_buf , batch_size=__A , **self.parquet_writer_kwargs ) return written def _lowerCamelCase ( self : List[str] , __A : BinaryIO , __A : int , **__A : List[str] ): __UpperCamelCase = 0 __UpperCamelCase = parquet_writer_kwargs.pop('path_or_buf' , __A ) __UpperCamelCase = self.dataset.features.arrow_schema __UpperCamelCase = pq.ParquetWriter(__A , schema=__A , **__A ) for offset in logging.tqdm( range(0 , len(self.dataset ) , __A ) , unit='ba' , disable=not logging.is_progress_bar_enabled() , desc='Creating parquet from Arrow format' , ): __UpperCamelCase = query_table( table=self.dataset._data , key=slice(__A , offset + batch_size ) , indices=self.dataset._indices if self.dataset._indices is not None else None , ) writer.write_table(__A ) written += batch.nbytes writer.close() return written
53
0
from __future__ import annotations from cmath import sqrt def snake_case (UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) -> tuple[complex, complex]: if a == 0: raise ValueError('Coefficient \'a\' must not be zero.' ) UpperCamelCase_: Optional[Any] = b * b - 4 * a * c UpperCamelCase_: str = (-b + sqrt(UpperCAmelCase__ )) / (2 * a) UpperCamelCase_: Optional[int] = (-b - sqrt(UpperCAmelCase__ )) / (2 * a) return ( root_a.real if not root_a.imag else root_a, root_a.real if not root_a.imag else root_a, ) def snake_case () -> int: UpperCamelCase_ ,UpperCamelCase_: Any = quadratic_roots(a=5 , b=6 , c=1 ) print(F'''The solutions are: {solutiona} and {solutiona}''' ) if __name__ == "__main__": main()
292
import argparse import json import os import re import torch from transformers import BloomConfig, BloomModel from transformers.file_utils import CONFIG_NAME, WEIGHTS_NAME from transformers.utils import logging logging.set_verbosity_info() A_ : str = [ 'word_embeddings_layernorm.weight', 'word_embeddings_layernorm.bias', 'input_layernorm.weight', 'input_layernorm.bias', 'post_attention_layernorm.weight', 'post_attention_layernorm.bias', 'self_attention.dense.bias', 'mlp.dense_4h_to_h.bias', 'ln_f.weight', 'ln_f.bias', ] A_ : Optional[int] = [ 'mlp.dense_4h_to_h.weight', 'self_attention.dense.weight', ] def snake_case (UpperCAmelCase__ , UpperCAmelCase__ ) -> List[str]: UpperCamelCase_: Dict = { 'word_embeddings.weight': 'word_embeddings.weight', 'word_embeddings.norm.weight': 'word_embeddings_layernorm.weight', 'word_embeddings.norm.bias': 'word_embeddings_layernorm.bias', 'weight': 'ln_f.weight', 'bias': 'ln_f.bias', } if key in layer_rename_map: return layer_rename_map[key] # Handle transformer blocks UpperCamelCase_: Tuple = int(re.match(R'.*layer_(\d*).*' , UpperCAmelCase__ )[1] ) layer_number -= 3 return F'''h.{layer_number}.''' + key def snake_case (UpperCAmelCase__ ) -> List[str]: if dtype == torch.bool: return 1 / 8 UpperCamelCase_: Optional[Any] = re.search(R'[^\d](\d+)$' , str(UpperCAmelCase__ ) ) if bit_search is None: raise ValueError(F'''`dtype` is not a valid dtype: {dtype}.''' ) UpperCamelCase_: List[Any] = int(bit_search.groups()[0] ) return bit_size // 8 def snake_case (UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) -> Any: # Construct model if bloom_config_file == "": UpperCamelCase_: List[str] = BloomConfig() else: UpperCamelCase_: List[str] = BloomConfig.from_json_file(UpperCAmelCase__ ) if shard_model: UpperCamelCase_: str = os.listdir(UpperCAmelCase__ ) UpperCamelCase_: List[str] = sorted(filter(lambda UpperCAmelCase__ : s.startswith('layer' ) and "model_00" in s , UpperCAmelCase__ ) ) UpperCamelCase_: Optional[int] = {'weight_map': {}, 'metadata': {}} UpperCamelCase_: str = 0 UpperCamelCase_: Optional[Any] = None UpperCamelCase_: int = BloomConfig() for j, file in enumerate(UpperCAmelCase__ ): print('Processing file: {}'.format(UpperCAmelCase__ ) ) UpperCamelCase_: Tuple = None for i in range(UpperCAmelCase__ ): # load all TP files UpperCamelCase_: List[Any] = file.replace('model_00' , F'''model_0{i}''' ) UpperCamelCase_: List[str] = torch.load(os.path.join(UpperCAmelCase__ , UpperCAmelCase__ ) , map_location='cpu' ) # Rename keys in the transformers names UpperCamelCase_: Optional[int] = list(temp.keys() ) for key in keys: UpperCamelCase_: List[Any] = temp.pop(UpperCAmelCase__ ) if tensors is None: UpperCamelCase_: Dict = temp else: for key in tensors.keys(): if any(key.endswith(UpperCAmelCase__ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel UpperCamelCase_: List[Any] = 1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks UpperCamelCase_: Dict = torch.cat([tensors[key], temp[key]] , dim=UpperCAmelCase__ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(UpperCAmelCase__ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): UpperCamelCase_: Optional[int] = tensors[key] / pretraining_tp torch.save( UpperCAmelCase__ , os.path.join( UpperCAmelCase__ , 'pytorch_model_{}-of-{}.bin'.format(str(j + 1 ).zfill(5 ) , str(len(UpperCAmelCase__ ) ).zfill(5 ) ) , ) , ) for key in tensors.keys(): UpperCamelCase_: int = tensors[key] total_size += value.numel() * get_dtype_size(value.dtype ) if key not in index_dict["weight_map"]: UpperCamelCase_: Dict = 'pytorch_model_{}-of-{}.bin'.format( str(j + 1 ).zfill(5 ) , str(len(UpperCAmelCase__ ) ).zfill(5 ) ) UpperCamelCase_: Union[str, Any] = BloomConfig() UpperCamelCase_: Any = pytorch_dump_folder_path + '/' + CONFIG_NAME UpperCamelCase_: Optional[int] = total_size with open(UpperCAmelCase__ , 'w' , encoding='utf-8' ) as f: f.write(config.to_json_string() ) with open(os.path.join(UpperCAmelCase__ , WEIGHTS_NAME + '.index.json' ) , 'w' , encoding='utf-8' ) as f: UpperCamelCase_: Tuple = json.dumps(UpperCAmelCase__ , indent=2 , sort_keys=UpperCAmelCase__ ) + '\n' f.write(UpperCAmelCase__ ) else: UpperCamelCase_: Optional[Any] = BloomModel(UpperCAmelCase__ ) UpperCamelCase_: Tuple = os.listdir(UpperCAmelCase__ ) UpperCamelCase_: Tuple = sorted(filter(lambda UpperCAmelCase__ : s.startswith('layer' ) and "model_00" in s , UpperCAmelCase__ ) ) UpperCamelCase_: Tuple = None for i, file in enumerate(UpperCAmelCase__ ): UpperCamelCase_: Union[str, Any] = None for i in range(UpperCAmelCase__ ): # load all TP files UpperCamelCase_: Any = file.replace('model_00' , F'''model_0{i}''' ) UpperCamelCase_: Union[str, Any] = torch.load(os.path.join(UpperCAmelCase__ , UpperCAmelCase__ ) , map_location='cpu' ) # Rename keys in the transformers names UpperCamelCase_: Dict = list(temp.keys() ) for key in keys: UpperCamelCase_: Any = temp.pop(UpperCAmelCase__ ) if tensors is None: UpperCamelCase_: Any = temp else: for key in tensors.keys(): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) if any(key.endswith(UpperCAmelCase__ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel UpperCamelCase_: int = 1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks UpperCamelCase_: Optional[int] = torch.cat([tensors[key], temp[key]] , dim=UpperCAmelCase__ ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(UpperCAmelCase__ ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): UpperCamelCase_: Tuple = tensors[key] / pretraining_tp UpperCamelCase_: Any = model.load_state_dict(UpperCAmelCase__ , strict=UpperCAmelCase__ ) assert not other_keys.unexpected_keys, F'''The keys {other_keys.unexpected_keys} are unexpected''' if missing_keys is None: UpperCamelCase_: Any = set(other_keys.missing_keys ) else: UpperCamelCase_: int = missing_keys.intersection(set(other_keys.missing_keys ) ) assert not missing_keys, F'''The keys {missing_keys} are missing''' # Save pytorch-model os.makedirs(UpperCAmelCase__ , exist_ok=UpperCAmelCase__ ) UpperCamelCase_: Optional[Any] = pytorch_dump_folder_path + '/' + WEIGHTS_NAME UpperCamelCase_: str = pytorch_dump_folder_path + '/' + CONFIG_NAME print(F'''Save PyTorch model to {pytorch_weights_dump_path} with dtype {config.torch_dtype}''' ) if config.torch_dtype is not None: UpperCamelCase_: Tuple = model.to(config.torch_dtype ) torch.save(model.state_dict() , UpperCAmelCase__ ) print(F'''Save configuration file to {pytorch_config_dump_path}''' ) with open(UpperCAmelCase__ , 'w' , encoding='utf-8' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": A_ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( '--bloom_checkpoint_path', default=None, type=str, required=True, help='Path to the Megatron-LM checkpoint path.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) parser.add_argument( '--bloom_config_file', default='', type=str, help=( 'An optional config json file corresponding to the pre-trained model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--shard_model', action='store_true', help='An optional setting to shard the output model \nThis enables sharding the converted checkpoint', ) parser.add_argument( '--pretraining_tp', default=4, type=int, help='Pretraining TP rank that has been used when training the model in Megatron-LM \n', ) A_ : Any = parser.parse_args() convert_bloom_checkpoint_to_pytorch( args.bloom_checkpoint_path, args.bloom_config_file, args.pytorch_dump_folder_path, args.shard_model, args.pretraining_tp, )
292
1
"""simple docstring""" from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __magic_name__ = { "configuration_informer": [ "INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "InformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __magic_name__ = [ "INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "InformerForPrediction", "InformerModel", "InformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_informer import INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, InformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_informer import ( INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, InformerForPrediction, InformerModel, InformerPreTrainedModel, ) else: import sys __magic_name__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
100
from typing import Optional from .. import Features, NamedSplit from ..packaged_modules.text.text import Text from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class _A ( __UpperCAmelCase ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : NestedDataStructureLike[PathLike] , __SCREAMING_SNAKE_CASE : Optional[NamedSplit] = None , __SCREAMING_SNAKE_CASE : Optional[Features] = None , __SCREAMING_SNAKE_CASE : str = None , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : Optional[int] = None , **__SCREAMING_SNAKE_CASE : List[str] , ): '''simple docstring''' super().__init__( __SCREAMING_SNAKE_CASE , split=__SCREAMING_SNAKE_CASE , features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE , keep_in_memory=__SCREAMING_SNAKE_CASE , streaming=__SCREAMING_SNAKE_CASE , num_proc=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) __a = path_or_paths if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) else {self.split: path_or_paths} __a = Text( cache_dir=__SCREAMING_SNAKE_CASE , data_files=__SCREAMING_SNAKE_CASE , features=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) def _lowerCamelCase ( self : List[str]): '''simple docstring''' if self.streaming: __a = self.builder.as_streaming_dataset(split=self.split) # Build regular (map-style) dataset else: __a = None __a = None __a = None __a = None self.builder.download_and_prepare( download_config=__SCREAMING_SNAKE_CASE , download_mode=__SCREAMING_SNAKE_CASE , verification_mode=__SCREAMING_SNAKE_CASE , base_path=__SCREAMING_SNAKE_CASE , num_proc=self.num_proc , ) __a = self.builder.as_dataset( split=self.split , verification_mode=__SCREAMING_SNAKE_CASE , in_memory=self.keep_in_memory) return dataset
49
0
'''simple docstring''' from __future__ import annotations def __lowerCamelCase ( __snake_case : list[int] ) -> bool: """simple docstring""" return len(set(__snake_case ) ) == len(__snake_case ) if __name__ == "__main__": import doctest doctest.testmod()
352
'''simple docstring''' import argparse import gc import json import os import shutil import warnings import torch from transformers import LlamaConfig, LlamaForCausalLM, LlamaTokenizer try: from transformers import LlamaTokenizerFast except ImportError as e: warnings.warn(e) warnings.warn( 'The converted tokenizer will be the `slow` tokenizer. To use the fast, update your `tokenizers` library and re-run the tokenizer conversion' ) __snake_case : Optional[Any] = None __snake_case : Optional[Any] = { '7B': 1_1008, '13B': 1_3824, '30B': 1_7920, '65B': 2_2016, '70B': 2_8672, } __snake_case : Union[str, Any] = { '7B': 1, '7Bf': 1, '13B': 2, '13Bf': 2, '30B': 4, '65B': 8, '70B': 8, '70Bf': 8, } def __lowerCamelCase ( __snake_case : Optional[Any], __snake_case : str=1, __snake_case : Tuple=256 ) -> str: """simple docstring""" return multiple_of * ((int(ffn_dim_multiplier * int(8 * n / 3 ) ) + multiple_of - 1) // multiple_of) def __lowerCamelCase ( __snake_case : Tuple ) -> Tuple: """simple docstring""" with open(__snake_case, """r""" ) as f: return json.load(__snake_case ) def __lowerCamelCase ( __snake_case : Optional[int], __snake_case : Tuple ) -> Dict: """simple docstring""" with open(__snake_case, """w""" ) as f: json.dump(__snake_case, __snake_case ) def __lowerCamelCase ( __snake_case : List[Any], __snake_case : Any, __snake_case : Any, __snake_case : Tuple=True ) -> Any: """simple docstring""" os.makedirs(__snake_case, exist_ok=__snake_case ) A__ : List[Any] =os.path.join(__snake_case, """tmp""" ) os.makedirs(__snake_case, exist_ok=__snake_case ) A__ : Dict =read_json(os.path.join(__snake_case, """params.json""" ) ) A__ : Dict =NUM_SHARDS[model_size] A__ : List[str] =params["""n_layers"""] A__ : int =params["""n_heads"""] A__ : str =n_heads // num_shards A__ : Tuple =params["""dim"""] A__ : Union[str, Any] =dim // n_heads A__ : str =1_00_00.0 A__ : Any =1.0 / (base ** (torch.arange(0, __snake_case, 2 ).float() / dims_per_head)) if "n_kv_heads" in params: A__ : Optional[Any] =params["""n_kv_heads"""] # for GQA / MQA A__ : int =n_heads_per_shard // num_key_value_heads A__ : int =dim // num_key_value_heads else: # compatibility with other checkpoints A__ : List[Any] =n_heads A__ : List[str] =n_heads_per_shard A__ : Dict =dim # permute for sliced rotary def permute(__snake_case : Tuple, __snake_case : Optional[int]=n_heads, __snake_case : int=dim, __snake_case : Optional[Any]=dim ): return w.view(__snake_case, dima // n_heads // 2, 2, __snake_case ).transpose(1, 2 ).reshape(__snake_case, __snake_case ) print(f"Fetching all parameters from the checkpoint at {input_base_path}." ) # Load weights if model_size == "7B": # Not sharded # (The sharded implementation would also work, but this is simpler.) A__ : List[str] =torch.load(os.path.join(__snake_case, """consolidated.00.pth""" ), map_location="""cpu""" ) else: # Sharded A__ : Optional[Any] =[ torch.load(os.path.join(__snake_case, f"consolidated.{i:02d}.pth" ), map_location="""cpu""" ) for i in range(__snake_case ) ] A__ : Optional[Any] =0 A__ : str ={"""weight_map""": {}} for layer_i in range(__snake_case ): A__ : Dict =f"pytorch_model-{layer_i + 1}-of-{n_layers + 1}.bin" if model_size == "7B": # Unsharded A__ : Dict ={ f"model.layers.{layer_i}.self_attn.q_proj.weight": permute( loaded[f"layers.{layer_i}.attention.wq.weight"] ), f"model.layers.{layer_i}.self_attn.k_proj.weight": permute( loaded[f"layers.{layer_i}.attention.wk.weight"] ), f"model.layers.{layer_i}.self_attn.v_proj.weight": loaded[f"layers.{layer_i}.attention.wv.weight"], f"model.layers.{layer_i}.self_attn.o_proj.weight": loaded[f"layers.{layer_i}.attention.wo.weight"], f"model.layers.{layer_i}.mlp.gate_proj.weight": loaded[f"layers.{layer_i}.feed_forward.w1.weight"], f"model.layers.{layer_i}.mlp.down_proj.weight": loaded[f"layers.{layer_i}.feed_forward.w2.weight"], f"model.layers.{layer_i}.mlp.up_proj.weight": loaded[f"layers.{layer_i}.feed_forward.w3.weight"], f"model.layers.{layer_i}.input_layernorm.weight": loaded[f"layers.{layer_i}.attention_norm.weight"], f"model.layers.{layer_i}.post_attention_layernorm.weight": loaded[f"layers.{layer_i}.ffn_norm.weight"], } else: # Sharded # Note that attention.w{q,k,v,o}, feed_fordward.w[1,2,3], attention_norm.weight and ffn_norm.weight share # the same storage object, saving attention_norm and ffn_norm will save other weights too, which is # redundant as other weights will be stitched from multiple shards. To avoid that, they are cloned. A__ : Any ={ f"model.layers.{layer_i}.input_layernorm.weight": loaded[0][ f"layers.{layer_i}.attention_norm.weight" ].clone(), f"model.layers.{layer_i}.post_attention_layernorm.weight": loaded[0][ f"layers.{layer_i}.ffn_norm.weight" ].clone(), } A__ : Optional[Any] =permute( torch.cat( [ loaded[i][f"layers.{layer_i}.attention.wq.weight"].view(__snake_case, __snake_case, __snake_case ) for i in range(__snake_case ) ], dim=0, ).reshape(__snake_case, __snake_case ) ) A__ : int =permute( torch.cat( [ loaded[i][f"layers.{layer_i}.attention.wk.weight"].view( __snake_case, __snake_case, __snake_case ) for i in range(__snake_case ) ], dim=0, ).reshape(__snake_case, __snake_case ), __snake_case, __snake_case, __snake_case, ) A__ : int =torch.cat( [ loaded[i][f"layers.{layer_i}.attention.wv.weight"].view( __snake_case, __snake_case, __snake_case ) for i in range(__snake_case ) ], dim=0, ).reshape(__snake_case, __snake_case ) A__ : List[str] =torch.cat( [loaded[i][f"layers.{layer_i}.attention.wo.weight"] for i in range(__snake_case )], dim=1 ) A__ : Optional[int] =torch.cat( [loaded[i][f"layers.{layer_i}.feed_forward.w1.weight"] for i in range(__snake_case )], dim=0 ) A__ : str =torch.cat( [loaded[i][f"layers.{layer_i}.feed_forward.w2.weight"] for i in range(__snake_case )], dim=1 ) A__ : List[str] =torch.cat( [loaded[i][f"layers.{layer_i}.feed_forward.w3.weight"] for i in range(__snake_case )], dim=0 ) A__ : List[Any] =inv_freq for k, v in state_dict.items(): A__ : Optional[Any] =filename param_count += v.numel() torch.save(__snake_case, os.path.join(__snake_case, __snake_case ) ) A__ : Tuple =f"pytorch_model-{n_layers + 1}-of-{n_layers + 1}.bin" if model_size == "7B": # Unsharded A__ : Tuple ={ """model.embed_tokens.weight""": loaded["""tok_embeddings.weight"""], """model.norm.weight""": loaded["""norm.weight"""], """lm_head.weight""": loaded["""output.weight"""], } else: A__ : Any ={ """model.norm.weight""": loaded[0]["""norm.weight"""], """model.embed_tokens.weight""": torch.cat( [loaded[i]["""tok_embeddings.weight"""] for i in range(__snake_case )], dim=1 ), """lm_head.weight""": torch.cat([loaded[i]["""output.weight"""] for i in range(__snake_case )], dim=0 ), } for k, v in state_dict.items(): A__ : int =filename param_count += v.numel() torch.save(__snake_case, os.path.join(__snake_case, __snake_case ) ) # Write configs A__ : Union[str, Any] ={"""total_size""": param_count * 2} write_json(__snake_case, os.path.join(__snake_case, """pytorch_model.bin.index.json""" ) ) A__ : Optional[Any] =params["""ffn_dim_multiplier"""] if """ffn_dim_multiplier""" in params else 1 A__ : List[Any] =params["""multiple_of"""] if """multiple_of""" in params else 256 A__ : int =LlamaConfig( hidden_size=__snake_case, intermediate_size=compute_intermediate_size(__snake_case, __snake_case, __snake_case ), num_attention_heads=params["""n_heads"""], num_hidden_layers=params["""n_layers"""], rms_norm_eps=params["""norm_eps"""], num_key_value_heads=__snake_case, ) config.save_pretrained(__snake_case ) # Make space so we can load the model properly now. del state_dict del loaded gc.collect() print("""Loading the checkpoint in a Llama model.""" ) A__ : List[Any] =LlamaForCausalLM.from_pretrained(__snake_case, torch_dtype=torch.floataa, low_cpu_mem_usage=__snake_case ) # Avoid saving this as part of the config. del model.config._name_or_path print("""Saving in the Transformers format.""" ) model.save_pretrained(__snake_case, safe_serialization=__snake_case ) shutil.rmtree(__snake_case ) def __lowerCamelCase ( __snake_case : Union[str, Any], __snake_case : Dict ) -> Tuple: """simple docstring""" A__ : List[Any] =LlamaTokenizer if LlamaTokenizerFast is None else LlamaTokenizerFast print(f"Saving a {tokenizer_class.__name__} to {tokenizer_path}." ) A__ : List[str] =tokenizer_class(__snake_case ) tokenizer.save_pretrained(__snake_case ) def __lowerCamelCase ( ) -> Any: """simple docstring""" A__ : List[str] =argparse.ArgumentParser() parser.add_argument( """--input_dir""", help="""Location of LLaMA weights, which contains tokenizer.model and model folders""", ) parser.add_argument( """--model_size""", choices=["""7B""", """7Bf""", """13B""", """13Bf""", """30B""", """65B""", """70B""", """70Bf""", """tokenizer_only"""], ) parser.add_argument( """--output_dir""", help="""Location to write HF model and tokenizer""", ) parser.add_argument("""--safe_serialization""", type=__snake_case, help="""Whether or not to save using `safetensors`.""" ) A__ : Any =parser.parse_args() if args.model_size != "tokenizer_only": write_model( model_path=args.output_dir, input_base_path=os.path.join(args.input_dir, args.model_size ), model_size=args.model_size, safe_serialization=args.safe_serialization, ) A__ : List[Any] =os.path.join(args.input_dir, """tokenizer.model""" ) write_tokenizer(args.output_dir, __snake_case ) if __name__ == "__main__": main()
136
0
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import TensorType, is_torch_available, logging _snake_case = logging.get_logger(__name__) _snake_case = { '''Helsinki-NLP/opus-mt-en-de''': '''https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json''', # See all Marian models at https://huggingface.co/models?filter=marian } class UpperCAmelCase_ ( a__ ): '''simple docstring''' __A : Any = """marian""" __A : Any = ["""past_key_values"""] __A : Optional[int] = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self , __A=5_8101 , __A=None , __A=1024 , __A=12 , __A=4096 , __A=16 , __A=12 , __A=4096 , __A=16 , __A=0.0 , __A=0.0 , __A=True , __A=True , __A="gelu" , __A=1024 , __A=0.1 , __A=0.0 , __A=0.0 , __A=0.02 , __A=5_8100 , __A=False , __A=5_8100 , __A=0 , __A=0 , __A=True , **__A , ): """simple docstring""" lowerCamelCase : Optional[Any] = vocab_size lowerCamelCase : Dict = decoder_vocab_size or vocab_size lowerCamelCase : Dict = max_position_embeddings lowerCamelCase : Dict = d_model lowerCamelCase : Optional[Any] = encoder_ffn_dim lowerCamelCase : Union[str, Any] = encoder_layers lowerCamelCase : Any = encoder_attention_heads lowerCamelCase : Optional[Any] = decoder_ffn_dim lowerCamelCase : Optional[int] = decoder_layers lowerCamelCase : Optional[int] = decoder_attention_heads lowerCamelCase : Dict = dropout lowerCamelCase : Optional[Any] = attention_dropout lowerCamelCase : Optional[int] = activation_dropout lowerCamelCase : int = activation_function lowerCamelCase : Union[str, Any] = init_std lowerCamelCase : Optional[int] = encoder_layerdrop lowerCamelCase : Dict = decoder_layerdrop lowerCamelCase : Any = use_cache lowerCamelCase : List[str] = encoder_layers lowerCamelCase : str = scale_embedding # scale factor will be sqrt(d_model) if True lowerCamelCase : List[str] = share_encoder_decoder_embeddings super().__init__( pad_token_id=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__ , is_encoder_decoder=lowerCAmelCase__ , decoder_start_token_id=lowerCAmelCase__ , forced_eos_token_id=lowerCAmelCase__ , **lowerCAmelCase__ , ) class UpperCAmelCase_ ( a__ ): '''simple docstring''' @property # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs def _snake_case ( self ): """simple docstring""" if self.task in ["default", "seq2seq-lm"]: lowerCamelCase : Dict = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: lowerCamelCase : Tuple = {0: "batch"} lowerCamelCase : Tuple = {0: "batch", 1: "past_decoder_sequence + sequence"} else: lowerCamelCase : str = {0: "batch", 1: "decoder_sequence"} lowerCamelCase : List[Any] = {0: "batch", 1: "decoder_sequence"} if self.use_past: self.fill_with_past_key_values_(lowerCAmelCase__ , direction="inputs" ) elif self.task == "causal-lm": # TODO: figure this case out. lowerCamelCase : int = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: lowerCamelCase : Tuple = self.num_layers for i in range(lowerCAmelCase__ ): lowerCamelCase : Optional[Any] = {0: "batch", 2: "past_sequence + sequence"} lowerCamelCase : Optional[Any] = {0: "batch", 2: "past_sequence + sequence"} else: lowerCamelCase : Optional[int] = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ("decoder_input_ids", {0: "batch", 1: "decoder_sequence"}), ("decoder_attention_mask", {0: "batch", 1: "decoder_sequence"}), ] ) return common_inputs @property # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs def _snake_case ( self ): """simple docstring""" if self.task in ["default", "seq2seq-lm"]: lowerCamelCase : int = super().outputs else: lowerCamelCase : int = super(lowerCAmelCase__ , self ).outputs if self.use_past: lowerCamelCase : str = self.num_layers for i in range(lowerCAmelCase__ ): lowerCamelCase : Union[str, Any] = {0: "batch", 2: "past_sequence + sequence"} lowerCamelCase : List[Any] = {0: "batch", 2: "past_sequence + sequence"} return common_outputs def _snake_case ( self , __A , __A = -1 , __A = -1 , __A = False , __A = None , ): """simple docstring""" lowerCamelCase : List[Any] = self._generate_dummy_inputs_for_encoder_and_decoder( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Generate decoder inputs lowerCamelCase : Optional[Any] = seq_length if not self.use_past else 1 lowerCamelCase : Any = self._generate_dummy_inputs_for_encoder_and_decoder( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) lowerCamelCase : Optional[int] = {F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()} lowerCamelCase : Optional[int] = dict(**lowerCAmelCase__ , **lowerCAmelCase__ ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch lowerCamelCase : Tuple = common_inputs["input_ids"].shape lowerCamelCase : List[Any] = common_inputs["decoder_input_ids"].shape[1] lowerCamelCase : List[str] = self.num_attention_heads lowerCamelCase : List[Any] = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) lowerCamelCase : Optional[int] = decoder_seq_length + 3 lowerCamelCase : Optional[int] = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) lowerCamelCase : int = torch.cat( [common_inputs["decoder_attention_mask"], torch.ones(lowerCAmelCase__ , lowerCAmelCase__ )] , dim=1 ) lowerCamelCase : Optional[Any] = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered lowerCamelCase : Union[str, Any] = self.num_layers lowerCamelCase : Tuple = min(lowerCAmelCase__ , lowerCAmelCase__ ) lowerCamelCase : Tuple = max(lowerCAmelCase__ , lowerCAmelCase__ ) - min_num_layers lowerCamelCase : Tuple = "encoder" if num_encoder_layers > num_decoder_layers else "decoder" for _ in range(lowerCAmelCase__ ): common_inputs["past_key_values"].append( ( torch.zeros(lowerCAmelCase__ ), torch.zeros(lowerCAmelCase__ ), torch.zeros(lowerCAmelCase__ ), torch.zeros(lowerCAmelCase__ ), ) ) # TODO: test this. lowerCamelCase : Any = encoder_shape if remaining_side_name == "encoder" else decoder_shape for _ in range(lowerCAmelCase__ , lowerCAmelCase__ ): common_inputs["past_key_values"].append((torch.zeros(lowerCAmelCase__ ), torch.zeros(lowerCAmelCase__ )) ) return common_inputs def _snake_case ( self , __A , __A = -1 , __A = -1 , __A = False , __A = None , ): """simple docstring""" lowerCamelCase : List[str] = self._generate_dummy_inputs_for_encoder_and_decoder( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch lowerCamelCase : int = common_inputs["input_ids"].shape # Not using the same length for past_key_values lowerCamelCase : Optional[Any] = seqlen + 2 lowerCamelCase : List[str] = self.num_layers lowerCamelCase : Optional[int] = self.num_attention_heads lowerCamelCase : Dict = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) lowerCamelCase : Optional[Any] = common_inputs["attention_mask"].dtype lowerCamelCase : Any = torch.cat( [common_inputs["attention_mask"], torch.ones(lowerCAmelCase__ , lowerCAmelCase__ , dtype=lowerCAmelCase__ )] , dim=1 ) lowerCamelCase : Dict = [ (torch.zeros(lowerCAmelCase__ ), torch.zeros(lowerCAmelCase__ )) for _ in range(lowerCAmelCase__ ) ] return common_inputs def _snake_case ( self , __A , __A = -1 , __A = -1 , __A = False , __A = None , ): """simple docstring""" lowerCamelCase : Tuple = compute_effective_axis_dimension( lowerCAmelCase__ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX lowerCamelCase : Optional[Any] = tokenizer.num_special_tokens_to_add(lowerCAmelCase__ ) lowerCamelCase : str = compute_effective_axis_dimension( lowerCAmelCase__ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=lowerCAmelCase__ ) # Generate dummy inputs according to compute batch and sequence lowerCamelCase : Optional[int] = [" ".join([tokenizer.unk_token] ) * seq_length] * batch_size lowerCamelCase : Optional[Any] = dict(tokenizer(lowerCAmelCase__ , return_tensors=lowerCAmelCase__ ) ) return common_inputs def _snake_case ( self , __A , __A = -1 , __A = -1 , __A = False , __A = None , ): """simple docstring""" if self.task in ["default", "seq2seq-lm"]: lowerCamelCase : Tuple = self._generate_dummy_inputs_for_default_and_seqaseq_lm( lowerCAmelCase__ , batch_size=lowerCAmelCase__ , seq_length=lowerCAmelCase__ , is_pair=lowerCAmelCase__ , framework=lowerCAmelCase__ ) else: lowerCamelCase : List[str] = self._generate_dummy_inputs_for_causal_lm( lowerCAmelCase__ , batch_size=lowerCAmelCase__ , seq_length=lowerCAmelCase__ , is_pair=lowerCAmelCase__ , framework=lowerCAmelCase__ ) return common_inputs def _snake_case ( self , __A , __A , __A , __A ): """simple docstring""" if self.task in ["default", "seq2seq-lm"]: lowerCamelCase : List[str] = super()._flatten_past_key_values_(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) else: lowerCamelCase : Optional[Any] = super(lowerCAmelCase__ , self )._flatten_past_key_values_( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) @property def _snake_case ( self ): """simple docstring""" return 1e-4
283
"""simple docstring""" from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig a : Optional[int] = logging.get_logger(__name__) # General docstring a : Union[str, Any] = '''MobileNetV1Config''' # Base docstring a : str = '''google/mobilenet_v1_1.0_224''' a : str = [1, 1024, 7, 7] # Image classification docstring a : Optional[Any] = '''google/mobilenet_v1_1.0_224''' a : Optional[int] = '''tabby, tabby cat''' a : List[str] = [ '''google/mobilenet_v1_1.0_224''', '''google/mobilenet_v1_0.75_192''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def _SCREAMING_SNAKE_CASE ( _lowercase : List[Any] , _lowercase : str , _lowercase : int=None ) ->int: '''simple docstring''' a : List[Any] = {} if isinstance(_lowercase , _lowercase ): a : Union[str, Any] = model.mobilenet_va else: a : List[str] = model a : Dict = "MobilenetV1/Conv2d_0/" a : Tuple = backbone.conv_stem.convolution.weight a : Dict = backbone.conv_stem.normalization.bias a : Optional[Any] = backbone.conv_stem.normalization.weight a : Optional[Any] = backbone.conv_stem.normalization.running_mean a : Tuple = backbone.conv_stem.normalization.running_var for i in range(13 ): a : List[str] = i + 1 a : Dict = i * 2 a : int = backbone.layer[pt_index] a : List[str] = F"""MobilenetV1/Conv2d_{tf_index}_depthwise/""" a : int = pointer.convolution.weight a : Union[str, Any] = pointer.normalization.bias a : Union[str, Any] = pointer.normalization.weight a : Optional[Any] = pointer.normalization.running_mean a : Dict = pointer.normalization.running_var a : List[Any] = backbone.layer[pt_index + 1] a : Union[str, Any] = F"""MobilenetV1/Conv2d_{tf_index}_pointwise/""" a : Dict = pointer.convolution.weight a : Optional[Any] = pointer.normalization.bias a : Dict = pointer.normalization.weight a : Optional[Any] = pointer.normalization.running_mean a : Optional[Any] = pointer.normalization.running_var if isinstance(_lowercase , _lowercase ): a : Dict = "MobilenetV1/Logits/Conv2d_1c_1x1/" a : Tuple = model.classifier.weight a : Optional[int] = model.classifier.bias return tf_to_pt_map def _SCREAMING_SNAKE_CASE ( _lowercase : Any , _lowercase : List[Any] , _lowercase : Tuple ) ->int: '''simple docstring''' try: import numpy as np import tensorflow as tf except ImportError: logger.error( "Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see " "https://www.tensorflow.org/install/ for installation instructions." ) raise # Load weights from TF model a : List[Any] = tf.train.list_variables(_lowercase ) a : Optional[int] = {} for name, shape in init_vars: logger.info(F"""Loading TF weight {name} with shape {shape}""" ) a : Union[str, Any] = tf.train.load_variable(_lowercase , _lowercase ) a : Optional[Any] = array # Build TF to PyTorch weights loading map a : Tuple = _build_tf_to_pytorch_map(_lowercase , _lowercase , _lowercase ) for name, pointer in tf_to_pt_map.items(): logger.info(F"""Importing {name}""" ) if name not in tf_weights: logger.info(F"""{name} not in tf pre-trained weights, skipping""" ) continue a : List[str] = tf_weights[name] if "depthwise_weights" in name: logger.info("Transposing depthwise" ) a : List[Any] = np.transpose(_lowercase , (2, 3, 0, 1) ) elif "weights" in name: logger.info("Transposing" ) if len(pointer.shape ) == 2: # copying into linear layer a : Union[str, Any] = array.squeeze().transpose() else: a : Any = np.transpose(_lowercase , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(F"""Pointer shape {pointer.shape} and array shape {array.shape} mismatched""" ) logger.info(F"""Initialize PyTorch weight {name} {array.shape}""" ) a : str = torch.from_numpy(_lowercase ) tf_weights.pop(_lowercase , _lowercase ) tf_weights.pop(name + "/RMSProp" , _lowercase ) tf_weights.pop(name + "/RMSProp_1" , _lowercase ) tf_weights.pop(name + "/ExponentialMovingAverage" , _lowercase ) logger.info(F"""Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}""" ) return model def _SCREAMING_SNAKE_CASE ( _lowercase : torch.Tensor , _lowercase : nn.Convad ) ->torch.Tensor: '''simple docstring''' a, a : Any = features.shape[-2:] a, a : Dict = conv_layer.stride a, a : int = conv_layer.kernel_size if in_height % stride_height == 0: a : Tuple = max(kernel_height - stride_height , 0 ) else: a : Optional[Any] = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: a : Optional[Any] = max(kernel_width - stride_width , 0 ) else: a : str = max(kernel_width - (in_width % stride_width) , 0 ) a : Any = pad_along_width // 2 a : List[str] = pad_along_width - pad_left a : List[str] = pad_along_height // 2 a : List[Any] = pad_along_height - pad_top a : int = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(_lowercase , _lowercase , "constant" , 0.0 ) class __UpperCamelCase ( nn.Module ): def __init__( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = 1 , lowerCAmelCase__ = 1 , lowerCAmelCase__ = False , lowerCAmelCase__ = True , lowerCAmelCase__ = True , ) -> None: super().__init__() a : str = config if in_channels % groups != 0: raise ValueError(f"""Input channels ({in_channels}) are not divisible by {groups} groups.""" ) if out_channels % groups != 0: raise ValueError(f"""Output channels ({out_channels}) are not divisible by {groups} groups.""" ) a : Optional[int] = 0 if config.tf_padding else int((kernel_size - 1) / 2 ) a : Tuple = nn.Convad( in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=lowerCAmelCase__ , stride=lowerCAmelCase__ , padding=lowerCAmelCase__ , groups=lowerCAmelCase__ , bias=lowerCAmelCase__ , padding_mode="zeros" , ) if use_normalization: a : Optional[int] = nn.BatchNormad( num_features=lowerCAmelCase__ , eps=config.layer_norm_eps , momentum=0.9_997 , affine=lowerCAmelCase__ , track_running_stats=lowerCAmelCase__ , ) else: a : int = None if use_activation: if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): a : Optional[int] = ACTaFN[use_activation] elif isinstance(config.hidden_act , lowerCAmelCase__ ): a : Dict = ACTaFN[config.hidden_act] else: a : Union[str, Any] = config.hidden_act else: a : int = None def __a ( self , lowerCAmelCase__ ) -> torch.Tensor: if self.config.tf_padding: a : Union[str, Any] = apply_tf_padding(lowerCAmelCase__ , self.convolution ) a : List[str] = self.convolution(lowerCAmelCase__ ) if self.normalization is not None: a : int = self.normalization(lowerCAmelCase__ ) if self.activation is not None: a : Dict = self.activation(lowerCAmelCase__ ) return features class __UpperCamelCase ( a__ ): lowerCamelCase : List[str] =MobileNetVaConfig lowerCamelCase : str =load_tf_weights_in_mobilenet_va lowerCamelCase : List[str] ="""mobilenet_v1""" lowerCamelCase : Tuple ="""pixel_values""" lowerCamelCase : Optional[Any] =False def __a ( self , lowerCAmelCase__ ) -> None: if isinstance(lowerCAmelCase__ , (nn.Linear, nn.Convad) ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() elif isinstance(lowerCAmelCase__ , nn.BatchNormad ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) a : Optional[Any] = R''' This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. ''' a : List[str] = R''' Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`MobileNetV1ImageProcessor.__call__`] for details. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. ''' @add_start_docstrings( """The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.""" , a__ , ) class __UpperCamelCase ( a__ ): def __init__( self , lowerCAmelCase__ , lowerCAmelCase__ = True ) -> List[str]: super().__init__(lowerCAmelCase__ ) a : Tuple = config a : Dict = 32 a : Optional[int] = max(int(depth * config.depth_multiplier ) , config.min_depth ) a : Dict = MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=config.num_channels , out_channels=lowerCAmelCase__ , kernel_size=3 , stride=2 , ) a : Dict = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] a : int = nn.ModuleList() for i in range(13 ): a : Optional[Any] = out_channels if strides[i] == 2 or i == 0: depth *= 2 a : List[str] = max(int(depth * config.depth_multiplier ) , config.min_depth ) self.layer.append( MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=3 , stride=strides[i] , groups=lowerCAmelCase__ , ) ) self.layer.append( MobileNetVaConvLayer( lowerCAmelCase__ , in_channels=lowerCAmelCase__ , out_channels=lowerCAmelCase__ , kernel_size=1 , ) ) a : Tuple = nn.AdaptiveAvgPoolad((1, 1) ) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def __a ( self , lowerCAmelCase__ ) -> Optional[Any]: raise NotImplementedError @add_start_docstrings_to_model_forward(lowerCAmelCase__ ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , modality="vision" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def __a ( self , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = None , ) -> Union[tuple, BaseModelOutputWithPoolingAndNoAttention]: a : Dict = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) a : Optional[int] = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError("You have to specify pixel_values" ) a : List[str] = self.conv_stem(lowerCAmelCase__ ) a : Dict = () if output_hidden_states else None for i, layer_module in enumerate(self.layer ): a : List[Any] = layer_module(lowerCAmelCase__ ) if output_hidden_states: a : Optional[Any] = all_hidden_states + (hidden_states,) a : Any = hidden_states if self.pooler is not None: a : Union[str, Any] = torch.flatten(self.pooler(lowerCAmelCase__ ) , start_dim=1 ) else: a : List[Any] = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None ) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=lowerCAmelCase__ , pooler_output=lowerCAmelCase__ , hidden_states=lowerCAmelCase__ , ) @add_start_docstrings( """ MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. """ , a__ , ) class __UpperCamelCase ( a__ ): def __init__( self , lowerCAmelCase__ ) -> None: super().__init__(lowerCAmelCase__ ) a : int = config.num_labels a : List[Any] = MobileNetVaModel(lowerCAmelCase__ ) a : List[str] = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head a : Union[str, Any] = nn.Dropout(config.classifier_dropout_prob , inplace=lowerCAmelCase__ ) a : str = nn.Linear(lowerCAmelCase__ , config.num_labels ) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(lowerCAmelCase__ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=lowerCAmelCase__ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def __a ( self , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = None , ) -> Union[tuple, ImageClassifierOutputWithNoAttention]: a : Optional[Any] = return_dict if return_dict is not None else self.config.use_return_dict a : Any = self.mobilenet_va(lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ , return_dict=lowerCAmelCase__ ) a : Optional[int] = outputs.pooler_output if return_dict else outputs[1] a : Tuple = self.classifier(self.dropout(lowerCAmelCase__ ) ) a : Tuple = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: a : List[Any] = "regression" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): a : Any = "single_label_classification" else: a : int = "multi_label_classification" if self.config.problem_type == "regression": a : Tuple = MSELoss() if self.num_labels == 1: a : Dict = loss_fct(logits.squeeze() , labels.squeeze() ) else: a : str = loss_fct(lowerCAmelCase__ , lowerCAmelCase__ ) elif self.config.problem_type == "single_label_classification": a : List[Any] = CrossEntropyLoss() a : Any = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": a : int = BCEWithLogitsLoss() a : Optional[int] = loss_fct(lowerCAmelCase__ , lowerCAmelCase__ ) if not return_dict: a : Optional[Any] = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=lowerCAmelCase__ , logits=lowerCAmelCase__ , hidden_states=outputs.hidden_states , )
105
0
_lowerCAmelCase : int = { 0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 10: "a", 11: "b", 12: "c", 13: "d", 14: "e", 15: "f", } def UpperCamelCase_( _snake_case : float ): """simple docstring""" assert type(_snake_case ) in (int, float) and decimal == int(_snake_case ) __a =int(_snake_case ) __a ='' __a =False if decimal < 0: __a =True decimal *= -1 while decimal > 0: __a , __a =divmod(_snake_case , 16 ) __a =values[remainder] + hexadecimal __a ='0x' + hexadecimal if negative: __a ='-' + hexadecimal return hexadecimal if __name__ == "__main__": import doctest doctest.testmod()
308
import string # frequency taken from https://en.wikipedia.org/wiki/Letter_frequency _lowerCAmelCase : Tuple = { "E": 12.70, "T": 9.06, "A": 8.17, "O": 7.51, "I": 6.97, "N": 6.75, "S": 6.33, "H": 6.09, "R": 5.99, "D": 4.25, "L": 4.03, "C": 2.78, "U": 2.76, "M": 2.41, "W": 2.36, "F": 2.23, "G": 2.02, "Y": 1.97, "P": 1.93, "B": 1.29, "V": 0.98, "K": 0.77, "J": 0.15, "X": 0.15, "Q": 0.10, "Z": 0.07, } _lowerCAmelCase : Optional[int] = "ETAOINSHRDLCUMWFGYPBVKJXQZ" _lowerCAmelCase : Any = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" def UpperCamelCase_( _snake_case : str ): """simple docstring""" __a ={letter: 0 for letter in string.ascii_uppercase} for letter in message.upper(): if letter in LETTERS: letter_count[letter] += 1 return letter_count def UpperCamelCase_( _snake_case : tuple ): """simple docstring""" return x[0] def UpperCamelCase_( _snake_case : str ): """simple docstring""" __a =get_letter_count(_snake_case ) __a ={ freq: [] for letter, freq in letter_to_freq.items() } for letter in LETTERS: freq_to_letter[letter_to_freq[letter]].append(_snake_case ) __a ={} for freq in freq_to_letter: freq_to_letter[freq].sort(key=ETAOIN.find , reverse=_snake_case ) __a =''.join(freq_to_letter[freq] ) __a =list(freq_to_letter_str.items() ) freq_pairs.sort(key=_snake_case , reverse=_snake_case ) __a =[freq_pair[1] for freq_pair in freq_pairs] return "".join(_snake_case ) def UpperCamelCase_( _snake_case : str ): """simple docstring""" __a =get_frequency_order(_snake_case ) __a =0 for common_letter in ETAOIN[:6]: if common_letter in freq_order[:6]: match_score += 1 for uncommon_letter in ETAOIN[-6:]: if uncommon_letter in freq_order[-6:]: match_score += 1 return match_score if __name__ == "__main__": import doctest doctest.testmod()
308
1
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_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_VISION_2_SEQ_MAPPING if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_VISION_2_SEQ_MAPPING A : Dict = logging.get_logger(__name__) @add_end_docstrings(SCREAMING_SNAKE_CASE__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" def __init__( self : List[str] , *__magic_name__ : Any , **__magic_name__ : List[str] ) -> List[str]: super().__init__(*__magic_name__ , **__magic_name__ ) requires_backends(self , "vision" ) self.check_model_type( TF_MODEL_FOR_VISION_2_SEQ_MAPPING if self.framework == "tf" else MODEL_FOR_VISION_2_SEQ_MAPPING ) def __A ( self : List[str] , __magic_name__ : Tuple=None , __magic_name__ : Dict=None , __magic_name__ : List[str]=None ) -> str: SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = {} if prompt is not None: SCREAMING_SNAKE_CASE_ = prompt if generate_kwargs is not None: SCREAMING_SNAKE_CASE_ = generate_kwargs if max_new_tokens is not None: if "generate_kwargs" not in forward_kwargs: SCREAMING_SNAKE_CASE_ = {} if "max_new_tokens" in forward_kwargs["generate_kwargs"]: raise ValueError( "'max_new_tokens' is defined twice, once in 'generate_kwargs' and once as a direct parameter," " please use only one" ) SCREAMING_SNAKE_CASE_ = max_new_tokens return preprocess_params, forward_kwargs, {} def __call__( self : Dict , __magic_name__ : Union[str, List[str], "Image.Image", List["Image.Image"]] , **__magic_name__ : int ) -> Optional[Any]: return super().__call__(__magic_name__ , **__magic_name__ ) def __A ( self : Tuple , __magic_name__ : Optional[int] , __magic_name__ : Any=None ) -> Tuple: SCREAMING_SNAKE_CASE_ = load_image(__magic_name__ ) if prompt is not None: if not isinstance(__magic_name__ , __magic_name__ ): raise ValueError( F'''Received an invalid text input, got - {type(__magic_name__ )} - but expected a single string. ''' "Note also that one single text can be provided for conditional image to text generation." ) SCREAMING_SNAKE_CASE_ = self.model.config.model_type if model_type == "git": SCREAMING_SNAKE_CASE_ = self.image_processor(images=__magic_name__ , return_tensors=self.framework ) SCREAMING_SNAKE_CASE_ = self.tokenizer(text=__magic_name__ , add_special_tokens=__magic_name__ ).input_ids SCREAMING_SNAKE_CASE_ = [self.tokenizer.cls_token_id] + input_ids SCREAMING_SNAKE_CASE_ = torch.tensor(__magic_name__ ).unsqueeze(0 ) model_inputs.update({"input_ids": input_ids} ) elif model_type == "pix2struct": SCREAMING_SNAKE_CASE_ = self.image_processor(images=__magic_name__ , header_text=__magic_name__ , return_tensors=self.framework ) elif model_type != "vision-encoder-decoder": # vision-encoder-decoder does not support conditional generation SCREAMING_SNAKE_CASE_ = self.image_processor(images=__magic_name__ , return_tensors=self.framework ) SCREAMING_SNAKE_CASE_ = self.tokenizer(__magic_name__ , return_tensors=self.framework ) model_inputs.update(__magic_name__ ) else: raise ValueError(F'''Model type {model_type} does not support conditional text generation''' ) else: SCREAMING_SNAKE_CASE_ = self.image_processor(images=__magic_name__ , return_tensors=self.framework ) if self.model.config.model_type == "git" and prompt is None: SCREAMING_SNAKE_CASE_ = None return model_inputs def __A ( self : Dict , __magic_name__ : Tuple , __magic_name__ : List[str]=None ) -> int: # Git model sets `model_inputs["input_ids"] = None` in `preprocess` (when `prompt=None`). In batch model, the # pipeline will group them into a list of `None`, which fail `_forward`. Avoid this by checking it first. if ( "input_ids" in model_inputs and isinstance(model_inputs["input_ids"] , __magic_name__ ) and all(x is None for x in model_inputs["input_ids"] ) ): SCREAMING_SNAKE_CASE_ = None if generate_kwargs is None: SCREAMING_SNAKE_CASE_ = {} # FIXME: We need to pop here due to a difference in how `generation.py` and `generation.tf_utils.py` # parse inputs. In the Tensorflow version, `generate` raises an error if we don't use `input_ids` whereas # the PyTorch version matches it with `self.model.main_input_name` or `self.model.encoder.main_input_name` # in the `_prepare_model_inputs` method. SCREAMING_SNAKE_CASE_ = model_inputs.pop(self.model.main_input_name ) SCREAMING_SNAKE_CASE_ = self.model.generate(__magic_name__ , **__magic_name__ , **__magic_name__ ) return model_outputs def __A ( self : Optional[int] , __magic_name__ : Any ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = [] for output_ids in model_outputs: SCREAMING_SNAKE_CASE_ = { "generated_text": self.tokenizer.decode( __magic_name__ , skip_special_tokens=__magic_name__ , ) } records.append(__magic_name__ ) return records
118
import json import os import unittest from transformers import DebertaTokenizer, DebertaTokenizerFast from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class lowerCamelCase (SCREAMING_SNAKE_CASE__ , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = DebertaTokenizer lowerCamelCase__ = True lowerCamelCase__ = DebertaTokenizerFast def __A ( self : List[Any] ) -> Dict: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt SCREAMING_SNAKE_CASE_ = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "\u0120", "\u0120l", "\u0120n", "\u0120lo", "\u0120low", "er", "\u0120lowest", "\u0120newer", "\u0120wider", "[UNK]", ] SCREAMING_SNAKE_CASE_ = dict(zip(__magic_name__ , range(len(__magic_name__ ) ) ) ) SCREAMING_SNAKE_CASE_ = ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""] SCREAMING_SNAKE_CASE_ = {"unk_token": "[UNK]"} SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) SCREAMING_SNAKE_CASE_ = 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(__magic_name__ ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(__magic_name__ ) ) def __A ( self : str , **__magic_name__ : int ) -> Union[str, Any]: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__magic_name__ ) def __A ( self : str , __magic_name__ : List[Any] ) -> Dict: SCREAMING_SNAKE_CASE_ = "lower newer" SCREAMING_SNAKE_CASE_ = "lower newer" return input_text, output_text def __A ( self : Union[str, Any] ) -> str: SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = "lower newer" SCREAMING_SNAKE_CASE_ = ["l", "o", "w", "er", "\u0120", "n", "e", "w", "er"] SCREAMING_SNAKE_CASE_ = tokenizer.tokenize(__magic_name__ ) self.assertListEqual(__magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE_ = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__magic_name__ ) , __magic_name__ ) def __A ( self : Optional[int] ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = tokenizer("Hello" , "World" ) SCREAMING_SNAKE_CASE_ = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] self.assertListEqual(tokd["token_type_ids"] , __magic_name__ ) @slow def __A ( self : Any ) -> Any: SCREAMING_SNAKE_CASE_ = self.tokenizer_class.from_pretrained("microsoft/deberta-base" ) SCREAMING_SNAKE_CASE_ = tokenizer.encode("sequence builders" , add_special_tokens=__magic_name__ ) SCREAMING_SNAKE_CASE_ = tokenizer.encode("multi-sequence build" , add_special_tokens=__magic_name__ ) SCREAMING_SNAKE_CASE_ = tokenizer.encode( "sequence builders" , add_special_tokens=__magic_name__ , add_prefix_space=__magic_name__ ) SCREAMING_SNAKE_CASE_ = tokenizer.encode( "sequence builders" , "multi-sequence build" , add_special_tokens=__magic_name__ , add_prefix_space=__magic_name__ ) SCREAMING_SNAKE_CASE_ = tokenizer.build_inputs_with_special_tokens(__magic_name__ ) SCREAMING_SNAKE_CASE_ = tokenizer.build_inputs_with_special_tokens(__magic_name__ , __magic_name__ ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode @slow def __A ( self : Tuple ) -> str: SCREAMING_SNAKE_CASE_ = [self.tokenizer_class] if self.test_rust_tokenizer: tokenizer_classes.append(self.rust_tokenizer_class ) for tokenizer_class in tokenizer_classes: SCREAMING_SNAKE_CASE_ = tokenizer_class.from_pretrained("microsoft/deberta-base" ) SCREAMING_SNAKE_CASE_ = [ "ALBERT: A Lite BERT for Self-supervised Learning of Language Representations", "ALBERT incorporates two parameter reduction techniques", "The first one is a factorized embedding parameterization. By decomposing the large vocabulary" " embedding matrix into two small matrices, we separate the size of the hidden layers from the size of" " vocabulary embedding.", ] SCREAMING_SNAKE_CASE_ = tokenizer(__magic_name__ , padding=__magic_name__ ) SCREAMING_SNAKE_CASE_ = [tokenizer.decode(__magic_name__ , skip_special_tokens=__magic_name__ ) for seq in encoding["input_ids"]] # fmt: off SCREAMING_SNAKE_CASE_ = { "input_ids": [ [1, 2_118, 11_126, 565, 35, 83, 25_191, 163, 18_854, 13, 12_156, 12, 16_101, 25_376, 13_807, 9, 22_205, 27_893, 1_635, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 2_118, 11_126, 565, 24_536, 80, 43_797, 4_878, 7_373, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 133, 78, 65, 16, 10, 3_724, 1_538, 33_183, 11_303, 43_797, 1_938, 4, 870, 24_165, 29_105, 5, 739, 32_644, 33_183, 11_303, 36_173, 88, 80, 650, 7_821, 45_940, 6, 52, 2_559, 5, 1_836, 9, 5, 7_397, 13_171, 31, 5, 1_836, 9, 32_644, 33_183, 11_303, 4, 2] ], "token_type_ids": [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], "attention_mask": [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] } # fmt: on SCREAMING_SNAKE_CASE_ = [ "ALBERT: A Lite BERT for Self-supervised Learning of Language Representations", "ALBERT incorporates two parameter reduction techniques", "The first one is a factorized embedding parameterization. By decomposing the large vocabulary" " embedding matrix into two small matrices, we separate the size of the hidden layers from the size of" " vocabulary embedding.", ] self.assertDictEqual(encoding.data , __magic_name__ ) for expected, decoded in zip(__magic_name__ , __magic_name__ ): self.assertEqual(__magic_name__ , __magic_name__ )
118
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_speech_available, is_torch_available __A = { '''configuration_audio_spectrogram_transformer''': [ '''AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ASTConfig''', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = [ '''AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ASTForAudioClassification''', '''ASTModel''', '''ASTPreTrainedModel''', ] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = ['''ASTFeatureExtractor'''] if TYPE_CHECKING: from .configuration_audio_spectrogram_transformer import ( AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, ASTConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_audio_spectrogram_transformer import ( AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ASTForAudioClassification, ASTModel, ASTPreTrainedModel, ) try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_audio_spectrogram_transformer import ASTFeatureExtractor else: import sys __A = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
64
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging __A = logging.get_logger(__name__) __A = { '''RWKV/rwkv-4-169m-pile''': '''https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json''', '''RWKV/rwkv-4-430m-pile''': '''https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json''', '''RWKV/rwkv-4-1b5-pile''': '''https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json''', '''RWKV/rwkv-4-3b-pile''': '''https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json''', '''RWKV/rwkv-4-7b-pile''': '''https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json''', '''RWKV/rwkv-4-14b-pile''': '''https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json''', '''RWKV/rwkv-raven-1b5''': '''https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json''', '''RWKV/rwkv-raven-3b''': '''https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json''', '''RWKV/rwkv-raven-7b''': '''https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json''', '''RWKV/rwkv-raven-14b''': '''https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json''', } class _snake_case ( a__ ): snake_case__ = "rwkv" snake_case__ = {"max_position_embeddings": "context_length"} def __init__( self : Optional[Any] , UpperCAmelCase : Union[str, Any]=50277 , UpperCAmelCase : Dict=1024 , UpperCAmelCase : int=4096 , UpperCAmelCase : Optional[Any]=32 , UpperCAmelCase : str=None , UpperCAmelCase : List[Any]=None , UpperCAmelCase : str=1E-5 , UpperCAmelCase : Optional[int]=0 , UpperCAmelCase : int=0 , UpperCAmelCase : Tuple=6 , UpperCAmelCase : Optional[Any]=False , UpperCAmelCase : List[Any]=True , **UpperCAmelCase : Any , ): __lowerCamelCase : Optional[Any] = vocab_size __lowerCamelCase : List[Any] = context_length __lowerCamelCase : Optional[int] = hidden_size __lowerCamelCase : Optional[Any] = num_hidden_layers __lowerCamelCase : Tuple = attention_hidden_size if attention_hidden_size is not None else hidden_size __lowerCamelCase : Dict = intermediate_size if intermediate_size is not None else 4 * hidden_size __lowerCamelCase : str = layer_norm_epsilon __lowerCamelCase : Dict = rescale_every __lowerCamelCase : Optional[Any] = use_cache __lowerCamelCase : int = bos_token_id __lowerCamelCase : Tuple = eos_token_id super().__init__( tie_word_embeddings=UpperCAmelCase , bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase , **UpperCAmelCase )
64
1
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() A_ : int = logging.get_logger(__name__) def snake_case_ ( lowerCAmelCase_ )-> Union[str, Any]: '''simple docstring''' _UpperCAmelCase : Union[str, Any] = """huggingface/label-files""" _UpperCAmelCase : Tuple = """imagenet-1k-id2label.json""" _UpperCAmelCase : Optional[Any] = json.load(open(hf_hub_download(lowerCAmelCase_ , lowerCAmelCase_ , repo_type="""dataset""" ) , """r""" ) ) _UpperCAmelCase : List[str] = {int(lowerCAmelCase_ ): v for k, v in idalabel.items()} _UpperCAmelCase : List[Any] = {v: k for k, v in idalabel.items()} _UpperCAmelCase : Any = """std_conv""" if """bit""" in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" _UpperCAmelCase : List[str] = BitConfig( conv_layer=lowerCAmelCase_ , num_labels=1000 , idalabel=lowerCAmelCase_ , labelaid=lowerCAmelCase_ , ) return config def snake_case_ ( lowerCAmelCase_ )-> str: '''simple docstring''' if "stem.conv" in name: _UpperCAmelCase : Dict = name.replace("""stem.conv""" , """bit.embedder.convolution""" ) if "blocks" in name: _UpperCAmelCase : int = name.replace("""blocks""" , """layers""" ) if "head.fc" in name: _UpperCAmelCase : Optional[Any] = name.replace("""head.fc""" , """classifier.1""" ) if name.startswith("""norm""" ): _UpperCAmelCase : Optional[int] = """bit.""" + name if "bit" not in name and "classifier" not in name: _UpperCAmelCase : int = """bit.encoder.""" + name return name def snake_case_ ( )-> Union[str, Any]: '''simple docstring''' _UpperCAmelCase : Optional[int] = """http://images.cocodataset.org/val2017/000000039769.jpg""" _UpperCAmelCase : int = Image.open(requests.get(lowerCAmelCase_ , stream=lowerCAmelCase_ ).raw ) return im @torch.no_grad() def snake_case_ ( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_=False )-> str: '''simple docstring''' _UpperCAmelCase : Union[str, Any] = get_config(lowerCAmelCase_ ) # load original model from timm _UpperCAmelCase : Dict = create_model(lowerCAmelCase_ , pretrained=lowerCAmelCase_ ) timm_model.eval() # load state_dict of original model _UpperCAmelCase : List[str] = timm_model.state_dict() for key in state_dict.copy().keys(): _UpperCAmelCase : str = state_dict.pop(lowerCAmelCase_ ) _UpperCAmelCase : Optional[int] = val.squeeze() if """head""" in key else val # load HuggingFace model _UpperCAmelCase : List[Any] = BitForImageClassification(lowerCAmelCase_ ) model.eval() model.load_state_dict(lowerCAmelCase_ ) # create image processor _UpperCAmelCase : Optional[int] = create_transform(**resolve_data_config({} , model=lowerCAmelCase_ ) ) _UpperCAmelCase : Tuple = transform.transforms _UpperCAmelCase : Tuple = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } _UpperCAmelCase : List[Any] = BitImageProcessor( do_resize=lowerCAmelCase_ , size={"""shortest_edge""": timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=lowerCAmelCase_ , crop_size={"""height""": timm_transforms[1].size[0], """width""": timm_transforms[1].size[1]} , do_normalize=lowerCAmelCase_ , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) _UpperCAmelCase : List[Any] = prepare_img() _UpperCAmelCase : Any = transform(lowerCAmelCase_ ).unsqueeze(0 ) _UpperCAmelCase : str = processor(lowerCAmelCase_ , return_tensors="""pt""" ).pixel_values # verify pixel values assert torch.allclose(lowerCAmelCase_ , lowerCAmelCase_ ) # verify logits with torch.no_grad(): _UpperCAmelCase : Optional[Any] = model(lowerCAmelCase_ ) _UpperCAmelCase : List[Any] = outputs.logits print("""Logits:""" , logits[0, :3] ) print("""Predicted class:""" , model.config.idalabel[logits.argmax(-1 ).item()] ) _UpperCAmelCase : Tuple = timm_model(lowerCAmelCase_ ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(lowerCAmelCase_ , outputs.logits , atol=1e-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(lowerCAmelCase_ ).mkdir(exist_ok=lowerCAmelCase_ ) print(F'''Saving model {model_name} and processor to {pytorch_dump_folder_path}''' ) model.save_pretrained(lowerCAmelCase_ ) processor.save_pretrained(lowerCAmelCase_ ) if push_to_hub: print(F'''Pushing model {model_name} and processor to the hub''' ) model.push_to_hub(F'''ybelkada/{model_name}''' ) processor.push_to_hub(F'''ybelkada/{model_name}''' ) if __name__ == "__main__": A_ : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""resnetv2_50x1_bitm""", type=str, help="""Name of the BiT timm model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to push the model to the hub.""", ) A_ : Tuple = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
215
'''simple docstring''' from torch import nn class lowercase ( nn.Module ): """simple docstring""" def __init__( self ,a_ ,a_ ) -> List[Any]: super().__init__() _UpperCAmelCase : Dict = class_size _UpperCAmelCase : Union[str, Any] = embed_size # self.mlp1 = nn.Linear(embed_size, embed_size) # self.mlp2 = (nn.Linear(embed_size, class_size)) _UpperCAmelCase : List[Any] = nn.Linear(a_ ,a_ ) def _snake_case ( self ,a_ ) -> Tuple: # hidden_state = nn.functional.relu(self.mlp1(hidden_state)) # hidden_state = self.mlp2(hidden_state) _UpperCAmelCase : Optional[int] = self.mlp(a_ ) return logits
215
1
import inspect import unittest import numpy as np from transformers import ViTConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor if is_flax_available(): import jax from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel class _UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __init__( self : Optional[int] , lowercase_ : Optional[int] , lowercase_ : int=13 , lowercase_ : List[Any]=30 , lowercase_ : Union[str, Any]=2 , lowercase_ : Any=3 , lowercase_ : Dict=True , lowercase_ : Tuple=True , lowercase_ : Union[str, Any]=32 , lowercase_ : Optional[int]=5 , lowercase_ : Dict=4 , lowercase_ : Union[str, Any]=37 , lowercase_ : List[Any]="gelu" , lowercase_ : Optional[Any]=0.1 , lowercase_ : Optional[int]=0.1 , lowercase_ : List[str]=10 , lowercase_ : Union[str, Any]=0.02 , ) -> Optional[Any]: """simple docstring""" _UpperCamelCase = parent _UpperCamelCase = batch_size _UpperCamelCase = image_size _UpperCamelCase = patch_size _UpperCamelCase = num_channels _UpperCamelCase = is_training _UpperCamelCase = use_labels _UpperCamelCase = hidden_size _UpperCamelCase = num_hidden_layers _UpperCamelCase = num_attention_heads _UpperCamelCase = intermediate_size _UpperCamelCase = hidden_act _UpperCamelCase = hidden_dropout_prob _UpperCamelCase = attention_probs_dropout_prob _UpperCamelCase = type_sequence_label_size _UpperCamelCase = initializer_range # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) _UpperCamelCase = (image_size // patch_size) ** 2 _UpperCamelCase = num_patches + 1 def __UpperCAmelCase ( self : Optional[Any]) -> Optional[int]: """simple docstring""" _UpperCamelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) _UpperCamelCase = ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=lowercase_ , initializer_range=self.initializer_range , ) return config, pixel_values def __UpperCAmelCase ( self : List[str] , lowercase_ : Union[str, Any] , lowercase_ : Optional[Any]) -> Optional[int]: """simple docstring""" _UpperCamelCase = FlaxViTModel(config=lowercase_) _UpperCamelCase = model(lowercase_) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) _UpperCamelCase = (self.image_size, self.image_size) _UpperCamelCase = (self.patch_size, self.patch_size) _UpperCamelCase = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, num_patches + 1, self.hidden_size)) def __UpperCAmelCase ( self : Tuple , lowercase_ : List[str] , lowercase_ : Union[str, Any]) -> Dict: """simple docstring""" _UpperCamelCase = self.type_sequence_label_size _UpperCamelCase = FlaxViTForImageClassification(config=lowercase_) _UpperCamelCase = model(lowercase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) # test greyscale images _UpperCamelCase = 1 _UpperCamelCase = FlaxViTForImageClassification(lowercase_) _UpperCamelCase = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) _UpperCamelCase = model(lowercase_) def __UpperCAmelCase ( self : Tuple) -> List[str]: """simple docstring""" _UpperCamelCase = self.prepare_config_and_inputs() ( _UpperCamelCase ) = config_and_inputs _UpperCamelCase = {'pixel_values': pixel_values} return config, inputs_dict @require_flax class _UpperCAmelCase ( _a, unittest.TestCase ): '''simple docstring''' __A = (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else () def __UpperCAmelCase ( self : Dict) -> None: """simple docstring""" _UpperCamelCase = FlaxViTModelTester(self) _UpperCamelCase = ConfigTester(self , config_class=lowercase_ , has_text_modality=lowercase_ , hidden_size=37) def __UpperCAmelCase ( self : Optional[Any]) -> Optional[int]: """simple docstring""" self.config_tester.run_common_tests() def __UpperCAmelCase ( self : Optional[Any]) -> Optional[Any]: """simple docstring""" _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowercase_) def __UpperCAmelCase ( self : Optional[int]) -> Optional[int]: """simple docstring""" _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowercase_) def __UpperCAmelCase ( self : Union[str, Any]) -> Union[str, Any]: """simple docstring""" _UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCamelCase = model_class(lowercase_) _UpperCamelCase = inspect.signature(model.__call__) # signature.parameters is an OrderedDict => so arg_names order is deterministic _UpperCamelCase = [*signature.parameters.keys()] _UpperCamelCase = ['pixel_values'] self.assertListEqual(arg_names[:1] , lowercase_) def __UpperCAmelCase ( self : List[Any]) -> Optional[Any]: """simple docstring""" _UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__): _UpperCamelCase = self._prepare_for_class(lowercase_ , lowercase_) _UpperCamelCase = model_class(lowercase_) @jax.jit def model_jitted(lowercase_ : str , **lowercase_ : List[Any]): return model(pixel_values=lowercase_ , **lowercase_) with self.subTest("JIT Enabled"): _UpperCamelCase = model_jitted(**lowercase_).to_tuple() with self.subTest("JIT Disabled"): with jax.disable_jit(): _UpperCamelCase = model_jitted(**lowercase_).to_tuple() self.assertEqual(len(lowercase_) , len(lowercase_)) for jitted_output, output in zip(lowercase_ , lowercase_): self.assertEqual(jitted_output.shape , output.shape) @slow def __UpperCAmelCase ( self : Optional[int]) -> Optional[Any]: """simple docstring""" for model_class_name in self.all_model_classes: _UpperCamelCase = model_class_name.from_pretrained("google/vit-base-patch16-224") _UpperCamelCase = model(np.ones((1, 3, 224, 224))) self.assertIsNotNone(lowercase_)
367
import os def lowerCAmelCase__ ( ) ->Any: '''simple docstring''' with open(os.path.dirname(a__ ) + "/grid.txt" ) as f: _UpperCamelCase = [] # noqa: E741 for _ in range(20 ): l.append([int(a__ ) for x in f.readline().split()] ) _UpperCamelCase = 0 # right for i in range(20 ): for j in range(17 ): _UpperCamelCase = l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3] if temp > maximum: _UpperCamelCase = temp # down for i in range(17 ): for j in range(20 ): _UpperCamelCase = l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j] if temp > maximum: _UpperCamelCase = temp # diagonal 1 for i in range(17 ): for j in range(17 ): _UpperCamelCase = l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3] if temp > maximum: _UpperCamelCase = temp # diagonal 2 for i in range(17 ): for j in range(3 , 20 ): _UpperCamelCase = l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3] if temp > maximum: _UpperCamelCase = temp return maximum if __name__ == "__main__": print(solution())
63
0
"""simple docstring""" # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import json import os from ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES from ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType from ...utils.imports import is_botoa_available from .config_args import SageMakerConfig from .config_utils import ( DYNAMO_BACKENDS, _ask_field, _ask_options, _convert_dynamo_backend, _convert_mixed_precision, _convert_sagemaker_distributed_mode, _convert_yes_no_to_bool, ) if is_botoa_available(): import botoa # noqa: F401 def lowerCamelCase__ ( _lowerCamelCase : int ) -> Any: lowerCamelCase_ = botoa.client('iam' ) lowerCamelCase_ = { 'Version': '2012-10-17', 'Statement': [ {'Effect': 'Allow', 'Principal': {'Service': 'sagemaker.amazonaws.com'}, 'Action': 'sts:AssumeRole'} ], } try: # create the role, associated with the chosen trust policy iam_client.create_role( RoleName=__UpperCamelCase , AssumeRolePolicyDocument=json.dumps(__UpperCamelCase , indent=2 ) ) lowerCamelCase_ = { 'Version': '2012-10-17', 'Statement': [ { 'Effect': 'Allow', 'Action': [ 'sagemaker:*', 'ecr:GetDownloadUrlForLayer', 'ecr:BatchGetImage', 'ecr:BatchCheckLayerAvailability', 'ecr:GetAuthorizationToken', 'cloudwatch:PutMetricData', 'cloudwatch:GetMetricData', 'cloudwatch:GetMetricStatistics', 'cloudwatch:ListMetrics', 'logs:CreateLogGroup', 'logs:CreateLogStream', 'logs:DescribeLogStreams', 'logs:PutLogEvents', 'logs:GetLogEvents', 's3:CreateBucket', 's3:ListBucket', 's3:GetBucketLocation', 's3:GetObject', 's3:PutObject', ], 'Resource': '*', } ], } # attach policy to role iam_client.put_role_policy( RoleName=__UpperCamelCase , PolicyName=F'''{role_name}_policy_permission''' , PolicyDocument=json.dumps(__UpperCamelCase , indent=2 ) , ) except iam_client.exceptions.EntityAlreadyExistsException: print(F'''role {role_name} already exists. Using existing one''' ) def lowerCamelCase__ ( _lowerCamelCase : Dict ) -> List[str]: lowerCamelCase_ = botoa.client('iam' ) return iam_client.get_role(RoleName=__UpperCamelCase )["Role"]["Arn"] def lowerCamelCase__ ( ) -> Any: lowerCamelCase_ = _ask_options( 'How do you want to authorize?' , ['AWS Profile', 'Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) '] , __UpperCamelCase , ) lowerCamelCase_ = None if credentials_configuration == 0: lowerCamelCase_ = _ask_field('Enter your AWS Profile name: [default] ' , default='default' ) lowerCamelCase_ = aws_profile else: print( 'Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,' '`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`' ) lowerCamelCase_ = _ask_field('AWS Access Key ID: ' ) lowerCamelCase_ = aws_access_key_id lowerCamelCase_ = _ask_field('AWS Secret Access Key: ' ) lowerCamelCase_ = aws_secret_access_key lowerCamelCase_ = _ask_field('Enter your AWS Region: [us-east-1]' , default='us-east-1' ) lowerCamelCase_ = aws_region lowerCamelCase_ = _ask_options( 'Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?' , ['Provide IAM Role name', 'Create new IAM role using credentials'] , __UpperCamelCase , ) if role_management == 0: lowerCamelCase_ = _ask_field('Enter your IAM role name: ' ) else: lowerCamelCase_ = 'accelerate_sagemaker_execution_role' print(F'''Accelerate will create an iam role "{iam_role_name}" using the provided credentials''' ) _create_iam_role_for_sagemaker(__UpperCamelCase ) lowerCamelCase_ = _ask_field( 'Do you want to use custom Docker image? [yes/NO]: ' , _convert_yes_no_to_bool , default=__UpperCamelCase , error_message='Please enter yes or no.' , ) lowerCamelCase_ = None if is_custom_docker_image: lowerCamelCase_ = _ask_field('Enter your Docker image: ' , lambda _lowerCamelCase : str(__UpperCamelCase ).lower() ) lowerCamelCase_ = _ask_field( 'Do you want to provide SageMaker input channels with data locations? [yes/NO]: ' , _convert_yes_no_to_bool , default=__UpperCamelCase , error_message='Please enter yes or no.' , ) lowerCamelCase_ = None if is_sagemaker_inputs_enabled: lowerCamelCase_ = _ask_field( 'Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): ' , lambda _lowerCamelCase : str(__UpperCamelCase ).lower() , ) lowerCamelCase_ = _ask_field( 'Do you want to enable SageMaker metrics? [yes/NO]: ' , _convert_yes_no_to_bool , default=__UpperCamelCase , error_message='Please enter yes or no.' , ) lowerCamelCase_ = None if is_sagemaker_metrics_enabled: lowerCamelCase_ = _ask_field( 'Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): ' , lambda _lowerCamelCase : str(__UpperCamelCase ).lower() , ) lowerCamelCase_ = _ask_options( 'What is the distributed mode?' , ['No distributed training', 'Data parallelism'] , _convert_sagemaker_distributed_mode , ) lowerCamelCase_ = {} lowerCamelCase_ = _ask_field( 'Do you wish to optimize your script with torch dynamo?[yes/NO]:' , _convert_yes_no_to_bool , default=__UpperCamelCase , error_message='Please enter yes or no.' , ) if use_dynamo: lowerCamelCase_ = 'dynamo_' lowerCamelCase_ = _ask_options( 'Which dynamo backend would you like to use?' , [x.lower() for x in DYNAMO_BACKENDS] , _convert_dynamo_backend , default=2 , ) lowerCamelCase_ = _ask_field( 'Do you want to customize the defaults sent to torch.compile? [yes/NO]: ' , _convert_yes_no_to_bool , default=__UpperCamelCase , error_message='Please enter yes or no.' , ) if use_custom_options: lowerCamelCase_ = _ask_options( 'Which mode do you want to use?' , __UpperCamelCase , lambda _lowerCamelCase : TORCH_DYNAMO_MODES[int(__UpperCamelCase )] , default='default' , ) lowerCamelCase_ = _ask_field( 'Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: ' , _convert_yes_no_to_bool , default=__UpperCamelCase , error_message='Please enter yes or no.' , ) lowerCamelCase_ = _ask_field( 'Do you want to enable dynamic shape tracing? [yes/NO]: ' , _convert_yes_no_to_bool , default=__UpperCamelCase , error_message='Please enter yes or no.' , ) lowerCamelCase_ = 'Which EC2 instance type you want to use for your training?' if distributed_type != SageMakerDistributedType.NO: lowerCamelCase_ = _ask_options( __UpperCamelCase , __UpperCamelCase , lambda _lowerCamelCase : SAGEMAKER_PARALLEL_EC2_INSTANCES[int(__UpperCamelCase )] ) else: eca_instance_query += "? [ml.p3.2xlarge]:" lowerCamelCase_ = _ask_field(__UpperCamelCase , lambda _lowerCamelCase : str(__UpperCamelCase ).lower() , default='ml.p3.2xlarge' ) lowerCamelCase_ = 1 if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL): lowerCamelCase_ = _ask_field( 'How many machines do you want use? [1]: ' , __UpperCamelCase , default=1 , ) lowerCamelCase_ = _ask_options( 'Do you wish to use FP16 or BF16 (mixed precision)?' , ['no', 'fp16', 'bf16', 'fp8'] , _convert_mixed_precision , ) if use_dynamo and mixed_precision == "no": print( 'Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts.' ) return SageMakerConfig( image_uri=__UpperCamelCase , compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER , distributed_type=__UpperCamelCase , use_cpu=__UpperCamelCase , dynamo_config=__UpperCamelCase , eca_instance_type=__UpperCamelCase , profile=__UpperCamelCase , region=__UpperCamelCase , iam_role_name=__UpperCamelCase , mixed_precision=__UpperCamelCase , num_machines=__UpperCamelCase , sagemaker_inputs_file=__UpperCamelCase , sagemaker_metrics_file=__UpperCamelCase , )
183
from itertools import product from cva import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey from numpy import dot, exp, mgrid, pi, ravel, square, uinta, zeros def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = k_size // 2 SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = mgrid[0 - center : k_size - center, 0 - center : k_size - center] SCREAMING_SNAKE_CASE_ = 1 / (2 * pi * sigma) * exp(-(square(__UpperCamelCase ) + square(__UpperCamelCase )) / (2 * square(__UpperCamelCase )) ) return g def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = image.shape[0], image.shape[1] # dst image height and width SCREAMING_SNAKE_CASE_ = height - k_size + 1 SCREAMING_SNAKE_CASE_ = width - k_size + 1 # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows SCREAMING_SNAKE_CASE_ = zeros((dst_height * dst_width, k_size * k_size) ) SCREAMING_SNAKE_CASE_ = 0 for i, j in product(range(__UpperCamelCase ) , range(__UpperCamelCase ) ): SCREAMING_SNAKE_CASE_ = ravel(image[i : i + k_size, j : j + k_size] ) SCREAMING_SNAKE_CASE_ = window row += 1 # turn the kernel into shape(k*k, 1) SCREAMING_SNAKE_CASE_ = gen_gaussian_kernel(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = ravel(__UpperCamelCase ) # reshape and get the dst image SCREAMING_SNAKE_CASE_ = dot(__UpperCamelCase , __UpperCamelCase ).reshape(__UpperCamelCase , __UpperCamelCase ).astype(__UpperCamelCase ) return dst if __name__ == "__main__": # read original image A : Tuple = imread(r"../image_data/lena.jpg") # turn image in gray scale value A : Optional[int] = cvtColor(img, COLOR_BGR2GRAY) # get values with two different mask size A : Tuple = gaussian_filter(gray, 3, sigma=1) A : Optional[int] = gaussian_filter(gray, 5, sigma=0.8) # show result images imshow("gaussian filter with 3x3 mask", gaussianaxa) imshow("gaussian filter with 5x5 mask", gaussianaxa) waitKey()
118
0
"""simple docstring""" import os import random import sys from . import cryptomath_module as cryptoMath # noqa: N812 from . import rabin_miller as rabinMiller # noqa: N812 def UpperCAmelCase ( ) -> Optional[int]: print('Making key files...' ) make_key_files('rsa' , 1024 ) print('Key files generation successful.' ) def UpperCAmelCase ( UpperCAmelCase ) -> Tuple: print('Generating prime p...' ) snake_case_ = rabinMiller.generate_large_prime(lowerCamelCase_ ) print('Generating prime q...' ) snake_case_ = rabinMiller.generate_large_prime(lowerCamelCase_ ) snake_case_ = p * q print('Generating e that is relatively prime to (p - 1) * (q - 1)...' ) while True: snake_case_ = random.randrange(2 ** (key_size - 1) , 2 ** (key_size) ) if cryptoMath.gcd(lowerCamelCase_ , (p - 1) * (q - 1) ) == 1: break print('Calculating d that is mod inverse of e...' ) snake_case_ = cryptoMath.find_mod_inverse(lowerCamelCase_ , (p - 1) * (q - 1) ) snake_case_ = (n, e) snake_case_ = (n, d) return (public_key, private_key) def UpperCAmelCase ( UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: if os.path.exists(f'{name}_pubkey.txt' ) or os.path.exists(f'{name}_privkey.txt' ): print('\nWARNING:' ) print( f'\"{name}_pubkey.txt\" or \"{name}_privkey.txt\" already exists. \n' 'Use a different name or delete these files and re-run this program.' ) sys.exit() snake_case_ = generate_key(lowerCamelCase_ ) print(f'\nWriting public key to file {name}_pubkey.txt...' ) with open(f'{name}_pubkey.txt' , 'w' ) as out_file: out_file.write(f'{key_size},{public_key[0]},{public_key[1]}' ) print(f'Writing private key to file {name}_privkey.txt...' ) with open(f'{name}_privkey.txt' , 'w' ) as out_file: out_file.write(f'{key_size},{private_key[0]},{private_key[1]}' ) if __name__ == "__main__": main()
356
"""simple docstring""" import functools def UpperCAmelCase ( UpperCAmelCase , UpperCAmelCase ) -> int: # Validation if not isinstance(UpperCAmelCase , UpperCAmelCase ) or not all(isinstance(UpperCAmelCase , UpperCAmelCase ) for day in days ): raise ValueError('The parameter days should be a list of integers' ) if len(UpperCAmelCase ) != 3 or not all(isinstance(UpperCAmelCase , UpperCAmelCase ) for cost in costs ): raise ValueError('The parameter costs should be a list of three integers' ) if len(UpperCAmelCase ) == 0: return 0 if min(UpperCAmelCase ) <= 0: raise ValueError('All days elements should be greater than 0' ) if max(UpperCAmelCase ) >= 366: raise ValueError('All days elements should be less than 366' ) snake_case_ = set(UpperCAmelCase ) @functools.cache def dynamic_programming(UpperCAmelCase ) -> int: if index > 365: return 0 if index not in days_set: return dynamic_programming(index + 1 ) return min( costs[0] + dynamic_programming(index + 1 ) , costs[1] + dynamic_programming(index + 7 ) , costs[2] + dynamic_programming(index + 30 ) , ) return dynamic_programming(1 ) if __name__ == "__main__": import doctest doctest.testmod()
312
0
from __future__ import annotations import unittest import numpy as np from transformers import OPTConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import GPTaTokenizer, TFOPTForCausalLM, TFOPTModel def __SCREAMING_SNAKE_CASE ( __UpperCamelCase : Union[str, Any] , __UpperCamelCase : Optional[int] , __UpperCamelCase : List[Any]=None , __UpperCamelCase : Tuple=None ) -> Dict: """simple docstring""" if attention_mask is None: SCREAMING_SNAKE_CASE__ = tf.cast(tf.math.not_equal(__UpperCamelCase , config.pad_token_id ) , tf.inta ) return {"input_ids": input_ids, "attention_mask": attention_mask} @require_tf class __snake_case : lowerCAmelCase_ = OPTConfig lowerCAmelCase_ = {} lowerCAmelCase_ = "gelu" def __init__( self : int , _lowercase : Union[str, Any] , _lowercase : Tuple=13 , _lowercase : List[str]=7 , _lowercase : Any=True , _lowercase : str=False , _lowercase : Union[str, Any]=99 , _lowercase : Optional[Any]=16 , _lowercase : Union[str, Any]=2 , _lowercase : Union[str, Any]=4 , _lowercase : Optional[Any]=4 , _lowercase : Optional[int]="gelu" , _lowercase : int=0.1 , _lowercase : str=0.1 , _lowercase : Optional[Any]=20 , _lowercase : Dict=2 , _lowercase : Optional[Any]=1 , _lowercase : Union[str, Any]=0 , _lowercase : Union[str, Any]=16 , _lowercase : Any=16 , ): """simple docstring""" SCREAMING_SNAKE_CASE__ = parent SCREAMING_SNAKE_CASE__ = batch_size SCREAMING_SNAKE_CASE__ = seq_length SCREAMING_SNAKE_CASE__ = is_training SCREAMING_SNAKE_CASE__ = use_labels SCREAMING_SNAKE_CASE__ = vocab_size SCREAMING_SNAKE_CASE__ = hidden_size SCREAMING_SNAKE_CASE__ = num_hidden_layers SCREAMING_SNAKE_CASE__ = num_attention_heads SCREAMING_SNAKE_CASE__ = intermediate_size SCREAMING_SNAKE_CASE__ = hidden_act SCREAMING_SNAKE_CASE__ = hidden_dropout_prob SCREAMING_SNAKE_CASE__ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE__ = max_position_embeddings SCREAMING_SNAKE_CASE__ = eos_token_id SCREAMING_SNAKE_CASE__ = pad_token_id SCREAMING_SNAKE_CASE__ = bos_token_id SCREAMING_SNAKE_CASE__ = embed_dim SCREAMING_SNAKE_CASE__ = word_embed_proj_dim SCREAMING_SNAKE_CASE__ = False def __a ( self : Optional[int] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) SCREAMING_SNAKE_CASE__ = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) SCREAMING_SNAKE_CASE__ = tf.concat([input_ids, eos_tensor] , axis=1 ) SCREAMING_SNAKE_CASE__ = self.config_cls( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , embed_dim=self.embed_dim , word_embed_proj_dim=self.word_embed_proj_dim , is_encoder_decoder=_lowercase , **self.config_updates , ) SCREAMING_SNAKE_CASE__ = prepare_opt_inputs_dict(_lowercase , _lowercase ) return config, inputs_dict def __a ( self : List[Any] , _lowercase : Dict , _lowercase : int ): """simple docstring""" SCREAMING_SNAKE_CASE__ = TFOPTModel(config=_lowercase ) SCREAMING_SNAKE_CASE__ = inputs_dict["""input_ids"""] SCREAMING_SNAKE_CASE__ = input_ids[:1, :] SCREAMING_SNAKE_CASE__ = inputs_dict["""attention_mask"""][:1, :] SCREAMING_SNAKE_CASE__ = 1 # first forward pass SCREAMING_SNAKE_CASE__ = model(_lowercase , attention_mask=_lowercase , use_cache=_lowercase ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids SCREAMING_SNAKE_CASE__ = ids_tensor((self.batch_size, 3) , config.vocab_size ) SCREAMING_SNAKE_CASE__ = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and SCREAMING_SNAKE_CASE__ = tf.concat([input_ids, next_tokens] , axis=-1 ) SCREAMING_SNAKE_CASE__ = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) SCREAMING_SNAKE_CASE__ = model(_lowercase , attention_mask=_lowercase )[0] SCREAMING_SNAKE_CASE__ = model(_lowercase , attention_mask=_lowercase , past_key_values=_lowercase )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice SCREAMING_SNAKE_CASE__ = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) SCREAMING_SNAKE_CASE__ = output_from_no_past[:, -3:, random_slice_idx] SCREAMING_SNAKE_CASE__ = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(_lowercase , _lowercase , rtol=1E-3 ) @require_tf class __snake_case ( lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): lowerCAmelCase_ = (TFOPTModel, TFOPTForCausalLM) if is_tf_available() else () lowerCAmelCase_ = (TFOPTForCausalLM,) if is_tf_available() else () lowerCAmelCase_ = ( {"feature-extraction": TFOPTModel, "text-generation": TFOPTForCausalLM} if is_tf_available() else {} ) lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = 10 def __a ( self : Union[str, Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = TFOPTModelTester(self ) SCREAMING_SNAKE_CASE__ = ConfigTester(self , config_class=_lowercase ) def __a ( self : Union[str, Any] ): """simple docstring""" self.config_tester.run_common_tests() def __a ( self : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_lowercase ) def __a ( self : List[str] ): """simple docstring""" SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs_for_common() def _get_word_embedding_weight(_lowercase : int , _lowercase : Union[str, Any] ): if hasattr(_lowercase , """weight""" ): return embedding_layer.weight else: # Here we build the word embeddings weights if not exists. # And then we retry to get the attribute once built. model.build() if hasattr(_lowercase , """weight""" ): return embedding_layer.weight else: return None for model_class in self.all_model_classes: for size in [config.vocab_size - 10, config.vocab_size + 10]: # build the embeddings SCREAMING_SNAKE_CASE__ = model_class(config=_lowercase ) SCREAMING_SNAKE_CASE__ = _get_word_embedding_weight(_lowercase , model.get_input_embeddings() ) SCREAMING_SNAKE_CASE__ = _get_word_embedding_weight(_lowercase , model.get_output_embeddings() ) # reshape the embeddings model.resize_token_embeddings(_lowercase ) SCREAMING_SNAKE_CASE__ = _get_word_embedding_weight(_lowercase , model.get_input_embeddings() ) SCREAMING_SNAKE_CASE__ = _get_word_embedding_weight(_lowercase , model.get_output_embeddings() ) # check that the resized embeddings size matches the desired size. SCREAMING_SNAKE_CASE__ = size if size is not None else config.vocab_size self.assertEqual(new_input_embeddings.shape[0] , _lowercase ) # check that weights remain the same after resizing SCREAMING_SNAKE_CASE__ = True for pa, pa in zip(old_input_embeddings.value() , new_input_embeddings.value() ): if tf.math.reduce_sum(tf.math.abs(pa - pa ) ) > 0: SCREAMING_SNAKE_CASE__ = False self.assertTrue(_lowercase ) if old_output_embeddings is not None and new_output_embeddings is not None: self.assertEqual(new_output_embeddings.shape[0] , _lowercase ) SCREAMING_SNAKE_CASE__ = True for pa, pa in zip(old_output_embeddings.value() , new_output_embeddings.value() ): if tf.math.reduce_sum(tf.math.abs(pa - pa ) ) > 0: SCREAMING_SNAKE_CASE__ = False self.assertTrue(_lowercase ) def __SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> Optional[Any]: """simple docstring""" return tf.constant(__UpperCamelCase , dtype=tf.intaa ) @require_tf class __snake_case ( unittest.TestCase ): lowerCAmelCase_ = 99 def __a ( self : Dict ): """simple docstring""" SCREAMING_SNAKE_CASE__ = tf.ones((4, 1) , dtype=tf.intaa ) * 2 SCREAMING_SNAKE_CASE__ = tf.concat([ids_tensor((4, 6) , self.vocab_size - 3 ) + 3, eos_column_vector] , axis=1 ) SCREAMING_SNAKE_CASE__ = input_ids.shape[0] SCREAMING_SNAKE_CASE__ = OPTConfig( vocab_size=self.vocab_size , hidden_size=24 , num_hidden_layers=2 , num_attention_heads=2 , ffn_dim=32 , max_position_embeddings=48 , eos_token_id=2 , pad_token_id=1 , bos_token_id=0 , ) return config, input_ids, batch_size @require_sentencepiece @require_tf class __snake_case ( unittest.TestCase ): @slow def __a ( self : Optional[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = TFOPTModel.from_pretrained("""facebook/opt-350m""" ) SCREAMING_SNAKE_CASE__ = _long_tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]] ) SCREAMING_SNAKE_CASE__ = tf.not_equal(_lowercase , model.config.pad_token_id ) with tf.GradientTape(): SCREAMING_SNAKE_CASE__ = model(input_ids=_lowercase , attention_mask=_lowercase ).last_hidden_state SCREAMING_SNAKE_CASE__ = (1, 11, 5_12) self.assertEqual(output.shape , _lowercase ) SCREAMING_SNAKE_CASE__ = tf.constant( [[-0.28_73, -1.92_18, -0.30_33], [-1.27_10, -0.13_38, -0.19_02], [0.40_95, 0.12_14, -1.31_21]] ) self.assertTrue(np.allclose(output[:, :3, :3] , _lowercase , atol=4E-3 ) ) SCREAMING_SNAKE_CASE__ = tf.function(_lowercase , jit_compile=_lowercase ) SCREAMING_SNAKE_CASE__ = xla_generate(_lowercase , _lowercase )[0] self.assertTrue(np.allclose(output[:, :3, :3] , _lowercase , atol=4E-2 ) ) @require_tf @slow class __snake_case ( unittest.TestCase ): def __a ( self : List[Any] ): """simple docstring""" super().setUp() SCREAMING_SNAKE_CASE__ = """facebook/opt-350m""" def __a ( self : int ): """simple docstring""" SCREAMING_SNAKE_CASE__ = TFOPTForCausalLM.from_pretrained(self.path_model ) SCREAMING_SNAKE_CASE__ = GPTaTokenizer.from_pretrained(self.path_model ) SCREAMING_SNAKE_CASE__ = [ """Today is a beautiful day and I want to""", """In the city of""", """Paris is the capital of France and""", """Computers and mobile phones have taken""", ] # verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False SCREAMING_SNAKE_CASE__ = tokenizer(_lowercase , return_tensors="""tf""" , padding=_lowercase , add_special_tokens=_lowercase ) SCREAMING_SNAKE_CASE__ = tf.math.reduce_mean(model(inputs.input_ids , attention_mask=inputs.attention_mask )[0] , axis=-1 ) SCREAMING_SNAKE_CASE__ = tf.constant( [ [1.38_51, -13.89_23, -10.52_29, -10.75_33, -0.23_09, -10.23_84, -0.53_65, -9.09_47, -5.16_70], [-4.70_73, -10.62_76, -3.94_15, -21.52_42, -0.28_22, -0.28_22, -0.28_22, -0.28_22, -0.28_22], [0.62_47, -3.42_29, -8.91_79, -1.42_97, -14.16_50, 1.41_46, -9.02_18, -0.27_03, -0.27_03], [6.47_83, -1.99_13, -10.79_26, -2.33_36, 1.50_92, -0.99_74, -6.82_13, 1.34_77, 1.34_77], ] ) self.assertTrue(np.allclose(_lowercase , _lowercase , atol=1E-4 ) ) SCREAMING_SNAKE_CASE__ = tf.function(_lowercase , jit_compile=_lowercase ) SCREAMING_SNAKE_CASE__ = tf.math.reduce_mean(xla_generate(inputs.input_ids , attention_mask=inputs.attention_mask )[0] , axis=-1 ) self.assertTrue(np.allclose(_lowercase , _lowercase , atol=1E-4 ) ) @require_tf @slow class __snake_case ( unittest.TestCase ): @property def __a ( self : List[Any] ): """simple docstring""" return [ "Today is a beautiful day and I want", "In the city of", "Paris is the capital of France and", "Computers and mobile phones have taken", ] def __a ( self : Optional[int] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = """facebook/opt-125m""" SCREAMING_SNAKE_CASE__ = [ """Today is a beautiful day and I want to""", """In the city of New York, the city""", """Paris is the capital of France and the capital""", """Computers and mobile phones have taken over the""", ] SCREAMING_SNAKE_CASE__ = [] SCREAMING_SNAKE_CASE__ = GPTaTokenizer.from_pretrained(_lowercase ) SCREAMING_SNAKE_CASE__ = TFOPTForCausalLM.from_pretrained(_lowercase ) for prompt in self.prompts: SCREAMING_SNAKE_CASE__ = tokenizer(_lowercase , return_tensors="""tf""" ).input_ids SCREAMING_SNAKE_CASE__ = model.generate(_lowercase , max_length=10 ) SCREAMING_SNAKE_CASE__ = tokenizer.batch_decode(_lowercase , skip_special_tokens=_lowercase ) predicted_outputs += generated_string self.assertListEqual(_lowercase , _lowercase ) def __a ( self : Dict ): """simple docstring""" SCREAMING_SNAKE_CASE__ = """facebook/opt-350m""" SCREAMING_SNAKE_CASE__ = GPTaTokenizer.from_pretrained(_lowercase ) SCREAMING_SNAKE_CASE__ = TFOPTForCausalLM.from_pretrained(_lowercase ) SCREAMING_SNAKE_CASE__ = """left""" # use different length sentences to test batching SCREAMING_SNAKE_CASE__ = [ """Hello, my dog is a little""", """Today, I""", ] SCREAMING_SNAKE_CASE__ = tokenizer(_lowercase , return_tensors="""tf""" , padding=_lowercase ) SCREAMING_SNAKE_CASE__ = inputs["""input_ids"""] SCREAMING_SNAKE_CASE__ = model.generate(input_ids=_lowercase , attention_mask=inputs["""attention_mask"""] ) SCREAMING_SNAKE_CASE__ = tokenizer(sentences[0] , return_tensors="""tf""" ).input_ids SCREAMING_SNAKE_CASE__ = model.generate(input_ids=_lowercase ) SCREAMING_SNAKE_CASE__ = inputs_non_padded.shape[-1] - tf.math.reduce_sum( tf.cast(inputs["""attention_mask"""][-1] , tf.intaa ) ) SCREAMING_SNAKE_CASE__ = tokenizer(sentences[1] , return_tensors="""tf""" ).input_ids SCREAMING_SNAKE_CASE__ = model.generate(input_ids=_lowercase , max_length=model.config.max_length - num_paddings ) SCREAMING_SNAKE_CASE__ = tokenizer.batch_decode(_lowercase , skip_special_tokens=_lowercase ) SCREAMING_SNAKE_CASE__ = tokenizer.decode(output_non_padded[0] , skip_special_tokens=_lowercase ) SCREAMING_SNAKE_CASE__ = tokenizer.decode(output_padded[0] , skip_special_tokens=_lowercase ) SCREAMING_SNAKE_CASE__ = [ """Hello, my dog is a little bit of a dork.\nI'm a little bit""", """Today, I was in the middle of a conversation with a friend about the""", ] self.assertListEqual(_lowercase , _lowercase ) self.assertListEqual(_lowercase , [non_padded_sentence, padded_sentence] ) def __a ( self : List[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = """facebook/opt-350m""" SCREAMING_SNAKE_CASE__ = [ """Today is a beautiful day and I want to""", """In the city of San Francisco, the city""", """Paris is the capital of France and the capital""", """Computers and mobile phones have taken over the""", ] SCREAMING_SNAKE_CASE__ = [] SCREAMING_SNAKE_CASE__ = GPTaTokenizer.from_pretrained(_lowercase ) SCREAMING_SNAKE_CASE__ = TFOPTForCausalLM.from_pretrained(_lowercase ) for prompt in self.prompts: SCREAMING_SNAKE_CASE__ = tokenizer(_lowercase , return_tensors="""tf""" ).input_ids SCREAMING_SNAKE_CASE__ = model.generate(_lowercase , max_length=10 ) SCREAMING_SNAKE_CASE__ = tokenizer.batch_decode(_lowercase , skip_special_tokens=_lowercase ) predicted_outputs += generated_string self.assertListEqual(_lowercase , _lowercase )
219
import unittest from dataclasses import dataclass import pytest from accelerate.commands.config.config_args import SageMakerConfig from accelerate.utils import ComputeEnvironment from accelerate.utils.launch import _convert_nargs_to_dict @dataclass class __snake_case ( lowerCamelCase_ ): lowerCAmelCase_ = ComputeEnvironment.AMAZON_SAGEMAKER lowerCAmelCase_ = True lowerCAmelCase_ = "ml.p3.2xlarge" lowerCAmelCase_ = "accelerate_sagemaker_execution_role" lowerCAmelCase_ = "hf-sm" lowerCAmelCase_ = "us-east-1" lowerCAmelCase_ = 1 lowerCAmelCase_ = "accelerate-sagemaker-1" lowerCAmelCase_ = "1.6" lowerCAmelCase_ = "4.4" lowerCAmelCase_ = "train.py" lowerCAmelCase_ = [ "--model_name_or_path", "bert", "--do_train", "False", "--epochs", "3", "--learning_rate", "5e-5", "--max_steps", "50.5", ] lowerCAmelCase_ = [ "--model_name_or_path", "bert", "--do_train", "--do_test", "False", "--do_predict", "--epochs", "3", "--learning_rate", "5e-5", "--max_steps", "50.5", ] class __snake_case ( unittest.TestCase ): def __a ( self : List[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = _convert_nargs_to_dict(MockLaunchConfig.success_training_script_args ) assert isinstance(converted_args["""model_name_or_path"""] , _lowercase ) assert isinstance(converted_args["""do_train"""] , _lowercase ) assert isinstance(converted_args["""epochs"""] , _lowercase ) assert isinstance(converted_args["""learning_rate"""] , _lowercase ) assert isinstance(converted_args["""max_steps"""] , _lowercase ) with pytest.raises(_lowercase ): _convert_nargs_to_dict(MockLaunchConfig.fail_training_script_args )
219
1
'''simple docstring''' import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.text import TextDatasetReader from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def UpperCamelCase_( snake_case : List[str] , snake_case : Tuple ): '''simple docstring''' assert isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def UpperCamelCase_( snake_case : str , snake_case : Optional[int] , snake_case : Union[str, Any] ): '''simple docstring''' snake_case_ = tmp_path / "cache" snake_case_ = {"text": "string"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): snake_case_ = TextDatasetReader(__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE , keep_in_memory=__SCREAMING_SNAKE_CASE ).read() _check_text_dataset(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( "features" , [ None, {"text": "string"}, {"text": "int32"}, {"text": "float32"}, ] , ) def UpperCamelCase_( snake_case : Dict , snake_case : List[str] , snake_case : int ): '''simple docstring''' snake_case_ = tmp_path / "cache" snake_case_ = {"text": "string"} snake_case_ = features.copy() if features else default_expected_features snake_case_ = ( Features({feature: Value(__SCREAMING_SNAKE_CASE ) for feature, dtype in features.items()} ) if features is not None else None ) snake_case_ = TextDatasetReader(__SCREAMING_SNAKE_CASE , features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE ).read() _check_text_dataset(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def UpperCamelCase_( snake_case : Tuple , snake_case : int , snake_case : Union[str, Any] ): '''simple docstring''' snake_case_ = tmp_path / "cache" snake_case_ = {"text": "string"} snake_case_ = TextDatasetReader(__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE , split=__SCREAMING_SNAKE_CASE ).read() _check_text_dataset(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("path_type" , [str, list] ) def UpperCamelCase_( snake_case : str , snake_case : Optional[Any] , snake_case : Tuple ): '''simple docstring''' if issubclass(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case_ = text_path elif issubclass(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case_ = [text_path] snake_case_ = tmp_path / "cache" snake_case_ = {"text": "string"} snake_case_ = TextDatasetReader(__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE ).read() _check_text_dataset(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def UpperCamelCase_( snake_case : Optional[Any] , snake_case : Tuple , snake_case : List[str]=("train",) ): '''simple docstring''' assert isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) for split in splits: snake_case_ = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def UpperCamelCase_( snake_case : List[str] , snake_case : List[Any] , snake_case : Any ): '''simple docstring''' snake_case_ = tmp_path / "cache" snake_case_ = {"text": "string"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): snake_case_ = TextDatasetReader({"train": text_path} , cache_dir=__SCREAMING_SNAKE_CASE , keep_in_memory=__SCREAMING_SNAKE_CASE ).read() _check_text_datasetdict(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize( "features" , [ None, {"text": "string"}, {"text": "int32"}, {"text": "float32"}, ] , ) def UpperCamelCase_( snake_case : str , snake_case : Optional[Any] , snake_case : Tuple ): '''simple docstring''' snake_case_ = tmp_path / "cache" # CSV file loses col_1 string dtype information: default now is "int64" instead of "string" snake_case_ = {"text": "string"} snake_case_ = features.copy() if features else default_expected_features snake_case_ = ( Features({feature: Value(__SCREAMING_SNAKE_CASE ) for feature, dtype in features.items()} ) if features is not None else None ) snake_case_ = TextDatasetReader({"train": text_path} , features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE ).read() _check_text_datasetdict(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def UpperCamelCase_( snake_case : Tuple , snake_case : Optional[Any] , snake_case : Any ): '''simple docstring''' if split: snake_case_ = {split: text_path} else: snake_case_ = "train" snake_case_ = {"train": text_path, "test": text_path} snake_case_ = tmp_path / "cache" snake_case_ = {"text": "string"} snake_case_ = TextDatasetReader(__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE ).read() _check_text_datasetdict(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() )
369
'''simple docstring''' def UpperCamelCase_( snake_case : int , snake_case : int ): '''simple docstring''' while b: snake_case_ , snake_case_ = b, a % b return a def UpperCamelCase_( snake_case : int , snake_case : int ): '''simple docstring''' return a if b == 0 else euclidean_gcd_recursive(snake_case , a % b ) def UpperCamelCase_( ): '''simple docstring''' print(f'euclidean_gcd(3, 5) = {euclidean_gcd(3 , 5 )}' ) print(f'euclidean_gcd(5, 3) = {euclidean_gcd(5 , 3 )}' ) print(f'euclidean_gcd(1, 3) = {euclidean_gcd(1 , 3 )}' ) print(f'euclidean_gcd(3, 6) = {euclidean_gcd(3 , 6 )}' ) print(f'euclidean_gcd(6, 3) = {euclidean_gcd(6 , 3 )}' ) print(f'euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3 , 5 )}' ) print(f'euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5 , 3 )}' ) print(f'euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1 , 3 )}' ) print(f'euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3 , 6 )}' ) print(f'euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6 , 3 )}' ) if __name__ == "__main__": main()
92
0
'''simple docstring''' def _UpperCamelCase ( UpperCamelCase__ ): UpperCAmelCase__ : list[list[int]] = [[0 for _ in range(UpperCamelCase__ )] for _ in range(m + 1 )] for i in range(m + 1 ): UpperCAmelCase__ : Union[str, Any] = 1 for n in range(m + 1 ): for k in range(1 , UpperCamelCase__ ): memo[n][k] += memo[n][k - 1] if n - k > 0: memo[n][k] += memo[n - k - 1][k] return memo[m][m - 1] if __name__ == "__main__": import sys if len(sys.argv) == 1: try: __A =int(input('Enter a number: ').strip()) print(partition(n)) except ValueError: print('Please enter a number.') else: try: __A =int(sys.argv[1]) print(partition(n)) except ValueError: print('Please pass a number.')
163
'''simple docstring''' import os import zipfile import pytest from datasets.utils.extract import ( BzipaExtractor, Extractor, GzipExtractor, LzaExtractor, SevenZipExtractor, TarExtractor, XzExtractor, ZipExtractor, ZstdExtractor, ) from .utils import require_lza, require_pyazr, require_zstandard @pytest.mark.parametrize( """compression_format, is_archive""" , [ ("""7z""", True), ("""bz2""", False), ("""gzip""", False), ("""lz4""", False), ("""tar""", True), ("""xz""", False), ("""zip""", True), ("""zstd""", False), ] , ) def _UpperCamelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ): UpperCAmelCase__ : str = { """7z""": (seven_zip_file, SevenZipExtractor), """bz2""": (bza_file, BzipaExtractor), """gzip""": (gz_file, GzipExtractor), """lz4""": (lza_file, LzaExtractor), """tar""": (tar_file, TarExtractor), """xz""": (xz_file, XzExtractor), """zip""": (zip_file, ZipExtractor), """zstd""": (zstd_file, ZstdExtractor), } UpperCAmelCase__ , UpperCAmelCase__ : int = input_paths_and_base_extractors[compression_format] if input_path is None: UpperCAmelCase__ : Union[str, Any] = f'''for \'{compression_format}\' compression_format, ''' if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(UpperCamelCase__ ) assert base_extractor.is_extractable(UpperCamelCase__ ) UpperCAmelCase__ : int = tmp_path / ("""extracted""" if is_archive else """extracted.txt""") base_extractor.extract(UpperCamelCase__ , UpperCamelCase__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name UpperCAmelCase__ : Union[str, Any] = file_path.read_text(encoding="""utf-8""" ) else: UpperCAmelCase__ : str = output_path.read_text(encoding="""utf-8""" ) UpperCAmelCase__ : Union[str, Any] = text_file.read_text(encoding="""utf-8""" ) assert extracted_file_content == expected_file_content @pytest.mark.parametrize( """compression_format, is_archive""" , [ ("""7z""", True), ("""bz2""", False), ("""gzip""", False), ("""lz4""", False), ("""tar""", True), ("""xz""", False), ("""zip""", True), ("""zstd""", False), ] , ) def _UpperCamelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ): UpperCAmelCase__ : Dict = { """7z""": seven_zip_file, """bz2""": bza_file, """gzip""": gz_file, """lz4""": lza_file, """tar""": tar_file, """xz""": xz_file, """zip""": zip_file, """zstd""": zstd_file, } UpperCAmelCase__ : List[str] = input_paths[compression_format] if input_path is None: UpperCAmelCase__ : Optional[Any] = f'''for \'{compression_format}\' compression_format, ''' if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(UpperCamelCase__ ) UpperCAmelCase__ : Dict = Extractor.infer_extractor_format(UpperCamelCase__ ) assert extractor_format is not None UpperCAmelCase__ : Tuple = tmp_path / ("""extracted""" if is_archive else """extracted.txt""") Extractor.extract(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name UpperCAmelCase__ : Dict = file_path.read_text(encoding="""utf-8""" ) else: UpperCAmelCase__ : Optional[Any] = output_path.read_text(encoding="""utf-8""" ) UpperCAmelCase__ : str = text_file.read_text(encoding="""utf-8""" ) assert extracted_file_content == expected_file_content @pytest.fixture def _UpperCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): import tarfile UpperCAmelCase__ : Optional[int] = tmp_path / """data_dot_dot""" directory.mkdir() UpperCAmelCase__ : Optional[Any] = directory / """tar_file_with_dot_dot.tar""" with tarfile.TarFile(UpperCamelCase__ , """w""" ) as f: f.add(UpperCamelCase__ , arcname=os.path.join("""..""" , text_file.name ) ) return path @pytest.fixture def _UpperCamelCase ( UpperCamelCase__ ): import tarfile UpperCAmelCase__ : List[str] = tmp_path / """data_sym_link""" directory.mkdir() UpperCAmelCase__ : Optional[int] = directory / """tar_file_with_sym_link.tar""" os.symlink("""..""" , directory / """subdir""" , target_is_directory=UpperCamelCase__ ) with tarfile.TarFile(UpperCamelCase__ , """w""" ) as f: f.add(str(directory / """subdir""" ) , arcname="""subdir""" ) # str required by os.readlink on Windows and Python < 3.8 return path @pytest.mark.parametrize( """insecure_tar_file, error_log""" , [("""tar_file_with_dot_dot""", """illegal path"""), ("""tar_file_with_sym_link""", """Symlink""")] , ) def _UpperCamelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): UpperCAmelCase__ : Any = { """tar_file_with_dot_dot""": tar_file_with_dot_dot, """tar_file_with_sym_link""": tar_file_with_sym_link, } UpperCAmelCase__ : str = insecure_tar_files[insecure_tar_file] UpperCAmelCase__ : Union[str, Any] = tmp_path / """extracted""" TarExtractor.extract(UpperCamelCase__ , UpperCamelCase__ ) assert caplog.text for record in caplog.records: assert record.levelname == "ERROR" assert error_log in record.msg def _UpperCamelCase ( UpperCamelCase__ ): # We should have less false positives than zipfile.is_zipfile # We do that by checking only the magic number UpperCAmelCase__ : Tuple = tmpdir / """not_a_zip_file""" # From: https://github.com/python/cpython/pull/5053 UpperCAmelCase__ : Any = ( b"""\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00""" b"""\x00\x02\x08\x06\x00\x00\x00\x99\x81\xb6'\x00\x00\x00\x15I""" b"""DATx\x01\x01\n\x00\xf5\xff\x00PK\x05\x06\x00PK\x06\x06\x07""" b"""\xac\x01N\xc6|a\r\x00\x00\x00\x00IEND\xaeB`\x82""" ) with not_a_zip_file.open("""wb""" ) as f: f.write(UpperCamelCase__ ) assert zipfile.is_zipfile(str(UpperCamelCase__ ) ) # is a false positive for `zipfile` assert not ZipExtractor.is_extractable(UpperCamelCase__ ) # but we're right
163
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, ) __snake_case = { """configuration_albert""": ["""ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """AlbertConfig""", """AlbertOnnxConfig"""], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ["""AlbertTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ["""AlbertTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ """ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """AlbertForMaskedLM""", """AlbertForMultipleChoice""", """AlbertForPreTraining""", """AlbertForQuestionAnswering""", """AlbertForSequenceClassification""", """AlbertForTokenClassification""", """AlbertModel""", """AlbertPreTrainedModel""", """load_tf_weights_in_albert""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ """TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFAlbertForMaskedLM""", """TFAlbertForMultipleChoice""", """TFAlbertForPreTraining""", """TFAlbertForQuestionAnswering""", """TFAlbertForSequenceClassification""", """TFAlbertForTokenClassification""", """TFAlbertMainLayer""", """TFAlbertModel""", """TFAlbertPreTrainedModel""", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ """FlaxAlbertForMaskedLM""", """FlaxAlbertForMultipleChoice""", """FlaxAlbertForPreTraining""", """FlaxAlbertForQuestionAnswering""", """FlaxAlbertForSequenceClassification""", """FlaxAlbertForTokenClassification""", """FlaxAlbertModel""", """FlaxAlbertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_albert import ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, AlbertConfig, AlbertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_albert import AlbertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_albert_fast import AlbertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_albert import ( ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForPreTraining, AlbertForQuestionAnswering, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertModel, AlbertPreTrainedModel, load_tf_weights_in_albert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_albert import ( TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFAlbertForMaskedLM, TFAlbertForMultipleChoice, TFAlbertForPreTraining, TFAlbertForQuestionAnswering, TFAlbertForSequenceClassification, TFAlbertForTokenClassification, TFAlbertMainLayer, TFAlbertModel, TFAlbertPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, FlaxAlbertPreTrainedModel, ) else: import sys __snake_case = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
112
"""simple docstring""" import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def __lowerCAmelCase ( lowercase : List[str] , lowercase : int , lowercase : Dict , lowercase : Dict , lowercase : int ) -> int: """simple docstring""" with open(lowercase ) as metadata_file: snake_case : str = json.load(lowercase ) snake_case : Optional[Any] = LukeConfig(use_entity_aware_attention=lowercase , **metadata["model_config"] ) # Load in the weights from the checkpoint_path snake_case : Tuple = torch.load(lowercase , map_location="cpu" )["module"] # Load the entity vocab file snake_case : Optional[Any] = load_original_entity_vocab(lowercase ) # add an entry for [MASK2] snake_case : Dict = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 snake_case : Union[str, Any] = XLMRobertaTokenizer.from_pretrained(metadata["model_config"]["bert_model_name"] ) # Add special tokens to the token vocabulary for downstream tasks snake_case : Tuple = AddedToken("<ent>" , lstrip=lowercase , rstrip=lowercase ) snake_case : str = AddedToken("<ent2>" , lstrip=lowercase , rstrip=lowercase ) tokenizer.add_special_tokens({"additional_special_tokens": [entity_token_a, entity_token_a]} ) config.vocab_size += 2 print(F'Saving tokenizer to {pytorch_dump_folder_path}' ) tokenizer.save_pretrained(lowercase ) with open(os.path.join(lowercase , "tokenizer_config.json" ) , "r" ) as f: snake_case : str = json.load(lowercase ) snake_case : List[str] = "MLukeTokenizer" with open(os.path.join(lowercase , "tokenizer_config.json" ) , "w" ) as f: json.dump(lowercase , lowercase ) with open(os.path.join(lowercase , MLukeTokenizer.vocab_files_names["entity_vocab_file"] ) , "w" ) as f: json.dump(lowercase , lowercase ) snake_case : Dict = MLukeTokenizer.from_pretrained(lowercase ) # Initialize the embeddings of the special tokens snake_case : Tuple = tokenizer.convert_tokens_to_ids(["@"] )[0] snake_case : str = tokenizer.convert_tokens_to_ids(["#"] )[0] snake_case : Union[str, Any] = state_dict["embeddings.word_embeddings.weight"] snake_case : str = word_emb[ent_init_index].unsqueeze(0 ) snake_case : Union[str, Any] = word_emb[enta_init_index].unsqueeze(0 ) snake_case : Union[str, Any] = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: snake_case : Tuple = state_dict[bias_name] snake_case : Optional[Any] = decoder_bias[ent_init_index].unsqueeze(0 ) snake_case : Optional[Any] = decoder_bias[enta_init_index].unsqueeze(0 ) snake_case : Any = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: snake_case : Optional[int] = F'encoder.layer.{layer_index}.attention.self.' snake_case : Optional[Any] = state_dict[prefix + matrix_name] snake_case : Optional[Any] = state_dict[prefix + matrix_name] snake_case : Any = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks snake_case : List[Any] = state_dict["entity_embeddings.entity_embeddings.weight"] snake_case : str = entity_emb[entity_vocab["[MASK]"]].unsqueeze(0 ) snake_case : Tuple = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' snake_case : Optional[int] = state_dict["entity_predictions.bias"] snake_case : Optional[int] = entity_prediction_bias[entity_vocab["[MASK]"]].unsqueeze(0 ) snake_case : Union[str, Any] = torch.cat([entity_prediction_bias, entity_mask_bias] ) snake_case : Union[str, Any] = LukeForMaskedLM(config=lowercase ).eval() state_dict.pop("entity_predictions.decoder.weight" ) state_dict.pop("lm_head.decoder.weight" ) state_dict.pop("lm_head.decoder.bias" ) snake_case : Tuple = OrderedDict() for key, value in state_dict.items(): if not (key.startswith("lm_head" ) or key.startswith("entity_predictions" )): snake_case : Any = state_dict[key] else: snake_case : Tuple = state_dict[key] snake_case ,snake_case : Optional[Any] = model.load_state_dict(lowercase , strict=lowercase ) if set(lowercase ) != {"luke.embeddings.position_ids"}: raise ValueError(F'Unexpected unexpected_keys: {unexpected_keys}' ) if set(lowercase ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(F'Unexpected missing_keys: {missing_keys}' ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs snake_case : Optional[Any] = MLukeTokenizer.from_pretrained(lowercase , task="entity_classification" ) snake_case : List[str] = "ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan)." snake_case : str = (0, 9) snake_case : Union[str, Any] = tokenizer(lowercase , entity_spans=[span] , return_tensors="pt" ) snake_case : int = model(**lowercase ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base snake_case : int = torch.Size((1, 33, 768) ) snake_case : str = torch.tensor([[0.0892, 0.0596, -0.2819], [0.0134, 0.1199, 0.0573], [-0.0169, 0.0927, 0.0644]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( F'Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}' ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowercase , atol=1e-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base snake_case : Any = torch.Size((1, 1, 768) ) snake_case : List[Any] = torch.tensor([[-0.1482, 0.0609, 0.0322]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( F'Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is' F' {expected_shape}' ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] , lowercase , atol=1e-4 ): raise ValueError # Verify masked word/entity prediction snake_case : List[str] = MLukeTokenizer.from_pretrained(lowercase ) snake_case : List[Any] = "Tokyo is the capital of <mask>." snake_case : Optional[Any] = (24, 30) snake_case : List[str] = tokenizer(lowercase , entity_spans=[span] , return_tensors="pt" ) snake_case : Any = model(**lowercase ) snake_case : int = encoding["input_ids"][0].tolist() snake_case : str = input_ids.index(tokenizer.convert_tokens_to_ids("<mask>" ) ) snake_case : Tuple = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(lowercase ) snake_case : Tuple = outputs.entity_logits[0][0].argmax().item() snake_case : Optional[int] = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith("en:" )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print("Saving PyTorch model to {}".format(lowercase ) ) model.save_pretrained(lowercase ) def __lowerCAmelCase ( lowercase : Union[str, Any] ) -> Dict: """simple docstring""" snake_case : Tuple = ["[MASK]", "[PAD]", "[UNK]"] snake_case : Optional[Any] = [json.loads(lowercase ) for line in open(lowercase )] snake_case : Any = {} for entry in data: snake_case : Union[str, Any] = entry["id"] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: snake_case : Union[str, Any] = entity_id break snake_case : Dict = F'{language}:{entity_name}' snake_case : str = entity_id return new_mapping if __name__ == "__main__": __snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument("""--checkpoint_path""", type=str, help="""Path to a pytorch_model.bin file.""") parser.add_argument( """--metadata_path""", default=None, type=str, help="""Path to a metadata.json file, defining the configuration.""" ) parser.add_argument( """--entity_vocab_path""", default=None, type=str, help="""Path to an entity_vocab.tsv file, containing the entity vocabulary.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to where to dump the output PyTorch model.""" ) parser.add_argument( """--model_size""", default="""base""", type=str, choices=["""base""", """large"""], help="""Size of the model to be converted.""" ) __snake_case = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
112
1
from __future__ import annotations from typing import Any class A_ : def __init__( self , _A ): '''simple docstring''' UpperCAmelCase = num_of_nodes UpperCAmelCase = [] UpperCAmelCase = {} def _lowercase ( self , _A , _A , _A ): '''simple docstring''' self.m_edges.append([u_node, v_node, weight] ) def _lowercase ( self , _A ): '''simple docstring''' if self.m_component[u_node] == u_node: return u_node return self.find_component(self.m_component[u_node] ) def _lowercase ( self , _A ): '''simple docstring''' if self.m_component[u_node] != u_node: for k in self.m_component: UpperCAmelCase = self.find_component(_A ) def _lowercase ( self , _A , _A , _A ): '''simple docstring''' if component_size[u_node] <= component_size[v_node]: UpperCAmelCase = v_node component_size[v_node] += component_size[u_node] self.set_component(_A ) elif component_size[u_node] >= component_size[v_node]: UpperCAmelCase = self.find_component(_A ) component_size[u_node] += component_size[v_node] self.set_component(_A ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = [] UpperCAmelCase = 0 UpperCAmelCase = [-1] * self.m_num_of_nodes # A list of components (initialized to all of the nodes) for node in range(self.m_num_of_nodes ): self.m_component.update({node: node} ) component_size.append(1 ) UpperCAmelCase = self.m_num_of_nodes while num_of_components > 1: for edge in self.m_edges: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = edge UpperCAmelCase = self.m_component[u] UpperCAmelCase = self.m_component[v] if u_component != v_component: for component in (u_component, v_component): if ( minimum_weight_edge[component] == -1 or minimum_weight_edge[component][2] > w ): UpperCAmelCase = [u, v, w] for edge in minimum_weight_edge: if isinstance(_A , _A ): UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = edge UpperCAmelCase = self.m_component[u] UpperCAmelCase = self.m_component[v] if u_component != v_component: mst_weight += w self.union(_A , _A , _A ) print(F"""Added edge [{u} - {v}]\nAdded weight: {w}\n""" ) num_of_components -= 1 UpperCAmelCase = [-1] * self.m_num_of_nodes print(F"""The total weight of the minimal spanning tree is: {mst_weight}""" ) def __SCREAMING_SNAKE_CASE ( ) -> None: '''simple docstring''' if __name__ == "__main__": import doctest doctest.testmod()
273
from datetime import datetime import requests def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> bytes: '''simple docstring''' UpperCAmelCase = '''https://downloadgram.net/wp-json/wppress/video-downloader/video?url=''' UpperCAmelCase = requests.get(base_url + url ).json()[0]['''urls'''][0]['''src'''] return requests.get(UpperCamelCase__ ).content if __name__ == "__main__": __A : Union[str, Any] = input("Enter Video/IGTV url: ").strip() __A : Tuple = F'{datetime.now():%Y-%m-%d_%H:%M:%S}.mp4' with open(file_name, "wb") as fp: fp.write(download_video(url)) print(F'Done. Video saved to disk as {file_name}.')
273
1
'''simple docstring''' import baseaa import io import json import os from copy import deepcopy from ..optimizer import AcceleratedOptimizer from ..scheduler import AcceleratedScheduler class A_ : '''simple docstring''' def __init__( self : str , lowercase_ : List[str] ) -> Any: if isinstance(lowercase_ , lowercase_ ): # Don't modify user's data should they want to reuse it (e.g. in tests), because once we # modified it, it will not be accepted here again, since `auto` values would have been overridden UpperCAmelCase : int = deepcopy(lowercase_ ) elif os.path.exists(lowercase_ ): with io.open(lowercase_ , 'r' , encoding='utf-8' ) as f: UpperCAmelCase : Dict = json.load(lowercase_ ) else: try: UpperCAmelCase : int = baseaa.urlsafe_baadecode(lowercase_ ).decode('utf-8' ) UpperCAmelCase : Dict = json.loads(lowercase_ ) except (UnicodeDecodeError, AttributeError, ValueError): raise ValueError( f"""Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}""" ) UpperCAmelCase : Optional[int] = config self.set_stage_and_offload() def UpperCAmelCase_ ( self : Optional[int] ) -> Dict: # zero stage - this is done as early as possible, before model is created, to allow # ``is_deepspeed_zero3_enabled`` query and getting to the early deepspeed config object # during ``zero.Init()`` which needs to know the dtype, and some other hparams. UpperCAmelCase : Any = self.get_value('zero_optimization.stage' , -1 ) # offload UpperCAmelCase : List[Any] = False if self.is_zeroa() or self.is_zeroa(): UpperCAmelCase : Tuple = set(['cpu', 'nvme'] ) UpperCAmelCase : Dict = set( [ self.get_value('zero_optimization.offload_optimizer.device' ), self.get_value('zero_optimization.offload_param.device' ), ] ) if len(offload_devices & offload_devices_valid ) > 0: UpperCAmelCase : Optional[int] = True def UpperCAmelCase_ ( self : Union[str, Any] , lowercase_ : List[Any] ) -> Any: UpperCAmelCase : str = self.config # find the config node of interest if it exists UpperCAmelCase : int = ds_key_long.split('.' ) UpperCAmelCase : Optional[int] = nodes.pop() for node in nodes: UpperCAmelCase : Dict = config.get(lowercase_ ) if config is None: return None, ds_key return config, ds_key def UpperCAmelCase_ ( self : List[Any] , lowercase_ : Optional[Any] , lowercase_ : Tuple=None ) -> Union[str, Any]: UpperCAmelCase , UpperCAmelCase : Optional[int] = self.find_config_node(lowercase_ ) if config is None: return default return config.get(lowercase_ , lowercase_ ) def UpperCAmelCase_ ( self : List[str] , lowercase_ : Union[str, Any] , lowercase_ : Union[str, Any]=False ) -> Optional[Any]: UpperCAmelCase : Union[str, Any] = self.config # find the config node of interest if it exists UpperCAmelCase : int = ds_key_long.split('.' ) for node in nodes: UpperCAmelCase : str = config UpperCAmelCase : Union[str, Any] = config.get(lowercase_ ) if config is None: if must_exist: raise ValueError(f"""Can't find {ds_key_long} entry in the config: {self.config}""" ) else: return # if found remove it if parent_config is not None: parent_config.pop(lowercase_ ) def UpperCAmelCase_ ( self : Tuple , lowercase_ : str ) -> Optional[int]: UpperCAmelCase : Dict = self.get_value(lowercase_ ) return False if value is None else bool(lowercase_ ) def UpperCAmelCase_ ( self : Tuple , lowercase_ : str ) -> Optional[Any]: UpperCAmelCase : List[Any] = self.get_value(lowercase_ ) return False if value is None else not bool(lowercase_ ) def UpperCAmelCase_ ( self : Union[str, Any] ) -> int: return self._stage == 2 def UpperCAmelCase_ ( self : str ) -> Dict: return self._stage == 3 def UpperCAmelCase_ ( self : Any ) -> Optional[Any]: return self._offload class A_ : '''simple docstring''' def __init__( self : str , lowercase_ : List[str] ) -> List[Any]: UpperCAmelCase : List[str] = engine def UpperCAmelCase_ ( self : str , lowercase_ : Dict , **lowercase_ : int ) -> Optional[Any]: # runs backpropagation and handles mixed precision self.engine.backward(lowercase_ , **lowercase_ ) # Deepspeed's `engine.step` performs the following operations: # - gradient accumulation check # - gradient clipping # - optimizer step # - zero grad # - checking overflow # - lr_scheduler step (only if engine.lr_scheduler is not None) self.engine.step() # and this plugin overrides the above calls with no-ops when Accelerate runs under # Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple # training loop that works transparently under many training regimes. class A_ ( _snake_case ): '''simple docstring''' def __init__( self : Tuple , lowercase_ : int ) -> List[str]: super().__init__(lowercase_ , device_placement=lowercase_ , scaler=lowercase_ ) UpperCAmelCase : str = hasattr(self.optimizer , 'overflow' ) def UpperCAmelCase_ ( self : Union[str, Any] , lowercase_ : List[Any]=None ) -> Any: pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed def UpperCAmelCase_ ( self : Any ) -> Any: pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed @property def UpperCAmelCase_ ( self : Tuple ) -> List[str]: if self.__has_overflow__: return self.optimizer.overflow return False class A_ ( _snake_case ): '''simple docstring''' def __init__( self : List[str] , lowercase_ : Optional[Any] , lowercase_ : Optional[Any] ) -> Dict: super().__init__(lowercase_ , lowercase_ ) def UpperCAmelCase_ ( self : List[str] ) -> Optional[int]: pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed class A_ : '''simple docstring''' def __init__( self : Optional[int] , lowercase_ : Optional[int] , lowercase_ : List[str]=0.001 , lowercase_ : str=0 , **lowercase_ : Dict ) -> Optional[Any]: UpperCAmelCase : List[str] = params UpperCAmelCase : Dict = lr UpperCAmelCase : List[str] = weight_decay UpperCAmelCase : List[str] = kwargs class A_ : '''simple docstring''' def __init__( self : int , lowercase_ : Any , lowercase_ : Union[str, Any]=None , lowercase_ : Any=0 , **lowercase_ : Optional[Any] ) -> Any: UpperCAmelCase : str = optimizer UpperCAmelCase : Tuple = total_num_steps UpperCAmelCase : int = warmup_num_steps UpperCAmelCase : int = kwargs
280
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import PoolFormerImageProcessor class A_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : Any , lowercase_ : str , lowercase_ : Union[str, Any]=7 , lowercase_ : Union[str, Any]=3 , lowercase_ : int=30 , lowercase_ : Tuple=400 , lowercase_ : Tuple=True , lowercase_ : Optional[int]=None , lowercase_ : List[str]=0.9 , lowercase_ : Tuple=None , lowercase_ : Union[str, Any]=True , lowercase_ : int=[0.5, 0.5, 0.5] , lowercase_ : List[str]=[0.5, 0.5, 0.5] , ) -> Tuple: UpperCAmelCase : Optional[int] = size if size is not None else {'shortest_edge': 30} UpperCAmelCase : int = crop_size if crop_size is not None else {'height': 30, 'width': 30} UpperCAmelCase : Tuple = parent UpperCAmelCase : Optional[Any] = batch_size UpperCAmelCase : int = num_channels UpperCAmelCase : int = min_resolution UpperCAmelCase : Optional[int] = max_resolution UpperCAmelCase : str = do_resize_and_center_crop UpperCAmelCase : int = size UpperCAmelCase : Dict = crop_pct UpperCAmelCase : Union[str, Any] = crop_size UpperCAmelCase : Optional[int] = do_normalize UpperCAmelCase : Optional[Any] = image_mean UpperCAmelCase : Optional[Any] = image_std def UpperCAmelCase_ ( self : str ) -> int: return { "size": self.size, "do_resize_and_center_crop": self.do_resize_and_center_crop, "crop_pct": self.crop_pct, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class A_ ( _snake_case , unittest.TestCase ): '''simple docstring''' UpperCAmelCase_ : Tuple = PoolFormerImageProcessor if is_vision_available() else None def UpperCAmelCase_ ( self : Dict ) -> str: UpperCAmelCase : Any = PoolFormerImageProcessingTester(self ) @property def UpperCAmelCase_ ( self : List[Any] ) -> Union[str, Any]: return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase_ ( self : Tuple ) -> str: UpperCAmelCase : Any = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowercase_ , 'do_resize_and_center_crop' ) ) self.assertTrue(hasattr(lowercase_ , 'size' ) ) self.assertTrue(hasattr(lowercase_ , 'crop_pct' ) ) self.assertTrue(hasattr(lowercase_ , 'do_normalize' ) ) self.assertTrue(hasattr(lowercase_ , 'image_mean' ) ) self.assertTrue(hasattr(lowercase_ , 'image_std' ) ) def UpperCAmelCase_ ( self : Optional[Any] ) -> Tuple: UpperCAmelCase : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'shortest_edge': 30} ) self.assertEqual(image_processor.crop_size , {'height': 30, 'width': 30} ) UpperCAmelCase : Any = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'shortest_edge': 42} ) self.assertEqual(image_processor.crop_size , {'height': 84, 'width': 84} ) def UpperCAmelCase_ ( self : Union[str, Any] ) -> str: pass def UpperCAmelCase_ ( self : List[Any] ) -> List[str]: # Initialize image_processing UpperCAmelCase : int = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCAmelCase : Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowercase_ ) for image in image_inputs: self.assertIsInstance(lowercase_ , Image.Image ) # Test not batched input UpperCAmelCase : Dict = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched UpperCAmelCase : str = image_processing(lowercase_ , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) def UpperCAmelCase_ ( self : List[Any] ) -> Dict: # Initialize image_processing UpperCAmelCase : int = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCAmelCase : Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowercase_ , numpify=lowercase_ ) for image in image_inputs: self.assertIsInstance(lowercase_ , np.ndarray ) # Test not batched input UpperCAmelCase : Optional[Any] = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched UpperCAmelCase : Optional[Any] = image_processing(lowercase_ , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) def UpperCAmelCase_ ( self : str ) -> Dict: # Initialize image_processing UpperCAmelCase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCAmelCase : Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowercase_ , torchify=lowercase_ ) for image in image_inputs: self.assertIsInstance(lowercase_ , torch.Tensor ) # Test not batched input UpperCAmelCase : int = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched UpperCAmelCase : Optional[int] = image_processing(lowercase_ , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , )
280
1
import os from typing import Optional import fsspec from fsspec.archive import AbstractArchiveFileSystem from fsspec.utils import DEFAULT_BLOCK_SIZE class a__ ( snake_case__ ): _a : Optional[int] = """""" _a : str = ( None # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz ) _a : str = None # compression type in fsspec. ex: "gzip" _a : str = None # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz def __init__( self , _A = "" , _A = None , _A = None , **_A ): """simple docstring""" super().__init__(self , **_A ) # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode __lowerCAmelCase = fsspec.open( _A , mode="rb" , protocol=_A , compression=self.compression , client_kwargs={ "requote_redirect_url": False, # see https://github.com/huggingface/datasets/pull/5459 "trust_env": True, # Enable reading proxy env variables. **(target_options or {}).pop("client_kwargs" , {} ), # To avoid issues if it was already passed. } , **(target_options or {}) , ) __lowerCAmelCase = os.path.basename(self.file.path.split("::" )[0] ) __lowerCAmelCase = ( self.compressed_name[: self.compressed_name.rindex("." )] if "." in self.compressed_name else self.compressed_name ) __lowerCAmelCase = None @classmethod def __SCREAMING_SNAKE_CASE( cls , _A ): """simple docstring""" return super()._strip_protocol(_A ).lstrip("/" ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" if self.dir_cache is None: __lowerCAmelCase = {**self.file.fs.info(self.file.path ), "name": self.uncompressed_name} __lowerCAmelCase = {f["name"]: f} def __SCREAMING_SNAKE_CASE( self , _A ): """simple docstring""" return self.file.open().read() def __SCREAMING_SNAKE_CASE( self , _A , _A = "rb" , _A=None , _A=True , _A=None , **_A , ): """simple docstring""" __lowerCAmelCase = self._strip_protocol(_A ) if mode != "rb": raise ValueError(f"""Tried to read with mode {mode} on file {self.file.path} opened with mode 'rb'""" ) return self.file.open() class a__ ( snake_case__ ): _a : Any = """bz2""" _a : Dict = """bz2""" _a : Union[str, Any] = """.bz2""" class a__ ( snake_case__ ): _a : int = """gzip""" _a : List[Any] = """gzip""" _a : Optional[int] = """.gz""" class a__ ( snake_case__ ): _a : Optional[int] = """lz4""" _a : Any = """lz4""" _a : Tuple = """.lz4""" class a__ ( snake_case__ ): _a : Dict = """xz""" _a : List[Any] = """xz""" _a : Optional[Any] = """.xz""" class a__ ( snake_case__ ): _a : Union[str, Any] = """zstd""" _a : int = """zstd""" _a : int = """.zst""" def __init__( self , _A , _A = "rb" , _A = None , _A = None , _A = DEFAULT_BLOCK_SIZE , **_A , ): """simple docstring""" super().__init__( fo=_A , mode=_A , target_protocol=_A , target_options=_A , block_size=_A , **_A , ) # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2: # # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open # out.close = close # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only # # see https://github.com/intake/filesystem_spec/issues/725 __lowerCAmelCase = self.file.__enter__ class a__ : def __init__( self , _A ): """simple docstring""" __lowerCAmelCase = file_ def __enter__( self ): """simple docstring""" self._file.__enter__() return self def __exit__( self , *_A , **_A ): """simple docstring""" self._file.__exit__(*_A , **_A ) def __iter__( self ): """simple docstring""" return iter(self._file ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" return next(self._file ) def __getattr__( self , _A ): """simple docstring""" return getattr(self._file , _A ) def fixed_enter(*_A , **_A ): return WrappedFile(_enter(*_A , **_A ) ) __lowerCAmelCase = fixed_enter
92
import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( """The `image_to_image.py` script is outdated. Please use directly `from diffusers import""" """ StableDiffusionImg2ImgPipeline` instead.""" )
92
1
"""simple docstring""" def lowercase__ ( snake_case_ :int = 10 , snake_case_ :int = 22 ): __UpperCAmelCase = range(1 , snake_case_ ) __UpperCAmelCase = range(1 , snake_case_ ) return sum( 1 for power in powers for base in bases if len(str(base**power ) ) == power ) if __name__ == "__main__": print(f"""{solution(10, 22) = }""")
86
"""simple docstring""" import collections import inspect import unittest from typing import Dict, List, Tuple from transformers import MaskFormerSwinConfig from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device from transformers.utils import is_torch_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import MaskFormerSwinBackbone from transformers.models.maskformer import MaskFormerSwinModel class _UpperCAmelCase : def __init__( self : Dict , _lowercase : int , _lowercase : List[str]=13 , _lowercase : Dict=32 , _lowercase : Any=2 , _lowercase : Optional[int]=3 , _lowercase : Optional[Any]=16 , _lowercase : Optional[int]=[1, 2, 1] , _lowercase : int=[2, 2, 4] , _lowercase : Optional[Any]=2 , _lowercase : Union[str, Any]=2.0 , _lowercase : Any=True , _lowercase : Optional[Any]=0.0 , _lowercase : Dict=0.0 , _lowercase : Dict=0.1 , _lowercase : str="gelu" , _lowercase : List[Any]=False , _lowercase : List[Any]=True , _lowercase : Optional[Any]=0.02 , _lowercase : str=1E-5 , _lowercase : str=True , _lowercase : Any=None , _lowercase : Tuple=True , _lowercase : Any=10 , _lowercase : int=8 , _lowercase : Optional[Any]=["stage1", "stage2", "stage3"] , _lowercase : Optional[Any]=[1, 2, 3] , ): __UpperCAmelCase = parent __UpperCAmelCase = batch_size __UpperCAmelCase = image_size __UpperCAmelCase = patch_size __UpperCAmelCase = num_channels __UpperCAmelCase = embed_dim __UpperCAmelCase = depths __UpperCAmelCase = num_heads __UpperCAmelCase = window_size __UpperCAmelCase = mlp_ratio __UpperCAmelCase = qkv_bias __UpperCAmelCase = hidden_dropout_prob __UpperCAmelCase = attention_probs_dropout_prob __UpperCAmelCase = drop_path_rate __UpperCAmelCase = hidden_act __UpperCAmelCase = use_absolute_embeddings __UpperCAmelCase = patch_norm __UpperCAmelCase = layer_norm_eps __UpperCAmelCase = initializer_range __UpperCAmelCase = is_training __UpperCAmelCase = scope __UpperCAmelCase = use_labels __UpperCAmelCase = type_sequence_label_size __UpperCAmelCase = encoder_stride __UpperCAmelCase = out_features __UpperCAmelCase = out_indices def a ( self : int ): __UpperCAmelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __UpperCAmelCase = None if self.use_labels: __UpperCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __UpperCAmelCase = self.get_config() return config, pixel_values, labels def a ( self : Dict ): return MaskFormerSwinConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , out_features=self.out_features , out_indices=self.out_indices , ) def a ( self : List[Any] , _lowercase : Union[str, Any] , _lowercase : str , _lowercase : int ): __UpperCAmelCase = MaskFormerSwinModel(config=_lowercase ) model.to(_lowercase ) model.eval() __UpperCAmelCase = model(_lowercase ) __UpperCAmelCase = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) __UpperCAmelCase = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def a ( self : int , _lowercase : Optional[Any] , _lowercase : Any , _lowercase : Dict ): __UpperCAmelCase = MaskFormerSwinBackbone(config=_lowercase ) model.to(_lowercase ) model.eval() __UpperCAmelCase = model(_lowercase ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [13, 16, 16, 16] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , [16, 32, 64] ) # verify ValueError with self.parent.assertRaises(_lowercase ): __UpperCAmelCase = ['''stem'''] __UpperCAmelCase = MaskFormerSwinBackbone(config=_lowercase ) def a ( self : Optional[int] ): __UpperCAmelCase = self.prepare_config_and_inputs() __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = config_and_inputs __UpperCAmelCase = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class _UpperCAmelCase ( _lowerCAmelCase , _lowerCAmelCase , unittest.TestCase ): a__ : List[Any] = ( ( MaskFormerSwinModel, MaskFormerSwinBackbone, ) if is_torch_available() else () ) a__ : Optional[int] = {"feature-extraction": MaskFormerSwinModel} if is_torch_available() else {} a__ : List[str] = False a__ : int = False a__ : str = False a__ : str = False a__ : Any = False def a ( self : Optional[Any] ): __UpperCAmelCase = MaskFormerSwinModelTester(self ) __UpperCAmelCase = ConfigTester(self , config_class=_lowercase , embed_dim=37 ) @require_torch_multi_gpu @unittest.skip( reason=( '''`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn\'t work well with''' ''' `nn.DataParallel`''' ) ) def a ( self : int ): pass def a ( self : Dict ): self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def a ( self : str ): return def a ( self : Optional[Any] ): __UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_lowercase ) def a ( self : Optional[int] ): __UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*_lowercase ) @unittest.skip('''Swin does not use inputs_embeds''' ) def a ( self : List[Any] ): pass @unittest.skip('''Swin does not support feedforward chunking''' ) def a ( self : str ): pass def a ( self : Union[str, Any] ): __UpperCAmelCase , __UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __UpperCAmelCase = model_class(_lowercase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) __UpperCAmelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_lowercase , nn.Linear ) ) def a ( self : Union[str, Any] ): __UpperCAmelCase , __UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __UpperCAmelCase = model_class(_lowercase ) __UpperCAmelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __UpperCAmelCase = [*signature.parameters.keys()] __UpperCAmelCase = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _lowercase ) @unittest.skip(reason='''MaskFormerSwin is only used as backbone and doesn\'t support output_attentions''' ) def a ( self : Optional[Any] ): pass @unittest.skip(reason='''MaskFormerSwin is only used as an internal backbone''' ) def a ( self : Optional[Any] ): pass def a ( self : List[Any] , _lowercase : Union[str, Any] , _lowercase : List[str] , _lowercase : Dict , _lowercase : Tuple ): __UpperCAmelCase = model_class(_lowercase ) model.to(_lowercase ) model.eval() with torch.no_grad(): __UpperCAmelCase = model(**self._prepare_for_class(_lowercase , _lowercase ) ) __UpperCAmelCase = outputs.hidden_states __UpperCAmelCase = getattr( self.model_tester , '''expected_num_hidden_layers''' , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(_lowercase ) , _lowercase ) # Swin has a different seq_length __UpperCAmelCase = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) __UpperCAmelCase = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def a ( self : str ): __UpperCAmelCase , __UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() __UpperCAmelCase = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: __UpperCAmelCase = True self.check_hidden_states_output(_lowercase , _lowercase , _lowercase , _lowercase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __UpperCAmelCase = True self.check_hidden_states_output(_lowercase , _lowercase , _lowercase , _lowercase ) def a ( self : Optional[Any] ): __UpperCAmelCase , __UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() __UpperCAmelCase = 3 __UpperCAmelCase = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) __UpperCAmelCase = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) __UpperCAmelCase = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) __UpperCAmelCase = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: __UpperCAmelCase = True self.check_hidden_states_output(_lowercase , _lowercase , _lowercase , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __UpperCAmelCase = True self.check_hidden_states_output(_lowercase , _lowercase , _lowercase , (padded_height, padded_width) ) @unittest.skip(reason='''MaskFormerSwin doesn\'t have pretrained checkpoints''' ) def a ( self : Any ): pass @unittest.skip(reason='''This will be fixed once MaskFormerSwin is replaced by native Swin''' ) def a ( self : str ): pass @unittest.skip(reason='''This will be fixed once MaskFormerSwin is replaced by native Swin''' ) def a ( self : Tuple ): pass def a ( self : Tuple ): __UpperCAmelCase , __UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() def set_nan_tensor_to_zero(_lowercase : List[str] ): __UpperCAmelCase = 0 return t def check_equivalence(_lowercase : List[Any] , _lowercase : Any , _lowercase : str , _lowercase : List[str]={} ): with torch.no_grad(): __UpperCAmelCase = model(**_lowercase , return_dict=_lowercase , **_lowercase ) __UpperCAmelCase = model(**_lowercase , return_dict=_lowercase , **_lowercase ).to_tuple() def recursive_check(_lowercase : Dict , _lowercase : Optional[Any] ): if isinstance(_lowercase , (List, Tuple) ): for tuple_iterable_value, dict_iterable_value in zip(_lowercase , _lowercase ): recursive_check(_lowercase , _lowercase ) elif isinstance(_lowercase , _lowercase ): for tuple_iterable_value, dict_iterable_value in zip( tuple_object.values() , dict_object.values() ): recursive_check(_lowercase , _lowercase ) elif tuple_object is None: return else: self.assertTrue( torch.allclose( set_nan_tensor_to_zero(_lowercase ) , set_nan_tensor_to_zero(_lowercase ) , atol=1E-5 ) , msg=( '''Tuple and dict output are not equal. Difference:''' F''' {torch.max(torch.abs(tuple_object - dict_object ) )}. Tuple has `nan`:''' F''' {torch.isnan(_lowercase ).any()} and `inf`: {torch.isinf(_lowercase )}. Dict has''' F''' `nan`: {torch.isnan(_lowercase ).any()} and `inf`: {torch.isinf(_lowercase )}.''' ) , ) recursive_check(_lowercase , _lowercase ) for model_class in self.all_model_classes: __UpperCAmelCase = model_class(_lowercase ) model.to(_lowercase ) model.eval() __UpperCAmelCase = self._prepare_for_class(_lowercase , _lowercase ) __UpperCAmelCase = self._prepare_for_class(_lowercase , _lowercase ) check_equivalence(_lowercase , _lowercase , _lowercase ) __UpperCAmelCase = self._prepare_for_class(_lowercase , _lowercase , return_labels=_lowercase ) __UpperCAmelCase = self._prepare_for_class(_lowercase , _lowercase , return_labels=_lowercase ) check_equivalence(_lowercase , _lowercase , _lowercase ) __UpperCAmelCase = self._prepare_for_class(_lowercase , _lowercase ) __UpperCAmelCase = self._prepare_for_class(_lowercase , _lowercase ) check_equivalence(_lowercase , _lowercase , _lowercase , {'''output_hidden_states''': True} ) __UpperCAmelCase = self._prepare_for_class(_lowercase , _lowercase , return_labels=_lowercase ) __UpperCAmelCase = self._prepare_for_class(_lowercase , _lowercase , return_labels=_lowercase ) check_equivalence(_lowercase , _lowercase , _lowercase , {'''output_hidden_states''': True} ) @require_torch class _UpperCAmelCase ( unittest.TestCase , _lowerCAmelCase ): a__ : Optional[Any] = (MaskFormerSwinBackbone,) if is_torch_available() else () a__ : List[str] = MaskFormerSwinConfig def a ( self : List[str] ): __UpperCAmelCase = MaskFormerSwinModelTester(self ) def a ( self : List[Any] ): __UpperCAmelCase , __UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() __UpperCAmelCase = inputs_dict['''pixel_values'''].shape[0] for backbone_class in self.all_model_classes: __UpperCAmelCase = backbone_class(_lowercase ) backbone.to(_lowercase ) backbone.eval() __UpperCAmelCase = backbone(**_lowercase ) # Test default outputs and verify feature maps self.assertIsInstance(outputs.feature_maps , _lowercase ) self.assertTrue(len(outputs.feature_maps ) == len(backbone.channels ) ) for feature_map, n_channels in zip(outputs.feature_maps , backbone.channels ): self.assertTrue(feature_map.shape[:2] , (batch_size, n_channels) ) self.assertIsNone(outputs.hidden_states ) self.assertIsNone(outputs.attentions ) # Test output_hidden_states=True __UpperCAmelCase = backbone(**_lowercase , output_hidden_states=_lowercase ) self.assertIsNotNone(outputs.hidden_states ) self.assertTrue(len(outputs.hidden_states ) , len(backbone.stage_names ) ) # We skip the stem layer for hidden_states, n_channels in zip(outputs.hidden_states[1:] , backbone.channels ): for hidden_state in hidden_states: # Hidden states are in the format (batch_size, (height * width), n_channels) __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = hidden_state.shape self.assertTrue((h_batch_size, h_n_channels) , (batch_size, n_channels) ) # Test output_attentions=True if self.has_attentions: __UpperCAmelCase = backbone(**_lowercase , output_attentions=_lowercase ) self.assertIsNotNone(outputs.attentions )
86
1
import glob import os import random from string import ascii_lowercase, digits import cva import numpy as np # Parrameters __A = (7_20, 12_80) # Height, Width __A = (0.4, 0.6) # if height or width lower than this scale, drop it. __A = 1 / 1_00 __A = "" __A = "" __A = "" __A = 2_50 def lowerCamelCase_ ( ) -> None: """simple docstring""" __lowerCamelCase , __lowerCamelCase = get_dataset(UpperCamelCase__ , UpperCamelCase__ ) for index in range(UpperCamelCase__ ): __lowerCamelCase = random.sample(range(len(UpperCamelCase__ ) ) , 4 ) __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = update_image_and_anno( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , filter_scale=UpperCamelCase__ , ) # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' __lowerCamelCase = random_chars(32 ) __lowerCamelCase = path.split(os.sep )[-1].rsplit('.' , 1 )[0] __lowerCamelCase = F"""{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}""" cva.imwrite(F"""{file_root}.jpg""" , UpperCamelCase__ , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(F"""Succeeded {index+1}/{NUMBER_IMAGES} with {file_name}""" ) __lowerCamelCase = [] for anno in new_annos: __lowerCamelCase = anno[3] - anno[1] __lowerCamelCase = anno[4] - anno[2] __lowerCamelCase = anno[1] + width / 2 __lowerCamelCase = anno[2] + height / 2 __lowerCamelCase = F"""{anno[0]} {x_center} {y_center} {width} {height}""" annos_list.append(UpperCamelCase__ ) with open(F"""{file_root}.txt""" , 'w' ) as outfile: outfile.write('\n'.join(line for line in annos_list ) ) def lowerCamelCase_ ( UpperCamelCase__ : str , UpperCamelCase__ : str ) -> tuple[list, list]: """simple docstring""" __lowerCamelCase = [] __lowerCamelCase = [] for label_file in glob.glob(os.path.join(UpperCamelCase__ , '*.txt' ) ): __lowerCamelCase = label_file.split(os.sep )[-1].rsplit('.' , 1 )[0] with open(UpperCamelCase__ ) as in_file: __lowerCamelCase = in_file.readlines() __lowerCamelCase = os.path.join(UpperCamelCase__ , F"""{label_name}.jpg""" ) __lowerCamelCase = [] for obj_list in obj_lists: __lowerCamelCase = obj_list.rstrip('\n' ).split(' ' ) __lowerCamelCase = float(obj[1] ) - float(obj[3] ) / 2 __lowerCamelCase = float(obj[2] ) - float(obj[4] ) / 2 __lowerCamelCase = float(obj[1] ) + float(obj[3] ) / 2 __lowerCamelCase = float(obj[2] ) + float(obj[4] ) / 2 boxes.append([int(obj[0] ), xmin, ymin, xmax, ymax] ) if not boxes: continue img_paths.append(UpperCamelCase__ ) labels.append(UpperCamelCase__ ) return img_paths, labels def lowerCamelCase_ ( UpperCamelCase__ : list , UpperCamelCase__ : list , UpperCamelCase__ : list[int] , UpperCamelCase__ : tuple[int, int] , UpperCamelCase__ : tuple[float, float] , UpperCamelCase__ : float = 0.0 , ) -> tuple[list, list, str]: """simple docstring""" __lowerCamelCase = np.zeros([output_size[0], output_size[1], 3] , dtype=np.uinta ) __lowerCamelCase = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) __lowerCamelCase = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) __lowerCamelCase = int(scale_x * output_size[1] ) __lowerCamelCase = int(scale_y * output_size[0] ) __lowerCamelCase = [] __lowerCamelCase = [] for i, index in enumerate(UpperCamelCase__ ): __lowerCamelCase = all_img_list[index] path_list.append(UpperCamelCase__ ) __lowerCamelCase = all_annos[index] __lowerCamelCase = cva.imread(UpperCamelCase__ ) if i == 0: # top-left __lowerCamelCase = cva.resize(UpperCamelCase__ , (divid_point_x, divid_point_y) ) __lowerCamelCase = img for bbox in img_annos: __lowerCamelCase = bbox[1] * scale_x __lowerCamelCase = bbox[2] * scale_y __lowerCamelCase = bbox[3] * scale_x __lowerCamelCase = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) elif i == 1: # top-right __lowerCamelCase = cva.resize(UpperCamelCase__ , (output_size[1] - divid_point_x, divid_point_y) ) __lowerCamelCase = img for bbox in img_annos: __lowerCamelCase = scale_x + bbox[1] * (1 - scale_x) __lowerCamelCase = bbox[2] * scale_y __lowerCamelCase = scale_x + bbox[3] * (1 - scale_x) __lowerCamelCase = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) elif i == 2: # bottom-left __lowerCamelCase = cva.resize(UpperCamelCase__ , (divid_point_x, output_size[0] - divid_point_y) ) __lowerCamelCase = img for bbox in img_annos: __lowerCamelCase = bbox[1] * scale_x __lowerCamelCase = scale_y + bbox[2] * (1 - scale_y) __lowerCamelCase = bbox[3] * scale_x __lowerCamelCase = scale_y + bbox[4] * (1 - scale_y) new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) else: # bottom-right __lowerCamelCase = cva.resize( UpperCamelCase__ , (output_size[1] - divid_point_x, output_size[0] - divid_point_y) ) __lowerCamelCase = img for bbox in img_annos: __lowerCamelCase = scale_x + bbox[1] * (1 - scale_x) __lowerCamelCase = scale_y + bbox[2] * (1 - scale_y) __lowerCamelCase = scale_x + bbox[3] * (1 - scale_x) __lowerCamelCase = scale_y + bbox[4] * (1 - scale_y) new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) # Remove bounding box small than scale of filter if filter_scale > 0: __lowerCamelCase = [ anno for anno in new_anno if filter_scale < (anno[3] - anno[1]) and filter_scale < (anno[4] - anno[2]) ] return output_img, new_anno, path_list[0] def lowerCamelCase_ ( UpperCamelCase__ : int ) -> str: """simple docstring""" assert number_char > 1, "The number of character should greater than 1" __lowerCamelCase = ascii_lowercase + digits return "".join(random.choice(UpperCamelCase__ ) for _ in range(UpperCamelCase__ ) ) if __name__ == "__main__": main() print("DONE ✅")
90
"""simple docstring""" def UpperCamelCase_ ( lowerCAmelCase__ : Optional[Any] ) -> Optional[int]: """simple docstring""" lowerCAmelCase_ : Tuple = [0] * len(lowerCAmelCase__ ) lowerCAmelCase_ : List[str] = [] lowerCAmelCase_ : Tuple = [1] * len(lowerCAmelCase__ ) for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(lowerCAmelCase__ ) ): if indegree[i] == 0: queue.append(lowerCAmelCase__ ) while queue: lowerCAmelCase_ : Union[str, Any] = queue.pop(0 ) for x in graph[vertex]: indegree[x] -= 1 if long_dist[vertex] + 1 > long_dist[x]: lowerCAmelCase_ : Any = long_dist[vertex] + 1 if indegree[x] == 0: queue.append(lowerCAmelCase__ ) print(max(lowerCAmelCase__ ) ) # Adjacency list of Graph lowercase__ : Any = {0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []} longest_distance(graph)
224
0
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING a_ = logging.get_logger(__name__) a_ = { """microsoft/table-transformer-detection""": ( """https://huggingface.co/microsoft/table-transformer-detection/resolve/main/config.json""" ), } class snake_case ( lowerCAmelCase__): __UpperCamelCase = "table-transformer" __UpperCamelCase = ["past_key_values"] __UpperCamelCase = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self : Tuple , a__ : Optional[int]=True , a__ : Optional[int]=None , a__ : Optional[Any]=3 , a__ : int=1_00 , a__ : Dict=6 , a__ : Any=20_48 , a__ : Optional[int]=8 , a__ : Optional[Any]=6 , a__ : Dict=20_48 , a__ : Any=8 , a__ : List[str]=0.0 , a__ : Dict=0.0 , a__ : Tuple=True , a__ : int="relu" , a__ : int=2_56 , a__ : Any=0.1 , a__ : List[str]=0.0 , a__ : List[Any]=0.0 , a__ : Optional[int]=0.0_2 , a__ : List[Any]=1.0 , a__ : List[str]=False , a__ : Any="sine" , a__ : Optional[int]="resnet50" , a__ : int=True , a__ : List[str]=False , a__ : Dict=1 , a__ : str=5 , a__ : Union[str, Any]=2 , a__ : Union[str, Any]=1 , a__ : Optional[int]=1 , a__ : Optional[Any]=5 , a__ : Dict=2 , a__ : Union[str, Any]=0.1 , **a__ : Tuple , ) -> int: '''simple docstring''' if backbone_config is not None and use_timm_backbone: raise ValueError("You can\'t specify both `backbone_config` and `use_timm_backbone`." ) if not use_timm_backbone: if backbone_config is None: logger.info("`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone." ) _A = CONFIG_MAPPING["resnet"](out_features=["stage4"] ) elif isinstance(a__ , a__ ): _A = backbone_config.get("model_type" ) _A = CONFIG_MAPPING[backbone_model_type] _A = config_class.from_dict(a__ ) # set timm attributes to None _A , _A , _A = None, None, None _A = use_timm_backbone _A = backbone_config _A = num_channels _A = num_queries _A = d_model _A = encoder_ffn_dim _A = encoder_layers _A = encoder_attention_heads _A = decoder_ffn_dim _A = decoder_layers _A = decoder_attention_heads _A = dropout _A = attention_dropout _A = activation_dropout _A = activation_function _A = init_std _A = init_xavier_std _A = encoder_layerdrop _A = decoder_layerdrop _A = encoder_layers _A = auxiliary_loss _A = position_embedding_type _A = backbone _A = use_pretrained_backbone _A = dilation # Hungarian matcher _A = class_cost _A = bbox_cost _A = giou_cost # Loss coefficients _A = mask_loss_coefficient _A = dice_loss_coefficient _A = bbox_loss_coefficient _A = giou_loss_coefficient _A = eos_coefficient super().__init__(is_encoder_decoder=a__ , **a__ ) @property def a_ ( self : Optional[int] ) -> Tuple: '''simple docstring''' return self.encoder_attention_heads @property def a_ ( self : Union[str, Any] ) -> List[str]: '''simple docstring''' return self.d_model class snake_case ( lowerCAmelCase__): __UpperCamelCase = version.parse('1.11') @property def a_ ( self : List[str] ) -> Any: '''simple docstring''' return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ("pixel_mask", {0: "batch"}), ] ) @property def a_ ( self : Any ) -> str: '''simple docstring''' return 1E-5 @property def a_ ( self : str ) -> Dict: '''simple docstring''' return 12
364
"""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 a__ ( __lowercase ) -> List[Any]: _A = 384 if "tiny" in model_name: _A = [3, 3, 9, 3] _A = [96, 192, 384, 768] if "small" in model_name: _A = [3, 3, 27, 3] _A = [96, 192, 384, 768] if "base" in model_name: _A = [3, 3, 27, 3] _A = [128, 256, 512, 1024] _A = 512 if "large" in model_name: _A = [3, 3, 27, 3] _A = [192, 384, 768, 1536] _A = 768 if "xlarge" in model_name: _A = [3, 3, 27, 3] _A = [256, 512, 1024, 2048] _A = 1024 # set label information _A = 150 _A = "huggingface/label-files" _A = "ade20k-id2label.json" _A = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type="dataset" ) , "r" ) ) _A = {int(__lowercase ): v for k, v in idalabel.items()} _A = {v: k for k, v in idalabel.items()} _A = ConvNextConfig( depths=__lowercase , hidden_sizes=__lowercase , out_features=["stage1", "stage2", "stage3", "stage4"] ) _A = UperNetConfig( backbone_config=__lowercase , auxiliary_in_channels=__lowercase , num_labels=__lowercase , idalabel=__lowercase , labelaid=__lowercase , ) return config def a__ ( __lowercase ) -> List[Any]: _A = [] # 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 a__ ( __lowercase , __lowercase , __lowercase ) -> List[Any]: _A = dct.pop(__lowercase ) _A = val def a__ ( __lowercase , __lowercase , __lowercase ) -> Optional[Any]: _A = { "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", } _A = model_name_to_url[model_name] _A = torch.hub.load_state_dict_from_url(__lowercase , map_location="cpu" )["state_dict"] _A = get_upernet_config(__lowercase ) _A = UperNetForSemanticSegmentation(__lowercase ) model.eval() # replace "bn" => "batch_norm" for key in state_dict.copy().keys(): _A = state_dict.pop(__lowercase ) if "bn" in key: _A = key.replace("bn" , "batch_norm" ) _A = val # rename keys _A = create_rename_keys(__lowercase ) for src, dest in rename_keys: rename_key(__lowercase , __lowercase , __lowercase ) model.load_state_dict(__lowercase ) # verify on image _A = "https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg" _A = Image.open(requests.get(__lowercase , stream=__lowercase ).raw ).convert("RGB" ) _A = SegformerImageProcessor() _A = processor(__lowercase , return_tensors="pt" ).pixel_values with torch.no_grad(): _A = model(__lowercase ) if model_name == "upernet-convnext-tiny": _A = torch.tensor( [[-8.8_110, -8.8_110, -8.6_521], [-8.8_110, -8.8_110, -8.6_521], [-8.7_746, -8.7_746, -8.6_130]] ) elif model_name == "upernet-convnext-small": _A = torch.tensor( [[-8.8_236, -8.8_236, -8.6_771], [-8.8_236, -8.8_236, -8.6_771], [-8.7_638, -8.7_638, -8.6_240]] ) elif model_name == "upernet-convnext-base": _A = torch.tensor( [[-8.8_558, -8.8_558, -8.6_905], [-8.8_558, -8.8_558, -8.6_905], [-8.7_669, -8.7_669, -8.6_021]] ) elif model_name == "upernet-convnext-large": _A = torch.tensor( [[-8.6_660, -8.6_660, -8.6_210], [-8.6_660, -8.6_660, -8.6_210], [-8.6_310, -8.6_310, -8.5_964]] ) elif model_name == "upernet-convnext-xlarge": _A = torch.tensor( [[-8.4_980, -8.4_980, -8.3_977], [-8.4_980, -8.4_980, -8.3_977], [-8.4_379, -8.4_379, -8.3_412]] ) print("Logits:" , outputs.logits[0, 0, :3, :3] ) assert torch.allclose(outputs.logits[0, 0, :3, :3] , __lowercase , 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(__lowercase ) print(f"""Saving processor to {pytorch_dump_folder_path}""" ) processor.save_pretrained(__lowercase ) 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_ = 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_ = parser.parse_args() convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
163
0
'''simple docstring''' import inspect import tempfile import unittest from huggingface_hub import hf_hub_download from transformers import is_torch_available from transformers.testing_utils import is_flaky, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin A_ = 1e-4 if is_torch_available(): import torch from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder @require_torch class _snake_case : def __init__( self : str ,SCREAMING_SNAKE_CASE__ : List[Any] ,SCREAMING_SNAKE_CASE__ : Tuple=16 ,SCREAMING_SNAKE_CASE__ : Dict=13 ,SCREAMING_SNAKE_CASE__ : Tuple=7 ,SCREAMING_SNAKE_CASE__ : Optional[int]=14 ,SCREAMING_SNAKE_CASE__ : List[str]=10 ,SCREAMING_SNAKE_CASE__ : str=19 ,SCREAMING_SNAKE_CASE__ : Union[str, Any]=5 ,SCREAMING_SNAKE_CASE__ : Union[str, Any]=4 ,SCREAMING_SNAKE_CASE__ : Optional[int]=True ,SCREAMING_SNAKE_CASE__ : Tuple=16 ,SCREAMING_SNAKE_CASE__ : List[str]=2 ,SCREAMING_SNAKE_CASE__ : List[Any]=4 ,SCREAMING_SNAKE_CASE__ : Dict=4 ,SCREAMING_SNAKE_CASE__ : List[str]="gelu" ,SCREAMING_SNAKE_CASE__ : Optional[Any]=0.1 ,SCREAMING_SNAKE_CASE__ : Tuple=0.1 ,SCREAMING_SNAKE_CASE__ : int=[1, 2, 3, 4, 5] ,SCREAMING_SNAKE_CASE__ : Union[str, Any]=25 ,SCREAMING_SNAKE_CASE__ : Any=5 ,): SCREAMING_SNAKE_CASE:Optional[int] = d_model SCREAMING_SNAKE_CASE:List[Any] = parent SCREAMING_SNAKE_CASE:str = batch_size SCREAMING_SNAKE_CASE:Union[str, Any] = prediction_length SCREAMING_SNAKE_CASE:str = context_length SCREAMING_SNAKE_CASE:List[Any] = cardinality SCREAMING_SNAKE_CASE:Dict = num_time_features SCREAMING_SNAKE_CASE:Optional[int] = lags_sequence SCREAMING_SNAKE_CASE:Optional[Any] = embedding_dimension SCREAMING_SNAKE_CASE:Union[str, Any] = is_training SCREAMING_SNAKE_CASE:Union[str, Any] = hidden_size SCREAMING_SNAKE_CASE:Optional[int] = num_hidden_layers SCREAMING_SNAKE_CASE:Union[str, Any] = num_attention_heads SCREAMING_SNAKE_CASE:Union[str, Any] = intermediate_size SCREAMING_SNAKE_CASE:str = hidden_act SCREAMING_SNAKE_CASE:str = hidden_dropout_prob SCREAMING_SNAKE_CASE:Optional[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE:List[Any] = context_length SCREAMING_SNAKE_CASE:Optional[Any] = prediction_length + label_length SCREAMING_SNAKE_CASE:Any = label_length SCREAMING_SNAKE_CASE:Tuple = moving_average SCREAMING_SNAKE_CASE:int = autocorrelation_factor def __UpperCamelCase ( self : str ): return AutoformerConfig( d_model=self.d_model ,encoder_layers=self.num_hidden_layers ,decoder_layers=self.num_hidden_layers ,encoder_attention_heads=self.num_attention_heads ,decoder_attention_heads=self.num_attention_heads ,encoder_ffn_dim=self.intermediate_size ,decoder_ffn_dim=self.intermediate_size ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,prediction_length=self.prediction_length ,context_length=self.context_length ,label_length=self.label_length ,lags_sequence=self.lags_sequence ,num_time_features=self.num_time_features ,num_static_categorical_features=1 ,cardinality=[self.cardinality] ,embedding_dimension=[self.embedding_dimension] ,moving_average=self.moving_average ,) def __UpperCamelCase ( self : Optional[int] ,SCREAMING_SNAKE_CASE__ : Optional[Any] ): SCREAMING_SNAKE_CASE:Optional[int] = config.context_length + max(config.lags_sequence ) SCREAMING_SNAKE_CASE:Optional[Any] = ids_tensor([self.batch_size, 1] ,config.cardinality[0] ) SCREAMING_SNAKE_CASE:List[str] = floats_tensor([self.batch_size, _past_length, config.num_time_features] ) SCREAMING_SNAKE_CASE:Dict = floats_tensor([self.batch_size, _past_length] ) SCREAMING_SNAKE_CASE:Any = floats_tensor([self.batch_size, _past_length] ) > 0.5 # decoder inputs SCREAMING_SNAKE_CASE:Union[str, Any] = floats_tensor([self.batch_size, config.prediction_length, config.num_time_features] ) SCREAMING_SNAKE_CASE:str = floats_tensor([self.batch_size, config.prediction_length] ) SCREAMING_SNAKE_CASE:Optional[int] = { "past_values": past_values, "static_categorical_features": static_categorical_features, "past_time_features": past_time_features, "past_observed_mask": past_observed_mask, "future_time_features": future_time_features, "future_values": future_values, } return inputs_dict def __UpperCamelCase ( self : List[Any] ): SCREAMING_SNAKE_CASE:Optional[int] = self.get_config() SCREAMING_SNAKE_CASE:List[str] = self.prepare_autoformer_inputs_dict(SCREAMING_SNAKE_CASE__ ) return config, inputs_dict def __UpperCamelCase ( self : Optional[int] ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE:Optional[int] = self.prepare_config_and_inputs() return config, inputs_dict def __UpperCamelCase ( self : Optional[Any] ,SCREAMING_SNAKE_CASE__ : Any ,SCREAMING_SNAKE_CASE__ : str ): SCREAMING_SNAKE_CASE:str = AutoformerModel(config=SCREAMING_SNAKE_CASE__ ).to(SCREAMING_SNAKE_CASE__ ).eval() SCREAMING_SNAKE_CASE:Tuple = model(**SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:Union[str, Any] = outputs.encoder_last_hidden_state SCREAMING_SNAKE_CASE:Optional[int] = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE:Optional[Any] = model.get_encoder() encoder.save_pretrained(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:str = AutoformerEncoder.from_pretrained(SCREAMING_SNAKE_CASE__ ).to(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE:Tuple = model.create_network_inputs(**SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE:Union[str, Any] = model.decomposition_layer(transformer_inputs[:, : config.context_length, ...] ) SCREAMING_SNAKE_CASE:Dict = torch.cat( (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...]) ,dim=-1 ,) SCREAMING_SNAKE_CASE:str = encoder(inputs_embeds=SCREAMING_SNAKE_CASE__ )[0] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1e-3 ) SCREAMING_SNAKE_CASE:List[str] = ( torch.mean(transformer_inputs[:, : config.context_length, ...] ,dim=1 ) .unsqueeze(1 ) .repeat(1 ,config.prediction_length ,1 ) ) SCREAMING_SNAKE_CASE:Optional[Any] = torch.zeros( [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]] ,device=enc_input.device ,) SCREAMING_SNAKE_CASE:int = torch.cat( ( torch.cat((seasonal_input[:, -config.label_length :, ...], zeros) ,dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) ,dim=-1 ,) SCREAMING_SNAKE_CASE:Tuple = torch.cat( ( torch.cat((trend_input[:, -config.label_length :, ...], mean) ,dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) ,dim=-1 ,) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE:List[Any] = model.get_decoder() decoder.save_pretrained(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:List[Any] = AutoformerDecoder.from_pretrained(SCREAMING_SNAKE_CASE__ ).to(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:Optional[int] = decoder( trend=SCREAMING_SNAKE_CASE__ ,inputs_embeds=SCREAMING_SNAKE_CASE__ ,encoder_hidden_states=SCREAMING_SNAKE_CASE__ ,)[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1e-3 ) @require_torch class _snake_case ( _a , _a , unittest.TestCase ): _A : Dict = (AutoformerModel, AutoformerForPrediction) if is_torch_available() else () _A : Optional[Any] = (AutoformerForPrediction,) if is_torch_available() else () _A : Any = {'feature-extraction': AutoformerModel} if is_torch_available() else {} _A : Dict = False _A : Any = False _A : Optional[Any] = False _A : Dict = False _A : int = False _A : int = False def __UpperCamelCase ( self : Any ): SCREAMING_SNAKE_CASE:Union[str, Any] = AutoformerModelTester(self ) SCREAMING_SNAKE_CASE:Any = ConfigTester(self ,config_class=SCREAMING_SNAKE_CASE__ ,has_text_modality=SCREAMING_SNAKE_CASE__ ) def __UpperCamelCase ( self : str ): self.config_tester.run_common_tests() def __UpperCamelCase ( self : Any ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE:str = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE:Union[str, Any] = model_class(SCREAMING_SNAKE_CASE__ ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE:List[str] = model_class.from_pretrained(SCREAMING_SNAKE_CASE__ ,output_loading_info=SCREAMING_SNAKE_CASE__ ) self.assertEqual(info["missing_keys"] ,[] ) def __UpperCamelCase ( self : Any ): SCREAMING_SNAKE_CASE:List[str] = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*SCREAMING_SNAKE_CASE__ ) @unittest.skip(reason="Model has no tokens embeddings" ) def __UpperCamelCase ( self : Dict ): pass def __UpperCamelCase ( self : str ): SCREAMING_SNAKE_CASE:Optional[Any] = inspect.signature(getattr(SCREAMING_SNAKE_CASE__ ,"forward" ) ) # The main input is the name of the argument after `self` SCREAMING_SNAKE_CASE:List[str] = list(model_signature.parameters.keys() )[1] self.assertEqual(AutoformerModel.main_input_name ,SCREAMING_SNAKE_CASE__ ) def __UpperCamelCase ( self : List[str] ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE:Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE:Tuple = model_class(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:List[Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE:List[str] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE:Dict = [ "past_values", "past_time_features", "past_observed_mask", "static_categorical_features", "static_real_features", "future_values", "future_time_features", ] if model.__class__.__name__ in ["AutoformerForPrediction"]: expected_arg_names.append("future_observed_mask" ) expected_arg_names.extend( [ "decoder_attention_mask", "head_mask", "decoder_head_mask", "cross_attn_head_mask", "encoder_outputs", "past_key_values", "output_hidden_states", "output_attentions", "use_cache", "return_dict", ] ) self.assertListEqual(arg_names[: len(SCREAMING_SNAKE_CASE__ )] ,SCREAMING_SNAKE_CASE__ ) def __UpperCamelCase ( self : List[Any] ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE:Tuple = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE:List[str] = True SCREAMING_SNAKE_CASE:Optional[Any] = getattr(self.model_tester ,"seq_length" ,SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:Optional[int] = getattr(self.model_tester ,"decoder_seq_length" ,SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:Dict = getattr(self.model_tester ,"encoder_seq_length" ,SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:int = getattr(self.model_tester ,"d_model" ,SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:List[Any] = getattr(self.model_tester ,"num_attention_heads" ,SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:Optional[Any] = d_model // num_attention_heads for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE:Any = True SCREAMING_SNAKE_CASE:Dict = False SCREAMING_SNAKE_CASE:Any = True SCREAMING_SNAKE_CASE:Any = model_class(SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE:Optional[int] = model(**self._prepare_for_class(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ) ) SCREAMING_SNAKE_CASE:Dict = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) ,self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] SCREAMING_SNAKE_CASE:int = True SCREAMING_SNAKE_CASE:Tuple = model_class(SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE:Any = model(**self._prepare_for_class(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ) ) SCREAMING_SNAKE_CASE:List[str] = outputs.encoder_attentions self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) ,self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) ,[self.model_tester.num_attention_heads, encoder_seq_length, dim] ,) SCREAMING_SNAKE_CASE:Optional[int] = len(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:Optional[int] = 7 if "last_hidden_state" in outputs: correct_outlen += 1 if "trend" in outputs: correct_outlen += 1 if "past_key_values" in outputs: correct_outlen += 1 # past_key_values have been returned if "loss" in outputs: correct_outlen += 1 if "params" in outputs: correct_outlen += 1 self.assertEqual(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ) # decoder attentions SCREAMING_SNAKE_CASE:str = outputs.decoder_attentions self.assertIsInstance(SCREAMING_SNAKE_CASE__ ,(list, tuple) ) self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) ,self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) ,[self.model_tester.num_attention_heads, decoder_seq_length, dim] ,) # cross attentions SCREAMING_SNAKE_CASE:Dict = outputs.cross_attentions self.assertIsInstance(SCREAMING_SNAKE_CASE__ ,(list, tuple) ) self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) ,self.model_tester.num_hidden_layers ) self.assertListEqual( list(cross_attentions[0].shape[-3:] ) ,[self.model_tester.num_attention_heads, decoder_seq_length, dim] ,) # Check attention is always last and order is fine SCREAMING_SNAKE_CASE:Dict = True SCREAMING_SNAKE_CASE:Dict = True SCREAMING_SNAKE_CASE:List[str] = model_class(SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE:Union[str, Any] = model(**self._prepare_for_class(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ) ) self.assertEqual(out_len + 2 ,len(SCREAMING_SNAKE_CASE__ ) ) SCREAMING_SNAKE_CASE:Any = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) ,self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) ,[self.model_tester.num_attention_heads, encoder_seq_length, dim] ,) @is_flaky() def __UpperCamelCase ( self : Union[str, Any] ): super().test_retain_grad_hidden_states_attentions() def A_ ( snake_case="train-batch.pt" ): SCREAMING_SNAKE_CASE:Dict = hf_hub_download(repo_id="hf-internal-testing/tourism-monthly-batch" , filename=__A , repo_type="dataset" ) SCREAMING_SNAKE_CASE:Optional[Any] = torch.load(__A , map_location=__A ) return batch @require_torch @slow class _snake_case ( unittest.TestCase ): def __UpperCamelCase ( self : int ): SCREAMING_SNAKE_CASE:Optional[Any] = AutoformerModel.from_pretrained("huggingface/autoformer-tourism-monthly" ).to(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:str = prepare_batch() with torch.no_grad(): SCREAMING_SNAKE_CASE:List[str] = model( past_values=batch["past_values"] ,past_time_features=batch["past_time_features"] ,past_observed_mask=batch["past_observed_mask"] ,static_categorical_features=batch["static_categorical_features"] ,future_values=batch["future_values"] ,future_time_features=batch["future_time_features"] ,)[0] SCREAMING_SNAKE_CASE:List[str] = torch.Size( (64, model.config.prediction_length + model.config.label_length, model.config.feature_size) ) self.assertEqual(output.shape ,SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:Dict = torch.tensor( [[0.3_593, -1.3_398, 0.6_330], [0.2_279, 1.5_396, -0.1_792], [0.0_450, 1.3_225, -0.2_335]] ,device=SCREAMING_SNAKE_CASE__ ) self.assertTrue(torch.allclose(output[0, :3, :3] ,SCREAMING_SNAKE_CASE__ ,atol=SCREAMING_SNAKE_CASE__ ) ) def __UpperCamelCase ( self : int ): SCREAMING_SNAKE_CASE:int = AutoformerForPrediction.from_pretrained("huggingface/autoformer-tourism-monthly" ).to(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:Tuple = prepare_batch("val-batch.pt" ) with torch.no_grad(): SCREAMING_SNAKE_CASE:Any = model( past_values=batch["past_values"] ,past_time_features=batch["past_time_features"] ,past_observed_mask=batch["past_observed_mask"] ,static_categorical_features=batch["static_categorical_features"] ,).encoder_last_hidden_state SCREAMING_SNAKE_CASE:Tuple = torch.Size((64, model.config.context_length, model.config.d_model) ) self.assertEqual(output.shape ,SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:Union[str, Any] = torch.tensor( [[-0.0_734, -0.9_036, 0.8_358], [4.7_186, 2.4_113, 1.9_581], [1.7_953, 2.3_558, 1.2_970]] ,device=SCREAMING_SNAKE_CASE__ ) self.assertTrue(torch.allclose(output[0, :3, :3] ,SCREAMING_SNAKE_CASE__ ,atol=SCREAMING_SNAKE_CASE__ ) ) def __UpperCamelCase ( self : List[str] ): SCREAMING_SNAKE_CASE:Union[str, Any] = AutoformerForPrediction.from_pretrained("huggingface/autoformer-tourism-monthly" ).to(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:str = prepare_batch("val-batch.pt" ) with torch.no_grad(): SCREAMING_SNAKE_CASE:Tuple = model.generate( static_categorical_features=batch["static_categorical_features"] ,past_time_features=batch["past_time_features"] ,past_values=batch["past_values"] ,future_time_features=batch["future_time_features"] ,past_observed_mask=batch["past_observed_mask"] ,) SCREAMING_SNAKE_CASE:Dict = torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length) ) self.assertEqual(outputs.sequences.shape ,SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:Dict = torch.tensor([3_130.6_763, 4_056.5_293, 7_053.0_786] ,device=SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:Optional[int] = outputs.sequences.mean(dim=1 ) self.assertTrue(torch.allclose(mean_prediction[0, -3:] ,SCREAMING_SNAKE_CASE__ ,rtol=1e-1 ) )
139
'''simple docstring''' from collections.abc import Generator from math import sin def lowerCAmelCase (__A): """simple docstring""" if len(__A) != 32: raise ValueError('''Input must be of length 32''') _a = b'''''' for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def lowerCAmelCase (__A): """simple docstring""" if i < 0: raise ValueError('''Input must be non-negative''') _a = format(__A , '''08x''')[-8:] _a = 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 lowerCAmelCase (__A): """simple docstring""" _a = b'''''' for char in message: bit_string += format(__A , '''08b''').encode('''utf-8''') _a = format(len(__A) , '''064b''').encode('''utf-8''') # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(__A) % 512 != 448: bit_string += b"0" bit_string += to_little_endian(start_len[32:]) + to_little_endian(start_len[:32]) return bit_string def lowerCAmelCase (__A): """simple docstring""" if len(__A) % 512 != 0: raise ValueError('''Input must have length that\'s a multiple of 512''') for pos in range(0 , len(__A) , 512): _a = bit_string[pos : pos + 512] _a = [] for i in range(0 , 512 , 32): block_words.append(int(to_little_endian(block[i : i + 32]) , 2)) yield block_words def lowerCAmelCase (__A): """simple docstring""" if i < 0: raise ValueError('''Input must be non-negative''') _a = format(__A , '''032b''') _a = '''''' for c in i_str: new_str += "1" if c == "0" else "0" return int(__A , 2) def lowerCAmelCase (__A , __A): """simple docstring""" return (a + b) % 2**32 def lowerCAmelCase (__A , __A): """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 lowerCAmelCase (__A): """simple docstring""" _a = preprocess(__A) _a = [int(2**32 * abs(sin(i + 1))) for i in range(64)] # Starting states _a = 0x67_452_301 _a = 0xEF_CDA_B89 _a = 0x98_BAD_CFE _a = 0x10_325_476 _a = [ 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(__A): _a = aa _a = ba _a = ca _a = da # Hash current chunk for i in range(64): if i <= 15: # f = (b & c) | (not_32(b) & d) # Alternate definition for f _a = d ^ (b & (c ^ d)) _a = i elif i <= 31: # f = (d & b) | (not_32(d) & c) # Alternate definition for f _a = c ^ (d & (b ^ c)) _a = (5 * i + 1) % 16 elif i <= 47: _a = b ^ c ^ d _a = (3 * i + 5) % 16 else: _a = c ^ (b | not_aa(__A)) _a = (7 * i) % 16 _a = (f + a + added_consts[i] + block_words[g]) % 2**32 _a = d _a = c _a = b _a = sum_aa(__A , left_rotate_aa(__A , shift_amounts[i])) # Add hashed chunk to running total _a = sum_aa(__A , __A) _a = sum_aa(__A , __A) _a = sum_aa(__A , __A) _a = sum_aa(__A , __A) _a = reformat_hex(__A) + reformat_hex(__A) + reformat_hex(__A) + reformat_hex(__A) return digest if __name__ == "__main__": import doctest doctest.testmod()
211
0
import json import os import tempfile import datasets from utils import generate_example_dataset, get_duration UpperCamelCase_ = 50000 UpperCamelCase_ = 5000 UpperCamelCase_ ,UpperCamelCase_ = os.path.split(__file__) UpperCamelCase_ = os.path.join(RESULTS_BASEPATH, '''results''', RESULTS_FILENAME.replace('''.py''', '''.json''')) @get_duration def lowerCamelCase_ ( _a : datasets.Dataset , _a : List[Any] ): '''simple docstring''' for i in range(_a ): UpperCAmelCase_ : List[Any] = dataset[i] @get_duration def lowerCamelCase_ ( _a : datasets.Dataset , _a : str , _a : str ): '''simple docstring''' for i in range(0 , len(_a ) , _a ): UpperCAmelCase_ : Optional[Any] = dataset[i : i + batch_size] @get_duration def lowerCamelCase_ ( _a : datasets.Dataset , _a : Tuple , _a : Dict ): '''simple docstring''' with dataset.formatted_as(type=_a ): for i in range(_a ): UpperCAmelCase_ : Optional[Any] = dataset[i] @get_duration def lowerCamelCase_ ( _a : datasets.Dataset , _a : Union[str, Any] , _a : Optional[int] , _a : List[Any] ): '''simple docstring''' with dataset.formatted_as(type=_a ): for i in range(0 , _a , _a ): UpperCAmelCase_ : Dict = dataset[i : i + batch_size] def lowerCamelCase_ ( ): '''simple docstring''' UpperCAmelCase_ : Union[str, Any] = {"""num examples""": SPEED_TEST_N_EXAMPLES} UpperCAmelCase_ : Any = [ (read, {"""length""": SMALL_TEST}), (read, {"""length""": SPEED_TEST_N_EXAMPLES}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 10}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 100}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 1000}), (read_formatted, {"""type""": """numpy""", """length""": SMALL_TEST}), (read_formatted, {"""type""": """pandas""", """length""": SMALL_TEST}), (read_formatted, {"""type""": """torch""", """length""": SMALL_TEST}), (read_formatted, {"""type""": """tensorflow""", """length""": SMALL_TEST}), (read_formatted_batch, {"""type""": """numpy""", """length""": SMALL_TEST, """batch_size""": 10}), (read_formatted_batch, {"""type""": """numpy""", """length""": SMALL_TEST, """batch_size""": 1000}), ] UpperCAmelCase_ : int = [ (read, {"""length""": SMALL_TEST}), (read, {"""length""": SPEED_TEST_N_EXAMPLES}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 10}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 100}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 1000}), (read_formatted, {"""type""": """numpy""", """length""": SMALL_TEST}), (read_formatted_batch, {"""type""": """numpy""", """length""": SMALL_TEST, """batch_size""": 10}), (read_formatted_batch, {"""type""": """numpy""", """length""": SMALL_TEST, """batch_size""": 1000}), ] with tempfile.TemporaryDirectory() as tmp_dir: print("""generating dataset""" ) UpperCAmelCase_ : int = datasets.Features( {"""list""": datasets.Sequence(datasets.Value("""float32""" ) ), """numbers""": datasets.Value("""float32""" )} ) UpperCAmelCase_ : Optional[int] = generate_example_dataset( os.path.join(_a , """dataset.arrow""" ) , _a , num_examples=_a , seq_shapes={"""list""": (100,)} , ) print("""first set of iterations""" ) for func, kwargs in functions: print(func.__name__ , str(_a ) ) UpperCAmelCase_ : Any = func(_a , **_a ) print("""shuffling dataset""" ) UpperCAmelCase_ : List[str] = dataset.shuffle() print("""Second set of iterations (after shuffling""" ) for func, kwargs in functions_shuffled: print("""shuffled """ , func.__name__ , str(_a ) ) UpperCAmelCase_ : Tuple = func( _a , **_a ) with open(_a , """wb""" ) as f: f.write(json.dumps(_a ).encode("""utf-8""" ) ) if __name__ == "__main__": # useful to run the profiler benchmark_iterating()
59
from typing import Dict, List, Optional, Union import numpy as np 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_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, ChannelDimension, ImageInput, PILImageResampling, is_batched, to_numpy_array, valid_images, ) from ...utils import TensorType, logging UpperCamelCase_ = logging.get_logger(__name__) class _snake_case ( __snake_case ): '''simple docstring''' A__ : str = ["pixel_values"] def __init__( self: Optional[Any] ,lowerCamelCase_: bool = True ,lowerCamelCase_: Optional[Dict[str, int]] = None ,lowerCamelCase_: PILImageResampling = PILImageResampling.BICUBIC ,lowerCamelCase_: bool = True ,lowerCamelCase_: bool = True ,lowerCamelCase_: Union[int, float] = 1 / 255 ,lowerCamelCase_: Dict[str, int] = None ,lowerCamelCase_: bool = True ,lowerCamelCase_: Optional[Union[float, List[float]]] = None ,lowerCamelCase_: Optional[Union[float, List[float]]] = None ,**lowerCamelCase_: Union[str, Any] ,) -> None: super().__init__(**lowerCamelCase_ ) UpperCAmelCase_ : Optional[int] = size if size is not None else {"""height""": 224, """width""": 224} UpperCAmelCase_ : Union[str, Any] = get_size_dict(lowerCamelCase_ ) UpperCAmelCase_ : Union[str, Any] = crop_size if crop_size is not None else {"""height""": 224, """width""": 224} UpperCAmelCase_ : Tuple = get_size_dict(lowerCamelCase_ ,default_to_square=lowerCamelCase_ ,param_name="""crop_size""" ) UpperCAmelCase_ : Union[str, Any] = do_resize UpperCAmelCase_ : Union[str, Any] = do_rescale UpperCAmelCase_ : str = do_normalize UpperCAmelCase_ : Optional[int] = do_center_crop UpperCAmelCase_ : str = crop_size UpperCAmelCase_ : List[str] = size UpperCAmelCase_ : Any = resample UpperCAmelCase_ : Tuple = rescale_factor UpperCAmelCase_ : int = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN UpperCAmelCase_ : str = image_std if image_std is not None else IMAGENET_DEFAULT_STD def A__ ( self: List[Any] ,lowerCamelCase_: np.ndarray ,lowerCamelCase_: Dict[str, int] ,lowerCamelCase_: PILImageResampling = PILImageResampling.BILINEAR ,lowerCamelCase_: Optional[Union[str, ChannelDimension]] = None ,**lowerCamelCase_: Optional[int] ,) -> np.ndarray: UpperCAmelCase_ : Tuple = get_size_dict(lowerCamelCase_ ) if "shortest_edge" in size: UpperCAmelCase_ : Optional[Any] = get_resize_output_image_size(lowerCamelCase_ ,size=size["""shortest_edge"""] ,default_to_square=lowerCamelCase_ ) # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"]) elif "height" in size and "width" in size: UpperCAmelCase_ : Tuple = (size["""height"""], size["""width"""]) else: raise ValueError(F'''Size must contain \'height\' and \'width\' keys or \'shortest_edge\' key. Got {size.keys()}''' ) return resize(lowerCamelCase_ ,size=lowerCamelCase_ ,resample=lowerCamelCase_ ,data_format=lowerCamelCase_ ,**lowerCamelCase_ ) def A__ ( self: List[Any] ,lowerCamelCase_: np.ndarray ,lowerCamelCase_: Dict[str, int] ,lowerCamelCase_: Optional[Union[str, ChannelDimension]] = None ,**lowerCamelCase_: str ,) -> np.ndarray: UpperCAmelCase_ : Dict = get_size_dict(lowerCamelCase_ ) if "height" not in size or "width" not in size: raise ValueError(F'''The `size` parameter must contain the keys (height, width). Got {size.keys()}''' ) return center_crop(lowerCamelCase_ ,size=(size["""height"""], size["""width"""]) ,data_format=lowerCamelCase_ ,**lowerCamelCase_ ) def A__ ( self: Optional[int] ,lowerCamelCase_: np.ndarray ,lowerCamelCase_: float ,lowerCamelCase_: Optional[Union[str, ChannelDimension]] = None ,**lowerCamelCase_: List[str] ) -> np.ndarray: return rescale(lowerCamelCase_ ,scale=lowerCamelCase_ ,data_format=lowerCamelCase_ ,**lowerCamelCase_ ) def A__ ( self: List[str] ,lowerCamelCase_: np.ndarray ,lowerCamelCase_: Union[float, List[float]] ,lowerCamelCase_: Union[float, List[float]] ,lowerCamelCase_: Optional[Union[str, ChannelDimension]] = None ,**lowerCamelCase_: Union[str, Any] ,) -> np.ndarray: return normalize(lowerCamelCase_ ,mean=lowerCamelCase_ ,std=lowerCamelCase_ ,data_format=lowerCamelCase_ ,**lowerCamelCase_ ) def A__ ( self: Any ,lowerCamelCase_: ImageInput ,lowerCamelCase_: Optional[bool] = None ,lowerCamelCase_: Dict[str, int] = None ,lowerCamelCase_: PILImageResampling = None ,lowerCamelCase_: bool = None ,lowerCamelCase_: int = None ,lowerCamelCase_: Optional[bool] = None ,lowerCamelCase_: Optional[float] = None ,lowerCamelCase_: Optional[bool] = None ,lowerCamelCase_: Optional[Union[float, List[float]]] = None ,lowerCamelCase_: Optional[Union[float, List[float]]] = None ,lowerCamelCase_: Optional[Union[str, TensorType]] = None ,lowerCamelCase_: Union[str, ChannelDimension] = ChannelDimension.FIRST ,**lowerCamelCase_: List[str] ,) -> BatchFeature: UpperCAmelCase_ : Tuple = do_resize if do_resize is not None else self.do_resize UpperCAmelCase_ : List[Any] = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase_ : Union[str, Any] = do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase_ : List[str] = do_center_crop if do_center_crop is not None else self.do_center_crop UpperCAmelCase_ : List[Any] = crop_size if crop_size is not None else self.crop_size UpperCAmelCase_ : str = get_size_dict(lowerCamelCase_ ,param_name="""crop_size""" ,default_to_square=lowerCamelCase_ ) UpperCAmelCase_ : Optional[int] = resample if resample is not None else self.resample UpperCAmelCase_ : Optional[int] = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase_ : Optional[Any] = image_mean if image_mean is not None else self.image_mean UpperCAmelCase_ : int = image_std if image_std is not None else self.image_std UpperCAmelCase_ : Dict = size if size is not None else self.size UpperCAmelCase_ : List[str] = get_size_dict(lowerCamelCase_ ) if not is_batched(lowerCamelCase_ ): UpperCAmelCase_ : Optional[int] = [images] if not valid_images(lowerCamelCase_ ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size 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.""" ) # All transformations expect numpy arrays. UpperCAmelCase_ : Tuple = [to_numpy_array(lowerCamelCase_ ) for image in images] if do_resize: UpperCAmelCase_ : int = [self.resize(image=lowerCamelCase_ ,size=lowerCamelCase_ ,resample=lowerCamelCase_ ) for image in images] if do_center_crop: UpperCAmelCase_ : Optional[int] = [self.center_crop(image=lowerCamelCase_ ,size=lowerCamelCase_ ) for image in images] if do_rescale: UpperCAmelCase_ : str = [self.rescale(image=lowerCamelCase_ ,scale=lowerCamelCase_ ) for image in images] if do_normalize: UpperCAmelCase_ : Dict = [self.normalize(image=lowerCamelCase_ ,mean=lowerCamelCase_ ,std=lowerCamelCase_ ) for image in images] UpperCAmelCase_ : Dict = [to_channel_dimension_format(lowerCamelCase_ ,lowerCamelCase_ ) for image in images] UpperCAmelCase_ : Tuple = {"""pixel_values""": images} return BatchFeature(data=lowerCamelCase_ ,tensor_type=lowerCamelCase_ )
59
1
'''simple docstring''' from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class __UpperCamelCase : def __init__( self , __a , __a=2 , __a=3 , __a=4 , __a=2 , __a=7 , __a=True , __a=True , __a=True , __a=True , __a=99 , __a=36 , __a=2 , __a=4 , __a=37 , __a="gelu" , __a=0.1 , __a=0.1 , __a=512 , __a=16 , __a=2 , __a=0.02 , __a=6 , __a=6 , __a=3 , __a=4 , __a=None , __a=1000 , ): '''simple docstring''' __a : Optional[Any] = parent __a : int = batch_size __a : Any = num_channels __a : Optional[int] = image_size __a : Dict = patch_size __a : int = is_training __a : Union[str, Any] = use_input_mask __a : Optional[int] = use_token_type_ids __a : Dict = use_labels __a : str = vocab_size __a : List[Any] = hidden_size __a : Union[str, Any] = num_hidden_layers __a : str = num_attention_heads __a : Union[str, Any] = intermediate_size __a : Any = hidden_act __a : List[str] = hidden_dropout_prob __a : List[str] = attention_probs_dropout_prob __a : List[Any] = max_position_embeddings __a : Tuple = type_vocab_size __a : Any = type_sequence_label_size __a : Optional[int] = initializer_range __a : Any = coordinate_size __a : List[Any] = shape_size __a : Optional[int] = num_labels __a : Dict = num_choices __a : Union[str, Any] = scope __a : Union[str, Any] = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) __a : Optional[int] = text_seq_length __a : Any = (image_size // patch_size) ** 2 + 1 __a : Dict = self.text_seq_length + self.image_seq_length def __UpperCAmelCase ( self ): '''simple docstring''' __a : str = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) __a : Tuple = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) __a : Any = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: __a : List[Any] = bbox[i, j, 3] __a : Tuple = bbox[i, j, 1] __a : str = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: __a : int = bbox[i, j, 2] __a : Dict = bbox[i, j, 0] __a : int = tmp_coordinate __a : Optional[int] = tf.constant(__a ) __a : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __a : str = None if self.use_input_mask: __a : Optional[Any] = random_attention_mask([self.batch_size, self.text_seq_length] ) __a : str = None if self.use_token_type_ids: __a : List[Any] = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) __a : Optional[Any] = None __a : Optional[int] = None if self.use_labels: __a : List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __a : Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) __a : int = LayoutLMvaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def __UpperCAmelCase ( self , __a , __a , __a , __a , __a , __a ): '''simple docstring''' __a : Dict = TFLayoutLMvaModel(config=__a ) # text + image __a : List[Any] = model(__a , pixel_values=__a , training=__a ) __a : Any = model( __a , bbox=__a , pixel_values=__a , attention_mask=__a , token_type_ids=__a , training=__a , ) __a : Optional[int] = model(__a , bbox=__a , pixel_values=__a , training=__a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only __a : Any = model(__a , training=__a ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only __a : str = model({'pixel_values': pixel_values} , training=__a ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __UpperCAmelCase ( self , __a , __a , __a , __a , __a , __a , __a ): '''simple docstring''' __a : Any = self.num_labels __a : Dict = TFLayoutLMvaForSequenceClassification(config=__a ) __a : List[str] = model( __a , bbox=__a , pixel_values=__a , attention_mask=__a , token_type_ids=__a , labels=__a , training=__a , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __UpperCAmelCase ( self , __a , __a , __a , __a , __a , __a , __a ): '''simple docstring''' __a : str = self.num_labels __a : Optional[Any] = TFLayoutLMvaForTokenClassification(config=__a ) __a : List[str] = model( __a , bbox=__a , pixel_values=__a , attention_mask=__a , token_type_ids=__a , labels=__a , training=__a , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __UpperCAmelCase ( self , __a , __a , __a , __a , __a , __a , __a ): '''simple docstring''' __a : List[Any] = 2 __a : Any = TFLayoutLMvaForQuestionAnswering(config=__a ) __a : Any = model( __a , bbox=__a , pixel_values=__a , attention_mask=__a , token_type_ids=__a , start_positions=__a , end_positions=__a , training=__a , ) 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''' __a : Dict = self.prepare_config_and_inputs() ((__a) , (__a) , (__a) , (__a) , (__a) , (__a) , (__a) , (__a)) : Dict = config_and_inputs __a : Any = { 'input_ids': input_ids, 'bbox': bbox, 'pixel_values': pixel_values, 'token_type_ids': token_type_ids, 'attention_mask': input_mask, } return config, inputs_dict @require_tf class __UpperCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ , unittest.TestCase ): A_ = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) A_ = ( {"document-question-answering": TFLayoutLMvaForQuestionAnswering, "feature-extraction": TFLayoutLMvaModel} if is_tf_available() else {} ) A_ = False A_ = False A_ = False def __UpperCAmelCase ( self , __a , __a , __a , __a , __a ): '''simple docstring''' return True def __UpperCAmelCase ( self , __a , __a , __a=False ): '''simple docstring''' __a : str = copy.deepcopy(__a ) if model_class in get_values(__a ): __a : str = { k: tf.tile(tf.expand_dims(__a , 1 ) , (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(__a , tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(__a ): __a : Optional[int] = tf.ones(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(__a ): __a : int = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) __a : Optional[Any] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(__a ): __a : Any = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(__a ): __a : Union[str, Any] = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=tf.intaa ) return inputs_dict def __UpperCAmelCase ( self ): '''simple docstring''' __a : Optional[Any] = TFLayoutLMvaModelTester(self ) __a : Optional[int] = ConfigTester(self , config_class=__a , hidden_size=37 ) def __UpperCAmelCase ( self ): '''simple docstring''' self.config_tester.run_common_tests() def __UpperCAmelCase ( self ): '''simple docstring''' __a , __a : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a : Dict = model_class(__a ) if getattr(__a , 'hf_compute_loss' , __a ): # The number of elements in the loss should be the same as the number of elements in the label __a : Union[str, Any] = self._prepare_for_class(inputs_dict.copy() , __a , return_labels=__a ) __a : str = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() , reverse=__a )[0] ] __a : Dict = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs __a : int = self._prepare_for_class(inputs_dict.copy() , __a , return_labels=__a ) __a : Dict = prepared_for_class.pop('input_ids' ) __a : Tuple = model(__a , **__a )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions __a : int = self._prepare_for_class(inputs_dict.copy() , __a , return_labels=__a ) __a : str = prepared_for_class.pop('input_ids' ) if "labels" in prepared_for_class: __a : Union[str, Any] = prepared_for_class['labels'].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: __a : List[Any] = -100 __a : List[str] = tf.convert_to_tensor(__a ) __a : Any = model(__a , **__a )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict __a : Union[str, Any] = self._prepare_for_class(inputs_dict.copy() , __a , return_labels=__a ) __a : str = model(__a )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple __a : Tuple = self._prepare_for_class(inputs_dict.copy() , __a , return_labels=__a ) # Get keys that were added with the _prepare_for_class function __a : Dict = prepared_for_class.keys() - inputs_dict.keys() __a : Any = inspect.signature(model.call ).parameters __a : str = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple __a : List[Any] = {0: 'input_ids'} for label_key in label_keys: __a : List[Any] = signature_names.index(__a ) __a : Union[str, Any] = label_key __a : List[str] = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple __a : Union[str, Any] = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: __a : Optional[Any] = prepared_for_class[value] __a : str = tuple(__a ) # Send to model __a : Tuple = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def __UpperCAmelCase ( self ): '''simple docstring''' ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(__a , __a , __a , __a , __a , __a ) def __UpperCAmelCase ( self ): '''simple docstring''' ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) : Dict = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: __a : Any = type self.model_tester.create_and_check_model(__a , __a , __a , __a , __a , __a ) def __UpperCAmelCase ( self ): '''simple docstring''' ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( __a , __a , __a , __a , __a , __a , __a ) def __UpperCAmelCase ( self ): '''simple docstring''' ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( __a , __a , __a , __a , __a , __a , __a ) def __UpperCAmelCase ( self ): '''simple docstring''' ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( __a , __a , __a , __a , __a , __a , __a ) @slow def __UpperCAmelCase ( self ): '''simple docstring''' for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a : List[Any] = TFLayoutLMvaModel.from_pretrained(__a ) self.assertIsNotNone(__a ) def lowerCamelCase (): __a : Optional[Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf class __UpperCamelCase ( unittest.TestCase ): @cached_property def __UpperCAmelCase ( self ): '''simple docstring''' return LayoutLMvaImageProcessor(apply_ocr=__a ) if is_vision_available() else None @slow def __UpperCAmelCase ( self ): '''simple docstring''' __a : str = TFLayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base' ) __a : Tuple = self.default_image_processor __a : List[Any] = prepare_img() __a : int = image_processor(images=__a , return_tensors='tf' ).pixel_values __a : Union[str, Any] = tf.constant([[1, 2]] ) __a : Optional[Any] = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) , axis=0 ) # forward pass __a : Tuple = model(input_ids=__a , bbox=__a , pixel_values=__a , training=__a ) # verify the logits __a : List[Any] = (1, 199, 768) self.assertEqual(outputs.last_hidden_state.shape , __a ) __a : Optional[Any] = tf.constant( [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , __a , atol=1E-4 ) )
27
"""simple docstring""" import importlib.util import json import os import warnings from dataclasses import dataclass, field import torch from ..training_args import TrainingArguments from ..utils import cached_property, is_sagemaker_dp_enabled, logging __snake_case = logging.get_logger(__name__) def __lowerCAmelCase ( ) -> str: """simple docstring""" snake_case : Dict = os.getenv("SM_HP_MP_PARAMETERS" , "{}" ) try: # Parse it and check the field "partitions" is included, it is required for model parallel. snake_case : Optional[int] = json.loads(lowercase ) if "partitions" not in smp_options: return False except json.JSONDecodeError: return False # Get the sagemaker specific framework parameters from mpi_options variable. snake_case : Optional[int] = os.getenv("SM_FRAMEWORK_PARAMS" , "{}" ) try: # Parse it and check the field "sagemaker_distributed_dataparallel_enabled". snake_case : Any = json.loads(lowercase ) if not mpi_options.get("sagemaker_mpi_enabled" , lowercase ): return False except json.JSONDecodeError: return False # Lastly, check if the `smdistributed` module is present. return importlib.util.find_spec("smdistributed" ) is not None if is_sagemaker_model_parallel_available(): import smdistributed.modelparallel.torch as smp smp.init() @dataclass class _lowerCAmelCase ( snake_case_ ): __UpperCAmelCase : str = field( default='''''' , metadata={'''help''': '''Used by the SageMaker launcher to send mp-specific args. Ignored in SageMakerTrainer'''} , ) def lowerCamelCase ( self ) -> List[Any]: '''simple docstring''' super().__post_init__() warnings.warn( "`SageMakerTrainingArguments` is deprecated and will be removed in v5 of Transformers. You can use " "`TrainingArguments` instead." , UpperCamelCase__ , ) @cached_property def lowerCamelCase ( self ) -> "torch.device": '''simple docstring''' logger.info("PyTorch: setting up devices" ) if torch.distributed.is_available() and torch.distributed.is_initialized() and self.local_rank == -1: logger.warning( "torch.distributed process group is initialized, but local_rank == -1. " "In order to use Torch DDP, launch your script with `python -m torch.distributed.launch" ) if self.no_cuda: snake_case : Optional[Any] = torch.device("cpu" ) snake_case : List[Any] = 0 elif is_sagemaker_model_parallel_available(): snake_case : Tuple = smp.local_rank() snake_case : int = torch.device("cuda" , UpperCamelCase__ ) snake_case : Dict = 1 elif is_sagemaker_dp_enabled(): import smdistributed.dataparallel.torch.torch_smddp # noqa: F401 torch.distributed.init_process_group(backend="smddp" , timeout=self.ddp_timeout_delta ) snake_case : Any = int(os.getenv("SMDATAPARALLEL_LOCAL_RANK" ) ) snake_case : Optional[Any] = torch.device("cuda" , self.local_rank ) snake_case : str = 1 elif self.local_rank == -1: # if n_gpu is > 1 we'll use nn.DataParallel. # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0` # Explicitly set CUDA to the first (index 0) CUDA device, otherwise `set_device` will # trigger an error that a device index is missing. Index 0 takes into account the # GPUs available in the environment, so `CUDA_VISIBLE_DEVICES=1,2` with `cuda:0` # will use the first GPU in that env, i.e. GPU#1 snake_case : List[str] = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" ) # Sometimes the line in the postinit has not been run before we end up here, so just checking we're not at # the default value. snake_case : Optional[Any] = torch.cuda.device_count() else: # Here, we'll use torch.distributed. # Initializes the distributed backend which will take care of synchronizing nodes/GPUs if not torch.distributed.is_initialized(): torch.distributed.init_process_group(backend="nccl" , timeout=self.ddp_timeout_delta ) snake_case : Any = torch.device("cuda" , self.local_rank ) snake_case : Dict = 1 if device.type == "cuda": torch.cuda.set_device(UpperCamelCase__ ) return device @property def lowerCamelCase ( self ) -> Optional[int]: '''simple docstring''' if is_sagemaker_model_parallel_available(): return smp.dp_size() return super().world_size @property def lowerCamelCase ( self ) -> List[str]: '''simple docstring''' return not is_sagemaker_model_parallel_available() @property def lowerCamelCase ( self ) -> str: '''simple docstring''' return False
203
0
from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , ) -> Any: __lowerCamelCase : str = len(_A ) # If row is equal to the size of the board it means there are a queen in each row in # the current board (possible_board) if row == n: # We convert the variable possible_board that looks like this: [1, 3, 0, 2] to # this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . '] boards.append(['. ' * i + 'Q ' + '. ' * (n - 1 - i) for i in possible_board] ) return # We iterate each column in the row to find all possible results in each row for col in range(_A ): # We apply that we learned previously. First we check that in the current board # (possible_board) there are not other same value because if there is it means # that there are a collision in vertical. Then we apply the two formulas we # learned before: # # 45º: y - x = b or 45: row - col = b # 135º: y + x = b or row + col = b. # # And we verify if the results of this two formulas not exist in their variables # respectively. (diagonal_right_collisions, diagonal_left_collisions) # # If any or these are True it means there is a collision so we continue to the # next value in the for loop. if ( col in possible_board or row - col in diagonal_right_collisions or row + col in diagonal_left_collisions ): continue # If it is False we call dfs function again and we update the inputs depth_first_search( [*possible_board, col] , [*diagonal_right_collisions, row - col] , [*diagonal_left_collisions, row + col] , _A , _A , ) def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> Optional[int]: __lowerCamelCase : list[list[str]] = [] depth_first_search([] , [] , [] , _A , _A ) # Print all the boards for board in boards: for column in board: print(_A ) print('' ) print(len(_A ) , 'solutions were found.' ) if __name__ == "__main__": import doctest doctest.testmod() n_queens_solution(4)
353
from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , ) -> None: __lowerCamelCase : int = len(lowerCamelCase__ ) # If row is equal to the size of the board it means there are a queen in each row in # the current board (possible_board) if row == n: # We convert the variable possible_board that looks like this: [1, 3, 0, 2] to # this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . '] boards.append(['. ' * i + 'Q ' + '. ' * (n - 1 - i) for i in possible_board] ) return # We iterate each column in the row to find all possible results in each row for col in range(lowerCamelCase__ ): # We apply that we learned previously. First we check that in the current board # (possible_board) there are not other same value because if there is it means # that there are a collision in vertical. Then we apply the two formulas we # learned before: # # 45º: y - x = b or 45: row - col = b # 135º: y + x = b or row + col = b. # # And we verify if the results of this two formulas not exist in their variables # respectively. (diagonal_right_collisions, diagonal_left_collisions) # # If any or these are True it means there is a collision so we continue to the # next value in the for loop. if ( col in possible_board or row - col in diagonal_right_collisions or row + col in diagonal_left_collisions ): continue # If it is False we call dfs function again and we update the inputs depth_first_search( [*possible_board, col] , [*diagonal_right_collisions, row - col] , [*diagonal_left_collisions, row + col] , lowerCamelCase__ , lowerCamelCase__ , ) def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> None: __lowerCamelCase : list[list[str]] = [] depth_first_search([] , [] , [] , lowerCamelCase__ , lowerCamelCase__ ) # Print all the boards for board in boards: for column in board: print(lowerCamelCase__ ) print('' ) print(len(lowerCamelCase__ ) , 'solutions were found.' ) if __name__ == "__main__": import doctest doctest.testmod() n_queens_solution(4)
113
0
__snake_case = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' __snake_case = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] __snake_case = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
348
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def lowerCAmelCase_ ( )-> int: '''simple docstring''' UpperCAmelCase : str ={ '''repo_name''': ['''test_repo1''', '''test_repo2''', '''test_repo3'''], '''path''': ['''test_1.py''', '''test_2.py''', '''unit_test.py'''], '''content''': ['''a ''' * 20, '''a ''' * 30, '''b ''' * 7], } UpperCAmelCase : Union[str, Any] =Dataset.from_dict(__lowerCAmelCase ) return dataset class __snake_case ( lowerCamelCase__ ): def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : List[str] =get_dataset() UpperCAmelCase : Optional[int] =make_duplicate_clusters(snake_case__ , 0.85 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : str =get_dataset() UpperCAmelCase , UpperCAmelCase : Tuple =deduplicate_dataset(snake_case__ ) self.assertEqual(len(snake_case__ ) , 2 ) print(snake_case__ ) self.assertEqual(duplicate_clusters[0][0]['''copies'''] , 2 ) self.assertEqual(duplicate_clusters[0][0]['''is_extreme'''] , snake_case__ )
348
1
"""simple docstring""" from __future__ import annotations class lowerCAmelCase__ : def __init__( self : Optional[int] , _lowerCamelCase : int = 0 ): _snake_case = key def lowercase ( self : Tuple , _lowerCamelCase : str , _lowerCamelCase : int ): assert isinstance(_lowerCamelCase , _lowerCamelCase ) and isinstance(_lowerCamelCase , _lowerCamelCase ) _snake_case = key or self.__key or 1 # make sure key is an appropriate size key %= 255 return [chr(ord(_lowerCamelCase ) ^ key ) for ch in content] def lowercase ( self : List[Any] , _lowerCamelCase : str , _lowerCamelCase : int ): assert isinstance(_lowerCamelCase , _lowerCamelCase ) and isinstance(_lowerCamelCase , _lowerCamelCase ) _snake_case = key or self.__key or 1 # make sure key is an appropriate size key %= 255 return [chr(ord(_lowerCamelCase ) ^ key ) for ch in content] def lowercase ( self : Union[str, Any] , _lowerCamelCase : str , _lowerCamelCase : int = 0 ): assert isinstance(_lowerCamelCase , _lowerCamelCase ) and isinstance(_lowerCamelCase , _lowerCamelCase ) _snake_case = key or self.__key or 1 # make sure key can be any size while key > 255: key -= 255 # This will be returned _snake_case = '''''' for ch in content: ans += chr(ord(_lowerCamelCase ) ^ key ) return ans def lowercase ( self : List[str] , _lowerCamelCase : str , _lowerCamelCase : int = 0 ): assert isinstance(_lowerCamelCase , _lowerCamelCase ) and isinstance(_lowerCamelCase , _lowerCamelCase ) _snake_case = key or self.__key or 1 # make sure key can be any size while key > 255: key -= 255 # This will be returned _snake_case = '''''' for ch in content: ans += chr(ord(_lowerCamelCase ) ^ key ) return ans def lowercase ( self : List[Any] , _lowerCamelCase : str , _lowerCamelCase : int = 0 ): assert isinstance(_lowerCamelCase , _lowerCamelCase ) and isinstance(_lowerCamelCase , _lowerCamelCase ) try: with open(_lowerCamelCase ) as fin, open('''encrypt.out''' , '''w+''' ) as fout: # actual encrypt-process for line in fin: fout.write(self.encrypt_string(_lowerCamelCase , _lowerCamelCase ) ) except OSError: return False return True def lowercase ( self : Optional[Any] , _lowerCamelCase : str , _lowerCamelCase : int ): assert isinstance(_lowerCamelCase , _lowerCamelCase ) and isinstance(_lowerCamelCase , _lowerCamelCase ) try: with open(_lowerCamelCase ) as fin, open('''decrypt.out''' , '''w+''' ) as fout: # actual encrypt-process for line in fin: fout.write(self.decrypt_string(_lowerCamelCase , _lowerCamelCase ) ) except OSError: return False return True # Tests # crypt = XORCipher() # key = 67 # # test encrypt # print(crypt.encrypt("hallo welt",key)) # # test decrypt # print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) # # test encrypt_string # print(crypt.encrypt_string("hallo welt",key)) # # test decrypt_string # print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) # if (crypt.encrypt_file("test.txt",key)): # print("encrypt successful") # else: # print("encrypt unsuccessful") # if (crypt.decrypt_file("encrypt.out",key)): # print("decrypt successful") # else: # print("decrypt unsuccessful")
40
"""simple docstring""" from timeit import timeit UpperCAmelCase__ = { '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 _UpperCAmelCase ( __lowerCamelCase : str ) -> bool: _snake_case = 0 _snake_case = 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 _UpperCAmelCase ( __lowerCamelCase : str ) -> bool: _snake_case = len(__lowerCamelCase ) // 2 _snake_case = 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 _UpperCAmelCase ( __lowerCamelCase : str ) -> bool: if len(__lowerCamelCase ) <= 2: return True if s[0] == s[len(__lowerCamelCase ) - 1]: return is_palindrome_recursive(s[1:-1] ) else: return False def _UpperCAmelCase ( __lowerCamelCase : str ) -> bool: return s == s[::-1] def _UpperCAmelCase ( __lowerCamelCase : str ) -> None: _snake_case = f'''all({name}(key) is value for key, value in test_data.items())''' _snake_case = f'''from __main__ import test_data, {name}''' _snake_case = 50_00_00 _snake_case = 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')
40
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCAmelCase__ = { "configuration_electra": ["ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP", "ElectraConfig", "ElectraOnnxConfig"], "tokenization_electra": ["ElectraTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ["ElectraTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST", "ElectraForCausalLM", "ElectraForMaskedLM", "ElectraForMultipleChoice", "ElectraForPreTraining", "ElectraForQuestionAnswering", "ElectraForSequenceClassification", "ElectraForTokenClassification", "ElectraModel", "ElectraPreTrainedModel", "load_tf_weights_in_electra", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST", "TFElectraForMaskedLM", "TFElectraForMultipleChoice", "TFElectraForPreTraining", "TFElectraForQuestionAnswering", "TFElectraForSequenceClassification", "TFElectraForTokenClassification", "TFElectraModel", "TFElectraPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ "FlaxElectraForCausalLM", "FlaxElectraForMaskedLM", "FlaxElectraForMultipleChoice", "FlaxElectraForPreTraining", "FlaxElectraForQuestionAnswering", "FlaxElectraForSequenceClassification", "FlaxElectraForTokenClassification", "FlaxElectraModel", "FlaxElectraPreTrainedModel", ] if TYPE_CHECKING: from .configuration_electra import ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ElectraConfig, ElectraOnnxConfig from .tokenization_electra import ElectraTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_electra_fast import ElectraTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_electra import ( ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, ElectraForCausalLM, ElectraForMaskedLM, ElectraForMultipleChoice, ElectraForPreTraining, ElectraForQuestionAnswering, ElectraForSequenceClassification, ElectraForTokenClassification, ElectraModel, ElectraPreTrainedModel, load_tf_weights_in_electra, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_electra import ( TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, TFElectraForMaskedLM, TFElectraForMultipleChoice, TFElectraForPreTraining, TFElectraForQuestionAnswering, TFElectraForSequenceClassification, TFElectraForTokenClassification, TFElectraModel, TFElectraPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_electra import ( FlaxElectraForCausalLM, FlaxElectraForMaskedLM, FlaxElectraForMultipleChoice, FlaxElectraForPreTraining, FlaxElectraForQuestionAnswering, FlaxElectraForSequenceClassification, FlaxElectraForTokenClassification, FlaxElectraModel, FlaxElectraPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _snake_case = { '''configuration_squeezebert''': [ '''SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SqueezeBertConfig''', '''SqueezeBertOnnxConfig''', ], '''tokenization_squeezebert''': ['''SqueezeBertTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ['''SqueezeBertTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ '''SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''SqueezeBertForMaskedLM''', '''SqueezeBertForMultipleChoice''', '''SqueezeBertForQuestionAnswering''', '''SqueezeBertForSequenceClassification''', '''SqueezeBertForTokenClassification''', '''SqueezeBertModel''', '''SqueezeBertModule''', '''SqueezeBertPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_squeezebert import ( SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, SqueezeBertConfig, SqueezeBertOnnxConfig, ) from .tokenization_squeezebert import SqueezeBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_squeezebert import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, SqueezeBertModule, SqueezeBertPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
283
0
"""simple docstring""" import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger lowerCAmelCase_ : List[str] = get_logger(__name__) lowerCAmelCase_ : int = Path(__file__).parent / '''model_card_template.md''' lowerCAmelCase_ : str = uuida().hex lowerCAmelCase_ : Optional[Any] = os.getenv('''HF_HUB_OFFLINE''', '''''').upper() in ENV_VARS_TRUE_VALUES lowerCAmelCase_ : Optional[int] = os.getenv('''DISABLE_TELEMETRY''', '''''').upper() in ENV_VARS_TRUE_VALUES lowerCAmelCase_ : Any = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '''/api/telemetry/''' def _lowerCAmelCase ( lowerCAmelCase = None ): '''simple docstring''' UpperCAmelCase = F'''diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}''' if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += F'''; torch/{_torch_version}''' if is_flax_available(): ua += F'''; jax/{_jax_version}''' ua += F'''; flax/{_flax_version}''' if is_onnx_available(): ua += F'''; onnxruntime/{_onnxruntime_version}''' # CI will set this value to True if os.environ.get("""DIFFUSERS_IS_CI""" , """""" ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(_UpperCAmelCase , _UpperCAmelCase ): ua += "; " + "; ".join(F'''{k}/{v}''' for k, v in user_agent.items() ) elif isinstance(_UpperCAmelCase , _UpperCAmelCase ): ua += "; " + user_agent return ua def _lowerCAmelCase ( lowerCAmelCase , lowerCAmelCase = None , lowerCAmelCase = None ): '''simple docstring''' if token is None: UpperCAmelCase = HfFolder.get_token() if organization is None: UpperCAmelCase = whoami(_UpperCAmelCase )['name'] return F'''{username}/{model_id}''' else: return F'''{organization}/{model_id}''' def _lowerCAmelCase ( lowerCAmelCase , lowerCAmelCase ): '''simple docstring''' if not is_jinja_available(): raise ValueError( """Modelcard rendering is based on Jinja templates.""" """ Please make sure to have `jinja` installed before using `create_model_card`.""" """ To install it, please run `pip install Jinja2`.""" ) if hasattr(_UpperCAmelCase , """local_rank""" ) and args.local_rank not in [-1, 0]: return UpperCAmelCase = args.hub_token if hasattr(_UpperCAmelCase , """hub_token""" ) else None UpperCAmelCase = get_full_repo_name(_UpperCAmelCase , token=_UpperCAmelCase ) UpperCAmelCase = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language="""en""" , license="""apache-2.0""" , library_name="""diffusers""" , tags=[] , datasets=args.dataset_name , metrics=[] , ) , template_path=_UpperCAmelCase , model_name=_UpperCAmelCase , repo_name=_UpperCAmelCase , dataset_name=args.dataset_name if hasattr(_UpperCAmelCase , """dataset_name""" ) else None , learning_rate=args.learning_rate , train_batch_size=args.train_batch_size , eval_batch_size=args.eval_batch_size , gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(_UpperCAmelCase , """gradient_accumulation_steps""" ) else None ) , adam_betaa=args.adam_betaa if hasattr(_UpperCAmelCase , """adam_beta1""" ) else None , adam_betaa=args.adam_betaa if hasattr(_UpperCAmelCase , """adam_beta2""" ) else None , adam_weight_decay=args.adam_weight_decay if hasattr(_UpperCAmelCase , """adam_weight_decay""" ) else None , adam_epsilon=args.adam_epsilon if hasattr(_UpperCAmelCase , """adam_epsilon""" ) else None , lr_scheduler=args.lr_scheduler if hasattr(_UpperCAmelCase , """lr_scheduler""" ) else None , lr_warmup_steps=args.lr_warmup_steps if hasattr(_UpperCAmelCase , """lr_warmup_steps""" ) else None , ema_inv_gamma=args.ema_inv_gamma if hasattr(_UpperCAmelCase , """ema_inv_gamma""" ) else None , ema_power=args.ema_power if hasattr(_UpperCAmelCase , """ema_power""" ) else None , ema_max_decay=args.ema_max_decay if hasattr(_UpperCAmelCase , """ema_max_decay""" ) else None , mixed_precision=args.mixed_precision , ) UpperCAmelCase = os.path.join(args.output_dir , """README.md""" ) model_card.save(_UpperCAmelCase ) def _lowerCAmelCase ( lowerCAmelCase , lowerCAmelCase = None ): '''simple docstring''' if resolved_file is None or commit_hash is not None: return commit_hash UpperCAmelCase = str(Path(_UpperCAmelCase ).as_posix() ) UpperCAmelCase = re.search(r"""snapshots/([^/]+)/""" , _UpperCAmelCase ) if search is None: return None UpperCAmelCase = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(_UpperCAmelCase ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. lowerCAmelCase_ : Optional[Any] = os.path.expanduser( os.getenv('''HF_HOME''', os.path.join(os.getenv('''XDG_CACHE_HOME''', '''~/.cache'''), '''huggingface''')) ) lowerCAmelCase_ : List[str] = os.path.join(hf_cache_home, '''diffusers''') def _lowerCAmelCase ( lowerCAmelCase = None , lowerCAmelCase = None ): '''simple docstring''' if new_cache_dir is None: UpperCAmelCase = DIFFUSERS_CACHE if old_cache_dir is None: UpperCAmelCase = old_diffusers_cache UpperCAmelCase = Path(_UpperCAmelCase ).expanduser() UpperCAmelCase = Path(_UpperCAmelCase ).expanduser() for old_blob_path in old_cache_dir.glob("""**/blobs/*""" ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): UpperCAmelCase = new_cache_dir / old_blob_path.relative_to(_UpperCAmelCase ) new_blob_path.parent.mkdir(parents=_UpperCAmelCase , exist_ok=_UpperCAmelCase ) os.replace(_UpperCAmelCase , _UpperCAmelCase ) try: os.symlink(_UpperCAmelCase , _UpperCAmelCase ) except OSError: logger.warning( """Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.""" ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). lowerCAmelCase_ : str = os.path.join(DIFFUSERS_CACHE, '''version_diffusers_cache.txt''') if not os.path.isfile(cache_version_file): lowerCAmelCase_ : List[Any] = 0 else: with open(cache_version_file) as f: try: lowerCAmelCase_ : Tuple = int(f.read()) except ValueError: lowerCAmelCase_ : List[str] = 0 if cache_version < 1: lowerCAmelCase_ : Any = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( '''The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ''' '''existing cached models. This is a one-time operation, you can interrupt it or run it ''' '''later by calling `diffusers.utils.hub_utils.move_cache()`.''' ) try: move_cache() except Exception as e: lowerCAmelCase_ : int = '''\n'''.join(traceback.format_tb(e.__traceback__)) logger.error( F'There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease ' '''file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ''' '''message and we will do our best to help.''' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, '''w''') as f: f.write('''1''') except Exception: logger.warning( F'There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure ' '''the directory exists and can be written to.''' ) def _lowerCAmelCase ( lowerCAmelCase , lowerCAmelCase = None ): '''simple docstring''' if variant is not None: UpperCAmelCase = weights_name.split(""".""" ) UpperCAmelCase = splits[:-1] + [variant] + splits[-1:] UpperCAmelCase = '.'.join(_UpperCAmelCase ) return weights_name def _lowerCAmelCase ( lowerCAmelCase , *, lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase=None , ): '''simple docstring''' UpperCAmelCase = str(_UpperCAmelCase ) if os.path.isfile(_UpperCAmelCase ): return pretrained_model_name_or_path elif os.path.isdir(_UpperCAmelCase ): if os.path.isfile(os.path.join(_UpperCAmelCase , _UpperCAmelCase ) ): # Load from a PyTorch checkpoint UpperCAmelCase = os.path.join(_UpperCAmelCase , _UpperCAmelCase ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) ): UpperCAmelCase = os.path.join(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) return model_file else: raise EnvironmentError( F'''Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.''' ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(_UpperCAmelCase ).base_version ) >= version.parse("""0.20.0""" ) ): try: UpperCAmelCase = hf_hub_download( _UpperCAmelCase , filename=_add_variant(_UpperCAmelCase , _UpperCAmelCase ) , cache_dir=_UpperCAmelCase , force_download=_UpperCAmelCase , proxies=_UpperCAmelCase , resume_download=_UpperCAmelCase , local_files_only=_UpperCAmelCase , use_auth_token=_UpperCAmelCase , user_agent=_UpperCAmelCase , subfolder=_UpperCAmelCase , revision=revision or commit_hash , ) warnings.warn( F'''Loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'` is deprecated. Loading instead from `revision=\'main\'` with `variant={revision}`. Loading model variants via `revision=\'{revision}\'` will be removed in diffusers v1. Please use `variant=\'{revision}\'` instead.''' , _UpperCAmelCase , ) return model_file except: # noqa: E722 warnings.warn( F'''You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant=\'{revision}\'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(_UpperCAmelCase , _UpperCAmelCase )} file in the \'main\' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title \'{pretrained_model_name_or_path} is missing {_add_variant(_UpperCAmelCase , _UpperCAmelCase )}\' so that the correct variant file can be added.''' , _UpperCAmelCase , ) try: # 2. Load model file as usual UpperCAmelCase = hf_hub_download( _UpperCAmelCase , filename=_UpperCAmelCase , cache_dir=_UpperCAmelCase , force_download=_UpperCAmelCase , proxies=_UpperCAmelCase , resume_download=_UpperCAmelCase , local_files_only=_UpperCAmelCase , use_auth_token=_UpperCAmelCase , user_agent=_UpperCAmelCase , subfolder=_UpperCAmelCase , revision=revision or commit_hash , ) return model_file except RepositoryNotFoundError: raise EnvironmentError( F'''{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier ''' """listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a """ """token having permission to this repo with `use_auth_token` or log in with `huggingface-cli """ """login`.""" ) except RevisionNotFoundError: raise EnvironmentError( F'''{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for ''' """this model name. Check the model page at """ F'''\'https://huggingface.co/{pretrained_model_name_or_path}\' for available revisions.''' ) except EntryNotFoundError: raise EnvironmentError( F'''{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.''' ) except HTTPError as err: raise EnvironmentError( F'''There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}''' ) except ValueError: raise EnvironmentError( F'''We couldn\'t connect to \'{HUGGINGFACE_CO_RESOLVE_ENDPOINT}\' to load this model, couldn\'t find it''' F''' in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a''' F''' directory containing a file named {weights_name} or''' """ \nCheckout your internet connection or see how to run the library in""" """ offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.""" ) except EnvironmentError: raise EnvironmentError( F'''Can\'t load the model for \'{pretrained_model_name_or_path}\'. If you were trying to load it from ''' """\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. """ F'''Otherwise, make sure \'{pretrained_model_name_or_path}\' is the correct path to a directory ''' F'''containing a file named {weights_name}''' )
350
"""simple docstring""" def _lowerCAmelCase ( ): '''simple docstring''' return [ a * b * (1000 - a - b) for a in range(1 , 999 ) for b in range(lowerCAmelCase , 999 ) if (a * a + b * b == (1000 - a - b) ** 2) ][0] if __name__ == "__main__": print(F'{solution() = }')
248
0
"""simple docstring""" def __magic_name__ ( __snake_case : int , __snake_case : int ) -> float: return base * power(__snake_case , (exponent - 1) ) if exponent else 1 if __name__ == "__main__": print("""Raise base to the power of exponent using recursion...""") _A : Optional[int] = int(input("""Enter the base: """).strip()) _A : Optional[Any] = int(input("""Enter the exponent: """).strip()) _A : List[Any] = power(base, abs(exponent)) if exponent < 0: # power() does not properly deal w/ negative exponents _A : Union[str, Any] = 1 / result print(F"{base} to the power of {exponent} is {result}")
202
"""simple docstring""" import logging import os from typing import List, TextIO, Union from conllu import parse_incr from utils_ner import InputExample, Split, TokenClassificationTask _A : Optional[int] = logging.getLogger(__name__) class a__ ( a_ ): def __init__( self , _a=-1 ): # in NER datasets, the last column is usually reserved for NER label lowercase : List[str] = label_idx def __magic_name__ ( self , _a , _a ): if isinstance(_a , _a ): lowercase : Optional[Any] = mode.value lowercase : List[str] = os.path.join(_a , f"""{mode}.txt""" ) lowercase : str = 1 lowercase : Optional[int] = [] with open(_a , encoding="utf-8" ) as f: lowercase : List[Any] = [] lowercase : Optional[int] = [] for line in f: if line.startswith("-DOCSTART-" ) or line == "" or line == "\n": if words: examples.append(InputExample(guid=f"""{mode}-{guid_index}""" , words=_a , labels=_a ) ) guid_index += 1 lowercase : int = [] lowercase : int = [] else: lowercase : Optional[Any] = line.split(" " ) words.append(splits[0] ) if len(_a ) > 1: labels.append(splits[self.label_idx].replace("\n" , "" ) ) else: # Examples could have no label for mode = "test" labels.append("O" ) if words: examples.append(InputExample(guid=f"""{mode}-{guid_index}""" , words=_a , labels=_a ) ) return examples def __magic_name__ ( self , _a , _a , _a ): lowercase : List[str] = 0 for line in test_input_reader: if line.startswith("-DOCSTART-" ) or line == "" or line == "\n": writer.write(_a ) if not preds_list[example_id]: example_id += 1 elif preds_list[example_id]: lowercase : Any = line.split()[0] + " " + preds_list[example_id].pop(0 ) + "\n" writer.write(_a ) else: logger.warning("Maximum sequence length exceeded: No prediction for '%s'." , line.split()[0] ) def __magic_name__ ( self , _a ): if path: with open(_a , "r" ) as f: lowercase : Optional[Any] = f.read().splitlines() if "O" not in labels: lowercase : List[Any] = ["O"] + labels return labels else: return ["O", "B-MISC", "I-MISC", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"] class a__ ( a_ ): def __init__( self ): # in CONLL2003 dataset chunk column is second-to-last super().__init__(label_idx=-2 ) def __magic_name__ ( self , _a ): if path: with open(_a , "r" ) as f: lowercase : Tuple = f.read().splitlines() if "O" not in labels: lowercase : Optional[int] = ["O"] + labels return labels else: return [ "O", "B-ADVP", "B-INTJ", "B-LST", "B-PRT", "B-NP", "B-SBAR", "B-VP", "B-ADJP", "B-CONJP", "B-PP", "I-ADVP", "I-INTJ", "I-LST", "I-PRT", "I-NP", "I-SBAR", "I-VP", "I-ADJP", "I-CONJP", "I-PP", ] class a__ ( a_ ): def __magic_name__ ( self , _a , _a ): if isinstance(_a , _a ): lowercase : List[Any] = mode.value lowercase : Optional[int] = os.path.join(_a , f"""{mode}.txt""" ) lowercase : Tuple = 1 lowercase : List[str] = [] with open(_a , encoding="utf-8" ) as f: for sentence in parse_incr(_a ): lowercase : Optional[Any] = [] lowercase : str = [] for token in sentence: words.append(token["form"] ) labels.append(token["upos"] ) assert len(_a ) == len(_a ) if words: examples.append(InputExample(guid=f"""{mode}-{guid_index}""" , words=_a , labels=_a ) ) guid_index += 1 return examples def __magic_name__ ( self , _a , _a , _a ): lowercase : str = 0 for sentence in parse_incr(_a ): lowercase : List[Any] = preds_list[example_id] lowercase : List[str] = "" for token in sentence: out += f"""{token["form"]} ({token["upos"]}|{s_p.pop(0 )}) """ out += "\n" writer.write(_a ) example_id += 1 def __magic_name__ ( self , _a ): if path: with open(_a , "r" ) as f: return f.read().splitlines() else: return [ "ADJ", "ADP", "ADV", "AUX", "CCONJ", "DET", "INTJ", "NOUN", "NUM", "PART", "PRON", "PROPN", "PUNCT", "SCONJ", "SYM", "VERB", "X", ]
202
1
from dataclasses import dataclass, field from typing import Tuple from ..utils import cached_property, is_torch_available, is_torch_tpu_available, logging, requires_backends from .benchmark_args_utils import BenchmarkArguments if is_torch_available(): import torch if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm A : Optional[int] = logging.get_logger(__name__) @dataclass class __A( a ): snake_case_ = [ '''no_inference''', '''no_cuda''', '''no_tpu''', '''no_speed''', '''no_memory''', '''no_env_print''', '''no_multi_process''', ] def __init__( self , **_snake_case ) -> Union[str, Any]: '''simple docstring''' for deprecated_arg in self.deprecated_args: if deprecated_arg in kwargs: __a = deprecated_arg[3:] setattr(self , _snake_case , not kwargs.pop(_snake_case ) ) logger.warning( F"""{deprecated_arg} is depreciated. Please use --no_{positive_arg} or""" F""" {positive_arg}={kwargs[positive_arg]}""" ) __a = kwargs.pop('''torchscript''' , self.torchscript ) __a = kwargs.pop('''torch_xla_tpu_print_metrics''' , self.torch_xla_tpu_print_metrics ) __a = kwargs.pop('''fp16_opt_level''' , self.fpaa_opt_level ) super().__init__(**_snake_case ) snake_case_ = field(default=a , metadata={'''help''': '''Trace the models using torchscript'''} ) snake_case_ = field(default=a , metadata={'''help''': '''Print Xla/PyTorch tpu metrics'''} ) snake_case_ = field( default='''O1''' , metadata={ '''help''': ( '''For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\']. ''' '''See details at https://nvidia.github.io/apex/amp.html''' ) } , ) @cached_property def SCREAMING_SNAKE_CASE_ ( self ) -> Tuple["torch.device", int]: '''simple docstring''' requires_backends(self , ['''torch'''] ) logger.info('''PyTorch: setting up devices''' ) if not self.cuda: __a = torch.device('''cpu''' ) __a = 0 elif is_torch_tpu_available(): __a = xm.xla_device() __a = 0 else: __a = torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''' ) __a = torch.cuda.device_count() return device, n_gpu @property def SCREAMING_SNAKE_CASE_ ( self ) -> int: '''simple docstring''' return is_torch_tpu_available() and self.tpu @property def SCREAMING_SNAKE_CASE_ ( self ) -> int: '''simple docstring''' requires_backends(self , ['''torch'''] ) # TODO(PVP): currently only single GPU is supported return torch.cuda.current_device() @property def SCREAMING_SNAKE_CASE_ ( self ) -> "torch.device": '''simple docstring''' requires_backends(self , ['''torch'''] ) return self._setup_devices[0] @property def SCREAMING_SNAKE_CASE_ ( self ) -> Any: '''simple docstring''' requires_backends(self , ['''torch'''] ) return self._setup_devices[1] @property def SCREAMING_SNAKE_CASE_ ( self ) -> Optional[int]: '''simple docstring''' return self.n_gpu > 0
33
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A : Dict = { 'configuration_instructblip': [ 'INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'InstructBlipConfig', 'InstructBlipQFormerConfig', 'InstructBlipVisionConfig', ], 'processing_instructblip': ['InstructBlipProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Union[str, Any] = [ 'INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'InstructBlipQFormerModel', 'InstructBlipPreTrainedModel', 'InstructBlipForConditionalGeneration', 'InstructBlipVisionModel', ] if TYPE_CHECKING: from .configuration_instructblip import ( INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, InstructBlipConfig, InstructBlipQFormerConfig, InstructBlipVisionConfig, ) from .processing_instructblip import InstructBlipProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_instructblip import ( INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST, InstructBlipForConditionalGeneration, InstructBlipPreTrainedModel, InstructBlipQFormerModel, InstructBlipVisionModel, ) else: import sys A : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
33
1
'''simple docstring''' import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging a_ : Dict = logging.get_logger(__name__) class __UpperCamelCase ( UpperCAmelCase__ ): def __init__( self, lowerCAmelCase ): """simple docstring""" super().__init__() lowerCamelCase_ =nn.ModuleList(lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = None, lowerCAmelCase = False, lowerCAmelCase = True, ): """simple docstring""" for i, (image, scale, controlnet) in enumerate(zip(lowerCAmelCase, lowerCAmelCase, self.nets ) ): lowerCamelCase_, lowerCamelCase_ =controlnet( lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, ) # merge samples if i == 0: lowerCamelCase_, lowerCamelCase_ =down_samples, mid_sample else: lowerCamelCase_ =[ samples_prev + samples_curr for samples_prev, samples_curr in zip(lowerCAmelCase, lowerCAmelCase ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase = True, lowerCAmelCase = None, lowerCAmelCase = False, lowerCAmelCase = None, ): """simple docstring""" lowerCamelCase_ =0 lowerCamelCase_ =save_directory for controlnet in self.nets: controlnet.save_pretrained( lowerCAmelCase, is_main_process=lowerCAmelCase, save_function=lowerCAmelCase, safe_serialization=lowerCAmelCase, variant=lowerCAmelCase, ) idx += 1 lowerCamelCase_ =model_path_to_save + f'''_{idx}''' @classmethod def lowercase__ ( cls, lowerCAmelCase, **lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =0 lowerCamelCase_ =[] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... lowerCamelCase_ =pretrained_model_path while os.path.isdir(lowerCAmelCase ): lowerCamelCase_ =ControlNetModel.from_pretrained(lowerCAmelCase, **lowerCAmelCase ) controlnets.append(lowerCAmelCase ) idx += 1 lowerCamelCase_ =pretrained_model_path + f'''_{idx}''' logger.info(f'''{len(lowerCAmelCase )} controlnets loaded from {pretrained_model_path}.''' ) if len(lowerCAmelCase ) == 0: raise ValueError( f'''No ControlNets found under {os.path.dirname(lowerCAmelCase )}. Expected at least {pretrained_model_path + '_0'}.''' ) return cls(lowerCAmelCase )
75
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Optional[int]: if b == 0: return 1 if (b % 2) == 0: return actual_power(UpperCamelCase__ , int(b / 2 ) ) * actual_power(UpperCamelCase__ , int(b / 2 ) ) else: return a * actual_power(UpperCamelCase__ , int(b / 2 ) ) * actual_power(UpperCamelCase__ , int(b / 2 ) ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> float: if b < 0: return 1 / actual_power(UpperCamelCase__ , UpperCamelCase__ ) return actual_power(UpperCamelCase__ , UpperCamelCase__ ) if __name__ == "__main__": print(power(-2, -3))
67
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __A = logging.get_logger(__name__) __A = { "facebook/convnextv2-tiny-1k-224": "https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json", } class lowerCamelCase__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase = '''convnextv2''' def __init__( self , __UpperCAmelCase=3 , __UpperCAmelCase=4 , __UpperCAmelCase=4 , __UpperCAmelCase=None , __UpperCAmelCase=None , __UpperCAmelCase="gelu" , __UpperCAmelCase=0.0_2 , __UpperCAmelCase=1e-12 , __UpperCAmelCase=0.0 , __UpperCAmelCase=2_24 , __UpperCAmelCase=None , __UpperCAmelCase=None , **__UpperCAmelCase , ) -> Union[str, Any]: super().__init__(**__UpperCAmelCase ) _lowerCAmelCase =num_channels _lowerCAmelCase =patch_size _lowerCAmelCase =num_stages _lowerCAmelCase =[96, 1_92, 3_84, 7_68] if hidden_sizes is None else hidden_sizes _lowerCAmelCase =[3, 3, 9, 3] if depths is None else depths _lowerCAmelCase =hidden_act _lowerCAmelCase =initializer_range _lowerCAmelCase =layer_norm_eps _lowerCAmelCase =drop_path_rate _lowerCAmelCase =image_size _lowerCAmelCase =["""stem"""] + [f'''stage{idx}''' for idx in range(1 , len(self.depths ) + 1 )] _lowerCAmelCase , _lowerCAmelCase =get_aligned_output_features_output_indices( out_features=__UpperCAmelCase , out_indices=__UpperCAmelCase , stage_names=self.stage_names )
366
"""simple docstring""" from __future__ import annotations def _lowerCamelCase(__UpperCamelCase ) -> bool: _lowerCAmelCase =str(__UpperCamelCase ) return n == n[::-1] def _lowerCamelCase(__UpperCamelCase = 1000000 ) -> str: _lowerCAmelCase =0 for i in range(1 , __UpperCamelCase ): if is_palindrome(__UpperCamelCase ) and is_palindrome(bin(__UpperCamelCase ).split("""b""" )[1] ): total += i return total if __name__ == "__main__": print(solution(int(str(input().strip()))))
341
0
from __future__ import annotations def __snake_case ( __UpperCamelCase : int ): """simple docstring""" A_ = str(__UpperCamelCase ) return len(__UpperCamelCase ) == 9 and set(__UpperCamelCase ) == set("123456789" ) def __snake_case ( ): """simple docstring""" for base_num in range(9999 ,4999 ,-1 ): A_ = 10_0002 * base_num if is_9_pandigital(__UpperCamelCase ): return candidate for base_num in range(333 ,99 ,-1 ): A_ = 100_2003 * base_num if is_9_pandigital(__UpperCamelCase ): return candidate return None if __name__ == "__main__": print(F"{solution() = }")
312
from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def __snake_case ( __UpperCamelCase : NDArray[floataa] ,__UpperCamelCase : NDArray[floataa] ,__UpperCamelCase : list[int] ,__UpperCamelCase : int ,): """simple docstring""" A_ , A_ = coefficient_matrix.shape A_ , A_ = constant_matrix.shape if rowsa != colsa: A_ = f'''Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}''' raise ValueError(__UpperCamelCase ) if colsa != 1: A_ = f'''Constant matrix must be nx1 but received {rowsa}x{colsa}''' raise ValueError(__UpperCamelCase ) if rowsa != rowsa: A_ = ( "Coefficient and constant matrices dimensions must be nxn and nx1 but " f'''received {rowsa}x{colsa} and {rowsa}x{colsa}''' ) raise ValueError(__UpperCamelCase ) if len(__UpperCamelCase ) != rowsa: A_ = ( "Number of initial values must be equal to number of rows in coefficient " f'''matrix but received {len(__UpperCamelCase )} and {rowsa}''' ) raise ValueError(__UpperCamelCase ) if iterations <= 0: raise ValueError("Iterations must be at least 1" ) A_ = np.concatenate( (coefficient_matrix, constant_matrix) ,axis=1 ) A_ , A_ = table.shape strictly_diagonally_dominant(__UpperCamelCase ) # Iterates the whole matrix for given number of times for _ in range(__UpperCamelCase ): A_ = [] for row in range(__UpperCamelCase ): A_ = 0 for col in range(__UpperCamelCase ): if col == row: A_ = table[row][col] elif col == cols - 1: A_ = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] A_ = (temp + val) / denom new_val.append(__UpperCamelCase ) A_ = new_val return [float(__UpperCamelCase ) for i in new_val] def __snake_case ( __UpperCamelCase : NDArray[floataa] ): """simple docstring""" A_ , A_ = table.shape A_ = True for i in range(0 ,__UpperCamelCase ): A_ = 0 for j in range(0 ,cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError("Coefficient matrix is not strictly diagonally dominant" ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
312
1
import sys __lowercase = ( '''73167176531330624919225119674426574742355349194934''' '''96983520312774506326239578318016984801869478851843''' '''85861560789112949495459501737958331952853208805511''' '''12540698747158523863050715693290963295227443043557''' '''66896648950445244523161731856403098711121722383113''' '''62229893423380308135336276614282806444486645238749''' '''30358907296290491560440772390713810515859307960866''' '''70172427121883998797908792274921901699720888093776''' '''65727333001053367881220235421809751254540594752243''' '''52584907711670556013604839586446706324415722155397''' '''53697817977846174064955149290862569321978468622482''' '''83972241375657056057490261407972968652414535100474''' '''82166370484403199890008895243450658541227588666881''' '''16427171479924442928230863465674813919123162824586''' '''17866458359124566529476545682848912883142607690042''' '''24219022671055626321111109370544217506941658960408''' '''07198403850962455444362981230987879927244284909188''' '''84580156166097919133875499200524063689912560717606''' '''05886116467109405077541002256983155200055935729725''' '''71636269561882670428252483600823257530420752963450''' ) def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :int = 1 for digit in s: product *= int(SCREAMING_SNAKE_CASE ) return product def lowerCamelCase ( SCREAMING_SNAKE_CASE = N ): '''simple docstring''' __UpperCamelCase :List[Any] = -sys.maxsize - 1 __UpperCamelCase :List[str] = n[:13] __UpperCamelCase :Union[str, Any] = 13 while cur_index < len(SCREAMING_SNAKE_CASE ) - 13: if int(n[cur_index] ) >= int(substr[0] ): __UpperCamelCase :Any = substr[1:] + n[cur_index] cur_index += 1 else: __UpperCamelCase :Optional[Any] = max(SCREAMING_SNAKE_CASE , str_eval(SCREAMING_SNAKE_CASE ) ) __UpperCamelCase :Tuple = n[cur_index : cur_index + 13] cur_index += 13 return largest_product if __name__ == "__main__": print(F'{solution() = }')
351
# 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 copy import importlib.metadata import json import os from dataclasses import dataclass from typing import Any, Dict, Union from packaging import version from ..utils import is_torch_available, logging if is_torch_available(): import torch __lowercase = logging.get_logger(__name__) @dataclass class lowerCamelCase_ : '''simple docstring''' def __init__( self , __lowercase=False , __lowercase=False , __lowercase=6.0 , __lowercase=None , __lowercase=False , __lowercase=False , __lowercase=None , __lowercase="fp4" , __lowercase=False , **__lowercase , ) -> Tuple: __UpperCamelCase :List[str] = load_in_abit __UpperCamelCase :Union[str, Any] = load_in_abit __UpperCamelCase :str = llm_inta_threshold __UpperCamelCase :List[str] = llm_inta_skip_modules __UpperCamelCase :Any = llm_inta_enable_fpaa_cpu_offload __UpperCamelCase :List[Any] = llm_inta_has_fpaa_weight __UpperCamelCase :str = bnb_abit_quant_type __UpperCamelCase :Optional[int] = bnb_abit_use_double_quant if bnb_abit_compute_dtype is None: __UpperCamelCase :Tuple = torch.floataa elif isinstance(__lowercase , __lowercase): __UpperCamelCase :Union[str, Any] = getattr(__lowercase , __lowercase) elif isinstance(__lowercase , torch.dtype): __UpperCamelCase :int = bnb_abit_compute_dtype else: raise ValueError('''bnb_4bit_compute_dtype must be a string or a torch.dtype''') self.post_init() def UpperCamelCase__ ( self) -> Union[str, Any]: if not isinstance(self.llm_inta_threshold , __lowercase): raise ValueError('''llm_int8_threshold must be a float''') if self.llm_inta_skip_modules is not None and not isinstance(self.llm_inta_skip_modules , __lowercase): raise ValueError('''llm_int8_skip_modules must be a list of strings''') if not isinstance(self.llm_inta_enable_fpaa_cpu_offload , __lowercase): raise ValueError('''llm_int8_enable_fp32_cpu_offload must be a boolean''') if not isinstance(self.llm_inta_has_fpaa_weight , __lowercase): raise ValueError('''llm_int8_has_fp16_weight must be a boolean''') if self.bnb_abit_compute_dtype is not None and not isinstance(self.bnb_abit_compute_dtype , torch.dtype): raise ValueError('''bnb_4bit_compute_dtype must be torch.dtype''') if not isinstance(self.bnb_abit_quant_type , __lowercase): raise ValueError('''bnb_4bit_quant_type must be a string''') if not isinstance(self.bnb_abit_use_double_quant , __lowercase): raise ValueError('''bnb_4bit_use_double_quant must be a boolean''') if self.load_in_abit and not version.parse(importlib.metadata.version('''bitsandbytes''')) >= version.parse( '''0.39.0'''): raise ValueError( '''4 bit quantization requires bitsandbytes>=0.39.0 - please upgrade your bitsandbytes version''') def UpperCamelCase__ ( self) -> Any: return self.load_in_abit or self.load_in_abit def UpperCamelCase__ ( self) -> List[Any]: if self.load_in_abit: return "llm_int8" elif self.load_in_abit and self.bnb_abit_quant_type == "fp4": return "fp4" elif self.load_in_abit and self.bnb_abit_quant_type == "nf4": return "nf4" else: return None @classmethod def UpperCamelCase__ ( cls , __lowercase , __lowercase , **__lowercase) -> List[str]: __UpperCamelCase :Optional[int] = cls(**__lowercase) __UpperCamelCase :Optional[Any] = [] for key, value in kwargs.items(): if hasattr(__lowercase , __lowercase): setattr(__lowercase , __lowercase , __lowercase) to_remove.append(__lowercase) for key in to_remove: kwargs.pop(__lowercase , __lowercase) if return_unused_kwargs: return config, kwargs else: return config def UpperCamelCase__ ( self , __lowercase) -> Union[str, Any]: with open(__lowercase , '''w''' , encoding='''utf-8''') as writer: __UpperCamelCase :Optional[int] = self.to_dict() __UpperCamelCase :Optional[int] = json.dumps(__lowercase , indent=2 , sort_keys=__lowercase) + '''\n''' writer.write(__lowercase) def UpperCamelCase__ ( self) -> Dict[str, Any]: __UpperCamelCase :Optional[Any] = copy.deepcopy(self.__dict__) __UpperCamelCase :Optional[int] = str(output['''bnb_4bit_compute_dtype''']).split('''.''')[1] return output def __repr__( self) -> Dict: return f"""{self.__class__.__name__} {self.to_json_string()}""" def UpperCamelCase__ ( self , __lowercase = True) -> str: if use_diff is True: __UpperCamelCase :Union[str, Any] = self.to_diff_dict() else: __UpperCamelCase :Dict = self.to_dict() return json.dumps(__lowercase , indent=2 , sort_keys=__lowercase) + "\n" def UpperCamelCase__ ( self) -> Dict[str, Any]: __UpperCamelCase :Union[str, Any] = self.to_dict() # get the default config dict __UpperCamelCase :Optional[Any] = BitsAndBytesConfig().to_dict() __UpperCamelCase :str = {} # only serialize values that differ from the default config for key, value in config_dict.items(): if value != default_config_dict[key]: __UpperCamelCase :str = value return serializable_config_dict
105
0
from typing import Dict, List, Optional, Tuple, Union import numpy as np 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, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, logging if is_torch_available(): import torch A__ = logging.get_logger(__name__) class __lowerCAmelCase ( lowerCamelCase__ ): __lowerCamelCase = ['''pixel_values'''] def __init__( self , _snake_case = True , _snake_case = None , _snake_case = PILImageResampling.BILINEAR , _snake_case = True , _snake_case = None , _snake_case = True , _snake_case = 1 / 255 , _snake_case = True , _snake_case = None , _snake_case = None , **_snake_case , ): """simple docstring""" super().__init__(**_snake_case ) _lowerCAmelCase = size if size is not None else {"""shortest_edge""": 256} _lowerCAmelCase = get_size_dict(_snake_case , default_to_square=_snake_case ) _lowerCAmelCase = crop_size if crop_size is not None else {"""height""": 224, """width""": 224} _lowerCAmelCase = get_size_dict(_snake_case , param_name="""crop_size""" ) _lowerCAmelCase = do_resize _lowerCAmelCase = size _lowerCAmelCase = resample _lowerCAmelCase = do_center_crop _lowerCAmelCase = crop_size _lowerCAmelCase = do_rescale _lowerCAmelCase = rescale_factor _lowerCAmelCase = do_normalize _lowerCAmelCase = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN _lowerCAmelCase = image_std if image_std is not None else IMAGENET_STANDARD_STD def snake_case ( self , _snake_case , _snake_case , _snake_case = PILImageResampling.BICUBIC , _snake_case = None , **_snake_case , ): """simple docstring""" _lowerCAmelCase = get_size_dict(_snake_case , default_to_square=_snake_case ) if "shortest_edge" not in size: raise ValueError(F'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}' ) _lowerCAmelCase = get_resize_output_image_size(_snake_case , size=size["""shortest_edge"""] , default_to_square=_snake_case ) return resize(_snake_case , size=_snake_case , resample=_snake_case , data_format=_snake_case , **_snake_case ) def snake_case ( self , _snake_case , _snake_case , _snake_case = None , **_snake_case , ): """simple docstring""" _lowerCAmelCase = get_size_dict(_snake_case ) if "height" not in size or "width" not in size: raise ValueError(F'The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}' ) return center_crop(_snake_case , size=(size["""height"""], size["""width"""]) , data_format=_snake_case , **_snake_case ) def snake_case ( self , _snake_case , _snake_case , _snake_case = None , **_snake_case ): """simple docstring""" return rescale(_snake_case , scale=_snake_case , data_format=_snake_case , **_snake_case ) def snake_case ( self , _snake_case , _snake_case , _snake_case , _snake_case = None , **_snake_case , ): """simple docstring""" return normalize(_snake_case , mean=_snake_case , std=_snake_case , data_format=_snake_case , **_snake_case ) def snake_case ( self , _snake_case , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = None , _snake_case = ChannelDimension.FIRST , **_snake_case , ): """simple docstring""" _lowerCAmelCase = do_resize if do_resize is not None else self.do_resize _lowerCAmelCase = size if size is not None else self.size _lowerCAmelCase = get_size_dict(_snake_case , default_to_square=_snake_case ) _lowerCAmelCase = resample if resample is not None else self.resample _lowerCAmelCase = do_center_crop if do_center_crop is not None else self.do_center_crop _lowerCAmelCase = crop_size if crop_size is not None else self.crop_size _lowerCAmelCase = get_size_dict(_snake_case , param_name="""crop_size""" ) _lowerCAmelCase = do_rescale if do_rescale is not None else self.do_rescale _lowerCAmelCase = rescale_factor if rescale_factor is not None else self.rescale_factor _lowerCAmelCase = do_normalize if do_normalize is not None else self.do_normalize _lowerCAmelCase = image_mean if image_mean is not None else self.image_mean _lowerCAmelCase = image_std if image_std is not None else self.image_std _lowerCAmelCase = make_list_of_images(_snake_case ) if not valid_images(_snake_case ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size 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.""" ) # All transformations expect numpy arrays. _lowerCAmelCase = [to_numpy_array(_snake_case ) for image in images] if do_resize: _lowerCAmelCase = [self.resize(image=_snake_case , size=_snake_case , resample=_snake_case ) for image in images] if do_center_crop: _lowerCAmelCase = [self.center_crop(image=_snake_case , size=_snake_case ) for image in images] if do_rescale: _lowerCAmelCase = [self.rescale(image=_snake_case , scale=_snake_case ) for image in images] if do_normalize: _lowerCAmelCase = [self.normalize(image=_snake_case , mean=_snake_case , std=_snake_case ) for image in images] _lowerCAmelCase = [to_channel_dimension_format(_snake_case , _snake_case ) for image in images] _lowerCAmelCase = {"""pixel_values""": images} return BatchFeature(data=_snake_case , tensor_type=_snake_case ) def snake_case ( self , _snake_case , _snake_case = None ): """simple docstring""" _lowerCAmelCase = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(_snake_case ) != len(_snake_case ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(_snake_case ): _lowerCAmelCase = target_sizes.numpy() _lowerCAmelCase = [] for idx in range(len(_snake_case ) ): _lowerCAmelCase = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode="""bilinear""" , align_corners=_snake_case ) _lowerCAmelCase = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(_snake_case ) else: _lowerCAmelCase = logits.argmax(dim=1 ) _lowerCAmelCase = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
82
'''simple docstring''' import json import os from typing import Dict, List, Optional, Tuple import regex as re from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging a_ : Optional[int] = logging.get_logger(__name__) a_ : Optional[int] = { """vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_config_file""": """tokenizer_config.json""", } a_ : List[Any] = { """vocab_file""": { """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json""" }, """merges_file""": { """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt""" }, """tokenizer_config_file""": { """facebook/blenderbot_small-90M""": ( """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json""" ) }, } a_ : Optional[int] = {"""facebook/blenderbot_small-90M""": 5_12} def a_ ( __snake_case : List[Any] ) -> Tuple: """simple docstring""" lowerCamelCase_ =set() lowerCamelCase_ =word[0] for char in word[1:]: pairs.add((prev_char, char) ) lowerCamelCase_ =char lowerCamelCase_ =set(__snake_case ) return pairs class __UpperCamelCase ( lowerCamelCase__ ): lowercase : Optional[int] =VOCAB_FILES_NAMES lowercase : Tuple =PRETRAINED_VOCAB_FILES_MAP lowercase : Tuple =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase : Dict =['input_ids', 'attention_mask'] def __init__( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase="__start__", lowerCAmelCase="__end__", lowerCAmelCase="__unk__", lowerCAmelCase="__null__", **lowerCAmelCase, ): """simple docstring""" super().__init__(unk_token=lowerCAmelCase, bos_token=lowerCAmelCase, eos_token=lowerCAmelCase, pad_token=lowerCAmelCase, **lowerCAmelCase ) with open(lowerCAmelCase, encoding='''utf-8''' ) as vocab_handle: lowerCamelCase_ =json.load(lowerCAmelCase ) lowerCamelCase_ ={v: k for k, v in self.encoder.items()} with open(lowerCAmelCase, encoding='''utf-8''' ) as merges_handle: lowerCamelCase_ =merges_handle.read().split('''\n''' )[1:-1] lowerCamelCase_ =[tuple(merge.split() ) for merge in merges] lowerCamelCase_ =dict(zip(lowerCAmelCase, range(len(lowerCAmelCase ) ) ) ) lowerCamelCase_ ={} @property def lowercase__ ( self ): """simple docstring""" return len(self.encoder ) def lowercase__ ( self ): """simple docstring""" return dict(self.encoder, **self.added_tokens_encoder ) def lowercase__ ( self, lowerCAmelCase ): """simple docstring""" if token in self.cache: return self.cache[token] lowerCamelCase_ =re.sub('''([.,!?()])''', R''' \1''', lowerCAmelCase ) lowerCamelCase_ =re.sub('''(\')''', R''' \1 ''', lowerCAmelCase ) lowerCamelCase_ =re.sub(R'''\s{2,}''', ''' ''', lowerCAmelCase ) if "\n" in token: lowerCamelCase_ =token.replace('''\n''', ''' __newln__''' ) lowerCamelCase_ =token.split(''' ''' ) lowerCamelCase_ =[] for token in tokens: if not len(lowerCAmelCase ): continue lowerCamelCase_ =token.lower() lowerCamelCase_ =tuple(lowerCAmelCase ) lowerCamelCase_ =tuple(list(word[:-1] ) + [word[-1] + '''</w>'''] ) lowerCamelCase_ =get_pairs(lowerCAmelCase ) if not pairs: words.append(lowerCAmelCase ) continue while True: lowerCamelCase_ =min(lowerCAmelCase, key=lambda lowerCAmelCase : self.bpe_ranks.get(lowerCAmelCase, float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break lowerCamelCase_, lowerCamelCase_ =bigram lowerCamelCase_ =[] lowerCamelCase_ =0 while i < len(lowerCAmelCase ): try: lowerCamelCase_ =word.index(lowerCAmelCase, lowerCAmelCase ) new_word.extend(word[i:j] ) lowerCamelCase_ =j except ValueError: new_word.extend(word[i:] ) break if word[i] == first and i < len(lowerCAmelCase ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 lowerCamelCase_ =tuple(lowerCAmelCase ) lowerCamelCase_ =new_word if len(lowerCAmelCase ) == 1: break else: lowerCamelCase_ =get_pairs(lowerCAmelCase ) lowerCamelCase_ ='''@@ '''.join(lowerCAmelCase ) lowerCamelCase_ =word[:-4] lowerCamelCase_ =word words.append(lowerCAmelCase ) return " ".join(lowerCAmelCase ) def lowercase__ ( self, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =[] lowerCamelCase_ =re.findall(R'''\S+\n?''', lowerCAmelCase ) for token in words: split_tokens.extend(list(self.bpe(lowerCAmelCase ).split(''' ''' ) ) ) return split_tokens def lowercase__ ( self, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =token.lower() return self.encoder.get(lowerCAmelCase, self.encoder.get(self.unk_token ) ) def lowercase__ ( self, lowerCAmelCase ): """simple docstring""" return self.decoder.get(lowerCAmelCase, self.unk_token ) def lowercase__ ( self, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =''' '''.join(lowerCAmelCase ).replace('''@@ ''', '''''' ).strip() return out_string def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase = None ): """simple docstring""" if not os.path.isdir(lowerCAmelCase ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return lowerCamelCase_ =os.path.join( lowerCAmelCase, (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) lowerCamelCase_ =os.path.join( lowerCAmelCase, (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file'''] ) with open(lowerCAmelCase, '''w''', encoding='''utf-8''' ) as f: f.write(json.dumps(self.encoder, indent=2, sort_keys=lowerCAmelCase, ensure_ascii=lowerCAmelCase ) + '''\n''' ) lowerCamelCase_ =0 with open(lowerCAmelCase, '''w''', encoding='''utf-8''' ) as writer: writer.write('''#version: 0.2\n''' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items(), key=lambda lowerCAmelCase : kv[1] ): if index != token_index: logger.warning( f'''Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.''' ''' Please check that the tokenizer is not corrupted!''' ) lowerCamelCase_ =token_index writer.write(''' '''.join(lowerCAmelCase ) + '''\n''' ) index += 1 return vocab_file, merge_file
75
0
"""simple docstring""" import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class UpperCamelCase ( unittest.TestCase ): def a_ ( self) -> List[Any]: snake_case_ = tempfile.mkdtemp() # fmt: off snake_case_ = ['l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>'] # fmt: on snake_case_ = dict(zip(lowerCAmelCase__, range(len(lowerCAmelCase__)))) snake_case_ = ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>', ''] snake_case_ = {'unk_token': '<unk>'} snake_case_ = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES['vocab_file']) snake_case_ = 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__)) snake_case_ = { 'do_resize': True, 'size': 20, 'do_center_crop': True, 'crop_size': 18, 'do_normalize': True, 'image_mean': [0.48145466, 0.4578275, 0.40821073], 'image_std': [0.26862954, 0.26130258, 0.27577711], } snake_case_ = os.path.join(self.tmpdirname, lowerCAmelCase__) with open(self.image_processor_file, 'w', encoding='utf-8') as fp: json.dump(lowerCAmelCase__, lowerCAmelCase__) def a_ ( self, **lowerCAmelCase__) -> Tuple: return CLIPTokenizer.from_pretrained(self.tmpdirname, **lowerCAmelCase__) def a_ ( self, **lowerCAmelCase__) -> int: return CLIPTokenizerFast.from_pretrained(self.tmpdirname, **lowerCAmelCase__) def a_ ( self, **lowerCAmelCase__) -> Dict: return CLIPImageProcessor.from_pretrained(self.tmpdirname, **lowerCAmelCase__) def a_ ( self) -> Union[str, Any]: shutil.rmtree(self.tmpdirname) def a_ ( self) -> Tuple: snake_case_ = [np.random.randint(255, size=(3, 30, 400), dtype=np.uinta)] snake_case_ = [Image.fromarray(np.moveaxis(lowerCAmelCase__, 0, -1)) for x in image_inputs] return image_inputs def a_ ( self) -> List[str]: snake_case_ = self.get_tokenizer() snake_case_ = self.get_rust_tokenizer() snake_case_ = self.get_image_processor() snake_case_ = CLIPProcessor(tokenizer=lowerCAmelCase__, image_processor=lowerCAmelCase__) processor_slow.save_pretrained(self.tmpdirname) snake_case_ = CLIPProcessor.from_pretrained(self.tmpdirname, use_fast=lowerCAmelCase__) snake_case_ = CLIPProcessor(tokenizer=lowerCAmelCase__, image_processor=lowerCAmelCase__) processor_fast.save_pretrained(self.tmpdirname) snake_case_ = CLIPProcessor.from_pretrained(self.tmpdirname) self.assertEqual(processor_slow.tokenizer.get_vocab(), tokenizer_slow.get_vocab()) self.assertEqual(processor_fast.tokenizer.get_vocab(), tokenizer_fast.get_vocab()) self.assertEqual(tokenizer_slow.get_vocab(), tokenizer_fast.get_vocab()) self.assertIsInstance(processor_slow.tokenizer, lowerCAmelCase__) self.assertIsInstance(processor_fast.tokenizer, lowerCAmelCase__) self.assertEqual(processor_slow.image_processor.to_json_string(), image_processor.to_json_string()) self.assertEqual(processor_fast.image_processor.to_json_string(), image_processor.to_json_string()) self.assertIsInstance(processor_slow.image_processor, lowerCAmelCase__) self.assertIsInstance(processor_fast.image_processor, lowerCAmelCase__) def a_ ( self) -> Any: snake_case_ = CLIPProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) processor.save_pretrained(self.tmpdirname) snake_case_ = self.get_tokenizer(bos_token='(BOS)', eos_token='(EOS)') snake_case_ = self.get_image_processor(do_normalize=lowerCAmelCase__, padding_value=1.0) snake_case_ = CLIPProcessor.from_pretrained( self.tmpdirname, bos_token='(BOS)', eos_token='(EOS)', do_normalize=lowerCAmelCase__, padding_value=1.0) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer, lowerCAmelCase__) self.assertEqual(processor.image_processor.to_json_string(), image_processor_add_kwargs.to_json_string()) self.assertIsInstance(processor.image_processor, lowerCAmelCase__) def a_ ( self) -> Optional[int]: snake_case_ = self.get_image_processor() snake_case_ = self.get_tokenizer() snake_case_ = CLIPProcessor(tokenizer=lowerCAmelCase__, image_processor=lowerCAmelCase__) snake_case_ = self.prepare_image_inputs() snake_case_ = image_processor(lowerCAmelCase__, return_tensors='np') snake_case_ = processor(images=lowerCAmelCase__, return_tensors='np') for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum(), input_processor[key].sum(), delta=1e-2) def a_ ( self) -> Tuple: snake_case_ = self.get_image_processor() snake_case_ = self.get_tokenizer() snake_case_ = CLIPProcessor(tokenizer=lowerCAmelCase__, image_processor=lowerCAmelCase__) snake_case_ = 'lower newer' snake_case_ = processor(text=lowerCAmelCase__) snake_case_ = tokenizer(lowerCAmelCase__) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key], encoded_processor[key]) def a_ ( self) -> List[str]: snake_case_ = self.get_image_processor() snake_case_ = self.get_tokenizer() snake_case_ = CLIPProcessor(tokenizer=lowerCAmelCase__, image_processor=lowerCAmelCase__) snake_case_ = 'lower newer' snake_case_ = self.prepare_image_inputs() snake_case_ = processor(text=lowerCAmelCase__, images=lowerCAmelCase__) self.assertListEqual(list(inputs.keys()), ['input_ids', 'attention_mask', 'pixel_values']) # test if it raises when no input is passed with pytest.raises(lowerCAmelCase__): processor() def a_ ( self) -> Dict: snake_case_ = self.get_image_processor() snake_case_ = self.get_tokenizer() snake_case_ = CLIPProcessor(tokenizer=lowerCAmelCase__, image_processor=lowerCAmelCase__) snake_case_ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] snake_case_ = processor.batch_decode(lowerCAmelCase__) snake_case_ = tokenizer.batch_decode(lowerCAmelCase__) self.assertListEqual(lowerCAmelCase__, lowerCAmelCase__) def a_ ( self) -> Optional[int]: snake_case_ = self.get_image_processor() snake_case_ = self.get_tokenizer() snake_case_ = CLIPProcessor(tokenizer=lowerCAmelCase__, image_processor=lowerCAmelCase__) snake_case_ = 'lower newer' snake_case_ = self.prepare_image_inputs() snake_case_ = processor(text=lowerCAmelCase__, images=lowerCAmelCase__) self.assertListEqual(list(inputs.keys()), processor.model_input_names)
356
"""simple docstring""" import functools def UpperCAmelCase ( UpperCAmelCase , UpperCAmelCase ) -> int: # Validation if not isinstance(UpperCAmelCase , UpperCAmelCase ) or not all(isinstance(UpperCAmelCase , UpperCAmelCase ) for day in days ): raise ValueError('The parameter days should be a list of integers' ) if len(UpperCAmelCase ) != 3 or not all(isinstance(UpperCAmelCase , UpperCAmelCase ) for cost in costs ): raise ValueError('The parameter costs should be a list of three integers' ) if len(UpperCAmelCase ) == 0: return 0 if min(UpperCAmelCase ) <= 0: raise ValueError('All days elements should be greater than 0' ) if max(UpperCAmelCase ) >= 366: raise ValueError('All days elements should be less than 366' ) snake_case_ = set(UpperCAmelCase ) @functools.cache def dynamic_programming(UpperCAmelCase ) -> int: if index > 365: return 0 if index not in days_set: return dynamic_programming(index + 1 ) return min( costs[0] + dynamic_programming(index + 1 ) , costs[1] + dynamic_programming(index + 7 ) , costs[2] + dynamic_programming(index + 30 ) , ) return dynamic_programming(1 ) if __name__ == "__main__": import doctest doctest.testmod()
312
0
'''simple docstring''' from typing import Any, Dict, List, Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, ChunkPipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): import torch from transformers.modeling_outputs import BaseModelOutput from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING UpperCamelCase__ = logging.get_logger(__name__) @add_end_docstrings(snake_case_ ) class lowerCamelCase_ ( snake_case_ ): def __init__( self : int , **_A : Tuple ): '''simple docstring''' super().__init__(**_A ) if self.framework == "tf": raise ValueError(f"""The {self.__class__} is only available in PyTorch.""" ) requires_backends(self , '''vision''' ) self.check_model_type(_A ) def __call__( self : str , _A : Tuple , _A : List[Any] = None , **_A : List[str] , ): '''simple docstring''' if "text_queries" in kwargs: UpperCAmelCase__ : Optional[Any] = kwargs.pop('''text_queries''' ) if isinstance(_A , (str, Image.Image) ): UpperCAmelCase__ : int = {'''image''': image, '''candidate_labels''': candidate_labels} else: UpperCAmelCase__ : Union[str, Any] = image UpperCAmelCase__ : int = super().__call__(_A , **_A ) return results def lowercase_ ( self : List[str] , **_A : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ : Tuple = {} if "threshold" in kwargs: UpperCAmelCase__ : int = kwargs['''threshold'''] if "top_k" in kwargs: UpperCAmelCase__ : Dict = kwargs['''top_k'''] return {}, {}, postprocess_params def lowercase_ ( self : int , _A : int ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = load_image(inputs['''image'''] ) UpperCAmelCase__ : Dict = inputs['''candidate_labels'''] if isinstance(_A , _A ): UpperCAmelCase__ : List[str] = candidate_labels.split(''',''' ) UpperCAmelCase__ : Dict = torch.tensor([[image.height, image.width]] , dtype=torch.intaa ) for i, candidate_label in enumerate(_A ): UpperCAmelCase__ : List[str] = self.tokenizer(_A , return_tensors=self.framework ) UpperCAmelCase__ : List[str] = self.image_processor(_A , return_tensors=self.framework ) yield { "is_last": i == len(_A ) - 1, "target_size": target_size, "candidate_label": candidate_label, **text_inputs, **image_features, } def lowercase_ ( self : str , _A : Dict ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = model_inputs.pop('''target_size''' ) UpperCAmelCase__ : Optional[int] = model_inputs.pop('''candidate_label''' ) UpperCAmelCase__ : Dict = model_inputs.pop('''is_last''' ) UpperCAmelCase__ : List[str] = self.model(**_A ) UpperCAmelCase__ : Any = {'''target_size''': target_size, '''candidate_label''': candidate_label, '''is_last''': is_last, **outputs} return model_outputs def lowercase_ ( self : Optional[int] , _A : str , _A : Optional[Any]=0.1 , _A : str=None ): '''simple docstring''' UpperCAmelCase__ : Tuple = [] for model_output in model_outputs: UpperCAmelCase__ : Any = model_output['''candidate_label'''] UpperCAmelCase__ : str = BaseModelOutput(_A ) UpperCAmelCase__ : int = self.image_processor.post_process_object_detection( outputs=_A , threshold=_A , target_sizes=model_output['''target_size'''] )[0] for index in outputs["scores"].nonzero(): UpperCAmelCase__ : str = outputs['''scores'''][index].item() UpperCAmelCase__ : str = self._get_bounding_box(outputs['''boxes'''][index][0] ) UpperCAmelCase__ : Union[str, Any] = {'''score''': score, '''label''': label, '''box''': box} results.append(_A ) UpperCAmelCase__ : Union[str, Any] = sorted(_A , key=lambda _A : x["score"] , reverse=_A ) if top_k: UpperCAmelCase__ : Optional[int] = results[:top_k] return results def lowercase_ ( self : Any , _A : List[str] ): '''simple docstring''' if self.framework != "pt": raise ValueError('''The ZeroShotObjectDetectionPipeline is only available in PyTorch.''' ) UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ : List[Any] = box.int().tolist() UpperCAmelCase__ : Dict = { '''xmin''': xmin, '''ymin''': ymin, '''xmax''': xmax, '''ymax''': ymax, } return bbox
181
'''simple docstring''' from typing import List, Optional, Union import torch from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) lowercase_ = logging.get_logger(__name__) # pylint: disable=invalid-name lowercase_ = """ Examples: ```py >>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline >>> import torch >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\"kandinsky-community/kandinsky-2-2-prior\") >>> pipe_prior.to(\"cuda\") >>> prompt = \"red cat, 4k photo\" >>> out = pipe_prior(prompt) >>> image_emb = out.image_embeds >>> zero_image_emb = out.negative_image_embeds >>> pipe = KandinskyV22Pipeline.from_pretrained(\"kandinsky-community/kandinsky-2-2-decoder\") >>> pipe.to(\"cuda\") >>> image = pipe( ... image_embeds=image_emb, ... negative_image_embeds=zero_image_emb, ... height=768, ... width=768, ... num_inference_steps=50, ... ).images >>> image[0].save(\"cat.png\") ``` """ def lowerCamelCase ( __lowerCamelCase : str , __lowerCamelCase : Optional[Any] , __lowerCamelCase : List[Any]=8 ) ->Tuple: _SCREAMING_SNAKE_CASE = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 _SCREAMING_SNAKE_CASE = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class a_ ( snake_case_ ): '''simple docstring''' def __init__( self , A , A , A , ) -> Union[str, Any]: super().__init__() self.register_modules( unet=A , scheduler=A , movq=A , ) _SCREAMING_SNAKE_CASE = 2 ** (len(self.movq.config.block_out_channels ) - 1) def snake_case_( self , A , A , A , A , A , A ) -> Union[str, Any]: if latents is None: _SCREAMING_SNAKE_CASE = randn_tensor(A , generator=A , device=A , dtype=A ) else: if latents.shape != shape: raise ValueError(f'Unexpected latents shape, got {latents.shape}, expected {shape}' ) _SCREAMING_SNAKE_CASE = latents.to(A ) _SCREAMING_SNAKE_CASE = latents * scheduler.init_noise_sigma return latents def snake_case_( self , A=0 ) -> Dict: if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("""Please install accelerate via `pip install accelerate`""" ) _SCREAMING_SNAKE_CASE = torch.device(f'cuda:{gpu_id}' ) _SCREAMING_SNAKE_CASE = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(A , A ) def snake_case_( self , A=0 ) -> str: if is_accelerate_available() and is_accelerate_version(""">=""" , """0.17.0.dev0""" ): from accelerate import cpu_offload_with_hook else: raise ImportError("""`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.""" ) _SCREAMING_SNAKE_CASE = torch.device(f'cuda:{gpu_id}' ) if self.device.type != "cpu": self.to("""cpu""" , silence_dtype_warnings=A ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) _SCREAMING_SNAKE_CASE = None for cpu_offloaded_model in [self.unet, self.movq]: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = cpu_offload_with_hook(A , A , prev_module_hook=A ) # We'll offload the last model manually. _SCREAMING_SNAKE_CASE = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def snake_case_( self ) -> Tuple: if not hasattr(self.unet , """_hf_hook""" ): return self.device for module in self.unet.modules(): if ( hasattr(A , """_hf_hook""" ) and hasattr(module._hf_hook , """execution_device""" ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(A ) def __call__( self , A , A , A = 512 , A = 512 , A = 100 , A = 4.0 , A = 1 , A = None , A = None , A = "pil" , A = True , ) -> List[str]: _SCREAMING_SNAKE_CASE = self._execution_device _SCREAMING_SNAKE_CASE = guidance_scale > 1.0 if isinstance(A , A ): _SCREAMING_SNAKE_CASE = torch.cat(A , dim=0 ) _SCREAMING_SNAKE_CASE = image_embeds.shape[0] * num_images_per_prompt if isinstance(A , A ): _SCREAMING_SNAKE_CASE = torch.cat(A , dim=0 ) if do_classifier_free_guidance: _SCREAMING_SNAKE_CASE = image_embeds.repeat_interleave(A , dim=0 ) _SCREAMING_SNAKE_CASE = negative_image_embeds.repeat_interleave(A , dim=0 ) _SCREAMING_SNAKE_CASE = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=A ) self.scheduler.set_timesteps(A , device=A ) _SCREAMING_SNAKE_CASE = self.scheduler.timesteps _SCREAMING_SNAKE_CASE = self.unet.config.in_channels _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = downscale_height_and_width(A , A , self.movq_scale_factor ) # create initial latent _SCREAMING_SNAKE_CASE = self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , A , A , A , self.scheduler , ) for i, t in enumerate(self.progress_bar(A ) ): # expand the latents if we are doing classifier free guidance _SCREAMING_SNAKE_CASE = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _SCREAMING_SNAKE_CASE = {"""image_embeds""": image_embeds} _SCREAMING_SNAKE_CASE = self.unet( sample=A , timestep=A , encoder_hidden_states=A , added_cond_kwargs=A , return_dict=A , )[0] if do_classifier_free_guidance: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = noise_pred.split(latents.shape[1] , dim=1 ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = noise_pred.chunk(2 ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = variance_pred.chunk(2 ) _SCREAMING_SNAKE_CASE = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) _SCREAMING_SNAKE_CASE = 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 , _SCREAMING_SNAKE_CASE = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 _SCREAMING_SNAKE_CASE = self.scheduler.step( A , A , A , generator=A , )[0] # post-processing _SCREAMING_SNAKE_CASE = self.movq.decode(A , force_not_quantize=A )["""sample"""] if output_type not in ["pt", "np", "pil"]: raise ValueError(f'Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}' ) if output_type in ["np", "pil"]: _SCREAMING_SNAKE_CASE = image * 0.5 + 0.5 _SCREAMING_SNAKE_CASE = image.clamp(0 , 1 ) _SCREAMING_SNAKE_CASE = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": _SCREAMING_SNAKE_CASE = self.numpy_to_pil(A ) if not return_dict: return (image,) return ImagePipelineOutput(images=A )
58
0
from math import sqrt def _lowerCAmelCase ( lowerCamelCase_ : int ): __lowercase = 0 for i in range(1 , int(sqrt(lowerCamelCase_ ) + 1 ) ): if n % i == 0 and i != sqrt(lowerCamelCase_ ): total += i + n // i elif i == sqrt(lowerCamelCase_ ): total += i return total - n def _lowerCAmelCase ( lowerCamelCase_ : int = 1_0_0_0_0 ): __lowercase = sum( i for i in range(1 , lowerCamelCase_ ) if sum_of_divisors(sum_of_divisors(lowerCamelCase_ ) ) == i and sum_of_divisors(lowerCamelCase_ ) != i ) return total if __name__ == "__main__": print(solution(int(str(input()).strip())))
367
'''simple docstring''' def _lowerCAmelCase ( lowerCamelCase_ : int = 6_0_0_8_5_1_4_7_5_1_4_3 ): try: __lowercase = int(lowerCamelCase_ ) except (TypeError, ValueError): raise TypeError('''Parameter n must be int or castable to int.''' ) if n <= 0: raise ValueError('''Parameter n must be greater than or equal to one.''' ) __lowercase = 1 __lowercase = 2 while i * i <= n: while n % i == 0: __lowercase = i n //= i i += 1 if n > 1: __lowercase = n return int(lowerCamelCase_ ) if __name__ == "__main__": print(f'''{solution() = }''')
217
0
import math def _UpperCAmelCase ( snake_case ): """simple docstring""" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(snake_case ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def _UpperCAmelCase ( snake_case = 0.1 ): """simple docstring""" _lowerCAmelCase = 3 _lowerCAmelCase = 3 while primes / (2 * j - 1) >= ratio: for i in range(j * j + j + 1 , (j + 2) * (j + 2) , j + 1 ): primes += is_prime(snake_case ) j += 2 return j if __name__ == "__main__": import doctest doctest.testmod()
82
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(__UpperCamelCase ) class __a ( __UpperCamelCase ): def __init__( self , **lowerCAmelCase__ ) -> List[str]: '''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 , lowerCAmelCase__ , **lowerCAmelCase__ ) -> Optional[Any]: '''simple docstring''' return super().__call__(lowerCAmelCase__ , **lowerCAmelCase__ ) def SCREAMING_SNAKE_CASE__ ( self , **lowerCAmelCase__ ) -> Dict: '''simple docstring''' lowercase__: int = {} if "candidate_labels" in kwargs: lowercase__: Dict = kwargs['candidate_labels'] if "hypothesis_template" in kwargs: lowercase__: List[Any] = kwargs['hypothesis_template'] return preprocess_params, {}, {} def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ , lowerCAmelCase__=None , lowerCAmelCase__="This is a photo of {}." ) -> int: '''simple docstring''' lowercase__: Optional[int] = load_image(lowerCAmelCase__ ) lowercase__: Dict = self.image_processor(images=[image] , return_tensors=self.framework ) lowercase__: Tuple = candidate_labels lowercase__: List[str] = [hypothesis_template.format(lowerCAmelCase__ ) for x in candidate_labels] lowercase__: Optional[Any] = self.tokenizer(lowerCAmelCase__ , return_tensors=self.framework , padding=lowerCAmelCase__ ) lowercase__: str = [text_inputs] return inputs def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ ) -> int: '''simple docstring''' lowercase__: str = model_inputs.pop('candidate_labels' ) lowercase__: List[str] = model_inputs.pop('text_inputs' ) if isinstance(text_inputs[0] , lowerCAmelCase__ ): lowercase__: Any = text_inputs[0] else: # Batching case. lowercase__: Optional[int] = text_inputs[0][0] lowercase__: Tuple = self.model(**lowerCAmelCase__ , **lowerCAmelCase__ ) lowercase__: Any = { 'candidate_labels': candidate_labels, 'logits': outputs.logits_per_image, } return model_outputs def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ ) -> Union[str, Any]: '''simple docstring''' lowercase__: Tuple = model_outputs.pop('candidate_labels' ) lowercase__: Dict = model_outputs['logits'][0] if self.framework == "pt": lowercase__: Optional[int] = logits.softmax(dim=-1 ).squeeze(-1 ) lowercase__: Dict = probs.tolist() if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): lowercase__: Dict = [scores] elif self.framework == "tf": lowercase__: Optional[int] = stable_softmax(lowerCAmelCase__ , axis=-1 ) lowercase__: Union[str, Any] = probs.numpy().tolist() else: raise ValueError(F'Unsupported framework: {self.framework}' ) lowercase__: List[Any] = [ {'score': score, 'label': candidate_label} for score, candidate_label in sorted(zip(lowerCAmelCase__ , lowerCAmelCase__ ) , key=lambda lowerCAmelCase__ : -x[0] ) ] return result
196
0
'''simple docstring''' import datasets from .evaluate import evaluate __SCREAMING_SNAKE_CASE :Optional[int] = '''\ @article{hendrycks2021cuad, title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review}, author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball}, journal={arXiv preprint arXiv:2103.06268}, year={2021} } ''' __SCREAMING_SNAKE_CASE :int = ''' This metric wrap the official scoring script for version 1 of the Contract Understanding Atticus Dataset (CUAD). Contract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510 commercial legal contracts that have been manually labeled to identify 41 categories of important clauses that lawyers look for when reviewing contracts in connection with corporate transactions. ''' __SCREAMING_SNAKE_CASE :Tuple = ''' Computes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall). Args: predictions: List of question-answers dictionaries with the following key-values: - \'id\': id of the question-answer pair as given in the references (see below) - \'prediction_text\': list of possible texts for the answer, as a list of strings depending on a threshold on the confidence probability of each prediction. references: List of question-answers dictionaries with the following key-values: - \'id\': id of the question-answer pair (see above), - \'answers\': a Dict in the CUAD dataset format { \'text\': list of possible texts for the answer, as a list of strings \'answer_start\': list of start positions for the answer, as a list of ints } Note that answer_start values are not taken into account to compute the metric. Returns: \'exact_match\': Exact match (the normalized answer exactly match the gold answer) \'f1\': The F-score of predicted tokens versus the gold answer \'aupr\': Area Under the Precision-Recall curve \'prec_at_80_recall\': Precision at 80% recall \'prec_at_90_recall\': Precision at 90% recall Examples: >>> predictions = [{\'prediction_text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\'], \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}] >>> references = [{\'answers\': {\'answer_start\': [143, 49], \'text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\']}, \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}] >>> cuad_metric = datasets.load_metric("cuad") >>> results = cuad_metric.compute(predictions=predictions, references=references) >>> print(results) {\'exact_match\': 100.0, \'f1\': 100.0, \'aupr\': 0.0, \'prec_at_80_recall\': 1.0, \'prec_at_90_recall\': 1.0} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __SCREAMING_SNAKE_CASE ( datasets.Metric ): def lowercase ( self : Dict ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": { "id": datasets.Value("string" ), "prediction_text": datasets.features.Sequence(datasets.Value("string" ) ), }, "references": { "id": datasets.Value("string" ), "answers": datasets.features.Sequence( { "text": datasets.Value("string" ), "answer_start": datasets.Value("int32" ), } ), }, } ) , codebase_urls=["https://www.atticusprojectai.org/cuad"] , reference_urls=["https://www.atticusprojectai.org/cuad"] , ) def lowercase ( self : Any , snake_case_ : Dict , snake_case_ : Optional[Any] ): _UpperCAmelCase = {prediction['''id''']: prediction['''prediction_text'''] for prediction in predictions} _UpperCAmelCase = [ { '''paragraphs''': [ { '''qas''': [ { '''answers''': [{'''text''': answer_text} for answer_text in ref['''answers''']['''text''']], '''id''': ref['''id'''], } for ref in references ] } ] } ] _UpperCAmelCase = evaluate(dataset=_a , predictions=_a ) return score
365
'''simple docstring''' def UpperCAmelCase_ ( __lowercase : list ) -> list: '''simple docstring''' for i in range(len(__lowercase ) - 1 , 0 , -1 ): _UpperCAmelCase = False for j in range(__lowercase , 0 , -1 ): if unsorted[j] < unsorted[j - 1]: _UpperCAmelCase , _UpperCAmelCase = unsorted[j - 1], unsorted[j] _UpperCAmelCase = True for j in range(__lowercase ): if unsorted[j] > unsorted[j + 1]: _UpperCAmelCase , _UpperCAmelCase = unsorted[j + 1], unsorted[j] _UpperCAmelCase = True if not swapped: break return unsorted if __name__ == "__main__": import doctest doctest.testmod() __SCREAMING_SNAKE_CASE :List[str] = input('''Enter numbers separated by a comma:\n''').strip() __SCREAMING_SNAKE_CASE :Any = [int(item) for item in user_input.split(''',''')] print(F"{cocktail_shaker_sort(unsorted) = }")
156
0
from ..utils import DummyObject, requires_backends class A__ ( metaclass=__magic_name__ ): lowercase = ['torch', 'torchsde'] def __init__( self : Optional[int] , *a : Union[str, Any] , **a : Optional[int] ): '''simple docstring''' requires_backends(self , ['torch', 'torchsde'] ) @classmethod def _lowerCamelCase ( cls : Tuple , *a : Dict , **a : Tuple ): '''simple docstring''' requires_backends(cls , ['torch', 'torchsde'] ) @classmethod def _lowerCamelCase ( cls : Optional[Any] , *a : str , **a : List[Any] ): '''simple docstring''' requires_backends(cls , ['torch', 'torchsde'] )
212
import unittest from transformers import SPIECE_UNDERLINE, ReformerTokenizer, ReformerTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCamelCase__ = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece @require_tokenizers class A__ ( __magic_name__ , unittest.TestCase ): lowercase = ReformerTokenizer lowercase = ReformerTokenizerFast lowercase = True lowercase = False lowercase = True def _lowerCamelCase ( self : Dict ): '''simple docstring''' super().setUp() lowerCAmelCase__ : int = ReformerTokenizer(a , keep_accents=a ) tokenizer.save_pretrained(self.tmpdirname ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : int = '<s>' lowerCAmelCase__ : Optional[int] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(a ) , a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(a ) , a ) def _lowerCamelCase ( self : str ): '''simple docstring''' lowerCAmelCase__ : Union[str, Any] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<unk>' ) self.assertEqual(vocab_keys[1] , '<s>' ) self.assertEqual(vocab_keys[-1] , 'j' ) self.assertEqual(len(a ) , 1_000 ) def _lowerCamelCase ( self : Union[str, Any] ): '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 1_000 ) def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' if not self.test_rust_tokenizer: return lowerCAmelCase__ : Optional[int] = self.get_tokenizer() lowerCAmelCase__ : Any = self.get_rust_tokenizer() lowerCAmelCase__ : Optional[int] = 'I was born in 92000, and this is falsé.' lowerCAmelCase__ : List[Any] = tokenizer.tokenize(a ) lowerCAmelCase__ : Optional[int] = rust_tokenizer.tokenize(a ) self.assertListEqual(a , a ) lowerCAmelCase__ : List[str] = tokenizer.encode(a , add_special_tokens=a ) lowerCAmelCase__ : Any = rust_tokenizer.encode(a , add_special_tokens=a ) self.assertListEqual(a , a ) lowerCAmelCase__ : Any = self.get_rust_tokenizer() lowerCAmelCase__ : List[str] = tokenizer.encode(a ) lowerCAmelCase__ : Optional[int] = rust_tokenizer.encode(a ) self.assertListEqual(a , a ) def _lowerCamelCase ( self : Optional[Any] , a : Union[str, Any]=15 ): '''simple docstring''' for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): lowerCAmelCase__ : List[str] = self.rust_tokenizer_class.from_pretrained(a , **a ) # Simple input lowerCAmelCase__ : Any = 'This is a simple input' lowerCAmelCase__ : str = ['This is a simple input 1', 'This is a simple input 2'] lowerCAmelCase__ : Optional[int] = ('This is a simple input', 'This is a pair') lowerCAmelCase__ : Optional[int] = [ ('This is a simple input 1', 'This is a simple input 2'), ('This is a simple pair 1', 'This is a simple pair 2'), ] # Simple input tests self.assertRaises(a , tokenizer_r.encode , a , max_length=a , padding='max_length' ) # Simple input self.assertRaises(a , tokenizer_r.encode_plus , a , max_length=a , padding='max_length' ) # Simple input self.assertRaises( a , tokenizer_r.batch_encode_plus , a , max_length=a , padding='max_length' , ) # Pair input self.assertRaises(a , tokenizer_r.encode , a , max_length=a , padding='max_length' ) # Pair input self.assertRaises(a , tokenizer_r.encode_plus , a , max_length=a , padding='max_length' ) # Pair input self.assertRaises( a , tokenizer_r.batch_encode_plus , a , max_length=a , padding='max_length' , ) def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' pass def _lowerCamelCase ( self : Optional[int] ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = ReformerTokenizer(a , keep_accents=a ) lowerCAmelCase__ : Dict = tokenizer.tokenize('This is a test' ) self.assertListEqual(a , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(a ) , [285, 46, 10, 170, 382] , ) lowerCAmelCase__ : List[str] = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( a , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) lowerCAmelCase__ : int = tokenizer.convert_tokens_to_ids(a ) self.assertListEqual( a , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) lowerCAmelCase__ : str = tokenizer.convert_ids_to_tokens(a ) self.assertListEqual( a , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] , ) @cached_property def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' return ReformerTokenizer.from_pretrained('google/reformer-crime-and-punishment' ) @slow def _lowerCamelCase ( self : Any ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = 'Hello World!' lowerCAmelCase__ : int = [126, 32, 262, 152, 38, 72, 287] self.assertListEqual(a , self.big_tokenizer.encode(a ) ) @slow def _lowerCamelCase ( self : int ): '''simple docstring''' lowerCAmelCase__ : int = ( 'This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will' ' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth' ) lowerCAmelCase__ : int = [ 108, 265, 24, 111, 4, 258, 156, 35, 28, 275, 3, 259, 297, 260, 84, 4, 35, 110, 44, 8, 259, 91, 268, 21, 11, 209, 274, 109, 266, 277, 117, 86, 93, 315, 258, 278, 258, 277, 258, 0, 258, 288, 258, 319, 258, 0, 258, 0, 258, 0, 258, 0, 258, 287, 258, 315, 258, 289, 258, 278, 99, 269, 266, 262, 8, 259, 241, 4, 217, 230, 268, 266, 55, 168, 106, 75, 193, 266, 223, 27, 49, 26, 282, 25, 264, 299, 19, 26, 0, 258, 277, 117, 86, 93, 176, 183, 270, 11, 262, 42, 61, 265, ] self.assertListEqual(a , self.big_tokenizer.encode(a ) ) @require_torch @slow def _lowerCamelCase ( self : Optional[Any] ): '''simple docstring''' import torch from transformers import ReformerConfig, ReformerModel # Build sequence lowerCAmelCase__ : Dict = list(self.big_tokenizer.get_vocab().keys() )[:10] lowerCAmelCase__ : Optional[Any] = ' '.join(a ) lowerCAmelCase__ : Any = self.big_tokenizer.encode_plus(a , return_tensors='pt' ) lowerCAmelCase__ : Optional[int] = self.big_tokenizer.batch_encode_plus([sequence, sequence] , return_tensors='pt' ) lowerCAmelCase__ : List[Any] = ReformerConfig() # The input gets padded during training so adjust the axial position encodings from the pretrained model value of (512, 1024) lowerCAmelCase__ : int = encoded_sequence['input_ids'].shape lowerCAmelCase__ : Union[str, Any] = ReformerModel(a ) # Reformer has config.vocab_size == tokenizer.vocab_size == len(tokenizer) - 1 = 320; len(tokenizer) is 321 (including a pad token with id 320) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**a ) model(**a ) @slow def _lowerCamelCase ( self : List[Any] ): '''simple docstring''' lowerCAmelCase__ : Optional[Any] = {'input_ids': [[108, 265, 24, 111, 4, 258, 156, 7, 51, 279, 58, 7, 76, 25, 69, 278], [140, 243, 264, 134, 17, 267, 77, 263, 22, 262, 297, 258, 304, 177, 279, 266, 14, 89, 13, 35, 261, 299, 272, 137, 275, 278]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # This tokenizer does not know some characters like ")". # That is the reason why we use very simple texts here. # Also see https://github.com/huggingface/transformers/pull/11737#issuecomment-850769064 lowerCAmelCase__ : List[str] = [ 'This is a very simple sentence.', 'The quick brown fox jumps over the lazy dog.', ] self.tokenizer_integration_test_util( expected_encoding=a , model_name='google/reformer-crime-and-punishment' , revision='0e6c3decb8211d49bf881013425dc8b0448b3f5a' , padding=a , sequences=a , )
212
1
import argparse import torch from transformers import ( UniSpeechSatConfig, UniSpeechSatForAudioFrameClassification, UniSpeechSatForSequenceClassification, UniSpeechSatForXVector, WavaVecaFeatureExtractor, logging, ) logging.set_verbosity_info() lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) def UpperCAmelCase_ ( __UpperCAmelCase : List[Any] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[int] ) -> List[str]: SCREAMING_SNAKE_CASE_ = UniSpeechSatForSequenceClassification.from_pretrained(__UpperCAmelCase , config=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = downstream_dict['projector.weight'] SCREAMING_SNAKE_CASE_ = downstream_dict['projector.bias'] SCREAMING_SNAKE_CASE_ = downstream_dict['model.post_net.linear.weight'] SCREAMING_SNAKE_CASE_ = downstream_dict['model.post_net.linear.bias'] return model def UpperCAmelCase_ ( __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Tuple ) -> int: SCREAMING_SNAKE_CASE_ = UniSpeechSatForAudioFrameClassification.from_pretrained(__UpperCAmelCase , config=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = downstream_dict['model.linear.weight'] SCREAMING_SNAKE_CASE_ = downstream_dict['model.linear.bias'] return model def UpperCAmelCase_ ( __UpperCAmelCase : Any , __UpperCAmelCase : Any , __UpperCAmelCase : int ) -> str: SCREAMING_SNAKE_CASE_ = UniSpeechSatForXVector.from_pretrained(__UpperCAmelCase , config=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = downstream_dict['connector.weight'] SCREAMING_SNAKE_CASE_ = downstream_dict['connector.bias'] for i, kernel_size in enumerate(hf_config.tdnn_kernel ): SCREAMING_SNAKE_CASE_ = downstream_dict[ f"model.framelevel_feature_extractor.module.{i}.kernel.weight" ] SCREAMING_SNAKE_CASE_ = downstream_dict[f"model.framelevel_feature_extractor.module.{i}.kernel.bias"] SCREAMING_SNAKE_CASE_ = downstream_dict['model.utterancelevel_feature_extractor.linear1.weight'] SCREAMING_SNAKE_CASE_ = downstream_dict['model.utterancelevel_feature_extractor.linear1.bias'] SCREAMING_SNAKE_CASE_ = downstream_dict['model.utterancelevel_feature_extractor.linear2.weight'] SCREAMING_SNAKE_CASE_ = downstream_dict['model.utterancelevel_feature_extractor.linear2.bias'] SCREAMING_SNAKE_CASE_ = downstream_dict['objective.W'] return model @torch.no_grad() def UpperCAmelCase_ ( __UpperCAmelCase : int , __UpperCAmelCase : Tuple , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Optional[int] ) -> List[str]: SCREAMING_SNAKE_CASE_ = torch.load(__UpperCAmelCase , map_location='cpu' ) SCREAMING_SNAKE_CASE_ = checkpoint['Downstream'] SCREAMING_SNAKE_CASE_ = UniSpeechSatConfig.from_pretrained(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = WavaVecaFeatureExtractor.from_pretrained( __UpperCAmelCase , return_attention_mask=__UpperCAmelCase , do_normalize=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = hf_config.architectures[0] if arch.endswith('ForSequenceClassification' ): SCREAMING_SNAKE_CASE_ = convert_classification(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) elif arch.endswith('ForAudioFrameClassification' ): SCREAMING_SNAKE_CASE_ = convert_diarization(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) elif arch.endswith('ForXVector' ): SCREAMING_SNAKE_CASE_ = convert_xvector(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) else: raise NotImplementedError(f"S3PRL weights conversion is not supported for {arch}" ) if hf_config.use_weighted_layer_sum: SCREAMING_SNAKE_CASE_ = checkpoint['Featurizer']['weights'] hf_feature_extractor.save_pretrained(__UpperCAmelCase ) hf_model.save_pretrained(__UpperCAmelCase ) if __name__ == "__main__": lowerCamelCase__ : Dict = argparse.ArgumentParser() parser.add_argument( '--base_model_name', default=None, type=str, help='Name of the huggingface pretrained base model.' ) parser.add_argument('--config_path', default=None, type=str, help='Path to the huggingface classifier config.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to the s3prl checkpoint.') parser.add_argument('--model_dump_path', default=None, type=str, help='Path to the final converted model.') lowerCamelCase__ : Dict = parser.parse_args() convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)
364
from __future__ import annotations from sys import maxsize from typing import Generic, TypeVar lowerCamelCase__ : Dict = TypeVar('T') def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: return (position - 1) // 2 def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: return (2 * position) + 1 def UpperCAmelCase_ ( __UpperCAmelCase : int ) -> int: return (2 * position) + 2 class lowerCamelCase_ ( Generic[T] ): '''simple docstring''' def __init__( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = 0 def __len__( self : Optional[Any] ): return self.elements def __repr__( self : Optional[int] ): return str(self.heap ) def lowerCAmelCase_ ( self : Union[str, Any] ): # Check if the priority queue is empty return self.elements == 0 def lowerCAmelCase_ ( self : Tuple , _lowerCAmelCase : T , _lowerCAmelCase : int ): # Add an element with given priority to the queue self.heap.append((elem, weight) ) SCREAMING_SNAKE_CASE_ = self.elements self.elements += 1 self._bubble_up(_lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] ): # Remove and return the element with lowest weight (highest priority) if self.elements > 1: self._swap_nodes(0 , self.elements - 1 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap.pop() del self.position_map[elem] self.elements -= 1 if self.elements > 0: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[0] self._bubble_down(_lowerCAmelCase ) return elem def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : T , _lowerCAmelCase : int ): # Update the weight of the given key SCREAMING_SNAKE_CASE_ = self.position_map[elem] SCREAMING_SNAKE_CASE_ = (elem, weight) if position > 0: SCREAMING_SNAKE_CASE_ = get_parent_position(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[parent_position] if parent_weight > weight: self._bubble_up(_lowerCAmelCase ) else: self._bubble_down(_lowerCAmelCase ) else: self._bubble_down(_lowerCAmelCase ) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : T ): # Place a node at the proper position (upward movement) [to be used internally # only] SCREAMING_SNAKE_CASE_ = self.position_map[elem] if curr_pos == 0: return None SCREAMING_SNAKE_CASE_ = get_parent_position(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[curr_pos] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[parent_position] if parent_weight > weight: self._swap_nodes(_lowerCAmelCase , _lowerCAmelCase ) return self._bubble_up(_lowerCAmelCase ) return None def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : T ): # Place a node at the proper position (downward movement) [to be used # internally only] SCREAMING_SNAKE_CASE_ = self.position_map[elem] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[curr_pos] SCREAMING_SNAKE_CASE_ = get_child_left_position(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = get_child_right_position(_lowerCAmelCase ) if child_left_position < self.elements and child_right_position < self.elements: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[child_left_position] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[child_right_position] if child_right_weight < child_left_weight and child_right_weight < weight: self._swap_nodes(_lowerCAmelCase , _lowerCAmelCase ) return self._bubble_down(_lowerCAmelCase ) if child_left_position < self.elements: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[child_left_position] if child_left_weight < weight: self._swap_nodes(_lowerCAmelCase , _lowerCAmelCase ) return self._bubble_down(_lowerCAmelCase ) else: return None if child_right_position < self.elements: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.heap[child_right_position] if child_right_weight < weight: self._swap_nodes(_lowerCAmelCase , _lowerCAmelCase ) return self._bubble_down(_lowerCAmelCase ) return None def lowerCAmelCase_ ( self : Optional[Any] , _lowerCAmelCase : int , _lowerCAmelCase : int ): # Swap the nodes at the given positions SCREAMING_SNAKE_CASE_ = self.heap[nodea_pos][0] SCREAMING_SNAKE_CASE_ = self.heap[nodea_pos][0] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = ( self.heap[nodea_pos], self.heap[nodea_pos], ) SCREAMING_SNAKE_CASE_ = nodea_pos SCREAMING_SNAKE_CASE_ = nodea_pos class lowerCamelCase_ ( Generic[T] ): '''simple docstring''' def __init__( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = 0 def __repr__( self : Optional[int] ): return str(self.connections ) def __len__( self : Tuple ): return self.nodes def lowerCAmelCase_ ( self : Dict , _lowerCAmelCase : T ): # Add a node in the graph if it is not in the graph if node not in self.connections: SCREAMING_SNAKE_CASE_ = {} self.nodes += 1 def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : T , _lowerCAmelCase : T , _lowerCAmelCase : int ): # Add an edge between 2 nodes in the graph self.add_node(_lowerCAmelCase ) self.add_node(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = weight SCREAMING_SNAKE_CASE_ = weight def UpperCAmelCase_ ( __UpperCAmelCase : GraphUndirectedWeighted[T] , ) -> tuple[dict[T, int], dict[T, T | None]]: SCREAMING_SNAKE_CASE_ = {node: maxsize for node in graph.connections} SCREAMING_SNAKE_CASE_ = {node: None for node in graph.connections} SCREAMING_SNAKE_CASE_ = MinPriorityQueue() for node, weight in dist.items(): priority_queue.push(__UpperCAmelCase , __UpperCAmelCase ) if priority_queue.is_empty(): return dist, parent # initialization SCREAMING_SNAKE_CASE_ = priority_queue.extract_min() SCREAMING_SNAKE_CASE_ = 0 for neighbour in graph.connections[node]: if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: SCREAMING_SNAKE_CASE_ = dist[node] + graph.connections[node][neighbour] priority_queue.update_key(__UpperCAmelCase , dist[neighbour] ) SCREAMING_SNAKE_CASE_ = node # running prim's algorithm while not priority_queue.is_empty(): SCREAMING_SNAKE_CASE_ = priority_queue.extract_min() for neighbour in graph.connections[node]: if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: SCREAMING_SNAKE_CASE_ = dist[node] + graph.connections[node][neighbour] priority_queue.update_key(__UpperCAmelCase , dist[neighbour] ) SCREAMING_SNAKE_CASE_ = node return dist, parent
210
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) lowercase_ = {"configuration_fnet": ["FNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "FNetConfig"]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["FNetTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["FNetTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "FNET_PRETRAINED_MODEL_ARCHIVE_LIST", "FNetForMaskedLM", "FNetForMultipleChoice", "FNetForNextSentencePrediction", "FNetForPreTraining", "FNetForQuestionAnswering", "FNetForSequenceClassification", "FNetForTokenClassification", "FNetLayer", "FNetModel", "FNetPreTrainedModel", ] if TYPE_CHECKING: from .configuration_fnet import FNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet import FNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet_fast import FNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_fnet import ( FNET_PRETRAINED_MODEL_ARCHIVE_LIST, FNetForMaskedLM, FNetForMultipleChoice, FNetForNextSentencePrediction, FNetForPreTraining, FNetForQuestionAnswering, FNetForSequenceClassification, FNetForTokenClassification, FNetLayer, FNetModel, FNetPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
7
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowercase_ = logging.get_logger(__name__) lowercase_ = { "microsoft/resnet-50": "https://huggingface.co/microsoft/resnet-50/blob/main/config.json", } class A ( _UpperCAmelCase , _UpperCAmelCase ): """simple docstring""" lowerCamelCase = 'resnet' lowerCamelCase = ['basic', 'bottleneck'] def __init__( self : Optional[Any],lowercase_ : int=3,lowercase_ : List[str]=6_4,lowercase_ : int=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8],lowercase_ : Tuple=[3, 4, 6, 3],lowercase_ : Union[str, Any]="bottleneck",lowercase_ : List[str]="relu",lowercase_ : Tuple=False,lowercase_ : List[str]=None,lowercase_ : List[Any]=None,**lowercase_ : str,)-> Optional[Any]: '''simple docstring''' super().__init__(**lowercase_ ) if layer_type not in self.layer_types: raise ValueError(F'layer_type={layer_type} is not one of {",".join(self.layer_types )}' ) A__ = num_channels A__ = embedding_size A__ = hidden_sizes A__ = depths A__ = layer_type A__ = hidden_act A__ = downsample_in_first_stage A__ = ['stem'] + [F'stage{idx}' for idx in range(1,len(lowercase_ ) + 1 )] A__ , A__ = get_aligned_output_features_output_indices( out_features=lowercase_,out_indices=lowercase_,stage_names=self.stage_names ) class A ( _UpperCAmelCase ): """simple docstring""" lowerCamelCase = version.parse('1.11' ) @property def snake_case__ ( self : List[Any] )-> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def snake_case__ ( self : Any )-> float: '''simple docstring''' return 1E-3
7
1
import inspect import math import tempfile import unittest import numpy as np from transformers import ViTMAEConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMAEForPreTraining, ViTMAEModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __a : def __init__( self : Optional[int] , UpperCAmelCase : str , UpperCAmelCase : int=13 , UpperCAmelCase : Tuple=30 , UpperCAmelCase : Optional[int]=2 , UpperCAmelCase : Union[str, Any]=3 , UpperCAmelCase : Optional[int]=True , UpperCAmelCase : Tuple=True , UpperCAmelCase : Optional[int]=32 , UpperCAmelCase : int=5 , UpperCAmelCase : Tuple=4 , UpperCAmelCase : Optional[int]=37 , UpperCAmelCase : List[Any]="gelu" , UpperCAmelCase : Optional[int]=0.1 , UpperCAmelCase : int=0.1 , UpperCAmelCase : Union[str, Any]=10 , UpperCAmelCase : str=0.02 , UpperCAmelCase : List[str]=3 , UpperCAmelCase : int=0.6 , UpperCAmelCase : Union[str, Any]=None , ): lowerCAmelCase_ : Optional[int] = parent lowerCAmelCase_ : str = batch_size lowerCAmelCase_ : str = image_size lowerCAmelCase_ : List[Any] = patch_size lowerCAmelCase_ : int = num_channels lowerCAmelCase_ : int = is_training lowerCAmelCase_ : Any = use_labels lowerCAmelCase_ : str = hidden_size lowerCAmelCase_ : List[Any] = num_hidden_layers lowerCAmelCase_ : List[Any] = num_attention_heads lowerCAmelCase_ : List[Any] = intermediate_size lowerCAmelCase_ : Dict = hidden_act lowerCAmelCase_ : List[Any] = hidden_dropout_prob lowerCAmelCase_ : Union[str, Any] = attention_probs_dropout_prob lowerCAmelCase_ : Optional[Any] = type_sequence_label_size lowerCAmelCase_ : List[str] = initializer_range lowerCAmelCase_ : List[Any] = mask_ratio lowerCAmelCase_ : Optional[int] = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) lowerCAmelCase_ : Tuple = (image_size // patch_size) ** 2 lowerCAmelCase_ : Tuple = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def A ( self : Optional[int] ): lowerCAmelCase_ : Tuple = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase_ : int = None if self.use_labels: lowerCAmelCase_ : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCAmelCase_ : Optional[Any] = self.get_config() return config, pixel_values, labels def A ( self : Tuple ): return ViTMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=lowerCAmelCase__ , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def A ( self : Optional[Any] , UpperCAmelCase : int , UpperCAmelCase : List[Any] , UpperCAmelCase : List[str] ): lowerCAmelCase_ : List[str] = ViTMAEModel(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() lowerCAmelCase_ : Any = model(lowerCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def A ( self : Tuple , UpperCAmelCase : List[str] , UpperCAmelCase : List[str] , UpperCAmelCase : List[str] ): lowerCAmelCase_ : Optional[int] = ViTMAEForPreTraining(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() lowerCAmelCase_ : Union[str, Any] = model(lowerCAmelCase__ ) lowerCAmelCase_ : Optional[Any] = (self.image_size // self.patch_size) ** 2 lowerCAmelCase_ : List[Any] = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images lowerCAmelCase_ : Dict = 1 lowerCAmelCase_ : Dict = ViTMAEForPreTraining(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() lowerCAmelCase_ : Optional[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCAmelCase_ : Dict = model(lowerCAmelCase__ ) lowerCAmelCase_ : Tuple = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def A ( self : Optional[Any] ): lowerCAmelCase_ : List[str] = self.prepare_config_and_inputs() lowerCAmelCase_ : str = config_and_inputs lowerCAmelCase_ : List[str] = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __a ( a__ ,a__ ,unittest.TestCase ): __snake_case : str = (ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else () __snake_case : Optional[int] = {"""feature-extraction""": ViTMAEModel} if is_torch_available() else {} __snake_case : Dict = False __snake_case : Optional[int] = False __snake_case : str = False __snake_case : Tuple = False def A ( self : List[Any] ): lowerCAmelCase_ : Union[str, Any] = ViTMAEModelTester(self ) lowerCAmelCase_ : Dict = ConfigTester(self , config_class=lowerCAmelCase__ , has_text_modality=lowerCAmelCase__ , hidden_size=37 ) def A ( self : Dict ): self.config_tester.run_common_tests() @unittest.skip(reason="""ViTMAE does not use inputs_embeds""" ) def A ( self : Any ): pass def A ( self : List[str] ): lowerCAmelCase_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase_ : Optional[Any] = model_class(lowerCAmelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowerCAmelCase_ : List[Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCAmelCase__ , nn.Linear ) ) def A ( self : List[Any] ): lowerCAmelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase_ : str = model_class(lowerCAmelCase__ ) lowerCAmelCase_ : Optional[int] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase_ : Optional[int] = [*signature.parameters.keys()] lowerCAmelCase_ : List[Any] = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCAmelCase__ ) def A ( self : str ): lowerCAmelCase_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__ ) def A ( self : List[Any] ): lowerCAmelCase_ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase__ ) def A ( self : List[Any] , UpperCAmelCase : Dict , UpperCAmelCase : Optional[Any] , UpperCAmelCase : List[Any] ): # make masks reproducible np.random.seed(2 ) lowerCAmelCase_ : List[Any] = int((pt_model.config.image_size // pt_model.config.patch_size) ** 2 ) lowerCAmelCase_ : List[Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) lowerCAmelCase_ : List[Any] = torch.from_numpy(lowerCAmelCase__ ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument lowerCAmelCase_ : int = pt_noise super().check_pt_tf_models(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def A ( self : List[str] ): lowerCAmelCase_ : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase_ : List[Any] = model_class(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): lowerCAmelCase_ : Any = model(**self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ ) ) lowerCAmelCase_ : Tuple = outputs[0].cpu().numpy() lowerCAmelCase_ : Optional[Any] = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(lowerCAmelCase__ ) lowerCAmelCase_ : Optional[int] = model_class.from_pretrained(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): lowerCAmelCase_ : Union[str, Any] = model(**self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Make sure we don't have nans lowerCAmelCase_ : Tuple = after_outputs[0].cpu().numpy() lowerCAmelCase_ : str = 0 lowerCAmelCase_ : Union[str, Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(lowerCAmelCase__ , 1e-5 ) @unittest.skip( reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n to get deterministic results.""" ) def A ( self : Dict ): pass @unittest.skip( reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n to get deterministic results.""" ) def A ( self : List[str] ): pass @unittest.skip( reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n to get deterministic results.""" ) def A ( self : str ): pass @unittest.skip(reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load""" ) def A ( self : Optional[Any] ): pass @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def A ( self : Any ): pass @slow def A ( self : str ): for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase_ : str = ViTMAEModel.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) def __UpperCamelCase ( ) -> Tuple: '''simple docstring''' lowerCAmelCase_ : Any = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class __a ( unittest.TestCase ): @cached_property def A ( self : int ): return ViTImageProcessor.from_pretrained("""facebook/vit-mae-base""" ) if is_vision_available() else None @slow def A ( self : Optional[int] ): # make random mask reproducible across the PT and TF model np.random.seed(2 ) lowerCAmelCase_ : Dict = ViTMAEForPreTraining.from_pretrained("""facebook/vit-mae-base""" ).to(lowerCAmelCase__ ) lowerCAmelCase_ : List[str] = self.default_image_processor lowerCAmelCase_ : Union[str, Any] = prepare_img() lowerCAmelCase_ : Union[str, Any] = image_processor(images=lowerCAmelCase__ , return_tensors="""pt""" ).to(lowerCAmelCase__ ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) lowerCAmelCase_ : List[str] = ViTMAEConfig() lowerCAmelCase_ : int = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) lowerCAmelCase_ : List[str] = np.random.uniform(size=(1, num_patches) ) # forward pass with torch.no_grad(): lowerCAmelCase_ : Dict = model(**lowerCAmelCase__ , noise=torch.from_numpy(lowerCAmelCase__ ).to(device=lowerCAmelCase__ ) ) # verify the logits lowerCAmelCase_ : Optional[Any] = torch.Size((1, 1_96, 7_68) ) self.assertEqual(outputs.logits.shape , lowerCAmelCase__ ) lowerCAmelCase_ : Optional[Any] = torch.tensor( [[-0.0548, -1.7023, -0.9325], [0.3721, -0.5670, -0.2233], [0.8235, -1.3878, -0.3524]] ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , expected_slice.to(lowerCAmelCase__ ) , atol=1e-4 ) )
362
from typing import List, Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __a ( __UpperCamelCase ): __snake_case : Any = ["""image_processor""", """tokenizer"""] __snake_case : Tuple = """BlipImageProcessor""" __snake_case : int = ("""BertTokenizer""", """BertTokenizerFast""") def __init__( self : int , UpperCAmelCase : List[str] , UpperCAmelCase : List[Any] ): lowerCAmelCase_ : str = False super().__init__(UpperCAmelCase , UpperCAmelCase ) lowerCAmelCase_ : Tuple = self.image_processor def __call__( self : Optional[int] , UpperCAmelCase : ImageInput = None , UpperCAmelCase : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , UpperCAmelCase : bool = True , UpperCAmelCase : Union[bool, str, PaddingStrategy] = False , UpperCAmelCase : Union[bool, str, TruncationStrategy] = None , UpperCAmelCase : Optional[int] = None , UpperCAmelCase : int = 0 , UpperCAmelCase : Optional[int] = None , UpperCAmelCase : Optional[bool] = None , UpperCAmelCase : bool = False , UpperCAmelCase : bool = False , UpperCAmelCase : bool = False , UpperCAmelCase : bool = False , UpperCAmelCase : bool = False , UpperCAmelCase : bool = True , UpperCAmelCase : Optional[Union[str, TensorType]] = None , **UpperCAmelCase : Tuple , ): if images is None and text is None: raise ValueError("""You have to specify either images or text.""" ) # Get only text if images is None: lowerCAmelCase_ : str = self.tokenizer lowerCAmelCase_ : List[Any] = self.tokenizer( text=UpperCAmelCase , add_special_tokens=UpperCAmelCase , padding=UpperCAmelCase , truncation=UpperCAmelCase , max_length=UpperCAmelCase , stride=UpperCAmelCase , pad_to_multiple_of=UpperCAmelCase , return_attention_mask=UpperCAmelCase , return_overflowing_tokens=UpperCAmelCase , return_special_tokens_mask=UpperCAmelCase , return_offsets_mapping=UpperCAmelCase , return_token_type_ids=UpperCAmelCase , return_length=UpperCAmelCase , verbose=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) return text_encoding # add pixel_values lowerCAmelCase_ : Union[str, Any] = self.image_processor(UpperCAmelCase , return_tensors=UpperCAmelCase ) if text is not None: lowerCAmelCase_ : Optional[Any] = self.tokenizer( text=UpperCAmelCase , add_special_tokens=UpperCAmelCase , padding=UpperCAmelCase , truncation=UpperCAmelCase , max_length=UpperCAmelCase , stride=UpperCAmelCase , pad_to_multiple_of=UpperCAmelCase , return_attention_mask=UpperCAmelCase , return_overflowing_tokens=UpperCAmelCase , return_special_tokens_mask=UpperCAmelCase , return_offsets_mapping=UpperCAmelCase , return_token_type_ids=UpperCAmelCase , return_length=UpperCAmelCase , verbose=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) else: lowerCAmelCase_ : int = None if text_encoding is not None: encoding_image_processor.update(UpperCAmelCase ) return encoding_image_processor def A ( self : Optional[Any] , *UpperCAmelCase : Optional[Any] , **UpperCAmelCase : int ): return self.tokenizer.batch_decode(*UpperCAmelCase , **UpperCAmelCase ) def A ( self : List[Any] , *UpperCAmelCase : Optional[int] , **UpperCAmelCase : Optional[Any] ): return self.tokenizer.decode(*UpperCAmelCase , **UpperCAmelCase ) @property def A ( self : int ): lowerCAmelCase_ : int = self.tokenizer.model_input_names lowerCAmelCase_ : Optional[int] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
28
0
"""simple docstring""" from __future__ import annotations def _snake_case ( UpperCamelCase : float , UpperCamelCase : float , UpperCamelCase : float ): if (voltage, current, resistance).count(0 ) != 1: raise ValueError("""One and only one argument must be 0""" ) if resistance < 0: raise ValueError("""Resistance cannot be negative""" ) if voltage == 0: return {"voltage": float(current * resistance )} elif current == 0: return {"current": voltage / resistance} elif resistance == 0: return {"resistance": voltage / current} else: raise ValueError("""Exactly one argument must be 0""" ) if __name__ == "__main__": import doctest doctest.testmod()
109
"""simple docstring""" from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available from ...utils import OptionalDependencyNotAvailable A: List[str] = {"configuration_dpt": ["DPT_PRETRAINED_CONFIG_ARCHIVE_MAP", "DPTConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A: Tuple = ["DPTFeatureExtractor"] A: int = ["DPTImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A: Optional[Any] = [ "DPT_PRETRAINED_MODEL_ARCHIVE_LIST", "DPTForDepthEstimation", "DPTForSemanticSegmentation", "DPTModel", "DPTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_dpt import DPT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPTConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_dpt import DPTFeatureExtractor from .image_processing_dpt import DPTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dpt import ( DPT_PRETRAINED_MODEL_ARCHIVE_LIST, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel, DPTPreTrainedModel, ) else: import sys A: str = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
109
1
from datasets.utils.patching import _PatchedModuleObj, patch_submodule from . import _test_patching def A ( ) -> Any: '''simple docstring''' import os as original_os from os import path as original_path from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join UpperCAmelCase_ = '''__test_patch_submodule_mock__''' with patch_submodule(_test_patching , '''os.path.join''' , __UpperCAmelCase ): # Every way to access os.path.join must be patched, and the rest must stay untouched # check os.path.join assert isinstance(_test_patching.os , _PatchedModuleObj ) assert isinstance(_test_patching.os.path , _PatchedModuleObj ) assert _test_patching.os.path.join is mock # check path.join assert isinstance(_test_patching.path , _PatchedModuleObj ) assert _test_patching.path.join is mock # check join assert _test_patching.join is mock # check that the other attributes are untouched assert _test_patching.os.rename is original_rename assert _test_patching.path.dirname is original_dirname assert _test_patching.os.path.dirname is original_dirname # Even renamed modules or objects must be patched # check renamed_os.path.join assert isinstance(_test_patching.renamed_os , _PatchedModuleObj ) assert isinstance(_test_patching.renamed_os.path , _PatchedModuleObj ) assert _test_patching.renamed_os.path.join is mock # check renamed_path.join assert isinstance(_test_patching.renamed_path , _PatchedModuleObj ) assert _test_patching.renamed_path.join is mock # check renamed_join assert _test_patching.renamed_join is mock # check that the other attributes are untouched assert _test_patching.renamed_os.rename is original_rename assert _test_patching.renamed_path.dirname is original_dirname assert _test_patching.renamed_os.path.dirname is original_dirname # check that everthing is back to normal when the patch is over assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join def A ( ) -> List[str]: '''simple docstring''' assert _test_patching.open is open UpperCAmelCase_ = '''__test_patch_submodule_builtin_mock__''' # _test_patching has "open" in its globals assert _test_patching.open is open with patch_submodule(_test_patching , '''open''' , __UpperCAmelCase ): assert _test_patching.open is mock # check that everthing is back to normal when the patch is over assert _test_patching.open is open def A ( ) -> str: '''simple docstring''' UpperCAmelCase_ = '''__test_patch_submodule_missing_mock__''' with patch_submodule(_test_patching , '''pandas.read_csv''' , __UpperCAmelCase ): pass def A ( ) -> int: '''simple docstring''' UpperCAmelCase_ = '''__test_patch_submodule_missing_builtin_mock__''' # _test_patching doesn't have "len" in its globals assert getattr(_test_patching , '''len''' , __UpperCAmelCase ) is None with patch_submodule(_test_patching , '''len''' , __UpperCAmelCase ): assert _test_patching.len is mock assert _test_patching.len is len def A ( ) -> List[str]: '''simple docstring''' UpperCAmelCase_ = '''__test_patch_submodule_start_and_stop_mock__''' UpperCAmelCase_ = patch_submodule(_test_patching , '''open''' , __UpperCAmelCase ) assert _test_patching.open is open patch.start() assert _test_patching.open is mock patch.stop() assert _test_patching.open is open def A ( ) -> Any: '''simple docstring''' from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join UpperCAmelCase_ = '''__test_patch_submodule_successive_join__''' UpperCAmelCase_ = '''__test_patch_submodule_successive_dirname__''' UpperCAmelCase_ = '''__test_patch_submodule_successive_rename__''' assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename with patch_submodule(_test_patching , '''os.path.join''' , __UpperCAmelCase ): with patch_submodule(_test_patching , '''os.rename''' , __UpperCAmelCase ): with patch_submodule(_test_patching , '''os.path.dirname''' , __UpperCAmelCase ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename # try another order with patch_submodule(_test_patching , '''os.rename''' , __UpperCAmelCase ): with patch_submodule(_test_patching , '''os.path.join''' , __UpperCAmelCase ): with patch_submodule(_test_patching , '''os.path.dirname''' , __UpperCAmelCase ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename def A ( ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase_ = '''__test_patch_submodule_doesnt_exist_mock__''' with patch_submodule(_test_patching , '''__module_that_doesn_exist__.__attribute_that_doesn_exist__''' , __UpperCAmelCase ): pass with patch_submodule(_test_patching , '''os.__attribute_that_doesn_exist__''' , __UpperCAmelCase ): pass
355
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor from .base import PipelineTool class a_ ( _snake_case ): UpperCamelCase__ : Dict ="openai/whisper-base" UpperCamelCase__ : int =( "This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the " "transcribed text." ) UpperCamelCase__ : Any ="transcriber" UpperCamelCase__ : Optional[int] =WhisperProcessor UpperCamelCase__ : List[str] =WhisperForConditionalGeneration UpperCamelCase__ : List[Any] =["audio"] UpperCamelCase__ : Union[str, Any] =["text"] def __a ( self :int , _lowercase :Any) -> Tuple: return self.pre_processor(_lowercase , return_tensors='''pt''').input_features def __a ( self :Dict , _lowercase :Tuple) -> Any: return self.model.generate(inputs=_lowercase) def __a ( self :int , _lowercase :Union[str, Any]) -> Optional[Any]: return self.pre_processor.batch_decode(_lowercase , skip_special_tokens=_lowercase)[0]
344
0
import enum import warnings from ..tokenization_utils import TruncationStrategy 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 from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING _snake_case = logging.get_logger(__name__) class lowercase ( enum.Enum ): _a = 0 _a = 1 @add_end_docstrings(UpperCamelCase__ ) class lowercase ( UpperCamelCase__ ): _a = "generated" def __init__( self , *_a , **_a ) -> Optional[int]: super().__init__(*_a , **_a ) self.check_model_type( TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING if self.framework == """tf""" else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING ) def a__ ( self , _a=None , _a=None , _a=None , _a=None , _a=None , _a=None , **_a , ) -> str: _A : Dict = {} if truncation is not None: _A : Union[str, Any] = truncation _A : Optional[Any] = generate_kwargs _A : List[str] = {} if return_tensors is not None and return_type is None: _A : Any = ReturnType.TENSORS if return_tensors else ReturnType.TEXT if return_type is not None: _A : Union[str, Any] = return_type if clean_up_tokenization_spaces is not None: _A : Tuple = clean_up_tokenization_spaces if stop_sequence is not None: _A : str = self.tokenizer.encode(_a , add_special_tokens=_a ) if len(_a ) > 1: warnings.warn( """Stopping on a multiple token sequence is not yet supported on transformers. The first token of""" """ the stop sequence will be used as the stop sequence string in the interim.""" ) _A : Any = stop_sequence_ids[0] return preprocess_params, forward_params, postprocess_params def a__ ( self , _a , _a , _a ) -> str: return True def a__ ( self , *_a , _a ) -> List[Any]: _A : Dict = self.model.config.prefix if self.model.config.prefix is not None else """""" if isinstance(args[0] , _a ): if self.tokenizer.pad_token_id is None: raise ValueError("""Please make sure that the tokenizer has a pad_token_id when using a batch input""" ) _A : Optional[int] = ([prefix + arg for arg in args[0]],) _A : Optional[Any] = True elif isinstance(args[0] , _a ): _A : Any = (prefix + args[0],) _A : str = False else: raise ValueError( F''' `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`''' ) _A : Optional[Any] = self.tokenizer(*_a , padding=_a , truncation=_a , return_tensors=self.framework ) # This is produced by tokenizers but is an invalid generate kwargs if "token_type_ids" in inputs: del inputs["token_type_ids"] return inputs def __call__( self , *_a , **_a ) -> Optional[Any]: _A : int = super().__call__(*_a , **_a ) if ( isinstance(args[0] , _a ) and all(isinstance(_a , _a ) for el in args[0] ) and all(len(_a ) == 1 for res in result ) ): return [res[0] for res in result] return result def a__ ( self , _a , _a=TruncationStrategy.DO_NOT_TRUNCATE , **_a ) -> Tuple: _A : Optional[Any] = self._parse_and_tokenize(_a , truncation=_a , **_a ) return inputs def a__ ( self , _a , **_a ) -> Optional[int]: if self.framework == "pt": _A , _A : Tuple = model_inputs["""input_ids"""].shape elif self.framework == "tf": _A , _A : Dict = tf.shape(model_inputs["""input_ids"""] ).numpy() _A : List[Any] = generate_kwargs.get("""min_length""" , self.model.config.min_length ) _A : Union[str, Any] = generate_kwargs.get("""max_length""" , self.model.config.max_length ) self.check_inputs(_a , generate_kwargs["""min_length"""] , generate_kwargs["""max_length"""] ) _A : str = self.model.generate(**_a , **_a ) _A : Optional[int] = output_ids.shape[0] if self.framework == "pt": _A : Dict = output_ids.reshape(_a , out_b // in_b , *output_ids.shape[1:] ) elif self.framework == "tf": _A : List[str] = tf.reshape(_a , (in_b, out_b // in_b, *output_ids.shape[1:]) ) return {"output_ids": output_ids} def a__ ( self , _a , _a=ReturnType.TEXT , _a=False ) -> List[Any]: _A : Union[str, Any] = [] for output_ids in model_outputs["output_ids"][0]: if return_type == ReturnType.TENSORS: _A : int = {F'''{self.return_name}_token_ids''': output_ids} elif return_type == ReturnType.TEXT: _A : Optional[int] = { F'''{self.return_name}_text''': self.tokenizer.decode( _a , skip_special_tokens=_a , clean_up_tokenization_spaces=_a , ) } records.append(_a ) return records @add_end_docstrings(UpperCamelCase__ ) class lowercase ( UpperCamelCase__ ): _a = "summary" def __call__( self , *_a , **_a ) -> str: return super().__call__(*_a , **_a ) def a__ ( self , _a , _a , _a ) -> bool: if max_length < min_length: logger.warning(F'''Your min_length={min_length} must be inferior than your max_length={max_length}.''' ) if input_length < max_length: logger.warning( F'''Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is ''' """a summarization task, where outputs shorter than the input are typically wanted, you might """ F'''consider decreasing max_length manually, e.g. summarizer(\'...\', max_length={input_length//2})''' ) @add_end_docstrings(UpperCamelCase__ ) class lowercase ( UpperCamelCase__ ): _a = "translation" def a__ ( self , _a , _a , _a ) -> Optional[int]: if input_length > 0.9 * max_length: logger.warning( F'''Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider ''' """increasing your max_length manually, e.g. translator('...', max_length=400)""" ) return True def a__ ( self , *_a , _a=TruncationStrategy.DO_NOT_TRUNCATE , _a=None , _a=None ) -> Optional[int]: if getattr(self.tokenizer , """_build_translation_inputs""" , _a ): return self.tokenizer._build_translation_inputs( *_a , return_tensors=self.framework , truncation=_a , src_lang=_a , tgt_lang=_a ) else: return super()._parse_and_tokenize(*_a , truncation=_a ) def a__ ( self , _a=None , _a=None , **_a ) -> List[Any]: _A , _A , _A : str = super()._sanitize_parameters(**_a ) if src_lang is not None: _A : Optional[Any] = src_lang if tgt_lang is not None: _A : Any = tgt_lang if src_lang is None and tgt_lang is None: # Backward compatibility, direct arguments use is preferred. _A : Any = kwargs.get("""task""" , self.task ) _A : Union[str, Any] = task.split("""_""" ) if task and len(_a ) == 4: # translation, XX, to YY _A : int = items[1] _A : Dict = items[3] return preprocess_params, forward_params, postprocess_params def __call__( self , *_a , **_a ) -> Union[str, Any]: return super().__call__(*_a , **_a )
26
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "facebook/xmod-base": "https://huggingface.co/facebook/xmod-base/resolve/main/config.json", "facebook/xmod-large-prenorm": "https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json", "facebook/xmod-base-13-125k": "https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json", "facebook/xmod-base-30-125k": "https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json", "facebook/xmod-base-30-195k": "https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json", "facebook/xmod-base-60-125k": "https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json", "facebook/xmod-base-60-265k": "https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json", "facebook/xmod-base-75-125k": "https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json", "facebook/xmod-base-75-269k": "https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json", } class lowercase ( UpperCamelCase__ ): _a = "xmod" 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-12 , _a=1 , _a=0 , _a=2 , _a="absolute" , _a=True , _a=None , _a=False , _a=2 , _a=False , _a=True , _a=True , _a=("en_XX",) , _a=None , **_a , ) -> str: super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) _A : Tuple = vocab_size _A : Union[str, Any] = hidden_size _A : Dict = num_hidden_layers _A : Dict = num_attention_heads _A : List[Any] = hidden_act _A : Optional[Any] = intermediate_size _A : Any = hidden_dropout_prob _A : str = attention_probs_dropout_prob _A : Dict = max_position_embeddings _A : Any = type_vocab_size _A : List[Any] = initializer_range _A : int = layer_norm_eps _A : int = position_embedding_type _A : Any = use_cache _A : int = classifier_dropout _A : int = pre_norm _A : Optional[Any] = adapter_reduction_factor _A : List[Any] = adapter_layer_norm _A : Optional[int] = adapter_reuse_layer_norm _A : Any = ln_before_adapter _A : Union[str, Any] = list(_a ) _A : List[Any] = default_language class lowercase ( UpperCamelCase__ ): @property def a__ ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": _A : Dict = {0: """batch""", 1: """choice""", 2: """sequence"""} else: _A : Dict = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
26
1
import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging a_ = logging.get_logger(__name__) a_ = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt'} a_ = { 'vocab_file': { 'allenai/longformer-base-4096': 'https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json', 'allenai/longformer-large-4096': ( 'https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json' ), 'allenai/longformer-large-4096-finetuned-triviaqa': ( 'https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json' ), 'allenai/longformer-base-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json' ), 'allenai/longformer-large-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json' ), }, 'merges_file': { 'allenai/longformer-base-4096': 'https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt', 'allenai/longformer-large-4096': ( 'https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt' ), 'allenai/longformer-large-4096-finetuned-triviaqa': ( 'https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt' ), 'allenai/longformer-base-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt' ), 'allenai/longformer-large-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt' ), }, } a_ = { 'allenai/longformer-base-4096': 4_096, 'allenai/longformer-large-4096': 4_096, 'allenai/longformer-large-4096-finetuned-triviaqa': 4_096, 'allenai/longformer-base-4096-extra.pos.embd.only': 4_096, 'allenai/longformer-large-4096-extra.pos.embd.only': 4_096, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def __lowercase ( ): UpperCamelCase_ : Dict = ( list(range(ord('!' ) , ord('~' ) + 1 ) ) + list(range(ord('¡' ) , ord('¬' ) + 1 ) ) + list(range(ord('®' ) , ord('ÿ' ) + 1 ) ) ) UpperCamelCase_ : str = bs[:] UpperCamelCase_ : Tuple = 0 for b in range(2**8 ): if b not in bs: bs.append(A__ ) cs.append(2**8 + n ) n += 1 UpperCamelCase_ : List[str] = [chr(A__ ) for n in cs] return dict(zip(A__ , A__ ) ) def __lowercase ( lowerCamelCase : Optional[Any] ): UpperCamelCase_ : Optional[Any] = set() UpperCamelCase_ : List[str] = word[0] for char in word[1:]: pairs.add((prev_char, char) ) UpperCamelCase_ : List[str] = char return pairs class _lowercase ( __lowerCamelCase ): lowercase = VOCAB_FILES_NAMES lowercase = PRETRAINED_VOCAB_FILES_MAP lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase = ['input_ids', 'attention_mask'] def __init__( self : Tuple , snake_case : List[str] , snake_case : Union[str, Any] , snake_case : List[str]="replace" , snake_case : str="<s>" , snake_case : Dict="</s>" , snake_case : Optional[Any]="</s>" , snake_case : Any="<s>" , snake_case : Optional[Any]="<unk>" , snake_case : int="<pad>" , snake_case : Tuple="<mask>" , snake_case : str=False , **snake_case : Any , ) -> Tuple: """simple docstring""" UpperCamelCase_ : Tuple = AddedToken(UpperCamelCase_ , lstrip=UpperCamelCase_ , rstrip=UpperCamelCase_ ) if isinstance(UpperCamelCase_ , UpperCamelCase_ ) else bos_token UpperCamelCase_ : Optional[Any] = AddedToken(UpperCamelCase_ , lstrip=UpperCamelCase_ , rstrip=UpperCamelCase_ ) if isinstance(UpperCamelCase_ , UpperCamelCase_ ) else eos_token UpperCamelCase_ : Optional[Any] = AddedToken(UpperCamelCase_ , lstrip=UpperCamelCase_ , rstrip=UpperCamelCase_ ) if isinstance(UpperCamelCase_ , UpperCamelCase_ ) else sep_token UpperCamelCase_ : Tuple = AddedToken(UpperCamelCase_ , lstrip=UpperCamelCase_ , rstrip=UpperCamelCase_ ) if isinstance(UpperCamelCase_ , UpperCamelCase_ ) else cls_token UpperCamelCase_ : Dict = AddedToken(UpperCamelCase_ , lstrip=UpperCamelCase_ , rstrip=UpperCamelCase_ ) if isinstance(UpperCamelCase_ , UpperCamelCase_ ) else unk_token UpperCamelCase_ : Any = AddedToken(UpperCamelCase_ , lstrip=UpperCamelCase_ , rstrip=UpperCamelCase_ ) if isinstance(UpperCamelCase_ , UpperCamelCase_ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it UpperCamelCase_ : Union[str, Any] = AddedToken(UpperCamelCase_ , lstrip=UpperCamelCase_ , rstrip=UpperCamelCase_ ) if isinstance(UpperCamelCase_ , UpperCamelCase_ ) else mask_token super().__init__( errors=UpperCamelCase_ , bos_token=UpperCamelCase_ , eos_token=UpperCamelCase_ , unk_token=UpperCamelCase_ , sep_token=UpperCamelCase_ , cls_token=UpperCamelCase_ , pad_token=UpperCamelCase_ , mask_token=UpperCamelCase_ , add_prefix_space=UpperCamelCase_ , **UpperCamelCase_ , ) with open(UpperCamelCase_ , encoding='utf-8' ) as vocab_handle: UpperCamelCase_ : List[Any] = json.load(UpperCamelCase_ ) UpperCamelCase_ : Tuple = {v: k for k, v in self.encoder.items()} UpperCamelCase_ : Optional[Any] = errors # how to handle errors in decoding UpperCamelCase_ : Optional[Any] = bytes_to_unicode() UpperCamelCase_ : str = {v: k for k, v in self.byte_encoder.items()} with open(UpperCamelCase_ , encoding='utf-8' ) as merges_handle: UpperCamelCase_ : Dict = merges_handle.read().split('\n' )[1:-1] UpperCamelCase_ : Tuple = [tuple(merge.split() ) for merge in bpe_merges] UpperCamelCase_ : Union[str, Any] = dict(zip(UpperCamelCase_ , range(len(UpperCamelCase_ ) ) ) ) UpperCamelCase_ : List[str] = {} UpperCamelCase_ : List[str] = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions UpperCamelCase_ : Optional[int] = re.compile(R'\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+' ) @property def SCREAMING_SNAKE_CASE__ ( self : Tuple ) -> Tuple: """simple docstring""" return len(self.encoder ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" return dict(self.encoder , **self.added_tokens_encoder ) def SCREAMING_SNAKE_CASE__ ( self : Tuple , snake_case : Any ) -> Optional[Any]: """simple docstring""" if token in self.cache: return self.cache[token] UpperCamelCase_ : List[Any] = tuple(UpperCamelCase_ ) UpperCamelCase_ : int = get_pairs(UpperCamelCase_ ) if not pairs: return token while True: UpperCamelCase_ : Tuple = min(UpperCamelCase_ , key=lambda snake_case : self.bpe_ranks.get(UpperCamelCase_ , float('inf' ) ) ) if bigram not in self.bpe_ranks: break UpperCamelCase_, UpperCamelCase_ : Union[str, Any] = bigram UpperCamelCase_ : List[Any] = [] UpperCamelCase_ : Any = 0 while i < len(UpperCamelCase_ ): try: UpperCamelCase_ : List[Any] = word.index(UpperCamelCase_ , UpperCamelCase_ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) UpperCamelCase_ : int = j if word[i] == first and i < len(UpperCamelCase_ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 UpperCamelCase_ : Optional[Any] = tuple(UpperCamelCase_ ) UpperCamelCase_ : List[str] = new_word if len(UpperCamelCase_ ) == 1: break else: UpperCamelCase_ : Any = get_pairs(UpperCamelCase_ ) UpperCamelCase_ : Tuple = ' '.join(UpperCamelCase_ ) UpperCamelCase_ : Dict = word return word def SCREAMING_SNAKE_CASE__ ( self : Tuple , snake_case : int ) -> List[Any]: """simple docstring""" UpperCamelCase_ : Tuple = [] for token in re.findall(self.pat , UpperCamelCase_ ): UpperCamelCase_ : List[str] = ''.join( self.byte_encoder[b] for b in token.encode('utf-8' ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(UpperCamelCase_ ).split(' ' ) ) return bpe_tokens def SCREAMING_SNAKE_CASE__ ( self : List[str] , snake_case : Any ) -> Tuple: """simple docstring""" return self.encoder.get(UpperCamelCase_ , self.encoder.get(self.unk_token ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , snake_case : Tuple ) -> Optional[int]: """simple docstring""" return self.decoder.get(UpperCamelCase_ ) def SCREAMING_SNAKE_CASE__ ( self : int , snake_case : Optional[Any] ) -> Any: """simple docstring""" UpperCamelCase_ : Optional[Any] = ''.join(UpperCamelCase_ ) UpperCamelCase_ : Dict = bytearray([self.byte_decoder[c] for c in text] ).decode('utf-8' , errors=self.errors ) return text def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , snake_case : str , snake_case : Optional[str] = None ) -> Dict: """simple docstring""" if not os.path.isdir(UpperCamelCase_ ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return UpperCamelCase_ : Optional[int] = os.path.join( UpperCamelCase_ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) UpperCamelCase_ : Dict = os.path.join( UpperCamelCase_ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file'] ) with open(UpperCamelCase_ , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=UpperCamelCase_ , ensure_ascii=UpperCamelCase_ ) + '\n' ) UpperCamelCase_ : List[Any] = 0 with open(UpperCamelCase_ , 'w' , encoding='utf-8' ) as writer: writer.write('#version: 0.2\n' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda snake_case : kv[1] ): if index != token_index: logger.warning( f"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive." ' Please check that the tokenizer is not corrupted!' ) UpperCamelCase_ : str = token_index writer.write(' '.join(UpperCamelCase_ ) + '\n' ) index += 1 return vocab_file, merge_file def SCREAMING_SNAKE_CASE__ ( self : Dict , snake_case : List[int] , snake_case : Optional[List[int]] = None ) -> Dict: """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] UpperCamelCase_ : str = [self.cls_token_id] UpperCamelCase_ : Optional[int] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , snake_case : List[int] , snake_case : Optional[List[int]] = None , snake_case : bool = False ) -> Union[str, Any]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=UpperCamelCase_ , token_ids_a=UpperCamelCase_ , already_has_special_tokens=UpperCamelCase_ ) if token_ids_a is None: return [1] + ([0] * len(UpperCamelCase_ )) + [1] return [1] + ([0] * len(UpperCamelCase_ )) + [1, 1] + ([0] * len(UpperCamelCase_ )) + [1] def SCREAMING_SNAKE_CASE__ ( self : Tuple , snake_case : List[int] , snake_case : Optional[List[int]] = None ) -> List[Any]: """simple docstring""" UpperCamelCase_ : Any = [self.sep_token_id] UpperCamelCase_ : 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 + sep + token_ids_a + sep ) * [0] def SCREAMING_SNAKE_CASE__ ( self : Any , snake_case : Tuple , snake_case : int=False , **snake_case : Dict ) -> Any: """simple docstring""" UpperCamelCase_ : List[Any] = kwargs.pop('add_prefix_space' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(UpperCamelCase_ ) > 0 and not text[0].isspace()): UpperCamelCase_ : str = ' ' + text return (text, kwargs)
357
from __future__ import annotations import unittest from transformers import MobileBertConfig, is_tf_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_MODEL_FOR_PRETRAINING_MAPPING, TFMobileBertForMaskedLM, TFMobileBertForMultipleChoice, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertModel, ) @require_tf class _lowercase ( snake_case_ , snake_case_ , unittest.TestCase ): lowercase = ( ( TFMobileBertModel, TFMobileBertForMaskedLM, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertForMultipleChoice, ) if is_tf_available() else () ) lowercase = ( { 'feature-extraction': TFMobileBertModel, 'fill-mask': TFMobileBertForMaskedLM, 'question-answering': TFMobileBertForQuestionAnswering, 'text-classification': TFMobileBertForSequenceClassification, 'token-classification': TFMobileBertForTokenClassification, 'zero-shot': TFMobileBertForSequenceClassification, } if is_tf_available() else {} ) lowercase = False lowercase = False def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , snake_case : Tuple , snake_case : Optional[Any] , snake_case : Dict=False ) -> Optional[Any]: """simple docstring""" UpperCamelCase_ : List[Any] = super()._prepare_for_class(snake_case , snake_case , return_labels=snake_case ) if return_labels: if model_class in get_values(snake_case ): UpperCamelCase_ : Optional[int] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) return inputs_dict class _lowercase ( snake_case_ ): def __init__( self : Tuple , snake_case : Optional[int] , snake_case : Optional[Any]=1_3 , snake_case : Optional[Any]=7 , snake_case : Any=True , snake_case : Optional[int]=True , snake_case : Union[str, Any]=True , snake_case : Optional[Any]=True , snake_case : List[Any]=9_9 , snake_case : int=3_2 , snake_case : str=3_2 , snake_case : str=2 , snake_case : List[Any]=4 , snake_case : Tuple=3_7 , snake_case : Any="gelu" , snake_case : str=0.1 , snake_case : Tuple=0.1 , snake_case : Optional[Any]=5_1_2 , snake_case : Optional[int]=1_6 , snake_case : List[Any]=2 , snake_case : Dict=0.02 , snake_case : List[str]=3 , snake_case : Any=4 , snake_case : Any=None , ) -> int: """simple docstring""" UpperCamelCase_ : Union[str, Any] = parent UpperCamelCase_ : Any = batch_size UpperCamelCase_ : List[str] = seq_length UpperCamelCase_ : List[Any] = is_training UpperCamelCase_ : Optional[Any] = use_input_mask UpperCamelCase_ : Tuple = use_token_type_ids UpperCamelCase_ : Optional[int] = use_labels UpperCamelCase_ : Dict = vocab_size UpperCamelCase_ : Dict = hidden_size UpperCamelCase_ : List[str] = num_hidden_layers UpperCamelCase_ : Tuple = num_attention_heads UpperCamelCase_ : Optional[int] = intermediate_size UpperCamelCase_ : int = hidden_act UpperCamelCase_ : List[str] = hidden_dropout_prob UpperCamelCase_ : Optional[Any] = attention_probs_dropout_prob UpperCamelCase_ : Tuple = max_position_embeddings UpperCamelCase_ : Tuple = type_vocab_size UpperCamelCase_ : Optional[Any] = type_sequence_label_size UpperCamelCase_ : Any = initializer_range UpperCamelCase_ : Tuple = num_labels UpperCamelCase_ : Tuple = num_choices UpperCamelCase_ : Tuple = scope UpperCamelCase_ : Dict = embedding_size def SCREAMING_SNAKE_CASE__ ( self : str ) -> str: """simple docstring""" UpperCamelCase_ : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase_ : Optional[Any] = None if self.use_input_mask: UpperCamelCase_ : List[str] = random_attention_mask([self.batch_size, self.seq_length] ) 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_ : Optional[int] = None UpperCamelCase_ : Tuple = None UpperCamelCase_ : Dict = None if self.use_labels: UpperCamelCase_ : Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase_ : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase_ : Tuple = ids_tensor([self.batch_size] , self.num_choices ) UpperCamelCase_ : Union[str, Any] = MobileBertConfig( 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 , embedding_size=self.embedding_size , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE__ ( self : Dict , snake_case : str , snake_case : Optional[int] , snake_case : Optional[Any] , snake_case : Tuple , snake_case : str , snake_case : Optional[Any] , snake_case : List[str] ) -> int: """simple docstring""" UpperCamelCase_ : str = TFMobileBertModel(config=snake_case ) UpperCamelCase_ : Optional[Any] = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} UpperCamelCase_ : Union[str, Any] = model(snake_case ) UpperCamelCase_ : Optional[Any] = [input_ids, input_mask] UpperCamelCase_ : List[Any] = model(snake_case ) UpperCamelCase_ : Union[str, Any] = model(snake_case ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def SCREAMING_SNAKE_CASE__ ( self : int , snake_case : Union[str, Any] , snake_case : Optional[int] , snake_case : Union[str, Any] , snake_case : Dict , snake_case : Any , snake_case : Dict , snake_case : int ) -> Union[str, Any]: """simple docstring""" UpperCamelCase_ : List[str] = TFMobileBertForMaskedLM(config=snake_case ) UpperCamelCase_ : Optional[int] = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} UpperCamelCase_ : int = model(snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self : Tuple , snake_case : Any , snake_case : int , snake_case : int , snake_case : Union[str, Any] , snake_case : Optional[int] , snake_case : Optional[Any] , snake_case : Optional[int] ) -> Optional[Any]: """simple docstring""" UpperCamelCase_ : Optional[int] = TFMobileBertForNextSentencePrediction(config=snake_case ) UpperCamelCase_ : Any = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} UpperCamelCase_ : List[Any] = model(snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , snake_case : Optional[Any] , snake_case : List[Any] , snake_case : int , snake_case : str , snake_case : str , snake_case : Any , snake_case : List[Any] ) -> Tuple: """simple docstring""" UpperCamelCase_ : List[str] = TFMobileBertForPreTraining(config=snake_case ) UpperCamelCase_ : List[Any] = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} UpperCamelCase_ : Any = model(snake_case ) self.parent.assertEqual( result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.seq_relationship_logits.shape , (self.batch_size, 2) ) def SCREAMING_SNAKE_CASE__ ( self : Any , snake_case : Optional[int] , snake_case : Optional[Any] , snake_case : Dict , snake_case : List[str] , snake_case : str , snake_case : List[str] , snake_case : List[Any] ) -> Optional[int]: """simple docstring""" UpperCamelCase_ : List[Any] = self.num_labels UpperCamelCase_ : Dict = TFMobileBertForSequenceClassification(config=snake_case ) UpperCamelCase_ : List[Any] = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} UpperCamelCase_ : List[Any] = model(snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] , snake_case : Tuple , snake_case : Any , snake_case : Optional[int] , snake_case : Optional[Any] , snake_case : str , snake_case : List[str] , snake_case : Any ) -> List[str]: """simple docstring""" UpperCamelCase_ : Optional[int] = self.num_choices UpperCamelCase_ : Dict = TFMobileBertForMultipleChoice(config=snake_case ) UpperCamelCase_ : int = tf.tile(tf.expand_dims(snake_case , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase_ : int = tf.tile(tf.expand_dims(snake_case , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase_ : List[str] = tf.tile(tf.expand_dims(snake_case , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase_ : Optional[Any] = { 'input_ids': multiple_choice_inputs_ids, 'attention_mask': multiple_choice_input_mask, 'token_type_ids': multiple_choice_token_type_ids, } UpperCamelCase_ : int = model(snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , snake_case : Optional[int] , snake_case : Tuple , snake_case : str , snake_case : str , snake_case : Optional[int] , snake_case : str , snake_case : List[str] ) -> Optional[int]: """simple docstring""" UpperCamelCase_ : Any = self.num_labels UpperCamelCase_ : Optional[Any] = TFMobileBertForTokenClassification(config=snake_case ) UpperCamelCase_ : str = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} UpperCamelCase_ : Tuple = model(snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self : int , snake_case : Tuple , snake_case : Tuple , snake_case : Union[str, Any] , snake_case : Optional[int] , snake_case : Union[str, Any] , snake_case : Dict , snake_case : List[str] ) -> List[str]: """simple docstring""" UpperCamelCase_ : Optional[Any] = TFMobileBertForQuestionAnswering(config=snake_case ) UpperCamelCase_ : int = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} UpperCamelCase_ : Tuple = model(snake_case ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> Dict: """simple docstring""" UpperCamelCase_ : Tuple = self.prepare_config_and_inputs() ( ( UpperCamelCase_ ), ( UpperCamelCase_ ), ( UpperCamelCase_ ), ( UpperCamelCase_ ), ( UpperCamelCase_ ), ( UpperCamelCase_ ), ( UpperCamelCase_ ), ) : Union[str, Any] = config_and_inputs UpperCamelCase_ : Tuple = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ) -> int: """simple docstring""" UpperCamelCase_ : Union[str, Any] = TFMobileBertModelTest.TFMobileBertModelTester(self ) UpperCamelCase_ : str = ConfigTester(self , config_class=snake_case , hidden_size=3_7 ) def SCREAMING_SNAKE_CASE__ ( self : str ) -> List[Any]: """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE__ ( self : int ) -> str: """simple docstring""" UpperCamelCase_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_model(*snake_case ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ) -> Tuple: """simple docstring""" UpperCamelCase_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_masked_lm(*snake_case ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ) -> str: """simple docstring""" UpperCamelCase_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_multiple_choice(*snake_case ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> Optional[Any]: """simple docstring""" UpperCamelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*snake_case ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ) -> Dict: """simple docstring""" UpperCamelCase_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_pretraining(*snake_case ) def SCREAMING_SNAKE_CASE__ ( self : int ) -> Optional[Any]: """simple docstring""" UpperCamelCase_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_question_answering(*snake_case ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ) -> str: """simple docstring""" UpperCamelCase_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_sequence_classification(*snake_case ) def SCREAMING_SNAKE_CASE__ ( self : Any ) -> str: """simple docstring""" UpperCamelCase_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_token_classification(*snake_case ) @slow def SCREAMING_SNAKE_CASE__ ( self : str ) -> Tuple: """simple docstring""" for model_name in ["google/mobilebert-uncased"]: UpperCamelCase_ : Optional[Any] = TFMobileBertModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) @require_tf class _lowercase ( unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> Dict: """simple docstring""" UpperCamelCase_ : Any = TFMobileBertForPreTraining.from_pretrained('google/mobilebert-uncased' ) UpperCamelCase_ : List[Any] = tf.constant([[0, 1, 2, 3, 4, 5]] ) UpperCamelCase_ : List[str] = model(snake_case )[0] UpperCamelCase_ : Any = [1, 6, 3_0_5_2_2] self.assertEqual(output.shape , snake_case ) UpperCamelCase_ : Dict = tf.constant( [ [ [-4.5919547, -9.248295, -9.645256], [-6.7306175, -6.440284, -6.6052837], [-7.2743506, -6.7847915, -6.024673], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , snake_case , atol=1e-4 )
50
0
'''simple docstring''' def lowerCAmelCase (__A = 1_000): """simple docstring""" return sum(2 * a * ((a - 1) // 2) for a in range(3 , n + 1)) if __name__ == "__main__": print(solution())
211
'''simple docstring''' import warnings from contextlib import contextmanager from ....processing_utils import ProcessorMixin class __A ( A ): '''simple docstring''' __lowerCamelCase : Optional[Any] = 'MCTCTFeatureExtractor' __lowerCamelCase : Optional[Any] = 'AutoTokenizer' def __init__(self , A , A ) -> Dict: """simple docstring""" super().__init__(A , A ) _a = self.feature_extractor _a = False def __call__(self , *A , **A ) -> Optional[int]: """simple docstring""" if self._in_target_context_manager: return self.current_processor(*A , **A ) if "raw_speech" in kwargs: warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.''' ) _a = kwargs.pop('''raw_speech''' ) else: _a = kwargs.pop('''audio''' , A ) _a = kwargs.pop('''sampling_rate''' , A ) _a = kwargs.pop('''text''' , A ) if len(A ) > 0: _a = args[0] _a = args[1:] if audio is None and text is None: raise ValueError('''You need to specify either an `audio` or `text` input to process.''' ) if audio is not None: _a = self.feature_extractor(A , *A , sampling_rate=A , **A ) if text is not None: _a = self.tokenizer(A , **A ) if text is None: return inputs elif audio is None: return encodings else: _a = encodings['''input_ids'''] return inputs def a__ (self , *A , **A ) -> int: """simple docstring""" return self.tokenizer.batch_decode(*A , **A ) def a__ (self , *A , **A ) -> Tuple: """simple docstring""" if self._in_target_context_manager: return self.current_processor.pad(*A , **A ) _a = kwargs.pop('''input_features''' , A ) _a = kwargs.pop('''labels''' , A ) if len(A ) > 0: _a = args[0] _a = args[1:] if input_features is not None: _a = self.feature_extractor.pad(A , *A , **A ) if labels is not None: _a = self.tokenizer.pad(A , **A ) if labels is None: return input_features elif input_features is None: return labels else: _a = labels['''input_ids'''] return input_features def a__ (self , *A , **A ) -> List[str]: """simple docstring""" return self.tokenizer.decode(*A , **A ) @contextmanager def a__ (self ) -> Dict: """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.''' ) _a = True _a = self.tokenizer yield _a = self.feature_extractor _a = False
211
1
import unittest import numpy as np import torch from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class SCREAMING_SNAKE_CASE ( unittest.TestCase ): """simple docstring""" @property def __A ( self: Dict ) -> Union[str, Any]: torch.manual_seed(0 ) _A = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def __A ( self: Any ) -> Union[str, Any]: _A = self.dummy_uncond_unet _A = ScoreSdeVeScheduler() _A = ScoreSdeVePipeline(unet=__A , scheduler=__A ) sde_ve.to(__A ) sde_ve.set_progress_bar_config(disable=__A ) _A = torch.manual_seed(0 ) _A = sde_ve(num_inference_steps=2 , output_type='''numpy''' , generator=__A ).images _A = torch.manual_seed(0 ) _A = sde_ve(num_inference_steps=2 , output_type='''numpy''' , generator=__A , return_dict=__A )[ 0 ] _A = image[0, -3:, -3:, -1] _A = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) _A = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class SCREAMING_SNAKE_CASE ( unittest.TestCase ): """simple docstring""" def __A ( self: Dict ) -> Any: _A = '''google/ncsnpp-church-256''' _A = UNetaDModel.from_pretrained(__A ) _A = ScoreSdeVeScheduler.from_pretrained(__A ) _A = ScoreSdeVePipeline(unet=__A , scheduler=__A ) sde_ve.to(__A ) sde_ve.set_progress_bar_config(disable=__A ) _A = torch.manual_seed(0 ) _A = sde_ve(num_inference_steps=10 , output_type='''numpy''' , generator=__A ).images _A = image[0, -3:, -3:, -1] assert image.shape == (1, 2_56, 2_56, 3) _A = np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
75
from abc import ABC, abstractmethod from typing import Optional, Union from .. import Dataset, DatasetDict, Features, IterableDataset, IterableDatasetDict, NamedSplit from ..utils.typing import NestedDataStructureLike, PathLike class SCREAMING_SNAKE_CASE ( snake_case ): """simple docstring""" def __init__( self: List[str] , __A: Optional[NestedDataStructureLike[PathLike]] = None , __A: Optional[NamedSplit] = None , __A: Optional[Features] = None , __A: str = None , __A: bool = False , __A: bool = False , __A: Optional[int] = None , **__A: Tuple , ) -> List[Any]: _A = path_or_paths _A = split if split or isinstance(__A , __A ) else '''train''' _A = features _A = cache_dir _A = keep_in_memory _A = streaming _A = num_proc _A = kwargs @abstractmethod def __A ( self: Union[str, Any] ) -> Union[Dataset, DatasetDict, IterableDataset, IterableDatasetDict]: pass class SCREAMING_SNAKE_CASE ( snake_case ): """simple docstring""" def __init__( self: str , __A: Optional[Features] = None , __A: str = None , __A: bool = False , __A: bool = False , __A: Optional[int] = None , **__A: Optional[int] , ) -> Union[str, Any]: _A = features _A = cache_dir _A = keep_in_memory _A = streaming _A = num_proc _A = kwargs @abstractmethod def __A ( self: Any ) -> Union[Dataset, IterableDataset]: pass
75
1
"""simple docstring""" import torch def a_ ( ): '''simple docstring''' if torch.cuda.is_available(): lowercase__ : Tuple = torch.cuda.device_count() else: lowercase__ : Optional[int] = 0 print(f"""Successfully ran on {num_gpus} GPUs""" ) if __name__ == "__main__": main()
77
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_big_bird import BigBirdTokenizer else: __snake_case = None __snake_case = logging.get_logger(__name__) __snake_case = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} __snake_case = { '''vocab_file''': { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model''', '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model''' ), }, '''tokenizer_file''': { '''google/bigbird-roberta-base''': ( '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/tokenizer.json''' ), '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/tokenizer.json''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/tokenizer.json''' ), }, } __snake_case = { '''google/bigbird-roberta-base''': 40_96, '''google/bigbird-roberta-large''': 40_96, '''google/bigbird-base-trivia-itc''': 40_96, } __snake_case = '''▁''' class __snake_case ( lowerCamelCase__ ): __lowerCamelCase : Dict = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = BigBirdTokenizer __lowerCamelCase : Any = ["""input_ids""", """attention_mask"""] __lowerCamelCase : List[int] = [] def __init__( self , snake_case__=None , snake_case__=None , snake_case__="<unk>" , snake_case__="<s>" , snake_case__="</s>" , snake_case__="<pad>" , snake_case__="[SEP]" , snake_case__="[MASK]" , snake_case__="[CLS]" , **snake_case__ , ) -> List[Any]: '''simple docstring''' UpperCAmelCase : Any =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else bos_token UpperCAmelCase : Optional[int] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else eos_token UpperCAmelCase : List[str] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else unk_token UpperCAmelCase : Union[str, Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else pad_token UpperCAmelCase : int =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else cls_token UpperCAmelCase : str =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else sep_token # Mask token behave like a normal word, i.e. include the space before it UpperCAmelCase : List[Any] =AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else mask_token super().__init__( snake_case__ , tokenizer_file=snake_case__ , bos_token=snake_case__ , eos_token=snake_case__ , unk_token=snake_case__ , sep_token=snake_case__ , pad_token=snake_case__ , cls_token=snake_case__ , mask_token=snake_case__ , **snake_case__ , ) UpperCAmelCase : Tuple =vocab_file UpperCAmelCase : Optional[int] =False if not self.vocab_file else True def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : int =[self.sep_token_id] UpperCAmelCase : Optional[int] =[self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None , snake_case__ = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is None: return [1] + ([0] * len(snake_case__ )) + [1] return [1] + ([0] * len(snake_case__ )) + [1] + ([0] * len(snake_case__ )) + [1] def UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> List[int]: '''simple docstring''' UpperCAmelCase : Optional[Any] =[self.sep_token_id] UpperCAmelCase : Optional[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 UpperCAmelCase__ ( self , snake_case__ , snake_case__ = None ) -> Tuple[str]: '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(snake_case__ ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCAmelCase : Optional[int] =os.path.join( snake_case__ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case__ ): copyfile(self.vocab_file , snake_case__ ) return (out_vocab_file,)
348
0
import math def a_ ( __lowercase : int = 100 ) -> int: _snake_case = sum(i * i for i in range(1 , n + 1 ) ) _snake_case = int(math.pow(sum(range(1 , n + 1 ) ) , 2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(F'{solution() = }')
355
import baseaa def a_ ( __lowercase : str ) -> bytes: return baseaa.aaaencode(string.encode('utf-8' ) ) def a_ ( __lowercase : bytes ) -> str: return baseaa.aaadecode(__lowercase ).decode('utf-8' ) if __name__ == "__main__": import doctest doctest.testmod()
130
0
'''simple docstring''' from __future__ import annotations def __lowerCamelCase ( A__ , A__ , A__ , A__ , A__ , ) -> None: """simple docstring""" UpperCamelCase = len(A__ ) # If row is equal to the size of the board it means there are a queen in each row in # the current board (possible_board) if row == n: # We convert the variable possible_board that looks like this: [1, 3, 0, 2] to # this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . '] boards.append(['. ' * i + 'Q ' + '. ' * (n - 1 - i) for i in possible_board] ) return # We iterate each column in the row to find all possible results in each row for col in range(A__ ): # We apply that we learned previously. First we check that in the current board # (possible_board) there are not other same value because if there is it means # that there are a collision in vertical. Then we apply the two formulas we # learned before: # # 45º: y - x = b or 45: row - col = b # 135º: y + x = b or row + col = b. # # And we verify if the results of this two formulas not exist in their variables # respectively. (diagonal_right_collisions, diagonal_left_collisions) # # If any or these are True it means there is a collision so we continue to the # next value in the for loop. if ( col in possible_board or row - col in diagonal_right_collisions or row + col in diagonal_left_collisions ): continue # If it is False we call dfs function again and we update the inputs depth_first_search( [*possible_board, col] , [*diagonal_right_collisions, row - col] , [*diagonal_left_collisions, row + col] , A__ , A__ , ) def __lowerCamelCase ( A__ ) -> None: """simple docstring""" UpperCamelCase = [] depth_first_search([] , [] , [] , A__ , A__ ) # Print all the boards for board in boards: for column in board: print(A__ ) print('' ) print(len(A__ ) , 'solutions were found.' ) if __name__ == "__main__": import doctest doctest.testmod() n_queens_solution(4)
28
'''simple docstring''' from statistics import mean, stdev def UpperCamelCase_( snake_case : list , snake_case : int = 3 ): '''simple docstring''' snake_case_ = min(snake_case ) snake_case_ = max(snake_case ) # normalize data return [round((x - x_min) / (x_max - x_min) , snake_case ) for x in data] def UpperCamelCase_( snake_case : list , snake_case : int = 3 ): '''simple docstring''' snake_case_ = mean(snake_case ) snake_case_ = stdev(snake_case ) # standardize data return [round((x - mu) / (sigma) , snake_case ) for x in data]
85
0
def __UpperCamelCase ( _A : int , _A : int ) ->int: """simple docstring""" return int(input_a == input_a == 0 ) def __UpperCamelCase ( ) ->None: """simple docstring""" print("""Truth Table of NOR Gate:""" ) print("""| Input 1 | Input 2 | Output |""" ) print(f'| 0 | 0 | {nor_gate(0 , 0 )} |' ) print(f'| 0 | 1 | {nor_gate(0 , 1 )} |' ) print(f'| 1 | 0 | {nor_gate(1 , 0 )} |' ) print(f'| 1 | 1 | {nor_gate(1 , 1 )} |' ) if __name__ == "__main__": import doctest doctest.testmod() main()
49
import argparse import torch from transformers import BlenderbotConfig, BlenderbotForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() __A : List[Any] = logging.get_logger(__name__) __A : List[Any] = [ ['attention', 'attn'], ['encoder_attention', 'encoder_attn'], ['q_lin', 'q_proj'], ['k_lin', 'k_proj'], ['v_lin', 'v_proj'], ['out_lin', 'out_proj'], ['norm_embeddings', 'layernorm_embedding'], ['position_embeddings', 'embed_positions'], ['embeddings', 'embed_tokens'], ['ffn.lin', 'fc'], ] def __UpperCamelCase ( _A : Optional[int] ) ->List[str]: """simple docstring""" if k == "embeddings.weight": return "shared.weight" for parlai_name, hf_name in PATTERNS: lowerCamelCase_ =k.replace(_A , _A ) if k.startswith("""encoder""" ): lowerCamelCase_ =k.replace(""".attn""" , """.self_attn""" ) lowerCamelCase_ =k.replace("""norm1""" , """self_attn_layer_norm""" ) lowerCamelCase_ =k.replace("""norm2""" , """final_layer_norm""" ) elif k.startswith("""decoder""" ): lowerCamelCase_ =k.replace("""norm1""" , """self_attn_layer_norm""" ) lowerCamelCase_ =k.replace("""norm2""" , """encoder_attn_layer_norm""" ) lowerCamelCase_ =k.replace("""norm3""" , """final_layer_norm""" ) return k def __UpperCamelCase ( _A : Union[str, Any] ) ->Optional[int]: """simple docstring""" lowerCamelCase_ =[ """model.encoder.layernorm_embedding.weight""", """model.encoder.layernorm_embedding.bias""", """model.decoder.layernorm_embedding.weight""", """model.decoder.layernorm_embedding.bias""", ] for k in keys: lowerCamelCase_ =sd.pop(_A ) lowerCamelCase_ =k.replace("""layernorm_embedding""" , """layer_norm""" ) assert new_k not in sd lowerCamelCase_ =v __A : Any = ['START'] @torch.no_grad() def __UpperCamelCase ( _A : List[Any] , _A : Union[str, Any] , _A : List[str] ) ->List[str]: """simple docstring""" lowerCamelCase_ =torch.load(_A , map_location="""cpu""" ) lowerCamelCase_ =model["""model"""] lowerCamelCase_ =BlenderbotConfig.from_json_file(_A ) lowerCamelCase_ =BlenderbotForConditionalGeneration(_A ) lowerCamelCase_ =m.model.state_dict().keys() lowerCamelCase_ =[] lowerCamelCase_ ={} for k, v in sd.items(): if k in IGNORE_KEYS: continue lowerCamelCase_ =rename_state_dict_key(_A ) if new_k not in valid_keys: failures.append([k, new_k] ) else: lowerCamelCase_ =v if cfg.normalize_before: # Blenderbot-3B checkpoints. Rename layernorm_embedding -> layer_norm rename_layernorm_keys(_A ) m.model.load_state_dict(_A , strict=_A ) m.half() m.save_pretrained(_A ) if __name__ == "__main__": __A : Any = argparse.ArgumentParser() # Required parameters parser.add_argument('--src_path', type=str, help='like blenderbot-model.bin') parser.add_argument('--save_dir', default='hf_blenderbot', type=str, help='Where to save converted model.') parser.add_argument( '--hf_config_json', default='blenderbot-3b-config.json', type=str, help='Path to config to use' ) __A : str = parser.parse_args() convert_parlai_checkpoint(args.src_path, args.save_dir, args.hf_config_json)
49
1
'''simple docstring''' import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import SPIECE_UNDERLINE, logging snake_case_ : int = logging.get_logger(__name__) snake_case_ : str = {'vocab_file': 'spiece.model'} snake_case_ : Union[str, Any] = { 'vocab_file': { 'TsinghuaAI/CPM-Generate': 'https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model', } } class lowercase__ ( lowercase ): def __init__( self : Union[str, Any] ,lowerCamelCase__ : Optional[Any] ,lowerCamelCase__ : Optional[Any]=False ,lowerCamelCase__ : Union[str, Any]=True ,lowerCamelCase__ : str=False ,lowerCamelCase__ : int="<s>" ,lowerCamelCase__ : str="</s>" ,lowerCamelCase__ : List[str]="<unk>" ,lowerCamelCase__ : List[Any]="<sep>" ,lowerCamelCase__ : Dict="<pad>" ,lowerCamelCase__ : Dict="<cls>" ,lowerCamelCase__ : str="<mask>" ,lowerCamelCase__ : Dict=["<eop>", "<eod>"] ,lowerCamelCase__ : Optional[Dict[str, Any]] = None ,**lowerCamelCase__ : Any ,): '''simple docstring''' _UpperCamelCase : Optional[Any] = AddedToken(lowerCamelCase__ ,lstrip=lowerCamelCase__ ,rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ ,lowerCamelCase__ ) else mask_token _UpperCamelCase : Dict = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=lowerCamelCase__ ,remove_space=lowerCamelCase__ ,keep_accents=lowerCamelCase__ ,bos_token=lowerCamelCase__ ,eos_token=lowerCamelCase__ ,unk_token=lowerCamelCase__ ,sep_token=lowerCamelCase__ ,pad_token=lowerCamelCase__ ,cls_token=lowerCamelCase__ ,mask_token=lowerCamelCase__ ,additional_special_tokens=lowerCamelCase__ ,sp_model_kwargs=self.sp_model_kwargs ,**lowerCamelCase__ ,) _UpperCamelCase : Union[str, Any] = 3 _UpperCamelCase : int = do_lower_case _UpperCamelCase : str = remove_space _UpperCamelCase : str = keep_accents _UpperCamelCase : Any = vocab_file _UpperCamelCase : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(lowerCamelCase__ ) try: import jieba except ModuleNotFoundError as error: raise error.__class__( 'You need to install jieba to use CpmTokenizer or CpmTokenizerFast. ' 'See https://pypi.org/project/jieba/ for installation.' ) _UpperCamelCase : Tuple = jieba _UpperCamelCase : Optional[Any] = str.maketrans(' \n' ,'\u2582\u2583' ) @property # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size def UpperCamelCase_ ( self : int ): '''simple docstring''' return len(self.sp_model ) def UpperCamelCase_ ( self : Dict ): '''simple docstring''' _UpperCamelCase : Any = {self.convert_ids_to_tokens(lowerCamelCase__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : List[Any] ): '''simple docstring''' _UpperCamelCase : Optional[Any] = self.__dict__.copy() _UpperCamelCase : int = None return state def __setstate__( self : List[str] ,lowerCamelCase__ : Any ): '''simple docstring''' _UpperCamelCase : Optional[Any] = d # for backward compatibility if not hasattr(self ,'sp_model_kwargs' ): _UpperCamelCase : List[Any] = {} _UpperCamelCase : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase_ ( self : int ,lowerCamelCase__ : Dict ): '''simple docstring''' if self.remove_space: _UpperCamelCase : str = ' '.join(inputs.strip().split() ) else: _UpperCamelCase : Union[str, Any] = inputs _UpperCamelCase : List[str] = outputs.replace('``' ,'"' ).replace('\'\'' ,'"' ) if not self.keep_accents: _UpperCamelCase : List[str] = unicodedata.normalize('NFKD' ,lowerCamelCase__ ) _UpperCamelCase : Any = ''.join([c for c in outputs if not unicodedata.combining(lowerCamelCase__ )] ) if self.do_lower_case: _UpperCamelCase : Any = outputs.lower() return outputs def UpperCamelCase_ ( self : Optional[Any] ,lowerCamelCase__ : str ): '''simple docstring''' _UpperCamelCase : Any = self.preprocess_text(lowerCamelCase__ ) _UpperCamelCase : Union[str, Any] = self.sp_model.encode(lowerCamelCase__ ,out_type=lowerCamelCase__ ) _UpperCamelCase : int = [] for piece in pieces: if len(lowerCamelCase__ ) > 1 and piece[-1] == str(',' ) and piece[-2].isdigit(): _UpperCamelCase : Union[str, Any] = self.sp_model.EncodeAsPieces(piece[:-1].replace(lowerCamelCase__ ,'' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: _UpperCamelCase : Tuple = cur_pieces[1:] else: _UpperCamelCase : Optional[int] = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(lowerCamelCase__ ) else: new_pieces.append(lowerCamelCase__ ) return new_pieces def UpperCamelCase_ ( self : Any ,lowerCamelCase__ : int ): '''simple docstring''' return self.sp_model.PieceToId(lowerCamelCase__ ) def UpperCamelCase_ ( self : List[Any] ,lowerCamelCase__ : Union[str, Any] ): '''simple docstring''' return self.sp_model.IdToPiece(lowerCamelCase__ ) def UpperCamelCase_ ( self : List[str] ,lowerCamelCase__ : str ): '''simple docstring''' _UpperCamelCase : int = ''.join(lowerCamelCase__ ).replace(lowerCamelCase__ ,' ' ).strip() return out_string def UpperCamelCase_ ( self : Union[str, Any] ,lowerCamelCase__ : List[int] ,lowerCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' _UpperCamelCase : Optional[Any] = [self.sep_token_id] _UpperCamelCase : Tuple = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def UpperCamelCase_ ( self : Optional[int] ,lowerCamelCase__ : List[int] ,lowerCamelCase__ : Optional[List[int]] = None ,lowerCamelCase__ : bool = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCamelCase__ ,token_ids_a=lowerCamelCase__ ,already_has_special_tokens=lowerCamelCase__ ) if token_ids_a is not None: return ([0] * len(lowerCamelCase__ )) + [1] + ([0] * len(lowerCamelCase__ )) + [1, 1] return ([0] * len(lowerCamelCase__ )) + [1, 1] def UpperCamelCase_ ( self : List[str] ,lowerCamelCase__ : List[int] ,lowerCamelCase__ : Optional[List[int]] = None ): '''simple docstring''' _UpperCamelCase : Union[str, Any] = [self.sep_token_id] _UpperCamelCase : int = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def UpperCamelCase_ ( self : List[str] ,lowerCamelCase__ : str ,lowerCamelCase__ : Optional[str] = None ): '''simple docstring''' if not os.path.isdir(lowerCamelCase__ ): logger.error(F'Vocabulary path ({save_directory}) should be a directory' ) return _UpperCamelCase : str = os.path.join( lowerCamelCase__ ,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowerCamelCase__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,lowerCamelCase__ ) elif not os.path.isfile(self.vocab_file ): with open(lowerCamelCase__ ,'wb' ) as fi: _UpperCamelCase : str = self.sp_model.serialized_model_proto() fi.write(lowerCamelCase__ ) return (out_vocab_file,) def UpperCamelCase_ ( self : Union[str, Any] ,*lowerCamelCase__ : Dict ,**lowerCamelCase__ : Tuple ): '''simple docstring''' _UpperCamelCase : List[str] = super()._decode(*lowerCamelCase__ ,**lowerCamelCase__ ) _UpperCamelCase : Dict = text.replace(' ' ,'' ).replace('\u2582' ,' ' ).replace('\u2583' ,'\n' ) return text
83
'''simple docstring''' 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(lowercase ) , """Tatoeba directory does not exist.""" ) class lowercase__ ( unittest.TestCase ): @cached_property def UpperCamelCase_ ( self : Optional[int] ): '''simple docstring''' _UpperCamelCase : str = tempfile.mkdtemp() return TatoebaConverter(save_dir=lowerCamelCase__ ) @slow def UpperCamelCase_ ( self : Any ): '''simple docstring''' self.resolver.convert_models(['heb-eng'] ) @slow def UpperCamelCase_ ( self : Dict ): '''simple docstring''' _UpperCamelCase , _UpperCamelCase : Dict = self.resolver.write_model_card('opus-mt-he-en' ,dry_run=lowerCamelCase__ ) assert mmeta["long_pair"] == "heb-eng"
83
1
"""simple docstring""" import warnings from ...utils import logging from .image_processing_poolformer import PoolFormerImageProcessor __UpperCamelCase : Optional[Any] = logging.get_logger(__name__) class a ( a__ ): def __init__( self , *_snake_case , **_snake_case ): """simple docstring""" warnings.warn( 'The class PoolFormerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use PoolFormerImageProcessor instead.' , _snake_case , ) super().__init__(*_snake_case , **_snake_case )
365
"""simple docstring""" import pytest from datasets.splits import SplitDict, SplitInfo from datasets.utils.py_utils import asdict @pytest.mark.parametrize( 'split_dict' , [ SplitDict(), SplitDict({'train': SplitInfo(name='train' , num_bytes=1337 , num_examples=42 , dataset_name='my_dataset' )} ), SplitDict({'train': SplitInfo(name='train' , num_bytes=1337 , num_examples=42 )} ), SplitDict({'train': SplitInfo()} ), ] , ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : SplitDict ): lowerCAmelCase = split_dict._to_yaml_list() assert len(_UpperCAmelCase ) == len(_UpperCAmelCase ) lowerCAmelCase = SplitDict._from_yaml_list(_UpperCAmelCase ) for split_name, split_info in split_dict.items(): # dataset_name field is deprecated, and is therefore not part of the YAML dump lowerCAmelCase = None # the split name of split_dict takes over the name of the split info object lowerCAmelCase = split_name assert split_dict == reloaded @pytest.mark.parametrize( 'split_info' , [SplitInfo(), SplitInfo(dataset_name=_UpperCAmelCase ), SplitInfo(dataset_name='my_dataset' )] ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : List[str] ): # For backward compatibility, we need asdict(split_dict) to return split info dictrionaries with the "dataset_name" # field even if it's deprecated. This way old versionso of `datasets` can still reload dataset_infos.json files lowerCAmelCase = asdict(SplitDict({'train': split_info} ) ) assert "dataset_name" in split_dict_asdict["train"] assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name
309
0
'''simple docstring''' import copy import inspect import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import VideoMAEConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEForPreTraining, VideoMAEForVideoClassification, VideoMAEModel, ) from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from transformers import VideoMAEImageProcessor class snake_case : """simple docstring""" def __init__( self , UpperCamelCase , UpperCamelCase=13 , UpperCamelCase=10 , UpperCamelCase=3 , UpperCamelCase=2 , UpperCamelCase=2 , UpperCamelCase=2 , UpperCamelCase=True , UpperCamelCase=True , UpperCamelCase=32 , UpperCamelCase=5 , UpperCamelCase=4 , UpperCamelCase=37 , UpperCamelCase="gelu" , UpperCamelCase=0.1 , UpperCamelCase=0.1 , UpperCamelCase=10 , UpperCamelCase=0.02 , UpperCamelCase=0.9 , UpperCamelCase=None , ): """simple docstring""" lowerCamelCase_ = parent lowerCamelCase_ = batch_size lowerCamelCase_ = image_size lowerCamelCase_ = num_channels lowerCamelCase_ = patch_size lowerCamelCase_ = tubelet_size lowerCamelCase_ = num_frames lowerCamelCase_ = is_training lowerCamelCase_ = use_labels 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_ = type_sequence_label_size lowerCamelCase_ = initializer_range lowerCamelCase_ = mask_ratio lowerCamelCase_ = scope # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame lowerCamelCase_ = (image_size // patch_size) ** 2 lowerCamelCase_ = (num_frames // tubelet_size) * self.num_patches_per_frame # use this variable to define bool_masked_pos lowerCamelCase_ = int(mask_ratio * self.seq_length ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = floats_tensor( [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase_ = None if self.use_labels: lowerCamelCase_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase_ = self.get_config() return config, pixel_values, labels def snake_case ( self ): """simple docstring""" return VideoMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , num_frames=self.num_frames , tubelet_size=self.tubelet_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 , is_decoder=UpperCamelCase , initializer_range=self.initializer_range , ) def snake_case ( self , UpperCamelCase , UpperCamelCase , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = VideoMAEModel(config=UpperCamelCase ) model.to(UpperCamelCase ) model.eval() lowerCamelCase_ = model(UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def snake_case ( self , UpperCamelCase , UpperCamelCase , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = VideoMAEForPreTraining(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch lowerCamelCase_ = torch.ones((self.num_masks,) ) lowerCamelCase_ = torch.cat([mask, torch.zeros(self.seq_length - mask.size(0 ) )] ) lowerCamelCase_ = mask.expand(self.batch_size , -1 ).bool() lowerCamelCase_ = model(UpperCamelCase , UpperCamelCase ) # model only returns predictions for masked patches lowerCamelCase_ = mask.sum().item() lowerCamelCase_ = 3 * self.tubelet_size * self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_masked_patches, decoder_num_labels) ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = self.prepare_config_and_inputs() lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ = config_and_inputs lowerCamelCase_ = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class snake_case ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" _lowerCamelCase = ( (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else () ) _lowerCamelCase = ( {"feature-extraction": VideoMAEModel, "video-classification": VideoMAEForVideoClassification} if is_torch_available() else {} ) _lowerCamelCase = False _lowerCamelCase = False _lowerCamelCase = False _lowerCamelCase = False def snake_case ( self ): """simple docstring""" lowerCamelCase_ = VideoMAEModelTester(self ) lowerCamelCase_ = ConfigTester(self , config_class=UpperCamelCase , has_text_modality=UpperCamelCase , hidden_size=37 ) def snake_case ( self , UpperCamelCase , UpperCamelCase , UpperCamelCase=False ): """simple docstring""" lowerCamelCase_ = copy.deepcopy(UpperCamelCase ) if model_class == VideoMAEForPreTraining: # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch lowerCamelCase_ = torch.ones((self.model_tester.num_masks,) ) lowerCamelCase_ = torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0 ) )] ) lowerCamelCase_ = mask.expand(self.model_tester.batch_size , -1 ).bool() lowerCamelCase_ = bool_masked_pos.to(UpperCamelCase ) if return_labels: if model_class in [ *get_values(UpperCamelCase ), ]: lowerCamelCase_ = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCamelCase ) return inputs_dict def snake_case ( self ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason="VideoMAE does not use inputs_embeds" ) def snake_case ( self ): """simple docstring""" pass def snake_case ( self ): """simple docstring""" lowerCamelCase_ ,lowerCamelCase_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase_ = model_class(UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowerCamelCase_ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCamelCase , nn.Linear ) ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ ,lowerCamelCase_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase_ = model_class(UpperCamelCase ) lowerCamelCase_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase_ = [*signature.parameters.keys()] lowerCamelCase_ = ["pixel_values"] self.assertListEqual(arg_names[:1] , UpperCamelCase ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*UpperCamelCase ) @slow def snake_case ( self ): """simple docstring""" for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase_ = VideoMAEModel.from_pretrained(UpperCamelCase ) self.assertIsNotNone(UpperCamelCase ) def snake_case ( self ): """simple docstring""" if not self.has_attentions: pass else: lowerCamelCase_ ,lowerCamelCase_ = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase_ = True for model_class in self.all_model_classes: lowerCamelCase_ = self.model_tester.seq_length - self.model_tester.num_masks lowerCamelCase_ = ( num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length ) lowerCamelCase_ = True lowerCamelCase_ = False lowerCamelCase_ = True lowerCamelCase_ = model_class(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() with torch.no_grad(): lowerCamelCase_ = model(**self._prepare_for_class(UpperCamelCase , UpperCamelCase ) ) lowerCamelCase_ = outputs.attentions self.assertEqual(len(UpperCamelCase ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] lowerCamelCase_ = True lowerCamelCase_ = model_class(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() with torch.no_grad(): lowerCamelCase_ = model(**self._prepare_for_class(UpperCamelCase , UpperCamelCase ) ) lowerCamelCase_ = outputs.attentions self.assertEqual(len(UpperCamelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) lowerCamelCase_ = len(UpperCamelCase ) # Check attention is always last and order is fine lowerCamelCase_ = True lowerCamelCase_ = True lowerCamelCase_ = model_class(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() with torch.no_grad(): lowerCamelCase_ = model(**self._prepare_for_class(UpperCamelCase , UpperCamelCase ) ) self.assertEqual(out_len + 1 , len(UpperCamelCase ) ) lowerCamelCase_ = outputs.attentions self.assertEqual(len(UpperCamelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) def snake_case ( self ): """simple docstring""" def check_hidden_states_output(UpperCamelCase , UpperCamelCase , UpperCamelCase ): lowerCamelCase_ = model_class(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() with torch.no_grad(): lowerCamelCase_ = model(**self._prepare_for_class(UpperCamelCase , UpperCamelCase ) ) lowerCamelCase_ = outputs.hidden_states lowerCamelCase_ = self.model_tester.num_hidden_layers + 1 self.assertEqual(len(UpperCamelCase ) , UpperCamelCase ) lowerCamelCase_ = self.model_tester.seq_length - self.model_tester.num_masks lowerCamelCase_ = num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) lowerCamelCase_ ,lowerCamelCase_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase_ = True check_hidden_states_output(UpperCamelCase , UpperCamelCase , UpperCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowerCamelCase_ = True check_hidden_states_output(UpperCamelCase , UpperCamelCase , UpperCamelCase ) @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def snake_case ( self ): """simple docstring""" pass def __snake_case ( ): lowerCamelCase_ = hf_hub_download( repo_id="hf-internal-testing/spaghetti-video" , filename="eating_spaghetti.npy" , repo_type="dataset" ) lowerCamelCase_ = np.load(UpperCAmelCase_ ) return list(UpperCAmelCase_ ) @require_torch @require_vision class snake_case ( unittest.TestCase ): """simple docstring""" @cached_property def snake_case ( self ): """simple docstring""" # logits were tested with a different mean and std, so we use the same here return ( VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5] ) if is_vision_available() else None ) @slow def snake_case ( self ): """simple docstring""" lowerCamelCase_ = VideoMAEForVideoClassification.from_pretrained("MCG-NJU/videomae-base-finetuned-kinetics" ).to( UpperCamelCase ) lowerCamelCase_ = self.default_image_processor lowerCamelCase_ = prepare_video() lowerCamelCase_ = image_processor(UpperCamelCase , return_tensors="pt" ).to(UpperCamelCase ) # forward pass with torch.no_grad(): lowerCamelCase_ = model(**UpperCamelCase ) # verify the logits lowerCamelCase_ = torch.Size((1, 400) ) self.assertEqual(outputs.logits.shape , UpperCamelCase ) lowerCamelCase_ = torch.tensor([0.3_669, -0.0_688, -0.2_421] ).to(UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , UpperCamelCase , atol=1e-4 ) ) @slow def snake_case ( self ): """simple docstring""" lowerCamelCase_ = VideoMAEForPreTraining.from_pretrained("MCG-NJU/videomae-base-short" ).to(UpperCamelCase ) lowerCamelCase_ = self.default_image_processor lowerCamelCase_ = prepare_video() lowerCamelCase_ = image_processor(UpperCamelCase , return_tensors="pt" ).to(UpperCamelCase ) # add boolean mask, indicating which patches to mask lowerCamelCase_ = hf_hub_download(repo_id="hf-internal-testing/bool-masked-pos" , filename="bool_masked_pos.pt" ) lowerCamelCase_ = torch.load(UpperCamelCase ) # forward pass with torch.no_grad(): lowerCamelCase_ = model(**UpperCamelCase ) # verify the logits lowerCamelCase_ = torch.Size([1, 1408, 1536] ) lowerCamelCase_ = torch.tensor( [[0.7_994, 0.9_612, 0.8_508], [0.7_401, 0.8_958, 0.8_302], [0.5_862, 0.7_468, 0.7_325]] , device=UpperCamelCase ) self.assertEqual(outputs.logits.shape , UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , UpperCamelCase , atol=1e-4 ) ) # verify the loss (`config.norm_pix_loss` = `True`) lowerCamelCase_ = torch.tensor([0.5_142] , device=UpperCamelCase ) self.assertTrue(torch.allclose(outputs.loss , UpperCamelCase , atol=1e-4 ) ) # verify the loss (`config.norm_pix_loss` = `False`) lowerCamelCase_ = VideoMAEForPreTraining.from_pretrained("MCG-NJU/videomae-base-short" , norm_pix_loss=UpperCamelCase ).to( UpperCamelCase ) with torch.no_grad(): lowerCamelCase_ = model(**UpperCamelCase ) lowerCamelCase_ = torch.tensor(torch.tensor([0.6_469] ) , device=UpperCamelCase ) self.assertTrue(torch.allclose(outputs.loss , UpperCamelCase , atol=1e-4 ) )
55
from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class __lowerCamelCase : _lowercase = XGLMConfig _lowercase = {} _lowercase = """gelu""" def __init__( self: Optional[int],A_: Dict,A_: Any=14,A_: Optional[int]=7,A_: str=True,A_: Any=True,A_: Optional[int]=True,A_: Optional[int]=99,A_: List[str]=32,A_: Any=2,A_: Tuple=4,A_: List[str]=37,A_: Dict="gelu",A_: int=0.1,A_: List[str]=0.1,A_: int=512,A_: List[Any]=0.0_2,): '''simple docstring''' __UpperCamelCase = parent __UpperCamelCase = batch_size __UpperCamelCase = seq_length __UpperCamelCase = is_training __UpperCamelCase = use_input_mask __UpperCamelCase = use_labels __UpperCamelCase = vocab_size __UpperCamelCase = d_model __UpperCamelCase = num_hidden_layers __UpperCamelCase = num_attention_heads __UpperCamelCase = ffn_dim __UpperCamelCase = activation_function __UpperCamelCase = activation_dropout __UpperCamelCase = attention_dropout __UpperCamelCase = max_position_embeddings __UpperCamelCase = initializer_range __UpperCamelCase = None __UpperCamelCase = 0 __UpperCamelCase = 2 __UpperCamelCase = 1 def snake_case_ ( self: Dict ): '''simple docstring''' return XGLMConfig.from_pretrained('facebook/xglm-564M' ) def snake_case_ ( self: Optional[Any] ): '''simple docstring''' __UpperCamelCase = tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length],self.vocab_size ),clip_value_min=0,clip_value_max=3 ) __UpperCamelCase = None if self.use_input_mask: __UpperCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) __UpperCamelCase = self.get_config() __UpperCamelCase = floats_tensor([self.num_hidden_layers, self.num_attention_heads],2 ) return ( config, input_ids, input_mask, head_mask, ) def snake_case_ ( self: Union[str, Any] ): '''simple docstring''' return XGLMConfig( vocab_size=self.vocab_size,d_model=self.hidden_size,num_layers=self.num_hidden_layers,attention_heads=self.num_attention_heads,ffn_dim=self.ffn_dim,activation_function=self.activation_function,activation_dropout=self.activation_dropout,attention_dropout=self.attention_dropout,max_position_embeddings=self.max_position_embeddings,initializer_range=self.initializer_range,use_cache=A_,bos_token_id=self.bos_token_id,eos_token_id=self.eos_token_id,pad_token_id=self.pad_token_id,return_dict=A_,) def snake_case_ ( self: int ): '''simple docstring''' __UpperCamelCase = self.prepare_config_and_inputs() ( ( __UpperCamelCase ), ( __UpperCamelCase ), ( __UpperCamelCase ), ( __UpperCamelCase ), ) = config_and_inputs __UpperCamelCase = { 'input_ids': input_ids, 'head_mask': head_mask, } return config, inputs_dict @require_tf class __lowerCamelCase (_a , _a , unittest.TestCase ): _lowercase = (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () _lowercase = (TFXGLMForCausalLM,) if is_tf_available() else () _lowercase = ( {"""feature-extraction""": TFXGLMModel, """text-generation""": TFXGLMForCausalLM} if is_tf_available() else {} ) _lowercase = False _lowercase = False _lowercase = False def snake_case_ ( self: List[Any] ): '''simple docstring''' __UpperCamelCase = TFXGLMModelTester(self ) __UpperCamelCase = ConfigTester(self,config_class=A_,n_embd=37 ) def snake_case_ ( self: Any ): '''simple docstring''' self.config_tester.run_common_tests() @slow def snake_case_ ( self: Any ): '''simple docstring''' for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __UpperCamelCase = TFXGLMModel.from_pretrained(A_ ) self.assertIsNotNone(A_ ) @unittest.skip(reason='Currently, model embeddings are going to undergo a major refactor.' ) def snake_case_ ( self: Tuple ): '''simple docstring''' super().test_resize_token_embeddings() @require_tf class __lowerCamelCase (unittest.TestCase ): @slow def snake_case_ ( self: Optional[Any],A_: int=True ): '''simple docstring''' __UpperCamelCase = TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase = tf.convert_to_tensor([[2, 268, 9865]],dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off __UpperCamelCase = [2, 268, 9865, 67, 11, 1988, 5_7252, 9865, 5, 984, 67, 1988, 21_3838, 1658, 53, 7_0446, 33, 6657, 278, 1581] # fmt: on __UpperCamelCase = model.generate(A_,do_sample=A_,num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist(),A_ ) @slow def snake_case_ ( self: Optional[Any] ): '''simple docstring''' __UpperCamelCase = XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase = TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) tf.random.set_seed(0 ) __UpperCamelCase = tokenizer('Today is a nice day and',return_tensors='tf' ) __UpperCamelCase = tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(':/CPU:0' ): __UpperCamelCase = model.generate(A_,do_sample=A_,seed=[7, 0] ) __UpperCamelCase = tokenizer.decode(output_ids[0],skip_special_tokens=A_ ) __UpperCamelCase = ( 'Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due' ) self.assertEqual(A_,A_ ) @slow def snake_case_ ( self: Optional[int] ): '''simple docstring''' __UpperCamelCase = TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase = XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) __UpperCamelCase = 'left' # use different length sentences to test batching __UpperCamelCase = [ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When', 'Hello, my dog is a little', ] __UpperCamelCase = tokenizer(A_,return_tensors='tf',padding=A_ ) __UpperCamelCase = inputs['input_ids'] __UpperCamelCase = model.generate(input_ids=A_,attention_mask=inputs['attention_mask'],max_new_tokens=12 ) __UpperCamelCase = tokenizer(sentences[0],return_tensors='tf' ).input_ids __UpperCamelCase = model.generate(input_ids=A_,max_new_tokens=12 ) __UpperCamelCase = tokenizer(sentences[1],return_tensors='tf' ).input_ids __UpperCamelCase = model.generate(input_ids=A_,max_new_tokens=12 ) __UpperCamelCase = tokenizer.batch_decode(A_,skip_special_tokens=A_ ) __UpperCamelCase = tokenizer.decode(output_non_padded[0],skip_special_tokens=A_ ) __UpperCamelCase = tokenizer.decode(output_padded[0],skip_special_tokens=A_ ) __UpperCamelCase = [ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When left padding is applied, the sequence will be ' 'a single', 'Hello, my dog is a little bit of a shy one, but he is very friendly', ] self.assertListEqual(A_,A_ ) self.assertListEqual(A_,[non_padded_sentence, padded_sentence] )
310
0
from typing import TYPE_CHECKING from ...utils import _LazyModule lowerCAmelCase : List[str] = {"""processing_wav2vec2_with_lm""": ["""Wav2Vec2ProcessorWithLM"""]} if TYPE_CHECKING: from .processing_wavaveca_with_lm import WavaVecaProcessorWithLM else: import sys lowerCAmelCase : Union[str, Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
366
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowerCAmelCase : Optional[int] = {"""configuration_yolos""": ["""YOLOS_PRETRAINED_CONFIG_ARCHIVE_MAP""", """YolosConfig""", """YolosOnnxConfig"""]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Any = ["""YolosFeatureExtractor"""] lowerCAmelCase : Tuple = ["""YolosImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Any = [ """YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST""", """YolosForObjectDetection""", """YolosModel""", """YolosPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_yolos import YOLOS_PRETRAINED_CONFIG_ARCHIVE_MAP, YolosConfig, YolosOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_yolos import YolosFeatureExtractor from .image_processing_yolos import YolosImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_yolos import ( YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST, YolosForObjectDetection, YolosModel, YolosPreTrainedModel, ) else: import sys lowerCAmelCase : int = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
127
0
'''simple docstring''' from math import factorial A__ : dict[str, int] = {str(digit): factorial(digit) for digit in range(10)} def UpperCAmelCase__ ( UpperCAmelCase_ : int ) -> int: if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): raise TypeError('Parameter number must be int' ) if number < 0: raise ValueError('Parameter number must be greater than or equal to 0' ) # Converts number in string to iterate on its digits and adds its factorial. return sum(DIGIT_FACTORIAL[digit] for digit in str(UpperCAmelCase_ ) ) def UpperCAmelCase__ ( UpperCAmelCase_ : int = 60 , UpperCAmelCase_ : int = 1_00_00_00 ) -> int: if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) or not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): raise TypeError('Parameters chain_length and number_limit must be int' ) if chain_length <= 0 or number_limit <= 0: raise ValueError( 'Parameters chain_length and number_limit must be greater than 0' ) # the counter for the chains with the exact desired length __lowerCamelCase : Tuple = 0 # the cached sizes of the previous chains __lowerCamelCase : dict[int, int] = {} for start_chain_element in range(1 , UpperCAmelCase_ ): # The temporary set will contain the elements of the chain __lowerCamelCase : List[str] = set() __lowerCamelCase : int = 0 # Stop computing the chain when you find a cached size, a repeating item or the # length is greater then the desired one. __lowerCamelCase : str = start_chain_element while ( chain_element not in chain_sets_lengths and chain_element not in chain_set and chain_set_length <= chain_length ): chain_set.add(UpperCAmelCase_ ) chain_set_length += 1 __lowerCamelCase : List[str] = digit_factorial_sum(UpperCAmelCase_ ) if chain_element in chain_sets_lengths: chain_set_length += chain_sets_lengths[chain_element] __lowerCamelCase : Optional[Any] = chain_set_length # If chain contains the exact amount of elements increase the counter if chain_set_length == chain_length: chains_counter += 1 return chains_counter if __name__ == "__main__": import doctest doctest.testmod() print(f'''{solution()}''')
185
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_xlnet import XLNetTokenizer else: A__ : Dict = None A__ : List[Any] = logging.get_logger(__name__) A__ : str = {"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""} A__ : Union[str, Any] = { """vocab_file""": { """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model""", """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model""", }, """tokenizer_file""": { """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json""", """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json""", }, } A__ : Any = { """xlnet-base-cased""": None, """xlnet-large-cased""": None, } A__ : Dict = """▁""" # Segments (not really needed) A__ : List[str] = 0 A__ : List[Any] = 1 A__ : Union[str, Any] = 2 A__ : List[Any] = 3 A__ : str = 4 class UpperCAmelCase_ (_UpperCAmelCase ): """simple docstring""" lowerCamelCase : str = VOCAB_FILES_NAMES lowerCamelCase : Dict = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : List[Any] = 'left' lowerCamelCase : Optional[Any] = XLNetTokenizer def __init__( self , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_="<s>" , SCREAMING_SNAKE_CASE_="</s>" , SCREAMING_SNAKE_CASE_="<unk>" , SCREAMING_SNAKE_CASE_="<sep>" , SCREAMING_SNAKE_CASE_="<pad>" , SCREAMING_SNAKE_CASE_="<cls>" , SCREAMING_SNAKE_CASE_="<mask>" , SCREAMING_SNAKE_CASE_=["<eop>", "<eod>"] , **SCREAMING_SNAKE_CASE_ , ) -> Union[str, Any]: # Mask token behave like a normal word, i.e. include the space before it __lowerCamelCase : str = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else mask_token super().__init__( vocab_file=SCREAMING_SNAKE_CASE_ , tokenizer_file=SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ , remove_space=SCREAMING_SNAKE_CASE_ , keep_accents=SCREAMING_SNAKE_CASE_ , bos_token=SCREAMING_SNAKE_CASE_ , eos_token=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_ , additional_special_tokens=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) __lowerCamelCase : Union[str, Any] = 3 __lowerCamelCase : int = do_lower_case __lowerCamelCase : Optional[Any] = remove_space __lowerCamelCase : int = keep_accents __lowerCamelCase : Any = vocab_file __lowerCamelCase : Any = False if not self.vocab_file else True def lowercase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None ) -> List[int]: __lowerCamelCase : Optional[Any] = [self.sep_token_id] __lowerCamelCase : Any = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def lowercase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None ) -> List[int]: __lowerCamelCase : int = [self.sep_token_id] __lowerCamelCase : Dict = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def lowercase_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None ) -> Tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(SCREAMING_SNAKE_CASE_ ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return __lowerCamelCase : Tuple = os.path.join( SCREAMING_SNAKE_CASE_ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(SCREAMING_SNAKE_CASE_ ): copyfile(self.vocab_file , SCREAMING_SNAKE_CASE_ ) return (out_vocab_file,)
185
1
"""simple docstring""" _lowercase : List[Any] = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] _lowercase : str = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] _lowercase : Optional[Any] = { 0: "Sunday", 1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", 5: "Friday", 6: "Saturday", } def snake_case__ ( __lowerCamelCase : int , __lowerCamelCase : int , __lowerCamelCase : int ): """simple docstring""" assert len(str(__lowerCamelCase ) ) > 2, "year should be in YYYY format" assert 1 <= month <= 12, "month should be between 1 to 12" assert 1 <= day <= 31, "day should be between 1 to 31" # Doomsday algorithm: lowerCamelCase__ : List[str] =year // 100 lowerCamelCase__ : Optional[int] =(5 * (century % 4) + 2) % 7 lowerCamelCase__ : List[str] =year % 100 lowerCamelCase__ : List[Any] =centurian % 12 lowerCamelCase__ : int =( (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor ) % 7 lowerCamelCase__ : Any =( DOOMSDAY_NOT_LEAP[month - 1] if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) else DOOMSDAY_LEAP[month - 1] ) lowerCamelCase__ : int =(dooms_day + day - day_anchor) % 7 return WEEK_DAY_NAMES[week_day] if __name__ == "__main__": import doctest doctest.testmod()
272
"""simple docstring""" import copy from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings _lowercase : Tuple = r"\n [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and\n can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information.\n\n Args:\n title_sep (`str`, *optional*, defaults to `\" / \"`):\n Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`].\n doc_sep (`str`, *optional*, defaults to `\" // \"`):\n Separator inserted between the text of the retrieved document and the original input when calling\n [`RagRetriever`].\n n_docs (`int`, *optional*, defaults to 5):\n Number of documents to retrieve.\n max_combined_length (`int`, *optional*, defaults to 300):\n Max length of contextualized input returned by [`~RagRetriever.__call__`].\n retrieval_vector_size (`int`, *optional*, defaults to 768):\n Dimensionality of the document embeddings indexed by [`RagRetriever`].\n retrieval_batch_size (`int`, *optional*, defaults to 8):\n Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated\n [`RagRetriever`].\n dataset (`str`, *optional*, defaults to `\"wiki_dpr\"`):\n A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids\n using `datasets.list_datasets()`).\n dataset_split (`str`, *optional*, defaults to `\"train\"`)\n Which split of the `dataset` to load.\n index_name (`str`, *optional*, defaults to `\"compressed\"`)\n The index name of the index associated with the `dataset`. One can choose between `\"legacy\"`, `\"exact\"` and\n `\"compressed\"`.\n index_path (`str`, *optional*)\n The path to the serialized faiss index on disk.\n passages_path (`str`, *optional*):\n A path to text passages compatible with the faiss index. Required if using\n [`~models.rag.retrieval_rag.LegacyIndex`]\n use_dummy_dataset (`bool`, *optional*, defaults to `False`)\n Whether to load a \"dummy\" variant of the dataset specified by `dataset`.\n label_smoothing (`float`, *optional*, defaults to 0.0):\n Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing\n in the loss calculation. If set to 0, no label smoothing is performed.\n do_marginalize (`bool`, *optional*, defaults to `False`):\n If `True`, the logits are marginalized over all documents by making use of\n `torch.nn.functional.log_softmax`.\n reduce_loss (`bool`, *optional*, defaults to `False`):\n Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation.\n do_deduplication (`bool`, *optional*, defaults to `True`):\n Whether or not to deduplicate the generations from different context documents for a given input. Has to be\n set to `False` if used while training with distributed backend.\n exclude_bos_score (`bool`, *optional*, defaults to `False`):\n Whether or not to disregard the BOS token when computing the loss.\n output_retrieved(`bool`, *optional*, defaults to `False`):\n If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and\n `context_attention_mask` are returned. See returned tensors for more detail.\n use_cache (`bool`, *optional*, defaults to `True`):\n Whether or not the model should return the last key/values attentions (not used by all models).\n forced_eos_token_id (`int`, *optional*):\n The id of the token to force as the last generated token when `max_length` is reached. Usually set to\n `eos_token_id`.\n" @add_start_docstrings(lowerCAmelCase_ ) class __SCREAMING_SNAKE_CASE ( lowerCAmelCase_ ): '''simple docstring''' _a = 'rag' _a = True def __init__( self : Optional[int], lowerCamelCase : Union[str, Any]=None, lowerCamelCase : Union[str, Any]=True, lowerCamelCase : Dict=None, lowerCamelCase : Optional[Any]=None, lowerCamelCase : Any=None, lowerCamelCase : List[Any]=None, lowerCamelCase : Optional[Any]=None, lowerCamelCase : Any=" / ", lowerCamelCase : Union[str, Any]=" // ", lowerCamelCase : List[Any]=5, lowerCamelCase : int=300, lowerCamelCase : Optional[Any]=768, lowerCamelCase : Optional[Any]=8, lowerCamelCase : Tuple="wiki_dpr", lowerCamelCase : Tuple="train", lowerCamelCase : Union[str, Any]="compressed", lowerCamelCase : List[str]=None, lowerCamelCase : Any=None, lowerCamelCase : Union[str, Any]=False, lowerCamelCase : Dict=False, lowerCamelCase : Optional[int]=0.0, lowerCamelCase : Any=True, lowerCamelCase : Dict=False, lowerCamelCase : Tuple=False, lowerCamelCase : List[str]=False, lowerCamelCase : List[Any]=True, lowerCamelCase : Optional[Any]=None, **lowerCamelCase : str, )-> List[Any]: super().__init__( bos_token_id=lowerCamelCase, pad_token_id=lowerCamelCase, eos_token_id=lowerCamelCase, decoder_start_token_id=lowerCamelCase, forced_eos_token_id=lowerCamelCase, is_encoder_decoder=lowerCamelCase, prefix=lowerCamelCase, vocab_size=lowerCamelCase, **lowerCamelCase, ) assert ( "question_encoder" in kwargs and "generator" in kwargs ), "Config has to be initialized with question_encoder and generator config" lowerCamelCase__ : Tuple =kwargs.pop('''question_encoder''' ) lowerCamelCase__ : int =question_encoder_config.pop('''model_type''' ) lowerCamelCase__ : Dict =kwargs.pop('''generator''' ) lowerCamelCase__ : Tuple =decoder_config.pop('''model_type''' ) from ..auto.configuration_auto import AutoConfig lowerCamelCase__ : Tuple =AutoConfig.for_model(lowerCamelCase, **lowerCamelCase ) lowerCamelCase__ : Dict =AutoConfig.for_model(lowerCamelCase, **lowerCamelCase ) lowerCamelCase__ : Any =reduce_loss lowerCamelCase__ : Tuple =label_smoothing lowerCamelCase__ : List[str] =exclude_bos_score lowerCamelCase__ : Dict =do_marginalize lowerCamelCase__ : Union[str, Any] =title_sep lowerCamelCase__ : Dict =doc_sep lowerCamelCase__ : List[Any] =n_docs lowerCamelCase__ : List[str] =max_combined_length lowerCamelCase__ : List[Any] =dataset lowerCamelCase__ : int =dataset_split lowerCamelCase__ : List[Any] =index_name lowerCamelCase__ : int =retrieval_vector_size lowerCamelCase__ : Dict =retrieval_batch_size lowerCamelCase__ : str =passages_path lowerCamelCase__ : Any =index_path lowerCamelCase__ : List[Any] =use_dummy_dataset lowerCamelCase__ : Optional[int] =output_retrieved lowerCamelCase__ : List[str] =do_deduplication lowerCamelCase__ : Tuple =use_cache if self.forced_eos_token_id is None: lowerCamelCase__ : int =getattr(self.generator, '''forced_eos_token_id''', lowerCamelCase ) @classmethod def snake_case ( cls : List[str], lowerCamelCase : PretrainedConfig, lowerCamelCase : PretrainedConfig, **lowerCamelCase : int )-> PretrainedConfig: return cls(question_encoder=question_encoder_config.to_dict(), generator=generator_config.to_dict(), **lowerCamelCase ) def snake_case ( self : Optional[int] )-> int: lowerCamelCase__ : Union[str, Any] =copy.deepcopy(self.__dict__ ) lowerCamelCase__ : Optional[int] =self.question_encoder.to_dict() lowerCamelCase__ : Tuple =self.generator.to_dict() lowerCamelCase__ : Optional[Any] =self.__class__.model_type return output
272
1
'''simple docstring''' from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder UpperCAmelCase_ : str = datasets.utils.logging.get_logger(__name__) class lowercase__ ( folder_based_builder.FolderBasedBuilderConfig ): '''simple docstring''' A_ : Dict = None A_ : Dict = None class lowercase__ ( folder_based_builder.FolderBasedBuilder ): '''simple docstring''' A_ : Union[str, Any] = datasets.Audio() A_ : Dict = """audio""" A_ : Any = AudioFolderConfig A_ : List[str] = 42 # definition at the bottom of the script A_ : List[Any] = AudioClassification(audio_column="""audio""" , label_column="""label""" ) UpperCAmelCase_ : List[str] = [ '.aiff', '.au', '.avr', '.caf', '.flac', '.htk', '.svx', '.mat4', '.mat5', '.mpc2k', '.ogg', '.paf', '.pvf', '.raw', '.rf64', '.sd2', '.sds', '.ircam', '.voc', '.w64', '.wav', '.nist', '.wavex', '.wve', '.xi', '.mp3', '.opus', ] UpperCAmelCase_ : int = AUDIO_EXTENSIONS
200
import torch from transformers import CamembertForMaskedLM, CamembertTokenizer def A ( _UpperCAmelCase : str , _UpperCAmelCase : Any , _UpperCAmelCase : List[str] , _UpperCAmelCase : Optional[int]=5 ) -> List[Any]: '''simple docstring''' # Adapted from https://github.com/pytorch/fairseq/blob/master/fairseq/models/roberta/hub_interface.py assert masked_input.count('<mask>' ) == 1 _UpperCAmelCase = torch.tensor(tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) ).unsqueeze(0 ) # Batch size 1 _UpperCAmelCase = model(_UpperCAmelCase )[0] # The last hidden-state is the first element of the output tuple _UpperCAmelCase = (input_ids.squeeze() == tokenizer.mask_token_id).nonzero().item() _UpperCAmelCase = logits[0, masked_index, :] _UpperCAmelCase = logits.softmax(dim=0 ) _UpperCAmelCase , _UpperCAmelCase = prob.topk(k=_UpperCAmelCase , dim=0 ) _UpperCAmelCase = ' '.join( [tokenizer.convert_ids_to_tokens(indices[i].item() ) for i in range(len(_UpperCAmelCase ) )] ) _UpperCAmelCase = tokenizer.mask_token _UpperCAmelCase = [] for index, predicted_token_bpe in enumerate(topk_predicted_token_bpe.split(' ' ) ): _UpperCAmelCase = predicted_token_bpe.replace('\u2581' , ' ' ) if " {0}".format(_UpperCAmelCase ) in masked_input: topk_filled_outputs.append( ( masked_input.replace(' {0}'.format(_UpperCAmelCase ) , _UpperCAmelCase ), values[index].item(), predicted_token, ) ) else: topk_filled_outputs.append( ( masked_input.replace(_UpperCAmelCase , _UpperCAmelCase ), values[index].item(), predicted_token, ) ) return topk_filled_outputs UpperCAmelCase__ = CamembertTokenizer.from_pretrained("camembert-base") UpperCAmelCase__ = CamembertForMaskedLM.from_pretrained("camembert-base") model.eval() UpperCAmelCase__ = "Le camembert est <mask> :)" print(fill_mask(masked_input, model, tokenizer, topk=3))
339
0
'''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 _snake_case (unittest.TestCase): __A : int =MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING __A : Optional[int] =TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def UpperCamelCase__ ( self ,_snake_case ,_snake_case ,_snake_case ): UpperCAmelCase_ : List[str] = TextaTextGenerationPipeline(model=_snake_case ,tokenizer=_snake_case ) return generator, ["Something to write", "Something else"] def UpperCamelCase__ ( self ,_snake_case ,_snake_case ): UpperCAmelCase_ : Dict = generator("Something there" ) self.assertEqual(_snake_case ,[{"generated_text": ANY(_snake_case )}] ) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["generated_text"].startswith("Something there" ) ) UpperCAmelCase_ : Any = generator(["This is great !", "Something else"] ,num_return_sequences=2 ,do_sample=_snake_case ) self.assertEqual( _snake_case ,[ [{"generated_text": ANY(_snake_case )}, {"generated_text": ANY(_snake_case )}], [{"generated_text": ANY(_snake_case )}, {"generated_text": ANY(_snake_case )}], ] ,) UpperCAmelCase_ : Union[str, Any] = generator( ["This is great !", "Something else"] ,num_return_sequences=2 ,batch_size=2 ,do_sample=_snake_case ) self.assertEqual( _snake_case ,[ [{"generated_text": ANY(_snake_case )}, {"generated_text": ANY(_snake_case )}], [{"generated_text": ANY(_snake_case )}, {"generated_text": ANY(_snake_case )}], ] ,) with self.assertRaises(_snake_case ): generator(4 ) @require_torch def UpperCamelCase__ ( self ): UpperCAmelCase_ : Union[str, Any] = pipeline("text2text-generation" ,model="patrickvonplaten/t5-tiny-random" ,framework="pt" ) # do_sample=False necessary for reproducibility UpperCAmelCase_ : List[Any] = generator("Something there" ,do_sample=_snake_case ) self.assertEqual(_snake_case ,[{"generated_text": ""}] ) UpperCAmelCase_ : Union[str, Any] = 3 UpperCAmelCase_ : List[Any] = generator( "Something there" ,num_return_sequences=_snake_case ,num_beams=_snake_case ,) UpperCAmelCase_ : int = [ {"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(_snake_case ,_snake_case ) UpperCAmelCase_ : Dict = generator("This is a test" ,do_sample=_snake_case ,num_return_sequences=2 ,return_tensors=_snake_case ) self.assertEqual( _snake_case ,[ {"generated_token_ids": ANY(torch.Tensor )}, {"generated_token_ids": ANY(torch.Tensor )}, ] ,) UpperCAmelCase_ : Any = generator.model.config.eos_token_id UpperCAmelCase_ : str = "<pad>" UpperCAmelCase_ : Optional[int] = generator( ["This is a test", "This is a second test"] ,do_sample=_snake_case ,num_return_sequences=2 ,batch_size=2 ,return_tensors=_snake_case ,) self.assertEqual( _snake_case ,[ [ {"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 UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = pipeline("text2text-generation" ,model="patrickvonplaten/t5-tiny-random" ,framework="tf" ) # do_sample=False necessary for reproducibility UpperCAmelCase_ : Optional[Any] = generator("Something there" ,do_sample=_snake_case ) self.assertEqual(_snake_case ,[{"generated_text": ""}] )
370
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) _lowerCamelCase = { """configuration_mobilevit""": ["""MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MobileViTConfig""", """MobileViTOnnxConfig"""], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase = ["""MobileViTFeatureExtractor"""] _lowerCamelCase = ["""MobileViTImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase = [ """MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """MobileViTForImageClassification""", """MobileViTForSemanticSegmentation""", """MobileViTModel""", """MobileViTPreTrainedModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase = [ """TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFMobileViTForImageClassification""", """TFMobileViTForSemanticSegmentation""", """TFMobileViTModel""", """TFMobileViTPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_mobilevit import MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileViTConfig, MobileViTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_mobilevit import MobileViTFeatureExtractor from .image_processing_mobilevit import MobileViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilevit import ( MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTModel, MobileViTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mobilevit import ( TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation, TFMobileViTModel, TFMobileViTPreTrainedModel, ) else: import sys _lowerCamelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
67
0
import os from bleurt import score # From: git+https://github.com/google-research/bleurt.git import datasets snake_case_ : Optional[int] = datasets.logging.get_logger(__name__) snake_case_ : Tuple = "\\n@inproceedings{bleurt,\n title={BLEURT: Learning Robust Metrics for Text Generation},\n author={Thibault Sellam and Dipanjan Das and Ankur P. Parikh},\n booktitle={ACL},\n year={2020},\n url={https://arxiv.org/abs/2004.04696}\n}\n" snake_case_ : int = "\\nBLEURT a learnt evaluation metric for Natural Language Generation. It is built using multiple phases of transfer learning starting from a pretrained BERT model (Devlin et al. 2018)\nand then employing another pre-training phrase using synthetic data. Finally it is trained on WMT human annotations. You may run BLEURT out-of-the-box or fine-tune\nit for your specific application (the latter is expected to perform better).\n\nSee the project's README at https://github.com/google-research/bleurt#readme for more information.\n" snake_case_ : str = "\nBLEURT score.\n\nArgs:\n `predictions` (list of str): prediction/candidate sentences\n `references` (list of str): reference sentences\n `checkpoint` BLEURT checkpoint. Will default to BLEURT-tiny if None.\n\nReturns:\n 'scores': List of scores.\nExamples:\n\n >>> predictions = [\"hello there\", \"general kenobi\"]\n >>> references = [\"hello there\", \"general kenobi\"]\n >>> bleurt = datasets.load_metric(\"bleurt\")\n >>> results = bleurt.compute(predictions=predictions, references=references)\n >>> print([round(v, 2) for v in results[\"scores\"]])\n [1.03, 1.04]\n" snake_case_ : Tuple = { "bleurt-tiny-128": "https://storage.googleapis.com/bleurt-oss/bleurt-tiny-128.zip", "bleurt-tiny-512": "https://storage.googleapis.com/bleurt-oss/bleurt-tiny-512.zip", "bleurt-base-128": "https://storage.googleapis.com/bleurt-oss/bleurt-base-128.zip", "bleurt-base-512": "https://storage.googleapis.com/bleurt-oss/bleurt-base-512.zip", "bleurt-large-128": "https://storage.googleapis.com/bleurt-oss/bleurt-large-128.zip", "bleurt-large-512": "https://storage.googleapis.com/bleurt-oss/bleurt-large-512.zip", "BLEURT-20-D3": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D3.zip", "BLEURT-20-D6": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D6.zip", "BLEURT-20-D12": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D12.zip", "BLEURT-20": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20.zip", } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __snake_case ( datasets.Metric ): def lowerCamelCase ( self : str): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='''https://github.com/google-research/bleurt''' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence'''), '''references''': datasets.Value('''string''' , id='''sequence'''), }) , codebase_urls=['''https://github.com/google-research/bleurt'''] , reference_urls=['''https://github.com/google-research/bleurt''', '''https://arxiv.org/abs/2004.04696'''] , ) def lowerCamelCase ( self : Optional[Any] , _snake_case : Dict): """simple docstring""" if self.config_name == "default": logger.warning( '''Using default BLEURT-Base checkpoint for sequence maximum length 128. ''' '''You can use a bigger model for better results with e.g.: datasets.load_metric(\'bleurt\', \'bleurt-large-512\').''') UpperCAmelCase_ = '''bleurt-base-128''' if self.config_name.lower() in CHECKPOINT_URLS: UpperCAmelCase_ = self.config_name.lower() elif self.config_name.upper() in CHECKPOINT_URLS: UpperCAmelCase_ = self.config_name.upper() else: raise KeyError( F"""{self.config_name} model not found. You should supply the name of a model checkpoint for bleurt in {CHECKPOINT_URLS.keys()}""") # download the model checkpoint specified by self.config_name and set up the scorer UpperCAmelCase_ = dl_manager.download_and_extract(CHECKPOINT_URLS[checkpoint_name]) UpperCAmelCase_ = score.BleurtScorer(os.path.join(_snake_case , _snake_case)) def lowerCamelCase ( self : Any , _snake_case : int , _snake_case : List[Any]): """simple docstring""" UpperCAmelCase_ = self.scorer.score(references=_snake_case , candidates=_snake_case) return {"scores": scores}
51
import inspect import unittest import numpy as np from tests.test_modeling_common import floats_tensor from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel if is_vision_available(): from transformers import MaskFormerImageProcessor if is_vision_available(): from PIL import Image class __snake_case : def __init__( self : List[str] , _snake_case : Union[str, Any] , _snake_case : List[str]=2 , _snake_case : Any=True , _snake_case : Any=False , _snake_case : List[str]=10 , _snake_case : Any=3 , _snake_case : Union[str, Any]=32 * 4 , _snake_case : List[Any]=32 * 6 , _snake_case : Tuple=4 , _snake_case : Dict=32 , ): """simple docstring""" UpperCAmelCase_ = parent UpperCAmelCase_ = batch_size UpperCAmelCase_ = is_training UpperCAmelCase_ = use_auxiliary_loss UpperCAmelCase_ = num_queries UpperCAmelCase_ = num_channels UpperCAmelCase_ = min_size UpperCAmelCase_ = max_size UpperCAmelCase_ = num_labels UpperCAmelCase_ = mask_feature_size def lowerCamelCase ( self : Any): """simple docstring""" UpperCAmelCase_ = floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]).to( _snake_case) UpperCAmelCase_ = torch.ones([self.batch_size, self.min_size, self.max_size] , device=_snake_case) UpperCAmelCase_ = ( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] , device=_snake_case) > 0.5 ).float() UpperCAmelCase_ = (torch.rand((self.batch_size, self.num_labels) , device=_snake_case) > 0.5).long() UpperCAmelCase_ = self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def lowerCamelCase ( self : Any): """simple docstring""" return MaskFormerConfig.from_backbone_and_decoder_configs( backbone_config=SwinConfig( depths=[1, 1, 1, 1] , ) , decoder_config=DetrConfig( decoder_ffn_dim=128 , num_queries=self.num_queries , decoder_attention_heads=2 , d_model=self.mask_feature_size , ) , mask_feature_size=self.mask_feature_size , fpn_feature_size=self.mask_feature_size , num_channels=self.num_channels , num_labels=self.num_labels , ) def lowerCamelCase ( self : int): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ = self.prepare_config_and_inputs() UpperCAmelCase_ = {'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask} return config, inputs_dict def lowerCamelCase ( self : str , _snake_case : List[Any] , _snake_case : List[str]): """simple docstring""" UpperCAmelCase_ = output.encoder_hidden_states UpperCAmelCase_ = output.pixel_decoder_hidden_states UpperCAmelCase_ = output.transformer_decoder_hidden_states self.parent.assertTrue(len(_snake_case) , len(config.backbone_config.depths)) self.parent.assertTrue(len(_snake_case) , len(config.backbone_config.depths)) self.parent.assertTrue(len(_snake_case) , config.decoder_config.decoder_layers) def lowerCamelCase ( self : Union[str, Any] , _snake_case : List[str] , _snake_case : int , _snake_case : Optional[Any] , _snake_case : str=False): """simple docstring""" with torch.no_grad(): UpperCAmelCase_ = MaskFormerModel(config=_snake_case) model.to(_snake_case) model.eval() UpperCAmelCase_ = model(pixel_values=_snake_case , pixel_mask=_snake_case) UpperCAmelCase_ = model(_snake_case , output_hidden_states=_snake_case) # the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the # encoder and pixel decoder self.parent.assertEqual( output.transformer_decoder_last_hidden_state.shape , (self.batch_size, self.num_queries, self.mask_feature_size) , ) # let's ensure the other two hidden state exists self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None) self.parent.assertTrue(output.encoder_last_hidden_state is not None) if output_hidden_states: self.check_output_hidden_state(_snake_case , _snake_case) def lowerCamelCase ( self : List[Any] , _snake_case : List[Any] , _snake_case : List[Any] , _snake_case : str , _snake_case : Optional[int] , _snake_case : Union[str, Any]): """simple docstring""" UpperCAmelCase_ = MaskFormerForInstanceSegmentation(config=_snake_case) model.to(_snake_case) model.eval() def comm_check_on_output(_snake_case : Tuple): # let's still check that all the required stuff is there self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None) self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None) self.parent.assertTrue(result.encoder_last_hidden_state is not None) # okay, now we need to check the logits shape # due to the encoder compression, masks have a //4 spatial size self.parent.assertEqual( result.masks_queries_logits.shape , (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4) , ) # + 1 for null class self.parent.assertEqual( result.class_queries_logits.shape , (self.batch_size, self.num_queries, self.num_labels + 1)) with torch.no_grad(): UpperCAmelCase_ = model(pixel_values=_snake_case , pixel_mask=_snake_case) UpperCAmelCase_ = model(_snake_case) comm_check_on_output(_snake_case) UpperCAmelCase_ = model( pixel_values=_snake_case , pixel_mask=_snake_case , mask_labels=_snake_case , class_labels=_snake_case) comm_check_on_output(_snake_case) self.parent.assertTrue(result.loss is not None) self.parent.assertEqual(result.loss.shape , torch.Size([1])) @require_torch class __snake_case ( a , a , unittest.TestCase ): UpperCAmelCase__ : Union[str, Any] = (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else () UpperCAmelCase__ : Optional[Any] = ( {'''feature-extraction''': MaskFormerModel, '''image-segmentation''': MaskFormerForInstanceSegmentation} if is_torch_available() else {} ) UpperCAmelCase__ : Dict = False UpperCAmelCase__ : List[str] = False UpperCAmelCase__ : Optional[Any] = False UpperCAmelCase__ : Union[str, Any] = False def lowerCamelCase ( self : List[Any]): """simple docstring""" UpperCAmelCase_ = MaskFormerModelTester(self) UpperCAmelCase_ = ConfigTester(self , config_class=_snake_case , has_text_modality=_snake_case) def lowerCamelCase ( self : List[Any]): """simple docstring""" self.config_tester.run_common_tests() def lowerCamelCase ( self : str): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskformer_model(_snake_case , **_snake_case , output_hidden_states=_snake_case) def lowerCamelCase ( self : str): """simple docstring""" UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*_snake_case) @unittest.skip(reason='''MaskFormer does not use inputs_embeds''') def lowerCamelCase ( self : Dict): """simple docstring""" pass @unittest.skip(reason='''MaskFormer does not have a get_input_embeddings method''') def lowerCamelCase ( self : int): """simple docstring""" pass @unittest.skip(reason='''MaskFormer is not a generative model''') def lowerCamelCase ( self : str): """simple docstring""" pass @unittest.skip(reason='''MaskFormer does not use token embeddings''') def lowerCamelCase ( self : int): """simple docstring""" pass @require_torch_multi_gpu @unittest.skip( reason='''MaskFormer has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`''') def lowerCamelCase ( self : Any): """simple docstring""" pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''') def lowerCamelCase ( self : str): """simple docstring""" pass def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase_ = model_class(_snake_case) UpperCAmelCase_ = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase_ = [*signature.parameters.keys()] UpperCAmelCase_ = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _snake_case) @slow def lowerCamelCase ( self : Union[str, Any]): """simple docstring""" for model_name in ["facebook/maskformer-swin-small-coco"]: UpperCAmelCase_ = MaskFormerModel.from_pretrained(_snake_case) self.assertIsNotNone(_snake_case) def lowerCamelCase ( self : str): """simple docstring""" UpperCAmelCase_ = (self.model_tester.min_size,) * 2 UpperCAmelCase_ = { '''pixel_values''': torch.randn((2, 3, *size) , device=_snake_case), '''mask_labels''': torch.randn((2, 10, *size) , device=_snake_case), '''class_labels''': torch.zeros(2 , 10 , device=_snake_case).long(), } UpperCAmelCase_ = MaskFormerForInstanceSegmentation(MaskFormerConfig()).to(_snake_case) UpperCAmelCase_ = model(**_snake_case) self.assertTrue(outputs.loss is not None) def lowerCamelCase ( self : Optional[Any]): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskformer_model(_snake_case , **_snake_case , output_hidden_states=_snake_case) def lowerCamelCase ( self : str): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase_ = model_class(_snake_case).to(_snake_case) UpperCAmelCase_ = model(**_snake_case , output_attentions=_snake_case) self.assertTrue(outputs.attentions is not None) def lowerCamelCase ( self : int): """simple docstring""" if not self.model_tester.is_training: return # only MaskFormerForInstanceSegmentation has the loss UpperCAmelCase_ = self.all_model_classes[1] UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs() UpperCAmelCase_ = model_class(_snake_case) model.to(_snake_case) model.train() UpperCAmelCase_ = model(_snake_case , mask_labels=_snake_case , class_labels=_snake_case).loss loss.backward() def lowerCamelCase ( self : Optional[int]): """simple docstring""" UpperCAmelCase_ = self.all_model_classes[1] UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs() UpperCAmelCase_ = True UpperCAmelCase_ = True UpperCAmelCase_ = model_class(_snake_case) model.to(_snake_case) model.train() UpperCAmelCase_ = model(_snake_case , mask_labels=_snake_case , class_labels=_snake_case) UpperCAmelCase_ = outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() UpperCAmelCase_ = outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() # we requires_grad=True in inputs_embeds (line 2152), the original implementation don't UpperCAmelCase_ = outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() UpperCAmelCase_ = outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=_snake_case) self.assertIsNotNone(encoder_hidden_states.grad) self.assertIsNotNone(pixel_decoder_hidden_states.grad) self.assertIsNotNone(transformer_decoder_hidden_states.grad) self.assertIsNotNone(attentions.grad) snake_case_ : Dict = 1e-4 def A () -> Union[str, Any]: """simple docstring""" UpperCAmelCase_ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_vision @slow class __snake_case ( unittest.TestCase ): @cached_property def lowerCamelCase ( self : List[str]): """simple docstring""" return ( MaskFormerImageProcessor.from_pretrained('''facebook/maskformer-swin-small-coco''') if is_vision_available() else None ) def lowerCamelCase ( self : List[Any]): """simple docstring""" UpperCAmelCase_ = MaskFormerModel.from_pretrained('''facebook/maskformer-swin-small-coco''').to(_snake_case) UpperCAmelCase_ = self.default_image_processor UpperCAmelCase_ = prepare_img() UpperCAmelCase_ = image_processor(_snake_case , return_tensors='''pt''').to(_snake_case) UpperCAmelCase_ = inputs['''pixel_values'''].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0) # check size self.assertEqual(_snake_case , (1, 3, 800, 1088)) with torch.no_grad(): UpperCAmelCase_ = model(**_snake_case) UpperCAmelCase_ = torch.tensor( [[-0.0_4_8_2, 0.9_2_2_8, 0.4_9_5_1], [-0.2_5_4_7, 0.8_0_1_7, 0.8_5_2_7], [-0.0_0_6_9, 0.3_3_8_5, -0.0_0_8_9]]).to(_snake_case) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3] , _snake_case , atol=_snake_case)) UpperCAmelCase_ = torch.tensor( [[-0.8_4_2_2, -0.8_4_3_4, -0.9_7_1_8], [-1.0_1_4_4, -0.5_5_6_5, -0.4_1_9_5], [-1.0_0_3_8, -0.4_4_8_4, -0.1_9_6_1]]).to(_snake_case) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] , _snake_case , atol=_snake_case)) UpperCAmelCase_ = torch.tensor( [[0.2_8_5_2, -0.0_1_5_9, 0.9_7_3_5], [0.6_2_5_4, 0.1_8_5_8, 0.8_5_2_9], [-0.0_6_8_0, -0.4_1_1_6, 1.8_4_1_3]]).to(_snake_case) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3] , _snake_case , atol=_snake_case)) def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ = ( MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco''') .to(_snake_case) .eval() ) UpperCAmelCase_ = self.default_image_processor UpperCAmelCase_ = prepare_img() UpperCAmelCase_ = image_processor(_snake_case , return_tensors='''pt''').to(_snake_case) UpperCAmelCase_ = inputs['''pixel_values'''].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0) # check size self.assertEqual(_snake_case , (1, 3, 800, 1088)) with torch.no_grad(): UpperCAmelCase_ = model(**_snake_case) # masks_queries_logits UpperCAmelCase_ = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) , ) UpperCAmelCase_ = [ [-1.3_7_3_7_1_2_4, -1.7_7_2_4_9_3_7, -1.9_3_6_4_2_3_3], [-1.5_9_7_7_2_8_1, -1.9_8_6_7_9_3_9, -2.1_5_2_3_6_9_5], [-1.5_7_9_5_3_9_8, -1.9_2_6_9_8_3_2, -2.0_9_3_9_4_2], ] UpperCAmelCase_ = torch.tensor(_snake_case).to(_snake_case) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , _snake_case , atol=_snake_case)) # class_queries_logits UpperCAmelCase_ = outputs.class_queries_logits self.assertEqual( class_queries_logits.shape , (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)) UpperCAmelCase_ = torch.tensor( [ [1.65_12e00, -5.25_72e00, -3.35_19e00], [3.61_69e-02, -5.90_25e00, -2.93_13e00], [1.07_66e-04, -7.76_30e00, -5.12_63e00], ]).to(_snake_case) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , _snake_case , atol=_snake_case)) def lowerCamelCase ( self : Optional[Any]): """simple docstring""" UpperCAmelCase_ = ( MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-resnet101-coco-stuff''') .to(_snake_case) .eval() ) UpperCAmelCase_ = self.default_image_processor UpperCAmelCase_ = prepare_img() UpperCAmelCase_ = image_processor(_snake_case , return_tensors='''pt''').to(_snake_case) UpperCAmelCase_ = inputs['''pixel_values'''].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0) # check size self.assertEqual(_snake_case , (1, 3, 800, 1088)) with torch.no_grad(): UpperCAmelCase_ = model(**_snake_case) # masks_queries_logits UpperCAmelCase_ = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) , ) UpperCAmelCase_ = [[-0.9_0_4_6, -2.6_3_6_6, -4.6_0_6_2], [-3.4_1_7_9, -5.7_8_9_0, -8.8_0_5_7], [-4.9_1_7_9, -7.6_5_6_0, -1_0.7_7_1_1]] UpperCAmelCase_ = torch.tensor(_snake_case).to(_snake_case) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , _snake_case , atol=_snake_case)) # class_queries_logits UpperCAmelCase_ = outputs.class_queries_logits self.assertEqual( class_queries_logits.shape , (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)) UpperCAmelCase_ = torch.tensor( [[4.7_1_8_8, -3.2_5_8_5, -2.8_8_5_7], [6.6_8_7_1, -2.9_1_8_1, -1.2_4_8_7], [7.2_4_4_9, -2.2_7_6_4, -2.1_8_7_4]]).to(_snake_case) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , _snake_case , atol=_snake_case)) def lowerCamelCase ( self : Tuple): """simple docstring""" UpperCAmelCase_ = ( MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco''') .to(_snake_case) .eval() ) UpperCAmelCase_ = self.default_image_processor UpperCAmelCase_ = image_processor( [np.zeros((3, 800, 1333)), np.zeros((3, 800, 1333))] , segmentation_maps=[np.zeros((384, 384)).astype(np.floataa), np.zeros((384, 384)).astype(np.floataa)] , return_tensors='''pt''' , ) UpperCAmelCase_ = inputs['''pixel_values'''].to(_snake_case) UpperCAmelCase_ = [el.to(_snake_case) for el in inputs['''mask_labels''']] UpperCAmelCase_ = [el.to(_snake_case) for el in inputs['''class_labels''']] with torch.no_grad(): UpperCAmelCase_ = model(**_snake_case) self.assertTrue(outputs.loss is not None)
51
1
from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block @dataclass class _UpperCamelCase ( lowerCAmelCase_ ): _UpperCamelCase : torch.FloatTensor class _UpperCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ ): @register_to_config def __init__( self: int , _SCREAMING_SNAKE_CASE: int = 65536 , _SCREAMING_SNAKE_CASE: Optional[int] = None , _SCREAMING_SNAKE_CASE: int = 2 , _SCREAMING_SNAKE_CASE: int = 2 , _SCREAMING_SNAKE_CASE: int = 0 , _SCREAMING_SNAKE_CASE: str = "fourier" , _SCREAMING_SNAKE_CASE: bool = True , _SCREAMING_SNAKE_CASE: bool = False , _SCREAMING_SNAKE_CASE: float = 0.0 , _SCREAMING_SNAKE_CASE: Tuple[str] = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") , _SCREAMING_SNAKE_CASE: Tuple[str] = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") , _SCREAMING_SNAKE_CASE: Tuple[str] = "UNetMidBlock1D" , _SCREAMING_SNAKE_CASE: str = None , _SCREAMING_SNAKE_CASE: Tuple[int] = (32, 32, 64) , _SCREAMING_SNAKE_CASE: str = None , _SCREAMING_SNAKE_CASE: int = 8 , _SCREAMING_SNAKE_CASE: int = 1 , _SCREAMING_SNAKE_CASE: bool = False , ) -> Optional[int]: """simple docstring""" super().__init__() UpperCamelCase_ = sample_size # time if time_embedding_type == "fourier": UpperCamelCase_ = GaussianFourierProjection( embedding_size=8 , set_W_to_weight=_SCREAMING_SNAKE_CASE , log=_SCREAMING_SNAKE_CASE , flip_sin_to_cos=_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = 2 * block_out_channels[0] elif time_embedding_type == "positional": UpperCamelCase_ = Timesteps( block_out_channels[0] , flip_sin_to_cos=_SCREAMING_SNAKE_CASE , downscale_freq_shift=_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = block_out_channels[0] if use_timestep_embedding: UpperCamelCase_ = block_out_channels[0] * 4 UpperCamelCase_ = TimestepEmbedding( in_channels=_SCREAMING_SNAKE_CASE , time_embed_dim=_SCREAMING_SNAKE_CASE , act_fn=_SCREAMING_SNAKE_CASE , out_dim=block_out_channels[0] , ) UpperCamelCase_ = nn.ModuleList([] ) UpperCamelCase_ = None UpperCamelCase_ = nn.ModuleList([] ) UpperCamelCase_ = None # down UpperCamelCase_ = in_channels for i, down_block_type in enumerate(_SCREAMING_SNAKE_CASE ): UpperCamelCase_ = output_channel UpperCamelCase_ = block_out_channels[i] if i == 0: input_channel += extra_in_channels UpperCamelCase_ = i == len(_SCREAMING_SNAKE_CASE ) - 1 UpperCamelCase_ = get_down_block( _SCREAMING_SNAKE_CASE , num_layers=_SCREAMING_SNAKE_CASE , in_channels=_SCREAMING_SNAKE_CASE , out_channels=_SCREAMING_SNAKE_CASE , temb_channels=block_out_channels[0] , add_downsample=not is_final_block or downsample_each_block , ) self.down_blocks.append(_SCREAMING_SNAKE_CASE ) # mid UpperCamelCase_ = get_mid_block( _SCREAMING_SNAKE_CASE , in_channels=block_out_channels[-1] , mid_channels=block_out_channels[-1] , out_channels=block_out_channels[-1] , embed_dim=block_out_channels[0] , num_layers=_SCREAMING_SNAKE_CASE , add_downsample=_SCREAMING_SNAKE_CASE , ) # up UpperCamelCase_ = list(reversed(_SCREAMING_SNAKE_CASE ) ) UpperCamelCase_ = reversed_block_out_channels[0] if out_block_type is None: UpperCamelCase_ = out_channels else: UpperCamelCase_ = block_out_channels[0] for i, up_block_type in enumerate(_SCREAMING_SNAKE_CASE ): UpperCamelCase_ = output_channel UpperCamelCase_ = ( reversed_block_out_channels[i + 1] if i < len(_SCREAMING_SNAKE_CASE ) - 1 else final_upsample_channels ) UpperCamelCase_ = i == len(_SCREAMING_SNAKE_CASE ) - 1 UpperCamelCase_ = get_up_block( _SCREAMING_SNAKE_CASE , num_layers=_SCREAMING_SNAKE_CASE , in_channels=_SCREAMING_SNAKE_CASE , out_channels=_SCREAMING_SNAKE_CASE , temb_channels=block_out_channels[0] , add_upsample=not is_final_block , ) self.up_blocks.append(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = output_channel # out UpperCamelCase_ = norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4 , 32 ) UpperCamelCase_ = get_out_block( out_block_type=_SCREAMING_SNAKE_CASE , num_groups_out=_SCREAMING_SNAKE_CASE , embed_dim=block_out_channels[0] , out_channels=_SCREAMING_SNAKE_CASE , act_fn=_SCREAMING_SNAKE_CASE , fc_dim=block_out_channels[-1] // 4 , ) def lowercase ( self: Optional[Any] , _SCREAMING_SNAKE_CASE: torch.FloatTensor , _SCREAMING_SNAKE_CASE: Union[torch.Tensor, float, int] , _SCREAMING_SNAKE_CASE: bool = True , ) -> Union[UNetaDOutput, Tuple]: """simple docstring""" UpperCamelCase_ = timestep if not torch.is_tensor(_SCREAMING_SNAKE_CASE ): UpperCamelCase_ = torch.tensor([timesteps] , dtype=torch.long , device=sample.device ) elif torch.is_tensor(_SCREAMING_SNAKE_CASE ) and len(timesteps.shape ) == 0: UpperCamelCase_ = timesteps[None].to(sample.device ) UpperCamelCase_ = self.time_proj(_SCREAMING_SNAKE_CASE ) if self.config.use_timestep_embedding: UpperCamelCase_ = self.time_mlp(_SCREAMING_SNAKE_CASE ) else: UpperCamelCase_ = timestep_embed[..., None] UpperCamelCase_ = timestep_embed.repeat([1, 1, sample.shape[2]] ).to(sample.dtype ) UpperCamelCase_ = timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]) ) # 2. down UpperCamelCase_ = () for downsample_block in self.down_blocks: UpperCamelCase_ , UpperCamelCase_ = downsample_block(hidden_states=_SCREAMING_SNAKE_CASE , temb=_SCREAMING_SNAKE_CASE ) down_block_res_samples += res_samples # 3. mid if self.mid_block: UpperCamelCase_ = self.mid_block(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # 4. up for i, upsample_block in enumerate(self.up_blocks ): UpperCamelCase_ = down_block_res_samples[-1:] UpperCamelCase_ = down_block_res_samples[:-1] UpperCamelCase_ = upsample_block(_SCREAMING_SNAKE_CASE , res_hidden_states_tuple=_SCREAMING_SNAKE_CASE , temb=_SCREAMING_SNAKE_CASE ) # 5. post-process if self.out_block: UpperCamelCase_ = self.out_block(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if not return_dict: return (sample,) return UNetaDOutput(sample=_SCREAMING_SNAKE_CASE )
351
from ...configuration_utils import PretrainedConfig from ...utils import logging _UpperCAmelCase = logging.get_logger(__name__) _UpperCAmelCase = { 'alibaba-damo/mgp-str-base': 'https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json', } class _UpperCamelCase ( lowerCAmelCase_ ): _UpperCamelCase : str = '''mgp-str''' def __init__( self: Optional[Any] , _SCREAMING_SNAKE_CASE: Optional[int]=[32, 128] , _SCREAMING_SNAKE_CASE: Tuple=4 , _SCREAMING_SNAKE_CASE: Optional[Any]=3 , _SCREAMING_SNAKE_CASE: Optional[int]=27 , _SCREAMING_SNAKE_CASE: Tuple=38 , _SCREAMING_SNAKE_CASE: Tuple=50257 , _SCREAMING_SNAKE_CASE: List[Any]=30522 , _SCREAMING_SNAKE_CASE: Optional[Any]=768 , _SCREAMING_SNAKE_CASE: Dict=12 , _SCREAMING_SNAKE_CASE: List[str]=12 , _SCREAMING_SNAKE_CASE: Dict=4.0 , _SCREAMING_SNAKE_CASE: int=True , _SCREAMING_SNAKE_CASE: Tuple=False , _SCREAMING_SNAKE_CASE: Tuple=1e-5 , _SCREAMING_SNAKE_CASE: Optional[Any]=0.0 , _SCREAMING_SNAKE_CASE: Tuple=0.0 , _SCREAMING_SNAKE_CASE: List[Any]=0.0 , _SCREAMING_SNAKE_CASE: List[str]=False , _SCREAMING_SNAKE_CASE: int=0.02 , **_SCREAMING_SNAKE_CASE: Any , ) -> str: """simple docstring""" super().__init__(**_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = image_size UpperCamelCase_ = patch_size UpperCamelCase_ = num_channels UpperCamelCase_ = max_token_length UpperCamelCase_ = num_character_labels UpperCamelCase_ = num_bpe_labels UpperCamelCase_ = num_wordpiece_labels UpperCamelCase_ = hidden_size UpperCamelCase_ = num_hidden_layers UpperCamelCase_ = num_attention_heads UpperCamelCase_ = mlp_ratio UpperCamelCase_ = distilled UpperCamelCase_ = layer_norm_eps UpperCamelCase_ = drop_rate UpperCamelCase_ = qkv_bias UpperCamelCase_ = attn_drop_rate UpperCamelCase_ = drop_path_rate UpperCamelCase_ = output_aa_attentions UpperCamelCase_ = initializer_range
328
0
class _lowercase : '''simple docstring''' def __init__( self :Optional[int] , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :Optional[Any] ) -> Dict: __SCREAMING_SNAKE_CASE : List[Any] = name __SCREAMING_SNAKE_CASE : Any = value __SCREAMING_SNAKE_CASE : List[Any] = weight def __repr__( self :List[Any] ) -> List[str]: return f'''{self.__class__.__name__}({self.name}, {self.value}, {self.weight})''' def __magic_name__( self :Union[str, Any] ) -> List[str]: return self.value def __magic_name__( self :Any ) -> Dict: return self.name def __magic_name__( self :str ) -> int: return self.weight def __magic_name__( self :Optional[int] ) -> Tuple: return self.value / self.weight def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Union[str, Any] = [] for i in range(len(lowercase__ ) ): menu.append(Things(name[i] , value[i] , weight[i] ) ) return menu def _UpperCamelCase ( lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Tuple = sorted(lowercase__ , key=lowercase__ , reverse=lowercase__ ) __SCREAMING_SNAKE_CASE : int = [] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Optional[Any] = 0.0, 0.0 for i in range(len(lowercase__ ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def _UpperCamelCase ( ): pass if __name__ == "__main__": import doctest doctest.testmod()
9
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 ( ): __SCREAMING_SNAKE_CASE : Dict = '''https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png''' __SCREAMING_SNAKE_CASE : List[str] = Image.open(requests.get(lowercase__ , stream=lowercase__ ).raw ).convert('''RGB''' ) return image def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : List[Any] = [] # 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 ( lowercase__ , lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : List[Any] = dct.pop(lowercase__ ) __SCREAMING_SNAKE_CASE : List[Any] = val def _UpperCamelCase ( lowercase__ , lowercase__ ): for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases __SCREAMING_SNAKE_CASE : Optional[int] = state_dict.pop(F'''visual_encoder.blocks.{i}.attn.q_bias''' ) __SCREAMING_SNAKE_CASE : int = state_dict.pop(F'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict __SCREAMING_SNAKE_CASE : Optional[int] = torch.cat((q_bias, torch.zeros_like(lowercase__ , requires_grad=lowercase__ ), v_bias) ) __SCREAMING_SNAKE_CASE : Optional[Any] = qkv_bias def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : Any = 364 if '''coco''' in model_name else 224 __SCREAMING_SNAKE_CASE : List[str] = BlipaVisionConfig(image_size=lowercase__ ).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: __SCREAMING_SNAKE_CASE : Union[str, Any] = OPTConfig.from_pretrained('''facebook/opt-2.7b''' , eos_token_id=lowercase__ ).to_dict() elif "opt-6.7b" in model_name: __SCREAMING_SNAKE_CASE : List[Any] = OPTConfig.from_pretrained('''facebook/opt-6.7b''' , eos_token_id=lowercase__ ).to_dict() elif "t5-xl" in model_name: __SCREAMING_SNAKE_CASE : Optional[Any] = TaConfig.from_pretrained('''google/flan-t5-xl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: __SCREAMING_SNAKE_CASE : Union[str, Any] = TaConfig.from_pretrained('''google/flan-t5-xxl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() __SCREAMING_SNAKE_CASE : Optional[int] = BlipaConfig(vision_config=lowercase__ , text_config=lowercase__ ) return config, image_size @torch.no_grad() def _UpperCamelCase ( lowercase__ , lowercase__=None , lowercase__=False ): __SCREAMING_SNAKE_CASE : Any = ( AutoTokenizer.from_pretrained('''facebook/opt-2.7b''' ) if '''opt''' in model_name else AutoTokenizer.from_pretrained('''google/flan-t5-xl''' ) ) __SCREAMING_SNAKE_CASE : str = tokenizer('''\n''' , add_special_tokens=lowercase__ ).input_ids[0] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Union[str, Any] = get_blipa_config(lowercase__ , eos_token_id=lowercase__ ) __SCREAMING_SNAKE_CASE : int = BlipaForConditionalGeneration(lowercase__ ).eval() __SCREAMING_SNAKE_CASE : int = { '''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'''), } __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Any = model_name_to_original[model_name] # load original model print('''Loading original model...''' ) __SCREAMING_SNAKE_CASE : List[str] = '''cuda''' if torch.cuda.is_available() else '''cpu''' __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Dict = load_model_and_preprocess( name=lowercase__ , model_type=lowercase__ , is_eval=lowercase__ , device=lowercase__ ) original_model.eval() print('''Done!''' ) # update state dict keys __SCREAMING_SNAKE_CASE : List[str] = original_model.state_dict() __SCREAMING_SNAKE_CASE : Optional[int] = create_rename_keys(lowercase__ ) for src, dest in rename_keys: rename_key(lowercase__ , lowercase__ , lowercase__ ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): __SCREAMING_SNAKE_CASE : Tuple = state_dict.pop(lowercase__ ) if key.startswith('''Qformer.bert''' ): __SCREAMING_SNAKE_CASE : List[str] = key.replace('''Qformer.bert''' , '''qformer''' ) if "attention.self" in key: __SCREAMING_SNAKE_CASE : Union[str, Any] = key.replace('''self''' , '''attention''' ) if "opt_proj" in key: __SCREAMING_SNAKE_CASE : Dict = key.replace('''opt_proj''' , '''language_projection''' ) if "t5_proj" in key: __SCREAMING_SNAKE_CASE : Tuple = key.replace('''t5_proj''' , '''language_projection''' ) if key.startswith('''opt''' ): __SCREAMING_SNAKE_CASE : List[str] = key.replace('''opt''' , '''language''' ) if key.startswith('''t5''' ): __SCREAMING_SNAKE_CASE : Tuple = key.replace('''t5''' , '''language''' ) __SCREAMING_SNAKE_CASE : Tuple = val # read in qv biases read_in_q_v_bias(lowercase__ , lowercase__ ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : List[str] = hf_model.load_state_dict(lowercase__ , strict=lowercase__ ) assert len(lowercase__ ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] __SCREAMING_SNAKE_CASE : List[str] = load_demo_image() __SCREAMING_SNAKE_CASE : Any = vis_processors['''eval'''](lowercase__ ).unsqueeze(0 ).to(lowercase__ ) __SCREAMING_SNAKE_CASE : str = tokenizer(['''\n'''] , return_tensors='''pt''' ).input_ids.to(lowercase__ ) # create processor __SCREAMING_SNAKE_CASE : List[Any] = BlipImageProcessor( size={'''height''': image_size, '''width''': image_size} , image_mean=lowercase__ , image_std=lowercase__ ) __SCREAMING_SNAKE_CASE : int = BlipaProcessor(image_processor=lowercase__ , tokenizer=lowercase__ ) __SCREAMING_SNAKE_CASE : Any = processor(images=lowercase__ , return_tensors='''pt''' ).pixel_values.to(lowercase__ ) # make sure processor creates exact same pixel values assert torch.allclose(lowercase__ , lowercase__ ) original_model.to(lowercase__ ) hf_model.to(lowercase__ ) with torch.no_grad(): if "opt" in model_name: __SCREAMING_SNAKE_CASE : Dict = original_model({'''image''': original_pixel_values, '''text_input''': ['''''']} ).logits __SCREAMING_SNAKE_CASE : Dict = hf_model(lowercase__ , lowercase__ ).logits else: __SCREAMING_SNAKE_CASE : int = original_model( {'''image''': original_pixel_values, '''text_input''': ['''\n'''], '''text_output''': ['''\n''']} ).logits __SCREAMING_SNAKE_CASE : List[Any] = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -100 ) __SCREAMING_SNAKE_CASE : Optional[int] = hf_model(lowercase__ , lowercase__ , labels=lowercase__ ).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": __SCREAMING_SNAKE_CASE : Dict = torch.tensor( [[-41.5850, -4.4440, -8.9922], [-47.4322, -5.9143, -1.7340]] , device=lowercase__ ) assert torch.allclose(logits[0, :3, :3] , lowercase__ , atol=1e-4 ) elif model_name == "blip2-flan-t5-xl-coco": __SCREAMING_SNAKE_CASE : Any = torch.tensor( [[-57.0109, -9.8967, -12.6280], [-68.6578, -12.7191, -10.5065]] , device=lowercase__ ) else: # cast to same type __SCREAMING_SNAKE_CASE : Optional[Any] = logits.dtype assert torch.allclose(original_logits.to(lowercase__ ) , lowercase__ , atol=1e-2 ) print('''Looks ok!''' ) print('''Generating a caption...''' ) __SCREAMING_SNAKE_CASE : Any = '''''' __SCREAMING_SNAKE_CASE : Optional[int] = tokenizer(lowercase__ , return_tensors='''pt''' ).input_ids.to(lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = original_model.generate({'''image''': original_pixel_values} ) __SCREAMING_SNAKE_CASE : Union[str, Any] = hf_model.generate( lowercase__ , lowercase__ , do_sample=lowercase__ , 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:''' , lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = input_ids.shape[1] __SCREAMING_SNAKE_CASE : Any = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=lowercase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = [text.strip() for text in output_text] print('''HF generation:''' , lowercase__ ) if pytorch_dump_folder_path is not None: processor.save_pretrained(lowercase__ ) hf_model.save_pretrained(lowercase__ ) if push_to_hub: processor.push_to_hub(F'''nielsr/{model_name}''' ) hf_model.push_to_hub(F'''nielsr/{model_name}''' ) if __name__ == "__main__": __lowerCAmelCase : List[str] =argparse.ArgumentParser() __lowerCAmelCase : Tuple =[ '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', ) __lowerCAmelCase : List[Any] =parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
9
1
from argparse import ArgumentParser, Namespace from typing import Any, List, Optional from ..pipelines import Pipeline, get_supported_tasks, pipeline from ..utils import logging from . import BaseTransformersCLICommand try: from fastapi import Body, FastAPI, HTTPException from fastapi.routing import APIRoute from pydantic import BaseModel from starlette.responses import JSONResponse from uvicorn import run A_ : Dict = True except (ImportError, AttributeError): A_ : Optional[Any] = object def UpperCamelCase (*lowercase_: Optional[int] , **lowercase_: Tuple ) -> Any: pass A_ : int = False A_ : Optional[int] = logging.get_logger('transformers-cli/serving') def UpperCamelCase (lowercase_: str ) -> List[Any]: A__ : Tuple = pipeline( task=args.task , model=args.model if args.model else None , config=args.config , tokenizer=args.tokenizer , device=args.device , ) return ServeCommand(_lowerCamelCase , args.host , args.port , args.workers ) class _a (__lowercase ): '''simple docstring''' UpperCAmelCase__: dict class _a (__lowercase ): '''simple docstring''' UpperCAmelCase__: List[str] UpperCAmelCase__: Optional[List[int]] class _a (__lowercase ): '''simple docstring''' UpperCAmelCase__: str class _a (__lowercase ): '''simple docstring''' UpperCAmelCase__: Any class _a (__lowercase ): '''simple docstring''' @staticmethod def __A ( A__ ): A__ : int = parser.add_parser( """serve""" , help="""CLI tool to run inference requests through REST and GraphQL endpoints.""" ) serve_parser.add_argument( """--task""" , type=A__ , choices=get_supported_tasks() , help="""The task to run the pipeline on""" , ) serve_parser.add_argument("""--host""" , type=A__ , default="""localhost""" , help="""Interface the server will listen on.""" ) serve_parser.add_argument("""--port""" , type=A__ , default=8888 , help="""Port the serving will listen to.""" ) serve_parser.add_argument("""--workers""" , type=A__ , default=1 , help="""Number of http workers""" ) serve_parser.add_argument("""--model""" , type=A__ , help="""Model's name or path to stored model.""" ) serve_parser.add_argument("""--config""" , type=A__ , help="""Model's config name or path to stored model.""" ) serve_parser.add_argument("""--tokenizer""" , type=A__ , help="""Tokenizer name to use.""" ) serve_parser.add_argument( """--device""" , type=A__ , default=-1 , help="""Indicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1)""" , ) serve_parser.set_defaults(func=A__ ) def __init__( self , A__ , A__ , A__ , A__ ): A__ : Dict = pipeline A__ : str = host A__ : List[Any] = port A__ : Any = workers if not _serve_dependencies_installed: raise RuntimeError( """Using serve command requires FastAPI and uvicorn. """ """Please install transformers with [serving]: pip install \"transformers[serving]\".""" """Or install FastAPI and uvicorn separately.""" ) else: logger.info(F"""Serving model over {host}:{port}""" ) A__ : int = FastAPI( routes=[ APIRoute( """/""" , self.model_info , response_model=A__ , response_class=A__ , methods=["""GET"""] , ), APIRoute( """/tokenize""" , self.tokenize , response_model=A__ , response_class=A__ , methods=["""POST"""] , ), APIRoute( """/detokenize""" , self.detokenize , response_model=A__ , response_class=A__ , methods=["""POST"""] , ), APIRoute( """/forward""" , self.forward , response_model=A__ , response_class=A__ , methods=["""POST"""] , ), ] , timeout=600 , ) def __A ( self ): run(self._app , host=self.host , port=self.port , workers=self.workers ) def __A ( self ): return ServeModelInfoResult(infos=vars(self._pipeline.model.config ) ) def __A ( self , A__ = Body(A__ , embed=A__ ) , A__ = Body(A__ , embed=A__ ) ): try: A__ : Union[str, Any] = self._pipeline.tokenizer.tokenize(A__ ) if return_ids: A__ : Tuple = self._pipeline.tokenizer.convert_tokens_to_ids(A__ ) return ServeTokenizeResult(tokens=A__ , tokens_ids=A__ ) else: return ServeTokenizeResult(tokens=A__ ) except Exception as e: raise HTTPException(status_code=500 , detail={"""model""": """""", """error""": str(A__ )} ) def __A ( self , A__ = Body(A__ , embed=A__ ) , A__ = Body(A__ , embed=A__ ) , A__ = Body(A__ , embed=A__ ) , ): try: A__ : Dict = self._pipeline.tokenizer.decode(A__ , A__ , A__ ) return ServeDeTokenizeResult(model="""""" , text=A__ ) except Exception as e: raise HTTPException(status_code=500 , detail={"""model""": """""", """error""": str(A__ )} ) async def __A ( self , A__=Body(A__ , embed=A__ ) ): if len(A__ ) == 0: return ServeForwardResult(output=[] , attention=[] ) try: # Forward through the model A__ : Union[str, Any] = self._pipeline(A__ ) return ServeForwardResult(output=A__ ) except Exception as e: raise HTTPException(500 , {"""error""": str(A__ )} )
363
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available from ...utils import OptionalDependencyNotAvailable A_ : List[Any] = {'configuration_gpt_neox': ['GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GPTNeoXConfig']} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Optional[Any] = ['GPTNeoXTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Optional[int] = [ 'GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST', 'GPTNeoXForCausalLM', 'GPTNeoXForQuestionAnswering', 'GPTNeoXForSequenceClassification', 'GPTNeoXForTokenClassification', 'GPTNeoXLayer', 'GPTNeoXModel', 'GPTNeoXPreTrainedModel', ] if TYPE_CHECKING: from .configuration_gpt_neox import GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_neox_fast import GPTNeoXTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox import ( GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, GPTNeoXLayer, GPTNeoXModel, GPTNeoXPreTrainedModel, ) else: import sys A_ : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
141
0