code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
from typing import Optional import pyspark from .. import Features, NamedSplit from ..download import DownloadMode from ..packaged_modules.spark.spark import Spark from .abc import AbstractDatasetReader class lowercase_ (lowercase__ ): def __init__( self , lowercase_ , lowercase_ = None , lowercase_ = None , lowercase_ = True , lowercase_ = None , lowercase_ = False , lowercase_ = None , lowercase_ = True , lowercase_ = "arrow" , **lowercase_ , ) -> Union[str, Any]: super().__init__( split=lowercase_ , features=lowercase_ , cache_dir=lowercase_ , keep_in_memory=lowercase_ , streaming=lowercase_ , **lowercase_ , ) a__ =load_from_cache_file a__ =file_format a__ =Spark( df=lowercase_ , features=lowercase_ , cache_dir=lowercase_ , working_dir=lowercase_ , **lowercase_ , ) def __UpperCamelCase ( self) -> List[Any]: if self.streaming: return self.builder.as_streaming_dataset(split=self.split) a__ =None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD self.builder.download_and_prepare( download_mode=lowercase_ , file_format=self._file_format , ) return self.builder.as_dataset(split=self.split)
20
'''simple docstring''' import asyncio import os import re import sys import tempfile import unittest from contextlib import contextmanager from copy import deepcopy from distutils.util import strtobool from enum import Enum from importlib.util import find_spec from pathlib import Path from unittest.mock import patch import pyarrow as pa import pytest import requests from packaging import version from datasets import config if config.PY_VERSION < version.parse('''3.8'''): import importlib_metadata else: import importlib.metadata as importlib_metadata def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : Tuple=False )-> Union[str, Any]: '''simple docstring''' try: __snake_case = os.environ[key] except KeyError: # KEY isn't set, default to `default`. __snake_case = default else: # KEY is set, convert it to True or False. try: __snake_case = strtobool(_lowerCamelCase ) except ValueError: # More values are supported, but let's keep the message simple. raise ValueError(f'''If set, {key} must be yes or no.''' ) return _value UpperCAmelCase_ : Optional[Any] = parse_flag_from_env('''RUN_SLOW''', default=False) UpperCAmelCase_ : Union[str, Any] = parse_flag_from_env('''RUN_REMOTE''', default=False) UpperCAmelCase_ : Optional[Any] = parse_flag_from_env('''RUN_LOCAL''', default=True) UpperCAmelCase_ : Union[str, Any] = parse_flag_from_env('''RUN_PACKAGED''', default=True) # Compression UpperCAmelCase_ : Dict = pytest.mark.skipif(not config.LZ4_AVAILABLE, reason='''test requires lz4''') UpperCAmelCase_ : int = pytest.mark.skipif(not config.PY7ZR_AVAILABLE, reason='''test requires py7zr''') UpperCAmelCase_ : Tuple = pytest.mark.skipif(not config.ZSTANDARD_AVAILABLE, reason='''test requires zstandard''') # Audio UpperCAmelCase_ : str = pytest.mark.skipif( # On Windows and OS X, soundfile installs sndfile find_spec('''soundfile''') is None or version.parse(importlib_metadata.version('''soundfile''')) < version.parse('''0.12.0'''), reason='''test requires sndfile>=0.12.1: \'pip install \"soundfile>=0.12.1\"\'; ''', ) # Beam UpperCAmelCase_ : Tuple = pytest.mark.skipif( not config.BEAM_AVAILABLE or config.DILL_VERSION >= version.parse('''0.3.2'''), reason='''test requires apache-beam and a compatible dill version''', ) # Dill-cloudpickle compatibility UpperCAmelCase_ : Union[str, Any] = pytest.mark.skipif( config.DILL_VERSION <= version.parse('''0.3.2'''), reason='''test requires dill>0.3.2 for cloudpickle compatibility''', ) # Windows UpperCAmelCase_ : int = pytest.mark.skipif( sys.platform == '''win32''', reason='''test should not be run on Windows''', ) def _UpperCamelCase (_lowerCamelCase : List[str] )-> List[Any]: '''simple docstring''' try: import faiss # noqa except ImportError: __snake_case = unittest.skip('''test requires faiss''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> List[str]: '''simple docstring''' try: import regex # noqa except ImportError: __snake_case = unittest.skip('''test requires regex''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> Union[str, Any]: '''simple docstring''' try: import elasticsearch # noqa except ImportError: __snake_case = unittest.skip('''test requires elasticsearch''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Dict )-> List[Any]: '''simple docstring''' try: import sqlalchemy # noqa except ImportError: __snake_case = unittest.skip('''test requires sqlalchemy''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : List[str] )-> List[str]: '''simple docstring''' if not config.TORCH_AVAILABLE: __snake_case = unittest.skip('''test requires PyTorch''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Optional[Any] )-> Dict: '''simple docstring''' if not config.TF_AVAILABLE: __snake_case = unittest.skip('''test requires TensorFlow''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : int )-> Any: '''simple docstring''' if not config.JAX_AVAILABLE: __snake_case = unittest.skip('''test requires JAX''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Tuple )-> str: '''simple docstring''' if not config.PIL_AVAILABLE: __snake_case = unittest.skip('''test requires Pillow''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Dict )-> Any: '''simple docstring''' try: import transformers # noqa F401 except ImportError: return unittest.skip('''test requires transformers''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : int )-> Tuple: '''simple docstring''' try: import tiktoken # noqa F401 except ImportError: return unittest.skip('''test requires tiktoken''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : Dict )-> str: '''simple docstring''' try: import spacy # noqa F401 except ImportError: return unittest.skip('''test requires spacy''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : int )-> Dict: '''simple docstring''' def _require_spacy_model(_lowerCamelCase : int ): try: import spacy # noqa F401 spacy.load(_lowerCamelCase ) except ImportError: return unittest.skip('''test requires spacy''' )(_lowerCamelCase ) except OSError: return unittest.skip('''test requires spacy model \'{}\''''.format(_lowerCamelCase ) )(_lowerCamelCase ) else: return test_case return _require_spacy_model def _UpperCamelCase (_lowerCamelCase : str )-> Dict: '''simple docstring''' try: import pyspark # noqa F401 except ImportError: return unittest.skip('''test requires pyspark''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : Tuple )-> str: '''simple docstring''' try: import joblibspark # noqa F401 except ImportError: return unittest.skip('''test requires joblibspark''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : Any )-> int: '''simple docstring''' if not _run_slow_tests or _run_slow_tests == 0: __snake_case = unittest.skip('''test is slow''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Any )-> Optional[Any]: '''simple docstring''' if not _run_local_tests or _run_local_tests == 0: __snake_case = unittest.skip('''test is local''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : str )-> int: '''simple docstring''' if not _run_packaged_tests or _run_packaged_tests == 0: __snake_case = unittest.skip('''test is packaged''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Optional[int] )-> str: '''simple docstring''' if not _run_remote_tests or _run_remote_tests == 0: __snake_case = unittest.skip('''test requires remote''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (*_lowerCamelCase : str )-> Optional[int]: '''simple docstring''' def decorate(cls : Optional[Any] ): for name, fn in cls.__dict__.items(): if callable(_lowerCamelCase ) and name.startswith('''test''' ): for decorator in decorators: __snake_case = decorator(_lowerCamelCase ) setattr(cls , _lowerCamelCase , _lowerCamelCase ) return cls return decorate class lowerCAmelCase ( __lowerCAmelCase): pass class lowerCAmelCase ( __lowerCAmelCase): __lowercase : List[str] = 0 __lowercase : Dict = 1 __lowercase : List[Any] = 2 @contextmanager def _UpperCamelCase (_lowerCamelCase : Dict=OfflineSimulationMode.CONNECTION_FAILS , _lowerCamelCase : Optional[int]=1E-16 )-> Tuple: '''simple docstring''' __snake_case = requests.Session().request def timeout_request(_lowerCamelCase : Any , _lowerCamelCase : str , _lowerCamelCase : str , **_lowerCamelCase : Any ): # Change the url to an invalid url so that the connection hangs __snake_case = '''https://10.255.255.1''' if kwargs.get('''timeout''' ) is None: raise RequestWouldHangIndefinitelyError( f'''Tried a call to {url} in offline mode with no timeout set. Please set a timeout.''' ) __snake_case = timeout try: return online_request(_lowerCamelCase , _lowerCamelCase , **_lowerCamelCase ) except Exception as e: # The following changes in the error are just here to make the offline timeout error prettier __snake_case = url __snake_case = e.args[0] __snake_case = (max_retry_error.args[0].replace('''10.255.255.1''' , f'''OfflineMock[{url}]''' ),) __snake_case = (max_retry_error,) raise def raise_connection_error(_lowerCamelCase : Union[str, Any] , _lowerCamelCase : Optional[int] , **_lowerCamelCase : Dict ): raise requests.ConnectionError('''Offline mode is enabled.''' , request=_lowerCamelCase ) if mode is OfflineSimulationMode.CONNECTION_FAILS: with patch('''requests.Session.send''' , _lowerCamelCase ): yield elif mode is OfflineSimulationMode.CONNECTION_TIMES_OUT: # inspired from https://stackoverflow.com/a/904609 with patch('''requests.Session.request''' , _lowerCamelCase ): yield elif mode is OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1: with patch('''datasets.config.HF_DATASETS_OFFLINE''' , _lowerCamelCase ): yield else: raise ValueError('''Please use a value from the OfflineSimulationMode enum.''' ) @contextmanager def _UpperCamelCase (*_lowerCamelCase : Union[str, Any] , **_lowerCamelCase : List[str] )-> Any: '''simple docstring''' __snake_case = str(Path().resolve() ) with tempfile.TemporaryDirectory(*_lowerCamelCase , **_lowerCamelCase ) as tmp_dir: try: os.chdir(_lowerCamelCase ) yield finally: os.chdir(_lowerCamelCase ) @contextmanager def _UpperCamelCase ()-> Optional[int]: '''simple docstring''' import gc gc.collect() __snake_case = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory > 0, "Arrow memory didn't increase." @contextmanager def _UpperCamelCase ()-> List[Any]: '''simple docstring''' import gc gc.collect() __snake_case = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory <= 0, "Arrow memory wasn't expected to increase." def _UpperCamelCase (_lowerCamelCase : Any , _lowerCamelCase : int )-> Any: '''simple docstring''' return deepcopy(_lowerCamelCase ).integers(0 , 1_00 , 10 ).tolist() == deepcopy(_lowerCamelCase ).integers(0 , 1_00 , 10 ).tolist() def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> List[Any]: '''simple docstring''' import decorator from requests.exceptions import HTTPError def _wrapper(_lowerCamelCase : int , *_lowerCamelCase : int , **_lowerCamelCase : Optional[int] ): try: return func(*_lowerCamelCase , **_lowerCamelCase ) except HTTPError as err: if str(_lowerCamelCase ).startswith('''500''' ) or str(_lowerCamelCase ).startswith('''502''' ): pytest.xfail(str(_lowerCamelCase ) ) raise err return decorator.decorator(_wrapper , _lowerCamelCase ) class lowerCAmelCase : def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' __snake_case = returncode __snake_case = stdout __snake_case = stderr async def _UpperCamelCase (_lowerCamelCase : List[str] , _lowerCamelCase : Union[str, Any] )-> Dict: '''simple docstring''' while True: __snake_case = await stream.readline() if line: callback(_lowerCamelCase ) else: break async def _UpperCamelCase (_lowerCamelCase : Tuple , _lowerCamelCase : Optional[int]=None , _lowerCamelCase : Optional[Any]=None , _lowerCamelCase : Tuple=None , _lowerCamelCase : Dict=False , _lowerCamelCase : List[Any]=False )-> _RunOutput: '''simple docstring''' if echo: print('''\nRunning: ''' , ''' '''.join(_lowerCamelCase ) ) __snake_case = await asyncio.create_subprocess_exec( cmd[0] , *cmd[1:] , stdin=_lowerCamelCase , stdout=asyncio.subprocess.PIPE , stderr=asyncio.subprocess.PIPE , env=_lowerCamelCase , ) # note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe # https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait # # If it starts hanging, will need to switch to the following code. The problem is that no data # will be seen until it's done and if it hangs for example there will be no debug info. # out, err = await p.communicate() # return _RunOutput(p.returncode, out, err) __snake_case = [] __snake_case = [] def tee(_lowerCamelCase : int , _lowerCamelCase : List[Any] , _lowerCamelCase : str , _lowerCamelCase : Dict="" ): __snake_case = line.decode('''utf-8''' ).rstrip() sink.append(_lowerCamelCase ) if not quiet: print(_lowerCamelCase , _lowerCamelCase , file=_lowerCamelCase ) # XXX: the timeout doesn't seem to make any difference here await asyncio.wait( [ _read_stream(p.stdout , lambda _lowerCamelCase : tee(_lowerCamelCase , _lowerCamelCase , sys.stdout , label='''stdout:''' ) ), _read_stream(p.stderr , lambda _lowerCamelCase : tee(_lowerCamelCase , _lowerCamelCase , sys.stderr , label='''stderr:''' ) ), ] , timeout=_lowerCamelCase , ) return _RunOutput(await p.wait() , _lowerCamelCase , _lowerCamelCase ) def _UpperCamelCase (_lowerCamelCase : Optional[Any] , _lowerCamelCase : Any=None , _lowerCamelCase : List[str]=None , _lowerCamelCase : Optional[Any]=1_80 , _lowerCamelCase : Dict=False , _lowerCamelCase : int=True )-> _RunOutput: '''simple docstring''' __snake_case = asyncio.get_event_loop() __snake_case = loop.run_until_complete( _stream_subprocess(_lowerCamelCase , env=_lowerCamelCase , stdin=_lowerCamelCase , timeout=_lowerCamelCase , quiet=_lowerCamelCase , echo=_lowerCamelCase ) ) __snake_case = ''' '''.join(_lowerCamelCase ) if result.returncode > 0: __snake_case = '''\n'''.join(result.stderr ) raise RuntimeError( f'''\'{cmd_str}\' failed with returncode {result.returncode}\n\n''' f'''The combined stderr from workers follows:\n{stderr}''' ) # check that the subprocess actually did run and produced some output, should the test rely on # the remote side to do the testing if not result.stdout and not result.stderr: raise RuntimeError(f'''\'{cmd_str}\' produced no output.''' ) return result def _UpperCamelCase ()-> Dict: '''simple docstring''' __snake_case = os.environ.get('''PYTEST_XDIST_WORKER''' , '''gw0''' ) __snake_case = re.sub(R'''^gw''' , '''''' , _lowerCamelCase , 0 , re.M ) return int(_lowerCamelCase ) def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = 2_95_00 __snake_case = pytest_xdist_worker_id() return port + uniq_delta
24
0
from sklearn.metrics import fa_score, matthews_corrcoef import datasets from .record_evaluation import evaluate as evaluate_record UpperCAmelCase_ : int = "\\n@article{wang2019superglue,\n title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems},\n author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R},\n journal={arXiv preprint arXiv:1905.00537},\n year={2019}\n}\n" UpperCAmelCase_ : Any = "\\nSuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after\nGLUE with a new set of more difficult language understanding tasks, improved\nresources, and a new public leaderboard.\n" UpperCAmelCase_ : Tuple = "\nCompute SuperGLUE evaluation metric associated to each SuperGLUE dataset.\nArgs:\n predictions: list of predictions to score. Depending on the SuperGlUE subset:\n - for 'record': list of question-answer dictionaries with the following keys:\n - 'idx': index of the question as specified by the dataset\n - 'prediction_text': the predicted answer text\n - for 'multirc': list of question-answer dictionaries with the following keys:\n - 'idx': index of the question-answer pair as specified by the dataset\n - 'prediction': the predicted answer label\n - otherwise: list of predicted labels\n references: list of reference labels. Depending on the SuperGLUE subset:\n - for 'record': list of question-answers dictionaries with the following keys:\n - 'idx': index of the question as specified by the dataset\n - 'answers': list of possible answers\n - otherwise: list of reference labels\nReturns: depending on the SuperGLUE subset:\n - for 'record':\n - 'exact_match': Exact match between answer and gold answer\n - 'f1': F1 score\n - for 'multirc':\n - 'exact_match': Exact match between answer and gold answer\n - 'f1_m': Per-question macro-F1 score\n - 'f1_a': Average F1 score over all answers\n - for 'axb':\n 'matthews_correlation': Matthew Correlation\n - for 'cb':\n - 'accuracy': Accuracy\n - 'f1': F1 score\n - for all others:\n - 'accuracy': Accuracy\nExamples:\n\n >>> super_glue_metric = datasets.load_metric('super_glue', 'copa') # any of [\"copa\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"boolq\", \"axg\"]\n >>> predictions = [0, 1]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'accuracy': 1.0}\n\n >>> super_glue_metric = datasets.load_metric('super_glue', 'cb')\n >>> predictions = [0, 1]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'accuracy': 1.0, 'f1': 1.0}\n\n >>> super_glue_metric = datasets.load_metric('super_glue', 'record')\n >>> predictions = [{'idx': {'passage': 0, 'query': 0}, 'prediction_text': 'answer'}]\n >>> references = [{'idx': {'passage': 0, 'query': 0}, 'answers': ['answer', 'another_answer']}]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'exact_match': 1.0, 'f1': 1.0}\n\n >>> super_glue_metric = datasets.load_metric('super_glue', 'multirc')\n >>> predictions = [{'idx': {'answer': 0, 'paragraph': 0, 'question': 0}, 'prediction': 0}, {'idx': {'answer': 1, 'paragraph': 2, 'question': 3}, 'prediction': 1}]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'exact_match': 1.0, 'f1_m': 1.0, 'f1_a': 1.0}\n\n >>> super_glue_metric = datasets.load_metric('super_glue', 'axb')\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'matthews_correlation': 1.0}\n" def lowerCAmelCase_ ( lowerCamelCase , lowerCamelCase ): return float((preds == labels).mean() ) def lowerCAmelCase_ ( lowerCamelCase , lowerCamelCase , lowerCamelCase="binary" ): __magic_name__ : Optional[int] =simple_accuracy(lowerCamelCase , lowerCamelCase ) __magic_name__ : Optional[int] =float(fa_score(y_true=lowerCamelCase , y_pred=lowerCamelCase , average=lowerCamelCase ) ) return { "accuracy": acc, "f1": fa, } def lowerCAmelCase_ ( lowerCamelCase , lowerCamelCase ): __magic_name__ : Tuple ={} for id_pred, label in zip(lowerCamelCase , lowerCamelCase ): __magic_name__ : List[Any] =F"{id_pred['idx']['paragraph']}-{id_pred['idx']['question']}" __magic_name__ : Any =id_pred["""prediction"""] if question_id in question_map: question_map[question_id].append((pred, label) ) else: __magic_name__ : Optional[int] =[(pred, label)] __magic_name__ , __magic_name__ : Dict =[], [] for question, preds_labels in question_map.items(): __magic_name__ , __magic_name__ : Tuple =zip(*lowerCamelCase ) __magic_name__ : str =fa_score(y_true=lowerCamelCase , y_pred=lowerCamelCase , average="""macro""" ) fas.append(lowerCamelCase ) __magic_name__ : Optional[int] =int(sum(pred == label for pred, label in preds_labels ) == len(lowerCamelCase ) ) ems.append(lowerCamelCase ) __magic_name__ : Any =float(sum(lowerCamelCase ) / len(lowerCamelCase ) ) __magic_name__ : str =sum(lowerCamelCase ) / len(lowerCamelCase ) __magic_name__ : Any =float(fa_score(y_true=lowerCamelCase , y_pred=[id_pred["""prediction"""] for id_pred in ids_preds] ) ) return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a} @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __A ( datasets.Metric ): def A__ ( self :str ): '''simple docstring''' if self.config_name not in [ "boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg", ]: raise KeyError( """You should supply a configuration name selected in """ """[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]""" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , codebase_urls=[] , reference_urls=[] , format="""numpy""" if not self.config_name == """record""" and not self.config_name == """multirc""" else None , ) def A__ ( self :int ): '''simple docstring''' if self.config_name == "record": return { "predictions": { "idx": { "passage": datasets.Value("""int64""" ), "query": datasets.Value("""int64""" ), }, "prediction_text": datasets.Value("""string""" ), }, "references": { "idx": { "passage": datasets.Value("""int64""" ), "query": datasets.Value("""int64""" ), }, "answers": datasets.Sequence(datasets.Value("""string""" ) ), }, } elif self.config_name == "multirc": return { "predictions": { "idx": { "answer": datasets.Value("""int64""" ), "paragraph": datasets.Value("""int64""" ), "question": datasets.Value("""int64""" ), }, "prediction": datasets.Value("""int64""" ), }, "references": datasets.Value("""int64""" ), } else: return { "predictions": datasets.Value("""int64""" ), "references": datasets.Value("""int64""" ), } def A__ ( self :Union[str, Any] , __snake_case :List[str] , __snake_case :List[Any] ): '''simple docstring''' if self.config_name == "axb": return {"matthews_correlation": matthews_corrcoef(__snake_case , __snake_case )} elif self.config_name == "cb": return acc_and_fa(__snake_case , __snake_case , fa_avg="""macro""" ) elif self.config_name == "record": __magic_name__ : List[str] =[ { """qas""": [ {"""id""": ref["""idx"""]["""query"""], """answers""": [{"""text""": ans} for ans in ref["""answers"""]]} for ref in references ] } ] __magic_name__ : Optional[int] ={pred["""idx"""]["""query"""]: pred["""prediction_text"""] for pred in predictions} return evaluate_record(__snake_case , __snake_case )[0] elif self.config_name == "multirc": return evaluate_multirc(__snake_case , __snake_case ) elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]: return {"accuracy": simple_accuracy(__snake_case , __snake_case )} else: raise KeyError( """You should supply a configuration name selected in """ """[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]""" )
21
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' __snake_case = [[0 for _ in range(_lowerCamelCase )] for _ in range(m + 1 )] for i in range(m + 1 ): __snake_case = 1 for n in range(m + 1 ): for k in range(1 , _lowerCamelCase ): 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: UpperCAmelCase_ : List[str] = int(input('''Enter a number: ''').strip()) print(partition(n)) except ValueError: print('''Please enter a number.''') else: try: UpperCAmelCase_ : Union[str, Any] = int(sys.argv[1]) print(partition(n)) except ValueError: print('''Please pass a number.''')
24
0
'''simple docstring''' from math import factorial _snake_case : Optional[int] = {str(d): factorial(d) for d in range(10)} def snake_case_ (UpperCamelCase : int ): '''simple docstring''' return sum(DIGIT_FACTORIAL[d] for d in str(UpperCamelCase ) ) def snake_case_ (): '''simple docstring''' _a = 7 * factorial(9 ) + 1 return sum(i for i in range(3 , UpperCamelCase ) if sum_of_digit_factorial(UpperCamelCase ) == i ) if __name__ == "__main__": print(F'''{solution() = }''')
22
'''simple docstring''' import argparse import os import re UpperCAmelCase_ : List[str] = '''src/transformers/models/auto''' # re pattern that matches mapping introductions: # SUPER_MODEL_MAPPING_NAMES = OrderedDict or SUPER_MODEL_MAPPING = OrderedDict UpperCAmelCase_ : Tuple = re.compile(R'''[A-Z_]+_MAPPING(\s+|_[A-Z_]+\s+)=\s+OrderedDict''') # re pattern that matches identifiers in mappings UpperCAmelCase_ : Dict = re.compile(R'''\s*\(\s*"(\S[^"]+)"''') def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : bool = False )-> str: '''simple docstring''' with open(_lowerCamelCase , '''r''' , encoding='''utf-8''' ) as f: __snake_case = f.read() __snake_case = content.split('''\n''' ) __snake_case = [] __snake_case = 0 while line_idx < len(_lowerCamelCase ): if _re_intro_mapping.search(lines[line_idx] ) is not None: __snake_case = len(re.search(R'''^(\s*)\S''' , lines[line_idx] ).groups()[0] ) + 8 # Start of a new mapping! while not lines[line_idx].startswith(''' ''' * indent + '''(''' ): new_lines.append(lines[line_idx] ) line_idx += 1 __snake_case = [] while lines[line_idx].strip() != "]": # Blocks either fit in one line or not if lines[line_idx].strip() == "(": __snake_case = line_idx while not lines[line_idx].startswith(''' ''' * indent + ''')''' ): line_idx += 1 blocks.append('''\n'''.join(lines[start_idx : line_idx + 1] ) ) else: blocks.append(lines[line_idx] ) line_idx += 1 # Sort blocks by their identifiers __snake_case = sorted(_lowerCamelCase , key=lambda _lowerCamelCase : _re_identifier.search(_lowerCamelCase ).groups()[0] ) new_lines += blocks else: new_lines.append(lines[line_idx] ) line_idx += 1 if overwrite: with open(_lowerCamelCase , '''w''' , encoding='''utf-8''' ) as f: f.write('''\n'''.join(_lowerCamelCase ) ) elif "\n".join(_lowerCamelCase ) != content: return True def _UpperCamelCase (_lowerCamelCase : bool = False )-> Tuple: '''simple docstring''' __snake_case = [os.path.join(_lowerCamelCase , _lowerCamelCase ) for f in os.listdir(_lowerCamelCase ) if f.endswith('''.py''' )] __snake_case = [sort_auto_mapping(_lowerCamelCase , overwrite=_lowerCamelCase ) for fname in fnames] if not overwrite and any(_lowerCamelCase ): __snake_case = [f for f, d in zip(_lowerCamelCase , _lowerCamelCase ) if d] raise ValueError( f'''The following files have auto mappings that need sorting: {", ".join(_lowerCamelCase )}. Run `make style` to fix''' ''' this.''' ) if __name__ == "__main__": UpperCAmelCase_ : str = argparse.ArgumentParser() parser.add_argument('''--check_only''', action='''store_true''', help='''Whether to only check or fix style.''') UpperCAmelCase_ : List[Any] = parser.parse_args() sort_all_auto_mappings(not args.check_only)
24
0
import os import sys from contextlib import contextmanager # Windows only if os.name == "nt": import ctypes import msvcrt # noqa class _a ( ctypes.Structure ): """simple docstring""" A_ = [("""size""", ctypes.c_int), ("""visible""", ctypes.c_byte)] def _snake_case (): if os.name == "nt": UpperCamelCase_ = CursorInfo() UpperCamelCase_ = ctypes.windll.kernelaa.GetStdHandle(-11) ctypes.windll.kernelaa.GetConsoleCursorInfo(__lowercase , ctypes.byref(__lowercase)) UpperCamelCase_ = False ctypes.windll.kernelaa.SetConsoleCursorInfo(__lowercase , ctypes.byref(__lowercase)) elif os.name == "posix": sys.stdout.write('\033[?25l') sys.stdout.flush() def _snake_case (): if os.name == "nt": UpperCamelCase_ = CursorInfo() UpperCamelCase_ = ctypes.windll.kernelaa.GetStdHandle(-11) ctypes.windll.kernelaa.GetConsoleCursorInfo(__lowercase , ctypes.byref(__lowercase)) UpperCamelCase_ = True ctypes.windll.kernelaa.SetConsoleCursorInfo(__lowercase , ctypes.byref(__lowercase)) elif os.name == "posix": sys.stdout.write('\033[?25h') sys.stdout.flush() @contextmanager def _snake_case (): try: hide_cursor() yield finally: show_cursor()
23
'''simple docstring''' import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _UpperCamelCase (*_lowerCamelCase : str , _lowerCamelCase : Optional[Union[Dict, Any]] = None , _lowerCamelCase : List[Any]=True , _lowerCamelCase : str=2 )-> str: '''simple docstring''' from .. import __version__ __snake_case = take_from __snake_case = () if not isinstance(args[0] , _lowerCamelCase ): __snake_case = (args,) for attribute, version_name, message in args: if version.parse(version.parse(_lowerCamelCase ).base_version ) >= version.parse(_lowerCamelCase ): raise ValueError( f'''The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\'''' f''' version {__version__} is >= {version_name}''' ) __snake_case = None if isinstance(_lowerCamelCase , _lowerCamelCase ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(_lowerCamelCase ),) __snake_case = f'''The `{attribute}` argument is deprecated and will be removed in version {version_name}.''' elif hasattr(_lowerCamelCase , _lowerCamelCase ): values += (getattr(_lowerCamelCase , _lowerCamelCase ),) __snake_case = f'''The `{attribute}` attribute is deprecated and will be removed in version {version_name}.''' elif deprecated_kwargs is None: __snake_case = f'''`{attribute}` is deprecated and will be removed in version {version_name}.''' if warning is not None: __snake_case = warning + ''' ''' if standard_warn else '''''' warnings.warn(warning + message , _lowerCamelCase , stacklevel=_lowerCamelCase ) if isinstance(_lowerCamelCase , _lowerCamelCase ) and len(_lowerCamelCase ) > 0: __snake_case = inspect.getouterframes(inspect.currentframe() )[1] __snake_case = call_frame.filename __snake_case = call_frame.lineno __snake_case = call_frame.function __snake_case , __snake_case = next(iter(deprecated_kwargs.items() ) ) raise TypeError(f'''{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`''' ) if len(_lowerCamelCase ) == 0: return elif len(_lowerCamelCase ) == 1: return values[0] return values
24
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) a_ = {'configuration_reformer': ['REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ReformerConfig']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = ['ReformerTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = ['ReformerTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ 'REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'ReformerAttention', 'ReformerForMaskedLM', 'ReformerForQuestionAnswering', 'ReformerForSequenceClassification', 'ReformerLayer', 'ReformerModel', 'ReformerModelWithLMHead', 'ReformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_reformer import REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, ReformerConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_reformer import ReformerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_reformer_fast import ReformerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_reformer import ( REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ReformerAttention, ReformerForMaskedLM, ReformerForQuestionAnswering, ReformerForSequenceClassification, ReformerLayer, ReformerModel, ReformerModelWithLMHead, ReformerPreTrainedModel, ) else: import sys a_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
25
'''simple docstring''' import argparse import re from pathlib import Path import requests import torch from PIL import Image from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor from transformers import ( EfficientFormerConfig, EfficientFormerForImageClassificationWithTeacher, EfficientFormerImageProcessor, ) from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def _UpperCamelCase (_lowerCamelCase : int , _lowerCamelCase : str )-> List[str]: '''simple docstring''' __snake_case = old_name if "patch_embed" in old_name: __snake_case , __snake_case , __snake_case = old_name.split('''.''' ) if layer == "0": __snake_case = old_name.replace('''0''' , '''convolution1''' ) elif layer == "1": __snake_case = old_name.replace('''1''' , '''batchnorm_before''' ) elif layer == "3": __snake_case = old_name.replace('''3''' , '''convolution2''' ) else: __snake_case = old_name.replace('''4''' , '''batchnorm_after''' ) if "network" in old_name and re.search(R'''\d\.\d''' , _lowerCamelCase ): __snake_case = R'''\b\d{2}\b''' if bool(re.search(_lowerCamelCase , _lowerCamelCase ) ): __snake_case = re.search(R'''\d\.\d\d.''' , _lowerCamelCase ).group() else: __snake_case = re.search(R'''\d\.\d.''' , _lowerCamelCase ).group() if int(match[0] ) < 6: __snake_case = old_name.replace(_lowerCamelCase , '''''' ) __snake_case = trimmed_name.replace('''network''' , match[0] + '''.meta4D_layers.blocks.''' + match[2:-1] ) __snake_case = '''intermediate_stages.''' + trimmed_name else: __snake_case = old_name.replace(_lowerCamelCase , '''''' ) if int(match[2] ) < num_meta4D_last_stage: __snake_case = trimmed_name.replace('''network''' , '''meta4D_layers.blocks.''' + match[2] ) else: __snake_case = str(int(match[2] ) - num_meta4D_last_stage ) __snake_case = trimmed_name.replace('''network''' , '''meta3D_layers.blocks.''' + layer_index ) if "norm1" in old_name: __snake_case = trimmed_name.replace('''norm1''' , '''layernorm1''' ) elif "norm2" in old_name: __snake_case = trimmed_name.replace('''norm2''' , '''layernorm2''' ) elif "fc1" in old_name: __snake_case = trimmed_name.replace('''fc1''' , '''linear_in''' ) elif "fc2" in old_name: __snake_case = trimmed_name.replace('''fc2''' , '''linear_out''' ) __snake_case = '''last_stage.''' + trimmed_name elif "network" in old_name and re.search(R'''.\d.''' , _lowerCamelCase ): __snake_case = old_name.replace('''network''' , '''intermediate_stages''' ) if "fc" in new_name: __snake_case = new_name.replace('''fc''' , '''convolution''' ) elif ("norm1" in new_name) and ("layernorm1" not in new_name): __snake_case = new_name.replace('''norm1''' , '''batchnorm_before''' ) elif ("norm2" in new_name) and ("layernorm2" not in new_name): __snake_case = new_name.replace('''norm2''' , '''batchnorm_after''' ) if "proj" in new_name: __snake_case = new_name.replace('''proj''' , '''projection''' ) if "dist_head" in new_name: __snake_case = new_name.replace('''dist_head''' , '''distillation_classifier''' ) elif "head" in new_name: __snake_case = new_name.replace('''head''' , '''classifier''' ) elif "patch_embed" in new_name: __snake_case = '''efficientformer.''' + new_name elif new_name == "norm.weight" or new_name == "norm.bias": __snake_case = new_name.replace('''norm''' , '''layernorm''' ) __snake_case = '''efficientformer.''' + new_name else: __snake_case = '''efficientformer.encoder.''' + new_name return new_name def _UpperCamelCase (_lowerCamelCase : str , _lowerCamelCase : Tuple )-> List[str]: '''simple docstring''' for key in checkpoint.copy().keys(): __snake_case = checkpoint.pop(_lowerCamelCase ) __snake_case = val return checkpoint def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = '''http://images.cocodataset.org/val2017/000000039769.jpg''' __snake_case = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return image def _UpperCamelCase (_lowerCamelCase : Path , _lowerCamelCase : Path , _lowerCamelCase : Path , _lowerCamelCase : bool )-> Optional[Any]: '''simple docstring''' __snake_case = torch.load(_lowerCamelCase , map_location='''cpu''' )['''model'''] __snake_case = EfficientFormerConfig.from_json_file(_lowerCamelCase ) __snake_case = EfficientFormerForImageClassificationWithTeacher(_lowerCamelCase ) __snake_case = '''_'''.join(checkpoint_path.split('''/''' )[-1].split('''.''' )[0].split('''_''' )[:-1] ) __snake_case = config.depths[-1] - config.num_metaad_blocks + 1 __snake_case = convert_torch_checkpoint(_lowerCamelCase , _lowerCamelCase ) model.load_state_dict(_lowerCamelCase ) model.eval() __snake_case = { '''bilinear''': PILImageResampling.BILINEAR, '''bicubic''': PILImageResampling.BICUBIC, '''nearest''': PILImageResampling.NEAREST, } # prepare image __snake_case = prepare_img() __snake_case = 2_56 __snake_case = 2_24 __snake_case = EfficientFormerImageProcessor( size={'''shortest_edge''': image_size} , crop_size={'''height''': crop_size, '''width''': crop_size} , resample=pillow_resamplings['''bicubic'''] , ) __snake_case = processor(images=_lowerCamelCase , return_tensors='''pt''' ).pixel_values # original processing pipeline __snake_case = Compose( [ Resize(_lowerCamelCase , interpolation=pillow_resamplings['''bicubic'''] ), CenterCrop(_lowerCamelCase ), ToTensor(), Normalize(_lowerCamelCase , _lowerCamelCase ), ] ) __snake_case = image_transforms(_lowerCamelCase ).unsqueeze(0 ) assert torch.allclose(_lowerCamelCase , _lowerCamelCase ) __snake_case = model(_lowerCamelCase ) __snake_case = outputs.logits __snake_case = (1, 10_00) if "l1" in model_name: __snake_case = torch.Tensor( [-0.1312, 0.4353, -1.0499, -0.5124, 0.4183, -0.6793, -1.3777, -0.0893, -0.7358, -2.4328] ) assert torch.allclose(logits[0, :10] , _lowerCamelCase , atol=1E-3 ) assert logits.shape == expected_shape elif "l3" in model_name: __snake_case = torch.Tensor( [-1.3150, -1.5456, -1.2556, -0.8496, -0.7127, -0.7897, -0.9728, -0.3052, 0.3751, -0.3127] ) assert torch.allclose(logits[0, :10] , _lowerCamelCase , atol=1E-3 ) assert logits.shape == expected_shape elif "l7" in model_name: __snake_case = torch.Tensor( [-1.0283, -1.4131, -0.5644, -1.3115, -0.5785, -1.2049, -0.7528, 0.1992, -0.3822, -0.0878] ) assert logits.shape == expected_shape else: raise ValueError( f'''Unknown model checkpoint: {checkpoint_path}. Supported version of efficientformer are l1, l3 and l7''' ) # Save Checkpoints Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase ) model.save_pretrained(_lowerCamelCase ) print(f'''Checkpoint successfuly converted. Model saved at {pytorch_dump_path}''' ) processor.save_pretrained(_lowerCamelCase ) print(f'''Processor successfuly saved at {pytorch_dump_path}''' ) if push_to_hub: print('''Pushing model to the hub...''' ) model.push_to_hub( repo_id=f'''Bearnardd/{pytorch_dump_path}''' , commit_message='''Add model''' , use_temp_dir=_lowerCamelCase , ) processor.push_to_hub( repo_id=f'''Bearnardd/{pytorch_dump_path}''' , commit_message='''Add image processor''' , use_temp_dir=_lowerCamelCase , ) if __name__ == "__main__": UpperCAmelCase_ : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--pytorch_model_path''', default=None, type=str, required=True, help='''Path to EfficientFormer pytorch checkpoint.''', ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The json file for EfficientFormer model config.''', ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') parser.add_argument( '''--no-push_to_hub''', dest='''push_to_hub''', action='''store_false''', help='''Do not push model and image processor to the hub''', ) parser.set_defaults(push_to_hub=True) UpperCAmelCase_ : Union[str, Any] = parser.parse_args() convert_efficientformer_checkpoint( checkpoint_path=args.pytorch_model_path, efficientformer_config_file=args.config_file, pytorch_dump_path=args.pytorch_dump_path, push_to_hub=args.push_to_hub, )
24
0
'''simple docstring''' import argparse import re from pathlib import Path import requests import torch from PIL import Image from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor from transformers import ( EfficientFormerConfig, EfficientFormerForImageClassificationWithTeacher, EfficientFormerImageProcessor, ) from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def _a ( _lowerCamelCase , _lowerCamelCase ) -> str: """simple docstring""" __snake_case : str = old_name if "patch_embed" in old_name: __snake_case , __snake_case , __snake_case : List[Any] = old_name.split(""".""" ) if layer == "0": __snake_case : int = old_name.replace("""0""" , """convolution1""" ) elif layer == "1": __snake_case : List[str] = old_name.replace("""1""" , """batchnorm_before""" ) elif layer == "3": __snake_case : List[str] = old_name.replace("""3""" , """convolution2""" ) else: __snake_case : str = old_name.replace("""4""" , """batchnorm_after""" ) if "network" in old_name and re.search(R"""\d\.\d""" , _lowerCamelCase ): __snake_case : str = R"""\b\d{2}\b""" if bool(re.search(_lowerCamelCase , _lowerCamelCase ) ): __snake_case : int = re.search(R"""\d\.\d\d.""" , _lowerCamelCase ).group() else: __snake_case : List[Any] = re.search(R"""\d\.\d.""" , _lowerCamelCase ).group() if int(match[0] ) < 6: __snake_case : Dict = old_name.replace(_lowerCamelCase , """""" ) __snake_case : Any = trimmed_name.replace("""network""" , match[0] + """.meta4D_layers.blocks.""" + match[2:-1] ) __snake_case : Optional[int] = """intermediate_stages.""" + trimmed_name else: __snake_case : Dict = old_name.replace(_lowerCamelCase , """""" ) if int(match[2] ) < num_meta4D_last_stage: __snake_case : Optional[Any] = trimmed_name.replace("""network""" , """meta4D_layers.blocks.""" + match[2] ) else: __snake_case : Union[str, Any] = str(int(match[2] ) - num_meta4D_last_stage ) __snake_case : Optional[int] = trimmed_name.replace("""network""" , """meta3D_layers.blocks.""" + layer_index ) if "norm1" in old_name: __snake_case : int = trimmed_name.replace("""norm1""" , """layernorm1""" ) elif "norm2" in old_name: __snake_case : Optional[int] = trimmed_name.replace("""norm2""" , """layernorm2""" ) elif "fc1" in old_name: __snake_case : List[Any] = trimmed_name.replace("""fc1""" , """linear_in""" ) elif "fc2" in old_name: __snake_case : List[Any] = trimmed_name.replace("""fc2""" , """linear_out""" ) __snake_case : Any = """last_stage.""" + trimmed_name elif "network" in old_name and re.search(R""".\d.""" , _lowerCamelCase ): __snake_case : int = old_name.replace("""network""" , """intermediate_stages""" ) if "fc" in new_name: __snake_case : Optional[Any] = new_name.replace("""fc""" , """convolution""" ) elif ("norm1" in new_name) and ("layernorm1" not in new_name): __snake_case : Tuple = new_name.replace("""norm1""" , """batchnorm_before""" ) elif ("norm2" in new_name) and ("layernorm2" not in new_name): __snake_case : Dict = new_name.replace("""norm2""" , """batchnorm_after""" ) if "proj" in new_name: __snake_case : str = new_name.replace("""proj""" , """projection""" ) if "dist_head" in new_name: __snake_case : str = new_name.replace("""dist_head""" , """distillation_classifier""" ) elif "head" in new_name: __snake_case : Union[str, Any] = new_name.replace("""head""" , """classifier""" ) elif "patch_embed" in new_name: __snake_case : int = """efficientformer.""" + new_name elif new_name == "norm.weight" or new_name == "norm.bias": __snake_case : Tuple = new_name.replace("""norm""" , """layernorm""" ) __snake_case : Dict = """efficientformer.""" + new_name else: __snake_case : str = """efficientformer.encoder.""" + new_name return new_name def _a ( _lowerCamelCase , _lowerCamelCase ) -> str: """simple docstring""" for key in checkpoint.copy().keys(): __snake_case : int = checkpoint.pop(_lowerCamelCase ) __snake_case : int = val return checkpoint def _a ( ) -> List[str]: """simple docstring""" __snake_case : Optional[int] = """http://images.cocodataset.org/val2017/000000039769.jpg""" __snake_case : List[str] = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return image def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> str: """simple docstring""" __snake_case : List[Any] = torch.load(_lowerCamelCase , map_location="""cpu""" )["""model"""] __snake_case : Union[str, Any] = EfficientFormerConfig.from_json_file(_lowerCamelCase ) __snake_case : Dict = EfficientFormerForImageClassificationWithTeacher(_lowerCamelCase ) __snake_case : List[Any] = """_""".join(checkpoint_path.split("""/""" )[-1].split(""".""" )[0].split("""_""" )[:-1] ) __snake_case : Dict = config.depths[-1] - config.num_metaad_blocks + 1 __snake_case : Any = convert_torch_checkpoint(_lowerCamelCase , _lowerCamelCase ) model.load_state_dict(_lowerCamelCase ) model.eval() __snake_case : Optional[int] = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } # prepare image __snake_case : List[str] = prepare_img() __snake_case : Tuple = 256 __snake_case : Dict = 224 __snake_case : Any = EfficientFormerImageProcessor( size={"""shortest_edge""": image_size} , crop_size={"""height""": crop_size, """width""": crop_size} , resample=pillow_resamplings["""bicubic"""] , ) __snake_case : Dict = processor(images=_lowerCamelCase , return_tensors="""pt""" ).pixel_values # original processing pipeline __snake_case : int = Compose( [ Resize(_lowerCamelCase , interpolation=pillow_resamplings["""bicubic"""] ), CenterCrop(_lowerCamelCase ), ToTensor(), Normalize(_lowerCamelCase , _lowerCamelCase ), ] ) __snake_case : int = image_transforms(_lowerCamelCase ).unsqueeze(0 ) assert torch.allclose(_lowerCamelCase , _lowerCamelCase ) __snake_case : Dict = model(_lowerCamelCase ) __snake_case : Dict = outputs.logits __snake_case : List[str] = (1, 1000) if "l1" in model_name: __snake_case : Dict = torch.Tensor( [-0.13_12, 0.43_53, -1.04_99, -0.51_24, 0.41_83, -0.67_93, -1.37_77, -0.08_93, -0.73_58, -2.43_28] ) assert torch.allclose(logits[0, :10] , _lowerCamelCase , atol=1E-3 ) assert logits.shape == expected_shape elif "l3" in model_name: __snake_case : int = torch.Tensor( [-1.31_50, -1.54_56, -1.25_56, -0.84_96, -0.71_27, -0.78_97, -0.97_28, -0.30_52, 0.37_51, -0.31_27] ) assert torch.allclose(logits[0, :10] , _lowerCamelCase , atol=1E-3 ) assert logits.shape == expected_shape elif "l7" in model_name: __snake_case : int = torch.Tensor( [-1.02_83, -1.41_31, -0.56_44, -1.31_15, -0.57_85, -1.20_49, -0.75_28, 0.19_92, -0.38_22, -0.08_78] ) assert logits.shape == expected_shape else: raise ValueError( F'''Unknown model checkpoint: {checkpoint_path}. Supported version of efficientformer are l1, l3 and l7''' ) # Save Checkpoints Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase ) model.save_pretrained(_lowerCamelCase ) print(F'''Checkpoint successfuly converted. Model saved at {pytorch_dump_path}''' ) processor.save_pretrained(_lowerCamelCase ) print(F'''Processor successfuly saved at {pytorch_dump_path}''' ) if push_to_hub: print("""Pushing model to the hub...""" ) model.push_to_hub( repo_id=F'''Bearnardd/{pytorch_dump_path}''' , commit_message="""Add model""" , use_temp_dir=_lowerCamelCase , ) processor.push_to_hub( repo_id=F'''Bearnardd/{pytorch_dump_path}''' , commit_message="""Add image processor""" , use_temp_dir=_lowerCamelCase , ) if __name__ == "__main__": __UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( "--pytorch_model_path", default=None, type=str, required=True, help="Path to EfficientFormer pytorch checkpoint.", ) parser.add_argument( "--config_file", default=None, type=str, required=True, help="The json file for EfficientFormer model config.", ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument("--push_to_hub", action="store_true", help="Push model and image processor to the hub") parser.add_argument( "--no-push_to_hub", dest="push_to_hub", action="store_false", help="Do not push model and image processor to the hub", ) parser.set_defaults(push_to_hub=True) __UpperCamelCase = parser.parse_args() convert_efficientformer_checkpoint( checkpoint_path=args.pytorch_model_path, efficientformer_config_file=args.config_file, pytorch_dump_path=args.pytorch_dump_path, push_to_hub=args.push_to_hub, )
26
'''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 MobileViTImageProcessor class lowerCAmelCase ( unittest.TestCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=18 , __SCREAMING_SNAKE_CASE=30 , __SCREAMING_SNAKE_CASE=400 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , ) -> Tuple: '''simple docstring''' __snake_case = size if size is not None else {'''shortest_edge''': 20} __snake_case = crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} __snake_case = parent __snake_case = batch_size __snake_case = num_channels __snake_case = image_size __snake_case = min_resolution __snake_case = max_resolution __snake_case = do_resize __snake_case = size __snake_case = do_center_crop __snake_case = crop_size __snake_case = do_flip_channel_order def lowerCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_flip_channel_order": self.do_flip_channel_order, } @require_torch @require_vision class lowerCAmelCase ( __lowerCAmelCase , unittest.TestCase): __lowercase : Union[str, Any] = MobileViTImageProcessor if is_vision_available() else None def lowerCAmelCase ( self ) -> str: '''simple docstring''' __snake_case = MobileViTImageProcessingTester(self ) @property def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def lowerCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_resize''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''size''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_center_crop''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''center_crop''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_flip_channel_order''' ) ) def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 20} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) __snake_case = 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 lowerCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' pass def lowerCAmelCase ( self ) -> str: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __snake_case = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input __snake_case = 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 __snake_case = image_processing(__SCREAMING_SNAKE_CASE , 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 lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __snake_case = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , numpify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) # Test not batched input __snake_case = 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 __snake_case = image_processing(__SCREAMING_SNAKE_CASE , 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 lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __snake_case = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , torchify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) # Test not batched input __snake_case = 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 __snake_case = image_processing(__SCREAMING_SNAKE_CASE , 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'''], ) , )
24
0
from typing import TYPE_CHECKING from ...utils import _LazyModule __A : List[str] = {"tokenization_byt5": ["ByT5Tokenizer"]} if TYPE_CHECKING: from .tokenization_byta import ByTaTokenizer else: import sys __A : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
27
'''simple docstring''' from typing import Optional import pyspark from .. import Features, NamedSplit from ..download import DownloadMode from ..packaged_modules.spark.spark import Spark from .abc import AbstractDatasetReader class lowerCAmelCase ( __lowerCAmelCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = "arrow" , **__SCREAMING_SNAKE_CASE , ) -> Union[str, Any]: '''simple docstring''' super().__init__( split=__SCREAMING_SNAKE_CASE , features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE , keep_in_memory=__SCREAMING_SNAKE_CASE , streaming=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) __snake_case = load_from_cache_file __snake_case = file_format __snake_case = Spark( df=__SCREAMING_SNAKE_CASE , features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE , working_dir=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) def lowerCAmelCase ( self ) -> str: '''simple docstring''' if self.streaming: return self.builder.as_streaming_dataset(split=self.split ) __snake_case = None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD self.builder.download_and_prepare( download_mode=__SCREAMING_SNAKE_CASE , file_format=self._file_format , ) return self.builder.as_dataset(split=self.split )
24
0
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class _a ( unittest.TestCase ): '''simple docstring''' def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = [[1, 2, 4], [1, 2, 3, 4]] SCREAMING_SNAKE_CASE : List[Any] = DisjunctiveConstraint(A ) self.assertTrue(isinstance(dc.token_ids, A ) ) with self.assertRaises(A ): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]] ) ) with self.assertRaises(A ): DisjunctiveConstraint([torch.LongTensor([1, 2, 4] ), torch.LongTensor([1, 2, 3, 4, 5] )] ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(A ): DisjunctiveConstraint(A ) # fails here def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = [[1, 2, 3], [1, 2, 4]] SCREAMING_SNAKE_CASE : List[str] = DisjunctiveConstraint(A ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : int = dc.update(1 ) SCREAMING_SNAKE_CASE : Dict = stepped is True and completed is False and reset is False self.assertTrue(A ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = dc.update(2 ) SCREAMING_SNAKE_CASE : int = stepped is True and completed is False and reset is False self.assertTrue(A ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[Any] = dc.update(3 ) SCREAMING_SNAKE_CASE : Union[str, Any] = stepped is True and completed is True and reset is False self.assertTrue(A ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3] ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] SCREAMING_SNAKE_CASE : Optional[Any] = DisjunctiveConstraint(A ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[str] = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[Any] = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = dc.update(4 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2, 4] ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5] ) dc.reset() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : str = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 3 ) self.assertTrue(dc.current_seq == [1] ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[str] = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 2 ) self.assertTrue(dc.current_seq == [1, 2] ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.remaining() == 0 ) self.assertTrue(dc.current_seq == [1, 2, 5] )
28
'''simple docstring''' import json from typing import Dict, List, Optional, Tuple, Union from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_led import LEDTokenizer UpperCAmelCase_ : Any = logging.get_logger(__name__) UpperCAmelCase_ : str = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} UpperCAmelCase_ : str = { '''vocab_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json''', }, '''merges_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt''', }, '''tokenizer_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json''', }, } UpperCAmelCase_ : Union[str, Any] = { '''allenai/led-base-16384''': 1_6_3_8_4, } class lowerCAmelCase ( __lowerCAmelCase): __lowercase : Dict = VOCAB_FILES_NAMES __lowercase : Dict = PRETRAINED_VOCAB_FILES_MAP __lowercase : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Union[str, Any] = LEDTokenizer __lowercase : int = ['''input_ids''', '''attention_mask'''] def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="replace" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="<mask>" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , **__SCREAMING_SNAKE_CASE , ) -> List[Any]: '''simple docstring''' super().__init__( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , errors=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) __snake_case = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: __snake_case = getattr(__SCREAMING_SNAKE_CASE , pre_tok_state.pop('''type''' ) ) __snake_case = add_prefix_space __snake_case = pre_tok_class(**__SCREAMING_SNAKE_CASE ) __snake_case = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` __snake_case = '''post_processor''' __snake_case = getattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if tokenizer_component_instance: __snake_case = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: __snake_case = tuple(state['''sep'''] ) if "cls" in state: __snake_case = tuple(state['''cls'''] ) __snake_case = False if state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: __snake_case = add_prefix_space __snake_case = True if state.get('''trim_offsets''' , __SCREAMING_SNAKE_CASE ) != trim_offsets: __snake_case = trim_offsets __snake_case = True if changes_to_apply: __snake_case = getattr(__SCREAMING_SNAKE_CASE , state.pop('''type''' ) ) __snake_case = component_class(**__SCREAMING_SNAKE_CASE ) setattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @property # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED def lowerCAmelCase ( self ) -> str: '''simple docstring''' if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' __snake_case = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else value __snake_case = value def lowerCAmelCase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> BatchEncoding: '''simple docstring''' __snake_case = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' '''to use it with pretokenized inputs.''' ) return super()._batch_encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> BatchEncoding: '''simple docstring''' __snake_case = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' '''to use it with pretokenized inputs.''' ) return super()._encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) -> Tuple[str]: '''simple docstring''' __snake_case = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) -> List[Any]: '''simple docstring''' __snake_case = [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 , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) -> List[int]: '''simple docstring''' __snake_case = [self.sep_token_id] __snake_case = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , ) -> dict: '''simple docstring''' __snake_case = super()._pad( encoded_inputs=__SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , padding_strategy=__SCREAMING_SNAKE_CASE , pad_to_multiple_of=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , ) # Load from model defaults if return_attention_mask is None: __snake_case = '''attention_mask''' in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: __snake_case = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. __snake_case = len(encoded_inputs['''global_attention_mask'''] ) != len(__SCREAMING_SNAKE_CASE ) if needs_to_be_padded: __snake_case = len(__SCREAMING_SNAKE_CASE ) - len(encoded_inputs['''global_attention_mask'''] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` __snake_case = ( encoded_inputs['''global_attention_mask'''] + [-1] * difference ) elif self.padding_side == "left": __snake_case = [-1] * difference + encoded_inputs[ '''global_attention_mask''' ] else: raise ValueError('''Invalid padding strategy:''' + str(self.padding_side ) ) return encoded_inputs
24
0
"""simple docstring""" import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES from ...utils import logging from ..auto import CONFIG_MAPPING A_ = logging.get_logger(__name__) A_ = { """salesforce/blip2-opt-2.7b""": """https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json""", } class __lowerCamelCase ( lowerCAmelCase ): a__: Optional[int] = 'blip_2_vision_model' def __init__( self , UpperCAmelCase=1408 , UpperCAmelCase=6144 , UpperCAmelCase=39 , UpperCAmelCase=16 , UpperCAmelCase=224 , UpperCAmelCase=14 , UpperCAmelCase="gelu" , UpperCAmelCase=0.0_0_0_0_1 , UpperCAmelCase=0.0 , UpperCAmelCase=1e-1_0 , UpperCAmelCase=True , **UpperCAmelCase , ): super().__init__(**UpperCAmelCase ) lowerCamelCase_ = hidden_size lowerCamelCase_ = intermediate_size lowerCamelCase_ = num_hidden_layers lowerCamelCase_ = num_attention_heads lowerCamelCase_ = patch_size lowerCamelCase_ = image_size lowerCamelCase_ = initializer_range lowerCamelCase_ = attention_dropout lowerCamelCase_ = layer_norm_eps lowerCamelCase_ = hidden_act lowerCamelCase_ = qkv_bias @classmethod def UpperCAmelCase__ ( cls , UpperCAmelCase , **UpperCAmelCase ): cls._set_token_in_kwargs(UpperCAmelCase ) lowerCamelCase_ , lowerCamelCase_ = cls.get_config_dict(UpperCAmelCase , **UpperCAmelCase ) # get the vision config dict if we are loading from Blip2Config if config_dict.get('''model_type''' ) == "blip-2": lowerCamelCase_ = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f"You are using a model of type {config_dict['model_type']} to instantiate a model of type " f"{cls.model_type}. This is not supported for all configurations of models and can yield errors." ) return cls.from_dict(UpperCAmelCase , **UpperCAmelCase ) class __lowerCamelCase ( lowerCAmelCase ): a__: Optional[int] = 'blip_2_qformer' def __init__( self , UpperCAmelCase=3_0522 , UpperCAmelCase=768 , UpperCAmelCase=12 , UpperCAmelCase=12 , UpperCAmelCase=3072 , UpperCAmelCase="gelu" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=512 , UpperCAmelCase=0.0_2 , UpperCAmelCase=1e-1_2 , UpperCAmelCase=0 , UpperCAmelCase="absolute" , UpperCAmelCase=2 , UpperCAmelCase=1408 , **UpperCAmelCase , ): super().__init__(pad_token_id=UpperCAmelCase , **UpperCAmelCase ) lowerCamelCase_ = vocab_size lowerCamelCase_ = hidden_size lowerCamelCase_ = num_hidden_layers lowerCamelCase_ = num_attention_heads lowerCamelCase_ = hidden_act lowerCamelCase_ = intermediate_size lowerCamelCase_ = hidden_dropout_prob lowerCamelCase_ = attention_probs_dropout_prob lowerCamelCase_ = max_position_embeddings lowerCamelCase_ = initializer_range lowerCamelCase_ = layer_norm_eps lowerCamelCase_ = position_embedding_type lowerCamelCase_ = cross_attention_frequency lowerCamelCase_ = encoder_hidden_size @classmethod def UpperCAmelCase__ ( cls , UpperCAmelCase , **UpperCAmelCase ): cls._set_token_in_kwargs(UpperCAmelCase ) lowerCamelCase_ , lowerCamelCase_ = cls.get_config_dict(UpperCAmelCase , **UpperCAmelCase ) # get the qformer config dict if we are loading from Blip2Config if config_dict.get('''model_type''' ) == "blip-2": lowerCamelCase_ = config_dict['''qformer_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f"You are using a model of type {config_dict['model_type']} to instantiate a model of type " f"{cls.model_type}. This is not supported for all configurations of models and can yield errors." ) return cls.from_dict(UpperCAmelCase , **UpperCAmelCase ) class __lowerCamelCase ( lowerCAmelCase ): a__: Union[str, Any] = 'blip-2' a__: str = True def __init__( self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=32 , **UpperCAmelCase ): super().__init__(**UpperCAmelCase ) if vision_config is None: lowerCamelCase_ = {} logger.info('''vision_config is None. initializing the Blip2VisionConfig with default values.''' ) if qformer_config is None: lowerCamelCase_ = {} logger.info('''qformer_config is None. Initializing the Blip2QFormerConfig with default values.''' ) if text_config is None: lowerCamelCase_ = {} logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).''' ) lowerCamelCase_ = BlipaVisionConfig(**UpperCAmelCase ) lowerCamelCase_ = BlipaQFormerConfig(**UpperCAmelCase ) lowerCamelCase_ = text_config['''model_type'''] if '''model_type''' in text_config else '''opt''' lowerCamelCase_ = CONFIG_MAPPING[text_model_type](**UpperCAmelCase ) lowerCamelCase_ = self.text_config.tie_word_embeddings lowerCamelCase_ = self.text_config.is_encoder_decoder lowerCamelCase_ = num_query_tokens lowerCamelCase_ = self.vision_config.hidden_size lowerCamelCase_ = self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES lowerCamelCase_ = 1.0 lowerCamelCase_ = 0.0_2 @classmethod def UpperCAmelCase__ ( cls , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase , ): return cls( vision_config=vision_config.to_dict() , qformer_config=qformer_config.to_dict() , text_config=text_config.to_dict() , **UpperCAmelCase , ) def UpperCAmelCase__ ( self ): lowerCamelCase_ = copy.deepcopy(self.__dict__ ) lowerCamelCase_ = self.vision_config.to_dict() lowerCamelCase_ = self.qformer_config.to_dict() lowerCamelCase_ = self.text_config.to_dict() lowerCamelCase_ = self.__class__.model_type return output
29
'''simple docstring''' from collections import deque def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> Optional[int]: '''simple docstring''' __snake_case = len(_lowerCamelCase ) __snake_case = deque() __snake_case = [False for _ in range(_lowerCamelCase )] __snake_case = [-1 for _ in range(_lowerCamelCase )] __snake_case = index_of[:] def strong_connect(_lowerCamelCase : Any , _lowerCamelCase : Dict , _lowerCamelCase : List[str] ): __snake_case = index # the number when this node is seen __snake_case = index # lowest rank node reachable from here index += 1 stack.append(_lowerCamelCase ) __snake_case = True for w in g[v]: if index_of[w] == -1: __snake_case = strong_connect(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) __snake_case = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) elif on_stack[w]: __snake_case = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) if lowlink_of[v] == index_of[v]: __snake_case = [] __snake_case = stack.pop() __snake_case = False component.append(_lowerCamelCase ) while w != v: __snake_case = stack.pop() __snake_case = False component.append(_lowerCamelCase ) components.append(_lowerCamelCase ) return index __snake_case = [] for v in range(_lowerCamelCase ): if index_of[v] == -1: strong_connect(_lowerCamelCase , 0 , _lowerCamelCase ) return components def _UpperCamelCase (_lowerCamelCase : Tuple , _lowerCamelCase : Optional[Any] )-> Dict: '''simple docstring''' __snake_case = [[] for _ in range(_lowerCamelCase )] for u, v in edges: g[u].append(_lowerCamelCase ) return g if __name__ == "__main__": # Test UpperCAmelCase_ : List[str] = 7 UpperCAmelCase_ : int = [0, 0, 1, 2, 3, 3, 4, 4, 6] UpperCAmelCase_ : Dict = [1, 3, 2, 0, 1, 4, 5, 6, 5] UpperCAmelCase_ : List[str] = [(u, v) for u, v in zip(source, target)] UpperCAmelCase_ : Tuple = create_graph(n_vertices, edges) assert [[5], [6], [4], [3, 2, 1, 0]] == tarjan(g)
24
0
from __future__ import annotations def lowerCamelCase__ ( _lowercase , _lowercase ): '''simple docstring''' UpperCAmelCase_ : str = 0 UpperCAmelCase_ : Tuple = len(_lowercase ) - 1 while i < j: if nums[i] + nums[j] == target: return [i, j] elif nums[i] + nums[j] < target: UpperCAmelCase_ : Tuple = i + 1 else: UpperCAmelCase_ : Union[str, Any] = j - 1 return [] if __name__ == "__main__": import doctest doctest.testmod() print(F"""{two_pointer([2, 7, 11, 15], 9) = }""")
30
'''simple docstring''' import unittest from transformers import BarthezTokenizer, BarthezTokenizerFast, BatchEncoding from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers @require_sentencepiece @slow # see https://github.com/huggingface/transformers/issues/11457 class lowerCAmelCase ( __lowerCAmelCase , unittest.TestCase): __lowercase : int = BarthezTokenizer __lowercase : Any = BarthezTokenizerFast __lowercase : Dict = True __lowercase : Optional[int] = True def lowerCAmelCase ( self ) -> Dict: '''simple docstring''' super().setUp() __snake_case = BarthezTokenizerFast.from_pretrained('''moussaKam/mbarthez''' ) tokenizer.save_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname , legacy_format=__SCREAMING_SNAKE_CASE ) __snake_case = tokenizer def lowerCAmelCase ( self ) -> Dict: '''simple docstring''' __snake_case = '''<pad>''' __snake_case = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<s>''' ) self.assertEqual(vocab_keys[1] , '''<pad>''' ) self.assertEqual(vocab_keys[-1] , '''<mask>''' ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 10_1122 ) def lowerCAmelCase ( self ) -> Any: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 10_1122 ) @require_torch def lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] __snake_case = [0, 57, 3018, 7_0307, 91, 2] __snake_case = self.tokenizer( __SCREAMING_SNAKE_CASE , max_length=len(__SCREAMING_SNAKE_CASE ) , padding=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertEqual((2, 6) , batch.input_ids.shape ) self.assertEqual((2, 6) , batch.attention_mask.shape ) __snake_case = batch.input_ids.tolist()[0] self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' if not self.test_rust_tokenizer: return __snake_case = self.get_tokenizer() __snake_case = self.get_rust_tokenizer() __snake_case = '''I was born in 92000, and this is falsé.''' __snake_case = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) __snake_case = rust_tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __snake_case = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) __snake_case = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __snake_case = self.get_rust_tokenizer() __snake_case = tokenizer.encode(__SCREAMING_SNAKE_CASE ) __snake_case = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = {'''input_ids''': [[0, 490, 1_4328, 4507, 354, 47, 4_3669, 95, 25, 7_8117, 2_0215, 1_9779, 190, 22, 400, 4, 3_5343, 8_0310, 603, 86, 2_4937, 105, 3_3438, 9_4762, 196, 3_9642, 7, 15, 1_5933, 173, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1_0534, 87, 25, 66, 3358, 196, 5_5289, 8, 8_2961, 81, 2204, 7_5203, 7, 15, 763, 1_2956, 216, 178, 1_4328, 9595, 1377, 6_9693, 7, 448, 7_1021, 196, 1_8106, 1437, 1_3974, 108, 9083, 4, 4_9315, 7, 39, 86, 1326, 2793, 4_6333, 4, 448, 196, 7_4588, 7, 4_9315, 7, 39, 21, 822, 3_8470, 74, 21, 6_6723, 6_2480, 8, 2_2050, 5, 2]], '''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, 0, 0, 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # moussaKam/mbarthez is a french model. So we also use french texts. __snake_case = [ '''Le transformeur est un modèle d\'apprentissage profond introduit en 2017, ''' '''utilisé principalement dans le domaine du traitement automatique des langues (TAL).''', '''À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus ''' '''pour gérer des données séquentielles, telles que le langage naturel, pour des tâches ''' '''telles que la traduction et la synthèse de texte.''', ] self.tokenizer_integration_test_util( expected_encoding=__SCREAMING_SNAKE_CASE , model_name='''moussaKam/mbarthez''' , revision='''c2e4ecbca5e3cd2c37fe1ac285ca4fbdf1366fb6''' , sequences=__SCREAMING_SNAKE_CASE , )
24
0
from functools import reduce lowerCamelCase__ : Any = ( '73167176531330624919225119674426574742355349194934' '96983520312774506326239578318016984801869478851843' '85861560789112949495459501737958331952853208805511' '12540698747158523863050715693290963295227443043557' '66896648950445244523161731856403098711121722383113' '62229893423380308135336276614282806444486645238749' '30358907296290491560440772390713810515859307960866' '70172427121883998797908792274921901699720888093776' '65727333001053367881220235421809751254540594752243' '52584907711670556013604839586446706324415722155397' '53697817977846174064955149290862569321978468622482' '83972241375657056057490261407972968652414535100474' '82166370484403199890008895243450658541227588666881' '16427171479924442928230863465674813919123162824586' '17866458359124566529476545682848912883142607690042' '24219022671055626321111109370544217506941658960408' '07198403850962455444362981230987879927244284909188' '84580156166097919133875499200524063689912560717606' '05886116467109405077541002256983155200055935729725' '71636269561882670428252483600823257530420752963450' ) def UpperCAmelCase_ ( __UpperCAmelCase : str = N ) -> int: return max( # mypy cannot properly interpret reduce int(reduce(lambda __UpperCAmelCase , __UpperCAmelCase : str(int(__UpperCAmelCase ) * int(__UpperCAmelCase ) ) , n[i : i + 13] ) ) for i in range(len(__UpperCAmelCase ) - 12 ) ) if __name__ == "__main__": print(f'''{solution() = }''')
31
'''simple docstring''' import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class lowerCAmelCase ( unittest.TestCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __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=32 , __SCREAMING_SNAKE_CASE=5 , __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=4 , ) -> Any: '''simple docstring''' __snake_case = parent __snake_case = batch_size __snake_case = seq_length __snake_case = is_training __snake_case = use_attention_mask __snake_case = use_token_type_ids __snake_case = use_labels __snake_case = vocab_size __snake_case = hidden_size __snake_case = num_hidden_layers __snake_case = num_attention_heads __snake_case = intermediate_size __snake_case = hidden_act __snake_case = hidden_dropout_prob __snake_case = attention_probs_dropout_prob __snake_case = max_position_embeddings __snake_case = type_vocab_size __snake_case = type_sequence_label_size __snake_case = initializer_range __snake_case = num_choices def lowerCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' __snake_case = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __snake_case = None if self.use_attention_mask: __snake_case = random_attention_mask([self.batch_size, self.seq_length] ) __snake_case = None if self.use_token_type_ids: __snake_case = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __snake_case = RoFormerConfig( 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=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = self.prepare_config_and_inputs() __snake_case , __snake_case , __snake_case , __snake_case = config_and_inputs __snake_case = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_flax class lowerCAmelCase ( __lowerCAmelCase , unittest.TestCase): __lowercase : Tuple = True __lowercase : Optional[int] = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = FlaxRoFormerModelTester(self ) @slow def lowerCAmelCase ( self ) -> List[Any]: '''simple docstring''' for model_class_name in self.all_model_classes: __snake_case = model_class_name.from_pretrained('''junnyu/roformer_chinese_small''' , from_pt=__SCREAMING_SNAKE_CASE ) __snake_case = model(np.ones((1, 1) ) ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) @require_flax class lowerCAmelCase ( unittest.TestCase): @slow def lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = FlaxRoFormerForMaskedLM.from_pretrained('''junnyu/roformer_chinese_base''' ) __snake_case = jnp.array([[0, 1, 2, 3, 4, 5]] ) __snake_case = model(__SCREAMING_SNAKE_CASE )[0] __snake_case = 5_0000 __snake_case = (1, 6, vocab_size) self.assertEqual(output.shape , __SCREAMING_SNAKE_CASE ) __snake_case = jnp.array( [[[-0.1_205, -1.0_265, 0.2_922], [-1.5_134, 0.1_974, 0.1_519], [-5.0_135, -3.9_003, -0.8_404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , __SCREAMING_SNAKE_CASE , atol=1E-4 ) )
24
0
def A__ ( SCREAMING_SNAKE_CASE_ : list , SCREAMING_SNAKE_CASE_ : list , SCREAMING_SNAKE_CASE_ : int ) -> list: """simple docstring""" _UpperCAmelCase = len(SCREAMING_SNAKE_CASE_ ) _UpperCAmelCase = [[0] * n for i in range(SCREAMING_SNAKE_CASE_ )] for i in range(SCREAMING_SNAKE_CASE_ ): _UpperCAmelCase = y_points[i] for i in range(2 , SCREAMING_SNAKE_CASE_ ): for j in range(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): _UpperCAmelCase = ( (xa - x_points[j - i + 1]) * q[j][i - 1] - (xa - x_points[j]) * q[j - 1][i - 1] ) / (x_points[j] - x_points[j - i + 1]) return [q[n - 1][n - 1], q] if __name__ == "__main__": import doctest doctest.testmod()
32
'''simple docstring''' 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 ()-> int: '''simple docstring''' __snake_case = '''https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png''' __snake_case = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ).convert('''RGB''' ) return image def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> List[Any]: '''simple docstring''' __snake_case = [] # fmt: off # vision encoder rename_keys.append(('''visual_encoder.cls_token''', '''vision_model.embeddings.class_embedding''') ) rename_keys.append(('''visual_encoder.pos_embed''', '''vision_model.embeddings.position_embedding''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.weight''', '''vision_model.embeddings.patch_embedding.weight''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.bias''', '''vision_model.embeddings.patch_embedding.bias''') ) rename_keys.append(('''ln_vision.weight''', '''vision_model.post_layernorm.weight''') ) rename_keys.append(('''ln_vision.bias''', '''vision_model.post_layernorm.bias''') ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.weight''', f'''vision_model.encoder.layers.{i}.layer_norm1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.bias''', f'''vision_model.encoder.layers.{i}.layer_norm1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.weight''', f'''vision_model.encoder.layers.{i}.layer_norm2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.bias''', f'''vision_model.encoder.layers.{i}.layer_norm2.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.qkv.weight''', f'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.weight''', f'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',) ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.bias''', f'''vision_model.encoder.layers.{i}.self_attn.projection.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc2.bias''') ) # QFormer rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.weight''', '''qformer.layernorm.weight''') ) rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.bias''', '''qformer.layernorm.bias''') ) # fmt: on return rename_keys def _UpperCamelCase (_lowerCamelCase : Dict , _lowerCamelCase : List[str] , _lowerCamelCase : Optional[int] )-> Tuple: '''simple docstring''' __snake_case = dct.pop(_lowerCamelCase ) __snake_case = val def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : Tuple )-> str: '''simple docstring''' for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases __snake_case = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.q_bias''' ) __snake_case = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict __snake_case = torch.cat((q_bias, torch.zeros_like(_lowerCamelCase , requires_grad=_lowerCamelCase ), v_bias) ) __snake_case = qkv_bias def _UpperCamelCase (_lowerCamelCase : Tuple , _lowerCamelCase : Tuple )-> Dict: '''simple docstring''' __snake_case = 3_64 if '''coco''' in model_name else 2_24 __snake_case = BlipaVisionConfig(image_size=_lowerCamelCase ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: __snake_case = OPTConfig.from_pretrained('''facebook/opt-2.7b''' , eos_token_id=_lowerCamelCase ).to_dict() elif "opt-6.7b" in model_name: __snake_case = OPTConfig.from_pretrained('''facebook/opt-6.7b''' , eos_token_id=_lowerCamelCase ).to_dict() elif "t5-xl" in model_name: __snake_case = TaConfig.from_pretrained('''google/flan-t5-xl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: __snake_case = TaConfig.from_pretrained('''google/flan-t5-xxl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() __snake_case = BlipaConfig(vision_config=_lowerCamelCase , text_config=_lowerCamelCase ) return config, image_size @torch.no_grad() def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : Union[str, Any]=None , _lowerCamelCase : Any=False )-> Dict: '''simple docstring''' __snake_case = ( AutoTokenizer.from_pretrained('''facebook/opt-2.7b''' ) if '''opt''' in model_name else AutoTokenizer.from_pretrained('''google/flan-t5-xl''' ) ) __snake_case = tokenizer('''\n''' , add_special_tokens=_lowerCamelCase ).input_ids[0] __snake_case , __snake_case = get_blipa_config(_lowerCamelCase , eos_token_id=_lowerCamelCase ) __snake_case = BlipaForConditionalGeneration(_lowerCamelCase ).eval() __snake_case = { '''blip2-opt-2.7b''': ('''blip2_opt''', '''pretrain_opt2.7b'''), '''blip2-opt-6.7b''': ('''blip2_opt''', '''pretrain_opt6.7b'''), '''blip2-opt-2.7b-coco''': ('''blip2_opt''', '''caption_coco_opt2.7b'''), '''blip2-opt-6.7b-coco''': ('''blip2_opt''', '''caption_coco_opt6.7b'''), '''blip2-flan-t5-xl''': ('''blip2_t5''', '''pretrain_flant5xl'''), '''blip2-flan-t5-xl-coco''': ('''blip2_t5''', '''caption_coco_flant5xl'''), '''blip2-flan-t5-xxl''': ('''blip2_t5''', '''pretrain_flant5xxl'''), } __snake_case , __snake_case = model_name_to_original[model_name] # load original model print('''Loading original model...''' ) __snake_case = '''cuda''' if torch.cuda.is_available() else '''cpu''' __snake_case , __snake_case , __snake_case = load_model_and_preprocess( name=_lowerCamelCase , model_type=_lowerCamelCase , is_eval=_lowerCamelCase , device=_lowerCamelCase ) original_model.eval() print('''Done!''' ) # update state dict keys __snake_case = original_model.state_dict() __snake_case = create_rename_keys(_lowerCamelCase ) for src, dest in rename_keys: rename_key(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): __snake_case = state_dict.pop(_lowerCamelCase ) if key.startswith('''Qformer.bert''' ): __snake_case = key.replace('''Qformer.bert''' , '''qformer''' ) if "attention.self" in key: __snake_case = key.replace('''self''' , '''attention''' ) if "opt_proj" in key: __snake_case = key.replace('''opt_proj''' , '''language_projection''' ) if "t5_proj" in key: __snake_case = key.replace('''t5_proj''' , '''language_projection''' ) if key.startswith('''opt''' ): __snake_case = key.replace('''opt''' , '''language''' ) if key.startswith('''t5''' ): __snake_case = key.replace('''t5''' , '''language''' ) __snake_case = val # read in qv biases read_in_q_v_bias(_lowerCamelCase , _lowerCamelCase ) __snake_case , __snake_case = hf_model.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) assert len(_lowerCamelCase ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] __snake_case = load_demo_image() __snake_case = vis_processors['''eval'''](_lowerCamelCase ).unsqueeze(0 ).to(_lowerCamelCase ) __snake_case = tokenizer(['''\n'''] , return_tensors='''pt''' ).input_ids.to(_lowerCamelCase ) # create processor __snake_case = BlipImageProcessor( size={'''height''': image_size, '''width''': image_size} , image_mean=_lowerCamelCase , image_std=_lowerCamelCase ) __snake_case = BlipaProcessor(image_processor=_lowerCamelCase , tokenizer=_lowerCamelCase ) __snake_case = processor(images=_lowerCamelCase , return_tensors='''pt''' ).pixel_values.to(_lowerCamelCase ) # make sure processor creates exact same pixel values assert torch.allclose(_lowerCamelCase , _lowerCamelCase ) original_model.to(_lowerCamelCase ) hf_model.to(_lowerCamelCase ) with torch.no_grad(): if "opt" in model_name: __snake_case = original_model({'''image''': original_pixel_values, '''text_input''': ['''''']} ).logits __snake_case = hf_model(_lowerCamelCase , _lowerCamelCase ).logits else: __snake_case = original_model( {'''image''': original_pixel_values, '''text_input''': ['''\n'''], '''text_output''': ['''\n''']} ).logits __snake_case = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -1_00 ) __snake_case = hf_model(_lowerCamelCase , _lowerCamelCase , labels=_lowerCamelCase ).logits assert original_logits.shape == logits.shape print('''First values of original logits:''' , original_logits[0, :3, :3] ) print('''First values of HF logits:''' , logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": __snake_case = torch.tensor( [[-41.5850, -4.4440, -8.9922], [-47.4322, -5.9143, -1.7340]] , device=_lowerCamelCase ) assert torch.allclose(logits[0, :3, :3] , _lowerCamelCase , atol=1E-4 ) elif model_name == "blip2-flan-t5-xl-coco": __snake_case = torch.tensor( [[-57.0109, -9.8967, -12.6280], [-68.6578, -12.7191, -10.5065]] , device=_lowerCamelCase ) else: # cast to same type __snake_case = logits.dtype assert torch.allclose(original_logits.to(_lowerCamelCase ) , _lowerCamelCase , atol=1E-2 ) print('''Looks ok!''' ) print('''Generating a caption...''' ) __snake_case = '''''' __snake_case = tokenizer(_lowerCamelCase , return_tensors='''pt''' ).input_ids.to(_lowerCamelCase ) __snake_case = original_model.generate({'''image''': original_pixel_values} ) __snake_case = hf_model.generate( _lowerCamelCase , _lowerCamelCase , do_sample=_lowerCamelCase , 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:''' , _lowerCamelCase ) __snake_case = input_ids.shape[1] __snake_case = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=_lowerCamelCase ) __snake_case = [text.strip() for text in output_text] print('''HF generation:''' , _lowerCamelCase ) if pytorch_dump_folder_path is not None: processor.save_pretrained(_lowerCamelCase ) hf_model.save_pretrained(_lowerCamelCase ) if push_to_hub: processor.push_to_hub(f'''nielsr/{model_name}''' ) hf_model.push_to_hub(f'''nielsr/{model_name}''' ) if __name__ == "__main__": UpperCAmelCase_ : Any = argparse.ArgumentParser() UpperCAmelCase_ : 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''', ) UpperCAmelCase_ : List[str] = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
24
0
from ..utils import DummyObject, requires_backends class __magic_name__ (metaclass=snake_case_ ): '''simple docstring''' __lowercase : Union[str, Any] = ['flax'] def __init__( self:str , *_a:Tuple , **_a:str ): requires_backends(self , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:str , *_a:Optional[int] , **_a:Optional[Any] ): requires_backends(cls , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:str , *_a:Union[str, Any] , **_a:List[str] ): requires_backends(cls , ['''flax'''] ) class __magic_name__ (metaclass=snake_case_ ): '''simple docstring''' __lowercase : List[Any] = ['flax'] def __init__( self:Dict , *_a:int , **_a:List[str] ): requires_backends(self , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:List[Any] , *_a:List[str] , **_a:List[Any] ): requires_backends(cls , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:Any , *_a:Tuple , **_a:List[str] ): requires_backends(cls , ['''flax'''] ) class __magic_name__ (metaclass=snake_case_ ): '''simple docstring''' __lowercase : Optional[int] = ['flax'] def __init__( self:List[Any] , *_a:Any , **_a:int ): requires_backends(self , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:Any , *_a:List[str] , **_a:Tuple ): requires_backends(cls , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:int , *_a:str , **_a:int ): requires_backends(cls , ['''flax'''] ) class __magic_name__ (metaclass=snake_case_ ): '''simple docstring''' __lowercase : Optional[Any] = ['flax'] def __init__( self:Tuple , *_a:List[Any] , **_a:Tuple ): requires_backends(self , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:Union[str, Any] , *_a:Any , **_a:int ): requires_backends(cls , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:List[Any] , *_a:Optional[int] , **_a:List[str] ): requires_backends(cls , ['''flax'''] ) class __magic_name__ (metaclass=snake_case_ ): '''simple docstring''' __lowercase : Optional[Any] = ['flax'] def __init__( self:List[str] , *_a:Any , **_a:int ): requires_backends(self , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:Any , *_a:Optional[Any] , **_a:Optional[int] ): requires_backends(cls , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:Optional[int] , *_a:Any , **_a:Tuple ): requires_backends(cls , ['''flax'''] ) class __magic_name__ (metaclass=snake_case_ ): '''simple docstring''' __lowercase : List[Any] = ['flax'] def __init__( self:Optional[Any] , *_a:int , **_a:List[str] ): requires_backends(self , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:Union[str, Any] , *_a:Tuple , **_a:Any ): requires_backends(cls , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:List[Any] , *_a:Any , **_a:Any ): requires_backends(cls , ['''flax'''] ) class __magic_name__ (metaclass=snake_case_ ): '''simple docstring''' __lowercase : Optional[Any] = ['flax'] def __init__( self:Any , *_a:Any , **_a:Tuple ): requires_backends(self , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:int , *_a:Dict , **_a:Union[str, Any] ): requires_backends(cls , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:int , *_a:int , **_a:List[str] ): requires_backends(cls , ['''flax'''] ) class __magic_name__ (metaclass=snake_case_ ): '''simple docstring''' __lowercase : Optional[Any] = ['flax'] def __init__( self:Any , *_a:Optional[int] , **_a:Any ): requires_backends(self , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:Optional[Any] , *_a:List[Any] , **_a:Tuple ): requires_backends(cls , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:List[str] , *_a:Any , **_a:List[Any] ): requires_backends(cls , ['''flax'''] ) class __magic_name__ (metaclass=snake_case_ ): '''simple docstring''' __lowercase : str = ['flax'] def __init__( self:Optional[Any] , *_a:Any , **_a:List[Any] ): requires_backends(self , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:Any , *_a:List[Any] , **_a:List[str] ): requires_backends(cls , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:str , *_a:Dict , **_a:List[str] ): requires_backends(cls , ['''flax'''] ) class __magic_name__ (metaclass=snake_case_ ): '''simple docstring''' __lowercase : List[str] = ['flax'] def __init__( self:int , *_a:Union[str, Any] , **_a:Tuple ): requires_backends(self , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:List[Any] , *_a:Optional[int] , **_a:int ): requires_backends(cls , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:Union[str, Any] , *_a:List[str] , **_a:Optional[Any] ): requires_backends(cls , ['''flax'''] ) class __magic_name__ (metaclass=snake_case_ ): '''simple docstring''' __lowercase : List[str] = ['flax'] def __init__( self:str , *_a:List[str] , **_a:List[Any] ): requires_backends(self , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:List[Any] , *_a:int , **_a:Union[str, Any] ): requires_backends(cls , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:Tuple , *_a:Optional[int] , **_a:List[str] ): requires_backends(cls , ['''flax'''] ) class __magic_name__ (metaclass=snake_case_ ): '''simple docstring''' __lowercase : Tuple = ['flax'] def __init__( self:Optional[Any] , *_a:Optional[int] , **_a:Optional[Any] ): requires_backends(self , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:Any , *_a:Any , **_a:Any ): requires_backends(cls , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:Any , *_a:Optional[Any] , **_a:Union[str, Any] ): requires_backends(cls , ['''flax'''] ) class __magic_name__ (metaclass=snake_case_ ): '''simple docstring''' __lowercase : List[Any] = ['flax'] def __init__( self:Optional[Any] , *_a:Optional[Any] , **_a:Any ): requires_backends(self , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:List[Any] , *_a:Union[str, Any] , **_a:Dict ): requires_backends(cls , ['''flax'''] ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls:Optional[int] , *_a:Any , **_a:str ): requires_backends(cls , ['''flax'''] )
33
'''simple docstring''' import warnings from typing import List, Optional, Tuple, Union import numpy as np import PIL import torch from ...models import UNetaDModel from ...schedulers import RePaintScheduler from ...utils import PIL_INTERPOLATION, logging, randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput UpperCAmelCase_ : Tuple = logging.get_logger(__name__) # pylint: disable=invalid-name def _UpperCamelCase (_lowerCamelCase : Union[List, PIL.Image.Image, torch.Tensor] )-> Optional[Any]: '''simple docstring''' warnings.warn( '''The preprocess method is deprecated and will be removed in a future version. Please''' ''' use VaeImageProcessor.preprocess instead''' , _lowerCamelCase , ) if isinstance(_lowerCamelCase , torch.Tensor ): return image elif isinstance(_lowerCamelCase , PIL.Image.Image ): __snake_case = [image] if isinstance(image[0] , PIL.Image.Image ): __snake_case , __snake_case = image[0].size __snake_case , __snake_case = (x - x % 8 for x in (w, h)) # resize to integer multiple of 8 __snake_case = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['''lanczos'''] ) )[None, :] for i in image] __snake_case = np.concatenate(_lowerCamelCase , axis=0 ) __snake_case = np.array(_lowerCamelCase ).astype(np.floataa ) / 255.0 __snake_case = image.transpose(0 , 3 , 1 , 2 ) __snake_case = 2.0 * image - 1.0 __snake_case = torch.from_numpy(_lowerCamelCase ) elif isinstance(image[0] , torch.Tensor ): __snake_case = torch.cat(_lowerCamelCase , dim=0 ) return image def _UpperCamelCase (_lowerCamelCase : Union[List, PIL.Image.Image, torch.Tensor] )-> Optional[Any]: '''simple docstring''' if isinstance(_lowerCamelCase , torch.Tensor ): return mask elif isinstance(_lowerCamelCase , PIL.Image.Image ): __snake_case = [mask] if isinstance(mask[0] , PIL.Image.Image ): __snake_case , __snake_case = mask[0].size __snake_case , __snake_case = (x - x % 32 for x in (w, h)) # resize to integer multiple of 32 __snake_case = [np.array(m.convert('''L''' ).resize((w, h) , resample=PIL_INTERPOLATION['''nearest'''] ) )[None, :] for m in mask] __snake_case = np.concatenate(_lowerCamelCase , axis=0 ) __snake_case = mask.astype(np.floataa ) / 255.0 __snake_case = 0 __snake_case = 1 __snake_case = torch.from_numpy(_lowerCamelCase ) elif isinstance(mask[0] , torch.Tensor ): __snake_case = torch.cat(_lowerCamelCase , dim=0 ) return mask class lowerCAmelCase ( __lowerCAmelCase): __lowercase : UNetaDModel __lowercase : RePaintScheduler def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) -> str: '''simple docstring''' super().__init__() self.register_modules(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) @torch.no_grad() def __call__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 250 , __SCREAMING_SNAKE_CASE = 0.0 , __SCREAMING_SNAKE_CASE = 10 , __SCREAMING_SNAKE_CASE = 10 , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = "pil" , __SCREAMING_SNAKE_CASE = True , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' __snake_case = image __snake_case = _preprocess_image(__SCREAMING_SNAKE_CASE ) __snake_case = original_image.to(device=self.device , dtype=self.unet.dtype ) __snake_case = _preprocess_mask(__SCREAMING_SNAKE_CASE ) __snake_case = mask_image.to(device=self.device , dtype=self.unet.dtype ) __snake_case = original_image.shape[0] # sample gaussian noise to begin the loop if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and len(__SCREAMING_SNAKE_CASE ) != batch_size: raise ValueError( F'''You have passed a list of generators of length {len(__SCREAMING_SNAKE_CASE )}, but requested an effective batch''' F''' size of {batch_size}. Make sure the batch size matches the length of the generators.''' ) __snake_case = original_image.shape __snake_case = randn_tensor(__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , device=self.device , dtype=self.unet.dtype ) # set step values self.scheduler.set_timesteps(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , self.device ) __snake_case = eta __snake_case = self.scheduler.timesteps[0] + 1 __snake_case = generator[0] if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else generator for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): if t < t_last: # predict the noise residual __snake_case = self.unet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).sample # compute previous image: x_t -> x_t-1 __snake_case = self.scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample else: # compute the reverse: x_t-1 -> x_t __snake_case = self.scheduler.undo_step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __snake_case = t __snake_case = (image / 2 + 0.5).clamp(0 , 1 ) __snake_case = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": __snake_case = self.numpy_to_pil(__SCREAMING_SNAKE_CASE ) if not return_dict: return (image,) return ImagePipelineOutput(images=__SCREAMING_SNAKE_CASE )
24
0
"""simple docstring""" from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=lowerCamelCase_ ) class snake_case_ ( lowerCamelCase_ ): """simple docstring""" A_ = field(default='''summarization''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) A_ = Features({'''text''': Value('''string''' )} ) A_ = Features({'''summary''': Value('''string''' )} ) A_ = "text" A_ = "summary" @property def UpperCAmelCase__ ( self) -> Dict[str, str]: return {self.text_column: "text", self.summary_column: "summary"}
34
'''simple docstring''' from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax UpperCAmelCase_ : str = logging.get_logger(__name__) @add_end_docstrings(__lowerCAmelCase) class lowerCAmelCase ( __lowerCAmelCase): def __init__( self , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' super().__init__(**__SCREAMING_SNAKE_CASE ) 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 , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> List[Any]: '''simple docstring''' return super().__call__(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , **__SCREAMING_SNAKE_CASE ) -> Tuple: '''simple docstring''' __snake_case = {} if "candidate_labels" in kwargs: __snake_case = kwargs['''candidate_labels'''] if "hypothesis_template" in kwargs: __snake_case = kwargs['''hypothesis_template'''] return preprocess_params, {}, {} def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="This is a photo of {}." ) -> Optional[Any]: '''simple docstring''' __snake_case = load_image(__SCREAMING_SNAKE_CASE ) __snake_case = self.image_processor(images=[image] , return_tensors=self.framework ) __snake_case = candidate_labels __snake_case = [hypothesis_template.format(__SCREAMING_SNAKE_CASE ) for x in candidate_labels] __snake_case = self.tokenizer(__SCREAMING_SNAKE_CASE , return_tensors=self.framework , padding=__SCREAMING_SNAKE_CASE ) __snake_case = [text_inputs] return inputs def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' __snake_case = model_inputs.pop('''candidate_labels''' ) __snake_case = model_inputs.pop('''text_inputs''' ) if isinstance(text_inputs[0] , __SCREAMING_SNAKE_CASE ): __snake_case = text_inputs[0] else: # Batching case. __snake_case = text_inputs[0][0] __snake_case = self.model(**__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) __snake_case = { '''candidate_labels''': candidate_labels, '''logits''': outputs.logits_per_image, } return model_outputs def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> str: '''simple docstring''' __snake_case = model_outputs.pop('''candidate_labels''' ) __snake_case = model_outputs['''logits'''][0] if self.framework == "pt": __snake_case = logits.softmax(dim=-1 ).squeeze(-1 ) __snake_case = probs.tolist() if not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): __snake_case = [scores] elif self.framework == "tf": __snake_case = stable_softmax(__SCREAMING_SNAKE_CASE , axis=-1 ) __snake_case = probs.numpy().tolist() else: raise ValueError(F'''Unsupported framework: {self.framework}''' ) __snake_case = [ {'''score''': score, '''label''': candidate_label} for score, candidate_label in sorted(zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) , key=lambda __SCREAMING_SNAKE_CASE : -x[0] ) ] return result
24
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a_ :Dict = logging.get_logger(__name__) a_ :List[str] = { 'google/bigbird-roberta-base': 'https://huggingface.co/google/bigbird-roberta-base/resolve/main/config.json', 'google/bigbird-roberta-large': 'https://huggingface.co/google/bigbird-roberta-large/resolve/main/config.json', 'google/bigbird-base-trivia-itc': 'https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/config.json', # See all BigBird models at https://huggingface.co/models?filter=big_bird } class lowercase ( _UpperCAmelCase ): lowerCamelCase : Optional[int] = '''big_bird''' def __init__( self : Any , _lowercase : str=5_03_58 , _lowercase : Optional[int]=7_68 , _lowercase : List[Any]=12 , _lowercase : Any=12 , _lowercase : Tuple=30_72 , _lowercase : int="gelu_new" , _lowercase : Any=0.1 , _lowercase : Optional[int]=0.1 , _lowercase : str=40_96 , _lowercase : Dict=2 , _lowercase : Union[str, Any]=0.02 , _lowercase : Tuple=1E-12 , _lowercase : Any=True , _lowercase : List[Any]=0 , _lowercase : Optional[int]=1 , _lowercase : List[Any]=2 , _lowercase : Union[str, Any]=66 , _lowercase : int="block_sparse" , _lowercase : str=True , _lowercase : Any=False , _lowercase : Tuple=64 , _lowercase : List[str]=3 , _lowercase : int=None , **_lowercase : List[str] , ): super().__init__( pad_token_id=_lowercase , bos_token_id=_lowercase , eos_token_id=_lowercase , sep_token_id=_lowercase , **_lowercase , ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = vocab_size SCREAMING_SNAKE_CASE__ : Dict = max_position_embeddings SCREAMING_SNAKE_CASE__ : Tuple = hidden_size SCREAMING_SNAKE_CASE__ : Optional[Any] = num_hidden_layers SCREAMING_SNAKE_CASE__ : Dict = num_attention_heads SCREAMING_SNAKE_CASE__ : Dict = intermediate_size SCREAMING_SNAKE_CASE__ : Optional[int] = hidden_act SCREAMING_SNAKE_CASE__ : Any = hidden_dropout_prob SCREAMING_SNAKE_CASE__ : List[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE__ : List[str] = initializer_range SCREAMING_SNAKE_CASE__ : Any = type_vocab_size SCREAMING_SNAKE_CASE__ : str = layer_norm_eps SCREAMING_SNAKE_CASE__ : List[Any] = use_cache SCREAMING_SNAKE_CASE__ : Optional[int] = rescale_embeddings SCREAMING_SNAKE_CASE__ : str = attention_type SCREAMING_SNAKE_CASE__ : List[str] = use_bias SCREAMING_SNAKE_CASE__ : Dict = block_size SCREAMING_SNAKE_CASE__ : Tuple = num_random_blocks SCREAMING_SNAKE_CASE__ : Optional[int] = classifier_dropout class lowercase ( _UpperCAmelCase ): @property def lowercase__ ( self : Tuple ): if self.task == "multiple-choice": SCREAMING_SNAKE_CASE__ : List[str] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: SCREAMING_SNAKE_CASE__ : str = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
35
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) UpperCAmelCase_ : List[str] = { '''configuration_gpt_bigcode''': ['''GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GPTBigCodeConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : int = [ '''GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST''', '''GPTBigCodeForSequenceClassification''', '''GPTBigCodeForTokenClassification''', '''GPTBigCodeForCausalLM''', '''GPTBigCodeModel''', '''GPTBigCodePreTrainedModel''', ] if TYPE_CHECKING: from .configuration_gpt_bigcode import GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTBigCodeConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_bigcode import ( GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTBigCodeForCausalLM, GPTBigCodeForSequenceClassification, GPTBigCodeForTokenClassification, GPTBigCodeModel, GPTBigCodePreTrainedModel, ) else: import sys UpperCAmelCase_ : Any = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
24
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __lowercase : Any = { '''configuration_roformer''': ['''ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''RoFormerConfig''', '''RoFormerOnnxConfig'''], '''tokenization_roformer''': ['''RoFormerTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : Optional[Any] = ['''RoFormerTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : Optional[Any] = [ '''ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''RoFormerForCausalLM''', '''RoFormerForMaskedLM''', '''RoFormerForMultipleChoice''', '''RoFormerForQuestionAnswering''', '''RoFormerForSequenceClassification''', '''RoFormerForTokenClassification''', '''RoFormerLayer''', '''RoFormerModel''', '''RoFormerPreTrainedModel''', '''load_tf_weights_in_roformer''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : int = [ '''TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFRoFormerForCausalLM''', '''TFRoFormerForMaskedLM''', '''TFRoFormerForMultipleChoice''', '''TFRoFormerForQuestionAnswering''', '''TFRoFormerForSequenceClassification''', '''TFRoFormerForTokenClassification''', '''TFRoFormerLayer''', '''TFRoFormerModel''', '''TFRoFormerPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : List[str] = [ '''FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FlaxRoFormerForMaskedLM''', '''FlaxRoFormerForMultipleChoice''', '''FlaxRoFormerForQuestionAnswering''', '''FlaxRoFormerForSequenceClassification''', '''FlaxRoFormerForTokenClassification''', '''FlaxRoFormerModel''', '''FlaxRoFormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys __lowercase : Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
36
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' __snake_case = abs(_lowerCamelCase ) __snake_case = 0 while n > 0: res += n % 10 n //= 10 return res def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' __snake_case = abs(_lowerCamelCase ) return n if n < 10 else n % 10 + sum_of_digits(n // 10 ) def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' return sum(int(_lowerCamelCase ) for c in str(abs(_lowerCamelCase ) ) ) def _UpperCamelCase ()-> None: '''simple docstring''' from collections.abc import Callable from timeit import timeit def benchmark_a_function(_lowerCamelCase : Callable , _lowerCamelCase : int ) -> None: __snake_case = f'''{func.__name__}({value})''' __snake_case = timeit(f'''__main__.{call}''' , setup='''import __main__''' ) print(f'''{call:56} = {func(_lowerCamelCase )} -- {timing:.4f} seconds''' ) for value in (26_21_44, 11_25_89_99_06_84_26_24, 1_26_76_50_60_02_28_22_94_01_49_67_03_20_53_76): for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact): benchmark_a_function(_lowerCamelCase , _lowerCamelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
24
0
from __future__ import annotations import queue class A__ : """simple docstring""" def __init__( self : Dict , lowerCamelCase__ : Optional[Any] ): a__ : Any = data a__ : str = None a__ : str = None def UpperCamelCase_ ( ) -> TreeNode: print("\n********Press N to stop entering at any point of time********\n" ) a__ : Union[str, Any] = input("Enter the value of the root node: " ).strip().lower() a__ : queue.Queue = queue.Queue() a__ : List[Any] = TreeNode(int(__a ) ) q.put(__a ) while not q.empty(): a__ : Dict = q.get() a__ : str = f'''Enter the left node of {node_found.data}: ''' a__ : Any = input(__a ).strip().lower() or "n" if check == "n": return tree_node a__ : Any = TreeNode(int(__a ) ) a__ : str = left_node q.put(__a ) a__ : Optional[Any] = f'''Enter the right node of {node_found.data}: ''' a__ : Any = input(__a ).strip().lower() or "n" if check == "n": return tree_node a__ : List[Any] = TreeNode(int(__a ) ) a__ : Tuple = right_node q.put(__a ) raise def UpperCamelCase_ ( __a ) -> None: if not isinstance(__a , __a ) or not node: return print(node.data , end="," ) pre_order(node.left ) pre_order(node.right ) def UpperCamelCase_ ( __a ) -> None: if not isinstance(__a , __a ) or not node: return in_order(node.left ) print(node.data , end="," ) in_order(node.right ) def UpperCamelCase_ ( __a ) -> None: if not isinstance(__a , __a ) or not node: return post_order(node.left ) post_order(node.right ) print(node.data , end="," ) def UpperCamelCase_ ( __a ) -> None: if not isinstance(__a , __a ) or not node: return a__ : queue.Queue = queue.Queue() q.put(__a ) while not q.empty(): a__ : Union[str, Any] = q.get() print(node_dequeued.data , end="," ) if node_dequeued.left: q.put(node_dequeued.left ) if node_dequeued.right: q.put(node_dequeued.right ) def UpperCamelCase_ ( __a ) -> None: if not isinstance(__a , __a ) or not node: return a__ : queue.Queue = queue.Queue() q.put(__a ) while not q.empty(): a__ : List[Any] = [] while not q.empty(): a__ : Tuple = q.get() print(node_dequeued.data , end="," ) if node_dequeued.left: list_.append(node_dequeued.left ) if node_dequeued.right: list_.append(node_dequeued.right ) print() for node in list_: q.put(__a ) def UpperCamelCase_ ( __a ) -> None: if not isinstance(__a , __a ) or not node: return a__ : list[TreeNode] = [] a__ : Union[str, Any] = node while n or stack: while n: # start from root node, find its left child print(n.data , end="," ) stack.append(__a ) a__ : List[Any] = n.left # end of while means current node doesn't have left child a__ : Optional[int] = stack.pop() # start to traverse its right child a__ : List[Any] = n.right def UpperCamelCase_ ( __a ) -> None: if not isinstance(__a , __a ) or not node: return a__ : list[TreeNode] = [] a__ : List[str] = node while n or stack: while n: stack.append(__a ) a__ : List[str] = n.left a__ : Dict = stack.pop() print(n.data , end="," ) a__ : str = n.right def UpperCamelCase_ ( __a ) -> None: if not isinstance(__a , __a ) or not node: return a__, a__ : Union[str, Any] = [], [] a__ : Tuple = node stacka.append(__a ) while stacka: # to find the reversed order of post order, store it in stack2 a__ : Union[str, Any] = stacka.pop() if n.left: stacka.append(n.left ) if n.right: stacka.append(n.right ) stacka.append(__a ) while stacka: # pop up from stack2 will be the post order print(stacka.pop().data , end="," ) def UpperCamelCase_ ( __a = "" , __a=50 , __a="*" ) -> str: if not s: return "\n" + width * char a__, a__ : Any = divmod(width - len(__a ) - 2 , 2 ) return f'''{left * char} {s} {(left + extra) * char}''' if __name__ == "__main__": import doctest doctest.testmod() print(prompt("""Binary Tree Traversals""")) UpperCamelCase : TreeNode = build_tree() print(prompt("""Pre Order Traversal""")) pre_order(node) print(prompt() + """\n""") print(prompt("""In Order Traversal""")) in_order(node) print(prompt() + """\n""") print(prompt("""Post Order Traversal""")) post_order(node) print(prompt() + """\n""") print(prompt("""Level Order Traversal""")) level_order(node) print(prompt() + """\n""") print(prompt("""Actual Level Order Traversal""")) level_order_actual(node) print("""*""" * 50 + """\n""") print(prompt("""Pre Order Traversal - Iteration Version""")) pre_order_iter(node) print(prompt() + """\n""") print(prompt("""In Order Traversal - Iteration Version""")) in_order_iter(node) print(prompt() + """\n""") print(prompt("""Post Order Traversal - Iteration Version""")) post_order_iter(node) print(prompt())
37
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : Optional[Any] )-> Dict: '''simple docstring''' __snake_case = [] __snake_case = [] __snake_case = { '''^''': 3, '''*''': 2, '''/''': 2, '''%''': 2, '''+''': 1, '''-''': 1, } # Priority of each operator __snake_case = len(_lowerCamelCase ) if (len(_lowerCamelCase ) > 7) else 7 # Print table header for output print( '''Symbol'''.center(8 ) , '''Stack'''.center(_lowerCamelCase ) , '''Postfix'''.center(_lowerCamelCase ) , sep=''' | ''' , ) print('''-''' * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(_lowerCamelCase ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(_lowerCamelCase ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(_lowerCamelCase ) == 0: stack.append(_lowerCamelCase ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(_lowerCamelCase ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(_lowerCamelCase ) # push x to stack print( x.center(8 ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , sep=''' | ''' , ) # Output in tabular format while len(_lowerCamelCase ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( ''' '''.center(8 ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , sep=''' | ''' , ) # Output in tabular format return "".join(_lowerCamelCase ) # return Postfix as str def _UpperCamelCase (_lowerCamelCase : Optional[Any] )-> str: '''simple docstring''' __snake_case = list(infix[::-1] ) # reverse the infix equation for i in range(len(_lowerCamelCase ) ): if infix[i] == "(": __snake_case = ''')''' # change "(" to ")" elif infix[i] == ")": __snake_case = '''(''' # change ")" to "(" return (infix_2_postfix(''''''.join(_lowerCamelCase ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": UpperCAmelCase_ : Dict = input('''\nEnter an Infix Equation = ''') # Input an Infix equation UpperCAmelCase_ : Optional[Any] = ''''''.join(Infix.split()) # Remove spaces from the input print('''\n\t''', Infix, '''(Infix) -> ''', infix_2_prefix(Infix), '''(Prefix)''')
24
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) A_ : str = {"configuration_xlnet": ["XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLNetConfig"]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : List[str] = ["XLNetTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Any = ["XLNetTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Tuple = [ "XLNET_PRETRAINED_MODEL_ARCHIVE_LIST", "XLNetForMultipleChoice", "XLNetForQuestionAnswering", "XLNetForQuestionAnsweringSimple", "XLNetForSequenceClassification", "XLNetForTokenClassification", "XLNetLMHeadModel", "XLNetModel", "XLNetPreTrainedModel", "load_tf_weights_in_xlnet", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Union[str, Any] = [ "TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST", "TFXLNetForMultipleChoice", "TFXLNetForQuestionAnsweringSimple", "TFXLNetForSequenceClassification", "TFXLNetForTokenClassification", "TFXLNetLMHeadModel", "TFXLNetMainLayer", "TFXLNetModel", "TFXLNetPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlnet import XLNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlnet_fast import XLNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlnet import ( XLNET_PRETRAINED_MODEL_ARCHIVE_LIST, XLNetForMultipleChoice, XLNetForQuestionAnswering, XLNetForQuestionAnsweringSimple, XLNetForSequenceClassification, XLNetForTokenClassification, XLNetLMHeadModel, XLNetModel, XLNetPreTrainedModel, load_tf_weights_in_xlnet, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlnet import ( TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLNetForMultipleChoice, TFXLNetForQuestionAnsweringSimple, TFXLNetForSequenceClassification, TFXLNetForTokenClassification, TFXLNetLMHeadModel, TFXLNetMainLayer, TFXLNetModel, TFXLNetPreTrainedModel, ) else: import sys A_ : List[str] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
38
'''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 ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices UpperCAmelCase_ : int = logging.get_logger(__name__) UpperCAmelCase_ : Dict = { '''microsoft/swin-tiny-patch4-window7-224''': ( '''https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json''' ), # See all Swin models at https://huggingface.co/models?filter=swin } class lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase): __lowercase : List[Any] = '''swin''' __lowercase : str = { '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self , __SCREAMING_SNAKE_CASE=224 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=96 , __SCREAMING_SNAKE_CASE=[2, 2, 6, 2] , __SCREAMING_SNAKE_CASE=[3, 6, 12, 24] , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=4.0 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1E-5 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ) -> int: '''simple docstring''' super().__init__(**__SCREAMING_SNAKE_CASE ) __snake_case = image_size __snake_case = patch_size __snake_case = num_channels __snake_case = embed_dim __snake_case = depths __snake_case = len(__SCREAMING_SNAKE_CASE ) __snake_case = num_heads __snake_case = window_size __snake_case = mlp_ratio __snake_case = qkv_bias __snake_case = hidden_dropout_prob __snake_case = attention_probs_dropout_prob __snake_case = drop_path_rate __snake_case = hidden_act __snake_case = use_absolute_embeddings __snake_case = layer_norm_eps __snake_case = initializer_range __snake_case = encoder_stride # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model __snake_case = int(embed_dim * 2 ** (len(__SCREAMING_SNAKE_CASE ) - 1) ) __snake_case = ['''stem'''] + [F'''stage{idx}''' for idx in range(1 , len(__SCREAMING_SNAKE_CASE ) + 1 )] __snake_case , __snake_case = get_aligned_output_features_output_indices( out_features=__SCREAMING_SNAKE_CASE , out_indices=__SCREAMING_SNAKE_CASE , stage_names=self.stage_names ) class lowerCAmelCase ( __lowerCAmelCase): __lowercase : Optional[int] = version.parse('''1.11''') @property def lowerCAmelCase ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def lowerCAmelCase ( self ) -> float: '''simple docstring''' return 1E-4
24
0
from abc import ABC, abstractmethod from typing import List, Optional class snake_case_ ( __A ): '''simple docstring''' def __init__( self : int ) ->Optional[Any]: # test for the above condition self.test() def snake_case__( self : int ) ->str: snake_case_ = 0 snake_case_ = False while not completed: if counter == 1: self.reset() snake_case_ = self.advance() if not self.does_advance(_UpperCamelCase ): raise Exception( '''Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.''' ) snake_case_, snake_case_, snake_case_ = self.update(_UpperCamelCase ) counter += 1 if counter > 1_0_0_0_0: raise Exception('''update() does not fulfill the constraint.''' ) if self.remaining() != 0: raise Exception('''Custom Constraint is not defined correctly.''' ) @abstractmethod def snake_case__( self : List[Any] ) ->Union[str, Any]: raise NotImplementedError( f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) @abstractmethod def snake_case__( self : int , _UpperCamelCase : int ) ->List[str]: raise NotImplementedError( f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) @abstractmethod def snake_case__( self : Union[str, Any] , _UpperCamelCase : int ) ->int: raise NotImplementedError( f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) @abstractmethod def snake_case__( self : int ) ->str: raise NotImplementedError( f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) @abstractmethod def snake_case__( self : int ) ->str: raise NotImplementedError( f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) @abstractmethod def snake_case__( self : List[str] , _UpperCamelCase : List[Any]=False ) ->List[Any]: raise NotImplementedError( f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.''' ) class snake_case_ ( __A ): '''simple docstring''' def __init__( self : Optional[Any] , _UpperCamelCase : List[int] ) ->Dict: super(_UpperCamelCase , self ).__init__() if not isinstance(_UpperCamelCase , _UpperCamelCase ) or len(_UpperCamelCase ) == 0: raise ValueError(f'''`token_ids` has to be a non-empty list, but is {token_ids}.''' ) if any((not isinstance(_UpperCamelCase , _UpperCamelCase ) or token_id < 0) for token_id in token_ids ): raise ValueError(f'''Each list in `token_ids` has to be a list of positive integers, but is {token_ids}.''' ) snake_case_ = token_ids snake_case_ = len(self.token_ids ) snake_case_ = -1 # the index of the currently fulfilled step snake_case_ = False def snake_case__( self : Dict ) ->Dict: if self.completed: return None return self.token_ids[self.fulfilled_idx + 1] def snake_case__( self : Union[str, Any] , _UpperCamelCase : int ) ->Optional[Any]: if not isinstance(_UpperCamelCase , _UpperCamelCase ): raise ValueError(f'''`token_id` has to be an `int`, but is {token_id} of type {type(_UpperCamelCase )}''' ) if self.completed: return False return token_id == self.token_ids[self.fulfilled_idx + 1] def snake_case__( self : Union[str, Any] , _UpperCamelCase : int ) ->int: if not isinstance(_UpperCamelCase , _UpperCamelCase ): raise ValueError(f'''`token_id` has to be an `int`, but is {token_id} of type {type(_UpperCamelCase )}''' ) snake_case_ = False snake_case_ = False snake_case_ = False if self.does_advance(_UpperCamelCase ): self.fulfilled_idx += 1 snake_case_ = True if self.fulfilled_idx == (self.seqlen - 1): snake_case_ = True snake_case_ = completed else: # failed to make progress. snake_case_ = True self.reset() return stepped, completed, reset def snake_case__( self : Any ) ->Union[str, Any]: snake_case_ = False snake_case_ = 0 def snake_case__( self : Union[str, Any] ) ->int: return self.seqlen - (self.fulfilled_idx + 1) def snake_case__( self : str , _UpperCamelCase : Union[str, Any]=False ) ->int: snake_case_ = PhrasalConstraint(self.token_ids ) if stateful: snake_case_ = self.seqlen snake_case_ = self.fulfilled_idx snake_case_ = self.completed return new_constraint class snake_case_ : '''simple docstring''' def __init__( self : List[str] , _UpperCamelCase : List[List[int]] , _UpperCamelCase : List[Any]=True ) ->str: snake_case_ = max([len(_UpperCamelCase ) for one in nested_token_ids] ) snake_case_ = {} for token_ids in nested_token_ids: snake_case_ = root for tidx, token_id in enumerate(_UpperCamelCase ): if token_id not in level: snake_case_ = {} snake_case_ = level[token_id] if no_subsets and self.has_subsets(_UpperCamelCase , _UpperCamelCase ): raise ValueError( '''Each list in `nested_token_ids` can\'t be a complete subset of another list, but is''' f''' {nested_token_ids}.''' ) snake_case_ = root def snake_case__( self : Any , _UpperCamelCase : List[Any] ) ->Optional[Any]: snake_case_ = self.trie for current_token in current_seq: snake_case_ = start[current_token] snake_case_ = list(start.keys() ) return next_tokens def snake_case__( self : Optional[int] , _UpperCamelCase : int ) ->Optional[int]: snake_case_ = self.next_tokens(_UpperCamelCase ) return len(_UpperCamelCase ) == 0 def snake_case__( self : List[Any] , _UpperCamelCase : List[Any] ) ->Dict: snake_case_ = list(root.values() ) if len(_UpperCamelCase ) == 0: return 1 else: return sum([self.count_leaves(_UpperCamelCase ) for nn in next_nodes] ) def snake_case__( self : Union[str, Any] , _UpperCamelCase : Dict , _UpperCamelCase : Union[str, Any] ) ->int: snake_case_ = self.count_leaves(_UpperCamelCase ) return len(_UpperCamelCase ) != leaf_count class snake_case_ ( __A ): '''simple docstring''' def __init__( self : Optional[int] , _UpperCamelCase : List[List[int]] ) ->Any: super(_UpperCamelCase , self ).__init__() if not isinstance(_UpperCamelCase , _UpperCamelCase ) or len(_UpperCamelCase ) == 0: raise ValueError(f'''`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}.''' ) if any(not isinstance(_UpperCamelCase , _UpperCamelCase ) for token_ids in nested_token_ids ): raise ValueError(f'''`nested_token_ids` has to be a list of lists, but is {nested_token_ids}.''' ) if any( any((not isinstance(_UpperCamelCase , _UpperCamelCase ) or token_id < 0) for token_id in token_ids ) for token_ids in nested_token_ids ): raise ValueError( f'''Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}.''' ) snake_case_ = DisjunctiveTrie(_UpperCamelCase ) snake_case_ = nested_token_ids snake_case_ = self.trie.max_height snake_case_ = [] snake_case_ = False def snake_case__( self : Optional[int] ) ->Optional[int]: snake_case_ = self.trie.next_tokens(self.current_seq ) if len(_UpperCamelCase ) == 0: return None else: return token_list def snake_case__( self : Dict , _UpperCamelCase : int ) ->Dict: if not isinstance(_UpperCamelCase , _UpperCamelCase ): raise ValueError(f'''`token_id` is supposed to be type `int`, but is {token_id} of type {type(_UpperCamelCase )}''' ) snake_case_ = self.trie.next_tokens(self.current_seq ) return token_id in next_tokens def snake_case__( self : Tuple , _UpperCamelCase : int ) ->Optional[Any]: if not isinstance(_UpperCamelCase , _UpperCamelCase ): raise ValueError(f'''`token_id` is supposed to be type `int`, but is {token_id} of type {type(_UpperCamelCase )}''' ) snake_case_ = False snake_case_ = False snake_case_ = False if self.does_advance(_UpperCamelCase ): self.current_seq.append(_UpperCamelCase ) snake_case_ = True else: snake_case_ = True self.reset() snake_case_ = self.trie.reached_leaf(self.current_seq ) snake_case_ = completed return stepped, completed, reset def snake_case__( self : List[Any] ) ->str: snake_case_ = False snake_case_ = [] def snake_case__( self : Tuple ) ->Dict: if self.completed: # since this can be completed without reaching max height return 0 else: return self.seqlen - len(self.current_seq ) def snake_case__( self : Union[str, Any] , _UpperCamelCase : List[Any]=False ) ->Optional[int]: snake_case_ = DisjunctiveConstraint(self.token_ids ) if stateful: snake_case_ = self.seqlen snake_case_ = self.current_seq snake_case_ = self.completed return new_constraint class snake_case_ : '''simple docstring''' def __init__( self : Tuple , _UpperCamelCase : List[Constraint] ) ->str: snake_case_ = constraints # max # of steps required to fulfill a given constraint snake_case_ = max([c.seqlen for c in constraints] ) snake_case_ = len(_UpperCamelCase ) snake_case_ = False self.init_state() def snake_case__( self : Tuple ) ->Dict: snake_case_ = [] snake_case_ = None snake_case_ = [constraint.copy(stateful=_UpperCamelCase ) for constraint in self.constraints] def snake_case__( self : Tuple ) ->int: snake_case_ = 0 if self.inprogress_constraint: # extra points for having a constraint mid-fulfilled add += self.max_seqlen - self.inprogress_constraint.remaining() return (len(self.complete_constraints ) * self.max_seqlen) + add def snake_case__( self : Optional[Any] ) ->Optional[Any]: snake_case_ = [] if self.inprogress_constraint is None: for constraint in self.pending_constraints: # "pending" == "unfulfilled yet" snake_case_ = constraint.advance() if isinstance(_UpperCamelCase , _UpperCamelCase ): token_list.append(_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): token_list.extend(_UpperCamelCase ) else: snake_case_ = self.inprogress_constraint.advance() if isinstance(_UpperCamelCase , _UpperCamelCase ): token_list.append(_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): token_list.extend(_UpperCamelCase ) if len(_UpperCamelCase ) == 0: return None else: return token_list def snake_case__( self : Dict , _UpperCamelCase : Optional[List[int]] ) ->List[Any]: self.init_state() if token_ids is not None: for token in token_ids: # completes or steps **one** constraint snake_case_, snake_case_ = self.add(_UpperCamelCase ) # the entire list of constraints are fulfilled if self.completed: break def snake_case__( self : Optional[int] , _UpperCamelCase : int ) ->List[Any]: if not isinstance(_UpperCamelCase , _UpperCamelCase ): raise ValueError(f'''`token_id` should be an `int`, but is `{token_id}`.''' ) snake_case_, snake_case_ = False, False if self.completed: snake_case_ = True snake_case_ = False return complete, stepped if self.inprogress_constraint is not None: # In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current # job, simply update the state snake_case_, snake_case_, snake_case_ = self.inprogress_constraint.update(_UpperCamelCase ) if reset: # 1. If the next token breaks the progress, then we must restart. # e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books". # But that doesn't mean we self.init_state(), since we only reset the state for this particular # constraint, not the full list of constraints. self.pending_constraints.append(self.inprogress_constraint.copy(stateful=_UpperCamelCase ) ) snake_case_ = None if complete: # 2. If the next token completes the constraint, move it to completed list, set # inprogress to None. If there are no pending constraints either, then this full list of constraints # is complete. self.complete_constraints.append(self.inprogress_constraint ) snake_case_ = None if len(self.pending_constraints ) == 0: # we're done! snake_case_ = True else: # Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list # of constraints? for cidx, pending_constraint in enumerate(self.pending_constraints ): if pending_constraint.does_advance(_UpperCamelCase ): snake_case_, snake_case_, snake_case_ = pending_constraint.update(_UpperCamelCase ) if not stepped: raise Exception( '''`constraint.update(token_id)` is not yielding incremental progress, ''' '''even though `constraint.does_advance(token_id)` is true.''' ) if complete: self.complete_constraints.append(_UpperCamelCase ) snake_case_ = None if not complete and stepped: snake_case_ = pending_constraint if complete or stepped: # If we made any progress at all, then it's at least not a "pending constraint". snake_case_ = ( self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :] ) if len(self.pending_constraints ) == 0 and self.inprogress_constraint is None: # If there's no longer any pending after this and no inprogress either, then we must be # complete. snake_case_ = True break # prevent accidentally stepping through multiple constraints with just one token. return complete, stepped def snake_case__( self : int , _UpperCamelCase : List[str]=True ) ->Optional[Any]: snake_case_ = ConstraintListState(self.constraints ) # we actually never though self.constraints objects # throughout this process. So it's at initialization state. if stateful: snake_case_ = [ constraint.copy(stateful=_UpperCamelCase ) for constraint in self.complete_constraints ] if self.inprogress_constraint is not None: snake_case_ = self.inprogress_constraint.copy(stateful=_UpperCamelCase ) snake_case_ = [constraint.copy() for constraint in self.pending_constraints] return new_state
39
'''simple docstring''' import re import time from typing import Optional import IPython.display as disp from ..trainer_callback import TrainerCallback from ..trainer_utils import IntervalStrategy, has_length def _UpperCamelCase (_lowerCamelCase : Tuple )-> List[str]: '''simple docstring''' __snake_case = int(_lowerCamelCase ) __snake_case , __snake_case , __snake_case = t // 36_00, (t // 60) % 60, t % 60 return f'''{h}:{m:02d}:{s:02d}''' if h != 0 else f'''{m:02d}:{s:02d}''' def _UpperCamelCase (_lowerCamelCase : Any , _lowerCamelCase : Union[str, Any] , _lowerCamelCase : str , _lowerCamelCase : int , _lowerCamelCase : List[Any]=3_00 )-> int: '''simple docstring''' return f''' <div> {prefix} <progress value=\'{value}\' max=\'{total}\' style=\'width:{width}px; height:20px; vertical-align: middle;\'></progress> {label} </div> ''' def _UpperCamelCase (_lowerCamelCase : int )-> List[Any]: '''simple docstring''' __snake_case = '''<table border="1" class="dataframe">\n''' html_code += """ <thead>\n <tr style="text-align: left;">\n""" for i in items[0]: html_code += f''' <th>{i}</th>\n''' html_code += " </tr>\n </thead>\n <tbody>\n" for line in items[1:]: html_code += " <tr>\n" for elt in line: __snake_case = f'''{elt:.6f}''' if isinstance(_lowerCamelCase , _lowerCamelCase ) else str(_lowerCamelCase ) html_code += f''' <td>{elt}</td>\n''' html_code += " </tr>\n" html_code += " </tbody>\n</table><p>" return html_code class lowerCAmelCase : __lowercase : str = 5 __lowercase : Optional[Any] = 0.2 def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = 300 , ) -> List[Any]: '''simple docstring''' __snake_case = total __snake_case = '''''' if prefix is None else prefix __snake_case = leave __snake_case = parent __snake_case = width __snake_case = None __snake_case = None __snake_case = None def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = None ) -> Any: '''simple docstring''' __snake_case = value if comment is not None: __snake_case = comment if self.last_value is None: __snake_case = __snake_case = time.time() __snake_case = __snake_case = value __snake_case = __snake_case = None __snake_case = self.warmup __snake_case = 1 self.update_bar(__SCREAMING_SNAKE_CASE ) elif value <= self.last_value and not force_update: return elif force_update or self.first_calls > 0 or value >= min(self.last_value + self.wait_for , self.total ): if self.first_calls > 0: self.first_calls -= 1 __snake_case = time.time() __snake_case = current_time - self.start_time # We could have value = self.start_value if the update is called twixe with the same start value. if value > self.start_value: __snake_case = self.elapsed_time / (value - self.start_value) else: __snake_case = None if value >= self.total: __snake_case = self.total __snake_case = None if not self.leave: self.close() elif self.average_time_per_item is not None: __snake_case = self.average_time_per_item * (self.total - value) self.update_bar(__SCREAMING_SNAKE_CASE ) __snake_case = value __snake_case = current_time if self.average_time_per_item is None: __snake_case = 1 else: __snake_case = max(int(self.update_every / self.average_time_per_item ) , 1 ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) -> List[str]: '''simple docstring''' __snake_case = ''' ''' * (len(str(self.total ) ) - len(str(__SCREAMING_SNAKE_CASE ) )) + str(__SCREAMING_SNAKE_CASE ) if self.elapsed_time is None: __snake_case = F'''[{spaced_value}/{self.total} : < :''' elif self.predicted_remaining is None: __snake_case = F'''[{spaced_value}/{self.total} {format_time(self.elapsed_time )}''' else: __snake_case = ( F'''[{spaced_value}/{self.total} {format_time(self.elapsed_time )} <''' F''' {format_time(self.predicted_remaining )}''' ) self.label += F''', {1/self.average_time_per_item:.2f} it/s''' self.label += "]" if self.comment is None or len(self.comment ) == 0 else F''', {self.comment}]''' self.display() def lowerCAmelCase ( self ) -> List[Any]: '''simple docstring''' __snake_case = html_progress_bar(self.value , self.total , self.prefix , self.label , self.width ) if self.parent is not None: # If this is a child bar, the parent will take care of the display. self.parent.display() return if self.output is None: __snake_case = disp.display(disp.HTML(self.html_code ) , display_id=__SCREAMING_SNAKE_CASE ) else: self.output.update(disp.HTML(self.html_code ) ) def lowerCAmelCase ( self ) -> Dict: '''simple docstring''' if self.parent is None and self.output is not None: self.output.update(disp.HTML('''''' ) ) class lowerCAmelCase ( __lowerCAmelCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) -> Any: '''simple docstring''' super().__init__(__SCREAMING_SNAKE_CASE ) __snake_case = None if column_names is None else [column_names] __snake_case = None def lowerCAmelCase ( self ) -> Any: '''simple docstring''' __snake_case = html_progress_bar(self.value , self.total , self.prefix , self.label , self.width ) if self.inner_table is not None: self.html_code += text_to_html_table(self.inner_table ) if self.child_bar is not None: self.html_code += self.child_bar.html_code if self.output is None: __snake_case = disp.display(disp.HTML(self.html_code ) , display_id=__SCREAMING_SNAKE_CASE ) else: self.output.update(disp.HTML(self.html_code ) ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' if self.inner_table is None: __snake_case = [list(values.keys() ), list(values.values() )] else: __snake_case = self.inner_table[0] if len(self.inner_table ) == 1: # We give a chance to update the column names at the first iteration for key in values.keys(): if key not in columns: columns.append(__SCREAMING_SNAKE_CASE ) __snake_case = columns self.inner_table.append([values[c] for c in columns] ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=300 ) -> List[str]: '''simple docstring''' __snake_case = NotebookProgressBar(__SCREAMING_SNAKE_CASE , prefix=__SCREAMING_SNAKE_CASE , parent=self , width=__SCREAMING_SNAKE_CASE ) return self.child_bar def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case = None self.display() class lowerCAmelCase ( __lowerCAmelCase): def __init__( self ) -> str: '''simple docstring''' __snake_case = None __snake_case = None __snake_case = False def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' __snake_case = '''Epoch''' if args.evaluation_strategy == IntervalStrategy.EPOCH else '''Step''' __snake_case = 0 __snake_case = 0 __snake_case = [self.first_column] + ['''Training Loss'''] if args.evaluation_strategy != IntervalStrategy.NO: column_names.append('''Validation Loss''' ) __snake_case = NotebookTrainingTracker(state.max_steps , __SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> List[Any]: '''simple docstring''' __snake_case = int(state.epoch ) if int(state.epoch ) == state.epoch else F'''{state.epoch:.2f}''' self.training_tracker.update( state.global_step + 1 , comment=F'''Epoch {epoch}/{state.num_train_epochs}''' , force_update=self._force_next_update , ) __snake_case = False def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' if not has_length(__SCREAMING_SNAKE_CASE ): return if self.prediction_bar is None: if self.training_tracker is not None: __snake_case = self.training_tracker.add_child(len(__SCREAMING_SNAKE_CASE ) ) else: __snake_case = NotebookProgressBar(len(__SCREAMING_SNAKE_CASE ) ) self.prediction_bar.update(1 ) else: self.prediction_bar.update(self.prediction_bar.value + 1 ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' if self.prediction_bar is not None: self.prediction_bar.close() __snake_case = None def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) -> List[Any]: '''simple docstring''' if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs: __snake_case = {'''Training Loss''': logs['''loss''']} # First column is necessarily Step sine we're not in epoch eval strategy __snake_case = state.global_step self.training_tracker.write_line(__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) -> Optional[Any]: '''simple docstring''' if self.training_tracker is not None: __snake_case = {'''Training Loss''': '''No log''', '''Validation Loss''': '''No log'''} for log in reversed(state.log_history ): if "loss" in log: __snake_case = log['''loss'''] break if self.first_column == "Epoch": __snake_case = int(state.epoch ) else: __snake_case = state.global_step __snake_case = '''eval''' for k in metrics: if k.endswith('''_loss''' ): __snake_case = re.sub(r'''\_loss$''' , '''''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop('''total_flos''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop('''epoch''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_runtime''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_samples_per_second''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_steps_per_second''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_jit_compilation_time''' , __SCREAMING_SNAKE_CASE ) for k, v in metrics.items(): if k == F'''{metric_key_prefix}_loss''': __snake_case = v else: __snake_case = k.split('''_''' ) __snake_case = ''' '''.join([part.capitalize() for part in splits[1:]] ) __snake_case = v self.training_tracker.write_line(__SCREAMING_SNAKE_CASE ) self.training_tracker.remove_child() __snake_case = None # Evaluation takes a long time so we should force the next update. __snake_case = True def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' self.training_tracker.update( state.global_step , comment=F'''Epoch {int(state.epoch )}/{state.num_train_epochs}''' , force_update=__SCREAMING_SNAKE_CASE ) __snake_case = None
24
0
import json import sys def UpperCamelCase ( snake_case__ : Optional[Any] , snake_case__ : Dict ) -> Dict: with open(snake_case__ , encoding='utf-8' ) as f: UpperCamelCase : Optional[Any] = json.load(snake_case__ ) UpperCamelCase : int = ['<details>', '<summary>Show updated benchmarks!</summary>', ' '] for benchmark_name in sorted(snake_case__ ): UpperCamelCase : List[str] = results[benchmark_name] UpperCamelCase : Tuple = benchmark_name.split('/' )[-1] output_md.append(F"""### Benchmark: {benchmark_file_name}""" ) UpperCamelCase : Optional[Any] = '| metric |' UpperCamelCase : List[Any] = '|--------|' UpperCamelCase : str = '| new / old (diff) |' for metric_name in sorted(snake_case__ ): UpperCamelCase : Dict = benchmark_res[metric_name] UpperCamelCase : str = metric_vals['new'] UpperCamelCase : Optional[int] = metric_vals.get('old' , snake_case__ ) UpperCamelCase : Optional[Any] = metric_vals.get('diff' , snake_case__ ) UpperCamelCase : Union[str, Any] = F""" {new_val:f}""" if isinstance(snake_case__ , (int, float) ) else 'None' if old_val is not None: val_str += F""" / {old_val:f}""" if isinstance(snake_case__ , (int, float) ) else "None" if dif_val is not None: val_str += F""" ({dif_val:f})""" if isinstance(snake_case__ , (int, float) ) else "None" title += " " + metric_name + " |" lines += "---|" value += val_str + " |" output_md += [title, lines, value, " "] output_md.append('</details>' ) with open(snake_case__ , 'w' , encoding='utf-8' ) as f: f.writelines('\n'.join(snake_case__ ) ) if __name__ == "__main__": __UpperCAmelCase = sys.argv[1] __UpperCAmelCase = sys.argv[2] format_json_to_md(input_json_file, output_md_file)
40
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' if divisor % 5 == 0 or divisor % 2 == 0: return 0 __snake_case = 1 __snake_case = 1 while repunit: __snake_case = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index def _UpperCamelCase (_lowerCamelCase : int = 1_00_00_00 )-> int: '''simple docstring''' __snake_case = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(_lowerCamelCase ) <= limit: divisor += 2 return divisor if __name__ == "__main__": print(F"""{solution() = }""")
24
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowerCAmelCase__ = { '''configuration_rag''': ['''RagConfig'''], '''retrieval_rag''': ['''RagRetriever'''], '''tokenization_rag''': ['''RagTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''RagModel''', '''RagPreTrainedModel''', '''RagSequenceForGeneration''', '''RagTokenForGeneration''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''TFRagModel''', '''TFRagPreTrainedModel''', '''TFRagSequenceForGeneration''', '''TFRagTokenForGeneration''', ] if TYPE_CHECKING: from .configuration_rag import RagConfig from .retrieval_rag import RagRetriever from .tokenization_rag import RagTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rag import RagModel, RagPreTrainedModel, RagSequenceForGeneration, RagTokenForGeneration try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rag import ( TFRagModel, TFRagPreTrainedModel, TFRagSequenceForGeneration, TFRagTokenForGeneration, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
41
'''simple docstring''' from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def _UpperCamelCase (_lowerCamelCase : Union[str, Any] , _lowerCamelCase : List[Any] )-> Optional[Any]: '''simple docstring''' __snake_case = [] for part_id in partition_order: __snake_case = df.where(f'''SPARK_PARTITION_ID() = {part_id}''' ).collect() for row_idx, row in enumerate(_lowerCamelCase ): expected_row_ids_and_row_dicts.append((f'''{part_id}_{row_idx}''', row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Any: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(1_00 ).repartition(1 ) __snake_case = Spark(_lowerCamelCase ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(10 ).repartition(2 ) __snake_case = [1, 0] __snake_case = _generate_iterable_examples(_lowerCamelCase , _lowerCamelCase ) # Reverse the partitions. __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , _lowerCamelCase ) for i, (row_id, row_dict) in enumerate(generate_fn() ): __snake_case , __snake_case = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> int: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(10 ).repartition(1 ) __snake_case = SparkExamplesIterable(_lowerCamelCase ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): assert row_id == f'''0_{i}''' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Union[str, Any]: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''' ) as generator_mock: __snake_case = lambda _lowerCamelCase : x.reverse() __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , [2, 1, 0] ) __snake_case = SparkExamplesIterable(_lowerCamelCase ).shuffle_data_sources(_lowerCamelCase ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): __snake_case , __snake_case = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 __snake_case = SparkExamplesIterable(_lowerCamelCase ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , [0, 2] ) for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): __snake_case , __snake_case = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 __snake_case = SparkExamplesIterable(_lowerCamelCase ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , [1, 3] ) for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): __snake_case , __snake_case = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Optional[int]: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(1_00 ).repartition(1 ) __snake_case = Spark(_lowerCamelCase ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 1_00
24
0
'''simple docstring''' import os import warnings from typing import List, Optional from ...tokenization_utils_base import BatchEncoding from ...utils import logging from .configuration_rag import RagConfig A_ = logging.get_logger(__name__) class UpperCAmelCase : '''simple docstring''' def __init__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> int: '''simple docstring''' lowerCamelCase_ = question_encoder lowerCamelCase_ = generator lowerCamelCase_ = self.question_encoder def UpperCamelCase( self , SCREAMING_SNAKE_CASE_ ) -> List[str]: '''simple docstring''' if os.path.isfile(SCREAMING_SNAKE_CASE_ ): raise ValueError(f'''Provided path ({save_directory}) should be a directory, not a file''' ) os.makedirs(SCREAMING_SNAKE_CASE_ , exist_ok=SCREAMING_SNAKE_CASE_ ) lowerCamelCase_ = os.path.join(SCREAMING_SNAKE_CASE_ , 'question_encoder_tokenizer' ) lowerCamelCase_ = os.path.join(SCREAMING_SNAKE_CASE_ , 'generator_tokenizer' ) self.question_encoder.save_pretrained(SCREAMING_SNAKE_CASE_ ) self.generator.save_pretrained(SCREAMING_SNAKE_CASE_ ) @classmethod def UpperCamelCase( cls , SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) -> Dict: '''simple docstring''' from ..auto.tokenization_auto import AutoTokenizer lowerCamelCase_ = kwargs.pop('config' , SCREAMING_SNAKE_CASE_ ) if config is None: lowerCamelCase_ = RagConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) lowerCamelCase_ = AutoTokenizer.from_pretrained( SCREAMING_SNAKE_CASE_ , config=config.question_encoder , subfolder='question_encoder_tokenizer' ) lowerCamelCase_ = AutoTokenizer.from_pretrained( SCREAMING_SNAKE_CASE_ , config=config.generator , subfolder='generator_tokenizer' ) return cls(question_encoder=SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ ) def __call__( self , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: '''simple docstring''' return self.current_tokenizer(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def UpperCamelCase( self , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) -> Dict: '''simple docstring''' return self.generator.batch_decode(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def UpperCamelCase( self , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) -> Dict: '''simple docstring''' return self.generator.decode(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def UpperCamelCase( self ) -> Dict: '''simple docstring''' lowerCamelCase_ = self.question_encoder def UpperCamelCase( self ) -> Optional[Any]: '''simple docstring''' lowerCamelCase_ = self.generator def UpperCamelCase( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = "longest" , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = True , **SCREAMING_SNAKE_CASE_ , ) -> BatchEncoding: '''simple docstring''' warnings.warn( '`prepare_seq2seq_batch` is deprecated and will be removed in version 5 of 🤗 Transformers. Use the ' 'regular `__call__` method to prepare your inputs and the tokenizer under the `with_target_tokenizer` ' 'context manager to prepare your targets. See the documentation of your specific tokenizer for more ' 'details' , SCREAMING_SNAKE_CASE_ , ) if max_length is None: lowerCamelCase_ = self.current_tokenizer.model_max_length lowerCamelCase_ = self( SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) if tgt_texts is None: return model_inputs # Process tgt_texts if max_target_length is None: lowerCamelCase_ = self.current_tokenizer.model_max_length lowerCamelCase_ = self( text_target=SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) lowerCamelCase_ = labels['input_ids'] return model_inputs
42
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int , _lowerCamelCase : int , _lowerCamelCase : int )-> float: '''simple docstring''' __snake_case = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) # formula for sum of series return total def _UpperCamelCase ()-> str: '''simple docstring''' print(sum_of_series(1 , 1 , 10 ) ) if __name__ == "__main__": import doctest doctest.testmod()
24
0
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" def is_in_circle(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) -> bool: lowercase__ = sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle lowercase__ = mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(SCREAMING_SNAKE_CASE ) ) # The ratio of the area for circle to square is pi/4. lowercase__ = proportion * 4 print(f'The estimated value of pi is {pi_estimate}' ) print(f'The numpy value of pi is {pi}' ) print(f'The total error is {abs(pi - pi_estimate )}' ) def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = 0.0 , SCREAMING_SNAKE_CASE = 1.0 , ): """simple docstring""" return mean( function_to_integrate(uniform(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) for _ in range(SCREAMING_SNAKE_CASE ) ) * (max_value - min_value) def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = 0.0 , SCREAMING_SNAKE_CASE = 1.0 ): """simple docstring""" def identity_function(SCREAMING_SNAKE_CASE ) -> float: return x lowercase__ = area_under_curve_estimator( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) lowercase__ = (max_value * max_value - min_value * min_value) / 2 print('''******************''' ) print(f'Estimating area under y=x where x varies from {min_value} to {max_value}' ) print(f'Estimated value is {estimated_value}' ) print(f'Expected value is {expected_value}' ) print(f'Total error is {abs(estimated_value - expected_value )}' ) print('''******************''' ) def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" def function_to_integrate(SCREAMING_SNAKE_CASE ) -> float: return sqrt(4.0 - x * x ) lowercase__ = area_under_curve_estimator( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , 0.0 , 2.0 ) print('''******************''' ) print('''Estimating pi using area_under_curve_estimator''' ) print(f'Estimated value is {estimated_value}' ) print(f'Expected value is {pi}' ) print(f'Total error is {abs(estimated_value - pi )}' ) print('''******************''' ) if __name__ == "__main__": import doctest doctest.testmod()
43
'''simple docstring''' import asyncio import os import re import sys import tempfile import unittest from contextlib import contextmanager from copy import deepcopy from distutils.util import strtobool from enum import Enum from importlib.util import find_spec from pathlib import Path from unittest.mock import patch import pyarrow as pa import pytest import requests from packaging import version from datasets import config if config.PY_VERSION < version.parse('''3.8'''): import importlib_metadata else: import importlib.metadata as importlib_metadata def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : Tuple=False )-> Union[str, Any]: '''simple docstring''' try: __snake_case = os.environ[key] except KeyError: # KEY isn't set, default to `default`. __snake_case = default else: # KEY is set, convert it to True or False. try: __snake_case = strtobool(_lowerCamelCase ) except ValueError: # More values are supported, but let's keep the message simple. raise ValueError(f'''If set, {key} must be yes or no.''' ) return _value UpperCAmelCase_ : Optional[Any] = parse_flag_from_env('''RUN_SLOW''', default=False) UpperCAmelCase_ : Union[str, Any] = parse_flag_from_env('''RUN_REMOTE''', default=False) UpperCAmelCase_ : Optional[Any] = parse_flag_from_env('''RUN_LOCAL''', default=True) UpperCAmelCase_ : Union[str, Any] = parse_flag_from_env('''RUN_PACKAGED''', default=True) # Compression UpperCAmelCase_ : Dict = pytest.mark.skipif(not config.LZ4_AVAILABLE, reason='''test requires lz4''') UpperCAmelCase_ : int = pytest.mark.skipif(not config.PY7ZR_AVAILABLE, reason='''test requires py7zr''') UpperCAmelCase_ : Tuple = pytest.mark.skipif(not config.ZSTANDARD_AVAILABLE, reason='''test requires zstandard''') # Audio UpperCAmelCase_ : str = pytest.mark.skipif( # On Windows and OS X, soundfile installs sndfile find_spec('''soundfile''') is None or version.parse(importlib_metadata.version('''soundfile''')) < version.parse('''0.12.0'''), reason='''test requires sndfile>=0.12.1: \'pip install \"soundfile>=0.12.1\"\'; ''', ) # Beam UpperCAmelCase_ : Tuple = pytest.mark.skipif( not config.BEAM_AVAILABLE or config.DILL_VERSION >= version.parse('''0.3.2'''), reason='''test requires apache-beam and a compatible dill version''', ) # Dill-cloudpickle compatibility UpperCAmelCase_ : Union[str, Any] = pytest.mark.skipif( config.DILL_VERSION <= version.parse('''0.3.2'''), reason='''test requires dill>0.3.2 for cloudpickle compatibility''', ) # Windows UpperCAmelCase_ : int = pytest.mark.skipif( sys.platform == '''win32''', reason='''test should not be run on Windows''', ) def _UpperCamelCase (_lowerCamelCase : List[str] )-> List[Any]: '''simple docstring''' try: import faiss # noqa except ImportError: __snake_case = unittest.skip('''test requires faiss''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> List[str]: '''simple docstring''' try: import regex # noqa except ImportError: __snake_case = unittest.skip('''test requires regex''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> Union[str, Any]: '''simple docstring''' try: import elasticsearch # noqa except ImportError: __snake_case = unittest.skip('''test requires elasticsearch''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Dict )-> List[Any]: '''simple docstring''' try: import sqlalchemy # noqa except ImportError: __snake_case = unittest.skip('''test requires sqlalchemy''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : List[str] )-> List[str]: '''simple docstring''' if not config.TORCH_AVAILABLE: __snake_case = unittest.skip('''test requires PyTorch''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Optional[Any] )-> Dict: '''simple docstring''' if not config.TF_AVAILABLE: __snake_case = unittest.skip('''test requires TensorFlow''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : int )-> Any: '''simple docstring''' if not config.JAX_AVAILABLE: __snake_case = unittest.skip('''test requires JAX''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Tuple )-> str: '''simple docstring''' if not config.PIL_AVAILABLE: __snake_case = unittest.skip('''test requires Pillow''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Dict )-> Any: '''simple docstring''' try: import transformers # noqa F401 except ImportError: return unittest.skip('''test requires transformers''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : int )-> Tuple: '''simple docstring''' try: import tiktoken # noqa F401 except ImportError: return unittest.skip('''test requires tiktoken''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : Dict )-> str: '''simple docstring''' try: import spacy # noqa F401 except ImportError: return unittest.skip('''test requires spacy''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : int )-> Dict: '''simple docstring''' def _require_spacy_model(_lowerCamelCase : int ): try: import spacy # noqa F401 spacy.load(_lowerCamelCase ) except ImportError: return unittest.skip('''test requires spacy''' )(_lowerCamelCase ) except OSError: return unittest.skip('''test requires spacy model \'{}\''''.format(_lowerCamelCase ) )(_lowerCamelCase ) else: return test_case return _require_spacy_model def _UpperCamelCase (_lowerCamelCase : str )-> Dict: '''simple docstring''' try: import pyspark # noqa F401 except ImportError: return unittest.skip('''test requires pyspark''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : Tuple )-> str: '''simple docstring''' try: import joblibspark # noqa F401 except ImportError: return unittest.skip('''test requires joblibspark''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : Any )-> int: '''simple docstring''' if not _run_slow_tests or _run_slow_tests == 0: __snake_case = unittest.skip('''test is slow''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Any )-> Optional[Any]: '''simple docstring''' if not _run_local_tests or _run_local_tests == 0: __snake_case = unittest.skip('''test is local''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : str )-> int: '''simple docstring''' if not _run_packaged_tests or _run_packaged_tests == 0: __snake_case = unittest.skip('''test is packaged''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Optional[int] )-> str: '''simple docstring''' if not _run_remote_tests or _run_remote_tests == 0: __snake_case = unittest.skip('''test requires remote''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (*_lowerCamelCase : str )-> Optional[int]: '''simple docstring''' def decorate(cls : Optional[Any] ): for name, fn in cls.__dict__.items(): if callable(_lowerCamelCase ) and name.startswith('''test''' ): for decorator in decorators: __snake_case = decorator(_lowerCamelCase ) setattr(cls , _lowerCamelCase , _lowerCamelCase ) return cls return decorate class lowerCAmelCase ( __lowerCAmelCase): pass class lowerCAmelCase ( __lowerCAmelCase): __lowercase : List[str] = 0 __lowercase : Dict = 1 __lowercase : List[Any] = 2 @contextmanager def _UpperCamelCase (_lowerCamelCase : Dict=OfflineSimulationMode.CONNECTION_FAILS , _lowerCamelCase : Optional[int]=1E-16 )-> Tuple: '''simple docstring''' __snake_case = requests.Session().request def timeout_request(_lowerCamelCase : Any , _lowerCamelCase : str , _lowerCamelCase : str , **_lowerCamelCase : Any ): # Change the url to an invalid url so that the connection hangs __snake_case = '''https://10.255.255.1''' if kwargs.get('''timeout''' ) is None: raise RequestWouldHangIndefinitelyError( f'''Tried a call to {url} in offline mode with no timeout set. Please set a timeout.''' ) __snake_case = timeout try: return online_request(_lowerCamelCase , _lowerCamelCase , **_lowerCamelCase ) except Exception as e: # The following changes in the error are just here to make the offline timeout error prettier __snake_case = url __snake_case = e.args[0] __snake_case = (max_retry_error.args[0].replace('''10.255.255.1''' , f'''OfflineMock[{url}]''' ),) __snake_case = (max_retry_error,) raise def raise_connection_error(_lowerCamelCase : Union[str, Any] , _lowerCamelCase : Optional[int] , **_lowerCamelCase : Dict ): raise requests.ConnectionError('''Offline mode is enabled.''' , request=_lowerCamelCase ) if mode is OfflineSimulationMode.CONNECTION_FAILS: with patch('''requests.Session.send''' , _lowerCamelCase ): yield elif mode is OfflineSimulationMode.CONNECTION_TIMES_OUT: # inspired from https://stackoverflow.com/a/904609 with patch('''requests.Session.request''' , _lowerCamelCase ): yield elif mode is OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1: with patch('''datasets.config.HF_DATASETS_OFFLINE''' , _lowerCamelCase ): yield else: raise ValueError('''Please use a value from the OfflineSimulationMode enum.''' ) @contextmanager def _UpperCamelCase (*_lowerCamelCase : Union[str, Any] , **_lowerCamelCase : List[str] )-> Any: '''simple docstring''' __snake_case = str(Path().resolve() ) with tempfile.TemporaryDirectory(*_lowerCamelCase , **_lowerCamelCase ) as tmp_dir: try: os.chdir(_lowerCamelCase ) yield finally: os.chdir(_lowerCamelCase ) @contextmanager def _UpperCamelCase ()-> Optional[int]: '''simple docstring''' import gc gc.collect() __snake_case = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory > 0, "Arrow memory didn't increase." @contextmanager def _UpperCamelCase ()-> List[Any]: '''simple docstring''' import gc gc.collect() __snake_case = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory <= 0, "Arrow memory wasn't expected to increase." def _UpperCamelCase (_lowerCamelCase : Any , _lowerCamelCase : int )-> Any: '''simple docstring''' return deepcopy(_lowerCamelCase ).integers(0 , 1_00 , 10 ).tolist() == deepcopy(_lowerCamelCase ).integers(0 , 1_00 , 10 ).tolist() def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> List[Any]: '''simple docstring''' import decorator from requests.exceptions import HTTPError def _wrapper(_lowerCamelCase : int , *_lowerCamelCase : int , **_lowerCamelCase : Optional[int] ): try: return func(*_lowerCamelCase , **_lowerCamelCase ) except HTTPError as err: if str(_lowerCamelCase ).startswith('''500''' ) or str(_lowerCamelCase ).startswith('''502''' ): pytest.xfail(str(_lowerCamelCase ) ) raise err return decorator.decorator(_wrapper , _lowerCamelCase ) class lowerCAmelCase : def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' __snake_case = returncode __snake_case = stdout __snake_case = stderr async def _UpperCamelCase (_lowerCamelCase : List[str] , _lowerCamelCase : Union[str, Any] )-> Dict: '''simple docstring''' while True: __snake_case = await stream.readline() if line: callback(_lowerCamelCase ) else: break async def _UpperCamelCase (_lowerCamelCase : Tuple , _lowerCamelCase : Optional[int]=None , _lowerCamelCase : Optional[Any]=None , _lowerCamelCase : Tuple=None , _lowerCamelCase : Dict=False , _lowerCamelCase : List[Any]=False )-> _RunOutput: '''simple docstring''' if echo: print('''\nRunning: ''' , ''' '''.join(_lowerCamelCase ) ) __snake_case = await asyncio.create_subprocess_exec( cmd[0] , *cmd[1:] , stdin=_lowerCamelCase , stdout=asyncio.subprocess.PIPE , stderr=asyncio.subprocess.PIPE , env=_lowerCamelCase , ) # note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe # https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait # # If it starts hanging, will need to switch to the following code. The problem is that no data # will be seen until it's done and if it hangs for example there will be no debug info. # out, err = await p.communicate() # return _RunOutput(p.returncode, out, err) __snake_case = [] __snake_case = [] def tee(_lowerCamelCase : int , _lowerCamelCase : List[Any] , _lowerCamelCase : str , _lowerCamelCase : Dict="" ): __snake_case = line.decode('''utf-8''' ).rstrip() sink.append(_lowerCamelCase ) if not quiet: print(_lowerCamelCase , _lowerCamelCase , file=_lowerCamelCase ) # XXX: the timeout doesn't seem to make any difference here await asyncio.wait( [ _read_stream(p.stdout , lambda _lowerCamelCase : tee(_lowerCamelCase , _lowerCamelCase , sys.stdout , label='''stdout:''' ) ), _read_stream(p.stderr , lambda _lowerCamelCase : tee(_lowerCamelCase , _lowerCamelCase , sys.stderr , label='''stderr:''' ) ), ] , timeout=_lowerCamelCase , ) return _RunOutput(await p.wait() , _lowerCamelCase , _lowerCamelCase ) def _UpperCamelCase (_lowerCamelCase : Optional[Any] , _lowerCamelCase : Any=None , _lowerCamelCase : List[str]=None , _lowerCamelCase : Optional[Any]=1_80 , _lowerCamelCase : Dict=False , _lowerCamelCase : int=True )-> _RunOutput: '''simple docstring''' __snake_case = asyncio.get_event_loop() __snake_case = loop.run_until_complete( _stream_subprocess(_lowerCamelCase , env=_lowerCamelCase , stdin=_lowerCamelCase , timeout=_lowerCamelCase , quiet=_lowerCamelCase , echo=_lowerCamelCase ) ) __snake_case = ''' '''.join(_lowerCamelCase ) if result.returncode > 0: __snake_case = '''\n'''.join(result.stderr ) raise RuntimeError( f'''\'{cmd_str}\' failed with returncode {result.returncode}\n\n''' f'''The combined stderr from workers follows:\n{stderr}''' ) # check that the subprocess actually did run and produced some output, should the test rely on # the remote side to do the testing if not result.stdout and not result.stderr: raise RuntimeError(f'''\'{cmd_str}\' produced no output.''' ) return result def _UpperCamelCase ()-> Dict: '''simple docstring''' __snake_case = os.environ.get('''PYTEST_XDIST_WORKER''' , '''gw0''' ) __snake_case = re.sub(R'''^gw''' , '''''' , _lowerCamelCase , 0 , re.M ) return int(_lowerCamelCase ) def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = 2_95_00 __snake_case = pytest_xdist_worker_id() return port + uniq_delta
24
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase_ : Union[str, Any] = logging.get_logger(__name__) UpperCAmelCase_ : List[Any] = { 'tanreinama/GPTSAN-2.8B-spout_is_uniform': ( 'https://huggingface.co/tanreinama/GPTSAN-2.8B-spout_is_uniform/resolve/main/config.json' ), } class UpperCAmelCase__ ( A ): lowerCAmelCase_ = 'gptsan-japanese' lowerCAmelCase_ = [ 'past_key_values', ] lowerCAmelCase_ = { 'hidden_size': 'd_model', 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self : List[str],__A : Union[str, Any]=3_6_0_0_0,__A : Any=1_2_8_0,__A : List[str]=1_0_2_4,__A : List[str]=8_1_9_2,__A : Any=4_0_9_6,__A : int=1_2_8,__A : List[Any]=1_0,__A : Any=0,__A : int=1_6,__A : str=1_6,__A : str=1_2_8,__A : List[str]=0.0,__A : int=1e-5,__A : List[str]=False,__A : List[Any]=0.0,__A : Optional[int]="float32",__A : Any=False,__A : List[Any]=False,__A : Any=False,__A : Dict=0.002,__A : Tuple=False,__A : Optional[Any]=True,__A : Union[str, Any]=3_5_9_9_8,__A : List[Any]=3_5_9_9_5,__A : Tuple=3_5_9_9_9,**__A : List[Any],): _lowerCamelCase : int = vocab_size _lowerCamelCase : List[str] = max_position_embeddings _lowerCamelCase : Dict = d_model _lowerCamelCase : List[str] = d_ff _lowerCamelCase : int = d_ext _lowerCamelCase : Optional[Any] = d_spout _lowerCamelCase : int = num_switch_layers _lowerCamelCase : Dict = num_ext_layers _lowerCamelCase : List[str] = num_switch_layers + num_ext_layers _lowerCamelCase : List[str] = num_heads _lowerCamelCase : Tuple = num_experts _lowerCamelCase : List[str] = expert_capacity _lowerCamelCase : str = dropout_rate _lowerCamelCase : List[Any] = layer_norm_epsilon _lowerCamelCase : Optional[int] = router_bias _lowerCamelCase : List[str] = router_jitter_noise _lowerCamelCase : int = router_dtype _lowerCamelCase : Optional[int] = router_ignore_padding_tokens _lowerCamelCase : Optional[Any] = output_hidden_states _lowerCamelCase : Optional[int] = output_attentions _lowerCamelCase : List[Any] = initializer_factor _lowerCamelCase : Union[str, Any] = output_router_logits _lowerCamelCase : Optional[Any] = use_cache super().__init__( separator_token_id=__A,pad_token_id=__A,eos_token_id=__A,**__A,)
44
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' __snake_case = [[0 for _ in range(_lowerCamelCase )] for _ in range(m + 1 )] for i in range(m + 1 ): __snake_case = 1 for n in range(m + 1 ): for k in range(1 , _lowerCamelCase ): 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: UpperCAmelCase_ : List[str] = int(input('''Enter a number: ''').strip()) print(partition(n)) except ValueError: print('''Please enter a number.''') else: try: UpperCAmelCase_ : Union[str, Any] = int(sys.argv[1]) print(partition(n)) except ValueError: print('''Please pass a number.''')
24
0
import unittest import numpy as np from transformers import RobertaConfig, 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(): from transformers.models.roberta.modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, ) class lowerCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __init__( self :Tuple , lowerCamelCase__ :str , lowerCamelCase__ :List[str]=13 , lowerCamelCase__ :Any=7 , lowerCamelCase__ :Dict=True , lowerCamelCase__ :Union[str, Any]=True , lowerCamelCase__ :Union[str, Any]=True , lowerCamelCase__ :Optional[int]=True , lowerCamelCase__ :List[str]=99 , lowerCamelCase__ :Optional[Any]=32 , lowerCamelCase__ :Any=5 , lowerCamelCase__ :Any=4 , lowerCamelCase__ :List[Any]=37 , lowerCamelCase__ :Optional[int]="gelu" , lowerCamelCase__ :Tuple=0.1 , lowerCamelCase__ :List[Any]=0.1 , lowerCamelCase__ :Tuple=5_12 , lowerCamelCase__ :int=16 , lowerCamelCase__ :Optional[Any]=2 , lowerCamelCase__ :Optional[int]=0.02 , lowerCamelCase__ :Optional[Any]=4 , ): UpperCamelCase__ :Union[str, Any] = parent UpperCamelCase__ :Optional[Any] = batch_size UpperCamelCase__ :Optional[Any] = seq_length UpperCamelCase__ :str = is_training UpperCamelCase__ :int = use_attention_mask UpperCamelCase__ :Dict = use_token_type_ids UpperCamelCase__ :int = use_labels UpperCamelCase__ :List[str] = vocab_size UpperCamelCase__ :Optional[int] = hidden_size UpperCamelCase__ :int = num_hidden_layers UpperCamelCase__ :Optional[int] = num_attention_heads UpperCamelCase__ :Dict = intermediate_size UpperCamelCase__ :str = hidden_act UpperCamelCase__ :List[Any] = hidden_dropout_prob UpperCamelCase__ :List[str] = attention_probs_dropout_prob UpperCamelCase__ :Optional[int] = max_position_embeddings UpperCamelCase__ :Dict = type_vocab_size UpperCamelCase__ :Dict = type_sequence_label_size UpperCamelCase__ :Dict = initializer_range UpperCamelCase__ :Union[str, Any] = num_choices def __a ( self :Optional[Any] ): UpperCamelCase__ :Any = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase__ :Optional[Any] = None if self.use_attention_mask: UpperCamelCase__ :List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase__ :List[Any] = None if self.use_token_type_ids: UpperCamelCase__ :Any = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCamelCase__ :Optional[int] = RobertaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowerCamelCase__ , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def __a ( self :int ): UpperCamelCase__ :Optional[Any] = self.prepare_config_and_inputs() UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Tuple = config_and_inputs UpperCamelCase__ :Union[str, Any] = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict def __a ( self :List[Any] ): UpperCamelCase__ :Dict = self.prepare_config_and_inputs() UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[Any] = config_and_inputs UpperCamelCase__ :Optional[Any] = True UpperCamelCase__ :Any = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) UpperCamelCase__ :List[str] = 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 class lowerCAmelCase_ ( lowercase , unittest.TestCase ): """simple docstring""" _snake_case : Any = True _snake_case : Union[str, Any] = ( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def __a ( self :Union[str, Any] ): UpperCamelCase__ :Optional[int] = FlaxRobertaModelTester(self ) @slow def __a ( self :Any ): for model_class_name in self.all_model_classes: UpperCamelCase__ :int = model_class_name.from_pretrained("""roberta-base""" , from_pt=lowerCamelCase__ ) UpperCamelCase__ :Optional[Any] = model(np.ones((1, 1) ) ) self.assertIsNotNone(lowerCamelCase__ )
45
'''simple docstring''' import argparse import os import re UpperCAmelCase_ : List[str] = '''src/transformers/models/auto''' # re pattern that matches mapping introductions: # SUPER_MODEL_MAPPING_NAMES = OrderedDict or SUPER_MODEL_MAPPING = OrderedDict UpperCAmelCase_ : Tuple = re.compile(R'''[A-Z_]+_MAPPING(\s+|_[A-Z_]+\s+)=\s+OrderedDict''') # re pattern that matches identifiers in mappings UpperCAmelCase_ : Dict = re.compile(R'''\s*\(\s*"(\S[^"]+)"''') def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : bool = False )-> str: '''simple docstring''' with open(_lowerCamelCase , '''r''' , encoding='''utf-8''' ) as f: __snake_case = f.read() __snake_case = content.split('''\n''' ) __snake_case = [] __snake_case = 0 while line_idx < len(_lowerCamelCase ): if _re_intro_mapping.search(lines[line_idx] ) is not None: __snake_case = len(re.search(R'''^(\s*)\S''' , lines[line_idx] ).groups()[0] ) + 8 # Start of a new mapping! while not lines[line_idx].startswith(''' ''' * indent + '''(''' ): new_lines.append(lines[line_idx] ) line_idx += 1 __snake_case = [] while lines[line_idx].strip() != "]": # Blocks either fit in one line or not if lines[line_idx].strip() == "(": __snake_case = line_idx while not lines[line_idx].startswith(''' ''' * indent + ''')''' ): line_idx += 1 blocks.append('''\n'''.join(lines[start_idx : line_idx + 1] ) ) else: blocks.append(lines[line_idx] ) line_idx += 1 # Sort blocks by their identifiers __snake_case = sorted(_lowerCamelCase , key=lambda _lowerCamelCase : _re_identifier.search(_lowerCamelCase ).groups()[0] ) new_lines += blocks else: new_lines.append(lines[line_idx] ) line_idx += 1 if overwrite: with open(_lowerCamelCase , '''w''' , encoding='''utf-8''' ) as f: f.write('''\n'''.join(_lowerCamelCase ) ) elif "\n".join(_lowerCamelCase ) != content: return True def _UpperCamelCase (_lowerCamelCase : bool = False )-> Tuple: '''simple docstring''' __snake_case = [os.path.join(_lowerCamelCase , _lowerCamelCase ) for f in os.listdir(_lowerCamelCase ) if f.endswith('''.py''' )] __snake_case = [sort_auto_mapping(_lowerCamelCase , overwrite=_lowerCamelCase ) for fname in fnames] if not overwrite and any(_lowerCamelCase ): __snake_case = [f for f, d in zip(_lowerCamelCase , _lowerCamelCase ) if d] raise ValueError( f'''The following files have auto mappings that need sorting: {", ".join(_lowerCamelCase )}. Run `make style` to fix''' ''' this.''' ) if __name__ == "__main__": UpperCAmelCase_ : str = argparse.ArgumentParser() parser.add_argument('''--check_only''', action='''store_true''', help='''Whether to only check or fix style.''') UpperCAmelCase_ : List[Any] = parser.parse_args() sort_all_auto_mappings(not args.check_only)
24
0
"""simple docstring""" import os from distutils.util import strtobool def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase ) -> List[str]: '''simple docstring''' for e in env_keys: _lowerCamelCase : List[Any] = int(os.environ.get(_lowerCamelCase , -1 ) ) if val >= 0: return val return default def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase=False ) -> Optional[int]: '''simple docstring''' _lowerCamelCase : Any = os.environ.get(_lowerCamelCase , str(_lowerCamelCase ) ) return strtobool(_lowerCamelCase ) == 1 # As its name indicates `strtobool` actually returns an int... def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase="no" ) -> Optional[Any]: '''simple docstring''' _lowerCamelCase : Tuple = os.environ.get(_lowerCamelCase , str(_lowerCamelCase ) ) return value
46
'''simple docstring''' import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _UpperCamelCase (*_lowerCamelCase : str , _lowerCamelCase : Optional[Union[Dict, Any]] = None , _lowerCamelCase : List[Any]=True , _lowerCamelCase : str=2 )-> str: '''simple docstring''' from .. import __version__ __snake_case = take_from __snake_case = () if not isinstance(args[0] , _lowerCamelCase ): __snake_case = (args,) for attribute, version_name, message in args: if version.parse(version.parse(_lowerCamelCase ).base_version ) >= version.parse(_lowerCamelCase ): raise ValueError( f'''The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\'''' f''' version {__version__} is >= {version_name}''' ) __snake_case = None if isinstance(_lowerCamelCase , _lowerCamelCase ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(_lowerCamelCase ),) __snake_case = f'''The `{attribute}` argument is deprecated and will be removed in version {version_name}.''' elif hasattr(_lowerCamelCase , _lowerCamelCase ): values += (getattr(_lowerCamelCase , _lowerCamelCase ),) __snake_case = f'''The `{attribute}` attribute is deprecated and will be removed in version {version_name}.''' elif deprecated_kwargs is None: __snake_case = f'''`{attribute}` is deprecated and will be removed in version {version_name}.''' if warning is not None: __snake_case = warning + ''' ''' if standard_warn else '''''' warnings.warn(warning + message , _lowerCamelCase , stacklevel=_lowerCamelCase ) if isinstance(_lowerCamelCase , _lowerCamelCase ) and len(_lowerCamelCase ) > 0: __snake_case = inspect.getouterframes(inspect.currentframe() )[1] __snake_case = call_frame.filename __snake_case = call_frame.lineno __snake_case = call_frame.function __snake_case , __snake_case = next(iter(deprecated_kwargs.items() ) ) raise TypeError(f'''{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`''' ) if len(_lowerCamelCase ) == 0: return elif len(_lowerCamelCase ) == 1: return values[0] return values
24
0
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 UpperCAmelCase__ ( lowerCamelCase_ : Tuple ): __a : List[str] = 3_8_4 if "tiny" in model_name: __a : Optional[int] = [3, 3, 9, 3] __a : Tuple = [9_6, 1_9_2, 3_8_4, 7_6_8] if "small" in model_name: __a : Any = [3, 3, 2_7, 3] __a : List[Any] = [9_6, 1_9_2, 3_8_4, 7_6_8] if "base" in model_name: __a : str = [3, 3, 2_7, 3] __a : Dict = [1_2_8, 2_5_6, 5_1_2, 1_0_2_4] __a : Optional[int] = 5_1_2 if "large" in model_name: __a : Tuple = [3, 3, 2_7, 3] __a : str = [1_9_2, 3_8_4, 7_6_8, 1_5_3_6] __a : Dict = 7_6_8 if "xlarge" in model_name: __a : int = [3, 3, 2_7, 3] __a : Optional[Any] = [2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] __a : Optional[int] = 1_0_2_4 # set label information __a : Tuple = 1_5_0 __a : Optional[int] = 'huggingface/label-files' __a : Any = 'ade20k-id2label.json' __a : Dict = json.load(open(hf_hub_download(lowerCamelCase_ , lowerCamelCase_ , repo_type='dataset' ) , 'r' ) ) __a : Union[str, Any] = {int(lowerCamelCase_ ): v for k, v in idalabel.items()} __a : Any = {v: k for k, v in idalabel.items()} __a : Optional[int] = ConvNextConfig( depths=lowerCamelCase_ , hidden_sizes=lowerCamelCase_ , out_features=['stage1', 'stage2', 'stage3', 'stage4'] ) __a : Optional[int] = UperNetConfig( backbone_config=lowerCamelCase_ , auxiliary_in_channels=lowerCamelCase_ , num_labels=lowerCamelCase_ , idalabel=lowerCamelCase_ , labelaid=lowerCamelCase_ , ) return config def UpperCAmelCase__ ( lowerCamelCase_ : str ): __a : int = [] # 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 UpperCAmelCase__ ( lowerCamelCase_ : int , lowerCamelCase_ : Optional[Any] , lowerCamelCase_ : Dict ): __a : List[str] = dct.pop(lowerCamelCase_ ) __a : Optional[Any] = val def UpperCAmelCase__ ( lowerCamelCase_ : str , lowerCamelCase_ : Union[str, Any] , lowerCamelCase_ : Tuple ): __a : str = { '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 : Any = model_name_to_url[model_name] __a : List[Any] = torch.hub.load_state_dict_from_url(lowerCamelCase_ , map_location='cpu' )['state_dict'] __a : Optional[int] = get_upernet_config(lowerCamelCase_ ) __a : Any = UperNetForSemanticSegmentation(lowerCamelCase_ ) model.eval() # replace "bn" => "batch_norm" for key in state_dict.copy().keys(): __a : Dict = state_dict.pop(lowerCamelCase_ ) if "bn" in key: __a : Dict = key.replace('bn' , 'batch_norm' ) __a : int = val # rename keys __a : Dict = create_rename_keys(lowerCamelCase_ ) for src, dest in rename_keys: rename_key(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) model.load_state_dict(lowerCamelCase_ ) # verify on image __a : str = 'https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg' __a : Optional[Any] = Image.open(requests.get(lowerCamelCase_ , stream=lowerCamelCase_ ).raw ).convert('RGB' ) __a : Union[str, Any] = SegformerImageProcessor() __a : str = processor(lowerCamelCase_ , return_tensors='pt' ).pixel_values with torch.no_grad(): __a : Union[str, Any] = model(lowerCamelCase_ ) if model_name == "upernet-convnext-tiny": __a : Optional[Any] = torch.tensor( [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]] ) elif model_name == "upernet-convnext-small": __a : Tuple = torch.tensor( [[-8.8236, -8.8236, -8.6771], [-8.8236, -8.8236, -8.6771], [-8.7638, -8.7638, -8.6240]] ) elif model_name == "upernet-convnext-base": __a : Optional[int] = torch.tensor( [[-8.8558, -8.8558, -8.6905], [-8.8558, -8.8558, -8.6905], [-8.7669, -8.7669, -8.6021]] ) elif model_name == "upernet-convnext-large": __a : str = torch.tensor( [[-8.6660, -8.6660, -8.6210], [-8.6660, -8.6660, -8.6210], [-8.6310, -8.6310, -8.5964]] ) elif model_name == "upernet-convnext-xlarge": __a : int = torch.tensor( [[-8.4980, -8.4980, -8.3977], [-8.4980, -8.4980, -8.3977], [-8.4379, -8.4379, -8.3412]] ) print('Logits:' , outputs.logits[0, 0, :3, :3] ) assert torch.allclose(outputs.logits[0, 0, :3, :3] , lowerCamelCase_ , atol=1e-4 ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: print(f'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(lowerCamelCase_ ) print(f'''Saving processor to {pytorch_dump_folder_path}''' ) processor.save_pretrained(lowerCamelCase_ ) if push_to_hub: print(f'''Pushing model and processor for {model_name} to hub''' ) model.push_to_hub(f'''openmmlab/{model_name}''' ) processor.push_to_hub(f'''openmmlab/{model_name}''' ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ = 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.''' ) SCREAMING_SNAKE_CASE__ = parser.parse_args() convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
47
'''simple docstring''' import argparse import re from pathlib import Path import requests import torch from PIL import Image from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor from transformers import ( EfficientFormerConfig, EfficientFormerForImageClassificationWithTeacher, EfficientFormerImageProcessor, ) from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def _UpperCamelCase (_lowerCamelCase : int , _lowerCamelCase : str )-> List[str]: '''simple docstring''' __snake_case = old_name if "patch_embed" in old_name: __snake_case , __snake_case , __snake_case = old_name.split('''.''' ) if layer == "0": __snake_case = old_name.replace('''0''' , '''convolution1''' ) elif layer == "1": __snake_case = old_name.replace('''1''' , '''batchnorm_before''' ) elif layer == "3": __snake_case = old_name.replace('''3''' , '''convolution2''' ) else: __snake_case = old_name.replace('''4''' , '''batchnorm_after''' ) if "network" in old_name and re.search(R'''\d\.\d''' , _lowerCamelCase ): __snake_case = R'''\b\d{2}\b''' if bool(re.search(_lowerCamelCase , _lowerCamelCase ) ): __snake_case = re.search(R'''\d\.\d\d.''' , _lowerCamelCase ).group() else: __snake_case = re.search(R'''\d\.\d.''' , _lowerCamelCase ).group() if int(match[0] ) < 6: __snake_case = old_name.replace(_lowerCamelCase , '''''' ) __snake_case = trimmed_name.replace('''network''' , match[0] + '''.meta4D_layers.blocks.''' + match[2:-1] ) __snake_case = '''intermediate_stages.''' + trimmed_name else: __snake_case = old_name.replace(_lowerCamelCase , '''''' ) if int(match[2] ) < num_meta4D_last_stage: __snake_case = trimmed_name.replace('''network''' , '''meta4D_layers.blocks.''' + match[2] ) else: __snake_case = str(int(match[2] ) - num_meta4D_last_stage ) __snake_case = trimmed_name.replace('''network''' , '''meta3D_layers.blocks.''' + layer_index ) if "norm1" in old_name: __snake_case = trimmed_name.replace('''norm1''' , '''layernorm1''' ) elif "norm2" in old_name: __snake_case = trimmed_name.replace('''norm2''' , '''layernorm2''' ) elif "fc1" in old_name: __snake_case = trimmed_name.replace('''fc1''' , '''linear_in''' ) elif "fc2" in old_name: __snake_case = trimmed_name.replace('''fc2''' , '''linear_out''' ) __snake_case = '''last_stage.''' + trimmed_name elif "network" in old_name and re.search(R'''.\d.''' , _lowerCamelCase ): __snake_case = old_name.replace('''network''' , '''intermediate_stages''' ) if "fc" in new_name: __snake_case = new_name.replace('''fc''' , '''convolution''' ) elif ("norm1" in new_name) and ("layernorm1" not in new_name): __snake_case = new_name.replace('''norm1''' , '''batchnorm_before''' ) elif ("norm2" in new_name) and ("layernorm2" not in new_name): __snake_case = new_name.replace('''norm2''' , '''batchnorm_after''' ) if "proj" in new_name: __snake_case = new_name.replace('''proj''' , '''projection''' ) if "dist_head" in new_name: __snake_case = new_name.replace('''dist_head''' , '''distillation_classifier''' ) elif "head" in new_name: __snake_case = new_name.replace('''head''' , '''classifier''' ) elif "patch_embed" in new_name: __snake_case = '''efficientformer.''' + new_name elif new_name == "norm.weight" or new_name == "norm.bias": __snake_case = new_name.replace('''norm''' , '''layernorm''' ) __snake_case = '''efficientformer.''' + new_name else: __snake_case = '''efficientformer.encoder.''' + new_name return new_name def _UpperCamelCase (_lowerCamelCase : str , _lowerCamelCase : Tuple )-> List[str]: '''simple docstring''' for key in checkpoint.copy().keys(): __snake_case = checkpoint.pop(_lowerCamelCase ) __snake_case = val return checkpoint def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = '''http://images.cocodataset.org/val2017/000000039769.jpg''' __snake_case = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return image def _UpperCamelCase (_lowerCamelCase : Path , _lowerCamelCase : Path , _lowerCamelCase : Path , _lowerCamelCase : bool )-> Optional[Any]: '''simple docstring''' __snake_case = torch.load(_lowerCamelCase , map_location='''cpu''' )['''model'''] __snake_case = EfficientFormerConfig.from_json_file(_lowerCamelCase ) __snake_case = EfficientFormerForImageClassificationWithTeacher(_lowerCamelCase ) __snake_case = '''_'''.join(checkpoint_path.split('''/''' )[-1].split('''.''' )[0].split('''_''' )[:-1] ) __snake_case = config.depths[-1] - config.num_metaad_blocks + 1 __snake_case = convert_torch_checkpoint(_lowerCamelCase , _lowerCamelCase ) model.load_state_dict(_lowerCamelCase ) model.eval() __snake_case = { '''bilinear''': PILImageResampling.BILINEAR, '''bicubic''': PILImageResampling.BICUBIC, '''nearest''': PILImageResampling.NEAREST, } # prepare image __snake_case = prepare_img() __snake_case = 2_56 __snake_case = 2_24 __snake_case = EfficientFormerImageProcessor( size={'''shortest_edge''': image_size} , crop_size={'''height''': crop_size, '''width''': crop_size} , resample=pillow_resamplings['''bicubic'''] , ) __snake_case = processor(images=_lowerCamelCase , return_tensors='''pt''' ).pixel_values # original processing pipeline __snake_case = Compose( [ Resize(_lowerCamelCase , interpolation=pillow_resamplings['''bicubic'''] ), CenterCrop(_lowerCamelCase ), ToTensor(), Normalize(_lowerCamelCase , _lowerCamelCase ), ] ) __snake_case = image_transforms(_lowerCamelCase ).unsqueeze(0 ) assert torch.allclose(_lowerCamelCase , _lowerCamelCase ) __snake_case = model(_lowerCamelCase ) __snake_case = outputs.logits __snake_case = (1, 10_00) if "l1" in model_name: __snake_case = torch.Tensor( [-0.1312, 0.4353, -1.0499, -0.5124, 0.4183, -0.6793, -1.3777, -0.0893, -0.7358, -2.4328] ) assert torch.allclose(logits[0, :10] , _lowerCamelCase , atol=1E-3 ) assert logits.shape == expected_shape elif "l3" in model_name: __snake_case = torch.Tensor( [-1.3150, -1.5456, -1.2556, -0.8496, -0.7127, -0.7897, -0.9728, -0.3052, 0.3751, -0.3127] ) assert torch.allclose(logits[0, :10] , _lowerCamelCase , atol=1E-3 ) assert logits.shape == expected_shape elif "l7" in model_name: __snake_case = torch.Tensor( [-1.0283, -1.4131, -0.5644, -1.3115, -0.5785, -1.2049, -0.7528, 0.1992, -0.3822, -0.0878] ) assert logits.shape == expected_shape else: raise ValueError( f'''Unknown model checkpoint: {checkpoint_path}. Supported version of efficientformer are l1, l3 and l7''' ) # Save Checkpoints Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase ) model.save_pretrained(_lowerCamelCase ) print(f'''Checkpoint successfuly converted. Model saved at {pytorch_dump_path}''' ) processor.save_pretrained(_lowerCamelCase ) print(f'''Processor successfuly saved at {pytorch_dump_path}''' ) if push_to_hub: print('''Pushing model to the hub...''' ) model.push_to_hub( repo_id=f'''Bearnardd/{pytorch_dump_path}''' , commit_message='''Add model''' , use_temp_dir=_lowerCamelCase , ) processor.push_to_hub( repo_id=f'''Bearnardd/{pytorch_dump_path}''' , commit_message='''Add image processor''' , use_temp_dir=_lowerCamelCase , ) if __name__ == "__main__": UpperCAmelCase_ : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--pytorch_model_path''', default=None, type=str, required=True, help='''Path to EfficientFormer pytorch checkpoint.''', ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The json file for EfficientFormer model config.''', ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') parser.add_argument( '''--no-push_to_hub''', dest='''push_to_hub''', action='''store_false''', help='''Do not push model and image processor to the hub''', ) parser.set_defaults(push_to_hub=True) UpperCAmelCase_ : Union[str, Any] = parser.parse_args() convert_efficientformer_checkpoint( checkpoint_path=args.pytorch_model_path, efficientformer_config_file=args.config_file, pytorch_dump_path=args.pytorch_dump_path, push_to_hub=args.push_to_hub, )
24
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCAmelCase__ : Union[str, Any] = { "configuration_conditional_detr": [ "CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP", "ConditionalDetrConfig", "ConditionalDetrOnnxConfig", ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ : Optional[Any] = ["ConditionalDetrFeatureExtractor"] UpperCAmelCase__ : Dict = ["ConditionalDetrImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ : List[str] = [ "CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST", "ConditionalDetrForObjectDetection", "ConditionalDetrForSegmentation", "ConditionalDetrModel", "ConditionalDetrPreTrainedModel", ] if TYPE_CHECKING: from .configuration_conditional_detr import ( CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP, ConditionalDetrConfig, ConditionalDetrOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_conditional_detr import ConditionalDetrFeatureExtractor from .image_processing_conditional_detr import ConditionalDetrImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_conditional_detr import ( CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST, ConditionalDetrForObjectDetection, ConditionalDetrForSegmentation, ConditionalDetrModel, ConditionalDetrPreTrainedModel, ) else: import sys UpperCAmelCase__ : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
48
'''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 MobileViTImageProcessor class lowerCAmelCase ( unittest.TestCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=18 , __SCREAMING_SNAKE_CASE=30 , __SCREAMING_SNAKE_CASE=400 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , ) -> Tuple: '''simple docstring''' __snake_case = size if size is not None else {'''shortest_edge''': 20} __snake_case = crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} __snake_case = parent __snake_case = batch_size __snake_case = num_channels __snake_case = image_size __snake_case = min_resolution __snake_case = max_resolution __snake_case = do_resize __snake_case = size __snake_case = do_center_crop __snake_case = crop_size __snake_case = do_flip_channel_order def lowerCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_flip_channel_order": self.do_flip_channel_order, } @require_torch @require_vision class lowerCAmelCase ( __lowerCAmelCase , unittest.TestCase): __lowercase : Union[str, Any] = MobileViTImageProcessor if is_vision_available() else None def lowerCAmelCase ( self ) -> str: '''simple docstring''' __snake_case = MobileViTImageProcessingTester(self ) @property def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def lowerCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_resize''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''size''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_center_crop''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''center_crop''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_flip_channel_order''' ) ) def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 20} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) __snake_case = 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 lowerCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' pass def lowerCAmelCase ( self ) -> str: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __snake_case = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input __snake_case = 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 __snake_case = image_processing(__SCREAMING_SNAKE_CASE , 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 lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __snake_case = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , numpify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) # Test not batched input __snake_case = 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 __snake_case = image_processing(__SCREAMING_SNAKE_CASE , 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 lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __snake_case = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , torchify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) # Test not batched input __snake_case = 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 __snake_case = image_processing(__SCREAMING_SNAKE_CASE , 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'''], ) , )
24
0
"""simple docstring""" def lowercase__ ( snake_case_ :str , snake_case_ :str ): __UpperCAmelCase = len(snake_case_ ) __UpperCAmelCase = len(snake_case_ ) __UpperCAmelCase = [[False for _ in range(m + 1 )] for _ in range(n + 1 )] __UpperCAmelCase = True for i in range(snake_case_ ): for j in range(m + 1 ): if dp[i][j]: if j < m and a[i].upper() == b[j]: __UpperCAmelCase = True if a[i].islower(): __UpperCAmelCase = True return dp[n][m] if __name__ == "__main__": import doctest doctest.testmod()
49
'''simple docstring''' from typing import Optional import pyspark from .. import Features, NamedSplit from ..download import DownloadMode from ..packaged_modules.spark.spark import Spark from .abc import AbstractDatasetReader class lowerCAmelCase ( __lowerCAmelCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = "arrow" , **__SCREAMING_SNAKE_CASE , ) -> Union[str, Any]: '''simple docstring''' super().__init__( split=__SCREAMING_SNAKE_CASE , features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE , keep_in_memory=__SCREAMING_SNAKE_CASE , streaming=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) __snake_case = load_from_cache_file __snake_case = file_format __snake_case = Spark( df=__SCREAMING_SNAKE_CASE , features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE , working_dir=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) def lowerCAmelCase ( self ) -> str: '''simple docstring''' if self.streaming: return self.builder.as_streaming_dataset(split=self.split ) __snake_case = None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD self.builder.download_and_prepare( download_mode=__SCREAMING_SNAKE_CASE , file_format=self._file_format , ) return self.builder.as_dataset(split=self.split )
24
0
'''simple docstring''' from collections import OrderedDict from ...utils import logging from .auto_factory import _BaseAutoModelClass, _LazyAutoMapping, auto_class_update from .configuration_auto import CONFIG_MAPPING_NAMES UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) UpperCamelCase : int = OrderedDict( [ # Base model mapping ('albert', 'FlaxAlbertModel'), ('bart', 'FlaxBartModel'), ('beit', 'FlaxBeitModel'), ('bert', 'FlaxBertModel'), ('big_bird', 'FlaxBigBirdModel'), ('blenderbot', 'FlaxBlenderbotModel'), ('blenderbot-small', 'FlaxBlenderbotSmallModel'), ('clip', 'FlaxCLIPModel'), ('distilbert', 'FlaxDistilBertModel'), ('electra', 'FlaxElectraModel'), ('gpt-sw3', 'FlaxGPT2Model'), ('gpt2', 'FlaxGPT2Model'), ('gpt_neo', 'FlaxGPTNeoModel'), ('gptj', 'FlaxGPTJModel'), ('longt5', 'FlaxLongT5Model'), ('marian', 'FlaxMarianModel'), ('mbart', 'FlaxMBartModel'), ('mt5', 'FlaxMT5Model'), ('opt', 'FlaxOPTModel'), ('pegasus', 'FlaxPegasusModel'), ('regnet', 'FlaxRegNetModel'), ('resnet', 'FlaxResNetModel'), ('roberta', 'FlaxRobertaModel'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormModel'), ('roformer', 'FlaxRoFormerModel'), ('t5', 'FlaxT5Model'), ('vision-text-dual-encoder', 'FlaxVisionTextDualEncoderModel'), ('vit', 'FlaxViTModel'), ('wav2vec2', 'FlaxWav2Vec2Model'), ('whisper', 'FlaxWhisperModel'), ('xglm', 'FlaxXGLMModel'), ('xlm-roberta', 'FlaxXLMRobertaModel'), ] ) UpperCamelCase : Optional[Any] = OrderedDict( [ # Model for pre-training mapping ('albert', 'FlaxAlbertForPreTraining'), ('bart', 'FlaxBartForConditionalGeneration'), ('bert', 'FlaxBertForPreTraining'), ('big_bird', 'FlaxBigBirdForPreTraining'), ('electra', 'FlaxElectraForPreTraining'), ('longt5', 'FlaxLongT5ForConditionalGeneration'), ('mbart', 'FlaxMBartForConditionalGeneration'), ('mt5', 'FlaxMT5ForConditionalGeneration'), ('roberta', 'FlaxRobertaForMaskedLM'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForMaskedLM'), ('roformer', 'FlaxRoFormerForMaskedLM'), ('t5', 'FlaxT5ForConditionalGeneration'), ('wav2vec2', 'FlaxWav2Vec2ForPreTraining'), ('whisper', 'FlaxWhisperForConditionalGeneration'), ('xlm-roberta', 'FlaxXLMRobertaForMaskedLM'), ] ) UpperCamelCase : List[str] = OrderedDict( [ # Model for Masked LM mapping ('albert', 'FlaxAlbertForMaskedLM'), ('bart', 'FlaxBartForConditionalGeneration'), ('bert', 'FlaxBertForMaskedLM'), ('big_bird', 'FlaxBigBirdForMaskedLM'), ('distilbert', 'FlaxDistilBertForMaskedLM'), ('electra', 'FlaxElectraForMaskedLM'), ('mbart', 'FlaxMBartForConditionalGeneration'), ('roberta', 'FlaxRobertaForMaskedLM'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForMaskedLM'), ('roformer', 'FlaxRoFormerForMaskedLM'), ('xlm-roberta', 'FlaxXLMRobertaForMaskedLM'), ] ) UpperCamelCase : Optional[Any] = OrderedDict( [ # Model for Seq2Seq Causal LM mapping ('bart', 'FlaxBartForConditionalGeneration'), ('blenderbot', 'FlaxBlenderbotForConditionalGeneration'), ('blenderbot-small', 'FlaxBlenderbotSmallForConditionalGeneration'), ('encoder-decoder', 'FlaxEncoderDecoderModel'), ('longt5', 'FlaxLongT5ForConditionalGeneration'), ('marian', 'FlaxMarianMTModel'), ('mbart', 'FlaxMBartForConditionalGeneration'), ('mt5', 'FlaxMT5ForConditionalGeneration'), ('pegasus', 'FlaxPegasusForConditionalGeneration'), ('t5', 'FlaxT5ForConditionalGeneration'), ] ) UpperCamelCase : Optional[Any] = OrderedDict( [ # Model for Image-classsification ('beit', 'FlaxBeitForImageClassification'), ('regnet', 'FlaxRegNetForImageClassification'), ('resnet', 'FlaxResNetForImageClassification'), ('vit', 'FlaxViTForImageClassification'), ] ) UpperCamelCase : Optional[int] = OrderedDict( [ ('vision-encoder-decoder', 'FlaxVisionEncoderDecoderModel'), ] ) UpperCamelCase : Dict = OrderedDict( [ # Model for Causal LM mapping ('bart', 'FlaxBartForCausalLM'), ('bert', 'FlaxBertForCausalLM'), ('big_bird', 'FlaxBigBirdForCausalLM'), ('electra', 'FlaxElectraForCausalLM'), ('gpt-sw3', 'FlaxGPT2LMHeadModel'), ('gpt2', 'FlaxGPT2LMHeadModel'), ('gpt_neo', 'FlaxGPTNeoForCausalLM'), ('gptj', 'FlaxGPTJForCausalLM'), ('opt', 'FlaxOPTForCausalLM'), ('roberta', 'FlaxRobertaForCausalLM'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForCausalLM'), ('xglm', 'FlaxXGLMForCausalLM'), ('xlm-roberta', 'FlaxXLMRobertaForCausalLM'), ] ) UpperCamelCase : List[Any] = OrderedDict( [ # Model for Sequence Classification mapping ('albert', 'FlaxAlbertForSequenceClassification'), ('bart', 'FlaxBartForSequenceClassification'), ('bert', 'FlaxBertForSequenceClassification'), ('big_bird', 'FlaxBigBirdForSequenceClassification'), ('distilbert', 'FlaxDistilBertForSequenceClassification'), ('electra', 'FlaxElectraForSequenceClassification'), ('mbart', 'FlaxMBartForSequenceClassification'), ('roberta', 'FlaxRobertaForSequenceClassification'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForSequenceClassification'), ('roformer', 'FlaxRoFormerForSequenceClassification'), ('xlm-roberta', 'FlaxXLMRobertaForSequenceClassification'), ] ) UpperCamelCase : List[str] = OrderedDict( [ # Model for Question Answering mapping ('albert', 'FlaxAlbertForQuestionAnswering'), ('bart', 'FlaxBartForQuestionAnswering'), ('bert', 'FlaxBertForQuestionAnswering'), ('big_bird', 'FlaxBigBirdForQuestionAnswering'), ('distilbert', 'FlaxDistilBertForQuestionAnswering'), ('electra', 'FlaxElectraForQuestionAnswering'), ('mbart', 'FlaxMBartForQuestionAnswering'), ('roberta', 'FlaxRobertaForQuestionAnswering'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForQuestionAnswering'), ('roformer', 'FlaxRoFormerForQuestionAnswering'), ('xlm-roberta', 'FlaxXLMRobertaForQuestionAnswering'), ] ) UpperCamelCase : int = OrderedDict( [ # Model for Token Classification mapping ('albert', 'FlaxAlbertForTokenClassification'), ('bert', 'FlaxBertForTokenClassification'), ('big_bird', 'FlaxBigBirdForTokenClassification'), ('distilbert', 'FlaxDistilBertForTokenClassification'), ('electra', 'FlaxElectraForTokenClassification'), ('roberta', 'FlaxRobertaForTokenClassification'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForTokenClassification'), ('roformer', 'FlaxRoFormerForTokenClassification'), ('xlm-roberta', 'FlaxXLMRobertaForTokenClassification'), ] ) UpperCamelCase : Tuple = OrderedDict( [ # Model for Multiple Choice mapping ('albert', 'FlaxAlbertForMultipleChoice'), ('bert', 'FlaxBertForMultipleChoice'), ('big_bird', 'FlaxBigBirdForMultipleChoice'), ('distilbert', 'FlaxDistilBertForMultipleChoice'), ('electra', 'FlaxElectraForMultipleChoice'), ('roberta', 'FlaxRobertaForMultipleChoice'), ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForMultipleChoice'), ('roformer', 'FlaxRoFormerForMultipleChoice'), ('xlm-roberta', 'FlaxXLMRobertaForMultipleChoice'), ] ) UpperCamelCase : Tuple = OrderedDict( [ ('bert', 'FlaxBertForNextSentencePrediction'), ] ) UpperCamelCase : str = OrderedDict( [ ('speech-encoder-decoder', 'FlaxSpeechEncoderDecoderModel'), ('whisper', 'FlaxWhisperForConditionalGeneration'), ] ) UpperCamelCase : Optional[int] = OrderedDict( [ ('whisper', 'FlaxWhisperForAudioClassification'), ] ) UpperCamelCase : int = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_MAPPING_NAMES) UpperCamelCase : List[str] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_PRETRAINING_MAPPING_NAMES) UpperCamelCase : Optional[Any] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MASKED_LM_MAPPING_NAMES) UpperCamelCase : Optional[Any] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES ) UpperCamelCase : str = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES ) UpperCamelCase : List[Any] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES) UpperCamelCase : Any = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_CAUSAL_LM_MAPPING_NAMES) UpperCamelCase : List[str] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES ) UpperCamelCase : Optional[Any] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES ) UpperCamelCase : List[Any] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES ) UpperCamelCase : List[str] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES ) UpperCamelCase : Tuple = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES ) UpperCamelCase : Union[str, Any] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES ) UpperCamelCase : Optional[Any] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES ) class UpperCamelCase__ (_BaseAutoModelClass ): '''simple docstring''' _UpperCamelCase = FLAX_MODEL_MAPPING UpperCamelCase : Union[str, Any] = auto_class_update(FlaxAutoModel) class UpperCamelCase__ (_BaseAutoModelClass ): '''simple docstring''' _UpperCamelCase = FLAX_MODEL_FOR_PRETRAINING_MAPPING UpperCamelCase : Optional[int] = auto_class_update(FlaxAutoModelForPreTraining, head_doc='pretraining') class UpperCamelCase__ (_BaseAutoModelClass ): '''simple docstring''' _UpperCamelCase = FLAX_MODEL_FOR_CAUSAL_LM_MAPPING UpperCamelCase : Dict = auto_class_update(FlaxAutoModelForCausalLM, head_doc='causal language modeling') class UpperCamelCase__ (_BaseAutoModelClass ): '''simple docstring''' _UpperCamelCase = FLAX_MODEL_FOR_MASKED_LM_MAPPING UpperCamelCase : List[str] = auto_class_update(FlaxAutoModelForMaskedLM, head_doc='masked language modeling') class UpperCamelCase__ (_BaseAutoModelClass ): '''simple docstring''' _UpperCamelCase = FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING UpperCamelCase : Tuple = auto_class_update( FlaxAutoModelForSeqaSeqLM, head_doc='sequence-to-sequence language modeling', checkpoint_for_example='t5-base' ) class UpperCamelCase__ (_BaseAutoModelClass ): '''simple docstring''' _UpperCamelCase = FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING UpperCamelCase : Any = auto_class_update( FlaxAutoModelForSequenceClassification, head_doc='sequence classification' ) class UpperCamelCase__ (_BaseAutoModelClass ): '''simple docstring''' _UpperCamelCase = FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING UpperCamelCase : Tuple = auto_class_update(FlaxAutoModelForQuestionAnswering, head_doc='question answering') class UpperCamelCase__ (_BaseAutoModelClass ): '''simple docstring''' _UpperCamelCase = FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING UpperCamelCase : int = auto_class_update( FlaxAutoModelForTokenClassification, head_doc='token classification' ) class UpperCamelCase__ (_BaseAutoModelClass ): '''simple docstring''' _UpperCamelCase = FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING UpperCamelCase : int = auto_class_update(FlaxAutoModelForMultipleChoice, head_doc='multiple choice') class UpperCamelCase__ (_BaseAutoModelClass ): '''simple docstring''' _UpperCamelCase = FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING UpperCamelCase : int = auto_class_update( FlaxAutoModelForNextSentencePrediction, head_doc='next sentence prediction' ) class UpperCamelCase__ (_BaseAutoModelClass ): '''simple docstring''' _UpperCamelCase = FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING UpperCamelCase : Union[str, Any] = auto_class_update( FlaxAutoModelForImageClassification, head_doc='image classification' ) class UpperCamelCase__ (_BaseAutoModelClass ): '''simple docstring''' _UpperCamelCase = FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING UpperCamelCase : Union[str, Any] = auto_class_update(FlaxAutoModelForVisionaSeq, head_doc='vision-to-text modeling') class UpperCamelCase__ (_BaseAutoModelClass ): '''simple docstring''' _UpperCamelCase = FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING UpperCamelCase : Union[str, Any] = auto_class_update( FlaxAutoModelForSpeechSeqaSeq, head_doc='sequence-to-sequence speech-to-text modeling' )
50
'''simple docstring''' import json from typing import Dict, List, Optional, Tuple, Union from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_led import LEDTokenizer UpperCAmelCase_ : Any = logging.get_logger(__name__) UpperCAmelCase_ : str = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} UpperCAmelCase_ : str = { '''vocab_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json''', }, '''merges_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt''', }, '''tokenizer_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json''', }, } UpperCAmelCase_ : Union[str, Any] = { '''allenai/led-base-16384''': 1_6_3_8_4, } class lowerCAmelCase ( __lowerCAmelCase): __lowercase : Dict = VOCAB_FILES_NAMES __lowercase : Dict = PRETRAINED_VOCAB_FILES_MAP __lowercase : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Union[str, Any] = LEDTokenizer __lowercase : int = ['''input_ids''', '''attention_mask'''] def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="replace" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="<mask>" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , **__SCREAMING_SNAKE_CASE , ) -> List[Any]: '''simple docstring''' super().__init__( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , errors=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) __snake_case = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: __snake_case = getattr(__SCREAMING_SNAKE_CASE , pre_tok_state.pop('''type''' ) ) __snake_case = add_prefix_space __snake_case = pre_tok_class(**__SCREAMING_SNAKE_CASE ) __snake_case = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` __snake_case = '''post_processor''' __snake_case = getattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if tokenizer_component_instance: __snake_case = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: __snake_case = tuple(state['''sep'''] ) if "cls" in state: __snake_case = tuple(state['''cls'''] ) __snake_case = False if state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: __snake_case = add_prefix_space __snake_case = True if state.get('''trim_offsets''' , __SCREAMING_SNAKE_CASE ) != trim_offsets: __snake_case = trim_offsets __snake_case = True if changes_to_apply: __snake_case = getattr(__SCREAMING_SNAKE_CASE , state.pop('''type''' ) ) __snake_case = component_class(**__SCREAMING_SNAKE_CASE ) setattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @property # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED def lowerCAmelCase ( self ) -> str: '''simple docstring''' if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' __snake_case = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else value __snake_case = value def lowerCAmelCase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> BatchEncoding: '''simple docstring''' __snake_case = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' '''to use it with pretokenized inputs.''' ) return super()._batch_encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> BatchEncoding: '''simple docstring''' __snake_case = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' '''to use it with pretokenized inputs.''' ) return super()._encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) -> Tuple[str]: '''simple docstring''' __snake_case = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) -> List[Any]: '''simple docstring''' __snake_case = [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 , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) -> List[int]: '''simple docstring''' __snake_case = [self.sep_token_id] __snake_case = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , ) -> dict: '''simple docstring''' __snake_case = super()._pad( encoded_inputs=__SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , padding_strategy=__SCREAMING_SNAKE_CASE , pad_to_multiple_of=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , ) # Load from model defaults if return_attention_mask is None: __snake_case = '''attention_mask''' in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: __snake_case = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. __snake_case = len(encoded_inputs['''global_attention_mask'''] ) != len(__SCREAMING_SNAKE_CASE ) if needs_to_be_padded: __snake_case = len(__SCREAMING_SNAKE_CASE ) - len(encoded_inputs['''global_attention_mask'''] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` __snake_case = ( encoded_inputs['''global_attention_mask'''] + [-1] * difference ) elif self.padding_side == "left": __snake_case = [-1] * difference + encoded_inputs[ '''global_attention_mask''' ] else: raise ValueError('''Invalid padding strategy:''' + str(self.padding_side ) ) return encoded_inputs
24
0
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_mobilebert import MobileBertTokenizer a__ : int = logging.get_logger(__name__) a__ : Dict = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} a__ : int = { 'vocab_file': {'mobilebert-uncased': 'https://huggingface.co/google/mobilebert-uncased/resolve/main/vocab.txt'}, 'tokenizer_file': { 'mobilebert-uncased': 'https://huggingface.co/google/mobilebert-uncased/resolve/main/tokenizer.json' }, } a__ : List[str] = {'mobilebert-uncased': 512} a__ : Union[str, Any] = {} class lowerCAmelCase__ ( UpperCAmelCase_ ): '''simple docstring''' _lowerCamelCase =VOCAB_FILES_NAMES _lowerCamelCase =PRETRAINED_VOCAB_FILES_MAP _lowerCamelCase =PRETRAINED_INIT_CONFIGURATION _lowerCamelCase =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCamelCase =MobileBertTokenizer def __init__( self : Dict , a__ : Optional[int]=None , a__ : Tuple=None , a__ : Optional[Any]=True , a__ : Any="[UNK]" , a__ : int="[SEP]" , a__ : Optional[int]="[PAD]" , a__ : int="[CLS]" , a__ : List[str]="[MASK]" , a__ : Tuple=True , a__ : List[str]=None , **a__ : Any , ): super().__init__( a__ , tokenizer_file=a__ , do_lower_case=a__ , unk_token=a__ , sep_token=a__ , pad_token=a__ , cls_token=a__ , mask_token=a__ , tokenize_chinese_chars=a__ , strip_accents=a__ , **a__ , ) UpperCAmelCase = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , a__ ) != do_lower_case or normalizer_state.get('''strip_accents''' , a__ ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , a__ ) != tokenize_chinese_chars ): UpperCAmelCase = getattr(a__ , normalizer_state.pop('''type''' ) ) UpperCAmelCase = do_lower_case UpperCAmelCase = strip_accents UpperCAmelCase = tokenize_chinese_chars UpperCAmelCase = normalizer_class(**a__ ) UpperCAmelCase = do_lower_case def __snake_case ( self : int , a__ : List[str] , a__ : Optional[int]=None ): UpperCAmelCase = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __snake_case ( self : List[Any] , a__ : List[int] , a__ : Optional[List[int]] = None ): UpperCAmelCase = [self.sep_token_id] UpperCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __snake_case ( self : Tuple , a__ : str , a__ : Optional[str] = None ): UpperCAmelCase = self._tokenizer.model.save(a__ , name=a__ ) return tuple(a__ )
51
'''simple docstring''' from collections import deque def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> Optional[int]: '''simple docstring''' __snake_case = len(_lowerCamelCase ) __snake_case = deque() __snake_case = [False for _ in range(_lowerCamelCase )] __snake_case = [-1 for _ in range(_lowerCamelCase )] __snake_case = index_of[:] def strong_connect(_lowerCamelCase : Any , _lowerCamelCase : Dict , _lowerCamelCase : List[str] ): __snake_case = index # the number when this node is seen __snake_case = index # lowest rank node reachable from here index += 1 stack.append(_lowerCamelCase ) __snake_case = True for w in g[v]: if index_of[w] == -1: __snake_case = strong_connect(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) __snake_case = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) elif on_stack[w]: __snake_case = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) if lowlink_of[v] == index_of[v]: __snake_case = [] __snake_case = stack.pop() __snake_case = False component.append(_lowerCamelCase ) while w != v: __snake_case = stack.pop() __snake_case = False component.append(_lowerCamelCase ) components.append(_lowerCamelCase ) return index __snake_case = [] for v in range(_lowerCamelCase ): if index_of[v] == -1: strong_connect(_lowerCamelCase , 0 , _lowerCamelCase ) return components def _UpperCamelCase (_lowerCamelCase : Tuple , _lowerCamelCase : Optional[Any] )-> Dict: '''simple docstring''' __snake_case = [[] for _ in range(_lowerCamelCase )] for u, v in edges: g[u].append(_lowerCamelCase ) return g if __name__ == "__main__": # Test UpperCAmelCase_ : List[str] = 7 UpperCAmelCase_ : int = [0, 0, 1, 2, 3, 3, 4, 4, 6] UpperCAmelCase_ : Dict = [1, 3, 2, 0, 1, 4, 5, 6, 5] UpperCAmelCase_ : List[str] = [(u, v) for u, v in zip(source, target)] UpperCAmelCase_ : Tuple = create_graph(n_vertices, edges) assert [[5], [6], [4], [3, 2, 1, 0]] == tarjan(g)
24
0
"""simple docstring""" from __future__ import annotations def __A ( a_ :str) -> list[int]: return [ord(a_) - 96 for elem in plain] def __A ( a_ :list[int]) -> str: return "".join(chr(elem + 96) for elem in encoded) def __A ( ) -> None: __a : Dict = encode(input('''-> ''').strip().lower()) print('''Encoded: ''' , a_) print('''Decoded:''' , decode(a_)) if __name__ == "__main__": main()
52
'''simple docstring''' import unittest from transformers import BarthezTokenizer, BarthezTokenizerFast, BatchEncoding from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers @require_sentencepiece @slow # see https://github.com/huggingface/transformers/issues/11457 class lowerCAmelCase ( __lowerCAmelCase , unittest.TestCase): __lowercase : int = BarthezTokenizer __lowercase : Any = BarthezTokenizerFast __lowercase : Dict = True __lowercase : Optional[int] = True def lowerCAmelCase ( self ) -> Dict: '''simple docstring''' super().setUp() __snake_case = BarthezTokenizerFast.from_pretrained('''moussaKam/mbarthez''' ) tokenizer.save_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname , legacy_format=__SCREAMING_SNAKE_CASE ) __snake_case = tokenizer def lowerCAmelCase ( self ) -> Dict: '''simple docstring''' __snake_case = '''<pad>''' __snake_case = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<s>''' ) self.assertEqual(vocab_keys[1] , '''<pad>''' ) self.assertEqual(vocab_keys[-1] , '''<mask>''' ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 10_1122 ) def lowerCAmelCase ( self ) -> Any: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 10_1122 ) @require_torch def lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] __snake_case = [0, 57, 3018, 7_0307, 91, 2] __snake_case = self.tokenizer( __SCREAMING_SNAKE_CASE , max_length=len(__SCREAMING_SNAKE_CASE ) , padding=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertEqual((2, 6) , batch.input_ids.shape ) self.assertEqual((2, 6) , batch.attention_mask.shape ) __snake_case = batch.input_ids.tolist()[0] self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' if not self.test_rust_tokenizer: return __snake_case = self.get_tokenizer() __snake_case = self.get_rust_tokenizer() __snake_case = '''I was born in 92000, and this is falsé.''' __snake_case = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) __snake_case = rust_tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __snake_case = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) __snake_case = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __snake_case = self.get_rust_tokenizer() __snake_case = tokenizer.encode(__SCREAMING_SNAKE_CASE ) __snake_case = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = {'''input_ids''': [[0, 490, 1_4328, 4507, 354, 47, 4_3669, 95, 25, 7_8117, 2_0215, 1_9779, 190, 22, 400, 4, 3_5343, 8_0310, 603, 86, 2_4937, 105, 3_3438, 9_4762, 196, 3_9642, 7, 15, 1_5933, 173, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1_0534, 87, 25, 66, 3358, 196, 5_5289, 8, 8_2961, 81, 2204, 7_5203, 7, 15, 763, 1_2956, 216, 178, 1_4328, 9595, 1377, 6_9693, 7, 448, 7_1021, 196, 1_8106, 1437, 1_3974, 108, 9083, 4, 4_9315, 7, 39, 86, 1326, 2793, 4_6333, 4, 448, 196, 7_4588, 7, 4_9315, 7, 39, 21, 822, 3_8470, 74, 21, 6_6723, 6_2480, 8, 2_2050, 5, 2]], '''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, 0, 0, 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # moussaKam/mbarthez is a french model. So we also use french texts. __snake_case = [ '''Le transformeur est un modèle d\'apprentissage profond introduit en 2017, ''' '''utilisé principalement dans le domaine du traitement automatique des langues (TAL).''', '''À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus ''' '''pour gérer des données séquentielles, telles que le langage naturel, pour des tâches ''' '''telles que la traduction et la synthèse de texte.''', ] self.tokenizer_integration_test_util( expected_encoding=__SCREAMING_SNAKE_CASE , model_name='''moussaKam/mbarthez''' , revision='''c2e4ecbca5e3cd2c37fe1ac285ca4fbdf1366fb6''' , sequences=__SCREAMING_SNAKE_CASE , )
24
0
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() _snake_case : List[Any] = logging.get_logger(__name__) def a_ ( lowerCAmelCase_ : str ): __lowerCAmelCase = 'huggingface/label-files' __lowerCAmelCase = 'imagenet-1k-id2label.json' __lowerCAmelCase = json.load(open(hf_hub_download(lowerCAmelCase_, lowerCAmelCase_, repo_type='dataset' ), 'r' ) ) __lowerCAmelCase = {int(lowerCAmelCase_ ): v for k, v in idalabel.items()} __lowerCAmelCase = {v: k for k, v in idalabel.items()} __lowerCAmelCase = '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" __lowerCAmelCase = BitConfig( conv_layer=lowerCAmelCase_, num_labels=1000, idalabel=lowerCAmelCase_, labelaid=lowerCAmelCase_, ) return config def a_ ( lowerCAmelCase_ : List[str] ): if "stem.conv" in name: __lowerCAmelCase = name.replace('stem.conv', 'bit.embedder.convolution' ) if "blocks" in name: __lowerCAmelCase = name.replace('blocks', 'layers' ) if "head.fc" in name: __lowerCAmelCase = name.replace('head.fc', 'classifier.1' ) if name.startswith('norm' ): __lowerCAmelCase = 'bit.' + name if "bit" not in name and "classifier" not in name: __lowerCAmelCase = 'bit.encoder.' + name return name def a_ ( ): __lowerCAmelCase = 'http://images.cocodataset.org/val2017/000000039769.jpg' __lowerCAmelCase = Image.open(requests.get(lowerCAmelCase_, stream=lowerCAmelCase_ ).raw ) return im @torch.no_grad() def a_ ( lowerCAmelCase_ : str, lowerCAmelCase_ : Any, lowerCAmelCase_ : Optional[int]=False ): __lowerCAmelCase = get_config(lowerCAmelCase_ ) # load original model from timm __lowerCAmelCase = create_model(lowerCAmelCase_, pretrained=lowerCAmelCase_ ) timm_model.eval() # load state_dict of original model __lowerCAmelCase = timm_model.state_dict() for key in state_dict.copy().keys(): __lowerCAmelCase = state_dict.pop(lowerCAmelCase_ ) __lowerCAmelCase = val.squeeze() if 'head' in key else val # load HuggingFace model __lowerCAmelCase = BitForImageClassification(lowerCAmelCase_ ) model.eval() model.load_state_dict(lowerCAmelCase_ ) # create image processor __lowerCAmelCase = create_transform(**resolve_data_config({}, model=lowerCAmelCase_ ) ) __lowerCAmelCase = transform.transforms __lowerCAmelCase = { 'bilinear': PILImageResampling.BILINEAR, 'bicubic': PILImageResampling.BICUBIC, 'nearest': PILImageResampling.NEAREST, } __lowerCAmelCase = 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(), ) __lowerCAmelCase = prepare_img() __lowerCAmelCase = transform(lowerCAmelCase_ ).unsqueeze(0 ) __lowerCAmelCase = processor(lowerCAmelCase_, return_tensors='pt' ).pixel_values # verify pixel values assert torch.allclose(lowerCAmelCase_, lowerCAmelCase_ ) # verify logits with torch.no_grad(): __lowerCAmelCase = model(lowerCAmelCase_ ) __lowerCAmelCase = outputs.logits print('Logits:', logits[0, :3] ) print('Predicted class:', model.config.idalabel[logits.argmax(-1 ).item()] ) __lowerCAmelCase = 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__": _snake_case : Tuple = 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.', ) _snake_case : Dict = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
53
'''simple docstring''' import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class lowerCAmelCase ( unittest.TestCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __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=32 , __SCREAMING_SNAKE_CASE=5 , __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=4 , ) -> Any: '''simple docstring''' __snake_case = parent __snake_case = batch_size __snake_case = seq_length __snake_case = is_training __snake_case = use_attention_mask __snake_case = use_token_type_ids __snake_case = use_labels __snake_case = vocab_size __snake_case = hidden_size __snake_case = num_hidden_layers __snake_case = num_attention_heads __snake_case = intermediate_size __snake_case = hidden_act __snake_case = hidden_dropout_prob __snake_case = attention_probs_dropout_prob __snake_case = max_position_embeddings __snake_case = type_vocab_size __snake_case = type_sequence_label_size __snake_case = initializer_range __snake_case = num_choices def lowerCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' __snake_case = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __snake_case = None if self.use_attention_mask: __snake_case = random_attention_mask([self.batch_size, self.seq_length] ) __snake_case = None if self.use_token_type_ids: __snake_case = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __snake_case = RoFormerConfig( 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=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = self.prepare_config_and_inputs() __snake_case , __snake_case , __snake_case , __snake_case = config_and_inputs __snake_case = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_flax class lowerCAmelCase ( __lowerCAmelCase , unittest.TestCase): __lowercase : Tuple = True __lowercase : Optional[int] = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = FlaxRoFormerModelTester(self ) @slow def lowerCAmelCase ( self ) -> List[Any]: '''simple docstring''' for model_class_name in self.all_model_classes: __snake_case = model_class_name.from_pretrained('''junnyu/roformer_chinese_small''' , from_pt=__SCREAMING_SNAKE_CASE ) __snake_case = model(np.ones((1, 1) ) ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) @require_flax class lowerCAmelCase ( unittest.TestCase): @slow def lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = FlaxRoFormerForMaskedLM.from_pretrained('''junnyu/roformer_chinese_base''' ) __snake_case = jnp.array([[0, 1, 2, 3, 4, 5]] ) __snake_case = model(__SCREAMING_SNAKE_CASE )[0] __snake_case = 5_0000 __snake_case = (1, 6, vocab_size) self.assertEqual(output.shape , __SCREAMING_SNAKE_CASE ) __snake_case = jnp.array( [[[-0.1_205, -1.0_265, 0.2_922], [-1.5_134, 0.1_974, 0.1_519], [-5.0_135, -3.9_003, -0.8_404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , __SCREAMING_SNAKE_CASE , atol=1E-4 ) )
24
0
class A : def __init__( self: List[Any] ) -> None: '''simple docstring''' UpperCAmelCase_ ={} # Mapping from char to TrieNode UpperCAmelCase_ =False def lowerCAmelCase__ ( self: int , _lowerCAmelCase: list[str] ) -> None: '''simple docstring''' for word in words: self.insert(_lowerCAmelCase ) def lowerCAmelCase__ ( self: Tuple , _lowerCAmelCase: str ) -> None: '''simple docstring''' UpperCAmelCase_ =self for char in word: if char not in curr.nodes: UpperCAmelCase_ =TrieNode() UpperCAmelCase_ =curr.nodes[char] UpperCAmelCase_ =True def lowerCAmelCase__ ( self: List[str] , _lowerCAmelCase: str ) -> bool: '''simple docstring''' UpperCAmelCase_ =self for char in word: if char not in curr.nodes: return False UpperCAmelCase_ =curr.nodes[char] return curr.is_leaf def lowerCAmelCase__ ( self: str , _lowerCAmelCase: str ) -> None: '''simple docstring''' def _delete(_lowerCAmelCase: TrieNode , _lowerCAmelCase: str , _lowerCAmelCase: int ) -> bool: if index == len(_lowerCAmelCase ): # If word does not exist if not curr.is_leaf: return False UpperCAmelCase_ =False return len(curr.nodes ) == 0 UpperCAmelCase_ =word[index] UpperCAmelCase_ =curr.nodes.get(_lowerCAmelCase ) # If char not in current trie node if not char_node: return False # Flag to check if node can be deleted UpperCAmelCase_ =_delete(_lowerCAmelCase , _lowerCAmelCase , index + 1 ) if delete_curr: del curr.nodes[char] return len(curr.nodes ) == 0 return delete_curr _delete(self , _lowerCAmelCase , 0 ) def a__ ( lowercase__ , lowercase__ ): '''simple docstring''' if node.is_leaf: print(lowercase__ , end=" " ) for key, value in node.nodes.items(): print_words(lowercase__ , word + key ) def a__ ( ): '''simple docstring''' UpperCAmelCase_ ="banana bananas bandana band apple all beast".split() UpperCAmelCase_ =TrieNode() root.insert_many(lowercase__ ) # print_words(root, "") assert all(root.find(lowercase__ ) for word in words ) assert root.find("banana" ) assert not root.find("bandanas" ) assert not root.find("apps" ) assert root.find("apple" ) assert root.find("all" ) root.delete("all" ) assert not root.find("all" ) root.delete("banana" ) assert not root.find("banana" ) assert root.find("bananas" ) return True def a__ ( lowercase__ , lowercase__ ): '''simple docstring''' print(str(lowercase__ ) , "works!" if passes else "doesn't work :(" ) def a__ ( ): '''simple docstring''' assert test_trie() def a__ ( ): '''simple docstring''' print_results("Testing trie functionality" , test_trie() ) if __name__ == "__main__": main()
54
'''simple docstring''' 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 ()-> int: '''simple docstring''' __snake_case = '''https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png''' __snake_case = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ).convert('''RGB''' ) return image def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> List[Any]: '''simple docstring''' __snake_case = [] # fmt: off # vision encoder rename_keys.append(('''visual_encoder.cls_token''', '''vision_model.embeddings.class_embedding''') ) rename_keys.append(('''visual_encoder.pos_embed''', '''vision_model.embeddings.position_embedding''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.weight''', '''vision_model.embeddings.patch_embedding.weight''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.bias''', '''vision_model.embeddings.patch_embedding.bias''') ) rename_keys.append(('''ln_vision.weight''', '''vision_model.post_layernorm.weight''') ) rename_keys.append(('''ln_vision.bias''', '''vision_model.post_layernorm.bias''') ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.weight''', f'''vision_model.encoder.layers.{i}.layer_norm1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.bias''', f'''vision_model.encoder.layers.{i}.layer_norm1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.weight''', f'''vision_model.encoder.layers.{i}.layer_norm2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.bias''', f'''vision_model.encoder.layers.{i}.layer_norm2.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.qkv.weight''', f'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.weight''', f'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',) ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.bias''', f'''vision_model.encoder.layers.{i}.self_attn.projection.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc2.bias''') ) # QFormer rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.weight''', '''qformer.layernorm.weight''') ) rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.bias''', '''qformer.layernorm.bias''') ) # fmt: on return rename_keys def _UpperCamelCase (_lowerCamelCase : Dict , _lowerCamelCase : List[str] , _lowerCamelCase : Optional[int] )-> Tuple: '''simple docstring''' __snake_case = dct.pop(_lowerCamelCase ) __snake_case = val def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : Tuple )-> str: '''simple docstring''' for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases __snake_case = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.q_bias''' ) __snake_case = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict __snake_case = torch.cat((q_bias, torch.zeros_like(_lowerCamelCase , requires_grad=_lowerCamelCase ), v_bias) ) __snake_case = qkv_bias def _UpperCamelCase (_lowerCamelCase : Tuple , _lowerCamelCase : Tuple )-> Dict: '''simple docstring''' __snake_case = 3_64 if '''coco''' in model_name else 2_24 __snake_case = BlipaVisionConfig(image_size=_lowerCamelCase ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: __snake_case = OPTConfig.from_pretrained('''facebook/opt-2.7b''' , eos_token_id=_lowerCamelCase ).to_dict() elif "opt-6.7b" in model_name: __snake_case = OPTConfig.from_pretrained('''facebook/opt-6.7b''' , eos_token_id=_lowerCamelCase ).to_dict() elif "t5-xl" in model_name: __snake_case = TaConfig.from_pretrained('''google/flan-t5-xl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: __snake_case = TaConfig.from_pretrained('''google/flan-t5-xxl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() __snake_case = BlipaConfig(vision_config=_lowerCamelCase , text_config=_lowerCamelCase ) return config, image_size @torch.no_grad() def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : Union[str, Any]=None , _lowerCamelCase : Any=False )-> Dict: '''simple docstring''' __snake_case = ( AutoTokenizer.from_pretrained('''facebook/opt-2.7b''' ) if '''opt''' in model_name else AutoTokenizer.from_pretrained('''google/flan-t5-xl''' ) ) __snake_case = tokenizer('''\n''' , add_special_tokens=_lowerCamelCase ).input_ids[0] __snake_case , __snake_case = get_blipa_config(_lowerCamelCase , eos_token_id=_lowerCamelCase ) __snake_case = BlipaForConditionalGeneration(_lowerCamelCase ).eval() __snake_case = { '''blip2-opt-2.7b''': ('''blip2_opt''', '''pretrain_opt2.7b'''), '''blip2-opt-6.7b''': ('''blip2_opt''', '''pretrain_opt6.7b'''), '''blip2-opt-2.7b-coco''': ('''blip2_opt''', '''caption_coco_opt2.7b'''), '''blip2-opt-6.7b-coco''': ('''blip2_opt''', '''caption_coco_opt6.7b'''), '''blip2-flan-t5-xl''': ('''blip2_t5''', '''pretrain_flant5xl'''), '''blip2-flan-t5-xl-coco''': ('''blip2_t5''', '''caption_coco_flant5xl'''), '''blip2-flan-t5-xxl''': ('''blip2_t5''', '''pretrain_flant5xxl'''), } __snake_case , __snake_case = model_name_to_original[model_name] # load original model print('''Loading original model...''' ) __snake_case = '''cuda''' if torch.cuda.is_available() else '''cpu''' __snake_case , __snake_case , __snake_case = load_model_and_preprocess( name=_lowerCamelCase , model_type=_lowerCamelCase , is_eval=_lowerCamelCase , device=_lowerCamelCase ) original_model.eval() print('''Done!''' ) # update state dict keys __snake_case = original_model.state_dict() __snake_case = create_rename_keys(_lowerCamelCase ) for src, dest in rename_keys: rename_key(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): __snake_case = state_dict.pop(_lowerCamelCase ) if key.startswith('''Qformer.bert''' ): __snake_case = key.replace('''Qformer.bert''' , '''qformer''' ) if "attention.self" in key: __snake_case = key.replace('''self''' , '''attention''' ) if "opt_proj" in key: __snake_case = key.replace('''opt_proj''' , '''language_projection''' ) if "t5_proj" in key: __snake_case = key.replace('''t5_proj''' , '''language_projection''' ) if key.startswith('''opt''' ): __snake_case = key.replace('''opt''' , '''language''' ) if key.startswith('''t5''' ): __snake_case = key.replace('''t5''' , '''language''' ) __snake_case = val # read in qv biases read_in_q_v_bias(_lowerCamelCase , _lowerCamelCase ) __snake_case , __snake_case = hf_model.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) assert len(_lowerCamelCase ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] __snake_case = load_demo_image() __snake_case = vis_processors['''eval'''](_lowerCamelCase ).unsqueeze(0 ).to(_lowerCamelCase ) __snake_case = tokenizer(['''\n'''] , return_tensors='''pt''' ).input_ids.to(_lowerCamelCase ) # create processor __snake_case = BlipImageProcessor( size={'''height''': image_size, '''width''': image_size} , image_mean=_lowerCamelCase , image_std=_lowerCamelCase ) __snake_case = BlipaProcessor(image_processor=_lowerCamelCase , tokenizer=_lowerCamelCase ) __snake_case = processor(images=_lowerCamelCase , return_tensors='''pt''' ).pixel_values.to(_lowerCamelCase ) # make sure processor creates exact same pixel values assert torch.allclose(_lowerCamelCase , _lowerCamelCase ) original_model.to(_lowerCamelCase ) hf_model.to(_lowerCamelCase ) with torch.no_grad(): if "opt" in model_name: __snake_case = original_model({'''image''': original_pixel_values, '''text_input''': ['''''']} ).logits __snake_case = hf_model(_lowerCamelCase , _lowerCamelCase ).logits else: __snake_case = original_model( {'''image''': original_pixel_values, '''text_input''': ['''\n'''], '''text_output''': ['''\n''']} ).logits __snake_case = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -1_00 ) __snake_case = hf_model(_lowerCamelCase , _lowerCamelCase , labels=_lowerCamelCase ).logits assert original_logits.shape == logits.shape print('''First values of original logits:''' , original_logits[0, :3, :3] ) print('''First values of HF logits:''' , logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": __snake_case = torch.tensor( [[-41.5850, -4.4440, -8.9922], [-47.4322, -5.9143, -1.7340]] , device=_lowerCamelCase ) assert torch.allclose(logits[0, :3, :3] , _lowerCamelCase , atol=1E-4 ) elif model_name == "blip2-flan-t5-xl-coco": __snake_case = torch.tensor( [[-57.0109, -9.8967, -12.6280], [-68.6578, -12.7191, -10.5065]] , device=_lowerCamelCase ) else: # cast to same type __snake_case = logits.dtype assert torch.allclose(original_logits.to(_lowerCamelCase ) , _lowerCamelCase , atol=1E-2 ) print('''Looks ok!''' ) print('''Generating a caption...''' ) __snake_case = '''''' __snake_case = tokenizer(_lowerCamelCase , return_tensors='''pt''' ).input_ids.to(_lowerCamelCase ) __snake_case = original_model.generate({'''image''': original_pixel_values} ) __snake_case = hf_model.generate( _lowerCamelCase , _lowerCamelCase , do_sample=_lowerCamelCase , 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:''' , _lowerCamelCase ) __snake_case = input_ids.shape[1] __snake_case = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=_lowerCamelCase ) __snake_case = [text.strip() for text in output_text] print('''HF generation:''' , _lowerCamelCase ) if pytorch_dump_folder_path is not None: processor.save_pretrained(_lowerCamelCase ) hf_model.save_pretrained(_lowerCamelCase ) if push_to_hub: processor.push_to_hub(f'''nielsr/{model_name}''' ) hf_model.push_to_hub(f'''nielsr/{model_name}''' ) if __name__ == "__main__": UpperCAmelCase_ : Any = argparse.ArgumentParser() UpperCAmelCase_ : 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''', ) UpperCAmelCase_ : List[str] = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
24
0
import argparse import glob import logging import os import time from argparse import Namespace import numpy as np import torch from lightning_base import BaseTransformer, add_generic_args, generic_train from torch.utils.data import DataLoader, TensorDataset from transformers import glue_compute_metrics as compute_metrics from transformers import glue_convert_examples_to_features as convert_examples_to_features from transformers import glue_output_modes, glue_tasks_num_labels from transformers import glue_processors as processors SCREAMING_SNAKE_CASE :List[Any] = logging.getLogger(__name__) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = "sequence-classification" def __init__( self : Tuple ,A : List[Any] ): if type(A ) == dict: __A = Namespace(**A ) __A = glue_output_modes[hparams.task] __A = glue_tasks_num_labels[hparams.task] super().__init__(A ,A ,self.mode ) def UpperCamelCase_ ( self : List[Any] ,**A : Optional[Any] ): return self.model(**A ) def UpperCamelCase_ ( self : Optional[Any] ,A : int ,A : Any ): __A = {"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]} if self.config.model_type not in ["distilbert", "bart"]: __A = batch[2] if self.config.model_type in ["bert", "xlnet", "albert"] else None __A = self(**A ) __A = outputs[0] __A = self.trainer.lr_schedulers[0]["scheduler"] __A = {"loss": loss, "rate": lr_scheduler.get_last_lr()[-1]} return {"loss": loss, "log": tensorboard_logs} def UpperCamelCase_ ( self : Optional[int] ): __A = self.hparams __A = processors[args.task]() __A = processor.get_labels() for mode in ["train", "dev"]: __A = self._feature_file(A ) if os.path.exists(A ) and not args.overwrite_cache: logger.info("Loading features from cached file %s" ,A ) else: logger.info("Creating features from dataset file at %s" ,args.data_dir ) __A = ( processor.get_dev_examples(args.data_dir ) if mode == "dev" else processor.get_train_examples(args.data_dir ) ) __A = convert_examples_to_features( A ,self.tokenizer ,max_length=args.max_seq_length ,label_list=self.labels ,output_mode=args.glue_output_mode ,) logger.info("Saving features into cached file %s" ,A ) torch.save(A ,A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : str ,A : int ,A : bool = False ): __A = "dev" if mode == "test" else mode __A = self._feature_file(A ) logger.info("Loading features from cached file %s" ,A ) __A = torch.load(A ) __A = torch.tensor([f.input_ids for f in features] ,dtype=torch.long ) __A = torch.tensor([f.attention_mask for f in features] ,dtype=torch.long ) __A = torch.tensor([f.token_type_ids for f in features] ,dtype=torch.long ) if self.hparams.glue_output_mode == "classification": __A = torch.tensor([f.label for f in features] ,dtype=torch.long ) elif self.hparams.glue_output_mode == "regression": __A = torch.tensor([f.label for f in features] ,dtype=torch.float ) return DataLoader( TensorDataset(A ,A ,A ,A ) ,batch_size=A ,shuffle=A ,) def UpperCamelCase_ ( self : Optional[Any] ,A : Optional[Any] ,A : int ): __A = {"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]} if self.config.model_type not in ["distilbert", "bart"]: __A = batch[2] if self.config.model_type in ["bert", "xlnet", "albert"] else None __A = self(**A ) __A , __A = outputs[:2] __A = logits.detach().cpu().numpy() __A = inputs["labels"].detach().cpu().numpy() return {"val_loss": tmp_eval_loss.detach().cpu(), "pred": preds, "target": out_label_ids} def UpperCamelCase_ ( self : Optional[Any] ,A : int ): __A = torch.stack([x["val_loss"] for x in outputs] ).mean().detach().cpu().item() __A = np.concatenate([x["pred"] for x in outputs] ,axis=0 ) if self.hparams.glue_output_mode == "classification": __A = np.argmax(A ,axis=1 ) elif self.hparams.glue_output_mode == "regression": __A = np.squeeze(A ) __A = np.concatenate([x["target"] for x in outputs] ,axis=0 ) __A = [[] for _ in range(out_label_ids.shape[0] )] __A = [[] for _ in range(out_label_ids.shape[0] )] __A = {**{"val_loss": val_loss_mean}, **compute_metrics(self.hparams.task ,A ,A )} __A = dict(results.items() ) __A = results return ret, preds_list, out_label_list def UpperCamelCase_ ( self : Any ,A : list ): __A , __A , __A = self._eval_end(A ) __A = ret["log"] return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs} def UpperCamelCase_ ( self : List[str] ,A : Union[str, Any] ): __A , __A , __A = self._eval_end(A ) __A = ret["log"] # `val_loss` is the key returned by `self._eval_end()` but actually refers to `test_loss` return {"avg_test_loss": logs["val_loss"], "log": logs, "progress_bar": logs} @staticmethod def UpperCamelCase_ ( A : Any ,A : List[Any] ): BaseTransformer.add_model_specific_args(A ,A ) parser.add_argument( "--max_seq_length" ,default=1_28 ,type=A ,help=( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) ,) parser.add_argument( "--task" ,default="" ,type=A ,required=A ,help="The GLUE task to run" ,) parser.add_argument( "--gpus" ,default=0 ,type=A ,help="The number of GPUs allocated for this, it is by default 0 meaning none" ,) parser.add_argument( "--overwrite_cache" ,action="store_true" ,help="Overwrite the cached training and evaluation sets" ) return parser def UpperCAmelCase ( ) -> Any: """simple docstring""" __A = argparse.ArgumentParser() add_generic_args(a_ , os.getcwd() ) __A = GLUETransformer.add_model_specific_args(a_ , os.getcwd() ) __A = parser.parse_args() # If output_dir not provided, a folder will be generated in pwd if args.output_dir is None: __A = os.path.join( "./results" , F'''{args.task}_{time.strftime('%Y%m%d_%H%M%S' )}''' , ) os.makedirs(args.output_dir ) __A = GLUETransformer(a_ ) __A = generic_train(a_ , a_ ) # Optionally, predict on dev set and write to output_dir if args.do_predict: __A = sorted(glob.glob(os.path.join(args.output_dir , "checkpoint-epoch=*.ckpt" ) , recursive=a_ ) ) __A = model.load_from_checkpoint(checkpoints[-1] ) return trainer.test(a_ ) if __name__ == "__main__": main()
55
'''simple docstring''' import warnings from typing import List, Optional, Tuple, Union import numpy as np import PIL import torch from ...models import UNetaDModel from ...schedulers import RePaintScheduler from ...utils import PIL_INTERPOLATION, logging, randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput UpperCAmelCase_ : Tuple = logging.get_logger(__name__) # pylint: disable=invalid-name def _UpperCamelCase (_lowerCamelCase : Union[List, PIL.Image.Image, torch.Tensor] )-> Optional[Any]: '''simple docstring''' warnings.warn( '''The preprocess method is deprecated and will be removed in a future version. Please''' ''' use VaeImageProcessor.preprocess instead''' , _lowerCamelCase , ) if isinstance(_lowerCamelCase , torch.Tensor ): return image elif isinstance(_lowerCamelCase , PIL.Image.Image ): __snake_case = [image] if isinstance(image[0] , PIL.Image.Image ): __snake_case , __snake_case = image[0].size __snake_case , __snake_case = (x - x % 8 for x in (w, h)) # resize to integer multiple of 8 __snake_case = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['''lanczos'''] ) )[None, :] for i in image] __snake_case = np.concatenate(_lowerCamelCase , axis=0 ) __snake_case = np.array(_lowerCamelCase ).astype(np.floataa ) / 255.0 __snake_case = image.transpose(0 , 3 , 1 , 2 ) __snake_case = 2.0 * image - 1.0 __snake_case = torch.from_numpy(_lowerCamelCase ) elif isinstance(image[0] , torch.Tensor ): __snake_case = torch.cat(_lowerCamelCase , dim=0 ) return image def _UpperCamelCase (_lowerCamelCase : Union[List, PIL.Image.Image, torch.Tensor] )-> Optional[Any]: '''simple docstring''' if isinstance(_lowerCamelCase , torch.Tensor ): return mask elif isinstance(_lowerCamelCase , PIL.Image.Image ): __snake_case = [mask] if isinstance(mask[0] , PIL.Image.Image ): __snake_case , __snake_case = mask[0].size __snake_case , __snake_case = (x - x % 32 for x in (w, h)) # resize to integer multiple of 32 __snake_case = [np.array(m.convert('''L''' ).resize((w, h) , resample=PIL_INTERPOLATION['''nearest'''] ) )[None, :] for m in mask] __snake_case = np.concatenate(_lowerCamelCase , axis=0 ) __snake_case = mask.astype(np.floataa ) / 255.0 __snake_case = 0 __snake_case = 1 __snake_case = torch.from_numpy(_lowerCamelCase ) elif isinstance(mask[0] , torch.Tensor ): __snake_case = torch.cat(_lowerCamelCase , dim=0 ) return mask class lowerCAmelCase ( __lowerCAmelCase): __lowercase : UNetaDModel __lowercase : RePaintScheduler def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) -> str: '''simple docstring''' super().__init__() self.register_modules(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) @torch.no_grad() def __call__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 250 , __SCREAMING_SNAKE_CASE = 0.0 , __SCREAMING_SNAKE_CASE = 10 , __SCREAMING_SNAKE_CASE = 10 , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = "pil" , __SCREAMING_SNAKE_CASE = True , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' __snake_case = image __snake_case = _preprocess_image(__SCREAMING_SNAKE_CASE ) __snake_case = original_image.to(device=self.device , dtype=self.unet.dtype ) __snake_case = _preprocess_mask(__SCREAMING_SNAKE_CASE ) __snake_case = mask_image.to(device=self.device , dtype=self.unet.dtype ) __snake_case = original_image.shape[0] # sample gaussian noise to begin the loop if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and len(__SCREAMING_SNAKE_CASE ) != batch_size: raise ValueError( F'''You have passed a list of generators of length {len(__SCREAMING_SNAKE_CASE )}, but requested an effective batch''' F''' size of {batch_size}. Make sure the batch size matches the length of the generators.''' ) __snake_case = original_image.shape __snake_case = randn_tensor(__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , device=self.device , dtype=self.unet.dtype ) # set step values self.scheduler.set_timesteps(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , self.device ) __snake_case = eta __snake_case = self.scheduler.timesteps[0] + 1 __snake_case = generator[0] if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else generator for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): if t < t_last: # predict the noise residual __snake_case = self.unet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).sample # compute previous image: x_t -> x_t-1 __snake_case = self.scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample else: # compute the reverse: x_t-1 -> x_t __snake_case = self.scheduler.undo_step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __snake_case = t __snake_case = (image / 2 + 0.5).clamp(0 , 1 ) __snake_case = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": __snake_case = self.numpy_to_pil(__SCREAMING_SNAKE_CASE ) if not return_dict: return (image,) return ImagePipelineOutput(images=__SCREAMING_SNAKE_CASE )
24
0
'''simple docstring''' from __future__ import annotations from typing import Any def _a (lowercase__ : list ) -> int: """simple docstring""" if not postfix_notation: return 0 __snake_case = {'+', '-', '*', '/'} __snake_case = [] for token in postfix_notation: if token in operations: __snake_case , __snake_case = stack.pop(), stack.pop() if token == "+": stack.append(a + b ) elif token == "-": stack.append(a - b ) elif token == "*": stack.append(a * b ) else: if a * b < 0 and a % b != 0: stack.append(a // b + 1 ) else: stack.append(a // b ) else: stack.append(int(lowercase__ ) ) return stack.pop() if __name__ == "__main__": import doctest doctest.testmod()
56
'''simple docstring''' from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax UpperCAmelCase_ : str = logging.get_logger(__name__) @add_end_docstrings(__lowerCAmelCase) class lowerCAmelCase ( __lowerCAmelCase): def __init__( self , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' super().__init__(**__SCREAMING_SNAKE_CASE ) 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 , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> List[Any]: '''simple docstring''' return super().__call__(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , **__SCREAMING_SNAKE_CASE ) -> Tuple: '''simple docstring''' __snake_case = {} if "candidate_labels" in kwargs: __snake_case = kwargs['''candidate_labels'''] if "hypothesis_template" in kwargs: __snake_case = kwargs['''hypothesis_template'''] return preprocess_params, {}, {} def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="This is a photo of {}." ) -> Optional[Any]: '''simple docstring''' __snake_case = load_image(__SCREAMING_SNAKE_CASE ) __snake_case = self.image_processor(images=[image] , return_tensors=self.framework ) __snake_case = candidate_labels __snake_case = [hypothesis_template.format(__SCREAMING_SNAKE_CASE ) for x in candidate_labels] __snake_case = self.tokenizer(__SCREAMING_SNAKE_CASE , return_tensors=self.framework , padding=__SCREAMING_SNAKE_CASE ) __snake_case = [text_inputs] return inputs def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' __snake_case = model_inputs.pop('''candidate_labels''' ) __snake_case = model_inputs.pop('''text_inputs''' ) if isinstance(text_inputs[0] , __SCREAMING_SNAKE_CASE ): __snake_case = text_inputs[0] else: # Batching case. __snake_case = text_inputs[0][0] __snake_case = self.model(**__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) __snake_case = { '''candidate_labels''': candidate_labels, '''logits''': outputs.logits_per_image, } return model_outputs def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> str: '''simple docstring''' __snake_case = model_outputs.pop('''candidate_labels''' ) __snake_case = model_outputs['''logits'''][0] if self.framework == "pt": __snake_case = logits.softmax(dim=-1 ).squeeze(-1 ) __snake_case = probs.tolist() if not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): __snake_case = [scores] elif self.framework == "tf": __snake_case = stable_softmax(__SCREAMING_SNAKE_CASE , axis=-1 ) __snake_case = probs.numpy().tolist() else: raise ValueError(F'''Unsupported framework: {self.framework}''' ) __snake_case = [ {'''score''': score, '''label''': candidate_label} for score, candidate_label in sorted(zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) , key=lambda __SCREAMING_SNAKE_CASE : -x[0] ) ] return result
24
0
def snake_case (UpperCAmelCase__ ) -> str: if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise TypeError('\'float\' object cannot be interpreted as an integer' ) if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise TypeError('\'str\' object cannot be interpreted as an integer' ) if num == 0: return "0b0" UpperCamelCase_: Optional[Any] = False if num < 0: UpperCamelCase_: Tuple = True UpperCamelCase_: int = -num UpperCamelCase_: list[int] = [] while num > 0: binary.insert(0 , num % 2 ) num >>= 1 if negative: return "-0b" + "".join(str(UpperCAmelCase__ ) for e in binary ) return "0b" + "".join(str(UpperCAmelCase__ ) for e in binary ) if __name__ == "__main__": import doctest doctest.testmod()
57
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) UpperCAmelCase_ : List[str] = { '''configuration_gpt_bigcode''': ['''GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GPTBigCodeConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : int = [ '''GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST''', '''GPTBigCodeForSequenceClassification''', '''GPTBigCodeForTokenClassification''', '''GPTBigCodeForCausalLM''', '''GPTBigCodeModel''', '''GPTBigCodePreTrainedModel''', ] if TYPE_CHECKING: from .configuration_gpt_bigcode import GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTBigCodeConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_bigcode import ( GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTBigCodeForCausalLM, GPTBigCodeForSequenceClassification, GPTBigCodeForTokenClassification, GPTBigCodeModel, GPTBigCodePreTrainedModel, ) else: import sys UpperCAmelCase_ : Any = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
24
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 __lowerCAmelCase : List[str] = logging.get_logger(__name__) @add_end_docstrings(SCREAMING_SNAKE_CASE__ ) class _lowerCAmelCase ( SCREAMING_SNAKE_CASE__ ): """simple docstring""" def __init__( self , **_lowercase ) -> List[Any]: '''simple docstring''' super().__init__(**_lowercase ) if self.framework == "tf": raise ValueError(f'The {self.__class__} is only available in PyTorch.' ) requires_backends(self , """vision""" ) self.check_model_type(_lowercase ) def __call__( self , _lowercase , _lowercase = None , **_lowercase , ) -> Any: '''simple docstring''' if "text_queries" in kwargs: snake_case_ : Optional[Any] = kwargs.pop("""text_queries""" ) if isinstance(_lowercase , (str, Image.Image) ): snake_case_ : int = {"""image""": image, """candidate_labels""": candidate_labels} else: snake_case_ : Optional[int] = image snake_case_ : Any = super().__call__(_lowercase , **_lowercase ) return results def UpperCAmelCase__ ( self , **_lowercase ) -> Dict: '''simple docstring''' snake_case_ : Optional[int] = {} if "threshold" in kwargs: snake_case_ : str = kwargs["""threshold"""] if "top_k" in kwargs: snake_case_ : Tuple = kwargs["""top_k"""] return {}, {}, postprocess_params def UpperCAmelCase__ ( self , _lowercase ) -> Optional[int]: '''simple docstring''' snake_case_ : List[str] = load_image(inputs["""image"""] ) snake_case_ : Tuple = inputs["""candidate_labels"""] if isinstance(_lowercase , _lowercase ): snake_case_ : Dict = candidate_labels.split(""",""" ) snake_case_ : int = torch.tensor([[image.height, image.width]] , dtype=torch.intaa ) for i, candidate_label in enumerate(_lowercase ): snake_case_ : Dict = self.tokenizer(_lowercase , return_tensors=self.framework ) snake_case_ : Union[str, Any] = self.image_processor(_lowercase , return_tensors=self.framework ) yield { "is_last": i == len(_lowercase ) - 1, "target_size": target_size, "candidate_label": candidate_label, **text_inputs, **image_features, } def UpperCAmelCase__ ( self , _lowercase ) -> Tuple: '''simple docstring''' snake_case_ : int = model_inputs.pop("""target_size""" ) snake_case_ : int = model_inputs.pop("""candidate_label""" ) snake_case_ : Optional[Any] = model_inputs.pop("""is_last""" ) snake_case_ : Dict = self.model(**_lowercase ) snake_case_ : Union[str, Any] = {"""target_size""": target_size, """candidate_label""": candidate_label, """is_last""": is_last, **outputs} return model_outputs def UpperCAmelCase__ ( self , _lowercase , _lowercase=0.1 , _lowercase=None ) -> Any: '''simple docstring''' snake_case_ : Any = [] for model_output in model_outputs: snake_case_ : List[str] = model_output["""candidate_label"""] snake_case_ : Union[str, Any] = BaseModelOutput(_lowercase ) snake_case_ : Dict = self.image_processor.post_process_object_detection( outputs=_lowercase , threshold=_lowercase , target_sizes=model_output["""target_size"""] )[0] for index in outputs["scores"].nonzero(): snake_case_ : List[Any] = outputs["""scores"""][index].item() snake_case_ : List[str] = self._get_bounding_box(outputs["""boxes"""][index][0] ) snake_case_ : int = {"""score""": score, """label""": label, """box""": box} results.append(_lowercase ) snake_case_ : Optional[int] = sorted(_lowercase , key=lambda _lowercase : x["score"] , reverse=_lowercase ) if top_k: snake_case_ : int = results[:top_k] return results def UpperCAmelCase__ ( self , _lowercase ) -> Dict[str, int]: '''simple docstring''' if self.framework != "pt": raise ValueError("""The ZeroShotObjectDetectionPipeline is only available in PyTorch.""" ) snake_case_ , snake_case_ , snake_case_ , snake_case_ : Optional[int] = box.int().tolist() snake_case_ : str = { """xmin""": xmin, """ymin""": ymin, """xmax""": xmax, """ymax""": ymax, } return bbox
58
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' __snake_case = abs(_lowerCamelCase ) __snake_case = 0 while n > 0: res += n % 10 n //= 10 return res def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' __snake_case = abs(_lowerCamelCase ) return n if n < 10 else n % 10 + sum_of_digits(n // 10 ) def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' return sum(int(_lowerCamelCase ) for c in str(abs(_lowerCamelCase ) ) ) def _UpperCamelCase ()-> None: '''simple docstring''' from collections.abc import Callable from timeit import timeit def benchmark_a_function(_lowerCamelCase : Callable , _lowerCamelCase : int ) -> None: __snake_case = f'''{func.__name__}({value})''' __snake_case = timeit(f'''__main__.{call}''' , setup='''import __main__''' ) print(f'''{call:56} = {func(_lowerCamelCase )} -- {timing:.4f} seconds''' ) for value in (26_21_44, 11_25_89_99_06_84_26_24, 1_26_76_50_60_02_28_22_94_01_49_67_03_20_53_76): for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact): benchmark_a_function(_lowerCamelCase , _lowerCamelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
24
0
from typing import List, Optional, Tuple, Union import PIL import torch from torchvision import transforms from diffusers.pipeline_utils import DiffusionPipeline, ImagePipelineOutput from diffusers.schedulers import DDIMScheduler from diffusers.utils import randn_tensor __A = transforms.Compose( [ transforms.Resize((256, 256)), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ] ) def lowerCAmelCase_ ( __a ) -> str: """simple docstring""" if isinstance(__a , torch.Tensor ): return image elif isinstance(__a , PIL.Image.Image ): lowerCamelCase__: Any =[image] lowerCamelCase__: Optional[Any] =[trans(img.convert("RGB" ) ) for img in image] lowerCamelCase__: Dict =torch.stack(__a ) return image class _SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__(self : Any , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Tuple) ->int: '''simple docstring''' super().__init__() # make sure scheduler can always be converted to DDIM lowerCamelCase__: Tuple =DDIMScheduler.from_config(scheduler.config) self.register_modules(unet=UpperCAmelCase_ , scheduler=UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ (self : Optional[int] , UpperCAmelCase_ : Union[str, Any]) ->Dict: '''simple docstring''' if strength < 0 or strength > 1: raise ValueError(F"""The value of strength should in [0.0, 1.0] but is {strength}""") def SCREAMING_SNAKE_CASE_ (self : Optional[int] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Tuple) ->Tuple: '''simple docstring''' lowerCamelCase__: int =min(int(num_inference_steps * strength) , UpperCAmelCase_) lowerCamelCase__: str =max(num_inference_steps - init_timestep , 0) lowerCamelCase__: int =self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def SCREAMING_SNAKE_CASE_ (self : Any , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[int]=None) ->Optional[int]: '''simple docstring''' if not isinstance(UpperCAmelCase_ , (torch.Tensor, PIL.Image.Image, list)): raise ValueError( F"""`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(UpperCAmelCase_)}""") lowerCamelCase__: Optional[int] =image.to(device=UpperCAmelCase_ , dtype=UpperCAmelCase_) if isinstance(UpperCAmelCase_ , UpperCAmelCase_) and len(UpperCAmelCase_) != batch_size: raise ValueError( F"""You have passed a list of generators of length {len(UpperCAmelCase_)}, but requested an effective batch""" F""" size of {batch_size}. Make sure the batch size matches the length of the generators.""") lowerCamelCase__: Dict =init_latents.shape lowerCamelCase__: int =randn_tensor(UpperCAmelCase_ , generator=UpperCAmelCase_ , device=UpperCAmelCase_ , dtype=UpperCAmelCase_) # get latents print("add noise to latents at timestep" , UpperCAmelCase_) lowerCamelCase__: Union[str, Any] =self.scheduler.add_noise(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) lowerCamelCase__: int =init_latents return latents @torch.no_grad() def __call__(self : Tuple , UpperCAmelCase_ : Union[torch.FloatTensor, PIL.Image.Image] = None , UpperCAmelCase_ : float = 0.8 , UpperCAmelCase_ : int = 1 , UpperCAmelCase_ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , UpperCAmelCase_ : float = 0.0 , UpperCAmelCase_ : int = 50 , UpperCAmelCase_ : Optional[bool] = None , UpperCAmelCase_ : Optional[str] = "pil" , UpperCAmelCase_ : bool = True , ) ->Union[ImagePipelineOutput, Tuple]: '''simple docstring''' self.check_inputs(UpperCAmelCase_) # 2. Preprocess image lowerCamelCase__: Dict =preprocess(UpperCAmelCase_) # 3. set timesteps self.scheduler.set_timesteps(UpperCAmelCase_ , device=self.device) lowerCamelCase__ , lowerCamelCase__: str =self.get_timesteps(UpperCAmelCase_ , UpperCAmelCase_ , self.device) lowerCamelCase__: Optional[int] =timesteps[:1].repeat(UpperCAmelCase_) # 4. Prepare latent variables lowerCamelCase__: int =self.prepare_latents(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , self.unet.dtype , self.device , UpperCAmelCase_) lowerCamelCase__: Tuple =latents # 5. Denoising loop for t in self.progress_bar(UpperCAmelCase_): # 1. predict noise model_output lowerCamelCase__: Dict =self.unet(UpperCAmelCase_ , UpperCAmelCase_).sample # 2. predict previous mean of image x_t-1 and add variance depending on eta # eta corresponds to η in paper and should be between [0, 1] # do x_t -> x_t-1 lowerCamelCase__: Optional[int] =self.scheduler.step( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , eta=UpperCAmelCase_ , use_clipped_model_output=UpperCAmelCase_ , generator=UpperCAmelCase_ , ).prev_sample lowerCamelCase__: str =(image / 2 + 0.5).clamp(0 , 1) lowerCamelCase__: Optional[Any] =image.cpu().permute(0 , 2 , 3 , 1).numpy() if output_type == "pil": lowerCamelCase__: Dict =self.numpy_to_pil(UpperCAmelCase_) if not return_dict: return (image, latent_timestep.item()) return ImagePipelineOutput(images=UpperCAmelCase_)
59
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : Optional[Any] )-> Dict: '''simple docstring''' __snake_case = [] __snake_case = [] __snake_case = { '''^''': 3, '''*''': 2, '''/''': 2, '''%''': 2, '''+''': 1, '''-''': 1, } # Priority of each operator __snake_case = len(_lowerCamelCase ) if (len(_lowerCamelCase ) > 7) else 7 # Print table header for output print( '''Symbol'''.center(8 ) , '''Stack'''.center(_lowerCamelCase ) , '''Postfix'''.center(_lowerCamelCase ) , sep=''' | ''' , ) print('''-''' * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(_lowerCamelCase ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(_lowerCamelCase ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(_lowerCamelCase ) == 0: stack.append(_lowerCamelCase ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(_lowerCamelCase ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(_lowerCamelCase ) # push x to stack print( x.center(8 ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , sep=''' | ''' , ) # Output in tabular format while len(_lowerCamelCase ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( ''' '''.center(8 ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , sep=''' | ''' , ) # Output in tabular format return "".join(_lowerCamelCase ) # return Postfix as str def _UpperCamelCase (_lowerCamelCase : Optional[Any] )-> str: '''simple docstring''' __snake_case = list(infix[::-1] ) # reverse the infix equation for i in range(len(_lowerCamelCase ) ): if infix[i] == "(": __snake_case = ''')''' # change "(" to ")" elif infix[i] == ")": __snake_case = '''(''' # change ")" to "(" return (infix_2_postfix(''''''.join(_lowerCamelCase ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": UpperCAmelCase_ : Dict = input('''\nEnter an Infix Equation = ''') # Input an Infix equation UpperCAmelCase_ : Optional[Any] = ''''''.join(Infix.split()) # Remove spaces from the input print('''\n\t''', Infix, '''(Infix) -> ''', infix_2_prefix(Infix), '''(Prefix)''')
24
0
from __future__ import annotations lowerCAmelCase_ = [] def lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) -> bool: """simple docstring""" for i in range(len(_UpperCamelCase ) ): if board[row][i] == 1: return False for i in range(len(_UpperCamelCase ) ): if board[i][column] == 1: return False for i, j in zip(range(_UpperCamelCase , -1 , -1 ) , range(_UpperCamelCase , -1 , -1 ) ): if board[i][j] == 1: return False for i, j in zip(range(_UpperCamelCase , -1 , -1 ) , range(_UpperCamelCase , len(_UpperCamelCase ) ) ): if board[i][j] == 1: return False return True def lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase ) -> bool: """simple docstring""" if row >= len(_UpperCamelCase ): solution.append(_UpperCamelCase ) printboard(_UpperCamelCase ) print() return True for i in range(len(_UpperCamelCase ) ): if is_safe(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): snake_case_ : Optional[int] = 1 solve(_UpperCamelCase , row + 1 ) snake_case_ : Dict = 0 return False def lowerCamelCase_ ( _UpperCamelCase ) -> None: """simple docstring""" for i in range(len(_UpperCamelCase ) ): for j in range(len(_UpperCamelCase ) ): if board[i][j] == 1: print('''Q''' , end=''' ''' ) else: print('''.''' , end=''' ''' ) print() # n=int(input("The no. of queens")) lowerCAmelCase_ = 8 lowerCAmelCase_ = [[0 for i in range(n)] for j in range(n)] solve(board, 0) print('''The total no. of solutions are :''', len(solution))
60
'''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 ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices UpperCAmelCase_ : int = logging.get_logger(__name__) UpperCAmelCase_ : Dict = { '''microsoft/swin-tiny-patch4-window7-224''': ( '''https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json''' ), # See all Swin models at https://huggingface.co/models?filter=swin } class lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase): __lowercase : List[Any] = '''swin''' __lowercase : str = { '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self , __SCREAMING_SNAKE_CASE=224 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=96 , __SCREAMING_SNAKE_CASE=[2, 2, 6, 2] , __SCREAMING_SNAKE_CASE=[3, 6, 12, 24] , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=4.0 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1E-5 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ) -> int: '''simple docstring''' super().__init__(**__SCREAMING_SNAKE_CASE ) __snake_case = image_size __snake_case = patch_size __snake_case = num_channels __snake_case = embed_dim __snake_case = depths __snake_case = len(__SCREAMING_SNAKE_CASE ) __snake_case = num_heads __snake_case = window_size __snake_case = mlp_ratio __snake_case = qkv_bias __snake_case = hidden_dropout_prob __snake_case = attention_probs_dropout_prob __snake_case = drop_path_rate __snake_case = hidden_act __snake_case = use_absolute_embeddings __snake_case = layer_norm_eps __snake_case = initializer_range __snake_case = encoder_stride # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model __snake_case = int(embed_dim * 2 ** (len(__SCREAMING_SNAKE_CASE ) - 1) ) __snake_case = ['''stem'''] + [F'''stage{idx}''' for idx in range(1 , len(__SCREAMING_SNAKE_CASE ) + 1 )] __snake_case , __snake_case = get_aligned_output_features_output_indices( out_features=__SCREAMING_SNAKE_CASE , out_indices=__SCREAMING_SNAKE_CASE , stage_names=self.stage_names ) class lowerCAmelCase ( __lowerCAmelCase): __lowercase : Optional[int] = version.parse('''1.11''') @property def lowerCAmelCase ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def lowerCAmelCase ( self ) -> float: '''simple docstring''' return 1E-4
24
0
from typing import Dict from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, get_torch_dist_unique_port, require_torch_multi_gpu, require_torch_neuroncore, ) from transformers.training_args import ParallelMode from transformers.utils import logging UpperCamelCase = logging.get_logger(__name__) if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset from transformers import Trainer class __lowerCamelCase ( UpperCamelCase__ ): """simple docstring""" def __init__( self : List[Any] , SCREAMING_SNAKE_CASE__ : int = 101 ) -> List[str]: lowerCAmelCase__ = length def __len__( self : Tuple ) -> Union[str, Any]: return self.length def __getitem__( self : Dict , SCREAMING_SNAKE_CASE__ : str ) -> int: return i class __lowerCamelCase : """simple docstring""" def __call__( self : int , SCREAMING_SNAKE_CASE__ : List[str] ) -> int: return {"input_ids": torch.tensor(SCREAMING_SNAKE_CASE__ ), "labels": torch.tensor(SCREAMING_SNAKE_CASE__ )} class __lowerCamelCase ( nn.Module ): """simple docstring""" def __init__( self : Optional[int] ) -> Any: super().__init__() # Add some (unused) params otherwise DDP will complain. lowerCAmelCase__ = nn.Linear(120 , 80 ) def a ( self : Any , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : List[str]=None ) -> Optional[Any]: if labels is not None: return torch.tensor(0.0 , device=input_ids.device ), input_ids else: return input_ids class __lowerCamelCase ( UpperCamelCase__ ): """simple docstring""" @require_torch_neuroncore def a ( self : Tuple ) -> Optional[Any]: lowerCAmelCase__ = f'--nproc_per_node=2\n --master_port={get_torch_dist_unique_port()}\n {self.test_file_dir}/test_trainer_distributed.py\n '.split() lowerCAmelCase__ = self.get_auto_remove_tmp_dir() lowerCAmelCase__ = f'--output_dir {output_dir}'.split() lowerCAmelCase__ = ["torchrun"] + distributed_args + args execute_subprocess_async(SCREAMING_SNAKE_CASE__ , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call class __lowerCamelCase ( UpperCamelCase__ ): """simple docstring""" @require_torch_multi_gpu def a ( self : Dict ) -> List[str]: lowerCAmelCase__ = f'--nproc_per_node={torch.cuda.device_count()}\n --master_port={get_torch_dist_unique_port()}\n {self.test_file_dir}/test_trainer_distributed.py\n '.split() lowerCAmelCase__ = self.get_auto_remove_tmp_dir() lowerCAmelCase__ = f'--output_dir {output_dir}'.split() lowerCAmelCase__ = ["torchrun"] + distributed_args + args execute_subprocess_async(SCREAMING_SNAKE_CASE__ , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call if __name__ == "__main__": # The script below is meant to be run under torch.distributed, on a machine with multiple GPUs: # # PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py UpperCamelCase = HfArgumentParser((TrainingArguments,)) UpperCamelCase = parser.parse_args_into_dataclasses()[0] logger.warning( F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, """ F"""distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}""" ) # Essentially, what we want to verify in the distributed case is that we get all samples back, # in the right order. (this is crucial for prediction for instance) for dataset_length in [101, 40, 7]: UpperCamelCase = DummyDataset(dataset_length) def _A ( lowerCAmelCase_ : EvalPrediction ): """simple docstring""" lowerCAmelCase__ = list(range(len(lowerCAmelCase_ ) ) ) lowerCAmelCase__ = p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential if not success and training_args.local_rank == 0: logger.warning( "Predictions and/or labels do not match expected results:\n - predictions: " F'{p.predictions.tolist()}\n - labels: {p.label_ids.tolist()}\n - expected: {sequential}' ) return {"success": success} UpperCamelCase = Trainer( model=DummyModel(), args=training_args, data_collator=DummyDataCollator(), eval_dataset=dataset, compute_metrics=compute_metrics, ) UpperCamelCase = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) UpperCamelCase = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) UpperCamelCase = 2 UpperCamelCase = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) UpperCamelCase = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) UpperCamelCase = None
61
'''simple docstring''' import re import time from typing import Optional import IPython.display as disp from ..trainer_callback import TrainerCallback from ..trainer_utils import IntervalStrategy, has_length def _UpperCamelCase (_lowerCamelCase : Tuple )-> List[str]: '''simple docstring''' __snake_case = int(_lowerCamelCase ) __snake_case , __snake_case , __snake_case = t // 36_00, (t // 60) % 60, t % 60 return f'''{h}:{m:02d}:{s:02d}''' if h != 0 else f'''{m:02d}:{s:02d}''' def _UpperCamelCase (_lowerCamelCase : Any , _lowerCamelCase : Union[str, Any] , _lowerCamelCase : str , _lowerCamelCase : int , _lowerCamelCase : List[Any]=3_00 )-> int: '''simple docstring''' return f''' <div> {prefix} <progress value=\'{value}\' max=\'{total}\' style=\'width:{width}px; height:20px; vertical-align: middle;\'></progress> {label} </div> ''' def _UpperCamelCase (_lowerCamelCase : int )-> List[Any]: '''simple docstring''' __snake_case = '''<table border="1" class="dataframe">\n''' html_code += """ <thead>\n <tr style="text-align: left;">\n""" for i in items[0]: html_code += f''' <th>{i}</th>\n''' html_code += " </tr>\n </thead>\n <tbody>\n" for line in items[1:]: html_code += " <tr>\n" for elt in line: __snake_case = f'''{elt:.6f}''' if isinstance(_lowerCamelCase , _lowerCamelCase ) else str(_lowerCamelCase ) html_code += f''' <td>{elt}</td>\n''' html_code += " </tr>\n" html_code += " </tbody>\n</table><p>" return html_code class lowerCAmelCase : __lowercase : str = 5 __lowercase : Optional[Any] = 0.2 def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = 300 , ) -> List[Any]: '''simple docstring''' __snake_case = total __snake_case = '''''' if prefix is None else prefix __snake_case = leave __snake_case = parent __snake_case = width __snake_case = None __snake_case = None __snake_case = None def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = None ) -> Any: '''simple docstring''' __snake_case = value if comment is not None: __snake_case = comment if self.last_value is None: __snake_case = __snake_case = time.time() __snake_case = __snake_case = value __snake_case = __snake_case = None __snake_case = self.warmup __snake_case = 1 self.update_bar(__SCREAMING_SNAKE_CASE ) elif value <= self.last_value and not force_update: return elif force_update or self.first_calls > 0 or value >= min(self.last_value + self.wait_for , self.total ): if self.first_calls > 0: self.first_calls -= 1 __snake_case = time.time() __snake_case = current_time - self.start_time # We could have value = self.start_value if the update is called twixe with the same start value. if value > self.start_value: __snake_case = self.elapsed_time / (value - self.start_value) else: __snake_case = None if value >= self.total: __snake_case = self.total __snake_case = None if not self.leave: self.close() elif self.average_time_per_item is not None: __snake_case = self.average_time_per_item * (self.total - value) self.update_bar(__SCREAMING_SNAKE_CASE ) __snake_case = value __snake_case = current_time if self.average_time_per_item is None: __snake_case = 1 else: __snake_case = max(int(self.update_every / self.average_time_per_item ) , 1 ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) -> List[str]: '''simple docstring''' __snake_case = ''' ''' * (len(str(self.total ) ) - len(str(__SCREAMING_SNAKE_CASE ) )) + str(__SCREAMING_SNAKE_CASE ) if self.elapsed_time is None: __snake_case = F'''[{spaced_value}/{self.total} : < :''' elif self.predicted_remaining is None: __snake_case = F'''[{spaced_value}/{self.total} {format_time(self.elapsed_time )}''' else: __snake_case = ( F'''[{spaced_value}/{self.total} {format_time(self.elapsed_time )} <''' F''' {format_time(self.predicted_remaining )}''' ) self.label += F''', {1/self.average_time_per_item:.2f} it/s''' self.label += "]" if self.comment is None or len(self.comment ) == 0 else F''', {self.comment}]''' self.display() def lowerCAmelCase ( self ) -> List[Any]: '''simple docstring''' __snake_case = html_progress_bar(self.value , self.total , self.prefix , self.label , self.width ) if self.parent is not None: # If this is a child bar, the parent will take care of the display. self.parent.display() return if self.output is None: __snake_case = disp.display(disp.HTML(self.html_code ) , display_id=__SCREAMING_SNAKE_CASE ) else: self.output.update(disp.HTML(self.html_code ) ) def lowerCAmelCase ( self ) -> Dict: '''simple docstring''' if self.parent is None and self.output is not None: self.output.update(disp.HTML('''''' ) ) class lowerCAmelCase ( __lowerCAmelCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) -> Any: '''simple docstring''' super().__init__(__SCREAMING_SNAKE_CASE ) __snake_case = None if column_names is None else [column_names] __snake_case = None def lowerCAmelCase ( self ) -> Any: '''simple docstring''' __snake_case = html_progress_bar(self.value , self.total , self.prefix , self.label , self.width ) if self.inner_table is not None: self.html_code += text_to_html_table(self.inner_table ) if self.child_bar is not None: self.html_code += self.child_bar.html_code if self.output is None: __snake_case = disp.display(disp.HTML(self.html_code ) , display_id=__SCREAMING_SNAKE_CASE ) else: self.output.update(disp.HTML(self.html_code ) ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' if self.inner_table is None: __snake_case = [list(values.keys() ), list(values.values() )] else: __snake_case = self.inner_table[0] if len(self.inner_table ) == 1: # We give a chance to update the column names at the first iteration for key in values.keys(): if key not in columns: columns.append(__SCREAMING_SNAKE_CASE ) __snake_case = columns self.inner_table.append([values[c] for c in columns] ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=300 ) -> List[str]: '''simple docstring''' __snake_case = NotebookProgressBar(__SCREAMING_SNAKE_CASE , prefix=__SCREAMING_SNAKE_CASE , parent=self , width=__SCREAMING_SNAKE_CASE ) return self.child_bar def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case = None self.display() class lowerCAmelCase ( __lowerCAmelCase): def __init__( self ) -> str: '''simple docstring''' __snake_case = None __snake_case = None __snake_case = False def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' __snake_case = '''Epoch''' if args.evaluation_strategy == IntervalStrategy.EPOCH else '''Step''' __snake_case = 0 __snake_case = 0 __snake_case = [self.first_column] + ['''Training Loss'''] if args.evaluation_strategy != IntervalStrategy.NO: column_names.append('''Validation Loss''' ) __snake_case = NotebookTrainingTracker(state.max_steps , __SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> List[Any]: '''simple docstring''' __snake_case = int(state.epoch ) if int(state.epoch ) == state.epoch else F'''{state.epoch:.2f}''' self.training_tracker.update( state.global_step + 1 , comment=F'''Epoch {epoch}/{state.num_train_epochs}''' , force_update=self._force_next_update , ) __snake_case = False def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' if not has_length(__SCREAMING_SNAKE_CASE ): return if self.prediction_bar is None: if self.training_tracker is not None: __snake_case = self.training_tracker.add_child(len(__SCREAMING_SNAKE_CASE ) ) else: __snake_case = NotebookProgressBar(len(__SCREAMING_SNAKE_CASE ) ) self.prediction_bar.update(1 ) else: self.prediction_bar.update(self.prediction_bar.value + 1 ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' if self.prediction_bar is not None: self.prediction_bar.close() __snake_case = None def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) -> List[Any]: '''simple docstring''' if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs: __snake_case = {'''Training Loss''': logs['''loss''']} # First column is necessarily Step sine we're not in epoch eval strategy __snake_case = state.global_step self.training_tracker.write_line(__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) -> Optional[Any]: '''simple docstring''' if self.training_tracker is not None: __snake_case = {'''Training Loss''': '''No log''', '''Validation Loss''': '''No log'''} for log in reversed(state.log_history ): if "loss" in log: __snake_case = log['''loss'''] break if self.first_column == "Epoch": __snake_case = int(state.epoch ) else: __snake_case = state.global_step __snake_case = '''eval''' for k in metrics: if k.endswith('''_loss''' ): __snake_case = re.sub(r'''\_loss$''' , '''''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop('''total_flos''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop('''epoch''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_runtime''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_samples_per_second''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_steps_per_second''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_jit_compilation_time''' , __SCREAMING_SNAKE_CASE ) for k, v in metrics.items(): if k == F'''{metric_key_prefix}_loss''': __snake_case = v else: __snake_case = k.split('''_''' ) __snake_case = ''' '''.join([part.capitalize() for part in splits[1:]] ) __snake_case = v self.training_tracker.write_line(__SCREAMING_SNAKE_CASE ) self.training_tracker.remove_child() __snake_case = None # Evaluation takes a long time so we should force the next update. __snake_case = True def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' self.training_tracker.update( state.global_step , comment=F'''Epoch {int(state.epoch )}/{state.num_train_epochs}''' , force_update=__SCREAMING_SNAKE_CASE ) __snake_case = None
24
0
import gc import random import unittest import numpy as np import torch from diffusers import DDIMScheduler, KandinskyVaaPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel from diffusers.utils import floats_tensor, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class SCREAMING_SNAKE_CASE ( lowerCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : List[str] = KandinskyVaaPipeline UpperCamelCase_ : List[Any] = [ '''image_embeds''', '''negative_image_embeds''', ] UpperCamelCase_ : Tuple = ['''image_embeds''', '''negative_image_embeds'''] UpperCamelCase_ : Any = [ '''generator''', '''height''', '''width''', '''latents''', '''guidance_scale''', '''num_inference_steps''', '''return_dict''', '''guidance_scale''', '''num_images_per_prompt''', '''output_type''', '''return_dict''', ] UpperCamelCase_ : List[str] = False @property def _A ( self : List[Any] ): return 32 @property def _A ( self : List[Any] ): return 32 @property def _A ( self : Any ): return self.time_input_dim @property def _A ( self : Union[str, Any] ): return self.time_input_dim * 4 @property def _A ( self : Tuple ): return 100 @property def _A ( self : Optional[int] ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : int = { "in_channels": 4, # Out channels is double in channels because predicts mean and variance "out_channels": 8, "addition_embed_type": "image", "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"), "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"), "mid_block_type": "UNetMidBlock2DSimpleCrossAttn", "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "layers_per_block": 1, "encoder_hid_dim": self.text_embedder_hidden_size, "encoder_hid_dim_type": "image_proj", "cross_attention_dim": self.cross_attention_dim, "attention_head_dim": 4, "resnet_time_scale_shift": "scale_shift", "class_embed_type": None, } SCREAMING_SNAKE_CASE : str = UNetaDConditionModel(**UpperCAmelCase_ ) return model @property def _A ( self : int ): return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def _A ( self : Any ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : List[str] = VQModel(**self.dummy_movq_kwargs ) return model def _A ( self : Optional[Any] ): SCREAMING_SNAKE_CASE : List[str] = self.dummy_unet SCREAMING_SNAKE_CASE : str = self.dummy_movq SCREAMING_SNAKE_CASE : Optional[int] = DDIMScheduler( num_train_timesteps=1000 , beta_schedule="linear" , beta_start=0.00_085 , beta_end=0.012 , clip_sample=UpperCAmelCase_ , set_alpha_to_one=UpperCAmelCase_ , steps_offset=1 , prediction_type="epsilon" , thresholding=UpperCAmelCase_ , ) SCREAMING_SNAKE_CASE : Optional[int] = { "unet": unet, "scheduler": scheduler, "movq": movq, } return components def _A ( self : List[str] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Optional[Any]=0 ): SCREAMING_SNAKE_CASE : List[Any] = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(UpperCAmelCase_ ) ).to(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( UpperCAmelCase_ ) if str(UpperCAmelCase_ ).startswith("mps" ): SCREAMING_SNAKE_CASE : List[Any] = torch.manual_seed(UpperCAmelCase_ ) else: SCREAMING_SNAKE_CASE : Dict = torch.Generator(device=UpperCAmelCase_ ).manual_seed(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Tuple = { "image_embeds": image_embeds, "negative_image_embeds": negative_image_embeds, "generator": generator, "height": 64, "width": 64, "guidance_scale": 4.0, "num_inference_steps": 2, "output_type": "np", } return inputs def _A ( self : Optional[Any] ): SCREAMING_SNAKE_CASE : Any = "cpu" SCREAMING_SNAKE_CASE : Tuple = self.get_dummy_components() SCREAMING_SNAKE_CASE : List[str] = self.pipeline_class(**UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : List[str] = pipe.to(UpperCAmelCase_ ) pipe.set_progress_bar_config(disable=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = pipe(**self.get_dummy_inputs(UpperCAmelCase_ ) ) SCREAMING_SNAKE_CASE : str = output.images SCREAMING_SNAKE_CASE : Tuple = pipe( **self.get_dummy_inputs(UpperCAmelCase_ ) , return_dict=UpperCAmelCase_ , )[0] SCREAMING_SNAKE_CASE : str = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE : Any = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE : List[Any] = np.array( [0.6_237_976, 1.0, 0.36_441_332, 1.0, 0.70_639_634, 0.29_877_186, 0.85_652_125, 0.5_216_843, 0.54_454_046] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), f''' expected_slice {expected_slice}, but got {image_slice.flatten()}''' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), f''' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}''' @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' def _A ( self : Any ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _A ( self : str ): SCREAMING_SNAKE_CASE : Dict = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinskyv22/kandinskyv22_text2img_cat_fp16.npy" ) SCREAMING_SNAKE_CASE : int = KandinskyVaaPriorPipeline.from_pretrained( "kandinsky-community/kandinsky-2-2-prior" , torch_dtype=torch.floataa ) pipe_prior.to(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : int = KandinskyVaaPipeline.from_pretrained( "kandinsky-community/kandinsky-2-2-decoder" , torch_dtype=torch.floataa ) SCREAMING_SNAKE_CASE : Tuple = pipeline.to(UpperCAmelCase_ ) pipeline.set_progress_bar_config(disable=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = "red cat, 4k photo" SCREAMING_SNAKE_CASE : Tuple = torch.Generator(device="cuda" ).manual_seed(0 ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = pipe_prior( UpperCAmelCase_ , generator=UpperCAmelCase_ , num_inference_steps=5 , negative_prompt="" , ).to_tuple() SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Generator(device="cuda" ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Tuple = pipeline( image_embeds=UpperCAmelCase_ , negative_image_embeds=UpperCAmelCase_ , generator=UpperCAmelCase_ , num_inference_steps=100 , output_type="np" , ) SCREAMING_SNAKE_CASE : List[Any] = output.images[0] assert image.shape == (512, 512, 3) assert_mean_pixel_difference(UpperCAmelCase_ , UpperCAmelCase_ )
62
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' if divisor % 5 == 0 or divisor % 2 == 0: return 0 __snake_case = 1 __snake_case = 1 while repunit: __snake_case = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index def _UpperCamelCase (_lowerCamelCase : int = 1_00_00_00 )-> int: '''simple docstring''' __snake_case = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(_lowerCamelCase ) <= limit: divisor += 2 return divisor if __name__ == "__main__": print(F"""{solution() = }""")
24
0
def lowerCamelCase__ ( __lowerCamelCase : int ): if num <= 0: raise ValueError("""Input must be a positive integer""" ) __UpperCAmelCase : int = [True] * (num + 1) __UpperCAmelCase : Tuple = 2 while p * p <= num: if primes[p]: for i in range(p * p , num + 1 , __lowerCamelCase ): __UpperCAmelCase : str = False p += 1 return [prime for prime in range(2 , num + 1 ) if primes[prime]] if __name__ == "__main__": import doctest doctest.testmod() a : Any = int(input("Enter a positive integer: ").strip()) print(prime_sieve_eratosthenes(user_num))
63
'''simple docstring''' from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def _UpperCamelCase (_lowerCamelCase : Union[str, Any] , _lowerCamelCase : List[Any] )-> Optional[Any]: '''simple docstring''' __snake_case = [] for part_id in partition_order: __snake_case = df.where(f'''SPARK_PARTITION_ID() = {part_id}''' ).collect() for row_idx, row in enumerate(_lowerCamelCase ): expected_row_ids_and_row_dicts.append((f'''{part_id}_{row_idx}''', row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Any: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(1_00 ).repartition(1 ) __snake_case = Spark(_lowerCamelCase ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(10 ).repartition(2 ) __snake_case = [1, 0] __snake_case = _generate_iterable_examples(_lowerCamelCase , _lowerCamelCase ) # Reverse the partitions. __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , _lowerCamelCase ) for i, (row_id, row_dict) in enumerate(generate_fn() ): __snake_case , __snake_case = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> int: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(10 ).repartition(1 ) __snake_case = SparkExamplesIterable(_lowerCamelCase ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): assert row_id == f'''0_{i}''' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Union[str, Any]: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''' ) as generator_mock: __snake_case = lambda _lowerCamelCase : x.reverse() __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , [2, 1, 0] ) __snake_case = SparkExamplesIterable(_lowerCamelCase ).shuffle_data_sources(_lowerCamelCase ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): __snake_case , __snake_case = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 __snake_case = SparkExamplesIterable(_lowerCamelCase ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , [0, 2] ) for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): __snake_case , __snake_case = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 __snake_case = SparkExamplesIterable(_lowerCamelCase ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , [1, 3] ) for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): __snake_case , __snake_case = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Optional[int]: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(1_00 ).repartition(1 ) __snake_case = Spark(_lowerCamelCase ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 1_00
24
0
import multiprocessing import os from typing import BinaryIO, Optional, Union import fsspec from .. import Dataset, Features, NamedSplit, config from ..formatting import query_table from ..packaged_modules.json.json import Json from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class _lowerCamelCase ( UpperCamelCase_ ): def __init__( self , lowerCAmelCase , lowerCAmelCase = None , lowerCAmelCase = None , lowerCAmelCase = None , lowerCAmelCase = False , lowerCAmelCase = False , lowerCAmelCase = None , lowerCAmelCase = None , **lowerCAmelCase , ) -> int: super().__init__( lowerCAmelCase , split=lowerCAmelCase , features=lowerCAmelCase , cache_dir=lowerCAmelCase , keep_in_memory=lowerCAmelCase , streaming=lowerCAmelCase , num_proc=lowerCAmelCase , **lowerCAmelCase , ) SCREAMING_SNAKE_CASE__: str= field SCREAMING_SNAKE_CASE__: Optional[int]= path_or_paths if isinstance(lowerCAmelCase , lowerCAmelCase ) else {self.split: path_or_paths} SCREAMING_SNAKE_CASE__: Optional[Any]= Json( cache_dir=lowerCAmelCase , data_files=lowerCAmelCase , features=lowerCAmelCase , field=lowerCAmelCase , **lowerCAmelCase , ) def UpperCamelCase_ ( self ) -> Dict: # Build iterable dataset if self.streaming: SCREAMING_SNAKE_CASE__: Dict= self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: SCREAMING_SNAKE_CASE__: str= None SCREAMING_SNAKE_CASE__: Any= None SCREAMING_SNAKE_CASE__: Optional[Any]= None SCREAMING_SNAKE_CASE__: Any= None self.builder.download_and_prepare( download_config=lowerCAmelCase , download_mode=lowerCAmelCase , verification_mode=lowerCAmelCase , base_path=lowerCAmelCase , num_proc=self.num_proc , ) SCREAMING_SNAKE_CASE__: Optional[int]= self.builder.as_dataset( split=self.split , verification_mode=lowerCAmelCase , in_memory=self.keep_in_memory ) return dataset class _lowerCamelCase : def __init__( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = None , lowerCAmelCase = None , **lowerCAmelCase , ) -> Tuple: if num_proc is not None and num_proc <= 0: raise ValueError(f'num_proc {num_proc} must be an integer > 0.' ) SCREAMING_SNAKE_CASE__: List[str]= dataset SCREAMING_SNAKE_CASE__: int= path_or_buf SCREAMING_SNAKE_CASE__: Dict= batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE SCREAMING_SNAKE_CASE__: List[Any]= num_proc SCREAMING_SNAKE_CASE__: int= '''utf-8''' SCREAMING_SNAKE_CASE__: Union[str, Any]= to_json_kwargs def UpperCamelCase_ ( self ) -> int: SCREAMING_SNAKE_CASE__: str= self.to_json_kwargs.pop('''path_or_buf''' , lowerCAmelCase ) SCREAMING_SNAKE_CASE__: Optional[Any]= self.to_json_kwargs.pop('''orient''' , '''records''' ) SCREAMING_SNAKE_CASE__: Union[str, Any]= self.to_json_kwargs.pop('''lines''' , True if orient == '''records''' else False ) SCREAMING_SNAKE_CASE__: int= self.to_json_kwargs.pop('''index''' , False if orient in ['''split''', '''table'''] else True ) SCREAMING_SNAKE_CASE__: Dict= self.to_json_kwargs.pop('''compression''' , lowerCAmelCase ) if compression not in [None, "infer", "gzip", "bz2", "xz"]: raise NotImplementedError(f'`datasets` currently does not support {compression} compression' ) if isinstance(self.path_or_buf , (str, bytes, os.PathLike) ): with fsspec.open(self.path_or_buf , '''wb''' , compression=lowerCAmelCase ) as buffer: SCREAMING_SNAKE_CASE__: Optional[Any]= self._write(file_obj=lowerCAmelCase , orient=lowerCAmelCase , lines=lowerCAmelCase , index=lowerCAmelCase , **self.to_json_kwargs ) else: if compression: raise NotImplementedError( f'The compression parameter is not supported when writing to a buffer, but compression={compression}' ''' was passed. Please provide a local path instead.''' ) SCREAMING_SNAKE_CASE__: str= self._write( file_obj=self.path_or_buf , orient=lowerCAmelCase , lines=lowerCAmelCase , index=lowerCAmelCase , **self.to_json_kwargs ) return written def UpperCamelCase_ ( self , lowerCAmelCase ) -> int: SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__: Union[str, Any]= args SCREAMING_SNAKE_CASE__: Union[str, Any]= query_table( table=self.dataset.data , key=slice(lowerCAmelCase , offset + self.batch_size ) , indices=self.dataset._indices , ) SCREAMING_SNAKE_CASE__: List[str]= batch.to_pandas().to_json( path_or_buf=lowerCAmelCase , orient=lowerCAmelCase , lines=lowerCAmelCase , index=lowerCAmelCase , **lowerCAmelCase ) if not json_str.endswith('''\n''' ): json_str += "\n" return json_str.encode(self.encoding ) def UpperCamelCase_ ( self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , **lowerCAmelCase , ) -> int: SCREAMING_SNAKE_CASE__: Union[str, Any]= 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 , len(self.dataset ) , self.batch_size ) , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating json from Arrow format''' , ): SCREAMING_SNAKE_CASE__: List[str]= self._batch_json((offset, orient, lines, index, to_json_kwargs) ) written += file_obj.write(lowerCAmelCase ) else: SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__: Dict= len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for json_str in logging.tqdm( pool.imap( self._batch_json , [(offset, orient, lines, index, to_json_kwargs) for offset in range(0 , lowerCAmelCase , lowerCAmelCase )] , ) , total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating json from Arrow format''' , ): written += file_obj.write(lowerCAmelCase ) return written
64
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int , _lowerCamelCase : int , _lowerCamelCase : int )-> float: '''simple docstring''' __snake_case = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) # formula for sum of series return total def _UpperCamelCase ()-> str: '''simple docstring''' print(sum_of_series(1 , 1 , 10 ) ) if __name__ == "__main__": import doctest doctest.testmod()
24
0
"""simple docstring""" import argparse import torch # Step 1. clone https://github.com/microsoft/unilm # Step 2. git checkout to https://github.com/microsoft/unilm/commit/b94ec76c36f02fb2b0bf0dcb0b8554a2185173cd # Step 3. cd unilm # Step 4. ln -s $(realpath wavlm/modules.py) ./ # create simlink # import classes from unilm.wavlm.WavLM import WavLM as WavLMOrig from unilm.wavlm.WavLM import WavLMConfig as WavLMConfigOrig from transformers import WavLMConfig, WavLMModel, logging logging.set_verbosity_info() __UpperCAmelCase = logging.get_logger(__name__) __UpperCAmelCase = { 'post_extract_proj': 'feature_projection.projection', 'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv', 'self_attn.k_proj': 'encoder.layers.*.attention.k_proj', 'self_attn.v_proj': 'encoder.layers.*.attention.v_proj', 'self_attn.q_proj': 'encoder.layers.*.attention.q_proj', 'self_attn.out_proj': 'encoder.layers.*.attention.out_proj', 'self_attn.grep_linear': 'encoder.layers.*.attention.gru_rel_pos_linear', 'self_attn.relative_attention_bias': 'encoder.layers.*.attention.rel_attn_embed', 'self_attn.grep_a': 'encoder.layers.*.attention.gru_rel_pos_const', 'self_attn_layer_norm': 'encoder.layers.*.layer_norm', 'fc1': 'encoder.layers.*.feed_forward.intermediate_dense', 'fc2': 'encoder.layers.*.feed_forward.output_dense', 'final_layer_norm': 'encoder.layers.*.final_layer_norm', 'encoder.layer_norm': 'encoder.layer_norm', 'w2v_model.layer_norm': 'feature_projection.layer_norm', 'quantizer.weight_proj': 'quantizer.weight_proj', 'quantizer.vars': 'quantizer.codevectors', 'project_q': 'project_q', 'final_proj': 'project_hid', 'w2v_encoder.proj': 'ctc_proj', 'mask_emb': 'masked_spec_embed', } __UpperCAmelCase = [ 'ctc_proj', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', ] def lowerCAmelCase ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): '''simple docstring''' for attribute in key.split(""".""" ): UpperCAmelCase__ : List[Any] = getattr(__UpperCamelCase , __UpperCamelCase ) if weight_type is not None: UpperCAmelCase__ : Optional[int] = getattr(__UpperCamelCase , __UpperCamelCase ).shape else: UpperCAmelCase__ : int = hf_pointer.shape assert hf_shape == value.shape, ( F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" F" {value.shape} for {full_name}" ) if weight_type == "weight": UpperCAmelCase__ : str = value elif weight_type == "weight_g": UpperCAmelCase__ : Dict = value elif weight_type == "weight_v": UpperCAmelCase__ : List[str] = value elif weight_type == "bias": UpperCAmelCase__ : Optional[Any] = value else: UpperCAmelCase__ : Optional[Any] = value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def lowerCAmelCase ( __UpperCamelCase , __UpperCamelCase ): '''simple docstring''' UpperCAmelCase__ : List[Any] = [] UpperCAmelCase__ : int = fairseq_model.state_dict() UpperCAmelCase__ : Any = hf_model.feature_extractor for name, value in fairseq_dict.items(): UpperCAmelCase__ : Optional[Any] = False if "conv_layers" in name: load_conv_layer( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , hf_model.config.feat_extract_norm == """group""" , ) UpperCAmelCase__ : Union[str, Any] = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("""w2v_model.""" )[-1] == name.split(""".""" )[0]: UpperCAmelCase__ : List[str] = True if "*" in mapped_key: UpperCAmelCase__ : Any = name.split(__UpperCamelCase )[0].split(""".""" )[-2] UpperCAmelCase__ : Optional[int] = mapped_key.replace("""*""" , __UpperCamelCase ) if "weight_g" in name: UpperCAmelCase__ : str = """weight_g""" elif "weight_v" in name: UpperCAmelCase__ : Optional[Any] = """weight_v""" elif "bias" in name and "relative_attention_bias" not in name: UpperCAmelCase__ : Dict = """bias""" elif "weight" in name: # TODO: don't match quantizer.weight_proj UpperCAmelCase__ : Tuple = """weight""" else: UpperCAmelCase__ : List[Any] = None set_recursively(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) continue if not is_used: unused_weights.append(__UpperCamelCase ) logger.warning(F"Unused weights: {unused_weights}" ) def lowerCAmelCase ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): '''simple docstring''' UpperCAmelCase__ : Dict = full_name.split("""conv_layers.""" )[-1] UpperCAmelCase__ : Tuple = name.split(""".""" ) UpperCAmelCase__ : Optional[Any] = int(items[0] ) UpperCAmelCase__ : int = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) UpperCAmelCase__ : int = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) UpperCAmelCase__ : Tuple = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) UpperCAmelCase__ : Optional[int] = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) UpperCAmelCase__ : Tuple = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(__UpperCamelCase ) @torch.no_grad() def lowerCAmelCase ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=None ): '''simple docstring''' UpperCAmelCase__ : str = torch.load(__UpperCamelCase ) UpperCAmelCase__ : List[str] = WavLMConfigOrig(checkpoint["""cfg"""] ) UpperCAmelCase__ : Optional[Any] = WavLMOrig(__UpperCamelCase ) model.load_state_dict(checkpoint["""model"""] ) model.eval() if config_path is not None: UpperCAmelCase__ : Tuple = WavLMConfig.from_pretrained(__UpperCamelCase ) else: UpperCAmelCase__ : List[Any] = WavLMConfig() UpperCAmelCase__ : Optional[int] = WavLMModel(__UpperCamelCase ) recursively_load_weights(__UpperCamelCase , __UpperCamelCase ) hf_wavlm.save_pretrained(__UpperCamelCase ) if __name__ == "__main__": __UpperCAmelCase = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') __UpperCAmelCase = parser.parse_args() convert_wavlm_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
65
'''simple docstring''' import asyncio import os import re import sys import tempfile import unittest from contextlib import contextmanager from copy import deepcopy from distutils.util import strtobool from enum import Enum from importlib.util import find_spec from pathlib import Path from unittest.mock import patch import pyarrow as pa import pytest import requests from packaging import version from datasets import config if config.PY_VERSION < version.parse('''3.8'''): import importlib_metadata else: import importlib.metadata as importlib_metadata def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : Tuple=False )-> Union[str, Any]: '''simple docstring''' try: __snake_case = os.environ[key] except KeyError: # KEY isn't set, default to `default`. __snake_case = default else: # KEY is set, convert it to True or False. try: __snake_case = strtobool(_lowerCamelCase ) except ValueError: # More values are supported, but let's keep the message simple. raise ValueError(f'''If set, {key} must be yes or no.''' ) return _value UpperCAmelCase_ : Optional[Any] = parse_flag_from_env('''RUN_SLOW''', default=False) UpperCAmelCase_ : Union[str, Any] = parse_flag_from_env('''RUN_REMOTE''', default=False) UpperCAmelCase_ : Optional[Any] = parse_flag_from_env('''RUN_LOCAL''', default=True) UpperCAmelCase_ : Union[str, Any] = parse_flag_from_env('''RUN_PACKAGED''', default=True) # Compression UpperCAmelCase_ : Dict = pytest.mark.skipif(not config.LZ4_AVAILABLE, reason='''test requires lz4''') UpperCAmelCase_ : int = pytest.mark.skipif(not config.PY7ZR_AVAILABLE, reason='''test requires py7zr''') UpperCAmelCase_ : Tuple = pytest.mark.skipif(not config.ZSTANDARD_AVAILABLE, reason='''test requires zstandard''') # Audio UpperCAmelCase_ : str = pytest.mark.skipif( # On Windows and OS X, soundfile installs sndfile find_spec('''soundfile''') is None or version.parse(importlib_metadata.version('''soundfile''')) < version.parse('''0.12.0'''), reason='''test requires sndfile>=0.12.1: \'pip install \"soundfile>=0.12.1\"\'; ''', ) # Beam UpperCAmelCase_ : Tuple = pytest.mark.skipif( not config.BEAM_AVAILABLE or config.DILL_VERSION >= version.parse('''0.3.2'''), reason='''test requires apache-beam and a compatible dill version''', ) # Dill-cloudpickle compatibility UpperCAmelCase_ : Union[str, Any] = pytest.mark.skipif( config.DILL_VERSION <= version.parse('''0.3.2'''), reason='''test requires dill>0.3.2 for cloudpickle compatibility''', ) # Windows UpperCAmelCase_ : int = pytest.mark.skipif( sys.platform == '''win32''', reason='''test should not be run on Windows''', ) def _UpperCamelCase (_lowerCamelCase : List[str] )-> List[Any]: '''simple docstring''' try: import faiss # noqa except ImportError: __snake_case = unittest.skip('''test requires faiss''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> List[str]: '''simple docstring''' try: import regex # noqa except ImportError: __snake_case = unittest.skip('''test requires regex''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> Union[str, Any]: '''simple docstring''' try: import elasticsearch # noqa except ImportError: __snake_case = unittest.skip('''test requires elasticsearch''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Dict )-> List[Any]: '''simple docstring''' try: import sqlalchemy # noqa except ImportError: __snake_case = unittest.skip('''test requires sqlalchemy''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : List[str] )-> List[str]: '''simple docstring''' if not config.TORCH_AVAILABLE: __snake_case = unittest.skip('''test requires PyTorch''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Optional[Any] )-> Dict: '''simple docstring''' if not config.TF_AVAILABLE: __snake_case = unittest.skip('''test requires TensorFlow''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : int )-> Any: '''simple docstring''' if not config.JAX_AVAILABLE: __snake_case = unittest.skip('''test requires JAX''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Tuple )-> str: '''simple docstring''' if not config.PIL_AVAILABLE: __snake_case = unittest.skip('''test requires Pillow''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Dict )-> Any: '''simple docstring''' try: import transformers # noqa F401 except ImportError: return unittest.skip('''test requires transformers''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : int )-> Tuple: '''simple docstring''' try: import tiktoken # noqa F401 except ImportError: return unittest.skip('''test requires tiktoken''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : Dict )-> str: '''simple docstring''' try: import spacy # noqa F401 except ImportError: return unittest.skip('''test requires spacy''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : int )-> Dict: '''simple docstring''' def _require_spacy_model(_lowerCamelCase : int ): try: import spacy # noqa F401 spacy.load(_lowerCamelCase ) except ImportError: return unittest.skip('''test requires spacy''' )(_lowerCamelCase ) except OSError: return unittest.skip('''test requires spacy model \'{}\''''.format(_lowerCamelCase ) )(_lowerCamelCase ) else: return test_case return _require_spacy_model def _UpperCamelCase (_lowerCamelCase : str )-> Dict: '''simple docstring''' try: import pyspark # noqa F401 except ImportError: return unittest.skip('''test requires pyspark''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : Tuple )-> str: '''simple docstring''' try: import joblibspark # noqa F401 except ImportError: return unittest.skip('''test requires joblibspark''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : Any )-> int: '''simple docstring''' if not _run_slow_tests or _run_slow_tests == 0: __snake_case = unittest.skip('''test is slow''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Any )-> Optional[Any]: '''simple docstring''' if not _run_local_tests or _run_local_tests == 0: __snake_case = unittest.skip('''test is local''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : str )-> int: '''simple docstring''' if not _run_packaged_tests or _run_packaged_tests == 0: __snake_case = unittest.skip('''test is packaged''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Optional[int] )-> str: '''simple docstring''' if not _run_remote_tests or _run_remote_tests == 0: __snake_case = unittest.skip('''test requires remote''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (*_lowerCamelCase : str )-> Optional[int]: '''simple docstring''' def decorate(cls : Optional[Any] ): for name, fn in cls.__dict__.items(): if callable(_lowerCamelCase ) and name.startswith('''test''' ): for decorator in decorators: __snake_case = decorator(_lowerCamelCase ) setattr(cls , _lowerCamelCase , _lowerCamelCase ) return cls return decorate class lowerCAmelCase ( __lowerCAmelCase): pass class lowerCAmelCase ( __lowerCAmelCase): __lowercase : List[str] = 0 __lowercase : Dict = 1 __lowercase : List[Any] = 2 @contextmanager def _UpperCamelCase (_lowerCamelCase : Dict=OfflineSimulationMode.CONNECTION_FAILS , _lowerCamelCase : Optional[int]=1E-16 )-> Tuple: '''simple docstring''' __snake_case = requests.Session().request def timeout_request(_lowerCamelCase : Any , _lowerCamelCase : str , _lowerCamelCase : str , **_lowerCamelCase : Any ): # Change the url to an invalid url so that the connection hangs __snake_case = '''https://10.255.255.1''' if kwargs.get('''timeout''' ) is None: raise RequestWouldHangIndefinitelyError( f'''Tried a call to {url} in offline mode with no timeout set. Please set a timeout.''' ) __snake_case = timeout try: return online_request(_lowerCamelCase , _lowerCamelCase , **_lowerCamelCase ) except Exception as e: # The following changes in the error are just here to make the offline timeout error prettier __snake_case = url __snake_case = e.args[0] __snake_case = (max_retry_error.args[0].replace('''10.255.255.1''' , f'''OfflineMock[{url}]''' ),) __snake_case = (max_retry_error,) raise def raise_connection_error(_lowerCamelCase : Union[str, Any] , _lowerCamelCase : Optional[int] , **_lowerCamelCase : Dict ): raise requests.ConnectionError('''Offline mode is enabled.''' , request=_lowerCamelCase ) if mode is OfflineSimulationMode.CONNECTION_FAILS: with patch('''requests.Session.send''' , _lowerCamelCase ): yield elif mode is OfflineSimulationMode.CONNECTION_TIMES_OUT: # inspired from https://stackoverflow.com/a/904609 with patch('''requests.Session.request''' , _lowerCamelCase ): yield elif mode is OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1: with patch('''datasets.config.HF_DATASETS_OFFLINE''' , _lowerCamelCase ): yield else: raise ValueError('''Please use a value from the OfflineSimulationMode enum.''' ) @contextmanager def _UpperCamelCase (*_lowerCamelCase : Union[str, Any] , **_lowerCamelCase : List[str] )-> Any: '''simple docstring''' __snake_case = str(Path().resolve() ) with tempfile.TemporaryDirectory(*_lowerCamelCase , **_lowerCamelCase ) as tmp_dir: try: os.chdir(_lowerCamelCase ) yield finally: os.chdir(_lowerCamelCase ) @contextmanager def _UpperCamelCase ()-> Optional[int]: '''simple docstring''' import gc gc.collect() __snake_case = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory > 0, "Arrow memory didn't increase." @contextmanager def _UpperCamelCase ()-> List[Any]: '''simple docstring''' import gc gc.collect() __snake_case = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory <= 0, "Arrow memory wasn't expected to increase." def _UpperCamelCase (_lowerCamelCase : Any , _lowerCamelCase : int )-> Any: '''simple docstring''' return deepcopy(_lowerCamelCase ).integers(0 , 1_00 , 10 ).tolist() == deepcopy(_lowerCamelCase ).integers(0 , 1_00 , 10 ).tolist() def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> List[Any]: '''simple docstring''' import decorator from requests.exceptions import HTTPError def _wrapper(_lowerCamelCase : int , *_lowerCamelCase : int , **_lowerCamelCase : Optional[int] ): try: return func(*_lowerCamelCase , **_lowerCamelCase ) except HTTPError as err: if str(_lowerCamelCase ).startswith('''500''' ) or str(_lowerCamelCase ).startswith('''502''' ): pytest.xfail(str(_lowerCamelCase ) ) raise err return decorator.decorator(_wrapper , _lowerCamelCase ) class lowerCAmelCase : def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' __snake_case = returncode __snake_case = stdout __snake_case = stderr async def _UpperCamelCase (_lowerCamelCase : List[str] , _lowerCamelCase : Union[str, Any] )-> Dict: '''simple docstring''' while True: __snake_case = await stream.readline() if line: callback(_lowerCamelCase ) else: break async def _UpperCamelCase (_lowerCamelCase : Tuple , _lowerCamelCase : Optional[int]=None , _lowerCamelCase : Optional[Any]=None , _lowerCamelCase : Tuple=None , _lowerCamelCase : Dict=False , _lowerCamelCase : List[Any]=False )-> _RunOutput: '''simple docstring''' if echo: print('''\nRunning: ''' , ''' '''.join(_lowerCamelCase ) ) __snake_case = await asyncio.create_subprocess_exec( cmd[0] , *cmd[1:] , stdin=_lowerCamelCase , stdout=asyncio.subprocess.PIPE , stderr=asyncio.subprocess.PIPE , env=_lowerCamelCase , ) # note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe # https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait # # If it starts hanging, will need to switch to the following code. The problem is that no data # will be seen until it's done and if it hangs for example there will be no debug info. # out, err = await p.communicate() # return _RunOutput(p.returncode, out, err) __snake_case = [] __snake_case = [] def tee(_lowerCamelCase : int , _lowerCamelCase : List[Any] , _lowerCamelCase : str , _lowerCamelCase : Dict="" ): __snake_case = line.decode('''utf-8''' ).rstrip() sink.append(_lowerCamelCase ) if not quiet: print(_lowerCamelCase , _lowerCamelCase , file=_lowerCamelCase ) # XXX: the timeout doesn't seem to make any difference here await asyncio.wait( [ _read_stream(p.stdout , lambda _lowerCamelCase : tee(_lowerCamelCase , _lowerCamelCase , sys.stdout , label='''stdout:''' ) ), _read_stream(p.stderr , lambda _lowerCamelCase : tee(_lowerCamelCase , _lowerCamelCase , sys.stderr , label='''stderr:''' ) ), ] , timeout=_lowerCamelCase , ) return _RunOutput(await p.wait() , _lowerCamelCase , _lowerCamelCase ) def _UpperCamelCase (_lowerCamelCase : Optional[Any] , _lowerCamelCase : Any=None , _lowerCamelCase : List[str]=None , _lowerCamelCase : Optional[Any]=1_80 , _lowerCamelCase : Dict=False , _lowerCamelCase : int=True )-> _RunOutput: '''simple docstring''' __snake_case = asyncio.get_event_loop() __snake_case = loop.run_until_complete( _stream_subprocess(_lowerCamelCase , env=_lowerCamelCase , stdin=_lowerCamelCase , timeout=_lowerCamelCase , quiet=_lowerCamelCase , echo=_lowerCamelCase ) ) __snake_case = ''' '''.join(_lowerCamelCase ) if result.returncode > 0: __snake_case = '''\n'''.join(result.stderr ) raise RuntimeError( f'''\'{cmd_str}\' failed with returncode {result.returncode}\n\n''' f'''The combined stderr from workers follows:\n{stderr}''' ) # check that the subprocess actually did run and produced some output, should the test rely on # the remote side to do the testing if not result.stdout and not result.stderr: raise RuntimeError(f'''\'{cmd_str}\' produced no output.''' ) return result def _UpperCamelCase ()-> Dict: '''simple docstring''' __snake_case = os.environ.get('''PYTEST_XDIST_WORKER''' , '''gw0''' ) __snake_case = re.sub(R'''^gw''' , '''''' , _lowerCamelCase , 0 , re.M ) return int(_lowerCamelCase ) def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = 2_95_00 __snake_case = pytest_xdist_worker_id() return port + uniq_delta
24
0
import os from datetime import datetime as dt from github import Github UpperCamelCase = [ "good first issue", "good second issue", "good difficult issue", "enhancement", "new pipeline/model", "new scheduler", "wip", ] def __magic_name__ ( ) -> Optional[Any]: _lowercase : List[str] = Github(os.environ['GITHUB_TOKEN'] ) _lowercase : Tuple = g.get_repo('huggingface/diffusers' ) _lowercase : Optional[int] = repo.get_issues(state='open' ) for issue in open_issues: _lowercase : List[Any] = sorted(issue.get_comments() , key=lambda SCREAMING_SNAKE_CASE : i.created_at , reverse=SCREAMING_SNAKE_CASE ) _lowercase : Any = comments[0] if len(SCREAMING_SNAKE_CASE ) > 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()
66
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' __snake_case = [[0 for _ in range(_lowerCamelCase )] for _ in range(m + 1 )] for i in range(m + 1 ): __snake_case = 1 for n in range(m + 1 ): for k in range(1 , _lowerCamelCase ): 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: UpperCAmelCase_ : List[str] = int(input('''Enter a number: ''').strip()) print(partition(n)) except ValueError: print('''Please enter a number.''') else: try: UpperCAmelCase_ : Union[str, Any] = int(sys.argv[1]) print(partition(n)) except ValueError: print('''Please pass a number.''')
24
0
import math import os import unittest from transformers import MegatronBertConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, MegatronBertForCausalLM, MegatronBertForMaskedLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, MegatronBertModel, ) class A_ : """simple docstring""" def __init__( self : List[str] ,__A : List[str] ,__A : List[str]=13 ,__A : Dict=7 ,__A : int=True ,__A : Tuple=True ,__A : Tuple=True ,__A : List[Any]=True ,__A : Any=99 ,__A : Union[str, Any]=64 ,__A : Dict=32 ,__A : Any=5 ,__A : List[str]=4 ,__A : Optional[int]=37 ,__A : Optional[int]="gelu" ,__A : Any=0.1 ,__A : str=0.1 ,__A : str=512 ,__A : List[str]=16 ,__A : List[str]=2 ,__A : List[Any]=0.02 ,__A : Optional[int]=3 ,__A : Optional[Any]=4 ,__A : Optional[Any]=None ,) -> Any: _lowercase = parent _lowercase = batch_size _lowercase = seq_length _lowercase = is_training _lowercase = use_input_mask _lowercase = use_token_type_ids _lowercase = use_labels _lowercase = vocab_size _lowercase = hidden_size _lowercase = embedding_size _lowercase = num_hidden_layers _lowercase = num_attention_heads _lowercase = intermediate_size _lowercase = hidden_act _lowercase = hidden_dropout_prob _lowercase = attention_probs_dropout_prob _lowercase = max_position_embeddings _lowercase = type_vocab_size _lowercase = type_sequence_label_size _lowercase = initializer_range _lowercase = num_labels _lowercase = num_choices _lowercase = scope def __UpperCAmelCase ( self : Dict ) -> Union[str, Any]: _lowercase = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) _lowercase = None if self.use_input_mask: _lowercase = random_attention_mask([self.batch_size, self.seq_length] ) _lowercase = None if self.use_token_type_ids: _lowercase = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) _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] ,self.num_choices ) _lowercase = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __UpperCAmelCase ( self : Tuple ) -> Union[str, Any]: return MegatronBertConfig( 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 ,embedding_size=self.embedding_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=__A ,initializer_range=self.initializer_range ,) def __UpperCAmelCase ( self : Union[str, Any] ,__A : Optional[Any] ,__A : Any ,__A : Optional[Any] ,__A : Any ,__A : str ,__A : Tuple ,__A : Optional[int] ) -> str: _lowercase = MegatronBertModel(config=__A ) model.to(__A ) model.eval() _lowercase = model(__A ,attention_mask=__A ,token_type_ids=__A ) _lowercase = model(__A ,token_type_ids=__A ) _lowercase = model(__A ) 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 __UpperCAmelCase ( self : Tuple ,__A : Tuple ,__A : Tuple ,__A : Tuple ,__A : Dict ,__A : Optional[int] ,__A : Dict ,__A : Any ) -> int: _lowercase = MegatronBertForMaskedLM(config=__A ) model.to(__A ) model.eval() _lowercase = model(__A ,attention_mask=__A ,token_type_ids=__A ,labels=__A ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def __UpperCAmelCase ( self : Tuple ,__A : Any ,__A : Union[str, Any] ,__A : Any ,__A : Union[str, Any] ,__A : Union[str, Any] ,__A : int ,__A : Tuple ) -> Any: _lowercase = MegatronBertForCausalLM(config=__A ) model.to(__A ) model.eval() _lowercase = model(__A ,attention_mask=__A ,token_type_ids=__A ,labels=__A ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def __UpperCAmelCase ( self : Tuple ,__A : Tuple ,__A : Dict ,__A : Dict ,__A : int ,__A : Optional[Any] ,__A : Tuple ,__A : str ) -> Tuple: _lowercase = MegatronBertForNextSentencePrediction(config=__A ) model.to(__A ) model.eval() _lowercase = model( __A ,attention_mask=__A ,token_type_ids=__A ,labels=__A ,) self.parent.assertEqual(result.logits.shape ,(self.batch_size, 2) ) def __UpperCAmelCase ( self : List[Any] ,__A : List[str] ,__A : Optional[int] ,__A : int ,__A : Optional[int] ,__A : Optional[Any] ,__A : Tuple ,__A : List[str] ) -> Optional[int]: _lowercase = MegatronBertForPreTraining(config=__A ) model.to(__A ) model.eval() _lowercase = model( __A ,attention_mask=__A ,token_type_ids=__A ,labels=__A ,next_sentence_label=__A ,) 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 __UpperCAmelCase ( self : int ,__A : Union[str, Any] ,__A : Optional[int] ,__A : Dict ,__A : List[Any] ,__A : int ,__A : Tuple ,__A : str ) -> Dict: _lowercase = MegatronBertForQuestionAnswering(config=__A ) model.to(__A ) model.eval() _lowercase = model( __A ,attention_mask=__A ,token_type_ids=__A ,start_positions=__A ,end_positions=__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 : List[str] ,__A : Optional[int] ,__A : Tuple ,__A : int ,__A : Any ,__A : int ,__A : int ,__A : str ) -> Any: _lowercase = self.num_labels _lowercase = MegatronBertForSequenceClassification(__A ) model.to(__A ) model.eval() _lowercase = model(__A ,attention_mask=__A ,token_type_ids=__A ,labels=__A ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def __UpperCAmelCase ( self : int ,__A : List[str] ,__A : str ,__A : Any ,__A : Union[str, Any] ,__A : Optional[int] ,__A : List[str] ,__A : List[Any] ) -> Optional[int]: _lowercase = self.num_labels _lowercase = MegatronBertForTokenClassification(config=__A ) model.to(__A ) model.eval() _lowercase = model(__A ,attention_mask=__A ,token_type_ids=__A ,labels=__A ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) ) def __UpperCAmelCase ( self : List[str] ,__A : List[Any] ,__A : List[Any] ,__A : Any ,__A : Optional[Any] ,__A : Dict ,__A : str ,__A : List[Any] ) -> Optional[Any]: _lowercase = self.num_choices _lowercase = MegatronBertForMultipleChoice(config=__A ) model.to(__A ) 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( __A ,attention_mask=__A ,token_type_ids=__A ,labels=__A ,) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_choices) ) def __UpperCAmelCase ( self : Any ) -> int: _lowercase = self.prepare_config_and_inputs() ( ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ) = config_and_inputs _lowercase = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class A_ ( UpperCAmelCase , UpperCAmelCase , unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ : str = ( ( MegatronBertModel, MegatronBertForMaskedLM, MegatronBertForCausalLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, ) if is_torch_available() else () ) SCREAMING_SNAKE_CASE_ : Any = ( { '''feature-extraction''': MegatronBertModel, '''fill-mask''': MegatronBertForMaskedLM, '''question-answering''': MegatronBertForQuestionAnswering, '''text-classification''': MegatronBertForSequenceClassification, '''text-generation''': MegatronBertForCausalLM, '''token-classification''': MegatronBertForTokenClassification, '''zero-shot''': MegatronBertForSequenceClassification, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE_ : Optional[Any] = True # test_resize_embeddings = False SCREAMING_SNAKE_CASE_ : str = False def __UpperCAmelCase ( self : List[Any] ,__A : Dict ,__A : Dict ,__A : List[str]=False ) -> Optional[Any]: _lowercase = super()._prepare_for_class(__A ,__A ,return_labels=__A ) if return_labels: if model_class in get_values(__A ): _lowercase = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) ,dtype=torch.long ,device=__A ) _lowercase = torch.zeros( self.model_tester.batch_size ,dtype=torch.long ,device=__A ) return inputs_dict def __UpperCAmelCase ( self : Optional[Any] ) -> Optional[int]: _lowercase = MegatronBertModelTester(self ) _lowercase = ConfigTester(self ,config_class=__A ,hidden_size=37 ) def __UpperCAmelCase ( self : Dict ) -> str: self.config_tester.run_common_tests() def __UpperCAmelCase ( self : List[Any] ) -> Optional[Any]: _lowercase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_model(*__A ) def __UpperCAmelCase ( self : int ) -> str: _lowercase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_masked_lm(*__A ) def __UpperCAmelCase ( self : List[str] ) -> int: _lowercase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*__A ) def __UpperCAmelCase ( self : Union[str, Any] ) -> Dict: _lowercase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*__A ) def __UpperCAmelCase ( self : List[Any] ) -> Any: _lowercase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_pretraining(*__A ) def __UpperCAmelCase ( self : Optional[int] ) -> Union[str, Any]: _lowercase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_question_answering(*__A ) def __UpperCAmelCase ( self : Dict ) -> Dict: _lowercase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*__A ) def __UpperCAmelCase ( self : List[Any] ) -> int: _lowercase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_token_classification(*__A ) def SCREAMING_SNAKE_CASE__ ( snake_case__ :Optional[int] ) -> Optional[Any]: return torch.tensor( snake_case__ , dtype=torch.long , device=snake_case__ , ) snake_case = 1e-4 @require_torch @require_sentencepiece @require_tokenizers class A_ ( unittest.TestCase ): """simple docstring""" @slow @unittest.skip('Model is not available.' ) def __UpperCAmelCase ( self : Any ) -> Tuple: _lowercase = 'nvidia/megatron-bert-uncased-345m' if "MYDIR" in os.environ: _lowercase = os.path.join(os.environ['MYDIR'] ,__A ) _lowercase = MegatronBertModel.from_pretrained(__A ) model.to(__A ) model.half() _lowercase = _long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]] ) with torch.no_grad(): _lowercase = model(__A )[0] _lowercase = torch.Size((1, 9, 1024) ) self.assertEqual(output.shape ,__A ) _lowercase = [-0.6040, -0.2517, -0.1025, 0.3420, -0.6758, -0.0017, -0.1089, -0.1990, 0.5728] for ii in range(3 ): for jj in range(3 ): _lowercase = output[0, ii, jj] _lowercase = expected[3 * ii + jj] _lowercase = 'ii={} jj={} a={} b={}'.format(__A ,__A ,__A ,__A ) self.assertTrue(math.isclose(__A ,__A ,rel_tol=__A ,abs_tol=__A ) ,msg=__A )
67
'''simple docstring''' import argparse import os import re UpperCAmelCase_ : List[str] = '''src/transformers/models/auto''' # re pattern that matches mapping introductions: # SUPER_MODEL_MAPPING_NAMES = OrderedDict or SUPER_MODEL_MAPPING = OrderedDict UpperCAmelCase_ : Tuple = re.compile(R'''[A-Z_]+_MAPPING(\s+|_[A-Z_]+\s+)=\s+OrderedDict''') # re pattern that matches identifiers in mappings UpperCAmelCase_ : Dict = re.compile(R'''\s*\(\s*"(\S[^"]+)"''') def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : bool = False )-> str: '''simple docstring''' with open(_lowerCamelCase , '''r''' , encoding='''utf-8''' ) as f: __snake_case = f.read() __snake_case = content.split('''\n''' ) __snake_case = [] __snake_case = 0 while line_idx < len(_lowerCamelCase ): if _re_intro_mapping.search(lines[line_idx] ) is not None: __snake_case = len(re.search(R'''^(\s*)\S''' , lines[line_idx] ).groups()[0] ) + 8 # Start of a new mapping! while not lines[line_idx].startswith(''' ''' * indent + '''(''' ): new_lines.append(lines[line_idx] ) line_idx += 1 __snake_case = [] while lines[line_idx].strip() != "]": # Blocks either fit in one line or not if lines[line_idx].strip() == "(": __snake_case = line_idx while not lines[line_idx].startswith(''' ''' * indent + ''')''' ): line_idx += 1 blocks.append('''\n'''.join(lines[start_idx : line_idx + 1] ) ) else: blocks.append(lines[line_idx] ) line_idx += 1 # Sort blocks by their identifiers __snake_case = sorted(_lowerCamelCase , key=lambda _lowerCamelCase : _re_identifier.search(_lowerCamelCase ).groups()[0] ) new_lines += blocks else: new_lines.append(lines[line_idx] ) line_idx += 1 if overwrite: with open(_lowerCamelCase , '''w''' , encoding='''utf-8''' ) as f: f.write('''\n'''.join(_lowerCamelCase ) ) elif "\n".join(_lowerCamelCase ) != content: return True def _UpperCamelCase (_lowerCamelCase : bool = False )-> Tuple: '''simple docstring''' __snake_case = [os.path.join(_lowerCamelCase , _lowerCamelCase ) for f in os.listdir(_lowerCamelCase ) if f.endswith('''.py''' )] __snake_case = [sort_auto_mapping(_lowerCamelCase , overwrite=_lowerCamelCase ) for fname in fnames] if not overwrite and any(_lowerCamelCase ): __snake_case = [f for f, d in zip(_lowerCamelCase , _lowerCamelCase ) if d] raise ValueError( f'''The following files have auto mappings that need sorting: {", ".join(_lowerCamelCase )}. Run `make style` to fix''' ''' this.''' ) if __name__ == "__main__": UpperCAmelCase_ : str = argparse.ArgumentParser() parser.add_argument('''--check_only''', action='''store_true''', help='''Whether to only check or fix style.''') UpperCAmelCase_ : List[Any] = parser.parse_args() sort_all_auto_mappings(not args.check_only)
24
0
import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing the experiment tracking capability, # and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## __A = 16 __A = 32 def lowercase__ ( A_: Accelerator , A_: int = 16 ) -> Tuple: """simple docstring""" __UpperCAmelCase =AutoTokenizer.from_pretrained("""bert-base-cased""" ) __UpperCAmelCase =load_dataset("""glue""" , """mrpc""" ) def tokenize_function(A_: Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) __UpperCAmelCase =tokenizer(examples["""sentence1"""] , examples["""sentence2"""] , truncation=A_ , max_length=A_ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): __UpperCAmelCase =datasets.map( A_ , batched=A_ , remove_columns=["""idx""", """sentence1""", """sentence2"""] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library __UpperCAmelCase =tokenized_datasets.rename_column("""label""" , """labels""" ) def collate_fn(A_: Union[str, Any] ): # On TPU it's best to pad everything to the same length or training will be very slow. __UpperCAmelCase =128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": __UpperCAmelCase =16 elif accelerator.mixed_precision != "no": __UpperCAmelCase =8 else: __UpperCAmelCase =None return tokenizer.pad( A_ , padding="""longest""" , max_length=A_ , pad_to_multiple_of=A_ , return_tensors="""pt""" , ) # Instantiate dataloaders. __UpperCAmelCase =DataLoader( tokenized_datasets["""train"""] , shuffle=A_ , collate_fn=A_ , batch_size=A_ ) __UpperCAmelCase =DataLoader( tokenized_datasets["""validation"""] , shuffle=A_ , collate_fn=A_ , batch_size=A_ ) return train_dataloader, eval_dataloader # For testing only if os.environ.get("TESTING_MOCKED_DATALOADERS", None) == "1": from accelerate.test_utils.training import mocked_dataloaders __A = mocked_dataloaders # noqa: F811 def lowercase__ ( A_: List[str] , A_: List[str] ) -> Dict: """simple docstring""" if os.environ.get("""TESTING_MOCKED_DATALOADERS""" , A_ ) == "1": __UpperCAmelCase =2 # Initialize Accelerator # New Code # # We pass in "all" to `log_with` to grab all available trackers in the environment # Note: If using a custom `Tracker` class, should be passed in here such as: # >>> log_with = ["all", MyCustomTrackerClassInstance()] if args.with_tracking: __UpperCAmelCase =Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , log_with="""all""" , project_dir=args.project_dir ) else: __UpperCAmelCase =Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __UpperCAmelCase =config["""lr"""] __UpperCAmelCase =int(config["""num_epochs"""] ) __UpperCAmelCase =int(config["""seed"""] ) __UpperCAmelCase =int(config["""batch_size"""] ) set_seed(A_ ) __UpperCAmelCase , __UpperCAmelCase =get_dataloaders(A_ , A_ ) __UpperCAmelCase =evaluate.load("""glue""" , """mrpc""" ) # If the batch size is too big we use gradient accumulation __UpperCAmelCase =1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: __UpperCAmelCase =batch_size // MAX_GPU_BATCH_SIZE __UpperCAmelCase =MAX_GPU_BATCH_SIZE # Instantiate the model (we build the model here so that the seed also control new weights initialization) __UpperCAmelCase =AutoModelForSequenceClassification.from_pretrained("""bert-base-cased""" , return_dict=A_ ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). __UpperCAmelCase =model.to(accelerator.device ) # Instantiate optimizer __UpperCAmelCase =AdamW(params=model.parameters() , lr=A_ ) # Instantiate scheduler __UpperCAmelCase =get_linear_schedule_with_warmup( optimizer=A_ , num_warmup_steps=100 , num_training_steps=(len(A_ ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase =accelerator.prepare( A_ , A_ , A_ , A_ , A_ ) # New Code # # We need to initialize the trackers we use. Overall configurations can also be stored if args.with_tracking: __UpperCAmelCase =os.path.split(A_ )[-1].split(""".""" )[0] accelerator.init_trackers(A_ , A_ ) # Now we train the model for epoch in range(A_ ): model.train() # New Code # # For our tracking example, we will log the total loss of each epoch if args.with_tracking: __UpperCAmelCase =0 for step, batch in enumerate(A_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) __UpperCAmelCase =model(**A_ ) __UpperCAmelCase =outputs.loss # New Code # if args.with_tracking: total_loss += loss.detach().float() __UpperCAmelCase =loss / gradient_accumulation_steps accelerator.backward(A_ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(A_ ): # We could avoid this line since we set the accelerator with `device_placement=True` (the default). batch.to(accelerator.device ) with torch.no_grad(): __UpperCAmelCase =model(**A_ ) __UpperCAmelCase =outputs.logits.argmax(dim=-1 ) __UpperCAmelCase , __UpperCAmelCase =accelerator.gather_for_metrics((predictions, batch["""labels"""]) ) metric.add_batch( predictions=A_ , references=A_ , ) __UpperCAmelCase =metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , A_ ) # New Code # # To actually log, we call `Accelerator.log` # The values passed can be of `str`, `int`, `float` or `dict` of `str` to `float`/`int` if args.with_tracking: accelerator.log( { """accuracy""": eval_metric["""accuracy"""], """f1""": eval_metric["""f1"""], """train_loss""": total_loss.item() / len(A_ ), """epoch""": epoch, } , step=A_ , ) # New Code # # When a run is finished, you should call `accelerator.end_training()` # to close all of the open trackers if args.with_tracking: accelerator.end_training() def lowercase__ ( ) -> List[Any]: """simple docstring""" __UpperCAmelCase =argparse.ArgumentParser(description="""Simple example of training script.""" ) parser.add_argument( """--mixed_precision""" , type=A_ , default=A_ , choices=["""no""", """fp16""", """bf16""", """fp8"""] , help="""Whether to use mixed precision. Choose""" """between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.""" """and an Nvidia Ampere GPU.""" , ) parser.add_argument("""--cpu""" , action="""store_true""" , help="""If passed, will train on the CPU.""" ) parser.add_argument( """--with_tracking""" , action="""store_true""" , help="""Whether to load in all available experiment trackers from the environment and use them for logging.""" , ) parser.add_argument( """--project_dir""" , type=A_ , default="""logs""" , help="""Location on where to store experiment tracking logs` and relevent project information""" , ) __UpperCAmelCase =parser.parse_args() __UpperCAmelCase ={"""lr""": 2e-5, """num_epochs""": 3, """seed""": 42, """batch_size""": 16} training_function(A_ , A_ ) if __name__ == "__main__": main()
68
'''simple docstring''' import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _UpperCamelCase (*_lowerCamelCase : str , _lowerCamelCase : Optional[Union[Dict, Any]] = None , _lowerCamelCase : List[Any]=True , _lowerCamelCase : str=2 )-> str: '''simple docstring''' from .. import __version__ __snake_case = take_from __snake_case = () if not isinstance(args[0] , _lowerCamelCase ): __snake_case = (args,) for attribute, version_name, message in args: if version.parse(version.parse(_lowerCamelCase ).base_version ) >= version.parse(_lowerCamelCase ): raise ValueError( f'''The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\'''' f''' version {__version__} is >= {version_name}''' ) __snake_case = None if isinstance(_lowerCamelCase , _lowerCamelCase ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(_lowerCamelCase ),) __snake_case = f'''The `{attribute}` argument is deprecated and will be removed in version {version_name}.''' elif hasattr(_lowerCamelCase , _lowerCamelCase ): values += (getattr(_lowerCamelCase , _lowerCamelCase ),) __snake_case = f'''The `{attribute}` attribute is deprecated and will be removed in version {version_name}.''' elif deprecated_kwargs is None: __snake_case = f'''`{attribute}` is deprecated and will be removed in version {version_name}.''' if warning is not None: __snake_case = warning + ''' ''' if standard_warn else '''''' warnings.warn(warning + message , _lowerCamelCase , stacklevel=_lowerCamelCase ) if isinstance(_lowerCamelCase , _lowerCamelCase ) and len(_lowerCamelCase ) > 0: __snake_case = inspect.getouterframes(inspect.currentframe() )[1] __snake_case = call_frame.filename __snake_case = call_frame.lineno __snake_case = call_frame.function __snake_case , __snake_case = next(iter(deprecated_kwargs.items() ) ) raise TypeError(f'''{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`''' ) if len(_lowerCamelCase ) == 0: return elif len(_lowerCamelCase ) == 1: return values[0] return values
24
0
'''simple docstring''' import math def __UpperCAmelCase ( _UpperCAmelCase : list , _UpperCAmelCase : int ) -> int: __snake_case = len(_UpperCAmelCase ) __snake_case = int(math.floor(math.sqrt(_UpperCAmelCase ) ) ) __snake_case = 0 while arr[min(_UpperCAmelCase , _UpperCAmelCase ) - 1] < x: __snake_case = step step += int(math.floor(math.sqrt(_UpperCAmelCase ) ) ) if prev >= n: return -1 while arr[prev] < x: __snake_case = prev + 1 if prev == min(_UpperCAmelCase , _UpperCAmelCase ): return -1 if arr[prev] == x: return prev return -1 if __name__ == "__main__": a : int = input('''Enter numbers separated by a comma:\n''').strip() a : int = [int(item) for item in user_input.split(''',''')] a : Optional[Any] = int(input('''Enter the number to be searched:\n''')) a : Tuple = jump_search(arr, x) if res == -1: print('''Number not found!''') else: print(F'''Number {x} is at index {res}''')
69
'''simple docstring''' import argparse import re from pathlib import Path import requests import torch from PIL import Image from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor from transformers import ( EfficientFormerConfig, EfficientFormerForImageClassificationWithTeacher, EfficientFormerImageProcessor, ) from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def _UpperCamelCase (_lowerCamelCase : int , _lowerCamelCase : str )-> List[str]: '''simple docstring''' __snake_case = old_name if "patch_embed" in old_name: __snake_case , __snake_case , __snake_case = old_name.split('''.''' ) if layer == "0": __snake_case = old_name.replace('''0''' , '''convolution1''' ) elif layer == "1": __snake_case = old_name.replace('''1''' , '''batchnorm_before''' ) elif layer == "3": __snake_case = old_name.replace('''3''' , '''convolution2''' ) else: __snake_case = old_name.replace('''4''' , '''batchnorm_after''' ) if "network" in old_name and re.search(R'''\d\.\d''' , _lowerCamelCase ): __snake_case = R'''\b\d{2}\b''' if bool(re.search(_lowerCamelCase , _lowerCamelCase ) ): __snake_case = re.search(R'''\d\.\d\d.''' , _lowerCamelCase ).group() else: __snake_case = re.search(R'''\d\.\d.''' , _lowerCamelCase ).group() if int(match[0] ) < 6: __snake_case = old_name.replace(_lowerCamelCase , '''''' ) __snake_case = trimmed_name.replace('''network''' , match[0] + '''.meta4D_layers.blocks.''' + match[2:-1] ) __snake_case = '''intermediate_stages.''' + trimmed_name else: __snake_case = old_name.replace(_lowerCamelCase , '''''' ) if int(match[2] ) < num_meta4D_last_stage: __snake_case = trimmed_name.replace('''network''' , '''meta4D_layers.blocks.''' + match[2] ) else: __snake_case = str(int(match[2] ) - num_meta4D_last_stage ) __snake_case = trimmed_name.replace('''network''' , '''meta3D_layers.blocks.''' + layer_index ) if "norm1" in old_name: __snake_case = trimmed_name.replace('''norm1''' , '''layernorm1''' ) elif "norm2" in old_name: __snake_case = trimmed_name.replace('''norm2''' , '''layernorm2''' ) elif "fc1" in old_name: __snake_case = trimmed_name.replace('''fc1''' , '''linear_in''' ) elif "fc2" in old_name: __snake_case = trimmed_name.replace('''fc2''' , '''linear_out''' ) __snake_case = '''last_stage.''' + trimmed_name elif "network" in old_name and re.search(R'''.\d.''' , _lowerCamelCase ): __snake_case = old_name.replace('''network''' , '''intermediate_stages''' ) if "fc" in new_name: __snake_case = new_name.replace('''fc''' , '''convolution''' ) elif ("norm1" in new_name) and ("layernorm1" not in new_name): __snake_case = new_name.replace('''norm1''' , '''batchnorm_before''' ) elif ("norm2" in new_name) and ("layernorm2" not in new_name): __snake_case = new_name.replace('''norm2''' , '''batchnorm_after''' ) if "proj" in new_name: __snake_case = new_name.replace('''proj''' , '''projection''' ) if "dist_head" in new_name: __snake_case = new_name.replace('''dist_head''' , '''distillation_classifier''' ) elif "head" in new_name: __snake_case = new_name.replace('''head''' , '''classifier''' ) elif "patch_embed" in new_name: __snake_case = '''efficientformer.''' + new_name elif new_name == "norm.weight" or new_name == "norm.bias": __snake_case = new_name.replace('''norm''' , '''layernorm''' ) __snake_case = '''efficientformer.''' + new_name else: __snake_case = '''efficientformer.encoder.''' + new_name return new_name def _UpperCamelCase (_lowerCamelCase : str , _lowerCamelCase : Tuple )-> List[str]: '''simple docstring''' for key in checkpoint.copy().keys(): __snake_case = checkpoint.pop(_lowerCamelCase ) __snake_case = val return checkpoint def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = '''http://images.cocodataset.org/val2017/000000039769.jpg''' __snake_case = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return image def _UpperCamelCase (_lowerCamelCase : Path , _lowerCamelCase : Path , _lowerCamelCase : Path , _lowerCamelCase : bool )-> Optional[Any]: '''simple docstring''' __snake_case = torch.load(_lowerCamelCase , map_location='''cpu''' )['''model'''] __snake_case = EfficientFormerConfig.from_json_file(_lowerCamelCase ) __snake_case = EfficientFormerForImageClassificationWithTeacher(_lowerCamelCase ) __snake_case = '''_'''.join(checkpoint_path.split('''/''' )[-1].split('''.''' )[0].split('''_''' )[:-1] ) __snake_case = config.depths[-1] - config.num_metaad_blocks + 1 __snake_case = convert_torch_checkpoint(_lowerCamelCase , _lowerCamelCase ) model.load_state_dict(_lowerCamelCase ) model.eval() __snake_case = { '''bilinear''': PILImageResampling.BILINEAR, '''bicubic''': PILImageResampling.BICUBIC, '''nearest''': PILImageResampling.NEAREST, } # prepare image __snake_case = prepare_img() __snake_case = 2_56 __snake_case = 2_24 __snake_case = EfficientFormerImageProcessor( size={'''shortest_edge''': image_size} , crop_size={'''height''': crop_size, '''width''': crop_size} , resample=pillow_resamplings['''bicubic'''] , ) __snake_case = processor(images=_lowerCamelCase , return_tensors='''pt''' ).pixel_values # original processing pipeline __snake_case = Compose( [ Resize(_lowerCamelCase , interpolation=pillow_resamplings['''bicubic'''] ), CenterCrop(_lowerCamelCase ), ToTensor(), Normalize(_lowerCamelCase , _lowerCamelCase ), ] ) __snake_case = image_transforms(_lowerCamelCase ).unsqueeze(0 ) assert torch.allclose(_lowerCamelCase , _lowerCamelCase ) __snake_case = model(_lowerCamelCase ) __snake_case = outputs.logits __snake_case = (1, 10_00) if "l1" in model_name: __snake_case = torch.Tensor( [-0.1312, 0.4353, -1.0499, -0.5124, 0.4183, -0.6793, -1.3777, -0.0893, -0.7358, -2.4328] ) assert torch.allclose(logits[0, :10] , _lowerCamelCase , atol=1E-3 ) assert logits.shape == expected_shape elif "l3" in model_name: __snake_case = torch.Tensor( [-1.3150, -1.5456, -1.2556, -0.8496, -0.7127, -0.7897, -0.9728, -0.3052, 0.3751, -0.3127] ) assert torch.allclose(logits[0, :10] , _lowerCamelCase , atol=1E-3 ) assert logits.shape == expected_shape elif "l7" in model_name: __snake_case = torch.Tensor( [-1.0283, -1.4131, -0.5644, -1.3115, -0.5785, -1.2049, -0.7528, 0.1992, -0.3822, -0.0878] ) assert logits.shape == expected_shape else: raise ValueError( f'''Unknown model checkpoint: {checkpoint_path}. Supported version of efficientformer are l1, l3 and l7''' ) # Save Checkpoints Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase ) model.save_pretrained(_lowerCamelCase ) print(f'''Checkpoint successfuly converted. Model saved at {pytorch_dump_path}''' ) processor.save_pretrained(_lowerCamelCase ) print(f'''Processor successfuly saved at {pytorch_dump_path}''' ) if push_to_hub: print('''Pushing model to the hub...''' ) model.push_to_hub( repo_id=f'''Bearnardd/{pytorch_dump_path}''' , commit_message='''Add model''' , use_temp_dir=_lowerCamelCase , ) processor.push_to_hub( repo_id=f'''Bearnardd/{pytorch_dump_path}''' , commit_message='''Add image processor''' , use_temp_dir=_lowerCamelCase , ) if __name__ == "__main__": UpperCAmelCase_ : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--pytorch_model_path''', default=None, type=str, required=True, help='''Path to EfficientFormer pytorch checkpoint.''', ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The json file for EfficientFormer model config.''', ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') parser.add_argument( '''--no-push_to_hub''', dest='''push_to_hub''', action='''store_false''', help='''Do not push model and image processor to the hub''', ) parser.set_defaults(push_to_hub=True) UpperCAmelCase_ : Union[str, Any] = parser.parse_args() convert_efficientformer_checkpoint( checkpoint_path=args.pytorch_model_path, efficientformer_config_file=args.config_file, pytorch_dump_path=args.pytorch_dump_path, push_to_hub=args.push_to_hub, )
24
0
import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import numpy as np import pandas as pd from datasets import load_dataset import transformers from transformers import ( AutoConfig, BartForSequenceClassification, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, TapexTokenizer, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.17.0.dev0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/text-classification/requirements.txt") lowerCamelCase : Dict = logging.getLogger(__name__) @dataclass class A: '''simple docstring''' UpperCamelCase = field( default='''tab_fact''' , metadata={'''help''': '''The name of the dataset to use (via the datasets library).'''} ) UpperCamelCase = field( default='''tab_fact''' , metadata={'''help''': '''The configuration name of the dataset to use (via the datasets library).'''} , ) UpperCamelCase = field( default=1024 , metadata={ '''help''': ( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) } , ) UpperCamelCase = field( default=UpperCamelCase , metadata={'''help''': '''Overwrite the cached preprocessed datasets or not.'''} ) UpperCamelCase = field( default=UpperCamelCase , metadata={ '''help''': ( '''Whether to pad all samples to `max_seq_length`. ''' '''If False, will pad the samples dynamically when batching to the maximum length in the batch.''' ) } , ) UpperCamelCase = field( default=UpperCamelCase , metadata={ '''help''': ( '''For debugging purposes or quicker training, truncate the number of training examples to this ''' '''value if set.''' ) } , ) UpperCamelCase = field( default=UpperCamelCase , metadata={ '''help''': ( '''For debugging purposes or quicker training, truncate the number of evaluation examples to this ''' '''value if set.''' ) } , ) UpperCamelCase = field( default=UpperCamelCase , metadata={ '''help''': ( '''For debugging purposes or quicker training, truncate the number of prediction examples to this ''' '''value if set.''' ) } , ) UpperCamelCase = field( default=UpperCamelCase , metadata={'''help''': '''A csv or a json file containing the training data.'''} ) UpperCamelCase = field( default=UpperCamelCase , metadata={'''help''': '''A csv or a json file containing the validation data.'''} ) UpperCamelCase = field(default=UpperCamelCase , metadata={'''help''': '''A csv or a json file containing the test data.'''} ) def a__ ( self : Dict ) -> Union[str, Any]: """simple docstring""" if self.dataset_name is not None: pass elif self.train_file is None or self.validation_file is None: raise ValueError('Need either a GLUE task, a training/validation file or a dataset name.' ) else: lowerCamelCase_ = self.train_file.split('.' )[-1] assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file." lowerCamelCase_ = self.validation_file.split('.' )[-1] assert ( validation_extension == train_extension ), "`validation_file` should have the same extension (csv or json) as `train_file`." @dataclass class A: '''simple docstring''' UpperCamelCase = field( default=UpperCamelCase , metadata={'''help''': '''Path to pretrained model or model identifier from huggingface.co/models'''} ) UpperCamelCase = field( default=UpperCamelCase , metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) UpperCamelCase = field( default=UpperCamelCase , metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} ) UpperCamelCase = field( default=UpperCamelCase , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''} , ) UpperCamelCase = field( default=UpperCamelCase , metadata={'''help''': '''Whether to use one of the fast tokenizer (backed by the tokenizers library) or not.'''} , ) UpperCamelCase = field( default='''main''' , metadata={'''help''': '''The specific model version to use (can be a branch name, tag name or commit id).'''} , ) UpperCamelCase = field( default=UpperCamelCase , metadata={ '''help''': ( '''Will use the token generated when running `huggingface-cli login` (necessary to use this script ''' '''with private models).''' ) } , ) def _SCREAMING_SNAKE_CASE ( ): '''simple docstring''' lowerCamelCase_ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('.json' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ = parser.parse_args_into_dataclasses() # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , handlers=[logging.StreamHandler(sys.stdout )] , ) lowerCamelCase_ = training_args.get_process_log_level() logger.setLevel(lowercase ) datasets.utils.logging.set_verbosity(lowercase ) transformers.utils.logging.set_verbosity(lowercase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + f"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(f"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. lowerCamelCase_ = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: lowerCamelCase_ = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. """ 'Use --overwrite_output_dir to overcome.' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ 'the `--output_dir` or add `--overwrite_output_dir` to train from scratch.' ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON training and evaluation files (see below) # or specify a GLUE benchmark task (the dataset will be downloaded automatically from the datasets Hub). # # For JSON files, this script will use the `question` column for the input question and `table` column for the corresponding table. # # If the CSVs/JSONs contain only one non-label column, the script does single sentence classification on this # single column. You can easily tweak this behavior (see below) # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. lowerCamelCase_ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) else: # Loading a dataset from your local files. # CSV/JSON training and evaluation files are needed. lowerCamelCase_ = {'train': data_args.train_file, 'validation': data_args.validation_file} # Get the test dataset: you can provide your own CSV/JSON test file (see below) # when you use `do_predict` without specifying a GLUE benchmark task. if training_args.do_predict: if data_args.test_file is not None: lowerCamelCase_ = data_args.train_file.split('.' )[-1] lowerCamelCase_ = data_args.test_file.split('.' )[-1] assert ( test_extension == train_extension ), "`test_file` should have the same extension (csv or json) as `train_file`." lowerCamelCase_ = data_args.test_file else: raise ValueError('Need either a GLUE task or a test file for `do_predict`.' ) for key in data_files.keys(): logger.info(f"""load a local file for {key}: {data_files[key]}""" ) if data_args.train_file.endswith('.csv' ): # Loading a dataset from local csv files lowerCamelCase_ = load_dataset('csv' , data_files=lowercase , cache_dir=model_args.cache_dir ) else: # Loading a dataset from local json files lowerCamelCase_ = load_dataset('json' , data_files=lowercase , cache_dir=model_args.cache_dir ) # See more about loading any type of standard or custom dataset at # https://huggingface.co/docs/datasets/loading_datasets.html. # Labels lowerCamelCase_ = raw_datasets['train'].features['label'].names lowerCamelCase_ = len(lowercase ) # Load pretrained model and tokenizer # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. lowerCamelCase_ = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=lowercase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # load tapex tokenizer lowerCamelCase_ = TapexTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , add_prefix_space=lowercase , ) lowerCamelCase_ = BartForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path ) , config=lowercase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Padding strategy if data_args.pad_to_max_length: lowerCamelCase_ = 'max_length' else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch lowerCamelCase_ = False # Some models have set the order of the labels to use, so let's make sure we do use it. lowerCamelCase_ = {'Refused': 0, 'Entailed': 1} lowerCamelCase_ = {0: 'Refused', 1: 'Entailed'} if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( f"""The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the""" f"""model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}.""" ) lowerCamelCase_ = min(data_args.max_seq_length , tokenizer.model_max_length ) def preprocess_tabfact_function(lowercase : Optional[Any] ): # Tokenize the texts def _convert_table_text_to_pandas(lowercase : Union[str, Any] ): lowerCamelCase_ = [_table_row.split('#' ) for _table_row in _table_text.strip('\n' ).split('\n' )] lowerCamelCase_ = pd.DataFrame.from_records(_table_content[1:] , columns=_table_content[0] ) return _table_pd lowerCamelCase_ = examples['statement'] lowerCamelCase_ = list(map(_convert_table_text_to_pandas , examples['table_text'] ) ) lowerCamelCase_ = tokenizer(lowercase , lowercase , padding=lowercase , max_length=lowercase , truncation=lowercase ) lowerCamelCase_ = examples['label'] return result with training_args.main_process_first(desc='dataset map pre-processing' ): lowerCamelCase_ = raw_datasets.map( lowercase , batched=lowercase , load_from_cache_file=not data_args.overwrite_cache , desc='Running tokenizer on dataset' , ) if training_args.do_train: if "train" not in raw_datasets: raise ValueError('--do_train requires a train dataset' ) lowerCamelCase_ = raw_datasets['train'] if data_args.max_train_samples is not None: lowerCamelCase_ = train_dataset.select(range(data_args.max_train_samples ) ) if training_args.do_eval: if "validation" not in raw_datasets and "validation_matched" not in raw_datasets: raise ValueError('--do_eval requires a validation dataset' ) lowerCamelCase_ = raw_datasets['validation'] if data_args.max_eval_samples is not None: lowerCamelCase_ = eval_dataset.select(range(data_args.max_eval_samples ) ) if training_args.do_predict or data_args.test_file is not None: if "test" not in raw_datasets and "test_matched" not in raw_datasets: raise ValueError('--do_predict requires a test dataset' ) lowerCamelCase_ = raw_datasets['test'] if data_args.max_predict_samples is not None: lowerCamelCase_ = predict_dataset.select(range(data_args.max_predict_samples ) ) # Log a few random samples from the training set: if training_args.do_train: for index in random.sample(range(len(lowercase ) ) , 3 ): logger.info(f"""Sample {index} of the training set: {train_dataset[index]}.""" ) # You can define your custom compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(lowercase : EvalPrediction ): lowerCamelCase_ = p.predictions[0] if isinstance(p.predictions , lowercase ) else p.predictions lowerCamelCase_ = np.argmax(lowercase , axis=1 ) return {"accuracy": (preds == p.label_ids).astype(np.floataa ).mean().item()} # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: lowerCamelCase_ = default_data_collator elif training_args.fpaa: lowerCamelCase_ = DataCollatorWithPadding(lowercase , pad_to_multiple_of=8 ) else: lowerCamelCase_ = None # Initialize our Trainer lowerCamelCase_ = Trainer( model=lowercase , args=lowercase , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=lowercase , tokenizer=lowercase , data_collator=lowercase , ) # Training if training_args.do_train: lowerCamelCase_ = None if training_args.resume_from_checkpoint is not None: lowerCamelCase_ = training_args.resume_from_checkpoint elif last_checkpoint is not None: lowerCamelCase_ = last_checkpoint lowerCamelCase_ = trainer.train(resume_from_checkpoint=lowercase ) lowerCamelCase_ = train_result.metrics lowerCamelCase_ = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(lowercase ) ) lowerCamelCase_ = min(lowercase , len(lowercase ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics('train' , lowercase ) trainer.save_metrics('train' , lowercase ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info('*** Evaluate ***' ) lowerCamelCase_ = trainer.evaluate(eval_dataset=lowercase ) lowerCamelCase_ = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(lowercase ) lowerCamelCase_ = min(lowercase , len(lowercase ) ) trainer.log_metrics('eval' , lowercase ) trainer.save_metrics('eval' , lowercase ) if training_args.do_predict: logger.info('*** Predict ***' ) # Removing the `label` columns because it contains -1 and Trainer won't like that. lowerCamelCase_ = predict_dataset.remove_columns('label' ) lowerCamelCase_ = trainer.predict(lowercase , metric_key_prefix='predict' ).predictions lowerCamelCase_ = np.argmax(lowercase , axis=1 ) lowerCamelCase_ = os.path.join(training_args.output_dir , 'predict_results_tabfact.txt' ) if trainer.is_world_process_zero(): with open(lowercase , 'w' ) as writer: logger.info('***** Predict Results *****' ) writer.write('index\tprediction\n' ) for index, item in enumerate(lowercase ): lowerCamelCase_ = label_list[item] writer.write(f"""{index}\t{item}\n""" ) lowerCamelCase_ = {'finetuned_from': model_args.model_name_or_path, 'tasks': 'text-classification'} if training_args.push_to_hub: trainer.push_to_hub(**lowercase ) else: trainer.create_model_card(**lowercase ) def _SCREAMING_SNAKE_CASE ( lowercase : Any ): '''simple docstring''' main() if __name__ == "__main__": main()
70
'''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 MobileViTImageProcessor class lowerCAmelCase ( unittest.TestCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=18 , __SCREAMING_SNAKE_CASE=30 , __SCREAMING_SNAKE_CASE=400 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , ) -> Tuple: '''simple docstring''' __snake_case = size if size is not None else {'''shortest_edge''': 20} __snake_case = crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} __snake_case = parent __snake_case = batch_size __snake_case = num_channels __snake_case = image_size __snake_case = min_resolution __snake_case = max_resolution __snake_case = do_resize __snake_case = size __snake_case = do_center_crop __snake_case = crop_size __snake_case = do_flip_channel_order def lowerCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_flip_channel_order": self.do_flip_channel_order, } @require_torch @require_vision class lowerCAmelCase ( __lowerCAmelCase , unittest.TestCase): __lowercase : Union[str, Any] = MobileViTImageProcessor if is_vision_available() else None def lowerCAmelCase ( self ) -> str: '''simple docstring''' __snake_case = MobileViTImageProcessingTester(self ) @property def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def lowerCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_resize''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''size''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_center_crop''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''center_crop''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_flip_channel_order''' ) ) def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 20} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) __snake_case = 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 lowerCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' pass def lowerCAmelCase ( self ) -> str: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __snake_case = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input __snake_case = 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 __snake_case = image_processing(__SCREAMING_SNAKE_CASE , 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 lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __snake_case = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , numpify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) # Test not batched input __snake_case = 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 __snake_case = image_processing(__SCREAMING_SNAKE_CASE , 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 lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __snake_case = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , torchify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) # Test not batched input __snake_case = 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 __snake_case = image_processing(__SCREAMING_SNAKE_CASE , 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'''], ) , )
24
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 _snake_case (unittest.TestCase): def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = tempfile.mkdtemp() # fmt: off UpperCAmelCase_ : List[str] = ["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 UpperCAmelCase_ : List[str] = dict(zip(_snake_case ,range(len(_snake_case ) ) ) ) UpperCAmelCase_ : List[Any] = ["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""] UpperCAmelCase_ : Dict = {"unk_token": "<unk>"} UpperCAmelCase_ : Optional[int] = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["vocab_file"] ) UpperCAmelCase_ : str = 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(_snake_case ) + "\n" ) with open(self.merges_file ,"w" ,encoding="utf-8" ) as fp: fp.write("\n".join(_snake_case ) ) UpperCAmelCase_ : Optional[Any] = { "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], } UpperCAmelCase_ : str = os.path.join(self.tmpdirname ,_snake_case ) with open(self.image_processor_file ,"w" ,encoding="utf-8" ) as fp: json.dump(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPTokenizer.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPTokenizerFast.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ,**_snake_case ): return CLIPImageProcessor.from_pretrained(self.tmpdirname ,**_snake_case ) def UpperCamelCase__ ( self ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = [np.random.randint(2_55 ,size=(3, 30, 4_00) ,dtype=np.uinta )] UpperCAmelCase_ : Union[str, Any] = [Image.fromarray(np.moveaxis(_snake_case ,0 ,-1 ) ) for x in image_inputs] return image_inputs def UpperCamelCase__ ( self ): UpperCAmelCase_ : Tuple = self.get_tokenizer() UpperCAmelCase_ : str = self.get_rust_tokenizer() UpperCAmelCase_ : List[str] = self.get_image_processor() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) processor_slow.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : int = CLIPProcessor.from_pretrained(self.tmpdirname ,use_fast=_snake_case ) UpperCAmelCase_ : str = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) processor_fast.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : str = 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 ,_snake_case ) self.assertIsInstance(processor_fast.tokenizer ,_snake_case ) 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 ,_snake_case ) self.assertIsInstance(processor_fast.image_processor ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = CLIPProcessor(tokenizer=self.get_tokenizer() ,image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer(bos_token="(BOS)" ,eos_token="(EOS)" ) UpperCAmelCase_ : Tuple = self.get_image_processor(do_normalize=_snake_case ,padding_value=1.0 ) UpperCAmelCase_ : int = CLIPProcessor.from_pretrained( self.tmpdirname ,bos_token="(BOS)" ,eos_token="(EOS)" ,do_normalize=_snake_case ,padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() ,tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer ,_snake_case ) self.assertEqual(processor.image_processor.to_json_string() ,image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : List[str] = self.get_image_processor() UpperCAmelCase_ : Dict = self.get_tokenizer() UpperCAmelCase_ : Dict = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Any = self.prepare_image_inputs() UpperCAmelCase_ : Optional[int] = image_processor(_snake_case ,return_tensors="np" ) UpperCAmelCase_ : Any = processor(images=_snake_case ,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 UpperCamelCase__ ( self ): UpperCAmelCase_ : Optional[Any] = self.get_image_processor() UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase_ : Optional[int] = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Tuple = "lower newer" UpperCAmelCase_ : Any = processor(text=_snake_case ) UpperCAmelCase_ : List[Any] = tokenizer(_snake_case ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] ,encoded_processor[key] ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = self.get_image_processor() UpperCAmelCase_ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Any = "lower newer" UpperCAmelCase_ : List[str] = self.prepare_image_inputs() UpperCAmelCase_ : str = processor(text=_snake_case ,images=_snake_case ) self.assertListEqual(list(inputs.keys() ) ,["input_ids", "attention_mask", "pixel_values"] ) # test if it raises when no input is passed with pytest.raises(_snake_case ): processor() def UpperCamelCase__ ( self ): UpperCAmelCase_ : str = self.get_image_processor() UpperCAmelCase_ : Dict = self.get_tokenizer() UpperCAmelCase_ : Optional[int] = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : List[Any] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] UpperCAmelCase_ : int = processor.batch_decode(_snake_case ) UpperCAmelCase_ : int = tokenizer.batch_decode(_snake_case ) self.assertListEqual(_snake_case ,_snake_case ) def UpperCamelCase__ ( self ): UpperCAmelCase_ : Dict = self.get_image_processor() UpperCAmelCase_ : int = self.get_tokenizer() UpperCAmelCase_ : Tuple = CLIPProcessor(tokenizer=_snake_case ,image_processor=_snake_case ) UpperCAmelCase_ : Optional[int] = "lower newer" UpperCAmelCase_ : Any = self.prepare_image_inputs() UpperCAmelCase_ : Dict = processor(text=_snake_case ,images=_snake_case ) self.assertListEqual(list(inputs.keys() ) ,processor.model_input_names )
71
'''simple docstring''' from typing import Optional import pyspark from .. import Features, NamedSplit from ..download import DownloadMode from ..packaged_modules.spark.spark import Spark from .abc import AbstractDatasetReader class lowerCAmelCase ( __lowerCAmelCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = "arrow" , **__SCREAMING_SNAKE_CASE , ) -> Union[str, Any]: '''simple docstring''' super().__init__( split=__SCREAMING_SNAKE_CASE , features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE , keep_in_memory=__SCREAMING_SNAKE_CASE , streaming=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) __snake_case = load_from_cache_file __snake_case = file_format __snake_case = Spark( df=__SCREAMING_SNAKE_CASE , features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE , working_dir=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) def lowerCAmelCase ( self ) -> str: '''simple docstring''' if self.streaming: return self.builder.as_streaming_dataset(split=self.split ) __snake_case = None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD self.builder.download_and_prepare( download_mode=__SCREAMING_SNAKE_CASE , file_format=self._file_format , ) return self.builder.as_dataset(split=self.split )
24
0
'''simple docstring''' # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer from .base import PipelineTool class __magic_name__ ( __SCREAMING_SNAKE_CASE ): UpperCamelCase__ = 'philschmid/bart-large-cnn-samsum' UpperCamelCase__ = ( 'This is a tool that summarizes an English text. It takes an input `text` containing the text to summarize, ' 'and returns a summary of the text.' ) UpperCamelCase__ = 'summarizer' UpperCamelCase__ = AutoTokenizer UpperCamelCase__ = AutoModelForSeqaSeqLM UpperCamelCase__ = ['text'] UpperCamelCase__ = ['text'] def _A( self , snake_case_ ): return self.pre_processor(snake_case_ , return_tensors='''pt''' , truncation=snake_case_ ) def _A( self , snake_case_ ): return self.model.generate(**snake_case_ )[0] def _A( self , snake_case_ ): return self.pre_processor.decode(snake_case_ , skip_special_tokens=snake_case_ , clean_up_tokenization_spaces=snake_case_ )
72
'''simple docstring''' import json from typing import Dict, List, Optional, Tuple, Union from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_led import LEDTokenizer UpperCAmelCase_ : Any = logging.get_logger(__name__) UpperCAmelCase_ : str = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} UpperCAmelCase_ : str = { '''vocab_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json''', }, '''merges_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt''', }, '''tokenizer_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json''', }, } UpperCAmelCase_ : Union[str, Any] = { '''allenai/led-base-16384''': 1_6_3_8_4, } class lowerCAmelCase ( __lowerCAmelCase): __lowercase : Dict = VOCAB_FILES_NAMES __lowercase : Dict = PRETRAINED_VOCAB_FILES_MAP __lowercase : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Union[str, Any] = LEDTokenizer __lowercase : int = ['''input_ids''', '''attention_mask'''] def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="replace" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="<mask>" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , **__SCREAMING_SNAKE_CASE , ) -> List[Any]: '''simple docstring''' super().__init__( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , errors=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) __snake_case = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: __snake_case = getattr(__SCREAMING_SNAKE_CASE , pre_tok_state.pop('''type''' ) ) __snake_case = add_prefix_space __snake_case = pre_tok_class(**__SCREAMING_SNAKE_CASE ) __snake_case = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` __snake_case = '''post_processor''' __snake_case = getattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if tokenizer_component_instance: __snake_case = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: __snake_case = tuple(state['''sep'''] ) if "cls" in state: __snake_case = tuple(state['''cls'''] ) __snake_case = False if state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: __snake_case = add_prefix_space __snake_case = True if state.get('''trim_offsets''' , __SCREAMING_SNAKE_CASE ) != trim_offsets: __snake_case = trim_offsets __snake_case = True if changes_to_apply: __snake_case = getattr(__SCREAMING_SNAKE_CASE , state.pop('''type''' ) ) __snake_case = component_class(**__SCREAMING_SNAKE_CASE ) setattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @property # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED def lowerCAmelCase ( self ) -> str: '''simple docstring''' if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' __snake_case = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else value __snake_case = value def lowerCAmelCase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> BatchEncoding: '''simple docstring''' __snake_case = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' '''to use it with pretokenized inputs.''' ) return super()._batch_encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> BatchEncoding: '''simple docstring''' __snake_case = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' '''to use it with pretokenized inputs.''' ) return super()._encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) -> Tuple[str]: '''simple docstring''' __snake_case = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) -> List[Any]: '''simple docstring''' __snake_case = [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 , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) -> List[int]: '''simple docstring''' __snake_case = [self.sep_token_id] __snake_case = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , ) -> dict: '''simple docstring''' __snake_case = super()._pad( encoded_inputs=__SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , padding_strategy=__SCREAMING_SNAKE_CASE , pad_to_multiple_of=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , ) # Load from model defaults if return_attention_mask is None: __snake_case = '''attention_mask''' in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: __snake_case = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. __snake_case = len(encoded_inputs['''global_attention_mask'''] ) != len(__SCREAMING_SNAKE_CASE ) if needs_to_be_padded: __snake_case = len(__SCREAMING_SNAKE_CASE ) - len(encoded_inputs['''global_attention_mask'''] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` __snake_case = ( encoded_inputs['''global_attention_mask'''] + [-1] * difference ) elif self.padding_side == "left": __snake_case = [-1] * difference + encoded_inputs[ '''global_attention_mask''' ] else: raise ValueError('''Invalid padding strategy:''' + str(self.padding_side ) ) return encoded_inputs
24
0
import json import logging import math import os import sys from dataclasses import dataclass, field from typing import Optional from datasets import Dataset, load_dataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_FOR_MASKED_LM_MAPPING, AutoConfig, AutoModelForMaskedLM, AutoTokenizer, DataCollatorForWholeWordMask, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process a_ : str = logging.getLogger(__name__) a_ : Dict = list(MODEL_FOR_MASKED_LM_MAPPING.keys()) a_ : Tuple = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class _snake_case : _lowercase : Optional[str] = field( default=A__ , metadata={ '''help''': ( '''The model checkpoint for weights initialization.Don\'t set if you want to train a model from scratch.''' ) } , ) _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''If training from scratch, pass a model type from the list: ''' + ''', '''.join(A__ )} , ) _lowercase : Optional[str] = field( default=A__ , metadata={ '''help''': ( '''Override some existing default config settings when a model is trained from scratch. Example: ''' '''n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index''' ) } , ) _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} ) _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''} , ) _lowercase : bool = field( default=A__ , metadata={'''help''': '''Whether to use one of the fast tokenizer (backed by the tokenizers library) or not.'''} , ) _lowercase : str = field( default='''main''' , metadata={'''help''': '''The specific model version to use (can be a branch name, tag name or commit id).'''} , ) _lowercase : bool = field( default=A__ , metadata={ '''help''': ( '''Will use the token generated when running `huggingface-cli login` (necessary to use this script ''' '''with private models).''' ) } , ) def SCREAMING_SNAKE_CASE__ ( self) -> List[str]: if self.config_overrides is not None and (self.config_name is not None or self.model_name_or_path is not None): raise ValueError( '--config_overrides can\'t be used in combination with --config_name or --model_name_or_path') @dataclass class _snake_case : _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''The name of the dataset to use (via the datasets library).'''} ) _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''The configuration name of the dataset to use (via the datasets library).'''} ) _lowercase : Optional[str] = field(default=A__ , metadata={'''help''': '''The input training data file (a text file).'''} ) _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''An optional input evaluation data file to evaluate the perplexity on (a text file).'''} , ) _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''An optional input train ref data file for whole word masking in Chinese.'''} , ) _lowercase : Optional[str] = field( default=A__ , metadata={'''help''': '''An optional input validation ref data file for whole word masking in Chinese.'''} , ) _lowercase : bool = field( default=A__ , metadata={'''help''': '''Overwrite the cached training and evaluation sets'''} ) _lowercase : Optional[int] = field( default=5 , metadata={ '''help''': '''The percentage of the train set used as validation set in case there\'s no validation split''' } , ) _lowercase : Optional[int] = field( default=A__ , metadata={ '''help''': ( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated. Default to the max input length of the model.''' ) } , ) _lowercase : Optional[int] = field( default=A__ , metadata={'''help''': '''The number of processes to use for the preprocessing.'''} , ) _lowercase : float = field( default=0.15 , metadata={'''help''': '''Ratio of tokens to mask for masked language modeling loss'''} ) _lowercase : bool = field( default=A__ , metadata={ '''help''': ( '''Whether to pad all samples to `max_seq_length`. ''' '''If False, will pad the samples dynamically when batching to the maximum length in the batch.''' ) } , ) def SCREAMING_SNAKE_CASE__ ( self) -> Optional[int]: if self.train_file is not None: SCREAMING_SNAKE_CASE = self.train_file.split('.')[-1] assert extension in ["csv", "json", "txt"], "`train_file` should be a csv, a json or a txt file." if self.validation_file is not None: SCREAMING_SNAKE_CASE = self.validation_file.split('.')[-1] assert extension in ["csv", "json", "txt"], "`validation_file` should be a csv, a json or a txt file." def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase): with open(_UpperCAmelCase , 'r' , encoding='utf-8') as f: SCREAMING_SNAKE_CASE = [json.loads(_UpperCAmelCase) for line in f.read().splitlines() if (len(_UpperCAmelCase) > 0 and not line.isspace())] assert len(_UpperCAmelCase) == len(_UpperCAmelCase) SCREAMING_SNAKE_CASE = {c: dataset[c] for c in dataset.column_names} SCREAMING_SNAKE_CASE = refs return Dataset.from_dict(_UpperCAmelCase) def lowerCamelCase__ (): # 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. SCREAMING_SNAKE_CASE = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)) if len(sys.argv) == 2 and sys.argv[1].endswith('.json'): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1])) else: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = parser.parse_args_into_dataclasses() # Detecting last checkpoint. SCREAMING_SNAKE_CASE = None if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir: SCREAMING_SNAKE_CASE = get_last_checkpoint(training_args.output_dir) if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0: raise ValueError( F'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' 'Use --overwrite_output_dir to overcome.') elif last_checkpoint is not None: logger.info( F'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' 'the `--output_dir` or add `--overwrite_output_dir` to train from scratch.') # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , handlers=[logging.StreamHandler(sys.stdout)] , ) logger.setLevel(logging.INFO if is_main_process(training_args.local_rank) else logging.WARN) # Log on each process the small summary: logger.warning( F'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}''' + F'''distributed training: {bool(training_args.local_rank != -1)}, 16-bits training: {training_args.fpaa}''') # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('Training/evaluation parameters %s' , _UpperCAmelCase) # Set seed before initializing model. set_seed(training_args.seed) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. SCREAMING_SNAKE_CASE = load_dataset(data_args.dataset_name , data_args.dataset_config_name) if "validation" not in datasets.keys(): SCREAMING_SNAKE_CASE = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F'''train[:{data_args.validation_split_percentage}%]''' , ) SCREAMING_SNAKE_CASE = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F'''train[{data_args.validation_split_percentage}%:]''' , ) else: SCREAMING_SNAKE_CASE = {} if data_args.train_file is not None: SCREAMING_SNAKE_CASE = data_args.train_file if data_args.validation_file is not None: SCREAMING_SNAKE_CASE = data_args.validation_file SCREAMING_SNAKE_CASE = data_args.train_file.split('.')[-1] if extension == "txt": SCREAMING_SNAKE_CASE = 'text' SCREAMING_SNAKE_CASE = load_dataset(_UpperCAmelCase , data_files=_UpperCAmelCase) # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. SCREAMING_SNAKE_CASE = { 'cache_dir': model_args.cache_dir, 'revision': model_args.model_revision, 'use_auth_token': True if model_args.use_auth_token else None, } if model_args.config_name: SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained(model_args.config_name , **_UpperCAmelCase) elif model_args.model_name_or_path: SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained(model_args.model_name_or_path , **_UpperCAmelCase) else: SCREAMING_SNAKE_CASE = CONFIG_MAPPING[model_args.model_type]() logger.warning('You are instantiating a new config instance from scratch.') if model_args.config_overrides is not None: logger.info(F'''Overriding config: {model_args.config_overrides}''') config.update_from_string(model_args.config_overrides) logger.info(F'''New config: {config}''') SCREAMING_SNAKE_CASE = { 'cache_dir': model_args.cache_dir, 'use_fast': model_args.use_fast_tokenizer, 'revision': model_args.model_revision, 'use_auth_token': True if model_args.use_auth_token else None, } if model_args.tokenizer_name: SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained(model_args.tokenizer_name , **_UpperCAmelCase) elif model_args.model_name_or_path: SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained(model_args.model_name_or_path , **_UpperCAmelCase) else: raise ValueError( 'You are instantiating a new tokenizer from scratch. This is not supported by this script.' 'You can do it from another script, save it, and load it from here, using --tokenizer_name.') if model_args.model_name_or_path: SCREAMING_SNAKE_CASE = AutoModelForMaskedLM.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path) , config=_UpperCAmelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info('Training new model from scratch') SCREAMING_SNAKE_CASE = AutoModelForMaskedLM.from_config(_UpperCAmelCase) model.resize_token_embeddings(len(_UpperCAmelCase)) # Preprocessing the datasets. # First we tokenize all the texts. if training_args.do_train: SCREAMING_SNAKE_CASE = datasets['train'].column_names else: SCREAMING_SNAKE_CASE = datasets['validation'].column_names SCREAMING_SNAKE_CASE = 'text' if 'text' in column_names else column_names[0] SCREAMING_SNAKE_CASE = 'max_length' if data_args.pad_to_max_length else False def tokenize_function(_UpperCAmelCase): # Remove empty lines SCREAMING_SNAKE_CASE = [line for line in examples['text'] if len(_UpperCAmelCase) > 0 and not line.isspace()] return tokenizer(examples['text'] , padding=_UpperCAmelCase , truncation=_UpperCAmelCase , max_length=data_args.max_seq_length) SCREAMING_SNAKE_CASE = datasets.map( _UpperCAmelCase , batched=_UpperCAmelCase , num_proc=data_args.preprocessing_num_workers , remove_columns=[text_column_name] , load_from_cache_file=not data_args.overwrite_cache , ) # Add the chinese references if provided if data_args.train_ref_file is not None: SCREAMING_SNAKE_CASE = add_chinese_references(tokenized_datasets['train'] , data_args.train_ref_file) if data_args.validation_ref_file is not None: SCREAMING_SNAKE_CASE = add_chinese_references( tokenized_datasets['validation'] , data_args.validation_ref_file) # If we have ref files, need to avoid it removed by trainer SCREAMING_SNAKE_CASE = data_args.train_ref_file or data_args.validation_ref_file if has_ref: SCREAMING_SNAKE_CASE = False # Data collator # This one will take care of randomly masking the tokens. SCREAMING_SNAKE_CASE = DataCollatorForWholeWordMask(tokenizer=_UpperCAmelCase , mlm_probability=data_args.mlm_probability) # Initialize our Trainer SCREAMING_SNAKE_CASE = Trainer( model=_UpperCAmelCase , args=_UpperCAmelCase , train_dataset=tokenized_datasets['train'] if training_args.do_train else None , eval_dataset=tokenized_datasets['validation'] if training_args.do_eval else None , tokenizer=_UpperCAmelCase , data_collator=_UpperCAmelCase , ) # Training if training_args.do_train: if last_checkpoint is not None: SCREAMING_SNAKE_CASE = last_checkpoint elif model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path): SCREAMING_SNAKE_CASE = model_args.model_name_or_path else: SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = trainer.train(resume_from_checkpoint=_UpperCAmelCase) trainer.save_model() # Saves the tokenizer too for easy upload SCREAMING_SNAKE_CASE = os.path.join(training_args.output_dir , 'train_results.txt') if trainer.is_world_process_zero(): with open(_UpperCAmelCase , 'w') as writer: logger.info('***** Train results *****') for key, value in sorted(train_result.metrics.items()): logger.info(F''' {key} = {value}''') writer.write(F'''{key} = {value}\n''') # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , 'trainer_state.json')) # Evaluation SCREAMING_SNAKE_CASE = {} if training_args.do_eval: logger.info('*** Evaluate ***') SCREAMING_SNAKE_CASE = trainer.evaluate() SCREAMING_SNAKE_CASE = math.exp(eval_output['eval_loss']) SCREAMING_SNAKE_CASE = perplexity SCREAMING_SNAKE_CASE = os.path.join(training_args.output_dir , 'eval_results_mlm_wwm.txt') if trainer.is_world_process_zero(): with open(_UpperCAmelCase , 'w') as writer: logger.info('***** Eval results *****') for key, value in sorted(results.items()): logger.info(F''' {key} = {value}''') writer.write(F'''{key} = {value}\n''') return results def lowerCamelCase__ (_UpperCAmelCase): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
73
'''simple docstring''' from collections import deque def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> Optional[int]: '''simple docstring''' __snake_case = len(_lowerCamelCase ) __snake_case = deque() __snake_case = [False for _ in range(_lowerCamelCase )] __snake_case = [-1 for _ in range(_lowerCamelCase )] __snake_case = index_of[:] def strong_connect(_lowerCamelCase : Any , _lowerCamelCase : Dict , _lowerCamelCase : List[str] ): __snake_case = index # the number when this node is seen __snake_case = index # lowest rank node reachable from here index += 1 stack.append(_lowerCamelCase ) __snake_case = True for w in g[v]: if index_of[w] == -1: __snake_case = strong_connect(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) __snake_case = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) elif on_stack[w]: __snake_case = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) if lowlink_of[v] == index_of[v]: __snake_case = [] __snake_case = stack.pop() __snake_case = False component.append(_lowerCamelCase ) while w != v: __snake_case = stack.pop() __snake_case = False component.append(_lowerCamelCase ) components.append(_lowerCamelCase ) return index __snake_case = [] for v in range(_lowerCamelCase ): if index_of[v] == -1: strong_connect(_lowerCamelCase , 0 , _lowerCamelCase ) return components def _UpperCamelCase (_lowerCamelCase : Tuple , _lowerCamelCase : Optional[Any] )-> Dict: '''simple docstring''' __snake_case = [[] for _ in range(_lowerCamelCase )] for u, v in edges: g[u].append(_lowerCamelCase ) return g if __name__ == "__main__": # Test UpperCAmelCase_ : List[str] = 7 UpperCAmelCase_ : int = [0, 0, 1, 2, 3, 3, 4, 4, 6] UpperCAmelCase_ : Dict = [1, 3, 2, 0, 1, 4, 5, 6, 5] UpperCAmelCase_ : List[str] = [(u, v) for u, v in zip(source, target)] UpperCAmelCase_ : Tuple = create_graph(n_vertices, edges) assert [[5], [6], [4], [3, 2, 1, 0]] == tarjan(g)
24
0
import os from glob import glob import imageio import torch import torchvision import wandb from img_processing import custom_to_pil, loop_post_process, preprocess, preprocess_vqgan from loaders import load_vqgan from PIL import Image from torch import nn from transformers import CLIPModel, CLIPTokenizerFast from utils import get_device, get_timestamp, show_pil class __UpperCamelCase : """simple docstring""" def __init__( self : int , _A : str = "cpu" , _A : str = "openai/clip-vit-large-patch14" ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = device __SCREAMING_SNAKE_CASE : int = CLIPTokenizerFast.from_pretrained(_A ) __SCREAMING_SNAKE_CASE : int = [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73] __SCREAMING_SNAKE_CASE : Optional[int] = [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11] __SCREAMING_SNAKE_CASE : Optional[int] = torchvision.transforms.Normalize(self.image_mean , self.image_std ) __SCREAMING_SNAKE_CASE : List[str] = torchvision.transforms.Resize(224 ) __SCREAMING_SNAKE_CASE : int = torchvision.transforms.CenterCrop(224 ) def UpperCAmelCase__ ( self : Any , _A : List[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = self.resize(_A ) __SCREAMING_SNAKE_CASE : Any = self.center_crop(_A ) __SCREAMING_SNAKE_CASE : List[Any] = self.normalize(_A ) return images def __call__( self : int , _A : Dict=None , _A : str=None , **_A : Dict ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = self.tokenizer(text=_A , **_A ) __SCREAMING_SNAKE_CASE : List[Any] = self.preprocess_img(_A ) __SCREAMING_SNAKE_CASE : str = {key: value.to(self.device ) for (key, value) in encoding.items()} return encoding class __UpperCamelCase ( nn.Module ): """simple docstring""" def __init__( self : Any , _A : Any=10 , _A : Tuple=0.01 , _A : int=None , _A : Any=None , _A : str=None , _A : str=None , _A : Union[str, Any]=None , _A : int=None , _A : str=False , _A : int=True , _A : str="image" , _A : Union[str, Any]=True , _A : Tuple=False , _A : str=False , _A : Dict=False , ): """simple docstring""" super().__init__() __SCREAMING_SNAKE_CASE : List[str] = None __SCREAMING_SNAKE_CASE : Optional[Any] = device if device else get_device() if vqgan: __SCREAMING_SNAKE_CASE : Any = vqgan else: __SCREAMING_SNAKE_CASE : Optional[Any] = load_vqgan(self.device , conf_path=_A , ckpt_path=_A ) self.vqgan.eval() if clip: __SCREAMING_SNAKE_CASE : int = clip else: __SCREAMING_SNAKE_CASE : Optional[Any] = CLIPModel.from_pretrained('''openai/clip-vit-base-patch32''' ) self.clip.to(self.device ) __SCREAMING_SNAKE_CASE : Any = ProcessorGradientFlow(device=self.device ) __SCREAMING_SNAKE_CASE : Any = iterations __SCREAMING_SNAKE_CASE : List[str] = lr __SCREAMING_SNAKE_CASE : List[str] = log __SCREAMING_SNAKE_CASE : List[str] = make_grid __SCREAMING_SNAKE_CASE : Optional[Any] = return_val __SCREAMING_SNAKE_CASE : Optional[Any] = quantize __SCREAMING_SNAKE_CASE : Any = self.vqgan.decoder.z_shape def UpperCAmelCase__ ( self : List[str] , _A : int=None , _A : str=None , _A : str=5 , _A : str=True ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = [] if output_path is None: __SCREAMING_SNAKE_CASE : Dict = '''./animation.gif''' if input_path is None: __SCREAMING_SNAKE_CASE : Optional[int] = self.save_path __SCREAMING_SNAKE_CASE : List[Any] = sorted(glob(input_path + '''/*''' ) ) if not len(_A ): raise ValueError( '''No images found in save path, aborting (did you pass save_intermediate=True to the generate''' ''' function?)''' ) if len(_A ) == 1: print('''Only one image found in save path, (did you pass save_intermediate=True to the generate function?)''' ) __SCREAMING_SNAKE_CASE : Tuple = total_duration / len(_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = [frame_duration] * len(_A ) if extend_frames: __SCREAMING_SNAKE_CASE : List[Any] = 1.5 __SCREAMING_SNAKE_CASE : Optional[int] = 3 for file_name in paths: if file_name.endswith('''.png''' ): images.append(imageio.imread(_A ) ) imageio.mimsave(_A , _A , duration=_A ) print(F'''gif saved to {output_path}''' ) def UpperCAmelCase__ ( self : List[Any] , _A : Dict=None , _A : Tuple=None ): """simple docstring""" if not (path or img): raise ValueError('''Input either path or tensor''' ) if img is not None: raise NotImplementedError __SCREAMING_SNAKE_CASE : Union[str, Any] = preprocess(Image.open(_A ) , target_image_size=256 ).to(self.device ) __SCREAMING_SNAKE_CASE : Tuple = preprocess_vqgan(_A ) __SCREAMING_SNAKE_CASE, *__SCREAMING_SNAKE_CASE : List[str] = self.vqgan.encode(_A ) return z def UpperCAmelCase__ ( self : Optional[Any] , _A : List[str] ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = self.latent.detach().requires_grad_() __SCREAMING_SNAKE_CASE : Any = base_latent + transform_vector if self.quantize: __SCREAMING_SNAKE_CASE, *__SCREAMING_SNAKE_CASE : List[Any] = self.vqgan.quantize(_A ) else: __SCREAMING_SNAKE_CASE : Tuple = trans_latent return self.vqgan.decode(_A ) def UpperCAmelCase__ ( self : List[Any] , _A : Tuple , _A : Dict , _A : Optional[Any]=None ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = self.clip_preprocessor(text=_A , images=_A , return_tensors='''pt''' , padding=_A ) __SCREAMING_SNAKE_CASE : str = self.clip(**_A ) __SCREAMING_SNAKE_CASE : List[str] = clip_outputs.logits_per_image if weights is not None: __SCREAMING_SNAKE_CASE : Optional[Any] = similarity_logits * weights return similarity_logits.sum() def UpperCAmelCase__ ( self : Tuple , _A : List[Any] , _A : List[Any] , _A : Union[str, Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = self._get_clip_similarity(pos_prompts['''prompts'''] , _A , weights=(1 / pos_prompts['''weights''']) ) if neg_prompts: __SCREAMING_SNAKE_CASE : Tuple = self._get_clip_similarity(neg_prompts['''prompts'''] , _A , weights=neg_prompts['''weights'''] ) else: __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor([1] , device=self.device ) __SCREAMING_SNAKE_CASE : Dict = -torch.log(_A ) + torch.log(_A ) return loss def UpperCAmelCase__ ( self : int , _A : str , _A : List[Any] , _A : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = torch.randn_like(self.latent , requires_grad=_A , device=self.device ) __SCREAMING_SNAKE_CASE : List[Any] = torch.optim.Adam([vector] , lr=self.lr ) for i in range(self.iterations ): optim.zero_grad() __SCREAMING_SNAKE_CASE : List[Any] = self._add_vector(_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = loop_post_process(_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = self._get_CLIP_loss(_A , _A , _A ) print('''CLIP loss''' , _A ) if self.log: wandb.log({'''CLIP Loss''': clip_loss} ) clip_loss.backward(retain_graph=_A ) optim.step() if self.return_val == "image": yield custom_to_pil(transformed_img[0] ) else: yield vector def UpperCAmelCase__ ( self : str , _A : List[str] , _A : int , _A : List[Any] ): """simple docstring""" wandb.init(reinit=_A , project='''face-editor''' ) wandb.config.update({'''Positive Prompts''': positive_prompts} ) wandb.config.update({'''Negative Prompts''': negative_prompts} ) wandb.config.update({'''lr''': self.lr, '''iterations''': self.iterations} ) if image_path: __SCREAMING_SNAKE_CASE : Dict = Image.open(_A ) __SCREAMING_SNAKE_CASE : Optional[int] = image.resize((256, 256) ) wandb.log('''Original Image''' , wandb.Image(_A ) ) def UpperCAmelCase__ ( self : Union[str, Any] , _A : Optional[int] ): """simple docstring""" if not prompts: return [] __SCREAMING_SNAKE_CASE : Dict = [] __SCREAMING_SNAKE_CASE : Tuple = [] if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : int = [prompt.strip() for prompt in prompts.split('''|''' )] for prompt in prompts: if isinstance(_A , (tuple, list) ): __SCREAMING_SNAKE_CASE : Union[str, Any] = prompt[0] __SCREAMING_SNAKE_CASE : int = float(prompt[1] ) elif ":" in prompt: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : List[Any] = prompt.split(''':''' ) __SCREAMING_SNAKE_CASE : str = float(_A ) else: __SCREAMING_SNAKE_CASE : int = prompt __SCREAMING_SNAKE_CASE : List[Any] = 1.0 processed_prompts.append(_A ) weights.append(_A ) return { "prompts": processed_prompts, "weights": torch.tensor(_A , device=self.device ), } def UpperCAmelCase__ ( self : Optional[int] , _A : List[str] , _A : List[Any]=None , _A : Tuple=None , _A : Dict=True , _A : int=False , _A : List[Any]=True , _A : Tuple=True , _A : int=None , ): """simple docstring""" if image_path: __SCREAMING_SNAKE_CASE : Union[str, Any] = self._get_latent(_A ) else: __SCREAMING_SNAKE_CASE : Optional[Any] = torch.randn(self.latent_dim , device=self.device ) if self.log: self._init_logging(_A , _A , _A ) assert pos_prompts, "You must provide at least one positive prompt." __SCREAMING_SNAKE_CASE : Tuple = self.process_prompts(_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = self.process_prompts(_A ) if save_final and save_path is None: __SCREAMING_SNAKE_CASE : List[str] = os.path.join('''./outputs/''' , '''_'''.join(pos_prompts['''prompts'''] ) ) if not os.path.exists(_A ): os.makedirs(_A ) else: __SCREAMING_SNAKE_CASE : str = save_path + '''_''' + get_timestamp() os.makedirs(_A ) __SCREAMING_SNAKE_CASE : Dict = save_path __SCREAMING_SNAKE_CASE : str = self.vqgan.decode(self.latent )[0] if show_intermediate: print('''Original Image''' ) show_pil(custom_to_pil(_A ) ) __SCREAMING_SNAKE_CASE : str = loop_post_process(_A ) for iter, transformed_img in enumerate(self._optimize_CLIP(_A , _A , _A ) ): if show_intermediate: show_pil(_A ) if save_intermediate: transformed_img.save(os.path.join(self.save_path , F'''iter_{iter:03d}.png''' ) ) if self.log: wandb.log({'''Image''': wandb.Image(_A )} ) if show_final: show_pil(_A ) if save_final: transformed_img.save(os.path.join(self.save_path , F'''iter_{iter:03d}_final.png''' ) )
74
'''simple docstring''' import unittest from transformers import BarthezTokenizer, BarthezTokenizerFast, BatchEncoding from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers @require_sentencepiece @slow # see https://github.com/huggingface/transformers/issues/11457 class lowerCAmelCase ( __lowerCAmelCase , unittest.TestCase): __lowercase : int = BarthezTokenizer __lowercase : Any = BarthezTokenizerFast __lowercase : Dict = True __lowercase : Optional[int] = True def lowerCAmelCase ( self ) -> Dict: '''simple docstring''' super().setUp() __snake_case = BarthezTokenizerFast.from_pretrained('''moussaKam/mbarthez''' ) tokenizer.save_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname , legacy_format=__SCREAMING_SNAKE_CASE ) __snake_case = tokenizer def lowerCAmelCase ( self ) -> Dict: '''simple docstring''' __snake_case = '''<pad>''' __snake_case = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<s>''' ) self.assertEqual(vocab_keys[1] , '''<pad>''' ) self.assertEqual(vocab_keys[-1] , '''<mask>''' ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 10_1122 ) def lowerCAmelCase ( self ) -> Any: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 10_1122 ) @require_torch def lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] __snake_case = [0, 57, 3018, 7_0307, 91, 2] __snake_case = self.tokenizer( __SCREAMING_SNAKE_CASE , max_length=len(__SCREAMING_SNAKE_CASE ) , padding=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertEqual((2, 6) , batch.input_ids.shape ) self.assertEqual((2, 6) , batch.attention_mask.shape ) __snake_case = batch.input_ids.tolist()[0] self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' if not self.test_rust_tokenizer: return __snake_case = self.get_tokenizer() __snake_case = self.get_rust_tokenizer() __snake_case = '''I was born in 92000, and this is falsé.''' __snake_case = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) __snake_case = rust_tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __snake_case = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) __snake_case = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __snake_case = self.get_rust_tokenizer() __snake_case = tokenizer.encode(__SCREAMING_SNAKE_CASE ) __snake_case = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = {'''input_ids''': [[0, 490, 1_4328, 4507, 354, 47, 4_3669, 95, 25, 7_8117, 2_0215, 1_9779, 190, 22, 400, 4, 3_5343, 8_0310, 603, 86, 2_4937, 105, 3_3438, 9_4762, 196, 3_9642, 7, 15, 1_5933, 173, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1_0534, 87, 25, 66, 3358, 196, 5_5289, 8, 8_2961, 81, 2204, 7_5203, 7, 15, 763, 1_2956, 216, 178, 1_4328, 9595, 1377, 6_9693, 7, 448, 7_1021, 196, 1_8106, 1437, 1_3974, 108, 9083, 4, 4_9315, 7, 39, 86, 1326, 2793, 4_6333, 4, 448, 196, 7_4588, 7, 4_9315, 7, 39, 21, 822, 3_8470, 74, 21, 6_6723, 6_2480, 8, 2_2050, 5, 2]], '''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, 0, 0, 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # moussaKam/mbarthez is a french model. So we also use french texts. __snake_case = [ '''Le transformeur est un modèle d\'apprentissage profond introduit en 2017, ''' '''utilisé principalement dans le domaine du traitement automatique des langues (TAL).''', '''À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus ''' '''pour gérer des données séquentielles, telles que le langage naturel, pour des tâches ''' '''telles que la traduction et la synthèse de texte.''', ] self.tokenizer_integration_test_util( expected_encoding=__SCREAMING_SNAKE_CASE , model_name='''moussaKam/mbarthez''' , revision='''c2e4ecbca5e3cd2c37fe1ac285ca4fbdf1366fb6''' , sequences=__SCREAMING_SNAKE_CASE , )
24
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase__ = { '''configuration_megatron_bert''': ['''MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MegatronBertConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ = [ '''MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MegatronBertForCausalLM''', '''MegatronBertForMaskedLM''', '''MegatronBertForMultipleChoice''', '''MegatronBertForNextSentencePrediction''', '''MegatronBertForPreTraining''', '''MegatronBertForQuestionAnswering''', '''MegatronBertForSequenceClassification''', '''MegatronBertForTokenClassification''', '''MegatronBertModel''', '''MegatronBertPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_megatron_bert import MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, MegatronBertConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_megatron_bert import ( MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, MegatronBertForCausalLM, MegatronBertForMaskedLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, MegatronBertModel, MegatronBertPreTrainedModel, ) else: import sys UpperCamelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
75
'''simple docstring''' import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class lowerCAmelCase ( unittest.TestCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __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=32 , __SCREAMING_SNAKE_CASE=5 , __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=4 , ) -> Any: '''simple docstring''' __snake_case = parent __snake_case = batch_size __snake_case = seq_length __snake_case = is_training __snake_case = use_attention_mask __snake_case = use_token_type_ids __snake_case = use_labels __snake_case = vocab_size __snake_case = hidden_size __snake_case = num_hidden_layers __snake_case = num_attention_heads __snake_case = intermediate_size __snake_case = hidden_act __snake_case = hidden_dropout_prob __snake_case = attention_probs_dropout_prob __snake_case = max_position_embeddings __snake_case = type_vocab_size __snake_case = type_sequence_label_size __snake_case = initializer_range __snake_case = num_choices def lowerCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' __snake_case = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __snake_case = None if self.use_attention_mask: __snake_case = random_attention_mask([self.batch_size, self.seq_length] ) __snake_case = None if self.use_token_type_ids: __snake_case = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __snake_case = RoFormerConfig( 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=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = self.prepare_config_and_inputs() __snake_case , __snake_case , __snake_case , __snake_case = config_and_inputs __snake_case = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_flax class lowerCAmelCase ( __lowerCAmelCase , unittest.TestCase): __lowercase : Tuple = True __lowercase : Optional[int] = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = FlaxRoFormerModelTester(self ) @slow def lowerCAmelCase ( self ) -> List[Any]: '''simple docstring''' for model_class_name in self.all_model_classes: __snake_case = model_class_name.from_pretrained('''junnyu/roformer_chinese_small''' , from_pt=__SCREAMING_SNAKE_CASE ) __snake_case = model(np.ones((1, 1) ) ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) @require_flax class lowerCAmelCase ( unittest.TestCase): @slow def lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = FlaxRoFormerForMaskedLM.from_pretrained('''junnyu/roformer_chinese_base''' ) __snake_case = jnp.array([[0, 1, 2, 3, 4, 5]] ) __snake_case = model(__SCREAMING_SNAKE_CASE )[0] __snake_case = 5_0000 __snake_case = (1, 6, vocab_size) self.assertEqual(output.shape , __SCREAMING_SNAKE_CASE ) __snake_case = jnp.array( [[[-0.1_205, -1.0_265, 0.2_922], [-1.5_134, 0.1_974, 0.1_519], [-5.0_135, -3.9_003, -0.8_404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , __SCREAMING_SNAKE_CASE , atol=1E-4 ) )
24
0
"""simple docstring""" from math import log from scipy.constants import Boltzmann, physical_constants a_ = 3_0_0 # TEMPERATURE (unit = K) def __UpperCAmelCase ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ): if donor_conc <= 0: raise ValueError('''Donor concentration should be positive''' ) elif acceptor_conc <= 0: raise ValueError('''Acceptor concentration should be positive''' ) elif intrinsic_conc <= 0: raise ValueError('''Intrinsic concentration should be positive''' ) elif donor_conc <= intrinsic_conc: raise ValueError( '''Donor concentration should be greater than intrinsic concentration''' ) elif acceptor_conc <= intrinsic_conc: raise ValueError( '''Acceptor concentration should be greater than intrinsic concentration''' ) else: return ( Boltzmann * T * log((donor_conc * acceptor_conc) / intrinsic_conc**2 ) / physical_constants["electron volt"][0] ) if __name__ == "__main__": import doctest doctest.testmod()
76
'''simple docstring''' 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 ()-> int: '''simple docstring''' __snake_case = '''https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png''' __snake_case = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ).convert('''RGB''' ) return image def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> List[Any]: '''simple docstring''' __snake_case = [] # fmt: off # vision encoder rename_keys.append(('''visual_encoder.cls_token''', '''vision_model.embeddings.class_embedding''') ) rename_keys.append(('''visual_encoder.pos_embed''', '''vision_model.embeddings.position_embedding''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.weight''', '''vision_model.embeddings.patch_embedding.weight''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.bias''', '''vision_model.embeddings.patch_embedding.bias''') ) rename_keys.append(('''ln_vision.weight''', '''vision_model.post_layernorm.weight''') ) rename_keys.append(('''ln_vision.bias''', '''vision_model.post_layernorm.bias''') ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.weight''', f'''vision_model.encoder.layers.{i}.layer_norm1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.bias''', f'''vision_model.encoder.layers.{i}.layer_norm1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.weight''', f'''vision_model.encoder.layers.{i}.layer_norm2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.bias''', f'''vision_model.encoder.layers.{i}.layer_norm2.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.qkv.weight''', f'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.weight''', f'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',) ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.bias''', f'''vision_model.encoder.layers.{i}.self_attn.projection.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc2.bias''') ) # QFormer rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.weight''', '''qformer.layernorm.weight''') ) rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.bias''', '''qformer.layernorm.bias''') ) # fmt: on return rename_keys def _UpperCamelCase (_lowerCamelCase : Dict , _lowerCamelCase : List[str] , _lowerCamelCase : Optional[int] )-> Tuple: '''simple docstring''' __snake_case = dct.pop(_lowerCamelCase ) __snake_case = val def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : Tuple )-> str: '''simple docstring''' for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases __snake_case = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.q_bias''' ) __snake_case = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict __snake_case = torch.cat((q_bias, torch.zeros_like(_lowerCamelCase , requires_grad=_lowerCamelCase ), v_bias) ) __snake_case = qkv_bias def _UpperCamelCase (_lowerCamelCase : Tuple , _lowerCamelCase : Tuple )-> Dict: '''simple docstring''' __snake_case = 3_64 if '''coco''' in model_name else 2_24 __snake_case = BlipaVisionConfig(image_size=_lowerCamelCase ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: __snake_case = OPTConfig.from_pretrained('''facebook/opt-2.7b''' , eos_token_id=_lowerCamelCase ).to_dict() elif "opt-6.7b" in model_name: __snake_case = OPTConfig.from_pretrained('''facebook/opt-6.7b''' , eos_token_id=_lowerCamelCase ).to_dict() elif "t5-xl" in model_name: __snake_case = TaConfig.from_pretrained('''google/flan-t5-xl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: __snake_case = TaConfig.from_pretrained('''google/flan-t5-xxl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() __snake_case = BlipaConfig(vision_config=_lowerCamelCase , text_config=_lowerCamelCase ) return config, image_size @torch.no_grad() def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : Union[str, Any]=None , _lowerCamelCase : Any=False )-> Dict: '''simple docstring''' __snake_case = ( AutoTokenizer.from_pretrained('''facebook/opt-2.7b''' ) if '''opt''' in model_name else AutoTokenizer.from_pretrained('''google/flan-t5-xl''' ) ) __snake_case = tokenizer('''\n''' , add_special_tokens=_lowerCamelCase ).input_ids[0] __snake_case , __snake_case = get_blipa_config(_lowerCamelCase , eos_token_id=_lowerCamelCase ) __snake_case = BlipaForConditionalGeneration(_lowerCamelCase ).eval() __snake_case = { '''blip2-opt-2.7b''': ('''blip2_opt''', '''pretrain_opt2.7b'''), '''blip2-opt-6.7b''': ('''blip2_opt''', '''pretrain_opt6.7b'''), '''blip2-opt-2.7b-coco''': ('''blip2_opt''', '''caption_coco_opt2.7b'''), '''blip2-opt-6.7b-coco''': ('''blip2_opt''', '''caption_coco_opt6.7b'''), '''blip2-flan-t5-xl''': ('''blip2_t5''', '''pretrain_flant5xl'''), '''blip2-flan-t5-xl-coco''': ('''blip2_t5''', '''caption_coco_flant5xl'''), '''blip2-flan-t5-xxl''': ('''blip2_t5''', '''pretrain_flant5xxl'''), } __snake_case , __snake_case = model_name_to_original[model_name] # load original model print('''Loading original model...''' ) __snake_case = '''cuda''' if torch.cuda.is_available() else '''cpu''' __snake_case , __snake_case , __snake_case = load_model_and_preprocess( name=_lowerCamelCase , model_type=_lowerCamelCase , is_eval=_lowerCamelCase , device=_lowerCamelCase ) original_model.eval() print('''Done!''' ) # update state dict keys __snake_case = original_model.state_dict() __snake_case = create_rename_keys(_lowerCamelCase ) for src, dest in rename_keys: rename_key(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): __snake_case = state_dict.pop(_lowerCamelCase ) if key.startswith('''Qformer.bert''' ): __snake_case = key.replace('''Qformer.bert''' , '''qformer''' ) if "attention.self" in key: __snake_case = key.replace('''self''' , '''attention''' ) if "opt_proj" in key: __snake_case = key.replace('''opt_proj''' , '''language_projection''' ) if "t5_proj" in key: __snake_case = key.replace('''t5_proj''' , '''language_projection''' ) if key.startswith('''opt''' ): __snake_case = key.replace('''opt''' , '''language''' ) if key.startswith('''t5''' ): __snake_case = key.replace('''t5''' , '''language''' ) __snake_case = val # read in qv biases read_in_q_v_bias(_lowerCamelCase , _lowerCamelCase ) __snake_case , __snake_case = hf_model.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) assert len(_lowerCamelCase ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] __snake_case = load_demo_image() __snake_case = vis_processors['''eval'''](_lowerCamelCase ).unsqueeze(0 ).to(_lowerCamelCase ) __snake_case = tokenizer(['''\n'''] , return_tensors='''pt''' ).input_ids.to(_lowerCamelCase ) # create processor __snake_case = BlipImageProcessor( size={'''height''': image_size, '''width''': image_size} , image_mean=_lowerCamelCase , image_std=_lowerCamelCase ) __snake_case = BlipaProcessor(image_processor=_lowerCamelCase , tokenizer=_lowerCamelCase ) __snake_case = processor(images=_lowerCamelCase , return_tensors='''pt''' ).pixel_values.to(_lowerCamelCase ) # make sure processor creates exact same pixel values assert torch.allclose(_lowerCamelCase , _lowerCamelCase ) original_model.to(_lowerCamelCase ) hf_model.to(_lowerCamelCase ) with torch.no_grad(): if "opt" in model_name: __snake_case = original_model({'''image''': original_pixel_values, '''text_input''': ['''''']} ).logits __snake_case = hf_model(_lowerCamelCase , _lowerCamelCase ).logits else: __snake_case = original_model( {'''image''': original_pixel_values, '''text_input''': ['''\n'''], '''text_output''': ['''\n''']} ).logits __snake_case = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -1_00 ) __snake_case = hf_model(_lowerCamelCase , _lowerCamelCase , labels=_lowerCamelCase ).logits assert original_logits.shape == logits.shape print('''First values of original logits:''' , original_logits[0, :3, :3] ) print('''First values of HF logits:''' , logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": __snake_case = torch.tensor( [[-41.5850, -4.4440, -8.9922], [-47.4322, -5.9143, -1.7340]] , device=_lowerCamelCase ) assert torch.allclose(logits[0, :3, :3] , _lowerCamelCase , atol=1E-4 ) elif model_name == "blip2-flan-t5-xl-coco": __snake_case = torch.tensor( [[-57.0109, -9.8967, -12.6280], [-68.6578, -12.7191, -10.5065]] , device=_lowerCamelCase ) else: # cast to same type __snake_case = logits.dtype assert torch.allclose(original_logits.to(_lowerCamelCase ) , _lowerCamelCase , atol=1E-2 ) print('''Looks ok!''' ) print('''Generating a caption...''' ) __snake_case = '''''' __snake_case = tokenizer(_lowerCamelCase , return_tensors='''pt''' ).input_ids.to(_lowerCamelCase ) __snake_case = original_model.generate({'''image''': original_pixel_values} ) __snake_case = hf_model.generate( _lowerCamelCase , _lowerCamelCase , do_sample=_lowerCamelCase , 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:''' , _lowerCamelCase ) __snake_case = input_ids.shape[1] __snake_case = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=_lowerCamelCase ) __snake_case = [text.strip() for text in output_text] print('''HF generation:''' , _lowerCamelCase ) if pytorch_dump_folder_path is not None: processor.save_pretrained(_lowerCamelCase ) hf_model.save_pretrained(_lowerCamelCase ) if push_to_hub: processor.push_to_hub(f'''nielsr/{model_name}''' ) hf_model.push_to_hub(f'''nielsr/{model_name}''' ) if __name__ == "__main__": UpperCAmelCase_ : Any = argparse.ArgumentParser() UpperCAmelCase_ : 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''', ) UpperCAmelCase_ : List[str] = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
24
0
"""simple docstring""" import numpy as np from PIL import Image def _UpperCamelCase ( UpperCamelCase , UpperCamelCase , UpperCamelCase ) -> np.ndarray: """simple docstring""" __UpperCAmelCase : str = np.array(UpperCamelCase ) if arr.shape[0] != arr.shape[1]: raise ValueError("The input array is not a square matrix" ) __UpperCAmelCase : Any = 0 __UpperCAmelCase : Dict = 0 __UpperCAmelCase : Tuple = 0 __UpperCAmelCase : Tuple = 0 # compute the shape of the output matrix __UpperCAmelCase : Optional[int] = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape maxpool_shape __UpperCAmelCase : List[str] = np.zeros((maxpool_shape, maxpool_shape) ) while i < arr.shape[0]: if i + size > arr.shape[0]: # if the end of the matrix is reached, break break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the maximum of the pooling matrix __UpperCAmelCase : str = np.max(arr[i : i + size, j : j + size] ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 __UpperCAmelCase : int = 0 __UpperCAmelCase : int = 0 return updated_arr def _UpperCamelCase ( UpperCamelCase , UpperCamelCase , UpperCamelCase ) -> np.ndarray: """simple docstring""" __UpperCAmelCase : List[str] = np.array(UpperCamelCase ) if arr.shape[0] != arr.shape[1]: raise ValueError("The input array is not a square matrix" ) __UpperCAmelCase : Tuple = 0 __UpperCAmelCase : List[str] = 0 __UpperCAmelCase : Tuple = 0 __UpperCAmelCase : Any = 0 # compute the shape of the output matrix __UpperCAmelCase : Tuple = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape avgpool_shape __UpperCAmelCase : str = np.zeros((avgpool_shape, avgpool_shape) ) while i < arr.shape[0]: # if the end of the matrix is reached, break if i + size > arr.shape[0]: break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the average of the pooling matrix __UpperCAmelCase : Tuple = int(np.average(arr[i : i + size, j : j + size] ) ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 __UpperCAmelCase : Tuple = 0 __UpperCAmelCase : Optional[Any] = 0 return updated_arr # Main Function if __name__ == "__main__": from doctest import testmod testmod(name="""avgpooling""", verbose=True) # Loading the image A = Image.open("""path_to_image""") # Converting the image to numpy array and maxpooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show() # Converting the image to numpy array and averagepooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()
77
'''simple docstring''' import warnings from typing import List, Optional, Tuple, Union import numpy as np import PIL import torch from ...models import UNetaDModel from ...schedulers import RePaintScheduler from ...utils import PIL_INTERPOLATION, logging, randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput UpperCAmelCase_ : Tuple = logging.get_logger(__name__) # pylint: disable=invalid-name def _UpperCamelCase (_lowerCamelCase : Union[List, PIL.Image.Image, torch.Tensor] )-> Optional[Any]: '''simple docstring''' warnings.warn( '''The preprocess method is deprecated and will be removed in a future version. Please''' ''' use VaeImageProcessor.preprocess instead''' , _lowerCamelCase , ) if isinstance(_lowerCamelCase , torch.Tensor ): return image elif isinstance(_lowerCamelCase , PIL.Image.Image ): __snake_case = [image] if isinstance(image[0] , PIL.Image.Image ): __snake_case , __snake_case = image[0].size __snake_case , __snake_case = (x - x % 8 for x in (w, h)) # resize to integer multiple of 8 __snake_case = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['''lanczos'''] ) )[None, :] for i in image] __snake_case = np.concatenate(_lowerCamelCase , axis=0 ) __snake_case = np.array(_lowerCamelCase ).astype(np.floataa ) / 255.0 __snake_case = image.transpose(0 , 3 , 1 , 2 ) __snake_case = 2.0 * image - 1.0 __snake_case = torch.from_numpy(_lowerCamelCase ) elif isinstance(image[0] , torch.Tensor ): __snake_case = torch.cat(_lowerCamelCase , dim=0 ) return image def _UpperCamelCase (_lowerCamelCase : Union[List, PIL.Image.Image, torch.Tensor] )-> Optional[Any]: '''simple docstring''' if isinstance(_lowerCamelCase , torch.Tensor ): return mask elif isinstance(_lowerCamelCase , PIL.Image.Image ): __snake_case = [mask] if isinstance(mask[0] , PIL.Image.Image ): __snake_case , __snake_case = mask[0].size __snake_case , __snake_case = (x - x % 32 for x in (w, h)) # resize to integer multiple of 32 __snake_case = [np.array(m.convert('''L''' ).resize((w, h) , resample=PIL_INTERPOLATION['''nearest'''] ) )[None, :] for m in mask] __snake_case = np.concatenate(_lowerCamelCase , axis=0 ) __snake_case = mask.astype(np.floataa ) / 255.0 __snake_case = 0 __snake_case = 1 __snake_case = torch.from_numpy(_lowerCamelCase ) elif isinstance(mask[0] , torch.Tensor ): __snake_case = torch.cat(_lowerCamelCase , dim=0 ) return mask class lowerCAmelCase ( __lowerCAmelCase): __lowercase : UNetaDModel __lowercase : RePaintScheduler def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) -> str: '''simple docstring''' super().__init__() self.register_modules(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) @torch.no_grad() def __call__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 250 , __SCREAMING_SNAKE_CASE = 0.0 , __SCREAMING_SNAKE_CASE = 10 , __SCREAMING_SNAKE_CASE = 10 , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = "pil" , __SCREAMING_SNAKE_CASE = True , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' __snake_case = image __snake_case = _preprocess_image(__SCREAMING_SNAKE_CASE ) __snake_case = original_image.to(device=self.device , dtype=self.unet.dtype ) __snake_case = _preprocess_mask(__SCREAMING_SNAKE_CASE ) __snake_case = mask_image.to(device=self.device , dtype=self.unet.dtype ) __snake_case = original_image.shape[0] # sample gaussian noise to begin the loop if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and len(__SCREAMING_SNAKE_CASE ) != batch_size: raise ValueError( F'''You have passed a list of generators of length {len(__SCREAMING_SNAKE_CASE )}, but requested an effective batch''' F''' size of {batch_size}. Make sure the batch size matches the length of the generators.''' ) __snake_case = original_image.shape __snake_case = randn_tensor(__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , device=self.device , dtype=self.unet.dtype ) # set step values self.scheduler.set_timesteps(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , self.device ) __snake_case = eta __snake_case = self.scheduler.timesteps[0] + 1 __snake_case = generator[0] if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else generator for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): if t < t_last: # predict the noise residual __snake_case = self.unet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).sample # compute previous image: x_t -> x_t-1 __snake_case = self.scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample else: # compute the reverse: x_t-1 -> x_t __snake_case = self.scheduler.undo_step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __snake_case = t __snake_case = (image / 2 + 0.5).clamp(0 , 1 ) __snake_case = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": __snake_case = self.numpy_to_pil(__SCREAMING_SNAKE_CASE ) if not return_dict: return (image,) return ImagePipelineOutput(images=__SCREAMING_SNAKE_CASE )
24
0
'''simple docstring''' import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( HubertConfig, HubertForCTC, HubertModel, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() SCREAMING_SNAKE_CASE_: str =logging.get_logger(__name__) SCREAMING_SNAKE_CASE_: Union[str, Any] ={ 'post_extract_proj': 'feature_projection.projection', 'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv', 'self_attn.k_proj': 'encoder.layers.*.attention.k_proj', 'self_attn.v_proj': 'encoder.layers.*.attention.v_proj', 'self_attn.q_proj': 'encoder.layers.*.attention.q_proj', 'self_attn.out_proj': 'encoder.layers.*.attention.out_proj', 'self_attn_layer_norm': 'encoder.layers.*.layer_norm', 'fc1': 'encoder.layers.*.feed_forward.intermediate_dense', 'fc2': 'encoder.layers.*.feed_forward.output_dense', 'final_layer_norm': 'encoder.layers.*.final_layer_norm', 'encoder.layer_norm': 'encoder.layer_norm', 'w2v_model.layer_norm': 'feature_projection.layer_norm', 'w2v_encoder.proj': 'lm_head', 'mask_emb': 'masked_spec_embed', } def lowerCAmelCase_ ( snake_case_ : int , snake_case_ : Optional[int] , snake_case_ : Union[str, Any] , snake_case_ : Dict , snake_case_ : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' for attribute in key.split("." ): UpperCAmelCase_ = getattr(snake_case_ , snake_case_ ) if weight_type is not None: UpperCAmelCase_ = getattr(snake_case_ , snake_case_ ).shape else: UpperCAmelCase_ = hf_pointer.shape assert hf_shape == value.shape, ( f"""Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be""" f""" {value.shape} for {full_name}""" ) if weight_type == "weight": UpperCAmelCase_ = value elif weight_type == "weight_g": UpperCAmelCase_ = value elif weight_type == "weight_v": UpperCAmelCase_ = value elif weight_type == "bias": UpperCAmelCase_ = value else: UpperCAmelCase_ = value logger.info(f"""{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.""" ) def lowerCAmelCase_ ( snake_case_ : Dict , snake_case_ : List[str] , snake_case_ : int ) -> int: '''simple docstring''' UpperCAmelCase_ = [] UpperCAmelCase_ = fairseq_model.state_dict() UpperCAmelCase_ = hf_model.hubert.feature_extractor if is_finetuned else hf_model.feature_extractor for name, value in fairseq_dict.items(): UpperCAmelCase_ = False if "conv_layers" in name: load_conv_layer( snake_case_ , snake_case_ , snake_case_ , snake_case_ , hf_model.config.feat_extract_norm == "group" , ) UpperCAmelCase_ = True else: for key, mapped_key in MAPPING.items(): UpperCAmelCase_ = "hubert." + mapped_key if (is_finetuned and mapped_key != "lm_head") else mapped_key if key in name or (key.split("w2v_model." )[-1] == name.split("." )[0] and not is_finetuned): UpperCAmelCase_ = True if "*" in mapped_key: UpperCAmelCase_ = name.split(snake_case_ )[0].split("." )[-2] UpperCAmelCase_ = mapped_key.replace("*" , snake_case_ ) if "weight_g" in name: UpperCAmelCase_ = "weight_g" elif "weight_v" in name: UpperCAmelCase_ = "weight_v" elif "weight" in name: UpperCAmelCase_ = "weight" elif "bias" in name: UpperCAmelCase_ = "bias" else: UpperCAmelCase_ = None set_recursively(snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ) continue if not is_used: unused_weights.append(snake_case_ ) logger.warning(f"""Unused weights: {unused_weights}""" ) def lowerCAmelCase_ ( snake_case_ : Optional[int] , snake_case_ : Union[str, Any] , snake_case_ : Any , snake_case_ : int , snake_case_ : Optional[int] ) -> List[Any]: '''simple docstring''' UpperCAmelCase_ = full_name.split("conv_layers." )[-1] UpperCAmelCase_ = name.split("." ) UpperCAmelCase_ = int(items[0] ) UpperCAmelCase_ = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f"""{full_name} has size {value.shape}, but""" f""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.""" ) UpperCAmelCase_ = value logger.info(f"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f"""{full_name} has size {value.shape}, but""" f""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.""" ) UpperCAmelCase_ = value logger.info(f"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f"""{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was""" " found." ) UpperCAmelCase_ = value logger.info(f"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f"""{full_name} has size {value.shape}, but""" f""" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.""" ) UpperCAmelCase_ = value logger.info(f"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) else: unused_weights.append(snake_case_ ) @torch.no_grad() def lowerCAmelCase_ ( snake_case_ : Dict , snake_case_ : Union[str, Any] , snake_case_ : str=None , snake_case_ : Tuple=None , snake_case_ : Dict=True ) -> Optional[int]: '''simple docstring''' if config_path is not None: UpperCAmelCase_ = HubertConfig.from_pretrained(snake_case_ ) else: UpperCAmelCase_ = HubertConfig() if is_finetuned: if dict_path: UpperCAmelCase_ = Dictionary.load(snake_case_ ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq UpperCAmelCase_ = target_dict.pad_index UpperCAmelCase_ = target_dict.bos_index UpperCAmelCase_ = target_dict.eos_index UpperCAmelCase_ = len(target_dict.symbols ) UpperCAmelCase_ = os.path.join(snake_case_ , "vocab.json" ) if not os.path.isdir(snake_case_ ): logger.error("--pytorch_dump_folder_path ({}) should be a directory".format(snake_case_ ) ) return os.makedirs(snake_case_ , exist_ok=snake_case_ ) with open(snake_case_ , "w" , encoding="utf-8" ) as vocab_handle: json.dump(target_dict.indices , snake_case_ ) UpperCAmelCase_ = WavaVecaCTCTokenizer( snake_case_ , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token="|" , do_lower_case=snake_case_ , ) UpperCAmelCase_ = True if config.feat_extract_norm == "layer" else False UpperCAmelCase_ = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_60_00 , padding_value=0 , do_normalize=snake_case_ , return_attention_mask=snake_case_ , ) UpperCAmelCase_ = WavaVecaProcessor(feature_extractor=snake_case_ , tokenizer=snake_case_ ) processor.save_pretrained(snake_case_ ) UpperCAmelCase_ = HubertForCTC(snake_case_ ) else: UpperCAmelCase_ = HubertModel(snake_case_ ) if is_finetuned: UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) else: UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) UpperCAmelCase_ = model[0].eval() recursively_load_weights(snake_case_ , snake_case_ , snake_case_ ) hf_wavavec.save_pretrained(snake_case_ ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_: Optional[int] =argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint') parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') parser.add_argument( '--not_finetuned', action='store_true', help='Whether the model to convert is a fine-tuned model or not' ) SCREAMING_SNAKE_CASE_: Optional[Any] =parser.parse_args() convert_hubert_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
78
'''simple docstring''' from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax UpperCAmelCase_ : str = logging.get_logger(__name__) @add_end_docstrings(__lowerCAmelCase) class lowerCAmelCase ( __lowerCAmelCase): def __init__( self , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' super().__init__(**__SCREAMING_SNAKE_CASE ) 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 , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> List[Any]: '''simple docstring''' return super().__call__(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , **__SCREAMING_SNAKE_CASE ) -> Tuple: '''simple docstring''' __snake_case = {} if "candidate_labels" in kwargs: __snake_case = kwargs['''candidate_labels'''] if "hypothesis_template" in kwargs: __snake_case = kwargs['''hypothesis_template'''] return preprocess_params, {}, {} def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="This is a photo of {}." ) -> Optional[Any]: '''simple docstring''' __snake_case = load_image(__SCREAMING_SNAKE_CASE ) __snake_case = self.image_processor(images=[image] , return_tensors=self.framework ) __snake_case = candidate_labels __snake_case = [hypothesis_template.format(__SCREAMING_SNAKE_CASE ) for x in candidate_labels] __snake_case = self.tokenizer(__SCREAMING_SNAKE_CASE , return_tensors=self.framework , padding=__SCREAMING_SNAKE_CASE ) __snake_case = [text_inputs] return inputs def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' __snake_case = model_inputs.pop('''candidate_labels''' ) __snake_case = model_inputs.pop('''text_inputs''' ) if isinstance(text_inputs[0] , __SCREAMING_SNAKE_CASE ): __snake_case = text_inputs[0] else: # Batching case. __snake_case = text_inputs[0][0] __snake_case = self.model(**__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) __snake_case = { '''candidate_labels''': candidate_labels, '''logits''': outputs.logits_per_image, } return model_outputs def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> str: '''simple docstring''' __snake_case = model_outputs.pop('''candidate_labels''' ) __snake_case = model_outputs['''logits'''][0] if self.framework == "pt": __snake_case = logits.softmax(dim=-1 ).squeeze(-1 ) __snake_case = probs.tolist() if not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): __snake_case = [scores] elif self.framework == "tf": __snake_case = stable_softmax(__SCREAMING_SNAKE_CASE , axis=-1 ) __snake_case = probs.numpy().tolist() else: raise ValueError(F'''Unsupported framework: {self.framework}''' ) __snake_case = [ {'''score''': score, '''label''': candidate_label} for score, candidate_label in sorted(zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) , key=lambda __SCREAMING_SNAKE_CASE : -x[0] ) ] return result
24
0
from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def _lowerCamelCase ( __lowerCamelCase = "laptop" ) -> DataFrame: '''simple docstring''' UpperCAmelCase__ : Dict = F"https://www.amazon.in/laptop/s?k={product}" UpperCAmelCase__ : Tuple = { """User-Agent""": """Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36""", """Accept-Language""": """en-US, en;q=0.5""", } UpperCAmelCase__ : Optional[int] = BeautifulSoup(requests.get(__lowerCamelCase , headers=__lowerCamelCase ).text ) # Initialize a Pandas dataframe with the column titles UpperCAmelCase__ : str = DataFrame( columns=[ """Product Title""", """Product Link""", """Current Price of the product""", """Product Rating""", """MRP of the product""", """Discount""", ] ) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( """div""" , attrs={"""class""": """s-result-item""", """data-component-type""": """s-search-result"""} , ) , soup.find_all("""div""" , attrs={"""class""": """a-row a-size-base a-color-base"""} ) , ): try: UpperCAmelCase__ : str = item.ha.text UpperCAmelCase__ : Union[str, Any] = """https://www.amazon.in/""" + item.ha.a["""href"""] UpperCAmelCase__ : Union[str, Any] = item.find("""span""" , attrs={"""class""": """a-offscreen"""} ).text try: UpperCAmelCase__ : Optional[Any] = item.find("""span""" , attrs={"""class""": """a-icon-alt"""} ).text except AttributeError: UpperCAmelCase__ : Dict = """Not available""" try: UpperCAmelCase__ : List[str] = ( """₹""" + item.find( """span""" , attrs={"""class""": """a-price a-text-price"""} ).text.split("""₹""" )[1] ) except AttributeError: UpperCAmelCase__ : int = """""" try: UpperCAmelCase__ : int = float( ( ( float(product_mrp.strip("""₹""" ).replace(""",""" , """""" ) ) - float(product_price.strip("""₹""" ).replace(""",""" , """""" ) ) ) / float(product_mrp.strip("""₹""" ).replace(""",""" , """""" ) ) ) * 100 ) except ValueError: UpperCAmelCase__ : Any = float("""nan""" ) except AttributeError: pass UpperCAmelCase__ : int = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] UpperCAmelCase__ : Any = """ """ UpperCAmelCase__ : Union[str, Any] = """ """ data_frame.index += 1 return data_frame if __name__ == "__main__": SCREAMING_SNAKE_CASE__ : Union[str, Any] = """headphones""" get_amazon_product_data(product).to_csv(f'''Amazon Product Data for {product}.csv''')
79
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) UpperCAmelCase_ : List[str] = { '''configuration_gpt_bigcode''': ['''GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GPTBigCodeConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : int = [ '''GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST''', '''GPTBigCodeForSequenceClassification''', '''GPTBigCodeForTokenClassification''', '''GPTBigCodeForCausalLM''', '''GPTBigCodeModel''', '''GPTBigCodePreTrainedModel''', ] if TYPE_CHECKING: from .configuration_gpt_bigcode import GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTBigCodeConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_bigcode import ( GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTBigCodeForCausalLM, GPTBigCodeForSequenceClassification, GPTBigCodeForTokenClassification, GPTBigCodeModel, GPTBigCodePreTrainedModel, ) else: import sys UpperCAmelCase_ : Any = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
24
0
import itertools import os from collections import Counter, defaultdict from concurrent.futures import ThreadPoolExecutor, as_completed import numpy as np import datasets from .execute import check_correctness __UpperCamelCase : int = """\ @misc{chen2021evaluating, title={Evaluating Large Language Models Trained on Code}, author={Mark Chen and Jerry Tworek and Heewoo Jun and Qiming Yuan \ and Henrique Ponde de Oliveira Pinto and Jared Kaplan and Harri Edwards \ and Yuri Burda and Nicholas Joseph and Greg Brockman and Alex Ray \ and Raul Puri and Gretchen Krueger and Michael Petrov and Heidy Khlaaf \ and Girish Sastry and Pamela Mishkin and Brooke Chan and Scott Gray \ and Nick Ryder and Mikhail Pavlov and Alethea Power and Lukasz Kaiser \ and Mohammad Bavarian and Clemens Winter and Philippe Tillet \ and Felipe Petroski Such and Dave Cummings and Matthias Plappert \ and Fotios Chantzis and Elizabeth Barnes and Ariel Herbert-Voss \ and William Hebgen Guss and Alex Nichol and Alex Paino and Nikolas Tezak \ and Jie Tang and Igor Babuschkin and Suchir Balaji and Shantanu Jain \ and William Saunders and Christopher Hesse and Andrew N. Carr \ and Jan Leike and Josh Achiam and Vedant Misra and Evan Morikawa \ and Alec Radford and Matthew Knight and Miles Brundage and Mira Murati \ and Katie Mayer and Peter Welinder and Bob McGrew and Dario Amodei \ and Sam McCandlish and Ilya Sutskever and Wojciech Zaremba}, year={2021}, eprint={2107.03374}, archivePrefix={arXiv}, primaryClass={cs.LG} } """ __UpperCamelCase : List[Any] = """\ This metric implements the evaluation harness for the HumanEval problem solving dataset described in the paper \"Evaluating Large Language Models Trained on Code\" (https://arxiv.org/abs/2107.03374). """ __UpperCamelCase : int = """ Calculates how good are predictions given some references, using certain scores Args: predictions: list of candidates to evaluate. Each candidates should be a list of strings with several code candidates to solve the problem. references: a list with a test for each prediction. Each test should evaluate the correctness of a code candidate. k: number of code candidates to consider in the evaluation (Default: [1, 10, 100]) num_workers: number of workers used to evaluate the canidate programs (Default: 4). timeout: Returns: pass_at_k: dict with pass rates for each k results: dict with granular results of each unittest Examples: >>> code_eval = datasets.load_metric(\"code_eval\") >>> test_cases = [\"assert add(2,3)==5\"] >>> candidates = [[\"def add(a,b): return a*b\", \"def add(a, b): return a+b\"]] >>> pass_at_k, results = code_eval.compute(references=test_cases, predictions=candidates, k=[1, 2]) >>> print(pass_at_k) {'pass@1': 0.5, 'pass@2': 1.0} """ __UpperCamelCase : Union[str, Any] = """ ################################################################################ !!!WARNING!!! ################################################################################ The \"code_eval\" metric executes untrusted model-generated code in Python. Although it is highly unlikely that model-generated code will do something overtly malicious in response to this test suite, model-generated code may act destructively due to a lack of model capability or alignment. Users are strongly encouraged to sandbox this evaluation suite so that it does not perform destructive actions on their host or network. For more information on how OpenAI sandboxes its code, see the paper \"Evaluating Large Language Models Trained on Code\" (https://arxiv.org/abs/2107.03374). Once you have read this disclaimer and taken appropriate precautions, set the environment variable HF_ALLOW_CODE_EVAL=\"1\". Within Python you can to this with: >>> import os >>> os.environ[\"HF_ALLOW_CODE_EVAL\"] = \"1\" ################################################################################\ """ __UpperCamelCase : Any = """The MIT License Copyright (c) OpenAI (https://openai.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.""" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCamelCase ( datasets.Metric ): def _a ( self : List[str] ) -> Optional[int]: """simple docstring""" return datasets.MetricInfo( # This is the description that will appear on the metrics page. description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""string""" ) ), """references""": datasets.Value("""string""" ), } ) , homepage="""https://github.com/openai/human-eval""" , codebase_urls=["""https://github.com/openai/human-eval"""] , reference_urls=["""https://github.com/openai/human-eval"""] , license=_LICENSE , ) def _a ( self : Optional[int] , _lowerCAmelCase : Tuple , _lowerCAmelCase : List[str] , _lowerCAmelCase : List[str]=[1, 10, 100] , _lowerCAmelCase : List[Any]=4 , _lowerCAmelCase : Optional[int]=3.0 ) -> List[Any]: """simple docstring""" if os.getenv("""HF_ALLOW_CODE_EVAL""" , 0 ) != "1": raise ValueError(_WARNING ) if os.name == "nt": raise NotImplementedError("""This metric is currently not supported on Windows.""" ) with ThreadPoolExecutor(max_workers=_lowerCAmelCase ) as executor: __lowercase = [] __lowercase = Counter() __lowercase = 0 __lowercase = defaultdict(_lowerCAmelCase ) for task_id, (candidates, test_case) in enumerate(zip(_lowerCAmelCase , _lowerCAmelCase ) ): for candidate in candidates: __lowercase = candidate + """\n""" + test_case __lowercase = (test_program, timeout, task_id, completion_id[task_id]) __lowercase = executor.submit(_lowerCAmelCase , *_lowerCAmelCase ) futures.append(_lowerCAmelCase ) completion_id[task_id] += 1 n_samples += 1 for future in as_completed(_lowerCAmelCase ): __lowercase = future.result() results[result["task_id"]].append((result["""completion_id"""], result) ) __lowercase , __lowercase = [], [] for result in results.values(): result.sort() __lowercase = [r[1]["""passed"""] for r in result] total.append(len(_lowerCAmelCase ) ) correct.append(sum(_lowerCAmelCase ) ) __lowercase = np.array(_lowerCAmelCase ) __lowercase = np.array(_lowerCAmelCase ) __lowercase = k __lowercase = {F'pass@{k}': estimate_pass_at_k(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ).mean() for k in ks if (total >= k).all()} return pass_at_k, results def snake_case ( lowerCamelCase , lowerCamelCase , lowerCamelCase ): '''simple docstring''' def estimator(lowerCamelCase , lowerCamelCase , lowerCamelCase ) -> float: if n - c < k: return 1.0 return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1 , n + 1 ) ) if isinstance(lowerCamelCase , lowerCamelCase ): __lowercase = itertools.repeat(lowerCamelCase , len(lowerCamelCase ) ) else: assert len(lowerCamelCase ) == len(lowerCamelCase ) __lowercase = iter(lowerCamelCase ) return np.array([estimator(int(lowerCamelCase ) , int(lowerCamelCase ) , lowerCamelCase ) for n, c in zip(lowerCamelCase , lowerCamelCase )] )
80
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' __snake_case = abs(_lowerCamelCase ) __snake_case = 0 while n > 0: res += n % 10 n //= 10 return res def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' __snake_case = abs(_lowerCamelCase ) return n if n < 10 else n % 10 + sum_of_digits(n // 10 ) def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' return sum(int(_lowerCamelCase ) for c in str(abs(_lowerCamelCase ) ) ) def _UpperCamelCase ()-> None: '''simple docstring''' from collections.abc import Callable from timeit import timeit def benchmark_a_function(_lowerCamelCase : Callable , _lowerCamelCase : int ) -> None: __snake_case = f'''{func.__name__}({value})''' __snake_case = timeit(f'''__main__.{call}''' , setup='''import __main__''' ) print(f'''{call:56} = {func(_lowerCamelCase )} -- {timing:.4f} seconds''' ) for value in (26_21_44, 11_25_89_99_06_84_26_24, 1_26_76_50_60_02_28_22_94_01_49_67_03_20_53_76): for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact): benchmark_a_function(_lowerCamelCase , _lowerCamelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
24
0
import warnings from ...utils import logging from .image_processing_deit import DeiTImageProcessor _snake_case : List[str] = logging.get_logger(__name__) class a (_lowerCAmelCase ): """simple docstring""" def __init__( self : List[str] , *lowerCamelCase : str , **lowerCamelCase : Union[str, Any] ) -> None: warnings.warn( "The class DeiTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use DeiTImageProcessor instead." , lowerCamelCase , ) super().__init__(*lowerCamelCase , **lowerCamelCase )
81
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : Optional[Any] )-> Dict: '''simple docstring''' __snake_case = [] __snake_case = [] __snake_case = { '''^''': 3, '''*''': 2, '''/''': 2, '''%''': 2, '''+''': 1, '''-''': 1, } # Priority of each operator __snake_case = len(_lowerCamelCase ) if (len(_lowerCamelCase ) > 7) else 7 # Print table header for output print( '''Symbol'''.center(8 ) , '''Stack'''.center(_lowerCamelCase ) , '''Postfix'''.center(_lowerCamelCase ) , sep=''' | ''' , ) print('''-''' * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(_lowerCamelCase ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(_lowerCamelCase ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(_lowerCamelCase ) == 0: stack.append(_lowerCamelCase ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(_lowerCamelCase ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(_lowerCamelCase ) # push x to stack print( x.center(8 ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , sep=''' | ''' , ) # Output in tabular format while len(_lowerCamelCase ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( ''' '''.center(8 ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , sep=''' | ''' , ) # Output in tabular format return "".join(_lowerCamelCase ) # return Postfix as str def _UpperCamelCase (_lowerCamelCase : Optional[Any] )-> str: '''simple docstring''' __snake_case = list(infix[::-1] ) # reverse the infix equation for i in range(len(_lowerCamelCase ) ): if infix[i] == "(": __snake_case = ''')''' # change "(" to ")" elif infix[i] == ")": __snake_case = '''(''' # change ")" to "(" return (infix_2_postfix(''''''.join(_lowerCamelCase ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": UpperCAmelCase_ : Dict = input('''\nEnter an Infix Equation = ''') # Input an Infix equation UpperCAmelCase_ : Optional[Any] = ''''''.join(Infix.split()) # Remove spaces from the input print('''\n\t''', Infix, '''(Infix) -> ''', infix_2_prefix(Infix), '''(Prefix)''')
24
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 lowerCamelCase = logging.get_logger(__name__) @add_end_docstrings(SCREAMING_SNAKE_CASE ) class lowercase__ ( SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Union[str, Any] , **_UpperCAmelCase : Optional[int] ) -> str: '''simple docstring''' super().__init__(**_UpperCAmelCase ) if self.framework == "tf": raise ValueError(F"""The {self.__class__} is only available in PyTorch.""" ) requires_backends(self , "vision" ) self.check_model_type(_UpperCAmelCase ) def __call__( self : int , _UpperCAmelCase : Union[str, "Image.Image", List[Dict[str, Any]]] , _UpperCAmelCase : Union[str, List[str]] = None , **_UpperCAmelCase : Optional[int] , ) -> List[Any]: '''simple docstring''' if "text_queries" in kwargs: UpperCAmelCase_ = kwargs.pop("text_queries" ) if isinstance(_UpperCAmelCase , (str, Image.Image) ): UpperCAmelCase_ = {"image": image, "candidate_labels": candidate_labels} else: UpperCAmelCase_ = image UpperCAmelCase_ = super().__call__(_UpperCAmelCase , **_UpperCAmelCase ) return results def lowercase__ ( self : str , **_UpperCAmelCase : List[Any] ) -> List[str]: '''simple docstring''' UpperCAmelCase_ = {} if "threshold" in kwargs: UpperCAmelCase_ = kwargs["threshold"] if "top_k" in kwargs: UpperCAmelCase_ = kwargs["top_k"] return {}, {}, postprocess_params def lowercase__ ( self : int , _UpperCAmelCase : int ) -> Any: '''simple docstring''' UpperCAmelCase_ = load_image(inputs["image"] ) UpperCAmelCase_ = inputs["candidate_labels"] if isinstance(_UpperCAmelCase , _UpperCAmelCase ): UpperCAmelCase_ = candidate_labels.split("," ) UpperCAmelCase_ = torch.tensor([[image.height, image.width]] , dtype=torch.intaa ) for i, candidate_label in enumerate(_UpperCAmelCase ): UpperCAmelCase_ = self.tokenizer(_UpperCAmelCase , return_tensors=self.framework ) UpperCAmelCase_ = self.image_processor(_UpperCAmelCase , return_tensors=self.framework ) yield { "is_last": i == len(_UpperCAmelCase ) - 1, "target_size": target_size, "candidate_label": candidate_label, **text_inputs, **image_features, } def lowercase__ ( self : int , _UpperCAmelCase : List[Any] ) -> int: '''simple docstring''' UpperCAmelCase_ = model_inputs.pop("target_size" ) UpperCAmelCase_ = model_inputs.pop("candidate_label" ) UpperCAmelCase_ = model_inputs.pop("is_last" ) UpperCAmelCase_ = self.model(**_UpperCAmelCase ) UpperCAmelCase_ = {"target_size": target_size, "candidate_label": candidate_label, "is_last": is_last, **outputs} return model_outputs def lowercase__ ( self : str , _UpperCAmelCase : List[str] , _UpperCAmelCase : Any=0.1 , _UpperCAmelCase : List[str]=None ) -> int: '''simple docstring''' UpperCAmelCase_ = [] for model_output in model_outputs: UpperCAmelCase_ = model_output["candidate_label"] UpperCAmelCase_ = BaseModelOutput(_UpperCAmelCase ) UpperCAmelCase_ = self.image_processor.post_process_object_detection( outputs=_UpperCAmelCase , threshold=_UpperCAmelCase , target_sizes=model_output["target_size"] )[0] for index in outputs["scores"].nonzero(): UpperCAmelCase_ = outputs["scores"][index].item() UpperCAmelCase_ = self._get_bounding_box(outputs["boxes"][index][0] ) UpperCAmelCase_ = {"score": score, "label": label, "box": box} results.append(_UpperCAmelCase ) UpperCAmelCase_ = sorted(_UpperCAmelCase , key=lambda _UpperCAmelCase : x["score"] , reverse=_UpperCAmelCase ) if top_k: UpperCAmelCase_ = results[:top_k] return results def lowercase__ ( self : str , _UpperCAmelCase : "torch.Tensor" ) -> Dict[str, int]: '''simple docstring''' if self.framework != "pt": raise ValueError("The ZeroShotObjectDetectionPipeline is only available in PyTorch." ) UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ = box.int().tolist() UpperCAmelCase_ = { "xmin": xmin, "ymin": ymin, "xmax": xmax, "ymax": ymax, } return bbox
82
'''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 ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices UpperCAmelCase_ : int = logging.get_logger(__name__) UpperCAmelCase_ : Dict = { '''microsoft/swin-tiny-patch4-window7-224''': ( '''https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json''' ), # See all Swin models at https://huggingface.co/models?filter=swin } class lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase): __lowercase : List[Any] = '''swin''' __lowercase : str = { '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self , __SCREAMING_SNAKE_CASE=224 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=96 , __SCREAMING_SNAKE_CASE=[2, 2, 6, 2] , __SCREAMING_SNAKE_CASE=[3, 6, 12, 24] , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=4.0 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1E-5 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ) -> int: '''simple docstring''' super().__init__(**__SCREAMING_SNAKE_CASE ) __snake_case = image_size __snake_case = patch_size __snake_case = num_channels __snake_case = embed_dim __snake_case = depths __snake_case = len(__SCREAMING_SNAKE_CASE ) __snake_case = num_heads __snake_case = window_size __snake_case = mlp_ratio __snake_case = qkv_bias __snake_case = hidden_dropout_prob __snake_case = attention_probs_dropout_prob __snake_case = drop_path_rate __snake_case = hidden_act __snake_case = use_absolute_embeddings __snake_case = layer_norm_eps __snake_case = initializer_range __snake_case = encoder_stride # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model __snake_case = int(embed_dim * 2 ** (len(__SCREAMING_SNAKE_CASE ) - 1) ) __snake_case = ['''stem'''] + [F'''stage{idx}''' for idx in range(1 , len(__SCREAMING_SNAKE_CASE ) + 1 )] __snake_case , __snake_case = get_aligned_output_features_output_indices( out_features=__SCREAMING_SNAKE_CASE , out_indices=__SCREAMING_SNAKE_CASE , stage_names=self.stage_names ) class lowerCAmelCase ( __lowerCAmelCase): __lowercase : Optional[int] = version.parse('''1.11''') @property def lowerCAmelCase ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def lowerCAmelCase ( self ) -> float: '''simple docstring''' return 1E-4
24
0
"""simple docstring""" lowerCAmelCase__ = [ [0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], [0, 4, 0, 0, 14, 0], [0, 0, 9, 0, 0, 20], [0, 0, 0, 7, 0, 4], [0, 0, 0, 0, 0, 0], ] def snake_case_ ( A_ : Tuple, A_ : List[str], A_ : Dict, A_ : Tuple ): '''simple docstring''' _lowerCamelCase : List[str] = [False] * len(A_ ) _lowerCamelCase : Tuple = [s] _lowerCamelCase : Any = True while queue: _lowerCamelCase : List[Any] = queue.pop(0 ) for ind in range(len(graph[u] ) ): if visited[ind] is False and graph[u][ind] > 0: queue.append(A_ ) _lowerCamelCase : Optional[int] = True _lowerCamelCase : Optional[int] = u return visited[t] def snake_case_ ( A_ : Tuple, A_ : str, A_ : Any ): '''simple docstring''' _lowerCamelCase : Any = [-1] * (len(A_ )) _lowerCamelCase : Any = 0 _lowerCamelCase : Optional[int] = [] _lowerCamelCase : Tuple = [i[:] for i in graph] # Record original cut, copy. while bfs(A_, A_, A_, A_ ): _lowerCamelCase : int = float('''Inf''' ) _lowerCamelCase : List[str] = sink while s != source: # Find the minimum value in select path _lowerCamelCase : List[Any] = min(A_, graph[parent[s]][s] ) _lowerCamelCase : List[str] = parent[s] max_flow += path_flow _lowerCamelCase : Optional[Any] = sink while v != source: _lowerCamelCase : Tuple = parent[v] graph[u][v] -= path_flow graph[v][u] += path_flow _lowerCamelCase : Optional[int] = parent[v] for i in range(len(A_ ) ): for j in range(len(graph[0] ) ): if graph[i][j] == 0 and temp[i][j] > 0: res.append((i, j) ) return res if __name__ == "__main__": print(mincut(test_graph, source=0, sink=5))
83
'''simple docstring''' import re import time from typing import Optional import IPython.display as disp from ..trainer_callback import TrainerCallback from ..trainer_utils import IntervalStrategy, has_length def _UpperCamelCase (_lowerCamelCase : Tuple )-> List[str]: '''simple docstring''' __snake_case = int(_lowerCamelCase ) __snake_case , __snake_case , __snake_case = t // 36_00, (t // 60) % 60, t % 60 return f'''{h}:{m:02d}:{s:02d}''' if h != 0 else f'''{m:02d}:{s:02d}''' def _UpperCamelCase (_lowerCamelCase : Any , _lowerCamelCase : Union[str, Any] , _lowerCamelCase : str , _lowerCamelCase : int , _lowerCamelCase : List[Any]=3_00 )-> int: '''simple docstring''' return f''' <div> {prefix} <progress value=\'{value}\' max=\'{total}\' style=\'width:{width}px; height:20px; vertical-align: middle;\'></progress> {label} </div> ''' def _UpperCamelCase (_lowerCamelCase : int )-> List[Any]: '''simple docstring''' __snake_case = '''<table border="1" class="dataframe">\n''' html_code += """ <thead>\n <tr style="text-align: left;">\n""" for i in items[0]: html_code += f''' <th>{i}</th>\n''' html_code += " </tr>\n </thead>\n <tbody>\n" for line in items[1:]: html_code += " <tr>\n" for elt in line: __snake_case = f'''{elt:.6f}''' if isinstance(_lowerCamelCase , _lowerCamelCase ) else str(_lowerCamelCase ) html_code += f''' <td>{elt}</td>\n''' html_code += " </tr>\n" html_code += " </tbody>\n</table><p>" return html_code class lowerCAmelCase : __lowercase : str = 5 __lowercase : Optional[Any] = 0.2 def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = 300 , ) -> List[Any]: '''simple docstring''' __snake_case = total __snake_case = '''''' if prefix is None else prefix __snake_case = leave __snake_case = parent __snake_case = width __snake_case = None __snake_case = None __snake_case = None def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = None ) -> Any: '''simple docstring''' __snake_case = value if comment is not None: __snake_case = comment if self.last_value is None: __snake_case = __snake_case = time.time() __snake_case = __snake_case = value __snake_case = __snake_case = None __snake_case = self.warmup __snake_case = 1 self.update_bar(__SCREAMING_SNAKE_CASE ) elif value <= self.last_value and not force_update: return elif force_update or self.first_calls > 0 or value >= min(self.last_value + self.wait_for , self.total ): if self.first_calls > 0: self.first_calls -= 1 __snake_case = time.time() __snake_case = current_time - self.start_time # We could have value = self.start_value if the update is called twixe with the same start value. if value > self.start_value: __snake_case = self.elapsed_time / (value - self.start_value) else: __snake_case = None if value >= self.total: __snake_case = self.total __snake_case = None if not self.leave: self.close() elif self.average_time_per_item is not None: __snake_case = self.average_time_per_item * (self.total - value) self.update_bar(__SCREAMING_SNAKE_CASE ) __snake_case = value __snake_case = current_time if self.average_time_per_item is None: __snake_case = 1 else: __snake_case = max(int(self.update_every / self.average_time_per_item ) , 1 ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) -> List[str]: '''simple docstring''' __snake_case = ''' ''' * (len(str(self.total ) ) - len(str(__SCREAMING_SNAKE_CASE ) )) + str(__SCREAMING_SNAKE_CASE ) if self.elapsed_time is None: __snake_case = F'''[{spaced_value}/{self.total} : < :''' elif self.predicted_remaining is None: __snake_case = F'''[{spaced_value}/{self.total} {format_time(self.elapsed_time )}''' else: __snake_case = ( F'''[{spaced_value}/{self.total} {format_time(self.elapsed_time )} <''' F''' {format_time(self.predicted_remaining )}''' ) self.label += F''', {1/self.average_time_per_item:.2f} it/s''' self.label += "]" if self.comment is None or len(self.comment ) == 0 else F''', {self.comment}]''' self.display() def lowerCAmelCase ( self ) -> List[Any]: '''simple docstring''' __snake_case = html_progress_bar(self.value , self.total , self.prefix , self.label , self.width ) if self.parent is not None: # If this is a child bar, the parent will take care of the display. self.parent.display() return if self.output is None: __snake_case = disp.display(disp.HTML(self.html_code ) , display_id=__SCREAMING_SNAKE_CASE ) else: self.output.update(disp.HTML(self.html_code ) ) def lowerCAmelCase ( self ) -> Dict: '''simple docstring''' if self.parent is None and self.output is not None: self.output.update(disp.HTML('''''' ) ) class lowerCAmelCase ( __lowerCAmelCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) -> Any: '''simple docstring''' super().__init__(__SCREAMING_SNAKE_CASE ) __snake_case = None if column_names is None else [column_names] __snake_case = None def lowerCAmelCase ( self ) -> Any: '''simple docstring''' __snake_case = html_progress_bar(self.value , self.total , self.prefix , self.label , self.width ) if self.inner_table is not None: self.html_code += text_to_html_table(self.inner_table ) if self.child_bar is not None: self.html_code += self.child_bar.html_code if self.output is None: __snake_case = disp.display(disp.HTML(self.html_code ) , display_id=__SCREAMING_SNAKE_CASE ) else: self.output.update(disp.HTML(self.html_code ) ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' if self.inner_table is None: __snake_case = [list(values.keys() ), list(values.values() )] else: __snake_case = self.inner_table[0] if len(self.inner_table ) == 1: # We give a chance to update the column names at the first iteration for key in values.keys(): if key not in columns: columns.append(__SCREAMING_SNAKE_CASE ) __snake_case = columns self.inner_table.append([values[c] for c in columns] ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=300 ) -> List[str]: '''simple docstring''' __snake_case = NotebookProgressBar(__SCREAMING_SNAKE_CASE , prefix=__SCREAMING_SNAKE_CASE , parent=self , width=__SCREAMING_SNAKE_CASE ) return self.child_bar def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case = None self.display() class lowerCAmelCase ( __lowerCAmelCase): def __init__( self ) -> str: '''simple docstring''' __snake_case = None __snake_case = None __snake_case = False def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' __snake_case = '''Epoch''' if args.evaluation_strategy == IntervalStrategy.EPOCH else '''Step''' __snake_case = 0 __snake_case = 0 __snake_case = [self.first_column] + ['''Training Loss'''] if args.evaluation_strategy != IntervalStrategy.NO: column_names.append('''Validation Loss''' ) __snake_case = NotebookTrainingTracker(state.max_steps , __SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> List[Any]: '''simple docstring''' __snake_case = int(state.epoch ) if int(state.epoch ) == state.epoch else F'''{state.epoch:.2f}''' self.training_tracker.update( state.global_step + 1 , comment=F'''Epoch {epoch}/{state.num_train_epochs}''' , force_update=self._force_next_update , ) __snake_case = False def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' if not has_length(__SCREAMING_SNAKE_CASE ): return if self.prediction_bar is None: if self.training_tracker is not None: __snake_case = self.training_tracker.add_child(len(__SCREAMING_SNAKE_CASE ) ) else: __snake_case = NotebookProgressBar(len(__SCREAMING_SNAKE_CASE ) ) self.prediction_bar.update(1 ) else: self.prediction_bar.update(self.prediction_bar.value + 1 ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' if self.prediction_bar is not None: self.prediction_bar.close() __snake_case = None def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) -> List[Any]: '''simple docstring''' if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs: __snake_case = {'''Training Loss''': logs['''loss''']} # First column is necessarily Step sine we're not in epoch eval strategy __snake_case = state.global_step self.training_tracker.write_line(__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) -> Optional[Any]: '''simple docstring''' if self.training_tracker is not None: __snake_case = {'''Training Loss''': '''No log''', '''Validation Loss''': '''No log'''} for log in reversed(state.log_history ): if "loss" in log: __snake_case = log['''loss'''] break if self.first_column == "Epoch": __snake_case = int(state.epoch ) else: __snake_case = state.global_step __snake_case = '''eval''' for k in metrics: if k.endswith('''_loss''' ): __snake_case = re.sub(r'''\_loss$''' , '''''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop('''total_flos''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop('''epoch''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_runtime''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_samples_per_second''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_steps_per_second''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_jit_compilation_time''' , __SCREAMING_SNAKE_CASE ) for k, v in metrics.items(): if k == F'''{metric_key_prefix}_loss''': __snake_case = v else: __snake_case = k.split('''_''' ) __snake_case = ''' '''.join([part.capitalize() for part in splits[1:]] ) __snake_case = v self.training_tracker.write_line(__SCREAMING_SNAKE_CASE ) self.training_tracker.remove_child() __snake_case = None # Evaluation takes a long time so we should force the next update. __snake_case = True def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' self.training_tracker.update( state.global_step , comment=F'''Epoch {int(state.epoch )}/{state.num_train_epochs}''' , force_update=__SCREAMING_SNAKE_CASE ) __snake_case = None
24
0
import json import os import unittest from transformers import OpenAIGPTTokenizer, OpenAIGPTTokenizerFast from transformers.models.openai.tokenization_openai import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_spacy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class A_ ( __lowerCamelCase , unittest.TestCase ): '''simple docstring''' _UpperCamelCase : Tuple = OpenAIGPTTokenizer _UpperCamelCase : List[Any] = OpenAIGPTTokenizerFast _UpperCamelCase : int = True _UpperCamelCase : List[Any] = False def SCREAMING_SNAKE_CASE__ ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt lowercase = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'w</w>', 'r</w>', 't</w>', 'lo', 'low', 'er</w>', 'low</w>', 'lowest</w>', 'newer</w>', 'wider</w>', '<unk>', ] lowercase = dict(zip(snake_case , range(len(snake_case ) ) ) ) lowercase = ['#version: 0.2', 'l o', 'lo w', 'e r</w>', ''] lowercase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) lowercase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' ) as fp: fp.write(json.dumps(snake_case ) ) with open(self.merges_file , 'w' ) as fp: fp.write('\n'.join(snake_case ) ) def SCREAMING_SNAKE_CASE__ ( self , snake_case ): return "lower newer", "lower newer" def SCREAMING_SNAKE_CASE__ ( self ): lowercase = OpenAIGPTTokenizer(self.vocab_file , self.merges_file ) lowercase = 'lower' lowercase = ['low', 'er</w>'] lowercase = tokenizer.tokenize(snake_case ) self.assertListEqual(snake_case , snake_case ) lowercase = tokens + ['<unk>'] lowercase = [14, 15, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case ) , snake_case ) def SCREAMING_SNAKE_CASE__ ( self , snake_case=15 ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): lowercase = self.rust_tokenizer_class.from_pretrained(snake_case , **snake_case ) # Simple input lowercase = 'This is a simple input' lowercase = ['This is a simple input 1', 'This is a simple input 2'] lowercase = ('This is a simple input', 'This is a pair') lowercase = [ ('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(snake_case , tokenizer_r.encode , snake_case , max_length=snake_case , padding='max_length' ) # Simple input self.assertRaises(snake_case , tokenizer_r.encode_plus , snake_case , max_length=snake_case , padding='max_length' ) # Simple input self.assertRaises( snake_case , tokenizer_r.batch_encode_plus , snake_case , max_length=snake_case , padding='max_length' , ) # Pair input self.assertRaises(snake_case , tokenizer_r.encode , snake_case , max_length=snake_case , padding='max_length' ) # Pair input self.assertRaises(snake_case , tokenizer_r.encode_plus , snake_case , max_length=snake_case , padding='max_length' ) # Pair input self.assertRaises( snake_case , tokenizer_r.batch_encode_plus , snake_case , max_length=snake_case , padding='max_length' , ) def SCREAMING_SNAKE_CASE__ ( self ): pass @require_ftfy @require_spacy @require_tokenizers class A_ ( __lowerCamelCase ): '''simple docstring''' pass
84
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' if divisor % 5 == 0 or divisor % 2 == 0: return 0 __snake_case = 1 __snake_case = 1 while repunit: __snake_case = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index def _UpperCamelCase (_lowerCamelCase : int = 1_00_00_00 )-> int: '''simple docstring''' __snake_case = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(_lowerCamelCase ) <= limit: divisor += 2 return divisor if __name__ == "__main__": print(F"""{solution() = }""")
24
0
from __future__ import annotations import unittest from transformers import BlenderbotSmallConfig, BlenderbotSmallTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel @require_tf class snake_case : lowercase_ = BlenderbotSmallConfig lowercase_ = {} lowercase_ = 'gelu' def __init__( self : List[Any] , a_ : int , a_ : Any=13 , a_ : List[str]=7 , a_ : Optional[int]=True , a_ : Tuple=False , a_ : Optional[int]=99 , a_ : Tuple=32 , a_ : Union[str, Any]=2 , a_ : Union[str, Any]=4 , a_ : str=37 , a_ : List[Any]=0.1 , a_ : int=0.1 , a_ : List[Any]=20 , a_ : Optional[Any]=2 , a_ : List[str]=1 , a_ : Tuple=0 , )-> int: """simple docstring""" SCREAMING_SNAKE_CASE__ : int = parent SCREAMING_SNAKE_CASE__ : Tuple = batch_size SCREAMING_SNAKE_CASE__ : Optional[int] = seq_length SCREAMING_SNAKE_CASE__ : Optional[Any] = is_training SCREAMING_SNAKE_CASE__ : Tuple = use_labels SCREAMING_SNAKE_CASE__ : List[Any] = vocab_size SCREAMING_SNAKE_CASE__ : List[Any] = hidden_size SCREAMING_SNAKE_CASE__ : List[Any] = num_hidden_layers SCREAMING_SNAKE_CASE__ : int = num_attention_heads SCREAMING_SNAKE_CASE__ : List[Any] = intermediate_size SCREAMING_SNAKE_CASE__ : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE__ : Union[str, Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE__ : List[Any] = max_position_embeddings SCREAMING_SNAKE_CASE__ : Optional[int] = eos_token_id SCREAMING_SNAKE_CASE__ : Any = pad_token_id SCREAMING_SNAKE_CASE__ : Optional[int] = bos_token_id def __lowercase( self : Optional[int] )-> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Dict = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) SCREAMING_SNAKE_CASE__ : Tuple = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) SCREAMING_SNAKE_CASE__ : Optional[Any] = tf.concat([input_ids, eos_tensor] , axis=1 ) SCREAMING_SNAKE_CASE__ : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE__ : List[Any] = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) SCREAMING_SNAKE_CASE__ : List[Any] = prepare_blenderbot_small_inputs_dict(a_ , a_ , a_ ) return config, inputs_dict def __lowercase( self : Dict , a_ : Dict , a_ : str )-> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[Any] = TFBlenderbotSmallModel(config=a_ ).get_decoder() SCREAMING_SNAKE_CASE__ : Optional[int] = inputs_dict['input_ids'] SCREAMING_SNAKE_CASE__ : Union[str, Any] = input_ids[:1, :] SCREAMING_SNAKE_CASE__ : List[str] = inputs_dict['attention_mask'][:1, :] SCREAMING_SNAKE_CASE__ : Any = inputs_dict['head_mask'] SCREAMING_SNAKE_CASE__ : str = 1 # first forward pass SCREAMING_SNAKE_CASE__ : Optional[int] = model(a_ , attention_mask=a_ , head_mask=a_ , use_cache=a_ ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : List[Any] = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids SCREAMING_SNAKE_CASE__ : Optional[Any] = ids_tensor((self.batch_size, 3) , config.vocab_size ) SCREAMING_SNAKE_CASE__ : List[Any] = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and SCREAMING_SNAKE_CASE__ : List[str] = tf.concat([input_ids, next_tokens] , axis=-1 ) SCREAMING_SNAKE_CASE__ : Optional[int] = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = model(a_ , attention_mask=a_ )[0] SCREAMING_SNAKE_CASE__ : int = model(a_ , attention_mask=a_ , past_key_values=a_ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice SCREAMING_SNAKE_CASE__ : Optional[Any] = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) SCREAMING_SNAKE_CASE__ : Any = output_from_no_past[:, -3:, random_slice_idx] SCREAMING_SNAKE_CASE__ : Union[str, Any] = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(a_ , a_ , rtol=1e-3 ) def _a ( lowercase__ : Any , lowercase__ : Any , lowercase__ : List[str] , lowercase__ : Any=None , lowercase__ : str=None , lowercase__ : int=None , lowercase__ : str=None , lowercase__ : Tuple=None , ): '''simple docstring''' if attention_mask is None: SCREAMING_SNAKE_CASE__ : Union[str, Any] = tf.cast(tf.math.not_equal(lowercase__ , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: SCREAMING_SNAKE_CASE__ : List[Any] = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: SCREAMING_SNAKE_CASE__ : Tuple = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: SCREAMING_SNAKE_CASE__ : List[str] = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: SCREAMING_SNAKE_CASE__ : Tuple = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class snake_case ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase ): lowercase_ = ( (TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel) if is_tf_available() else () ) lowercase_ = (TFBlenderbotSmallForConditionalGeneration,) if is_tf_available() else () lowercase_ = ( { 'conversational': TFBlenderbotSmallForConditionalGeneration, 'feature-extraction': TFBlenderbotSmallModel, 'summarization': TFBlenderbotSmallForConditionalGeneration, 'text2text-generation': TFBlenderbotSmallForConditionalGeneration, 'translation': TFBlenderbotSmallForConditionalGeneration, } if is_tf_available() else {} ) lowercase_ = True lowercase_ = False lowercase_ = False def __lowercase( self : Any )-> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[Any] = TFBlenderbotSmallModelTester(self ) SCREAMING_SNAKE_CASE__ : Dict = ConfigTester(self , config_class=a_ ) def __lowercase( self : Optional[Any] )-> Dict: """simple docstring""" self.config_tester.run_common_tests() def __lowercase( self : List[str] )-> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*a_ ) @require_tokenizers @require_tf class snake_case ( unittest.TestCase ): lowercase_ = [ 'Social anxiety\nWow, I am never shy. Do you have anxiety?\nYes. I end up sweating and blushing and feel like ' ' i\'m going to throw up.\nand why is that?' ] lowercase_ = 'facebook/blenderbot_small-90M' @cached_property def __lowercase( self : int )-> Any: """simple docstring""" # use "old" tokenizer here because of bug when downloading new tokenizer return BlenderbotSmallTokenizer.from_pretrained('facebook/blenderbot-90M' ) @cached_property def __lowercase( self : Optional[int] )-> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[Any] = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def __lowercase( self : str )-> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Any = self.tokenizer(self.src_text , return_tensors='tf' ) SCREAMING_SNAKE_CASE__ : int = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 , use_cache=a_ , ) SCREAMING_SNAKE_CASE__ : Dict = self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=a_ )[0] assert generated_words in ( "i don't know. i just feel like i'm going to throw up. it's not fun.", "i'm not sure. i just feel like i've been feeling like i have to be in a certain place", "i'm not sure. i just feel like i've been in a bad situation.", )
85
'''simple docstring''' from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def _UpperCamelCase (_lowerCamelCase : Union[str, Any] , _lowerCamelCase : List[Any] )-> Optional[Any]: '''simple docstring''' __snake_case = [] for part_id in partition_order: __snake_case = df.where(f'''SPARK_PARTITION_ID() = {part_id}''' ).collect() for row_idx, row in enumerate(_lowerCamelCase ): expected_row_ids_and_row_dicts.append((f'''{part_id}_{row_idx}''', row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Any: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(1_00 ).repartition(1 ) __snake_case = Spark(_lowerCamelCase ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(10 ).repartition(2 ) __snake_case = [1, 0] __snake_case = _generate_iterable_examples(_lowerCamelCase , _lowerCamelCase ) # Reverse the partitions. __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , _lowerCamelCase ) for i, (row_id, row_dict) in enumerate(generate_fn() ): __snake_case , __snake_case = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> int: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(10 ).repartition(1 ) __snake_case = SparkExamplesIterable(_lowerCamelCase ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): assert row_id == f'''0_{i}''' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Union[str, Any]: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''' ) as generator_mock: __snake_case = lambda _lowerCamelCase : x.reverse() __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , [2, 1, 0] ) __snake_case = SparkExamplesIterable(_lowerCamelCase ).shuffle_data_sources(_lowerCamelCase ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): __snake_case , __snake_case = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 __snake_case = SparkExamplesIterable(_lowerCamelCase ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , [0, 2] ) for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): __snake_case , __snake_case = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 __snake_case = SparkExamplesIterable(_lowerCamelCase ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , [1, 3] ) for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): __snake_case , __snake_case = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Optional[int]: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(1_00 ).repartition(1 ) __snake_case = Spark(_lowerCamelCase ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 1_00
24
0
def __snake_case ( __UpperCamelCase : List[Any] ): """simple docstring""" if collection == []: return [] # get some information about the collection A_ = len(__UpperCamelCase ) A_ = max(__UpperCamelCase ) A_ = min(__UpperCamelCase ) # create the counting array A_ = coll_max + 1 - coll_min A_ = [0] * counting_arr_length # count how much a number appears in the collection for number in collection: counting_arr[number - coll_min] += 1 # sum each position with it's predecessors. now, counting_arr[i] tells # us how many elements <= i has in the collection for i in range(1 ,__UpperCamelCase ): A_ = counting_arr[i] + counting_arr[i - 1] # create the output collection A_ = [0] * coll_len # place the elements in the output, respecting the original order (stable # sort) from end to begin, updating counting_arr for i in reversed(range(0 ,__UpperCamelCase ) ): A_ = collection[i] counting_arr[collection[i] - coll_min] -= 1 return ordered def __snake_case ( __UpperCamelCase : Optional[int] ): """simple docstring""" return "".join([chr(__UpperCamelCase ) for i in counting_sort([ord(__UpperCamelCase ) for c in string] )] ) if __name__ == "__main__": # Test string sort assert counting_sort_string('thisisthestring') == "eghhiiinrsssttt" __a :Tuple = input('Enter numbers separated by a comma:\n').strip() __a :Dict = [int(item) for item in user_input.split(',')] print(counting_sort(unsorted))
86
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int , _lowerCamelCase : int , _lowerCamelCase : int )-> float: '''simple docstring''' __snake_case = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) # formula for sum of series return total def _UpperCamelCase ()-> str: '''simple docstring''' print(sum_of_series(1 , 1 , 10 ) ) if __name__ == "__main__": import doctest doctest.testmod()
24
0
import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE ( lowercase_ ) -> Optional[Any]: """simple docstring""" A__ = torch.load(lowercase_ , map_location='''cpu''' ) if "model" in sd.keys(): A__ = torch.load(lowercase_ , map_location='''cpu''' )['''model'''] # pop unnecessary weights A__ = [ '''decoder.version''', '''decoder.output_projection.weight''', ] for key in keys_to_delete: if key in sd: sd.pop(lowercase_ ) A__ = { '''decoder.project_in_dim.weight''': '''decoder.project_in.weight''', '''decoder.project_out_dim.weight''': '''decoder.project_out.weight''', '''decoder.layer_norm.weight''': '''decoder.final_layer_norm.weight''', '''decoder.layer_norm.bias''': '''decoder.final_layer_norm.bias''', } for old_key, new_key in keys_to_rename.items(): if old_key in sd: A__ = sd.pop(lowercase_ ) A__ = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: A__ = sd[key] # We split QKV in separate Q,K,V A__ = key.replace('''.qkv_proj.''' , '''.q_proj.''' ) A__ = key.replace('''.qkv_proj.''' , '''.k_proj.''' ) A__ = key.replace('''.qkv_proj.''' , '''.v_proj.''' ) A__ = value.shape[0] assert depth % 3 == 0 # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming: # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97 A__ , A__ , A__ = torch.split(lowercase_ , depth // 3 , dim=0 ) A__ = q A__ = k A__ = v del sd[key] return sd @torch.no_grad() def SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ , lowercase_=None ) -> Any: """simple docstring""" A__ = load_checkpoint(lowercase_ ) if config is not None: A__ = OPTConfig.from_pretrained(lowercase_ ) else: A__ = OPTConfig() A__ = OPTModel(lowercase_ ).half().eval() model.load_state_dict(lowercase_ ) # Check results Path(lowercase_ ).mkdir(exist_ok=lowercase_ ) model.save_pretrained(lowercase_ ) if __name__ == "__main__": _lowerCamelCase : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--fairseq_path""", type=str, help=( """path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:""" """ https://huggingface.co/models?other=opt_metasq""" ), ) parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--hf_config""", default=None, type=str, help="""Define HF config.""") _lowerCamelCase : Tuple = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
87
'''simple docstring''' import asyncio import os import re import sys import tempfile import unittest from contextlib import contextmanager from copy import deepcopy from distutils.util import strtobool from enum import Enum from importlib.util import find_spec from pathlib import Path from unittest.mock import patch import pyarrow as pa import pytest import requests from packaging import version from datasets import config if config.PY_VERSION < version.parse('''3.8'''): import importlib_metadata else: import importlib.metadata as importlib_metadata def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : Tuple=False )-> Union[str, Any]: '''simple docstring''' try: __snake_case = os.environ[key] except KeyError: # KEY isn't set, default to `default`. __snake_case = default else: # KEY is set, convert it to True or False. try: __snake_case = strtobool(_lowerCamelCase ) except ValueError: # More values are supported, but let's keep the message simple. raise ValueError(f'''If set, {key} must be yes or no.''' ) return _value UpperCAmelCase_ : Optional[Any] = parse_flag_from_env('''RUN_SLOW''', default=False) UpperCAmelCase_ : Union[str, Any] = parse_flag_from_env('''RUN_REMOTE''', default=False) UpperCAmelCase_ : Optional[Any] = parse_flag_from_env('''RUN_LOCAL''', default=True) UpperCAmelCase_ : Union[str, Any] = parse_flag_from_env('''RUN_PACKAGED''', default=True) # Compression UpperCAmelCase_ : Dict = pytest.mark.skipif(not config.LZ4_AVAILABLE, reason='''test requires lz4''') UpperCAmelCase_ : int = pytest.mark.skipif(not config.PY7ZR_AVAILABLE, reason='''test requires py7zr''') UpperCAmelCase_ : Tuple = pytest.mark.skipif(not config.ZSTANDARD_AVAILABLE, reason='''test requires zstandard''') # Audio UpperCAmelCase_ : str = pytest.mark.skipif( # On Windows and OS X, soundfile installs sndfile find_spec('''soundfile''') is None or version.parse(importlib_metadata.version('''soundfile''')) < version.parse('''0.12.0'''), reason='''test requires sndfile>=0.12.1: \'pip install \"soundfile>=0.12.1\"\'; ''', ) # Beam UpperCAmelCase_ : Tuple = pytest.mark.skipif( not config.BEAM_AVAILABLE or config.DILL_VERSION >= version.parse('''0.3.2'''), reason='''test requires apache-beam and a compatible dill version''', ) # Dill-cloudpickle compatibility UpperCAmelCase_ : Union[str, Any] = pytest.mark.skipif( config.DILL_VERSION <= version.parse('''0.3.2'''), reason='''test requires dill>0.3.2 for cloudpickle compatibility''', ) # Windows UpperCAmelCase_ : int = pytest.mark.skipif( sys.platform == '''win32''', reason='''test should not be run on Windows''', ) def _UpperCamelCase (_lowerCamelCase : List[str] )-> List[Any]: '''simple docstring''' try: import faiss # noqa except ImportError: __snake_case = unittest.skip('''test requires faiss''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> List[str]: '''simple docstring''' try: import regex # noqa except ImportError: __snake_case = unittest.skip('''test requires regex''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> Union[str, Any]: '''simple docstring''' try: import elasticsearch # noqa except ImportError: __snake_case = unittest.skip('''test requires elasticsearch''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Dict )-> List[Any]: '''simple docstring''' try: import sqlalchemy # noqa except ImportError: __snake_case = unittest.skip('''test requires sqlalchemy''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : List[str] )-> List[str]: '''simple docstring''' if not config.TORCH_AVAILABLE: __snake_case = unittest.skip('''test requires PyTorch''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Optional[Any] )-> Dict: '''simple docstring''' if not config.TF_AVAILABLE: __snake_case = unittest.skip('''test requires TensorFlow''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : int )-> Any: '''simple docstring''' if not config.JAX_AVAILABLE: __snake_case = unittest.skip('''test requires JAX''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Tuple )-> str: '''simple docstring''' if not config.PIL_AVAILABLE: __snake_case = unittest.skip('''test requires Pillow''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Dict )-> Any: '''simple docstring''' try: import transformers # noqa F401 except ImportError: return unittest.skip('''test requires transformers''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : int )-> Tuple: '''simple docstring''' try: import tiktoken # noqa F401 except ImportError: return unittest.skip('''test requires tiktoken''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : Dict )-> str: '''simple docstring''' try: import spacy # noqa F401 except ImportError: return unittest.skip('''test requires spacy''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : int )-> Dict: '''simple docstring''' def _require_spacy_model(_lowerCamelCase : int ): try: import spacy # noqa F401 spacy.load(_lowerCamelCase ) except ImportError: return unittest.skip('''test requires spacy''' )(_lowerCamelCase ) except OSError: return unittest.skip('''test requires spacy model \'{}\''''.format(_lowerCamelCase ) )(_lowerCamelCase ) else: return test_case return _require_spacy_model def _UpperCamelCase (_lowerCamelCase : str )-> Dict: '''simple docstring''' try: import pyspark # noqa F401 except ImportError: return unittest.skip('''test requires pyspark''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : Tuple )-> str: '''simple docstring''' try: import joblibspark # noqa F401 except ImportError: return unittest.skip('''test requires joblibspark''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : Any )-> int: '''simple docstring''' if not _run_slow_tests or _run_slow_tests == 0: __snake_case = unittest.skip('''test is slow''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Any )-> Optional[Any]: '''simple docstring''' if not _run_local_tests or _run_local_tests == 0: __snake_case = unittest.skip('''test is local''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : str )-> int: '''simple docstring''' if not _run_packaged_tests or _run_packaged_tests == 0: __snake_case = unittest.skip('''test is packaged''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Optional[int] )-> str: '''simple docstring''' if not _run_remote_tests or _run_remote_tests == 0: __snake_case = unittest.skip('''test requires remote''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (*_lowerCamelCase : str )-> Optional[int]: '''simple docstring''' def decorate(cls : Optional[Any] ): for name, fn in cls.__dict__.items(): if callable(_lowerCamelCase ) and name.startswith('''test''' ): for decorator in decorators: __snake_case = decorator(_lowerCamelCase ) setattr(cls , _lowerCamelCase , _lowerCamelCase ) return cls return decorate class lowerCAmelCase ( __lowerCAmelCase): pass class lowerCAmelCase ( __lowerCAmelCase): __lowercase : List[str] = 0 __lowercase : Dict = 1 __lowercase : List[Any] = 2 @contextmanager def _UpperCamelCase (_lowerCamelCase : Dict=OfflineSimulationMode.CONNECTION_FAILS , _lowerCamelCase : Optional[int]=1E-16 )-> Tuple: '''simple docstring''' __snake_case = requests.Session().request def timeout_request(_lowerCamelCase : Any , _lowerCamelCase : str , _lowerCamelCase : str , **_lowerCamelCase : Any ): # Change the url to an invalid url so that the connection hangs __snake_case = '''https://10.255.255.1''' if kwargs.get('''timeout''' ) is None: raise RequestWouldHangIndefinitelyError( f'''Tried a call to {url} in offline mode with no timeout set. Please set a timeout.''' ) __snake_case = timeout try: return online_request(_lowerCamelCase , _lowerCamelCase , **_lowerCamelCase ) except Exception as e: # The following changes in the error are just here to make the offline timeout error prettier __snake_case = url __snake_case = e.args[0] __snake_case = (max_retry_error.args[0].replace('''10.255.255.1''' , f'''OfflineMock[{url}]''' ),) __snake_case = (max_retry_error,) raise def raise_connection_error(_lowerCamelCase : Union[str, Any] , _lowerCamelCase : Optional[int] , **_lowerCamelCase : Dict ): raise requests.ConnectionError('''Offline mode is enabled.''' , request=_lowerCamelCase ) if mode is OfflineSimulationMode.CONNECTION_FAILS: with patch('''requests.Session.send''' , _lowerCamelCase ): yield elif mode is OfflineSimulationMode.CONNECTION_TIMES_OUT: # inspired from https://stackoverflow.com/a/904609 with patch('''requests.Session.request''' , _lowerCamelCase ): yield elif mode is OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1: with patch('''datasets.config.HF_DATASETS_OFFLINE''' , _lowerCamelCase ): yield else: raise ValueError('''Please use a value from the OfflineSimulationMode enum.''' ) @contextmanager def _UpperCamelCase (*_lowerCamelCase : Union[str, Any] , **_lowerCamelCase : List[str] )-> Any: '''simple docstring''' __snake_case = str(Path().resolve() ) with tempfile.TemporaryDirectory(*_lowerCamelCase , **_lowerCamelCase ) as tmp_dir: try: os.chdir(_lowerCamelCase ) yield finally: os.chdir(_lowerCamelCase ) @contextmanager def _UpperCamelCase ()-> Optional[int]: '''simple docstring''' import gc gc.collect() __snake_case = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory > 0, "Arrow memory didn't increase." @contextmanager def _UpperCamelCase ()-> List[Any]: '''simple docstring''' import gc gc.collect() __snake_case = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory <= 0, "Arrow memory wasn't expected to increase." def _UpperCamelCase (_lowerCamelCase : Any , _lowerCamelCase : int )-> Any: '''simple docstring''' return deepcopy(_lowerCamelCase ).integers(0 , 1_00 , 10 ).tolist() == deepcopy(_lowerCamelCase ).integers(0 , 1_00 , 10 ).tolist() def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> List[Any]: '''simple docstring''' import decorator from requests.exceptions import HTTPError def _wrapper(_lowerCamelCase : int , *_lowerCamelCase : int , **_lowerCamelCase : Optional[int] ): try: return func(*_lowerCamelCase , **_lowerCamelCase ) except HTTPError as err: if str(_lowerCamelCase ).startswith('''500''' ) or str(_lowerCamelCase ).startswith('''502''' ): pytest.xfail(str(_lowerCamelCase ) ) raise err return decorator.decorator(_wrapper , _lowerCamelCase ) class lowerCAmelCase : def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' __snake_case = returncode __snake_case = stdout __snake_case = stderr async def _UpperCamelCase (_lowerCamelCase : List[str] , _lowerCamelCase : Union[str, Any] )-> Dict: '''simple docstring''' while True: __snake_case = await stream.readline() if line: callback(_lowerCamelCase ) else: break async def _UpperCamelCase (_lowerCamelCase : Tuple , _lowerCamelCase : Optional[int]=None , _lowerCamelCase : Optional[Any]=None , _lowerCamelCase : Tuple=None , _lowerCamelCase : Dict=False , _lowerCamelCase : List[Any]=False )-> _RunOutput: '''simple docstring''' if echo: print('''\nRunning: ''' , ''' '''.join(_lowerCamelCase ) ) __snake_case = await asyncio.create_subprocess_exec( cmd[0] , *cmd[1:] , stdin=_lowerCamelCase , stdout=asyncio.subprocess.PIPE , stderr=asyncio.subprocess.PIPE , env=_lowerCamelCase , ) # note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe # https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait # # If it starts hanging, will need to switch to the following code. The problem is that no data # will be seen until it's done and if it hangs for example there will be no debug info. # out, err = await p.communicate() # return _RunOutput(p.returncode, out, err) __snake_case = [] __snake_case = [] def tee(_lowerCamelCase : int , _lowerCamelCase : List[Any] , _lowerCamelCase : str , _lowerCamelCase : Dict="" ): __snake_case = line.decode('''utf-8''' ).rstrip() sink.append(_lowerCamelCase ) if not quiet: print(_lowerCamelCase , _lowerCamelCase , file=_lowerCamelCase ) # XXX: the timeout doesn't seem to make any difference here await asyncio.wait( [ _read_stream(p.stdout , lambda _lowerCamelCase : tee(_lowerCamelCase , _lowerCamelCase , sys.stdout , label='''stdout:''' ) ), _read_stream(p.stderr , lambda _lowerCamelCase : tee(_lowerCamelCase , _lowerCamelCase , sys.stderr , label='''stderr:''' ) ), ] , timeout=_lowerCamelCase , ) return _RunOutput(await p.wait() , _lowerCamelCase , _lowerCamelCase ) def _UpperCamelCase (_lowerCamelCase : Optional[Any] , _lowerCamelCase : Any=None , _lowerCamelCase : List[str]=None , _lowerCamelCase : Optional[Any]=1_80 , _lowerCamelCase : Dict=False , _lowerCamelCase : int=True )-> _RunOutput: '''simple docstring''' __snake_case = asyncio.get_event_loop() __snake_case = loop.run_until_complete( _stream_subprocess(_lowerCamelCase , env=_lowerCamelCase , stdin=_lowerCamelCase , timeout=_lowerCamelCase , quiet=_lowerCamelCase , echo=_lowerCamelCase ) ) __snake_case = ''' '''.join(_lowerCamelCase ) if result.returncode > 0: __snake_case = '''\n'''.join(result.stderr ) raise RuntimeError( f'''\'{cmd_str}\' failed with returncode {result.returncode}\n\n''' f'''The combined stderr from workers follows:\n{stderr}''' ) # check that the subprocess actually did run and produced some output, should the test rely on # the remote side to do the testing if not result.stdout and not result.stderr: raise RuntimeError(f'''\'{cmd_str}\' produced no output.''' ) return result def _UpperCamelCase ()-> Dict: '''simple docstring''' __snake_case = os.environ.get('''PYTEST_XDIST_WORKER''' , '''gw0''' ) __snake_case = re.sub(R'''^gw''' , '''''' , _lowerCamelCase , 0 , re.M ) return int(_lowerCamelCase ) def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = 2_95_00 __snake_case = pytest_xdist_worker_id() return port + uniq_delta
24
0
"""simple docstring""" import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging UpperCAmelCase = logging.get_logger(__name__) UpperCAmelCase = """▁""" UpperCAmelCase = { """vocab_file""": """vocab.json""", """spm_file""": """sentencepiece.bpe.model""", """tokenizer_config_file""": """tokenizer_config.json""", } UpperCAmelCase = { """vocab_file""": { """facebook/m2m100_418M""": """https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json""", """facebook/m2m100_1.2B""": """https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json""", }, """spm_file""": { """facebook/m2m100_418M""": """https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model""", """facebook/m2m100_1.2B""": """https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model""", }, """tokenizer_config_file""": { """facebook/m2m100_418M""": """https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json""", """facebook/m2m100_1.2B""": """https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json""", }, } UpperCAmelCase = { """facebook/m2m100_418M""": 1024, } # fmt: off UpperCAmelCase = { """m2m100""": ["""af""", """am""", """ar""", """ast""", """az""", """ba""", """be""", """bg""", """bn""", """br""", """bs""", """ca""", """ceb""", """cs""", """cy""", """da""", """de""", """el""", """en""", """es""", """et""", """fa""", """ff""", """fi""", """fr""", """fy""", """ga""", """gd""", """gl""", """gu""", """ha""", """he""", """hi""", """hr""", """ht""", """hu""", """hy""", """id""", """ig""", """ilo""", """is""", """it""", """ja""", """jv""", """ka""", """kk""", """km""", """kn""", """ko""", """lb""", """lg""", """ln""", """lo""", """lt""", """lv""", """mg""", """mk""", """ml""", """mn""", """mr""", """ms""", """my""", """ne""", """nl""", """no""", """ns""", """oc""", """or""", """pa""", """pl""", """ps""", """pt""", """ro""", """ru""", """sd""", """si""", """sk""", """sl""", """so""", """sq""", """sr""", """ss""", """su""", """sv""", """sw""", """ta""", """th""", """tl""", """tn""", """tr""", """uk""", """ur""", """uz""", """vi""", """wo""", """xh""", """yi""", """yo""", """zh""", """zu"""], """wmt21""": ["""en""", """ha""", """is""", """ja""", """cs""", """ru""", """zh""", """de"""] } class lowercase__ ( A_ ): __UpperCAmelCase = VOCAB_FILES_NAMES __UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCAmelCase = ['''input_ids''', '''attention_mask'''] __UpperCAmelCase = [] __UpperCAmelCase = [] def __init__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=None , SCREAMING_SNAKE_CASE=None , SCREAMING_SNAKE_CASE="<s>" , SCREAMING_SNAKE_CASE="</s>" , SCREAMING_SNAKE_CASE="</s>" , SCREAMING_SNAKE_CASE="<pad>" , SCREAMING_SNAKE_CASE="<unk>" , SCREAMING_SNAKE_CASE="m2m100" , SCREAMING_SNAKE_CASE = None , SCREAMING_SNAKE_CASE=8 , **SCREAMING_SNAKE_CASE , ) -> None: _lowerCamelCase : List[Any] = {} if sp_model_kwargs is None else sp_model_kwargs _lowerCamelCase : List[Any] = language_codes _lowerCamelCase : List[Any] = FAIRSEQ_LANGUAGE_CODES[language_codes] _lowerCamelCase : str = {lang_code: F'__{lang_code}__' for lang_code in fairseq_language_code} _lowerCamelCase : Optional[Any] = kwargs.get("""additional_special_tokens""" , []) kwargs["additional_special_tokens"] += [ self.get_lang_token(SCREAMING_SNAKE_CASE) for lang_code in fairseq_language_code if self.get_lang_token(SCREAMING_SNAKE_CASE) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=SCREAMING_SNAKE_CASE , tgt_lang=SCREAMING_SNAKE_CASE , bos_token=SCREAMING_SNAKE_CASE , eos_token=SCREAMING_SNAKE_CASE , sep_token=SCREAMING_SNAKE_CASE , unk_token=SCREAMING_SNAKE_CASE , pad_token=SCREAMING_SNAKE_CASE , language_codes=SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE , ) _lowerCamelCase : Union[str, Any] = vocab_file _lowerCamelCase : str = load_json(SCREAMING_SNAKE_CASE) _lowerCamelCase : List[Any] = {v: k for k, v in self.encoder.items()} _lowerCamelCase : Optional[int] = spm_file _lowerCamelCase : Union[str, Any] = load_spm(SCREAMING_SNAKE_CASE , self.sp_model_kwargs) _lowerCamelCase : Optional[int] = len(self.encoder) _lowerCamelCase : Dict = { self.get_lang_token(SCREAMING_SNAKE_CASE): self.encoder_size + i for i, lang_code in enumerate(SCREAMING_SNAKE_CASE) } _lowerCamelCase : List[Any] = {lang_code: self.encoder_size + i for i, lang_code in enumerate(SCREAMING_SNAKE_CASE)} _lowerCamelCase : str = {v: k for k, v in self.lang_token_to_id.items()} _lowerCamelCase : List[str] = src_lang if src_lang is not None else """en""" _lowerCamelCase : List[str] = tgt_lang _lowerCamelCase : List[str] = self.get_lang_id(self._src_lang) self.set_src_lang_special_tokens(self._src_lang) _lowerCamelCase : Dict = num_madeup_words @property def UpperCamelCase_ ( self) -> int: return len(self.encoder) + len(self.lang_token_to_id) @property def UpperCamelCase_ ( self) -> str: return self._src_lang @src_lang.setter def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE) -> None: _lowerCamelCase : Dict = new_src_lang self.set_src_lang_special_tokens(self._src_lang) def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE) -> List[str]: return self.sp_model.encode(SCREAMING_SNAKE_CASE , out_type=SCREAMING_SNAKE_CASE) def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE) -> List[str]: if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(SCREAMING_SNAKE_CASE , self.encoder[self.unk_token]) def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE) -> str: if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(SCREAMING_SNAKE_CASE , self.unk_token) def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE) -> Dict: _lowerCamelCase : Tuple = [] _lowerCamelCase : Optional[int] = """""" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(SCREAMING_SNAKE_CASE) + token _lowerCamelCase : Tuple = [] else: current_sub_tokens.append(SCREAMING_SNAKE_CASE) out_string += self.sp_model.decode(SCREAMING_SNAKE_CASE) return out_string.strip() def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = None , SCREAMING_SNAKE_CASE = False) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=SCREAMING_SNAKE_CASE , token_ids_a=SCREAMING_SNAKE_CASE , already_has_special_tokens=SCREAMING_SNAKE_CASE) _lowerCamelCase : Union[str, Any] = [1] * len(self.prefix_tokens) _lowerCamelCase : Dict = [1] * len(self.suffix_tokens) if token_ids_a is None: return prefix_ones + ([0] * len(SCREAMING_SNAKE_CASE)) + suffix_ones return prefix_ones + ([0] * len(SCREAMING_SNAKE_CASE)) + ([0] * len(SCREAMING_SNAKE_CASE)) + suffix_ones def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = None) -> List[int]: if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def UpperCamelCase_ ( self) -> Dict: _lowerCamelCase : Dict = {self.convert_ids_to_tokens(SCREAMING_SNAKE_CASE): i for i in range(self.vocab_size)} vocab.update(self.added_tokens_encoder) return vocab def __getstate__( self) -> Dict: _lowerCamelCase : str = self.__dict__.copy() _lowerCamelCase : List[Any] = None return state def __setstate__( self , SCREAMING_SNAKE_CASE) -> None: _lowerCamelCase : Any = d # for backward compatibility if not hasattr(self , """sp_model_kwargs"""): _lowerCamelCase : int = {} _lowerCamelCase : Any = load_spm(self.spm_file , self.sp_model_kwargs) def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = None) -> Tuple[str]: _lowerCamelCase : Optional[int] = Path(SCREAMING_SNAKE_CASE) if not save_dir.is_dir(): raise OSError(F'{save_directory} should be a directory') _lowerCamelCase : Any = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) _lowerCamelCase : Optional[Any] = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , SCREAMING_SNAKE_CASE) if os.path.abspath(self.spm_file) != os.path.abspath(SCREAMING_SNAKE_CASE) and os.path.isfile(self.spm_file): copyfile(self.spm_file , SCREAMING_SNAKE_CASE) elif not os.path.isfile(self.spm_file): with open(SCREAMING_SNAKE_CASE , """wb""") as fi: _lowerCamelCase : Union[str, Any] = self.sp_model.serialized_model_proto() fi.write(SCREAMING_SNAKE_CASE) return (str(SCREAMING_SNAKE_CASE), str(SCREAMING_SNAKE_CASE)) def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = "en" , SCREAMING_SNAKE_CASE = None , SCREAMING_SNAKE_CASE = "ro" , **SCREAMING_SNAKE_CASE , ) -> BatchEncoding: _lowerCamelCase : List[Any] = src_lang _lowerCamelCase : Union[str, Any] = tgt_lang self.set_src_lang_special_tokens(self.src_lang) return super().prepare_seqaseq_batch(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE) def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE) -> List[str]: if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""") _lowerCamelCase : List[str] = src_lang _lowerCamelCase : str = self(SCREAMING_SNAKE_CASE , add_special_tokens=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE) _lowerCamelCase : List[Any] = self.get_lang_id(SCREAMING_SNAKE_CASE) _lowerCamelCase : str = tgt_lang_id return inputs def UpperCamelCase_ ( self) -> Any: self.set_src_lang_special_tokens(self.src_lang) def UpperCamelCase_ ( self) -> Tuple: self.set_tgt_lang_special_tokens(self.tgt_lang) def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE) -> None: _lowerCamelCase : List[Any] = self.get_lang_token(SCREAMING_SNAKE_CASE) _lowerCamelCase : List[str] = self.lang_token_to_id[lang_token] _lowerCamelCase : str = [self.cur_lang_id] _lowerCamelCase : int = [self.eos_token_id] def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE) -> None: _lowerCamelCase : int = self.get_lang_token(SCREAMING_SNAKE_CASE) _lowerCamelCase : Optional[Any] = self.lang_token_to_id[lang_token] _lowerCamelCase : List[Any] = [self.cur_lang_id] _lowerCamelCase : Tuple = [self.eos_token_id] def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE) -> str: return self.lang_code_to_token[lang] def UpperCamelCase_ ( self , SCREAMING_SNAKE_CASE) -> int: _lowerCamelCase : int = self.get_lang_token(SCREAMING_SNAKE_CASE) return self.lang_token_to_id[lang_token] def _snake_case ( __snake_case : str , __snake_case : Dict[str, Any] ): """simple docstring""" _lowerCamelCase : List[str] = sentencepiece.SentencePieceProcessor(**__snake_case ) spm.Load(str(__snake_case ) ) return spm def _snake_case ( __snake_case : str ): """simple docstring""" with open(__snake_case , """r""" ) as f: return json.load(__snake_case ) def _snake_case ( __snake_case : List[str] , __snake_case : str ): """simple docstring""" with open(__snake_case , """w""" ) as f: json.dump(__snake_case , __snake_case , indent=2 )
88
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' __snake_case = [[0 for _ in range(_lowerCamelCase )] for _ in range(m + 1 )] for i in range(m + 1 ): __snake_case = 1 for n in range(m + 1 ): for k in range(1 , _lowerCamelCase ): 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: UpperCAmelCase_ : List[str] = int(input('''Enter a number: ''').strip()) print(partition(n)) except ValueError: print('''Please enter a number.''') else: try: UpperCAmelCase_ : Union[str, Any] = int(sys.argv[1]) print(partition(n)) except ValueError: print('''Please pass a number.''')
24
0
def UpperCamelCase_( lowerCamelCase_ ) -> str: if number > 0: raise ValueError('input must be a negative integer' ) _lowercase : Any = len(bin(lowerCamelCase_ )[3:] ) _lowercase : List[Any] = bin(abs(lowerCamelCase_ ) - (1 << binary_number_length) )[3:] _lowercase : Optional[int] = ( ( '1' + '0' * (binary_number_length - len(lowerCamelCase_ )) + twos_complement_number ) if number < 0 else '0' ) return "0b" + twos_complement_number if __name__ == "__main__": import doctest doctest.testmod()
89
'''simple docstring''' import argparse import os import re UpperCAmelCase_ : List[str] = '''src/transformers/models/auto''' # re pattern that matches mapping introductions: # SUPER_MODEL_MAPPING_NAMES = OrderedDict or SUPER_MODEL_MAPPING = OrderedDict UpperCAmelCase_ : Tuple = re.compile(R'''[A-Z_]+_MAPPING(\s+|_[A-Z_]+\s+)=\s+OrderedDict''') # re pattern that matches identifiers in mappings UpperCAmelCase_ : Dict = re.compile(R'''\s*\(\s*"(\S[^"]+)"''') def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : bool = False )-> str: '''simple docstring''' with open(_lowerCamelCase , '''r''' , encoding='''utf-8''' ) as f: __snake_case = f.read() __snake_case = content.split('''\n''' ) __snake_case = [] __snake_case = 0 while line_idx < len(_lowerCamelCase ): if _re_intro_mapping.search(lines[line_idx] ) is not None: __snake_case = len(re.search(R'''^(\s*)\S''' , lines[line_idx] ).groups()[0] ) + 8 # Start of a new mapping! while not lines[line_idx].startswith(''' ''' * indent + '''(''' ): new_lines.append(lines[line_idx] ) line_idx += 1 __snake_case = [] while lines[line_idx].strip() != "]": # Blocks either fit in one line or not if lines[line_idx].strip() == "(": __snake_case = line_idx while not lines[line_idx].startswith(''' ''' * indent + ''')''' ): line_idx += 1 blocks.append('''\n'''.join(lines[start_idx : line_idx + 1] ) ) else: blocks.append(lines[line_idx] ) line_idx += 1 # Sort blocks by their identifiers __snake_case = sorted(_lowerCamelCase , key=lambda _lowerCamelCase : _re_identifier.search(_lowerCamelCase ).groups()[0] ) new_lines += blocks else: new_lines.append(lines[line_idx] ) line_idx += 1 if overwrite: with open(_lowerCamelCase , '''w''' , encoding='''utf-8''' ) as f: f.write('''\n'''.join(_lowerCamelCase ) ) elif "\n".join(_lowerCamelCase ) != content: return True def _UpperCamelCase (_lowerCamelCase : bool = False )-> Tuple: '''simple docstring''' __snake_case = [os.path.join(_lowerCamelCase , _lowerCamelCase ) for f in os.listdir(_lowerCamelCase ) if f.endswith('''.py''' )] __snake_case = [sort_auto_mapping(_lowerCamelCase , overwrite=_lowerCamelCase ) for fname in fnames] if not overwrite and any(_lowerCamelCase ): __snake_case = [f for f, d in zip(_lowerCamelCase , _lowerCamelCase ) if d] raise ValueError( f'''The following files have auto mappings that need sorting: {", ".join(_lowerCamelCase )}. Run `make style` to fix''' ''' this.''' ) if __name__ == "__main__": UpperCAmelCase_ : str = argparse.ArgumentParser() parser.add_argument('''--check_only''', action='''store_true''', help='''Whether to only check or fix style.''') UpperCAmelCase_ : List[Any] = parser.parse_args() sort_all_auto_mappings(not args.check_only)
24
0
'''simple docstring''' import argparse import re import requests import torch # git clone https://github.com/salesforce/BLIP.git from models.blip import blip_decoder from models.blip_itm import blip_itm from models.blip_vqa import blip_vqa from PIL import Image from torchvision import transforms from torchvision.transforms.functional import InterpolationMode from transformers import ( BertTokenizer, BlipConfig, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, ) def _snake_case ( A , A ) -> Tuple: lowerCAmelCase__ = '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg''' lowerCAmelCase__ = Image.open(requests.get(A , stream=A ).raw ).convert('''RGB''' ) lowerCAmelCase__ = transforms.Compose( [ transforms.Resize((image_size, image_size) , interpolation=InterpolationMode.BICUBIC ), transforms.ToTensor(), transforms.Normalize((0.48_145_466, 0.4_578_275, 0.40_821_073) , (0.26_862_954, 0.26_130_258, 0.27_577_711) ), ] ) lowerCAmelCase__ = transform(A ).unsqueeze(0 ).to(A ) return image def _snake_case ( A ) -> Optional[int]: if "visual_encoder" in key: lowerCAmelCase__ = re.sub('''visual_encoder*''' , '''vision_model.encoder''' , A ) if "blocks" in key: lowerCAmelCase__ = re.sub(R'''blocks''' , '''layers''' , A ) if "attn" in key: lowerCAmelCase__ = re.sub(R'''attn''' , '''self_attn''' , A ) if "norm1" in key: lowerCAmelCase__ = re.sub(R'''norm1''' , '''layer_norm1''' , A ) if "norm2" in key: lowerCAmelCase__ = re.sub(R'''norm2''' , '''layer_norm2''' , A ) if "encoder.norm" in key: lowerCAmelCase__ = re.sub(R'''encoder.norm''' , '''post_layernorm''' , A ) if "encoder.patch_embed.proj" in key: lowerCAmelCase__ = re.sub(R'''encoder.patch_embed.proj''' , '''embeddings.patch_embedding''' , A ) if "encoder.pos_embed" in key: lowerCAmelCase__ = re.sub(R'''encoder.pos_embed''' , '''embeddings.position_embedding''' , A ) if "encoder.cls_token" in key: lowerCAmelCase__ = re.sub(R'''encoder.cls_token''' , '''embeddings.class_embedding''' , A ) if "self_attn" in key: lowerCAmelCase__ = re.sub(R'''self_attn.proj''' , '''self_attn.projection''' , A ) return key @torch.no_grad() def _snake_case ( A , A=None ) -> str: if config_path is not None: lowerCAmelCase__ = BlipConfig.from_pretrained(A ) else: lowerCAmelCase__ = BlipConfig(projection_dim=512 , text_config={} , vision_config={} ) lowerCAmelCase__ = BlipForConditionalGeneration(A ).eval() lowerCAmelCase__ = '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_capfilt_large.pth''' lowerCAmelCase__ = blip_decoder(pretrained=A , image_size=384 , vit='''base''' ) lowerCAmelCase__ = pt_model.eval() lowerCAmelCase__ = pt_model.state_dict() for key in modified_state_dict.copy(): lowerCAmelCase__ = modified_state_dict.pop(A ) lowerCAmelCase__ = rename_key(A ) lowerCAmelCase__ = value hf_model.load_state_dict(A ) lowerCAmelCase__ = 384 lowerCAmelCase__ = load_demo_image(image_size=A , device='''cpu''' ) lowerCAmelCase__ = BertTokenizer.from_pretrained('''bert-base-uncased''' ) lowerCAmelCase__ = tokenizer(['''a picture of'''] ).input_ids lowerCAmelCase__ = hf_model.generate(A , A ) assert out[0].tolist() == [30522, 1037, 3861, 1997, 1037, 2450, 3564, 2006, 1996, 3509, 2007, 2014, 3899, 102] lowerCAmelCase__ = hf_model.generate(A ) assert out[0].tolist() == [30522, 1037, 2450, 3564, 2006, 1996, 3509, 2007, 2014, 3899, 102] if pytorch_dump_folder_path is not None: hf_model.save_pretrained(A ) # model_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_vqa.pth' lowerCAmelCase__ = ( '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_vqa_capfilt_large.pth''' ) lowerCAmelCase__ = blip_vqa(pretrained=A , image_size=A , vit='''base''' ) vqa_model.eval() lowerCAmelCase__ = vqa_model.state_dict() for key in modified_state_dict.copy(): lowerCAmelCase__ = modified_state_dict.pop(A ) lowerCAmelCase__ = rename_key(A ) lowerCAmelCase__ = value lowerCAmelCase__ = BlipForQuestionAnswering(A ) hf_vqa_model.load_state_dict(A ) lowerCAmelCase__ = ['''How many dogs are in this image?'''] lowerCAmelCase__ = tokenizer(A , return_tensors='''pt''' ).input_ids lowerCAmelCase__ = hf_vqa_model.generate(A , A ) print(tokenizer.decode(answer[0] ) ) assert tokenizer.decode(answer[0] ) == "[UNK] 1 [SEP]" if pytorch_dump_folder_path is not None: hf_vqa_model.save_pretrained(pytorch_dump_folder_path + '''_vqa''' ) lowerCAmelCase__ = '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_retrieval_coco.pth''' lowerCAmelCase__ = blip_itm(pretrained=A , image_size=A , vit='''base''' ) itm_model.eval() lowerCAmelCase__ = itm_model.state_dict() for key in modified_state_dict.copy(): lowerCAmelCase__ = modified_state_dict.pop(A ) lowerCAmelCase__ = rename_key(A ) lowerCAmelCase__ = value lowerCAmelCase__ = BlipForImageTextRetrieval(A ) lowerCAmelCase__ = ['''A picture of a woman with a dog sitting in a beach'''] lowerCAmelCase__ = tokenizer( A , return_tensors='''pt''' , padding='''max_length''' , truncation=A , max_length=35 , ).input_ids hf_itm_model.load_state_dict(A ) hf_itm_model.eval() lowerCAmelCase__ = hf_itm_model(A , A , use_itm_head=A ) lowerCAmelCase__ = hf_itm_model(A , A , use_itm_head=A ) assert out[0].item() == 0.2_110_687_494_277_954 assert torch.nn.functional.softmax(out_itm[0] , dim=1 )[:, 1].item() == 0.45_698_845_386_505_127 if pytorch_dump_folder_path is not None: hf_itm_model.save_pretrained(pytorch_dump_folder_path + '''_itm''' ) if __name__ == "__main__": __UpperCAmelCase = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') __UpperCAmelCase = parser.parse_args() convert_blip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
90
'''simple docstring''' import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _UpperCamelCase (*_lowerCamelCase : str , _lowerCamelCase : Optional[Union[Dict, Any]] = None , _lowerCamelCase : List[Any]=True , _lowerCamelCase : str=2 )-> str: '''simple docstring''' from .. import __version__ __snake_case = take_from __snake_case = () if not isinstance(args[0] , _lowerCamelCase ): __snake_case = (args,) for attribute, version_name, message in args: if version.parse(version.parse(_lowerCamelCase ).base_version ) >= version.parse(_lowerCamelCase ): raise ValueError( f'''The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\'''' f''' version {__version__} is >= {version_name}''' ) __snake_case = None if isinstance(_lowerCamelCase , _lowerCamelCase ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(_lowerCamelCase ),) __snake_case = f'''The `{attribute}` argument is deprecated and will be removed in version {version_name}.''' elif hasattr(_lowerCamelCase , _lowerCamelCase ): values += (getattr(_lowerCamelCase , _lowerCamelCase ),) __snake_case = f'''The `{attribute}` attribute is deprecated and will be removed in version {version_name}.''' elif deprecated_kwargs is None: __snake_case = f'''`{attribute}` is deprecated and will be removed in version {version_name}.''' if warning is not None: __snake_case = warning + ''' ''' if standard_warn else '''''' warnings.warn(warning + message , _lowerCamelCase , stacklevel=_lowerCamelCase ) if isinstance(_lowerCamelCase , _lowerCamelCase ) and len(_lowerCamelCase ) > 0: __snake_case = inspect.getouterframes(inspect.currentframe() )[1] __snake_case = call_frame.filename __snake_case = call_frame.lineno __snake_case = call_frame.function __snake_case , __snake_case = next(iter(deprecated_kwargs.items() ) ) raise TypeError(f'''{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`''' ) if len(_lowerCamelCase ) == 0: return elif len(_lowerCamelCase ) == 1: return values[0] return values
24
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 _lowercase = logging.get_logger(__name__) _lowercase = { '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''', '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class lowerCAmelCase_ ( _lowercase ): '''simple docstring''' _lowerCamelCase: Optional[Any] = '''mobilenet_v1''' def __init__( self : Optional[int] ,A_ : Optional[int]=3 ,A_ : Any=224 ,A_ : List[Any]=1.0 ,A_ : Union[str, Any]=8 ,A_ : Union[str, Any]="relu6" ,A_ : Optional[Any]=True ,A_ : List[str]=0.9_99 ,A_ : int=0.02 ,A_ : int=0.0_01 ,**A_ : Union[str, Any] ,) -> Dict: super().__init__(**A_ ) if depth_multiplier <= 0: raise ValueError('depth_multiplier must be greater than zero.' ) A = num_channels A = image_size A = depth_multiplier A = min_depth A = hidden_act A = tf_padding A = classifier_dropout_prob A = initializer_range A = layer_norm_eps class lowerCAmelCase_ ( _lowercase ): '''simple docstring''' _lowerCamelCase: Optional[int] = version.parse('''1.11''' ) @property def _SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Mapping[str, Mapping[int, str]]: return OrderedDict([('pixel_values', {0: 'batch'})] ) @property def _SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Mapping[str, Mapping[int, str]]: if self.task == "image-classification": return OrderedDict([('logits', {0: 'batch'})] ) else: return OrderedDict([('last_hidden_state', {0: 'batch'}), ('pooler_output', {0: 'batch'})] ) @property def _SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> float: return 1e-4
91
'''simple docstring''' import argparse import re from pathlib import Path import requests import torch from PIL import Image from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor from transformers import ( EfficientFormerConfig, EfficientFormerForImageClassificationWithTeacher, EfficientFormerImageProcessor, ) from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def _UpperCamelCase (_lowerCamelCase : int , _lowerCamelCase : str )-> List[str]: '''simple docstring''' __snake_case = old_name if "patch_embed" in old_name: __snake_case , __snake_case , __snake_case = old_name.split('''.''' ) if layer == "0": __snake_case = old_name.replace('''0''' , '''convolution1''' ) elif layer == "1": __snake_case = old_name.replace('''1''' , '''batchnorm_before''' ) elif layer == "3": __snake_case = old_name.replace('''3''' , '''convolution2''' ) else: __snake_case = old_name.replace('''4''' , '''batchnorm_after''' ) if "network" in old_name and re.search(R'''\d\.\d''' , _lowerCamelCase ): __snake_case = R'''\b\d{2}\b''' if bool(re.search(_lowerCamelCase , _lowerCamelCase ) ): __snake_case = re.search(R'''\d\.\d\d.''' , _lowerCamelCase ).group() else: __snake_case = re.search(R'''\d\.\d.''' , _lowerCamelCase ).group() if int(match[0] ) < 6: __snake_case = old_name.replace(_lowerCamelCase , '''''' ) __snake_case = trimmed_name.replace('''network''' , match[0] + '''.meta4D_layers.blocks.''' + match[2:-1] ) __snake_case = '''intermediate_stages.''' + trimmed_name else: __snake_case = old_name.replace(_lowerCamelCase , '''''' ) if int(match[2] ) < num_meta4D_last_stage: __snake_case = trimmed_name.replace('''network''' , '''meta4D_layers.blocks.''' + match[2] ) else: __snake_case = str(int(match[2] ) - num_meta4D_last_stage ) __snake_case = trimmed_name.replace('''network''' , '''meta3D_layers.blocks.''' + layer_index ) if "norm1" in old_name: __snake_case = trimmed_name.replace('''norm1''' , '''layernorm1''' ) elif "norm2" in old_name: __snake_case = trimmed_name.replace('''norm2''' , '''layernorm2''' ) elif "fc1" in old_name: __snake_case = trimmed_name.replace('''fc1''' , '''linear_in''' ) elif "fc2" in old_name: __snake_case = trimmed_name.replace('''fc2''' , '''linear_out''' ) __snake_case = '''last_stage.''' + trimmed_name elif "network" in old_name and re.search(R'''.\d.''' , _lowerCamelCase ): __snake_case = old_name.replace('''network''' , '''intermediate_stages''' ) if "fc" in new_name: __snake_case = new_name.replace('''fc''' , '''convolution''' ) elif ("norm1" in new_name) and ("layernorm1" not in new_name): __snake_case = new_name.replace('''norm1''' , '''batchnorm_before''' ) elif ("norm2" in new_name) and ("layernorm2" not in new_name): __snake_case = new_name.replace('''norm2''' , '''batchnorm_after''' ) if "proj" in new_name: __snake_case = new_name.replace('''proj''' , '''projection''' ) if "dist_head" in new_name: __snake_case = new_name.replace('''dist_head''' , '''distillation_classifier''' ) elif "head" in new_name: __snake_case = new_name.replace('''head''' , '''classifier''' ) elif "patch_embed" in new_name: __snake_case = '''efficientformer.''' + new_name elif new_name == "norm.weight" or new_name == "norm.bias": __snake_case = new_name.replace('''norm''' , '''layernorm''' ) __snake_case = '''efficientformer.''' + new_name else: __snake_case = '''efficientformer.encoder.''' + new_name return new_name def _UpperCamelCase (_lowerCamelCase : str , _lowerCamelCase : Tuple )-> List[str]: '''simple docstring''' for key in checkpoint.copy().keys(): __snake_case = checkpoint.pop(_lowerCamelCase ) __snake_case = val return checkpoint def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = '''http://images.cocodataset.org/val2017/000000039769.jpg''' __snake_case = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return image def _UpperCamelCase (_lowerCamelCase : Path , _lowerCamelCase : Path , _lowerCamelCase : Path , _lowerCamelCase : bool )-> Optional[Any]: '''simple docstring''' __snake_case = torch.load(_lowerCamelCase , map_location='''cpu''' )['''model'''] __snake_case = EfficientFormerConfig.from_json_file(_lowerCamelCase ) __snake_case = EfficientFormerForImageClassificationWithTeacher(_lowerCamelCase ) __snake_case = '''_'''.join(checkpoint_path.split('''/''' )[-1].split('''.''' )[0].split('''_''' )[:-1] ) __snake_case = config.depths[-1] - config.num_metaad_blocks + 1 __snake_case = convert_torch_checkpoint(_lowerCamelCase , _lowerCamelCase ) model.load_state_dict(_lowerCamelCase ) model.eval() __snake_case = { '''bilinear''': PILImageResampling.BILINEAR, '''bicubic''': PILImageResampling.BICUBIC, '''nearest''': PILImageResampling.NEAREST, } # prepare image __snake_case = prepare_img() __snake_case = 2_56 __snake_case = 2_24 __snake_case = EfficientFormerImageProcessor( size={'''shortest_edge''': image_size} , crop_size={'''height''': crop_size, '''width''': crop_size} , resample=pillow_resamplings['''bicubic'''] , ) __snake_case = processor(images=_lowerCamelCase , return_tensors='''pt''' ).pixel_values # original processing pipeline __snake_case = Compose( [ Resize(_lowerCamelCase , interpolation=pillow_resamplings['''bicubic'''] ), CenterCrop(_lowerCamelCase ), ToTensor(), Normalize(_lowerCamelCase , _lowerCamelCase ), ] ) __snake_case = image_transforms(_lowerCamelCase ).unsqueeze(0 ) assert torch.allclose(_lowerCamelCase , _lowerCamelCase ) __snake_case = model(_lowerCamelCase ) __snake_case = outputs.logits __snake_case = (1, 10_00) if "l1" in model_name: __snake_case = torch.Tensor( [-0.1312, 0.4353, -1.0499, -0.5124, 0.4183, -0.6793, -1.3777, -0.0893, -0.7358, -2.4328] ) assert torch.allclose(logits[0, :10] , _lowerCamelCase , atol=1E-3 ) assert logits.shape == expected_shape elif "l3" in model_name: __snake_case = torch.Tensor( [-1.3150, -1.5456, -1.2556, -0.8496, -0.7127, -0.7897, -0.9728, -0.3052, 0.3751, -0.3127] ) assert torch.allclose(logits[0, :10] , _lowerCamelCase , atol=1E-3 ) assert logits.shape == expected_shape elif "l7" in model_name: __snake_case = torch.Tensor( [-1.0283, -1.4131, -0.5644, -1.3115, -0.5785, -1.2049, -0.7528, 0.1992, -0.3822, -0.0878] ) assert logits.shape == expected_shape else: raise ValueError( f'''Unknown model checkpoint: {checkpoint_path}. Supported version of efficientformer are l1, l3 and l7''' ) # Save Checkpoints Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase ) model.save_pretrained(_lowerCamelCase ) print(f'''Checkpoint successfuly converted. Model saved at {pytorch_dump_path}''' ) processor.save_pretrained(_lowerCamelCase ) print(f'''Processor successfuly saved at {pytorch_dump_path}''' ) if push_to_hub: print('''Pushing model to the hub...''' ) model.push_to_hub( repo_id=f'''Bearnardd/{pytorch_dump_path}''' , commit_message='''Add model''' , use_temp_dir=_lowerCamelCase , ) processor.push_to_hub( repo_id=f'''Bearnardd/{pytorch_dump_path}''' , commit_message='''Add image processor''' , use_temp_dir=_lowerCamelCase , ) if __name__ == "__main__": UpperCAmelCase_ : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--pytorch_model_path''', default=None, type=str, required=True, help='''Path to EfficientFormer pytorch checkpoint.''', ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The json file for EfficientFormer model config.''', ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') parser.add_argument( '''--no-push_to_hub''', dest='''push_to_hub''', action='''store_false''', help='''Do not push model and image processor to the hub''', ) parser.set_defaults(push_to_hub=True) UpperCAmelCase_ : Union[str, Any] = parser.parse_args() convert_efficientformer_checkpoint( checkpoint_path=args.pytorch_model_path, efficientformer_config_file=args.config_file, pytorch_dump_path=args.pytorch_dump_path, push_to_hub=args.push_to_hub, )
24
0
'''simple docstring''' 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 ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_outputs import ( BaseModelOutputWithNoAttention, BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention, ) from ...modeling_utils import PreTrainedModel from ...utils import logging from .configuration_regnet import RegNetConfig UpperCamelCase_ = logging.get_logger(__name__) # General docstring UpperCamelCase_ = """RegNetConfig""" # Base docstring UpperCamelCase_ = """facebook/regnet-y-040""" UpperCamelCase_ = [1, 1088, 7, 7] # Image classification docstring UpperCamelCase_ = """facebook/regnet-y-040""" UpperCamelCase_ = """tabby, tabby cat""" UpperCamelCase_ = [ """facebook/regnet-y-040""", # See all regnet models at https://huggingface.co/models?filter=regnet ] class __SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Dict , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : int = 3 , UpperCAmelCase__ : int = 1 , UpperCAmelCase__ : int = 1 , UpperCAmelCase__ : Optional[str] = "relu" , ): '''simple docstring''' super().__init__() lowercase : Tuple =nn.Convad( UpperCAmelCase__ , UpperCAmelCase__ , kernel_size=UpperCAmelCase__ , stride=UpperCAmelCase__ , padding=kernel_size // 2 , groups=UpperCAmelCase__ , bias=UpperCAmelCase__ , ) lowercase : Optional[int] =nn.BatchNormad(UpperCAmelCase__ ) lowercase : Any =ACTaFN[activation] if activation is not None else nn.Identity() def lowerCamelCase_ ( self : Optional[int] , UpperCAmelCase__ : Any ): '''simple docstring''' lowercase : List[Any] =self.convolution(UpperCAmelCase__ ) lowercase : int =self.normalization(UpperCAmelCase__ ) lowercase : Tuple =self.activation(UpperCAmelCase__ ) return hidden_state class __SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : str , UpperCAmelCase__ : RegNetConfig ): '''simple docstring''' super().__init__() lowercase : Tuple =RegNetConvLayer( config.num_channels , config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act ) lowercase : Optional[int] =config.num_channels def lowerCamelCase_ ( self : Union[str, Any] , UpperCAmelCase__ : List[str] ): '''simple docstring''' lowercase : Optional[Any] =pixel_values.shape[1] if num_channels != self.num_channels: raise ValueError( '''Make sure that the channel dimension of the pixel values match with the one set in the configuration.''' ) lowercase : List[Any] =self.embedder(UpperCAmelCase__ ) return hidden_state class __SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : List[Any] , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : int = 2 ): '''simple docstring''' super().__init__() lowercase : Tuple =nn.Convad(UpperCAmelCase__ , UpperCAmelCase__ , kernel_size=1 , stride=UpperCAmelCase__ , bias=UpperCAmelCase__ ) lowercase : List[Any] =nn.BatchNormad(UpperCAmelCase__ ) def lowerCamelCase_ ( self : Tuple , UpperCAmelCase__ : Tensor ): '''simple docstring''' lowercase : List[str] =self.convolution(UpperCAmelCase__ ) lowercase : Any =self.normalization(UpperCAmelCase__ ) return hidden_state class __SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Union[str, Any] , UpperCAmelCase__ : int , UpperCAmelCase__ : int ): '''simple docstring''' super().__init__() lowercase : Union[str, Any] =nn.AdaptiveAvgPoolad((1, 1) ) lowercase : Dict =nn.Sequential( nn.Convad(UpperCAmelCase__ , UpperCAmelCase__ , kernel_size=1 ) , nn.ReLU() , nn.Convad(UpperCAmelCase__ , UpperCAmelCase__ , kernel_size=1 ) , nn.Sigmoid() , ) def lowerCamelCase_ ( self : Optional[int] , UpperCAmelCase__ : List[Any] ): '''simple docstring''' # b c h w -> b c 1 1 lowercase : int =self.pooler(UpperCAmelCase__ ) lowercase : List[str] =self.attention(UpperCAmelCase__ ) lowercase : List[str] =hidden_state * attention return hidden_state class __SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : str , UpperCAmelCase__ : RegNetConfig , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : int = 1 ): '''simple docstring''' super().__init__() lowercase : Any =in_channels != out_channels or stride != 1 lowercase : int =max(1 , out_channels // config.groups_width ) lowercase : Optional[int] =( RegNetShortCut(UpperCAmelCase__ , UpperCAmelCase__ , stride=UpperCAmelCase__ ) if should_apply_shortcut else nn.Identity() ) lowercase : Tuple =nn.Sequential( RegNetConvLayer(UpperCAmelCase__ , UpperCAmelCase__ , kernel_size=1 , activation=config.hidden_act ) , RegNetConvLayer(UpperCAmelCase__ , UpperCAmelCase__ , stride=UpperCAmelCase__ , groups=UpperCAmelCase__ , activation=config.hidden_act ) , RegNetConvLayer(UpperCAmelCase__ , UpperCAmelCase__ , kernel_size=1 , activation=UpperCAmelCase__ ) , ) lowercase : str =ACTaFN[config.hidden_act] def lowerCamelCase_ ( self : List[Any] , UpperCAmelCase__ : Optional[Any] ): '''simple docstring''' lowercase : str =hidden_state lowercase : int =self.layer(UpperCAmelCase__ ) lowercase : Any =self.shortcut(UpperCAmelCase__ ) hidden_state += residual lowercase : Union[str, Any] =self.activation(UpperCAmelCase__ ) return hidden_state class __SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : List[Any] , UpperCAmelCase__ : RegNetConfig , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : int = 1 ): '''simple docstring''' super().__init__() lowercase : str =in_channels != out_channels or stride != 1 lowercase : Optional[int] =max(1 , out_channels // config.groups_width ) lowercase : str =( RegNetShortCut(UpperCAmelCase__ , UpperCAmelCase__ , stride=UpperCAmelCase__ ) if should_apply_shortcut else nn.Identity() ) lowercase : Union[str, Any] =nn.Sequential( RegNetConvLayer(UpperCAmelCase__ , UpperCAmelCase__ , kernel_size=1 , activation=config.hidden_act ) , RegNetConvLayer(UpperCAmelCase__ , UpperCAmelCase__ , stride=UpperCAmelCase__ , groups=UpperCAmelCase__ , activation=config.hidden_act ) , RegNetSELayer(UpperCAmelCase__ , reduced_channels=int(round(in_channels / 4 ) ) ) , RegNetConvLayer(UpperCAmelCase__ , UpperCAmelCase__ , kernel_size=1 , activation=UpperCAmelCase__ ) , ) lowercase : Union[str, Any] =ACTaFN[config.hidden_act] def lowerCamelCase_ ( self : Any , UpperCAmelCase__ : Tuple ): '''simple docstring''' lowercase : Dict =hidden_state lowercase : Any =self.layer(UpperCAmelCase__ ) lowercase : Dict =self.shortcut(UpperCAmelCase__ ) hidden_state += residual lowercase : str =self.activation(UpperCAmelCase__ ) return hidden_state class __SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Dict , UpperCAmelCase__ : RegNetConfig , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : int = 2 , UpperCAmelCase__ : int = 2 , ): '''simple docstring''' super().__init__() lowercase : Union[str, Any] =RegNetXLayer if config.layer_type == '''x''' else RegNetYLayer lowercase : List[Any] =nn.Sequential( # downsampling is done in the first layer with stride of 2 layer( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , stride=UpperCAmelCase__ , ) , *[layer(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) for _ in range(depth - 1 )] , ) def lowerCamelCase_ ( self : List[Any] , UpperCAmelCase__ : Union[str, Any] ): '''simple docstring''' lowercase : List[Any] =self.layers(UpperCAmelCase__ ) return hidden_state class __SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : int , UpperCAmelCase__ : RegNetConfig ): '''simple docstring''' super().__init__() lowercase : str =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( RegNetStage( UpperCAmelCase__ , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , ) ) lowercase : List[str] =zip(config.hidden_sizes , config.hidden_sizes[1:] ) for (in_channels, out_channels), depth in zip(UpperCAmelCase__ , config.depths[1:] ): self.stages.append(RegNetStage(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , depth=UpperCAmelCase__ ) ) def lowerCamelCase_ ( self : List[str] , UpperCAmelCase__ : Tensor , UpperCAmelCase__ : bool = False , UpperCAmelCase__ : bool = True ): '''simple docstring''' lowercase : List[str] =() if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: lowercase : List[Any] =hidden_states + (hidden_state,) lowercase : int =stage_module(UpperCAmelCase__ ) if output_hidden_states: lowercase : Tuple =hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return BaseModelOutputWithNoAttention(last_hidden_state=UpperCAmelCase__ , hidden_states=UpperCAmelCase__ ) class __SCREAMING_SNAKE_CASE ( lowercase__ ): lowerCamelCase_ = RegNetConfig lowerCamelCase_ = 'regnet' lowerCamelCase_ = 'pixel_values' lowerCamelCase_ = True def lowerCamelCase_ ( self : Union[str, Any] , UpperCAmelCase__ : Tuple ): '''simple docstring''' if isinstance(UpperCAmelCase__ , nn.Convad ): nn.init.kaiming_normal_(module.weight , mode='''fan_out''' , nonlinearity='''relu''' ) elif isinstance(UpperCAmelCase__ , (nn.BatchNormad, nn.GroupNorm) ): nn.init.constant_(module.weight , 1 ) nn.init.constant_(module.bias , 0 ) def lowerCamelCase_ ( self : Optional[int] , UpperCAmelCase__ : Any , UpperCAmelCase__ : Optional[int]=False ): '''simple docstring''' if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): lowercase : int =value UpperCamelCase_ = r""" This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`RegNetConfig`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. """ UpperCamelCase_ = r""" Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`ConvNextImageProcessor.__call__`] for details. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~file_utils.ModelOutput`] instead of a plain tuple. """ @add_start_docstrings( 'The bare RegNet model outputting raw features without any specific head on top.' , lowercase__ , ) # Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet class __SCREAMING_SNAKE_CASE ( lowercase__ ): def __init__( self : Any , UpperCAmelCase__ : List[Any] ): '''simple docstring''' super().__init__(UpperCAmelCase__ ) lowercase : str =config lowercase : Any =RegNetEmbeddings(UpperCAmelCase__ ) lowercase : Union[str, Any] =RegNetEncoder(UpperCAmelCase__ ) lowercase : Any =nn.AdaptiveAvgPoolad((1, 1) ) # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UpperCAmelCase__ ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=UpperCAmelCase__ , config_class=_CONFIG_FOR_DOC , modality='''vision''' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def lowerCamelCase_ ( self : List[str] , UpperCAmelCase__ : Tensor , UpperCAmelCase__ : Optional[bool] = None , UpperCAmelCase__ : Optional[bool] = None ): '''simple docstring''' lowercase : str =( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowercase : Dict =return_dict if return_dict is not None else self.config.use_return_dict lowercase : Tuple =self.embedder(UpperCAmelCase__ ) lowercase : Optional[Any] =self.encoder( UpperCAmelCase__ , output_hidden_states=UpperCAmelCase__ , return_dict=UpperCAmelCase__ ) lowercase : Optional[int] =encoder_outputs[0] lowercase : Optional[int] =self.pooler(UpperCAmelCase__ ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=UpperCAmelCase__ , pooler_output=UpperCAmelCase__ , hidden_states=encoder_outputs.hidden_states , ) @add_start_docstrings( '\n RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n ' , lowercase__ , ) # Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet class __SCREAMING_SNAKE_CASE ( lowercase__ ): def __init__( self : str , UpperCAmelCase__ : Any ): '''simple docstring''' super().__init__(UpperCAmelCase__ ) lowercase : Dict =config.num_labels lowercase : Union[str, Any] =RegNetModel(UpperCAmelCase__ ) # classification head lowercase : Optional[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(UpperCAmelCase__ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=UpperCAmelCase__ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def lowerCamelCase_ ( self : str , UpperCAmelCase__ : Optional[torch.FloatTensor] = None , UpperCAmelCase__ : Optional[torch.LongTensor] = None , UpperCAmelCase__ : Optional[bool] = None , UpperCAmelCase__ : Optional[bool] = None , ): '''simple docstring''' lowercase : Dict =return_dict if return_dict is not None else self.config.use_return_dict lowercase : Union[str, Any] =self.regnet(UpperCAmelCase__ , output_hidden_states=UpperCAmelCase__ , return_dict=UpperCAmelCase__ ) lowercase : int =outputs.pooler_output if return_dict else outputs[1] lowercase : Optional[int] =self.classifier(UpperCAmelCase__ ) lowercase : Any =None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: lowercase : Optional[int] ='''regression''' elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): lowercase : List[str] ='''single_label_classification''' else: lowercase : int ='''multi_label_classification''' if self.config.problem_type == "regression": lowercase : str =MSELoss() if self.num_labels == 1: lowercase : Union[str, Any] =loss_fct(logits.squeeze() , labels.squeeze() ) else: lowercase : str =loss_fct(UpperCAmelCase__ , UpperCAmelCase__ ) elif self.config.problem_type == "single_label_classification": lowercase : Tuple =CrossEntropyLoss() lowercase : Union[str, Any] =loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": lowercase : Optional[int] =BCEWithLogitsLoss() lowercase : List[Any] =loss_fct(UpperCAmelCase__ , UpperCAmelCase__ ) if not return_dict: lowercase : str =(logits,) + outputs[2:] return (loss,) + output if loss is not None else output return ImageClassifierOutputWithNoAttention(loss=UpperCAmelCase__ , logits=UpperCAmelCase__ , hidden_states=outputs.hidden_states )
92
'''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 MobileViTImageProcessor class lowerCAmelCase ( unittest.TestCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=18 , __SCREAMING_SNAKE_CASE=30 , __SCREAMING_SNAKE_CASE=400 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , ) -> Tuple: '''simple docstring''' __snake_case = size if size is not None else {'''shortest_edge''': 20} __snake_case = crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} __snake_case = parent __snake_case = batch_size __snake_case = num_channels __snake_case = image_size __snake_case = min_resolution __snake_case = max_resolution __snake_case = do_resize __snake_case = size __snake_case = do_center_crop __snake_case = crop_size __snake_case = do_flip_channel_order def lowerCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_flip_channel_order": self.do_flip_channel_order, } @require_torch @require_vision class lowerCAmelCase ( __lowerCAmelCase , unittest.TestCase): __lowercase : Union[str, Any] = MobileViTImageProcessor if is_vision_available() else None def lowerCAmelCase ( self ) -> str: '''simple docstring''' __snake_case = MobileViTImageProcessingTester(self ) @property def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def lowerCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_resize''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''size''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_center_crop''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''center_crop''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_flip_channel_order''' ) ) def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 20} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) __snake_case = 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 lowerCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' pass def lowerCAmelCase ( self ) -> str: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __snake_case = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input __snake_case = 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 __snake_case = image_processing(__SCREAMING_SNAKE_CASE , 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 lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __snake_case = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , numpify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) # Test not batched input __snake_case = 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 __snake_case = image_processing(__SCREAMING_SNAKE_CASE , 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 lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __snake_case = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , torchify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) # Test not batched input __snake_case = 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 __snake_case = image_processing(__SCREAMING_SNAKE_CASE , 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'''], ) , )
24
0
"""simple docstring""" import importlib.metadata import operator import re import sys from typing import Optional from packaging import version __A = { """<""": operator.lt, """<=""": operator.le, """==""": operator.eq, """!=""": operator.ne, """>=""": operator.ge, """>""": operator.gt, } def __A (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ->Union[str, Any]: """simple docstring""" if got_ver is None or want_ver is None: raise ValueError( F"Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider" F" reinstalling {pkg}." ) if not ops[op](version.parse(_SCREAMING_SNAKE_CASE ) , version.parse(_SCREAMING_SNAKE_CASE ) ): raise ImportError( F"{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}" ) def __A (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) ->None: """simple docstring""" lowerCAmelCase__ :List[str] = F"\n{hint}" if hint is not None else '' # non-versioned check if re.match(r'^[\w_\-\d]+$' , _SCREAMING_SNAKE_CASE ): lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ :Any = requirement, None, None else: lowerCAmelCase__ :List[str] = re.findall(r'^([^!=<>\s]+)([\s!=<>]{1,2}.+)' , _SCREAMING_SNAKE_CASE ) if not match: raise ValueError( 'requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but' F" got {requirement}" ) lowerCAmelCase__ , lowerCAmelCase__ :Union[str, Any] = match[0] lowerCAmelCase__ :List[Any] = want_full.split(',' ) # there could be multiple requirements lowerCAmelCase__ :Any = {} for w in want_range: lowerCAmelCase__ :Tuple = re.findall(r'^([\s!=<>]{1,2})(.+)' , _SCREAMING_SNAKE_CASE ) if not match: raise ValueError( 'requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,' F" but got {requirement}" ) lowerCAmelCase__ , lowerCAmelCase__ :int = match[0] lowerCAmelCase__ :str = want_ver if op not in ops: raise ValueError(F"{requirement}: need one of {list(ops.keys() )}, but got {op}" ) # special case if pkg == "python": lowerCAmelCase__ :Any = '.'.join([str(_SCREAMING_SNAKE_CASE ) for x in sys.version_info[:3]] ) for op, want_ver in wanted.items(): _compare_versions(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return # check if any version is installed try: lowerCAmelCase__ :List[Any] = importlib.metadata.version(_SCREAMING_SNAKE_CASE ) except importlib.metadata.PackageNotFoundError: raise importlib.metadata.PackageNotFoundError( F"The '{requirement}' distribution was not found and is required by this application. {hint}" ) # check that the right version is installed if version number or a range was provided if want_ver is not None: for op, want_ver in wanted.items(): _compare_versions(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __A (_SCREAMING_SNAKE_CASE ) ->List[Any]: """simple docstring""" lowerCAmelCase__ :Optional[Any] = 'Try: pip install transformers -U or pip install -e \'.[dev]\' if you\'re working with git main' return require_version(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )
93
'''simple docstring''' from typing import Optional import pyspark from .. import Features, NamedSplit from ..download import DownloadMode from ..packaged_modules.spark.spark import Spark from .abc import AbstractDatasetReader class lowerCAmelCase ( __lowerCAmelCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = "arrow" , **__SCREAMING_SNAKE_CASE , ) -> Union[str, Any]: '''simple docstring''' super().__init__( split=__SCREAMING_SNAKE_CASE , features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE , keep_in_memory=__SCREAMING_SNAKE_CASE , streaming=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) __snake_case = load_from_cache_file __snake_case = file_format __snake_case = Spark( df=__SCREAMING_SNAKE_CASE , features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE , working_dir=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) def lowerCAmelCase ( self ) -> str: '''simple docstring''' if self.streaming: return self.builder.as_streaming_dataset(split=self.split ) __snake_case = None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD self.builder.download_and_prepare( download_mode=__SCREAMING_SNAKE_CASE , file_format=self._file_format , ) return self.builder.as_dataset(split=self.split )
24
0
'''simple docstring''' from __future__ import annotations import string from itertools import cycle, product from pathlib import Path SCREAMING_SNAKE_CASE = ( string.ascii_letters + string.digits + string.punctuation + string.whitespace ) SCREAMING_SNAKE_CASE = [ord(letter) for letter in string.ascii_lowercase] SCREAMING_SNAKE_CASE = {ord(char) for char in VALID_CHARS} SCREAMING_SNAKE_CASE = ["the", "be", "to", "of", "and", "in", "that", "have"] def lowercase_ ( __A : list[int] , __A : tuple[int, ...] ) -> str | None: """simple docstring""" lowercase : str ="" lowercase : int lowercase : int lowercase : int for keychar, cipherchar in zip(cycle(__A ) , __A ): lowercase : str =cipherchar ^ keychar if decodedchar not in VALID_INTS: return None decoded += chr(__A ) return decoded def lowercase_ ( __A : list[int] ) -> list[str]: """simple docstring""" lowercase : list[str] =[] for key in product(__A , repeat=3 ): lowercase : List[str] =try_key(__A , __A ) if encoded is not None: possibles.append(__A ) return possibles def lowercase_ ( __A : list[str] , __A : str ) -> list[str]: """simple docstring""" return [possible for possible in possibles if common_word in possible.lower()] def lowercase_ ( __A : str = "p059_cipher.txt" ) -> int: """simple docstring""" lowercase : list[int] lowercase : list[str] lowercase : str lowercase : str lowercase : str =Path(__A ).parent.joinpath(__A ).read_text(encoding='''utf-8''' ) lowercase : List[Any] =[int(__A ) for number in data.strip().split(''',''' )] lowercase : Tuple =filter_valid_chars(__A ) for common_word in COMMON_WORDS: lowercase : Optional[Any] =filter_common_word(__A , __A ) if len(__A ) == 1: break lowercase : List[Any] =possibles[0] return sum(ord(__A ) for char in decoded_text ) if __name__ == "__main__": print(f"""{solution() = }""")
94
'''simple docstring''' import json from typing import Dict, List, Optional, Tuple, Union from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_led import LEDTokenizer UpperCAmelCase_ : Any = logging.get_logger(__name__) UpperCAmelCase_ : str = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} UpperCAmelCase_ : str = { '''vocab_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json''', }, '''merges_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt''', }, '''tokenizer_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json''', }, } UpperCAmelCase_ : Union[str, Any] = { '''allenai/led-base-16384''': 1_6_3_8_4, } class lowerCAmelCase ( __lowerCAmelCase): __lowercase : Dict = VOCAB_FILES_NAMES __lowercase : Dict = PRETRAINED_VOCAB_FILES_MAP __lowercase : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Union[str, Any] = LEDTokenizer __lowercase : int = ['''input_ids''', '''attention_mask'''] def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="replace" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="<mask>" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , **__SCREAMING_SNAKE_CASE , ) -> List[Any]: '''simple docstring''' super().__init__( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , errors=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) __snake_case = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: __snake_case = getattr(__SCREAMING_SNAKE_CASE , pre_tok_state.pop('''type''' ) ) __snake_case = add_prefix_space __snake_case = pre_tok_class(**__SCREAMING_SNAKE_CASE ) __snake_case = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` __snake_case = '''post_processor''' __snake_case = getattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if tokenizer_component_instance: __snake_case = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: __snake_case = tuple(state['''sep'''] ) if "cls" in state: __snake_case = tuple(state['''cls'''] ) __snake_case = False if state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: __snake_case = add_prefix_space __snake_case = True if state.get('''trim_offsets''' , __SCREAMING_SNAKE_CASE ) != trim_offsets: __snake_case = trim_offsets __snake_case = True if changes_to_apply: __snake_case = getattr(__SCREAMING_SNAKE_CASE , state.pop('''type''' ) ) __snake_case = component_class(**__SCREAMING_SNAKE_CASE ) setattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @property # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED def lowerCAmelCase ( self ) -> str: '''simple docstring''' if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' __snake_case = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else value __snake_case = value def lowerCAmelCase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> BatchEncoding: '''simple docstring''' __snake_case = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' '''to use it with pretokenized inputs.''' ) return super()._batch_encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> BatchEncoding: '''simple docstring''' __snake_case = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' '''to use it with pretokenized inputs.''' ) return super()._encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) -> Tuple[str]: '''simple docstring''' __snake_case = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) -> List[Any]: '''simple docstring''' __snake_case = [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 , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) -> List[int]: '''simple docstring''' __snake_case = [self.sep_token_id] __snake_case = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , ) -> dict: '''simple docstring''' __snake_case = super()._pad( encoded_inputs=__SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , padding_strategy=__SCREAMING_SNAKE_CASE , pad_to_multiple_of=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , ) # Load from model defaults if return_attention_mask is None: __snake_case = '''attention_mask''' in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: __snake_case = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. __snake_case = len(encoded_inputs['''global_attention_mask'''] ) != len(__SCREAMING_SNAKE_CASE ) if needs_to_be_padded: __snake_case = len(__SCREAMING_SNAKE_CASE ) - len(encoded_inputs['''global_attention_mask'''] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` __snake_case = ( encoded_inputs['''global_attention_mask'''] + [-1] * difference ) elif self.padding_side == "left": __snake_case = [-1] * difference + encoded_inputs[ '''global_attention_mask''' ] else: raise ValueError('''Invalid padding strategy:''' + str(self.padding_side ) ) return encoded_inputs
24
0
"""simple docstring""" lowerCamelCase_ = 9.80665 def snake_case ( A__ ,A__ ,A__ = g ): if fluid_density <= 0: raise ValueError("Impossible fluid density" ) if volume < 0: raise ValueError("Impossible Object volume" ) if gravity <= 0: raise ValueError("Impossible Gravity" ) return fluid_density * gravity * volume if __name__ == "__main__": import doctest # run doctest doctest.testmod()
95
'''simple docstring''' from collections import deque def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> Optional[int]: '''simple docstring''' __snake_case = len(_lowerCamelCase ) __snake_case = deque() __snake_case = [False for _ in range(_lowerCamelCase )] __snake_case = [-1 for _ in range(_lowerCamelCase )] __snake_case = index_of[:] def strong_connect(_lowerCamelCase : Any , _lowerCamelCase : Dict , _lowerCamelCase : List[str] ): __snake_case = index # the number when this node is seen __snake_case = index # lowest rank node reachable from here index += 1 stack.append(_lowerCamelCase ) __snake_case = True for w in g[v]: if index_of[w] == -1: __snake_case = strong_connect(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) __snake_case = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) elif on_stack[w]: __snake_case = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) if lowlink_of[v] == index_of[v]: __snake_case = [] __snake_case = stack.pop() __snake_case = False component.append(_lowerCamelCase ) while w != v: __snake_case = stack.pop() __snake_case = False component.append(_lowerCamelCase ) components.append(_lowerCamelCase ) return index __snake_case = [] for v in range(_lowerCamelCase ): if index_of[v] == -1: strong_connect(_lowerCamelCase , 0 , _lowerCamelCase ) return components def _UpperCamelCase (_lowerCamelCase : Tuple , _lowerCamelCase : Optional[Any] )-> Dict: '''simple docstring''' __snake_case = [[] for _ in range(_lowerCamelCase )] for u, v in edges: g[u].append(_lowerCamelCase ) return g if __name__ == "__main__": # Test UpperCAmelCase_ : List[str] = 7 UpperCAmelCase_ : int = [0, 0, 1, 2, 3, 3, 4, 4, 6] UpperCAmelCase_ : Dict = [1, 3, 2, 0, 1, 4, 5, 6, 5] UpperCAmelCase_ : List[str] = [(u, v) for u, v in zip(source, target)] UpperCAmelCase_ : Tuple = create_graph(n_vertices, edges) assert [[5], [6], [4], [3, 2, 1, 0]] == tarjan(g)
24
0
"""simple docstring""" import os from bleurt import score # From: git+https://github.com/google-research/bleurt.git import datasets __lowerCamelCase = datasets.logging.get_logger(__name__) __lowerCamelCase = '\\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' __lowerCamelCase = '\\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' __lowerCamelCase = '\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' __lowerCamelCase = { '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 __A ( datasets.Metric ): def lowerCamelCase__ ( self : Any ) -> Dict: 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 : List[Any] , __snake_case : Optional[int] ) -> Tuple: # check that config name specifies a valid BLEURT model 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').""" ) __magic_name__: List[Any] = """bleurt-base-128""" if self.config_name.lower() in CHECKPOINT_URLS: __magic_name__: str = self.config_name.lower() elif self.config_name.upper() in CHECKPOINT_URLS: __magic_name__: List[Any] = 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 __magic_name__: Union[str, Any] = dl_manager.download_and_extract(CHECKPOINT_URLS[checkpoint_name] ) __magic_name__: Any = score.BleurtScorer(os.path.join(__snake_case , __snake_case ) ) def lowerCamelCase__ ( self : Dict , __snake_case : Any , __snake_case : List[Any] ) -> Optional[Any]: __magic_name__: Union[str, Any] = self.scorer.score(references=__snake_case , candidates=__snake_case ) return {"scores": scores}
96
'''simple docstring''' import unittest from transformers import BarthezTokenizer, BarthezTokenizerFast, BatchEncoding from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers @require_sentencepiece @slow # see https://github.com/huggingface/transformers/issues/11457 class lowerCAmelCase ( __lowerCAmelCase , unittest.TestCase): __lowercase : int = BarthezTokenizer __lowercase : Any = BarthezTokenizerFast __lowercase : Dict = True __lowercase : Optional[int] = True def lowerCAmelCase ( self ) -> Dict: '''simple docstring''' super().setUp() __snake_case = BarthezTokenizerFast.from_pretrained('''moussaKam/mbarthez''' ) tokenizer.save_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname , legacy_format=__SCREAMING_SNAKE_CASE ) __snake_case = tokenizer def lowerCAmelCase ( self ) -> Dict: '''simple docstring''' __snake_case = '''<pad>''' __snake_case = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<s>''' ) self.assertEqual(vocab_keys[1] , '''<pad>''' ) self.assertEqual(vocab_keys[-1] , '''<mask>''' ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 10_1122 ) def lowerCAmelCase ( self ) -> Any: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 10_1122 ) @require_torch def lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] __snake_case = [0, 57, 3018, 7_0307, 91, 2] __snake_case = self.tokenizer( __SCREAMING_SNAKE_CASE , max_length=len(__SCREAMING_SNAKE_CASE ) , padding=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertEqual((2, 6) , batch.input_ids.shape ) self.assertEqual((2, 6) , batch.attention_mask.shape ) __snake_case = batch.input_ids.tolist()[0] self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' if not self.test_rust_tokenizer: return __snake_case = self.get_tokenizer() __snake_case = self.get_rust_tokenizer() __snake_case = '''I was born in 92000, and this is falsé.''' __snake_case = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) __snake_case = rust_tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __snake_case = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) __snake_case = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __snake_case = self.get_rust_tokenizer() __snake_case = tokenizer.encode(__SCREAMING_SNAKE_CASE ) __snake_case = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = {'''input_ids''': [[0, 490, 1_4328, 4507, 354, 47, 4_3669, 95, 25, 7_8117, 2_0215, 1_9779, 190, 22, 400, 4, 3_5343, 8_0310, 603, 86, 2_4937, 105, 3_3438, 9_4762, 196, 3_9642, 7, 15, 1_5933, 173, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1_0534, 87, 25, 66, 3358, 196, 5_5289, 8, 8_2961, 81, 2204, 7_5203, 7, 15, 763, 1_2956, 216, 178, 1_4328, 9595, 1377, 6_9693, 7, 448, 7_1021, 196, 1_8106, 1437, 1_3974, 108, 9083, 4, 4_9315, 7, 39, 86, 1326, 2793, 4_6333, 4, 448, 196, 7_4588, 7, 4_9315, 7, 39, 21, 822, 3_8470, 74, 21, 6_6723, 6_2480, 8, 2_2050, 5, 2]], '''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, 0, 0, 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # moussaKam/mbarthez is a french model. So we also use french texts. __snake_case = [ '''Le transformeur est un modèle d\'apprentissage profond introduit en 2017, ''' '''utilisé principalement dans le domaine du traitement automatique des langues (TAL).''', '''À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus ''' '''pour gérer des données séquentielles, telles que le langage naturel, pour des tâches ''' '''telles que la traduction et la synthèse de texte.''', ] self.tokenizer_integration_test_util( expected_encoding=__SCREAMING_SNAKE_CASE , model_name='''moussaKam/mbarthez''' , revision='''c2e4ecbca5e3cd2c37fe1ac285ca4fbdf1366fb6''' , sequences=__SCREAMING_SNAKE_CASE , )
24
0
from math import sqrt def a ( snake_case__: int ): '''simple docstring''' lowercase_ = 0 for i in range(1 , int(sqrt(snake_case__ ) + 1 ) ): if n % i == 0 and i != sqrt(snake_case__ ): total += i + n // i elif i == sqrt(snake_case__ ): total += i return total - n def a ( snake_case__: int = 10_000 ): '''simple docstring''' lowercase_ = sum( i for i in range(1 , snake_case__ ) if sum_of_divisors(sum_of_divisors(snake_case__ ) ) == i and sum_of_divisors(snake_case__ ) != i ) return total if __name__ == "__main__": print(solution(int(str(input()).strip())))
97
'''simple docstring''' import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class lowerCAmelCase ( unittest.TestCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __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=32 , __SCREAMING_SNAKE_CASE=5 , __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=4 , ) -> Any: '''simple docstring''' __snake_case = parent __snake_case = batch_size __snake_case = seq_length __snake_case = is_training __snake_case = use_attention_mask __snake_case = use_token_type_ids __snake_case = use_labels __snake_case = vocab_size __snake_case = hidden_size __snake_case = num_hidden_layers __snake_case = num_attention_heads __snake_case = intermediate_size __snake_case = hidden_act __snake_case = hidden_dropout_prob __snake_case = attention_probs_dropout_prob __snake_case = max_position_embeddings __snake_case = type_vocab_size __snake_case = type_sequence_label_size __snake_case = initializer_range __snake_case = num_choices def lowerCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' __snake_case = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __snake_case = None if self.use_attention_mask: __snake_case = random_attention_mask([self.batch_size, self.seq_length] ) __snake_case = None if self.use_token_type_ids: __snake_case = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __snake_case = RoFormerConfig( 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=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = self.prepare_config_and_inputs() __snake_case , __snake_case , __snake_case , __snake_case = config_and_inputs __snake_case = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_flax class lowerCAmelCase ( __lowerCAmelCase , unittest.TestCase): __lowercase : Tuple = True __lowercase : Optional[int] = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = FlaxRoFormerModelTester(self ) @slow def lowerCAmelCase ( self ) -> List[Any]: '''simple docstring''' for model_class_name in self.all_model_classes: __snake_case = model_class_name.from_pretrained('''junnyu/roformer_chinese_small''' , from_pt=__SCREAMING_SNAKE_CASE ) __snake_case = model(np.ones((1, 1) ) ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) @require_flax class lowerCAmelCase ( unittest.TestCase): @slow def lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = FlaxRoFormerForMaskedLM.from_pretrained('''junnyu/roformer_chinese_base''' ) __snake_case = jnp.array([[0, 1, 2, 3, 4, 5]] ) __snake_case = model(__SCREAMING_SNAKE_CASE )[0] __snake_case = 5_0000 __snake_case = (1, 6, vocab_size) self.assertEqual(output.shape , __SCREAMING_SNAKE_CASE ) __snake_case = jnp.array( [[[-0.1_205, -1.0_265, 0.2_922], [-1.5_134, 0.1_974, 0.1_519], [-5.0_135, -3.9_003, -0.8_404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , __SCREAMING_SNAKE_CASE , atol=1E-4 ) )
24
0
'''simple docstring''' import unittest from transformers import RoFormerTokenizer, RoFormerTokenizerFast from transformers.testing_utils import require_rjieba, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_rjieba @require_tokenizers class __lowerCAmelCase ( __magic_name__ , unittest.TestCase ): """simple docstring""" _snake_case : Tuple = RoFormerTokenizer _snake_case : Union[str, Any] = RoFormerTokenizerFast _snake_case : Optional[Any] = True _snake_case : Dict = True def snake_case__ ( self : Any ) -> str: '''simple docstring''' super().setUp() def snake_case__ ( self : List[str] , **lowerCAmelCase__ : Dict ) -> List[Any]: '''simple docstring''' return self.tokenizer_class.from_pretrained('''junnyu/roformer_chinese_base''' , **lowerCAmelCase__ ) def snake_case__ ( self : int , **lowerCAmelCase__ : Optional[int] ) -> List[Any]: '''simple docstring''' return self.rust_tokenizer_class.from_pretrained('''junnyu/roformer_chinese_base''' , **lowerCAmelCase__ ) def snake_case__ ( self : List[str] ) -> Union[str, Any]: '''simple docstring''' _UpperCamelCase = '''永和服装饰品有限公司,今天天气非常好''' _UpperCamelCase = '''永和 服装 饰品 有限公司 , 今 天 天 气 非常 好''' return input_text, output_text def snake_case__ ( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' _UpperCamelCase = self.get_tokenizer() _UpperCamelCase , _UpperCamelCase = self.get_chinese_input_output_texts() _UpperCamelCase = tokenizer.tokenize(lowerCAmelCase__ ) self.assertListEqual(lowerCAmelCase__ , output_text.split() ) _UpperCamelCase = tokens + [tokenizer.unk_token] _UpperCamelCase = [22943, 21332, 34431, 45904, 117, 306, 1231, 1231, 2653, 33994, 1266, 100] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase__ ) , lowerCAmelCase__ ) def snake_case__ ( self : int ) -> str: '''simple docstring''' _UpperCamelCase = self.get_rust_tokenizer() _UpperCamelCase , _UpperCamelCase = self.get_chinese_input_output_texts() _UpperCamelCase = tokenizer.tokenize(lowerCAmelCase__ ) self.assertListEqual(lowerCAmelCase__ , output_text.split() ) _UpperCamelCase = tokens + [tokenizer.unk_token] _UpperCamelCase = [22943, 21332, 34431, 45904, 117, 306, 1231, 1231, 2653, 33994, 1266, 100] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase__ ) , lowerCAmelCase__ ) def snake_case__ ( self : str ) -> int: '''simple docstring''' pass def snake_case__ ( self : Optional[int] ) -> List[Any]: '''simple docstring''' pass def snake_case__ ( self : Optional[int] ) -> Any: '''simple docstring''' pass
98
'''simple docstring''' 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 ()-> int: '''simple docstring''' __snake_case = '''https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png''' __snake_case = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ).convert('''RGB''' ) return image def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> List[Any]: '''simple docstring''' __snake_case = [] # fmt: off # vision encoder rename_keys.append(('''visual_encoder.cls_token''', '''vision_model.embeddings.class_embedding''') ) rename_keys.append(('''visual_encoder.pos_embed''', '''vision_model.embeddings.position_embedding''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.weight''', '''vision_model.embeddings.patch_embedding.weight''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.bias''', '''vision_model.embeddings.patch_embedding.bias''') ) rename_keys.append(('''ln_vision.weight''', '''vision_model.post_layernorm.weight''') ) rename_keys.append(('''ln_vision.bias''', '''vision_model.post_layernorm.bias''') ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.weight''', f'''vision_model.encoder.layers.{i}.layer_norm1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.bias''', f'''vision_model.encoder.layers.{i}.layer_norm1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.weight''', f'''vision_model.encoder.layers.{i}.layer_norm2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.bias''', f'''vision_model.encoder.layers.{i}.layer_norm2.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.qkv.weight''', f'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.weight''', f'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',) ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.bias''', f'''vision_model.encoder.layers.{i}.self_attn.projection.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc2.bias''') ) # QFormer rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.weight''', '''qformer.layernorm.weight''') ) rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.bias''', '''qformer.layernorm.bias''') ) # fmt: on return rename_keys def _UpperCamelCase (_lowerCamelCase : Dict , _lowerCamelCase : List[str] , _lowerCamelCase : Optional[int] )-> Tuple: '''simple docstring''' __snake_case = dct.pop(_lowerCamelCase ) __snake_case = val def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : Tuple )-> str: '''simple docstring''' for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases __snake_case = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.q_bias''' ) __snake_case = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict __snake_case = torch.cat((q_bias, torch.zeros_like(_lowerCamelCase , requires_grad=_lowerCamelCase ), v_bias) ) __snake_case = qkv_bias def _UpperCamelCase (_lowerCamelCase : Tuple , _lowerCamelCase : Tuple )-> Dict: '''simple docstring''' __snake_case = 3_64 if '''coco''' in model_name else 2_24 __snake_case = BlipaVisionConfig(image_size=_lowerCamelCase ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: __snake_case = OPTConfig.from_pretrained('''facebook/opt-2.7b''' , eos_token_id=_lowerCamelCase ).to_dict() elif "opt-6.7b" in model_name: __snake_case = OPTConfig.from_pretrained('''facebook/opt-6.7b''' , eos_token_id=_lowerCamelCase ).to_dict() elif "t5-xl" in model_name: __snake_case = TaConfig.from_pretrained('''google/flan-t5-xl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: __snake_case = TaConfig.from_pretrained('''google/flan-t5-xxl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() __snake_case = BlipaConfig(vision_config=_lowerCamelCase , text_config=_lowerCamelCase ) return config, image_size @torch.no_grad() def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : Union[str, Any]=None , _lowerCamelCase : Any=False )-> Dict: '''simple docstring''' __snake_case = ( AutoTokenizer.from_pretrained('''facebook/opt-2.7b''' ) if '''opt''' in model_name else AutoTokenizer.from_pretrained('''google/flan-t5-xl''' ) ) __snake_case = tokenizer('''\n''' , add_special_tokens=_lowerCamelCase ).input_ids[0] __snake_case , __snake_case = get_blipa_config(_lowerCamelCase , eos_token_id=_lowerCamelCase ) __snake_case = BlipaForConditionalGeneration(_lowerCamelCase ).eval() __snake_case = { '''blip2-opt-2.7b''': ('''blip2_opt''', '''pretrain_opt2.7b'''), '''blip2-opt-6.7b''': ('''blip2_opt''', '''pretrain_opt6.7b'''), '''blip2-opt-2.7b-coco''': ('''blip2_opt''', '''caption_coco_opt2.7b'''), '''blip2-opt-6.7b-coco''': ('''blip2_opt''', '''caption_coco_opt6.7b'''), '''blip2-flan-t5-xl''': ('''blip2_t5''', '''pretrain_flant5xl'''), '''blip2-flan-t5-xl-coco''': ('''blip2_t5''', '''caption_coco_flant5xl'''), '''blip2-flan-t5-xxl''': ('''blip2_t5''', '''pretrain_flant5xxl'''), } __snake_case , __snake_case = model_name_to_original[model_name] # load original model print('''Loading original model...''' ) __snake_case = '''cuda''' if torch.cuda.is_available() else '''cpu''' __snake_case , __snake_case , __snake_case = load_model_and_preprocess( name=_lowerCamelCase , model_type=_lowerCamelCase , is_eval=_lowerCamelCase , device=_lowerCamelCase ) original_model.eval() print('''Done!''' ) # update state dict keys __snake_case = original_model.state_dict() __snake_case = create_rename_keys(_lowerCamelCase ) for src, dest in rename_keys: rename_key(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): __snake_case = state_dict.pop(_lowerCamelCase ) if key.startswith('''Qformer.bert''' ): __snake_case = key.replace('''Qformer.bert''' , '''qformer''' ) if "attention.self" in key: __snake_case = key.replace('''self''' , '''attention''' ) if "opt_proj" in key: __snake_case = key.replace('''opt_proj''' , '''language_projection''' ) if "t5_proj" in key: __snake_case = key.replace('''t5_proj''' , '''language_projection''' ) if key.startswith('''opt''' ): __snake_case = key.replace('''opt''' , '''language''' ) if key.startswith('''t5''' ): __snake_case = key.replace('''t5''' , '''language''' ) __snake_case = val # read in qv biases read_in_q_v_bias(_lowerCamelCase , _lowerCamelCase ) __snake_case , __snake_case = hf_model.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) assert len(_lowerCamelCase ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] __snake_case = load_demo_image() __snake_case = vis_processors['''eval'''](_lowerCamelCase ).unsqueeze(0 ).to(_lowerCamelCase ) __snake_case = tokenizer(['''\n'''] , return_tensors='''pt''' ).input_ids.to(_lowerCamelCase ) # create processor __snake_case = BlipImageProcessor( size={'''height''': image_size, '''width''': image_size} , image_mean=_lowerCamelCase , image_std=_lowerCamelCase ) __snake_case = BlipaProcessor(image_processor=_lowerCamelCase , tokenizer=_lowerCamelCase ) __snake_case = processor(images=_lowerCamelCase , return_tensors='''pt''' ).pixel_values.to(_lowerCamelCase ) # make sure processor creates exact same pixel values assert torch.allclose(_lowerCamelCase , _lowerCamelCase ) original_model.to(_lowerCamelCase ) hf_model.to(_lowerCamelCase ) with torch.no_grad(): if "opt" in model_name: __snake_case = original_model({'''image''': original_pixel_values, '''text_input''': ['''''']} ).logits __snake_case = hf_model(_lowerCamelCase , _lowerCamelCase ).logits else: __snake_case = original_model( {'''image''': original_pixel_values, '''text_input''': ['''\n'''], '''text_output''': ['''\n''']} ).logits __snake_case = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -1_00 ) __snake_case = hf_model(_lowerCamelCase , _lowerCamelCase , labels=_lowerCamelCase ).logits assert original_logits.shape == logits.shape print('''First values of original logits:''' , original_logits[0, :3, :3] ) print('''First values of HF logits:''' , logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": __snake_case = torch.tensor( [[-41.5850, -4.4440, -8.9922], [-47.4322, -5.9143, -1.7340]] , device=_lowerCamelCase ) assert torch.allclose(logits[0, :3, :3] , _lowerCamelCase , atol=1E-4 ) elif model_name == "blip2-flan-t5-xl-coco": __snake_case = torch.tensor( [[-57.0109, -9.8967, -12.6280], [-68.6578, -12.7191, -10.5065]] , device=_lowerCamelCase ) else: # cast to same type __snake_case = logits.dtype assert torch.allclose(original_logits.to(_lowerCamelCase ) , _lowerCamelCase , atol=1E-2 ) print('''Looks ok!''' ) print('''Generating a caption...''' ) __snake_case = '''''' __snake_case = tokenizer(_lowerCamelCase , return_tensors='''pt''' ).input_ids.to(_lowerCamelCase ) __snake_case = original_model.generate({'''image''': original_pixel_values} ) __snake_case = hf_model.generate( _lowerCamelCase , _lowerCamelCase , do_sample=_lowerCamelCase , 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:''' , _lowerCamelCase ) __snake_case = input_ids.shape[1] __snake_case = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=_lowerCamelCase ) __snake_case = [text.strip() for text in output_text] print('''HF generation:''' , _lowerCamelCase ) if pytorch_dump_folder_path is not None: processor.save_pretrained(_lowerCamelCase ) hf_model.save_pretrained(_lowerCamelCase ) if push_to_hub: processor.push_to_hub(f'''nielsr/{model_name}''' ) hf_model.push_to_hub(f'''nielsr/{model_name}''' ) if __name__ == "__main__": UpperCAmelCase_ : Any = argparse.ArgumentParser() UpperCAmelCase_ : 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''', ) UpperCAmelCase_ : List[str] = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
24
0
import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class __UpperCAmelCase ( unittest.TestCase ): """simple docstring""" def snake_case_ ( self ): __a = inspect.getfile(accelerate.test_utils ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["""scripts""", """test_script.py"""] ) __a = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ["""scripts""", """test_distributed_data_loop.py"""] ) __a = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["""scripts""", """test_ops.py"""] ) @require_multi_gpu def snake_case_ ( self ): print(f'''Found {torch.cuda.device_count()} devices.''' ) __a = ["""torchrun""", f'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(__A , env=os.environ.copy() ) @require_multi_gpu def snake_case_ ( self ): print(f'''Found {torch.cuda.device_count()} devices.''' ) __a = ["""torchrun""", f'''--nproc_per_node={torch.cuda.device_count()}''', self.operation_file_path] print(f'''Command: {cmd}''' ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(__A , env=os.environ.copy() ) @require_multi_gpu def snake_case_ ( self ): __a = ["""torchrun""", f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(__A , env=os.environ.copy() ) @require_multi_gpu def snake_case_ ( self ): print(f'''Found {torch.cuda.device_count()} devices, using 2 devices only''' ) __a = ["""torchrun""", f'''--nproc_per_node={torch.cuda.device_count()}''', self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices="""0,1""" ): execute_subprocess_async(__A , env=os.environ.copy() ) if __name__ == "__main__": SCREAMING_SNAKE_CASE = Accelerator() SCREAMING_SNAKE_CASE = (accelerator.state.process_index + 2, 1_0) SCREAMING_SNAKE_CASE = torch.randint(0, 1_0, shape).to(accelerator.device) SCREAMING_SNAKE_CASE = '' SCREAMING_SNAKE_CASE = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." SCREAMING_SNAKE_CASE = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." SCREAMING_SNAKE_CASE = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
99
'''simple docstring''' import warnings from typing import List, Optional, Tuple, Union import numpy as np import PIL import torch from ...models import UNetaDModel from ...schedulers import RePaintScheduler from ...utils import PIL_INTERPOLATION, logging, randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput UpperCAmelCase_ : Tuple = logging.get_logger(__name__) # pylint: disable=invalid-name def _UpperCamelCase (_lowerCamelCase : Union[List, PIL.Image.Image, torch.Tensor] )-> Optional[Any]: '''simple docstring''' warnings.warn( '''The preprocess method is deprecated and will be removed in a future version. Please''' ''' use VaeImageProcessor.preprocess instead''' , _lowerCamelCase , ) if isinstance(_lowerCamelCase , torch.Tensor ): return image elif isinstance(_lowerCamelCase , PIL.Image.Image ): __snake_case = [image] if isinstance(image[0] , PIL.Image.Image ): __snake_case , __snake_case = image[0].size __snake_case , __snake_case = (x - x % 8 for x in (w, h)) # resize to integer multiple of 8 __snake_case = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['''lanczos'''] ) )[None, :] for i in image] __snake_case = np.concatenate(_lowerCamelCase , axis=0 ) __snake_case = np.array(_lowerCamelCase ).astype(np.floataa ) / 255.0 __snake_case = image.transpose(0 , 3 , 1 , 2 ) __snake_case = 2.0 * image - 1.0 __snake_case = torch.from_numpy(_lowerCamelCase ) elif isinstance(image[0] , torch.Tensor ): __snake_case = torch.cat(_lowerCamelCase , dim=0 ) return image def _UpperCamelCase (_lowerCamelCase : Union[List, PIL.Image.Image, torch.Tensor] )-> Optional[Any]: '''simple docstring''' if isinstance(_lowerCamelCase , torch.Tensor ): return mask elif isinstance(_lowerCamelCase , PIL.Image.Image ): __snake_case = [mask] if isinstance(mask[0] , PIL.Image.Image ): __snake_case , __snake_case = mask[0].size __snake_case , __snake_case = (x - x % 32 for x in (w, h)) # resize to integer multiple of 32 __snake_case = [np.array(m.convert('''L''' ).resize((w, h) , resample=PIL_INTERPOLATION['''nearest'''] ) )[None, :] for m in mask] __snake_case = np.concatenate(_lowerCamelCase , axis=0 ) __snake_case = mask.astype(np.floataa ) / 255.0 __snake_case = 0 __snake_case = 1 __snake_case = torch.from_numpy(_lowerCamelCase ) elif isinstance(mask[0] , torch.Tensor ): __snake_case = torch.cat(_lowerCamelCase , dim=0 ) return mask class lowerCAmelCase ( __lowerCAmelCase): __lowercase : UNetaDModel __lowercase : RePaintScheduler def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) -> str: '''simple docstring''' super().__init__() self.register_modules(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) @torch.no_grad() def __call__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 250 , __SCREAMING_SNAKE_CASE = 0.0 , __SCREAMING_SNAKE_CASE = 10 , __SCREAMING_SNAKE_CASE = 10 , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = "pil" , __SCREAMING_SNAKE_CASE = True , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' __snake_case = image __snake_case = _preprocess_image(__SCREAMING_SNAKE_CASE ) __snake_case = original_image.to(device=self.device , dtype=self.unet.dtype ) __snake_case = _preprocess_mask(__SCREAMING_SNAKE_CASE ) __snake_case = mask_image.to(device=self.device , dtype=self.unet.dtype ) __snake_case = original_image.shape[0] # sample gaussian noise to begin the loop if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and len(__SCREAMING_SNAKE_CASE ) != batch_size: raise ValueError( F'''You have passed a list of generators of length {len(__SCREAMING_SNAKE_CASE )}, but requested an effective batch''' F''' size of {batch_size}. Make sure the batch size matches the length of the generators.''' ) __snake_case = original_image.shape __snake_case = randn_tensor(__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , device=self.device , dtype=self.unet.dtype ) # set step values self.scheduler.set_timesteps(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , self.device ) __snake_case = eta __snake_case = self.scheduler.timesteps[0] + 1 __snake_case = generator[0] if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else generator for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): if t < t_last: # predict the noise residual __snake_case = self.unet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).sample # compute previous image: x_t -> x_t-1 __snake_case = self.scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample else: # compute the reverse: x_t-1 -> x_t __snake_case = self.scheduler.undo_step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __snake_case = t __snake_case = (image / 2 + 0.5).clamp(0 , 1 ) __snake_case = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": __snake_case = self.numpy_to_pil(__SCREAMING_SNAKE_CASE ) if not return_dict: return (image,) return ImagePipelineOutput(images=__SCREAMING_SNAKE_CASE )
24
0
import baseaa import io import json import os from copy import deepcopy from ..optimizer import AcceleratedOptimizer from ..scheduler import AcceleratedScheduler class __snake_case : '''simple docstring''' def __init__( self , A_ ): '''simple docstring''' if isinstance(A_ , A_ ): # 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 SCREAMING_SNAKE_CASE__ = deepcopy(A_ ) elif os.path.exists(A_ ): with io.open(A_ , '''r''' , encoding='''utf-8''' ) as f: SCREAMING_SNAKE_CASE__ = json.load(A_ ) else: try: SCREAMING_SNAKE_CASE__ = baseaa.urlsafe_baadecode(A_ ).decode('''utf-8''' ) SCREAMING_SNAKE_CASE__ = json.loads(A_ ) 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}''' ) SCREAMING_SNAKE_CASE__ = config self.set_stage_and_offload() def lowercase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = self.get_value('''zero_optimization.stage''' , -1 ) # offload SCREAMING_SNAKE_CASE__ = False if self.is_zeroa() or self.is_zeroa(): SCREAMING_SNAKE_CASE__ = set(['''cpu''', '''nvme'''] ) SCREAMING_SNAKE_CASE__ = 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: SCREAMING_SNAKE_CASE__ = True def lowercase_ ( self , A_ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = self.config # find the config node of interest if it exists SCREAMING_SNAKE_CASE__ = ds_key_long.split('''.''' ) SCREAMING_SNAKE_CASE__ = nodes.pop() for node in nodes: SCREAMING_SNAKE_CASE__ = config.get(A_ ) if config is None: return None, ds_key return config, ds_key def lowercase_ ( self , A_ , A_=None ): '''simple docstring''' SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = self.find_config_node(A_ ) if config is None: return default return config.get(A_ , A_ ) def lowercase_ ( self , A_ , A_=False ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = self.config # find the config node of interest if it exists SCREAMING_SNAKE_CASE__ = ds_key_long.split('''.''' ) for node in nodes: SCREAMING_SNAKE_CASE__ = config SCREAMING_SNAKE_CASE__ = config.get(A_ ) 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(A_ ) def lowercase_ ( self , A_ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = self.get_value(A_ ) return False if value is None else bool(A_ ) def lowercase_ ( self , A_ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = self.get_value(A_ ) return False if value is None else not bool(A_ ) def lowercase_ ( self ): '''simple docstring''' return self._stage == 2 def lowercase_ ( self ): '''simple docstring''' return self._stage == 3 def lowercase_ ( self ): '''simple docstring''' return self._offload class __snake_case : '''simple docstring''' def __init__( self , A_ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = engine def lowercase_ ( self , A_ , **A_ ): '''simple docstring''' self.engine.backward(A_ , **A_ ) # 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 __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , A_ ): '''simple docstring''' super().__init__(A_ , device_placement=A_ , scaler=A_ ) SCREAMING_SNAKE_CASE__ = hasattr(self.optimizer , '''overflow''' ) def lowercase_ ( self , A_=None ): '''simple docstring''' pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed def lowercase_ ( self ): '''simple docstring''' pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed @property def lowercase_ ( self ): '''simple docstring''' if self.__has_overflow__: return self.optimizer.overflow return False class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , A_ , A_ ): '''simple docstring''' super().__init__(A_ , A_ ) def lowercase_ ( self ): '''simple docstring''' pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed class __snake_case : '''simple docstring''' def __init__( self , A_ , A_=0.001 , A_=0 , **A_ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = params SCREAMING_SNAKE_CASE__ = lr SCREAMING_SNAKE_CASE__ = weight_decay SCREAMING_SNAKE_CASE__ = kwargs class __snake_case : '''simple docstring''' def __init__( self , A_ , A_=None , A_=0 , **A_ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ = optimizer SCREAMING_SNAKE_CASE__ = total_num_steps SCREAMING_SNAKE_CASE__ = warmup_num_steps SCREAMING_SNAKE_CASE__ = kwargs
100
'''simple docstring''' from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax UpperCAmelCase_ : str = logging.get_logger(__name__) @add_end_docstrings(__lowerCAmelCase) class lowerCAmelCase ( __lowerCAmelCase): def __init__( self , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' super().__init__(**__SCREAMING_SNAKE_CASE ) 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 , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> List[Any]: '''simple docstring''' return super().__call__(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , **__SCREAMING_SNAKE_CASE ) -> Tuple: '''simple docstring''' __snake_case = {} if "candidate_labels" in kwargs: __snake_case = kwargs['''candidate_labels'''] if "hypothesis_template" in kwargs: __snake_case = kwargs['''hypothesis_template'''] return preprocess_params, {}, {} def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="This is a photo of {}." ) -> Optional[Any]: '''simple docstring''' __snake_case = load_image(__SCREAMING_SNAKE_CASE ) __snake_case = self.image_processor(images=[image] , return_tensors=self.framework ) __snake_case = candidate_labels __snake_case = [hypothesis_template.format(__SCREAMING_SNAKE_CASE ) for x in candidate_labels] __snake_case = self.tokenizer(__SCREAMING_SNAKE_CASE , return_tensors=self.framework , padding=__SCREAMING_SNAKE_CASE ) __snake_case = [text_inputs] return inputs def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' __snake_case = model_inputs.pop('''candidate_labels''' ) __snake_case = model_inputs.pop('''text_inputs''' ) if isinstance(text_inputs[0] , __SCREAMING_SNAKE_CASE ): __snake_case = text_inputs[0] else: # Batching case. __snake_case = text_inputs[0][0] __snake_case = self.model(**__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) __snake_case = { '''candidate_labels''': candidate_labels, '''logits''': outputs.logits_per_image, } return model_outputs def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> str: '''simple docstring''' __snake_case = model_outputs.pop('''candidate_labels''' ) __snake_case = model_outputs['''logits'''][0] if self.framework == "pt": __snake_case = logits.softmax(dim=-1 ).squeeze(-1 ) __snake_case = probs.tolist() if not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): __snake_case = [scores] elif self.framework == "tf": __snake_case = stable_softmax(__SCREAMING_SNAKE_CASE , axis=-1 ) __snake_case = probs.numpy().tolist() else: raise ValueError(F'''Unsupported framework: {self.framework}''' ) __snake_case = [ {'''score''': score, '''label''': candidate_label} for score, candidate_label in sorted(zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) , key=lambda __SCREAMING_SNAKE_CASE : -x[0] ) ] return result
24
0
import copy import fnmatch import json import os import pickle as pkl import shutil import sys import tarfile import tempfile from collections import OrderedDict from contextlib import contextmanager from functools import partial from hashlib import shaaaa from io import BytesIO from pathlib import Path from urllib.parse import urlparse from zipfile import ZipFile, is_zipfile import cva import numpy as np import requests import wget from filelock import FileLock from PIL import Image from tqdm.auto import tqdm from yaml import Loader, dump, load try: import torch lowerCAmelCase__ : Optional[Any] =True except ImportError: lowerCAmelCase__ : List[Any] =False try: from torch.hub import _get_torch_home lowerCAmelCase__ : Optional[int] =_get_torch_home() except ImportError: lowerCAmelCase__ : Dict =os.path.expanduser( os.getenv('TORCH_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'torch')) ) lowerCAmelCase__ : int =os.path.join(torch_cache_home, 'transformers') lowerCAmelCase__ : Optional[int] ='https://cdn.huggingface.co' lowerCAmelCase__ : str ='https://s3.amazonaws.com/models.huggingface.co/bert' lowerCAmelCase__ : Tuple ='/'.join(str(Path(__file__).resolve()).split('/')[:-1]) lowerCAmelCase__ : Union[str, Any] =os.path.join(PATH, 'config.yaml') lowerCAmelCase__ : Optional[int] =os.path.join(PATH, 'attributes.txt') lowerCAmelCase__ : Union[str, Any] =os.path.join(PATH, 'objects.txt') lowerCAmelCase__ : Dict =os.getenv('PYTORCH_PRETRAINED_BERT_CACHE', default_cache_path) lowerCAmelCase__ : Union[str, Any] =os.getenv('PYTORCH_TRANSFORMERS_CACHE', PYTORCH_PRETRAINED_BERT_CACHE) lowerCAmelCase__ : Tuple =os.getenv('TRANSFORMERS_CACHE', PYTORCH_TRANSFORMERS_CACHE) lowerCAmelCase__ : List[str] ='pytorch_model.bin' lowerCAmelCase__ : List[Any] ='config.yaml' def a__ ( A__=OBJECTS, A__=ATTRIBUTES ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = [] with open(A__ ) as f: for object in f.readlines(): vg_classes.append(object.split(',' )[0].lower().strip() ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = [] with open(A__ ) as f: for object in f.readlines(): vg_attrs.append(object.split(',' )[0].lower().strip() ) return vg_classes, vg_attrs def a__ ( A__ ): SCREAMING_SNAKE_CASE_ : Any = OrderedDict() with open(A__, 'rb' ) as f: SCREAMING_SNAKE_CASE_ : Dict = pkl.load(A__ )['model'] for k in copy.deepcopy(list(ckp.keys() ) ): SCREAMING_SNAKE_CASE_ : Tuple = ckp.pop(A__ ) if isinstance(A__, np.ndarray ): SCREAMING_SNAKE_CASE_ : Any = torch.tensor(A__ ) else: assert isinstance(A__, torch.tensor ), type(A__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = v return r class __lowercase : """simple docstring""" _UpperCAmelCase = {} def __init__( self , lowerCAmelCase__ , lowerCAmelCase__ = "root" , lowerCAmelCase__=0 ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = name SCREAMING_SNAKE_CASE_ : Union[str, Any] = level SCREAMING_SNAKE_CASE_ : Any = {} for k, v in dictionary.items(): if v is None: raise ValueError() SCREAMING_SNAKE_CASE_ : str = copy.deepcopy(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : List[Any] = copy.deepcopy(lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = Config(lowerCAmelCase__ , name=lowerCAmelCase__ , level=level + 1 ) SCREAMING_SNAKE_CASE_ : Dict = v setattr(self , lowerCAmelCase__ , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = d def __repr__( self ): """simple docstring""" return str(list((self._pointer.keys()) ) ) def __setattr__( self , lowerCAmelCase__ , lowerCAmelCase__ ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = val SCREAMING_SNAKE_CASE_ : Tuple = val SCREAMING_SNAKE_CASE_ : List[str] = key.split('.' ) SCREAMING_SNAKE_CASE_ : Tuple = len(lowerCAmelCase__ ) - 1 SCREAMING_SNAKE_CASE_ : str = self._pointer if len(lowerCAmelCase__ ) > 1: for i, l in enumerate(lowerCAmelCase__ ): if hasattr(self , lowerCAmelCase__ ) and isinstance(getattr(self , lowerCAmelCase__ ) , lowerCAmelCase__ ): setattr(getattr(self , lowerCAmelCase__ ) , '.'.join(levels[i:] ) , lowerCAmelCase__ ) if l == last_level: SCREAMING_SNAKE_CASE_ : Dict = val else: SCREAMING_SNAKE_CASE_ : int = pointer[l] def UpperCamelCase__ ( self ): """simple docstring""" return self._pointer def UpperCamelCase__ ( self , lowerCAmelCase__ , lowerCAmelCase__ ): """simple docstring""" with open(F'''{file_name}''' , 'w' ) as stream: dump(lowerCAmelCase__ , lowerCAmelCase__ ) def UpperCamelCase__ ( self , lowerCAmelCase__ , lowerCAmelCase__ ): """simple docstring""" with open(F'''{file_name}''' , 'w' ) as stream: json.dump(lowerCAmelCase__ , lowerCAmelCase__ ) @staticmethod def UpperCamelCase__ ( lowerCAmelCase__ ): """simple docstring""" with open(lowerCAmelCase__ ) as stream: SCREAMING_SNAKE_CASE_ : Optional[int] = load(lowerCAmelCase__ , Loader=lowerCAmelCase__ ) return data def __str__( self ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = ' ' if self._name != "root": SCREAMING_SNAKE_CASE_ : int = F'''{t * (self._level-1)}{self._name}:\n''' else: SCREAMING_SNAKE_CASE_ : str = '' SCREAMING_SNAKE_CASE_ : Union[str, Any] = self._level for i, (k, v) in enumerate(self._pointer.items() ): if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): r += F'''{t * (self._level)}{v}\n''' self._level += 1 else: r += F'''{t * (self._level)}{k}: {v} ({type(lowerCAmelCase__ ).__name__})\n''' SCREAMING_SNAKE_CASE_ : Tuple = level return r[:-1] @classmethod def UpperCamelCase__ ( cls , lowerCAmelCase__ , **lowerCAmelCase__ ): """simple docstring""" SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Tuple = cls.get_config_dict(lowerCAmelCase__ , **lowerCAmelCase__ ) return cls(lowerCAmelCase__ ) @classmethod def UpperCamelCase__ ( cls , lowerCAmelCase__ , **lowerCAmelCase__ ): """simple docstring""" SCREAMING_SNAKE_CASE_ : int = kwargs.pop('cache_dir' , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : Tuple = kwargs.pop('force_download' , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = kwargs.pop('resume_download' , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : int = kwargs.pop('proxies' , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : str = kwargs.pop('local_files_only' , lowerCAmelCase__ ) if os.path.isdir(lowerCAmelCase__ ): SCREAMING_SNAKE_CASE_ : List[str] = os.path.join(lowerCAmelCase__ , lowerCAmelCase__ ) elif os.path.isfile(lowerCAmelCase__ ) or is_remote_url(lowerCAmelCase__ ): SCREAMING_SNAKE_CASE_ : str = pretrained_model_name_or_path else: SCREAMING_SNAKE_CASE_ : Any = hf_bucket_url(lowerCAmelCase__ , filename=lowerCAmelCase__ , use_cdn=lowerCAmelCase__ ) try: # Load from URL or cache if already cached SCREAMING_SNAKE_CASE_ : Any = cached_path( lowerCAmelCase__ , cache_dir=lowerCAmelCase__ , force_download=lowerCAmelCase__ , proxies=lowerCAmelCase__ , resume_download=lowerCAmelCase__ , local_files_only=lowerCAmelCase__ , ) # Load config dict if resolved_config_file is None: raise EnvironmentError SCREAMING_SNAKE_CASE_ : Optional[int] = Config.load_yaml(lowerCAmelCase__ ) except EnvironmentError: SCREAMING_SNAKE_CASE_ : Tuple = 'Can\'t load config for' raise EnvironmentError(lowerCAmelCase__ ) if resolved_config_file == config_file: print('loading configuration file from path' ) else: print('loading configuration file cache' ) return Config.load_yaml(lowerCAmelCase__ ), kwargs def a__ ( A__ ): SCREAMING_SNAKE_CASE_ : str = torch.load('dump.pt', map_location=in_tensor.device ) SCREAMING_SNAKE_CASE_ : int = in_tensor.numpy() SCREAMING_SNAKE_CASE_ : Any = out_tensor.numpy()[0] print(na.shape, na[0, 0, :5] ) print(na.shape, na[0, 0, :5] ) assert np.allclose(A__, A__, rtol=0.01, atol=0.1 ), ( F'''{sum([1 for x in np.isclose(A__, A__, rtol=0.01, atol=0.1 ).flatten() if x is False] )/len(na.flatten() )*1_0_0:.4f} %''' " element-wise mismatch" ) raise Exception('tensors are all good' ) # Hugging face functions below def a__ ( A__ ): SCREAMING_SNAKE_CASE_ : int = urlparse(A__ ) return parsed.scheme in ("http", "https") def a__ ( A__, A__, A__=True ): SCREAMING_SNAKE_CASE_ : Optional[int] = CLOUDFRONT_DISTRIB_PREFIX if use_cdn else S3_BUCKET_PREFIX SCREAMING_SNAKE_CASE_ : Optional[int] = '/' not in model_id if legacy_format: return F'''{endpoint}/{model_id}-{filename}''' else: return F'''{endpoint}/{model_id}/{filename}''' def a__ ( A__, A__, A__=None, A__=0, A__=None, ): SCREAMING_SNAKE_CASE_ : Any = 'python/{}'.format(sys.version.split()[0] ) if _torch_available: ua += "; torch/{}".format(torch.__version__ ) if isinstance(A__, A__ ): ua += "; " + "; ".join('{}/{}'.format(A__, A__ ) for k, v in user_agent.items() ) elif isinstance(A__, A__ ): ua += "; " + user_agent SCREAMING_SNAKE_CASE_ : str = {'user-agent': ua} if resume_size > 0: SCREAMING_SNAKE_CASE_ : Tuple = 'bytes=%d-' % (resume_size,) SCREAMING_SNAKE_CASE_ : Dict = requests.get(A__, stream=A__, proxies=A__, headers=A__ ) if response.status_code == 4_1_6: # Range not satisfiable return SCREAMING_SNAKE_CASE_ : int = response.headers.get('Content-Length' ) SCREAMING_SNAKE_CASE_ : Optional[int] = resume_size + int(A__ ) if content_length is not None else None SCREAMING_SNAKE_CASE_ : List[str] = tqdm( unit='B', unit_scale=A__, total=A__, initial=A__, desc='Downloading', ) for chunk in response.iter_content(chunk_size=1_0_2_4 ): if chunk: # filter out keep-alive new chunks progress.update(len(A__ ) ) temp_file.write(A__ ) progress.close() def a__ ( A__, A__=None, A__=False, A__=None, A__=1_0, A__=False, A__=None, A__=False, ): if cache_dir is None: SCREAMING_SNAKE_CASE_ : List[str] = TRANSFORMERS_CACHE if isinstance(A__, A__ ): SCREAMING_SNAKE_CASE_ : int = str(A__ ) os.makedirs(A__, exist_ok=A__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = None if not local_files_only: try: SCREAMING_SNAKE_CASE_ : Any = requests.head(A__, allow_redirects=A__, proxies=A__, timeout=A__ ) if response.status_code == 2_0_0: SCREAMING_SNAKE_CASE_ : Any = response.headers.get('ETag' ) except (EnvironmentError, requests.exceptions.Timeout): # etag is already None pass SCREAMING_SNAKE_CASE_ : List[Any] = url_to_filename(A__, A__ ) # get cache path to put the file SCREAMING_SNAKE_CASE_ : int = os.path.join(A__, A__ ) # etag is None = we don't have a connection, or url doesn't exist, or is otherwise inaccessible. # try to get the last downloaded one if etag is None: if os.path.exists(A__ ): return cache_path else: SCREAMING_SNAKE_CASE_ : str = [ file for file in fnmatch.filter(os.listdir(A__ ), filename + '.*' ) if not file.endswith('.json' ) and not file.endswith('.lock' ) ] if len(A__ ) > 0: return os.path.join(A__, matching_files[-1] ) else: # If files cannot be found and local_files_only=True, # the models might've been found if local_files_only=False # Notify the user about that if local_files_only: raise ValueError( 'Cannot find the requested files in the cached path and outgoing traffic has been' ' disabled. To enable model look-ups and downloads online, set \'local_files_only\'' ' to False.' ) return None # From now on, etag is not None. if os.path.exists(A__ ) and not force_download: return cache_path # Prevent parallel downloads of the same file with a lock. SCREAMING_SNAKE_CASE_ : Optional[Any] = cache_path + '.lock' with FileLock(A__ ): # If the download just completed while the lock was activated. if os.path.exists(A__ ) and not force_download: # Even if returning early like here, the lock will be released. return cache_path if resume_download: SCREAMING_SNAKE_CASE_ : List[str] = cache_path + '.incomplete' @contextmanager def _resumable_file_manager(): with open(A__, 'a+b' ) as f: yield f SCREAMING_SNAKE_CASE_ : Any = _resumable_file_manager if os.path.exists(A__ ): SCREAMING_SNAKE_CASE_ : int = os.stat(A__ ).st_size else: SCREAMING_SNAKE_CASE_ : Optional[Any] = 0 else: SCREAMING_SNAKE_CASE_ : Any = partial(tempfile.NamedTemporaryFile, dir=A__, delete=A__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = 0 # Download to temporary file, then copy to cache dir once finished. # Otherwise you get corrupt cache entries if the download gets interrupted. with temp_file_manager() as temp_file: print( '%s not found in cache or force_download set to True, downloading to %s', A__, temp_file.name, ) http_get( A__, A__, proxies=A__, resume_size=A__, user_agent=A__, ) os.replace(temp_file.name, A__ ) SCREAMING_SNAKE_CASE_ : Tuple = {'url': url, 'etag': etag} SCREAMING_SNAKE_CASE_ : int = cache_path + '.json' with open(A__, 'w' ) as meta_file: json.dump(A__, A__ ) return cache_path def a__ ( A__, A__=None ): SCREAMING_SNAKE_CASE_ : List[str] = url.encode('utf-8' ) SCREAMING_SNAKE_CASE_ : List[Any] = shaaaa(A__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = url_hash.hexdigest() if etag: SCREAMING_SNAKE_CASE_ : int = etag.encode('utf-8' ) SCREAMING_SNAKE_CASE_ : Any = shaaaa(A__ ) filename += "." + etag_hash.hexdigest() if url.endswith('.h5' ): filename += ".h5" return filename def a__ ( A__, A__=None, A__=False, A__=None, A__=False, A__=None, A__=False, A__=False, A__=False, ): if cache_dir is None: SCREAMING_SNAKE_CASE_ : Tuple = TRANSFORMERS_CACHE if isinstance(A__, A__ ): SCREAMING_SNAKE_CASE_ : Tuple = str(A__ ) if isinstance(A__, A__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = str(A__ ) if is_remote_url(A__ ): # URL, so get it from the cache (downloading if necessary) SCREAMING_SNAKE_CASE_ : Optional[Any] = get_from_cache( A__, cache_dir=A__, force_download=A__, proxies=A__, resume_download=A__, user_agent=A__, local_files_only=A__, ) elif os.path.exists(A__ ): # File, and it exists. SCREAMING_SNAKE_CASE_ : List[Any] = url_or_filename elif urlparse(A__ ).scheme == "": # File, but it doesn't exist. raise EnvironmentError('file {} not found'.format(A__ ) ) else: # Something unknown raise ValueError('unable to parse {} as a URL or as a local path'.format(A__ ) ) if extract_compressed_file: if not is_zipfile(A__ ) and not tarfile.is_tarfile(A__ ): return output_path # Path where we extract compressed archives # We avoid '.' in dir name and add "-extracted" at the end: "./model.zip" => "./model-zip-extracted/" SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Dict = os.path.split(A__ ) SCREAMING_SNAKE_CASE_ : Tuple = output_file.replace('.', '-' ) + '-extracted' SCREAMING_SNAKE_CASE_ : Optional[int] = os.path.join(A__, A__ ) if os.path.isdir(A__ ) and os.listdir(A__ ) and not force_extract: return output_path_extracted # Prevent parallel extractions SCREAMING_SNAKE_CASE_ : Optional[Any] = output_path + '.lock' with FileLock(A__ ): shutil.rmtree(A__, ignore_errors=A__ ) os.makedirs(A__ ) if is_zipfile(A__ ): with ZipFile(A__, 'r' ) as zip_file: zip_file.extractall(A__ ) zip_file.close() elif tarfile.is_tarfile(A__ ): SCREAMING_SNAKE_CASE_ : List[Any] = tarfile.open(A__ ) tar_file.extractall(A__ ) tar_file.close() else: raise EnvironmentError('Archive format of {} could not be identified'.format(A__ ) ) return output_path_extracted return output_path def a__ ( A__, A__="," ): assert isinstance(A__, A__ ) if os.path.isfile(A__ ): with open(A__ ) as f: SCREAMING_SNAKE_CASE_ : Dict = eval(f.read() ) else: SCREAMING_SNAKE_CASE_ : Tuple = requests.get(A__ ) try: SCREAMING_SNAKE_CASE_ : Union[str, Any] = requests.json() except Exception: SCREAMING_SNAKE_CASE_ : Optional[int] = req.content.decode() assert data is not None, "could not connect" try: SCREAMING_SNAKE_CASE_ : Dict = eval(A__ ) except Exception: SCREAMING_SNAKE_CASE_ : Any = data.split('\n' ) req.close() return data def a__ ( A__ ): SCREAMING_SNAKE_CASE_ : Any = requests.get(A__ ) SCREAMING_SNAKE_CASE_ : str = np.array(Image.open(BytesIO(response.content ) ) ) return img def a__ ( A__ ): SCREAMING_SNAKE_CASE_ : Optional[int] = url.split('/' )[-1] if fn not in os.listdir(os.getcwd() ): wget.download(A__ ) with open(A__, 'rb' ) as stream: SCREAMING_SNAKE_CASE_ : List[str] = pkl.load(A__ ) SCREAMING_SNAKE_CASE_ : List[Any] = weights.pop('model' ) SCREAMING_SNAKE_CASE_ : Dict = {} for k, v in model.items(): SCREAMING_SNAKE_CASE_ : Tuple = torch.from_numpy(A__ ) if "running_var" in k: SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.tensor([0] ) SCREAMING_SNAKE_CASE_ : Tuple = k.replace('running_var', 'num_batches_tracked' ) SCREAMING_SNAKE_CASE_ : List[str] = zero return new def a__ ( ): print(F'''{os.path.abspath(os.path.join(A__, os.pardir ) )}/demo.ipynb''' ) def a__ ( A__, A__="RGB" ): assert isinstance(A__, A__ ) if os.path.isfile(A__ ): SCREAMING_SNAKE_CASE_ : Tuple = cva.imread(A__ ) else: SCREAMING_SNAKE_CASE_ : Optional[Any] = get_image_from_url(A__ ) assert img is not None, F'''could not connect to: {im}''' SCREAMING_SNAKE_CASE_ : Tuple = cva.cvtColor(A__, cva.COLOR_BGR2RGB ) if input_format == "RGB": SCREAMING_SNAKE_CASE_ : Optional[Any] = img[:, :, ::-1] return img def a__ ( A__, A__=1 ): return (images[i : i + batch] for i in range(0, len(A__ ), A__ ))
101
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) UpperCAmelCase_ : List[str] = { '''configuration_gpt_bigcode''': ['''GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GPTBigCodeConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : int = [ '''GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST''', '''GPTBigCodeForSequenceClassification''', '''GPTBigCodeForTokenClassification''', '''GPTBigCodeForCausalLM''', '''GPTBigCodeModel''', '''GPTBigCodePreTrainedModel''', ] if TYPE_CHECKING: from .configuration_gpt_bigcode import GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTBigCodeConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_bigcode import ( GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTBigCodeForCausalLM, GPTBigCodeForSequenceClassification, GPTBigCodeForTokenClassification, GPTBigCodeModel, GPTBigCodePreTrainedModel, ) else: import sys UpperCAmelCase_ : Any = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
24
0
"""simple docstring""" from collections import OrderedDict from typing import Any, List, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import logging __magic_name__ : List[str] = logging.get_logger(__name__) __magic_name__ : Any = { """EleutherAI/gpt-j-6B""": """https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json""", # See all GPT-J models at https://huggingface.co/models?filter=gpt_j } class lowercase__ ( __SCREAMING_SNAKE_CASE ): """simple docstring""" __lowerCAmelCase : Optional[int] = """gptj""" __lowerCAmelCase : Optional[Any] = { """max_position_embeddings""": """n_positions""", """hidden_size""": """n_embd""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self , _A=5_0_4_0_0 , _A=2_0_4_8 , _A=4_0_9_6 , _A=2_8 , _A=1_6 , _A=6_4 , _A=None , _A="gelu_new" , _A=0.0 , _A=0.0 , _A=0.0 , _A=1e-5 , _A=0.02 , _A=True , _A=5_0_2_5_6 , _A=5_0_2_5_6 , _A=False , **_A , ): '''simple docstring''' UpperCamelCase : Tuple = vocab_size UpperCamelCase : Any = n_positions UpperCamelCase : List[str] = n_embd UpperCamelCase : List[str] = n_layer UpperCamelCase : Optional[int] = n_head UpperCamelCase : int = n_inner UpperCamelCase : Optional[Any] = rotary_dim UpperCamelCase : Optional[int] = activation_function UpperCamelCase : str = resid_pdrop UpperCamelCase : Union[str, Any] = embd_pdrop UpperCamelCase : Optional[Any] = attn_pdrop UpperCamelCase : Optional[int] = layer_norm_epsilon UpperCamelCase : Any = initializer_range UpperCamelCase : Optional[int] = use_cache UpperCamelCase : List[Any] = bos_token_id UpperCamelCase : List[str] = eos_token_id super().__init__( bos_token_id=_A , eos_token_id=_A , tie_word_embeddings=_A , **_A ) class lowercase__ ( __SCREAMING_SNAKE_CASE ): """simple docstring""" def __init__( self , _A , _A = "default" , _A = None , _A = False , ): '''simple docstring''' super().__init__(_A , task=_A , patching_specs=_A , use_past=_A ) if not getattr(self._config , """pad_token_id""" , _A ): # TODO: how to do that better? UpperCamelCase : Optional[Any] = 0 @property def _a ( self ): '''simple docstring''' UpperCamelCase : List[str] = OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}} ) if self.use_past: self.fill_with_past_key_values_(_A , direction="""inputs""" ) UpperCamelCase : Any = {0: """batch""", 1: """past_sequence + sequence"""} else: UpperCamelCase : List[Any] = {0: """batch""", 1: """sequence"""} return common_inputs @property def _a ( self ): '''simple docstring''' return self._config.n_layer @property def _a ( self ): '''simple docstring''' return self._config.n_head def _a ( self , _A , _A = -1 , _A = -1 , _A = False , _A = None , ): '''simple docstring''' UpperCamelCase : Optional[Any] = super(_A , self ).generate_dummy_inputs( _A , batch_size=_A , seq_length=_A , is_pair=_A , framework=_A ) # We need to order the input in the way they appears in the forward() UpperCamelCase : Optional[Any] = OrderedDict({"""input_ids""": common_inputs["""input_ids"""]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed.""" ) else: import torch UpperCamelCase , UpperCamelCase : Dict = common_inputs["""input_ids"""].shape # Not using the same length for past_key_values UpperCamelCase : Dict = seqlen + 2 UpperCamelCase : Union[str, Any] = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) UpperCamelCase : List[Any] = [ (torch.zeros(_A ), torch.zeros(_A )) for _ in range(self.num_layers ) ] UpperCamelCase : str = common_inputs["""attention_mask"""] if self.use_past: UpperCamelCase : Any = ordered_inputs["""attention_mask"""].dtype UpperCamelCase : List[Any] = torch.cat( [ordered_inputs["""attention_mask"""], torch.ones(_A , _A , dtype=_A )] , dim=1 ) return ordered_inputs @property def _a ( self ): '''simple docstring''' return 1_3
102
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' __snake_case = abs(_lowerCamelCase ) __snake_case = 0 while n > 0: res += n % 10 n //= 10 return res def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' __snake_case = abs(_lowerCamelCase ) return n if n < 10 else n % 10 + sum_of_digits(n // 10 ) def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' return sum(int(_lowerCamelCase ) for c in str(abs(_lowerCamelCase ) ) ) def _UpperCamelCase ()-> None: '''simple docstring''' from collections.abc import Callable from timeit import timeit def benchmark_a_function(_lowerCamelCase : Callable , _lowerCamelCase : int ) -> None: __snake_case = f'''{func.__name__}({value})''' __snake_case = timeit(f'''__main__.{call}''' , setup='''import __main__''' ) print(f'''{call:56} = {func(_lowerCamelCase )} -- {timing:.4f} seconds''' ) for value in (26_21_44, 11_25_89_99_06_84_26_24, 1_26_76_50_60_02_28_22_94_01_49_67_03_20_53_76): for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact): benchmark_a_function(_lowerCamelCase , _lowerCamelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
24
0
"""simple docstring""" import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation snake_case = logging.get_logger(__name__) snake_case = { '''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_config_file''': '''tokenizer_config.json''', } snake_case = { '''vocab_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json'''}, '''merges_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt'''}, '''tokenizer_config_file''': { '''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json''' }, } snake_case = {'''facebook/blenderbot-3B''': 1_2_8} class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): A__ : Dict = VOCAB_FILES_NAMES A__ : Optional[int] = PRETRAINED_VOCAB_FILES_MAP A__ : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ : int = ['''input_ids''', '''attention_mask'''] A__ : int = BlenderbotTokenizer def __init__( self : Union[str, Any] , __lowerCamelCase : Tuple=None , __lowerCamelCase : Optional[Any]=None , __lowerCamelCase : Optional[int]=None , __lowerCamelCase : List[str]="replace" , __lowerCamelCase : Dict="<s>" , __lowerCamelCase : Optional[Any]="</s>" , __lowerCamelCase : Dict="</s>" , __lowerCamelCase : Any="<s>" , __lowerCamelCase : int="<unk>" , __lowerCamelCase : str="<pad>" , __lowerCamelCase : str="<mask>" , __lowerCamelCase : Union[str, Any]=False , __lowerCamelCase : Optional[int]=True , **__lowerCamelCase : List[Any] , ): """simple docstring""" super().__init__( __lowerCamelCase , __lowerCamelCase , tokenizer_file=__lowerCamelCase , errors=__lowerCamelCase , bos_token=__lowerCamelCase , eos_token=__lowerCamelCase , sep_token=__lowerCamelCase , cls_token=__lowerCamelCase , unk_token=__lowerCamelCase , pad_token=__lowerCamelCase , mask_token=__lowerCamelCase , add_prefix_space=__lowerCamelCase , trim_offsets=__lowerCamelCase , **__lowerCamelCase , ) _snake_case = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , __lowerCamelCase ) != add_prefix_space: _snake_case = getattr(__lowerCamelCase , pre_tok_state.pop('''type''' ) ) _snake_case = add_prefix_space _snake_case = pre_tok_class(**__lowerCamelCase ) _snake_case = add_prefix_space _snake_case = '''post_processor''' _snake_case = getattr(self.backend_tokenizer , __lowerCamelCase , __lowerCamelCase ) if tokenizer_component_instance: _snake_case = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: _snake_case = tuple(state['''sep'''] ) if "cls" in state: _snake_case = tuple(state['''cls'''] ) _snake_case = False if state.get('''add_prefix_space''' , __lowerCamelCase ) != add_prefix_space: _snake_case = add_prefix_space _snake_case = True if state.get('''trim_offsets''' , __lowerCamelCase ) != trim_offsets: _snake_case = trim_offsets _snake_case = True if changes_to_apply: _snake_case = getattr(__lowerCamelCase , state.pop('''type''' ) ) _snake_case = component_class(**__lowerCamelCase ) setattr(self.backend_tokenizer , __lowerCamelCase , __lowerCamelCase ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def __UpperCAmelCase ( self : Union[str, Any] ): """simple docstring""" if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def __UpperCAmelCase ( self : List[Any] , __lowerCamelCase : Any ): """simple docstring""" _snake_case = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else value _snake_case = value def __UpperCAmelCase ( self : Optional[int] , *__lowerCamelCase : Optional[int] , **__lowerCamelCase : Optional[Any] ): """simple docstring""" _snake_case = kwargs.get('''is_split_into_words''' , __lowerCamelCase ) assert self.add_prefix_space or not is_split_into_words, ( f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*__lowerCamelCase , **__lowerCamelCase ) def __UpperCAmelCase ( self : int , *__lowerCamelCase : Optional[Any] , **__lowerCamelCase : Optional[int] ): """simple docstring""" _snake_case = kwargs.get('''is_split_into_words''' , __lowerCamelCase ) assert self.add_prefix_space or not is_split_into_words, ( f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ "to use it with pretokenized inputs." ) return super()._encode_plus(*__lowerCamelCase , **__lowerCamelCase ) def __UpperCAmelCase ( self : Optional[int] , __lowerCamelCase : str , __lowerCamelCase : Optional[str] = None ): """simple docstring""" _snake_case = self._tokenizer.model.save(__lowerCamelCase , name=__lowerCamelCase ) return tuple(__lowerCamelCase ) def __UpperCAmelCase ( self : List[Any] , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None ): """simple docstring""" _snake_case = [self.sep_token_id] _snake_case = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __UpperCAmelCase ( self : List[Any] , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None ): """simple docstring""" return token_ids_a + [self.eos_token_id] def __UpperCAmelCase ( self : str , __lowerCamelCase : "Conversation" ): """simple docstring""" _snake_case = [] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(''' ''' + text ) else: # Generated responses should contain them already. inputs.append(__lowerCamelCase ) _snake_case = ''' '''.join(__lowerCamelCase ) _snake_case = self.encode(__lowerCamelCase ) if len(__lowerCamelCase ) > self.model_max_length: _snake_case = input_ids[-self.model_max_length :] logger.warning(f"""Trimmed input from conversation as it was longer than {self.model_max_length} tokens.""" ) return input_ids
103
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : Optional[Any] )-> Dict: '''simple docstring''' __snake_case = [] __snake_case = [] __snake_case = { '''^''': 3, '''*''': 2, '''/''': 2, '''%''': 2, '''+''': 1, '''-''': 1, } # Priority of each operator __snake_case = len(_lowerCamelCase ) if (len(_lowerCamelCase ) > 7) else 7 # Print table header for output print( '''Symbol'''.center(8 ) , '''Stack'''.center(_lowerCamelCase ) , '''Postfix'''.center(_lowerCamelCase ) , sep=''' | ''' , ) print('''-''' * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(_lowerCamelCase ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(_lowerCamelCase ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(_lowerCamelCase ) == 0: stack.append(_lowerCamelCase ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(_lowerCamelCase ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(_lowerCamelCase ) # push x to stack print( x.center(8 ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , sep=''' | ''' , ) # Output in tabular format while len(_lowerCamelCase ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( ''' '''.center(8 ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , (''''''.join(_lowerCamelCase )).ljust(_lowerCamelCase ) , sep=''' | ''' , ) # Output in tabular format return "".join(_lowerCamelCase ) # return Postfix as str def _UpperCamelCase (_lowerCamelCase : Optional[Any] )-> str: '''simple docstring''' __snake_case = list(infix[::-1] ) # reverse the infix equation for i in range(len(_lowerCamelCase ) ): if infix[i] == "(": __snake_case = ''')''' # change "(" to ")" elif infix[i] == ")": __snake_case = '''(''' # change ")" to "(" return (infix_2_postfix(''''''.join(_lowerCamelCase ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": UpperCAmelCase_ : Dict = input('''\nEnter an Infix Equation = ''') # Input an Infix equation UpperCAmelCase_ : Optional[Any] = ''''''.join(Infix.split()) # Remove spaces from the input print('''\n\t''', Infix, '''(Infix) -> ''', infix_2_prefix(Infix), '''(Prefix)''')
24
0
"""simple docstring""" def _lowerCamelCase ( UpperCAmelCase_ : int, UpperCAmelCase_ : int ) -> int: """simple docstring""" return int(input_a == input_a == 0 ) def _lowerCamelCase ( ) -> 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()
104
'''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 ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices UpperCAmelCase_ : int = logging.get_logger(__name__) UpperCAmelCase_ : Dict = { '''microsoft/swin-tiny-patch4-window7-224''': ( '''https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json''' ), # See all Swin models at https://huggingface.co/models?filter=swin } class lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase): __lowercase : List[Any] = '''swin''' __lowercase : str = { '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self , __SCREAMING_SNAKE_CASE=224 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=96 , __SCREAMING_SNAKE_CASE=[2, 2, 6, 2] , __SCREAMING_SNAKE_CASE=[3, 6, 12, 24] , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=4.0 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1E-5 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ) -> int: '''simple docstring''' super().__init__(**__SCREAMING_SNAKE_CASE ) __snake_case = image_size __snake_case = patch_size __snake_case = num_channels __snake_case = embed_dim __snake_case = depths __snake_case = len(__SCREAMING_SNAKE_CASE ) __snake_case = num_heads __snake_case = window_size __snake_case = mlp_ratio __snake_case = qkv_bias __snake_case = hidden_dropout_prob __snake_case = attention_probs_dropout_prob __snake_case = drop_path_rate __snake_case = hidden_act __snake_case = use_absolute_embeddings __snake_case = layer_norm_eps __snake_case = initializer_range __snake_case = encoder_stride # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model __snake_case = int(embed_dim * 2 ** (len(__SCREAMING_SNAKE_CASE ) - 1) ) __snake_case = ['''stem'''] + [F'''stage{idx}''' for idx in range(1 , len(__SCREAMING_SNAKE_CASE ) + 1 )] __snake_case , __snake_case = get_aligned_output_features_output_indices( out_features=__SCREAMING_SNAKE_CASE , out_indices=__SCREAMING_SNAKE_CASE , stage_names=self.stage_names ) class lowerCAmelCase ( __lowerCAmelCase): __lowercase : Optional[int] = version.parse('''1.11''') @property def lowerCAmelCase ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def lowerCAmelCase ( self ) -> float: '''simple docstring''' return 1E-4
24
0
def __UpperCAmelCase ( lowerCamelCase_ : str ) -> list: """simple docstring""" if n_term == "": return [] SCREAMING_SNAKE_CASE_ : list = [] for temp in range(int(lowerCamelCase_ ) ): series.append(F'1/{temp + 1}' if series else '1' ) return series if __name__ == "__main__": UpperCamelCase__ : int = input('''Enter the last number (nth term) of the Harmonic Series''') print('''Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n''') print(harmonic_series(nth_term))
105
'''simple docstring''' import re import time from typing import Optional import IPython.display as disp from ..trainer_callback import TrainerCallback from ..trainer_utils import IntervalStrategy, has_length def _UpperCamelCase (_lowerCamelCase : Tuple )-> List[str]: '''simple docstring''' __snake_case = int(_lowerCamelCase ) __snake_case , __snake_case , __snake_case = t // 36_00, (t // 60) % 60, t % 60 return f'''{h}:{m:02d}:{s:02d}''' if h != 0 else f'''{m:02d}:{s:02d}''' def _UpperCamelCase (_lowerCamelCase : Any , _lowerCamelCase : Union[str, Any] , _lowerCamelCase : str , _lowerCamelCase : int , _lowerCamelCase : List[Any]=3_00 )-> int: '''simple docstring''' return f''' <div> {prefix} <progress value=\'{value}\' max=\'{total}\' style=\'width:{width}px; height:20px; vertical-align: middle;\'></progress> {label} </div> ''' def _UpperCamelCase (_lowerCamelCase : int )-> List[Any]: '''simple docstring''' __snake_case = '''<table border="1" class="dataframe">\n''' html_code += """ <thead>\n <tr style="text-align: left;">\n""" for i in items[0]: html_code += f''' <th>{i}</th>\n''' html_code += " </tr>\n </thead>\n <tbody>\n" for line in items[1:]: html_code += " <tr>\n" for elt in line: __snake_case = f'''{elt:.6f}''' if isinstance(_lowerCamelCase , _lowerCamelCase ) else str(_lowerCamelCase ) html_code += f''' <td>{elt}</td>\n''' html_code += " </tr>\n" html_code += " </tbody>\n</table><p>" return html_code class lowerCAmelCase : __lowercase : str = 5 __lowercase : Optional[Any] = 0.2 def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = 300 , ) -> List[Any]: '''simple docstring''' __snake_case = total __snake_case = '''''' if prefix is None else prefix __snake_case = leave __snake_case = parent __snake_case = width __snake_case = None __snake_case = None __snake_case = None def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = None ) -> Any: '''simple docstring''' __snake_case = value if comment is not None: __snake_case = comment if self.last_value is None: __snake_case = __snake_case = time.time() __snake_case = __snake_case = value __snake_case = __snake_case = None __snake_case = self.warmup __snake_case = 1 self.update_bar(__SCREAMING_SNAKE_CASE ) elif value <= self.last_value and not force_update: return elif force_update or self.first_calls > 0 or value >= min(self.last_value + self.wait_for , self.total ): if self.first_calls > 0: self.first_calls -= 1 __snake_case = time.time() __snake_case = current_time - self.start_time # We could have value = self.start_value if the update is called twixe with the same start value. if value > self.start_value: __snake_case = self.elapsed_time / (value - self.start_value) else: __snake_case = None if value >= self.total: __snake_case = self.total __snake_case = None if not self.leave: self.close() elif self.average_time_per_item is not None: __snake_case = self.average_time_per_item * (self.total - value) self.update_bar(__SCREAMING_SNAKE_CASE ) __snake_case = value __snake_case = current_time if self.average_time_per_item is None: __snake_case = 1 else: __snake_case = max(int(self.update_every / self.average_time_per_item ) , 1 ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) -> List[str]: '''simple docstring''' __snake_case = ''' ''' * (len(str(self.total ) ) - len(str(__SCREAMING_SNAKE_CASE ) )) + str(__SCREAMING_SNAKE_CASE ) if self.elapsed_time is None: __snake_case = F'''[{spaced_value}/{self.total} : < :''' elif self.predicted_remaining is None: __snake_case = F'''[{spaced_value}/{self.total} {format_time(self.elapsed_time )}''' else: __snake_case = ( F'''[{spaced_value}/{self.total} {format_time(self.elapsed_time )} <''' F''' {format_time(self.predicted_remaining )}''' ) self.label += F''', {1/self.average_time_per_item:.2f} it/s''' self.label += "]" if self.comment is None or len(self.comment ) == 0 else F''', {self.comment}]''' self.display() def lowerCAmelCase ( self ) -> List[Any]: '''simple docstring''' __snake_case = html_progress_bar(self.value , self.total , self.prefix , self.label , self.width ) if self.parent is not None: # If this is a child bar, the parent will take care of the display. self.parent.display() return if self.output is None: __snake_case = disp.display(disp.HTML(self.html_code ) , display_id=__SCREAMING_SNAKE_CASE ) else: self.output.update(disp.HTML(self.html_code ) ) def lowerCAmelCase ( self ) -> Dict: '''simple docstring''' if self.parent is None and self.output is not None: self.output.update(disp.HTML('''''' ) ) class lowerCAmelCase ( __lowerCAmelCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) -> Any: '''simple docstring''' super().__init__(__SCREAMING_SNAKE_CASE ) __snake_case = None if column_names is None else [column_names] __snake_case = None def lowerCAmelCase ( self ) -> Any: '''simple docstring''' __snake_case = html_progress_bar(self.value , self.total , self.prefix , self.label , self.width ) if self.inner_table is not None: self.html_code += text_to_html_table(self.inner_table ) if self.child_bar is not None: self.html_code += self.child_bar.html_code if self.output is None: __snake_case = disp.display(disp.HTML(self.html_code ) , display_id=__SCREAMING_SNAKE_CASE ) else: self.output.update(disp.HTML(self.html_code ) ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' if self.inner_table is None: __snake_case = [list(values.keys() ), list(values.values() )] else: __snake_case = self.inner_table[0] if len(self.inner_table ) == 1: # We give a chance to update the column names at the first iteration for key in values.keys(): if key not in columns: columns.append(__SCREAMING_SNAKE_CASE ) __snake_case = columns self.inner_table.append([values[c] for c in columns] ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=300 ) -> List[str]: '''simple docstring''' __snake_case = NotebookProgressBar(__SCREAMING_SNAKE_CASE , prefix=__SCREAMING_SNAKE_CASE , parent=self , width=__SCREAMING_SNAKE_CASE ) return self.child_bar def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case = None self.display() class lowerCAmelCase ( __lowerCAmelCase): def __init__( self ) -> str: '''simple docstring''' __snake_case = None __snake_case = None __snake_case = False def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' __snake_case = '''Epoch''' if args.evaluation_strategy == IntervalStrategy.EPOCH else '''Step''' __snake_case = 0 __snake_case = 0 __snake_case = [self.first_column] + ['''Training Loss'''] if args.evaluation_strategy != IntervalStrategy.NO: column_names.append('''Validation Loss''' ) __snake_case = NotebookTrainingTracker(state.max_steps , __SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> List[Any]: '''simple docstring''' __snake_case = int(state.epoch ) if int(state.epoch ) == state.epoch else F'''{state.epoch:.2f}''' self.training_tracker.update( state.global_step + 1 , comment=F'''Epoch {epoch}/{state.num_train_epochs}''' , force_update=self._force_next_update , ) __snake_case = False def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' if not has_length(__SCREAMING_SNAKE_CASE ): return if self.prediction_bar is None: if self.training_tracker is not None: __snake_case = self.training_tracker.add_child(len(__SCREAMING_SNAKE_CASE ) ) else: __snake_case = NotebookProgressBar(len(__SCREAMING_SNAKE_CASE ) ) self.prediction_bar.update(1 ) else: self.prediction_bar.update(self.prediction_bar.value + 1 ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' if self.prediction_bar is not None: self.prediction_bar.close() __snake_case = None def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) -> List[Any]: '''simple docstring''' if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs: __snake_case = {'''Training Loss''': logs['''loss''']} # First column is necessarily Step sine we're not in epoch eval strategy __snake_case = state.global_step self.training_tracker.write_line(__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) -> Optional[Any]: '''simple docstring''' if self.training_tracker is not None: __snake_case = {'''Training Loss''': '''No log''', '''Validation Loss''': '''No log'''} for log in reversed(state.log_history ): if "loss" in log: __snake_case = log['''loss'''] break if self.first_column == "Epoch": __snake_case = int(state.epoch ) else: __snake_case = state.global_step __snake_case = '''eval''' for k in metrics: if k.endswith('''_loss''' ): __snake_case = re.sub(r'''\_loss$''' , '''''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop('''total_flos''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop('''epoch''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_runtime''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_samples_per_second''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_steps_per_second''' , __SCREAMING_SNAKE_CASE ) __snake_case = metrics.pop(F'''{metric_key_prefix}_jit_compilation_time''' , __SCREAMING_SNAKE_CASE ) for k, v in metrics.items(): if k == F'''{metric_key_prefix}_loss''': __snake_case = v else: __snake_case = k.split('''_''' ) __snake_case = ''' '''.join([part.capitalize() for part in splits[1:]] ) __snake_case = v self.training_tracker.write_line(__SCREAMING_SNAKE_CASE ) self.training_tracker.remove_child() __snake_case = None # Evaluation takes a long time so we should force the next update. __snake_case = True def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' self.training_tracker.update( state.global_step , comment=F'''Epoch {int(state.epoch )}/{state.num_train_epochs}''' , force_update=__SCREAMING_SNAKE_CASE ) __snake_case = None
24
0
import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class lowerCAmelCase__ ( _lowerCamelCase ): A_ : Optional[Any] = (DEISMultistepScheduler,) A_ : int = (('num_inference_steps', 2_5),) def __UpperCamelCase ( self : Union[str, Any] , **__UpperCamelCase : List[str] ) -> Dict: A = { 'num_train_timesteps': 1_000, 'beta_start': 0.0_0_0_1, 'beta_end': 0.0_2, 'beta_schedule': 'linear', 'solver_order': 2, } config.update(**__UpperCamelCase ) return config def __UpperCamelCase ( self : Union[str, Any] , __UpperCamelCase : str=0 , **__UpperCamelCase : Optional[int] ) -> List[Any]: A = dict(self.forward_default_kwargs ) A = kwargs.pop('num_inference_steps' , __UpperCamelCase ) A = self.dummy_sample A = 0.1 * sample A = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: A = self.get_scheduler_config(**__UpperCamelCase ) A = scheduler_class(**__UpperCamelCase ) scheduler.set_timesteps(__UpperCamelCase ) # copy over dummy past residuals A = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__UpperCamelCase ) A = scheduler_class.from_pretrained(__UpperCamelCase ) new_scheduler.set_timesteps(__UpperCamelCase ) # copy over dummy past residuals A = dummy_past_residuals[: new_scheduler.config.solver_order] A , A = sample, sample for t in range(__UpperCamelCase , time_step + scheduler.config.solver_order + 1 ): A = scheduler.step(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ).prev_sample A = new_scheduler.step(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def __UpperCamelCase ( self : str ) -> Dict: pass def __UpperCamelCase ( self : List[Any] , __UpperCamelCase : int=0 , **__UpperCamelCase : Dict ) -> List[str]: A = dict(self.forward_default_kwargs ) A = kwargs.pop('num_inference_steps' , __UpperCamelCase ) A = self.dummy_sample A = 0.1 * sample A = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: A = self.get_scheduler_config() A = scheduler_class(**__UpperCamelCase ) scheduler.set_timesteps(__UpperCamelCase ) # copy over dummy past residuals (must be after setting timesteps) A = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__UpperCamelCase ) A = scheduler_class.from_pretrained(__UpperCamelCase ) # copy over dummy past residuals new_scheduler.set_timesteps(__UpperCamelCase ) # copy over dummy past residual (must be after setting timesteps) A = dummy_past_residuals[: new_scheduler.config.solver_order] A = scheduler.step(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ).prev_sample A = new_scheduler.step(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def __UpperCamelCase ( self : Tuple , __UpperCamelCase : Tuple=None , **__UpperCamelCase : Dict ) -> Tuple: if scheduler is None: A = self.scheduler_classes[0] A = self.get_scheduler_config(**__UpperCamelCase ) A = scheduler_class(**__UpperCamelCase ) A = self.scheduler_classes[0] A = self.get_scheduler_config(**__UpperCamelCase ) A = scheduler_class(**__UpperCamelCase ) A = 10 A = self.dummy_model() A = self.dummy_sample_deter scheduler.set_timesteps(__UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): A = model(__UpperCamelCase , __UpperCamelCase ) A = scheduler.step(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ).prev_sample return sample def __UpperCamelCase ( self : Union[str, Any] ) -> List[Any]: A = dict(self.forward_default_kwargs ) A = kwargs.pop('num_inference_steps' , __UpperCamelCase ) for scheduler_class in self.scheduler_classes: A = self.get_scheduler_config() A = scheduler_class(**__UpperCamelCase ) A = self.dummy_sample A = 0.1 * sample if num_inference_steps is not None and hasattr(__UpperCamelCase , 'set_timesteps' ): scheduler.set_timesteps(__UpperCamelCase ) elif num_inference_steps is not None and not hasattr(__UpperCamelCase , 'set_timesteps' ): A = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) A = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] A = dummy_past_residuals[: scheduler.config.solver_order] A = scheduler.timesteps[5] A = scheduler.timesteps[6] A = scheduler.step(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ).prev_sample A = scheduler.step(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def __UpperCamelCase ( self : Any ) -> int: # make sure that iterating over schedulers with same config names gives same results # for defaults A = DEISMultistepScheduler(**self.get_scheduler_config() ) A = self.full_loop(scheduler=__UpperCamelCase ) A = torch.mean(torch.abs(__UpperCamelCase ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 A = DPMSolverSinglestepScheduler.from_config(scheduler.config ) A = DPMSolverMultistepScheduler.from_config(scheduler.config ) A = UniPCMultistepScheduler.from_config(scheduler.config ) A = DEISMultistepScheduler.from_config(scheduler.config ) A = self.full_loop(scheduler=__UpperCamelCase ) A = torch.mean(torch.abs(__UpperCamelCase ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 def __UpperCamelCase ( self : List[Any] ) -> Dict: for timesteps in [25, 50, 100, 999, 1_000]: self.check_over_configs(num_train_timesteps=__UpperCamelCase ) def __UpperCamelCase ( self : str ) -> List[Any]: self.check_over_configs(thresholding=__UpperCamelCase ) for order in [1, 2, 3]: for solver_type in ["logrho"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=__UpperCamelCase , prediction_type=__UpperCamelCase , sample_max_value=__UpperCamelCase , algorithm_type='deis' , solver_order=__UpperCamelCase , solver_type=__UpperCamelCase , ) def __UpperCamelCase ( self : Dict ) -> int: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__UpperCamelCase ) def __UpperCamelCase ( self : Optional[int] ) -> Any: for algorithm_type in ["deis"]: for solver_type in ["logrho"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=__UpperCamelCase , solver_type=__UpperCamelCase , prediction_type=__UpperCamelCase , algorithm_type=__UpperCamelCase , ) A = self.full_loop( solver_order=__UpperCamelCase , solver_type=__UpperCamelCase , prediction_type=__UpperCamelCase , algorithm_type=__UpperCamelCase , ) assert not torch.isnan(__UpperCamelCase ).any(), "Samples have nan numbers" def __UpperCamelCase ( self : Optional[int] ) -> Optional[Any]: self.check_over_configs(lower_order_final=__UpperCamelCase ) self.check_over_configs(lower_order_final=__UpperCamelCase ) def __UpperCamelCase ( self : int ) -> Tuple: for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1_000]: self.check_over_forward(num_inference_steps=__UpperCamelCase , time_step=0 ) def __UpperCamelCase ( self : Union[str, Any] ) -> str: A = self.full_loop() A = torch.mean(torch.abs(__UpperCamelCase ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 def __UpperCamelCase ( self : int ) -> Optional[Any]: A = self.full_loop(prediction_type='v_prediction' ) A = torch.mean(torch.abs(__UpperCamelCase ) ) assert abs(result_mean.item() - 0.0_9_1 ) < 1e-3 def __UpperCamelCase ( self : List[Any] ) -> List[str]: A = self.scheduler_classes[0] A = self.get_scheduler_config(thresholding=__UpperCamelCase , dynamic_thresholding_ratio=0 ) A = scheduler_class(**__UpperCamelCase ) A = 10 A = self.dummy_model() A = self.dummy_sample_deter.half() scheduler.set_timesteps(__UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): A = model(__UpperCamelCase , __UpperCamelCase ) A = scheduler.step(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ).prev_sample assert sample.dtype == torch.floataa
106
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' if divisor % 5 == 0 or divisor % 2 == 0: return 0 __snake_case = 1 __snake_case = 1 while repunit: __snake_case = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index def _UpperCamelCase (_lowerCamelCase : int = 1_00_00_00 )-> int: '''simple docstring''' __snake_case = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(_lowerCamelCase ) <= limit: divisor += 2 return divisor if __name__ == "__main__": print(F"""{solution() = }""")
24
0
'''simple docstring''' import baseaa def _SCREAMING_SNAKE_CASE ( __snake_case : str ): return baseaa.aaaencode(string.encode('utf-8' ) ) def _SCREAMING_SNAKE_CASE ( __snake_case : bytes ): return baseaa.aaadecode(__snake_case ).decode('utf-8' ) if __name__ == "__main__": import doctest doctest.testmod()
107
'''simple docstring''' from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def _UpperCamelCase (_lowerCamelCase : Union[str, Any] , _lowerCamelCase : List[Any] )-> Optional[Any]: '''simple docstring''' __snake_case = [] for part_id in partition_order: __snake_case = df.where(f'''SPARK_PARTITION_ID() = {part_id}''' ).collect() for row_idx, row in enumerate(_lowerCamelCase ): expected_row_ids_and_row_dicts.append((f'''{part_id}_{row_idx}''', row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Any: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(1_00 ).repartition(1 ) __snake_case = Spark(_lowerCamelCase ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(10 ).repartition(2 ) __snake_case = [1, 0] __snake_case = _generate_iterable_examples(_lowerCamelCase , _lowerCamelCase ) # Reverse the partitions. __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , _lowerCamelCase ) for i, (row_id, row_dict) in enumerate(generate_fn() ): __snake_case , __snake_case = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> int: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(10 ).repartition(1 ) __snake_case = SparkExamplesIterable(_lowerCamelCase ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): assert row_id == f'''0_{i}''' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Union[str, Any]: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''' ) as generator_mock: __snake_case = lambda _lowerCamelCase : x.reverse() __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , [2, 1, 0] ) __snake_case = SparkExamplesIterable(_lowerCamelCase ).shuffle_data_sources(_lowerCamelCase ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): __snake_case , __snake_case = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 __snake_case = SparkExamplesIterable(_lowerCamelCase ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , [0, 2] ) for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): __snake_case , __snake_case = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 __snake_case = SparkExamplesIterable(_lowerCamelCase ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 __snake_case = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowerCamelCase , [1, 3] ) for i, (row_id, row_dict) in enumerate(_lowerCamelCase ): __snake_case , __snake_case = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCamelCase ()-> Optional[int]: '''simple docstring''' __snake_case = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() __snake_case = spark.range(1_00 ).repartition(1 ) __snake_case = Spark(_lowerCamelCase ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 1_00
24
0
from ...configuration_utils import PretrainedConfig from ...utils import logging __a: List[Any] = logging.get_logger(__name__) __a: Dict = { '''sayakpaul/vit-msn-base''': '''https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json''', # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class SCREAMING_SNAKE_CASE__ ( UpperCAmelCase ): '''simple docstring''' _lowerCamelCase = '''vit_msn''' def __init__( self : List[str] , lowerCamelCase : Dict=768 , lowerCamelCase : Union[str, Any]=12 , lowerCamelCase : Optional[Any]=12 , lowerCamelCase : str=3072 , lowerCamelCase : Optional[Any]="gelu" , lowerCamelCase : Any=0.0 , lowerCamelCase : Optional[Any]=0.0 , lowerCamelCase : int=0.02 , lowerCamelCase : List[Any]=1E-06 , lowerCamelCase : List[str]=224 , lowerCamelCase : List[Any]=16 , lowerCamelCase : Optional[int]=3 , lowerCamelCase : int=True , **lowerCamelCase : Union[str, Any] , ) -> int: """simple docstring""" super().__init__(**lowerCamelCase ) _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 = initializer_range _UpperCAmelCase = layer_norm_eps _UpperCAmelCase = image_size _UpperCAmelCase = patch_size _UpperCAmelCase = num_channels _UpperCAmelCase = qkv_bias
108
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int , _lowerCamelCase : int , _lowerCamelCase : int )-> float: '''simple docstring''' __snake_case = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) # formula for sum of series return total def _UpperCamelCase ()-> str: '''simple docstring''' print(sum_of_series(1 , 1 , 10 ) ) if __name__ == "__main__": import doctest doctest.testmod()
24
0
'''simple docstring''' import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging a = logging.get_logger(__name__) def __magic_name__ ( __UpperCAmelCase=None , __UpperCAmelCase=None ) -> Dict: '''simple docstring''' return field(default_factory=lambda: default , metadata=__UpperCAmelCase ) @dataclass class __a : __UpperCamelCase : List[str] = list_field( default=[], metadata={ 'help': ( 'Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version' ' of all available models' ) }, ) __UpperCamelCase : List[int] = list_field( default=[8], metadata={'help': 'List of batch sizes for which memory and time performance will be evaluated'} ) __UpperCamelCase : List[int] = list_field( default=[8, 32, 128, 512], metadata={'help': 'List of sequence lengths for which memory and time performance will be evaluated'}, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={'help': 'Whether to benchmark inference of model. Inference can be disabled via --no-inference.'}, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={'help': 'Whether to run on available cuda devices. Cuda can be disabled via --no-cuda.'}, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={'help': 'Whether to run on available tpu devices. TPU can be disabled via --no-tpu.'} ) __UpperCamelCase : bool = field(default=_snake_case, metadata={'help': 'Use FP16 to accelerate inference.'} ) __UpperCamelCase : bool = field(default=_snake_case, metadata={'help': 'Benchmark training of model'} ) __UpperCamelCase : bool = field(default=_snake_case, metadata={'help': 'Verbose memory tracing'} ) __UpperCamelCase : bool = field( default=_snake_case, metadata={'help': 'Whether to perform speed measurements. Speed measurements can be disabled via --no-speed.'}, ) __UpperCamelCase : bool = field( default=_snake_case, metadata={ 'help': 'Whether to perform memory measurements. Memory measurements can be disabled via --no-memory' }, ) __UpperCamelCase : bool = field(default=_snake_case, metadata={'help': 'Trace memory line by line'} ) __UpperCamelCase : bool = field(default=_snake_case, metadata={'help': 'Save result to a CSV file'} ) __UpperCamelCase : bool = field(default=_snake_case, metadata={'help': 'Save all print statements in a log file'} ) __UpperCamelCase : bool = field(default=_snake_case, metadata={'help': 'Whether to print environment information'} ) __UpperCamelCase : bool = field( default=_snake_case, metadata={ 'help': ( 'Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use' ' multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled' ' for debugging / testing and on TPU.' ) }, ) __UpperCamelCase : str = field( default=F'inference_time_{round(time() )}.csv', metadata={'help': 'CSV filename used if saving time results to csv.'}, ) __UpperCamelCase : str = field( default=F'inference_memory_{round(time() )}.csv', metadata={'help': 'CSV filename used if saving memory results to csv.'}, ) __UpperCamelCase : str = field( default=F'train_time_{round(time() )}.csv', metadata={'help': 'CSV filename used if saving time results to csv for training.'}, ) __UpperCamelCase : str = field( default=F'train_memory_{round(time() )}.csv', metadata={'help': 'CSV filename used if saving memory results to csv for training.'}, ) __UpperCamelCase : str = field( default=F'env_info_{round(time() )}.csv', metadata={'help': 'CSV filename used if saving environment information.'}, ) __UpperCamelCase : str = field( default=F'log_{round(time() )}.csv', metadata={'help': 'Log filename used if print statements are saved in log.'}, ) __UpperCamelCase : int = field(default=3, metadata={'help': 'Times an experiment will be run.'} ) __UpperCamelCase : bool = field( default=_snake_case, metadata={ 'help': ( 'Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain' ' model weights.' ) }, ) def UpperCAmelCase__ ( self : List[Any] ): '''simple docstring''' warnings.warn( f"""The class {self.__class__} is deprecated. Hugging Face Benchmarking utils""" """ are deprecated in general and it is advised to use external Benchmarking libraries """ """ to benchmark Transformer models.""" ,lowerCamelCase ,) def UpperCAmelCase__ ( self : int ): '''simple docstring''' return json.dumps(dataclasses.asdict(self ) ,indent=2 ) @property def UpperCAmelCase__ ( self : str ): '''simple docstring''' if len(self.models ) <= 0: raise ValueError( """Please make sure you provide at least one model name / model identifier, *e.g.* `--models""" """ bert-base-cased` or `args.models = ['bert-base-cased'].""" ) return self.models @property def UpperCAmelCase__ ( self : Tuple ): '''simple docstring''' if not self.multi_process: return False elif self.is_tpu: logger.info("""Multiprocessing is currently not possible on TPU.""" ) return False else: return True
109
'''simple docstring''' import asyncio import os import re import sys import tempfile import unittest from contextlib import contextmanager from copy import deepcopy from distutils.util import strtobool from enum import Enum from importlib.util import find_spec from pathlib import Path from unittest.mock import patch import pyarrow as pa import pytest import requests from packaging import version from datasets import config if config.PY_VERSION < version.parse('''3.8'''): import importlib_metadata else: import importlib.metadata as importlib_metadata def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : Tuple=False )-> Union[str, Any]: '''simple docstring''' try: __snake_case = os.environ[key] except KeyError: # KEY isn't set, default to `default`. __snake_case = default else: # KEY is set, convert it to True or False. try: __snake_case = strtobool(_lowerCamelCase ) except ValueError: # More values are supported, but let's keep the message simple. raise ValueError(f'''If set, {key} must be yes or no.''' ) return _value UpperCAmelCase_ : Optional[Any] = parse_flag_from_env('''RUN_SLOW''', default=False) UpperCAmelCase_ : Union[str, Any] = parse_flag_from_env('''RUN_REMOTE''', default=False) UpperCAmelCase_ : Optional[Any] = parse_flag_from_env('''RUN_LOCAL''', default=True) UpperCAmelCase_ : Union[str, Any] = parse_flag_from_env('''RUN_PACKAGED''', default=True) # Compression UpperCAmelCase_ : Dict = pytest.mark.skipif(not config.LZ4_AVAILABLE, reason='''test requires lz4''') UpperCAmelCase_ : int = pytest.mark.skipif(not config.PY7ZR_AVAILABLE, reason='''test requires py7zr''') UpperCAmelCase_ : Tuple = pytest.mark.skipif(not config.ZSTANDARD_AVAILABLE, reason='''test requires zstandard''') # Audio UpperCAmelCase_ : str = pytest.mark.skipif( # On Windows and OS X, soundfile installs sndfile find_spec('''soundfile''') is None or version.parse(importlib_metadata.version('''soundfile''')) < version.parse('''0.12.0'''), reason='''test requires sndfile>=0.12.1: \'pip install \"soundfile>=0.12.1\"\'; ''', ) # Beam UpperCAmelCase_ : Tuple = pytest.mark.skipif( not config.BEAM_AVAILABLE or config.DILL_VERSION >= version.parse('''0.3.2'''), reason='''test requires apache-beam and a compatible dill version''', ) # Dill-cloudpickle compatibility UpperCAmelCase_ : Union[str, Any] = pytest.mark.skipif( config.DILL_VERSION <= version.parse('''0.3.2'''), reason='''test requires dill>0.3.2 for cloudpickle compatibility''', ) # Windows UpperCAmelCase_ : int = pytest.mark.skipif( sys.platform == '''win32''', reason='''test should not be run on Windows''', ) def _UpperCamelCase (_lowerCamelCase : List[str] )-> List[Any]: '''simple docstring''' try: import faiss # noqa except ImportError: __snake_case = unittest.skip('''test requires faiss''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> List[str]: '''simple docstring''' try: import regex # noqa except ImportError: __snake_case = unittest.skip('''test requires regex''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> Union[str, Any]: '''simple docstring''' try: import elasticsearch # noqa except ImportError: __snake_case = unittest.skip('''test requires elasticsearch''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Dict )-> List[Any]: '''simple docstring''' try: import sqlalchemy # noqa except ImportError: __snake_case = unittest.skip('''test requires sqlalchemy''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : List[str] )-> List[str]: '''simple docstring''' if not config.TORCH_AVAILABLE: __snake_case = unittest.skip('''test requires PyTorch''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Optional[Any] )-> Dict: '''simple docstring''' if not config.TF_AVAILABLE: __snake_case = unittest.skip('''test requires TensorFlow''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : int )-> Any: '''simple docstring''' if not config.JAX_AVAILABLE: __snake_case = unittest.skip('''test requires JAX''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Tuple )-> str: '''simple docstring''' if not config.PIL_AVAILABLE: __snake_case = unittest.skip('''test requires Pillow''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Dict )-> Any: '''simple docstring''' try: import transformers # noqa F401 except ImportError: return unittest.skip('''test requires transformers''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : int )-> Tuple: '''simple docstring''' try: import tiktoken # noqa F401 except ImportError: return unittest.skip('''test requires tiktoken''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : Dict )-> str: '''simple docstring''' try: import spacy # noqa F401 except ImportError: return unittest.skip('''test requires spacy''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : int )-> Dict: '''simple docstring''' def _require_spacy_model(_lowerCamelCase : int ): try: import spacy # noqa F401 spacy.load(_lowerCamelCase ) except ImportError: return unittest.skip('''test requires spacy''' )(_lowerCamelCase ) except OSError: return unittest.skip('''test requires spacy model \'{}\''''.format(_lowerCamelCase ) )(_lowerCamelCase ) else: return test_case return _require_spacy_model def _UpperCamelCase (_lowerCamelCase : str )-> Dict: '''simple docstring''' try: import pyspark # noqa F401 except ImportError: return unittest.skip('''test requires pyspark''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : Tuple )-> str: '''simple docstring''' try: import joblibspark # noqa F401 except ImportError: return unittest.skip('''test requires joblibspark''' )(_lowerCamelCase ) else: return test_case def _UpperCamelCase (_lowerCamelCase : Any )-> int: '''simple docstring''' if not _run_slow_tests or _run_slow_tests == 0: __snake_case = unittest.skip('''test is slow''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Any )-> Optional[Any]: '''simple docstring''' if not _run_local_tests or _run_local_tests == 0: __snake_case = unittest.skip('''test is local''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : str )-> int: '''simple docstring''' if not _run_packaged_tests or _run_packaged_tests == 0: __snake_case = unittest.skip('''test is packaged''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (_lowerCamelCase : Optional[int] )-> str: '''simple docstring''' if not _run_remote_tests or _run_remote_tests == 0: __snake_case = unittest.skip('''test requires remote''' )(_lowerCamelCase ) return test_case def _UpperCamelCase (*_lowerCamelCase : str )-> Optional[int]: '''simple docstring''' def decorate(cls : Optional[Any] ): for name, fn in cls.__dict__.items(): if callable(_lowerCamelCase ) and name.startswith('''test''' ): for decorator in decorators: __snake_case = decorator(_lowerCamelCase ) setattr(cls , _lowerCamelCase , _lowerCamelCase ) return cls return decorate class lowerCAmelCase ( __lowerCAmelCase): pass class lowerCAmelCase ( __lowerCAmelCase): __lowercase : List[str] = 0 __lowercase : Dict = 1 __lowercase : List[Any] = 2 @contextmanager def _UpperCamelCase (_lowerCamelCase : Dict=OfflineSimulationMode.CONNECTION_FAILS , _lowerCamelCase : Optional[int]=1E-16 )-> Tuple: '''simple docstring''' __snake_case = requests.Session().request def timeout_request(_lowerCamelCase : Any , _lowerCamelCase : str , _lowerCamelCase : str , **_lowerCamelCase : Any ): # Change the url to an invalid url so that the connection hangs __snake_case = '''https://10.255.255.1''' if kwargs.get('''timeout''' ) is None: raise RequestWouldHangIndefinitelyError( f'''Tried a call to {url} in offline mode with no timeout set. Please set a timeout.''' ) __snake_case = timeout try: return online_request(_lowerCamelCase , _lowerCamelCase , **_lowerCamelCase ) except Exception as e: # The following changes in the error are just here to make the offline timeout error prettier __snake_case = url __snake_case = e.args[0] __snake_case = (max_retry_error.args[0].replace('''10.255.255.1''' , f'''OfflineMock[{url}]''' ),) __snake_case = (max_retry_error,) raise def raise_connection_error(_lowerCamelCase : Union[str, Any] , _lowerCamelCase : Optional[int] , **_lowerCamelCase : Dict ): raise requests.ConnectionError('''Offline mode is enabled.''' , request=_lowerCamelCase ) if mode is OfflineSimulationMode.CONNECTION_FAILS: with patch('''requests.Session.send''' , _lowerCamelCase ): yield elif mode is OfflineSimulationMode.CONNECTION_TIMES_OUT: # inspired from https://stackoverflow.com/a/904609 with patch('''requests.Session.request''' , _lowerCamelCase ): yield elif mode is OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1: with patch('''datasets.config.HF_DATASETS_OFFLINE''' , _lowerCamelCase ): yield else: raise ValueError('''Please use a value from the OfflineSimulationMode enum.''' ) @contextmanager def _UpperCamelCase (*_lowerCamelCase : Union[str, Any] , **_lowerCamelCase : List[str] )-> Any: '''simple docstring''' __snake_case = str(Path().resolve() ) with tempfile.TemporaryDirectory(*_lowerCamelCase , **_lowerCamelCase ) as tmp_dir: try: os.chdir(_lowerCamelCase ) yield finally: os.chdir(_lowerCamelCase ) @contextmanager def _UpperCamelCase ()-> Optional[int]: '''simple docstring''' import gc gc.collect() __snake_case = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory > 0, "Arrow memory didn't increase." @contextmanager def _UpperCamelCase ()-> List[Any]: '''simple docstring''' import gc gc.collect() __snake_case = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory <= 0, "Arrow memory wasn't expected to increase." def _UpperCamelCase (_lowerCamelCase : Any , _lowerCamelCase : int )-> Any: '''simple docstring''' return deepcopy(_lowerCamelCase ).integers(0 , 1_00 , 10 ).tolist() == deepcopy(_lowerCamelCase ).integers(0 , 1_00 , 10 ).tolist() def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> List[Any]: '''simple docstring''' import decorator from requests.exceptions import HTTPError def _wrapper(_lowerCamelCase : int , *_lowerCamelCase : int , **_lowerCamelCase : Optional[int] ): try: return func(*_lowerCamelCase , **_lowerCamelCase ) except HTTPError as err: if str(_lowerCamelCase ).startswith('''500''' ) or str(_lowerCamelCase ).startswith('''502''' ): pytest.xfail(str(_lowerCamelCase ) ) raise err return decorator.decorator(_wrapper , _lowerCamelCase ) class lowerCAmelCase : def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' __snake_case = returncode __snake_case = stdout __snake_case = stderr async def _UpperCamelCase (_lowerCamelCase : List[str] , _lowerCamelCase : Union[str, Any] )-> Dict: '''simple docstring''' while True: __snake_case = await stream.readline() if line: callback(_lowerCamelCase ) else: break async def _UpperCamelCase (_lowerCamelCase : Tuple , _lowerCamelCase : Optional[int]=None , _lowerCamelCase : Optional[Any]=None , _lowerCamelCase : Tuple=None , _lowerCamelCase : Dict=False , _lowerCamelCase : List[Any]=False )-> _RunOutput: '''simple docstring''' if echo: print('''\nRunning: ''' , ''' '''.join(_lowerCamelCase ) ) __snake_case = await asyncio.create_subprocess_exec( cmd[0] , *cmd[1:] , stdin=_lowerCamelCase , stdout=asyncio.subprocess.PIPE , stderr=asyncio.subprocess.PIPE , env=_lowerCamelCase , ) # note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe # https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait # # If it starts hanging, will need to switch to the following code. The problem is that no data # will be seen until it's done and if it hangs for example there will be no debug info. # out, err = await p.communicate() # return _RunOutput(p.returncode, out, err) __snake_case = [] __snake_case = [] def tee(_lowerCamelCase : int , _lowerCamelCase : List[Any] , _lowerCamelCase : str , _lowerCamelCase : Dict="" ): __snake_case = line.decode('''utf-8''' ).rstrip() sink.append(_lowerCamelCase ) if not quiet: print(_lowerCamelCase , _lowerCamelCase , file=_lowerCamelCase ) # XXX: the timeout doesn't seem to make any difference here await asyncio.wait( [ _read_stream(p.stdout , lambda _lowerCamelCase : tee(_lowerCamelCase , _lowerCamelCase , sys.stdout , label='''stdout:''' ) ), _read_stream(p.stderr , lambda _lowerCamelCase : tee(_lowerCamelCase , _lowerCamelCase , sys.stderr , label='''stderr:''' ) ), ] , timeout=_lowerCamelCase , ) return _RunOutput(await p.wait() , _lowerCamelCase , _lowerCamelCase ) def _UpperCamelCase (_lowerCamelCase : Optional[Any] , _lowerCamelCase : Any=None , _lowerCamelCase : List[str]=None , _lowerCamelCase : Optional[Any]=1_80 , _lowerCamelCase : Dict=False , _lowerCamelCase : int=True )-> _RunOutput: '''simple docstring''' __snake_case = asyncio.get_event_loop() __snake_case = loop.run_until_complete( _stream_subprocess(_lowerCamelCase , env=_lowerCamelCase , stdin=_lowerCamelCase , timeout=_lowerCamelCase , quiet=_lowerCamelCase , echo=_lowerCamelCase ) ) __snake_case = ''' '''.join(_lowerCamelCase ) if result.returncode > 0: __snake_case = '''\n'''.join(result.stderr ) raise RuntimeError( f'''\'{cmd_str}\' failed with returncode {result.returncode}\n\n''' f'''The combined stderr from workers follows:\n{stderr}''' ) # check that the subprocess actually did run and produced some output, should the test rely on # the remote side to do the testing if not result.stdout and not result.stderr: raise RuntimeError(f'''\'{cmd_str}\' produced no output.''' ) return result def _UpperCamelCase ()-> Dict: '''simple docstring''' __snake_case = os.environ.get('''PYTEST_XDIST_WORKER''' , '''gw0''' ) __snake_case = re.sub(R'''^gw''' , '''''' , _lowerCamelCase , 0 , re.M ) return int(_lowerCamelCase ) def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = 2_95_00 __snake_case = pytest_xdist_worker_id() return port + uniq_delta
24
0
"""simple docstring""" import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation UpperCamelCase__ = logging.get_logger(__name__) UpperCamelCase__ = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} UpperCamelCase__ = { 'tokenizer_file': { 'EleutherAI/gpt-neox-20b': 'https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/tokenizer.json', }, } UpperCamelCase__ = { 'gpt-neox-20b': 20_48, } class a ( lowercase ): UpperCamelCase : Any = VOCAB_FILES_NAMES UpperCamelCase : Dict = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase : List[Any] = ["""input_ids""", """attention_mask"""] def __init__( self , UpperCamelCase_=None , UpperCamelCase_=None , UpperCamelCase_=None , UpperCamelCase_="<|endoftext|>" , UpperCamelCase_="<|endoftext|>" , UpperCamelCase_="<|endoftext|>" , UpperCamelCase_=False , **UpperCamelCase_ , ): super().__init__( UpperCamelCase_ , UpperCamelCase_ , tokenizer_file=UpperCamelCase_ , unk_token=UpperCamelCase_ , bos_token=UpperCamelCase_ , eos_token=UpperCamelCase_ , add_prefix_space=UpperCamelCase_ , **UpperCamelCase_ , ) UpperCAmelCase__ : Union[str, Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('add_prefix_space' , UpperCamelCase_ ) != add_prefix_space: UpperCAmelCase__ : str = getattr(UpperCamelCase_ , pre_tok_state.pop('type' ) ) UpperCAmelCase__ : Union[str, Any] = add_prefix_space UpperCAmelCase__ : List[Any] = pre_tok_class(**UpperCamelCase_ ) UpperCAmelCase__ : Optional[int] = add_prefix_space def __snake_case ( self , UpperCamelCase_ , UpperCamelCase_ = None ): UpperCAmelCase__ : List[str] = self._tokenizer.model.save(UpperCamelCase_ , name=UpperCamelCase_ ) return tuple(UpperCamelCase_ ) def __snake_case ( self , UpperCamelCase_ ): UpperCAmelCase__ : Tuple = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(UpperCamelCase_ , add_special_tokens=UpperCamelCase_ ) + [self.eos_token_id] ) if len(UpperCamelCase_ ) > self.model_max_length: UpperCAmelCase__ : str = input_ids[-self.model_max_length :] return input_ids
110
'''simple docstring''' def _UpperCamelCase (_lowerCamelCase : int )-> int: '''simple docstring''' __snake_case = [[0 for _ in range(_lowerCamelCase )] for _ in range(m + 1 )] for i in range(m + 1 ): __snake_case = 1 for n in range(m + 1 ): for k in range(1 , _lowerCamelCase ): 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: UpperCAmelCase_ : List[str] = int(input('''Enter a number: ''').strip()) print(partition(n)) except ValueError: print('''Please enter a number.''') else: try: UpperCAmelCase_ : Union[str, Any] = int(sys.argv[1]) print(partition(n)) except ValueError: print('''Please pass a number.''')
24
0
import random def lowercase ( __A : list , __A : Union[str, Any] ) -> tuple: '''simple docstring''' snake_case , snake_case , snake_case : Tuple = [], [], [] for element in data: if element < pivot: less.append(_lowerCamelCase ) elif element > pivot: greater.append(_lowerCamelCase ) else: equal.append(_lowerCamelCase ) return less, equal, greater def lowercase ( __A : list , __A : int ) -> int: '''simple docstring''' if index >= len(_lowerCamelCase ) or index < 0: return None snake_case : Any = items[random.randint(0 , len(_lowerCamelCase ) - 1 )] snake_case : str = 0 snake_case , snake_case , snake_case : int = _partition(_lowerCamelCase , _lowerCamelCase ) snake_case : List[str] = len(_lowerCamelCase ) snake_case : Dict = len(_lowerCamelCase ) # index is the pivot if m <= index < m + count: return pivot # must be in smaller elif m > index: return quick_select(_lowerCamelCase , _lowerCamelCase ) # must be in larger else: return quick_select(_lowerCamelCase , index - (m + count) )
36
'''simple docstring''' import argparse import os import re UpperCAmelCase_ : List[str] = '''src/transformers/models/auto''' # re pattern that matches mapping introductions: # SUPER_MODEL_MAPPING_NAMES = OrderedDict or SUPER_MODEL_MAPPING = OrderedDict UpperCAmelCase_ : Tuple = re.compile(R'''[A-Z_]+_MAPPING(\s+|_[A-Z_]+\s+)=\s+OrderedDict''') # re pattern that matches identifiers in mappings UpperCAmelCase_ : Dict = re.compile(R'''\s*\(\s*"(\S[^"]+)"''') def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : bool = False )-> str: '''simple docstring''' with open(_lowerCamelCase , '''r''' , encoding='''utf-8''' ) as f: __snake_case = f.read() __snake_case = content.split('''\n''' ) __snake_case = [] __snake_case = 0 while line_idx < len(_lowerCamelCase ): if _re_intro_mapping.search(lines[line_idx] ) is not None: __snake_case = len(re.search(R'''^(\s*)\S''' , lines[line_idx] ).groups()[0] ) + 8 # Start of a new mapping! while not lines[line_idx].startswith(''' ''' * indent + '''(''' ): new_lines.append(lines[line_idx] ) line_idx += 1 __snake_case = [] while lines[line_idx].strip() != "]": # Blocks either fit in one line or not if lines[line_idx].strip() == "(": __snake_case = line_idx while not lines[line_idx].startswith(''' ''' * indent + ''')''' ): line_idx += 1 blocks.append('''\n'''.join(lines[start_idx : line_idx + 1] ) ) else: blocks.append(lines[line_idx] ) line_idx += 1 # Sort blocks by their identifiers __snake_case = sorted(_lowerCamelCase , key=lambda _lowerCamelCase : _re_identifier.search(_lowerCamelCase ).groups()[0] ) new_lines += blocks else: new_lines.append(lines[line_idx] ) line_idx += 1 if overwrite: with open(_lowerCamelCase , '''w''' , encoding='''utf-8''' ) as f: f.write('''\n'''.join(_lowerCamelCase ) ) elif "\n".join(_lowerCamelCase ) != content: return True def _UpperCamelCase (_lowerCamelCase : bool = False )-> Tuple: '''simple docstring''' __snake_case = [os.path.join(_lowerCamelCase , _lowerCamelCase ) for f in os.listdir(_lowerCamelCase ) if f.endswith('''.py''' )] __snake_case = [sort_auto_mapping(_lowerCamelCase , overwrite=_lowerCamelCase ) for fname in fnames] if not overwrite and any(_lowerCamelCase ): __snake_case = [f for f, d in zip(_lowerCamelCase , _lowerCamelCase ) if d] raise ValueError( f'''The following files have auto mappings that need sorting: {", ".join(_lowerCamelCase )}. Run `make style` to fix''' ''' this.''' ) if __name__ == "__main__": UpperCAmelCase_ : str = argparse.ArgumentParser() parser.add_argument('''--check_only''', action='''store_true''', help='''Whether to only check or fix style.''') UpperCAmelCase_ : List[Any] = parser.parse_args() sort_all_auto_mappings(not args.check_only)
24
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) __a = { '''configuration_efficientformer''': [ '''EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EfficientFormerConfig''', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ['''EfficientFormerImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ '''EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EfficientFormerForImageClassification''', '''EfficientFormerForImageClassificationWithTeacher''', '''EfficientFormerModel''', '''EfficientFormerPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ '''TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFEfficientFormerForImageClassification''', '''TFEfficientFormerForImageClassificationWithTeacher''', '''TFEfficientFormerModel''', '''TFEfficientFormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_efficientformer import EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientformer import EfficientFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientformer import ( EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientFormerForImageClassification, EfficientFormerForImageClassificationWithTeacher, EfficientFormerModel, EfficientFormerPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_efficientformer import ( TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFEfficientFormerForImageClassification, TFEfficientFormerForImageClassificationWithTeacher, TFEfficientFormerModel, TFEfficientFormerPreTrainedModel, ) else: import sys __a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
97
'''simple docstring''' import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _UpperCamelCase (*_lowerCamelCase : str , _lowerCamelCase : Optional[Union[Dict, Any]] = None , _lowerCamelCase : List[Any]=True , _lowerCamelCase : str=2 )-> str: '''simple docstring''' from .. import __version__ __snake_case = take_from __snake_case = () if not isinstance(args[0] , _lowerCamelCase ): __snake_case = (args,) for attribute, version_name, message in args: if version.parse(version.parse(_lowerCamelCase ).base_version ) >= version.parse(_lowerCamelCase ): raise ValueError( f'''The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\'''' f''' version {__version__} is >= {version_name}''' ) __snake_case = None if isinstance(_lowerCamelCase , _lowerCamelCase ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(_lowerCamelCase ),) __snake_case = f'''The `{attribute}` argument is deprecated and will be removed in version {version_name}.''' elif hasattr(_lowerCamelCase , _lowerCamelCase ): values += (getattr(_lowerCamelCase , _lowerCamelCase ),) __snake_case = f'''The `{attribute}` attribute is deprecated and will be removed in version {version_name}.''' elif deprecated_kwargs is None: __snake_case = f'''`{attribute}` is deprecated and will be removed in version {version_name}.''' if warning is not None: __snake_case = warning + ''' ''' if standard_warn else '''''' warnings.warn(warning + message , _lowerCamelCase , stacklevel=_lowerCamelCase ) if isinstance(_lowerCamelCase , _lowerCamelCase ) and len(_lowerCamelCase ) > 0: __snake_case = inspect.getouterframes(inspect.currentframe() )[1] __snake_case = call_frame.filename __snake_case = call_frame.lineno __snake_case = call_frame.function __snake_case , __snake_case = next(iter(deprecated_kwargs.items() ) ) raise TypeError(f'''{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`''' ) if len(_lowerCamelCase ) == 0: return elif len(_lowerCamelCase ) == 1: return values[0] return values
24
0
import unittest from transformers import EsmConfig, is_torch_available from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device 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.models.esm.modeling_esmfold import EsmForProteinFolding class UpperCamelCase__ : '''simple docstring''' def __init__( self, snake_case__, snake_case__=13, snake_case__=7, snake_case__=False, snake_case__=True, snake_case__=False, snake_case__=False, snake_case__=19, 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__=5_12, snake_case__=16, snake_case__=2, snake_case__=0.02, snake_case__=3, snake_case__=4, snake_case__=None, ) -> List[Any]: """simple docstring""" lowercase_ : Union[str, Any] = parent lowercase_ : List[Any] = batch_size lowercase_ : Any = seq_length lowercase_ : Any = is_training lowercase_ : str = use_input_mask lowercase_ : List[Any] = use_token_type_ids lowercase_ : Union[str, Any] = use_labels lowercase_ : int = vocab_size lowercase_ : Union[str, Any] = hidden_size lowercase_ : Optional[int] = num_hidden_layers lowercase_ : Dict = num_attention_heads lowercase_ : List[str] = intermediate_size lowercase_ : str = hidden_act lowercase_ : Optional[int] = hidden_dropout_prob lowercase_ : Union[str, Any] = attention_probs_dropout_prob lowercase_ : Dict = max_position_embeddings lowercase_ : List[Any] = type_vocab_size lowercase_ : Any = type_sequence_label_size lowercase_ : Dict = initializer_range lowercase_ : str = num_labels lowercase_ : List[Any] = num_choices lowercase_ : List[str] = scope def snake_case__ ( self ) -> Dict: """simple docstring""" lowercase_ : List[str] = ids_tensor([self.batch_size, self.seq_length], self.vocab_size ) lowercase_ : Dict = None if self.use_input_mask: lowercase_ : List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) lowercase_ : List[Any] = None lowercase_ : str = None lowercase_ : Tuple = None if self.use_labels: lowercase_ : Any = ids_tensor([self.batch_size], self.type_sequence_label_size ) lowercase_ : List[Any] = ids_tensor([self.batch_size, self.seq_length], self.num_labels ) lowercase_ : Any = ids_tensor([self.batch_size], self.num_choices ) lowercase_ : Tuple = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def snake_case__ ( self ) -> Any: """simple docstring""" lowercase_ : int = EsmConfig( vocab_size=33, hidden_size=self.hidden_size, pad_token_id=1, 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, is_folding_model=__SCREAMING_SNAKE_CASE, esmfold_config={"""trunk""": {"""num_blocks""": 2}, """fp16_esm""": False}, ) return config def snake_case__ ( self, snake_case__, snake_case__, snake_case__, snake_case__, snake_case__, snake_case__ ) -> Optional[Any]: """simple docstring""" lowercase_ : int = EsmForProteinFolding(config=__SCREAMING_SNAKE_CASE ).float() model.to(__SCREAMING_SNAKE_CASE ) model.eval() lowercase_ : Dict = model(__SCREAMING_SNAKE_CASE, attention_mask=__SCREAMING_SNAKE_CASE ) lowercase_ : int = model(__SCREAMING_SNAKE_CASE ) lowercase_ : Tuple = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.positions.shape, (8, self.batch_size, self.seq_length, 14, 3) ) self.parent.assertEqual(result.angles.shape, (8, self.batch_size, self.seq_length, 7, 2) ) def snake_case__ ( self ) -> Optional[int]: """simple docstring""" lowercase_ : Optional[Any] = self.prepare_config_and_inputs() ( ( lowercase_ ) , ( lowercase_ ) , ( lowercase_ ) , ( lowercase_ ) , ( lowercase_ ) , ( lowercase_ ) , ) : Optional[int] = config_and_inputs lowercase_ : Any = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class UpperCamelCase__ ( __lowerCAmelCase , __lowerCAmelCase , unittest.TestCase ): '''simple docstring''' __a : List[str] = False __a : Dict = (EsmForProteinFolding,) if is_torch_available() else () __a : Union[str, Any] = () __a : List[str] = {} if is_torch_available() else {} __a : List[Any] = False def snake_case__ ( self ) -> Any: """simple docstring""" lowercase_ : Optional[int] = EsmFoldModelTester(self ) lowercase_ : Union[str, Any] = ConfigTester(self, config_class=__SCREAMING_SNAKE_CASE, hidden_size=37 ) def snake_case__ ( self ) -> List[Any]: """simple docstring""" self.config_tester.run_common_tests() def snake_case__ ( self ) -> Optional[Any]: """simple docstring""" lowercase_ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) @unittest.skip("""Does not support attention outputs""" ) def snake_case__ ( self ) -> Dict: """simple docstring""" pass @unittest.skip def snake_case__ ( self ) -> Union[str, Any]: """simple docstring""" pass @unittest.skip("""Esm does not support embedding resizing""" ) def snake_case__ ( self ) -> List[Any]: """simple docstring""" pass @unittest.skip("""Esm does not support embedding resizing""" ) def snake_case__ ( self ) -> str: """simple docstring""" pass @unittest.skip("""ESMFold does not support passing input embeds!""" ) def snake_case__ ( self ) -> Tuple: """simple docstring""" pass @unittest.skip("""ESMFold does not support head pruning.""" ) def snake_case__ ( self ) -> Optional[int]: """simple docstring""" pass @unittest.skip("""ESMFold does not support head pruning.""" ) def snake_case__ ( self ) -> List[str]: """simple docstring""" pass @unittest.skip("""ESMFold does not support head pruning.""" ) def snake_case__ ( self ) -> int: """simple docstring""" pass @unittest.skip("""ESMFold does not support head pruning.""" ) def snake_case__ ( self ) -> Optional[Any]: """simple docstring""" pass @unittest.skip("""ESMFold does not support head pruning.""" ) def snake_case__ ( self ) -> str: """simple docstring""" pass @unittest.skip("""ESMFold does not output hidden states in the normal way.""" ) def snake_case__ ( self ) -> str: """simple docstring""" pass @unittest.skip("""ESMfold does not output hidden states in the normal way.""" ) def snake_case__ ( self ) -> Tuple: """simple docstring""" pass @unittest.skip("""ESMFold only has one output format.""" ) def snake_case__ ( self ) -> Optional[Any]: """simple docstring""" pass @unittest.skip("""This test doesn\'t work for ESMFold and doesn\'t test core functionality""" ) def snake_case__ ( self ) -> Dict: """simple docstring""" pass @unittest.skip("""ESMFold does not support input chunking.""" ) def snake_case__ ( self ) -> Optional[Any]: """simple docstring""" pass @unittest.skip("""ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.""" ) def snake_case__ ( self ) -> Optional[int]: """simple docstring""" pass @unittest.skip("""ESMFold doesn\'t support torchscript compilation.""" ) def snake_case__ ( self ) -> Any: """simple docstring""" pass @unittest.skip("""ESMFold doesn\'t support torchscript compilation.""" ) def snake_case__ ( self ) -> int: """simple docstring""" pass @unittest.skip("""ESMFold doesn\'t support torchscript compilation.""" ) def snake_case__ ( self ) -> List[Any]: """simple docstring""" pass @unittest.skip("""ESMFold doesn\'t support data parallel.""" ) def snake_case__ ( self ) -> Any: """simple docstring""" pass @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def snake_case__ ( self ) -> Tuple: """simple docstring""" pass @require_torch class UpperCamelCase__ ( __lowerCAmelCase ): '''simple docstring''' @slow def snake_case__ ( self ) -> str: """simple docstring""" lowercase_ : str = EsmForProteinFolding.from_pretrained("""facebook/esmfold_v1""" ).float() model.eval() lowercase_ : Dict = torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) lowercase_ : str = model(__SCREAMING_SNAKE_CASE )["""positions"""] lowercase_ : str = torch.tensor([2.5828, 0.7993, -10.9334], dtype=torch.floataa ) self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0], __SCREAMING_SNAKE_CASE, atol=1E-4 ) )
458
'''simple docstring''' import argparse import re from pathlib import Path import requests import torch from PIL import Image from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor from transformers import ( EfficientFormerConfig, EfficientFormerForImageClassificationWithTeacher, EfficientFormerImageProcessor, ) from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def _UpperCamelCase (_lowerCamelCase : int , _lowerCamelCase : str )-> List[str]: '''simple docstring''' __snake_case = old_name if "patch_embed" in old_name: __snake_case , __snake_case , __snake_case = old_name.split('''.''' ) if layer == "0": __snake_case = old_name.replace('''0''' , '''convolution1''' ) elif layer == "1": __snake_case = old_name.replace('''1''' , '''batchnorm_before''' ) elif layer == "3": __snake_case = old_name.replace('''3''' , '''convolution2''' ) else: __snake_case = old_name.replace('''4''' , '''batchnorm_after''' ) if "network" in old_name and re.search(R'''\d\.\d''' , _lowerCamelCase ): __snake_case = R'''\b\d{2}\b''' if bool(re.search(_lowerCamelCase , _lowerCamelCase ) ): __snake_case = re.search(R'''\d\.\d\d.''' , _lowerCamelCase ).group() else: __snake_case = re.search(R'''\d\.\d.''' , _lowerCamelCase ).group() if int(match[0] ) < 6: __snake_case = old_name.replace(_lowerCamelCase , '''''' ) __snake_case = trimmed_name.replace('''network''' , match[0] + '''.meta4D_layers.blocks.''' + match[2:-1] ) __snake_case = '''intermediate_stages.''' + trimmed_name else: __snake_case = old_name.replace(_lowerCamelCase , '''''' ) if int(match[2] ) < num_meta4D_last_stage: __snake_case = trimmed_name.replace('''network''' , '''meta4D_layers.blocks.''' + match[2] ) else: __snake_case = str(int(match[2] ) - num_meta4D_last_stage ) __snake_case = trimmed_name.replace('''network''' , '''meta3D_layers.blocks.''' + layer_index ) if "norm1" in old_name: __snake_case = trimmed_name.replace('''norm1''' , '''layernorm1''' ) elif "norm2" in old_name: __snake_case = trimmed_name.replace('''norm2''' , '''layernorm2''' ) elif "fc1" in old_name: __snake_case = trimmed_name.replace('''fc1''' , '''linear_in''' ) elif "fc2" in old_name: __snake_case = trimmed_name.replace('''fc2''' , '''linear_out''' ) __snake_case = '''last_stage.''' + trimmed_name elif "network" in old_name and re.search(R'''.\d.''' , _lowerCamelCase ): __snake_case = old_name.replace('''network''' , '''intermediate_stages''' ) if "fc" in new_name: __snake_case = new_name.replace('''fc''' , '''convolution''' ) elif ("norm1" in new_name) and ("layernorm1" not in new_name): __snake_case = new_name.replace('''norm1''' , '''batchnorm_before''' ) elif ("norm2" in new_name) and ("layernorm2" not in new_name): __snake_case = new_name.replace('''norm2''' , '''batchnorm_after''' ) if "proj" in new_name: __snake_case = new_name.replace('''proj''' , '''projection''' ) if "dist_head" in new_name: __snake_case = new_name.replace('''dist_head''' , '''distillation_classifier''' ) elif "head" in new_name: __snake_case = new_name.replace('''head''' , '''classifier''' ) elif "patch_embed" in new_name: __snake_case = '''efficientformer.''' + new_name elif new_name == "norm.weight" or new_name == "norm.bias": __snake_case = new_name.replace('''norm''' , '''layernorm''' ) __snake_case = '''efficientformer.''' + new_name else: __snake_case = '''efficientformer.encoder.''' + new_name return new_name def _UpperCamelCase (_lowerCamelCase : str , _lowerCamelCase : Tuple )-> List[str]: '''simple docstring''' for key in checkpoint.copy().keys(): __snake_case = checkpoint.pop(_lowerCamelCase ) __snake_case = val return checkpoint def _UpperCamelCase ()-> Tuple: '''simple docstring''' __snake_case = '''http://images.cocodataset.org/val2017/000000039769.jpg''' __snake_case = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return image def _UpperCamelCase (_lowerCamelCase : Path , _lowerCamelCase : Path , _lowerCamelCase : Path , _lowerCamelCase : bool )-> Optional[Any]: '''simple docstring''' __snake_case = torch.load(_lowerCamelCase , map_location='''cpu''' )['''model'''] __snake_case = EfficientFormerConfig.from_json_file(_lowerCamelCase ) __snake_case = EfficientFormerForImageClassificationWithTeacher(_lowerCamelCase ) __snake_case = '''_'''.join(checkpoint_path.split('''/''' )[-1].split('''.''' )[0].split('''_''' )[:-1] ) __snake_case = config.depths[-1] - config.num_metaad_blocks + 1 __snake_case = convert_torch_checkpoint(_lowerCamelCase , _lowerCamelCase ) model.load_state_dict(_lowerCamelCase ) model.eval() __snake_case = { '''bilinear''': PILImageResampling.BILINEAR, '''bicubic''': PILImageResampling.BICUBIC, '''nearest''': PILImageResampling.NEAREST, } # prepare image __snake_case = prepare_img() __snake_case = 2_56 __snake_case = 2_24 __snake_case = EfficientFormerImageProcessor( size={'''shortest_edge''': image_size} , crop_size={'''height''': crop_size, '''width''': crop_size} , resample=pillow_resamplings['''bicubic'''] , ) __snake_case = processor(images=_lowerCamelCase , return_tensors='''pt''' ).pixel_values # original processing pipeline __snake_case = Compose( [ Resize(_lowerCamelCase , interpolation=pillow_resamplings['''bicubic'''] ), CenterCrop(_lowerCamelCase ), ToTensor(), Normalize(_lowerCamelCase , _lowerCamelCase ), ] ) __snake_case = image_transforms(_lowerCamelCase ).unsqueeze(0 ) assert torch.allclose(_lowerCamelCase , _lowerCamelCase ) __snake_case = model(_lowerCamelCase ) __snake_case = outputs.logits __snake_case = (1, 10_00) if "l1" in model_name: __snake_case = torch.Tensor( [-0.1312, 0.4353, -1.0499, -0.5124, 0.4183, -0.6793, -1.3777, -0.0893, -0.7358, -2.4328] ) assert torch.allclose(logits[0, :10] , _lowerCamelCase , atol=1E-3 ) assert logits.shape == expected_shape elif "l3" in model_name: __snake_case = torch.Tensor( [-1.3150, -1.5456, -1.2556, -0.8496, -0.7127, -0.7897, -0.9728, -0.3052, 0.3751, -0.3127] ) assert torch.allclose(logits[0, :10] , _lowerCamelCase , atol=1E-3 ) assert logits.shape == expected_shape elif "l7" in model_name: __snake_case = torch.Tensor( [-1.0283, -1.4131, -0.5644, -1.3115, -0.5785, -1.2049, -0.7528, 0.1992, -0.3822, -0.0878] ) assert logits.shape == expected_shape else: raise ValueError( f'''Unknown model checkpoint: {checkpoint_path}. Supported version of efficientformer are l1, l3 and l7''' ) # Save Checkpoints Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase ) model.save_pretrained(_lowerCamelCase ) print(f'''Checkpoint successfuly converted. Model saved at {pytorch_dump_path}''' ) processor.save_pretrained(_lowerCamelCase ) print(f'''Processor successfuly saved at {pytorch_dump_path}''' ) if push_to_hub: print('''Pushing model to the hub...''' ) model.push_to_hub( repo_id=f'''Bearnardd/{pytorch_dump_path}''' , commit_message='''Add model''' , use_temp_dir=_lowerCamelCase , ) processor.push_to_hub( repo_id=f'''Bearnardd/{pytorch_dump_path}''' , commit_message='''Add image processor''' , use_temp_dir=_lowerCamelCase , ) if __name__ == "__main__": UpperCAmelCase_ : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--pytorch_model_path''', default=None, type=str, required=True, help='''Path to EfficientFormer pytorch checkpoint.''', ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The json file for EfficientFormer model config.''', ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') parser.add_argument( '''--no-push_to_hub''', dest='''push_to_hub''', action='''store_false''', help='''Do not push model and image processor to the hub''', ) parser.set_defaults(push_to_hub=True) UpperCAmelCase_ : Union[str, Any] = parser.parse_args() convert_efficientformer_checkpoint( checkpoint_path=args.pytorch_model_path, efficientformer_config_file=args.config_file, pytorch_dump_path=args.pytorch_dump_path, push_to_hub=args.push_to_hub, )
24
0
"""simple docstring""" import argparse import json import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinConfig, SwinForImageClassification def _snake_case ( _snake_case : List[Any] ) -> str: '''simple docstring''' _A = SwinConfig() _A = swin_name.split('_' ) _A = name_split[1] _A = int(name_split[4] ) _A = int(name_split[3][-1] ) if model_size == "tiny": _A = 96 _A = (2, 2, 6, 2) _A = (3, 6, 12, 24) elif model_size == "small": _A = 96 _A = (2, 2, 18, 2) _A = (3, 6, 12, 24) elif model_size == "base": _A = 1_28 _A = (2, 2, 18, 2) _A = (4, 8, 16, 32) else: _A = 1_92 _A = (2, 2, 18, 2) _A = (6, 12, 24, 48) if "in22k" in swin_name: _A = 2_18_41 else: _A = 10_00 _A = 'huggingface/label-files' _A = 'imagenet-1k-id2label.json' _A = json.load(open(hf_hub_download(_lowerCamelCase , _lowerCamelCase , repo_type='dataset' ) , 'r' ) ) _A = {int(_lowerCamelCase ): v for k, v in idalabel.items()} _A = idalabel _A = {v: k for k, v in idalabel.items()} _A = img_size _A = num_classes _A = embed_dim _A = depths _A = num_heads _A = window_size return config def _snake_case ( _snake_case : str ) -> List[str]: '''simple docstring''' if "patch_embed.proj" in name: _A = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "patch_embed.norm" in name: _A = name.replace('patch_embed.norm' , 'embeddings.norm' ) if "layers" in name: _A = 'encoder.' + name if "attn.proj" in name: _A = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: _A = name.replace('attn' , 'attention.self' ) if "norm1" in name: _A = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: _A = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: _A = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: _A = name.replace('mlp.fc2' , 'output.dense' ) if name == "norm.weight": _A = 'layernorm.weight' if name == "norm.bias": _A = 'layernorm.bias' if "head" in name: _A = name.replace('head' , 'classifier' ) else: _A = 'swin.' + name return name def _snake_case ( _snake_case : str , _snake_case : Union[str, Any] ) -> Dict: '''simple docstring''' for key in orig_state_dict.copy().keys(): _A = orig_state_dict.pop(_lowerCamelCase ) if "mask" in key: continue elif "qkv" in key: _A = key.split('.' ) _A = int(key_split[1] ) _A = int(key_split[3] ) _A = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: _A = val[:dim, :] _A = val[ dim : dim * 2, : ] _A = val[-dim:, :] else: _A = val[ :dim ] _A = val[ dim : dim * 2 ] _A = val[ -dim: ] else: _A = val return orig_state_dict def _snake_case ( _snake_case : str , _snake_case : Any ) -> List[Any]: '''simple docstring''' _A = timm.create_model(_lowerCamelCase , pretrained=_lowerCamelCase ) timm_model.eval() _A = get_swin_config(_lowerCamelCase ) _A = SwinForImageClassification(_lowerCamelCase ) model.eval() _A = convert_state_dict(timm_model.state_dict() , _lowerCamelCase ) model.load_state_dict(_lowerCamelCase ) _A = 'http://images.cocodataset.org/val2017/000000039769.jpg' _A = AutoImageProcessor.from_pretrained('microsoft/{}'.format(swin_name.replace('_' , '-' ) ) ) _A = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) _A = image_processor(images=_lowerCamelCase , return_tensors='pt' ) _A = timm_model(inputs['pixel_values'] ) _A = model(**_lowerCamelCase ).logits assert torch.allclose(_lowerCamelCase , _lowerCamelCase , atol=1E-3 ) print(F'''Saving model {swin_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(_lowerCamelCase ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(_lowerCamelCase ) if __name__ == "__main__": a = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--swin_name''', default='''swin_tiny_patch4_window7_224''', type=str, help='''Name of the Swin timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) a = parser.parse_args() convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
7
'''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 MobileViTImageProcessor class lowerCAmelCase ( unittest.TestCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=18 , __SCREAMING_SNAKE_CASE=30 , __SCREAMING_SNAKE_CASE=400 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , ) -> Tuple: '''simple docstring''' __snake_case = size if size is not None else {'''shortest_edge''': 20} __snake_case = crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} __snake_case = parent __snake_case = batch_size __snake_case = num_channels __snake_case = image_size __snake_case = min_resolution __snake_case = max_resolution __snake_case = do_resize __snake_case = size __snake_case = do_center_crop __snake_case = crop_size __snake_case = do_flip_channel_order def lowerCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_flip_channel_order": self.do_flip_channel_order, } @require_torch @require_vision class lowerCAmelCase ( __lowerCAmelCase , unittest.TestCase): __lowercase : Union[str, Any] = MobileViTImageProcessor if is_vision_available() else None def lowerCAmelCase ( self ) -> str: '''simple docstring''' __snake_case = MobileViTImageProcessingTester(self ) @property def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def lowerCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_resize''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''size''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_center_crop''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''center_crop''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_flip_channel_order''' ) ) def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 20} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) __snake_case = 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 lowerCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' pass def lowerCAmelCase ( self ) -> str: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __snake_case = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input __snake_case = 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 __snake_case = image_processing(__SCREAMING_SNAKE_CASE , 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 lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __snake_case = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , numpify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) # Test not batched input __snake_case = 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 __snake_case = image_processing(__SCREAMING_SNAKE_CASE , 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 lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __snake_case = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , torchify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) # Test not batched input __snake_case = 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 __snake_case = image_processing(__SCREAMING_SNAKE_CASE , 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'''], ) , )
24
0
'''simple docstring''' import string from math import logaa def UpperCAmelCase_ ( A , A ): '''simple docstring''' _a : int = document.translate( str.maketrans('' , '' , string.punctuation ) ).replace('\n' , '' ) _a : Union[str, Any] = document_without_punctuation.split(' ' ) # word tokenization return len([word for word in tokenize_document if word.lower() == term.lower()] ) def UpperCAmelCase_ ( A , A ): '''simple docstring''' _a : Any = corpus.lower().translate( str.maketrans('' , '' , string.punctuation ) ) # strip all punctuation and replace it with '' _a : Union[str, Any] = corpus_without_punctuation.split('\n' ) _a : Union[str, Any] = term.lower() return (len([doc for doc in docs if term in doc] ), len(_lowerCamelCase )) def UpperCAmelCase_ ( A , A , A=False ): '''simple docstring''' if smoothing: if n == 0: raise ValueError('log10(0) is undefined.' ) return round(1 + logaa(n / (1 + df) ) , 3 ) if df == 0: raise ZeroDivisionError('df must be > 0' ) elif n == 0: raise ValueError('log10(0) is undefined.' ) return round(logaa(n / df ) , 3 ) def UpperCAmelCase_ ( A , A ): '''simple docstring''' return round(tf * idf , 3 )
120
'''simple docstring''' from typing import Optional import pyspark from .. import Features, NamedSplit from ..download import DownloadMode from ..packaged_modules.spark.spark import Spark from .abc import AbstractDatasetReader class lowerCAmelCase ( __lowerCAmelCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = "arrow" , **__SCREAMING_SNAKE_CASE , ) -> Union[str, Any]: '''simple docstring''' super().__init__( split=__SCREAMING_SNAKE_CASE , features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE , keep_in_memory=__SCREAMING_SNAKE_CASE , streaming=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) __snake_case = load_from_cache_file __snake_case = file_format __snake_case = Spark( df=__SCREAMING_SNAKE_CASE , features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE , working_dir=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) def lowerCAmelCase ( self ) -> str: '''simple docstring''' if self.streaming: return self.builder.as_streaming_dataset(split=self.split ) __snake_case = None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD self.builder.download_and_prepare( download_mode=__SCREAMING_SNAKE_CASE , file_format=self._file_format , ) return self.builder.as_dataset(split=self.split )
24
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 UpperCAmelCase ( UpperCAmelCase ,UpperCAmelCase ,UpperCAmelCase=None ,UpperCAmelCase=None )-> List[Any]: '''simple docstring''' if attention_mask is None: SCREAMING_SNAKE_CASE_ = tf.cast(tf.math.not_equal(_lowerCamelCase ,config.pad_token_id ) ,tf.inta ) return {"input_ids": input_ids, "attention_mask": attention_mask} @require_tf class snake_case : '''simple docstring''' UpperCAmelCase : List[Any] = OPTConfig UpperCAmelCase : List[str] = {} UpperCAmelCase : Tuple = '''gelu''' def __init__( self : Tuple , lowerCAmelCase_ : List[Any] , lowerCAmelCase_ : Optional[Any]=13 , lowerCAmelCase_ : Optional[Any]=7 , lowerCAmelCase_ : List[Any]=True , lowerCAmelCase_ : List[str]=False , lowerCAmelCase_ : str=99 , lowerCAmelCase_ : List[str]=16 , lowerCAmelCase_ : List[Any]=2 , lowerCAmelCase_ : Optional[Any]=4 , lowerCAmelCase_ : Dict=4 , lowerCAmelCase_ : Optional[int]="gelu" , lowerCAmelCase_ : List[str]=0.1 , lowerCAmelCase_ : str=0.1 , lowerCAmelCase_ : Dict=20 , lowerCAmelCase_ : str=2 , lowerCAmelCase_ : Dict=1 , lowerCAmelCase_ : Optional[int]=0 , lowerCAmelCase_ : str=16 , lowerCAmelCase_ : int=16 , ) -> Tuple: """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 _lowercase ( self : List[Any] ) -> str: """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=__SCREAMING_SNAKE_CASE , **self.config_updates , ) SCREAMING_SNAKE_CASE_ = prepare_opt_inputs_dict(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) return config, inputs_dict def _lowercase ( self : Any , lowerCAmelCase_ : List[Any] , lowerCAmelCase_ : List[str] ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE_ = TFOPTModel(config=__SCREAMING_SNAKE_CASE ) 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(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , use_cache=__SCREAMING_SNAKE_CASE ) 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(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE )[0] SCREAMING_SNAKE_CASE_ = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , past_key_values=__SCREAMING_SNAKE_CASE )[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(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , rtol=1e-3 ) @require_tf class snake_case ( __lowerCAmelCase , __lowerCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCAmelCase : Optional[int] = (TFOPTModel, TFOPTForCausalLM) if is_tf_available() else () UpperCAmelCase : Tuple = (TFOPTForCausalLM,) if is_tf_available() else () UpperCAmelCase : int = ( {'''feature-extraction''': TFOPTModel, '''text-generation''': TFOPTForCausalLM} if is_tf_available() else {} ) UpperCAmelCase : Tuple = False UpperCAmelCase : int = False UpperCAmelCase : Any = False UpperCAmelCase : Optional[Any] = 10 def _lowercase ( self : Any ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE_ = TFOPTModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE ) def _lowercase ( self : int ) -> List[str]: """simple docstring""" self.config_tester.run_common_tests() def _lowercase ( self : Union[str, Any] ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self : List[str] ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() def _get_word_embedding_weight(lowerCAmelCase_ : Union[str, Any] , lowerCAmelCase_ : int ): if hasattr(__SCREAMING_SNAKE_CASE , '''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(__SCREAMING_SNAKE_CASE , '''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=__SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = _get_word_embedding_weight(__SCREAMING_SNAKE_CASE , model.get_input_embeddings() ) SCREAMING_SNAKE_CASE_ = _get_word_embedding_weight(__SCREAMING_SNAKE_CASE , model.get_output_embeddings() ) # reshape the embeddings model.resize_token_embeddings(__SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = _get_word_embedding_weight(__SCREAMING_SNAKE_CASE , model.get_input_embeddings() ) SCREAMING_SNAKE_CASE_ = _get_word_embedding_weight(__SCREAMING_SNAKE_CASE , 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] , __SCREAMING_SNAKE_CASE ) # 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(__SCREAMING_SNAKE_CASE ) if old_output_embeddings is not None and new_output_embeddings is not None: self.assertEqual(new_output_embeddings.shape[0] , __SCREAMING_SNAKE_CASE ) 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(__SCREAMING_SNAKE_CASE ) def UpperCAmelCase ( UpperCAmelCase )-> List[str]: '''simple docstring''' return tf.constant(_lowerCamelCase ,dtype=tf.intaa ) @require_tf class snake_case ( unittest.TestCase ): '''simple docstring''' UpperCAmelCase : Dict = 99 def _lowercase ( self : List[str] ) -> str: """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 ): '''simple docstring''' @slow def _lowercase ( self : Tuple ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ = TFOPTModel.from_pretrained('''facebook/opt-350m''' ) SCREAMING_SNAKE_CASE_ = _long_tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]] ) SCREAMING_SNAKE_CASE_ = tf.not_equal(__SCREAMING_SNAKE_CASE , model.config.pad_token_id ) with tf.GradientTape(): SCREAMING_SNAKE_CASE_ = model(input_ids=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE ).last_hidden_state SCREAMING_SNAKE_CASE_ = (1, 11, 512) self.assertEqual(output.shape , __SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = tf.constant( [[-0.2_873, -1.9_218, -0.3_033], [-1.2_710, -0.1_338, -0.1_902], [0.4_095, 0.1_214, -1.3_121]] ) self.assertTrue(np.allclose(output[:, :3, :3] , __SCREAMING_SNAKE_CASE , atol=4e-3 ) ) SCREAMING_SNAKE_CASE_ = tf.function(__SCREAMING_SNAKE_CASE , jit_compile=__SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = xla_generate(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )[0] self.assertTrue(np.allclose(output[:, :3, :3] , __SCREAMING_SNAKE_CASE , atol=4e-2 ) ) @require_tf @slow class snake_case ( unittest.TestCase ): '''simple docstring''' def _lowercase ( self : int ) -> Optional[Any]: """simple docstring""" super().setUp() SCREAMING_SNAKE_CASE_ = '''facebook/opt-350m''' def _lowercase ( self : Optional[Any] ) -> Union[str, Any]: """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(__SCREAMING_SNAKE_CASE , return_tensors='''tf''' , padding=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) 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.3_851, -13.8_923, -10.5_229, -10.7_533, -0.2_309, -10.2_384, -0.5_365, -9.0_947, -5.1_670], [-4.7_073, -10.6_276, -3.9_415, -21.5_242, -0.2_822, -0.2_822, -0.2_822, -0.2_822, -0.2_822], [0.6_247, -3.4_229, -8.9_179, -1.4_297, -14.1_650, 1.4_146, -9.0_218, -0.2_703, -0.2_703], [6.4_783, -1.9_913, -10.7_926, -2.3_336, 1.5_092, -0.9_974, -6.8_213, 1.3_477, 1.3_477], ] ) self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , atol=1e-4 ) ) SCREAMING_SNAKE_CASE_ = tf.function(__SCREAMING_SNAKE_CASE , jit_compile=__SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = tf.math.reduce_mean(xla_generate(inputs.input_ids , attention_mask=inputs.attention_mask )[0] , axis=-1 ) self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , atol=1e-4 ) ) @require_tf @slow class snake_case ( unittest.TestCase ): '''simple docstring''' @property def _lowercase ( self : Union[str, Any] ) -> Dict: """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 _lowercase ( self : str ) -> List[str]: """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(__SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = TFOPTForCausalLM.from_pretrained(__SCREAMING_SNAKE_CASE ) for prompt in self.prompts: SCREAMING_SNAKE_CASE_ = tokenizer(__SCREAMING_SNAKE_CASE , return_tensors='''tf''' ).input_ids SCREAMING_SNAKE_CASE_ = model.generate(__SCREAMING_SNAKE_CASE , max_length=10 ) SCREAMING_SNAKE_CASE_ = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) predicted_outputs += generated_string self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self : Tuple ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ = '''facebook/opt-350m''' SCREAMING_SNAKE_CASE_ = GPTaTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = TFOPTForCausalLM.from_pretrained(__SCREAMING_SNAKE_CASE ) 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(__SCREAMING_SNAKE_CASE , return_tensors='''tf''' , padding=__SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = inputs['''input_ids'''] SCREAMING_SNAKE_CASE_ = model.generate(input_ids=__SCREAMING_SNAKE_CASE , attention_mask=inputs['''attention_mask'''] ) SCREAMING_SNAKE_CASE_ = tokenizer(sentences[0] , return_tensors='''tf''' ).input_ids SCREAMING_SNAKE_CASE_ = model.generate(input_ids=__SCREAMING_SNAKE_CASE ) 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=__SCREAMING_SNAKE_CASE , max_length=model.config.max_length - num_paddings ) SCREAMING_SNAKE_CASE_ = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = tokenizer.decode(output_non_padded[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = tokenizer.decode(output_padded[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) 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(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , [non_padded_sentence, padded_sentence] ) def _lowercase ( self : List[str] ) -> Dict: """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(__SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = TFOPTForCausalLM.from_pretrained(__SCREAMING_SNAKE_CASE ) for prompt in self.prompts: SCREAMING_SNAKE_CASE_ = tokenizer(__SCREAMING_SNAKE_CASE , return_tensors='''tf''' ).input_ids SCREAMING_SNAKE_CASE_ = model.generate(__SCREAMING_SNAKE_CASE , max_length=10 ) SCREAMING_SNAKE_CASE_ = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) predicted_outputs += generated_string self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
393
'''simple docstring''' import json from typing import Dict, List, Optional, Tuple, Union from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_led import LEDTokenizer UpperCAmelCase_ : Any = logging.get_logger(__name__) UpperCAmelCase_ : str = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} UpperCAmelCase_ : str = { '''vocab_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json''', }, '''merges_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt''', }, '''tokenizer_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json''', }, } UpperCAmelCase_ : Union[str, Any] = { '''allenai/led-base-16384''': 1_6_3_8_4, } class lowerCAmelCase ( __lowerCAmelCase): __lowercase : Dict = VOCAB_FILES_NAMES __lowercase : Dict = PRETRAINED_VOCAB_FILES_MAP __lowercase : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Union[str, Any] = LEDTokenizer __lowercase : int = ['''input_ids''', '''attention_mask'''] def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="replace" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="<mask>" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , **__SCREAMING_SNAKE_CASE , ) -> List[Any]: '''simple docstring''' super().__init__( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , errors=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) __snake_case = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: __snake_case = getattr(__SCREAMING_SNAKE_CASE , pre_tok_state.pop('''type''' ) ) __snake_case = add_prefix_space __snake_case = pre_tok_class(**__SCREAMING_SNAKE_CASE ) __snake_case = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` __snake_case = '''post_processor''' __snake_case = getattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if tokenizer_component_instance: __snake_case = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: __snake_case = tuple(state['''sep'''] ) if "cls" in state: __snake_case = tuple(state['''cls'''] ) __snake_case = False if state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: __snake_case = add_prefix_space __snake_case = True if state.get('''trim_offsets''' , __SCREAMING_SNAKE_CASE ) != trim_offsets: __snake_case = trim_offsets __snake_case = True if changes_to_apply: __snake_case = getattr(__SCREAMING_SNAKE_CASE , state.pop('''type''' ) ) __snake_case = component_class(**__SCREAMING_SNAKE_CASE ) setattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @property # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED def lowerCAmelCase ( self ) -> str: '''simple docstring''' if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' __snake_case = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else value __snake_case = value def lowerCAmelCase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> BatchEncoding: '''simple docstring''' __snake_case = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' '''to use it with pretokenized inputs.''' ) return super()._batch_encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) -> BatchEncoding: '''simple docstring''' __snake_case = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' '''to use it with pretokenized inputs.''' ) return super()._encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) -> Tuple[str]: '''simple docstring''' __snake_case = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) -> List[Any]: '''simple docstring''' __snake_case = [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 , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) -> List[int]: '''simple docstring''' __snake_case = [self.sep_token_id] __snake_case = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def lowerCAmelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , ) -> dict: '''simple docstring''' __snake_case = super()._pad( encoded_inputs=__SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , padding_strategy=__SCREAMING_SNAKE_CASE , pad_to_multiple_of=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , ) # Load from model defaults if return_attention_mask is None: __snake_case = '''attention_mask''' in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: __snake_case = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. __snake_case = len(encoded_inputs['''global_attention_mask'''] ) != len(__SCREAMING_SNAKE_CASE ) if needs_to_be_padded: __snake_case = len(__SCREAMING_SNAKE_CASE ) - len(encoded_inputs['''global_attention_mask'''] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` __snake_case = ( encoded_inputs['''global_attention_mask'''] + [-1] * difference ) elif self.padding_side == "left": __snake_case = [-1] * difference + encoded_inputs[ '''global_attention_mask''' ] else: raise ValueError('''Invalid padding strategy:''' + str(self.padding_side ) ) return encoded_inputs
24
0
"""simple docstring""" import json import os import shutil import tempfile import unittest from multiprocessing import get_context from pathlib import Path import datasets import numpy as np from datasets import load_dataset from parameterized import parameterized from transformers import AutoProcessor from transformers.models.wavaveca import WavaVecaCTCTokenizer, WavaVecaFeatureExtractor from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.testing_utils import require_pyctcdecode, require_torch, require_torchaudio, slow from transformers.utils import FEATURE_EXTRACTOR_NAME, is_pyctcdecode_available, is_torch_available from ..wavaveca.test_feature_extraction_wavaveca import floats_list if is_pyctcdecode_available(): from huggingface_hub import snapshot_download from pyctcdecode import BeamSearchDecoderCTC from transformers.models.wavaveca_with_lm import WavaVecaProcessorWithLM from transformers.models.wavaveca_with_lm.processing_wavaveca_with_lm import WavaVecaDecoderWithLMOutput if is_torch_available(): from transformers import WavaVecaForCTC @require_pyctcdecode class __a ( unittest.TestCase ): def lowerCamelCase_ ( self ): '''simple docstring''' lowerCAmelCase_ = '''| <pad> <unk> <s> </s> a b c d e f g h i j k'''.split() lowerCAmelCase_ = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) lowerCAmelCase_ = { '''unk_token''': '''<unk>''', '''bos_token''': '''<s>''', '''eos_token''': '''</s>''', } lowerCAmelCase_ = { '''feature_size''': 1, '''padding_value''': 0.0, '''sampling_rate''': 1_6000, '''return_attention_mask''': False, '''do_normalize''': True, } lowerCAmelCase_ = tempfile.mkdtemp() lowerCAmelCase_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) lowerCAmelCase_ = os.path.join(self.tmpdirname , __SCREAMING_SNAKE_CASE ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.feature_extraction_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) # load decoder from hub lowerCAmelCase_ = '''hf-internal-testing/ngram-beam-search-decoder''' def lowerCamelCase_ ( self , **UpperCAmelCase ): '''simple docstring''' lowerCAmelCase_ = self.add_kwargs_tokens_map.copy() kwargs.update(__SCREAMING_SNAKE_CASE ) return WavaVecaCTCTokenizer.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def lowerCamelCase_ ( self , **UpperCAmelCase ): '''simple docstring''' return WavaVecaFeatureExtractor.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def lowerCamelCase_ ( self , **UpperCAmelCase ): '''simple docstring''' return BeamSearchDecoderCTC.load_from_hf_hub(self.decoder_name , **__SCREAMING_SNAKE_CASE ) def lowerCamelCase_ ( self ): '''simple docstring''' shutil.rmtree(self.tmpdirname ) def lowerCamelCase_ ( self ): '''simple docstring''' lowerCAmelCase_ = self.get_tokenizer() lowerCAmelCase_ = self.get_feature_extractor() lowerCAmelCase_ = self.get_decoder() lowerCAmelCase_ = WavaVecaProcessorWithLM(tokenizer=__SCREAMING_SNAKE_CASE , feature_extractor=__SCREAMING_SNAKE_CASE , decoder=__SCREAMING_SNAKE_CASE ) processor.save_pretrained(self.tmpdirname ) lowerCAmelCase_ = WavaVecaProcessorWithLM.from_pretrained(self.tmpdirname ) # tokenizer self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , __SCREAMING_SNAKE_CASE ) # feature extractor self.assertEqual(processor.feature_extractor.to_json_string() , feature_extractor.to_json_string() ) self.assertIsInstance(processor.feature_extractor , __SCREAMING_SNAKE_CASE ) # decoder self.assertEqual(processor.decoder._alphabet.labels , decoder._alphabet.labels ) self.assertEqual( processor.decoder.model_container[decoder._model_key]._unigram_set , decoder.model_container[decoder._model_key]._unigram_set , ) self.assertIsInstance(processor.decoder , __SCREAMING_SNAKE_CASE ) def lowerCamelCase_ ( self ): '''simple docstring''' lowerCAmelCase_ = WavaVecaProcessorWithLM( tokenizer=self.get_tokenizer() , feature_extractor=self.get_feature_extractor() , decoder=self.get_decoder() ) processor.save_pretrained(self.tmpdirname ) # make sure that error is thrown when decoder alphabet doesn't match lowerCAmelCase_ = WavaVecaProcessorWithLM.from_pretrained( self.tmpdirname , alpha=5.0 , beta=3.0 , score_boundary=-7.0 , unk_score_offset=3 ) # decoder self.assertEqual(processor.language_model.alpha , 5.0 ) self.assertEqual(processor.language_model.beta , 3.0 ) self.assertEqual(processor.language_model.score_boundary , -7.0 ) self.assertEqual(processor.language_model.unk_score_offset , 3 ) def lowerCamelCase_ ( self ): '''simple docstring''' lowerCAmelCase_ = self.get_tokenizer() # add token to trigger raise tokenizer.add_tokens(['''xx'''] ) with self.assertRaisesRegex(__SCREAMING_SNAKE_CASE , '''include''' ): WavaVecaProcessorWithLM( tokenizer=__SCREAMING_SNAKE_CASE , feature_extractor=self.get_feature_extractor() , decoder=self.get_decoder() ) def lowerCamelCase_ ( self ): '''simple docstring''' lowerCAmelCase_ = self.get_feature_extractor() lowerCAmelCase_ = self.get_tokenizer() lowerCAmelCase_ = self.get_decoder() lowerCAmelCase_ = WavaVecaProcessorWithLM(tokenizer=__SCREAMING_SNAKE_CASE , feature_extractor=__SCREAMING_SNAKE_CASE , decoder=__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = floats_list((3, 1000) ) lowerCAmelCase_ = feature_extractor(__SCREAMING_SNAKE_CASE , return_tensors='''np''' ) lowerCAmelCase_ = processor(__SCREAMING_SNAKE_CASE , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2 ) def lowerCamelCase_ ( self ): '''simple docstring''' lowerCAmelCase_ = self.get_feature_extractor() lowerCAmelCase_ = self.get_tokenizer() lowerCAmelCase_ = self.get_decoder() lowerCAmelCase_ = WavaVecaProcessorWithLM(tokenizer=__SCREAMING_SNAKE_CASE , feature_extractor=__SCREAMING_SNAKE_CASE , decoder=__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = '''This is a test string''' lowerCAmelCase_ = processor(text=__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = tokenizer(__SCREAMING_SNAKE_CASE ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def lowerCamelCase_ ( self , UpperCAmelCase=(2, 10, 16) , UpperCAmelCase=77 ): '''simple docstring''' np.random.seed(__SCREAMING_SNAKE_CASE ) return np.random.rand(*__SCREAMING_SNAKE_CASE ) def lowerCamelCase_ ( self ): '''simple docstring''' lowerCAmelCase_ = self.get_feature_extractor() lowerCAmelCase_ = self.get_tokenizer() lowerCAmelCase_ = self.get_decoder() lowerCAmelCase_ = WavaVecaProcessorWithLM(tokenizer=__SCREAMING_SNAKE_CASE , feature_extractor=__SCREAMING_SNAKE_CASE , decoder=__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = self._get_dummy_logits(shape=(10, 16) , seed=13 ) lowerCAmelCase_ = processor.decode(__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = decoder.decode_beams(__SCREAMING_SNAKE_CASE )[0] self.assertEqual(decoded_decoder[0] , decoded_processor.text ) self.assertEqual('''</s> <s> </s>''' , decoded_processor.text ) self.assertEqual(decoded_decoder[-2] , decoded_processor.logit_score ) self.assertEqual(decoded_decoder[-1] , decoded_processor.lm_score ) @parameterized.expand([[None], ['''fork'''], ['''spawn''']] ) def lowerCamelCase_ ( self , UpperCAmelCase ): '''simple docstring''' lowerCAmelCase_ = self.get_feature_extractor() lowerCAmelCase_ = self.get_tokenizer() lowerCAmelCase_ = self.get_decoder() lowerCAmelCase_ = WavaVecaProcessorWithLM(tokenizer=__SCREAMING_SNAKE_CASE , feature_extractor=__SCREAMING_SNAKE_CASE , decoder=__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = self._get_dummy_logits() # note: pool should be instantiated *after* Wav2Vec2ProcessorWithLM. # otherwise, the LM won't be available to the pool's sub-processes. # manual logic used to allow parameterized test for both pool=None and pool=Pool(...) if pool_context is None: lowerCAmelCase_ = processor.batch_decode(__SCREAMING_SNAKE_CASE ) else: with get_context(__SCREAMING_SNAKE_CASE ).Pool() as pool: lowerCAmelCase_ = processor.batch_decode(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = list(__SCREAMING_SNAKE_CASE ) with get_context('''fork''' ).Pool() as p: lowerCAmelCase_ = decoder.decode_beams_batch(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ = [], [], [] for beams in decoded_beams: texts_decoder.append(beams[0][0] ) logit_scores_decoder.append(beams[0][-2] ) lm_scores_decoder.append(beams[0][-1] ) self.assertListEqual(__SCREAMING_SNAKE_CASE , decoded_processor.text ) self.assertListEqual(['''<s> <s> </s>''', '''<s> <s> <s>'''] , decoded_processor.text ) self.assertListEqual(__SCREAMING_SNAKE_CASE , decoded_processor.logit_score ) self.assertListEqual(__SCREAMING_SNAKE_CASE , decoded_processor.lm_score ) def lowerCamelCase_ ( self ): '''simple docstring''' lowerCAmelCase_ = self.get_feature_extractor() lowerCAmelCase_ = self.get_tokenizer() lowerCAmelCase_ = self.get_decoder() lowerCAmelCase_ = WavaVecaProcessorWithLM(tokenizer=__SCREAMING_SNAKE_CASE , feature_extractor=__SCREAMING_SNAKE_CASE , decoder=__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = self._get_dummy_logits() lowerCAmelCase_ = 15 lowerCAmelCase_ = -2_0.0 lowerCAmelCase_ = -4.0 lowerCAmelCase_ = processor.batch_decode( __SCREAMING_SNAKE_CASE , beam_width=__SCREAMING_SNAKE_CASE , beam_prune_logp=__SCREAMING_SNAKE_CASE , token_min_logp=__SCREAMING_SNAKE_CASE , ) lowerCAmelCase_ = decoded_processor_out.text lowerCAmelCase_ = list(__SCREAMING_SNAKE_CASE ) with get_context('''fork''' ).Pool() as pool: lowerCAmelCase_ = decoder.decode_beams_batch( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , beam_width=__SCREAMING_SNAKE_CASE , beam_prune_logp=__SCREAMING_SNAKE_CASE , token_min_logp=__SCREAMING_SNAKE_CASE , ) lowerCAmelCase_ = [d[0][0] for d in decoded_decoder_out] lowerCAmelCase_ = [d[0][2] for d in decoded_decoder_out] lowerCAmelCase_ = [d[0][3] for d in decoded_decoder_out] self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertListEqual(['''</s> <s> <s>''', '''<s> <s> <s>'''] , __SCREAMING_SNAKE_CASE ) self.assertTrue(np.array_equal(__SCREAMING_SNAKE_CASE , decoded_processor_out.logit_score ) ) self.assertTrue(np.allclose([-2_0.0_5_4, -1_8.4_4_7] , __SCREAMING_SNAKE_CASE , atol=1E-3 ) ) self.assertTrue(np.array_equal(__SCREAMING_SNAKE_CASE , decoded_processor_out.lm_score ) ) self.assertTrue(np.allclose([-1_5.5_5_4, -1_3.9_4_7_4] , __SCREAMING_SNAKE_CASE , atol=1E-3 ) ) def lowerCamelCase_ ( self ): '''simple docstring''' lowerCAmelCase_ = self.get_feature_extractor() lowerCAmelCase_ = self.get_tokenizer() lowerCAmelCase_ = self.get_decoder() lowerCAmelCase_ = WavaVecaProcessorWithLM(tokenizer=__SCREAMING_SNAKE_CASE , feature_extractor=__SCREAMING_SNAKE_CASE , decoder=__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = self._get_dummy_logits() lowerCAmelCase_ = 2.0 lowerCAmelCase_ = 5.0 lowerCAmelCase_ = -2_0.0 lowerCAmelCase_ = True lowerCAmelCase_ = processor.batch_decode( __SCREAMING_SNAKE_CASE , alpha=__SCREAMING_SNAKE_CASE , beta=__SCREAMING_SNAKE_CASE , unk_score_offset=__SCREAMING_SNAKE_CASE , lm_score_boundary=__SCREAMING_SNAKE_CASE , ) lowerCAmelCase_ = decoded_processor_out.text lowerCAmelCase_ = list(__SCREAMING_SNAKE_CASE ) decoder.reset_params( alpha=__SCREAMING_SNAKE_CASE , beta=__SCREAMING_SNAKE_CASE , unk_score_offset=__SCREAMING_SNAKE_CASE , lm_score_boundary=__SCREAMING_SNAKE_CASE , ) with get_context('''fork''' ).Pool() as pool: lowerCAmelCase_ = decoder.decode_beams_batch( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , ) lowerCAmelCase_ = [d[0][0] for d in decoded_decoder_out] self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertListEqual(['''<s> </s> <s> </s> </s>''', '''</s> </s> <s> </s> </s>'''] , __SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = processor.decoder.model_container[processor.decoder._model_key] self.assertEqual(lm_model.alpha , 2.0 ) self.assertEqual(lm_model.beta , 5.0 ) self.assertEqual(lm_model.unk_score_offset , -2_0.0 ) self.assertEqual(lm_model.score_boundary , __SCREAMING_SNAKE_CASE ) def lowerCamelCase_ ( self ): '''simple docstring''' lowerCAmelCase_ = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) lowerCAmelCase_ = processor.decoder.model_container[processor.decoder._model_key] lowerCAmelCase_ = Path(language_model._kenlm_model.path.decode('''utf-8''' ) ).parent.parent.absolute() lowerCAmelCase_ = os.listdir(__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = ['''alphabet.json''', '''language_model'''] downloaded_decoder_files.sort() expected_decoder_files.sort() # test that only decoder relevant files from # https://huggingface.co/hf-internal-testing/processor_with_lm/tree/main # are downloaded and none of the rest (e.g. README.md, ...) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def lowerCamelCase_ ( self ): '''simple docstring''' lowerCAmelCase_ = snapshot_download('''hf-internal-testing/processor_with_lm''' ) lowerCAmelCase_ = WavaVecaProcessorWithLM.from_pretrained(__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = processor.decoder.model_container[processor.decoder._model_key] lowerCAmelCase_ = Path(language_model._kenlm_model.path.decode('''utf-8''' ) ).parent.parent.absolute() lowerCAmelCase_ = os.listdir(__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = os.listdir(__SCREAMING_SNAKE_CASE ) local_decoder_files.sort() expected_decoder_files.sort() # test that both decoder form hub and local files in cache are the same self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def lowerCamelCase_ ( self ): '''simple docstring''' lowerCAmelCase_ = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) lowerCAmelCase_ = AutoProcessor.from_pretrained('''hf-internal-testing/processor_with_lm''' ) lowerCAmelCase_ = floats_list((3, 1000) ) lowerCAmelCase_ = processor_wavaveca(__SCREAMING_SNAKE_CASE , return_tensors='''np''' ) lowerCAmelCase_ = processor_auto(__SCREAMING_SNAKE_CASE , return_tensors='''np''' ) for key in input_wavaveca.keys(): self.assertAlmostEqual(input_wavaveca[key].sum() , input_auto[key].sum() , delta=1E-2 ) lowerCAmelCase_ = self._get_dummy_logits() lowerCAmelCase_ = processor_wavaveca.batch_decode(__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = processor_auto.batch_decode(__SCREAMING_SNAKE_CASE ) self.assertListEqual(decoded_wavaveca.text , decoded_auto.text ) def lowerCamelCase_ ( self ): '''simple docstring''' lowerCAmelCase_ = self.get_feature_extractor() lowerCAmelCase_ = self.get_tokenizer() lowerCAmelCase_ = self.get_decoder() lowerCAmelCase_ = WavaVecaProcessorWithLM(tokenizer=__SCREAMING_SNAKE_CASE , feature_extractor=__SCREAMING_SNAKE_CASE , decoder=__SCREAMING_SNAKE_CASE ) self.assertListEqual( processor.model_input_names , feature_extractor.model_input_names , msg='''`processor` and `feature_extractor` model input names do not match''' , ) @staticmethod def lowerCamelCase_ ( UpperCAmelCase , UpperCAmelCase ): '''simple docstring''' lowerCAmelCase_ = [d[key] for d in offsets] return retrieved_list def lowerCamelCase_ ( self ): '''simple docstring''' lowerCAmelCase_ = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) lowerCAmelCase_ = self._get_dummy_logits()[0] lowerCAmelCase_ = processor.decode(__SCREAMING_SNAKE_CASE , output_word_offsets=__SCREAMING_SNAKE_CASE ) # check Wav2Vec2CTCTokenizerOutput keys for word self.assertEqual(len(outputs.keys() ) , 4 ) self.assertTrue('''text''' in outputs ) self.assertTrue('''word_offsets''' in outputs ) self.assertTrue(isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) self.assertEqual(''' '''.join(self.get_from_offsets(outputs['''word_offsets'''] , '''word''' ) ) , outputs.text ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''] , '''word''' ) , ['''<s>''', '''<s>''', '''</s>'''] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''] , '''start_offset''' ) , [0, 2, 4] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''] , '''end_offset''' ) , [1, 3, 5] ) def lowerCamelCase_ ( self ): '''simple docstring''' lowerCAmelCase_ = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) lowerCAmelCase_ = self._get_dummy_logits() lowerCAmelCase_ = processor.batch_decode(__SCREAMING_SNAKE_CASE , output_word_offsets=__SCREAMING_SNAKE_CASE ) # check Wav2Vec2CTCTokenizerOutput keys for word self.assertEqual(len(outputs.keys() ) , 4 ) self.assertTrue('''text''' in outputs ) self.assertTrue('''word_offsets''' in outputs ) self.assertTrue(isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) self.assertListEqual( [''' '''.join(self.get_from_offsets(__SCREAMING_SNAKE_CASE , '''word''' ) ) for o in outputs['''word_offsets''']] , outputs.text ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''][0] , '''word''' ) , ['''<s>''', '''<s>''', '''</s>'''] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''][0] , '''start_offset''' ) , [0, 2, 4] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''][0] , '''end_offset''' ) , [1, 3, 5] ) @slow @require_torch @require_torchaudio def lowerCamelCase_ ( self ): '''simple docstring''' import torch lowerCAmelCase_ = load_dataset('''common_voice''' , '''en''' , split='''train''' , streaming=__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = ds.cast_column('''audio''' , datasets.Audio(sampling_rate=1_6000 ) ) lowerCAmelCase_ = iter(__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = next(__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = AutoProcessor.from_pretrained('''patrickvonplaten/wav2vec2-base-100h-with-lm''' ) lowerCAmelCase_ = WavaVecaForCTC.from_pretrained('''patrickvonplaten/wav2vec2-base-100h-with-lm''' ) # compare to filename `common_voice_en_100038.mp3` of dataset viewer on https://huggingface.co/datasets/common_voice/viewer/en/train lowerCAmelCase_ = processor(sample['''audio''']['''array'''] , return_tensors='''pt''' ).input_values with torch.no_grad(): lowerCAmelCase_ = model(__SCREAMING_SNAKE_CASE ).logits.cpu().numpy() lowerCAmelCase_ = processor.decode(logits[0] , output_word_offsets=__SCREAMING_SNAKE_CASE ) lowerCAmelCase_ = model.config.inputs_to_logits_ratio / processor.feature_extractor.sampling_rate lowerCAmelCase_ = [ { '''start_time''': d['''start_offset'''] * time_offset, '''end_time''': d['''end_offset'''] * time_offset, '''word''': d['''word'''], } for d in output['''word_offsets'''] ] lowerCAmelCase_ = '''WHY DOES MILISANDRA LOOK LIKE SHE WANTS TO CONSUME JOHN SNOW ON THE RIVER AT THE WALL''' # output words self.assertEqual(''' '''.join(self.get_from_offsets(__SCREAMING_SNAKE_CASE , '''word''' ) ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(''' '''.join(self.get_from_offsets(__SCREAMING_SNAKE_CASE , '''word''' ) ) , output.text ) # output times lowerCAmelCase_ = torch.tensor(self.get_from_offsets(__SCREAMING_SNAKE_CASE , '''start_time''' ) ) lowerCAmelCase_ = torch.tensor(self.get_from_offsets(__SCREAMING_SNAKE_CASE , '''end_time''' ) ) # fmt: off lowerCAmelCase_ = torch.tensor([1.4_1_9_9, 1.6_5_9_9, 2.2_5_9_9, 3.0, 3.2_4, 3.5_9_9_9, 3.7_9_9_9, 4.0_9_9_9, 4.2_6, 4.9_4, 5.2_8, 5.6_5_9_9, 5.7_8, 5.9_4, 6.3_2, 6.5_3_9_9, 6.6_5_9_9] ) lowerCAmelCase_ = torch.tensor([1.5_3_9_9, 1.8_9_9_9, 2.9, 3.1_6, 3.5_3_9_9, 3.7_2, 4.0_1_9_9, 4.1_7_9_9, 4.7_6, 5.1_5_9_9, 5.5_5_9_9, 5.6_9_9_9, 5.8_6, 6.1_9_9_9, 6.3_8, 6.6_1_9_9, 6.9_4] ) # fmt: on self.assertTrue(torch.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , atol=0.0_1 ) ) self.assertTrue(torch.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , atol=0.0_1 ) )
552
'''simple docstring''' from collections import deque def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> Optional[int]: '''simple docstring''' __snake_case = len(_lowerCamelCase ) __snake_case = deque() __snake_case = [False for _ in range(_lowerCamelCase )] __snake_case = [-1 for _ in range(_lowerCamelCase )] __snake_case = index_of[:] def strong_connect(_lowerCamelCase : Any , _lowerCamelCase : Dict , _lowerCamelCase : List[str] ): __snake_case = index # the number when this node is seen __snake_case = index # lowest rank node reachable from here index += 1 stack.append(_lowerCamelCase ) __snake_case = True for w in g[v]: if index_of[w] == -1: __snake_case = strong_connect(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) __snake_case = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) elif on_stack[w]: __snake_case = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) if lowlink_of[v] == index_of[v]: __snake_case = [] __snake_case = stack.pop() __snake_case = False component.append(_lowerCamelCase ) while w != v: __snake_case = stack.pop() __snake_case = False component.append(_lowerCamelCase ) components.append(_lowerCamelCase ) return index __snake_case = [] for v in range(_lowerCamelCase ): if index_of[v] == -1: strong_connect(_lowerCamelCase , 0 , _lowerCamelCase ) return components def _UpperCamelCase (_lowerCamelCase : Tuple , _lowerCamelCase : Optional[Any] )-> Dict: '''simple docstring''' __snake_case = [[] for _ in range(_lowerCamelCase )] for u, v in edges: g[u].append(_lowerCamelCase ) return g if __name__ == "__main__": # Test UpperCAmelCase_ : List[str] = 7 UpperCAmelCase_ : int = [0, 0, 1, 2, 3, 3, 4, 4, 6] UpperCAmelCase_ : Dict = [1, 3, 2, 0, 1, 4, 5, 6, 5] UpperCAmelCase_ : List[str] = [(u, v) for u, v in zip(source, target)] UpperCAmelCase_ : Tuple = create_graph(n_vertices, edges) assert [[5], [6], [4], [3, 2, 1, 0]] == tarjan(g)
24
0
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ( ImageTextPipelineOutput, UniDiffuserPipeline, ) else: from .modeling_text_decoder import UniDiffuserTextDecoder from .modeling_uvit import UniDiffuserModel, UTransformeraDModel from .pipeline_unidiffuser import ImageTextPipelineOutput, UniDiffuserPipeline
457
'''simple docstring''' import unittest from transformers import BarthezTokenizer, BarthezTokenizerFast, BatchEncoding from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers @require_sentencepiece @slow # see https://github.com/huggingface/transformers/issues/11457 class lowerCAmelCase ( __lowerCAmelCase , unittest.TestCase): __lowercase : int = BarthezTokenizer __lowercase : Any = BarthezTokenizerFast __lowercase : Dict = True __lowercase : Optional[int] = True def lowerCAmelCase ( self ) -> Dict: '''simple docstring''' super().setUp() __snake_case = BarthezTokenizerFast.from_pretrained('''moussaKam/mbarthez''' ) tokenizer.save_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname , legacy_format=__SCREAMING_SNAKE_CASE ) __snake_case = tokenizer def lowerCAmelCase ( self ) -> Dict: '''simple docstring''' __snake_case = '''<pad>''' __snake_case = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<s>''' ) self.assertEqual(vocab_keys[1] , '''<pad>''' ) self.assertEqual(vocab_keys[-1] , '''<mask>''' ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 10_1122 ) def lowerCAmelCase ( self ) -> Any: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 10_1122 ) @require_torch def lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] __snake_case = [0, 57, 3018, 7_0307, 91, 2] __snake_case = self.tokenizer( __SCREAMING_SNAKE_CASE , max_length=len(__SCREAMING_SNAKE_CASE ) , padding=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertEqual((2, 6) , batch.input_ids.shape ) self.assertEqual((2, 6) , batch.attention_mask.shape ) __snake_case = batch.input_ids.tolist()[0] self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def lowerCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' if not self.test_rust_tokenizer: return __snake_case = self.get_tokenizer() __snake_case = self.get_rust_tokenizer() __snake_case = '''I was born in 92000, and this is falsé.''' __snake_case = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) __snake_case = rust_tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __snake_case = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) __snake_case = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __snake_case = self.get_rust_tokenizer() __snake_case = tokenizer.encode(__SCREAMING_SNAKE_CASE ) __snake_case = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = {'''input_ids''': [[0, 490, 1_4328, 4507, 354, 47, 4_3669, 95, 25, 7_8117, 2_0215, 1_9779, 190, 22, 400, 4, 3_5343, 8_0310, 603, 86, 2_4937, 105, 3_3438, 9_4762, 196, 3_9642, 7, 15, 1_5933, 173, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1_0534, 87, 25, 66, 3358, 196, 5_5289, 8, 8_2961, 81, 2204, 7_5203, 7, 15, 763, 1_2956, 216, 178, 1_4328, 9595, 1377, 6_9693, 7, 448, 7_1021, 196, 1_8106, 1437, 1_3974, 108, 9083, 4, 4_9315, 7, 39, 86, 1326, 2793, 4_6333, 4, 448, 196, 7_4588, 7, 4_9315, 7, 39, 21, 822, 3_8470, 74, 21, 6_6723, 6_2480, 8, 2_2050, 5, 2]], '''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, 0, 0, 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # moussaKam/mbarthez is a french model. So we also use french texts. __snake_case = [ '''Le transformeur est un modèle d\'apprentissage profond introduit en 2017, ''' '''utilisé principalement dans le domaine du traitement automatique des langues (TAL).''', '''À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus ''' '''pour gérer des données séquentielles, telles que le langage naturel, pour des tâches ''' '''telles que la traduction et la synthèse de texte.''', ] self.tokenizer_integration_test_util( expected_encoding=__SCREAMING_SNAKE_CASE , model_name='''moussaKam/mbarthez''' , revision='''c2e4ecbca5e3cd2c37fe1ac285ca4fbdf1366fb6''' , sequences=__SCREAMING_SNAKE_CASE , )
24
0
'''simple docstring''' from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder UpperCamelCase_ : int = datasets.utils.logging.get_logger(__name__) class lowerCamelCase__ ( folder_based_builder.FolderBasedBuilderConfig ): """simple docstring""" UpperCamelCase__ = None UpperCamelCase__ = None class lowerCamelCase__ ( folder_based_builder.FolderBasedBuilder ): """simple docstring""" UpperCamelCase__ = datasets.Audio() UpperCamelCase__ = '''audio''' UpperCamelCase__ = AudioFolderConfig UpperCamelCase__ = 42 # definition at the bottom of the script UpperCamelCase__ = AudioClassification(audio_column='''audio''' , label_column='''label''' ) UpperCamelCase_ : Dict = [ '''.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_ : Dict = AUDIO_EXTENSIONS
331
'''simple docstring''' import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class lowerCAmelCase ( unittest.TestCase): def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __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=32 , __SCREAMING_SNAKE_CASE=5 , __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=4 , ) -> Any: '''simple docstring''' __snake_case = parent __snake_case = batch_size __snake_case = seq_length __snake_case = is_training __snake_case = use_attention_mask __snake_case = use_token_type_ids __snake_case = use_labels __snake_case = vocab_size __snake_case = hidden_size __snake_case = num_hidden_layers __snake_case = num_attention_heads __snake_case = intermediate_size __snake_case = hidden_act __snake_case = hidden_dropout_prob __snake_case = attention_probs_dropout_prob __snake_case = max_position_embeddings __snake_case = type_vocab_size __snake_case = type_sequence_label_size __snake_case = initializer_range __snake_case = num_choices def lowerCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' __snake_case = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __snake_case = None if self.use_attention_mask: __snake_case = random_attention_mask([self.batch_size, self.seq_length] ) __snake_case = None if self.use_token_type_ids: __snake_case = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __snake_case = RoFormerConfig( 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=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = self.prepare_config_and_inputs() __snake_case , __snake_case , __snake_case , __snake_case = config_and_inputs __snake_case = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_flax class lowerCAmelCase ( __lowerCAmelCase , unittest.TestCase): __lowercase : Tuple = True __lowercase : Optional[int] = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def lowerCAmelCase ( self ) -> int: '''simple docstring''' __snake_case = FlaxRoFormerModelTester(self ) @slow def lowerCAmelCase ( self ) -> List[Any]: '''simple docstring''' for model_class_name in self.all_model_classes: __snake_case = model_class_name.from_pretrained('''junnyu/roformer_chinese_small''' , from_pt=__SCREAMING_SNAKE_CASE ) __snake_case = model(np.ones((1, 1) ) ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) @require_flax class lowerCAmelCase ( unittest.TestCase): @slow def lowerCAmelCase ( self ) -> Tuple: '''simple docstring''' __snake_case = FlaxRoFormerForMaskedLM.from_pretrained('''junnyu/roformer_chinese_base''' ) __snake_case = jnp.array([[0, 1, 2, 3, 4, 5]] ) __snake_case = model(__SCREAMING_SNAKE_CASE )[0] __snake_case = 5_0000 __snake_case = (1, 6, vocab_size) self.assertEqual(output.shape , __SCREAMING_SNAKE_CASE ) __snake_case = jnp.array( [[[-0.1_205, -1.0_265, 0.2_922], [-1.5_134, 0.1_974, 0.1_519], [-5.0_135, -3.9_003, -0.8_404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , __SCREAMING_SNAKE_CASE , atol=1E-4 ) )
24
0
'''simple docstring''' import os import re import shutil from argparse import ArgumentParser, Namespace from datasets.commands import BaseDatasetsCLICommand from datasets.utils.logging import get_logger UpperCamelCase__ = '''<<<<<<< This should probably be modified because it mentions: ''' UpperCamelCase__ = '''======= >>>>>>> ''' UpperCamelCase__ = [ '''TextEncoderConfig''', '''ByteTextEncoder''', '''SubwordTextEncoder''', '''encoder_config''', '''maybe_build_from_corpus''', '''manual_dir''', ] UpperCamelCase__ = [ # (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 a__ ( lowerCAmelCase__ ) -> int: return ConvertCommand(args.tfds_path , args.datasets_directory ) class lowerCamelCase_ ( __lowerCAmelCase ): @staticmethod def lowercase_ ( _A : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ : str = parser.add_parser( '''convert''' , help='''Convert a TensorFlow Datasets dataset to a HuggingFace Datasets dataset.''' , ) train_parser.add_argument( '''--tfds_path''' , type=__SCREAMING_SNAKE_CASE , required=__SCREAMING_SNAKE_CASE , help='''Path to a TensorFlow Datasets folder to convert or a single tfds file to convert.''' , ) train_parser.add_argument( '''--datasets_directory''' , type=__SCREAMING_SNAKE_CASE , required=__SCREAMING_SNAKE_CASE , help='''Path to the HuggingFace Datasets folder.''' ) train_parser.set_defaults(func=__SCREAMING_SNAKE_CASE ) def __init__( self : str , _A : List[str] , _A : Tuple , *_A : int ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = get_logger('''datasets-cli/converting''' ) UpperCAmelCase__ : Tuple = tfds_path UpperCAmelCase__ : str = datasets_directory def lowercase_ ( self : str ): '''simple docstring''' if os.path.isdir(self._tfds_path ): UpperCAmelCase__ : str = os.path.abspath(self._tfds_path ) elif os.path.isfile(self._tfds_path ): UpperCAmelCase__ : List[Any] = os.path.dirname(self._tfds_path ) else: raise ValueError('''--tfds_path is neither a directory nor a file. Please check path.''' ) UpperCAmelCase__ : Dict = os.path.abspath(self._datasets_directory ) self._logger.info(f"""Converting datasets from {abs_tfds_path} to {abs_datasets_path}""" ) UpperCAmelCase__ : Dict = [] UpperCAmelCase__ : Any = [] UpperCAmelCase__ : Union[str, Any] = {} if os.path.isdir(self._tfds_path ): UpperCAmelCase__ : List[Any] = os.listdir(__SCREAMING_SNAKE_CASE ) else: UpperCAmelCase__ : Dict = [os.path.basename(self._tfds_path )] for f_name in file_names: self._logger.info(f"""Looking at file {f_name}""" ) UpperCAmelCase__ : List[Any] = os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCAmelCase__ : Union[str, Any] = os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if not os.path.isfile(__SCREAMING_SNAKE_CASE ) or "__init__" in f_name or "_test" in f_name or ".py" not in f_name: self._logger.info('''Skipping file''' ) continue with open(__SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as f: UpperCAmelCase__ : Any = f.readlines() UpperCAmelCase__ : List[str] = [] UpperCAmelCase__ : str = False UpperCAmelCase__ : Optional[int] = False UpperCAmelCase__ : Dict = [] for line in lines: UpperCAmelCase__ : 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: UpperCAmelCase__ : Any = '''import datasets\n''' elif "import tensorflow" in out_line: # order is important here UpperCAmelCase__ : Optional[Any] = '''''' continue elif "from absl import logging" in out_line: UpperCAmelCase__ : int = '''from datasets import logging\n''' elif "getLogger" in out_line: UpperCAmelCase__ : int = out_line.replace('''getLogger''' , '''get_logger''' ) elif any(expression in out_line for expression in TO_HIGHLIGHT ): UpperCAmelCase__ : str = True UpperCAmelCase__ : List[str] = list(filter(lambda _A : e in out_line , __SCREAMING_SNAKE_CASE ) ) out_lines.append(HIGHLIGHT_MESSAGE_PRE + str(__SCREAMING_SNAKE_CASE ) + '''\n''' ) out_lines.append(__SCREAMING_SNAKE_CASE ) out_lines.append(__SCREAMING_SNAKE_CASE ) continue else: for pattern, replacement in TO_CONVERT: UpperCAmelCase__ : Optional[Any] = re.sub(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Take care of saving utilities (to later move them together with main script) if "tensorflow_datasets" in out_line: UpperCAmelCase__ : int = re.match(R'''from\stensorflow_datasets.*import\s([^\.\r\n]+)''' , __SCREAMING_SNAKE_CASE ) tfds_imports.extend(imp.strip() for imp in match.group(1 ).split(''',''' ) ) UpperCAmelCase__ : 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: UpperCAmelCase__ : List[Any] = True out_lines.append(__SCREAMING_SNAKE_CASE ) if is_builder or "wmt" in f_name: # We create a new directory for each dataset UpperCAmelCase__ : int = f_name.replace('''.py''' , '''''' ) UpperCAmelCase__ : Optional[Any] = os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCAmelCase__ : List[str] = os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) 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(__SCREAMING_SNAKE_CASE ) if needs_manual_update: with_manual_update.append(__SCREAMING_SNAKE_CASE ) with open(__SCREAMING_SNAKE_CASE , '''w''' , encoding='''utf-8''' ) as f: f.writelines(__SCREAMING_SNAKE_CASE ) self._logger.info(f"""Converted in {output_file}""" ) for utils_file in utils_files: try: UpperCAmelCase__ : Any = os.path.basename(__SCREAMING_SNAKE_CASE ) UpperCAmelCase__ : Optional[Any] = imports_to_builder_map[f_name.replace('''.py''' , '''''' )] self._logger.info(f"""Moving {dest_folder} to {utils_file}""" ) shutil.copy(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) 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\'.""" )
75
'''simple docstring''' 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 ()-> int: '''simple docstring''' __snake_case = '''https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png''' __snake_case = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ).convert('''RGB''' ) return image def _UpperCamelCase (_lowerCamelCase : Union[str, Any] )-> List[Any]: '''simple docstring''' __snake_case = [] # fmt: off # vision encoder rename_keys.append(('''visual_encoder.cls_token''', '''vision_model.embeddings.class_embedding''') ) rename_keys.append(('''visual_encoder.pos_embed''', '''vision_model.embeddings.position_embedding''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.weight''', '''vision_model.embeddings.patch_embedding.weight''') ) rename_keys.append(('''visual_encoder.patch_embed.proj.bias''', '''vision_model.embeddings.patch_embedding.bias''') ) rename_keys.append(('''ln_vision.weight''', '''vision_model.post_layernorm.weight''') ) rename_keys.append(('''ln_vision.bias''', '''vision_model.post_layernorm.bias''') ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.weight''', f'''vision_model.encoder.layers.{i}.layer_norm1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm1.bias''', f'''vision_model.encoder.layers.{i}.layer_norm1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.weight''', f'''vision_model.encoder.layers.{i}.layer_norm2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.norm2.bias''', f'''vision_model.encoder.layers.{i}.layer_norm2.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.qkv.weight''', f'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.weight''', f'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',) ) rename_keys.append((f'''visual_encoder.blocks.{i}.attn.proj.bias''', f'''vision_model.encoder.layers.{i}.self_attn.projection.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc1.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc1.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc1.bias''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.weight''', f'''vision_model.encoder.layers.{i}.mlp.fc2.weight''') ) rename_keys.append((f'''visual_encoder.blocks.{i}.mlp.fc2.bias''', f'''vision_model.encoder.layers.{i}.mlp.fc2.bias''') ) # QFormer rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.weight''', '''qformer.layernorm.weight''') ) rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.bias''', '''qformer.layernorm.bias''') ) # fmt: on return rename_keys def _UpperCamelCase (_lowerCamelCase : Dict , _lowerCamelCase : List[str] , _lowerCamelCase : Optional[int] )-> Tuple: '''simple docstring''' __snake_case = dct.pop(_lowerCamelCase ) __snake_case = val def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : Tuple )-> str: '''simple docstring''' for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases __snake_case = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.q_bias''' ) __snake_case = state_dict.pop(f'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict __snake_case = torch.cat((q_bias, torch.zeros_like(_lowerCamelCase , requires_grad=_lowerCamelCase ), v_bias) ) __snake_case = qkv_bias def _UpperCamelCase (_lowerCamelCase : Tuple , _lowerCamelCase : Tuple )-> Dict: '''simple docstring''' __snake_case = 3_64 if '''coco''' in model_name else 2_24 __snake_case = BlipaVisionConfig(image_size=_lowerCamelCase ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: __snake_case = OPTConfig.from_pretrained('''facebook/opt-2.7b''' , eos_token_id=_lowerCamelCase ).to_dict() elif "opt-6.7b" in model_name: __snake_case = OPTConfig.from_pretrained('''facebook/opt-6.7b''' , eos_token_id=_lowerCamelCase ).to_dict() elif "t5-xl" in model_name: __snake_case = TaConfig.from_pretrained('''google/flan-t5-xl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: __snake_case = TaConfig.from_pretrained('''google/flan-t5-xxl''' , dense_act_fn='''gelu''' , bos_token_id=1 ).to_dict() __snake_case = BlipaConfig(vision_config=_lowerCamelCase , text_config=_lowerCamelCase ) return config, image_size @torch.no_grad() def _UpperCamelCase (_lowerCamelCase : List[Any] , _lowerCamelCase : Union[str, Any]=None , _lowerCamelCase : Any=False )-> Dict: '''simple docstring''' __snake_case = ( AutoTokenizer.from_pretrained('''facebook/opt-2.7b''' ) if '''opt''' in model_name else AutoTokenizer.from_pretrained('''google/flan-t5-xl''' ) ) __snake_case = tokenizer('''\n''' , add_special_tokens=_lowerCamelCase ).input_ids[0] __snake_case , __snake_case = get_blipa_config(_lowerCamelCase , eos_token_id=_lowerCamelCase ) __snake_case = BlipaForConditionalGeneration(_lowerCamelCase ).eval() __snake_case = { '''blip2-opt-2.7b''': ('''blip2_opt''', '''pretrain_opt2.7b'''), '''blip2-opt-6.7b''': ('''blip2_opt''', '''pretrain_opt6.7b'''), '''blip2-opt-2.7b-coco''': ('''blip2_opt''', '''caption_coco_opt2.7b'''), '''blip2-opt-6.7b-coco''': ('''blip2_opt''', '''caption_coco_opt6.7b'''), '''blip2-flan-t5-xl''': ('''blip2_t5''', '''pretrain_flant5xl'''), '''blip2-flan-t5-xl-coco''': ('''blip2_t5''', '''caption_coco_flant5xl'''), '''blip2-flan-t5-xxl''': ('''blip2_t5''', '''pretrain_flant5xxl'''), } __snake_case , __snake_case = model_name_to_original[model_name] # load original model print('''Loading original model...''' ) __snake_case = '''cuda''' if torch.cuda.is_available() else '''cpu''' __snake_case , __snake_case , __snake_case = load_model_and_preprocess( name=_lowerCamelCase , model_type=_lowerCamelCase , is_eval=_lowerCamelCase , device=_lowerCamelCase ) original_model.eval() print('''Done!''' ) # update state dict keys __snake_case = original_model.state_dict() __snake_case = create_rename_keys(_lowerCamelCase ) for src, dest in rename_keys: rename_key(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): __snake_case = state_dict.pop(_lowerCamelCase ) if key.startswith('''Qformer.bert''' ): __snake_case = key.replace('''Qformer.bert''' , '''qformer''' ) if "attention.self" in key: __snake_case = key.replace('''self''' , '''attention''' ) if "opt_proj" in key: __snake_case = key.replace('''opt_proj''' , '''language_projection''' ) if "t5_proj" in key: __snake_case = key.replace('''t5_proj''' , '''language_projection''' ) if key.startswith('''opt''' ): __snake_case = key.replace('''opt''' , '''language''' ) if key.startswith('''t5''' ): __snake_case = key.replace('''t5''' , '''language''' ) __snake_case = val # read in qv biases read_in_q_v_bias(_lowerCamelCase , _lowerCamelCase ) __snake_case , __snake_case = hf_model.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) assert len(_lowerCamelCase ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] __snake_case = load_demo_image() __snake_case = vis_processors['''eval'''](_lowerCamelCase ).unsqueeze(0 ).to(_lowerCamelCase ) __snake_case = tokenizer(['''\n'''] , return_tensors='''pt''' ).input_ids.to(_lowerCamelCase ) # create processor __snake_case = BlipImageProcessor( size={'''height''': image_size, '''width''': image_size} , image_mean=_lowerCamelCase , image_std=_lowerCamelCase ) __snake_case = BlipaProcessor(image_processor=_lowerCamelCase , tokenizer=_lowerCamelCase ) __snake_case = processor(images=_lowerCamelCase , return_tensors='''pt''' ).pixel_values.to(_lowerCamelCase ) # make sure processor creates exact same pixel values assert torch.allclose(_lowerCamelCase , _lowerCamelCase ) original_model.to(_lowerCamelCase ) hf_model.to(_lowerCamelCase ) with torch.no_grad(): if "opt" in model_name: __snake_case = original_model({'''image''': original_pixel_values, '''text_input''': ['''''']} ).logits __snake_case = hf_model(_lowerCamelCase , _lowerCamelCase ).logits else: __snake_case = original_model( {'''image''': original_pixel_values, '''text_input''': ['''\n'''], '''text_output''': ['''\n''']} ).logits __snake_case = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -1_00 ) __snake_case = hf_model(_lowerCamelCase , _lowerCamelCase , labels=_lowerCamelCase ).logits assert original_logits.shape == logits.shape print('''First values of original logits:''' , original_logits[0, :3, :3] ) print('''First values of HF logits:''' , logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": __snake_case = torch.tensor( [[-41.5850, -4.4440, -8.9922], [-47.4322, -5.9143, -1.7340]] , device=_lowerCamelCase ) assert torch.allclose(logits[0, :3, :3] , _lowerCamelCase , atol=1E-4 ) elif model_name == "blip2-flan-t5-xl-coco": __snake_case = torch.tensor( [[-57.0109, -9.8967, -12.6280], [-68.6578, -12.7191, -10.5065]] , device=_lowerCamelCase ) else: # cast to same type __snake_case = logits.dtype assert torch.allclose(original_logits.to(_lowerCamelCase ) , _lowerCamelCase , atol=1E-2 ) print('''Looks ok!''' ) print('''Generating a caption...''' ) __snake_case = '''''' __snake_case = tokenizer(_lowerCamelCase , return_tensors='''pt''' ).input_ids.to(_lowerCamelCase ) __snake_case = original_model.generate({'''image''': original_pixel_values} ) __snake_case = hf_model.generate( _lowerCamelCase , _lowerCamelCase , do_sample=_lowerCamelCase , 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:''' , _lowerCamelCase ) __snake_case = input_ids.shape[1] __snake_case = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=_lowerCamelCase ) __snake_case = [text.strip() for text in output_text] print('''HF generation:''' , _lowerCamelCase ) if pytorch_dump_folder_path is not None: processor.save_pretrained(_lowerCamelCase ) hf_model.save_pretrained(_lowerCamelCase ) if push_to_hub: processor.push_to_hub(f'''nielsr/{model_name}''' ) hf_model.push_to_hub(f'''nielsr/{model_name}''' ) if __name__ == "__main__": UpperCAmelCase_ : Any = argparse.ArgumentParser() UpperCAmelCase_ : 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''', ) UpperCAmelCase_ : List[str] = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
24
0