code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
'''simple docstring''' import contextlib import csv import json import os import sqlitea import tarfile import textwrap import zipfile import pyarrow as pa import pyarrow.parquet as pq import pytest import datasets import datasets.config @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( ) -> str: __lowerCamelCase = 10 __lowerCamelCase = datasets.Features( { '''tokens''': datasets.Sequence(datasets.Value('''string''' ) ), '''labels''': datasets.Sequence(datasets.ClassLabel(names=['''negative''', '''positive'''] ) ), '''answers''': datasets.Sequence( { '''text''': datasets.Value('''string''' ), '''answer_start''': datasets.Value('''int32''' ), } ), '''id''': datasets.Value('''int64''' ), } ) __lowerCamelCase = datasets.Dataset.from_dict( { '''tokens''': [['''foo'''] * 5] * n, '''labels''': [[1] * 5] * n, '''answers''': [{'''answer_start''': [97], '''text''': ['''1976''']}] * 10, '''id''': list(range(UpperCamelCase__ ) ), } , features=UpperCamelCase__ , ) return dataset @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> List[Any]: __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''file.arrow''' ) dataset.map(cache_file_name=UpperCamelCase__ ) return filename # FILE_CONTENT + files __UpperCAmelCase ="\\n Text data.\n Second line of data." @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Union[str, Any]: __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''file.txt''' __lowerCamelCase = FILE_CONTENT with open(UpperCamelCase__ , '''w''' ) as f: f.write(UpperCamelCase__ ) return filename @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[Any]: import bza __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''file.txt.bz2''' __lowerCamelCase = bytes(UpperCamelCase__ , '''utf-8''' ) with bza.open(UpperCamelCase__ , '''wb''' ) as f: f.write(UpperCamelCase__ ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Dict: import gzip __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''file.txt.gz''' ) __lowerCamelCase = bytes(UpperCamelCase__ , '''utf-8''' ) with gzip.open(UpperCamelCase__ , '''wb''' ) as f: f.write(UpperCamelCase__ ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> str: if datasets.config.LZ4_AVAILABLE: import lza.frame __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''file.txt.lz4''' __lowerCamelCase = bytes(UpperCamelCase__ , '''utf-8''' ) with lza.frame.open(UpperCamelCase__ , '''wb''' ) as f: f.write(UpperCamelCase__ ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> int: if datasets.config.PY7ZR_AVAILABLE: import pyazr __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''file.txt.7z''' with pyazr.SevenZipFile(UpperCamelCase__ , '''w''' ) as archive: archive.write(UpperCamelCase__ , arcname=os.path.basename(UpperCamelCase__ ) ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> List[Any]: import tarfile __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''file.txt.tar''' with tarfile.TarFile(UpperCamelCase__ , '''w''' ) as f: f.add(UpperCamelCase__ , arcname=os.path.basename(UpperCamelCase__ ) ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> str: import lzma __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''file.txt.xz''' __lowerCamelCase = bytes(UpperCamelCase__ , '''utf-8''' ) with lzma.open(UpperCamelCase__ , '''wb''' ) as f: f.write(UpperCamelCase__ ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Optional[Any]: import zipfile __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''file.txt.zip''' with zipfile.ZipFile(UpperCamelCase__ , '''w''' ) as f: f.write(UpperCamelCase__ , arcname=os.path.basename(UpperCamelCase__ ) ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> List[Any]: if datasets.config.ZSTANDARD_AVAILABLE: import zstandard as zstd __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''file.txt.zst''' __lowerCamelCase = bytes(UpperCamelCase__ , '''utf-8''' ) with zstd.open(UpperCamelCase__ , '''wb''' ) as f: f.write(UpperCamelCase__ ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Tuple: __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''file.xml''' __lowerCamelCase = textwrap.dedent( '''\ <?xml version="1.0" encoding="UTF-8" ?> <tmx version="1.4"> <header segtype="sentence" srclang="ca" /> <body> <tu> <tuv xml:lang="ca"><seg>Contingut 1</seg></tuv> <tuv xml:lang="en"><seg>Content 1</seg></tuv> </tu> <tu> <tuv xml:lang="ca"><seg>Contingut 2</seg></tuv> <tuv xml:lang="en"><seg>Content 2</seg></tuv> </tu> <tu> <tuv xml:lang="ca"><seg>Contingut 3</seg></tuv> <tuv xml:lang="en"><seg>Content 3</seg></tuv> </tu> <tu> <tuv xml:lang="ca"><seg>Contingut 4</seg></tuv> <tuv xml:lang="en"><seg>Content 4</seg></tuv> </tu> <tu> <tuv xml:lang="ca"><seg>Contingut 5</seg></tuv> <tuv xml:lang="en"><seg>Content 5</seg></tuv> </tu> </body> </tmx>''' ) with open(UpperCamelCase__ , '''w''' ) as f: f.write(UpperCamelCase__ ) return filename __UpperCAmelCase =[ {"col_1": "0", "col_2": 0, "col_3": 0.0}, {"col_1": "1", "col_2": 1, "col_3": 1.0}, {"col_1": "2", "col_2": 2, "col_3": 2.0}, {"col_1": "3", "col_2": 3, "col_3": 3.0}, ] __UpperCAmelCase =[ {"col_1": "4", "col_2": 4, "col_3": 4.0}, {"col_1": "5", "col_2": 5, "col_3": 5.0}, ] __UpperCAmelCase ={ "col_1": ["0", "1", "2", "3"], "col_2": [0, 1, 2, 3], "col_3": [0.0, 1.0, 2.0, 3.0], } __UpperCAmelCase =[ {"col_3": 0.0, "col_1": "0", "col_2": 0}, {"col_3": 1.0, "col_1": "1", "col_2": 1}, ] __UpperCAmelCase =[ {"col_1": "s0", "col_2": 0, "col_3": 0.0}, {"col_1": "s1", "col_2": 1, "col_3": 1.0}, {"col_1": "s2", "col_2": 2, "col_3": 2.0}, {"col_1": "s3", "col_2": 3, "col_3": 3.0}, ] @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( ) -> Any: return DATA_DICT_OF_LISTS @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> List[str]: __lowerCamelCase = datasets.Dataset.from_dict(UpperCamelCase__ ) __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset.arrow''' ) dataset.map(cache_file_name=UpperCamelCase__ ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> int: __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset.sqlite''' ) with contextlib.closing(sqlitea.connect(UpperCamelCase__ ) ) as con: __lowerCamelCase = con.cursor() cur.execute('''CREATE TABLE dataset(col_1 text, col_2 int, col_3 real)''' ) for item in DATA: cur.execute('''INSERT INTO dataset(col_1, col_2, col_3) VALUES (?, ?, ?)''' , tuple(item.values() ) ) con.commit() return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Any: __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset.csv''' ) with open(UpperCamelCase__ , '''w''' , newline='''''' ) as f: __lowerCamelCase = csv.DictWriter(UpperCamelCase__ , fieldnames=['''col_1''', '''col_2''', '''col_3'''] ) writer.writeheader() for item in DATA: writer.writerow(UpperCamelCase__ ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Dict: __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset2.csv''' ) with open(UpperCamelCase__ , '''w''' , newline='''''' ) as f: __lowerCamelCase = csv.DictWriter(UpperCamelCase__ , fieldnames=['''col_1''', '''col_2''', '''col_3'''] ) writer.writeheader() for item in DATA: writer.writerow(UpperCamelCase__ ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> List[Any]: import bza __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''dataset.csv.bz2''' with open(UpperCamelCase__ , '''rb''' ) as f: __lowerCamelCase = f.read() # data = bytes(FILE_CONTENT, "utf-8") with bza.open(UpperCamelCase__ , '''wb''' ) as f: f.write(UpperCamelCase__ ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> int: __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''dataset.csv.zip''' with zipfile.ZipFile(UpperCamelCase__ , '''w''' ) as f: f.write(UpperCamelCase__ , arcname=os.path.basename(UpperCamelCase__ ) ) f.write(UpperCamelCase__ , arcname=os.path.basename(UpperCamelCase__ ) ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> List[str]: __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''dataset.csv.zip''' with zipfile.ZipFile(UpperCamelCase__ , '''w''' ) as f: f.write(UpperCamelCase__ , arcname=os.path.basename(csv_path.replace('''.csv''' , '''.CSV''' ) ) ) f.write(UpperCamelCase__ , arcname=os.path.basename(csva_path.replace('''.csv''' , '''.CSV''' ) ) ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Any: __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''dataset_with_dir.csv.zip''' with zipfile.ZipFile(UpperCamelCase__ , '''w''' ) as f: f.write(UpperCamelCase__ , arcname=os.path.join('''main_dir''' , os.path.basename(UpperCamelCase__ ) ) ) f.write(UpperCamelCase__ , arcname=os.path.join('''main_dir''' , os.path.basename(UpperCamelCase__ ) ) ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Union[str, Any]: __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset.parquet''' ) __lowerCamelCase = pa.schema( { '''col_1''': pa.string(), '''col_2''': pa.intaa(), '''col_3''': pa.floataa(), } ) with open(UpperCamelCase__ , '''wb''' ) as f: __lowerCamelCase = pq.ParquetWriter(UpperCamelCase__ , schema=UpperCamelCase__ ) __lowerCamelCase = pa.Table.from_pydict({k: [DATA[i][k] for i in range(len(UpperCamelCase__ ) )] for k in DATA[0]} , schema=UpperCamelCase__ ) writer.write_table(UpperCamelCase__ ) writer.close() return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Union[str, Any]: __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset.json''' ) __lowerCamelCase = {'''data''': DATA} with open(UpperCamelCase__ , '''w''' ) as f: json.dump(UpperCamelCase__ , UpperCamelCase__ ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Tuple: __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset.json''' ) __lowerCamelCase = {'''data''': DATA_DICT_OF_LISTS} with open(UpperCamelCase__ , '''w''' ) as f: json.dump(UpperCamelCase__ , UpperCamelCase__ ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Dict: __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset.jsonl''' ) with open(UpperCamelCase__ , '''w''' ) as f: for item in DATA: f.write(json.dumps(UpperCamelCase__ ) + '''\n''' ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> str: __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset2.jsonl''' ) with open(UpperCamelCase__ , '''w''' ) as f: for item in DATA: f.write(json.dumps(UpperCamelCase__ ) + '''\n''' ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Dict: __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset_312.jsonl''' ) with open(UpperCamelCase__ , '''w''' ) as f: for item in DATA_312: f.write(json.dumps(UpperCamelCase__ ) + '''\n''' ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[int]: __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset-str.jsonl''' ) with open(UpperCamelCase__ , '''w''' ) as f: for item in DATA_STR: f.write(json.dumps(UpperCamelCase__ ) + '''\n''' ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> str: import gzip __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset.txt.gz''' ) with open(UpperCamelCase__ , '''rb''' ) as orig_file: with gzip.open(UpperCamelCase__ , '''wb''' ) as zipped_file: zipped_file.writelines(UpperCamelCase__ ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> int: import gzip __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset.jsonl.gz''' ) with open(UpperCamelCase__ , '''rb''' ) as orig_file: with gzip.open(UpperCamelCase__ , '''wb''' ) as zipped_file: zipped_file.writelines(UpperCamelCase__ ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> List[str]: __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''dataset.jsonl.zip''' with zipfile.ZipFile(UpperCamelCase__ , '''w''' ) as f: f.write(UpperCamelCase__ , arcname=os.path.basename(UpperCamelCase__ ) ) f.write(UpperCamelCase__ , arcname=os.path.basename(UpperCamelCase__ ) ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> List[Any]: __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''dataset_nested.jsonl.zip''' with zipfile.ZipFile(UpperCamelCase__ , '''w''' ) as f: f.write(UpperCamelCase__ , arcname=os.path.join('''nested''' , os.path.basename(UpperCamelCase__ ) ) ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> str: __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''dataset_with_dir.jsonl.zip''' with zipfile.ZipFile(UpperCamelCase__ , '''w''' ) as f: f.write(UpperCamelCase__ , arcname=os.path.join('''main_dir''' , os.path.basename(UpperCamelCase__ ) ) ) f.write(UpperCamelCase__ , arcname=os.path.join('''main_dir''' , os.path.basename(UpperCamelCase__ ) ) ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Any: __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''dataset.jsonl.tar''' with tarfile.TarFile(UpperCamelCase__ , '''w''' ) as f: f.add(UpperCamelCase__ , arcname=os.path.basename(UpperCamelCase__ ) ) f.add(UpperCamelCase__ , arcname=os.path.basename(UpperCamelCase__ ) ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Optional[Any]: __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''dataset_nested.jsonl.tar''' with tarfile.TarFile(UpperCamelCase__ , '''w''' ) as f: f.add(UpperCamelCase__ , arcname=os.path.join('''nested''' , os.path.basename(UpperCamelCase__ ) ) ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Any: __lowerCamelCase = ['''0''', '''1''', '''2''', '''3'''] __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset.txt''' ) with open(UpperCamelCase__ , '''w''' ) as f: for item in data: f.write(item + '''\n''' ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[Any]: __lowerCamelCase = ['''0''', '''1''', '''2''', '''3'''] __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset2.txt''' ) with open(UpperCamelCase__ , '''w''' ) as f: for item in data: f.write(item + '''\n''' ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> List[Any]: __lowerCamelCase = ['''0''', '''1''', '''2''', '''3'''] __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''dataset.abc''' with open(UpperCamelCase__ , '''w''' ) as f: for item in data: f.write(item + '''\n''' ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Union[str, Any]: __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''dataset.text.zip''' with zipfile.ZipFile(UpperCamelCase__ , '''w''' ) as f: f.write(UpperCamelCase__ , arcname=os.path.basename(UpperCamelCase__ ) ) f.write(UpperCamelCase__ , arcname=os.path.basename(UpperCamelCase__ ) ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> str: __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''dataset_with_dir.text.zip''' with zipfile.ZipFile(UpperCamelCase__ , '''w''' ) as f: f.write(UpperCamelCase__ , arcname=os.path.join('''main_dir''' , os.path.basename(UpperCamelCase__ ) ) ) f.write(UpperCamelCase__ , arcname=os.path.join('''main_dir''' , os.path.basename(UpperCamelCase__ ) ) ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Union[str, Any]: __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''dataset.ext.zip''' with zipfile.ZipFile(UpperCamelCase__ , '''w''' ) as f: f.write(UpperCamelCase__ , arcname=os.path.basename('''unsupported.ext''' ) ) f.write(UpperCamelCase__ , arcname=os.path.basename('''unsupported_2.ext''' ) ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> List[str]: __lowerCamelCase = '''\n'''.join(['''First''', '''Second\u2029with Unicode new line''', '''Third'''] ) __lowerCamelCase = str(tmp_path_factory.mktemp('''data''' ) / '''dataset_with_unicode_new_lines.txt''' ) with open(UpperCamelCase__ , '''w''' , encoding='''utf-8''' ) as f: f.write(UpperCamelCase__ ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( ) -> Optional[Any]: return os.path.join('''tests''' , '''features''' , '''data''' , '''test_image_rgb.jpg''' ) @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( ) -> Optional[Any]: return os.path.join('''tests''' , '''features''' , '''data''' , '''test_audio_44100.wav''' ) @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Optional[Any]: __lowerCamelCase = tmp_path_factory.mktemp('''data''' ) / '''dataset.img.zip''' with zipfile.ZipFile(UpperCamelCase__ , '''w''' ) as f: f.write(UpperCamelCase__ , arcname=os.path.basename(UpperCamelCase__ ) ) f.write(UpperCamelCase__ , arcname=os.path.basename(UpperCamelCase__ ).replace('''.jpg''' , '''2.jpg''' ) ) return path @pytest.fixture(scope='''session''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> int: __lowerCamelCase = tmp_path_factory.mktemp('''data_dir''' ) (data_dir / "subdir").mkdir() with open(data_dir / '''subdir''' / '''train.txt''' , '''w''' ) as f: f.write('''foo\n''' * 10 ) with open(data_dir / '''subdir''' / '''test.txt''' , '''w''' ) as f: f.write('''bar\n''' * 10 ) # hidden file with open(data_dir / '''subdir''' / '''.test.txt''' , '''w''' ) as f: f.write('''bar\n''' * 10 ) # hidden directory (data_dir / ".subdir").mkdir() with open(data_dir / '''.subdir''' / '''train.txt''' , '''w''' ) as f: f.write('''foo\n''' * 10 ) with open(data_dir / '''.subdir''' / '''test.txt''' , '''w''' ) as f: f.write('''bar\n''' * 10 ) return data_dir
67
'''simple docstring''' from typing import List, Union import numpy as np from ..utils import add_end_docstrings, 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(): import torch from ..models.auto.modeling_auto import MODEL_FOR_DEPTH_ESTIMATION_MAPPING __UpperCAmelCase =logging.get_logger(__name__) @add_end_docstrings(UpperCAmelCase__ ) class a__ ( UpperCAmelCase__ ): def __init__( self : List[str] , *a : Union[str, Any] , **a : Optional[Any] ): """simple docstring""" super().__init__(*a , **a ) requires_backends(self , '''vision''' ) self.check_model_type(a ) def __call__( self : Any , a : Union[str, List[str], "Image.Image", List["Image.Image"]] , **a : Optional[int] ): """simple docstring""" return super().__call__(a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , **a : Any ): """simple docstring""" return {}, {}, {} def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : List[str] ): """simple docstring""" __lowerCamelCase = load_image(a ) __lowerCamelCase = image.size __lowerCamelCase = self.image_processor(images=a , return_tensors=self.framework ) return model_inputs def SCREAMING_SNAKE_CASE__ ( self : int , a : Optional[Any] ): """simple docstring""" __lowerCamelCase = self.model(**a ) return model_outputs def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : Any ): """simple docstring""" __lowerCamelCase = model_outputs.predicted_depth __lowerCamelCase = torch.nn.functional.interpolate( predicted_depth.unsqueeze(1 ) , size=self.image_size[::-1] , mode='''bicubic''' , align_corners=a ) __lowerCamelCase = prediction.squeeze().cpu().numpy() __lowerCamelCase = (output * 2_55 / np.max(a )).astype('''uint8''' ) __lowerCamelCase = Image.fromarray(a ) __lowerCamelCase = {} __lowerCamelCase = predicted_depth __lowerCamelCase = depth return output_dict
67
1
'''simple docstring''' import json import os from datetime import date from pathlib import Path from tabulate import DataRow, TableFormat, tabulate __UpperCAmelCase =TableFormat( lineabove=None, linebelowheader=None, linebetweenrows=None, linebelow=None, headerrow=DataRow("", "|", "|"), datarow=DataRow("", "|", "|"), padding=1, with_header_hide=None, ) __UpperCAmelCase =[] __UpperCAmelCase =[] __UpperCAmelCase ={"type": "section", "text": {"type": "plain_text", "text": "No failed tests! 🤗", "emoji": True}} __UpperCAmelCase =[ { "type": "header", "text": { "type": "plain_text", "text": f'🤗 Accelerate nightly {os.environ.get("TEST_TYPE", "")} test results', "emoji": True, }, } ] __UpperCAmelCase =0 for log in Path().glob("*.log"): __UpperCAmelCase =0 with open(log, "r") as f: for line in f: __UpperCAmelCase =json.loads(line) if line.get("nodeid", "") != "": __UpperCAmelCase =line["nodeid"] if line.get("duration", None) is not None: __UpperCAmelCase =f'{line["duration"]:.4f}' if line.get("outcome", "") == "failed": section_num_failed += 1 failed.append([test, duration, log.name.split("_")[0]]) total_num_failed += 1 group_info.append([str(log), section_num_failed, failed]) __UpperCAmelCase =[] log.unlink() __UpperCAmelCase ="" __UpperCAmelCase =[] if total_num_failed > 0: for name, num_failed, failed_tests in group_info: if num_failed > 0: if num_failed == 1: message += f"*{name[1:]}: {num_failed} failed test*\n" else: message += f"*{name[1:]}: {num_failed} failed tests*\n" __UpperCAmelCase =[] __UpperCAmelCase ={} for test in failed_tests: __UpperCAmelCase =test[0].split("::") __UpperCAmelCase =data[0].split("/")[-1] if data[0] not in filesafailed: __UpperCAmelCase =[data[1:]] else: filesafailed[data[0]] += [data[1:]] failed_table.append(data) __UpperCAmelCase =[test[0] for test in failed_table] __UpperCAmelCase =list(set(files)) # Count number of instances in failed_tests __UpperCAmelCase =[] for file in individual_files: table.append([file, len(filesafailed[file])]) __UpperCAmelCase =tabulate( table, headers=["Test Location", "Num Failed"], tablefmt=hf_table_format, stralign="right", ) message += f"\n```\n{failed_table}\n```" all_filesafailed.append(filesafailed) if len(message) > 3_0_0_0: __UpperCAmelCase ="Too many failed tests, please see the full report in the Action results." __UpperCAmelCase =len(err) + 1_0 __UpperCAmelCase =message[: 3_0_0_0 - offset] + f'\n...\n```\n{err}' print(f'### {message}') else: __UpperCAmelCase ="No failed tests! 🤗" print(f'## {message}') payload.append(no_error_payload) if os.environ.get("TEST_TYPE", "") != "": from slack_sdk import WebClient __UpperCAmelCase =WebClient(token=os.environ["SLACK_API_TOKEN"]) if message != "No failed tests! 🤗": __UpperCAmelCase ={ "type": "section", "text": { "type": "mrkdwn", "text": message, }, } payload.append(md_report) __UpperCAmelCase ={ "type": "section", "text": { "type": "mrkdwn", "text": "*For more details:*", }, "accessory": { "type": "button", "text": { "type": "plain_text", "text": "Check Action results", "emoji": True, }, "url": f'https://github.com/{os.environ["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } payload.append(action_button) __UpperCAmelCase ={ "type": "context", "elements": [ { "type": "plain_text", "text": f'Nightly {os.environ.get("TEST_TYPE")} test results for {date.today()}', } ], } payload.append(date_report) __UpperCAmelCase =client.chat_postMessage(channel="#accelerate-ci-daily", text=message, blocks=payload) __UpperCAmelCase =response.data["ts"] for failed_file in all_filesafailed: for test_location, test_failures in failed_file.items(): # Keep only the first instance of the test name __UpperCAmelCase ="" for i, row in enumerate(test_failures): if row[0] != test_class: __UpperCAmelCase =row[0] else: __UpperCAmelCase ="" __UpperCAmelCase ={ "type": "section", "text": { "type": "mrkdwn", "text": f'Test location: {test_location}\n```\n{tabulate(test_failures, headers=["Class", "Test"], tablefmt=hf_table_format, stralign="right")}\n```', }, } client.chat_postMessage( channel="#accelerate-ci-daily", thread_ts=ts, blocks=[payload], )
67
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase ={ "configuration_clap": [ "CLAP_PRETRAINED_MODEL_ARCHIVE_LIST", "ClapAudioConfig", "ClapConfig", "ClapTextConfig", ], "processing_clap": ["ClapProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "CLAP_PRETRAINED_MODEL_ARCHIVE_LIST", "ClapModel", "ClapPreTrainedModel", "ClapTextModel", "ClapTextModelWithProjection", "ClapAudioModel", "ClapAudioModelWithProjection", ] __UpperCAmelCase =["ClapFeatureExtractor"] if TYPE_CHECKING: from .configuration_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioConfig, ClapConfig, ClapTextConfig, ) from .processing_clap import ClapProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clap import ClapFeatureExtractor from .modeling_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioModel, ClapAudioModelWithProjection, ClapModel, ClapPreTrainedModel, ClapTextModel, ClapTextModelWithProjection, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
67
1
'''simple docstring''' from typing import Any, Callable, Dict, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker __UpperCAmelCase ="CompVis/stable-diffusion-v1-1" __UpperCAmelCase ="CompVis/stable-diffusion-v1-2" __UpperCAmelCase ="CompVis/stable-diffusion-v1-3" __UpperCAmelCase ="CompVis/stable-diffusion-v1-4" class a__ ( UpperCAmelCase__ ): def __init__( self : Optional[Any] , a : AutoencoderKL , a : CLIPTextModel , a : CLIPTokenizer , a : UNetaDConditionModel , a : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , a : StableDiffusionSafetyChecker , a : CLIPImageProcessor , a : bool = True , ): """simple docstring""" super()._init_() __lowerCamelCase = StableDiffusionPipeline.from_pretrained(a ) __lowerCamelCase = StableDiffusionPipeline.from_pretrained(a ) __lowerCamelCase = StableDiffusionPipeline.from_pretrained(a ) __lowerCamelCase = StableDiffusionPipeline( vae=a , text_encoder=a , tokenizer=a , unet=a , scheduler=a , safety_checker=a , feature_extractor=a , requires_safety_checker=a , ) self.register_modules(pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea ) @property def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" return {k: getattr(self , a ) for k in self.config.keys() if not k.startswith('''_''' )} def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : Optional[Union[str, int]] = "auto" ): """simple docstring""" if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory __lowerCamelCase = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(a ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" self.enable_attention_slicing(a ) @torch.no_grad() def SCREAMING_SNAKE_CASE__ ( self : Any , a : Union[str, List[str]] , a : int = 5_12 , a : int = 5_12 , a : int = 50 , a : float = 7.5 , a : Optional[Union[str, List[str]]] = None , a : Optional[int] = 1 , a : float = 0.0 , a : Optional[torch.Generator] = None , a : Optional[torch.FloatTensor] = None , a : Optional[str] = "pil" , a : bool = True , a : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , a : int = 1 , **a : Union[str, Any] , ): """simple docstring""" return self.pipea( prompt=a , height=a , width=a , num_inference_steps=a , guidance_scale=a , negative_prompt=a , num_images_per_prompt=a , eta=a , generator=a , latents=a , output_type=a , return_dict=a , callback=a , callback_steps=a , **a , ) @torch.no_grad() def SCREAMING_SNAKE_CASE__ ( self : List[str] , a : Union[str, List[str]] , a : int = 5_12 , a : int = 5_12 , a : int = 50 , a : float = 7.5 , a : Optional[Union[str, List[str]]] = None , a : Optional[int] = 1 , a : float = 0.0 , a : Optional[torch.Generator] = None , a : Optional[torch.FloatTensor] = None , a : Optional[str] = "pil" , a : bool = True , a : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , a : int = 1 , **a : Tuple , ): """simple docstring""" return self.pipea( prompt=a , height=a , width=a , num_inference_steps=a , guidance_scale=a , negative_prompt=a , num_images_per_prompt=a , eta=a , generator=a , latents=a , output_type=a , return_dict=a , callback=a , callback_steps=a , **a , ) @torch.no_grad() def SCREAMING_SNAKE_CASE__ ( self : List[str] , a : Union[str, List[str]] , a : int = 5_12 , a : int = 5_12 , a : int = 50 , a : float = 7.5 , a : Optional[Union[str, List[str]]] = None , a : Optional[int] = 1 , a : float = 0.0 , a : Optional[torch.Generator] = None , a : Optional[torch.FloatTensor] = None , a : Optional[str] = "pil" , a : bool = True , a : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , a : int = 1 , **a : str , ): """simple docstring""" return self.pipea( prompt=a , height=a , width=a , num_inference_steps=a , guidance_scale=a , negative_prompt=a , num_images_per_prompt=a , eta=a , generator=a , latents=a , output_type=a , return_dict=a , callback=a , callback_steps=a , **a , ) @torch.no_grad() def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : Union[str, List[str]] , a : int = 5_12 , a : int = 5_12 , a : int = 50 , a : float = 7.5 , a : Optional[Union[str, List[str]]] = None , a : Optional[int] = 1 , a : float = 0.0 , a : Optional[torch.Generator] = None , a : Optional[torch.FloatTensor] = None , a : Optional[str] = "pil" , a : bool = True , a : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , a : int = 1 , **a : Tuple , ): """simple docstring""" return self.pipea( prompt=a , height=a , width=a , num_inference_steps=a , guidance_scale=a , negative_prompt=a , num_images_per_prompt=a , eta=a , generator=a , latents=a , output_type=a , return_dict=a , callback=a , callback_steps=a , **a , ) @torch.no_grad() def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Union[str, List[str]] , a : int = 5_12 , a : int = 5_12 , a : int = 50 , a : float = 7.5 , a : Optional[Union[str, List[str]]] = None , a : Optional[int] = 1 , a : float = 0.0 , a : Optional[torch.Generator] = None , a : Optional[torch.FloatTensor] = None , a : Optional[str] = "pil" , a : bool = True , a : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , a : int = 1 , **a : str , ): """simple docstring""" __lowerCamelCase = '''cuda''' if torch.cuda.is_available() else '''cpu''' self.to(a ) # Checks if the height and width are divisible by 8 or not if height % 8 != 0 or width % 8 != 0: raise ValueError(f"""`height` and `width` must be divisible by 8 but are {height} and {width}.""" ) # Get first result from Stable Diffusion Checkpoint v1.1 __lowerCamelCase = self.textaimg_sda_a( prompt=a , height=a , width=a , num_inference_steps=a , guidance_scale=a , negative_prompt=a , num_images_per_prompt=a , eta=a , generator=a , latents=a , output_type=a , return_dict=a , callback=a , callback_steps=a , **a , ) # Get first result from Stable Diffusion Checkpoint v1.2 __lowerCamelCase = self.textaimg_sda_a( prompt=a , height=a , width=a , num_inference_steps=a , guidance_scale=a , negative_prompt=a , num_images_per_prompt=a , eta=a , generator=a , latents=a , output_type=a , return_dict=a , callback=a , callback_steps=a , **a , ) # Get first result from Stable Diffusion Checkpoint v1.3 __lowerCamelCase = self.textaimg_sda_a( prompt=a , height=a , width=a , num_inference_steps=a , guidance_scale=a , negative_prompt=a , num_images_per_prompt=a , eta=a , generator=a , latents=a , output_type=a , return_dict=a , callback=a , callback_steps=a , **a , ) # Get first result from Stable Diffusion Checkpoint v1.4 __lowerCamelCase = self.textaimg_sda_a( prompt=a , height=a , width=a , num_inference_steps=a , guidance_scale=a , negative_prompt=a , num_images_per_prompt=a , eta=a , generator=a , latents=a , output_type=a , return_dict=a , callback=a , callback_steps=a , **a , ) # Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]] )
67
'''simple docstring''' import os import shutil import tempfile import unittest import numpy as np from transformers import AutoTokenizer, BarkProcessor from transformers.testing_utils import require_torch, slow @require_torch class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = '''ylacombe/bark-small''' __lowerCamelCase = tempfile.mkdtemp() __lowerCamelCase = '''en_speaker_1''' __lowerCamelCase = '''This is a test string''' __lowerCamelCase = '''speaker_embeddings_path.json''' __lowerCamelCase = '''speaker_embeddings''' def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , **a : Dict ): """simple docstring""" return AutoTokenizer.from_pretrained(self.checkpoint , **a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) processor.save_pretrained(self.tmpdirname ) __lowerCamelCase = BarkProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) @slow def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) processor.save_pretrained( self.tmpdirname , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , speaker_embeddings_directory=self.speaker_embeddings_directory , ) __lowerCamelCase = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) __lowerCamelCase = BarkProcessor.from_pretrained( self.tmpdirname , self.speaker_embeddings_dict_path , bos_token='''(BOS)''' , eos_token='''(EOS)''' , ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) __lowerCamelCase = 35 __lowerCamelCase = 2 __lowerCamelCase = 8 __lowerCamelCase = { '''semantic_prompt''': np.ones(a ), '''coarse_prompt''': np.ones((nb_codebooks_coarse, seq_len) ), '''fine_prompt''': np.ones((nb_codebooks_total, seq_len) ), } # test providing already loaded voice_preset __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from npz file __lowerCamelCase = os.path.join(self.tmpdirname , '''file.npz''' ) np.savez(a , **a ) __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from the hub __lowerCamelCase = processor(text=self.input_string , voice_preset=self.voice_preset ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) __lowerCamelCase = processor(text=self.input_string ) __lowerCamelCase = tokenizer( self.input_string , padding='''max_length''' , max_length=2_56 , add_special_tokens=a , return_attention_mask=a , return_token_type_ids=a , ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key].squeeze().tolist() )
67
1
'''simple docstring''' import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class a__ ( UpperCAmelCase__ ): lowerCamelCase : Any =(KDPMaDiscreteScheduler,) lowerCamelCase : Any =1_0 def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , **a : List[Any] ): """simple docstring""" __lowerCamelCase = { '''num_train_timesteps''': 11_00, '''beta_start''': 0.00_01, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', } config.update(**a ) return config def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" for timesteps in [10, 50, 1_00, 10_00]: self.check_over_configs(num_train_timesteps=a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" for beta_start, beta_end in zip([0.0_00_01, 0.00_01, 0.0_01] , [0.00_02, 0.0_02, 0.02] ): self.check_over_configs(beta_start=a , beta_end=a ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=a ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = self.scheduler_classes[0] __lowerCamelCase = self.get_scheduler_config(prediction_type='''v_prediction''' ) __lowerCamelCase = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps ) __lowerCamelCase = self.dummy_model() __lowerCamelCase = self.dummy_sample_deter * scheduler.init_noise_sigma __lowerCamelCase = sample.to(a ) for i, t in enumerate(scheduler.timesteps ): __lowerCamelCase = scheduler.scale_model_input(a , a ) __lowerCamelCase = model(a , a ) __lowerCamelCase = scheduler.step(a , a , a ) __lowerCamelCase = output.prev_sample __lowerCamelCase = torch.sum(torch.abs(a ) ) __lowerCamelCase = torch.mean(torch.abs(a ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6_9_3_4e-0_7 ) < 1e-2 assert abs(result_mean.item() - 6.1_1_1_2e-1_0 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.6_9_3_4_2_8_6_5_0_1_7_0_9_7_2e-0_7 ) < 1e-2 assert abs(result_mean.item() - 0.00_02 ) < 1e-3 def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" if torch_device == "mps": return __lowerCamelCase = self.scheduler_classes[0] __lowerCamelCase = self.get_scheduler_config() __lowerCamelCase = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps ) __lowerCamelCase = self.dummy_model() __lowerCamelCase = self.dummy_sample_deter * scheduler.init_noise_sigma __lowerCamelCase = sample.to(a ) for i, t in enumerate(scheduler.timesteps ): __lowerCamelCase = scheduler.scale_model_input(a , a ) __lowerCamelCase = model(a , a ) __lowerCamelCase = scheduler.step(a , a , a ) __lowerCamelCase = output.prev_sample __lowerCamelCase = torch.sum(torch.abs(a ) ) __lowerCamelCase = torch.mean(torch.abs(a ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.41_25 ) < 1e-2 assert abs(result_mean.item() - 0.02_66 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.41_25 ) < 1e-2 assert abs(result_mean.item() - 0.02_66 ) < 1e-3 def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" if torch_device == "mps": return __lowerCamelCase = self.scheduler_classes[0] __lowerCamelCase = self.get_scheduler_config() __lowerCamelCase = scheduler_class(**a ) scheduler.set_timesteps(self.num_inference_steps , device=a ) __lowerCamelCase = self.dummy_model() __lowerCamelCase = self.dummy_sample_deter.to(a ) * scheduler.init_noise_sigma for t in scheduler.timesteps: __lowerCamelCase = scheduler.scale_model_input(a , a ) __lowerCamelCase = model(a , a ) __lowerCamelCase = scheduler.step(a , a , a ) __lowerCamelCase = output.prev_sample __lowerCamelCase = torch.sum(torch.abs(a ) ) __lowerCamelCase = torch.mean(torch.abs(a ) ) if str(a ).startswith('''cpu''' ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.41_25 ) < 1e-2 assert abs(result_mean.item() - 0.02_66 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.41_25 ) < 1e-2 assert abs(result_mean.item() - 0.02_66 ) < 1e-3
67
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase ={"configuration_vit_msn": ["VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP", "ViTMSNConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST", "ViTMSNModel", "ViTMSNForImageClassification", "ViTMSNPreTrainedModel", ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
67
1
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import json import os from ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES from ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType from ...utils.imports import is_botoa_available from .config_args import SageMakerConfig from .config_utils import ( DYNAMO_BACKENDS, _ask_field, _ask_options, _convert_dynamo_backend, _convert_mixed_precision, _convert_sagemaker_distributed_mode, _convert_yes_no_to_bool, ) if is_botoa_available(): import botoa # noqa: F401 def __lowerCAmelCase ( UpperCamelCase__ ) -> Union[str, Any]: __lowerCamelCase = botoa.client('''iam''' ) __lowerCamelCase = { '''Version''': '''2012-10-17''', '''Statement''': [ {'''Effect''': '''Allow''', '''Principal''': {'''Service''': '''sagemaker.amazonaws.com'''}, '''Action''': '''sts:AssumeRole'''} ], } try: # create the role, associated with the chosen trust policy iam_client.create_role( RoleName=UpperCamelCase__ , AssumeRolePolicyDocument=json.dumps(UpperCamelCase__ , indent=2 ) ) __lowerCamelCase = { '''Version''': '''2012-10-17''', '''Statement''': [ { '''Effect''': '''Allow''', '''Action''': [ '''sagemaker:*''', '''ecr:GetDownloadUrlForLayer''', '''ecr:BatchGetImage''', '''ecr:BatchCheckLayerAvailability''', '''ecr:GetAuthorizationToken''', '''cloudwatch:PutMetricData''', '''cloudwatch:GetMetricData''', '''cloudwatch:GetMetricStatistics''', '''cloudwatch:ListMetrics''', '''logs:CreateLogGroup''', '''logs:CreateLogStream''', '''logs:DescribeLogStreams''', '''logs:PutLogEvents''', '''logs:GetLogEvents''', '''s3:CreateBucket''', '''s3:ListBucket''', '''s3:GetBucketLocation''', '''s3:GetObject''', '''s3:PutObject''', ], '''Resource''': '''*''', } ], } # attach policy to role iam_client.put_role_policy( RoleName=UpperCamelCase__ , PolicyName=f"""{role_name}_policy_permission""" , PolicyDocument=json.dumps(UpperCamelCase__ , indent=2 ) , ) except iam_client.exceptions.EntityAlreadyExistsException: print(f"""role {role_name} already exists. Using existing one""" ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Any: __lowerCamelCase = botoa.client('''iam''' ) return iam_client.get_role(RoleName=UpperCamelCase__ )["Role"]["Arn"] def __lowerCAmelCase ( ) -> str: __lowerCamelCase = _ask_options( '''How do you want to authorize?''' , ['''AWS Profile''', '''Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) '''] , UpperCamelCase__ , ) __lowerCamelCase = None if credentials_configuration == 0: __lowerCamelCase = _ask_field('''Enter your AWS Profile name: [default] ''' , default='''default''' ) __lowerCamelCase = aws_profile else: print( '''Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,''' '''`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`''' ) __lowerCamelCase = _ask_field('''AWS Access Key ID: ''' ) __lowerCamelCase = aws_access_key_id __lowerCamelCase = _ask_field('''AWS Secret Access Key: ''' ) __lowerCamelCase = aws_secret_access_key __lowerCamelCase = _ask_field('''Enter your AWS Region: [us-east-1]''' , default='''us-east-1''' ) __lowerCamelCase = aws_region __lowerCamelCase = _ask_options( '''Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?''' , ['''Provide IAM Role name''', '''Create new IAM role using credentials'''] , UpperCamelCase__ , ) if role_management == 0: __lowerCamelCase = _ask_field('''Enter your IAM role name: ''' ) else: __lowerCamelCase = '''accelerate_sagemaker_execution_role''' print(f"""Accelerate will create an iam role \"{iam_role_name}\" using the provided credentials""" ) _create_iam_role_for_sagemaker(UpperCamelCase__ ) __lowerCamelCase = _ask_field( '''Do you want to use custom Docker image? [yes/NO]: ''' , _convert_yes_no_to_bool , default=UpperCamelCase__ , error_message='''Please enter yes or no.''' , ) __lowerCamelCase = None if is_custom_docker_image: __lowerCamelCase = _ask_field('''Enter your Docker image: ''' , lambda UpperCamelCase__ : str(UpperCamelCase__ ).lower() ) __lowerCamelCase = _ask_field( '''Do you want to provide SageMaker input channels with data locations? [yes/NO]: ''' , _convert_yes_no_to_bool , default=UpperCamelCase__ , error_message='''Please enter yes or no.''' , ) __lowerCamelCase = None if is_sagemaker_inputs_enabled: __lowerCamelCase = _ask_field( '''Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): ''' , lambda UpperCamelCase__ : str(UpperCamelCase__ ).lower() , ) __lowerCamelCase = _ask_field( '''Do you want to enable SageMaker metrics? [yes/NO]: ''' , _convert_yes_no_to_bool , default=UpperCamelCase__ , error_message='''Please enter yes or no.''' , ) __lowerCamelCase = None if is_sagemaker_metrics_enabled: __lowerCamelCase = _ask_field( '''Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): ''' , lambda UpperCamelCase__ : str(UpperCamelCase__ ).lower() , ) __lowerCamelCase = _ask_options( '''What is the distributed mode?''' , ['''No distributed training''', '''Data parallelism'''] , _convert_sagemaker_distributed_mode , ) __lowerCamelCase = {} __lowerCamelCase = _ask_field( '''Do you wish to optimize your script with torch dynamo?[yes/NO]:''' , _convert_yes_no_to_bool , default=UpperCamelCase__ , error_message='''Please enter yes or no.''' , ) if use_dynamo: __lowerCamelCase = '''dynamo_''' __lowerCamelCase = _ask_options( '''Which dynamo backend would you like to use?''' , [x.lower() for x in DYNAMO_BACKENDS] , _convert_dynamo_backend , default=2 , ) __lowerCamelCase = _ask_field( '''Do you want to customize the defaults sent to torch.compile? [yes/NO]: ''' , _convert_yes_no_to_bool , default=UpperCamelCase__ , error_message='''Please enter yes or no.''' , ) if use_custom_options: __lowerCamelCase = _ask_options( '''Which mode do you want to use?''' , UpperCamelCase__ , lambda UpperCamelCase__ : TORCH_DYNAMO_MODES[int(UpperCamelCase__ )] , default='''default''' , ) __lowerCamelCase = _ask_field( '''Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: ''' , _convert_yes_no_to_bool , default=UpperCamelCase__ , error_message='''Please enter yes or no.''' , ) __lowerCamelCase = _ask_field( '''Do you want to enable dynamic shape tracing? [yes/NO]: ''' , _convert_yes_no_to_bool , default=UpperCamelCase__ , error_message='''Please enter yes or no.''' , ) __lowerCamelCase = '''Which EC2 instance type you want to use for your training?''' if distributed_type != SageMakerDistributedType.NO: __lowerCamelCase = _ask_options( UpperCamelCase__ , UpperCamelCase__ , lambda UpperCamelCase__ : SAGEMAKER_PARALLEL_EC2_INSTANCES[int(UpperCamelCase__ )] ) else: eca_instance_query += "? [ml.p3.2xlarge]:" __lowerCamelCase = _ask_field(UpperCamelCase__ , lambda UpperCamelCase__ : str(UpperCamelCase__ ).lower() , default='''ml.p3.2xlarge''' ) __lowerCamelCase = 1 if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL): __lowerCamelCase = _ask_field( '''How many machines do you want use? [1]: ''' , UpperCamelCase__ , default=1 , ) __lowerCamelCase = _ask_options( '''Do you wish to use FP16 or BF16 (mixed precision)?''' , ['''no''', '''fp16''', '''bf16''', '''fp8'''] , _convert_mixed_precision , ) if use_dynamo and mixed_precision == "no": print( '''Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts.''' ) return SageMakerConfig( image_uri=UpperCamelCase__ , compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER , distributed_type=UpperCamelCase__ , use_cpu=UpperCamelCase__ , dynamo_config=UpperCamelCase__ , eca_instance_type=UpperCamelCase__ , profile=UpperCamelCase__ , region=UpperCamelCase__ , iam_role_name=UpperCamelCase__ , mixed_precision=UpperCamelCase__ , num_machines=UpperCamelCase__ , sagemaker_inputs_file=UpperCamelCase__ , sagemaker_metrics_file=UpperCamelCase__ , )
67
'''simple docstring''' import re from filelock import FileLock try: import nltk __UpperCAmelCase =True except (ImportError, ModuleNotFoundError): __UpperCAmelCase =False if NLTK_AVAILABLE: with FileLock(".lock") as lock: nltk.download("punkt", quiet=True) def __lowerCAmelCase ( UpperCamelCase__ ) -> str: re.sub('''<n>''' , '''''' , UpperCamelCase__ ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(UpperCamelCase__ ) )
67
1
'''simple docstring''' import numpy as np def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> List[Any]: __lowerCamelCase = int(np.ceil((x_end - xa) / h ) ) __lowerCamelCase = np.zeros((n + 1,) ) __lowerCamelCase = ya __lowerCamelCase = xa for k in range(UpperCamelCase__ ): __lowerCamelCase = f(UpperCamelCase__ , y[k] ) __lowerCamelCase = f(x + 0.5 * h , y[k] + 0.5 * h * ka ) __lowerCamelCase = f(x + 0.5 * h , y[k] + 0.5 * h * ka ) __lowerCamelCase = f(x + h , y[k] + h * ka ) __lowerCamelCase = y[k] + (1 / 6) * h * (ka + 2 * ka + 2 * ka + ka) x += h return y if __name__ == "__main__": import doctest doctest.testmod()
67
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={ "abeja/gpt-neox-japanese-2.7b": "https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json", } class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] ="gpt_neox_japanese" def __init__( self : List[Any] , a : Tuple=3_20_00 , a : Dict=25_60 , a : Union[str, Any]=32 , a : Dict=32 , a : Dict=4 , a : Optional[Any]="gelu" , a : Any=1.00 , a : str=1_00_00 , a : List[str]=20_48 , a : str=0.02 , a : Union[str, Any]=1e-5 , a : Optional[Any]=True , a : str=3_19_96 , a : List[str]=3_19_99 , a : str=0.1 , a : Union[str, Any]=0.0 , **a : Optional[Any] , ): """simple docstring""" super().__init__(bos_token_id=a , eos_token_id=a , **a ) __lowerCamelCase = vocab_size __lowerCamelCase = max_position_embeddings __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_multiple_size __lowerCamelCase = hidden_act __lowerCamelCase = rotary_pct __lowerCamelCase = rotary_emb_base __lowerCamelCase = initializer_range __lowerCamelCase = layer_norm_eps __lowerCamelCase = use_cache __lowerCamelCase = attention_dropout __lowerCamelCase = hidden_dropout
67
1
'''simple docstring''' import logging import torch from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.bert.modeling_bert import ( BERT_INPUTS_DOCSTRING, BERT_START_DOCSTRING, BertEncoder, BertModel, BertPreTrainedModel, ) __UpperCAmelCase =logging.getLogger(__name__) class a__ ( UpperCAmelCase__ ): def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : int , a : Optional[int] , a : List[str]=None , a : str=None ): """simple docstring""" __lowerCamelCase = self.layer[current_layer](a , a , head_mask[current_layer] ) __lowerCamelCase = layer_outputs[0] return hidden_states @add_start_docstrings( "The bare Bert Model transformer with PABEE outputting raw hidden-states without any specific head on top." , UpperCAmelCase__ , ) class a__ ( UpperCAmelCase__ ): def __init__( self : List[str] , a : Tuple ): """simple docstring""" super().__init__(a ) __lowerCamelCase = BertEncoderWithPabee(a ) self.init_weights() __lowerCamelCase = 0 __lowerCamelCase = 0 __lowerCamelCase = 0 __lowerCamelCase = 0 def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : List[Any] ): """simple docstring""" __lowerCamelCase = threshold def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Optional[int] ): """simple docstring""" __lowerCamelCase = patience def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = 0 __lowerCamelCase = 0 def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = self.inference_layers_num / self.inference_instances_num __lowerCamelCase = ( f"""*** Patience = {self.patience} Avg. Inference Layers = {avg_inf_layers:.2f} Speed Up =""" f""" {1 - avg_inf_layers / self.config.num_hidden_layers:.2f} ***""" ) print(a ) @add_start_docstrings_to_model_forward(a ) def SCREAMING_SNAKE_CASE__ ( self : Any , a : List[Any]=None , a : List[str]=None , a : str=None , a : Optional[int]=None , a : Any=None , a : Tuple=None , a : str=None , a : Optional[Any]=None , a : Dict=None , a : Tuple=None , a : Any=False , ): """simple docstring""" if input_ids is not None and inputs_embeds is not None: raise ValueError('''You cannot specify both input_ids and inputs_embeds at the same time''' ) elif input_ids is not None: __lowerCamelCase = input_ids.size() elif inputs_embeds is not None: __lowerCamelCase = inputs_embeds.size()[:-1] else: raise ValueError('''You have to specify either input_ids or inputs_embeds''' ) __lowerCamelCase = input_ids.device if input_ids is not None else inputs_embeds.device if attention_mask is None: __lowerCamelCase = torch.ones(a , device=a ) if token_type_ids is None: __lowerCamelCase = torch.zeros(a , dtype=torch.long , device=a ) # We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length] # ourselves in which case we just need to make it broadcastable to all heads. __lowerCamelCase = self.get_extended_attention_mask(a , a , a ) # If a 2D ou 3D attention mask is provided for the cross-attention # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length] if self.config.is_decoder and encoder_hidden_states is not None: __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = encoder_hidden_states.size() __lowerCamelCase = (encoder_batch_size, encoder_sequence_length) if encoder_attention_mask is None: __lowerCamelCase = torch.ones(a , device=a ) __lowerCamelCase = self.invert_attention_mask(a ) else: __lowerCamelCase = None # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape bsz x n_heads x N x N # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads] # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length] __lowerCamelCase = self.get_head_mask(a , self.config.num_hidden_layers ) __lowerCamelCase = self.embeddings( input_ids=a , position_ids=a , token_type_ids=a , inputs_embeds=a ) __lowerCamelCase = embedding_output if self.training: __lowerCamelCase = [] for i in range(self.config.num_hidden_layers ): __lowerCamelCase = self.encoder.adaptive_forward( a , current_layer=a , attention_mask=a , head_mask=a ) __lowerCamelCase = self.pooler(a ) __lowerCamelCase = output_layers[i](output_dropout(a ) ) res.append(a ) elif self.patience == 0: # Use all layers for inference __lowerCamelCase = self.encoder( a , attention_mask=a , head_mask=a , encoder_hidden_states=a , encoder_attention_mask=a , ) __lowerCamelCase = self.pooler(encoder_outputs[0] ) __lowerCamelCase = [output_layers[self.config.num_hidden_layers - 1](a )] else: __lowerCamelCase = 0 __lowerCamelCase = None __lowerCamelCase = 0 for i in range(self.config.num_hidden_layers ): calculated_layer_num += 1 __lowerCamelCase = self.encoder.adaptive_forward( a , current_layer=a , attention_mask=a , head_mask=a ) __lowerCamelCase = self.pooler(a ) __lowerCamelCase = output_layers[i](a ) if regression: __lowerCamelCase = logits.detach() if patient_result is not None: __lowerCamelCase = patient_result.detach() if (patient_result is not None) and torch.abs(patient_result - labels ) < self.regression_threshold: patient_counter += 1 else: __lowerCamelCase = 0 else: __lowerCamelCase = logits.detach().argmax(dim=1 ) if patient_result is not None: __lowerCamelCase = patient_result.detach().argmax(dim=1 ) if (patient_result is not None) and torch.all(labels.eq(a ) ): patient_counter += 1 else: __lowerCamelCase = 0 __lowerCamelCase = logits if patient_counter == self.patience: break __lowerCamelCase = [patient_result] self.inference_layers_num += calculated_layer_num self.inference_instances_num += 1 return res @add_start_docstrings( "Bert Model transformer with PABEE and a sequence classification/regression head on top (a linear layer on top of\n the pooled output) e.g. for GLUE tasks. " , UpperCAmelCase__ , ) class a__ ( UpperCAmelCase__ ): def __init__( self : Any , a : Optional[int] ): """simple docstring""" super().__init__(a ) __lowerCamelCase = config.num_labels __lowerCamelCase = BertModelWithPabee(a ) __lowerCamelCase = nn.Dropout(config.hidden_dropout_prob ) __lowerCamelCase = nn.ModuleList( [nn.Linear(config.hidden_size , self.config.num_labels ) for _ in range(config.num_hidden_layers )] ) self.init_weights() @add_start_docstrings_to_model_forward(a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : Optional[int]=None , a : List[str]=None , a : List[Any]=None , a : Dict=None , a : Any=None , a : Any=None , a : Tuple=None , ): """simple docstring""" __lowerCamelCase = self.bert( input_ids=a , attention_mask=a , token_type_ids=a , position_ids=a , head_mask=a , inputs_embeds=a , output_dropout=self.dropout , output_layers=self.classifiers , regression=self.num_labels == 1 , ) __lowerCamelCase = (logits[-1],) if labels is not None: __lowerCamelCase = None __lowerCamelCase = 0 for ix, logits_item in enumerate(a ): if self.num_labels == 1: # We are doing regression __lowerCamelCase = MSELoss() __lowerCamelCase = loss_fct(logits_item.view(-1 ) , labels.view(-1 ) ) else: __lowerCamelCase = CrossEntropyLoss() __lowerCamelCase = loss_fct(logits_item.view(-1 , self.num_labels ) , labels.view(-1 ) ) if total_loss is None: __lowerCamelCase = loss else: total_loss += loss * (ix + 1) total_weights += ix + 1 __lowerCamelCase = (total_loss / total_weights,) + outputs return outputs
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> int: while second != 0: __lowerCamelCase = first & second first ^= second __lowerCamelCase = c << 1 return first if __name__ == "__main__": import doctest doctest.testmod() __UpperCAmelCase =int(input("Enter the first number: ").strip()) __UpperCAmelCase =int(input("Enter the second number: ").strip()) print(f'{add(first, second) = }')
67
1
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST, OpenAIGPTConfig, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification, OpenAIGPTLMHeadModel, OpenAIGPTModel, ) class a__ : def __init__( self : Union[str, Any] , a : Union[str, Any] , a : Tuple=13 , a : Optional[Any]=7 , a : List[Any]=True , a : Optional[Any]=True , a : Any=True , a : Union[str, Any]=99 , a : Any=32 , a : int=5 , a : Optional[int]=4 , a : Union[str, Any]=37 , a : Optional[Any]="gelu" , a : Union[str, Any]=0.1 , a : Any=0.1 , a : Optional[int]=5_12 , a : int=16 , a : Optional[Any]=2 , a : Union[str, Any]=0.02 , a : Any=3 , a : Dict=4 , a : Any=None , ): """simple docstring""" __lowerCamelCase = parent __lowerCamelCase = batch_size __lowerCamelCase = seq_length __lowerCamelCase = is_training __lowerCamelCase = use_token_type_ids __lowerCamelCase = use_labels __lowerCamelCase = vocab_size __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_size __lowerCamelCase = hidden_act __lowerCamelCase = hidden_dropout_prob __lowerCamelCase = attention_probs_dropout_prob __lowerCamelCase = max_position_embeddings __lowerCamelCase = type_vocab_size __lowerCamelCase = type_sequence_label_size __lowerCamelCase = initializer_range __lowerCamelCase = num_labels __lowerCamelCase = num_choices __lowerCamelCase = scope __lowerCamelCase = self.vocab_size - 1 def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCamelCase = None if self.use_token_type_ids: __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = None if self.use_labels: __lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __lowerCamelCase = ids_tensor([self.batch_size] , self.num_choices ) __lowerCamelCase = OpenAIGPTConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) __lowerCamelCase = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, head_mask, token_type_ids, sequence_labels, token_labels, choice_labels, ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : Dict , a : List[str] , a : Tuple , a : List[Any] , *a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTModel(config=a ) model.to(a ) model.eval() __lowerCamelCase = model(a , token_type_ids=a , head_mask=a ) __lowerCamelCase = model(a , token_type_ids=a ) __lowerCamelCase = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Union[str, Any] , a : Dict , a : Union[str, Any] , a : Tuple , *a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTLMHeadModel(a ) model.to(a ) model.eval() __lowerCamelCase = model(a , token_type_ids=a , labels=a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Tuple , a : Optional[int] , a : Union[str, Any] , a : Optional[Any] , *a : Optional[Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTDoubleHeadsModel(a ) model.to(a ) model.eval() __lowerCamelCase = model(a , token_type_ids=a , labels=a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self : List[str] , a : int , a : Dict , a : Optional[Any] , a : str , *a : int ): """simple docstring""" __lowerCamelCase = self.num_labels __lowerCamelCase = OpenAIGPTForSequenceClassification(a ) model.to(a ) model.eval() __lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCamelCase = model(a , token_type_ids=a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = self.prepare_config_and_inputs() ( ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ) = config_and_inputs __lowerCamelCase = { '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask, } return config, inputs_dict @require_torch class a__ ( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): lowerCamelCase : List[str] =( (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification) if is_torch_available() else () ) lowerCamelCase : str =( (OpenAIGPTLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly lowerCamelCase : Optional[int] =( { "feature-extraction": OpenAIGPTModel, "text-classification": OpenAIGPTForSequenceClassification, "text-generation": OpenAIGPTLMHeadModel, "zero-shot": OpenAIGPTForSequenceClassification, } if is_torch_available() else {} ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : Tuple , a : Optional[int] , a : int , a : str , a : Any ): """simple docstring""" if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a # tiny config could not be created. return True return False def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : int , a : Optional[int] , a : str=False ): """simple docstring""" __lowerCamelCase = super()._prepare_for_class(a , a , return_labels=a ) if return_labels: if model_class.__name__ == "OpenAIGPTDoubleHeadsModel": __lowerCamelCase = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length) , dtype=torch.long , device=a , ) __lowerCamelCase = inputs_dict['''labels'''] __lowerCamelCase = inputs_dict['''labels'''] __lowerCamelCase = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices) , dtype=torch.long , device=a , ) __lowerCamelCase = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=a ) return inputs_dict def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTModelTester(self ) __lowerCamelCase = ConfigTester(self , config_class=a , n_embd=37 ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_double_lm_head_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*a ) @slow def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __lowerCamelCase = OpenAIGPTModel.from_pretrained(a ) self.assertIsNotNone(a ) @require_torch class a__ ( unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt''' ) model.to(a ) __lowerCamelCase = torch.tensor([[4_81, 47_35, 5_44]] , dtype=torch.long , device=a ) # the president is __lowerCamelCase = [ 4_81, 47_35, 5_44, 2_46, 9_63, 8_70, 7_62, 2_39, 2_44, 4_04_77, 2_44, 2_49, 7_19, 8_81, 4_87, 5_44, 2_40, 2_44, 6_03, 4_81, ] # the president is a very good man. " \n " i\'m sure he is, " said the __lowerCamelCase = model.generate(a , do_sample=a ) self.assertListEqual(output_ids[0].tolist() , a )
67
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase ={ "configuration_time_series_transformer": [ "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimeSeriesTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimeSeriesTransformerForPrediction", "TimeSeriesTransformerModel", "TimeSeriesTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
67
1
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ) -> float: __lowerCamelCase = [redshift, radiation_density, matter_density, dark_energy] if any(p < 0 for p in parameters ): raise ValueError('''All input parameters must be positive''' ) if any(p > 1 for p in parameters[1:4] ): raise ValueError('''Relative densities cannot be greater than one''' ) else: __lowerCamelCase = 1 - (matter_density + radiation_density + dark_energy) __lowerCamelCase = ( radiation_density * (redshift + 1) ** 4 + matter_density * (redshift + 1) ** 3 + curvature * (redshift + 1) ** 2 + dark_energy ) __lowerCamelCase = hubble_constant * e_a ** (1 / 2) return hubble if __name__ == "__main__": import doctest # run doctest doctest.testmod() # demo LCDM approximation __UpperCAmelCase =0.3 print( hubble_parameter( hubble_constant=68.3, radiation_density=1e-4, matter_density=matter_density, dark_energy=1 - matter_density, redshift=0, ) )
67
'''simple docstring''' import warnings from pathlib import Path from typing import List, Tuple, Union import fire from torch import nn from transformers import AutoModelForSeqaSeqLM, AutoTokenizer, PreTrainedModel from transformers.utils import logging __UpperCAmelCase =logging.get_logger(__name__) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> None: __lowerCamelCase = nn.ModuleList([src_layers[i] for i in layers_to_copy] ) assert len(UpperCamelCase__ ) == len(UpperCamelCase__ ), f"""{len(UpperCamelCase__ )} != {len(UpperCamelCase__ )}""" dest_layers.load_state_dict(layers_to_copy.state_dict() ) __UpperCAmelCase ={ # maps num layers in teacher -> num_layers in student -> which teacher layers to copy. # 12: bart, 16: pegasus, 6: marian/Helsinki-NLP 1_2: { 1: [0], # This says that if the teacher has 12 layers and the student has 1, copy layer 0 of the teacher 2: [0, 6], 3: [0, 6, 1_1], 4: [0, 4, 8, 1_1], 6: [0, 2, 4, 7, 9, 1_1], 9: [0, 1, 2, 4, 5, 7, 9, 1_0, 1_1], 1_2: list(range(1_2)), }, 1_6: { # maps num layers in student -> which teacher layers to copy 1: [0], 2: [0, 1_5], 3: [0, 8, 1_5], 4: [0, 5, 1_0, 1_5], 6: [0, 3, 6, 9, 1_2, 1_5], 8: [0, 2, 4, 6, 8, 1_0, 1_2, 1_5], 9: [0, 1, 3, 5, 7, 9, 1_1, 1_3, 1_5], 1_2: [0, 1, 2, 3, 4, 5, 6, 7, 9, 1_1, 1_3, 1_5], 1_6: list(range(1_6)), }, 6: {1: [0], 2: [0, 5], 3: [0, 2, 5], 4: [0, 1, 3, 5], 6: list(range(6))}, } __UpperCAmelCase ={ # maps num layers in student -> which teacher layers to copy. 6: {1: [5], 2: [3, 5], 3: [1, 4, 5], 4: [1, 2, 4, 5]}, 1_2: {1: [1_1], 2: [5, 1_1], 3: [3, 7, 1_1], 6: [1, 3, 5, 8, 1_0, 1_1]}, 1_6: {1: [1_5], 4: [4, 9, 1_2, 1_5], 8: [1, 3, 5, 7, 9, 1_1, 1_3, 1_5]}, } def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Union[str, Any]: try: __lowerCamelCase = LAYERS_TO_COPY[n_teacher][n_student] return val except KeyError: if n_student != n_teacher: warnings.warn( f"""no hardcoded layers to copy for teacher {n_teacher} -> student {n_student}, defaulting to first""" f""" {n_student}""" ) return list(range(UpperCamelCase__ ) ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> List[int]: if n_student > n_teacher: raise ValueError(f"""Cannot perform intermediate supervision for student {n_student} > teacher {n_teacher}""" ) elif n_teacher == n_student: return list(range(UpperCamelCase__ ) ) elif n_student == 1: return [n_teacher - 1] else: return LAYERS_TO_SUPERVISE[n_teacher][n_student] def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ = "student" , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__=False , UpperCamelCase__=None , UpperCamelCase__=None , **UpperCamelCase__ , ) -> Tuple[PreTrainedModel, List[int], List[int]]: __lowerCamelCase = '''encoder_layers and decoder_layers cannot be both None-- you would just have an identical teacher.''' assert (e is not None) or (d is not None), _msg if isinstance(UpperCamelCase__ , UpperCamelCase__ ): AutoTokenizer.from_pretrained(UpperCamelCase__ ).save_pretrained(UpperCamelCase__ ) # purely for convenience __lowerCamelCase = AutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__ ).eval() else: assert isinstance(UpperCamelCase__ , UpperCamelCase__ ), f"""teacher must be a model or string got type {type(UpperCamelCase__ )}""" __lowerCamelCase = teacher.config.to_diff_dict() try: __lowerCamelCase , __lowerCamelCase = teacher.config.encoder_layers, teacher.config.decoder_layers if e is None: __lowerCamelCase = teacher_e if d is None: __lowerCamelCase = teacher_d init_kwargs.update({'''encoder_layers''': e, '''decoder_layers''': d} ) except AttributeError: # T5 if hasattr(teacher.config , '''num_encoder_layers''' ): __lowerCamelCase , __lowerCamelCase = teacher.config.num_encoder_layers, teacher.config.num_decoder_layers else: __lowerCamelCase , __lowerCamelCase = teacher.config.num_layers, teacher.config.num_decoder_layers if e is None: __lowerCamelCase = teacher_e if d is None: __lowerCamelCase = teacher_d if hasattr(teacher.config , '''num_encoder_layers''' ): init_kwargs.update({'''num_encoder_layers''': e, '''num_decoder_layers''': d} ) else: init_kwargs.update({'''num_layers''': e, '''num_decoder_layers''': d} ) # Kwargs to instantiate student: teacher kwargs with updated layer numbers + **extra_config_kwargs init_kwargs.update(UpperCamelCase__ ) # Copy weights __lowerCamelCase = teacher.config_class(**UpperCamelCase__ ) __lowerCamelCase = AutoModelForSeqaSeqLM.from_config(UpperCamelCase__ ) # Start by copying the full teacher state dict this will copy the first N teacher layers to the student. __lowerCamelCase = student.load_state_dict(teacher.state_dict() , strict=UpperCamelCase__ ) assert info.missing_keys == [], info.missing_keys # every student key should have a teacher keys. if copy_first_teacher_layers: # Our copying is done. We just log and save __lowerCamelCase , __lowerCamelCase = list(range(UpperCamelCase__ ) ), list(range(UpperCamelCase__ ) ) logger.info( f"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to""" f""" {save_path}""" ) student.save_pretrained(UpperCamelCase__ ) return student, e_layers_to_copy, d_layers_to_copy # Decide which layers of the teacher to copy. Not exactly alternating -- we try to keep first and last layer. if e_layers_to_copy is None: __lowerCamelCase = pick_layers_to_copy(UpperCamelCase__ , UpperCamelCase__ ) if d_layers_to_copy is None: __lowerCamelCase = pick_layers_to_copy(UpperCamelCase__ , UpperCamelCase__ ) try: if hasattr( UpperCamelCase__ , '''prophetnet''' ): # For ProphetNet, student.model.encoder.layers is called student.prophetnet.encoder.layers copy_layers(teacher.prophetnet.encoder.layers , student.prophetnet.encoder.layers , UpperCamelCase__ ) copy_layers(teacher.prophetnet.decoder.layers , student.prophetnet.decoder.layers , UpperCamelCase__ ) else: copy_layers(teacher.model.encoder.layers , student.model.encoder.layers , UpperCamelCase__ ) copy_layers(teacher.model.decoder.layers , student.model.decoder.layers , UpperCamelCase__ ) except AttributeError: # For t5, student.model.encoder.layers is called student.encoder.block copy_layers(teacher.encoder.block , student.encoder.block , UpperCamelCase__ ) copy_layers(teacher.decoder.block , student.decoder.block , UpperCamelCase__ ) logger.info( f"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to {save_path}""" ) __lowerCamelCase = { '''teacher_type''': teacher.config.model_type, '''copied_encoder_layers''': e_layers_to_copy, '''copied_decoder_layers''': d_layers_to_copy, } student.save_pretrained(UpperCamelCase__ ) # Save information about copying for easier reproducibility return student, e_layers_to_copy, d_layers_to_copy if __name__ == "__main__": fire.Fire(create_student_by_copying_alternating_layers)
67
1
'''simple docstring''' # NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from ...utils import deprecate from ..controlnet.multicontrolnet import MultiControlNetModel # noqa: F401 from ..controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline # noqa: F401 deprecate( "stable diffusion controlnet", "0.22.0", "Importing `StableDiffusionControlNetPipeline` or `MultiControlNetModel` from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import StableDiffusionControlNetPipeline` instead.", standard_warn=False, stacklevel=3, )
67
'''simple docstring''' import unittest from pathlib import Path from tempfile import TemporaryDirectory from transformers import AutoConfig, TFGPTaLMHeadModel, is_keras_nlp_available, is_tf_available from transformers.models.gpta.tokenization_gpta import GPTaTokenizer from transformers.testing_utils import require_keras_nlp, require_tf, slow if is_tf_available(): import tensorflow as tf if is_keras_nlp_available(): from transformers.models.gpta import TFGPTaTokenizer __UpperCAmelCase =["gpt2"] __UpperCAmelCase ="gpt2" if is_tf_available(): class a__ ( tf.Module ): def __init__( self : str , a : Union[str, Any] ): """simple docstring""" super().__init__() __lowerCamelCase = tokenizer __lowerCamelCase = AutoConfig.from_pretrained(a ) __lowerCamelCase = TFGPTaLMHeadModel.from_config(a ) @tf.function(input_signature=(tf.TensorSpec((None,) , tf.string , name='''text''' ),) ) def SCREAMING_SNAKE_CASE__ ( self : str , a : Tuple ): """simple docstring""" __lowerCamelCase = self.tokenizer(a ) __lowerCamelCase = tokenized['''input_ids'''].to_tensor() __lowerCamelCase = tf.cast(input_ids_dense > 0 , tf.intaa ) # input_mask = tf.reshape(input_mask, [-1, MAX_SEQ_LEN]) __lowerCamelCase = self.model(input_ids=a , attention_mask=a )['''logits'''] return outputs @require_tf @require_keras_nlp class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" super().setUp() __lowerCamelCase = [GPTaTokenizer.from_pretrained(a ) for checkpoint in (TOKENIZER_CHECKPOINTS)] __lowerCamelCase = [TFGPTaTokenizer.from_pretrained(a ) for checkpoint in TOKENIZER_CHECKPOINTS] assert len(self.tokenizers ) == len(self.tf_tokenizers ) __lowerCamelCase = [ '''This is a straightforward English test sentence.''', '''This one has some weird characters\rto\nsee\r\nif those\u00E9break things.''', '''Now we\'re going to add some Chinese: 一 二 三 一二三''', '''And some much more rare Chinese: 齉 堃 齉堃''', '''Je vais aussi écrire en français pour tester les accents''', '''Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ''', ] __lowerCamelCase = list(zip(self.test_sentences , self.test_sentences[::-1] ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" for tokenizer, tf_tokenizer in zip(self.tokenizers , self.tf_tokenizers ): for test_inputs in self.test_sentences: __lowerCamelCase = tokenizer([test_inputs] , return_tensors='''tf''' ) __lowerCamelCase = tf_tokenizer([test_inputs] ) for key in python_outputs.keys(): # convert them to numpy to avoid messing with ragged tensors __lowerCamelCase = python_outputs[key].numpy() __lowerCamelCase = tf_outputs[key].numpy() self.assertTrue(tf.reduce_all(python_outputs_values.shape == tf_outputs_values.shape ) ) self.assertTrue(tf.reduce_all(tf.cast(a , tf.intaa ) == tf_outputs_values ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: __lowerCamelCase = tf.function(a ) for test_inputs in self.test_sentences: __lowerCamelCase = tf.constant(a ) __lowerCamelCase = compiled_tokenizer(a ) __lowerCamelCase = tf_tokenizer(a ) for key in eager_outputs.keys(): self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key] ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: __lowerCamelCase = ModelToSave(tokenizer=a ) __lowerCamelCase = tf.convert_to_tensor([self.test_sentences[0]] ) __lowerCamelCase = model.serving(a ) # Build model with some sample inputs with TemporaryDirectory() as tempdir: __lowerCamelCase = Path(a ) / '''saved.model''' tf.saved_model.save(a , a , signatures={'''serving_default''': model.serving} ) __lowerCamelCase = tf.saved_model.load(a ) __lowerCamelCase = loaded_model.signatures['''serving_default'''](a )['''output_0'''] # We may see small differences because the loaded model is compiled, so we need an epsilon for the test self.assertTrue(tf.reduce_all(out == loaded_output ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: __lowerCamelCase = tf.convert_to_tensor([self.test_sentences[0]] ) __lowerCamelCase = tf_tokenizer(a ) # Build model with some sample inputs __lowerCamelCase = tf_tokenizer.get_config() __lowerCamelCase = TFGPTaTokenizer.from_config(a ) __lowerCamelCase = model_from_config(a ) for key in from_config_output.keys(): self.assertTrue(tf.reduce_all(from_config_output[key] == out[key] ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: # for the test to run __lowerCamelCase = 12_31_23 for max_length in [3, 5, 10_24]: __lowerCamelCase = tf.convert_to_tensor([self.test_sentences[0]] ) __lowerCamelCase = tf_tokenizer(a , max_length=a ) __lowerCamelCase = out['''input_ids'''].numpy().shape[1] assert out_length == max_length
67
1
'''simple docstring''' import copy import inspect import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import VideoMAEConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEForPreTraining, VideoMAEForVideoClassification, VideoMAEModel, ) from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from transformers import VideoMAEImageProcessor class a__ : def __init__( self : Union[str, Any] , a : Union[str, Any] , a : Any=13 , a : Optional[Any]=10 , a : Union[str, Any]=3 , a : str=2 , a : Union[str, Any]=2 , a : Optional[Any]=2 , a : Tuple=True , a : str=True , a : Tuple=32 , a : Union[str, Any]=5 , a : int=4 , a : List[str]=37 , a : Tuple="gelu" , a : Union[str, Any]=0.1 , a : Dict=0.1 , a : List[str]=10 , a : Any=0.02 , a : Any=0.9 , a : Dict=None , ): """simple docstring""" __lowerCamelCase = parent __lowerCamelCase = batch_size __lowerCamelCase = image_size __lowerCamelCase = num_channels __lowerCamelCase = patch_size __lowerCamelCase = tubelet_size __lowerCamelCase = num_frames __lowerCamelCase = is_training __lowerCamelCase = use_labels __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_size __lowerCamelCase = hidden_act __lowerCamelCase = hidden_dropout_prob __lowerCamelCase = attention_probs_dropout_prob __lowerCamelCase = type_sequence_label_size __lowerCamelCase = initializer_range __lowerCamelCase = mask_ratio __lowerCamelCase = scope # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame __lowerCamelCase = (image_size // patch_size) ** 2 __lowerCamelCase = (num_frames // tubelet_size) * self.num_patches_per_frame # use this variable to define bool_masked_pos __lowerCamelCase = int(mask_ratio * self.seq_length ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = floats_tensor( [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] ) __lowerCamelCase = None if self.use_labels: __lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCamelCase = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" return VideoMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , num_frames=self.num_frames , tubelet_size=self.tubelet_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=a , initializer_range=self.initializer_range , ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : str , a : int , a : Any ): """simple docstring""" __lowerCamelCase = VideoMAEModel(config=a ) model.to(a ) model.eval() __lowerCamelCase = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE__ ( self : int , a : List[Any] , a : int , a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = VideoMAEForPreTraining(a ) model.to(a ) model.eval() # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch __lowerCamelCase = torch.ones((self.num_masks,) ) __lowerCamelCase = torch.cat([mask, torch.zeros(self.seq_length - mask.size(0 ) )] ) __lowerCamelCase = mask.expand(self.batch_size , -1 ).bool() __lowerCamelCase = model(a , a ) # model only returns predictions for masked patches __lowerCamelCase = mask.sum().item() __lowerCamelCase = 3 * self.tubelet_size * self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_masked_patches, decoder_num_labels) ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = self.prepare_config_and_inputs() __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = config_and_inputs __lowerCamelCase = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class a__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): lowerCamelCase : Tuple =( (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else () ) lowerCamelCase : int =( {"feature-extraction": VideoMAEModel, "video-classification": VideoMAEForVideoClassification} if is_torch_available() else {} ) lowerCamelCase : Optional[Any] =False lowerCamelCase : int =False lowerCamelCase : List[Any] =False lowerCamelCase : Optional[int] =False def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = VideoMAEModelTester(self ) __lowerCamelCase = ConfigTester(self , config_class=a , has_text_modality=a , hidden_size=37 ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : Any , a : Any , a : Any=False ): """simple docstring""" __lowerCamelCase = copy.deepcopy(a ) if model_class == VideoMAEForPreTraining: # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch __lowerCamelCase = torch.ones((self.model_tester.num_masks,) ) __lowerCamelCase = torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0 ) )] ) __lowerCamelCase = mask.expand(self.model_tester.batch_size , -1 ).bool() __lowerCamelCase = bool_masked_pos.to(a ) if return_labels: if model_class in [ *get_values(a ), ]: __lowerCamelCase = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=a ) return inputs_dict def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='''VideoMAE does not use inputs_embeds''' ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" pass def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase , __lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowerCamelCase = model_class(a ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) __lowerCamelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(a , nn.Linear ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase , __lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowerCamelCase = model_class(a ) __lowerCamelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __lowerCamelCase = [*signature.parameters.keys()] __lowerCamelCase = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , a ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*a ) @slow def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __lowerCamelCase = VideoMAEModel.from_pretrained(a ) self.assertIsNotNone(a ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" if not self.has_attentions: pass else: __lowerCamelCase , __lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() __lowerCamelCase = True for model_class in self.all_model_classes: __lowerCamelCase = self.model_tester.seq_length - self.model_tester.num_masks __lowerCamelCase = ( num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length ) __lowerCamelCase = True __lowerCamelCase = False __lowerCamelCase = True __lowerCamelCase = model_class(a ) model.to(a ) model.eval() with torch.no_grad(): __lowerCamelCase = model(**self._prepare_for_class(a , a ) ) __lowerCamelCase = outputs.attentions self.assertEqual(len(a ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] __lowerCamelCase = True __lowerCamelCase = model_class(a ) model.to(a ) model.eval() with torch.no_grad(): __lowerCamelCase = model(**self._prepare_for_class(a , a ) ) __lowerCamelCase = outputs.attentions self.assertEqual(len(a ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) __lowerCamelCase = len(a ) # Check attention is always last and order is fine __lowerCamelCase = True __lowerCamelCase = True __lowerCamelCase = model_class(a ) model.to(a ) model.eval() with torch.no_grad(): __lowerCamelCase = model(**self._prepare_for_class(a , a ) ) self.assertEqual(out_len + 1 , len(a ) ) __lowerCamelCase = outputs.attentions self.assertEqual(len(a ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" def check_hidden_states_output(a : int , a : Optional[int] , a : int ): __lowerCamelCase = model_class(a ) model.to(a ) model.eval() with torch.no_grad(): __lowerCamelCase = model(**self._prepare_for_class(a , a ) ) __lowerCamelCase = outputs.hidden_states __lowerCamelCase = self.model_tester.num_hidden_layers + 1 self.assertEqual(len(a ) , a ) __lowerCamelCase = self.model_tester.seq_length - self.model_tester.num_masks __lowerCamelCase = num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) __lowerCamelCase , __lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowerCamelCase = True check_hidden_states_output(a , a , a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __lowerCamelCase = True check_hidden_states_output(a , a , a ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" pass def __lowerCAmelCase ( ) -> List[str]: __lowerCamelCase = hf_hub_download( repo_id='''hf-internal-testing/spaghetti-video''' , filename='''eating_spaghetti.npy''' , repo_type='''dataset''' ) __lowerCamelCase = np.load(UpperCamelCase__ ) return list(UpperCamelCase__ ) @require_torch @require_vision class a__ ( unittest.TestCase ): @cached_property def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" return ( VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5] ) if is_vision_available() else None ) @slow def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics''' ).to( a ) __lowerCamelCase = self.default_image_processor __lowerCamelCase = prepare_video() __lowerCamelCase = image_processor(a , return_tensors='''pt''' ).to(a ) # forward pass with torch.no_grad(): __lowerCamelCase = model(**a ) # verify the logits __lowerCamelCase = torch.Size((1, 4_00) ) self.assertEqual(outputs.logits.shape , a ) __lowerCamelCase = torch.tensor([0.36_69, -0.06_88, -0.24_21] ).to(a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , a , atol=1e-4 ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short''' ).to(a ) __lowerCamelCase = self.default_image_processor __lowerCamelCase = prepare_video() __lowerCamelCase = image_processor(a , return_tensors='''pt''' ).to(a ) # add boolean mask, indicating which patches to mask __lowerCamelCase = hf_hub_download(repo_id='''hf-internal-testing/bool-masked-pos''' , filename='''bool_masked_pos.pt''' ) __lowerCamelCase = torch.load(a ) # forward pass with torch.no_grad(): __lowerCamelCase = model(**a ) # verify the logits __lowerCamelCase = torch.Size([1, 14_08, 15_36] ) __lowerCamelCase = torch.tensor( [[0.79_94, 0.96_12, 0.85_08], [0.74_01, 0.89_58, 0.83_02], [0.58_62, 0.74_68, 0.73_25]] , device=a ) self.assertEqual(outputs.logits.shape , a ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , a , atol=1e-4 ) ) # verify the loss (`config.norm_pix_loss` = `True`) __lowerCamelCase = torch.tensor([0.51_42] , device=a ) self.assertTrue(torch.allclose(outputs.loss , a , atol=1e-4 ) ) # verify the loss (`config.norm_pix_loss` = `False`) __lowerCamelCase = VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short''' , norm_pix_loss=a ).to( a ) with torch.no_grad(): __lowerCamelCase = model(**a ) __lowerCamelCase = torch.tensor(torch.tensor([0.64_69] ) , device=a ) self.assertTrue(torch.allclose(outputs.loss , a , atol=1e-4 ) )
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Optional[int]: if b == 0: return 1 if (b % 2) == 0: return actual_power(UpperCamelCase__ , int(b / 2 ) ) * actual_power(UpperCamelCase__ , int(b / 2 ) ) else: return a * actual_power(UpperCamelCase__ , int(b / 2 ) ) * actual_power(UpperCamelCase__ , int(b / 2 ) ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> float: if b < 0: return 1 / actual_power(UpperCamelCase__ , UpperCamelCase__ ) return actual_power(UpperCamelCase__ , UpperCamelCase__ ) if __name__ == "__main__": print(power(-2, -3))
67
1
'''simple docstring''' import os import unittest from huggingface_hub.utils import are_progress_bars_disabled import transformers.models.bart.tokenization_bart from transformers import logging from transformers.testing_utils import CaptureLogger, mockenv, mockenv_context from transformers.utils.logging import disable_progress_bar, enable_progress_bar class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = logging.get_logger() # the current default level is logging.WARNING __lowerCamelCase = logging.get_verbosity() logging.set_verbosity_error() self.assertEqual(logger.getEffectiveLevel() , logging.get_verbosity() ) logging.set_verbosity_warning() self.assertEqual(logger.getEffectiveLevel() , logging.get_verbosity() ) logging.set_verbosity_info() self.assertEqual(logger.getEffectiveLevel() , logging.get_verbosity() ) logging.set_verbosity_debug() self.assertEqual(logger.getEffectiveLevel() , logging.get_verbosity() ) # restore to the original level logging.set_verbosity(a ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = logging.get_verbosity() __lowerCamelCase = logging.get_logger('''transformers.models.bart.tokenization_bart''' ) __lowerCamelCase = '''Testing 1, 2, 3''' # should be able to log warnings (if default settings weren't overridden by `pytest --log-level-all`) if level_origin <= logging.WARNING: with CaptureLogger(a ) as cl: logger.warning(a ) self.assertEqual(cl.out , msg + '''\n''' ) # this is setting the level for all of `transformers.*` loggers logging.set_verbosity_error() # should not be able to log warnings with CaptureLogger(a ) as cl: logger.warning(a ) self.assertEqual(cl.out , '''''' ) # should be able to log warnings again logging.set_verbosity_warning() with CaptureLogger(a ) as cl: logger.warning(a ) self.assertEqual(cl.out , msg + '''\n''' ) # restore to the original level logging.set_verbosity(a ) @mockenv(TRANSFORMERS_VERBOSITY='''error''' ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" transformers.utils.logging._reset_library_root_logger() # this action activates the env var __lowerCamelCase = logging.get_logger('''transformers.models.bart.tokenization_bart''' ) __lowerCamelCase = os.getenv('''TRANSFORMERS_VERBOSITY''' , a ) __lowerCamelCase = logging.log_levels[env_level_str] __lowerCamelCase = logging.get_verbosity() self.assertEqual( a , a , f"""TRANSFORMERS_VERBOSITY={env_level_str}/{env_level}, but internal verbosity is {current_level}""" , ) # restore to the original level __lowerCamelCase = '''''' transformers.utils.logging._reset_library_root_logger() @mockenv(TRANSFORMERS_VERBOSITY='''super-error''' ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" transformers.utils.logging._reset_library_root_logger() __lowerCamelCase = logging.logging.getLogger() with CaptureLogger(a ) as cl: # this action activates the env var logging.get_logger('''transformers.models.bart.tokenization_bart''' ) self.assertIn('''Unknown option TRANSFORMERS_VERBOSITY=super-error''' , cl.out ) # no need to restore as nothing was changed def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" transformers.utils.logging._reset_library_root_logger() __lowerCamelCase = logging.get_logger('''transformers.models.bart.tokenization_bart''' ) __lowerCamelCase = '''Testing 1, 2, 3''' with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS='''1''' ): # nothing should be logged as env var disables this method with CaptureLogger(a ) as cl: logger.warning_advice(a ) self.assertEqual(cl.out , '''''' ) with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS='''''' ): # should log normally as TRANSFORMERS_NO_ADVISORY_WARNINGS is unset with CaptureLogger(a ) as cl: logger.warning_advice(a ) self.assertEqual(cl.out , msg + '''\n''' ) def __lowerCAmelCase ( ) -> List[str]: disable_progress_bar() assert are_progress_bars_disabled() enable_progress_bar() assert not are_progress_bars_disabled()
67
'''simple docstring''' import logging import os from .state import PartialState class a__ ( logging.LoggerAdapter ): @staticmethod def SCREAMING_SNAKE_CASE__ ( a : Optional[Any] ): """simple docstring""" __lowerCamelCase = PartialState() return not main_process_only or (main_process_only and state.is_main_process) def SCREAMING_SNAKE_CASE__ ( self : int , a : Optional[int] , a : str , *a : Optional[int] , **a : List[Any] ): """simple docstring""" if PartialState._shared_state == {}: raise RuntimeError( '''You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.''' ) __lowerCamelCase = kwargs.pop('''main_process_only''' , a ) __lowerCamelCase = kwargs.pop('''in_order''' , a ) if self.isEnabledFor(a ): if self._should_log(a ): __lowerCamelCase , __lowerCamelCase = self.process(a , a ) self.logger.log(a , a , *a , **a ) elif in_order: __lowerCamelCase = PartialState() for i in range(state.num_processes ): if i == state.process_index: __lowerCamelCase , __lowerCamelCase = self.process(a , a ) self.logger.log(a , a , *a , **a ) state.wait_for_everyone() def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ = None ) -> Optional[int]: if log_level is None: __lowerCamelCase = os.environ.get('''ACCELERATE_LOG_LEVEL''' , UpperCamelCase__ ) __lowerCamelCase = logging.getLogger(UpperCamelCase__ ) if log_level is not None: logger.setLevel(log_level.upper() ) logger.root.setLevel(log_level.upper() ) return MultiProcessAdapter(UpperCamelCase__ , {} )
67
1
'''simple docstring''' import logging import os import sys import warnings from dataclasses import dataclass, field from random import randint from typing import Optional import datasets import evaluate import numpy as np from datasets import DatasetDict, load_dataset import transformers from transformers import ( AutoConfig, AutoFeatureExtractor, AutoModelForAudioClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version __UpperCAmelCase =logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") require_version("datasets>=1.14.0", "To fix: pip install -r examples/pytorch/audio-classification/requirements.txt") def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = 1_60_00 ) -> Any: __lowerCamelCase = int(round(sample_rate * max_length ) ) if len(UpperCamelCase__ ) <= sample_length: return wav __lowerCamelCase = randint(0 , len(UpperCamelCase__ ) - sample_length - 1 ) return wav[random_offset : random_offset + sample_length] @dataclass class a__ : lowerCamelCase : Optional[str] =field(default=UpperCAmelCase__ , metadata={"help": "Name of a dataset from the datasets package"} ) lowerCamelCase : Optional[str] =field( default=UpperCAmelCase__ , metadata={"help": "The configuration name of the dataset to use (via the datasets library)."} ) lowerCamelCase : Optional[str] =field( default=UpperCAmelCase__ , metadata={"help": "A file containing the training audio paths and labels."} ) lowerCamelCase : Optional[str] =field( default=UpperCAmelCase__ , metadata={"help": "A file containing the validation audio paths and labels."} ) lowerCamelCase : str =field( default="train" , metadata={ "help": "The name of the training data set split to use (via the datasets library). Defaults to 'train'" } , ) lowerCamelCase : str =field( default="validation" , metadata={ "help": ( "The name of the training data set split to use (via the datasets library). Defaults to 'validation'" ) } , ) lowerCamelCase : str =field( default="audio" , metadata={"help": "The name of the dataset column containing the audio data. Defaults to 'audio'"} , ) lowerCamelCase : str =field( default="label" , metadata={"help": "The name of the dataset column containing the labels. Defaults to 'label'"} ) lowerCamelCase : Optional[int] =field( default=UpperCAmelCase__ , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of training examples to this " "value if set." ) } , ) lowerCamelCase : Optional[int] =field( default=UpperCAmelCase__ , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of evaluation examples to this " "value if set." ) } , ) lowerCamelCase : float =field( default=2_0 , metadata={"help": "Audio clips will be randomly cut to this length during training if the value is set."} , ) @dataclass class a__ : lowerCamelCase : str =field( default="facebook/wav2vec2-base" , metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} , ) lowerCamelCase : Optional[str] =field( default=UpperCAmelCase__ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) lowerCamelCase : Optional[str] =field( default=UpperCAmelCase__ , metadata={"help": "Where do you want to store the pretrained models downloaded from the Hub"} ) lowerCamelCase : str =field( default="main" , metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."} , ) lowerCamelCase : Optional[str] =field( default=UpperCAmelCase__ , metadata={"help": "Name or path of preprocessor config."} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to freeze the feature encoder layers of the model."} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to generate an attention mask in the feature extractor."} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": ( "Will use the token generated when running `huggingface-cli login` (necessary to use this script " "with private models)." ) } , ) lowerCamelCase : Optional[bool] =field( default=UpperCAmelCase__ , metadata={"help": "Whether to freeze the feature extractor layers of the model."} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Will enable to load a pretrained model whose head dimensions are different."} , ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" if not self.freeze_feature_extractor and self.freeze_feature_encoder: warnings.warn( '''The argument `--freeze_feature_extractor` is deprecated and ''' '''will be removed in a future version. Use `--freeze_feature_encoder`''' '''instead. Setting `freeze_feature_encoder==True`.''' , a , ) if self.freeze_feature_extractor and not self.freeze_feature_encoder: raise ValueError( '''The argument `--freeze_feature_extractor` is deprecated and ''' '''should not be used in combination with `--freeze_feature_encoder`.''' '''Only make use of `--freeze_feature_encoder`.''' ) def __lowerCAmelCase ( ) -> Any: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. __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() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('''run_audio_classification''' , UpperCamelCase__ , UpperCamelCase__ ) # 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 )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() __lowerCamelCase = training_args.get_process_log_level() logger.setLevel(UpperCamelCase__ ) transformers.utils.logging.set_verbosity(UpperCamelCase__ ) 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}""" ) # Set seed before initializing model. set_seed(training_args.seed ) # 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 train from scratch.''' ) 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.''' ) # Initialize our dataset and prepare it for the audio classification task. __lowerCamelCase = DatasetDict() __lowerCamelCase = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.train_split_name , use_auth_token=True if model_args.use_auth_token else None , ) __lowerCamelCase = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.eval_split_name , use_auth_token=True if model_args.use_auth_token else None , ) if data_args.audio_column_name not in raw_datasets["train"].column_names: raise ValueError( f"""--audio_column_name {data_args.audio_column_name} not found in dataset '{data_args.dataset_name}'. """ '''Make sure to set `--audio_column_name` to the correct audio column - one of ''' f"""{', '.join(raw_datasets['train'].column_names )}.""" ) if data_args.label_column_name not in raw_datasets["train"].column_names: raise ValueError( f"""--label_column_name {data_args.label_column_name} not found in dataset '{data_args.dataset_name}'. """ '''Make sure to set `--label_column_name` to the correct text column - one of ''' f"""{', '.join(raw_datasets['train'].column_names )}.""" ) # Setting `return_attention_mask=True` is the way to get a correctly masked mean-pooling over # transformer outputs in the classifier, but it doesn't always lead to better accuracy __lowerCamelCase = AutoFeatureExtractor.from_pretrained( model_args.feature_extractor_name or model_args.model_name_or_path , return_attention_mask=model_args.attention_mask , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # `datasets` takes care of automatically loading and resampling the audio, # so we just need to set the correct target sampling rate. __lowerCamelCase = raw_datasets.cast_column( data_args.audio_column_name , datasets.features.Audio(sampling_rate=feature_extractor.sampling_rate ) ) __lowerCamelCase = feature_extractor.model_input_names[0] def train_transforms(UpperCamelCase__ ): __lowerCamelCase = [] for audio in batch[data_args.audio_column_name]: __lowerCamelCase = random_subsample( audio['''array'''] , max_length=data_args.max_length_seconds , sample_rate=feature_extractor.sampling_rate ) subsampled_wavs.append(UpperCamelCase__ ) __lowerCamelCase = feature_extractor(UpperCamelCase__ , sampling_rate=feature_extractor.sampling_rate ) __lowerCamelCase = {model_input_name: inputs.get(UpperCamelCase__ )} __lowerCamelCase = list(batch[data_args.label_column_name] ) return output_batch def val_transforms(UpperCamelCase__ ): __lowerCamelCase = [audio['''array'''] for audio in batch[data_args.audio_column_name]] __lowerCamelCase = feature_extractor(UpperCamelCase__ , sampling_rate=feature_extractor.sampling_rate ) __lowerCamelCase = {model_input_name: inputs.get(UpperCamelCase__ )} __lowerCamelCase = list(batch[data_args.label_column_name] ) return output_batch # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. __lowerCamelCase = raw_datasets['''train'''].features[data_args.label_column_name].names __lowerCamelCase , __lowerCamelCase = {}, {} for i, label in enumerate(UpperCamelCase__ ): __lowerCamelCase = str(UpperCamelCase__ ) __lowerCamelCase = label # Load the accuracy metric from the datasets package __lowerCamelCase = evaluate.load('''accuracy''' ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with # `predictions` and `label_ids` fields) and has to return a dictionary string to float. def compute_metrics(UpperCamelCase__ ): __lowerCamelCase = np.argmax(eval_pred.predictions , axis=1 ) return metric.compute(predictions=UpperCamelCase__ , references=eval_pred.label_ids ) __lowerCamelCase = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path , num_labels=len(UpperCamelCase__ ) , labelaid=UpperCamelCase__ , idalabel=UpperCamelCase__ , finetuning_task='''audio-classification''' , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) __lowerCamelCase = AutoModelForAudioClassification.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 , ignore_mismatched_sizes=model_args.ignore_mismatched_sizes , ) # freeze the convolutional waveform encoder if model_args.freeze_feature_encoder: model.freeze_feature_encoder() if training_args.do_train: if data_args.max_train_samples is not None: __lowerCamelCase = ( raw_datasets['''train'''].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms raw_datasets["train"].set_transform(UpperCamelCase__ , output_all_columns=UpperCamelCase__ ) if training_args.do_eval: if data_args.max_eval_samples is not None: __lowerCamelCase = ( raw_datasets['''eval'''].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms raw_datasets["eval"].set_transform(UpperCamelCase__ , output_all_columns=UpperCamelCase__ ) # Initialize our trainer __lowerCamelCase = Trainer( model=UpperCamelCase__ , args=UpperCamelCase__ , train_dataset=raw_datasets['''train'''] if training_args.do_train else None , eval_dataset=raw_datasets['''eval'''] if training_args.do_eval else None , compute_metrics=UpperCamelCase__ , tokenizer=UpperCamelCase__ , ) # 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=UpperCamelCase__ ) trainer.save_model() trainer.log_metrics('''train''' , train_result.metrics ) trainer.save_metrics('''train''' , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: __lowerCamelCase = trainer.evaluate() trainer.log_metrics('''eval''' , UpperCamelCase__ ) trainer.save_metrics('''eval''' , UpperCamelCase__ ) # Write model card and (optionally) push to hub __lowerCamelCase = { '''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''audio-classification''', '''dataset''': data_args.dataset_name, '''tags''': ['''audio-classification'''], } if training_args.push_to_hub: trainer.push_to_hub(**UpperCamelCase__ ) else: trainer.create_model_card(**UpperCamelCase__ ) if __name__ == "__main__": main()
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[Any]: __lowerCamelCase = [] __lowerCamelCase = set({'''(''', '''[''', '''{'''} ) __lowerCamelCase = set({''')''', ''']''', '''}'''} ) __lowerCamelCase = {'''{''': '''}''', '''[''': ''']''', '''(''': ''')'''} for i in range(len(UpperCamelCase__ ) ): if s[i] in open_brackets: stack.append(s[i] ) elif s[i] in closed_brackets and ( len(UpperCamelCase__ ) == 0 or (len(UpperCamelCase__ ) > 0 and open_to_closed[stack.pop()] != s[i]) ): return False return len(UpperCamelCase__ ) == 0 def __lowerCAmelCase ( ) -> str: __lowerCamelCase = input('''Enter sequence of brackets: ''' ) if is_balanced(UpperCamelCase__ ): print(UpperCamelCase__ , '''is balanced''' ) else: print(UpperCamelCase__ , '''is not balanced''' ) if __name__ == "__main__": main()
67
1
'''simple docstring''' # Copyright (c) 2021-, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #################################################################################################### # # Note: If when running this conversion script you're getting an exception: # ModuleNotFoundError: No module named 'megatron.model.enums' # you need to tell python where to find the clone of Megatron-LM, e.g.: # # cd /tmp # git clone https://github.com/NVIDIA/Megatron-LM # PYTHONPATH=/tmp/Megatron-LM python src/transformers/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py ... # # if you already have it cloned elsewhere, simply adjust the path to the existing path # # If the training was done using a Megatron-LM fork, e.g., # https://github.com/microsoft/Megatron-DeepSpeed/ then chances are that you need to have that one # in your path, i.e., /path/to/Megatron-DeepSpeed/ # import argparse import os import re import zipfile import torch from transformers import AutoTokenizer, GPTaConfig def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=0 ) -> str: # Format the message. if name is None: __lowerCamelCase = None else: __lowerCamelCase = '''.''' * max(0 , spaces - 2 ) + '''# {:''' + str(50 - spaces ) + '''s}''' __lowerCamelCase = fmt.format(UpperCamelCase__ ) # Print and recurse (if needed). if isinstance(UpperCamelCase__ , UpperCamelCase__ ): if msg is not None: print(UpperCamelCase__ ) for k in val.keys(): recursive_print(UpperCamelCase__ , val[k] , spaces + 2 ) elif isinstance(UpperCamelCase__ , torch.Tensor ): print(UpperCamelCase__ , ''':''' , val.size() ) else: print(UpperCamelCase__ , ''':''' , UpperCamelCase__ ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Union[str, Any]: # Permutes layout of param tensor to [num_splits * num_heads * hidden_size, :] # for compatibility with later versions of NVIDIA Megatron-LM. # The inverse operation is performed inside Megatron-LM to read checkpoints: # https://github.com/NVIDIA/Megatron-LM/blob/v2.4/megatron/checkpointing.py#L209 # If param is the weight tensor of the self-attention block, the returned tensor # will have to be transposed one more time to be read by HuggingFace GPT2. __lowerCamelCase = param.size() if checkpoint_version == 1.0: # version 1.0 stores [num_heads * hidden_size * num_splits, :] __lowerCamelCase = (num_heads, hidden_size, num_splits) + input_shape[1:] __lowerCamelCase = param.view(*UpperCamelCase__ ) __lowerCamelCase = param.transpose(0 , 2 ) __lowerCamelCase = param.transpose(1 , 2 ).contiguous() elif checkpoint_version >= 2.0: # other versions store [num_heads * num_splits * hidden_size, :] __lowerCamelCase = (num_heads, num_splits, hidden_size) + input_shape[1:] __lowerCamelCase = param.view(*UpperCamelCase__ ) __lowerCamelCase = param.transpose(0 , 1 ).contiguous() __lowerCamelCase = param.view(*UpperCamelCase__ ) return param def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Any: # The converted output model. __lowerCamelCase = {} # old versions did not store training args __lowerCamelCase = input_state_dict.get('''args''' , UpperCamelCase__ ) if ds_args is not None: # do not make the user write a config file when the exact dimensions/sizes are already in the checkpoint # from pprint import pprint # pprint(vars(ds_args)) __lowerCamelCase = ds_args.padded_vocab_size __lowerCamelCase = ds_args.max_position_embeddings __lowerCamelCase = ds_args.hidden_size __lowerCamelCase = ds_args.num_layers __lowerCamelCase = ds_args.num_attention_heads __lowerCamelCase = ds_args.ffn_hidden_size # pprint(config) # The number of heads. __lowerCamelCase = config.n_head # The hidden_size per head. __lowerCamelCase = config.n_embd // config.n_head # Megatron-LM checkpoint version if "checkpoint_version" in input_state_dict.keys(): __lowerCamelCase = input_state_dict['''checkpoint_version'''] else: __lowerCamelCase = 0.0 # The model. __lowerCamelCase = input_state_dict['''model'''] # The language model. __lowerCamelCase = model['''language_model'''] # The embeddings. __lowerCamelCase = lm['''embedding'''] # The word embeddings. __lowerCamelCase = embeddings['''word_embeddings''']['''weight'''] # Truncate the embedding table to vocab_size rows. __lowerCamelCase = word_embeddings[: config.vocab_size, :] __lowerCamelCase = word_embeddings # The position embeddings. __lowerCamelCase = embeddings['''position_embeddings''']['''weight'''] # Read the causal mask dimension (seqlen). [max_sequence_length, hidden_size] __lowerCamelCase = pos_embeddings.size(0 ) if n_positions != config.n_positions: raise ValueError( f"""pos_embeddings.max_sequence_length={n_positions} and config.n_positions={config.n_positions} don't match""" ) # Store the position embeddings. __lowerCamelCase = pos_embeddings # The transformer. __lowerCamelCase = lm['''transformer'''] if '''transformer''' in lm.keys() else lm['''encoder'''] # The regex to extract layer names. __lowerCamelCase = re.compile(r'''layers\.(\d+)\.([a-z0-9_.]+)\.([a-z]+)''' ) # The simple map of names for "automated" rules. __lowerCamelCase = { '''attention.dense''': '''.attn.c_proj.''', '''self_attention.dense''': '''.attn.c_proj.''', '''mlp.dense_h_to_4h''': '''.mlp.c_fc.''', '''mlp.dense_4h_to_h''': '''.mlp.c_proj.''', } # Extract the layers. for key, val in transformer.items(): # Match the name. __lowerCamelCase = layer_re.match(UpperCamelCase__ ) # Stop if that's not a layer if m is None: break # The index of the layer. __lowerCamelCase = int(m.group(1 ) ) # The name of the operation. __lowerCamelCase = m.group(2 ) # Is it a weight or a bias? __lowerCamelCase = m.group(3 ) # The name of the layer. __lowerCamelCase = f"""transformer.h.{layer_idx}""" # For layernorm(s), simply store the layer norm. if op_name.endswith('''layernorm''' ): __lowerCamelCase = '''ln_1''' if op_name.startswith('''input''' ) else '''ln_2''' __lowerCamelCase = val # Transpose the QKV matrix. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "weight": # Insert a tensor of 1x1xDxD bias. __lowerCamelCase = torch.tril(torch.ones((n_positions, n_positions) , dtype=torch.floataa ) ).view( 1 , 1 , UpperCamelCase__ , UpperCamelCase__ ) __lowerCamelCase = causal_mask # Insert a "dummy" tensor for masked_bias. __lowerCamelCase = torch.tensor(-1E4 , dtype=torch.floataa ) __lowerCamelCase = masked_bias __lowerCamelCase = fix_query_key_value_ordering(UpperCamelCase__ , UpperCamelCase__ , 3 , UpperCamelCase__ , UpperCamelCase__ ) # Megatron stores (3*D) x D but transformers-GPT2 expects D x 3*D. __lowerCamelCase = out_val.transpose(0 , 1 ).contiguous() # Store. __lowerCamelCase = out_val # Transpose the bias. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "bias": __lowerCamelCase = fix_query_key_value_ordering(UpperCamelCase__ , UpperCamelCase__ , 3 , UpperCamelCase__ , UpperCamelCase__ ) # Store. No change of shape. __lowerCamelCase = out_val # Transpose the weights. elif weight_or_bias == "weight": __lowerCamelCase = megatron_to_transformers[op_name] __lowerCamelCase = val.transpose(0 , 1 ) # Copy the bias. elif weight_or_bias == "bias": __lowerCamelCase = megatron_to_transformers[op_name] __lowerCamelCase = val # DEBUG. assert config.n_layer == layer_idx + 1 # The final layernorm. __lowerCamelCase = transformer['''final_layernorm.weight'''] __lowerCamelCase = transformer['''final_layernorm.bias'''] # For LM head, transformers' wants the matrix to weight embeddings. __lowerCamelCase = word_embeddings # It should be done! return output_state_dict def __lowerCAmelCase ( ) -> Optional[int]: # Create the argument parser. __lowerCamelCase = argparse.ArgumentParser() parser.add_argument('''--print-checkpoint-structure''' , action='''store_true''' ) parser.add_argument( '''path_to_checkpoint''' , type=UpperCamelCase__ , help='''Path to the checkpoint file (.zip archive or direct .pt file)''' , ) parser.add_argument( '''--config_file''' , default='''''' , type=UpperCamelCase__ , help='''An optional config json file describing the pre-trained model.''' , ) __lowerCamelCase = parser.parse_args() # Extract the basename. __lowerCamelCase = os.path.dirname(args.path_to_checkpoint ) # Load the model. # the .zip is very optional, let's keep it for backward compatibility print(f"""Extracting PyTorch state dictionary from {args.path_to_checkpoint}""" ) if args.path_to_checkpoint.endswith('''.zip''' ): with zipfile.ZipFile(args.path_to_checkpoint , '''r''' ) as checkpoint: with checkpoint.open('''release/mp_rank_00/model_optim_rng.pt''' ) as pytorch_dict: __lowerCamelCase = torch.load(UpperCamelCase__ , map_location='''cpu''' ) else: __lowerCamelCase = torch.load(args.path_to_checkpoint , map_location='''cpu''' ) __lowerCamelCase = input_state_dict.get('''args''' , UpperCamelCase__ ) # Read the config, or default to the model released by NVIDIA. if args.config_file == "": if ds_args is not None: if ds_args.bias_gelu_fusion: __lowerCamelCase = '''gelu_fast''' elif ds_args.openai_gelu: __lowerCamelCase = '''gelu_new''' else: __lowerCamelCase = '''gelu''' else: # in the very early days this used to be "gelu_new" __lowerCamelCase = '''gelu_new''' # Spell out all parameters in case the defaults change. __lowerCamelCase = GPTaConfig( vocab_size=5_02_57 , n_positions=10_24 , n_embd=10_24 , n_layer=24 , n_head=16 , n_inner=40_96 , activation_function=UpperCamelCase__ , resid_pdrop=0.1 , embd_pdrop=0.1 , attn_pdrop=0.1 , layer_norm_epsilon=1E-5 , initializer_range=0.0_2 , summary_type='''cls_index''' , summary_use_proj=UpperCamelCase__ , summary_activation=UpperCamelCase__ , summary_proj_to_labels=UpperCamelCase__ , summary_first_dropout=0.1 , scale_attn_weights=UpperCamelCase__ , use_cache=UpperCamelCase__ , bos_token_id=5_02_56 , eos_token_id=5_02_56 , ) else: __lowerCamelCase = GPTaConfig.from_json_file(args.config_file ) __lowerCamelCase = ['''GPT2LMHeadModel'''] # Convert. print('''Converting''' ) __lowerCamelCase = convert_megatron_checkpoint(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # Print the structure of converted state dict. if args.print_checkpoint_structure: recursive_print(UpperCamelCase__ , UpperCamelCase__ ) # Add tokenizer class info to config # see https://github.com/huggingface/transformers/issues/13906) if ds_args is not None: __lowerCamelCase = ds_args.tokenizer_type if tokenizer_type == "GPT2BPETokenizer": __lowerCamelCase = '''gpt2''' elif tokenizer_type == "PretrainedFromHF": __lowerCamelCase = ds_args.tokenizer_name_or_path else: raise ValueError(f"""Unrecognized tokenizer_type {tokenizer_type}""" ) else: __lowerCamelCase = '''gpt2''' __lowerCamelCase = AutoTokenizer.from_pretrained(UpperCamelCase__ ) __lowerCamelCase = type(UpperCamelCase__ ).__name__ __lowerCamelCase = tokenizer_class # Store the config to file. print('''Saving config''' ) config.save_pretrained(UpperCamelCase__ ) # Save tokenizer based on args print(f"""Adding {tokenizer_class} tokenizer files""" ) tokenizer.save_pretrained(UpperCamelCase__ ) # Store the state_dict to file. __lowerCamelCase = os.path.join(UpperCamelCase__ , '''pytorch_model.bin''' ) print(f"""Saving checkpoint to \"{output_checkpoint_file}\"""" ) torch.save(UpperCamelCase__ , UpperCamelCase__ ) #################################################################################################### if __name__ == "__main__": main() ####################################################################################################
67
'''simple docstring''' import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class a__ ( UpperCAmelCase__ ): lowerCamelCase : Dict ="M-CLIP" def __init__( self : Tuple , a : Optional[int]=10_24 , a : Tuple=7_68 , **a : List[str] ): """simple docstring""" __lowerCamelCase = transformerDimSize __lowerCamelCase = imageDimSize super().__init__(**a ) class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[Any] =MCLIPConfig def __init__( self : str , a : List[Any] , *a : Dict , **a : str ): """simple docstring""" super().__init__(a , *a , **a ) __lowerCamelCase = XLMRobertaModel(a ) __lowerCamelCase = torch.nn.Linear( in_features=config.transformerDimensions , out_features=config.numDims ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : int , a : List[Any] ): """simple docstring""" __lowerCamelCase = self.transformer(input_ids=a , attention_mask=a )[0] __lowerCamelCase = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(a ), embs
67
1
'''simple docstring''' from collections import Counter import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split __UpperCAmelCase =datasets.load_iris() __UpperCAmelCase =np.array(data["data"]) __UpperCAmelCase =np.array(data["target"]) __UpperCAmelCase =data["target_names"] __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase =train_test_split(X, y) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Tuple: return np.linalg.norm(np.array(UpperCamelCase__ ) - np.array(UpperCamelCase__ ) ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=5 ) -> int: __lowerCamelCase = zip(UpperCamelCase__ , UpperCamelCase__ ) # List of distances of all points from the point to be classified __lowerCamelCase = [] for data_point in data: __lowerCamelCase = euclidean_distance(data_point[0] , UpperCamelCase__ ) distances.append((distance, data_point[1]) ) # Choosing 'k' points with the least distances. __lowerCamelCase = [i[1] for i in sorted(UpperCamelCase__ )[:k]] # Most commonly occurring class among them # is the class into which the point is classified __lowerCamelCase = Counter(UpperCamelCase__ ).most_common(1 )[0][0] return classes[result] if __name__ == "__main__": print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))
67
'''simple docstring''' from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
67
1
'''simple docstring''' from collections.abc import Sequence from queue import Queue class a__ : def __init__( self : str , a : Any , a : Optional[int] , a : Tuple , a : Union[str, Any]=None , a : int=None ): """simple docstring""" __lowerCamelCase = start __lowerCamelCase = end __lowerCamelCase = val __lowerCamelCase = (start + end) // 2 __lowerCamelCase = left __lowerCamelCase = right def __repr__( self : int ): """simple docstring""" return f"""SegmentTreeNode(start={self.start}, end={self.end}, val={self.val})""" class a__ : def __init__( self : Dict , a : Sequence , a : Optional[Any] ): """simple docstring""" __lowerCamelCase = collection __lowerCamelCase = function if self.collection: __lowerCamelCase = self._build_tree(0 , len(a ) - 1 ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : Union[str, Any] , a : str ): """simple docstring""" self._update_tree(self.root , a , a ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : List[Any] , a : Tuple ): """simple docstring""" return self._query_range(self.root , a , a ) def SCREAMING_SNAKE_CASE__ ( self : str , a : Tuple , a : str ): """simple docstring""" if start == end: return SegmentTreeNode(a , a , self.collection[start] ) __lowerCamelCase = (start + end) // 2 __lowerCamelCase = self._build_tree(a , a ) __lowerCamelCase = self._build_tree(mid + 1 , a ) return SegmentTreeNode(a , a , self.fn(left.val , right.val ) , a , a ) def SCREAMING_SNAKE_CASE__ ( self : str , a : str , a : Optional[Any] , a : int ): """simple docstring""" if node.start == i and node.end == i: __lowerCamelCase = val return if i <= node.mid: self._update_tree(node.left , a , a ) else: self._update_tree(node.right , a , a ) __lowerCamelCase = self.fn(node.left.val , node.right.val ) def SCREAMING_SNAKE_CASE__ ( self : List[str] , a : Union[str, Any] , a : str , a : str ): """simple docstring""" if node.start == i and node.end == j: return node.val if i <= node.mid: if j <= node.mid: # range in left child tree return self._query_range(node.left , a , a ) else: # range in left child tree and right child tree return self.fn( self._query_range(node.left , a , node.mid ) , self._query_range(node.right , node.mid + 1 , a ) , ) else: # range in right child tree return self._query_range(node.right , a , a ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" if self.root is not None: __lowerCamelCase = Queue() queue.put(self.root ) while not queue.empty(): __lowerCamelCase = queue.get() yield node if node.left is not None: queue.put(node.left ) if node.right is not None: queue.put(node.right ) if __name__ == "__main__": import operator for fn in [operator.add, max, min]: print("*" * 5_0) __UpperCAmelCase =SegmentTree([2, 1, 5, 3, 4], fn) for node in arr.traverse(): print(node) print() arr.update(1, 5) for node in arr.traverse(): print(node) print() print(arr.query_range(3, 4)) # 7 print(arr.query_range(2, 2)) # 5 print(arr.query_range(1, 3)) # 13 print()
67
'''simple docstring''' from __future__ import annotations from decimal import Decimal from numpy import array def __lowerCAmelCase ( UpperCamelCase__ ) -> list[list[float]]: __lowerCamelCase = Decimal # Check if the provided matrix has 2 rows and 2 columns # since this implementation only works for 2x2 matrices if len(UpperCamelCase__ ) == 2 and len(matrix[0] ) == 2 and len(matrix[1] ) == 2: # Calculate the determinant of the matrix __lowerCamelCase = float( d(matrix[0][0] ) * d(matrix[1][1] ) - d(matrix[1][0] ) * d(matrix[0][1] ) ) if determinant == 0: raise ValueError('''This matrix has no inverse.''' ) # Creates a copy of the matrix with swapped positions of the elements __lowerCamelCase = [[0.0, 0.0], [0.0, 0.0]] __lowerCamelCase , __lowerCamelCase = matrix[1][1], matrix[0][0] __lowerCamelCase , __lowerCamelCase = -matrix[1][0], -matrix[0][1] # Calculate the inverse of the matrix return [ [(float(d(UpperCamelCase__ ) ) / determinant) or 0.0 for n in row] for row in swapped_matrix ] elif ( len(UpperCamelCase__ ) == 3 and len(matrix[0] ) == 3 and len(matrix[1] ) == 3 and len(matrix[2] ) == 3 ): # Calculate the determinant of the matrix using Sarrus rule __lowerCamelCase = float( ( (d(matrix[0][0] ) * d(matrix[1][1] ) * d(matrix[2][2] )) + (d(matrix[0][1] ) * d(matrix[1][2] ) * d(matrix[2][0] )) + (d(matrix[0][2] ) * d(matrix[1][0] ) * d(matrix[2][1] )) ) - ( (d(matrix[0][2] ) * d(matrix[1][1] ) * d(matrix[2][0] )) + (d(matrix[0][1] ) * d(matrix[1][0] ) * d(matrix[2][2] )) + (d(matrix[0][0] ) * d(matrix[1][2] ) * d(matrix[2][1] )) ) ) if determinant == 0: raise ValueError('''This matrix has no inverse.''' ) # Creating cofactor matrix __lowerCamelCase = [ [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], ] __lowerCamelCase = (d(matrix[1][1] ) * d(matrix[2][2] )) - ( d(matrix[1][2] ) * d(matrix[2][1] ) ) __lowerCamelCase = -( (d(matrix[1][0] ) * d(matrix[2][2] )) - (d(matrix[1][2] ) * d(matrix[2][0] )) ) __lowerCamelCase = (d(matrix[1][0] ) * d(matrix[2][1] )) - ( d(matrix[1][1] ) * d(matrix[2][0] ) ) __lowerCamelCase = -( (d(matrix[0][1] ) * d(matrix[2][2] )) - (d(matrix[0][2] ) * d(matrix[2][1] )) ) __lowerCamelCase = (d(matrix[0][0] ) * d(matrix[2][2] )) - ( d(matrix[0][2] ) * d(matrix[2][0] ) ) __lowerCamelCase = -( (d(matrix[0][0] ) * d(matrix[2][1] )) - (d(matrix[0][1] ) * d(matrix[2][0] )) ) __lowerCamelCase = (d(matrix[0][1] ) * d(matrix[1][2] )) - ( d(matrix[0][2] ) * d(matrix[1][1] ) ) __lowerCamelCase = -( (d(matrix[0][0] ) * d(matrix[1][2] )) - (d(matrix[0][2] ) * d(matrix[1][0] )) ) __lowerCamelCase = (d(matrix[0][0] ) * d(matrix[1][1] )) - ( d(matrix[0][1] ) * d(matrix[1][0] ) ) # Transpose the cofactor matrix (Adjoint matrix) __lowerCamelCase = array(UpperCamelCase__ ) for i in range(3 ): for j in range(3 ): __lowerCamelCase = cofactor_matrix[j][i] # Inverse of the matrix using the formula (1/determinant) * adjoint matrix __lowerCamelCase = array(UpperCamelCase__ ) for i in range(3 ): for j in range(3 ): inverse_matrix[i][j] /= d(UpperCamelCase__ ) # Calculate the inverse of the matrix return [[float(d(UpperCamelCase__ ) ) or 0.0 for n in row] for row in inverse_matrix] raise ValueError('''Please provide a matrix of size 2x2 or 3x3.''' )
67
1
'''simple docstring''' import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class a__ ( UpperCAmelCase__ ): def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = tempfile.mkdtemp() __lowerCamelCase = 8 # DPR tok __lowerCamelCase = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] __lowerCamelCase = os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(a , exist_ok=a ) __lowerCamelCase = os.path.join(a , DPR_VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) # BART tok __lowerCamelCase = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] __lowerCamelCase = dict(zip(a , range(len(a ) ) ) ) __lowerCamelCase = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] __lowerCamelCase = {'''unk_token''': '''<unk>'''} __lowerCamelCase = os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(a , exist_ok=a ) __lowerCamelCase = os.path.join(a , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) __lowerCamelCase = os.path.join(a , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(a ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(a ) ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) @require_tokenizers def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = os.path.join(self.tmpdirname , '''rag_tokenizer''' ) __lowerCamelCase = RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) __lowerCamelCase = RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(a ) rag_tokenizer.save_pretrained(a ) __lowerCamelCase = RagTokenizer.from_pretrained(a , config=a ) self.assertIsInstance(new_rag_tokenizer.question_encoder , a ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , a ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = RagTokenizer.from_pretrained('''facebook/rag-token-nq''' ) __lowerCamelCase = [ '''who got the first nobel prize in physics''', '''when is the next deadpool movie being released''', '''which mode is used for short wave broadcast service''', '''who is the owner of reading football club''', '''when is the next scandal episode coming out''', '''when is the last time the philadelphia won the superbowl''', '''what is the most current adobe flash player version''', '''how many episodes are there in dragon ball z''', '''what is the first step in the evolution of the eye''', '''where is gall bladder situated in human body''', '''what is the main mineral in lithium batteries''', '''who is the president of usa right now''', '''where do the greasers live in the outsiders''', '''panda is a national animal of which country''', '''what is the name of manchester united stadium''', ] __lowerCamelCase = tokenizer(a ) self.assertIsNotNone(a ) @slow def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = RagTokenizer.from_pretrained('''facebook/rag-sequence-nq''' ) __lowerCamelCase = [ '''who got the first nobel prize in physics''', '''when is the next deadpool movie being released''', '''which mode is used for short wave broadcast service''', '''who is the owner of reading football club''', '''when is the next scandal episode coming out''', '''when is the last time the philadelphia won the superbowl''', '''what is the most current adobe flash player version''', '''how many episodes are there in dragon ball z''', '''what is the first step in the evolution of the eye''', '''where is gall bladder situated in human body''', '''what is the main mineral in lithium batteries''', '''who is the president of usa right now''', '''where do the greasers live in the outsiders''', '''panda is a national animal of which country''', '''what is the name of manchester united stadium''', ] __lowerCamelCase = tokenizer(a ) self.assertIsNotNone(a )
67
'''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 __UpperCAmelCase =logging.get_logger(__name__) def __lowerCAmelCase ( UpperCamelCase__=None , UpperCamelCase__=None ) -> int: return field(default_factory=lambda: default , metadata=UpperCamelCase__ ) @dataclass class a__ : lowerCamelCase : List[str] =list_field( default=[] , metadata={ "help": ( "Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version" " of all available models" ) } , ) lowerCamelCase : List[int] =list_field( default=[8] , metadata={"help": "List of batch sizes for which memory and time performance will be evaluated"} ) lowerCamelCase : List[int] =list_field( default=[8, 3_2, 1_2_8, 5_1_2] , metadata={"help": "List of sequence lengths for which memory and time performance will be evaluated"} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to benchmark inference of model. Inference can be disabled via --no-inference."} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to run on available cuda devices. Cuda can be disabled via --no-cuda."} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to run on available tpu devices. TPU can be disabled via --no-tpu."} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Use FP16 to accelerate inference."} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Benchmark training of model"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Verbose memory tracing"} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to perform speed measurements. Speed measurements can be disabled via --no-speed."} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": "Whether to perform memory measurements. Memory measurements can be disabled via --no-memory" } , ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Trace memory line by line"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Save result to a CSV file"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Save all print statements in a log file"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Whether to print environment information"} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": ( "Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use" " multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled" " for debugging / testing and on TPU." ) } , ) lowerCamelCase : str =field( default=F'''inference_time_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving time results to csv."} , ) lowerCamelCase : str =field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving memory results to csv."} , ) lowerCamelCase : str =field( default=F'''train_time_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving time results to csv for training."} , ) lowerCamelCase : str =field( default=F'''train_memory_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving memory results to csv for training."} , ) lowerCamelCase : str =field( default=F'''env_info_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving environment information."} , ) lowerCamelCase : str =field( default=F'''log_{round(time() )}.csv''' , metadata={"help": "Log filename used if print statements are saved in log."} , ) lowerCamelCase : int =field(default=3 , metadata={"help": "Times an experiment will be run."} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": ( "Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain" " model weights." ) } , ) def SCREAMING_SNAKE_CASE__ ( self : 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.''' , a , ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """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 SCREAMING_SNAKE_CASE__ ( 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
67
1
'''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 __UpperCAmelCase =logging.get_logger(__name__) def __lowerCAmelCase ( UpperCamelCase__=None , UpperCamelCase__=None ) -> int: return field(default_factory=lambda: default , metadata=UpperCamelCase__ ) @dataclass class a__ : lowerCamelCase : List[str] =list_field( default=[] , metadata={ "help": ( "Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version" " of all available models" ) } , ) lowerCamelCase : List[int] =list_field( default=[8] , metadata={"help": "List of batch sizes for which memory and time performance will be evaluated"} ) lowerCamelCase : List[int] =list_field( default=[8, 3_2, 1_2_8, 5_1_2] , metadata={"help": "List of sequence lengths for which memory and time performance will be evaluated"} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to benchmark inference of model. Inference can be disabled via --no-inference."} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to run on available cuda devices. Cuda can be disabled via --no-cuda."} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to run on available tpu devices. TPU can be disabled via --no-tpu."} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Use FP16 to accelerate inference."} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Benchmark training of model"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Verbose memory tracing"} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to perform speed measurements. Speed measurements can be disabled via --no-speed."} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": "Whether to perform memory measurements. Memory measurements can be disabled via --no-memory" } , ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Trace memory line by line"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Save result to a CSV file"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Save all print statements in a log file"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Whether to print environment information"} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": ( "Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use" " multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled" " for debugging / testing and on TPU." ) } , ) lowerCamelCase : str =field( default=F'''inference_time_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving time results to csv."} , ) lowerCamelCase : str =field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving memory results to csv."} , ) lowerCamelCase : str =field( default=F'''train_time_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving time results to csv for training."} , ) lowerCamelCase : str =field( default=F'''train_memory_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving memory results to csv for training."} , ) lowerCamelCase : str =field( default=F'''env_info_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving environment information."} , ) lowerCamelCase : str =field( default=F'''log_{round(time() )}.csv''' , metadata={"help": "Log filename used if print statements are saved in log."} , ) lowerCamelCase : int =field(default=3 , metadata={"help": "Times an experiment will be run."} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": ( "Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain" " model weights." ) } , ) def SCREAMING_SNAKE_CASE__ ( self : 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.''' , a , ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """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 SCREAMING_SNAKE_CASE__ ( 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
67
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_barthez import BarthezTokenizer else: __UpperCAmelCase =None __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase ={ "vocab_file": { "moussaKam/mbarthez": "https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model", "moussaKam/barthez": "https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model", "moussaKam/barthez-orangesum-title": ( "https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "moussaKam/mbarthez": "https://huggingface.co/moussaKam/mbarthez/resolve/main/tokenizer.json", "moussaKam/barthez": "https://huggingface.co/moussaKam/barthez/resolve/main/tokenizer.json", "moussaKam/barthez-orangesum-title": ( "https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/tokenizer.json" ), }, } __UpperCAmelCase ={ "moussaKam/mbarthez": 1_0_2_4, "moussaKam/barthez": 1_0_2_4, "moussaKam/barthez-orangesum-title": 1_0_2_4, } __UpperCAmelCase ="▁" class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] =VOCAB_FILES_NAMES lowerCamelCase : Optional[Any] =PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : int =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[Any] =["input_ids", "attention_mask"] lowerCamelCase : Union[str, Any] =BarthezTokenizer def __init__( self : Optional[Any] , a : Dict=None , a : str=None , a : List[Any]="<s>" , a : Optional[int]="</s>" , a : List[str]="</s>" , a : Tuple="<s>" , a : str="<unk>" , a : Any="<pad>" , a : Union[str, Any]="<mask>" , **a : Union[str, Any] , ): """simple docstring""" __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else mask_token super().__init__( a , tokenizer_file=a , bos_token=a , eos_token=a , unk_token=a , sep_token=a , cls_token=a , pad_token=a , mask_token=a , **a , ) __lowerCamelCase = vocab_file __lowerCamelCase = False if not self.vocab_file else True def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __lowerCamelCase = [self.cls_token_id] __lowerCamelCase = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" __lowerCamelCase = [self.sep_token_id] __lowerCamelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : str , a : Optional[str] = None ): """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return __lowerCamelCase = os.path.join( a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(a ): copyfile(self.vocab_file , a ) return (out_vocab_file,)
67
1
'''simple docstring''' import argparse import torch from torch import nn from transformers import MBartConfig, MBartForConditionalGeneration def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[int]: __lowerCamelCase = [ '''encoder.version''', '''decoder.version''', '''model.encoder.version''', '''model.decoder.version''', '''_float_tensor''', '''decoder.output_projection.weight''', ] for k in ignore_keys: state_dict.pop(UpperCamelCase__ , UpperCamelCase__ ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Dict: __lowerCamelCase , __lowerCamelCase = emb.weight.shape __lowerCamelCase = nn.Linear(UpperCamelCase__ , UpperCamelCase__ , bias=UpperCamelCase__ ) __lowerCamelCase = emb.weight.data return lin_layer def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__="facebook/mbart-large-en-ro" , UpperCamelCase__=False , UpperCamelCase__=False ) -> List[str]: __lowerCamelCase = torch.load(UpperCamelCase__ , map_location='''cpu''' )['''model'''] remove_ignore_keys_(UpperCamelCase__ ) __lowerCamelCase = state_dict['''encoder.embed_tokens.weight'''].shape[0] __lowerCamelCase = MBartConfig.from_pretrained(UpperCamelCase__ , vocab_size=UpperCamelCase__ ) if mbart_aa and finetuned: __lowerCamelCase = '''relu''' __lowerCamelCase = state_dict['''decoder.embed_tokens.weight'''] __lowerCamelCase = MBartForConditionalGeneration(UpperCamelCase__ ) model.model.load_state_dict(UpperCamelCase__ ) if finetuned: __lowerCamelCase = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": __UpperCAmelCase =argparse.ArgumentParser() # Required parameters parser.add_argument( "fairseq_path", type=str, help="bart.large, bart.large.cnn or a path to a model.pt on local filesystem." ) parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument( "--hf_config", default="facebook/mbart-large-cc25", type=str, help="Which huggingface architecture to use: mbart-large", ) parser.add_argument("--mbart_50", action="store_true", help="whether the model is mMART-50 checkpoint") parser.add_argument("--finetuned", action="store_true", help="whether the model is a fine-tuned checkpoint") __UpperCAmelCase =parser.parse_args() __UpperCAmelCase =convert_fairseq_mbart_checkpoint_from_disk( args.fairseq_path, hf_config_path=args.hf_config, finetuned=args.finetuned, mbart_aa=args.mbart_aa ) model.save_pretrained(args.pytorch_dump_folder_path)
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ = 1_00_00_00 ) -> int: __lowerCamelCase = set(range(3 , UpperCamelCase__ , 2 ) ) primes.add(2 ) for p in range(3 , UpperCamelCase__ , 2 ): if p not in primes: continue primes.difference_update(set(range(p * p , UpperCamelCase__ , UpperCamelCase__ ) ) ) __lowerCamelCase = [float(UpperCamelCase__ ) for n in range(limit + 1 )] for p in primes: for n in range(UpperCamelCase__ , limit + 1 , UpperCamelCase__ ): phi[n] *= 1 - 1 / p return int(sum(phi[2:] ) ) if __name__ == "__main__": print(f'{solution() = }')
67
1
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPanoramaPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() @skip_mps class a__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): lowerCamelCase : int =StableDiffusionPanoramaPipeline lowerCamelCase : Dict =TEXT_TO_IMAGE_PARAMS lowerCamelCase : Union[str, Any] =TEXT_TO_IMAGE_BATCH_PARAMS lowerCamelCase : Dict =TEXT_TO_IMAGE_IMAGE_PARAMS lowerCamelCase : int =TEXT_TO_IMAGE_IMAGE_PARAMS def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" torch.manual_seed(0 ) __lowerCamelCase = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=1 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) __lowerCamelCase = DDIMScheduler() torch.manual_seed(0 ) __lowerCamelCase = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) __lowerCamelCase = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) __lowerCamelCase = CLIPTextModel(a ) __lowerCamelCase = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __lowerCamelCase = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def SCREAMING_SNAKE_CASE__ ( self : List[str] , a : Dict , a : Union[str, Any]=0 ): """simple docstring""" __lowerCamelCase = torch.manual_seed(a ) __lowerCamelCase = { '''prompt''': '''a photo of the dolomites''', '''generator''': generator, # Setting height and width to None to prevent OOMs on CPU. '''height''': None, '''width''': None, '''num_inference_steps''': 1, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = '''cpu''' # ensure determinism for the device-dependent torch.Generator __lowerCamelCase = self.get_dummy_components() __lowerCamelCase = StableDiffusionPanoramaPipeline(**a ) __lowerCamelCase = sd_pipe.to(a ) sd_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = self.get_dummy_inputs(a ) __lowerCamelCase = sd_pipe(**a ).images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __lowerCamelCase = np.array([0.61_86, 0.53_74, 0.49_15, 0.41_35, 0.41_14, 0.45_63, 0.51_28, 0.49_77, 0.47_57] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" super().test_inference_batch_consistent(batch_sizes=[1, 2] ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" super().test_inference_batch_single_identical(batch_size=2 , expected_max_diff=3.2_5e-3 ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = '''cpu''' # ensure determinism for the device-dependent torch.Generator __lowerCamelCase = self.get_dummy_components() __lowerCamelCase = StableDiffusionPanoramaPipeline(**a ) __lowerCamelCase = sd_pipe.to(a ) sd_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = self.get_dummy_inputs(a ) __lowerCamelCase = '''french fries''' __lowerCamelCase = sd_pipe(**a , negative_prompt=a ) __lowerCamelCase = output.images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __lowerCamelCase = np.array([0.61_87, 0.53_75, 0.49_15, 0.41_36, 0.41_14, 0.45_63, 0.51_28, 0.49_76, 0.47_57] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" __lowerCamelCase = '''cpu''' # ensure determinism for the device-dependent torch.Generator __lowerCamelCase = self.get_dummy_components() __lowerCamelCase = StableDiffusionPanoramaPipeline(**a ) __lowerCamelCase = sd_pipe.to(a ) sd_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = self.get_dummy_inputs(a ) __lowerCamelCase = sd_pipe(**a , view_batch_size=2 ) __lowerCamelCase = output.images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __lowerCamelCase = np.array([0.61_87, 0.53_75, 0.49_15, 0.41_36, 0.41_14, 0.45_63, 0.51_28, 0.49_76, 0.47_57] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = '''cpu''' # ensure determinism for the device-dependent torch.Generator __lowerCamelCase = self.get_dummy_components() __lowerCamelCase = EulerAncestralDiscreteScheduler( beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule='''scaled_linear''' ) __lowerCamelCase = StableDiffusionPanoramaPipeline(**a ) __lowerCamelCase = sd_pipe.to(a ) sd_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = self.get_dummy_inputs(a ) __lowerCamelCase = sd_pipe(**a ).images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __lowerCamelCase = np.array([0.40_24, 0.65_10, 0.49_01, 0.53_78, 0.58_13, 0.56_22, 0.47_95, 0.44_67, 0.49_52] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = '''cpu''' # ensure determinism for the device-dependent torch.Generator __lowerCamelCase = self.get_dummy_components() __lowerCamelCase = PNDMScheduler( beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule='''scaled_linear''' , skip_prk_steps=a ) __lowerCamelCase = StableDiffusionPanoramaPipeline(**a ) __lowerCamelCase = sd_pipe.to(a ) sd_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = self.get_dummy_inputs(a ) __lowerCamelCase = sd_pipe(**a ).images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __lowerCamelCase = np.array([0.63_91, 0.62_91, 0.48_61, 0.51_34, 0.55_52, 0.45_78, 0.50_32, 0.50_23, 0.45_39] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE__ ( self : str , a : str=0 ): """simple docstring""" __lowerCamelCase = torch.manual_seed(a ) __lowerCamelCase = { '''prompt''': '''a photo of the dolomites''', '''generator''': generator, '''num_inference_steps''': 3, '''guidance_scale''': 7.5, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = '''stabilityai/stable-diffusion-2-base''' __lowerCamelCase = DDIMScheduler.from_pretrained(a , subfolder='''scheduler''' ) __lowerCamelCase = StableDiffusionPanoramaPipeline.from_pretrained(a , scheduler=a , safety_checker=a ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) pipe.enable_attention_slicing() __lowerCamelCase = self.get_inputs() __lowerCamelCase = pipe(**a ).images __lowerCamelCase = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_12, 20_48, 3) __lowerCamelCase = np.array( [ 0.36_96_83_92, 0.27_02_53_72, 0.32_44_67_66, 0.28_37_93_87, 0.36_36_32_74, 0.30_73_33_47, 0.27_10_00_27, 0.27_05_41_25, 0.25_53_60_96, ] ) assert np.abs(expected_slice - image_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = StableDiffusionPanoramaPipeline.from_pretrained( '''stabilityai/stable-diffusion-2-base''' , safety_checker=a ) __lowerCamelCase = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) pipe.enable_attention_slicing() __lowerCamelCase = self.get_inputs() __lowerCamelCase = pipe(**a ).images __lowerCamelCase = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_12, 20_48, 3) __lowerCamelCase = np.array( [ [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ] ] ) assert np.abs(expected_slice - image_slice ).max() < 1e-3 def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" __lowerCamelCase = 0 def callback_fn(a : int , a : int , a : torch.FloatTensor ) -> None: __lowerCamelCase = True nonlocal number_of_steps number_of_steps += 1 if step == 1: __lowerCamelCase = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 2_56) __lowerCamelCase = latents[0, -3:, -3:, -1] __lowerCamelCase = np.array( [ 0.18_68_18_69, 0.33_90_78_16, 0.5_36_12_76, 0.14_43_28_65, -0.02_85_66_11, -0.73_94_11_23, 0.23_39_79_87, 0.47_32_26_82, -0.37_82_31_64, ] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 elif step == 2: __lowerCamelCase = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 2_56) __lowerCamelCase = latents[0, -3:, -3:, -1] __lowerCamelCase = np.array( [ 0.18_53_96_45, 0.33_98_72_48, 0.5_37_85_59, 0.14_43_71_42, -0.02_45_52_61, -0.7_33_83_17, 0.23_99_07_55, 0.47_35_62_72, -0.3_78_65_05, ] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 __lowerCamelCase = False __lowerCamelCase = '''stabilityai/stable-diffusion-2-base''' __lowerCamelCase = DDIMScheduler.from_pretrained(a , subfolder='''scheduler''' ) __lowerCamelCase = StableDiffusionPanoramaPipeline.from_pretrained(a , scheduler=a , safety_checker=a ) __lowerCamelCase = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) pipe.enable_attention_slicing() __lowerCamelCase = self.get_inputs() pipe(**a , callback=a , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __lowerCamelCase = '''stabilityai/stable-diffusion-2-base''' __lowerCamelCase = DDIMScheduler.from_pretrained(a , subfolder='''scheduler''' ) __lowerCamelCase = StableDiffusionPanoramaPipeline.from_pretrained(a , scheduler=a , safety_checker=a ) __lowerCamelCase = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() __lowerCamelCase = self.get_inputs() __lowerCamelCase = pipe(**a ) __lowerCamelCase = torch.cuda.max_memory_allocated() # make sure that less than 5.2 GB is allocated assert mem_bytes < 5.5 * 10**9
67
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST, OpenAIGPTConfig, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification, OpenAIGPTLMHeadModel, OpenAIGPTModel, ) class a__ : def __init__( self : Union[str, Any] , a : Union[str, Any] , a : Tuple=13 , a : Optional[Any]=7 , a : List[Any]=True , a : Optional[Any]=True , a : Any=True , a : Union[str, Any]=99 , a : Any=32 , a : int=5 , a : Optional[int]=4 , a : Union[str, Any]=37 , a : Optional[Any]="gelu" , a : Union[str, Any]=0.1 , a : Any=0.1 , a : Optional[int]=5_12 , a : int=16 , a : Optional[Any]=2 , a : Union[str, Any]=0.02 , a : Any=3 , a : Dict=4 , a : Any=None , ): """simple docstring""" __lowerCamelCase = parent __lowerCamelCase = batch_size __lowerCamelCase = seq_length __lowerCamelCase = is_training __lowerCamelCase = use_token_type_ids __lowerCamelCase = use_labels __lowerCamelCase = vocab_size __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_size __lowerCamelCase = hidden_act __lowerCamelCase = hidden_dropout_prob __lowerCamelCase = attention_probs_dropout_prob __lowerCamelCase = max_position_embeddings __lowerCamelCase = type_vocab_size __lowerCamelCase = type_sequence_label_size __lowerCamelCase = initializer_range __lowerCamelCase = num_labels __lowerCamelCase = num_choices __lowerCamelCase = scope __lowerCamelCase = self.vocab_size - 1 def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCamelCase = None if self.use_token_type_ids: __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = None if self.use_labels: __lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __lowerCamelCase = ids_tensor([self.batch_size] , self.num_choices ) __lowerCamelCase = OpenAIGPTConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) __lowerCamelCase = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, head_mask, token_type_ids, sequence_labels, token_labels, choice_labels, ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : Dict , a : List[str] , a : Tuple , a : List[Any] , *a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTModel(config=a ) model.to(a ) model.eval() __lowerCamelCase = model(a , token_type_ids=a , head_mask=a ) __lowerCamelCase = model(a , token_type_ids=a ) __lowerCamelCase = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Union[str, Any] , a : Dict , a : Union[str, Any] , a : Tuple , *a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTLMHeadModel(a ) model.to(a ) model.eval() __lowerCamelCase = model(a , token_type_ids=a , labels=a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Tuple , a : Optional[int] , a : Union[str, Any] , a : Optional[Any] , *a : Optional[Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTDoubleHeadsModel(a ) model.to(a ) model.eval() __lowerCamelCase = model(a , token_type_ids=a , labels=a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self : List[str] , a : int , a : Dict , a : Optional[Any] , a : str , *a : int ): """simple docstring""" __lowerCamelCase = self.num_labels __lowerCamelCase = OpenAIGPTForSequenceClassification(a ) model.to(a ) model.eval() __lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCamelCase = model(a , token_type_ids=a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = self.prepare_config_and_inputs() ( ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ) = config_and_inputs __lowerCamelCase = { '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask, } return config, inputs_dict @require_torch class a__ ( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): lowerCamelCase : List[str] =( (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification) if is_torch_available() else () ) lowerCamelCase : str =( (OpenAIGPTLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly lowerCamelCase : Optional[int] =( { "feature-extraction": OpenAIGPTModel, "text-classification": OpenAIGPTForSequenceClassification, "text-generation": OpenAIGPTLMHeadModel, "zero-shot": OpenAIGPTForSequenceClassification, } if is_torch_available() else {} ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : Tuple , a : Optional[int] , a : int , a : str , a : Any ): """simple docstring""" if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a # tiny config could not be created. return True return False def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : int , a : Optional[int] , a : str=False ): """simple docstring""" __lowerCamelCase = super()._prepare_for_class(a , a , return_labels=a ) if return_labels: if model_class.__name__ == "OpenAIGPTDoubleHeadsModel": __lowerCamelCase = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length) , dtype=torch.long , device=a , ) __lowerCamelCase = inputs_dict['''labels'''] __lowerCamelCase = inputs_dict['''labels'''] __lowerCamelCase = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices) , dtype=torch.long , device=a , ) __lowerCamelCase = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=a ) return inputs_dict def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTModelTester(self ) __lowerCamelCase = ConfigTester(self , config_class=a , n_embd=37 ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_double_lm_head_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*a ) @slow def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __lowerCamelCase = OpenAIGPTModel.from_pretrained(a ) self.assertIsNotNone(a ) @require_torch class a__ ( unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt''' ) model.to(a ) __lowerCamelCase = torch.tensor([[4_81, 47_35, 5_44]] , dtype=torch.long , device=a ) # the president is __lowerCamelCase = [ 4_81, 47_35, 5_44, 2_46, 9_63, 8_70, 7_62, 2_39, 2_44, 4_04_77, 2_44, 2_49, 7_19, 8_81, 4_87, 5_44, 2_40, 2_44, 6_03, 4_81, ] # the president is a very good man. " \n " i\'m sure he is, " said the __lowerCamelCase = model.generate(a , do_sample=a ) self.assertListEqual(output_ids[0].tolist() , a )
67
1
'''simple docstring''' from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxSeqaSeqConfigWithPast from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={ "google/umt5-small": "https://huggingface.co/google/umt5-small/resolve/main/config.json", # See all umt5 models at https://huggingface.co/models?filter=umt5 } class a__ ( UpperCAmelCase__ ): lowerCamelCase : Any ="umt5" lowerCamelCase : Dict =["past_key_values"] def __init__( self : Optional[Any] , a : Optional[Any]=25_01_12 , a : str=5_12 , a : Union[str, Any]=64 , a : Union[str, Any]=10_24 , a : Dict=8 , a : Any=None , a : str=6 , a : Optional[int]=32 , a : List[str]=1_28 , a : Optional[int]=0.1 , a : Any=1e-6 , a : List[Any]=1.0 , a : int="gated-gelu" , a : str=True , a : Dict=True , a : Optional[int]="T5Tokenizer" , a : List[Any]=True , a : Optional[Any]=0 , a : Any=1 , a : Union[str, Any]=0 , **a : str , ): """simple docstring""" super().__init__( is_encoder_decoder=a , tokenizer_class=a , tie_word_embeddings=a , pad_token_id=a , eos_token_id=a , decoder_start_token_id=a , **a , ) __lowerCamelCase = vocab_size __lowerCamelCase = d_model __lowerCamelCase = d_kv __lowerCamelCase = d_ff __lowerCamelCase = num_layers __lowerCamelCase = ( num_decoder_layers if num_decoder_layers is not None else self.num_layers ) # default = symmetry __lowerCamelCase = num_heads __lowerCamelCase = relative_attention_num_buckets __lowerCamelCase = relative_attention_max_distance __lowerCamelCase = dropout_rate __lowerCamelCase = layer_norm_epsilon __lowerCamelCase = initializer_factor __lowerCamelCase = feed_forward_proj __lowerCamelCase = use_cache __lowerCamelCase = self.feed_forward_proj.split('''-''' ) __lowerCamelCase = act_info[-1] __lowerCamelCase = act_info[0] == '''gated''' if len(a ) > 1 and act_info[0] != "gated" or len(a ) > 2: raise ValueError( f"""`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.""" '''Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. ''' '''\'gated-gelu\' or \'relu\'''' ) if feed_forward_proj == "gated-gelu": __lowerCamelCase = '''gelu_new''' @property def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" return self.d_model @property def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" return self.num_heads @property def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" return self.num_layers class a__ ( UpperCAmelCase__ ): @property # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = { '''input_ids''': {0: '''batch''', 1: '''encoder_sequence'''}, '''attention_mask''': {0: '''batch''', 1: '''encoder_sequence'''}, } if self.use_past: __lowerCamelCase = '''past_encoder_sequence + sequence''' __lowerCamelCase = {0: '''batch'''} __lowerCamelCase = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: __lowerCamelCase = {0: '''batch''', 1: '''decoder_sequence'''} __lowerCamelCase = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(a , direction='''inputs''' ) return common_inputs @property # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" return 13 @property def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" return 5e-4
67
'''simple docstring''' import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] =["image_processor", "tokenizer"] lowerCamelCase : Union[str, Any] ="LayoutLMv2ImageProcessor" lowerCamelCase : int =("LayoutXLMTokenizer", "LayoutXLMTokenizerFast") def __init__( self : Optional[int] , a : Any=None , a : Any=None , **a : Union[str, Any] ): """simple docstring""" if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , a , ) __lowerCamelCase = kwargs.pop('''feature_extractor''' ) __lowerCamelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(a , a ) def __call__( self : Tuple , a : Optional[int] , a : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , a : Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None , a : Union[List[List[int]], List[List[List[int]]]] = None , a : Optional[Union[List[int], List[List[int]]]] = None , a : bool = True , a : Union[bool, str, PaddingStrategy] = False , a : Union[bool, str, TruncationStrategy] = None , a : Optional[int] = None , a : int = 0 , a : Optional[int] = None , a : Optional[bool] = None , a : Optional[bool] = None , a : bool = False , a : bool = False , a : bool = False , a : bool = False , a : bool = True , a : Optional[Union[str, TensorType]] = None , **a : Tuple , ): """simple docstring""" if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( '''You cannot provide bounding boxes ''' '''if you initialized the image processor with apply_ocr set to True.''' ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.''' ) if return_overflowing_tokens is True and return_offsets_mapping is False: raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.''' ) # first, apply the image processor __lowerCamelCase = self.image_processor(images=a , return_tensors=a ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(a , a ): __lowerCamelCase = [text] # add batch dimension (as the image processor always adds a batch dimension) __lowerCamelCase = features['''words'''] __lowerCamelCase = self.tokenizer( text=text if text is not None else features['''words'''] , text_pair=text_pair if text_pair is not None else None , boxes=boxes if boxes is not None else features['''boxes'''] , word_labels=a , add_special_tokens=a , padding=a , truncation=a , max_length=a , stride=a , pad_to_multiple_of=a , return_token_type_ids=a , return_attention_mask=a , return_overflowing_tokens=a , return_special_tokens_mask=a , return_offsets_mapping=a , return_length=a , verbose=a , return_tensors=a , **a , ) # add pixel values __lowerCamelCase = features.pop('''pixel_values''' ) if return_overflowing_tokens is True: __lowerCamelCase = self.get_overflowing_images(a , encoded_inputs['''overflow_to_sample_mapping'''] ) __lowerCamelCase = images return encoded_inputs def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : Optional[Any] , a : str ): """simple docstring""" __lowerCamelCase = [] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(a ) != len(a ): raise ValueError( '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got''' f""" {len(a )} and {len(a )}""" ) return images_with_overflow def SCREAMING_SNAKE_CASE__ ( self : List[str] , *a : Optional[Any] , **a : Union[str, Any] ): """simple docstring""" return self.tokenizer.batch_decode(*a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , *a : Union[str, Any] , **a : Tuple ): """simple docstring""" return self.tokenizer.decode(*a , **a ) @property def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" return ["input_ids", "bbox", "attention_mask", "image"] @property def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , a , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , a , ) return self.image_processor
67
1
'''simple docstring''' import re import jax.numpy as jnp from flax.traverse_util import flatten_dict, unflatten_dict from jax.random import PRNGKey from ..utils import logging __UpperCAmelCase =logging.get_logger(__name__) def __lowerCAmelCase ( UpperCamelCase__ ) -> Any: __lowerCamelCase = r'''\w+[.]\d+''' __lowerCamelCase = re.findall(UpperCamelCase__ , UpperCamelCase__ ) for pat in pats: __lowerCamelCase = key.replace(UpperCamelCase__ , '''_'''.join(pat.split('''.''' ) ) ) return key def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Dict: __lowerCamelCase = pt_tuple_key[:-1] + ('''scale''',) if ( any('''norm''' in str_ for str_ in pt_tuple_key ) and (pt_tuple_key[-1] == "bias") and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict) and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict) ): __lowerCamelCase = pt_tuple_key[:-1] + ('''scale''',) return renamed_pt_tuple_key, pt_tensor elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict: __lowerCamelCase = pt_tuple_key[:-1] + ('''scale''',) return renamed_pt_tuple_key, pt_tensor # embedding if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict: __lowerCamelCase = pt_tuple_key[:-1] + ('''embedding''',) return renamed_pt_tuple_key, pt_tensor # conv layer __lowerCamelCase = pt_tuple_key[:-1] + ('''kernel''',) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4: __lowerCamelCase = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer __lowerCamelCase = pt_tuple_key[:-1] + ('''kernel''',) if pt_tuple_key[-1] == "weight": __lowerCamelCase = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight __lowerCamelCase = pt_tuple_key[:-1] + ('''weight''',) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias __lowerCamelCase = pt_tuple_key[:-1] + ('''bias''',) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=42 ) -> Optional[Any]: # Step 1: Convert pytorch tensor to numpy __lowerCamelCase = {k: v.numpy() for k, v in pt_state_dict.items()} # Step 2: Since the model is stateless, get random Flax params __lowerCamelCase = flax_model.init_weights(PRNGKey(UpperCamelCase__ ) ) __lowerCamelCase = flatten_dict(UpperCamelCase__ ) __lowerCamelCase = {} # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): __lowerCamelCase = rename_key(UpperCamelCase__ ) __lowerCamelCase = tuple(renamed_pt_key.split('''.''' ) ) # Correctly rename weight parameters __lowerCamelCase , __lowerCamelCase = rename_key_and_reshape_tensor(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( f"""PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape """ f"""{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.""" ) # also add unexpected weight so that warning is thrown __lowerCamelCase = jnp.asarray(UpperCamelCase__ ) return unflatten_dict(UpperCamelCase__ )
67
'''simple docstring''' import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) class a__ ( UpperCAmelCase__ ): def __init__( self : Optional[Any] , a : Union[List[ControlNetModel], Tuple[ControlNetModel]] ): """simple docstring""" super().__init__() __lowerCamelCase = nn.ModuleList(a ) def SCREAMING_SNAKE_CASE__ ( self : Any , a : torch.FloatTensor , a : Union[torch.Tensor, float, int] , a : torch.Tensor , a : List[torch.tensor] , a : List[float] , a : Optional[torch.Tensor] = None , a : Optional[torch.Tensor] = None , a : Optional[torch.Tensor] = None , a : Optional[Dict[str, Any]] = None , a : bool = False , a : bool = True , ): """simple docstring""" for i, (image, scale, controlnet) in enumerate(zip(a , a , self.nets ) ): __lowerCamelCase , __lowerCamelCase = controlnet( a , a , a , a , a , a , a , a , a , a , a , ) # merge samples if i == 0: __lowerCamelCase , __lowerCamelCase = down_samples, mid_sample else: __lowerCamelCase = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(a , a ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def SCREAMING_SNAKE_CASE__ ( self : Any , a : Union[str, os.PathLike] , a : bool = True , a : Callable = None , a : bool = False , a : Optional[str] = None , ): """simple docstring""" __lowerCamelCase = 0 __lowerCamelCase = save_directory for controlnet in self.nets: controlnet.save_pretrained( a , is_main_process=a , save_function=a , safe_serialization=a , variant=a , ) idx += 1 __lowerCamelCase = model_path_to_save + f"""_{idx}""" @classmethod def SCREAMING_SNAKE_CASE__ ( cls : List[str] , a : Optional[Union[str, os.PathLike]] , **a : Optional[Any] ): """simple docstring""" __lowerCamelCase = 0 __lowerCamelCase = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... __lowerCamelCase = pretrained_model_path while os.path.isdir(a ): __lowerCamelCase = ControlNetModel.from_pretrained(a , **a ) controlnets.append(a ) idx += 1 __lowerCamelCase = pretrained_model_path + f"""_{idx}""" logger.info(f"""{len(a )} controlnets loaded from {pretrained_model_path}.""" ) if len(a ) == 0: raise ValueError( f"""No ControlNets found under {os.path.dirname(a )}. Expected at least {pretrained_model_path + '_0'}.""" ) return cls(a )
67
1
'''simple docstring''' import re from typing import Callable, List, Optional, Union import tensorflow as tf try: from tensorflow.keras.optimizers.legacy import Adam except ImportError: from tensorflow.keras.optimizers import Adam class a__ ( tf.keras.optimizers.schedules.LearningRateSchedule ): def __init__( self : str , a : float , a : Callable , a : int , a : float = 1.0 , a : str = None , ): """simple docstring""" super().__init__() __lowerCamelCase = initial_learning_rate __lowerCamelCase = warmup_steps __lowerCamelCase = power __lowerCamelCase = decay_schedule_fn __lowerCamelCase = name def __call__( self : Dict , a : str ): """simple docstring""" with tf.name_scope(self.name or '''WarmUp''' ) as name: # Implements polynomial warmup. i.e., if global_step < warmup_steps, the # learning rate will be `global_step/num_warmup_steps * init_lr`. __lowerCamelCase = tf.cast(a , tf.floataa ) __lowerCamelCase = tf.cast(self.warmup_steps , tf.floataa ) __lowerCamelCase = global_step_float / warmup_steps_float __lowerCamelCase = self.initial_learning_rate * tf.math.pow(a , self.power ) return tf.cond( global_step_float < warmup_steps_float , lambda: warmup_learning_rate , lambda: self.decay_schedule_fn(step - self.warmup_steps ) , name=a , ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" return { "initial_learning_rate": self.initial_learning_rate, "decay_schedule_fn": self.decay_schedule_fn, "warmup_steps": self.warmup_steps, "power": self.power, "name": self.name, } def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = 0.0 , UpperCamelCase__ = 0.9 , UpperCamelCase__ = 0.9_9_9 , UpperCamelCase__ = 1E-8 , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = 0.0 , UpperCamelCase__ = 1.0 , UpperCamelCase__ = None , ) -> Dict: __lowerCamelCase = tf.keras.optimizers.schedules.PolynomialDecay( initial_learning_rate=UpperCamelCase__ , decay_steps=num_train_steps - num_warmup_steps , end_learning_rate=init_lr * min_lr_ratio , power=UpperCamelCase__ , ) if num_warmup_steps: __lowerCamelCase = WarmUp( initial_learning_rate=UpperCamelCase__ , decay_schedule_fn=UpperCamelCase__ , warmup_steps=UpperCamelCase__ , ) if weight_decay_rate > 0.0: __lowerCamelCase = AdamWeightDecay( learning_rate=UpperCamelCase__ , weight_decay_rate=UpperCamelCase__ , beta_a=UpperCamelCase__ , beta_a=UpperCamelCase__ , epsilon=UpperCamelCase__ , clipnorm=UpperCamelCase__ , global_clipnorm=UpperCamelCase__ , exclude_from_weight_decay=['''LayerNorm''', '''layer_norm''', '''bias'''] , include_in_weight_decay=UpperCamelCase__ , ) else: __lowerCamelCase = tf.keras.optimizers.Adam( learning_rate=UpperCamelCase__ , beta_a=UpperCamelCase__ , beta_a=UpperCamelCase__ , epsilon=UpperCamelCase__ , clipnorm=UpperCamelCase__ , global_clipnorm=UpperCamelCase__ , ) # We return the optimizer and the LR scheduler in order to better track the # evolution of the LR independently of the optimizer. return optimizer, lr_schedule class a__ ( UpperCAmelCase__ ): def __init__( self : List[Any] , a : Union[float, tf.keras.optimizers.schedules.LearningRateSchedule] = 0.0_01 , a : float = 0.9 , a : float = 0.9_99 , a : float = 1e-7 , a : bool = False , a : float = 0.0 , a : Optional[List[str]] = None , a : Optional[List[str]] = None , a : str = "AdamWeightDecay" , **a : Union[str, Any] , ): """simple docstring""" super().__init__(a , a , a , a , a , a , **a ) __lowerCamelCase = weight_decay_rate __lowerCamelCase = include_in_weight_decay __lowerCamelCase = exclude_from_weight_decay @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Any , a : int ): """simple docstring""" __lowerCamelCase = {'''WarmUp''': WarmUp} return super(a , cls ).from_config(a , custom_objects=a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : int , a : List[str] , a : Any ): """simple docstring""" super(a , self )._prepare_local(a , a , a ) __lowerCamelCase = tf.constant( self.weight_decay_rate , name='''adam_weight_decay_rate''' ) def SCREAMING_SNAKE_CASE__ ( self : str , a : List[str] , a : List[str] , a : str ): """simple docstring""" __lowerCamelCase = self._do_use_weight_decay(var.name ) if do_decay: return var.assign_sub( learning_rate * var * apply_state[(var.device, var.dtype.base_dtype)]['''weight_decay_rate'''] , use_locking=self._use_locking , ) return tf.no_op() def SCREAMING_SNAKE_CASE__ ( self : List[str] , a : List[Any] , a : int=None , **a : Optional[int] ): """simple docstring""" __lowerCamelCase , __lowerCamelCase = list(zip(*a ) ) return super(a , self ).apply_gradients(zip(a , a ) , name=a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : Any , a : Tuple , a : int ): """simple docstring""" if apply_state is None: return self._decayed_lr_t[var_dtype], {} __lowerCamelCase = apply_state or {} __lowerCamelCase = apply_state.get((var_device, var_dtype) ) if coefficients is None: __lowerCamelCase = self._fallback_apply_state(a , a ) __lowerCamelCase = coefficients return coefficients["lr_t"], {"apply_state": apply_state} def SCREAMING_SNAKE_CASE__ ( self : str , a : Optional[int] , a : Tuple , a : Tuple=None ): """simple docstring""" __lowerCamelCase , __lowerCamelCase = self._get_lr(var.device , var.dtype.base_dtype , a ) __lowerCamelCase = self._decay_weights_op(a , a , a ) with tf.control_dependencies([decay] ): return super(a , self )._resource_apply_dense(a , a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : int , a : List[str] , a : str , a : Optional[int]=None ): """simple docstring""" __lowerCamelCase , __lowerCamelCase = self._get_lr(var.device , var.dtype.base_dtype , a ) __lowerCamelCase = self._decay_weights_op(a , a , a ) with tf.control_dependencies([decay] ): return super(a , self )._resource_apply_sparse(a , a , a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = super().get_config() config.update({'''weight_decay_rate''': self.weight_decay_rate} ) return config def SCREAMING_SNAKE_CASE__ ( self : int , a : List[str] ): """simple docstring""" if self.weight_decay_rate == 0: return False if self._include_in_weight_decay: for r in self._include_in_weight_decay: if re.search(a , a ) is not None: return True if self._exclude_from_weight_decay: for r in self._exclude_from_weight_decay: if re.search(a , a ) is not None: return False return True class a__ ( UpperCAmelCase__ ): def __init__( self : Tuple ): """simple docstring""" __lowerCamelCase = [] __lowerCamelCase = None @property def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" if self._accum_steps is None: __lowerCamelCase = tf.Variable( tf.constant(0 , dtype=tf.intaa ) , trainable=a , synchronization=tf.VariableSynchronization.ON_READ , aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA , ) return self._accum_steps.value() @property def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" if not self._gradients: raise ValueError('''The accumulator should be called first to initialize the gradients''' ) return [gradient.value() if gradient is not None else gradient for gradient in self._gradients] def __call__( self : int , a : Dict ): """simple docstring""" if not self._gradients: __lowerCamelCase = self.step # Create the step variable. self._gradients.extend( [ tf.Variable( tf.zeros_like(a ) , trainable=a , synchronization=tf.VariableSynchronization.ON_READ , aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA , ) if gradient is not None else gradient for gradient in gradients ] ) if len(a ) != len(self._gradients ): raise ValueError(f"""Expected {len(self._gradients )} gradients, but got {len(a )}""" ) for accum_gradient, gradient in zip(self._gradients , a ): if accum_gradient is not None and gradient is not None: accum_gradient.assign_add(a ) self._accum_steps.assign_add(1 ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" if not self._gradients: return self._accum_steps.assign(0 ) for gradient in self._gradients: if gradient is not None: gradient.assign(tf.zeros_like(a ) )
67
'''simple docstring''' from typing import List, Union import numpy as np from ..utils import add_end_docstrings, 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(): import torch from ..models.auto.modeling_auto import MODEL_FOR_DEPTH_ESTIMATION_MAPPING __UpperCAmelCase =logging.get_logger(__name__) @add_end_docstrings(UpperCAmelCase__ ) class a__ ( UpperCAmelCase__ ): def __init__( self : List[str] , *a : Union[str, Any] , **a : Optional[Any] ): """simple docstring""" super().__init__(*a , **a ) requires_backends(self , '''vision''' ) self.check_model_type(a ) def __call__( self : Any , a : Union[str, List[str], "Image.Image", List["Image.Image"]] , **a : Optional[int] ): """simple docstring""" return super().__call__(a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , **a : Any ): """simple docstring""" return {}, {}, {} def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : List[str] ): """simple docstring""" __lowerCamelCase = load_image(a ) __lowerCamelCase = image.size __lowerCamelCase = self.image_processor(images=a , return_tensors=self.framework ) return model_inputs def SCREAMING_SNAKE_CASE__ ( self : int , a : Optional[Any] ): """simple docstring""" __lowerCamelCase = self.model(**a ) return model_outputs def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : Any ): """simple docstring""" __lowerCamelCase = model_outputs.predicted_depth __lowerCamelCase = torch.nn.functional.interpolate( predicted_depth.unsqueeze(1 ) , size=self.image_size[::-1] , mode='''bicubic''' , align_corners=a ) __lowerCamelCase = prediction.squeeze().cpu().numpy() __lowerCamelCase = (output * 2_55 / np.max(a )).astype('''uint8''' ) __lowerCamelCase = Image.fromarray(a ) __lowerCamelCase = {} __lowerCamelCase = predicted_depth __lowerCamelCase = depth return output_dict
67
1
'''simple docstring''' from __future__ import annotations from decimal import Decimal from numpy import array def __lowerCAmelCase ( UpperCamelCase__ ) -> list[list[float]]: __lowerCamelCase = Decimal # Check if the provided matrix has 2 rows and 2 columns # since this implementation only works for 2x2 matrices if len(UpperCamelCase__ ) == 2 and len(matrix[0] ) == 2 and len(matrix[1] ) == 2: # Calculate the determinant of the matrix __lowerCamelCase = float( d(matrix[0][0] ) * d(matrix[1][1] ) - d(matrix[1][0] ) * d(matrix[0][1] ) ) if determinant == 0: raise ValueError('''This matrix has no inverse.''' ) # Creates a copy of the matrix with swapped positions of the elements __lowerCamelCase = [[0.0, 0.0], [0.0, 0.0]] __lowerCamelCase , __lowerCamelCase = matrix[1][1], matrix[0][0] __lowerCamelCase , __lowerCamelCase = -matrix[1][0], -matrix[0][1] # Calculate the inverse of the matrix return [ [(float(d(UpperCamelCase__ ) ) / determinant) or 0.0 for n in row] for row in swapped_matrix ] elif ( len(UpperCamelCase__ ) == 3 and len(matrix[0] ) == 3 and len(matrix[1] ) == 3 and len(matrix[2] ) == 3 ): # Calculate the determinant of the matrix using Sarrus rule __lowerCamelCase = float( ( (d(matrix[0][0] ) * d(matrix[1][1] ) * d(matrix[2][2] )) + (d(matrix[0][1] ) * d(matrix[1][2] ) * d(matrix[2][0] )) + (d(matrix[0][2] ) * d(matrix[1][0] ) * d(matrix[2][1] )) ) - ( (d(matrix[0][2] ) * d(matrix[1][1] ) * d(matrix[2][0] )) + (d(matrix[0][1] ) * d(matrix[1][0] ) * d(matrix[2][2] )) + (d(matrix[0][0] ) * d(matrix[1][2] ) * d(matrix[2][1] )) ) ) if determinant == 0: raise ValueError('''This matrix has no inverse.''' ) # Creating cofactor matrix __lowerCamelCase = [ [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], ] __lowerCamelCase = (d(matrix[1][1] ) * d(matrix[2][2] )) - ( d(matrix[1][2] ) * d(matrix[2][1] ) ) __lowerCamelCase = -( (d(matrix[1][0] ) * d(matrix[2][2] )) - (d(matrix[1][2] ) * d(matrix[2][0] )) ) __lowerCamelCase = (d(matrix[1][0] ) * d(matrix[2][1] )) - ( d(matrix[1][1] ) * d(matrix[2][0] ) ) __lowerCamelCase = -( (d(matrix[0][1] ) * d(matrix[2][2] )) - (d(matrix[0][2] ) * d(matrix[2][1] )) ) __lowerCamelCase = (d(matrix[0][0] ) * d(matrix[2][2] )) - ( d(matrix[0][2] ) * d(matrix[2][0] ) ) __lowerCamelCase = -( (d(matrix[0][0] ) * d(matrix[2][1] )) - (d(matrix[0][1] ) * d(matrix[2][0] )) ) __lowerCamelCase = (d(matrix[0][1] ) * d(matrix[1][2] )) - ( d(matrix[0][2] ) * d(matrix[1][1] ) ) __lowerCamelCase = -( (d(matrix[0][0] ) * d(matrix[1][2] )) - (d(matrix[0][2] ) * d(matrix[1][0] )) ) __lowerCamelCase = (d(matrix[0][0] ) * d(matrix[1][1] )) - ( d(matrix[0][1] ) * d(matrix[1][0] ) ) # Transpose the cofactor matrix (Adjoint matrix) __lowerCamelCase = array(UpperCamelCase__ ) for i in range(3 ): for j in range(3 ): __lowerCamelCase = cofactor_matrix[j][i] # Inverse of the matrix using the formula (1/determinant) * adjoint matrix __lowerCamelCase = array(UpperCamelCase__ ) for i in range(3 ): for j in range(3 ): inverse_matrix[i][j] /= d(UpperCamelCase__ ) # Calculate the inverse of the matrix return [[float(d(UpperCamelCase__ ) ) or 0.0 for n in row] for row in inverse_matrix] raise ValueError('''Please provide a matrix of size 2x2 or 3x3.''' )
67
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase ={ "configuration_clap": [ "CLAP_PRETRAINED_MODEL_ARCHIVE_LIST", "ClapAudioConfig", "ClapConfig", "ClapTextConfig", ], "processing_clap": ["ClapProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "CLAP_PRETRAINED_MODEL_ARCHIVE_LIST", "ClapModel", "ClapPreTrainedModel", "ClapTextModel", "ClapTextModelWithProjection", "ClapAudioModel", "ClapAudioModelWithProjection", ] __UpperCAmelCase =["ClapFeatureExtractor"] if TYPE_CHECKING: from .configuration_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioConfig, ClapConfig, ClapTextConfig, ) from .processing_clap import ClapProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clap import ClapFeatureExtractor from .modeling_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioModel, ClapAudioModelWithProjection, ClapModel, ClapPreTrainedModel, ClapTextModel, ClapTextModelWithProjection, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
67
1
'''simple docstring''' import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class a__ ( unittest.TestCase ): lowerCamelCase : List[Any] =MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING lowerCamelCase : int =TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def SCREAMING_SNAKE_CASE__ ( self : Dict , a : Union[str, Any] , a : Any , a : Any ): """simple docstring""" __lowerCamelCase = TextaTextGenerationPipeline(model=a , tokenizer=a ) return generator, ["Something to write", "Something else"] def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Any , a : Optional[int] ): """simple docstring""" __lowerCamelCase = generator('''Something there''' ) self.assertEqual(a , [{'''generated_text''': ANY(a )}] ) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]['''generated_text'''].startswith('''Something there''' ) ) __lowerCamelCase = generator(['''This is great !''', '''Something else'''] , num_return_sequences=2 , do_sample=a ) self.assertEqual( a , [ [{'''generated_text''': ANY(a )}, {'''generated_text''': ANY(a )}], [{'''generated_text''': ANY(a )}, {'''generated_text''': ANY(a )}], ] , ) __lowerCamelCase = generator( ['''This is great !''', '''Something else'''] , num_return_sequences=2 , batch_size=2 , do_sample=a ) self.assertEqual( a , [ [{'''generated_text''': ANY(a )}, {'''generated_text''': ANY(a )}], [{'''generated_text''': ANY(a )}, {'''generated_text''': ANY(a )}], ] , ) with self.assertRaises(a ): generator(4 ) @require_torch def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" __lowerCamelCase = pipeline('''text2text-generation''' , model='''patrickvonplaten/t5-tiny-random''' , framework='''pt''' ) # do_sample=False necessary for reproducibility __lowerCamelCase = generator('''Something there''' , do_sample=a ) self.assertEqual(a , [{'''generated_text''': ''''''}] ) __lowerCamelCase = 3 __lowerCamelCase = generator( '''Something there''' , num_return_sequences=a , num_beams=a , ) __lowerCamelCase = [ {'''generated_text''': '''Beide Beide Beide Beide Beide Beide Beide Beide Beide'''}, {'''generated_text''': '''Beide Beide Beide Beide Beide Beide Beide Beide'''}, {'''generated_text''': ''''''}, ] self.assertEqual(a , a ) __lowerCamelCase = generator('''This is a test''' , do_sample=a , num_return_sequences=2 , return_tensors=a ) self.assertEqual( a , [ {'''generated_token_ids''': ANY(torch.Tensor )}, {'''generated_token_ids''': ANY(torch.Tensor )}, ] , ) __lowerCamelCase = generator.model.config.eos_token_id __lowerCamelCase = '''<pad>''' __lowerCamelCase = generator( ['''This is a test''', '''This is a second test'''] , do_sample=a , num_return_sequences=2 , batch_size=2 , return_tensors=a , ) self.assertEqual( a , [ [ {'''generated_token_ids''': ANY(torch.Tensor )}, {'''generated_token_ids''': ANY(torch.Tensor )}, ], [ {'''generated_token_ids''': ANY(torch.Tensor )}, {'''generated_token_ids''': ANY(torch.Tensor )}, ], ] , ) @require_tf def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = pipeline('''text2text-generation''' , model='''patrickvonplaten/t5-tiny-random''' , framework='''tf''' ) # do_sample=False necessary for reproducibility __lowerCamelCase = generator('''Something there''' , do_sample=a ) self.assertEqual(a , [{'''generated_text''': ''''''}] )
67
'''simple docstring''' import os import shutil import tempfile import unittest import numpy as np from transformers import AutoTokenizer, BarkProcessor from transformers.testing_utils import require_torch, slow @require_torch class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = '''ylacombe/bark-small''' __lowerCamelCase = tempfile.mkdtemp() __lowerCamelCase = '''en_speaker_1''' __lowerCamelCase = '''This is a test string''' __lowerCamelCase = '''speaker_embeddings_path.json''' __lowerCamelCase = '''speaker_embeddings''' def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , **a : Dict ): """simple docstring""" return AutoTokenizer.from_pretrained(self.checkpoint , **a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) processor.save_pretrained(self.tmpdirname ) __lowerCamelCase = BarkProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) @slow def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) processor.save_pretrained( self.tmpdirname , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , speaker_embeddings_directory=self.speaker_embeddings_directory , ) __lowerCamelCase = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) __lowerCamelCase = BarkProcessor.from_pretrained( self.tmpdirname , self.speaker_embeddings_dict_path , bos_token='''(BOS)''' , eos_token='''(EOS)''' , ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) __lowerCamelCase = 35 __lowerCamelCase = 2 __lowerCamelCase = 8 __lowerCamelCase = { '''semantic_prompt''': np.ones(a ), '''coarse_prompt''': np.ones((nb_codebooks_coarse, seq_len) ), '''fine_prompt''': np.ones((nb_codebooks_total, seq_len) ), } # test providing already loaded voice_preset __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from npz file __lowerCamelCase = os.path.join(self.tmpdirname , '''file.npz''' ) np.savez(a , **a ) __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from the hub __lowerCamelCase = processor(text=self.input_string , voice_preset=self.voice_preset ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) __lowerCamelCase = processor(text=self.input_string ) __lowerCamelCase = tokenizer( self.input_string , padding='''max_length''' , max_length=2_56 , add_special_tokens=a , return_attention_mask=a , return_token_type_ids=a , ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key].squeeze().tolist() )
67
1
'''simple docstring''' import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={"vocab_file": "spm_char.model"} __UpperCAmelCase ={ "vocab_file": { "microsoft/speecht5_asr": "https://huggingface.co/microsoft/speecht5_asr/resolve/main/spm_char.model", "microsoft/speecht5_tts": "https://huggingface.co/microsoft/speecht5_tts/resolve/main/spm_char.model", "microsoft/speecht5_vc": "https://huggingface.co/microsoft/speecht5_vc/resolve/main/spm_char.model", } } __UpperCAmelCase ={ "microsoft/speecht5_asr": 1_0_2_4, "microsoft/speecht5_tts": 1_0_2_4, "microsoft/speecht5_vc": 1_0_2_4, } class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] =VOCAB_FILES_NAMES lowerCamelCase : Optional[Any] =PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : Union[str, Any] =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : int =["input_ids", "attention_mask"] def __init__( self : List[Any] , a : Dict , a : Tuple="<s>" , a : List[Any]="</s>" , a : Union[str, Any]="<unk>" , a : List[Any]="<pad>" , a : Optional[Dict[str, Any]] = None , **a : Dict , ): """simple docstring""" __lowerCamelCase = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=a , eos_token=a , unk_token=a , pad_token=a , sp_model_kwargs=self.sp_model_kwargs , **a , ) __lowerCamelCase = vocab_file __lowerCamelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(a ) @property def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" return self.sp_model.get_piece_size() def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = {self.convert_ids_to_tokens(a ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Optional[int] ): """simple docstring""" __lowerCamelCase = self.__dict__.copy() __lowerCamelCase = None return state def __setstate__( self : Tuple , a : Dict ): """simple docstring""" __lowerCamelCase = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): __lowerCamelCase = {} __lowerCamelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : str ): """simple docstring""" return self.sp_model.encode(a , out_type=a ) def SCREAMING_SNAKE_CASE__ ( self : Any , a : List[str] ): """simple docstring""" return self.sp_model.piece_to_id(a ) def SCREAMING_SNAKE_CASE__ ( self : str , a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = self.sp_model.IdToPiece(a ) return token def SCREAMING_SNAKE_CASE__ ( self : int , a : Dict ): """simple docstring""" __lowerCamelCase = [] __lowerCamelCase = '''''' 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(a ) + token __lowerCamelCase = [] else: current_sub_tokens.append(a ) out_string += self.sp_model.decode(a ) return out_string.strip() def SCREAMING_SNAKE_CASE__ ( self : Any , a : str , a : List[str]=None ): """simple docstring""" if token_ids_a is None: return token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_a + token_ids_a + [self.eos_token_id] def SCREAMING_SNAKE_CASE__ ( self : Any , a : List[int] , a : Optional[List[int]] = None , a : bool = False ): """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=a , token_ids_a=a , already_has_special_tokens=a ) __lowerCamelCase = [1] if token_ids_a is None: return ([0] * len(a )) + suffix_ones return ([0] * len(a )) + ([0] * len(a )) + suffix_ones def SCREAMING_SNAKE_CASE__ ( self : Any , a : str , a : Optional[str] = None ): """simple docstring""" if not os.path.isdir(a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return __lowerCamelCase = os.path.join( a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(a ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , a ) elif not os.path.isfile(self.vocab_file ): with open(a , '''wb''' ) as fi: __lowerCamelCase = self.sp_model.serialized_model_proto() fi.write(a ) return (out_vocab_file,)
67
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase ={"configuration_vit_msn": ["VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP", "ViTMSNConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST", "ViTMSNModel", "ViTMSNForImageClassification", "ViTMSNPreTrainedModel", ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
67
1
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ ) -> int: assert ( isinstance(UpperCamelCase__ , UpperCamelCase__ ) and number_of_steps > 0 ), f"""number_of_steps needs to be positive integer, your input {number_of_steps}""" if number_of_steps == 1: return 1 __lowerCamelCase , __lowerCamelCase = 1, 1 for _ in range(number_of_steps - 1 ): __lowerCamelCase , __lowerCamelCase = current + previous, current return current if __name__ == "__main__": import doctest doctest.testmod()
67
'''simple docstring''' import re from filelock import FileLock try: import nltk __UpperCAmelCase =True except (ImportError, ModuleNotFoundError): __UpperCAmelCase =False if NLTK_AVAILABLE: with FileLock(".lock") as lock: nltk.download("punkt", quiet=True) def __lowerCAmelCase ( UpperCamelCase__ ) -> str: re.sub('''<n>''' , '''''' , UpperCamelCase__ ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(UpperCamelCase__ ) )
67
1
'''simple docstring''' import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from torchvision import transforms from transformers import BitImageProcessor, FocalNetConfig, FocalNetForImageClassification from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[Any]: __lowerCamelCase = [2, 2, 6, 2] if '''tiny''' in model_name else [2, 2, 18, 2] __lowerCamelCase = True if '''large''' in model_name or '''huge''' in model_name else False __lowerCamelCase = True if '''large''' in model_name or '''huge''' in model_name else False __lowerCamelCase = True if '''large''' in model_name or '''huge''' in model_name else False if "large" in model_name or "xlarge" in model_name or "huge" in model_name: if "fl3" in model_name: __lowerCamelCase = [3, 3, 3, 3] __lowerCamelCase = [5, 5, 5, 5] elif "fl4" in model_name: __lowerCamelCase = [4, 4, 4, 4] __lowerCamelCase = [3, 3, 3, 3] if "tiny" in model_name or "small" in model_name or "base" in model_name: __lowerCamelCase = [3, 3, 3, 3] if "lrf" in model_name: __lowerCamelCase = [3, 3, 3, 3] else: __lowerCamelCase = [2, 2, 2, 2] if "tiny" in model_name: __lowerCamelCase = 96 elif "small" in model_name: __lowerCamelCase = 96 elif "base" in model_name: __lowerCamelCase = 1_28 elif "large" in model_name: __lowerCamelCase = 1_92 elif "xlarge" in model_name: __lowerCamelCase = 2_56 elif "huge" in model_name: __lowerCamelCase = 3_52 # set label information __lowerCamelCase = '''huggingface/label-files''' if "large" in model_name or "huge" in model_name: __lowerCamelCase = '''imagenet-22k-id2label.json''' else: __lowerCamelCase = '''imagenet-1k-id2label.json''' __lowerCamelCase = json.load(open(hf_hub_download(UpperCamelCase__ , UpperCamelCase__ , repo_type='''dataset''' ) , '''r''' ) ) __lowerCamelCase = {int(UpperCamelCase__ ): v for k, v in idalabel.items()} __lowerCamelCase = {v: k for k, v in idalabel.items()} __lowerCamelCase = FocalNetConfig( embed_dim=UpperCamelCase__ , depths=UpperCamelCase__ , focal_levels=UpperCamelCase__ , focal_windows=UpperCamelCase__ , use_conv_embed=UpperCamelCase__ , idalabel=UpperCamelCase__ , labelaid=UpperCamelCase__ , use_post_layernorm=UpperCamelCase__ , use_layerscale=UpperCamelCase__ , ) return config def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[Any]: if "patch_embed.proj" in name: __lowerCamelCase = name.replace('''patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) if "patch_embed.norm" in name: __lowerCamelCase = name.replace('''patch_embed.norm''' , '''embeddings.norm''' ) if "layers" in name: __lowerCamelCase = '''encoder.''' + name if "encoder.layers" in name: __lowerCamelCase = name.replace('''encoder.layers''' , '''encoder.stages''' ) if "downsample.proj" in name: __lowerCamelCase = name.replace('''downsample.proj''' , '''downsample.projection''' ) if "blocks" in name: __lowerCamelCase = name.replace('''blocks''' , '''layers''' ) if "modulation.f.weight" in name or "modulation.f.bias" in name: __lowerCamelCase = name.replace('''modulation.f''' , '''modulation.projection_in''' ) if "modulation.h.weight" in name or "modulation.h.bias" in name: __lowerCamelCase = name.replace('''modulation.h''' , '''modulation.projection_context''' ) if "modulation.proj.weight" in name or "modulation.proj.bias" in name: __lowerCamelCase = name.replace('''modulation.proj''' , '''modulation.projection_out''' ) if name == "norm.weight": __lowerCamelCase = '''layernorm.weight''' if name == "norm.bias": __lowerCamelCase = '''layernorm.bias''' if "head" in name: __lowerCamelCase = name.replace('''head''' , '''classifier''' ) else: __lowerCamelCase = '''focalnet.''' + name return name def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=False ) -> Optional[Any]: # fmt: off __lowerCamelCase = { '''focalnet-tiny''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_srf.pth''', '''focalnet-tiny-lrf''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_lrf.pth''', '''focalnet-small''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_srf.pth''', '''focalnet-small-lrf''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_lrf.pth''', '''focalnet-base''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_srf.pth''', '''focalnet-base-lrf''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_lrf.pth''', '''focalnet-large-lrf-fl3''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384.pth''', '''focalnet-large-lrf-fl4''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384_fl4.pth''', '''focalnet-xlarge-lrf-fl3''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384.pth''', '''focalnet-xlarge-lrf-fl4''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384_fl4.pth''', } # fmt: on __lowerCamelCase = model_name_to_url[model_name] print('''Checkpoint URL: ''' , UpperCamelCase__ ) __lowerCamelCase = torch.hub.load_state_dict_from_url(UpperCamelCase__ , map_location='''cpu''' )['''model'''] # rename keys for key in state_dict.copy().keys(): __lowerCamelCase = state_dict.pop(UpperCamelCase__ ) __lowerCamelCase = val __lowerCamelCase = get_focalnet_config(UpperCamelCase__ ) __lowerCamelCase = FocalNetForImageClassification(UpperCamelCase__ ) model.eval() # load state dict model.load_state_dict(UpperCamelCase__ ) # verify conversion __lowerCamelCase = '''http://images.cocodataset.org/val2017/000000039769.jpg''' __lowerCamelCase = BitImageProcessor( do_resize=UpperCamelCase__ , size={'''shortest_edge''': 2_56} , resample=PILImageResampling.BILINEAR , do_center_crop=UpperCamelCase__ , crop_size=2_24 , do_normalize=UpperCamelCase__ , image_mean=UpperCamelCase__ , image_std=UpperCamelCase__ , ) __lowerCamelCase = Image.open(requests.get(UpperCamelCase__ , stream=UpperCamelCase__ ).raw ) __lowerCamelCase = processor(images=UpperCamelCase__ , return_tensors='''pt''' ) __lowerCamelCase = transforms.Compose( [ transforms.Resize(2_56 ), transforms.CenterCrop(2_24 ), transforms.ToTensor(), transforms.Normalize(mean=[0.4_8_5, 0.4_5_6, 0.4_0_6] , std=[0.2_2_9, 0.2_2_4, 0.2_2_5] ), ] ) __lowerCamelCase = image_transforms(UpperCamelCase__ ).unsqueeze(0 ) # verify pixel_values assert torch.allclose(inputs.pixel_values , UpperCamelCase__ , atol=1E-4 ) __lowerCamelCase = model(**UpperCamelCase__ ) __lowerCamelCase = outputs.logits.argmax(-1 ).item() print('''Predicted class:''' , model.config.idalabel[predicted_class_idx] ) print('''First values of logits:''' , outputs.logits[0, :3] ) if model_name == "focalnet-tiny": __lowerCamelCase = torch.tensor([0.2_1_6_6, -0.4_3_6_8, 0.2_1_9_1] ) elif model_name == "focalnet-tiny-lrf": __lowerCamelCase = torch.tensor([1.1_6_6_9, 0.0_1_2_5, -0.1_6_9_5] ) elif model_name == "focalnet-small": __lowerCamelCase = torch.tensor([0.4_9_1_7, -0.0_4_3_0, 0.1_3_4_1] ) elif model_name == "focalnet-small-lrf": __lowerCamelCase = torch.tensor([-0.2_5_8_8, -0.5_3_4_2, -0.2_3_3_1] ) elif model_name == "focalnet-base": __lowerCamelCase = torch.tensor([-0.1_6_5_5, -0.4_0_9_0, -0.1_7_3_0] ) elif model_name == "focalnet-base-lrf": __lowerCamelCase = torch.tensor([0.5_3_0_6, -0.0_4_8_3, -0.3_9_2_8] ) assert torch.allclose(outputs.logits[0, :3] , UpperCamelCase__ , atol=1E-4 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(f"""Saving model and processor of {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(UpperCamelCase__ ) processor.save_pretrained(UpperCamelCase__ ) if push_to_hub: print(f"""Pushing model and processor of {model_name} to the hub...""" ) model.push_to_hub(f"""{model_name}""" ) processor.push_to_hub(f"""{model_name}""" ) if __name__ == "__main__": __UpperCAmelCase =argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="focalnet-tiny", type=str, help="Name of the FocalNet 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 and processor to the hub.", ) __UpperCAmelCase =parser.parse_args() convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
67
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={ "abeja/gpt-neox-japanese-2.7b": "https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json", } class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] ="gpt_neox_japanese" def __init__( self : List[Any] , a : Tuple=3_20_00 , a : Dict=25_60 , a : Union[str, Any]=32 , a : Dict=32 , a : Dict=4 , a : Optional[Any]="gelu" , a : Any=1.00 , a : str=1_00_00 , a : List[str]=20_48 , a : str=0.02 , a : Union[str, Any]=1e-5 , a : Optional[Any]=True , a : str=3_19_96 , a : List[str]=3_19_99 , a : str=0.1 , a : Union[str, Any]=0.0 , **a : Optional[Any] , ): """simple docstring""" super().__init__(bos_token_id=a , eos_token_id=a , **a ) __lowerCamelCase = vocab_size __lowerCamelCase = max_position_embeddings __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_multiple_size __lowerCamelCase = hidden_act __lowerCamelCase = rotary_pct __lowerCamelCase = rotary_emb_base __lowerCamelCase = initializer_range __lowerCamelCase = layer_norm_eps __lowerCamelCase = use_cache __lowerCamelCase = attention_dropout __lowerCamelCase = hidden_dropout
67
1
'''simple docstring''' from torch import nn def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[Any]: if act_fn in ["swish", "silu"]: return nn.SiLU() elif act_fn == "mish": return nn.Mish() elif act_fn == "gelu": return nn.GELU() else: raise ValueError(f"""Unsupported activation function: {act_fn}""" )
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> int: while second != 0: __lowerCamelCase = first & second first ^= second __lowerCamelCase = c << 1 return first if __name__ == "__main__": import doctest doctest.testmod() __UpperCAmelCase =int(input("Enter the first number: ").strip()) __UpperCAmelCase =int(input("Enter the second number: ").strip()) print(f'{add(first, second) = }')
67
1
'''simple docstring''' import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv("TEST_SAGEMAKER" , "False" ) ) is not True , reason="Skipping test because should only be run when releasing minor transformers version" , ) @pytest.mark.usefixtures("sm_env" ) @parameterized_class( [ { "framework": "pytorch", "script": "run_glue.py", "model_name_or_path": "distilbert-base-cased", "instance_type": "ml.g4dn.xlarge", "results": {"train_runtime": 6_5_0, "eval_accuracy": 0.6, "eval_loss": 0.9}, }, { "framework": "tensorflow", "script": "run_tf.py", "model_name_or_path": "distilbert-base-cased", "instance_type": "ml.g4dn.xlarge", "results": {"train_runtime": 6_0_0, "eval_accuracy": 0.3, "eval_loss": 0.9}, }, ] ) class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" if self.framework == "pytorch": subprocess.run( f"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split() , encoding='''utf-8''' , check=a , ) assert hasattr(self , '''env''' ) def SCREAMING_SNAKE_CASE__ ( self : int , a : Optional[int]=1 ): """simple docstring""" return HuggingFace( entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=f"""{self.env.base_job_name}-single""" , instance_count=a , instance_type=self.instance_type , debugger_hook_config=a , hyperparameters={**self.env.hyperparameters, '''model_name_or_path''': self.model_name_or_path} , metric_definitions=self.env.metric_definitions , py_version='''py36''' , ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : str ): """simple docstring""" TrainingJobAnalytics(a ).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv""" ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" __lowerCamelCase = self.create_estimator() # run training estimator.fit() # result dataframe __lowerCamelCase = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis __lowerCamelCase = list(result_metrics_df[result_metrics_df.metric_name == '''eval_accuracy''']['''value'''] ) __lowerCamelCase = list(result_metrics_df[result_metrics_df.metric_name == '''eval_loss''']['''value'''] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping __lowerCamelCase = ( Session().describe_training_job(estimator.latest_training_job.name ).get('''TrainingTimeInSeconds''' , 99_99_99 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results['''eval_accuracy'''] for t in eval_accuracy ) assert all(t <= self.results['''eval_loss'''] for t in eval_loss ) # dump tests result into json file to share in PR with open(f"""{estimator.latest_training_job.name}.json""" , '''w''' ) as outfile: json.dump({'''train_time''': train_runtime, '''eval_accuracy''': eval_accuracy, '''eval_loss''': eval_loss} , a )
67
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase ={ "configuration_time_series_transformer": [ "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimeSeriesTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimeSeriesTransformerForPrediction", "TimeSeriesTransformerModel", "TimeSeriesTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
67
1
'''simple docstring''' import unittest import numpy as np from transformers import RobertaPreLayerNormConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roberta_prelayernorm.modeling_flax_roberta_prelayernorm import ( FlaxRobertaPreLayerNormForCausalLM, FlaxRobertaPreLayerNormForMaskedLM, FlaxRobertaPreLayerNormForMultipleChoice, FlaxRobertaPreLayerNormForQuestionAnswering, FlaxRobertaPreLayerNormForSequenceClassification, FlaxRobertaPreLayerNormForTokenClassification, FlaxRobertaPreLayerNormModel, ) class a__ ( unittest.TestCase ): def __init__( self : Union[str, Any] , a : Optional[int] , a : str=13 , a : str=7 , a : Optional[Any]=True , a : List[Any]=True , a : str=True , a : str=True , a : Dict=99 , a : Dict=32 , a : Union[str, Any]=5 , a : Tuple=4 , a : List[str]=37 , a : Optional[int]="gelu" , a : Dict=0.1 , a : List[str]=0.1 , a : Any=5_12 , a : int=16 , a : List[Any]=2 , a : List[str]=0.02 , a : Any=4 , ): """simple docstring""" __lowerCamelCase = parent __lowerCamelCase = batch_size __lowerCamelCase = seq_length __lowerCamelCase = is_training __lowerCamelCase = use_attention_mask __lowerCamelCase = use_token_type_ids __lowerCamelCase = use_labels __lowerCamelCase = vocab_size __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_size __lowerCamelCase = hidden_act __lowerCamelCase = hidden_dropout_prob __lowerCamelCase = attention_probs_dropout_prob __lowerCamelCase = max_position_embeddings __lowerCamelCase = type_vocab_size __lowerCamelCase = type_sequence_label_size __lowerCamelCase = initializer_range __lowerCamelCase = num_choices def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCamelCase = None if self.use_attention_mask: __lowerCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) __lowerCamelCase = None if self.use_token_type_ids: __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __lowerCamelCase = RobertaPreLayerNormConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=a , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" __lowerCamelCase = self.prepare_config_and_inputs() __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = config_and_inputs __lowerCamelCase = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = self.prepare_config_and_inputs() __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = config_and_inputs __lowerCamelCase = True __lowerCamelCase = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax # Copied from tests.models.roberta.test_modelling_flax_roberta.FlaxRobertaPreLayerNormModelTest with ROBERTA->ROBERTA_PRELAYERNORM,Roberta->RobertaPreLayerNorm,roberta-base->andreasmadsen/efficient_mlm_m0.40 class a__ ( UpperCAmelCase__ , unittest.TestCase ): lowerCamelCase : Optional[Any] =True lowerCamelCase : Dict =( ( FlaxRobertaPreLayerNormModel, FlaxRobertaPreLayerNormForCausalLM, FlaxRobertaPreLayerNormForMaskedLM, FlaxRobertaPreLayerNormForSequenceClassification, FlaxRobertaPreLayerNormForTokenClassification, FlaxRobertaPreLayerNormForMultipleChoice, FlaxRobertaPreLayerNormForQuestionAnswering, ) if is_flax_available() else () ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = FlaxRobertaPreLayerNormModelTester(self ) @slow def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" for model_class_name in self.all_model_classes: __lowerCamelCase = model_class_name.from_pretrained('''andreasmadsen/efficient_mlm_m0.40''' , from_pt=a ) __lowerCamelCase = model(np.ones((1, 1) ) ) self.assertIsNotNone(a ) @require_flax class a__ ( unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = FlaxRobertaPreLayerNormForMaskedLM.from_pretrained('''andreasmadsen/efficient_mlm_m0.40''' , from_pt=a ) __lowerCamelCase = np.array([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]] , dtype=jnp.intaa ) __lowerCamelCase = model(a )[0] __lowerCamelCase = [1, 11, 5_02_65] self.assertEqual(list(output.shape ) , a ) # compare the actual values for a slice. __lowerCamelCase = np.array( [[[40.48_80, 18.01_99, -5.23_67], [-1.88_77, -4.08_85, 10.70_85], [-2.26_13, -5.61_10, 7.26_65]]] , dtype=np.floataa ) self.assertTrue(np.allclose(output[:, :3, :3] , a , atol=1e-4 ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = FlaxRobertaPreLayerNormModel.from_pretrained('''andreasmadsen/efficient_mlm_m0.40''' , from_pt=a ) __lowerCamelCase = np.array([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]] , dtype=jnp.intaa ) __lowerCamelCase = model(a )[0] # compare the actual values for a slice. __lowerCamelCase = np.array( [[[0.02_08, -0.03_56, 0.02_37], [-0.15_69, -0.04_11, -0.26_26], [0.18_79, 0.01_25, -0.00_89]]] , dtype=np.floataa ) self.assertTrue(np.allclose(output[:, :3, :3] , a , atol=1e-4 ) )
67
'''simple docstring''' import warnings from pathlib import Path from typing import List, Tuple, Union import fire from torch import nn from transformers import AutoModelForSeqaSeqLM, AutoTokenizer, PreTrainedModel from transformers.utils import logging __UpperCAmelCase =logging.get_logger(__name__) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> None: __lowerCamelCase = nn.ModuleList([src_layers[i] for i in layers_to_copy] ) assert len(UpperCamelCase__ ) == len(UpperCamelCase__ ), f"""{len(UpperCamelCase__ )} != {len(UpperCamelCase__ )}""" dest_layers.load_state_dict(layers_to_copy.state_dict() ) __UpperCAmelCase ={ # maps num layers in teacher -> num_layers in student -> which teacher layers to copy. # 12: bart, 16: pegasus, 6: marian/Helsinki-NLP 1_2: { 1: [0], # This says that if the teacher has 12 layers and the student has 1, copy layer 0 of the teacher 2: [0, 6], 3: [0, 6, 1_1], 4: [0, 4, 8, 1_1], 6: [0, 2, 4, 7, 9, 1_1], 9: [0, 1, 2, 4, 5, 7, 9, 1_0, 1_1], 1_2: list(range(1_2)), }, 1_6: { # maps num layers in student -> which teacher layers to copy 1: [0], 2: [0, 1_5], 3: [0, 8, 1_5], 4: [0, 5, 1_0, 1_5], 6: [0, 3, 6, 9, 1_2, 1_5], 8: [0, 2, 4, 6, 8, 1_0, 1_2, 1_5], 9: [0, 1, 3, 5, 7, 9, 1_1, 1_3, 1_5], 1_2: [0, 1, 2, 3, 4, 5, 6, 7, 9, 1_1, 1_3, 1_5], 1_6: list(range(1_6)), }, 6: {1: [0], 2: [0, 5], 3: [0, 2, 5], 4: [0, 1, 3, 5], 6: list(range(6))}, } __UpperCAmelCase ={ # maps num layers in student -> which teacher layers to copy. 6: {1: [5], 2: [3, 5], 3: [1, 4, 5], 4: [1, 2, 4, 5]}, 1_2: {1: [1_1], 2: [5, 1_1], 3: [3, 7, 1_1], 6: [1, 3, 5, 8, 1_0, 1_1]}, 1_6: {1: [1_5], 4: [4, 9, 1_2, 1_5], 8: [1, 3, 5, 7, 9, 1_1, 1_3, 1_5]}, } def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Union[str, Any]: try: __lowerCamelCase = LAYERS_TO_COPY[n_teacher][n_student] return val except KeyError: if n_student != n_teacher: warnings.warn( f"""no hardcoded layers to copy for teacher {n_teacher} -> student {n_student}, defaulting to first""" f""" {n_student}""" ) return list(range(UpperCamelCase__ ) ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> List[int]: if n_student > n_teacher: raise ValueError(f"""Cannot perform intermediate supervision for student {n_student} > teacher {n_teacher}""" ) elif n_teacher == n_student: return list(range(UpperCamelCase__ ) ) elif n_student == 1: return [n_teacher - 1] else: return LAYERS_TO_SUPERVISE[n_teacher][n_student] def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ = "student" , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__=False , UpperCamelCase__=None , UpperCamelCase__=None , **UpperCamelCase__ , ) -> Tuple[PreTrainedModel, List[int], List[int]]: __lowerCamelCase = '''encoder_layers and decoder_layers cannot be both None-- you would just have an identical teacher.''' assert (e is not None) or (d is not None), _msg if isinstance(UpperCamelCase__ , UpperCamelCase__ ): AutoTokenizer.from_pretrained(UpperCamelCase__ ).save_pretrained(UpperCamelCase__ ) # purely for convenience __lowerCamelCase = AutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__ ).eval() else: assert isinstance(UpperCamelCase__ , UpperCamelCase__ ), f"""teacher must be a model or string got type {type(UpperCamelCase__ )}""" __lowerCamelCase = teacher.config.to_diff_dict() try: __lowerCamelCase , __lowerCamelCase = teacher.config.encoder_layers, teacher.config.decoder_layers if e is None: __lowerCamelCase = teacher_e if d is None: __lowerCamelCase = teacher_d init_kwargs.update({'''encoder_layers''': e, '''decoder_layers''': d} ) except AttributeError: # T5 if hasattr(teacher.config , '''num_encoder_layers''' ): __lowerCamelCase , __lowerCamelCase = teacher.config.num_encoder_layers, teacher.config.num_decoder_layers else: __lowerCamelCase , __lowerCamelCase = teacher.config.num_layers, teacher.config.num_decoder_layers if e is None: __lowerCamelCase = teacher_e if d is None: __lowerCamelCase = teacher_d if hasattr(teacher.config , '''num_encoder_layers''' ): init_kwargs.update({'''num_encoder_layers''': e, '''num_decoder_layers''': d} ) else: init_kwargs.update({'''num_layers''': e, '''num_decoder_layers''': d} ) # Kwargs to instantiate student: teacher kwargs with updated layer numbers + **extra_config_kwargs init_kwargs.update(UpperCamelCase__ ) # Copy weights __lowerCamelCase = teacher.config_class(**UpperCamelCase__ ) __lowerCamelCase = AutoModelForSeqaSeqLM.from_config(UpperCamelCase__ ) # Start by copying the full teacher state dict this will copy the first N teacher layers to the student. __lowerCamelCase = student.load_state_dict(teacher.state_dict() , strict=UpperCamelCase__ ) assert info.missing_keys == [], info.missing_keys # every student key should have a teacher keys. if copy_first_teacher_layers: # Our copying is done. We just log and save __lowerCamelCase , __lowerCamelCase = list(range(UpperCamelCase__ ) ), list(range(UpperCamelCase__ ) ) logger.info( f"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to""" f""" {save_path}""" ) student.save_pretrained(UpperCamelCase__ ) return student, e_layers_to_copy, d_layers_to_copy # Decide which layers of the teacher to copy. Not exactly alternating -- we try to keep first and last layer. if e_layers_to_copy is None: __lowerCamelCase = pick_layers_to_copy(UpperCamelCase__ , UpperCamelCase__ ) if d_layers_to_copy is None: __lowerCamelCase = pick_layers_to_copy(UpperCamelCase__ , UpperCamelCase__ ) try: if hasattr( UpperCamelCase__ , '''prophetnet''' ): # For ProphetNet, student.model.encoder.layers is called student.prophetnet.encoder.layers copy_layers(teacher.prophetnet.encoder.layers , student.prophetnet.encoder.layers , UpperCamelCase__ ) copy_layers(teacher.prophetnet.decoder.layers , student.prophetnet.decoder.layers , UpperCamelCase__ ) else: copy_layers(teacher.model.encoder.layers , student.model.encoder.layers , UpperCamelCase__ ) copy_layers(teacher.model.decoder.layers , student.model.decoder.layers , UpperCamelCase__ ) except AttributeError: # For t5, student.model.encoder.layers is called student.encoder.block copy_layers(teacher.encoder.block , student.encoder.block , UpperCamelCase__ ) copy_layers(teacher.decoder.block , student.decoder.block , UpperCamelCase__ ) logger.info( f"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to {save_path}""" ) __lowerCamelCase = { '''teacher_type''': teacher.config.model_type, '''copied_encoder_layers''': e_layers_to_copy, '''copied_decoder_layers''': d_layers_to_copy, } student.save_pretrained(UpperCamelCase__ ) # Save information about copying for easier reproducibility return student, e_layers_to_copy, d_layers_to_copy if __name__ == "__main__": fire.Fire(create_student_by_copying_alternating_layers)
67
1
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> str: if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): raise ValueError('''iterations must be defined as integers''' ) if not isinstance(UpperCamelCase__ , UpperCamelCase__ ) or not number >= 1: raise ValueError( '''starting number must be and integer and be more than 0''' ) if not iterations >= 1: raise ValueError('''Iterations must be done more than 0 times to play FizzBuzz''' ) __lowerCamelCase = '''''' while number <= iterations: if number % 3 == 0: out += "Fizz" if number % 5 == 0: out += "Buzz" if 0 not in (number % 3, number % 5): out += str(UpperCamelCase__ ) # print(out) number += 1 out += " " return out if __name__ == "__main__": import doctest doctest.testmod()
67
'''simple docstring''' import unittest from pathlib import Path from tempfile import TemporaryDirectory from transformers import AutoConfig, TFGPTaLMHeadModel, is_keras_nlp_available, is_tf_available from transformers.models.gpta.tokenization_gpta import GPTaTokenizer from transformers.testing_utils import require_keras_nlp, require_tf, slow if is_tf_available(): import tensorflow as tf if is_keras_nlp_available(): from transformers.models.gpta import TFGPTaTokenizer __UpperCAmelCase =["gpt2"] __UpperCAmelCase ="gpt2" if is_tf_available(): class a__ ( tf.Module ): def __init__( self : str , a : Union[str, Any] ): """simple docstring""" super().__init__() __lowerCamelCase = tokenizer __lowerCamelCase = AutoConfig.from_pretrained(a ) __lowerCamelCase = TFGPTaLMHeadModel.from_config(a ) @tf.function(input_signature=(tf.TensorSpec((None,) , tf.string , name='''text''' ),) ) def SCREAMING_SNAKE_CASE__ ( self : str , a : Tuple ): """simple docstring""" __lowerCamelCase = self.tokenizer(a ) __lowerCamelCase = tokenized['''input_ids'''].to_tensor() __lowerCamelCase = tf.cast(input_ids_dense > 0 , tf.intaa ) # input_mask = tf.reshape(input_mask, [-1, MAX_SEQ_LEN]) __lowerCamelCase = self.model(input_ids=a , attention_mask=a )['''logits'''] return outputs @require_tf @require_keras_nlp class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" super().setUp() __lowerCamelCase = [GPTaTokenizer.from_pretrained(a ) for checkpoint in (TOKENIZER_CHECKPOINTS)] __lowerCamelCase = [TFGPTaTokenizer.from_pretrained(a ) for checkpoint in TOKENIZER_CHECKPOINTS] assert len(self.tokenizers ) == len(self.tf_tokenizers ) __lowerCamelCase = [ '''This is a straightforward English test sentence.''', '''This one has some weird characters\rto\nsee\r\nif those\u00E9break things.''', '''Now we\'re going to add some Chinese: 一 二 三 一二三''', '''And some much more rare Chinese: 齉 堃 齉堃''', '''Je vais aussi écrire en français pour tester les accents''', '''Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ''', ] __lowerCamelCase = list(zip(self.test_sentences , self.test_sentences[::-1] ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" for tokenizer, tf_tokenizer in zip(self.tokenizers , self.tf_tokenizers ): for test_inputs in self.test_sentences: __lowerCamelCase = tokenizer([test_inputs] , return_tensors='''tf''' ) __lowerCamelCase = tf_tokenizer([test_inputs] ) for key in python_outputs.keys(): # convert them to numpy to avoid messing with ragged tensors __lowerCamelCase = python_outputs[key].numpy() __lowerCamelCase = tf_outputs[key].numpy() self.assertTrue(tf.reduce_all(python_outputs_values.shape == tf_outputs_values.shape ) ) self.assertTrue(tf.reduce_all(tf.cast(a , tf.intaa ) == tf_outputs_values ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: __lowerCamelCase = tf.function(a ) for test_inputs in self.test_sentences: __lowerCamelCase = tf.constant(a ) __lowerCamelCase = compiled_tokenizer(a ) __lowerCamelCase = tf_tokenizer(a ) for key in eager_outputs.keys(): self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key] ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: __lowerCamelCase = ModelToSave(tokenizer=a ) __lowerCamelCase = tf.convert_to_tensor([self.test_sentences[0]] ) __lowerCamelCase = model.serving(a ) # Build model with some sample inputs with TemporaryDirectory() as tempdir: __lowerCamelCase = Path(a ) / '''saved.model''' tf.saved_model.save(a , a , signatures={'''serving_default''': model.serving} ) __lowerCamelCase = tf.saved_model.load(a ) __lowerCamelCase = loaded_model.signatures['''serving_default'''](a )['''output_0'''] # We may see small differences because the loaded model is compiled, so we need an epsilon for the test self.assertTrue(tf.reduce_all(out == loaded_output ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: __lowerCamelCase = tf.convert_to_tensor([self.test_sentences[0]] ) __lowerCamelCase = tf_tokenizer(a ) # Build model with some sample inputs __lowerCamelCase = tf_tokenizer.get_config() __lowerCamelCase = TFGPTaTokenizer.from_config(a ) __lowerCamelCase = model_from_config(a ) for key in from_config_output.keys(): self.assertTrue(tf.reduce_all(from_config_output[key] == out[key] ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: # for the test to run __lowerCamelCase = 12_31_23 for max_length in [3, 5, 10_24]: __lowerCamelCase = tf.convert_to_tensor([self.test_sentences[0]] ) __lowerCamelCase = tf_tokenizer(a , max_length=a ) __lowerCamelCase = out['''input_ids'''].numpy().shape[1] assert out_length == max_length
67
1
'''simple docstring''' import argparse import re import torch from CLAP import create_model from transformers import AutoFeatureExtractor, ClapConfig, ClapModel __UpperCAmelCase ={ "text_branch": "text_model", "audio_branch": "audio_model.audio_encoder", "attn": "attention.self", "self.proj": "output.dense", "attention.self_mask": "attn_mask", "mlp.fc1": "intermediate.dense", "mlp.fc2": "output.dense", "norm1": "layernorm_before", "norm2": "layernorm_after", "bn0": "batch_norm", } __UpperCAmelCase =AutoFeatureExtractor.from_pretrained("laion/clap-htsat-unfused", truncation="rand_trunc") def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__=False ) -> List[str]: __lowerCamelCase , __lowerCamelCase = create_model( '''HTSAT-tiny''' , '''roberta''' , UpperCamelCase__ , precision='''fp32''' , device='''cuda:0''' if torch.cuda.is_available() else '''cpu''' , enable_fusion=UpperCamelCase__ , fusion_type='''aff_2d''' if enable_fusion else None , ) return model, model_cfg def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[Any]: __lowerCamelCase = {} __lowerCamelCase = r'''.*sequential.(\d+).*''' __lowerCamelCase = r'''.*_projection.(\d+).*''' for key, value in state_dict.items(): # check if any key needs to be modified for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items(): if key_to_modify in key: __lowerCamelCase = key.replace(UpperCamelCase__ , UpperCamelCase__ ) if re.match(UpperCamelCase__ , UpperCamelCase__ ): # replace sequential layers with list __lowerCamelCase = re.match(UpperCamelCase__ , UpperCamelCase__ ).group(1 ) __lowerCamelCase = key.replace(f"""sequential.{sequential_layer}.""" , f"""layers.{int(UpperCamelCase__ )//3}.linear.""" ) elif re.match(UpperCamelCase__ , UpperCamelCase__ ): __lowerCamelCase = int(re.match(UpperCamelCase__ , UpperCamelCase__ ).group(1 ) ) # Because in CLAP they use `nn.Sequential`... __lowerCamelCase = 1 if projecton_layer == 0 else 2 __lowerCamelCase = key.replace(f"""_projection.{projecton_layer}.""" , f"""_projection.linear{transformers_projection_layer}.""" ) if "audio" and "qkv" in key: # split qkv into query key and value __lowerCamelCase = value __lowerCamelCase = mixed_qkv.size(0 ) // 3 __lowerCamelCase = mixed_qkv[:qkv_dim] __lowerCamelCase = mixed_qkv[qkv_dim : qkv_dim * 2] __lowerCamelCase = mixed_qkv[qkv_dim * 2 :] __lowerCamelCase = query_layer __lowerCamelCase = key_layer __lowerCamelCase = value_layer else: __lowerCamelCase = value return model_state_dict def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=False ) -> Optional[int]: __lowerCamelCase , __lowerCamelCase = init_clap(UpperCamelCase__ , enable_fusion=UpperCamelCase__ ) clap_model.eval() __lowerCamelCase = clap_model.state_dict() __lowerCamelCase = rename_state_dict(UpperCamelCase__ ) __lowerCamelCase = ClapConfig() __lowerCamelCase = enable_fusion __lowerCamelCase = ClapModel(UpperCamelCase__ ) # ignore the spectrogram embedding layer model.load_state_dict(UpperCamelCase__ , strict=UpperCamelCase__ ) model.save_pretrained(UpperCamelCase__ ) transformers_config.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") parser.add_argument("--enable_fusion", action="store_true", help="Whether to enable fusion or not") __UpperCAmelCase =parser.parse_args() convert_clap_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.enable_fusion)
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Optional[int]: if b == 0: return 1 if (b % 2) == 0: return actual_power(UpperCamelCase__ , int(b / 2 ) ) * actual_power(UpperCamelCase__ , int(b / 2 ) ) else: return a * actual_power(UpperCamelCase__ , int(b / 2 ) ) * actual_power(UpperCamelCase__ , int(b / 2 ) ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> float: if b < 0: return 1 / actual_power(UpperCamelCase__ , UpperCamelCase__ ) return actual_power(UpperCamelCase__ , UpperCamelCase__ ) if __name__ == "__main__": print(power(-2, -3))
67
1
'''simple docstring''' import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={"vocab_file": "vocab.json", "merges_file": "merges.txt"} # See all BART models at https://huggingface.co/models?filter=bart __UpperCAmelCase ={ "vocab_file": { "facebook/bart-base": "https://huggingface.co/facebook/bart-base/resolve/main/vocab.json", "facebook/bart-large": "https://huggingface.co/facebook/bart-large/resolve/main/vocab.json", "facebook/bart-large-mnli": "https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json", "facebook/bart-large-cnn": "https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json", "facebook/bart-large-xsum": "https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json", "yjernite/bart_eli5": "https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json", }, "merges_file": { "facebook/bart-base": "https://huggingface.co/facebook/bart-base/resolve/main/merges.txt", "facebook/bart-large": "https://huggingface.co/facebook/bart-large/resolve/main/merges.txt", "facebook/bart-large-mnli": "https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt", "facebook/bart-large-cnn": "https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt", "facebook/bart-large-xsum": "https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt", "yjernite/bart_eli5": "https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt", }, } __UpperCAmelCase ={ "facebook/bart-base": 1_0_2_4, "facebook/bart-large": 1_0_2_4, "facebook/bart-large-mnli": 1_0_2_4, "facebook/bart-large-cnn": 1_0_2_4, "facebook/bart-large-xsum": 1_0_2_4, "yjernite/bart_eli5": 1_0_2_4, } @lru_cache() def __lowerCAmelCase ( ) -> Union[str, Any]: __lowerCamelCase = ( list(range(ord('''!''' ) , ord('''~''' ) + 1 ) ) + list(range(ord('''¡''' ) , ord('''¬''' ) + 1 ) ) + list(range(ord('''®''' ) , ord('''ÿ''' ) + 1 ) ) ) __lowerCamelCase = bs[:] __lowerCamelCase = 0 for b in range(2**8 ): if b not in bs: bs.append(UpperCamelCase__ ) cs.append(2**8 + n ) n += 1 __lowerCamelCase = [chr(UpperCamelCase__ ) for n in cs] return dict(zip(UpperCamelCase__ , UpperCamelCase__ ) ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Dict: __lowerCamelCase = set() __lowerCamelCase = word[0] for char in word[1:]: pairs.add((prev_char, char) ) __lowerCamelCase = char return pairs class a__ ( UpperCAmelCase__ ): lowerCamelCase : Tuple =VOCAB_FILES_NAMES lowerCamelCase : Any =PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : Dict =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Any =["input_ids", "attention_mask"] def __init__( self : List[str] , a : Tuple , a : Any , a : Optional[Any]="replace" , a : int="<s>" , a : Union[str, Any]="</s>" , a : int="</s>" , a : Tuple="<s>" , a : Any="<unk>" , a : Tuple="<pad>" , a : Dict="<mask>" , a : Any=False , **a : str , ): """simple docstring""" __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else bos_token __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else eos_token __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else sep_token __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else cls_token __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else unk_token __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else pad_token # Mask token behave like a normal word, i.e. include the space before it __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else mask_token super().__init__( errors=a , bos_token=a , eos_token=a , unk_token=a , sep_token=a , cls_token=a , pad_token=a , mask_token=a , add_prefix_space=a , **a , ) with open(a , encoding='''utf-8''' ) as vocab_handle: __lowerCamelCase = json.load(a ) __lowerCamelCase = {v: k for k, v in self.encoder.items()} __lowerCamelCase = errors # how to handle errors in decoding __lowerCamelCase = bytes_to_unicode() __lowerCamelCase = {v: k for k, v in self.byte_encoder.items()} with open(a , encoding='''utf-8''' ) as merges_handle: __lowerCamelCase = merges_handle.read().split('''\n''' )[1:-1] __lowerCamelCase = [tuple(merge.split() ) for merge in bpe_merges] __lowerCamelCase = dict(zip(a , range(len(a ) ) ) ) __lowerCamelCase = {} __lowerCamelCase = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions __lowerCamelCase = re.compile(R'''\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+''' ) @property def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" return len(self.encoder ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" return dict(self.encoder , **self.added_tokens_encoder ) def SCREAMING_SNAKE_CASE__ ( self : int , a : List[Any] ): """simple docstring""" if token in self.cache: return self.cache[token] __lowerCamelCase = tuple(a ) __lowerCamelCase = get_pairs(a ) if not pairs: return token while True: __lowerCamelCase = min(a , key=lambda a : self.bpe_ranks.get(a , float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break __lowerCamelCase , __lowerCamelCase = bigram __lowerCamelCase = [] __lowerCamelCase = 0 while i < len(a ): try: __lowerCamelCase = word.index(a , a ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) __lowerCamelCase = j if word[i] == first and i < len(a ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 __lowerCamelCase = tuple(a ) __lowerCamelCase = new_word if len(a ) == 1: break else: __lowerCamelCase = get_pairs(a ) __lowerCamelCase = ''' '''.join(a ) __lowerCamelCase = word return word def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = [] for token in re.findall(self.pat , a ): __lowerCamelCase = ''''''.join( self.byte_encoder[b] for b in token.encode('''utf-8''' ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(a ).split(''' ''' ) ) return bpe_tokens def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : Union[str, Any] ): """simple docstring""" return self.encoder.get(a , self.encoder.get(self.unk_token ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Dict ): """simple docstring""" return self.decoder.get(a ) def SCREAMING_SNAKE_CASE__ ( self : Any , a : int ): """simple docstring""" __lowerCamelCase = ''''''.join(a ) __lowerCamelCase = bytearray([self.byte_decoder[c] for c in text] ).decode('''utf-8''' , errors=self.errors ) return text def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : str , a : Optional[str] = None ): """simple docstring""" if not os.path.isdir(a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return __lowerCamelCase = os.path.join( a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) __lowerCamelCase = os.path.join( a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file'''] ) with open(a , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=a , ensure_ascii=a ) + '''\n''' ) __lowerCamelCase = 0 with open(a , '''w''' , encoding='''utf-8''' ) as writer: writer.write('''#version: 0.2\n''' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda a : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.""" ''' Please check that the tokenizer is not corrupted!''' ) __lowerCamelCase = token_index writer.write(''' '''.join(a ) + '''\n''' ) index += 1 return vocab_file, merge_file def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __lowerCamelCase = [self.cls_token_id] __lowerCamelCase = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE__ ( self : Dict , a : List[int] , a : Optional[List[int]] = None , a : bool = False ): """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=a , token_ids_a=a , already_has_special_tokens=a ) if token_ids_a is None: return [1] + ([0] * len(a )) + [1] return [1] + ([0] * len(a )) + [1, 1] + ([0] * len(a )) + [1] def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" __lowerCamelCase = [self.sep_token_id] __lowerCamelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def SCREAMING_SNAKE_CASE__ ( self : int , a : Any , a : List[Any]=False , **a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = kwargs.pop('''add_prefix_space''' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(a ) > 0 and not text[0].isspace()): __lowerCamelCase = ''' ''' + text return (text, kwargs)
67
'''simple docstring''' import logging import os from .state import PartialState class a__ ( logging.LoggerAdapter ): @staticmethod def SCREAMING_SNAKE_CASE__ ( a : Optional[Any] ): """simple docstring""" __lowerCamelCase = PartialState() return not main_process_only or (main_process_only and state.is_main_process) def SCREAMING_SNAKE_CASE__ ( self : int , a : Optional[int] , a : str , *a : Optional[int] , **a : List[Any] ): """simple docstring""" if PartialState._shared_state == {}: raise RuntimeError( '''You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.''' ) __lowerCamelCase = kwargs.pop('''main_process_only''' , a ) __lowerCamelCase = kwargs.pop('''in_order''' , a ) if self.isEnabledFor(a ): if self._should_log(a ): __lowerCamelCase , __lowerCamelCase = self.process(a , a ) self.logger.log(a , a , *a , **a ) elif in_order: __lowerCamelCase = PartialState() for i in range(state.num_processes ): if i == state.process_index: __lowerCamelCase , __lowerCamelCase = self.process(a , a ) self.logger.log(a , a , *a , **a ) state.wait_for_everyone() def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ = None ) -> Optional[int]: if log_level is None: __lowerCamelCase = os.environ.get('''ACCELERATE_LOG_LEVEL''' , UpperCamelCase__ ) __lowerCamelCase = logging.getLogger(UpperCamelCase__ ) if log_level is not None: logger.setLevel(log_level.upper() ) logger.root.setLevel(log_level.upper() ) return MultiProcessAdapter(UpperCamelCase__ , {} )
67
1
'''simple docstring''' import re from filelock import FileLock try: import nltk __UpperCAmelCase =True except (ImportError, ModuleNotFoundError): __UpperCAmelCase =False if NLTK_AVAILABLE: with FileLock(".lock") as lock: nltk.download("punkt", quiet=True) def __lowerCAmelCase ( UpperCamelCase__ ) -> str: re.sub('''<n>''' , '''''' , UpperCamelCase__ ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(UpperCamelCase__ ) )
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[Any]: __lowerCamelCase = [] __lowerCamelCase = set({'''(''', '''[''', '''{'''} ) __lowerCamelCase = set({''')''', ''']''', '''}'''} ) __lowerCamelCase = {'''{''': '''}''', '''[''': ''']''', '''(''': ''')'''} for i in range(len(UpperCamelCase__ ) ): if s[i] in open_brackets: stack.append(s[i] ) elif s[i] in closed_brackets and ( len(UpperCamelCase__ ) == 0 or (len(UpperCamelCase__ ) > 0 and open_to_closed[stack.pop()] != s[i]) ): return False return len(UpperCamelCase__ ) == 0 def __lowerCAmelCase ( ) -> str: __lowerCamelCase = input('''Enter sequence of brackets: ''' ) if is_balanced(UpperCamelCase__ ): print(UpperCamelCase__ , '''is balanced''' ) else: print(UpperCamelCase__ , '''is not balanced''' ) if __name__ == "__main__": main()
67
1
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ ) -> int: if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): raise ValueError('''Input must be an integer''' ) if input_num <= 0: raise ValueError('''Input must be positive''' ) return sum( divisor for divisor in range(1 , input_num // 2 + 1 ) if input_num % divisor == 0 ) if __name__ == "__main__": import doctest doctest.testmod()
67
'''simple docstring''' import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class a__ ( UpperCAmelCase__ ): lowerCamelCase : Dict ="M-CLIP" def __init__( self : Tuple , a : Optional[int]=10_24 , a : Tuple=7_68 , **a : List[str] ): """simple docstring""" __lowerCamelCase = transformerDimSize __lowerCamelCase = imageDimSize super().__init__(**a ) class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[Any] =MCLIPConfig def __init__( self : str , a : List[Any] , *a : Dict , **a : str ): """simple docstring""" super().__init__(a , *a , **a ) __lowerCamelCase = XLMRobertaModel(a ) __lowerCamelCase = torch.nn.Linear( in_features=config.transformerDimensions , out_features=config.numDims ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : int , a : List[Any] ): """simple docstring""" __lowerCamelCase = self.transformer(input_ids=a , attention_mask=a )[0] __lowerCamelCase = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(a ), embs
67
1
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_barthez import BarthezTokenizer else: __UpperCAmelCase =None __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase ={ "vocab_file": { "moussaKam/mbarthez": "https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model", "moussaKam/barthez": "https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model", "moussaKam/barthez-orangesum-title": ( "https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "moussaKam/mbarthez": "https://huggingface.co/moussaKam/mbarthez/resolve/main/tokenizer.json", "moussaKam/barthez": "https://huggingface.co/moussaKam/barthez/resolve/main/tokenizer.json", "moussaKam/barthez-orangesum-title": ( "https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/tokenizer.json" ), }, } __UpperCAmelCase ={ "moussaKam/mbarthez": 1_0_2_4, "moussaKam/barthez": 1_0_2_4, "moussaKam/barthez-orangesum-title": 1_0_2_4, } __UpperCAmelCase ="▁" class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] =VOCAB_FILES_NAMES lowerCamelCase : Optional[Any] =PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : int =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[Any] =["input_ids", "attention_mask"] lowerCamelCase : Union[str, Any] =BarthezTokenizer def __init__( self : Optional[Any] , a : Dict=None , a : str=None , a : List[Any]="<s>" , a : Optional[int]="</s>" , a : List[str]="</s>" , a : Tuple="<s>" , a : str="<unk>" , a : Any="<pad>" , a : Union[str, Any]="<mask>" , **a : Union[str, Any] , ): """simple docstring""" __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else mask_token super().__init__( a , tokenizer_file=a , bos_token=a , eos_token=a , unk_token=a , sep_token=a , cls_token=a , pad_token=a , mask_token=a , **a , ) __lowerCamelCase = vocab_file __lowerCamelCase = False if not self.vocab_file else True def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __lowerCamelCase = [self.cls_token_id] __lowerCamelCase = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" __lowerCamelCase = [self.sep_token_id] __lowerCamelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : str , a : Optional[str] = None ): """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return __lowerCamelCase = os.path.join( a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(a ): copyfile(self.vocab_file , a ) return (out_vocab_file,)
67
'''simple docstring''' from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
67
1
'''simple docstring''' import copy from typing import Dict, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING from ..detr import DetrConfig from ..swin import SwinConfig __UpperCAmelCase ={ "facebook/maskformer-swin-base-ade": ( "https://huggingface.co/facebook/maskformer-swin-base-ade/blob/main/config.json" ) # See all MaskFormer models at https://huggingface.co/models?filter=maskformer } __UpperCAmelCase =logging.get_logger(__name__) class a__ ( UpperCAmelCase__ ): lowerCamelCase : Union[str, Any] ="maskformer" lowerCamelCase : Dict ={"hidden_size": "mask_feature_size"} lowerCamelCase : Optional[Any] =["resnet", "swin"] lowerCamelCase : Optional[int] =["detr"] def __init__( self : List[Any] , a : int = 2_56 , a : int = 2_56 , a : float = 0.1 , a : bool = False , a : Optional[Dict] = None , a : Optional[Dict] = None , a : float = 0.02 , a : float = 1.0 , a : float = 1.0 , a : float = 1.0 , a : float = 20.0 , a : Optional[bool] = None , **a : List[Any] , ): """simple docstring""" if backbone_config is None: # fall back to https://huggingface.co/microsoft/swin-base-patch4-window12-384-in22k __lowerCamelCase = SwinConfig( image_size=3_84 , in_channels=3 , patch_size=4 , embed_dim=1_28 , depths=[2, 2, 18, 2] , num_heads=[4, 8, 16, 32] , window_size=12 , drop_path_rate=0.3 , out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] , ) if isinstance(a , a ): __lowerCamelCase = backbone_config.pop('''model_type''' ) __lowerCamelCase = CONFIG_MAPPING[backbone_model_type] __lowerCamelCase = config_class.from_dict(a ) # verify that the backbone is supported if backbone_config.model_type not in self.backbones_supported: logger.warning_once( f"""Backbone {backbone_config.model_type} is not a supported model and may not be compatible with MaskFormer. """ f"""Supported model types: {','.join(self.backbones_supported )}""" ) if decoder_config is None: # fall back to https://huggingface.co/facebook/detr-resnet-50 __lowerCamelCase = DetrConfig() else: # verify that the decoder is supported __lowerCamelCase = ( decoder_config.pop('''model_type''' ) if isinstance(a , a ) else decoder_config.model_type ) if decoder_type not in self.decoders_supported: raise ValueError( f"""Transformer Decoder {decoder_type} not supported, please use one of""" f""" {','.join(self.decoders_supported )}""" ) if isinstance(a , a ): __lowerCamelCase = CONFIG_MAPPING[decoder_type] __lowerCamelCase = config_class.from_dict(a ) __lowerCamelCase = backbone_config __lowerCamelCase = decoder_config # main feature dimension for the model __lowerCamelCase = fpn_feature_size __lowerCamelCase = mask_feature_size # initializer __lowerCamelCase = init_std __lowerCamelCase = init_xavier_std # Hungarian matcher && loss __lowerCamelCase = cross_entropy_weight __lowerCamelCase = dice_weight __lowerCamelCase = mask_weight __lowerCamelCase = use_auxiliary_loss __lowerCamelCase = no_object_weight __lowerCamelCase = output_auxiliary_logits __lowerCamelCase = self.decoder_config.encoder_attention_heads __lowerCamelCase = self.decoder_config.num_hidden_layers super().__init__(**a ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : str , a : PretrainedConfig , a : PretrainedConfig , **a : Union[str, Any] ): """simple docstring""" return cls( backbone_config=a , decoder_config=a , **a , ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" __lowerCamelCase = copy.deepcopy(self.__dict__ ) __lowerCamelCase = self.backbone_config.to_dict() __lowerCamelCase = self.decoder_config.to_dict() __lowerCamelCase = self.__class__.model_type return output
67
'''simple docstring''' from __future__ import annotations from decimal import Decimal from numpy import array def __lowerCAmelCase ( UpperCamelCase__ ) -> list[list[float]]: __lowerCamelCase = Decimal # Check if the provided matrix has 2 rows and 2 columns # since this implementation only works for 2x2 matrices if len(UpperCamelCase__ ) == 2 and len(matrix[0] ) == 2 and len(matrix[1] ) == 2: # Calculate the determinant of the matrix __lowerCamelCase = float( d(matrix[0][0] ) * d(matrix[1][1] ) - d(matrix[1][0] ) * d(matrix[0][1] ) ) if determinant == 0: raise ValueError('''This matrix has no inverse.''' ) # Creates a copy of the matrix with swapped positions of the elements __lowerCamelCase = [[0.0, 0.0], [0.0, 0.0]] __lowerCamelCase , __lowerCamelCase = matrix[1][1], matrix[0][0] __lowerCamelCase , __lowerCamelCase = -matrix[1][0], -matrix[0][1] # Calculate the inverse of the matrix return [ [(float(d(UpperCamelCase__ ) ) / determinant) or 0.0 for n in row] for row in swapped_matrix ] elif ( len(UpperCamelCase__ ) == 3 and len(matrix[0] ) == 3 and len(matrix[1] ) == 3 and len(matrix[2] ) == 3 ): # Calculate the determinant of the matrix using Sarrus rule __lowerCamelCase = float( ( (d(matrix[0][0] ) * d(matrix[1][1] ) * d(matrix[2][2] )) + (d(matrix[0][1] ) * d(matrix[1][2] ) * d(matrix[2][0] )) + (d(matrix[0][2] ) * d(matrix[1][0] ) * d(matrix[2][1] )) ) - ( (d(matrix[0][2] ) * d(matrix[1][1] ) * d(matrix[2][0] )) + (d(matrix[0][1] ) * d(matrix[1][0] ) * d(matrix[2][2] )) + (d(matrix[0][0] ) * d(matrix[1][2] ) * d(matrix[2][1] )) ) ) if determinant == 0: raise ValueError('''This matrix has no inverse.''' ) # Creating cofactor matrix __lowerCamelCase = [ [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], ] __lowerCamelCase = (d(matrix[1][1] ) * d(matrix[2][2] )) - ( d(matrix[1][2] ) * d(matrix[2][1] ) ) __lowerCamelCase = -( (d(matrix[1][0] ) * d(matrix[2][2] )) - (d(matrix[1][2] ) * d(matrix[2][0] )) ) __lowerCamelCase = (d(matrix[1][0] ) * d(matrix[2][1] )) - ( d(matrix[1][1] ) * d(matrix[2][0] ) ) __lowerCamelCase = -( (d(matrix[0][1] ) * d(matrix[2][2] )) - (d(matrix[0][2] ) * d(matrix[2][1] )) ) __lowerCamelCase = (d(matrix[0][0] ) * d(matrix[2][2] )) - ( d(matrix[0][2] ) * d(matrix[2][0] ) ) __lowerCamelCase = -( (d(matrix[0][0] ) * d(matrix[2][1] )) - (d(matrix[0][1] ) * d(matrix[2][0] )) ) __lowerCamelCase = (d(matrix[0][1] ) * d(matrix[1][2] )) - ( d(matrix[0][2] ) * d(matrix[1][1] ) ) __lowerCamelCase = -( (d(matrix[0][0] ) * d(matrix[1][2] )) - (d(matrix[0][2] ) * d(matrix[1][0] )) ) __lowerCamelCase = (d(matrix[0][0] ) * d(matrix[1][1] )) - ( d(matrix[0][1] ) * d(matrix[1][0] ) ) # Transpose the cofactor matrix (Adjoint matrix) __lowerCamelCase = array(UpperCamelCase__ ) for i in range(3 ): for j in range(3 ): __lowerCamelCase = cofactor_matrix[j][i] # Inverse of the matrix using the formula (1/determinant) * adjoint matrix __lowerCamelCase = array(UpperCamelCase__ ) for i in range(3 ): for j in range(3 ): inverse_matrix[i][j] /= d(UpperCamelCase__ ) # Calculate the inverse of the matrix return [[float(d(UpperCamelCase__ ) ) or 0.0 for n in row] for row in inverse_matrix] raise ValueError('''Please provide a matrix of size 2x2 or 3x3.''' )
67
1
'''simple docstring''' import math import sys def __lowerCAmelCase ( UpperCamelCase__ ) -> str: __lowerCamelCase = '''''' try: with open(UpperCamelCase__ , '''rb''' ) as binary_file: __lowerCamelCase = binary_file.read() for dat in data: __lowerCamelCase = f"""{dat:08b}""" result += curr_byte return result except OSError: print('''File not accessible''' ) sys.exit() def __lowerCAmelCase ( UpperCamelCase__ ) -> str: __lowerCamelCase = {'''0''': '''0''', '''1''': '''1'''} __lowerCamelCase , __lowerCamelCase = '''''', '''''' __lowerCamelCase = len(UpperCamelCase__ ) for i in range(len(UpperCamelCase__ ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue __lowerCamelCase = lexicon[curr_string] result += last_match_id __lowerCamelCase = last_match_id + '''0''' if math.loga(UpperCamelCase__ ).is_integer(): __lowerCamelCase = {} for curr_key in list(UpperCamelCase__ ): __lowerCamelCase = lexicon.pop(UpperCamelCase__ ) __lowerCamelCase = new_lex __lowerCamelCase = last_match_id + '''1''' index += 1 __lowerCamelCase = '''''' return result def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> None: __lowerCamelCase = 8 try: with open(UpperCamelCase__ , '''wb''' ) as opened_file: __lowerCamelCase = [ to_write[i : i + byte_length] for i in range(0 , len(UpperCamelCase__ ) , UpperCamelCase__ ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append('''10000000''' ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array[:-1]: opened_file.write(int(UpperCamelCase__ , 2 ).to_bytes(1 , byteorder='''big''' ) ) except OSError: print('''File not accessible''' ) sys.exit() def __lowerCAmelCase ( UpperCamelCase__ ) -> str: __lowerCamelCase = 0 for letter in data_bits: if letter == "1": break counter += 1 __lowerCamelCase = data_bits[counter:] __lowerCamelCase = data_bits[counter + 1 :] return data_bits def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> None: __lowerCamelCase = read_file_binary(UpperCamelCase__ ) __lowerCamelCase = remove_prefix(UpperCamelCase__ ) __lowerCamelCase = decompress_data(UpperCamelCase__ ) write_file_binary(UpperCamelCase__ , UpperCamelCase__ ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
67
'''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 __UpperCAmelCase =logging.get_logger(__name__) def __lowerCAmelCase ( UpperCamelCase__=None , UpperCamelCase__=None ) -> int: return field(default_factory=lambda: default , metadata=UpperCamelCase__ ) @dataclass class a__ : lowerCamelCase : List[str] =list_field( default=[] , metadata={ "help": ( "Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version" " of all available models" ) } , ) lowerCamelCase : List[int] =list_field( default=[8] , metadata={"help": "List of batch sizes for which memory and time performance will be evaluated"} ) lowerCamelCase : List[int] =list_field( default=[8, 3_2, 1_2_8, 5_1_2] , metadata={"help": "List of sequence lengths for which memory and time performance will be evaluated"} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to benchmark inference of model. Inference can be disabled via --no-inference."} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to run on available cuda devices. Cuda can be disabled via --no-cuda."} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to run on available tpu devices. TPU can be disabled via --no-tpu."} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Use FP16 to accelerate inference."} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Benchmark training of model"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Verbose memory tracing"} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to perform speed measurements. Speed measurements can be disabled via --no-speed."} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": "Whether to perform memory measurements. Memory measurements can be disabled via --no-memory" } , ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Trace memory line by line"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Save result to a CSV file"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Save all print statements in a log file"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Whether to print environment information"} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": ( "Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use" " multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled" " for debugging / testing and on TPU." ) } , ) lowerCamelCase : str =field( default=F'''inference_time_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving time results to csv."} , ) lowerCamelCase : str =field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving memory results to csv."} , ) lowerCamelCase : str =field( default=F'''train_time_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving time results to csv for training."} , ) lowerCamelCase : str =field( default=F'''train_memory_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving memory results to csv for training."} , ) lowerCamelCase : str =field( default=F'''env_info_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving environment information."} , ) lowerCamelCase : str =field( default=F'''log_{round(time() )}.csv''' , metadata={"help": "Log filename used if print statements are saved in log."} , ) lowerCamelCase : int =field(default=3 , metadata={"help": "Times an experiment will be run."} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": ( "Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain" " model weights." ) } , ) def SCREAMING_SNAKE_CASE__ ( self : 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.''' , a , ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """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 SCREAMING_SNAKE_CASE__ ( 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
67
1
'''simple docstring''' from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo __UpperCAmelCase ="\\n@misc{wu2016googles,\n title={Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation},\n author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey\n and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin\n Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto\n Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and\n Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes\n and Jeffrey Dean},\n year={2016},\n eprint={1609.08144},\n archivePrefix={arXiv},\n primaryClass={cs.CL}\n}\n" __UpperCAmelCase ="\\nThe BLEU score has some undesirable properties when used for single\nsentences, as it was designed to be a corpus measure. We therefore\nuse a slightly different score for our RL experiments which we call\nthe 'GLEU score'. For the GLEU score, we record all sub-sequences of\n1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then\ncompute a recall, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the target (ground truth) sequence,\nand a precision, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the generated output sequence. Then\nGLEU score is simply the minimum of recall and precision. This GLEU\nscore's range is always between 0 (no matches) and 1 (all match) and\nit is symmetrical when switching output and target. According to\nour experiments, GLEU score correlates quite well with the BLEU\nmetric on a corpus level but does not have its drawbacks for our per\nsentence reward objective.\n" __UpperCAmelCase ="\\nComputes corpus-level Google BLEU (GLEU) score of translated segments against one or more references.\nInstead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching\ntokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values.\n\nArgs:\n predictions (list of str): list of translations to score.\n Each translation should be tokenized into a list of tokens.\n references (list of list of str): list of lists of references for each translation.\n Each reference should be tokenized into a list of tokens.\n min_len (int): The minimum order of n-gram this function should extract. Defaults to 1.\n max_len (int): The maximum order of n-gram this function should extract. Defaults to 4.\n\nReturns:\n 'google_bleu': google_bleu score\n\nExamples:\n Example 1:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.44\n\n Example 2:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.61\n\n Example 3:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.53\n\n Example 4:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.4\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a__ ( datasets.Metric ): def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Sequence(datasets.Value('''string''' , id='''token''' ) , id='''sequence''' ), '''references''': datasets.Sequence( datasets.Sequence(datasets.Value('''string''' , id='''token''' ) , id='''sequence''' ) , id='''references''' ), } ) , ) def SCREAMING_SNAKE_CASE__ ( self : Any , a : List[List[List[str]]] , a : List[List[str]] , a : int = 1 , a : int = 4 , ): """simple docstring""" return { "google_bleu": gleu_score.corpus_gleu( list_of_references=a , hypotheses=a , min_len=a , max_len=a ) }
67
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_barthez import BarthezTokenizer else: __UpperCAmelCase =None __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase ={ "vocab_file": { "moussaKam/mbarthez": "https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model", "moussaKam/barthez": "https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model", "moussaKam/barthez-orangesum-title": ( "https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "moussaKam/mbarthez": "https://huggingface.co/moussaKam/mbarthez/resolve/main/tokenizer.json", "moussaKam/barthez": "https://huggingface.co/moussaKam/barthez/resolve/main/tokenizer.json", "moussaKam/barthez-orangesum-title": ( "https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/tokenizer.json" ), }, } __UpperCAmelCase ={ "moussaKam/mbarthez": 1_0_2_4, "moussaKam/barthez": 1_0_2_4, "moussaKam/barthez-orangesum-title": 1_0_2_4, } __UpperCAmelCase ="▁" class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] =VOCAB_FILES_NAMES lowerCamelCase : Optional[Any] =PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : int =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[Any] =["input_ids", "attention_mask"] lowerCamelCase : Union[str, Any] =BarthezTokenizer def __init__( self : Optional[Any] , a : Dict=None , a : str=None , a : List[Any]="<s>" , a : Optional[int]="</s>" , a : List[str]="</s>" , a : Tuple="<s>" , a : str="<unk>" , a : Any="<pad>" , a : Union[str, Any]="<mask>" , **a : Union[str, Any] , ): """simple docstring""" __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else mask_token super().__init__( a , tokenizer_file=a , bos_token=a , eos_token=a , unk_token=a , sep_token=a , cls_token=a , pad_token=a , mask_token=a , **a , ) __lowerCamelCase = vocab_file __lowerCamelCase = False if not self.vocab_file else True def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __lowerCamelCase = [self.cls_token_id] __lowerCamelCase = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" __lowerCamelCase = [self.sep_token_id] __lowerCamelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : str , a : Optional[str] = None ): """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return __lowerCamelCase = os.path.join( a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(a ): copyfile(self.vocab_file , a ) return (out_vocab_file,)
67
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={ "abeja/gpt-neox-japanese-2.7b": "https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json", } class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] ="gpt_neox_japanese" def __init__( self : List[Any] , a : Tuple=3_20_00 , a : Dict=25_60 , a : Union[str, Any]=32 , a : Dict=32 , a : Dict=4 , a : Optional[Any]="gelu" , a : Any=1.00 , a : str=1_00_00 , a : List[str]=20_48 , a : str=0.02 , a : Union[str, Any]=1e-5 , a : Optional[Any]=True , a : str=3_19_96 , a : List[str]=3_19_99 , a : str=0.1 , a : Union[str, Any]=0.0 , **a : Optional[Any] , ): """simple docstring""" super().__init__(bos_token_id=a , eos_token_id=a , **a ) __lowerCamelCase = vocab_size __lowerCamelCase = max_position_embeddings __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_multiple_size __lowerCamelCase = hidden_act __lowerCamelCase = rotary_pct __lowerCamelCase = rotary_emb_base __lowerCamelCase = initializer_range __lowerCamelCase = layer_norm_eps __lowerCamelCase = use_cache __lowerCamelCase = attention_dropout __lowerCamelCase = hidden_dropout
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ = 1_00_00_00 ) -> int: __lowerCamelCase = set(range(3 , UpperCamelCase__ , 2 ) ) primes.add(2 ) for p in range(3 , UpperCamelCase__ , 2 ): if p not in primes: continue primes.difference_update(set(range(p * p , UpperCamelCase__ , UpperCamelCase__ ) ) ) __lowerCamelCase = [float(UpperCamelCase__ ) for n in range(limit + 1 )] for p in primes: for n in range(UpperCamelCase__ , limit + 1 , UpperCamelCase__ ): phi[n] *= 1 - 1 / p return int(sum(phi[2:] ) ) if __name__ == "__main__": print(f'{solution() = }')
67
1
'''simple docstring''' import builtins import sys from ...utils.imports import _is_package_available from . import cursor, input from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor from .keymap import KEYMAP __UpperCAmelCase =False try: __UpperCAmelCase =_is_package_available("google.colab") except ModuleNotFoundError: pass @input.register class a__ : def __init__( self : Dict , a : str = None , a : list = [] ): """simple docstring""" __lowerCamelCase = 0 __lowerCamelCase = choices __lowerCamelCase = prompt if sys.platform == "win32": __lowerCamelCase = '''*''' else: __lowerCamelCase = '''➔ ''' def SCREAMING_SNAKE_CASE__ ( self : str , a : int , a : str = "" ): """simple docstring""" if sys.platform != "win32": writeColor(self.choices[index] , 32 , a ) else: forceWrite(self.choices[index] , a ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : int ): """simple docstring""" if index == self.position: forceWrite(f""" {self.arrow_char} """ ) self.write_choice(a ) else: forceWrite(f""" {self.choices[index]}""" ) reset_cursor() def SCREAMING_SNAKE_CASE__ ( self : Any , a : Direction , a : int = 1 ): """simple docstring""" __lowerCamelCase = self.position if direction == Direction.DOWN: if self.position + 1 >= len(self.choices ): return self.position += num_spaces else: if self.position - 1 < 0: return self.position -= num_spaces clear_line() self.print_choice(a ) move_cursor(a , direction.name ) self.print_choice(self.position ) @input.mark(KEYMAP['''up'''] ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" self.move_direction(Direction.UP ) @input.mark(KEYMAP['''down'''] ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" self.move_direction(Direction.DOWN ) @input.mark(KEYMAP['''newline'''] ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" move_cursor(len(self.choices ) - self.position , '''DOWN''' ) return self.position @input.mark(KEYMAP['''interrupt'''] ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" move_cursor(len(self.choices ) - self.position , '''DOWN''' ) raise KeyboardInterrupt @input.mark_multiple(*[KEYMAP[str(a )] for number in range(10 )] ) def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" __lowerCamelCase = int(chr(self.current_selection ) ) __lowerCamelCase = index - self.position if index == self.position: return if index < len(self.choices ): if self.position > index: self.move_direction(Direction.UP , -movement ) elif self.position < index: self.move_direction(Direction.DOWN , a ) else: return else: return def SCREAMING_SNAKE_CASE__ ( self : List[str] , a : int = 0 ): """simple docstring""" if self.prompt: linebreak() forceWrite(self.prompt , '''\n''' ) if in_colab: forceWrite('''Please input a choice index (starting from 0), and press enter''' , '''\n''' ) else: forceWrite('''Please select a choice using the arrow or number keys, and selecting with enter''' , '''\n''' ) __lowerCamelCase = default_choice for i in range(len(self.choices ) ): self.print_choice(a ) forceWrite('''\n''' ) move_cursor(len(self.choices ) - self.position , '''UP''' ) with cursor.hide(): while True: if in_colab: try: __lowerCamelCase = int(builtins.input() ) except ValueError: __lowerCamelCase = default_choice else: __lowerCamelCase = self.handle_input() if choice is not None: reset_cursor() for _ in range(len(self.choices ) + 1 ): move_cursor(1 , '''UP''' ) clear_line() self.write_choice(a , '''\n''' ) return choice
67
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST, OpenAIGPTConfig, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification, OpenAIGPTLMHeadModel, OpenAIGPTModel, ) class a__ : def __init__( self : Union[str, Any] , a : Union[str, Any] , a : Tuple=13 , a : Optional[Any]=7 , a : List[Any]=True , a : Optional[Any]=True , a : Any=True , a : Union[str, Any]=99 , a : Any=32 , a : int=5 , a : Optional[int]=4 , a : Union[str, Any]=37 , a : Optional[Any]="gelu" , a : Union[str, Any]=0.1 , a : Any=0.1 , a : Optional[int]=5_12 , a : int=16 , a : Optional[Any]=2 , a : Union[str, Any]=0.02 , a : Any=3 , a : Dict=4 , a : Any=None , ): """simple docstring""" __lowerCamelCase = parent __lowerCamelCase = batch_size __lowerCamelCase = seq_length __lowerCamelCase = is_training __lowerCamelCase = use_token_type_ids __lowerCamelCase = use_labels __lowerCamelCase = vocab_size __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_size __lowerCamelCase = hidden_act __lowerCamelCase = hidden_dropout_prob __lowerCamelCase = attention_probs_dropout_prob __lowerCamelCase = max_position_embeddings __lowerCamelCase = type_vocab_size __lowerCamelCase = type_sequence_label_size __lowerCamelCase = initializer_range __lowerCamelCase = num_labels __lowerCamelCase = num_choices __lowerCamelCase = scope __lowerCamelCase = self.vocab_size - 1 def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCamelCase = None if self.use_token_type_ids: __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = None if self.use_labels: __lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __lowerCamelCase = ids_tensor([self.batch_size] , self.num_choices ) __lowerCamelCase = OpenAIGPTConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) __lowerCamelCase = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, head_mask, token_type_ids, sequence_labels, token_labels, choice_labels, ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : Dict , a : List[str] , a : Tuple , a : List[Any] , *a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTModel(config=a ) model.to(a ) model.eval() __lowerCamelCase = model(a , token_type_ids=a , head_mask=a ) __lowerCamelCase = model(a , token_type_ids=a ) __lowerCamelCase = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Union[str, Any] , a : Dict , a : Union[str, Any] , a : Tuple , *a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTLMHeadModel(a ) model.to(a ) model.eval() __lowerCamelCase = model(a , token_type_ids=a , labels=a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Tuple , a : Optional[int] , a : Union[str, Any] , a : Optional[Any] , *a : Optional[Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTDoubleHeadsModel(a ) model.to(a ) model.eval() __lowerCamelCase = model(a , token_type_ids=a , labels=a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self : List[str] , a : int , a : Dict , a : Optional[Any] , a : str , *a : int ): """simple docstring""" __lowerCamelCase = self.num_labels __lowerCamelCase = OpenAIGPTForSequenceClassification(a ) model.to(a ) model.eval() __lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCamelCase = model(a , token_type_ids=a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = self.prepare_config_and_inputs() ( ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ) = config_and_inputs __lowerCamelCase = { '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask, } return config, inputs_dict @require_torch class a__ ( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): lowerCamelCase : List[str] =( (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification) if is_torch_available() else () ) lowerCamelCase : str =( (OpenAIGPTLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly lowerCamelCase : Optional[int] =( { "feature-extraction": OpenAIGPTModel, "text-classification": OpenAIGPTForSequenceClassification, "text-generation": OpenAIGPTLMHeadModel, "zero-shot": OpenAIGPTForSequenceClassification, } if is_torch_available() else {} ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : Tuple , a : Optional[int] , a : int , a : str , a : Any ): """simple docstring""" if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a # tiny config could not be created. return True return False def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : int , a : Optional[int] , a : str=False ): """simple docstring""" __lowerCamelCase = super()._prepare_for_class(a , a , return_labels=a ) if return_labels: if model_class.__name__ == "OpenAIGPTDoubleHeadsModel": __lowerCamelCase = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length) , dtype=torch.long , device=a , ) __lowerCamelCase = inputs_dict['''labels'''] __lowerCamelCase = inputs_dict['''labels'''] __lowerCamelCase = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices) , dtype=torch.long , device=a , ) __lowerCamelCase = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=a ) return inputs_dict def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTModelTester(self ) __lowerCamelCase = ConfigTester(self , config_class=a , n_embd=37 ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_double_lm_head_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*a ) @slow def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __lowerCamelCase = OpenAIGPTModel.from_pretrained(a ) self.assertIsNotNone(a ) @require_torch class a__ ( unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt''' ) model.to(a ) __lowerCamelCase = torch.tensor([[4_81, 47_35, 5_44]] , dtype=torch.long , device=a ) # the president is __lowerCamelCase = [ 4_81, 47_35, 5_44, 2_46, 9_63, 8_70, 7_62, 2_39, 2_44, 4_04_77, 2_44, 2_49, 7_19, 8_81, 4_87, 5_44, 2_40, 2_44, 6_03, 4_81, ] # the president is a very good man. " \n " i\'m sure he is, " said the __lowerCamelCase = model.generate(a , do_sample=a ) self.assertListEqual(output_ids[0].tolist() , a )
67
1
'''simple docstring''' import re def __lowerCAmelCase ( UpperCamelCase__ ) -> str: if len(re.findall('''[ATCG]''' , UpperCamelCase__ ) ) != len(UpperCamelCase__ ): raise ValueError('''Invalid Strand''' ) return dna.translate(dna.maketrans('''ATCG''' , '''TAGC''' ) ) if __name__ == "__main__": import doctest doctest.testmod()
67
'''simple docstring''' import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] =["image_processor", "tokenizer"] lowerCamelCase : Union[str, Any] ="LayoutLMv2ImageProcessor" lowerCamelCase : int =("LayoutXLMTokenizer", "LayoutXLMTokenizerFast") def __init__( self : Optional[int] , a : Any=None , a : Any=None , **a : Union[str, Any] ): """simple docstring""" if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , a , ) __lowerCamelCase = kwargs.pop('''feature_extractor''' ) __lowerCamelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(a , a ) def __call__( self : Tuple , a : Optional[int] , a : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , a : Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None , a : Union[List[List[int]], List[List[List[int]]]] = None , a : Optional[Union[List[int], List[List[int]]]] = None , a : bool = True , a : Union[bool, str, PaddingStrategy] = False , a : Union[bool, str, TruncationStrategy] = None , a : Optional[int] = None , a : int = 0 , a : Optional[int] = None , a : Optional[bool] = None , a : Optional[bool] = None , a : bool = False , a : bool = False , a : bool = False , a : bool = False , a : bool = True , a : Optional[Union[str, TensorType]] = None , **a : Tuple , ): """simple docstring""" if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( '''You cannot provide bounding boxes ''' '''if you initialized the image processor with apply_ocr set to True.''' ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.''' ) if return_overflowing_tokens is True and return_offsets_mapping is False: raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.''' ) # first, apply the image processor __lowerCamelCase = self.image_processor(images=a , return_tensors=a ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(a , a ): __lowerCamelCase = [text] # add batch dimension (as the image processor always adds a batch dimension) __lowerCamelCase = features['''words'''] __lowerCamelCase = self.tokenizer( text=text if text is not None else features['''words'''] , text_pair=text_pair if text_pair is not None else None , boxes=boxes if boxes is not None else features['''boxes'''] , word_labels=a , add_special_tokens=a , padding=a , truncation=a , max_length=a , stride=a , pad_to_multiple_of=a , return_token_type_ids=a , return_attention_mask=a , return_overflowing_tokens=a , return_special_tokens_mask=a , return_offsets_mapping=a , return_length=a , verbose=a , return_tensors=a , **a , ) # add pixel values __lowerCamelCase = features.pop('''pixel_values''' ) if return_overflowing_tokens is True: __lowerCamelCase = self.get_overflowing_images(a , encoded_inputs['''overflow_to_sample_mapping'''] ) __lowerCamelCase = images return encoded_inputs def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : Optional[Any] , a : str ): """simple docstring""" __lowerCamelCase = [] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(a ) != len(a ): raise ValueError( '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got''' f""" {len(a )} and {len(a )}""" ) return images_with_overflow def SCREAMING_SNAKE_CASE__ ( self : List[str] , *a : Optional[Any] , **a : Union[str, Any] ): """simple docstring""" return self.tokenizer.batch_decode(*a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , *a : Union[str, Any] , **a : Tuple ): """simple docstring""" return self.tokenizer.decode(*a , **a ) @property def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" return ["input_ids", "bbox", "attention_mask", "image"] @property def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , a , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , a , ) return self.image_processor
67
1
'''simple docstring''' import tensorflow as tf from ...tf_utils import shape_list class a__ ( tf.keras.layers.Layer ): def __init__( self : Tuple , a : Any , a : Any , a : Optional[Any] , a : Optional[int] , a : str=1 , a : Optional[int]=False , **a : str ): """simple docstring""" super().__init__(**a ) __lowerCamelCase = vocab_size __lowerCamelCase = d_embed __lowerCamelCase = d_proj __lowerCamelCase = cutoffs + [vocab_size] __lowerCamelCase = [0] + self.cutoffs __lowerCamelCase = div_val __lowerCamelCase = self.cutoffs[0] __lowerCamelCase = len(self.cutoffs ) - 1 __lowerCamelCase = self.shortlist_size + self.n_clusters __lowerCamelCase = keep_order __lowerCamelCase = [] __lowerCamelCase = [] def SCREAMING_SNAKE_CASE__ ( self : int , a : int ): """simple docstring""" if self.n_clusters > 0: __lowerCamelCase = self.add_weight( shape=(self.n_clusters, self.d_embed) , initializer='''zeros''' , trainable=a , name='''cluster_weight''' ) __lowerCamelCase = self.add_weight( shape=(self.n_clusters,) , initializer='''zeros''' , trainable=a , name='''cluster_bias''' ) if self.div_val == 1: for i in range(len(self.cutoffs ) ): if self.d_proj != self.d_embed: __lowerCamelCase = self.add_weight( shape=(self.d_embed, self.d_proj) , initializer='''zeros''' , trainable=a , name=f"""out_projs_._{i}""" , ) self.out_projs.append(a ) else: self.out_projs.append(a ) __lowerCamelCase = self.add_weight( shape=(self.vocab_size, self.d_embed) , initializer='''zeros''' , trainable=a , name=f"""out_layers_._{i}_._weight""" , ) __lowerCamelCase = self.add_weight( shape=(self.vocab_size,) , initializer='''zeros''' , trainable=a , name=f"""out_layers_._{i}_._bias""" , ) self.out_layers.append((weight, bias) ) else: for i in range(len(self.cutoffs ) ): __lowerCamelCase , __lowerCamelCase = self.cutoff_ends[i], self.cutoff_ends[i + 1] __lowerCamelCase = self.d_embed // (self.div_val**i) __lowerCamelCase = self.add_weight( shape=(d_emb_i, self.d_proj) , initializer='''zeros''' , trainable=a , name=f"""out_projs_._{i}""" ) self.out_projs.append(a ) __lowerCamelCase = self.add_weight( shape=(r_idx - l_idx, d_emb_i) , initializer='''zeros''' , trainable=a , name=f"""out_layers_._{i}_._weight""" , ) __lowerCamelCase = self.add_weight( shape=(r_idx - l_idx,) , initializer='''zeros''' , trainable=a , name=f"""out_layers_._{i}_._bias""" , ) self.out_layers.append((weight, bias) ) super().build(a ) @staticmethod def SCREAMING_SNAKE_CASE__ ( a : Optional[Any] , a : Dict , a : Optional[Any] , a : List[str]=None ): """simple docstring""" __lowerCamelCase = x if proj is not None: __lowerCamelCase = tf.einsum('''ibd,ed->ibe''' , a , a ) return tf.einsum('''ibd,nd->ibn''' , a , a ) + b @staticmethod def SCREAMING_SNAKE_CASE__ ( a : List[str] , a : Tuple ): """simple docstring""" __lowerCamelCase = shape_list(a ) __lowerCamelCase = tf.range(lp_size[0] , dtype=target.dtype ) __lowerCamelCase = tf.stack([r, target] , 1 ) return tf.gather_nd(a , a ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : Optional[int] , a : Dict , a : int=True , a : Dict=False ): """simple docstring""" __lowerCamelCase = 0 if self.n_clusters == 0: __lowerCamelCase = self._logit(a , self.out_layers[0][0] , self.out_layers[0][1] , self.out_projs[0] ) if target is not None: __lowerCamelCase = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=a , logits=a ) __lowerCamelCase = tf.nn.log_softmax(a , axis=-1 ) else: __lowerCamelCase = shape_list(a ) __lowerCamelCase = [] __lowerCamelCase = tf.zeros(hidden_sizes[:2] ) for i in range(len(self.cutoffs ) ): __lowerCamelCase , __lowerCamelCase = self.cutoff_ends[i], self.cutoff_ends[i + 1] if target is not None: __lowerCamelCase = (target >= l_idx) & (target < r_idx) __lowerCamelCase = tf.where(a ) __lowerCamelCase = tf.boolean_mask(a , a ) - l_idx if self.div_val == 1: __lowerCamelCase = self.out_layers[0][0][l_idx:r_idx] __lowerCamelCase = self.out_layers[0][1][l_idx:r_idx] else: __lowerCamelCase = self.out_layers[i][0] __lowerCamelCase = self.out_layers[i][1] if i == 0: __lowerCamelCase = tf.concat([cur_W, self.cluster_weight] , 0 ) __lowerCamelCase = tf.concat([cur_b, self.cluster_bias] , 0 ) __lowerCamelCase = self._logit(a , a , a , self.out_projs[0] ) __lowerCamelCase = tf.nn.log_softmax(a ) out.append(head_logprob[..., : self.cutoffs[0]] ) if target is not None: __lowerCamelCase = tf.boolean_mask(a , a ) __lowerCamelCase = self._gather_logprob(a , a ) else: __lowerCamelCase = self._logit(a , a , a , self.out_projs[i] ) __lowerCamelCase = tf.nn.log_softmax(a ) __lowerCamelCase = self.cutoffs[0] + i - 1 # No probability for the head cluster __lowerCamelCase = head_logprob[..., cluster_prob_idx, None] + tail_logprob out.append(a ) if target is not None: __lowerCamelCase = tf.boolean_mask(a , a ) __lowerCamelCase = tf.boolean_mask(a , a ) __lowerCamelCase = self._gather_logprob(a , a ) cur_logprob += cur_head_logprob[:, self.cutoff_ends[1] + i - 1] if target is not None: loss += tf.scatter_nd(a , -cur_logprob , shape_list(a ) ) __lowerCamelCase = tf.concat(a , axis=-1 ) if target is not None: if return_mean: __lowerCamelCase = tf.reduce_mean(a ) # Add the training-time loss value to the layer using `self.add_loss()`. self.add_loss(a ) # Log the loss as a metric (we could log arbitrary metrics, # including different metrics for training and inference. self.add_metric(a , name=self.name , aggregation='''mean''' if return_mean else '''''' ) return out
67
'''simple docstring''' import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) class a__ ( UpperCAmelCase__ ): def __init__( self : Optional[Any] , a : Union[List[ControlNetModel], Tuple[ControlNetModel]] ): """simple docstring""" super().__init__() __lowerCamelCase = nn.ModuleList(a ) def SCREAMING_SNAKE_CASE__ ( self : Any , a : torch.FloatTensor , a : Union[torch.Tensor, float, int] , a : torch.Tensor , a : List[torch.tensor] , a : List[float] , a : Optional[torch.Tensor] = None , a : Optional[torch.Tensor] = None , a : Optional[torch.Tensor] = None , a : Optional[Dict[str, Any]] = None , a : bool = False , a : bool = True , ): """simple docstring""" for i, (image, scale, controlnet) in enumerate(zip(a , a , self.nets ) ): __lowerCamelCase , __lowerCamelCase = controlnet( a , a , a , a , a , a , a , a , a , a , a , ) # merge samples if i == 0: __lowerCamelCase , __lowerCamelCase = down_samples, mid_sample else: __lowerCamelCase = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(a , a ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def SCREAMING_SNAKE_CASE__ ( self : Any , a : Union[str, os.PathLike] , a : bool = True , a : Callable = None , a : bool = False , a : Optional[str] = None , ): """simple docstring""" __lowerCamelCase = 0 __lowerCamelCase = save_directory for controlnet in self.nets: controlnet.save_pretrained( a , is_main_process=a , save_function=a , safe_serialization=a , variant=a , ) idx += 1 __lowerCamelCase = model_path_to_save + f"""_{idx}""" @classmethod def SCREAMING_SNAKE_CASE__ ( cls : List[str] , a : Optional[Union[str, os.PathLike]] , **a : Optional[Any] ): """simple docstring""" __lowerCamelCase = 0 __lowerCamelCase = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... __lowerCamelCase = pretrained_model_path while os.path.isdir(a ): __lowerCamelCase = ControlNetModel.from_pretrained(a , **a ) controlnets.append(a ) idx += 1 __lowerCamelCase = pretrained_model_path + f"""_{idx}""" logger.info(f"""{len(a )} controlnets loaded from {pretrained_model_path}.""" ) if len(a ) == 0: raise ValueError( f"""No ControlNets found under {os.path.dirname(a )}. Expected at least {pretrained_model_path + '_0'}.""" ) return cls(a )
67
1
'''simple docstring''' import shutil import tempfile import unittest from transformers import ( SPIECE_UNDERLINE, AddedToken, BatchEncoding, NllbTokenizer, NllbTokenizerFast, is_torch_available, ) from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin __UpperCAmelCase =get_tests_dir("fixtures/test_sentencepiece.model") if is_torch_available(): from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right __UpperCAmelCase =2_5_6_0_4_7 __UpperCAmelCase =2_5_6_1_4_5 @require_sentencepiece @require_tokenizers class a__ ( UpperCAmelCase__ , unittest.TestCase ): lowerCamelCase : Union[str, Any] =NllbTokenizer lowerCamelCase : Optional[int] =NllbTokenizerFast lowerCamelCase : str =True lowerCamelCase : str =True lowerCamelCase : Tuple ={} def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing __lowerCamelCase = NllbTokenizer(a , keep_accents=a ) tokenizer.save_pretrained(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = NllbTokenizer(a , keep_accents=a ) __lowerCamelCase = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(a , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(a ) , [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]] , ) __lowerCamelCase = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) __lowerCamelCase = tokenizer.convert_tokens_to_ids(a ) self.assertListEqual( a , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) __lowerCamelCase = tokenizer.convert_ids_to_tokens(a ) self.assertListEqual( a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" __lowerCamelCase = (self.rust_tokenizer_class, '''hf-internal-testing/tiny-random-nllb''', {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): __lowerCamelCase = self.rust_tokenizer_class.from_pretrained(a , **a ) __lowerCamelCase = self.tokenizer_class.from_pretrained(a , **a ) __lowerCamelCase = tempfile.mkdtemp() __lowerCamelCase = tokenizer_r.save_pretrained(a ) __lowerCamelCase = tokenizer_p.save_pretrained(a ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) __lowerCamelCase = tuple(f for f in tokenizer_r_files if '''tokenizer.json''' not in f ) self.assertSequenceEqual(a , a ) # Checks everything loads correctly in the same way __lowerCamelCase = tokenizer_r.from_pretrained(a ) __lowerCamelCase = tokenizer_p.from_pretrained(a ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(a , a ) ) shutil.rmtree(a ) # Save tokenizer rust, legacy_format=True __lowerCamelCase = tempfile.mkdtemp() __lowerCamelCase = tokenizer_r.save_pretrained(a , legacy_format=a ) __lowerCamelCase = tokenizer_p.save_pretrained(a ) # Checks it save with the same files self.assertSequenceEqual(a , a ) # Checks everything loads correctly in the same way __lowerCamelCase = tokenizer_r.from_pretrained(a ) __lowerCamelCase = tokenizer_p.from_pretrained(a ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(a , a ) ) shutil.rmtree(a ) # Save tokenizer rust, legacy_format=False __lowerCamelCase = tempfile.mkdtemp() __lowerCamelCase = tokenizer_r.save_pretrained(a , legacy_format=a ) __lowerCamelCase = tokenizer_p.save_pretrained(a ) # Checks it saved the tokenizer.json file self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way __lowerCamelCase = tokenizer_r.from_pretrained(a ) __lowerCamelCase = tokenizer_p.from_pretrained(a ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(a , a ) ) shutil.rmtree(a ) @require_torch def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" if not self.test_seqaseq: return __lowerCamelCase = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): # Longer text that will definitely require truncation. __lowerCamelCase = [ ''' UN Chief Says There Is No Military Solution in Syria''', ''' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for''' ''' Syria is that \'there is no military solution\' to the nearly five-year conflict and more weapons''' ''' will only worsen the violence and misery for millions of people.''', ] __lowerCamelCase = [ '''Şeful ONU declară că nu există o soluţie militară în Siria''', '''Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al''' ''' Rusiei pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi''' ''' că noi arme nu vor face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.''', ] try: __lowerCamelCase = tokenizer.prepare_seqaseq_batch( src_texts=a , tgt_texts=a , max_length=3 , max_target_length=10 , return_tensors='''pt''' , src_lang='''eng_Latn''' , tgt_lang='''ron_Latn''' , ) except NotImplementedError: return self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.labels.shape[1] , 10 ) # max_target_length will default to max_length if not specified __lowerCamelCase = tokenizer.prepare_seqaseq_batch( a , tgt_texts=a , max_length=3 , return_tensors='''pt''' ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.labels.shape[1] , 3 ) __lowerCamelCase = tokenizer.prepare_seqaseq_batch( src_texts=a , max_length=3 , max_target_length=10 , return_tensors='''pt''' ) self.assertEqual(batch_encoder_only.input_ids.shape[1] , 3 ) self.assertEqual(batch_encoder_only.attention_mask.shape[1] , 3 ) self.assertNotIn('''decoder_input_ids''' , a ) @unittest.skip('''Unfortunately way too slow to build a BPE with SentencePiece.''' ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" pass def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): __lowerCamelCase = [AddedToken('''<special>''' , lstrip=a )] __lowerCamelCase = self.rust_tokenizer_class.from_pretrained( a , additional_special_tokens=a , **a ) __lowerCamelCase = tokenizer_r.encode('''Hey this is a <special> token''' ) __lowerCamelCase = tokenizer_r.encode('''<special>''' , add_special_tokens=a )[0] self.assertTrue(special_token_id in r_output ) if self.test_slow_tokenizer: __lowerCamelCase = self.rust_tokenizer_class.from_pretrained( a , additional_special_tokens=a , **a , ) __lowerCamelCase = self.tokenizer_class.from_pretrained( a , additional_special_tokens=a , **a ) __lowerCamelCase = tokenizer_p.encode('''Hey this is a <special> token''' ) __lowerCamelCase = tokenizer_cr.encode('''Hey this is a <special> token''' ) self.assertEqual(a , a ) self.assertEqual(a , a ) self.assertTrue(special_token_id in p_output ) self.assertTrue(special_token_id in cr_output ) @require_torch @require_sentencepiece @require_tokenizers class a__ ( unittest.TestCase ): lowerCamelCase : int ="facebook/nllb-200-distilled-600M" lowerCamelCase : Optional[int] =[ " UN Chief Says There Is No Military Solution in Syria", " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.", ] lowerCamelCase : Tuple =[ "Şeful ONU declară că nu există o soluţie militară în Siria", "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei" " pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor" " face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.", ] lowerCamelCase : Union[str, Any] =[ 2_5_6_0_4_7, 1_6_2_9_7, 1_3_4_4_0_8, 8_1_6_5, 2_4_8_0_6_6, 1_4_7_3_4, 9_5_0, 1_1_3_5, 1_0_5_7_2_1, 3_5_7_3, 8_3, 2_7_3_5_2, 1_0_8, 4_9_4_8_6, 2, ] @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Tuple ): """simple docstring""" __lowerCamelCase = NllbTokenizer.from_pretrained( cls.checkpoint_name , src_lang='''eng_Latn''' , tgt_lang='''ron_Latn''' ) __lowerCamelCase = 1 return cls def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''ace_Arab'''] , 25_60_01 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''ace_Latn'''] , 25_60_02 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''fra_Latn'''] , 25_60_57 ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" self.assertIn(a , self.tokenizer.all_special_ids ) # fmt: off __lowerCamelCase = [RO_CODE, 42_54, 9_80_68, 11_29_23, 3_90_72, 39_09, 7_13, 10_27_67, 26, 1_73_14, 3_56_42, 1_46_83, 3_31_18, 20_22, 6_69_87, 2, 25_60_47] # fmt: on __lowerCamelCase = self.tokenizer.decode(a , skip_special_tokens=a ) __lowerCamelCase = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=a ) self.assertEqual(a , a ) self.assertNotIn(self.tokenizer.eos_token , a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = ['''this is gunna be a long sentence ''' * 20] assert isinstance(src_text[0] , a ) __lowerCamelCase = 10 __lowerCamelCase = self.tokenizer(a , max_length=a , truncation=a ).input_ids[0] self.assertEqual(ids[-1] , 2 ) self.assertEqual(ids[0] , a ) self.assertEqual(len(a ) , a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['''<mask>''', '''ar_AR'''] ) , [25_62_03, 3] ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = tempfile.mkdtemp() __lowerCamelCase = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(a ) __lowerCamelCase = NllbTokenizer.from_pretrained(a ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , a ) @require_torch def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=a , truncation=a , max_length=len(self.expected_src_tokens ) , return_tensors='''pt''' , ) __lowerCamelCase = shift_tokens_right( batch['''labels'''] , self.tokenizer.pad_token_id , self.tokenizer.lang_code_to_id['''ron_Latn'''] ) self.assertIsInstance(a , a ) self.assertEqual((2, 15) , batch.input_ids.shape ) self.assertEqual((2, 15) , batch.attention_mask.shape ) __lowerCamelCase = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , a ) self.assertEqual(a , batch.decoder_input_ids[0, 0] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [EN_CODE] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" __lowerCamelCase = self.tokenizer(self.src_text , padding=a , truncation=a , max_length=3 , return_tensors='''pt''' ) __lowerCamelCase = self.tokenizer( text_target=self.tgt_text , padding=a , truncation=a , max_length=10 , return_tensors='''pt''' ) __lowerCamelCase = targets['''input_ids'''] __lowerCamelCase = shift_tokens_right( a , self.tokenizer.pad_token_id , decoder_start_token_id=self.tokenizer.lang_code_to_id[self.tokenizer.tgt_lang] , ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = self.tokenizer._build_translation_inputs( '''A test''' , return_tensors='''pt''' , src_lang='''eng_Latn''' , tgt_lang='''fra_Latn''' ) self.assertEqual( nested_simplify(a ) , { # A, test, EOS, en_XX '''input_ids''': [[25_60_47, 70, 73_56, 2]], '''attention_mask''': [[1, 1, 1, 1]], # ar_AR '''forced_bos_token_id''': 25_60_57, } , ) @require_torch def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = True __lowerCamelCase = self.tokenizer( '''UN Chief says there is no military solution in Syria''' , src_lang='''eng_Latn''' , tgt_lang='''fra_Latn''' ) self.assertEqual( inputs.input_ids , [1_62_97, 13_44_08, 2_56_53, 63_70, 2_48, 2_54, 10_39_29, 9_49_95, 1_08, 4_94_86, 2, 25_60_47] ) __lowerCamelCase = False __lowerCamelCase = self.tokenizer( '''UN Chief says there is no military solution in Syria''' , src_lang='''eng_Latn''' , tgt_lang='''fra_Latn''' ) self.assertEqual( inputs.input_ids , [25_60_47, 1_62_97, 13_44_08, 2_56_53, 63_70, 2_48, 2_54, 10_39_29, 9_49_95, 1_08, 4_94_86, 2] )
67
'''simple docstring''' from typing import List, Union import numpy as np from ..utils import add_end_docstrings, 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(): import torch from ..models.auto.modeling_auto import MODEL_FOR_DEPTH_ESTIMATION_MAPPING __UpperCAmelCase =logging.get_logger(__name__) @add_end_docstrings(UpperCAmelCase__ ) class a__ ( UpperCAmelCase__ ): def __init__( self : List[str] , *a : Union[str, Any] , **a : Optional[Any] ): """simple docstring""" super().__init__(*a , **a ) requires_backends(self , '''vision''' ) self.check_model_type(a ) def __call__( self : Any , a : Union[str, List[str], "Image.Image", List["Image.Image"]] , **a : Optional[int] ): """simple docstring""" return super().__call__(a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , **a : Any ): """simple docstring""" return {}, {}, {} def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : List[str] ): """simple docstring""" __lowerCamelCase = load_image(a ) __lowerCamelCase = image.size __lowerCamelCase = self.image_processor(images=a , return_tensors=self.framework ) return model_inputs def SCREAMING_SNAKE_CASE__ ( self : int , a : Optional[Any] ): """simple docstring""" __lowerCamelCase = self.model(**a ) return model_outputs def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : Any ): """simple docstring""" __lowerCamelCase = model_outputs.predicted_depth __lowerCamelCase = torch.nn.functional.interpolate( predicted_depth.unsqueeze(1 ) , size=self.image_size[::-1] , mode='''bicubic''' , align_corners=a ) __lowerCamelCase = prediction.squeeze().cpu().numpy() __lowerCamelCase = (output * 2_55 / np.max(a )).astype('''uint8''' ) __lowerCamelCase = Image.fromarray(a ) __lowerCamelCase = {} __lowerCamelCase = predicted_depth __lowerCamelCase = depth return output_dict
67
1
'''simple docstring''' import argparse import re from typing import Dict import torch from datasets import Audio, Dataset, load_dataset, load_metric from transformers import AutoFeatureExtractor, pipeline def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Tuple: __lowerCamelCase = args.log_outputs __lowerCamelCase = '''_'''.join(args.dataset.split('''/''' ) + [args.config, args.split] ) # load metric __lowerCamelCase = load_metric('''wer''' ) __lowerCamelCase = load_metric('''cer''' ) # compute metrics __lowerCamelCase = wer.compute(references=result['''target'''] , predictions=result['''prediction'''] ) __lowerCamelCase = cer.compute(references=result['''target'''] , predictions=result['''prediction'''] ) # print & log results __lowerCamelCase = f"""WER: {wer_result}\nCER: {cer_result}""" print(UpperCamelCase__ ) with open(f"""{dataset_id}_eval_results.txt""" , '''w''' ) as f: f.write(UpperCamelCase__ ) # log all results in text file. Possibly interesting for analysis if log_outputs is not None: __lowerCamelCase = f"""log_{dataset_id}_predictions.txt""" __lowerCamelCase = f"""log_{dataset_id}_targets.txt""" with open(UpperCamelCase__ , '''w''' ) as p, open(UpperCamelCase__ , '''w''' ) as t: # mapping function to write output def write_to_file(UpperCamelCase__ , UpperCamelCase__ ): p.write(f"""{i}""" + '''\n''' ) p.write(batch['''prediction'''] + '''\n''' ) t.write(f"""{i}""" + '''\n''' ) t.write(batch['''target'''] + '''\n''' ) result.map(UpperCamelCase__ , with_indices=UpperCamelCase__ ) def __lowerCAmelCase ( UpperCamelCase__ ) -> str: __lowerCamelCase = '''[,?.!\-\;\:"“%‘”�—’…–]''' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training __lowerCamelCase = re.sub(UpperCamelCase__ , '''''' , text.lower() ) # In addition, we can normalize the target text, e.g. removing new lines characters etc... # note that order is important here! __lowerCamelCase = ['''\n\n''', '''\n''', ''' ''', ''' '''] for t in token_sequences_to_ignore: __lowerCamelCase = ''' '''.join(text.split(UpperCamelCase__ ) ) return text def __lowerCAmelCase ( UpperCamelCase__ ) -> List[Any]: # load dataset __lowerCamelCase = load_dataset(args.dataset , args.config , split=args.split , use_auth_token=UpperCamelCase__ ) # for testing: only process the first two examples as a test # dataset = dataset.select(range(10)) # load processor __lowerCamelCase = AutoFeatureExtractor.from_pretrained(args.model_id ) __lowerCamelCase = feature_extractor.sampling_rate # resample audio __lowerCamelCase = dataset.cast_column('''audio''' , Audio(sampling_rate=UpperCamelCase__ ) ) # load eval pipeline if args.device is None: __lowerCamelCase = 0 if torch.cuda.is_available() else -1 __lowerCamelCase = pipeline('''automatic-speech-recognition''' , model=args.model_id , device=args.device ) # map function to decode audio def map_to_pred(UpperCamelCase__ ): __lowerCamelCase = asr( batch['''audio''']['''array'''] , chunk_length_s=args.chunk_length_s , stride_length_s=args.stride_length_s ) __lowerCamelCase = prediction['''text'''] __lowerCamelCase = normalize_text(batch['''sentence'''] ) return batch # run inference on all examples __lowerCamelCase = dataset.map(UpperCamelCase__ , remove_columns=dataset.column_names ) # compute and log_results # do not change function below log_results(UpperCamelCase__ , UpperCamelCase__ ) if __name__ == "__main__": __UpperCAmelCase =argparse.ArgumentParser() parser.add_argument( "--model_id", type=str, required=True, help="Model identifier. Should be loadable with 🤗 Transformers" ) parser.add_argument( "--dataset", type=str, required=True, help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets", ) parser.add_argument( "--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice" ) parser.add_argument("--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`") parser.add_argument( "--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to 5 seconds." ) parser.add_argument( "--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to 1 second." ) parser.add_argument( "--log_outputs", action="store_true", help="If defined, write outputs to log file for analysis." ) parser.add_argument( "--device", type=int, default=None, help="The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.", ) __UpperCAmelCase =parser.parse_args() main(args)
67
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase ={ "configuration_clap": [ "CLAP_PRETRAINED_MODEL_ARCHIVE_LIST", "ClapAudioConfig", "ClapConfig", "ClapTextConfig", ], "processing_clap": ["ClapProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "CLAP_PRETRAINED_MODEL_ARCHIVE_LIST", "ClapModel", "ClapPreTrainedModel", "ClapTextModel", "ClapTextModelWithProjection", "ClapAudioModel", "ClapAudioModelWithProjection", ] __UpperCAmelCase =["ClapFeatureExtractor"] if TYPE_CHECKING: from .configuration_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioConfig, ClapConfig, ClapTextConfig, ) from .processing_clap import ClapProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clap import ClapFeatureExtractor from .modeling_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioModel, ClapAudioModelWithProjection, ClapModel, ClapPreTrainedModel, ClapTextModel, ClapTextModelWithProjection, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
67
1
'''simple docstring''' import os import shutil import tempfile import unittest import numpy as np from transformers import AutoTokenizer, BarkProcessor from transformers.testing_utils import require_torch, slow @require_torch class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = '''ylacombe/bark-small''' __lowerCamelCase = tempfile.mkdtemp() __lowerCamelCase = '''en_speaker_1''' __lowerCamelCase = '''This is a test string''' __lowerCamelCase = '''speaker_embeddings_path.json''' __lowerCamelCase = '''speaker_embeddings''' def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , **a : Dict ): """simple docstring""" return AutoTokenizer.from_pretrained(self.checkpoint , **a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) processor.save_pretrained(self.tmpdirname ) __lowerCamelCase = BarkProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) @slow def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) processor.save_pretrained( self.tmpdirname , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , speaker_embeddings_directory=self.speaker_embeddings_directory , ) __lowerCamelCase = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) __lowerCamelCase = BarkProcessor.from_pretrained( self.tmpdirname , self.speaker_embeddings_dict_path , bos_token='''(BOS)''' , eos_token='''(EOS)''' , ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) __lowerCamelCase = 35 __lowerCamelCase = 2 __lowerCamelCase = 8 __lowerCamelCase = { '''semantic_prompt''': np.ones(a ), '''coarse_prompt''': np.ones((nb_codebooks_coarse, seq_len) ), '''fine_prompt''': np.ones((nb_codebooks_total, seq_len) ), } # test providing already loaded voice_preset __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from npz file __lowerCamelCase = os.path.join(self.tmpdirname , '''file.npz''' ) np.savez(a , **a ) __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from the hub __lowerCamelCase = processor(text=self.input_string , voice_preset=self.voice_preset ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) __lowerCamelCase = processor(text=self.input_string ) __lowerCamelCase = tokenizer( self.input_string , padding='''max_length''' , max_length=2_56 , add_special_tokens=a , return_attention_mask=a , return_token_type_ids=a , ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key].squeeze().tolist() )
67
'''simple docstring''' import os import shutil import tempfile import unittest import numpy as np from transformers import AutoTokenizer, BarkProcessor from transformers.testing_utils import require_torch, slow @require_torch class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = '''ylacombe/bark-small''' __lowerCamelCase = tempfile.mkdtemp() __lowerCamelCase = '''en_speaker_1''' __lowerCamelCase = '''This is a test string''' __lowerCamelCase = '''speaker_embeddings_path.json''' __lowerCamelCase = '''speaker_embeddings''' def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , **a : Dict ): """simple docstring""" return AutoTokenizer.from_pretrained(self.checkpoint , **a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) processor.save_pretrained(self.tmpdirname ) __lowerCamelCase = BarkProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) @slow def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) processor.save_pretrained( self.tmpdirname , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , speaker_embeddings_directory=self.speaker_embeddings_directory , ) __lowerCamelCase = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) __lowerCamelCase = BarkProcessor.from_pretrained( self.tmpdirname , self.speaker_embeddings_dict_path , bos_token='''(BOS)''' , eos_token='''(EOS)''' , ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) __lowerCamelCase = 35 __lowerCamelCase = 2 __lowerCamelCase = 8 __lowerCamelCase = { '''semantic_prompt''': np.ones(a ), '''coarse_prompt''': np.ones((nb_codebooks_coarse, seq_len) ), '''fine_prompt''': np.ones((nb_codebooks_total, seq_len) ), } # test providing already loaded voice_preset __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from npz file __lowerCamelCase = os.path.join(self.tmpdirname , '''file.npz''' ) np.savez(a , **a ) __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from the hub __lowerCamelCase = processor(text=self.input_string , voice_preset=self.voice_preset ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) __lowerCamelCase = processor(text=self.input_string ) __lowerCamelCase = tokenizer( self.input_string , padding='''max_length''' , max_length=2_56 , add_special_tokens=a , return_attention_mask=a , return_token_type_ids=a , ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key].squeeze().tolist() )
67
1
'''simple docstring''' import json import os import pickle import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers import is_faiss_available from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bart.tokenization_bart import BartTokenizer from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch if is_faiss_available(): import faiss @require_faiss class a__ ( UpperCAmelCase__ ): def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = tempfile.mkdtemp() __lowerCamelCase = 8 # DPR tok __lowerCamelCase = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] __lowerCamelCase = os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(a , exist_ok=a ) __lowerCamelCase = os.path.join(a , DPR_VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) # BART tok __lowerCamelCase = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] __lowerCamelCase = dict(zip(a , range(len(a ) ) ) ) __lowerCamelCase = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] __lowerCamelCase = {'''unk_token''': '''<unk>'''} __lowerCamelCase = os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(a , exist_ok=a ) __lowerCamelCase = os.path.join(a , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) __lowerCamelCase = os.path.join(a , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(a ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(a ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size ), 2 * np.ones(self.retrieval_vector_size )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) return dataset def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = self.get_dummy_dataset() __lowerCamelCase = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , ) with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: __lowerCamelCase = dataset __lowerCamelCase = RagRetriever( a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def SCREAMING_SNAKE_CASE__ ( self : str , a : bool ): """simple docstring""" __lowerCamelCase = self.get_dummy_dataset() __lowerCamelCase = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''custom''' , ) if from_disk: __lowerCamelCase = os.path.join(self.tmpdirname , '''dataset''' ) __lowerCamelCase = os.path.join(self.tmpdirname , '''index.faiss''' ) dataset.get_index('''embeddings''' ).save(os.path.join(self.tmpdirname , '''index.faiss''' ) ) dataset.drop_index('''embeddings''' ) dataset.save_to_disk(os.path.join(self.tmpdirname , '''dataset''' ) ) del dataset __lowerCamelCase = RagRetriever( a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: __lowerCamelCase = RagRetriever( a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , a ) , ) return retriever def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" __lowerCamelCase = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size + 1 ), 2 * np.ones(self.retrieval_vector_size + 1 )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) __lowerCamelCase = os.path.join(self.tmpdirname , '''hf_bert_base.hnswSQ8_correct_phi_128.c_index''' ) dataset.save_faiss_index('''embeddings''' , index_file_name + '''.index.dpr''' ) pickle.dump(dataset['''id'''] , open(index_file_name + '''.index_meta.dpr''' , '''wb''' ) ) __lowerCamelCase = os.path.join(self.tmpdirname , '''psgs_w100.tsv.pkl''' ) __lowerCamelCase = {sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset} pickle.dump(a , open(a , '''wb''' ) ) __lowerCamelCase = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''legacy''' , index_path=self.tmpdirname , ) __lowerCamelCase = RagRetriever( a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = 1 __lowerCamelCase = self.get_dummy_canonical_hf_index_retriever() __lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = retriever.retrieve(a , n_docs=a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , a ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" __lowerCamelCase = self.get_dummy_canonical_hf_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: __lowerCamelCase = self.get_dummy_dataset() retriever.save_pretrained(a ) __lowerCamelCase = RagRetriever.from_pretrained(a ) self.assertIsInstance(a , a ) __lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) __lowerCamelCase = retriever.retrieve(a , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = 1 __lowerCamelCase = self.get_dummy_custom_hf_index_retriever(from_disk=a ) __lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = retriever.retrieve(a , n_docs=a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , a ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = self.get_dummy_custom_hf_index_retriever(from_disk=a ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(a ) __lowerCamelCase = RagRetriever.from_pretrained(a ) self.assertIsInstance(a , a ) __lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) __lowerCamelCase = retriever.retrieve(a , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = 1 __lowerCamelCase = self.get_dummy_custom_hf_index_retriever(from_disk=a ) __lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = retriever.retrieve(a , n_docs=a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , a ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = self.get_dummy_custom_hf_index_retriever(from_disk=a ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(a ) __lowerCamelCase = RagRetriever.from_pretrained(a ) self.assertIsInstance(a , a ) __lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) __lowerCamelCase = retriever.retrieve(a , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = 1 __lowerCamelCase = self.get_dummy_legacy_index_retriever() __lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = retriever.retrieve(a , n_docs=a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''text'''] ) , a ) self.assertEqual(doc_dicts[0]['''text'''][0] , '''bar''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''text'''][0] , '''foo''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(a ) __lowerCamelCase = RagRetriever.from_pretrained(a ) self.assertIsInstance(a , a ) __lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) __lowerCamelCase = retriever.retrieve(a , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" import torch __lowerCamelCase = 1 __lowerCamelCase = self.get_dummy_canonical_hf_index_retriever() __lowerCamelCase = [[5, 7], [10, 11]] __lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) __lowerCamelCase = retriever(a , a , prefix=retriever.config.generator.prefix , n_docs=a ) __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = ( out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(a , a ) self.assertIsInstance(a , a ) self.assertIsInstance(a , np.ndarray ) __lowerCamelCase = retriever( a , a , prefix=retriever.config.generator.prefix , n_docs=a , return_tensors='''pt''' , ) __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = ( # noqa: F841 out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], out['''doc_ids'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(a , torch.Tensor ) self.assertIsInstance(a , torch.Tensor ) self.assertIsInstance(a , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" __lowerCamelCase = self.get_dpr_ctx_encoder_tokenizer() __lowerCamelCase = 1 __lowerCamelCase = self.get_dummy_custom_hf_index_retriever(from_disk=a ) retriever.set_ctx_encoder_tokenizer(a ) __lowerCamelCase = [[5, 7], [10, 11]] __lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) __lowerCamelCase = retriever(a , a , prefix=retriever.config.generator.prefix , n_docs=a ) self.assertEqual( len(a ) , 6 ) # check whether the retriever output consist of 6 attributes including tokenized docs self.assertEqual( all(k in out for k in ('''tokenized_doc_ids''', '''tokenized_doc_attention_mask''') ) , a ) # check for doc token related keys in dictionary.
67
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase ={"configuration_vit_msn": ["VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP", "ViTMSNConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST", "ViTMSNModel", "ViTMSNForImageClassification", "ViTMSNPreTrainedModel", ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
67
1
'''simple docstring''' import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__=0.9_9_9 , UpperCamelCase__="cosine" , ) -> Optional[int]: if alpha_transform_type == "cosine": def alpha_bar_fn(UpperCamelCase__ ): return math.cos((t + 0.0_0_8) / 1.0_0_8 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(UpperCamelCase__ ): return math.exp(t * -1_2.0 ) else: raise ValueError(f"""Unsupported alpha_tranform_type: {alpha_transform_type}""" ) __lowerCamelCase = [] for i in range(UpperCamelCase__ ): __lowerCamelCase = i / num_diffusion_timesteps __lowerCamelCase = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(UpperCamelCase__ ) / alpha_bar_fn(UpperCamelCase__ ) , UpperCamelCase__ ) ) return torch.tensor(UpperCamelCase__ , dtype=torch.floataa ) class a__ ( UpperCAmelCase__ , UpperCAmelCase__ ): lowerCamelCase : Dict =[e.name for e in KarrasDiffusionSchedulers] lowerCamelCase : Optional[Any] =2 @register_to_config def __init__( self : Optional[Any] , a : int = 10_00 , a : float = 0.0_00_85 , a : float = 0.0_12 , a : str = "linear" , a : Optional[Union[np.ndarray, List[float]]] = None , a : str = "epsilon" , a : Optional[bool] = False , a : Optional[bool] = False , a : float = 1.0 , a : str = "linspace" , a : int = 0 , ): """simple docstring""" if trained_betas is not None: __lowerCamelCase = torch.tensor(a , dtype=torch.floataa ) elif beta_schedule == "linear": __lowerCamelCase = torch.linspace(a , a , a , dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. __lowerCamelCase = ( torch.linspace(beta_start**0.5 , beta_end**0.5 , a , dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule __lowerCamelCase = betas_for_alpha_bar(a , alpha_transform_type='''cosine''' ) elif beta_schedule == "exp": __lowerCamelCase = betas_for_alpha_bar(a , alpha_transform_type='''exp''' ) else: raise NotImplementedError(f"""{beta_schedule} does is not implemented for {self.__class__}""" ) __lowerCamelCase = 1.0 - self.betas __lowerCamelCase = torch.cumprod(self.alphas , dim=0 ) # set all values self.set_timesteps(a , a , a ) __lowerCamelCase = use_karras_sigmas def SCREAMING_SNAKE_CASE__ ( self : Dict , a : Optional[Any] , a : List[Any]=None ): """simple docstring""" if schedule_timesteps is None: __lowerCamelCase = self.timesteps __lowerCamelCase = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: __lowerCamelCase = 1 if len(a ) > 1 else 0 else: __lowerCamelCase = timestep.cpu().item() if torch.is_tensor(a ) else timestep __lowerCamelCase = self._index_counter[timestep_int] return indices[pos].item() @property def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : torch.FloatTensor , a : Union[float, torch.FloatTensor] , ): """simple docstring""" __lowerCamelCase = self.index_for_timestep(a ) __lowerCamelCase = self.sigmas[step_index] __lowerCamelCase = sample / ((sigma**2 + 1) ** 0.5) return sample def SCREAMING_SNAKE_CASE__ ( self : Any , a : int , a : Union[str, torch.device] = None , a : Optional[int] = None , ): """simple docstring""" __lowerCamelCase = num_inference_steps __lowerCamelCase = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": __lowerCamelCase = np.linspace(0 , num_train_timesteps - 1 , a , dtype=a )[::-1].copy() elif self.config.timestep_spacing == "leading": __lowerCamelCase = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 __lowerCamelCase = (np.arange(0 , a ) * step_ratio).round()[::-1].copy().astype(a ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": __lowerCamelCase = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 __lowerCamelCase = (np.arange(a , 0 , -step_ratio )).round().copy().astype(a ) timesteps -= 1 else: raise ValueError( f"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'.""" ) __lowerCamelCase = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) __lowerCamelCase = np.log(a ) __lowerCamelCase = np.interp(a , np.arange(0 , len(a ) ) , a ) if self.config.use_karras_sigmas: __lowerCamelCase = self._convert_to_karras(in_sigmas=a , num_inference_steps=self.num_inference_steps ) __lowerCamelCase = np.array([self._sigma_to_t(a , a ) for sigma in sigmas] ) __lowerCamelCase = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) __lowerCamelCase = torch.from_numpy(a ).to(device=a ) __lowerCamelCase = torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2 ), sigmas[-1:]] ) __lowerCamelCase = torch.from_numpy(a ) __lowerCamelCase = torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2 )] ) if str(a ).startswith('''mps''' ): # mps does not support float64 __lowerCamelCase = timesteps.to(a , dtype=torch.floataa ) else: __lowerCamelCase = timesteps.to(device=a ) # empty dt and derivative __lowerCamelCase = None __lowerCamelCase = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter __lowerCamelCase = defaultdict(a ) def SCREAMING_SNAKE_CASE__ ( self : int , a : Any , a : List[Any] ): """simple docstring""" __lowerCamelCase = np.log(a ) # get distribution __lowerCamelCase = log_sigma - log_sigmas[:, np.newaxis] # get sigmas range __lowerCamelCase = np.cumsum((dists >= 0) , axis=0 ).argmax(axis=0 ).clip(max=log_sigmas.shape[0] - 2 ) __lowerCamelCase = low_idx + 1 __lowerCamelCase = log_sigmas[low_idx] __lowerCamelCase = log_sigmas[high_idx] # interpolate sigmas __lowerCamelCase = (low - log_sigma) / (low - high) __lowerCamelCase = np.clip(a , 0 , 1 ) # transform interpolation to time range __lowerCamelCase = (1 - w) * low_idx + w * high_idx __lowerCamelCase = t.reshape(sigma.shape ) return t def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : torch.FloatTensor , a : Dict ): """simple docstring""" __lowerCamelCase = in_sigmas[-1].item() __lowerCamelCase = in_sigmas[0].item() __lowerCamelCase = 7.0 # 7.0 is the value used in the paper __lowerCamelCase = np.linspace(0 , 1 , a ) __lowerCamelCase = sigma_min ** (1 / rho) __lowerCamelCase = sigma_max ** (1 / rho) __lowerCamelCase = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho return sigmas @property def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" return self.dt is None def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : Union[torch.FloatTensor, np.ndarray] , a : Union[float, torch.FloatTensor] , a : Union[torch.FloatTensor, np.ndarray] , a : bool = True , ): """simple docstring""" __lowerCamelCase = self.index_for_timestep(a ) # advance index counter by 1 __lowerCamelCase = timestep.cpu().item() if torch.is_tensor(a ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: __lowerCamelCase = self.sigmas[step_index] __lowerCamelCase = self.sigmas[step_index + 1] else: # 2nd order / Heun's method __lowerCamelCase = self.sigmas[step_index - 1] __lowerCamelCase = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API __lowerCamelCase = 0 __lowerCamelCase = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": __lowerCamelCase = sigma_hat if self.state_in_first_order else sigma_next __lowerCamelCase = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": __lowerCamelCase = sigma_hat if self.state_in_first_order else sigma_next __lowerCamelCase = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": __lowerCamelCase = model_output else: raise ValueError( f"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`""" ) if self.config.clip_sample: __lowerCamelCase = pred_original_sample.clamp( -self.config.clip_sample_range , self.config.clip_sample_range ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order __lowerCamelCase = (sample - pred_original_sample) / sigma_hat # 3. delta timestep __lowerCamelCase = sigma_next - sigma_hat # store for 2nd order step __lowerCamelCase = derivative __lowerCamelCase = dt __lowerCamelCase = sample else: # 2. 2nd order / Heun's method __lowerCamelCase = (sample - pred_original_sample) / sigma_next __lowerCamelCase = (self.prev_derivative + derivative) / 2 # 3. take prev timestep & sample __lowerCamelCase = self.dt __lowerCamelCase = self.sample # free dt and derivative # Note, this puts the scheduler in "first order mode" __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=a ) def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : torch.FloatTensor , a : torch.FloatTensor , a : torch.FloatTensor , ): """simple docstring""" __lowerCamelCase = self.sigmas.to(device=original_samples.device , dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(a ): # mps does not support float64 __lowerCamelCase = self.timesteps.to(original_samples.device , dtype=torch.floataa ) __lowerCamelCase = timesteps.to(original_samples.device , dtype=torch.floataa ) else: __lowerCamelCase = self.timesteps.to(original_samples.device ) __lowerCamelCase = timesteps.to(original_samples.device ) __lowerCamelCase = [self.index_for_timestep(a , a ) for t in timesteps] __lowerCamelCase = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): __lowerCamelCase = sigma.unsqueeze(-1 ) __lowerCamelCase = original_samples + noise * sigma return noisy_samples def __len__( self : Tuple ): """simple docstring""" return self.config.num_train_timesteps
67
'''simple docstring''' import re from filelock import FileLock try: import nltk __UpperCAmelCase =True except (ImportError, ModuleNotFoundError): __UpperCAmelCase =False if NLTK_AVAILABLE: with FileLock(".lock") as lock: nltk.download("punkt", quiet=True) def __lowerCAmelCase ( UpperCamelCase__ ) -> str: re.sub('''<n>''' , '''''' , UpperCamelCase__ ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(UpperCamelCase__ ) )
67
1
'''simple docstring''' # Copyright 2022 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os import platform import numpy as np import psutil import torch from accelerate import __version__ as version from accelerate.commands.config import default_config_file, load_config_from_file from ..utils import is_npu_available, is_xpu_available def __lowerCAmelCase ( UpperCamelCase__=None ) -> Tuple: if subparsers is not None: __lowerCamelCase = subparsers.add_parser('''env''' ) else: __lowerCamelCase = argparse.ArgumentParser('''Accelerate env command''' ) parser.add_argument( '''--config_file''' , default=UpperCamelCase__ , help='''The config file to use for the default values in the launching script.''' ) if subparsers is not None: parser.set_defaults(func=UpperCamelCase__ ) return parser def __lowerCAmelCase ( UpperCamelCase__ ) -> Union[str, Any]: __lowerCamelCase = torch.__version__ __lowerCamelCase = torch.cuda.is_available() __lowerCamelCase = is_xpu_available() __lowerCamelCase = is_npu_available() __lowerCamelCase = '''Not found''' # Get the default from the config file. if args.config_file is not None or os.path.isfile(UpperCamelCase__ ): __lowerCamelCase = load_config_from_file(args.config_file ).to_dict() __lowerCamelCase = { '''`Accelerate` version''': version, '''Platform''': platform.platform(), '''Python version''': platform.python_version(), '''Numpy version''': np.__version__, '''PyTorch version (GPU?)''': f"""{pt_version} ({pt_cuda_available})""", '''PyTorch XPU available''': str(UpperCamelCase__ ), '''PyTorch NPU available''': str(UpperCamelCase__ ), '''System RAM''': f"""{psutil.virtual_memory().total / 10_24 ** 3:.2f} GB""", } if pt_cuda_available: __lowerCamelCase = torch.cuda.get_device_name() print('''\nCopy-and-paste the text below in your GitHub issue\n''' ) print('''\n'''.join([f"""- {prop}: {val}""" for prop, val in info.items()] ) ) print('''- `Accelerate` default config:''' if args.config_file is None else '''- `Accelerate` config passed:''' ) __lowerCamelCase = ( '''\n'''.join([f"""\t- {prop}: {val}""" for prop, val in accelerate_config.items()] ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else f"""\t{accelerate_config}""" ) print(UpperCamelCase__ ) __lowerCamelCase = accelerate_config return info def __lowerCAmelCase ( ) -> int: __lowerCamelCase = env_command_parser() __lowerCamelCase = parser.parse_args() env_command(UpperCamelCase__ ) return 0 if __name__ == "__main__": raise SystemExit(main())
67
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={ "abeja/gpt-neox-japanese-2.7b": "https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json", } class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] ="gpt_neox_japanese" def __init__( self : List[Any] , a : Tuple=3_20_00 , a : Dict=25_60 , a : Union[str, Any]=32 , a : Dict=32 , a : Dict=4 , a : Optional[Any]="gelu" , a : Any=1.00 , a : str=1_00_00 , a : List[str]=20_48 , a : str=0.02 , a : Union[str, Any]=1e-5 , a : Optional[Any]=True , a : str=3_19_96 , a : List[str]=3_19_99 , a : str=0.1 , a : Union[str, Any]=0.0 , **a : Optional[Any] , ): """simple docstring""" super().__init__(bos_token_id=a , eos_token_id=a , **a ) __lowerCamelCase = vocab_size __lowerCamelCase = max_position_embeddings __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_multiple_size __lowerCamelCase = hidden_act __lowerCamelCase = rotary_pct __lowerCamelCase = rotary_emb_base __lowerCamelCase = initializer_range __lowerCamelCase = layer_norm_eps __lowerCamelCase = use_cache __lowerCamelCase = attention_dropout __lowerCamelCase = hidden_dropout
67
1
'''simple docstring''' from typing import List, Optional import numpy as np from ...processing_utils import ProcessorMixin from ...utils import to_numpy class a__ ( UpperCAmelCase__ ): lowerCamelCase : Dict ="EncodecFeatureExtractor" lowerCamelCase : List[str] =("T5Tokenizer", "T5TokenizerFast") def __init__( self : Dict , a : Optional[int] , a : Optional[int] ): """simple docstring""" super().__init__(a , a ) __lowerCamelCase = self.feature_extractor __lowerCamelCase = False def SCREAMING_SNAKE_CASE__ ( self : int , a : Optional[Any]=None , a : List[str]=None , a : Tuple=True ): """simple docstring""" return self.tokenizer.get_decoder_prompt_ids(task=a , language=a , no_timestamps=a ) def __call__( self : int , *a : List[str] , **a : List[Any] ): """simple docstring""" if self._in_target_context_manager: return self.current_processor(*a , **a ) __lowerCamelCase = kwargs.pop('''audio''' , a ) __lowerCamelCase = kwargs.pop('''sampling_rate''' , a ) __lowerCamelCase = kwargs.pop('''text''' , a ) if len(a ) > 0: __lowerCamelCase = args[0] __lowerCamelCase = args[1:] if audio is None and text is None: raise ValueError('''You need to specify either an `audio` or `text` input to process.''' ) if text is not None: __lowerCamelCase = self.tokenizer(a , **a ) if audio is not None: __lowerCamelCase = self.feature_extractor(a , *a , sampling_rate=a , **a ) if audio is None: return inputs elif text is None: return audio_inputs else: __lowerCamelCase = audio_inputs['''input_values'''] if "padding_mask" in audio_inputs: __lowerCamelCase = audio_inputs['''padding_mask'''] return inputs def SCREAMING_SNAKE_CASE__ ( self : Tuple , *a : Any , **a : str ): """simple docstring""" __lowerCamelCase = kwargs.pop('''audio''' , a ) __lowerCamelCase = kwargs.pop('''padding_mask''' , a ) if len(a ) > 0: __lowerCamelCase = args[0] __lowerCamelCase = args[1:] if audio_values is not None: return self._decode_audio(a , padding_mask=a ) else: return self.tokenizer.batch_decode(*a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , *a : Dict , **a : List[Any] ): """simple docstring""" return self.tokenizer.decode(*a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Dict , a : Any , a : Optional = None ): """simple docstring""" __lowerCamelCase = to_numpy(a ) __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = audio_values.shape if padding_mask is None: return list(a ) __lowerCamelCase = to_numpy(a ) # match the sequence length of the padding mask to the generated audio arrays by padding with the **non-padding** # token (so that the generated audio values are **not** treated as padded tokens) __lowerCamelCase = seq_len - padding_mask.shape[-1] __lowerCamelCase = 1 - self.feature_extractor.padding_value __lowerCamelCase = np.pad(a , ((0, 0), (0, difference)) , '''constant''' , constant_values=a ) __lowerCamelCase = audio_values.tolist() for i in range(a ): __lowerCamelCase = np.asarray(audio_values[i] )[ padding_mask[i][None, :] != self.feature_extractor.padding_value ] __lowerCamelCase = sliced_audio.reshape(a , -1 ) return audio_values
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> int: while second != 0: __lowerCamelCase = first & second first ^= second __lowerCamelCase = c << 1 return first if __name__ == "__main__": import doctest doctest.testmod() __UpperCAmelCase =int(input("Enter the first number: ").strip()) __UpperCAmelCase =int(input("Enter the second number: ").strip()) print(f'{add(first, second) = }')
67
1
'''simple docstring''' import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) class a__ ( UpperCAmelCase__ ): def __init__( self : Optional[Any] , a : Union[List[ControlNetModel], Tuple[ControlNetModel]] ): """simple docstring""" super().__init__() __lowerCamelCase = nn.ModuleList(a ) def SCREAMING_SNAKE_CASE__ ( self : Any , a : torch.FloatTensor , a : Union[torch.Tensor, float, int] , a : torch.Tensor , a : List[torch.tensor] , a : List[float] , a : Optional[torch.Tensor] = None , a : Optional[torch.Tensor] = None , a : Optional[torch.Tensor] = None , a : Optional[Dict[str, Any]] = None , a : bool = False , a : bool = True , ): """simple docstring""" for i, (image, scale, controlnet) in enumerate(zip(a , a , self.nets ) ): __lowerCamelCase , __lowerCamelCase = controlnet( a , a , a , a , a , a , a , a , a , a , a , ) # merge samples if i == 0: __lowerCamelCase , __lowerCamelCase = down_samples, mid_sample else: __lowerCamelCase = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(a , a ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def SCREAMING_SNAKE_CASE__ ( self : Any , a : Union[str, os.PathLike] , a : bool = True , a : Callable = None , a : bool = False , a : Optional[str] = None , ): """simple docstring""" __lowerCamelCase = 0 __lowerCamelCase = save_directory for controlnet in self.nets: controlnet.save_pretrained( a , is_main_process=a , save_function=a , safe_serialization=a , variant=a , ) idx += 1 __lowerCamelCase = model_path_to_save + f"""_{idx}""" @classmethod def SCREAMING_SNAKE_CASE__ ( cls : List[str] , a : Optional[Union[str, os.PathLike]] , **a : Optional[Any] ): """simple docstring""" __lowerCamelCase = 0 __lowerCamelCase = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... __lowerCamelCase = pretrained_model_path while os.path.isdir(a ): __lowerCamelCase = ControlNetModel.from_pretrained(a , **a ) controlnets.append(a ) idx += 1 __lowerCamelCase = pretrained_model_path + f"""_{idx}""" logger.info(f"""{len(a )} controlnets loaded from {pretrained_model_path}.""" ) if len(a ) == 0: raise ValueError( f"""No ControlNets found under {os.path.dirname(a )}. Expected at least {pretrained_model_path + '_0'}.""" ) return cls(a )
67
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase ={ "configuration_time_series_transformer": [ "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimeSeriesTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimeSeriesTransformerForPrediction", "TimeSeriesTransformerModel", "TimeSeriesTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
67
1
'''simple docstring''' import collections import gzip import os import urllib import numpy from tensorflow.python.framework import dtypes, random_seed from tensorflow.python.platform import gfile from tensorflow.python.util.deprecation import deprecated __UpperCAmelCase =collections.namedtuple("_Datasets", ["train", "validation", "test"]) # CVDF mirror of http://yann.lecun.com/exdb/mnist/ __UpperCAmelCase ="https://storage.googleapis.com/cvdf-datasets/mnist/" def __lowerCAmelCase ( UpperCamelCase__ ) -> Any: __lowerCamelCase = numpy.dtype(numpy.uintaa ).newbyteorder('''>''' ) return numpy.frombuffer(bytestream.read(4 ) , dtype=UpperCamelCase__ )[0] @deprecated(UpperCamelCase__ , '''Please use tf.data to implement this functionality.''' ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[Any]: print('''Extracting''' , f.name ) with gzip.GzipFile(fileobj=UpperCamelCase__ ) as bytestream: __lowerCamelCase = _readaa(UpperCamelCase__ ) if magic != 20_51: raise ValueError( '''Invalid magic number %d in MNIST image file: %s''' % (magic, f.name) ) __lowerCamelCase = _readaa(UpperCamelCase__ ) __lowerCamelCase = _readaa(UpperCamelCase__ ) __lowerCamelCase = _readaa(UpperCamelCase__ ) __lowerCamelCase = bytestream.read(rows * cols * num_images ) __lowerCamelCase = numpy.frombuffer(UpperCamelCase__ , dtype=numpy.uinta ) __lowerCamelCase = data.reshape(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , 1 ) return data @deprecated(UpperCamelCase__ , '''Please use tf.one_hot on tensors.''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Dict: __lowerCamelCase = labels_dense.shape[0] __lowerCamelCase = numpy.arange(UpperCamelCase__ ) * num_classes __lowerCamelCase = numpy.zeros((num_labels, num_classes) ) __lowerCamelCase = 1 return labels_one_hot @deprecated(UpperCamelCase__ , '''Please use tf.data to implement this functionality.''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__=False , UpperCamelCase__=10 ) -> Union[str, Any]: print('''Extracting''' , f.name ) with gzip.GzipFile(fileobj=UpperCamelCase__ ) as bytestream: __lowerCamelCase = _readaa(UpperCamelCase__ ) if magic != 20_49: raise ValueError( '''Invalid magic number %d in MNIST label file: %s''' % (magic, f.name) ) __lowerCamelCase = _readaa(UpperCamelCase__ ) __lowerCamelCase = bytestream.read(UpperCamelCase__ ) __lowerCamelCase = numpy.frombuffer(UpperCamelCase__ , dtype=numpy.uinta ) if one_hot: return _dense_to_one_hot(UpperCamelCase__ , UpperCamelCase__ ) return labels class a__ : @deprecated( a , '''Please use alternatives such as official/mnist/_DataSet.py''' ''' from tensorflow/models.''' , ) def __init__( self : List[Any] , a : List[Any] , a : Tuple , a : Tuple=False , a : Tuple=False , a : Any=dtypes.floataa , a : Union[str, Any]=True , a : Tuple=None , ): """simple docstring""" __lowerCamelCase , __lowerCamelCase = random_seed.get_seed(a ) # If op level seed is not set, use whatever graph level seed is returned numpy.random.seed(seeda if seed is None else seeda ) __lowerCamelCase = dtypes.as_dtype(a ).base_dtype if dtype not in (dtypes.uinta, dtypes.floataa): raise TypeError('''Invalid image dtype %r, expected uint8 or float32''' % dtype ) if fake_data: __lowerCamelCase = 1_00_00 __lowerCamelCase = one_hot else: assert ( images.shape[0] == labels.shape[0] ), f"""images.shape: {images.shape} labels.shape: {labels.shape}""" __lowerCamelCase = images.shape[0] # Convert shape from [num examples, rows, columns, depth] # to [num examples, rows*columns] (assuming depth == 1) if reshape: assert images.shape[3] == 1 __lowerCamelCase = images.reshape( images.shape[0] , images.shape[1] * images.shape[2] ) if dtype == dtypes.floataa: # Convert from [0, 255] -> [0.0, 1.0]. __lowerCamelCase = images.astype(numpy.floataa ) __lowerCamelCase = numpy.multiply(a , 1.0 / 2_55.0 ) __lowerCamelCase = images __lowerCamelCase = labels __lowerCamelCase = 0 __lowerCamelCase = 0 @property def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" return self._images @property def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" return self._labels @property def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" return self._num_examples @property def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" return self._epochs_completed def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Any , a : List[Any]=False , a : Union[str, Any]=True ): """simple docstring""" if fake_data: __lowerCamelCase = [1] * 7_84 __lowerCamelCase = [1] + [0] * 9 if self.one_hot else 0 return ( [fake_image for _ in range(a )], [fake_label for _ in range(a )], ) __lowerCamelCase = self._index_in_epoch # Shuffle for the first epoch if self._epochs_completed == 0 and start == 0 and shuffle: __lowerCamelCase = numpy.arange(self._num_examples ) numpy.random.shuffle(a ) __lowerCamelCase = self.images[perma] __lowerCamelCase = self.labels[perma] # Go to the next epoch if start + batch_size > self._num_examples: # Finished epoch self._epochs_completed += 1 # Get the rest examples in this epoch __lowerCamelCase = self._num_examples - start __lowerCamelCase = self._images[start : self._num_examples] __lowerCamelCase = self._labels[start : self._num_examples] # Shuffle the data if shuffle: __lowerCamelCase = numpy.arange(self._num_examples ) numpy.random.shuffle(a ) __lowerCamelCase = self.images[perm] __lowerCamelCase = self.labels[perm] # Start next epoch __lowerCamelCase = 0 __lowerCamelCase = batch_size - rest_num_examples __lowerCamelCase = self._index_in_epoch __lowerCamelCase = self._images[start:end] __lowerCamelCase = self._labels[start:end] return ( numpy.concatenate((images_rest_part, images_new_part) , axis=0 ), numpy.concatenate((labels_rest_part, labels_new_part) , axis=0 ), ) else: self._index_in_epoch += batch_size __lowerCamelCase = self._index_in_epoch return self._images[start:end], self._labels[start:end] @deprecated(UpperCamelCase__ , '''Please write your own downloading logic.''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> List[str]: if not gfile.Exists(UpperCamelCase__ ): gfile.MakeDirs(UpperCamelCase__ ) __lowerCamelCase = os.path.join(UpperCamelCase__ , UpperCamelCase__ ) if not gfile.Exists(UpperCamelCase__ ): urllib.request.urlretrieve(UpperCamelCase__ , UpperCamelCase__ ) # noqa: S310 with gfile.GFile(UpperCamelCase__ ) as f: __lowerCamelCase = f.size() print('''Successfully downloaded''' , UpperCamelCase__ , UpperCamelCase__ , '''bytes.''' ) return filepath @deprecated( UpperCamelCase__ , '''Please use alternatives such as:''' ''' tensorflow_datasets.load(\'mnist\')''' ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__=False , UpperCamelCase__=False , UpperCamelCase__=dtypes.floataa , UpperCamelCase__=True , UpperCamelCase__=50_00 , UpperCamelCase__=None , UpperCamelCase__=DEFAULT_SOURCE_URL , ) -> Tuple: if fake_data: def fake(): return _DataSet( [] , [] , fake_data=UpperCamelCase__ , one_hot=UpperCamelCase__ , dtype=UpperCamelCase__ , seed=UpperCamelCase__ ) __lowerCamelCase = fake() __lowerCamelCase = fake() __lowerCamelCase = fake() return _Datasets(train=UpperCamelCase__ , validation=UpperCamelCase__ , test=UpperCamelCase__ ) if not source_url: # empty string check __lowerCamelCase = DEFAULT_SOURCE_URL __lowerCamelCase = '''train-images-idx3-ubyte.gz''' __lowerCamelCase = '''train-labels-idx1-ubyte.gz''' __lowerCamelCase = '''t10k-images-idx3-ubyte.gz''' __lowerCamelCase = '''t10k-labels-idx1-ubyte.gz''' __lowerCamelCase = _maybe_download( UpperCamelCase__ , UpperCamelCase__ , source_url + train_images_file ) with gfile.Open(UpperCamelCase__ , '''rb''' ) as f: __lowerCamelCase = _extract_images(UpperCamelCase__ ) __lowerCamelCase = _maybe_download( UpperCamelCase__ , UpperCamelCase__ , source_url + train_labels_file ) with gfile.Open(UpperCamelCase__ , '''rb''' ) as f: __lowerCamelCase = _extract_labels(UpperCamelCase__ , one_hot=UpperCamelCase__ ) __lowerCamelCase = _maybe_download( UpperCamelCase__ , UpperCamelCase__ , source_url + test_images_file ) with gfile.Open(UpperCamelCase__ , '''rb''' ) as f: __lowerCamelCase = _extract_images(UpperCamelCase__ ) __lowerCamelCase = _maybe_download( UpperCamelCase__ , UpperCamelCase__ , source_url + test_labels_file ) with gfile.Open(UpperCamelCase__ , '''rb''' ) as f: __lowerCamelCase = _extract_labels(UpperCamelCase__ , one_hot=UpperCamelCase__ ) if not 0 <= validation_size <= len(UpperCamelCase__ ): __lowerCamelCase = ( '''Validation size should be between 0 and ''' f"""{len(UpperCamelCase__ )}. Received: {validation_size}.""" ) raise ValueError(UpperCamelCase__ ) __lowerCamelCase = train_images[:validation_size] __lowerCamelCase = train_labels[:validation_size] __lowerCamelCase = train_images[validation_size:] __lowerCamelCase = train_labels[validation_size:] __lowerCamelCase = {'''dtype''': dtype, '''reshape''': reshape, '''seed''': seed} __lowerCamelCase = _DataSet(UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ) __lowerCamelCase = _DataSet(UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ) __lowerCamelCase = _DataSet(UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ) return _Datasets(train=UpperCamelCase__ , validation=UpperCamelCase__ , test=UpperCamelCase__ )
67
'''simple docstring''' import warnings from pathlib import Path from typing import List, Tuple, Union import fire from torch import nn from transformers import AutoModelForSeqaSeqLM, AutoTokenizer, PreTrainedModel from transformers.utils import logging __UpperCAmelCase =logging.get_logger(__name__) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> None: __lowerCamelCase = nn.ModuleList([src_layers[i] for i in layers_to_copy] ) assert len(UpperCamelCase__ ) == len(UpperCamelCase__ ), f"""{len(UpperCamelCase__ )} != {len(UpperCamelCase__ )}""" dest_layers.load_state_dict(layers_to_copy.state_dict() ) __UpperCAmelCase ={ # maps num layers in teacher -> num_layers in student -> which teacher layers to copy. # 12: bart, 16: pegasus, 6: marian/Helsinki-NLP 1_2: { 1: [0], # This says that if the teacher has 12 layers and the student has 1, copy layer 0 of the teacher 2: [0, 6], 3: [0, 6, 1_1], 4: [0, 4, 8, 1_1], 6: [0, 2, 4, 7, 9, 1_1], 9: [0, 1, 2, 4, 5, 7, 9, 1_0, 1_1], 1_2: list(range(1_2)), }, 1_6: { # maps num layers in student -> which teacher layers to copy 1: [0], 2: [0, 1_5], 3: [0, 8, 1_5], 4: [0, 5, 1_0, 1_5], 6: [0, 3, 6, 9, 1_2, 1_5], 8: [0, 2, 4, 6, 8, 1_0, 1_2, 1_5], 9: [0, 1, 3, 5, 7, 9, 1_1, 1_3, 1_5], 1_2: [0, 1, 2, 3, 4, 5, 6, 7, 9, 1_1, 1_3, 1_5], 1_6: list(range(1_6)), }, 6: {1: [0], 2: [0, 5], 3: [0, 2, 5], 4: [0, 1, 3, 5], 6: list(range(6))}, } __UpperCAmelCase ={ # maps num layers in student -> which teacher layers to copy. 6: {1: [5], 2: [3, 5], 3: [1, 4, 5], 4: [1, 2, 4, 5]}, 1_2: {1: [1_1], 2: [5, 1_1], 3: [3, 7, 1_1], 6: [1, 3, 5, 8, 1_0, 1_1]}, 1_6: {1: [1_5], 4: [4, 9, 1_2, 1_5], 8: [1, 3, 5, 7, 9, 1_1, 1_3, 1_5]}, } def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Union[str, Any]: try: __lowerCamelCase = LAYERS_TO_COPY[n_teacher][n_student] return val except KeyError: if n_student != n_teacher: warnings.warn( f"""no hardcoded layers to copy for teacher {n_teacher} -> student {n_student}, defaulting to first""" f""" {n_student}""" ) return list(range(UpperCamelCase__ ) ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> List[int]: if n_student > n_teacher: raise ValueError(f"""Cannot perform intermediate supervision for student {n_student} > teacher {n_teacher}""" ) elif n_teacher == n_student: return list(range(UpperCamelCase__ ) ) elif n_student == 1: return [n_teacher - 1] else: return LAYERS_TO_SUPERVISE[n_teacher][n_student] def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ = "student" , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__=False , UpperCamelCase__=None , UpperCamelCase__=None , **UpperCamelCase__ , ) -> Tuple[PreTrainedModel, List[int], List[int]]: __lowerCamelCase = '''encoder_layers and decoder_layers cannot be both None-- you would just have an identical teacher.''' assert (e is not None) or (d is not None), _msg if isinstance(UpperCamelCase__ , UpperCamelCase__ ): AutoTokenizer.from_pretrained(UpperCamelCase__ ).save_pretrained(UpperCamelCase__ ) # purely for convenience __lowerCamelCase = AutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__ ).eval() else: assert isinstance(UpperCamelCase__ , UpperCamelCase__ ), f"""teacher must be a model or string got type {type(UpperCamelCase__ )}""" __lowerCamelCase = teacher.config.to_diff_dict() try: __lowerCamelCase , __lowerCamelCase = teacher.config.encoder_layers, teacher.config.decoder_layers if e is None: __lowerCamelCase = teacher_e if d is None: __lowerCamelCase = teacher_d init_kwargs.update({'''encoder_layers''': e, '''decoder_layers''': d} ) except AttributeError: # T5 if hasattr(teacher.config , '''num_encoder_layers''' ): __lowerCamelCase , __lowerCamelCase = teacher.config.num_encoder_layers, teacher.config.num_decoder_layers else: __lowerCamelCase , __lowerCamelCase = teacher.config.num_layers, teacher.config.num_decoder_layers if e is None: __lowerCamelCase = teacher_e if d is None: __lowerCamelCase = teacher_d if hasattr(teacher.config , '''num_encoder_layers''' ): init_kwargs.update({'''num_encoder_layers''': e, '''num_decoder_layers''': d} ) else: init_kwargs.update({'''num_layers''': e, '''num_decoder_layers''': d} ) # Kwargs to instantiate student: teacher kwargs with updated layer numbers + **extra_config_kwargs init_kwargs.update(UpperCamelCase__ ) # Copy weights __lowerCamelCase = teacher.config_class(**UpperCamelCase__ ) __lowerCamelCase = AutoModelForSeqaSeqLM.from_config(UpperCamelCase__ ) # Start by copying the full teacher state dict this will copy the first N teacher layers to the student. __lowerCamelCase = student.load_state_dict(teacher.state_dict() , strict=UpperCamelCase__ ) assert info.missing_keys == [], info.missing_keys # every student key should have a teacher keys. if copy_first_teacher_layers: # Our copying is done. We just log and save __lowerCamelCase , __lowerCamelCase = list(range(UpperCamelCase__ ) ), list(range(UpperCamelCase__ ) ) logger.info( f"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to""" f""" {save_path}""" ) student.save_pretrained(UpperCamelCase__ ) return student, e_layers_to_copy, d_layers_to_copy # Decide which layers of the teacher to copy. Not exactly alternating -- we try to keep first and last layer. if e_layers_to_copy is None: __lowerCamelCase = pick_layers_to_copy(UpperCamelCase__ , UpperCamelCase__ ) if d_layers_to_copy is None: __lowerCamelCase = pick_layers_to_copy(UpperCamelCase__ , UpperCamelCase__ ) try: if hasattr( UpperCamelCase__ , '''prophetnet''' ): # For ProphetNet, student.model.encoder.layers is called student.prophetnet.encoder.layers copy_layers(teacher.prophetnet.encoder.layers , student.prophetnet.encoder.layers , UpperCamelCase__ ) copy_layers(teacher.prophetnet.decoder.layers , student.prophetnet.decoder.layers , UpperCamelCase__ ) else: copy_layers(teacher.model.encoder.layers , student.model.encoder.layers , UpperCamelCase__ ) copy_layers(teacher.model.decoder.layers , student.model.decoder.layers , UpperCamelCase__ ) except AttributeError: # For t5, student.model.encoder.layers is called student.encoder.block copy_layers(teacher.encoder.block , student.encoder.block , UpperCamelCase__ ) copy_layers(teacher.decoder.block , student.decoder.block , UpperCamelCase__ ) logger.info( f"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to {save_path}""" ) __lowerCamelCase = { '''teacher_type''': teacher.config.model_type, '''copied_encoder_layers''': e_layers_to_copy, '''copied_decoder_layers''': d_layers_to_copy, } student.save_pretrained(UpperCamelCase__ ) # Save information about copying for easier reproducibility return student, e_layers_to_copy, d_layers_to_copy if __name__ == "__main__": fire.Fire(create_student_by_copying_alternating_layers)
67
1
'''simple docstring''' from datetime import datetime import requests def __lowerCAmelCase ( UpperCamelCase__ ) -> bytes: __lowerCamelCase = '''https://downloadgram.net/wp-json/wppress/video-downloader/video?url=''' __lowerCamelCase = requests.get(base_url + url ).json()[0]['''urls'''][0]['''src'''] return requests.get(UpperCamelCase__ ).content if __name__ == "__main__": __UpperCAmelCase =input("Enter Video/IGTV url: ").strip() __UpperCAmelCase =f'{datetime.now():%Y-%m-%d_%H:%M:%S}.mp4' with open(file_name, "wb") as fp: fp.write(download_video(url)) print(f'Done. Video saved to disk as {file_name}.')
67
'''simple docstring''' import unittest from pathlib import Path from tempfile import TemporaryDirectory from transformers import AutoConfig, TFGPTaLMHeadModel, is_keras_nlp_available, is_tf_available from transformers.models.gpta.tokenization_gpta import GPTaTokenizer from transformers.testing_utils import require_keras_nlp, require_tf, slow if is_tf_available(): import tensorflow as tf if is_keras_nlp_available(): from transformers.models.gpta import TFGPTaTokenizer __UpperCAmelCase =["gpt2"] __UpperCAmelCase ="gpt2" if is_tf_available(): class a__ ( tf.Module ): def __init__( self : str , a : Union[str, Any] ): """simple docstring""" super().__init__() __lowerCamelCase = tokenizer __lowerCamelCase = AutoConfig.from_pretrained(a ) __lowerCamelCase = TFGPTaLMHeadModel.from_config(a ) @tf.function(input_signature=(tf.TensorSpec((None,) , tf.string , name='''text''' ),) ) def SCREAMING_SNAKE_CASE__ ( self : str , a : Tuple ): """simple docstring""" __lowerCamelCase = self.tokenizer(a ) __lowerCamelCase = tokenized['''input_ids'''].to_tensor() __lowerCamelCase = tf.cast(input_ids_dense > 0 , tf.intaa ) # input_mask = tf.reshape(input_mask, [-1, MAX_SEQ_LEN]) __lowerCamelCase = self.model(input_ids=a , attention_mask=a )['''logits'''] return outputs @require_tf @require_keras_nlp class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" super().setUp() __lowerCamelCase = [GPTaTokenizer.from_pretrained(a ) for checkpoint in (TOKENIZER_CHECKPOINTS)] __lowerCamelCase = [TFGPTaTokenizer.from_pretrained(a ) for checkpoint in TOKENIZER_CHECKPOINTS] assert len(self.tokenizers ) == len(self.tf_tokenizers ) __lowerCamelCase = [ '''This is a straightforward English test sentence.''', '''This one has some weird characters\rto\nsee\r\nif those\u00E9break things.''', '''Now we\'re going to add some Chinese: 一 二 三 一二三''', '''And some much more rare Chinese: 齉 堃 齉堃''', '''Je vais aussi écrire en français pour tester les accents''', '''Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ''', ] __lowerCamelCase = list(zip(self.test_sentences , self.test_sentences[::-1] ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" for tokenizer, tf_tokenizer in zip(self.tokenizers , self.tf_tokenizers ): for test_inputs in self.test_sentences: __lowerCamelCase = tokenizer([test_inputs] , return_tensors='''tf''' ) __lowerCamelCase = tf_tokenizer([test_inputs] ) for key in python_outputs.keys(): # convert them to numpy to avoid messing with ragged tensors __lowerCamelCase = python_outputs[key].numpy() __lowerCamelCase = tf_outputs[key].numpy() self.assertTrue(tf.reduce_all(python_outputs_values.shape == tf_outputs_values.shape ) ) self.assertTrue(tf.reduce_all(tf.cast(a , tf.intaa ) == tf_outputs_values ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: __lowerCamelCase = tf.function(a ) for test_inputs in self.test_sentences: __lowerCamelCase = tf.constant(a ) __lowerCamelCase = compiled_tokenizer(a ) __lowerCamelCase = tf_tokenizer(a ) for key in eager_outputs.keys(): self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key] ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: __lowerCamelCase = ModelToSave(tokenizer=a ) __lowerCamelCase = tf.convert_to_tensor([self.test_sentences[0]] ) __lowerCamelCase = model.serving(a ) # Build model with some sample inputs with TemporaryDirectory() as tempdir: __lowerCamelCase = Path(a ) / '''saved.model''' tf.saved_model.save(a , a , signatures={'''serving_default''': model.serving} ) __lowerCamelCase = tf.saved_model.load(a ) __lowerCamelCase = loaded_model.signatures['''serving_default'''](a )['''output_0'''] # We may see small differences because the loaded model is compiled, so we need an epsilon for the test self.assertTrue(tf.reduce_all(out == loaded_output ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: __lowerCamelCase = tf.convert_to_tensor([self.test_sentences[0]] ) __lowerCamelCase = tf_tokenizer(a ) # Build model with some sample inputs __lowerCamelCase = tf_tokenizer.get_config() __lowerCamelCase = TFGPTaTokenizer.from_config(a ) __lowerCamelCase = model_from_config(a ) for key in from_config_output.keys(): self.assertTrue(tf.reduce_all(from_config_output[key] == out[key] ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: # for the test to run __lowerCamelCase = 12_31_23 for max_length in [3, 5, 10_24]: __lowerCamelCase = tf.convert_to_tensor([self.test_sentences[0]] ) __lowerCamelCase = tf_tokenizer(a , max_length=a ) __lowerCamelCase = out['''input_ids'''].numpy().shape[1] assert out_length == max_length
67
1
'''simple docstring''' import json import os from typing import Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={"vocab_file": "vocab.json"} __UpperCAmelCase ={ "vocab_file": { "mgp-str": "https://huggingface.co/alibaba-damo/mgp-str-base/blob/main/vocab.json", } } __UpperCAmelCase ={"mgp-str": 2_7} class a__ ( UpperCAmelCase__ ): lowerCamelCase : List[Any] =VOCAB_FILES_NAMES lowerCamelCase : Optional[Any] =PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : Union[str, Any] =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self : int , a : str , a : Optional[int]="[GO]" , a : int="[GO]" , a : Union[str, Any]="[s]" , a : Union[str, Any]="[GO]" , **a : Optional[int] ): """simple docstring""" super().__init__( unk_token=a , bos_token=a , eos_token=a , pad_token=a , **a , ) with open(a , encoding='''utf-8''' ) as vocab_handle: __lowerCamelCase = json.load(a ) __lowerCamelCase = {v: k for k, v in self.vocab.items()} @property def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" return len(self.vocab ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" return dict(self.vocab , **self.added_tokens_encoder ) def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : str ): """simple docstring""" __lowerCamelCase = [] for s in text: char_tokens.extend(a ) return char_tokens def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : List[Any] ): """simple docstring""" return self.vocab.get(a , self.vocab.get(self.unk_token ) ) def SCREAMING_SNAKE_CASE__ ( self : int , a : Any ): """simple docstring""" return self.decoder.get(a ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : str , a : Optional[str] = None ): """simple docstring""" if not os.path.isdir(a ): logger.error('''Vocabulary path ({}) should be a directory'''.format(a ) ) return __lowerCamelCase = os.path.join( a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) with open(a , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.vocab , indent=2 , sort_keys=a , ensure_ascii=a ) + '''\n''' ) return (vocab_file,)
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Optional[int]: if b == 0: return 1 if (b % 2) == 0: return actual_power(UpperCamelCase__ , int(b / 2 ) ) * actual_power(UpperCamelCase__ , int(b / 2 ) ) else: return a * actual_power(UpperCamelCase__ , int(b / 2 ) ) * actual_power(UpperCamelCase__ , int(b / 2 ) ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> float: if b < 0: return 1 / actual_power(UpperCamelCase__ , UpperCamelCase__ ) return actual_power(UpperCamelCase__ , UpperCamelCase__ ) if __name__ == "__main__": print(power(-2, -3))
67
1
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ = 50 ) -> int: __lowerCamelCase = [1] * (length + 1) for row_length in range(length + 1 ): for tile_length in range(2 , 5 ): for tile_start in range(row_length - tile_length + 1 ): ways_number[row_length] += ways_number[ row_length - tile_start - tile_length ] return ways_number[length] if __name__ == "__main__": print(f'{solution() = }')
67
'''simple docstring''' import logging import os from .state import PartialState class a__ ( logging.LoggerAdapter ): @staticmethod def SCREAMING_SNAKE_CASE__ ( a : Optional[Any] ): """simple docstring""" __lowerCamelCase = PartialState() return not main_process_only or (main_process_only and state.is_main_process) def SCREAMING_SNAKE_CASE__ ( self : int , a : Optional[int] , a : str , *a : Optional[int] , **a : List[Any] ): """simple docstring""" if PartialState._shared_state == {}: raise RuntimeError( '''You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.''' ) __lowerCamelCase = kwargs.pop('''main_process_only''' , a ) __lowerCamelCase = kwargs.pop('''in_order''' , a ) if self.isEnabledFor(a ): if self._should_log(a ): __lowerCamelCase , __lowerCamelCase = self.process(a , a ) self.logger.log(a , a , *a , **a ) elif in_order: __lowerCamelCase = PartialState() for i in range(state.num_processes ): if i == state.process_index: __lowerCamelCase , __lowerCamelCase = self.process(a , a ) self.logger.log(a , a , *a , **a ) state.wait_for_everyone() def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ = None ) -> Optional[int]: if log_level is None: __lowerCamelCase = os.environ.get('''ACCELERATE_LOG_LEVEL''' , UpperCamelCase__ ) __lowerCamelCase = logging.getLogger(UpperCamelCase__ ) if log_level is not None: logger.setLevel(log_level.upper() ) logger.root.setLevel(log_level.upper() ) return MultiProcessAdapter(UpperCamelCase__ , {} )
67
1
'''simple docstring''' from __future__ import annotations class a__ : def __init__( self : Tuple , a : str , a : str ): """simple docstring""" __lowerCamelCase , __lowerCamelCase = text, pattern __lowerCamelCase , __lowerCamelCase = len(a ), len(a ) def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : str ): """simple docstring""" for i in range(self.patLen - 1 , -1 , -1 ): if char == self.pattern[i]: return i return -1 def SCREAMING_SNAKE_CASE__ ( self : Dict , a : int ): """simple docstring""" for i in range(self.patLen - 1 , -1 , -1 ): if self.pattern[i] != self.text[current_pos + i]: return current_pos + i return -1 def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = [] for i in range(self.textLen - self.patLen + 1 ): __lowerCamelCase = self.mismatch_in_text(a ) if mismatch_index == -1: positions.append(a ) else: __lowerCamelCase = self.match_in_pattern(self.text[mismatch_index] ) __lowerCamelCase = ( mismatch_index - match_index ) # shifting index lgtm [py/multiple-definition] return positions __UpperCAmelCase ="ABAABA" __UpperCAmelCase ="AB" __UpperCAmelCase =BoyerMooreSearch(text, pattern) __UpperCAmelCase =bms.bad_character_heuristic() if len(positions) == 0: print("No match found") else: print("Pattern found in following positions: ") print(positions)
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[Any]: __lowerCamelCase = [] __lowerCamelCase = set({'''(''', '''[''', '''{'''} ) __lowerCamelCase = set({''')''', ''']''', '''}'''} ) __lowerCamelCase = {'''{''': '''}''', '''[''': ''']''', '''(''': ''')'''} for i in range(len(UpperCamelCase__ ) ): if s[i] in open_brackets: stack.append(s[i] ) elif s[i] in closed_brackets and ( len(UpperCamelCase__ ) == 0 or (len(UpperCamelCase__ ) > 0 and open_to_closed[stack.pop()] != s[i]) ): return False return len(UpperCamelCase__ ) == 0 def __lowerCAmelCase ( ) -> str: __lowerCamelCase = input('''Enter sequence of brackets: ''' ) if is_balanced(UpperCamelCase__ ): print(UpperCamelCase__ , '''is balanced''' ) else: print(UpperCamelCase__ , '''is not balanced''' ) if __name__ == "__main__": main()
67
1
'''simple docstring''' import inspect from typing import Optional, Union import numpy as np import PIL import torch from torch.nn import functional as F from torchvision import transforms from transformers import CLIPFeatureExtractor, CLIPModel, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, DPMSolverMultistepScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.utils import ( PIL_INTERPOLATION, randn_tensor, ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> int: if isinstance(UpperCamelCase__ , torch.Tensor ): return image elif isinstance(UpperCamelCase__ , PIL.Image.Image ): __lowerCamelCase = [image] if isinstance(image[0] , PIL.Image.Image ): __lowerCamelCase = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['''lanczos'''] ) )[None, :] for i in image] __lowerCamelCase = np.concatenate(UpperCamelCase__ , axis=0 ) __lowerCamelCase = np.array(UpperCamelCase__ ).astype(np.floataa ) / 2_5_5.0 __lowerCamelCase = image.transpose(0 , 3 , 1 , 2 ) __lowerCamelCase = 2.0 * image - 1.0 __lowerCamelCase = torch.from_numpy(UpperCamelCase__ ) elif isinstance(image[0] , torch.Tensor ): __lowerCamelCase = torch.cat(UpperCamelCase__ , dim=0 ) return image def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=0.9_9_9_5 ) -> Dict: if not isinstance(UpperCamelCase__ , np.ndarray ): __lowerCamelCase = True __lowerCamelCase = va.device __lowerCamelCase = va.cpu().numpy() __lowerCamelCase = va.cpu().numpy() __lowerCamelCase = np.sum(va * va / (np.linalg.norm(UpperCamelCase__ ) * np.linalg.norm(UpperCamelCase__ )) ) if np.abs(UpperCamelCase__ ) > DOT_THRESHOLD: __lowerCamelCase = (1 - t) * va + t * va else: __lowerCamelCase = np.arccos(UpperCamelCase__ ) __lowerCamelCase = np.sin(UpperCamelCase__ ) __lowerCamelCase = theta_a * t __lowerCamelCase = np.sin(UpperCamelCase__ ) __lowerCamelCase = np.sin(theta_a - theta_t ) / sin_theta_a __lowerCamelCase = sin_theta_t / sin_theta_a __lowerCamelCase = sa * va + sa * va if inputs_are_torch: __lowerCamelCase = torch.from_numpy(UpperCamelCase__ ).to(UpperCamelCase__ ) return va def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> List[Any]: __lowerCamelCase = F.normalize(UpperCamelCase__ , dim=-1 ) __lowerCamelCase = F.normalize(UpperCamelCase__ , dim=-1 ) return (x - y).norm(dim=-1 ).div(2 ).arcsin().pow(2 ).mul(2 ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> str: for param in model.parameters(): __lowerCamelCase = value class a__ ( UpperCAmelCase__ ): def __init__( self : int , a : AutoencoderKL , a : CLIPTextModel , a : CLIPModel , a : CLIPTokenizer , a : UNetaDConditionModel , a : Union[PNDMScheduler, LMSDiscreteScheduler, DDIMScheduler, DPMSolverMultistepScheduler] , a : CLIPFeatureExtractor , a : Any=None , a : str=None , a : int=None , ): """simple docstring""" super().__init__() self.register_modules( vae=a , text_encoder=a , clip_model=a , tokenizer=a , unet=a , scheduler=a , feature_extractor=a , coca_model=a , coca_tokenizer=a , coca_transform=a , ) __lowerCamelCase = ( feature_extractor.size if isinstance(feature_extractor.size , a ) else feature_extractor.size['''shortest_edge'''] ) __lowerCamelCase = transforms.Normalize(mean=feature_extractor.image_mean , std=feature_extractor.image_std ) set_requires_grad(self.text_encoder , a ) set_requires_grad(self.clip_model , a ) def SCREAMING_SNAKE_CASE__ ( self : str , a : Optional[Union[str, int]] = "auto" ): """simple docstring""" if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory __lowerCamelCase = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(a ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" self.enable_attention_slicing(a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" set_requires_grad(self.vae , a ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" set_requires_grad(self.vae , a ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" set_requires_grad(self.unet , a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" set_requires_grad(self.unet , a ) def SCREAMING_SNAKE_CASE__ ( self : int , a : Dict , a : List[str] , a : Tuple ): """simple docstring""" __lowerCamelCase = min(int(num_inference_steps * strength ) , a ) __lowerCamelCase = max(num_inference_steps - init_timestep , 0 ) __lowerCamelCase = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def SCREAMING_SNAKE_CASE__ ( self : Any , a : int , a : Dict , a : List[str] , a : int , a : str , a : int=None ): """simple docstring""" if not isinstance(a , torch.Tensor ): raise ValueError(f"""`image` has to be of type `torch.Tensor` but is {type(a )}""" ) __lowerCamelCase = image.to(device=a , dtype=a ) if isinstance(a , a ): __lowerCamelCase = [ self.vae.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(a ) ] __lowerCamelCase = torch.cat(a , dim=0 ) else: __lowerCamelCase = self.vae.encode(a ).latent_dist.sample(a ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor __lowerCamelCase = 0.1_82_15 * init_latents __lowerCamelCase = init_latents.repeat_interleave(a , dim=0 ) __lowerCamelCase = randn_tensor(init_latents.shape , generator=a , device=a , dtype=a ) # get latents __lowerCamelCase = self.scheduler.add_noise(a , a , a ) __lowerCamelCase = init_latents return latents def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : List[str] ): """simple docstring""" __lowerCamelCase = self.coca_transform(a ).unsqueeze(0 ) with torch.no_grad(), torch.cuda.amp.autocast(): __lowerCamelCase = self.coca_model.generate(transformed_image.to(device=self.device , dtype=self.coca_model.dtype ) ) __lowerCamelCase = self.coca_tokenizer.decode(generated[0].cpu().numpy() ) return generated.split('''<end_of_text>''' )[0].replace('''<start_of_text>''' , '''''' ).rstrip(''' .,''' ) def SCREAMING_SNAKE_CASE__ ( self : str , a : Union[str, Any] , a : Optional[int] ): """simple docstring""" __lowerCamelCase = self.feature_extractor.preprocess(a ) __lowerCamelCase = torch.from_numpy(clip_image_input['''pixel_values'''][0] ).unsqueeze(0 ).to(self.device ).half() __lowerCamelCase = self.clip_model.get_image_features(a ) __lowerCamelCase = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=a ) __lowerCamelCase = image_embeddings_clip.repeat_interleave(a , dim=0 ) return image_embeddings_clip @torch.enable_grad() def SCREAMING_SNAKE_CASE__ ( self : Dict , a : Optional[int] , a : List[Any] , a : Union[str, Any] , a : str , a : Tuple , a : Optional[Any] , a : List[str] , ): """simple docstring""" __lowerCamelCase = latents.detach().requires_grad_() __lowerCamelCase = self.scheduler.scale_model_input(a , a ) # predict the noise residual __lowerCamelCase = self.unet(a , a , encoder_hidden_states=a ).sample if isinstance(self.scheduler , (PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler) ): __lowerCamelCase = self.scheduler.alphas_cumprod[timestep] __lowerCamelCase = 1 - alpha_prod_t # compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf __lowerCamelCase = (latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5 __lowerCamelCase = torch.sqrt(a ) __lowerCamelCase = pred_original_sample * (fac) + latents * (1 - fac) elif isinstance(self.scheduler , a ): __lowerCamelCase = self.scheduler.sigmas[index] __lowerCamelCase = latents - sigma * noise_pred else: raise ValueError(f"""scheduler type {type(self.scheduler )} not supported""" ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor __lowerCamelCase = 1 / 0.1_82_15 * sample __lowerCamelCase = self.vae.decode(a ).sample __lowerCamelCase = (image / 2 + 0.5).clamp(0 , 1 ) __lowerCamelCase = transforms.Resize(self.feature_extractor_size )(a ) __lowerCamelCase = self.normalize(a ).to(latents.dtype ) __lowerCamelCase = self.clip_model.get_image_features(a ) __lowerCamelCase = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=a ) __lowerCamelCase = spherical_dist_loss(a , a ).mean() * clip_guidance_scale __lowerCamelCase = -torch.autograd.grad(a , a )[0] if isinstance(self.scheduler , a ): __lowerCamelCase = latents.detach() + grads * (sigma**2) __lowerCamelCase = noise_pred_original else: __lowerCamelCase = noise_pred_original - torch.sqrt(a ) * grads return noise_pred, latents @torch.no_grad() def __call__( self : Union[str, Any] , a : Union[torch.FloatTensor, PIL.Image.Image] , a : Union[torch.FloatTensor, PIL.Image.Image] , a : Optional[str] = None , a : Optional[str] = None , a : Optional[int] = 5_12 , a : Optional[int] = 5_12 , a : float = 0.6 , a : Optional[int] = 50 , a : Optional[float] = 7.5 , a : Optional[int] = 1 , a : float = 0.0 , a : Optional[float] = 1_00 , a : Optional[torch.Generator] = None , a : Optional[str] = "pil" , a : bool = True , a : float = 0.8 , a : float = 0.1 , a : float = 0.1 , ): """simple docstring""" if isinstance(a , a ) and len(a ) != batch_size: raise ValueError(f"""You have passed {batch_size} batch_size, but only {len(a )} generators.""" ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f"""`height` and `width` have to be divisible by 8 but are {height} and {width}.""" ) if isinstance(a , torch.Generator ) and batch_size > 1: __lowerCamelCase = [generator] + [None] * (batch_size - 1) __lowerCamelCase = [ ('''model''', self.coca_model is None), ('''tokenizer''', self.coca_tokenizer is None), ('''transform''', self.coca_transform is None), ] __lowerCamelCase = [x[0] for x in coca_is_none if x[1]] __lowerCamelCase = ''', '''.join(a ) # generate prompts with coca model if prompt is None if content_prompt is None: if len(a ): raise ValueError( f"""Content prompt is None and CoCa [{coca_is_none_str}] is None.""" f"""Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.""" ) __lowerCamelCase = self.get_image_description(a ) if style_prompt is None: if len(a ): raise ValueError( f"""Style prompt is None and CoCa [{coca_is_none_str}] is None.""" f""" Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.""" ) __lowerCamelCase = self.get_image_description(a ) # get prompt text embeddings for content and style __lowerCamelCase = self.tokenizer( a , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=a , return_tensors='''pt''' , ) __lowerCamelCase = self.text_encoder(content_text_input.input_ids.to(self.device ) )[0] __lowerCamelCase = self.tokenizer( a , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=a , return_tensors='''pt''' , ) __lowerCamelCase = self.text_encoder(style_text_input.input_ids.to(self.device ) )[0] __lowerCamelCase = slerp(a , a , a ) # duplicate text embeddings for each generation per prompt __lowerCamelCase = text_embeddings.repeat_interleave(a , dim=0 ) # set timesteps __lowerCamelCase = '''offset''' in set(inspect.signature(self.scheduler.set_timesteps ).parameters.keys() ) __lowerCamelCase = {} if accepts_offset: __lowerCamelCase = 1 self.scheduler.set_timesteps(a , **a ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand self.scheduler.timesteps.to(self.device ) __lowerCamelCase , __lowerCamelCase = self.get_timesteps(a , a , self.device ) __lowerCamelCase = timesteps[:1].repeat(a ) # Preprocess image __lowerCamelCase = preprocess(a , a , a ) __lowerCamelCase = self.prepare_latents( a , a , a , text_embeddings.dtype , self.device , a ) __lowerCamelCase = preprocess(a , a , a ) __lowerCamelCase = self.prepare_latents( a , a , a , text_embeddings.dtype , self.device , a ) __lowerCamelCase = slerp(a , a , a ) if clip_guidance_scale > 0: __lowerCamelCase = self.get_clip_image_embeddings(a , a ) __lowerCamelCase = self.get_clip_image_embeddings(a , a ) __lowerCamelCase = slerp( a , a , a ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. __lowerCamelCase = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: __lowerCamelCase = content_text_input.input_ids.shape[-1] __lowerCamelCase = self.tokenizer([''''''] , padding='''max_length''' , max_length=a , return_tensors='''pt''' ) __lowerCamelCase = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt __lowerCamelCase = uncond_embeddings.repeat_interleave(a , dim=0 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes __lowerCamelCase = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. __lowerCamelCase = (batch_size, self.unet.config.in_channels, height // 8, width // 8) __lowerCamelCase = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not work reproducibly on mps __lowerCamelCase = torch.randn(a , generator=a , device='''cpu''' , dtype=a ).to( self.device ) else: __lowerCamelCase = torch.randn(a , generator=a , device=self.device , dtype=a ) else: if latents.shape != latents_shape: raise ValueError(f"""Unexpected latents shape, got {latents.shape}, expected {latents_shape}""" ) __lowerCamelCase = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler __lowerCamelCase = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] __lowerCamelCase = '''eta''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) __lowerCamelCase = {} if accepts_eta: __lowerCamelCase = eta # check if the scheduler accepts generator __lowerCamelCase = '''generator''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) if accepts_generator: __lowerCamelCase = generator with self.progress_bar(total=a ): for i, t in enumerate(a ): # expand the latents if we are doing classifier free guidance __lowerCamelCase = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents __lowerCamelCase = self.scheduler.scale_model_input(a , a ) # predict the noise residual __lowerCamelCase = self.unet(a , a , encoder_hidden_states=a ).sample # perform classifier free guidance if do_classifier_free_guidance: __lowerCamelCase , __lowerCamelCase = noise_pred.chunk(2 ) __lowerCamelCase = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # perform clip guidance if clip_guidance_scale > 0: __lowerCamelCase = ( text_embeddings.chunk(2 )[1] if do_classifier_free_guidance else text_embeddings ) __lowerCamelCase , __lowerCamelCase = self.cond_fn( a , a , a , a , a , a , a , ) # compute the previous noisy sample x_t -> x_t-1 __lowerCamelCase = self.scheduler.step(a , a , a , **a ).prev_sample # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor __lowerCamelCase = 1 / 0.1_82_15 * latents __lowerCamelCase = self.vae.decode(a ).sample __lowerCamelCase = (image / 2 + 0.5).clamp(0 , 1 ) __lowerCamelCase = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": __lowerCamelCase = self.numpy_to_pil(a ) if not return_dict: return (image, None) return StableDiffusionPipelineOutput(images=a , nsfw_content_detected=a )
67
'''simple docstring''' import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class a__ ( UpperCAmelCase__ ): lowerCamelCase : Dict ="M-CLIP" def __init__( self : Tuple , a : Optional[int]=10_24 , a : Tuple=7_68 , **a : List[str] ): """simple docstring""" __lowerCamelCase = transformerDimSize __lowerCamelCase = imageDimSize super().__init__(**a ) class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[Any] =MCLIPConfig def __init__( self : str , a : List[Any] , *a : Dict , **a : str ): """simple docstring""" super().__init__(a , *a , **a ) __lowerCamelCase = XLMRobertaModel(a ) __lowerCamelCase = torch.nn.Linear( in_features=config.transformerDimensions , out_features=config.numDims ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : int , a : List[Any] ): """simple docstring""" __lowerCamelCase = self.transformer(input_ids=a , attention_mask=a )[0] __lowerCamelCase = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(a ), embs
67
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={ "facebook/convnextv2-tiny-1k-224": "https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json", } class a__ ( UpperCAmelCase__ , UpperCAmelCase__ ): lowerCamelCase : Optional[int] ="convnextv2" def __init__( self : int , a : Dict=3 , a : List[str]=4 , a : str=4 , a : Dict=None , a : List[str]=None , a : Tuple="gelu" , a : Tuple=0.02 , a : Dict=1e-1_2 , a : Any=0.0 , a : List[str]=2_24 , a : Optional[Any]=None , a : int=None , **a : Any , ): """simple docstring""" super().__init__(**a ) __lowerCamelCase = num_channels __lowerCamelCase = patch_size __lowerCamelCase = num_stages __lowerCamelCase = [96, 1_92, 3_84, 7_68] if hidden_sizes is None else hidden_sizes __lowerCamelCase = [3, 3, 9, 3] if depths is None else depths __lowerCamelCase = hidden_act __lowerCamelCase = initializer_range __lowerCamelCase = layer_norm_eps __lowerCamelCase = drop_path_rate __lowerCamelCase = image_size __lowerCamelCase = ['''stem'''] + [f"""stage{idx}""" for idx in range(1 , len(self.depths ) + 1 )] __lowerCamelCase , __lowerCamelCase = get_aligned_output_features_output_indices( out_features=a , out_indices=a , stage_names=self.stage_names )
67
'''simple docstring''' from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
67
1
'''simple docstring''' import qiskit def __lowerCAmelCase ( UpperCamelCase__ = 2 ) -> qiskit.result.counts.Counts: __lowerCamelCase = qubits # Using Aer's simulator __lowerCamelCase = qiskit.Aer.get_backend('''aer_simulator''' ) # Creating a Quantum Circuit acting on the q register __lowerCamelCase = qiskit.QuantumCircuit(UpperCamelCase__ , UpperCamelCase__ ) # Adding a H gate on qubit 0 (now q0 in superposition) circuit.h(0 ) for i in range(1 , UpperCamelCase__ ): # Adding CX (CNOT) gate circuit.cx(i - 1 , UpperCamelCase__ ) # Mapping the quantum measurement to the classical bits circuit.measure(list(range(UpperCamelCase__ ) ) , list(range(UpperCamelCase__ ) ) ) # Now measuring any one qubit would affect other qubits to collapse # their super position and have same state as the measured one. # Executing the circuit on the simulator __lowerCamelCase = qiskit.execute(UpperCamelCase__ , UpperCamelCase__ , shots=10_00 ) return job.result().get_counts(UpperCamelCase__ ) if __name__ == "__main__": print(f'Total count for various states are: {quantum_entanglement(3)}')
67
'''simple docstring''' from __future__ import annotations from decimal import Decimal from numpy import array def __lowerCAmelCase ( UpperCamelCase__ ) -> list[list[float]]: __lowerCamelCase = Decimal # Check if the provided matrix has 2 rows and 2 columns # since this implementation only works for 2x2 matrices if len(UpperCamelCase__ ) == 2 and len(matrix[0] ) == 2 and len(matrix[1] ) == 2: # Calculate the determinant of the matrix __lowerCamelCase = float( d(matrix[0][0] ) * d(matrix[1][1] ) - d(matrix[1][0] ) * d(matrix[0][1] ) ) if determinant == 0: raise ValueError('''This matrix has no inverse.''' ) # Creates a copy of the matrix with swapped positions of the elements __lowerCamelCase = [[0.0, 0.0], [0.0, 0.0]] __lowerCamelCase , __lowerCamelCase = matrix[1][1], matrix[0][0] __lowerCamelCase , __lowerCamelCase = -matrix[1][0], -matrix[0][1] # Calculate the inverse of the matrix return [ [(float(d(UpperCamelCase__ ) ) / determinant) or 0.0 for n in row] for row in swapped_matrix ] elif ( len(UpperCamelCase__ ) == 3 and len(matrix[0] ) == 3 and len(matrix[1] ) == 3 and len(matrix[2] ) == 3 ): # Calculate the determinant of the matrix using Sarrus rule __lowerCamelCase = float( ( (d(matrix[0][0] ) * d(matrix[1][1] ) * d(matrix[2][2] )) + (d(matrix[0][1] ) * d(matrix[1][2] ) * d(matrix[2][0] )) + (d(matrix[0][2] ) * d(matrix[1][0] ) * d(matrix[2][1] )) ) - ( (d(matrix[0][2] ) * d(matrix[1][1] ) * d(matrix[2][0] )) + (d(matrix[0][1] ) * d(matrix[1][0] ) * d(matrix[2][2] )) + (d(matrix[0][0] ) * d(matrix[1][2] ) * d(matrix[2][1] )) ) ) if determinant == 0: raise ValueError('''This matrix has no inverse.''' ) # Creating cofactor matrix __lowerCamelCase = [ [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], ] __lowerCamelCase = (d(matrix[1][1] ) * d(matrix[2][2] )) - ( d(matrix[1][2] ) * d(matrix[2][1] ) ) __lowerCamelCase = -( (d(matrix[1][0] ) * d(matrix[2][2] )) - (d(matrix[1][2] ) * d(matrix[2][0] )) ) __lowerCamelCase = (d(matrix[1][0] ) * d(matrix[2][1] )) - ( d(matrix[1][1] ) * d(matrix[2][0] ) ) __lowerCamelCase = -( (d(matrix[0][1] ) * d(matrix[2][2] )) - (d(matrix[0][2] ) * d(matrix[2][1] )) ) __lowerCamelCase = (d(matrix[0][0] ) * d(matrix[2][2] )) - ( d(matrix[0][2] ) * d(matrix[2][0] ) ) __lowerCamelCase = -( (d(matrix[0][0] ) * d(matrix[2][1] )) - (d(matrix[0][1] ) * d(matrix[2][0] )) ) __lowerCamelCase = (d(matrix[0][1] ) * d(matrix[1][2] )) - ( d(matrix[0][2] ) * d(matrix[1][1] ) ) __lowerCamelCase = -( (d(matrix[0][0] ) * d(matrix[1][2] )) - (d(matrix[0][2] ) * d(matrix[1][0] )) ) __lowerCamelCase = (d(matrix[0][0] ) * d(matrix[1][1] )) - ( d(matrix[0][1] ) * d(matrix[1][0] ) ) # Transpose the cofactor matrix (Adjoint matrix) __lowerCamelCase = array(UpperCamelCase__ ) for i in range(3 ): for j in range(3 ): __lowerCamelCase = cofactor_matrix[j][i] # Inverse of the matrix using the formula (1/determinant) * adjoint matrix __lowerCamelCase = array(UpperCamelCase__ ) for i in range(3 ): for j in range(3 ): inverse_matrix[i][j] /= d(UpperCamelCase__ ) # Calculate the inverse of the matrix return [[float(d(UpperCamelCase__ ) ) or 0.0 for n in row] for row in inverse_matrix] raise ValueError('''Please provide a matrix of size 2x2 or 3x3.''' )
67
1
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ ) -> int: __lowerCamelCase = hex_num.strip() if not hex_num: raise ValueError('''No value was passed to the function''' ) __lowerCamelCase = hex_num[0] == '''-''' if is_negative: __lowerCamelCase = hex_num[1:] try: __lowerCamelCase = int(UpperCamelCase__ , 16 ) except ValueError: raise ValueError('''Invalid value was passed to the function''' ) __lowerCamelCase = '''''' while int_num > 0: __lowerCamelCase = str(int_num % 2 ) + bin_str int_num >>= 1 return int(('''-''' + bin_str) if is_negative else bin_str ) if __name__ == "__main__": import doctest doctest.testmod()
67
'''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 __UpperCAmelCase =logging.get_logger(__name__) def __lowerCAmelCase ( UpperCamelCase__=None , UpperCamelCase__=None ) -> int: return field(default_factory=lambda: default , metadata=UpperCamelCase__ ) @dataclass class a__ : lowerCamelCase : List[str] =list_field( default=[] , metadata={ "help": ( "Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version" " of all available models" ) } , ) lowerCamelCase : List[int] =list_field( default=[8] , metadata={"help": "List of batch sizes for which memory and time performance will be evaluated"} ) lowerCamelCase : List[int] =list_field( default=[8, 3_2, 1_2_8, 5_1_2] , metadata={"help": "List of sequence lengths for which memory and time performance will be evaluated"} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to benchmark inference of model. Inference can be disabled via --no-inference."} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to run on available cuda devices. Cuda can be disabled via --no-cuda."} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to run on available tpu devices. TPU can be disabled via --no-tpu."} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Use FP16 to accelerate inference."} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Benchmark training of model"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Verbose memory tracing"} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to perform speed measurements. Speed measurements can be disabled via --no-speed."} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": "Whether to perform memory measurements. Memory measurements can be disabled via --no-memory" } , ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Trace memory line by line"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Save result to a CSV file"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Save all print statements in a log file"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Whether to print environment information"} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": ( "Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use" " multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled" " for debugging / testing and on TPU." ) } , ) lowerCamelCase : str =field( default=F'''inference_time_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving time results to csv."} , ) lowerCamelCase : str =field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving memory results to csv."} , ) lowerCamelCase : str =field( default=F'''train_time_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving time results to csv for training."} , ) lowerCamelCase : str =field( default=F'''train_memory_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving memory results to csv for training."} , ) lowerCamelCase : str =field( default=F'''env_info_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving environment information."} , ) lowerCamelCase : str =field( default=F'''log_{round(time() )}.csv''' , metadata={"help": "Log filename used if print statements are saved in log."} , ) lowerCamelCase : int =field(default=3 , metadata={"help": "Times an experiment will be run."} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": ( "Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain" " model weights." ) } , ) def SCREAMING_SNAKE_CASE__ ( self : 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.''' , a , ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """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 SCREAMING_SNAKE_CASE__ ( 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
67
1
'''simple docstring''' import math from collections.abc import Callable def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> float: __lowerCamelCase = xa __lowerCamelCase = xa while True: if x_n == x_na or function(UpperCamelCase__ ) == function(UpperCamelCase__ ): raise ZeroDivisionError('''float division by zero, could not find root''' ) __lowerCamelCase = x_na - ( function(UpperCamelCase__ ) / ((function(UpperCamelCase__ ) - function(UpperCamelCase__ )) / (x_na - x_n)) ) if abs(x_na - x_na ) < 10**-5: return x_na __lowerCamelCase = x_na __lowerCamelCase = x_na def __lowerCAmelCase ( UpperCamelCase__ ) -> float: return math.pow(UpperCamelCase__ , 3 ) - (2 * x) - 5 if __name__ == "__main__": print(intersection(f, 3, 3.5))
67
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_barthez import BarthezTokenizer else: __UpperCAmelCase =None __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase ={ "vocab_file": { "moussaKam/mbarthez": "https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model", "moussaKam/barthez": "https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model", "moussaKam/barthez-orangesum-title": ( "https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "moussaKam/mbarthez": "https://huggingface.co/moussaKam/mbarthez/resolve/main/tokenizer.json", "moussaKam/barthez": "https://huggingface.co/moussaKam/barthez/resolve/main/tokenizer.json", "moussaKam/barthez-orangesum-title": ( "https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/tokenizer.json" ), }, } __UpperCAmelCase ={ "moussaKam/mbarthez": 1_0_2_4, "moussaKam/barthez": 1_0_2_4, "moussaKam/barthez-orangesum-title": 1_0_2_4, } __UpperCAmelCase ="▁" class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] =VOCAB_FILES_NAMES lowerCamelCase : Optional[Any] =PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : int =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[Any] =["input_ids", "attention_mask"] lowerCamelCase : Union[str, Any] =BarthezTokenizer def __init__( self : Optional[Any] , a : Dict=None , a : str=None , a : List[Any]="<s>" , a : Optional[int]="</s>" , a : List[str]="</s>" , a : Tuple="<s>" , a : str="<unk>" , a : Any="<pad>" , a : Union[str, Any]="<mask>" , **a : Union[str, Any] , ): """simple docstring""" __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else mask_token super().__init__( a , tokenizer_file=a , bos_token=a , eos_token=a , unk_token=a , sep_token=a , cls_token=a , pad_token=a , mask_token=a , **a , ) __lowerCamelCase = vocab_file __lowerCamelCase = False if not self.vocab_file else True def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __lowerCamelCase = [self.cls_token_id] __lowerCamelCase = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" __lowerCamelCase = [self.sep_token_id] __lowerCamelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : str , a : Optional[str] = None ): """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return __lowerCamelCase = os.path.join( a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(a ): copyfile(self.vocab_file , a ) return (out_vocab_file,)
67
1
'''simple docstring''' import warnings from transformers import AutoTokenizer from transformers.utils import is_torch_available from transformers.utils.generic import ExplicitEnum from ...processing_utils import ProcessorMixin if is_torch_available(): import torch class a__ ( UpperCAmelCase__ ): lowerCamelCase : Tuple ="char" lowerCamelCase : List[str] ="bpe" lowerCamelCase : Tuple ="wp" __UpperCAmelCase =(DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE) class a__ ( UpperCAmelCase__ ): lowerCamelCase : List[str] =["image_processor", "char_tokenizer"] lowerCamelCase : List[Any] ="ViTImageProcessor" lowerCamelCase : Tuple ="MgpstrTokenizer" def __init__( self : int , a : str=None , a : int=None , **a : List[Any] ): """simple docstring""" __lowerCamelCase = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , a , ) __lowerCamelCase = kwargs.pop('''feature_extractor''' ) __lowerCamelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) __lowerCamelCase = tokenizer __lowerCamelCase = AutoTokenizer.from_pretrained('''gpt2''' ) __lowerCamelCase = AutoTokenizer.from_pretrained('''bert-base-uncased''' ) super().__init__(a , a ) def __call__( self : Optional[Any] , a : Tuple=None , a : Dict=None , a : List[Any]=None , **a : Optional[int] ): """simple docstring""" if images is None and text is None: raise ValueError('''You need to specify either an `images` or `text` input to process.''' ) if images is not None: __lowerCamelCase = self.image_processor(a , return_tensors=a , **a ) if text is not None: __lowerCamelCase = self.char_tokenizer(a , return_tensors=a , **a ) if text is None: return inputs elif images is None: return encodings else: __lowerCamelCase = encodings['''input_ids'''] return inputs def SCREAMING_SNAKE_CASE__ ( self : Dict , a : Optional[Any] ): """simple docstring""" __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = sequences __lowerCamelCase = char_preds.size(0 ) __lowerCamelCase , __lowerCamelCase = self._decode_helper(a , '''char''' ) __lowerCamelCase , __lowerCamelCase = self._decode_helper(a , '''bpe''' ) __lowerCamelCase , __lowerCamelCase = self._decode_helper(a , '''wp''' ) __lowerCamelCase = [] __lowerCamelCase = [] for i in range(a ): __lowerCamelCase = [char_scores[i], bpe_scores[i], wp_scores[i]] __lowerCamelCase = [char_strs[i], bpe_strs[i], wp_strs[i]] __lowerCamelCase = scores.index(max(a ) ) final_strs.append(strs[max_score_index] ) final_scores.append(scores[max_score_index] ) __lowerCamelCase = {} __lowerCamelCase = final_strs __lowerCamelCase = final_scores __lowerCamelCase = char_strs __lowerCamelCase = bpe_strs __lowerCamelCase = wp_strs return out def SCREAMING_SNAKE_CASE__ ( self : Dict , a : Tuple , a : List[str] ): """simple docstring""" if format == DecodeType.CHARACTER: __lowerCamelCase = self.char_decode __lowerCamelCase = 1 __lowerCamelCase = '''[s]''' elif format == DecodeType.BPE: __lowerCamelCase = self.bpe_decode __lowerCamelCase = 2 __lowerCamelCase = '''#''' elif format == DecodeType.WORDPIECE: __lowerCamelCase = self.wp_decode __lowerCamelCase = 1_02 __lowerCamelCase = '''[SEP]''' else: raise ValueError(f"""Format {format} is not supported.""" ) __lowerCamelCase , __lowerCamelCase = [], [] __lowerCamelCase = pred_logits.size(0 ) __lowerCamelCase = pred_logits.size(1 ) __lowerCamelCase , __lowerCamelCase = pred_logits.topk(1 , dim=-1 , largest=a , sorted=a ) __lowerCamelCase = preds_index.view(-1 , a )[:, 1:] __lowerCamelCase = decoder(a ) __lowerCamelCase , __lowerCamelCase = torch.nn.functional.softmax(a , dim=2 ).max(dim=2 ) __lowerCamelCase = preds_max_prob[:, 1:] for index in range(a ): __lowerCamelCase = preds_str[index].find(a ) __lowerCamelCase = preds_str[index][:pred_eos] __lowerCamelCase = preds_index[index].cpu().tolist() __lowerCamelCase = pred_index.index(a ) if eos_token in pred_index else -1 __lowerCamelCase = preds_max_prob[index][: pred_eos_index + 1] __lowerCamelCase = pred_max_prob.cumprod(dim=0 )[-1] if pred_max_prob.nelement() != 0 else 0.0 dec_strs.append(a ) conf_scores.append(a ) return dec_strs, conf_scores def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : Tuple ): """simple docstring""" __lowerCamelCase = [seq.replace(''' ''' , '''''' ) for seq in self.char_tokenizer.batch_decode(a )] return decode_strs def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : Union[str, Any] ): """simple docstring""" return self.bpe_tokenizer.batch_decode(a ) def SCREAMING_SNAKE_CASE__ ( self : Dict , a : Tuple ): """simple docstring""" __lowerCamelCase = [seq.replace(''' ''' , '''''' ) for seq in self.wp_tokenizer.batch_decode(a )] return decode_strs
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ = 1_00_00_00 ) -> int: __lowerCamelCase = set(range(3 , UpperCamelCase__ , 2 ) ) primes.add(2 ) for p in range(3 , UpperCamelCase__ , 2 ): if p not in primes: continue primes.difference_update(set(range(p * p , UpperCamelCase__ , UpperCamelCase__ ) ) ) __lowerCamelCase = [float(UpperCamelCase__ ) for n in range(limit + 1 )] for p in primes: for n in range(UpperCamelCase__ , limit + 1 , UpperCamelCase__ ): phi[n] *= 1 - 1 / p return int(sum(phi[2:] ) ) if __name__ == "__main__": print(f'{solution() = }')
67
1
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> int: def update_area_of_max_square(UpperCamelCase__ , UpperCamelCase__ ) -> int: # BASE CASE if row >= rows or col >= cols: return 0 __lowerCamelCase = update_area_of_max_square(UpperCamelCase__ , col + 1 ) __lowerCamelCase = update_area_of_max_square(row + 1 , col + 1 ) __lowerCamelCase = update_area_of_max_square(row + 1 , UpperCamelCase__ ) if mat[row][col]: __lowerCamelCase = 1 + min([right, diagonal, down] ) __lowerCamelCase = max(largest_square_area[0] , UpperCamelCase__ ) return sub_problem_sol else: return 0 __lowerCamelCase = [0] update_area_of_max_square(0 , 0 ) return largest_square_area[0] def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> int: def update_area_of_max_square_using_dp_array( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> int: if row >= rows or col >= cols: return 0 if dp_array[row][col] != -1: return dp_array[row][col] __lowerCamelCase = update_area_of_max_square_using_dp_array(UpperCamelCase__ , col + 1 , UpperCamelCase__ ) __lowerCamelCase = update_area_of_max_square_using_dp_array(row + 1 , col + 1 , UpperCamelCase__ ) __lowerCamelCase = update_area_of_max_square_using_dp_array(row + 1 , UpperCamelCase__ , UpperCamelCase__ ) if mat[row][col]: __lowerCamelCase = 1 + min([right, diagonal, down] ) __lowerCamelCase = max(largest_square_area[0] , UpperCamelCase__ ) __lowerCamelCase = sub_problem_sol return sub_problem_sol else: return 0 __lowerCamelCase = [0] __lowerCamelCase = [[-1] * cols for _ in range(UpperCamelCase__ )] update_area_of_max_square_using_dp_array(0 , 0 , UpperCamelCase__ ) return largest_square_area[0] def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> int: __lowerCamelCase = [[0] * (cols + 1) for _ in range(rows + 1 )] __lowerCamelCase = 0 for row in range(rows - 1 , -1 , -1 ): for col in range(cols - 1 , -1 , -1 ): __lowerCamelCase = dp_array[row][col + 1] __lowerCamelCase = dp_array[row + 1][col + 1] __lowerCamelCase = dp_array[row + 1][col] if mat[row][col] == 1: __lowerCamelCase = 1 + min(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) __lowerCamelCase = max(dp_array[row][col] , UpperCamelCase__ ) else: __lowerCamelCase = 0 return largest_square_area def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> int: __lowerCamelCase = [0] * (cols + 1) __lowerCamelCase = [0] * (cols + 1) __lowerCamelCase = 0 for row in range(rows - 1 , -1 , -1 ): for col in range(cols - 1 , -1 , -1 ): __lowerCamelCase = current_row[col + 1] __lowerCamelCase = next_row[col + 1] __lowerCamelCase = next_row[col] if mat[row][col] == 1: __lowerCamelCase = 1 + min(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) __lowerCamelCase = max(current_row[col] , UpperCamelCase__ ) else: __lowerCamelCase = 0 __lowerCamelCase = current_row return largest_square_area if __name__ == "__main__": import doctest doctest.testmod() print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))
67
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST, OpenAIGPTConfig, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification, OpenAIGPTLMHeadModel, OpenAIGPTModel, ) class a__ : def __init__( self : Union[str, Any] , a : Union[str, Any] , a : Tuple=13 , a : Optional[Any]=7 , a : List[Any]=True , a : Optional[Any]=True , a : Any=True , a : Union[str, Any]=99 , a : Any=32 , a : int=5 , a : Optional[int]=4 , a : Union[str, Any]=37 , a : Optional[Any]="gelu" , a : Union[str, Any]=0.1 , a : Any=0.1 , a : Optional[int]=5_12 , a : int=16 , a : Optional[Any]=2 , a : Union[str, Any]=0.02 , a : Any=3 , a : Dict=4 , a : Any=None , ): """simple docstring""" __lowerCamelCase = parent __lowerCamelCase = batch_size __lowerCamelCase = seq_length __lowerCamelCase = is_training __lowerCamelCase = use_token_type_ids __lowerCamelCase = use_labels __lowerCamelCase = vocab_size __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_size __lowerCamelCase = hidden_act __lowerCamelCase = hidden_dropout_prob __lowerCamelCase = attention_probs_dropout_prob __lowerCamelCase = max_position_embeddings __lowerCamelCase = type_vocab_size __lowerCamelCase = type_sequence_label_size __lowerCamelCase = initializer_range __lowerCamelCase = num_labels __lowerCamelCase = num_choices __lowerCamelCase = scope __lowerCamelCase = self.vocab_size - 1 def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCamelCase = None if self.use_token_type_ids: __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = None if self.use_labels: __lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __lowerCamelCase = ids_tensor([self.batch_size] , self.num_choices ) __lowerCamelCase = OpenAIGPTConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) __lowerCamelCase = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, head_mask, token_type_ids, sequence_labels, token_labels, choice_labels, ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : Dict , a : List[str] , a : Tuple , a : List[Any] , *a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTModel(config=a ) model.to(a ) model.eval() __lowerCamelCase = model(a , token_type_ids=a , head_mask=a ) __lowerCamelCase = model(a , token_type_ids=a ) __lowerCamelCase = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Union[str, Any] , a : Dict , a : Union[str, Any] , a : Tuple , *a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTLMHeadModel(a ) model.to(a ) model.eval() __lowerCamelCase = model(a , token_type_ids=a , labels=a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Tuple , a : Optional[int] , a : Union[str, Any] , a : Optional[Any] , *a : Optional[Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTDoubleHeadsModel(a ) model.to(a ) model.eval() __lowerCamelCase = model(a , token_type_ids=a , labels=a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self : List[str] , a : int , a : Dict , a : Optional[Any] , a : str , *a : int ): """simple docstring""" __lowerCamelCase = self.num_labels __lowerCamelCase = OpenAIGPTForSequenceClassification(a ) model.to(a ) model.eval() __lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCamelCase = model(a , token_type_ids=a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = self.prepare_config_and_inputs() ( ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ) = config_and_inputs __lowerCamelCase = { '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask, } return config, inputs_dict @require_torch class a__ ( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): lowerCamelCase : List[str] =( (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification) if is_torch_available() else () ) lowerCamelCase : str =( (OpenAIGPTLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly lowerCamelCase : Optional[int] =( { "feature-extraction": OpenAIGPTModel, "text-classification": OpenAIGPTForSequenceClassification, "text-generation": OpenAIGPTLMHeadModel, "zero-shot": OpenAIGPTForSequenceClassification, } if is_torch_available() else {} ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : Tuple , a : Optional[int] , a : int , a : str , a : Any ): """simple docstring""" if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a # tiny config could not be created. return True return False def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : int , a : Optional[int] , a : str=False ): """simple docstring""" __lowerCamelCase = super()._prepare_for_class(a , a , return_labels=a ) if return_labels: if model_class.__name__ == "OpenAIGPTDoubleHeadsModel": __lowerCamelCase = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length) , dtype=torch.long , device=a , ) __lowerCamelCase = inputs_dict['''labels'''] __lowerCamelCase = inputs_dict['''labels'''] __lowerCamelCase = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices) , dtype=torch.long , device=a , ) __lowerCamelCase = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=a ) return inputs_dict def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTModelTester(self ) __lowerCamelCase = ConfigTester(self , config_class=a , n_embd=37 ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_double_lm_head_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*a ) @slow def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __lowerCamelCase = OpenAIGPTModel.from_pretrained(a ) self.assertIsNotNone(a ) @require_torch class a__ ( unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt''' ) model.to(a ) __lowerCamelCase = torch.tensor([[4_81, 47_35, 5_44]] , dtype=torch.long , device=a ) # the president is __lowerCamelCase = [ 4_81, 47_35, 5_44, 2_46, 9_63, 8_70, 7_62, 2_39, 2_44, 4_04_77, 2_44, 2_49, 7_19, 8_81, 4_87, 5_44, 2_40, 2_44, 6_03, 4_81, ] # the president is a very good man. " \n " i\'m sure he is, " said the __lowerCamelCase = model.generate(a , do_sample=a ) self.assertListEqual(output_ids[0].tolist() , a )
67
1
'''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 =logging.get_logger(__name__) @add_end_docstrings(UpperCAmelCase__ ) class a__ ( UpperCAmelCase__ ): def __init__( self : str , **a : List[Any] ): """simple docstring""" super().__init__(**a ) 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 : Dict , a : Union[str, List[str], "Image", List["Image"]] , **a : Dict ): """simple docstring""" return super().__call__(a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , **a : Optional[Any] ): """simple docstring""" __lowerCamelCase = {} if "candidate_labels" in kwargs: __lowerCamelCase = kwargs['''candidate_labels'''] if "hypothesis_template" in kwargs: __lowerCamelCase = kwargs['''hypothesis_template'''] return preprocess_params, {}, {} def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : str , a : Dict=None , a : Any="This is a photo of {}." ): """simple docstring""" __lowerCamelCase = load_image(a ) __lowerCamelCase = self.image_processor(images=[image] , return_tensors=self.framework ) __lowerCamelCase = candidate_labels __lowerCamelCase = [hypothesis_template.format(a ) for x in candidate_labels] __lowerCamelCase = self.tokenizer(a , return_tensors=self.framework , padding=a ) __lowerCamelCase = [text_inputs] return inputs def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : Optional[Any] ): """simple docstring""" __lowerCamelCase = model_inputs.pop('''candidate_labels''' ) __lowerCamelCase = model_inputs.pop('''text_inputs''' ) if isinstance(text_inputs[0] , a ): __lowerCamelCase = text_inputs[0] else: # Batching case. __lowerCamelCase = text_inputs[0][0] __lowerCamelCase = self.model(**a , **a ) __lowerCamelCase = { '''candidate_labels''': candidate_labels, '''logits''': outputs.logits_per_image, } return model_outputs def SCREAMING_SNAKE_CASE__ ( self : Dict , a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = model_outputs.pop('''candidate_labels''' ) __lowerCamelCase = model_outputs['''logits'''][0] if self.framework == "pt": __lowerCamelCase = logits.softmax(dim=-1 ).squeeze(-1 ) __lowerCamelCase = probs.tolist() if not isinstance(a , a ): __lowerCamelCase = [scores] elif self.framework == "tf": __lowerCamelCase = stable_softmax(a , axis=-1 ) __lowerCamelCase = probs.numpy().tolist() else: raise ValueError(f"""Unsupported framework: {self.framework}""" ) __lowerCamelCase = [ {'''score''': score, '''label''': candidate_label} for score, candidate_label in sorted(zip(a , a ) , key=lambda a : -x[0] ) ] return result
67
'''simple docstring''' import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] =["image_processor", "tokenizer"] lowerCamelCase : Union[str, Any] ="LayoutLMv2ImageProcessor" lowerCamelCase : int =("LayoutXLMTokenizer", "LayoutXLMTokenizerFast") def __init__( self : Optional[int] , a : Any=None , a : Any=None , **a : Union[str, Any] ): """simple docstring""" if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , a , ) __lowerCamelCase = kwargs.pop('''feature_extractor''' ) __lowerCamelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(a , a ) def __call__( self : Tuple , a : Optional[int] , a : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , a : Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None , a : Union[List[List[int]], List[List[List[int]]]] = None , a : Optional[Union[List[int], List[List[int]]]] = None , a : bool = True , a : Union[bool, str, PaddingStrategy] = False , a : Union[bool, str, TruncationStrategy] = None , a : Optional[int] = None , a : int = 0 , a : Optional[int] = None , a : Optional[bool] = None , a : Optional[bool] = None , a : bool = False , a : bool = False , a : bool = False , a : bool = False , a : bool = True , a : Optional[Union[str, TensorType]] = None , **a : Tuple , ): """simple docstring""" if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( '''You cannot provide bounding boxes ''' '''if you initialized the image processor with apply_ocr set to True.''' ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.''' ) if return_overflowing_tokens is True and return_offsets_mapping is False: raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.''' ) # first, apply the image processor __lowerCamelCase = self.image_processor(images=a , return_tensors=a ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(a , a ): __lowerCamelCase = [text] # add batch dimension (as the image processor always adds a batch dimension) __lowerCamelCase = features['''words'''] __lowerCamelCase = self.tokenizer( text=text if text is not None else features['''words'''] , text_pair=text_pair if text_pair is not None else None , boxes=boxes if boxes is not None else features['''boxes'''] , word_labels=a , add_special_tokens=a , padding=a , truncation=a , max_length=a , stride=a , pad_to_multiple_of=a , return_token_type_ids=a , return_attention_mask=a , return_overflowing_tokens=a , return_special_tokens_mask=a , return_offsets_mapping=a , return_length=a , verbose=a , return_tensors=a , **a , ) # add pixel values __lowerCamelCase = features.pop('''pixel_values''' ) if return_overflowing_tokens is True: __lowerCamelCase = self.get_overflowing_images(a , encoded_inputs['''overflow_to_sample_mapping'''] ) __lowerCamelCase = images return encoded_inputs def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : Optional[Any] , a : str ): """simple docstring""" __lowerCamelCase = [] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(a ) != len(a ): raise ValueError( '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got''' f""" {len(a )} and {len(a )}""" ) return images_with_overflow def SCREAMING_SNAKE_CASE__ ( self : List[str] , *a : Optional[Any] , **a : Union[str, Any] ): """simple docstring""" return self.tokenizer.batch_decode(*a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , *a : Union[str, Any] , **a : Tuple ): """simple docstring""" return self.tokenizer.decode(*a , **a ) @property def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" return ["input_ids", "bbox", "attention_mask", "image"] @property def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , a , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , a , ) return self.image_processor
67
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={ "google/pegasus-large": "https://huggingface.co/google/pegasus-large/resolve/main/config.json", # See all PEGASUS models at https://huggingface.co/models?filter=pegasus } class a__ ( UpperCAmelCase__ ): lowerCamelCase : Tuple ="pegasus" lowerCamelCase : Union[str, Any] =["past_key_values"] lowerCamelCase : str ={"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"} def __init__( self : List[str] , a : str=5_02_65 , a : Union[str, Any]=10_24 , a : Tuple=12 , a : Optional[Any]=40_96 , a : Optional[int]=16 , a : Dict=12 , a : str=40_96 , a : Union[str, Any]=16 , a : str=0.0 , a : Optional[Any]=0.0 , a : int=True , a : Union[str, Any]=True , a : Union[str, Any]="gelu" , a : str=10_24 , a : Optional[int]=0.1 , a : Union[str, Any]=0.0 , a : List[str]=0.0 , a : Optional[Any]=0.02 , a : Tuple=0 , a : Any=False , a : int=0 , a : Dict=1 , a : Tuple=1 , **a : Optional[Any] , ): """simple docstring""" __lowerCamelCase = vocab_size __lowerCamelCase = max_position_embeddings __lowerCamelCase = d_model __lowerCamelCase = encoder_ffn_dim __lowerCamelCase = encoder_layers __lowerCamelCase = encoder_attention_heads __lowerCamelCase = decoder_ffn_dim __lowerCamelCase = decoder_layers __lowerCamelCase = decoder_attention_heads __lowerCamelCase = dropout __lowerCamelCase = attention_dropout __lowerCamelCase = activation_dropout __lowerCamelCase = activation_function __lowerCamelCase = init_std __lowerCamelCase = encoder_layerdrop __lowerCamelCase = decoder_layerdrop __lowerCamelCase = use_cache __lowerCamelCase = encoder_layers __lowerCamelCase = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=a , eos_token_id=a , is_encoder_decoder=a , decoder_start_token_id=a , forced_eos_token_id=a , **a , ) @property def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" return self.encoder_attention_heads @property def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" return self.d_model
67
'''simple docstring''' import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) class a__ ( UpperCAmelCase__ ): def __init__( self : Optional[Any] , a : Union[List[ControlNetModel], Tuple[ControlNetModel]] ): """simple docstring""" super().__init__() __lowerCamelCase = nn.ModuleList(a ) def SCREAMING_SNAKE_CASE__ ( self : Any , a : torch.FloatTensor , a : Union[torch.Tensor, float, int] , a : torch.Tensor , a : List[torch.tensor] , a : List[float] , a : Optional[torch.Tensor] = None , a : Optional[torch.Tensor] = None , a : Optional[torch.Tensor] = None , a : Optional[Dict[str, Any]] = None , a : bool = False , a : bool = True , ): """simple docstring""" for i, (image, scale, controlnet) in enumerate(zip(a , a , self.nets ) ): __lowerCamelCase , __lowerCamelCase = controlnet( a , a , a , a , a , a , a , a , a , a , a , ) # merge samples if i == 0: __lowerCamelCase , __lowerCamelCase = down_samples, mid_sample else: __lowerCamelCase = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(a , a ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def SCREAMING_SNAKE_CASE__ ( self : Any , a : Union[str, os.PathLike] , a : bool = True , a : Callable = None , a : bool = False , a : Optional[str] = None , ): """simple docstring""" __lowerCamelCase = 0 __lowerCamelCase = save_directory for controlnet in self.nets: controlnet.save_pretrained( a , is_main_process=a , save_function=a , safe_serialization=a , variant=a , ) idx += 1 __lowerCamelCase = model_path_to_save + f"""_{idx}""" @classmethod def SCREAMING_SNAKE_CASE__ ( cls : List[str] , a : Optional[Union[str, os.PathLike]] , **a : Optional[Any] ): """simple docstring""" __lowerCamelCase = 0 __lowerCamelCase = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... __lowerCamelCase = pretrained_model_path while os.path.isdir(a ): __lowerCamelCase = ControlNetModel.from_pretrained(a , **a ) controlnets.append(a ) idx += 1 __lowerCamelCase = pretrained_model_path + f"""_{idx}""" logger.info(f"""{len(a )} controlnets loaded from {pretrained_model_path}.""" ) if len(a ) == 0: raise ValueError( f"""No ControlNets found under {os.path.dirname(a )}. Expected at least {pretrained_model_path + '_0'}.""" ) return cls(a )
67
1
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> float: if digit_amount > 0: return round(number - int(UpperCamelCase__ ) , UpperCamelCase__ ) return number - int(UpperCamelCase__ ) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.345, 1)) print(decimal_isolate(35.345, 2)) print(decimal_isolate(35.345, 3)) print(decimal_isolate(-14.789, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.123, 1)) print(decimal_isolate(-14.123, 2)) print(decimal_isolate(-14.123, 3))
67
'''simple docstring''' from typing import List, Union import numpy as np from ..utils import add_end_docstrings, 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(): import torch from ..models.auto.modeling_auto import MODEL_FOR_DEPTH_ESTIMATION_MAPPING __UpperCAmelCase =logging.get_logger(__name__) @add_end_docstrings(UpperCAmelCase__ ) class a__ ( UpperCAmelCase__ ): def __init__( self : List[str] , *a : Union[str, Any] , **a : Optional[Any] ): """simple docstring""" super().__init__(*a , **a ) requires_backends(self , '''vision''' ) self.check_model_type(a ) def __call__( self : Any , a : Union[str, List[str], "Image.Image", List["Image.Image"]] , **a : Optional[int] ): """simple docstring""" return super().__call__(a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , **a : Any ): """simple docstring""" return {}, {}, {} def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : List[str] ): """simple docstring""" __lowerCamelCase = load_image(a ) __lowerCamelCase = image.size __lowerCamelCase = self.image_processor(images=a , return_tensors=self.framework ) return model_inputs def SCREAMING_SNAKE_CASE__ ( self : int , a : Optional[Any] ): """simple docstring""" __lowerCamelCase = self.model(**a ) return model_outputs def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : Any ): """simple docstring""" __lowerCamelCase = model_outputs.predicted_depth __lowerCamelCase = torch.nn.functional.interpolate( predicted_depth.unsqueeze(1 ) , size=self.image_size[::-1] , mode='''bicubic''' , align_corners=a ) __lowerCamelCase = prediction.squeeze().cpu().numpy() __lowerCamelCase = (output * 2_55 / np.max(a )).astype('''uint8''' ) __lowerCamelCase = Image.fromarray(a ) __lowerCamelCase = {} __lowerCamelCase = predicted_depth __lowerCamelCase = depth return output_dict
67
1
'''simple docstring''' from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class a__ ( nn.Module ): def __init__( self : Dict , a : int = 16 , a : int = 88 , a : Optional[int] = None , a : int = 1 , a : float = 0.0 , a : int = 32 , a : Optional[int] = None , a : bool = False , a : Optional[int] = None , a : Optional[int] = None , a : str = "geglu" , a : Optional[int] = None , ): """simple docstring""" super().__init__() __lowerCamelCase = nn.ModuleList( [ TransformeraDModel( num_attention_heads=a , attention_head_dim=a , in_channels=a , num_layers=a , dropout=a , norm_num_groups=a , cross_attention_dim=a , attention_bias=a , sample_size=a , num_vector_embeds=a , activation_fn=a , num_embeds_ada_norm=a , ) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference __lowerCamelCase = 0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` __lowerCamelCase = [77, 2_57] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` __lowerCamelCase = [1, 0] def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : Any , a : List[Any] , a : Optional[Any]=None , a : Optional[Any]=None , a : Tuple=None , a : bool = True , ): """simple docstring""" __lowerCamelCase = hidden_states __lowerCamelCase = [] __lowerCamelCase = 0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens __lowerCamelCase = encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] __lowerCamelCase = self.transformer_index_for_condition[i] __lowerCamelCase = self.transformers[transformer_index]( a , encoder_hidden_states=a , timestep=a , cross_attention_kwargs=a , return_dict=a , )[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] __lowerCamelCase = encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) __lowerCamelCase = output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=a )
67
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase ={ "configuration_clap": [ "CLAP_PRETRAINED_MODEL_ARCHIVE_LIST", "ClapAudioConfig", "ClapConfig", "ClapTextConfig", ], "processing_clap": ["ClapProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "CLAP_PRETRAINED_MODEL_ARCHIVE_LIST", "ClapModel", "ClapPreTrainedModel", "ClapTextModel", "ClapTextModelWithProjection", "ClapAudioModel", "ClapAudioModelWithProjection", ] __UpperCAmelCase =["ClapFeatureExtractor"] if TYPE_CHECKING: from .configuration_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioConfig, ClapConfig, ClapTextConfig, ) from .processing_clap import ClapProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clap import ClapFeatureExtractor from .modeling_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioModel, ClapAudioModelWithProjection, ClapModel, ClapPreTrainedModel, ClapTextModel, ClapTextModelWithProjection, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
67
1
'''simple docstring''' import itertools import math def __lowerCAmelCase ( UpperCamelCase__ ) -> bool: if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(UpperCamelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def __lowerCAmelCase ( ) -> List[Any]: __lowerCamelCase = 2 while True: if is_prime(UpperCamelCase__ ): yield num num += 1 def __lowerCAmelCase ( UpperCamelCase__ = 1_00_01 ) -> int: return next(itertools.islice(prime_generator() , nth - 1 , UpperCamelCase__ ) ) if __name__ == "__main__": print(f'{solution() = }')
67
'''simple docstring''' import os import shutil import tempfile import unittest import numpy as np from transformers import AutoTokenizer, BarkProcessor from transformers.testing_utils import require_torch, slow @require_torch class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = '''ylacombe/bark-small''' __lowerCamelCase = tempfile.mkdtemp() __lowerCamelCase = '''en_speaker_1''' __lowerCamelCase = '''This is a test string''' __lowerCamelCase = '''speaker_embeddings_path.json''' __lowerCamelCase = '''speaker_embeddings''' def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , **a : Dict ): """simple docstring""" return AutoTokenizer.from_pretrained(self.checkpoint , **a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) processor.save_pretrained(self.tmpdirname ) __lowerCamelCase = BarkProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) @slow def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) processor.save_pretrained( self.tmpdirname , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , speaker_embeddings_directory=self.speaker_embeddings_directory , ) __lowerCamelCase = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) __lowerCamelCase = BarkProcessor.from_pretrained( self.tmpdirname , self.speaker_embeddings_dict_path , bos_token='''(BOS)''' , eos_token='''(EOS)''' , ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) __lowerCamelCase = 35 __lowerCamelCase = 2 __lowerCamelCase = 8 __lowerCamelCase = { '''semantic_prompt''': np.ones(a ), '''coarse_prompt''': np.ones((nb_codebooks_coarse, seq_len) ), '''fine_prompt''': np.ones((nb_codebooks_total, seq_len) ), } # test providing already loaded voice_preset __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from npz file __lowerCamelCase = os.path.join(self.tmpdirname , '''file.npz''' ) np.savez(a , **a ) __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from the hub __lowerCamelCase = processor(text=self.input_string , voice_preset=self.voice_preset ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) __lowerCamelCase = processor(text=self.input_string ) __lowerCamelCase = tokenizer( self.input_string , padding='''max_length''' , max_length=2_56 , add_special_tokens=a , return_attention_mask=a , return_token_type_ids=a , ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key].squeeze().tolist() )
67
1
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class a__ ( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): lowerCamelCase : Tuple =AltDiffusionPipeline lowerCamelCase : List[str] =TEXT_TO_IMAGE_PARAMS lowerCamelCase : Any =TEXT_TO_IMAGE_BATCH_PARAMS lowerCamelCase : Union[str, Any] =TEXT_TO_IMAGE_IMAGE_PARAMS lowerCamelCase : Optional[Any] =TEXT_TO_IMAGE_IMAGE_PARAMS def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" torch.manual_seed(0 ) __lowerCamelCase = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) __lowerCamelCase = DDIMScheduler( beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule='''scaled_linear''' , clip_sample=a , set_alpha_to_one=a , ) torch.manual_seed(0 ) __lowerCamelCase = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) # TODO: address the non-deterministic text encoder (fails for save-load tests) # torch.manual_seed(0) # text_encoder_config = RobertaSeriesConfig( # hidden_size=32, # project_dim=32, # intermediate_size=37, # layer_norm_eps=1e-05, # num_attention_heads=4, # num_hidden_layers=5, # vocab_size=5002, # ) # text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config) torch.manual_seed(0 ) __lowerCamelCase = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=50_02 , ) __lowerCamelCase = CLIPTextModel(a ) __lowerCamelCase = XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta''' ) __lowerCamelCase = 77 __lowerCamelCase = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def SCREAMING_SNAKE_CASE__ ( self : List[str] , a : List[Any] , a : Union[str, Any]=0 ): """simple docstring""" if str(a ).startswith('''mps''' ): __lowerCamelCase = torch.manual_seed(a ) else: __lowerCamelCase = torch.Generator(device=a ).manual_seed(a ) __lowerCamelCase = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" super().test_attention_slicing_forward_pass(expected_max_diff=3e-3 ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" __lowerCamelCase = '''cpu''' # ensure determinism for the device-dependent torch.Generator __lowerCamelCase = self.get_dummy_components() torch.manual_seed(0 ) __lowerCamelCase = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=50_02 , ) # TODO: remove after fixing the non-deterministic text encoder __lowerCamelCase = RobertaSeriesModelWithTransformation(a ) __lowerCamelCase = text_encoder __lowerCamelCase = AltDiffusionPipeline(**a ) __lowerCamelCase = alt_pipe.to(a ) alt_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = self.get_dummy_inputs(a ) __lowerCamelCase = '''A photo of an astronaut''' __lowerCamelCase = alt_pipe(**a ) __lowerCamelCase = output.images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __lowerCamelCase = np.array( [0.5_74_81_62, 0.60_44_71_45, 0.48_82_12_17, 0.50_10_06_36, 0.5_43_11_85, 0.45_76_36_83, 0.49_65_76_96, 0.48_13_27_33, 0.47_57_30_93] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = '''cpu''' # ensure determinism for the device-dependent torch.Generator __lowerCamelCase = self.get_dummy_components() __lowerCamelCase = PNDMScheduler(skip_prk_steps=a ) torch.manual_seed(0 ) __lowerCamelCase = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=50_02 , ) # TODO: remove after fixing the non-deterministic text encoder __lowerCamelCase = RobertaSeriesModelWithTransformation(a ) __lowerCamelCase = text_encoder __lowerCamelCase = AltDiffusionPipeline(**a ) __lowerCamelCase = alt_pipe.to(a ) alt_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = self.get_dummy_inputs(a ) __lowerCamelCase = alt_pipe(**a ) __lowerCamelCase = output.images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __lowerCamelCase = np.array( [0.51_60_50_93, 0.5_70_72_41, 0.47_36_55_07, 0.50_57_88_86, 0.5_63_38_77, 0.4_64_25_03, 0.5_18_20_81, 0.48_76_34_84, 0.49_08_42_37] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion''' , safety_checker=a ) __lowerCamelCase = alt_pipe.to(a ) alt_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = '''A painting of a squirrel eating a burger''' __lowerCamelCase = torch.manual_seed(0 ) __lowerCamelCase = alt_pipe([prompt] , generator=a , guidance_scale=6.0 , num_inference_steps=20 , output_type='''np''' ) __lowerCamelCase = output.images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) __lowerCamelCase = np.array([0.10_10, 0.08_00, 0.07_94, 0.08_85, 0.08_43, 0.07_62, 0.07_69, 0.07_29, 0.05_86] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = DDIMScheduler.from_pretrained('''BAAI/AltDiffusion''' , subfolder='''scheduler''' ) __lowerCamelCase = AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion''' , scheduler=a , safety_checker=a ) __lowerCamelCase = alt_pipe.to(a ) alt_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = '''A painting of a squirrel eating a burger''' __lowerCamelCase = torch.manual_seed(0 ) __lowerCamelCase = alt_pipe([prompt] , generator=a , num_inference_steps=2 , output_type='''numpy''' ) __lowerCamelCase = output.images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) __lowerCamelCase = np.array([0.40_19, 0.40_52, 0.38_10, 0.41_19, 0.39_16, 0.39_82, 0.46_51, 0.41_95, 0.53_23] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
67
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase ={"configuration_vit_msn": ["VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP", "ViTMSNConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST", "ViTMSNModel", "ViTMSNForImageClassification", "ViTMSNPreTrainedModel", ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
67
1
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionSAGPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class a__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): lowerCamelCase : Optional[Any] =StableDiffusionSAGPipeline lowerCamelCase : List[str] =TEXT_TO_IMAGE_PARAMS lowerCamelCase : List[Any] =TEXT_TO_IMAGE_BATCH_PARAMS lowerCamelCase : List[str] =TEXT_TO_IMAGE_IMAGE_PARAMS lowerCamelCase : Tuple =TEXT_TO_IMAGE_IMAGE_PARAMS lowerCamelCase : Optional[int] =False def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" torch.manual_seed(0 ) __lowerCamelCase = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) __lowerCamelCase = DDIMScheduler( beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule='''scaled_linear''' , clip_sample=a , set_alpha_to_one=a , ) torch.manual_seed(0 ) __lowerCamelCase = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) __lowerCamelCase = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) __lowerCamelCase = CLIPTextModel(a ) __lowerCamelCase = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __lowerCamelCase = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : str , a : str=0 ): """simple docstring""" if str(a ).startswith('''mps''' ): __lowerCamelCase = torch.manual_seed(a ) else: __lowerCamelCase = torch.Generator(device=a ).manual_seed(a ) __lowerCamelCase = { '''prompt''': '''.''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 1.0, '''sag_scale''': 1.0, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" __lowerCamelCase = StableDiffusionSAGPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' ) __lowerCamelCase = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = '''.''' __lowerCamelCase = torch.manual_seed(0 ) __lowerCamelCase = sag_pipe( [prompt] , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' ) __lowerCamelCase = output.images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) __lowerCamelCase = np.array([0.15_68, 0.17_38, 0.16_95, 0.16_93, 0.15_07, 0.17_05, 0.15_47, 0.17_51, 0.19_49] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-2 def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) __lowerCamelCase = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = '''.''' __lowerCamelCase = torch.manual_seed(0 ) __lowerCamelCase = sag_pipe( [prompt] , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' ) __lowerCamelCase = output.images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) __lowerCamelCase = np.array([0.34_59, 0.28_76, 0.25_37, 0.30_02, 0.26_71, 0.21_60, 0.30_26, 0.22_62, 0.23_71] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-2 def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" __lowerCamelCase = StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) __lowerCamelCase = sag_pipe.to(a ) sag_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = '''.''' __lowerCamelCase = torch.manual_seed(0 ) __lowerCamelCase = sag_pipe( [prompt] , width=7_68 , height=5_12 , generator=a , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' , ) __lowerCamelCase = output.images assert image.shape == (1, 5_12, 7_68, 3)
67
'''simple docstring''' import re from filelock import FileLock try: import nltk __UpperCAmelCase =True except (ImportError, ModuleNotFoundError): __UpperCAmelCase =False if NLTK_AVAILABLE: with FileLock(".lock") as lock: nltk.download("punkt", quiet=True) def __lowerCAmelCase ( UpperCamelCase__ ) -> str: re.sub('''<n>''' , '''''' , UpperCamelCase__ ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(UpperCamelCase__ ) )
67
1
'''simple docstring''' import os from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE, hf_cache_home __UpperCAmelCase =HUGGINGFACE_HUB_CACHE __UpperCAmelCase ="config.json" __UpperCAmelCase ="diffusion_pytorch_model.bin" __UpperCAmelCase ="diffusion_flax_model.msgpack" __UpperCAmelCase ="model.onnx" __UpperCAmelCase ="diffusion_pytorch_model.safetensors" __UpperCAmelCase ="weights.pb" __UpperCAmelCase ="https://huggingface.co" __UpperCAmelCase =default_cache_path __UpperCAmelCase ="diffusers_modules" __UpperCAmelCase =os.getenv("HF_MODULES_CACHE", os.path.join(hf_cache_home, "modules")) __UpperCAmelCase =["fp16", "non-ema"] __UpperCAmelCase =".self_attn"
67
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={ "abeja/gpt-neox-japanese-2.7b": "https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json", } class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] ="gpt_neox_japanese" def __init__( self : List[Any] , a : Tuple=3_20_00 , a : Dict=25_60 , a : Union[str, Any]=32 , a : Dict=32 , a : Dict=4 , a : Optional[Any]="gelu" , a : Any=1.00 , a : str=1_00_00 , a : List[str]=20_48 , a : str=0.02 , a : Union[str, Any]=1e-5 , a : Optional[Any]=True , a : str=3_19_96 , a : List[str]=3_19_99 , a : str=0.1 , a : Union[str, Any]=0.0 , **a : Optional[Any] , ): """simple docstring""" super().__init__(bos_token_id=a , eos_token_id=a , **a ) __lowerCamelCase = vocab_size __lowerCamelCase = max_position_embeddings __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_multiple_size __lowerCamelCase = hidden_act __lowerCamelCase = rotary_pct __lowerCamelCase = rotary_emb_base __lowerCamelCase = initializer_range __lowerCamelCase = layer_norm_eps __lowerCamelCase = use_cache __lowerCamelCase = attention_dropout __lowerCamelCase = hidden_dropout
67
1
'''simple docstring''' from __future__ import annotations from collections.abc import Sequence from typing import Literal def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> str | Literal[False]: __lowerCamelCase = list(UpperCamelCase__ ) __lowerCamelCase = list(UpperCamelCase__ ) __lowerCamelCase = 0 for i in range(len(UpperCamelCase__ ) ): if lista[i] != lista[i]: count += 1 __lowerCamelCase = '''_''' if count > 1: return False else: return "".join(UpperCamelCase__ ) def __lowerCAmelCase ( UpperCamelCase__ ) -> list[str]: __lowerCamelCase = [] while True: __lowerCamelCase = ['''$'''] * len(UpperCamelCase__ ) __lowerCamelCase = [] for i in range(len(UpperCamelCase__ ) ): for j in range(i + 1 , len(UpperCamelCase__ ) ): __lowerCamelCase = compare_string(binary[i] , binary[j] ) if k is False: __lowerCamelCase = '''*''' __lowerCamelCase = '''*''' temp.append('''X''' ) for i in range(len(UpperCamelCase__ ) ): if checka[i] == "$": pi.append(binary[i] ) if len(UpperCamelCase__ ) == 0: return pi __lowerCamelCase = list(set(UpperCamelCase__ ) ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> list[str]: __lowerCamelCase = [] for minterm in minterms: __lowerCamelCase = '''''' for _ in range(UpperCamelCase__ ): __lowerCamelCase = str(minterm % 2 ) + string minterm //= 2 temp.append(UpperCamelCase__ ) return temp def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> bool: __lowerCamelCase = list(UpperCamelCase__ ) __lowerCamelCase = list(UpperCamelCase__ ) __lowerCamelCase = 0 for i in range(len(UpperCamelCase__ ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> list[str]: __lowerCamelCase = [] __lowerCamelCase = [0] * len(UpperCamelCase__ ) for i in range(len(chart[0] ) ): __lowerCamelCase = 0 __lowerCamelCase = -1 for j in range(len(UpperCamelCase__ ) ): if chart[j][i] == 1: count += 1 __lowerCamelCase = j if count == 1: __lowerCamelCase = 1 for i in range(len(UpperCamelCase__ ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(UpperCamelCase__ ) ): __lowerCamelCase = 0 temp.append(prime_implicants[i] ) while True: __lowerCamelCase = 0 __lowerCamelCase = -1 __lowerCamelCase = 0 for i in range(len(UpperCamelCase__ ) ): __lowerCamelCase = chart[i].count(1 ) if count_n > max_n: __lowerCamelCase = count_n __lowerCamelCase = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(UpperCamelCase__ ) ): __lowerCamelCase = 0 def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> list[list[int]]: __lowerCamelCase = [[0 for x in range(len(UpperCamelCase__ ) )] for x in range(len(UpperCamelCase__ ) )] for i in range(len(UpperCamelCase__ ) ): __lowerCamelCase = prime_implicants[i].count('''_''' ) for j in range(len(UpperCamelCase__ ) ): if is_for_table(prime_implicants[i] , binary[j] , UpperCamelCase__ ): __lowerCamelCase = 1 return chart def __lowerCAmelCase ( ) -> None: __lowerCamelCase = int(input('''Enter the no. of variables\n''' ) ) __lowerCamelCase = [ float(UpperCamelCase__ ) for x in input( '''Enter the decimal representation of Minterms \'Spaces Separated\'\n''' ).split() ] __lowerCamelCase = decimal_to_binary(UpperCamelCase__ , UpperCamelCase__ ) __lowerCamelCase = check(UpperCamelCase__ ) print('''Prime Implicants are:''' ) print(UpperCamelCase__ ) __lowerCamelCase = prime_implicant_chart(UpperCamelCase__ , UpperCamelCase__ ) __lowerCamelCase = selection(UpperCamelCase__ , UpperCamelCase__ ) print('''Essential Prime Implicants are:''' ) print(UpperCamelCase__ ) if __name__ == "__main__": import doctest doctest.testmod() main()
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> int: while second != 0: __lowerCamelCase = first & second first ^= second __lowerCamelCase = c << 1 return first if __name__ == "__main__": import doctest doctest.testmod() __UpperCAmelCase =int(input("Enter the first number: ").strip()) __UpperCAmelCase =int(input("Enter the second number: ").strip()) print(f'{add(first, second) = }')
67
1
'''simple docstring''' from math import factorial def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> int: # If either of the conditions are true, the function is being asked # to calculate a factorial of a negative number, which is not possible if n < k or k < 0: raise ValueError('''Please enter positive integers for n and k where n >= k''' ) return factorial(UpperCamelCase__ ) // (factorial(UpperCamelCase__ ) * factorial(n - k )) if __name__ == "__main__": print( "The number of five-card hands possible from a standard", f'fifty-two card deck is: {combinations(5_2, 5)}\n', ) print( "If a class of 40 students must be arranged into groups of", f'4 for group projects, there are {combinations(4_0, 4)} ways', "to arrange them.\n", ) print( "If 10 teams are competing in a Formula One race, there", f'are {combinations(1_0, 3)} ways that first, second and', "third place can be awarded.", )
67
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase ={ "configuration_time_series_transformer": [ "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimeSeriesTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimeSeriesTransformerForPrediction", "TimeSeriesTransformerModel", "TimeSeriesTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
67
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={ "microsoft/swinv2-tiny-patch4-window8-256": ( "https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json" ), } class a__ ( UpperCAmelCase__ ): lowerCamelCase : int ="swinv2" lowerCamelCase : Optional[Any] ={ "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : Dict , a : Dict=2_24 , a : Tuple=4 , a : Union[str, Any]=3 , a : Tuple=96 , a : Union[str, Any]=[2, 2, 6, 2] , a : Union[str, Any]=[3, 6, 12, 24] , a : Optional[int]=7 , a : List[Any]=4.0 , a : Optional[int]=True , a : Any=0.0 , a : Any=0.0 , a : Optional[Any]=0.1 , a : int="gelu" , a : str=False , a : int=0.02 , a : int=1e-5 , a : List[str]=32 , **a : Optional[Any] , ): """simple docstring""" super().__init__(**a ) __lowerCamelCase = image_size __lowerCamelCase = patch_size __lowerCamelCase = num_channels __lowerCamelCase = embed_dim __lowerCamelCase = depths __lowerCamelCase = len(a ) __lowerCamelCase = num_heads __lowerCamelCase = window_size __lowerCamelCase = mlp_ratio __lowerCamelCase = qkv_bias __lowerCamelCase = hidden_dropout_prob __lowerCamelCase = attention_probs_dropout_prob __lowerCamelCase = drop_path_rate __lowerCamelCase = hidden_act __lowerCamelCase = use_absolute_embeddings __lowerCamelCase = layer_norm_eps __lowerCamelCase = initializer_range __lowerCamelCase = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model __lowerCamelCase = int(embed_dim * 2 ** (len(a ) - 1) ) __lowerCamelCase = (0, 0, 0, 0)
67
'''simple docstring''' import warnings from pathlib import Path from typing import List, Tuple, Union import fire from torch import nn from transformers import AutoModelForSeqaSeqLM, AutoTokenizer, PreTrainedModel from transformers.utils import logging __UpperCAmelCase =logging.get_logger(__name__) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> None: __lowerCamelCase = nn.ModuleList([src_layers[i] for i in layers_to_copy] ) assert len(UpperCamelCase__ ) == len(UpperCamelCase__ ), f"""{len(UpperCamelCase__ )} != {len(UpperCamelCase__ )}""" dest_layers.load_state_dict(layers_to_copy.state_dict() ) __UpperCAmelCase ={ # maps num layers in teacher -> num_layers in student -> which teacher layers to copy. # 12: bart, 16: pegasus, 6: marian/Helsinki-NLP 1_2: { 1: [0], # This says that if the teacher has 12 layers and the student has 1, copy layer 0 of the teacher 2: [0, 6], 3: [0, 6, 1_1], 4: [0, 4, 8, 1_1], 6: [0, 2, 4, 7, 9, 1_1], 9: [0, 1, 2, 4, 5, 7, 9, 1_0, 1_1], 1_2: list(range(1_2)), }, 1_6: { # maps num layers in student -> which teacher layers to copy 1: [0], 2: [0, 1_5], 3: [0, 8, 1_5], 4: [0, 5, 1_0, 1_5], 6: [0, 3, 6, 9, 1_2, 1_5], 8: [0, 2, 4, 6, 8, 1_0, 1_2, 1_5], 9: [0, 1, 3, 5, 7, 9, 1_1, 1_3, 1_5], 1_2: [0, 1, 2, 3, 4, 5, 6, 7, 9, 1_1, 1_3, 1_5], 1_6: list(range(1_6)), }, 6: {1: [0], 2: [0, 5], 3: [0, 2, 5], 4: [0, 1, 3, 5], 6: list(range(6))}, } __UpperCAmelCase ={ # maps num layers in student -> which teacher layers to copy. 6: {1: [5], 2: [3, 5], 3: [1, 4, 5], 4: [1, 2, 4, 5]}, 1_2: {1: [1_1], 2: [5, 1_1], 3: [3, 7, 1_1], 6: [1, 3, 5, 8, 1_0, 1_1]}, 1_6: {1: [1_5], 4: [4, 9, 1_2, 1_5], 8: [1, 3, 5, 7, 9, 1_1, 1_3, 1_5]}, } def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Union[str, Any]: try: __lowerCamelCase = LAYERS_TO_COPY[n_teacher][n_student] return val except KeyError: if n_student != n_teacher: warnings.warn( f"""no hardcoded layers to copy for teacher {n_teacher} -> student {n_student}, defaulting to first""" f""" {n_student}""" ) return list(range(UpperCamelCase__ ) ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> List[int]: if n_student > n_teacher: raise ValueError(f"""Cannot perform intermediate supervision for student {n_student} > teacher {n_teacher}""" ) elif n_teacher == n_student: return list(range(UpperCamelCase__ ) ) elif n_student == 1: return [n_teacher - 1] else: return LAYERS_TO_SUPERVISE[n_teacher][n_student] def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ = "student" , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__=False , UpperCamelCase__=None , UpperCamelCase__=None , **UpperCamelCase__ , ) -> Tuple[PreTrainedModel, List[int], List[int]]: __lowerCamelCase = '''encoder_layers and decoder_layers cannot be both None-- you would just have an identical teacher.''' assert (e is not None) or (d is not None), _msg if isinstance(UpperCamelCase__ , UpperCamelCase__ ): AutoTokenizer.from_pretrained(UpperCamelCase__ ).save_pretrained(UpperCamelCase__ ) # purely for convenience __lowerCamelCase = AutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__ ).eval() else: assert isinstance(UpperCamelCase__ , UpperCamelCase__ ), f"""teacher must be a model or string got type {type(UpperCamelCase__ )}""" __lowerCamelCase = teacher.config.to_diff_dict() try: __lowerCamelCase , __lowerCamelCase = teacher.config.encoder_layers, teacher.config.decoder_layers if e is None: __lowerCamelCase = teacher_e if d is None: __lowerCamelCase = teacher_d init_kwargs.update({'''encoder_layers''': e, '''decoder_layers''': d} ) except AttributeError: # T5 if hasattr(teacher.config , '''num_encoder_layers''' ): __lowerCamelCase , __lowerCamelCase = teacher.config.num_encoder_layers, teacher.config.num_decoder_layers else: __lowerCamelCase , __lowerCamelCase = teacher.config.num_layers, teacher.config.num_decoder_layers if e is None: __lowerCamelCase = teacher_e if d is None: __lowerCamelCase = teacher_d if hasattr(teacher.config , '''num_encoder_layers''' ): init_kwargs.update({'''num_encoder_layers''': e, '''num_decoder_layers''': d} ) else: init_kwargs.update({'''num_layers''': e, '''num_decoder_layers''': d} ) # Kwargs to instantiate student: teacher kwargs with updated layer numbers + **extra_config_kwargs init_kwargs.update(UpperCamelCase__ ) # Copy weights __lowerCamelCase = teacher.config_class(**UpperCamelCase__ ) __lowerCamelCase = AutoModelForSeqaSeqLM.from_config(UpperCamelCase__ ) # Start by copying the full teacher state dict this will copy the first N teacher layers to the student. __lowerCamelCase = student.load_state_dict(teacher.state_dict() , strict=UpperCamelCase__ ) assert info.missing_keys == [], info.missing_keys # every student key should have a teacher keys. if copy_first_teacher_layers: # Our copying is done. We just log and save __lowerCamelCase , __lowerCamelCase = list(range(UpperCamelCase__ ) ), list(range(UpperCamelCase__ ) ) logger.info( f"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to""" f""" {save_path}""" ) student.save_pretrained(UpperCamelCase__ ) return student, e_layers_to_copy, d_layers_to_copy # Decide which layers of the teacher to copy. Not exactly alternating -- we try to keep first and last layer. if e_layers_to_copy is None: __lowerCamelCase = pick_layers_to_copy(UpperCamelCase__ , UpperCamelCase__ ) if d_layers_to_copy is None: __lowerCamelCase = pick_layers_to_copy(UpperCamelCase__ , UpperCamelCase__ ) try: if hasattr( UpperCamelCase__ , '''prophetnet''' ): # For ProphetNet, student.model.encoder.layers is called student.prophetnet.encoder.layers copy_layers(teacher.prophetnet.encoder.layers , student.prophetnet.encoder.layers , UpperCamelCase__ ) copy_layers(teacher.prophetnet.decoder.layers , student.prophetnet.decoder.layers , UpperCamelCase__ ) else: copy_layers(teacher.model.encoder.layers , student.model.encoder.layers , UpperCamelCase__ ) copy_layers(teacher.model.decoder.layers , student.model.decoder.layers , UpperCamelCase__ ) except AttributeError: # For t5, student.model.encoder.layers is called student.encoder.block copy_layers(teacher.encoder.block , student.encoder.block , UpperCamelCase__ ) copy_layers(teacher.decoder.block , student.decoder.block , UpperCamelCase__ ) logger.info( f"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to {save_path}""" ) __lowerCamelCase = { '''teacher_type''': teacher.config.model_type, '''copied_encoder_layers''': e_layers_to_copy, '''copied_decoder_layers''': d_layers_to_copy, } student.save_pretrained(UpperCamelCase__ ) # Save information about copying for easier reproducibility return student, e_layers_to_copy, d_layers_to_copy if __name__ == "__main__": fire.Fire(create_student_by_copying_alternating_layers)
67
1
'''simple docstring''' from google.protobuf import descriptor as _descriptor from google.protobuf import descriptor_pool as _descriptor_pool from google.protobuf import symbol_database as _symbol_database from google.protobuf.internal import builder as _builder # @@protoc_insertion_point(imports) __UpperCAmelCase =_symbol_database.Default() __UpperCAmelCase =_descriptor_pool.Default().AddSerializedFile( b"\n\x19sentencepiece_model.proto\x12\rsentencepiece\"\x80\x0c\n\x0bTrainerSpec\x12\r\n\x05input\x18\x01 \x03(\t\x12\x14\n\x0cinput_format\x18\x07 \x01(\t\x12\x14\n\x0cmodel_prefix\x18\x02 \x01(\t\x12\x41\n\nmodel_type\x18\x03 \x01(\x0e\x32$.sentencepiece.TrainerSpec.ModelType:\x07UNIGRAM\x12\x18\n\nvocab_size\x18\x04 \x01(\x05:\x04\x38\x30\x30\x30\x12\x17\n\x0f\x61\x63\x63\x65pt_language\x18\x05 \x03(\t\x12 \n\x15self_test_sample_size\x18\x06 \x01(\x05:\x01\x30\x12*\n\x1b\x65nable_differential_privacy\x18\x32 \x01(\x08:\x05\x66\x61lse\x12+\n differential_privacy_noise_level\x18\x33 \x01(\x02:\x01\x30\x12\x32\n\'differential_privacy_clipping_threshold\x18\x34 \x01(\x04:\x01\x30\x12\"\n\x12\x63haracter_coverage\x18\n \x01(\x02:\x06\x30.9995\x12\x1e\n\x13input_sentence_size\x18\x0b \x01(\x04:\x01\x30\x12$\n\x16shuffle_input_sentence\x18\x13 \x01(\x08:\x04true\x12 \n\x14mining_sentence_size\x18\x0c \x01(\x05\x42\x02\x18\x01\x12\"\n\x16training_sentence_size\x18\r \x01(\x05\x42\x02\x18\x01\x12(\n\x17seed_sentencepiece_size\x18\x0e \x01(\x05:\x07\x31\x30\x30\x30\x30\x30\x30\x12\x1e\n\x10shrinking_factor\x18\x0f \x01(\x02:\x04\x30.75\x12!\n\x13max_sentence_length\x18\x12 \x01(\x05:\x04\x34\x31\x39\x32\x12\x17\n\x0bnum_threads\x18\x10 \x01(\x05:\x02\x31\x36\x12\x1d\n\x12num_sub_iterations\x18\x11 \x01(\x05:\x01\x32\x12$\n\x18max_sentencepiece_length\x18\x14 \x01(\x05:\x02\x31\x36\x12%\n\x17split_by_unicode_script\x18\x15 \x01(\x08:\x04true\x12\x1d\n\x0fsplit_by_number\x18\x17 \x01(\x08:\x04true\x12!\n\x13split_by_whitespace\x18\x16 \x01(\x08:\x04true\x12)\n\x1atreat_whitespace_as_suffix\x18\x18 \x01(\x08:\x05\x66\x61lse\x12+\n\x1c\x61llow_whitespace_only_pieces\x18\x1a \x01(\x08:\x05\x66\x61lse\x12\x1b\n\x0csplit_digits\x18\x19 \x01(\x08:\x05\x66\x61lse\x12#\n\x19pretokenization_delimiter\x18\x35 \x01(\t:\x00\x12\x17\n\x0f\x63ontrol_symbols\x18\x1e \x03(\t\x12\x1c\n\x14user_defined_symbols\x18\x1f \x03(\t\x12\x16\n\x0erequired_chars\x18$ \x01(\t\x12\x1c\n\rbyte_fallback\x18# \x01(\x08:\x05\x66\x61lse\x12+\n\x1dvocabulary_output_piece_score\x18 \x01(\x08:\x04true\x12\x1e\n\x10hard_vocab_limit\x18! \x01(\x08:\x04true\x12\x1c\n\ruse_all_vocab\x18\" \x01(\x08:\x05\x66\x61lse\x12\x11\n\x06unk_id\x18( \x01(\x05:\x01\x30\x12\x11\n\x06\x62os_id\x18) \x01(\x05:\x01\x31\x12\x11\n\x06\x65os_id\x18* \x01(\x05:\x01\x32\x12\x12\n\x06pad_id\x18+ \x01(\x05:\x02-1\x12\x18\n\tunk_piece\x18- \x01(\t:\x05<unk>\x12\x16\n\tbos_piece\x18. \x01(\t:\x03<s>\x12\x17\n\teos_piece\x18/ \x01(\t:\x04</s>\x12\x18\n\tpad_piece\x18\x30 \x01(\t:\x05<pad>\x12\x1a\n\x0bunk_surface\x18, \x01(\t:\x05 \xe2\x81\x87 \x12+\n\x1ctrain_extremely_large_corpus\x18\x31 \x01(\x08:\x05\x66\x61lse\"5\n\tModelType\x12\x0b\n\x07UNIGRAM\x10\x01\x12\x07\n\x03\x42PE\x10\x02\x12\x08\n\x04WORD\x10\x03\x12\x08\n\x04\x43HAR\x10\x04*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\"\xd1\x01\n\x0eNormalizerSpec\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x1c\n\x14precompiled_charsmap\x18\x02 \x01(\x0c\x12\x1e\n\x10\x61\x64\x64_dummy_prefix\x18\x03 \x01(\x08:\x04true\x12&\n\x18remove_extra_whitespaces\x18\x04 \x01(\x08:\x04true\x12 \n\x12\x65scape_whitespaces\x18\x05 \x01(\x08:\x04true\x12\x1e\n\x16normalization_rule_tsv\x18\x06 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\"y\n\x0cSelfTestData\x12\x33\n\x07samples\x18\x01 \x03(\x0b\x32\".sentencepiece.SelfTestData.Sample\x1a)\n\x06Sample\x12\r\n\x05input\x18\x01 \x01(\t\x12\x10\n\x08\x65xpected\x18\x02 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\"\xfe\x03\n\nModelProto\x12\x37\n\x06pieces\x18\x01 \x03(\x0b\x32\'.sentencepiece.ModelProto.SentencePiece\x12\x30\n\x0ctrainer_spec\x18\x02 \x01(\x0b\x32\x1a.sentencepiece.TrainerSpec\x12\x36\n\x0fnormalizer_spec\x18\x03 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x12\x33\n\x0eself_test_data\x18\x04 \x01(\x0b\x32\x1b.sentencepiece.SelfTestData\x12\x38\n\x11\x64\x65normalizer_spec\x18\x05 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x1a\xd2\x01\n\rSentencePiece\x12\r\n\x05piece\x18\x01 \x01(\t\x12\r\n\x05score\x18\x02 \x01(\x02\x12\x42\n\x04type\x18\x03 \x01(\x0e\x32,.sentencepiece.ModelProto.SentencePiece.Type:\x06NORMAL\"T\n\x04Type\x12\n\n\x06NORMAL\x10\x01\x12\x0b\n\x07UNKNOWN\x10\x02\x12\x0b\n\x07\x43ONTROL\x10\x03\x12\x10\n\x0cUSER_DEFINED\x10\x04\x12\x08\n\x04\x42YTE\x10\x06\x12\n\n\x06UNUSED\x10\x05*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\x42\x02H\x03" ) __UpperCAmelCase =globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, "sentencepiece_model_pb2", _globals) if _descriptor._USE_C_DESCRIPTORS is False: __UpperCAmelCase =None __UpperCAmelCase =b"H\003" # (generated by protobuf compiler, but `_TRAINERSPEC` is not defined) # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._serialized_options = b"\030\001" # _TRAINERSPEC.fields_by_name["training_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["training_sentence_size"]._serialized_options = b"\030\001" __UpperCAmelCase =4_5 __UpperCAmelCase =1_5_8_1 __UpperCAmelCase =1_5_1_7 __UpperCAmelCase =1_5_7_0 __UpperCAmelCase =1_5_8_4 __UpperCAmelCase =1_7_9_3 __UpperCAmelCase =1_7_9_5 __UpperCAmelCase =1_9_1_6 __UpperCAmelCase =1_8_6_4 __UpperCAmelCase =1_9_0_5 __UpperCAmelCase =1_9_1_9 __UpperCAmelCase =2_4_2_9 __UpperCAmelCase =2_2_0_8 __UpperCAmelCase =2_4_1_8 __UpperCAmelCase =2_3_2_3 __UpperCAmelCase =2_4_0_7 # @@protoc_insertion_point(module_scope)
67
'''simple docstring''' import unittest from pathlib import Path from tempfile import TemporaryDirectory from transformers import AutoConfig, TFGPTaLMHeadModel, is_keras_nlp_available, is_tf_available from transformers.models.gpta.tokenization_gpta import GPTaTokenizer from transformers.testing_utils import require_keras_nlp, require_tf, slow if is_tf_available(): import tensorflow as tf if is_keras_nlp_available(): from transformers.models.gpta import TFGPTaTokenizer __UpperCAmelCase =["gpt2"] __UpperCAmelCase ="gpt2" if is_tf_available(): class a__ ( tf.Module ): def __init__( self : str , a : Union[str, Any] ): """simple docstring""" super().__init__() __lowerCamelCase = tokenizer __lowerCamelCase = AutoConfig.from_pretrained(a ) __lowerCamelCase = TFGPTaLMHeadModel.from_config(a ) @tf.function(input_signature=(tf.TensorSpec((None,) , tf.string , name='''text''' ),) ) def SCREAMING_SNAKE_CASE__ ( self : str , a : Tuple ): """simple docstring""" __lowerCamelCase = self.tokenizer(a ) __lowerCamelCase = tokenized['''input_ids'''].to_tensor() __lowerCamelCase = tf.cast(input_ids_dense > 0 , tf.intaa ) # input_mask = tf.reshape(input_mask, [-1, MAX_SEQ_LEN]) __lowerCamelCase = self.model(input_ids=a , attention_mask=a )['''logits'''] return outputs @require_tf @require_keras_nlp class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" super().setUp() __lowerCamelCase = [GPTaTokenizer.from_pretrained(a ) for checkpoint in (TOKENIZER_CHECKPOINTS)] __lowerCamelCase = [TFGPTaTokenizer.from_pretrained(a ) for checkpoint in TOKENIZER_CHECKPOINTS] assert len(self.tokenizers ) == len(self.tf_tokenizers ) __lowerCamelCase = [ '''This is a straightforward English test sentence.''', '''This one has some weird characters\rto\nsee\r\nif those\u00E9break things.''', '''Now we\'re going to add some Chinese: 一 二 三 一二三''', '''And some much more rare Chinese: 齉 堃 齉堃''', '''Je vais aussi écrire en français pour tester les accents''', '''Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ''', ] __lowerCamelCase = list(zip(self.test_sentences , self.test_sentences[::-1] ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" for tokenizer, tf_tokenizer in zip(self.tokenizers , self.tf_tokenizers ): for test_inputs in self.test_sentences: __lowerCamelCase = tokenizer([test_inputs] , return_tensors='''tf''' ) __lowerCamelCase = tf_tokenizer([test_inputs] ) for key in python_outputs.keys(): # convert them to numpy to avoid messing with ragged tensors __lowerCamelCase = python_outputs[key].numpy() __lowerCamelCase = tf_outputs[key].numpy() self.assertTrue(tf.reduce_all(python_outputs_values.shape == tf_outputs_values.shape ) ) self.assertTrue(tf.reduce_all(tf.cast(a , tf.intaa ) == tf_outputs_values ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: __lowerCamelCase = tf.function(a ) for test_inputs in self.test_sentences: __lowerCamelCase = tf.constant(a ) __lowerCamelCase = compiled_tokenizer(a ) __lowerCamelCase = tf_tokenizer(a ) for key in eager_outputs.keys(): self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key] ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: __lowerCamelCase = ModelToSave(tokenizer=a ) __lowerCamelCase = tf.convert_to_tensor([self.test_sentences[0]] ) __lowerCamelCase = model.serving(a ) # Build model with some sample inputs with TemporaryDirectory() as tempdir: __lowerCamelCase = Path(a ) / '''saved.model''' tf.saved_model.save(a , a , signatures={'''serving_default''': model.serving} ) __lowerCamelCase = tf.saved_model.load(a ) __lowerCamelCase = loaded_model.signatures['''serving_default'''](a )['''output_0'''] # We may see small differences because the loaded model is compiled, so we need an epsilon for the test self.assertTrue(tf.reduce_all(out == loaded_output ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: __lowerCamelCase = tf.convert_to_tensor([self.test_sentences[0]] ) __lowerCamelCase = tf_tokenizer(a ) # Build model with some sample inputs __lowerCamelCase = tf_tokenizer.get_config() __lowerCamelCase = TFGPTaTokenizer.from_config(a ) __lowerCamelCase = model_from_config(a ) for key in from_config_output.keys(): self.assertTrue(tf.reduce_all(from_config_output[key] == out[key] ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: # for the test to run __lowerCamelCase = 12_31_23 for max_length in [3, 5, 10_24]: __lowerCamelCase = tf.convert_to_tensor([self.test_sentences[0]] ) __lowerCamelCase = tf_tokenizer(a , max_length=a ) __lowerCamelCase = out['''input_ids'''].numpy().shape[1] assert out_length == max_length
67
1
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Union[str, Any]: global f # a global dp table for knapsack if f[i][j] < 0: if j < wt[i - 1]: __lowerCamelCase = mf_knapsack(i - 1 , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) else: __lowerCamelCase = max( mf_knapsack(i - 1 , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) , mf_knapsack(i - 1 , UpperCamelCase__ , UpperCamelCase__ , j - wt[i - 1] ) + val[i - 1] , ) __lowerCamelCase = val return f[i][j] def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> List[Any]: __lowerCamelCase = [[0] * (w + 1) for _ in range(n + 1 )] for i in range(1 , n + 1 ): for w_ in range(1 , w + 1 ): if wt[i - 1] <= w_: __lowerCamelCase = max(val[i - 1] + dp[i - 1][w_ - wt[i - 1]] , dp[i - 1][w_] ) else: __lowerCamelCase = dp[i - 1][w_] return dp[n][w_], dp def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> int: if not (isinstance(UpperCamelCase__ , (list, tuple) ) and isinstance(UpperCamelCase__ , (list, tuple) )): raise ValueError( '''Both the weights and values vectors must be either lists or tuples''' ) __lowerCamelCase = len(UpperCamelCase__ ) if num_items != len(UpperCamelCase__ ): __lowerCamelCase = ( '''The number of weights must be the same as the number of values.\n''' f"""But got {num_items} weights and {len(UpperCamelCase__ )} values""" ) raise ValueError(UpperCamelCase__ ) for i in range(UpperCamelCase__ ): if not isinstance(wt[i] , UpperCamelCase__ ): __lowerCamelCase = ( '''All weights must be integers but got weight of ''' f"""type {type(wt[i] )} at index {i}""" ) raise TypeError(UpperCamelCase__ ) __lowerCamelCase , __lowerCamelCase = knapsack(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) __lowerCamelCase = set() _construct_solution(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) return optimal_val, example_optional_set def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Union[str, Any]: # for the current item i at a maximum weight j to be part of an optimal subset, # the optimal value at (i, j) must be greater than the optimal value at (i-1, j). # where i - 1 means considering only the previous items at the given maximum weight if i > 0 and j > 0: if dp[i - 1][j] == dp[i][j]: _construct_solution(UpperCamelCase__ , UpperCamelCase__ , i - 1 , UpperCamelCase__ , UpperCamelCase__ ) else: optimal_set.add(UpperCamelCase__ ) _construct_solution(UpperCamelCase__ , UpperCamelCase__ , i - 1 , j - wt[i - 1] , UpperCamelCase__ ) if __name__ == "__main__": __UpperCAmelCase =[3, 2, 4, 4] __UpperCAmelCase =[4, 3, 2, 3] __UpperCAmelCase =4 __UpperCAmelCase =6 __UpperCAmelCase =[[0] * (w + 1)] + [[0] + [-1] * (w + 1) for _ in range(n + 1)] __UpperCAmelCase , __UpperCAmelCase =knapsack(w, wt, val, n) print(optimal_solution) print(mf_knapsack(n, wt, val, w)) # switched the n and w # testing the dynamic programming problem with example # the optimal subset for the above example are items 3 and 4 __UpperCAmelCase , __UpperCAmelCase =knapsack_with_example_solution(w, wt, val) assert optimal_solution == 8 assert optimal_subset == {3, 4} print("optimal_value = ", optimal_solution) print("An optimal subset corresponding to the optimal value", optimal_subset)
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Optional[int]: if b == 0: return 1 if (b % 2) == 0: return actual_power(UpperCamelCase__ , int(b / 2 ) ) * actual_power(UpperCamelCase__ , int(b / 2 ) ) else: return a * actual_power(UpperCamelCase__ , int(b / 2 ) ) * actual_power(UpperCamelCase__ , int(b / 2 ) ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> float: if b < 0: return 1 / actual_power(UpperCamelCase__ , UpperCamelCase__ ) return actual_power(UpperCamelCase__ , UpperCamelCase__ ) if __name__ == "__main__": print(power(-2, -3))
67
1
'''simple docstring''' import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class a__ : @property def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" return self.get_dummy_input() @property def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(f"""'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'.""" ) def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : Any=True , a : Any=False , a : List[Any]=False , a : List[Any]=False , ): """simple docstring""" __lowerCamelCase = 4 __lowerCamelCase = 32 __lowerCamelCase = (32, 32) __lowerCamelCase = torch.manual_seed(0 ) __lowerCamelCase = torch.device(a ) __lowerCamelCase = (batch_size, num_channels) + sizes __lowerCamelCase = randn_tensor(a , generator=a , device=a ) __lowerCamelCase = {'''hidden_states''': hidden_states} if include_temb: __lowerCamelCase = 1_28 __lowerCamelCase = randn_tensor((batch_size, temb_channels) , generator=a , device=a ) if include_res_hidden_states_tuple: __lowerCamelCase = torch.manual_seed(1 ) __lowerCamelCase = (randn_tensor(a , generator=a , device=a ),) if include_encoder_hidden_states: __lowerCamelCase = floats_tensor((batch_size, 32, 32) ).to(a ) if include_skip_sample: __lowerCamelCase = randn_tensor(((batch_size, 3) + sizes) , generator=a , device=a ) return dummy_input def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" __lowerCamelCase = { '''in_channels''': 32, '''out_channels''': 32, '''temb_channels''': 1_28, } if self.block_type == "up": __lowerCamelCase = 32 if self.block_type == "mid": init_dict.pop('''out_channels''' ) __lowerCamelCase = self.dummy_input return init_dict, inputs_dict def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : str ): """simple docstring""" __lowerCamelCase , __lowerCamelCase = self.prepare_init_args_and_inputs_for_common() __lowerCamelCase = self.block_class(**a ) unet_block.to(a ) unet_block.eval() with torch.no_grad(): __lowerCamelCase = unet_block(**a ) if isinstance(a , a ): __lowerCamelCase = output[0] self.assertEqual(output.shape , self.output_shape ) __lowerCamelCase = output[0, -1, -3:, -3:] __lowerCamelCase = torch.tensor(a ).to(a ) assert torch_all_close(output_slice.flatten() , a , atol=5e-3 ) @unittest.skipIf(torch_device == '''mps''' , '''Training is not supported in mps''' ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase , __lowerCamelCase = self.prepare_init_args_and_inputs_for_common() __lowerCamelCase = self.block_class(**a ) model.to(a ) model.train() __lowerCamelCase = model(**a ) if isinstance(a , a ): __lowerCamelCase = output[0] __lowerCamelCase = torch.device(a ) __lowerCamelCase = randn_tensor(output.shape , device=a ) __lowerCamelCase = torch.nn.functional.mse_loss(a , a ) loss.backward()
67
'''simple docstring''' import logging import os from .state import PartialState class a__ ( logging.LoggerAdapter ): @staticmethod def SCREAMING_SNAKE_CASE__ ( a : Optional[Any] ): """simple docstring""" __lowerCamelCase = PartialState() return not main_process_only or (main_process_only and state.is_main_process) def SCREAMING_SNAKE_CASE__ ( self : int , a : Optional[int] , a : str , *a : Optional[int] , **a : List[Any] ): """simple docstring""" if PartialState._shared_state == {}: raise RuntimeError( '''You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.''' ) __lowerCamelCase = kwargs.pop('''main_process_only''' , a ) __lowerCamelCase = kwargs.pop('''in_order''' , a ) if self.isEnabledFor(a ): if self._should_log(a ): __lowerCamelCase , __lowerCamelCase = self.process(a , a ) self.logger.log(a , a , *a , **a ) elif in_order: __lowerCamelCase = PartialState() for i in range(state.num_processes ): if i == state.process_index: __lowerCamelCase , __lowerCamelCase = self.process(a , a ) self.logger.log(a , a , *a , **a ) state.wait_for_everyone() def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ = None ) -> Optional[int]: if log_level is None: __lowerCamelCase = os.environ.get('''ACCELERATE_LOG_LEVEL''' , UpperCamelCase__ ) __lowerCamelCase = logging.getLogger(UpperCamelCase__ ) if log_level is not None: logger.setLevel(log_level.upper() ) logger.root.setLevel(log_level.upper() ) return MultiProcessAdapter(UpperCamelCase__ , {} )
67
1
'''simple docstring''' from collections import OrderedDict from typing import List, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={ "google/efficientnet-b7": "https://huggingface.co/google/efficientnet-b7/resolve/main/config.json", } class a__ ( UpperCAmelCase__ ): lowerCamelCase : str ="efficientnet" def __init__( self : Any , a : int = 3 , a : int = 6_00 , a : float = 2.0 , a : float = 3.1 , a : int = 8 , a : List[int] = [3, 3, 5, 3, 5, 5, 3] , a : List[int] = [32, 16, 24, 40, 80, 1_12, 1_92] , a : List[int] = [16, 24, 40, 80, 1_12, 1_92, 3_20] , a : List[int] = [] , a : List[int] = [1, 2, 2, 2, 1, 2, 1] , a : List[int] = [1, 2, 2, 3, 3, 4, 1] , a : List[int] = [1, 6, 6, 6, 6, 6, 6] , a : float = 0.25 , a : str = "swish" , a : int = 25_60 , a : str = "mean" , a : float = 0.02 , a : float = 0.0_01 , a : float = 0.99 , a : float = 0.5 , a : float = 0.2 , **a : Union[str, Any] , ): """simple docstring""" super().__init__(**a ) __lowerCamelCase = num_channels __lowerCamelCase = image_size __lowerCamelCase = width_coefficient __lowerCamelCase = depth_coefficient __lowerCamelCase = depth_divisor __lowerCamelCase = kernel_sizes __lowerCamelCase = in_channels __lowerCamelCase = out_channels __lowerCamelCase = depthwise_padding __lowerCamelCase = strides __lowerCamelCase = num_block_repeats __lowerCamelCase = expand_ratios __lowerCamelCase = squeeze_expansion_ratio __lowerCamelCase = hidden_act __lowerCamelCase = hidden_dim __lowerCamelCase = pooling_type __lowerCamelCase = initializer_range __lowerCamelCase = batch_norm_eps __lowerCamelCase = batch_norm_momentum __lowerCamelCase = dropout_rate __lowerCamelCase = drop_connect_rate __lowerCamelCase = sum(a ) * 4 class a__ ( UpperCAmelCase__ ): lowerCamelCase : int =version.parse("1.11" ) @property def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" return 1e-5
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[Any]: __lowerCamelCase = [] __lowerCamelCase = set({'''(''', '''[''', '''{'''} ) __lowerCamelCase = set({''')''', ''']''', '''}'''} ) __lowerCamelCase = {'''{''': '''}''', '''[''': ''']''', '''(''': ''')'''} for i in range(len(UpperCamelCase__ ) ): if s[i] in open_brackets: stack.append(s[i] ) elif s[i] in closed_brackets and ( len(UpperCamelCase__ ) == 0 or (len(UpperCamelCase__ ) > 0 and open_to_closed[stack.pop()] != s[i]) ): return False return len(UpperCamelCase__ ) == 0 def __lowerCAmelCase ( ) -> str: __lowerCamelCase = input('''Enter sequence of brackets: ''' ) if is_balanced(UpperCamelCase__ ): print(UpperCamelCase__ , '''is balanced''' ) else: print(UpperCamelCase__ , '''is not balanced''' ) if __name__ == "__main__": main()
67
1
'''simple docstring''' class a__ : def __init__( self : List[Any] , a : Tuple ): """simple docstring""" __lowerCamelCase = arr.split(''',''' ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = [int(self.array[0] )] * len(self.array ) __lowerCamelCase = [int(self.array[0] )] * len(self.array ) for i in range(1 , len(self.array ) ): __lowerCamelCase = max( int(self.array[i] ) + sum_value[i - 1] , int(self.array[i] ) ) __lowerCamelCase = max(sum_value[i] , rear[i - 1] ) return rear[len(self.array ) - 1] if __name__ == "__main__": __UpperCAmelCase =input("please input some numbers:") __UpperCAmelCase =SubArray(whole_array) __UpperCAmelCase =array.solve_sub_array() print(("the results is:", re))
67
'''simple docstring''' import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class a__ ( UpperCAmelCase__ ): lowerCamelCase : Dict ="M-CLIP" def __init__( self : Tuple , a : Optional[int]=10_24 , a : Tuple=7_68 , **a : List[str] ): """simple docstring""" __lowerCamelCase = transformerDimSize __lowerCamelCase = imageDimSize super().__init__(**a ) class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[Any] =MCLIPConfig def __init__( self : str , a : List[Any] , *a : Dict , **a : str ): """simple docstring""" super().__init__(a , *a , **a ) __lowerCamelCase = XLMRobertaModel(a ) __lowerCamelCase = torch.nn.Linear( in_features=config.transformerDimensions , out_features=config.numDims ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : int , a : List[Any] ): """simple docstring""" __lowerCamelCase = self.transformer(input_ids=a , attention_mask=a )[0] __lowerCamelCase = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(a ), embs
67
1
'''simple docstring''' import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] =["image_processor", "tokenizer"] lowerCamelCase : Union[str, Any] ="LayoutLMv2ImageProcessor" lowerCamelCase : int =("LayoutXLMTokenizer", "LayoutXLMTokenizerFast") def __init__( self : Optional[int] , a : Any=None , a : Any=None , **a : Union[str, Any] ): """simple docstring""" if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , a , ) __lowerCamelCase = kwargs.pop('''feature_extractor''' ) __lowerCamelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(a , a ) def __call__( self : Tuple , a : Optional[int] , a : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , a : Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None , a : Union[List[List[int]], List[List[List[int]]]] = None , a : Optional[Union[List[int], List[List[int]]]] = None , a : bool = True , a : Union[bool, str, PaddingStrategy] = False , a : Union[bool, str, TruncationStrategy] = None , a : Optional[int] = None , a : int = 0 , a : Optional[int] = None , a : Optional[bool] = None , a : Optional[bool] = None , a : bool = False , a : bool = False , a : bool = False , a : bool = False , a : bool = True , a : Optional[Union[str, TensorType]] = None , **a : Tuple , ): """simple docstring""" if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( '''You cannot provide bounding boxes ''' '''if you initialized the image processor with apply_ocr set to True.''' ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.''' ) if return_overflowing_tokens is True and return_offsets_mapping is False: raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.''' ) # first, apply the image processor __lowerCamelCase = self.image_processor(images=a , return_tensors=a ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(a , a ): __lowerCamelCase = [text] # add batch dimension (as the image processor always adds a batch dimension) __lowerCamelCase = features['''words'''] __lowerCamelCase = self.tokenizer( text=text if text is not None else features['''words'''] , text_pair=text_pair if text_pair is not None else None , boxes=boxes if boxes is not None else features['''boxes'''] , word_labels=a , add_special_tokens=a , padding=a , truncation=a , max_length=a , stride=a , pad_to_multiple_of=a , return_token_type_ids=a , return_attention_mask=a , return_overflowing_tokens=a , return_special_tokens_mask=a , return_offsets_mapping=a , return_length=a , verbose=a , return_tensors=a , **a , ) # add pixel values __lowerCamelCase = features.pop('''pixel_values''' ) if return_overflowing_tokens is True: __lowerCamelCase = self.get_overflowing_images(a , encoded_inputs['''overflow_to_sample_mapping'''] ) __lowerCamelCase = images return encoded_inputs def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : Optional[Any] , a : str ): """simple docstring""" __lowerCamelCase = [] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(a ) != len(a ): raise ValueError( '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got''' f""" {len(a )} and {len(a )}""" ) return images_with_overflow def SCREAMING_SNAKE_CASE__ ( self : List[str] , *a : Optional[Any] , **a : Union[str, Any] ): """simple docstring""" return self.tokenizer.batch_decode(*a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , *a : Union[str, Any] , **a : Tuple ): """simple docstring""" return self.tokenizer.decode(*a , **a ) @property def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" return ["input_ids", "bbox", "attention_mask", "image"] @property def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , a , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , a , ) return self.image_processor
67
'''simple docstring''' from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
67
1
'''simple docstring''' import gc import random import unittest import torch from diffusers import ( IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ) from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference from . import IFPipelineTesterMixin @skip_mps class a__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): lowerCamelCase : Union[str, Any] =IFPipeline lowerCamelCase : str =TEXT_TO_IMAGE_PARAMS - {"width", "height", "latents"} lowerCamelCase : int =TEXT_TO_IMAGE_BATCH_PARAMS lowerCamelCase : List[str] =PipelineTesterMixin.required_optional_params - {"latents"} def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" return self._get_dummy_components() def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : Union[str, Any] , a : List[str]=0 ): """simple docstring""" if str(a ).startswith('''mps''' ): __lowerCamelCase = torch.manual_seed(a ) else: __lowerCamelCase = torch.Generator(device=a ).manual_seed(a ) __lowerCamelCase = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" super().test_save_load_floataa(expected_max_diff=1e-1 ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" self._test_save_load_local() def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1e-2 , ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) @slow @require_torch_gpu class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" __lowerCamelCase = IFPipeline.from_pretrained('''DeepFloyd/IF-I-XL-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa ) __lowerCamelCase = IFSuperResolutionPipeline.from_pretrained( '''DeepFloyd/IF-II-L-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa , text_encoder=a , tokenizer=a ) # pre compute text embeddings and remove T5 to save memory pipe_a.text_encoder.to('''cuda''' ) __lowerCamelCase , __lowerCamelCase = pipe_a.encode_prompt('''anime turtle''' , device='''cuda''' ) del pipe_a.tokenizer del pipe_a.text_encoder gc.collect() __lowerCamelCase = None __lowerCamelCase = None pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if(a , a , a , a ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # img2img __lowerCamelCase = IFImgaImgPipeline(**pipe_a.components ) __lowerCamelCase = IFImgaImgSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_imgaimg(a , a , a , a ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # inpainting __lowerCamelCase = IFInpaintingPipeline(**pipe_a.components ) __lowerCamelCase = IFInpaintingSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_inpainting(a , a , a , a ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : Tuple , a : str , a : Tuple , a : List[str] ): """simple docstring""" _start_torch_memory_measurement() __lowerCamelCase = torch.Generator(device='''cpu''' ).manual_seed(0 ) __lowerCamelCase = pipe_a( prompt_embeds=a , negative_prompt_embeds=a , num_inference_steps=2 , generator=a , output_type='''np''' , ) __lowerCamelCase = output.images[0] assert image.shape == (64, 64, 3) __lowerCamelCase = torch.cuda.max_memory_allocated() assert mem_bytes < 13 * 10**9 __lowerCamelCase = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy''' ) assert_mean_pixel_difference(a , a ) # pipeline 2 _start_torch_memory_measurement() __lowerCamelCase = torch.Generator(device='''cpu''' ).manual_seed(0 ) __lowerCamelCase = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(a ) __lowerCamelCase = pipe_a( prompt_embeds=a , negative_prompt_embeds=a , image=a , generator=a , num_inference_steps=2 , output_type='''np''' , ) __lowerCamelCase = output.images[0] assert image.shape == (2_56, 2_56, 3) __lowerCamelCase = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 __lowerCamelCase = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(a , a ) def SCREAMING_SNAKE_CASE__ ( self : str , a : Optional[Any] , a : Union[str, Any] , a : Any , a : int ): """simple docstring""" _start_torch_memory_measurement() __lowerCamelCase = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(a ) __lowerCamelCase = torch.Generator(device='''cpu''' ).manual_seed(0 ) __lowerCamelCase = pipe_a( prompt_embeds=a , negative_prompt_embeds=a , image=a , num_inference_steps=2 , generator=a , output_type='''np''' , ) __lowerCamelCase = output.images[0] assert image.shape == (64, 64, 3) __lowerCamelCase = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 __lowerCamelCase = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy''' ) assert_mean_pixel_difference(a , a ) # pipeline 2 _start_torch_memory_measurement() __lowerCamelCase = torch.Generator(device='''cpu''' ).manual_seed(0 ) __lowerCamelCase = floats_tensor((1, 3, 2_56, 2_56) , rng=random.Random(0 ) ).to(a ) __lowerCamelCase = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(a ) __lowerCamelCase = pipe_a( prompt_embeds=a , negative_prompt_embeds=a , image=a , original_image=a , generator=a , num_inference_steps=2 , output_type='''np''' , ) __lowerCamelCase = output.images[0] assert image.shape == (2_56, 2_56, 3) __lowerCamelCase = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 __lowerCamelCase = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(a , a ) def SCREAMING_SNAKE_CASE__ ( self : str , a : Optional[int] , a : Union[str, Any] , a : List[str] , a : Tuple ): """simple docstring""" _start_torch_memory_measurement() __lowerCamelCase = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(a ) __lowerCamelCase = floats_tensor((1, 3, 64, 64) , rng=random.Random(1 ) ).to(a ) __lowerCamelCase = torch.Generator(device='''cpu''' ).manual_seed(0 ) __lowerCamelCase = pipe_a( prompt_embeds=a , negative_prompt_embeds=a , image=a , mask_image=a , num_inference_steps=2 , generator=a , output_type='''np''' , ) __lowerCamelCase = output.images[0] assert image.shape == (64, 64, 3) __lowerCamelCase = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 __lowerCamelCase = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy''' ) assert_mean_pixel_difference(a , a ) # pipeline 2 _start_torch_memory_measurement() __lowerCamelCase = torch.Generator(device='''cpu''' ).manual_seed(0 ) __lowerCamelCase = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(a ) __lowerCamelCase = floats_tensor((1, 3, 2_56, 2_56) , rng=random.Random(0 ) ).to(a ) __lowerCamelCase = floats_tensor((1, 3, 2_56, 2_56) , rng=random.Random(1 ) ).to(a ) __lowerCamelCase = pipe_a( prompt_embeds=a , negative_prompt_embeds=a , image=a , mask_image=a , original_image=a , generator=a , num_inference_steps=2 , output_type='''np''' , ) __lowerCamelCase = output.images[0] assert image.shape == (2_56, 2_56, 3) __lowerCamelCase = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 __lowerCamelCase = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(a , a ) def __lowerCAmelCase ( ) -> Union[str, Any]: torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats()
67
'''simple docstring''' from __future__ import annotations from decimal import Decimal from numpy import array def __lowerCAmelCase ( UpperCamelCase__ ) -> list[list[float]]: __lowerCamelCase = Decimal # Check if the provided matrix has 2 rows and 2 columns # since this implementation only works for 2x2 matrices if len(UpperCamelCase__ ) == 2 and len(matrix[0] ) == 2 and len(matrix[1] ) == 2: # Calculate the determinant of the matrix __lowerCamelCase = float( d(matrix[0][0] ) * d(matrix[1][1] ) - d(matrix[1][0] ) * d(matrix[0][1] ) ) if determinant == 0: raise ValueError('''This matrix has no inverse.''' ) # Creates a copy of the matrix with swapped positions of the elements __lowerCamelCase = [[0.0, 0.0], [0.0, 0.0]] __lowerCamelCase , __lowerCamelCase = matrix[1][1], matrix[0][0] __lowerCamelCase , __lowerCamelCase = -matrix[1][0], -matrix[0][1] # Calculate the inverse of the matrix return [ [(float(d(UpperCamelCase__ ) ) / determinant) or 0.0 for n in row] for row in swapped_matrix ] elif ( len(UpperCamelCase__ ) == 3 and len(matrix[0] ) == 3 and len(matrix[1] ) == 3 and len(matrix[2] ) == 3 ): # Calculate the determinant of the matrix using Sarrus rule __lowerCamelCase = float( ( (d(matrix[0][0] ) * d(matrix[1][1] ) * d(matrix[2][2] )) + (d(matrix[0][1] ) * d(matrix[1][2] ) * d(matrix[2][0] )) + (d(matrix[0][2] ) * d(matrix[1][0] ) * d(matrix[2][1] )) ) - ( (d(matrix[0][2] ) * d(matrix[1][1] ) * d(matrix[2][0] )) + (d(matrix[0][1] ) * d(matrix[1][0] ) * d(matrix[2][2] )) + (d(matrix[0][0] ) * d(matrix[1][2] ) * d(matrix[2][1] )) ) ) if determinant == 0: raise ValueError('''This matrix has no inverse.''' ) # Creating cofactor matrix __lowerCamelCase = [ [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], ] __lowerCamelCase = (d(matrix[1][1] ) * d(matrix[2][2] )) - ( d(matrix[1][2] ) * d(matrix[2][1] ) ) __lowerCamelCase = -( (d(matrix[1][0] ) * d(matrix[2][2] )) - (d(matrix[1][2] ) * d(matrix[2][0] )) ) __lowerCamelCase = (d(matrix[1][0] ) * d(matrix[2][1] )) - ( d(matrix[1][1] ) * d(matrix[2][0] ) ) __lowerCamelCase = -( (d(matrix[0][1] ) * d(matrix[2][2] )) - (d(matrix[0][2] ) * d(matrix[2][1] )) ) __lowerCamelCase = (d(matrix[0][0] ) * d(matrix[2][2] )) - ( d(matrix[0][2] ) * d(matrix[2][0] ) ) __lowerCamelCase = -( (d(matrix[0][0] ) * d(matrix[2][1] )) - (d(matrix[0][1] ) * d(matrix[2][0] )) ) __lowerCamelCase = (d(matrix[0][1] ) * d(matrix[1][2] )) - ( d(matrix[0][2] ) * d(matrix[1][1] ) ) __lowerCamelCase = -( (d(matrix[0][0] ) * d(matrix[1][2] )) - (d(matrix[0][2] ) * d(matrix[1][0] )) ) __lowerCamelCase = (d(matrix[0][0] ) * d(matrix[1][1] )) - ( d(matrix[0][1] ) * d(matrix[1][0] ) ) # Transpose the cofactor matrix (Adjoint matrix) __lowerCamelCase = array(UpperCamelCase__ ) for i in range(3 ): for j in range(3 ): __lowerCamelCase = cofactor_matrix[j][i] # Inverse of the matrix using the formula (1/determinant) * adjoint matrix __lowerCamelCase = array(UpperCamelCase__ ) for i in range(3 ): for j in range(3 ): inverse_matrix[i][j] /= d(UpperCamelCase__ ) # Calculate the inverse of the matrix return [[float(d(UpperCamelCase__ ) ) or 0.0 for n in row] for row in inverse_matrix] raise ValueError('''Please provide a matrix of size 2x2 or 3x3.''' )
67
1
'''simple docstring''' import json import os from typing import Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={ "vocab_file": "vocab.json", "tokenizer_config_file": "tokenizer_config.json", "merges_file": "merges.txt", } __UpperCAmelCase ={ "vocab_file": { "facebook/s2t-wav2vec2-large-en-de": ( "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/vocab.json" ), }, "tokenizer_config_file": { "facebook/s2t-wav2vec2-large-en-de": ( "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/tokenizer_config.json" ), }, "merges_file": { "facebook/s2t-wav2vec2-large-en-de": ( "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/merges.txt" ), }, } __UpperCAmelCase ="</w>" __UpperCAmelCase ="@@ " def __lowerCAmelCase ( UpperCamelCase__ ) -> str: __lowerCamelCase = set() __lowerCamelCase = word[0] for char in word[1:]: pairs.add((prev_char, char) ) __lowerCamelCase = char return pairs # Speech2Text2 has no max input length __UpperCAmelCase ={"facebook/s2t-wav2vec2-large-en-de": 1_0_2_4} class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] =VOCAB_FILES_NAMES lowerCamelCase : Optional[int] =PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : Any =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Dict =["input_ids", "attention_mask"] def __init__( self : int , a : List[str] , a : int="<s>" , a : Any="<pad>" , a : Union[str, Any]="</s>" , a : Optional[Any]="<unk>" , a : List[Any]=False , a : Tuple=None , **a : str , ): """simple docstring""" super().__init__( unk_token=a , bos_token=a , eos_token=a , pad_token=a , do_lower_case=a , **a , ) __lowerCamelCase = do_lower_case with open(a , encoding='''utf-8''' ) as vocab_handle: __lowerCamelCase = json.load(a ) __lowerCamelCase = {v: k for k, v in self.encoder.items()} if merges_file is None: logger.info(f"""No merges files provided. {self.__class__.__name__} can only be used for decoding.""" ) __lowerCamelCase = None __lowerCamelCase = None else: with open(a , encoding='''utf-8''' ) as merges_handle: __lowerCamelCase = merges_handle.read().split('''\n''' )[:-1] __lowerCamelCase = [tuple(merge.split()[:2] ) for merge in merges] __lowerCamelCase = dict(zip(a , range(len(a ) ) ) ) __lowerCamelCase = {} @property def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" return len(self.decoder ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" return dict(self.encoder , **self.added_tokens_encoder ) def SCREAMING_SNAKE_CASE__ ( self : Any , a : List[str] ): """simple docstring""" __lowerCamelCase = tuple(token[:-1] ) + (token[-1] + BPE_TOKEN_MERGES,) if token in self.cache: return self.cache[token] __lowerCamelCase = get_pairs(a ) if not pairs: return token while True: __lowerCamelCase = min(a , key=lambda a : self.bpe_ranks.get(a , float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break __lowerCamelCase , __lowerCamelCase = bigram __lowerCamelCase = [] __lowerCamelCase = 0 while i < len(a ): try: __lowerCamelCase = word.index(a , a ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) __lowerCamelCase = j if word[i] == first and i < len(a ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 __lowerCamelCase = tuple(a ) __lowerCamelCase = new_word if len(a ) == 1: break else: __lowerCamelCase = get_pairs(a ) __lowerCamelCase = ''' '''.join(a ) if word == "\n " + BPE_TOKEN_MERGES: __lowerCamelCase = '''\n''' + BPE_TOKEN_MERGES if word.endswith(a ): __lowerCamelCase = word.replace(a , '''''' ) __lowerCamelCase = word.replace(''' ''' , a ) __lowerCamelCase = word return word def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : Any ): """simple docstring""" if self.bpe_ranks is None: raise ValueError( '''This tokenizer was instantiated without a `merges.txt` file, so''' ''' that it can only be used for decoding, not for encoding.''' '''Make sure to provide `merges.txt` file at instantiation to enable ''' '''encoding.''' ) if self.do_lower_case: __lowerCamelCase = text.lower() __lowerCamelCase = text.split() __lowerCamelCase = [] for token in text: if token: split_tokens.extend(list(self.bpe(a ).split(''' ''' ) ) ) return split_tokens def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : str ): """simple docstring""" return self.encoder.get(a , self.encoder.get(self.unk_token ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : int ): """simple docstring""" __lowerCamelCase = self.decoder.get(a , self.unk_token ) return result def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : List[str] ): """simple docstring""" __lowerCamelCase = ''' '''.join(a ) # make sure @@ tokens are concatenated __lowerCamelCase = ''''''.join(string.split(a ) ) return string def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : str , a : Optional[str] = None ): """simple docstring""" if not os.path.isdir(a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return __lowerCamelCase = os.path.join( a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) __lowerCamelCase = os.path.join( a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file'''] ) with open(a , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=a , ensure_ascii=a ) + '''\n''' ) __lowerCamelCase = 0 if self.bpe_ranks is None: return (vocab_file,) with open(a , '''w''' , encoding='''utf-8''' ) as writer: for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda a : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {merges_file}: BPE merge indices are not consecutive.""" ''' Please check that the tokenizer is not corrupted!''' ) __lowerCamelCase = token_index writer.write(''' '''.join(a ) + '''\n''' ) index += 1 return (vocab_file, merges_file)
67
'''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 __UpperCAmelCase =logging.get_logger(__name__) def __lowerCAmelCase ( UpperCamelCase__=None , UpperCamelCase__=None ) -> int: return field(default_factory=lambda: default , metadata=UpperCamelCase__ ) @dataclass class a__ : lowerCamelCase : List[str] =list_field( default=[] , metadata={ "help": ( "Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version" " of all available models" ) } , ) lowerCamelCase : List[int] =list_field( default=[8] , metadata={"help": "List of batch sizes for which memory and time performance will be evaluated"} ) lowerCamelCase : List[int] =list_field( default=[8, 3_2, 1_2_8, 5_1_2] , metadata={"help": "List of sequence lengths for which memory and time performance will be evaluated"} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to benchmark inference of model. Inference can be disabled via --no-inference."} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to run on available cuda devices. Cuda can be disabled via --no-cuda."} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to run on available tpu devices. TPU can be disabled via --no-tpu."} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Use FP16 to accelerate inference."} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Benchmark training of model"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Verbose memory tracing"} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Whether to perform speed measurements. Speed measurements can be disabled via --no-speed."} , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": "Whether to perform memory measurements. Memory measurements can be disabled via --no-memory" } , ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Trace memory line by line"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Save result to a CSV file"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Save all print statements in a log file"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Whether to print environment information"} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": ( "Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use" " multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled" " for debugging / testing and on TPU." ) } , ) lowerCamelCase : str =field( default=F'''inference_time_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving time results to csv."} , ) lowerCamelCase : str =field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving memory results to csv."} , ) lowerCamelCase : str =field( default=F'''train_time_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving time results to csv for training."} , ) lowerCamelCase : str =field( default=F'''train_memory_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving memory results to csv for training."} , ) lowerCamelCase : str =field( default=F'''env_info_{round(time() )}.csv''' , metadata={"help": "CSV filename used if saving environment information."} , ) lowerCamelCase : str =field( default=F'''log_{round(time() )}.csv''' , metadata={"help": "Log filename used if print statements are saved in log."} , ) lowerCamelCase : int =field(default=3 , metadata={"help": "Times an experiment will be run."} ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={ "help": ( "Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain" " model weights." ) } , ) def SCREAMING_SNAKE_CASE__ ( self : 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.''' , a , ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """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 SCREAMING_SNAKE_CASE__ ( 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
67
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={ "camembert-base": "https://huggingface.co/camembert-base/resolve/main/config.json", "umberto-commoncrawl-cased-v1": ( "https://huggingface.co/Musixmatch/umberto-commoncrawl-cased-v1/resolve/main/config.json" ), "umberto-wikipedia-uncased-v1": ( "https://huggingface.co/Musixmatch/umberto-wikipedia-uncased-v1/resolve/main/config.json" ), } class a__ ( UpperCAmelCase__ ): lowerCamelCase : Union[str, Any] ="camembert" def __init__( self : int , a : Dict=3_05_22 , a : List[Any]=7_68 , a : int=12 , a : Union[str, Any]=12 , a : int=30_72 , a : Any="gelu" , a : Optional[Any]=0.1 , a : Any=0.1 , a : List[Any]=5_12 , a : List[str]=2 , a : Union[str, Any]=0.02 , a : List[Any]=1e-1_2 , a : Any=1 , a : List[str]=0 , a : Optional[int]=2 , a : Union[str, Any]="absolute" , a : Optional[int]=True , a : List[str]=None , **a : Any , ): """simple docstring""" super().__init__(pad_token_id=a , bos_token_id=a , eos_token_id=a , **a ) __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 = type_vocab_size __lowerCamelCase = initializer_range __lowerCamelCase = layer_norm_eps __lowerCamelCase = position_embedding_type __lowerCamelCase = use_cache __lowerCamelCase = classifier_dropout class a__ ( UpperCAmelCase__ ): @property def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" if self.task == "multiple-choice": __lowerCamelCase = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: __lowerCamelCase = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
67
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_barthez import BarthezTokenizer else: __UpperCAmelCase =None __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase ={ "vocab_file": { "moussaKam/mbarthez": "https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model", "moussaKam/barthez": "https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model", "moussaKam/barthez-orangesum-title": ( "https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "moussaKam/mbarthez": "https://huggingface.co/moussaKam/mbarthez/resolve/main/tokenizer.json", "moussaKam/barthez": "https://huggingface.co/moussaKam/barthez/resolve/main/tokenizer.json", "moussaKam/barthez-orangesum-title": ( "https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/tokenizer.json" ), }, } __UpperCAmelCase ={ "moussaKam/mbarthez": 1_0_2_4, "moussaKam/barthez": 1_0_2_4, "moussaKam/barthez-orangesum-title": 1_0_2_4, } __UpperCAmelCase ="▁" class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] =VOCAB_FILES_NAMES lowerCamelCase : Optional[Any] =PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : int =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[Any] =["input_ids", "attention_mask"] lowerCamelCase : Union[str, Any] =BarthezTokenizer def __init__( self : Optional[Any] , a : Dict=None , a : str=None , a : List[Any]="<s>" , a : Optional[int]="</s>" , a : List[str]="</s>" , a : Tuple="<s>" , a : str="<unk>" , a : Any="<pad>" , a : Union[str, Any]="<mask>" , **a : Union[str, Any] , ): """simple docstring""" __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else mask_token super().__init__( a , tokenizer_file=a , bos_token=a , eos_token=a , unk_token=a , sep_token=a , cls_token=a , pad_token=a , mask_token=a , **a , ) __lowerCamelCase = vocab_file __lowerCamelCase = False if not self.vocab_file else True def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __lowerCamelCase = [self.cls_token_id] __lowerCamelCase = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" __lowerCamelCase = [self.sep_token_id] __lowerCamelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : str , a : Optional[str] = None ): """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return __lowerCamelCase = os.path.join( a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(a ): copyfile(self.vocab_file , a ) return (out_vocab_file,)
67
1
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> int: return int((input_a, input_a).count(0 ) != 0 ) def __lowerCAmelCase ( ) -> None: assert nand_gate(0 , 0 ) == 1 assert nand_gate(0 , 1 ) == 1 assert nand_gate(1 , 0 ) == 1 assert nand_gate(1 , 1 ) == 0 if __name__ == "__main__": print(nand_gate(0, 0)) print(nand_gate(0, 1)) print(nand_gate(1, 0)) print(nand_gate(1, 1))
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ = 1_00_00_00 ) -> int: __lowerCamelCase = set(range(3 , UpperCamelCase__ , 2 ) ) primes.add(2 ) for p in range(3 , UpperCamelCase__ , 2 ): if p not in primes: continue primes.difference_update(set(range(p * p , UpperCamelCase__ , UpperCamelCase__ ) ) ) __lowerCamelCase = [float(UpperCamelCase__ ) for n in range(limit + 1 )] for p in primes: for n in range(UpperCamelCase__ , limit + 1 , UpperCamelCase__ ): phi[n] *= 1 - 1 / p return int(sum(phi[2:] ) ) if __name__ == "__main__": print(f'{solution() = }')
67
1
'''simple docstring''' from __future__ import annotations import pandas as pd def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> list[int]: __lowerCamelCase = [0] * no_of_processes __lowerCamelCase = [0] * no_of_processes # Copy the burst time into remaining_time[] for i in range(UpperCamelCase__ ): __lowerCamelCase = burst_time[i] __lowerCamelCase = 0 __lowerCamelCase = 0 __lowerCamelCase = 9_99_99_99_99 __lowerCamelCase = 0 __lowerCamelCase = False # Process until all processes are completed while complete != no_of_processes: for j in range(UpperCamelCase__ ): if arrival_time[j] <= increment_time and remaining_time[j] > 0: if remaining_time[j] < minm: __lowerCamelCase = remaining_time[j] __lowerCamelCase = j __lowerCamelCase = True if not check: increment_time += 1 continue remaining_time[short] -= 1 __lowerCamelCase = remaining_time[short] if minm == 0: __lowerCamelCase = 9_99_99_99_99 if remaining_time[short] == 0: complete += 1 __lowerCamelCase = False # Find finish time of current process __lowerCamelCase = increment_time + 1 # Calculate waiting time __lowerCamelCase = finish_time - arrival_time[short] __lowerCamelCase = finar - burst_time[short] if waiting_time[short] < 0: __lowerCamelCase = 0 # Increment time increment_time += 1 return waiting_time def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> list[int]: __lowerCamelCase = [0] * no_of_processes for i in range(UpperCamelCase__ ): __lowerCamelCase = burst_time[i] + waiting_time[i] return turn_around_time def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> None: __lowerCamelCase = 0 __lowerCamelCase = 0 for i in range(UpperCamelCase__ ): __lowerCamelCase = total_waiting_time + waiting_time[i] __lowerCamelCase = total_turn_around_time + turn_around_time[i] print(f"""Average waiting time = {total_waiting_time / no_of_processes:.5f}""" ) print('''Average turn around time =''' , total_turn_around_time / no_of_processes ) if __name__ == "__main__": print("Enter how many process you want to analyze") __UpperCAmelCase =int(input()) __UpperCAmelCase =[0] * no_of_processes __UpperCAmelCase =[0] * no_of_processes __UpperCAmelCase =list(range(1, no_of_processes + 1)) for i in range(no_of_processes): print("Enter the arrival time and burst time for process:--" + str(i + 1)) __UpperCAmelCase , __UpperCAmelCase =map(int, input().split()) __UpperCAmelCase =calculate_waitingtime(arrival_time, burst_time, no_of_processes) __UpperCAmelCase =burst_time __UpperCAmelCase =no_of_processes __UpperCAmelCase =waiting_time __UpperCAmelCase =calculate_turnaroundtime(bt, n, wt) calculate_average_times(waiting_time, turn_around_time, no_of_processes) __UpperCAmelCase =pd.DataFrame( list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)), columns=[ "Process", "BurstTime", "ArrivalTime", "WaitingTime", "TurnAroundTime", ], ) # Printing the dataFrame pd.set_option("display.max_rows", fcfs.shape[0] + 1) print(fcfs)
67
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST, OpenAIGPTConfig, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification, OpenAIGPTLMHeadModel, OpenAIGPTModel, ) class a__ : def __init__( self : Union[str, Any] , a : Union[str, Any] , a : Tuple=13 , a : Optional[Any]=7 , a : List[Any]=True , a : Optional[Any]=True , a : Any=True , a : Union[str, Any]=99 , a : Any=32 , a : int=5 , a : Optional[int]=4 , a : Union[str, Any]=37 , a : Optional[Any]="gelu" , a : Union[str, Any]=0.1 , a : Any=0.1 , a : Optional[int]=5_12 , a : int=16 , a : Optional[Any]=2 , a : Union[str, Any]=0.02 , a : Any=3 , a : Dict=4 , a : Any=None , ): """simple docstring""" __lowerCamelCase = parent __lowerCamelCase = batch_size __lowerCamelCase = seq_length __lowerCamelCase = is_training __lowerCamelCase = use_token_type_ids __lowerCamelCase = use_labels __lowerCamelCase = vocab_size __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_size __lowerCamelCase = hidden_act __lowerCamelCase = hidden_dropout_prob __lowerCamelCase = attention_probs_dropout_prob __lowerCamelCase = max_position_embeddings __lowerCamelCase = type_vocab_size __lowerCamelCase = type_sequence_label_size __lowerCamelCase = initializer_range __lowerCamelCase = num_labels __lowerCamelCase = num_choices __lowerCamelCase = scope __lowerCamelCase = self.vocab_size - 1 def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCamelCase = None if self.use_token_type_ids: __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = None if self.use_labels: __lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __lowerCamelCase = ids_tensor([self.batch_size] , self.num_choices ) __lowerCamelCase = OpenAIGPTConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) __lowerCamelCase = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, head_mask, token_type_ids, sequence_labels, token_labels, choice_labels, ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : Dict , a : List[str] , a : Tuple , a : List[Any] , *a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTModel(config=a ) model.to(a ) model.eval() __lowerCamelCase = model(a , token_type_ids=a , head_mask=a ) __lowerCamelCase = model(a , token_type_ids=a ) __lowerCamelCase = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Union[str, Any] , a : Dict , a : Union[str, Any] , a : Tuple , *a : Union[str, Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTLMHeadModel(a ) model.to(a ) model.eval() __lowerCamelCase = model(a , token_type_ids=a , labels=a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Tuple , a : Optional[int] , a : Union[str, Any] , a : Optional[Any] , *a : Optional[Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTDoubleHeadsModel(a ) model.to(a ) model.eval() __lowerCamelCase = model(a , token_type_ids=a , labels=a ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self : List[str] , a : int , a : Dict , a : Optional[Any] , a : str , *a : int ): """simple docstring""" __lowerCamelCase = self.num_labels __lowerCamelCase = OpenAIGPTForSequenceClassification(a ) model.to(a ) model.eval() __lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCamelCase = model(a , token_type_ids=a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = self.prepare_config_and_inputs() ( ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ) = config_and_inputs __lowerCamelCase = { '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask, } return config, inputs_dict @require_torch class a__ ( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): lowerCamelCase : List[str] =( (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification) if is_torch_available() else () ) lowerCamelCase : str =( (OpenAIGPTLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly lowerCamelCase : Optional[int] =( { "feature-extraction": OpenAIGPTModel, "text-classification": OpenAIGPTForSequenceClassification, "text-generation": OpenAIGPTLMHeadModel, "zero-shot": OpenAIGPTForSequenceClassification, } if is_torch_available() else {} ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : Tuple , a : Optional[int] , a : int , a : str , a : Any ): """simple docstring""" if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a # tiny config could not be created. return True return False def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : int , a : Optional[int] , a : str=False ): """simple docstring""" __lowerCamelCase = super()._prepare_for_class(a , a , return_labels=a ) if return_labels: if model_class.__name__ == "OpenAIGPTDoubleHeadsModel": __lowerCamelCase = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length) , dtype=torch.long , device=a , ) __lowerCamelCase = inputs_dict['''labels'''] __lowerCamelCase = inputs_dict['''labels'''] __lowerCamelCase = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices) , dtype=torch.long , device=a , ) __lowerCamelCase = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=a ) return inputs_dict def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" __lowerCamelCase = OpenAIGPTModelTester(self ) __lowerCamelCase = ConfigTester(self , config_class=a , n_embd=37 ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_double_lm_head_model(*a ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*a ) @slow def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __lowerCamelCase = OpenAIGPTModel.from_pretrained(a ) self.assertIsNotNone(a ) @require_torch class a__ ( unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt''' ) model.to(a ) __lowerCamelCase = torch.tensor([[4_81, 47_35, 5_44]] , dtype=torch.long , device=a ) # the president is __lowerCamelCase = [ 4_81, 47_35, 5_44, 2_46, 9_63, 8_70, 7_62, 2_39, 2_44, 4_04_77, 2_44, 2_49, 7_19, 8_81, 4_87, 5_44, 2_40, 2_44, 6_03, 4_81, ] # the president is a very good man. " \n " i\'m sure he is, " said the __lowerCamelCase = model.generate(a , do_sample=a ) self.assertListEqual(output_ids[0].tolist() , a )
67
1
'''simple docstring''' from typing import Dict, Iterable, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging __UpperCAmelCase =logging.get_logger(__name__) class a__ ( UpperCAmelCase__ ): lowerCamelCase : List[str] =["pixel_values"] def __init__( self : List[Any] , a : bool = True , a : Dict[str, int] = None , a : PILImageResampling = PILImageResampling.BICUBIC , a : bool = True , a : Dict[str, int] = None , a : bool = True , a : Union[int, float] = 1 / 2_55 , a : bool = True , a : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_MEAN , a : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_STD , **a : List[Any] , ): """simple docstring""" super().__init__(**a ) __lowerCamelCase = size if size is not None else {'''shortest_edge''': 2_24} __lowerCamelCase = get_size_dict(a , default_to_square=a ) __lowerCamelCase = crop_size if crop_size is not None else {'''height''': 2_24, '''width''': 2_24} __lowerCamelCase = get_size_dict(a , param_name='''crop_size''' ) __lowerCamelCase = do_resize __lowerCamelCase = size __lowerCamelCase = resample __lowerCamelCase = do_center_crop __lowerCamelCase = crop_size __lowerCamelCase = do_rescale __lowerCamelCase = rescale_factor __lowerCamelCase = do_normalize __lowerCamelCase = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN __lowerCamelCase = image_std if image_std is not None else IMAGENET_DEFAULT_STD def SCREAMING_SNAKE_CASE__ ( self : int , a : np.ndarray , a : Dict[str, int] , a : PILImageResampling = PILImageResampling.BICUBIC , a : Optional[Union[str, ChannelDimension]] = None , **a : List[Any] , ): """simple docstring""" __lowerCamelCase = get_size_dict(a , default_to_square=a ) # size_dict is a dict with either keys "height" and "width" or "shortest_edge" if "shortest_edge" in size: __lowerCamelCase = int((2_56 / 2_24) * size['''shortest_edge'''] ) __lowerCamelCase = get_resize_output_image_size(a , size=a , default_to_square=a ) __lowerCamelCase = {'''height''': output_size[0], '''width''': output_size[1]} if "height" not in size_dict or "width" not in size_dict: raise ValueError( f"""Size dict must have keys 'height' and 'width' or 'shortest_edge'. Got {size_dict.keys()}""" ) return resize( a , size=(size_dict['''height'''], size_dict['''width''']) , resample=a , data_format=a , **a ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : np.ndarray , a : Dict[str, int] , a : Optional[Union[str, ChannelDimension]] = None , **a : Tuple , ): """simple docstring""" __lowerCamelCase = get_size_dict(a ) if "height" not in size or "width" not in size: raise ValueError(f"""Size dict must have keys 'height' and 'width'. Got {size.keys()}""" ) return center_crop(a , size=(size['''height'''], size['''width''']) , data_format=a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Dict , a : np.ndarray , a : Union[int, float] , a : Optional[Union[str, ChannelDimension]] = None , **a : Union[str, Any] , ): """simple docstring""" return rescale(a , scale=a , data_format=a , **a ) def SCREAMING_SNAKE_CASE__ ( self : int , a : np.ndarray , a : Union[float, List[float]] , a : Union[float, List[float]] , a : Optional[Union[str, ChannelDimension]] = None , **a : Any , ): """simple docstring""" return normalize(a , mean=a , std=a , data_format=a , **a ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : ImageInput , a : Optional[bool] = None , a : Optional[Dict[str, int]] = None , a : PILImageResampling = None , a : Optional[bool] = None , a : Optional[Dict[str, int]] = None , a : Optional[bool] = None , a : Optional[float] = None , a : Optional[bool] = None , a : Optional[Union[float, Iterable[float]]] = None , a : Optional[Union[float, Iterable[float]]] = None , a : Optional[TensorType] = None , a : ChannelDimension = ChannelDimension.FIRST , **a : str , ): """simple docstring""" __lowerCamelCase = do_resize if do_resize is not None else self.do_resize __lowerCamelCase = resample if resample is not None else self.resample __lowerCamelCase = do_center_crop if do_center_crop is not None else self.do_center_crop __lowerCamelCase = do_rescale if do_rescale is not None else self.do_rescale __lowerCamelCase = rescale_factor if rescale_factor is not None else self.rescale_factor __lowerCamelCase = do_normalize if do_normalize is not None else self.do_normalize __lowerCamelCase = image_mean if image_mean is not None else self.image_mean __lowerCamelCase = image_std if image_std is not None else self.image_std __lowerCamelCase = size if size is not None else self.size __lowerCamelCase = get_size_dict(a , default_to_square=a ) __lowerCamelCase = crop_size if crop_size is not None else self.crop_size __lowerCamelCase = get_size_dict(a , param_name='''crop_size''' ) __lowerCamelCase = make_list_of_images(a ) if not valid_images(a ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None: raise ValueError('''Size must be specified if do_resize is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # All transformations expect numpy arrays. __lowerCamelCase = [to_numpy_array(a ) for image in images] if do_resize: __lowerCamelCase = [self.resize(a , a , a ) for image in images] if do_center_crop: __lowerCamelCase = [self.center_crop(a , a ) for image in images] if do_rescale: __lowerCamelCase = [self.rescale(a , a ) for image in images] if do_normalize: __lowerCamelCase = [self.normalize(a , a , a ) for image in images] __lowerCamelCase = [to_channel_dimension_format(a , a ) for image in images] __lowerCamelCase = {'''pixel_values''': images} return BatchFeature(data=a , tensor_type=a )
67
'''simple docstring''' import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] =["image_processor", "tokenizer"] lowerCamelCase : Union[str, Any] ="LayoutLMv2ImageProcessor" lowerCamelCase : int =("LayoutXLMTokenizer", "LayoutXLMTokenizerFast") def __init__( self : Optional[int] , a : Any=None , a : Any=None , **a : Union[str, Any] ): """simple docstring""" if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , a , ) __lowerCamelCase = kwargs.pop('''feature_extractor''' ) __lowerCamelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(a , a ) def __call__( self : Tuple , a : Optional[int] , a : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , a : Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None , a : Union[List[List[int]], List[List[List[int]]]] = None , a : Optional[Union[List[int], List[List[int]]]] = None , a : bool = True , a : Union[bool, str, PaddingStrategy] = False , a : Union[bool, str, TruncationStrategy] = None , a : Optional[int] = None , a : int = 0 , a : Optional[int] = None , a : Optional[bool] = None , a : Optional[bool] = None , a : bool = False , a : bool = False , a : bool = False , a : bool = False , a : bool = True , a : Optional[Union[str, TensorType]] = None , **a : Tuple , ): """simple docstring""" if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( '''You cannot provide bounding boxes ''' '''if you initialized the image processor with apply_ocr set to True.''' ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.''' ) if return_overflowing_tokens is True and return_offsets_mapping is False: raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.''' ) # first, apply the image processor __lowerCamelCase = self.image_processor(images=a , return_tensors=a ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(a , a ): __lowerCamelCase = [text] # add batch dimension (as the image processor always adds a batch dimension) __lowerCamelCase = features['''words'''] __lowerCamelCase = self.tokenizer( text=text if text is not None else features['''words'''] , text_pair=text_pair if text_pair is not None else None , boxes=boxes if boxes is not None else features['''boxes'''] , word_labels=a , add_special_tokens=a , padding=a , truncation=a , max_length=a , stride=a , pad_to_multiple_of=a , return_token_type_ids=a , return_attention_mask=a , return_overflowing_tokens=a , return_special_tokens_mask=a , return_offsets_mapping=a , return_length=a , verbose=a , return_tensors=a , **a , ) # add pixel values __lowerCamelCase = features.pop('''pixel_values''' ) if return_overflowing_tokens is True: __lowerCamelCase = self.get_overflowing_images(a , encoded_inputs['''overflow_to_sample_mapping'''] ) __lowerCamelCase = images return encoded_inputs def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : Optional[Any] , a : str ): """simple docstring""" __lowerCamelCase = [] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(a ) != len(a ): raise ValueError( '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got''' f""" {len(a )} and {len(a )}""" ) return images_with_overflow def SCREAMING_SNAKE_CASE__ ( self : List[str] , *a : Optional[Any] , **a : Union[str, Any] ): """simple docstring""" return self.tokenizer.batch_decode(*a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , *a : Union[str, Any] , **a : Tuple ): """simple docstring""" return self.tokenizer.decode(*a , **a ) @property def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" return ["input_ids", "bbox", "attention_mask", "image"] @property def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , a , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , a , ) return self.image_processor
67
1
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Optional[int]: if b == 0: return 1 if (b % 2) == 0: return actual_power(UpperCamelCase__ , int(b / 2 ) ) * actual_power(UpperCamelCase__ , int(b / 2 ) ) else: return a * actual_power(UpperCamelCase__ , int(b / 2 ) ) * actual_power(UpperCamelCase__ , int(b / 2 ) ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> float: if b < 0: return 1 / actual_power(UpperCamelCase__ , UpperCamelCase__ ) return actual_power(UpperCamelCase__ , UpperCamelCase__ ) if __name__ == "__main__": print(power(-2, -3))
67
'''simple docstring''' import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) class a__ ( UpperCAmelCase__ ): def __init__( self : Optional[Any] , a : Union[List[ControlNetModel], Tuple[ControlNetModel]] ): """simple docstring""" super().__init__() __lowerCamelCase = nn.ModuleList(a ) def SCREAMING_SNAKE_CASE__ ( self : Any , a : torch.FloatTensor , a : Union[torch.Tensor, float, int] , a : torch.Tensor , a : List[torch.tensor] , a : List[float] , a : Optional[torch.Tensor] = None , a : Optional[torch.Tensor] = None , a : Optional[torch.Tensor] = None , a : Optional[Dict[str, Any]] = None , a : bool = False , a : bool = True , ): """simple docstring""" for i, (image, scale, controlnet) in enumerate(zip(a , a , self.nets ) ): __lowerCamelCase , __lowerCamelCase = controlnet( a , a , a , a , a , a , a , a , a , a , a , ) # merge samples if i == 0: __lowerCamelCase , __lowerCamelCase = down_samples, mid_sample else: __lowerCamelCase = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(a , a ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def SCREAMING_SNAKE_CASE__ ( self : Any , a : Union[str, os.PathLike] , a : bool = True , a : Callable = None , a : bool = False , a : Optional[str] = None , ): """simple docstring""" __lowerCamelCase = 0 __lowerCamelCase = save_directory for controlnet in self.nets: controlnet.save_pretrained( a , is_main_process=a , save_function=a , safe_serialization=a , variant=a , ) idx += 1 __lowerCamelCase = model_path_to_save + f"""_{idx}""" @classmethod def SCREAMING_SNAKE_CASE__ ( cls : List[str] , a : Optional[Union[str, os.PathLike]] , **a : Optional[Any] ): """simple docstring""" __lowerCamelCase = 0 __lowerCamelCase = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... __lowerCamelCase = pretrained_model_path while os.path.isdir(a ): __lowerCamelCase = ControlNetModel.from_pretrained(a , **a ) controlnets.append(a ) idx += 1 __lowerCamelCase = pretrained_model_path + f"""_{idx}""" logger.info(f"""{len(a )} controlnets loaded from {pretrained_model_path}.""" ) if len(a ) == 0: raise ValueError( f"""No ControlNets found under {os.path.dirname(a )}. Expected at least {pretrained_model_path + '_0'}.""" ) return cls(a )
67
1
'''simple docstring''' import os from typing import List, Optional, Union from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType from ..auto import AutoTokenizer class a__ ( UpperCAmelCase__ ): lowerCamelCase : Union[str, Any] =["image_processor", "tokenizer"] lowerCamelCase : int ="BlipImageProcessor" lowerCamelCase : int ="AutoTokenizer" def __init__( self : Dict , a : Any , a : Union[str, Any] , a : str ): """simple docstring""" super().__init__(a , a ) # add QFormer tokenizer __lowerCamelCase = qformer_tokenizer def __call__( self : Union[str, Any] , a : ImageInput = None , a : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , a : bool = True , a : Union[bool, str, PaddingStrategy] = False , a : Union[bool, str, TruncationStrategy] = None , a : Optional[int] = None , a : int = 0 , a : Optional[int] = None , a : Optional[bool] = None , a : bool = False , a : bool = False , a : bool = False , a : bool = False , a : bool = False , a : bool = True , a : Optional[Union[str, TensorType]] = None , **a : List[Any] , ): """simple docstring""" if images is None and text is None: raise ValueError('''You have to specify at least images or text.''' ) __lowerCamelCase = BatchFeature() if text is not None: __lowerCamelCase = self.tokenizer( text=a , add_special_tokens=a , padding=a , truncation=a , max_length=a , stride=a , pad_to_multiple_of=a , return_attention_mask=a , return_overflowing_tokens=a , return_special_tokens_mask=a , return_offsets_mapping=a , return_token_type_ids=a , return_length=a , verbose=a , return_tensors=a , **a , ) encoding.update(a ) __lowerCamelCase = self.qformer_tokenizer( text=a , add_special_tokens=a , padding=a , truncation=a , max_length=a , stride=a , pad_to_multiple_of=a , return_attention_mask=a , return_overflowing_tokens=a , return_special_tokens_mask=a , return_offsets_mapping=a , return_token_type_ids=a , return_length=a , verbose=a , return_tensors=a , **a , ) __lowerCamelCase = qformer_text_encoding.pop('''input_ids''' ) __lowerCamelCase = qformer_text_encoding.pop('''attention_mask''' ) if images is not None: __lowerCamelCase = self.image_processor(a , return_tensors=a ) encoding.update(a ) return encoding def SCREAMING_SNAKE_CASE__ ( self : Dict , *a : Dict , **a : List[str] ): """simple docstring""" return self.tokenizer.batch_decode(*a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Any , *a : List[str] , **a : Tuple ): """simple docstring""" return self.tokenizer.decode(*a , **a ) @property # Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" __lowerCamelCase = self.tokenizer.model_input_names __lowerCamelCase = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) def SCREAMING_SNAKE_CASE__ ( self : Dict , a : str , **a : List[Any] ): """simple docstring""" if os.path.isfile(a ): raise ValueError(f"""Provided path ({save_directory}) should be a directory, not a file""" ) os.makedirs(a , exist_ok=a ) __lowerCamelCase = os.path.join(a , '''qformer_tokenizer''' ) self.qformer_tokenizer.save_pretrained(a ) return super().save_pretrained(a , **a ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : str , a : List[str] , **a : Dict ): """simple docstring""" __lowerCamelCase = AutoTokenizer.from_pretrained(a , subfolder='''qformer_tokenizer''' ) __lowerCamelCase = cls._get_arguments_from_pretrained(a , **a ) args.append(a ) return cls(*a )
67
'''simple docstring''' from typing import List, Union import numpy as np from ..utils import add_end_docstrings, 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(): import torch from ..models.auto.modeling_auto import MODEL_FOR_DEPTH_ESTIMATION_MAPPING __UpperCAmelCase =logging.get_logger(__name__) @add_end_docstrings(UpperCAmelCase__ ) class a__ ( UpperCAmelCase__ ): def __init__( self : List[str] , *a : Union[str, Any] , **a : Optional[Any] ): """simple docstring""" super().__init__(*a , **a ) requires_backends(self , '''vision''' ) self.check_model_type(a ) def __call__( self : Any , a : Union[str, List[str], "Image.Image", List["Image.Image"]] , **a : Optional[int] ): """simple docstring""" return super().__call__(a , **a ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , **a : Any ): """simple docstring""" return {}, {}, {} def SCREAMING_SNAKE_CASE__ ( self : List[Any] , a : List[str] ): """simple docstring""" __lowerCamelCase = load_image(a ) __lowerCamelCase = image.size __lowerCamelCase = self.image_processor(images=a , return_tensors=self.framework ) return model_inputs def SCREAMING_SNAKE_CASE__ ( self : int , a : Optional[Any] ): """simple docstring""" __lowerCamelCase = self.model(**a ) return model_outputs def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : Any ): """simple docstring""" __lowerCamelCase = model_outputs.predicted_depth __lowerCamelCase = torch.nn.functional.interpolate( predicted_depth.unsqueeze(1 ) , size=self.image_size[::-1] , mode='''bicubic''' , align_corners=a ) __lowerCamelCase = prediction.squeeze().cpu().numpy() __lowerCamelCase = (output * 2_55 / np.max(a )).astype('''uint8''' ) __lowerCamelCase = Image.fromarray(a ) __lowerCamelCase = {} __lowerCamelCase = predicted_depth __lowerCamelCase = depth return output_dict
67
1
'''simple docstring''' # DISCLAIMER: This code is strongly influenced by https://github.com/pesser/pytorch_diffusion # and https://github.com/hojonathanho/diffusion import math from dataclasses import dataclass from typing import List, Optional, Tuple, Union import numpy as np import torch from diffusers.configuration_utils import ConfigMixin, register_to_config from diffusers.schedulers.scheduling_utils import SchedulerMixin from diffusers.utils import BaseOutput, deprecate @dataclass # Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->DDIM class a__ ( UpperCAmelCase__ ): lowerCamelCase : torch.FloatTensor lowerCamelCase : Optional[torch.FloatTensor] =None def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__=0.9_9_9 , UpperCamelCase__="cosine" , ) -> int: if alpha_transform_type == "cosine": def alpha_bar_fn(UpperCamelCase__ ): return math.cos((t + 0.0_0_8) / 1.0_0_8 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(UpperCamelCase__ ): return math.exp(t * -1_2.0 ) else: raise ValueError(f"""Unsupported alpha_tranform_type: {alpha_transform_type}""" ) __lowerCamelCase = [] for i in range(UpperCamelCase__ ): __lowerCamelCase = i / num_diffusion_timesteps __lowerCamelCase = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(UpperCamelCase__ ) / alpha_bar_fn(UpperCamelCase__ ) , UpperCamelCase__ ) ) return torch.tensor(UpperCamelCase__ , dtype=torch.floataa ) class a__ ( UpperCAmelCase__ , UpperCAmelCase__ ): lowerCamelCase : List[Any] =1 @register_to_config def __init__( self : Union[str, Any] , a : int = 10_00 , a : float = 0.00_01 , a : float = 0.02 , a : str = "linear" , a : Optional[Union[np.ndarray, List[float]]] = None , a : bool = True , a : bool = True , a : int = 0 , a : str = "epsilon" , a : float = 1.0 , **a : List[str] , ): """simple docstring""" if kwargs.get('''set_alpha_to_one''' , a ) is not None: __lowerCamelCase = ( '''The `set_alpha_to_one` argument is deprecated. Please use `set_alpha_to_zero` instead.''' ) deprecate('''set_alpha_to_one''' , '''1.0.0''' , a , standard_warn=a ) __lowerCamelCase = kwargs['''set_alpha_to_one'''] if trained_betas is not None: __lowerCamelCase = torch.tensor(a , dtype=torch.floataa ) elif beta_schedule == "linear": __lowerCamelCase = torch.linspace(a , a , a , dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. __lowerCamelCase = ( torch.linspace(beta_start**0.5 , beta_end**0.5 , a , dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule __lowerCamelCase = betas_for_alpha_bar(a ) else: raise NotImplementedError(f"""{beta_schedule} does is not implemented for {self.__class__}""" ) __lowerCamelCase = 1.0 - self.betas __lowerCamelCase = torch.cumprod(self.alphas , dim=0 ) # At every step in inverted ddim, we are looking into the next alphas_cumprod # For the final step, there is no next alphas_cumprod, and the index is out of bounds # `set_alpha_to_zero` decides whether we set this parameter simply to zero # in this case, self.step() just output the predicted noise # or whether we use the final alpha of the "non-previous" one. __lowerCamelCase = torch.tensor(0.0 ) if set_alpha_to_zero else self.alphas_cumprod[-1] # standard deviation of the initial noise distribution __lowerCamelCase = 1.0 # setable values __lowerCamelCase = None __lowerCamelCase = torch.from_numpy(np.arange(0 , a ).copy().astype(np.intaa ) ) def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : torch.FloatTensor , a : Optional[int] = None ): """simple docstring""" return sample def SCREAMING_SNAKE_CASE__ ( self : Dict , a : int , a : Union[str, torch.device] = None ): """simple docstring""" if num_inference_steps > self.config.num_train_timesteps: raise ValueError( f"""`num_inference_steps`: {num_inference_steps} cannot be larger than `self.config.train_timesteps`:""" f""" {self.config.num_train_timesteps} as the unet model trained with this scheduler can only handle""" f""" maximal {self.config.num_train_timesteps} timesteps.""" ) __lowerCamelCase = num_inference_steps __lowerCamelCase = self.config.num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 __lowerCamelCase = (np.arange(0 , a ) * step_ratio).round().copy().astype(np.intaa ) __lowerCamelCase = torch.from_numpy(a ).to(a ) self.timesteps += self.config.steps_offset def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : torch.FloatTensor , a : int , a : torch.FloatTensor , a : float = 0.0 , a : bool = False , a : Optional[torch.FloatTensor] = None , a : bool = True , ): """simple docstring""" __lowerCamelCase = timestep + self.config.num_train_timesteps // self.num_inference_steps # 2. compute alphas, betas # change original implementation to exactly match noise levels for analogous forward process __lowerCamelCase = self.alphas_cumprod[timestep] __lowerCamelCase = ( self.alphas_cumprod[prev_timestep] if prev_timestep < self.config.num_train_timesteps else self.final_alpha_cumprod ) __lowerCamelCase = 1 - alpha_prod_t # 3. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf if self.config.prediction_type == "epsilon": __lowerCamelCase = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 __lowerCamelCase = model_output elif self.config.prediction_type == "sample": __lowerCamelCase = model_output __lowerCamelCase = (sample - alpha_prod_t ** 0.5 * pred_original_sample) / beta_prod_t ** 0.5 elif self.config.prediction_type == "v_prediction": __lowerCamelCase = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output __lowerCamelCase = (alpha_prod_t**0.5) * model_output + (beta_prod_t**0.5) * sample else: raise ValueError( f"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample`, or""" ''' `v_prediction`''' ) # 4. Clip or threshold "predicted x_0" if self.config.clip_sample: __lowerCamelCase = pred_original_sample.clamp( -self.config.clip_sample_range , self.config.clip_sample_range ) # 5. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf __lowerCamelCase = (1 - alpha_prod_t_prev) ** 0.5 * pred_epsilon # 6. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf __lowerCamelCase = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction if not return_dict: return (prev_sample, pred_original_sample) return DDIMSchedulerOutput(prev_sample=a , pred_original_sample=a ) def __len__( self : str ): """simple docstring""" return self.config.num_train_timesteps
67
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase ={ "configuration_clap": [ "CLAP_PRETRAINED_MODEL_ARCHIVE_LIST", "ClapAudioConfig", "ClapConfig", "ClapTextConfig", ], "processing_clap": ["ClapProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "CLAP_PRETRAINED_MODEL_ARCHIVE_LIST", "ClapModel", "ClapPreTrainedModel", "ClapTextModel", "ClapTextModelWithProjection", "ClapAudioModel", "ClapAudioModelWithProjection", ] __UpperCAmelCase =["ClapFeatureExtractor"] if TYPE_CHECKING: from .configuration_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioConfig, ClapConfig, ClapTextConfig, ) from .processing_clap import ClapProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clap import ClapFeatureExtractor from .modeling_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioModel, ClapAudioModelWithProjection, ClapModel, ClapPreTrainedModel, ClapTextModel, ClapTextModelWithProjection, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
67
1
'''simple docstring''' import os from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen, xsplitext from ..table import array_cast from ..utils.py_utils import no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: from .features import FeatureType __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase =False, False, False @dataclass class a__ : lowerCamelCase : Optional[int] =None lowerCamelCase : bool =True lowerCamelCase : bool =True lowerCamelCase : Optional[str] =None # Automatically constructed lowerCamelCase : ClassVar[str] ="dict" lowerCamelCase : ClassVar[Any] =pa.struct({"bytes": pa.binary(), "path": pa.string()} ) lowerCamelCase : str =field(default="Audio" , init=UpperCAmelCase__ , repr=UpperCAmelCase__ ) def __call__( self : Dict ): """simple docstring""" return self.pa_type def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : Union[str, bytes, dict] ): """simple docstring""" try: import soundfile as sf # soundfile is a dependency of librosa, needed to decode audio files. except ImportError as err: raise ImportError('''To support encoding audio data, please install \'soundfile\'.''' ) from err if isinstance(a , a ): return {"bytes": None, "path": value} elif isinstance(a , a ): return {"bytes": value, "path": None} elif "array" in value: # convert the audio array to wav bytes __lowerCamelCase = BytesIO() sf.write(a , value['''array'''] , value['''sampling_rate'''] , format='''wav''' ) return {"bytes": buffer.getvalue(), "path": None} elif value.get('''path''' ) is not None and os.path.isfile(value['''path'''] ): # we set "bytes": None to not duplicate the data if they're already available locally if value["path"].endswith('''pcm''' ): # "PCM" only has raw audio bytes if value.get('''sampling_rate''' ) is None: # At least, If you want to convert "PCM-byte" to "WAV-byte", you have to know sampling rate raise KeyError('''To use PCM files, please specify a \'sampling_rate\' in Audio object''' ) if value.get('''bytes''' ): # If we already had PCM-byte, we don`t have to make "read file, make bytes" (just use it!) __lowerCamelCase = np.frombuffer(value['''bytes'''] , dtype=np.intaa ).astype(np.floataa ) / 3_27_67 else: __lowerCamelCase = np.memmap(value['''path'''] , dtype='''h''' , mode='''r''' ).astype(np.floataa ) / 3_27_67 __lowerCamelCase = BytesIO(bytes() ) sf.write(a , a , value['''sampling_rate'''] , format='''wav''' ) return {"bytes": buffer.getvalue(), "path": None} else: return {"bytes": None, "path": value.get('''path''' )} elif value.get('''bytes''' ) is not None or value.get('''path''' ) is not None: # store the audio bytes, and path is used to infer the audio format using the file extension return {"bytes": value.get('''bytes''' ), "path": value.get('''path''' )} else: raise ValueError( f"""An audio sample should have one of 'path' or 'bytes' but they are missing or None in {value}.""" ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : dict , a : Optional[Dict[str, Union[str, bool, None]]] = None ): """simple docstring""" if not self.decode: raise RuntimeError('''Decoding is disabled for this feature. Please use Audio(decode=True) instead.''' ) __lowerCamelCase , __lowerCamelCase = (value['''path'''], BytesIO(value['''bytes'''] )) if value['''bytes'''] is not None else (value['''path'''], None) if path is None and file is None: raise ValueError(f"""An audio sample should have one of 'path' or 'bytes' but both are None in {value}.""" ) try: import librosa import soundfile as sf except ImportError as err: raise ImportError('''To support decoding audio files, please install \'librosa\' and \'soundfile\'.''' ) from err __lowerCamelCase = xsplitext(a )[1][1:].lower() if path is not None else None if not config.IS_OPUS_SUPPORTED and audio_format == "opus": raise RuntimeError( '''Decoding \'opus\' files requires system library \'libsndfile\'>=1.0.31, ''' '''You can try to update `soundfile` python library: `pip install "soundfile>=0.12.1"`. ''' ) elif not config.IS_MP3_SUPPORTED and audio_format == "mp3": raise RuntimeError( '''Decoding \'mp3\' files requires system library \'libsndfile\'>=1.1.0, ''' '''You can try to update `soundfile` python library: `pip install "soundfile>=0.12.1"`. ''' ) if file is None: __lowerCamelCase = token_per_repo_id or {} __lowerCamelCase = path.split('''::''' )[-1] try: __lowerCamelCase = string_to_dict(a , config.HUB_DATASETS_URL )['''repo_id'''] __lowerCamelCase = token_per_repo_id[repo_id] except (ValueError, KeyError): __lowerCamelCase = None with xopen(a , '''rb''' , use_auth_token=a ) as f: __lowerCamelCase , __lowerCamelCase = sf.read(a ) else: __lowerCamelCase , __lowerCamelCase = sf.read(a ) __lowerCamelCase = array.T if self.mono: __lowerCamelCase = librosa.to_mono(a ) if self.sampling_rate and self.sampling_rate != sampling_rate: __lowerCamelCase = librosa.resample(a , orig_sr=a , target_sr=self.sampling_rate ) __lowerCamelCase = self.sampling_rate return {"path": path, "array": array, "sampling_rate": sampling_rate} def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" from .features import Value if self.decode: raise ValueError('''Cannot flatten a decoded Audio feature.''' ) return { "bytes": Value('''binary''' ), "path": Value('''string''' ), } def SCREAMING_SNAKE_CASE__ ( self : str , a : Union[pa.StringArray, pa.StructArray] ): """simple docstring""" if pa.types.is_string(storage.type ): __lowerCamelCase = pa.array([None] * len(a ) , type=pa.binary() ) __lowerCamelCase = pa.StructArray.from_arrays([bytes_array, storage] , ['''bytes''', '''path'''] , mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): __lowerCamelCase = pa.array([None] * len(a ) , type=pa.string() ) __lowerCamelCase = pa.StructArray.from_arrays([storage, path_array] , ['''bytes''', '''path'''] , mask=storage.is_null() ) elif pa.types.is_struct(storage.type ) and storage.type.get_all_field_indices('''array''' ): __lowerCamelCase = pa.array([Audio().encode_example(a ) if x is not None else None for x in storage.to_pylist()] ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index('''bytes''' ) >= 0: __lowerCamelCase = storage.field('''bytes''' ) else: __lowerCamelCase = pa.array([None] * len(a ) , type=pa.binary() ) if storage.type.get_field_index('''path''' ) >= 0: __lowerCamelCase = storage.field('''path''' ) else: __lowerCamelCase = pa.array([None] * len(a ) , type=pa.string() ) __lowerCamelCase = pa.StructArray.from_arrays([bytes_array, path_array] , ['''bytes''', '''path'''] , mask=storage.is_null() ) return array_cast(a , self.pa_type ) def SCREAMING_SNAKE_CASE__ ( self : Dict , a : pa.StructArray ): """simple docstring""" @no_op_if_value_is_null def path_to_bytes(a : Optional[Any] ): with xopen(a , '''rb''' ) as f: __lowerCamelCase = f.read() return bytes_ __lowerCamelCase = pa.array( [ (path_to_bytes(x['''path'''] ) if x['''bytes'''] is None else x['''bytes''']) if x is not None else None for x in storage.to_pylist() ] , type=pa.binary() , ) __lowerCamelCase = pa.array( [os.path.basename(a ) if path is not None else None for path in storage.field('''path''' ).to_pylist()] , type=pa.string() , ) __lowerCamelCase = pa.StructArray.from_arrays([bytes_array, path_array] , ['''bytes''', '''path'''] , mask=bytes_array.is_null() ) return array_cast(a , self.pa_type )
67
'''simple docstring''' import os import shutil import tempfile import unittest import numpy as np from transformers import AutoTokenizer, BarkProcessor from transformers.testing_utils import require_torch, slow @require_torch class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" __lowerCamelCase = '''ylacombe/bark-small''' __lowerCamelCase = tempfile.mkdtemp() __lowerCamelCase = '''en_speaker_1''' __lowerCamelCase = '''This is a test string''' __lowerCamelCase = '''speaker_embeddings_path.json''' __lowerCamelCase = '''speaker_embeddings''' def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , **a : Dict ): """simple docstring""" return AutoTokenizer.from_pretrained(self.checkpoint , **a ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) processor.save_pretrained(self.tmpdirname ) __lowerCamelCase = BarkProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) @slow def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) processor.save_pretrained( self.tmpdirname , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , speaker_embeddings_directory=self.speaker_embeddings_directory , ) __lowerCamelCase = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) __lowerCamelCase = BarkProcessor.from_pretrained( self.tmpdirname , self.speaker_embeddings_dict_path , bos_token='''(BOS)''' , eos_token='''(EOS)''' , ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" __lowerCamelCase = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) __lowerCamelCase = 35 __lowerCamelCase = 2 __lowerCamelCase = 8 __lowerCamelCase = { '''semantic_prompt''': np.ones(a ), '''coarse_prompt''': np.ones((nb_codebooks_coarse, seq_len) ), '''fine_prompt''': np.ones((nb_codebooks_total, seq_len) ), } # test providing already loaded voice_preset __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from npz file __lowerCamelCase = os.path.join(self.tmpdirname , '''file.npz''' ) np.savez(a , **a ) __lowerCamelCase = processor(text=self.input_string , voice_preset=a ) __lowerCamelCase = inputs['''history_prompt'''] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(a , np.array([] ) ).tolist() ) # test loading voice preset from the hub __lowerCamelCase = processor(text=self.input_string , voice_preset=self.voice_preset ) def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" __lowerCamelCase = self.get_tokenizer() __lowerCamelCase = BarkProcessor(tokenizer=a ) __lowerCamelCase = processor(text=self.input_string ) __lowerCamelCase = tokenizer( self.input_string , padding='''max_length''' , max_length=2_56 , add_special_tokens=a , return_attention_mask=a , return_token_type_ids=a , ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key].squeeze().tolist() )
67
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={ "uw-madison/mra-base-512-4": "https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json", } class a__ ( UpperCAmelCase__ ): lowerCamelCase : str ="mra" def __init__( self : List[str] , a : int=5_02_65 , a : Union[str, Any]=7_68 , a : List[Any]=12 , a : str=12 , a : int=30_72 , a : str="gelu" , a : List[str]=0.1 , a : Union[str, Any]=0.1 , a : Optional[int]=5_12 , a : Union[str, Any]=1 , a : List[Any]=0.02 , a : str=1e-5 , a : int="absolute" , a : int=4 , a : str="full" , a : Optional[Any]=0 , a : Any=0 , a : List[str]=1 , a : Union[str, Any]=0 , a : Union[str, Any]=2 , **a : Optional[Any] , ): """simple docstring""" super().__init__(pad_token_id=a , bos_token_id=a , eos_token_id=a , **a ) __lowerCamelCase = vocab_size __lowerCamelCase = max_position_embeddings __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_size __lowerCamelCase = hidden_act __lowerCamelCase = hidden_dropout_prob __lowerCamelCase = attention_probs_dropout_prob __lowerCamelCase = initializer_range __lowerCamelCase = type_vocab_size __lowerCamelCase = layer_norm_eps __lowerCamelCase = position_embedding_type __lowerCamelCase = block_per_row __lowerCamelCase = approx_mode __lowerCamelCase = initial_prior_first_n_blocks __lowerCamelCase = initial_prior_diagonal_n_blocks
67
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase ={"configuration_vit_msn": ["VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP", "ViTMSNConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST", "ViTMSNModel", "ViTMSNForImageClassification", "ViTMSNPreTrainedModel", ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
67
1
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=UpperCAmelCase__ ) class a__ ( UpperCAmelCase__ ): lowerCamelCase : str =field(default="image-classification" , metadata={"include_in_asdict_even_if_is_default": True} ) lowerCamelCase : ClassVar[Features] =Features({"image": Image()} ) lowerCamelCase : ClassVar[Features] =Features({"labels": ClassLabel} ) lowerCamelCase : str ="image" lowerCamelCase : str ="labels" def SCREAMING_SNAKE_CASE__ ( self : Any , a : int ): """simple docstring""" if self.label_column not in features: raise ValueError(f"""Column {self.label_column} is not present in features.""" ) if not isinstance(features[self.label_column] , a ): raise ValueError(f"""Column {self.label_column} is not a ClassLabel.""" ) __lowerCamelCase = copy.deepcopy(self ) __lowerCamelCase = self.label_schema.copy() __lowerCamelCase = features[self.label_column] __lowerCamelCase = label_schema return task_template @property def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" return { self.image_column: "image", self.label_column: "labels", }
67
'''simple docstring''' import re from filelock import FileLock try: import nltk __UpperCAmelCase =True except (ImportError, ModuleNotFoundError): __UpperCAmelCase =False if NLTK_AVAILABLE: with FileLock(".lock") as lock: nltk.download("punkt", quiet=True) def __lowerCAmelCase ( UpperCamelCase__ ) -> str: re.sub('''<n>''' , '''''' , UpperCamelCase__ ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(UpperCamelCase__ ) )
67
1
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ ) -> tuple[int, int]: try: __lowerCamelCase = float(UpperCamelCase__ ) except ValueError: raise ValueError('''Please enter a valid number''' ) __lowerCamelCase = decimal - int(UpperCamelCase__ ) if fractional_part == 0: return int(UpperCamelCase__ ), 1 else: __lowerCamelCase = len(str(UpperCamelCase__ ).split('''.''' )[1] ) __lowerCamelCase = int(decimal * (10**number_of_frac_digits) ) __lowerCamelCase = 10**number_of_frac_digits __lowerCamelCase , __lowerCamelCase = denominator, numerator while True: __lowerCamelCase = dividend % divisor if remainder == 0: break __lowerCamelCase , __lowerCamelCase = divisor, remainder __lowerCamelCase , __lowerCamelCase = numerator / divisor, denominator / divisor return int(UpperCamelCase__ ), int(UpperCamelCase__ ) if __name__ == "__main__": print(f'{decimal_to_fraction(2) = }') print(f'{decimal_to_fraction(89.0) = }') print(f'{decimal_to_fraction("67") = }') print(f'{decimal_to_fraction("45.0") = }') print(f'{decimal_to_fraction(1.5) = }') print(f'{decimal_to_fraction("6.25") = }') print(f'{decimal_to_fraction("78td") = }')
67
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ={ "abeja/gpt-neox-japanese-2.7b": "https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json", } class a__ ( UpperCAmelCase__ ): lowerCamelCase : Optional[int] ="gpt_neox_japanese" def __init__( self : List[Any] , a : Tuple=3_20_00 , a : Dict=25_60 , a : Union[str, Any]=32 , a : Dict=32 , a : Dict=4 , a : Optional[Any]="gelu" , a : Any=1.00 , a : str=1_00_00 , a : List[str]=20_48 , a : str=0.02 , a : Union[str, Any]=1e-5 , a : Optional[Any]=True , a : str=3_19_96 , a : List[str]=3_19_99 , a : str=0.1 , a : Union[str, Any]=0.0 , **a : Optional[Any] , ): """simple docstring""" super().__init__(bos_token_id=a , eos_token_id=a , **a ) __lowerCamelCase = vocab_size __lowerCamelCase = max_position_embeddings __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_multiple_size __lowerCamelCase = hidden_act __lowerCamelCase = rotary_pct __lowerCamelCase = rotary_emb_base __lowerCamelCase = initializer_range __lowerCamelCase = layer_norm_eps __lowerCamelCase = use_cache __lowerCamelCase = attention_dropout __lowerCamelCase = hidden_dropout
67
1
'''simple docstring''' # This is the module that test_patching.py uses to test patch_submodule() import os # noqa: this is just for tests import os as renamed_os # noqa: this is just for tests from os import path # noqa: this is just for tests from os import path as renamed_path # noqa: this is just for tests from os.path import join # noqa: this is just for tests from os.path import join as renamed_join # noqa: this is just for tests __UpperCAmelCase =open # noqa: we just need to have a builtin inside this module to test it properly
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> int: while second != 0: __lowerCamelCase = first & second first ^= second __lowerCamelCase = c << 1 return first if __name__ == "__main__": import doctest doctest.testmod() __UpperCAmelCase =int(input("Enter the first number: ").strip()) __UpperCAmelCase =int(input("Enter the second number: ").strip()) print(f'{add(first, second) = }')
67
1
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy __UpperCAmelCase =logging.get_logger(__name__) class a__ ( UpperCAmelCase__ ): def __init__( self : List[Any] , a : int , a : int , a : float , **a : str ): """simple docstring""" __lowerCamelCase = feature_size __lowerCamelCase = sampling_rate __lowerCamelCase = padding_value __lowerCamelCase = kwargs.pop('''padding_side''' , '''right''' ) __lowerCamelCase = kwargs.pop('''return_attention_mask''' , a ) super().__init__(**a ) def SCREAMING_SNAKE_CASE__ ( self : str , a : Union[ BatchFeature, List[BatchFeature], Dict[str, BatchFeature], Dict[str, List[BatchFeature]], List[Dict[str, BatchFeature]], ] , a : Union[bool, str, PaddingStrategy] = True , a : Optional[int] = None , a : bool = False , a : Optional[int] = None , a : Optional[bool] = None , a : Optional[Union[str, TensorType]] = None , ): """simple docstring""" if isinstance(a , (list, tuple) ) and isinstance(processed_features[0] , (dict, BatchFeature) ): __lowerCamelCase = { key: [example[key] for example in processed_features] for key in processed_features[0].keys() } # The model's main input name, usually `input_values`, has be passed for padding if self.model_input_names[0] not in processed_features: raise ValueError( '''You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`''' f""" to this method that includes {self.model_input_names[0]}, but you provided""" f""" {list(processed_features.keys() )}""" ) __lowerCamelCase = processed_features[self.model_input_names[0]] __lowerCamelCase = ( return_attention_mask if return_attention_mask is not None else self.return_attention_mask ) if len(a ) == 0: if return_attention_mask: __lowerCamelCase = [] return processed_features # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays # and rebuild them afterwards if no return_tensors is specified # Note that we lose the specific device the tensor may be on for PyTorch __lowerCamelCase = required_input[0] if isinstance(a , (list, tuple) ): # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element. __lowerCamelCase = 0 while len(required_input[index] ) == 0: index += 1 if index < len(a ): __lowerCamelCase = required_input[index][0] if return_tensors is None: if is_tf_tensor(a ): __lowerCamelCase = '''tf''' elif is_torch_tensor(a ): __lowerCamelCase = '''pt''' elif isinstance(a , (int, float, list, tuple, np.ndarray) ): __lowerCamelCase = '''np''' else: raise ValueError( f"""type of {first_element} unknown: {type(a )}. """ '''Should be one of a python, numpy, pytorch or tensorflow object.''' ) for key, value in processed_features.items(): if isinstance(value[0] , (int, float) ): __lowerCamelCase = to_numpy(a ) else: __lowerCamelCase = [to_numpy(a ) for v in value] # Convert padding_strategy in PaddingStrategy __lowerCamelCase = self._get_padding_strategies(padding=a , max_length=a ) __lowerCamelCase = processed_features[self.model_input_names[0]] __lowerCamelCase = len(a ) if not all(len(a ) == batch_size for v in processed_features.values() ): raise ValueError('''Some items in the output dictionary have a different batch size than others.''' ) __lowerCamelCase = [] for i in range(a ): __lowerCamelCase = {k: v[i] for k, v in processed_features.items()} # truncation __lowerCamelCase = self._truncate( a , max_length=a , pad_to_multiple_of=a , truncation=a , ) truncated_inputs.append(a ) if padding_strategy == PaddingStrategy.LONGEST: # make sure that `max_length` cannot be longer than the longest truncated length __lowerCamelCase = max(len(input_slice[self.model_input_names[0]] ) for input_slice in truncated_inputs ) __lowerCamelCase = PaddingStrategy.MAX_LENGTH __lowerCamelCase = {} for i in range(a ): # padding __lowerCamelCase = self._pad( truncated_inputs[i] , max_length=a , padding_strategy=a , pad_to_multiple_of=a , return_attention_mask=a , ) for key, value in outputs.items(): if key not in batch_outputs: __lowerCamelCase = [] if value.dtype is np.dtype(np.floataa ): __lowerCamelCase = value.astype(np.floataa ) batch_outputs[key].append(a ) return BatchFeature(a , tensor_type=a ) def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : Union[Dict[str, np.ndarray], BatchFeature] , a : Optional[int] = None , a : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , a : Optional[int] = None , a : Optional[bool] = None , ): """simple docstring""" __lowerCamelCase = processed_features[self.model_input_names[0]] if padding_strategy == PaddingStrategy.LONGEST: __lowerCamelCase = len(a ) if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): __lowerCamelCase = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of __lowerCamelCase = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(a ) < max_length if return_attention_mask and "attention_mask" not in processed_features: __lowerCamelCase = np.ones(len(a ) , dtype=np.intaa ) if needs_to_be_padded: __lowerCamelCase = max_length - len(a ) if self.padding_side == "right": if return_attention_mask: __lowerCamelCase = np.pad( processed_features['''attention_mask'''] , (0, difference) ) __lowerCamelCase = ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference) __lowerCamelCase = np.pad( a , a , '''constant''' , constant_values=self.padding_value ) elif self.padding_side == "left": if return_attention_mask: __lowerCamelCase = np.pad( processed_features['''attention_mask'''] , (difference, 0) ) __lowerCamelCase = ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0) __lowerCamelCase = np.pad( a , a , '''constant''' , constant_values=self.padding_value ) else: raise ValueError('''Invalid padding strategy:''' + str(self.padding_side ) ) return processed_features def SCREAMING_SNAKE_CASE__ ( self : int , a : Union[Dict[str, np.ndarray], BatchFeature] , a : Optional[int] = None , a : Optional[int] = None , a : Optional[bool] = None , ): """simple docstring""" if not truncation: return processed_features elif truncation and max_length is None: raise ValueError('''When setting ``truncation=True``, make sure that ``max_length`` is defined.''' ) __lowerCamelCase = processed_features[self.model_input_names[0]] # find `max_length` that fits `pad_to_multiple_of` if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): __lowerCamelCase = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of __lowerCamelCase = len(a ) > max_length if needs_to_be_truncated: __lowerCamelCase = processed_features[self.model_input_names[0]][:max_length] if "attention_mask" in processed_features: __lowerCamelCase = processed_features['''attention_mask'''][:max_length] return processed_features def SCREAMING_SNAKE_CASE__ ( self : Any , a : Any=False , a : Any=None ): """simple docstring""" if padding is not False: if padding is True: __lowerCamelCase = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch elif not isinstance(a , a ): __lowerCamelCase = PaddingStrategy(a ) elif isinstance(a , a ): __lowerCamelCase = padding else: __lowerCamelCase = PaddingStrategy.DO_NOT_PAD # Set max length if needed if max_length is None: if padding_strategy == PaddingStrategy.MAX_LENGTH: raise ValueError( f"""When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined""" ) # Test if we have a padding value if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None): raise ValueError( '''Asking to pad but the feature_extractor does not have a padding value. Please select a value to use''' ''' as `padding_value`. For example: `feature_extractor.padding_value = 0.0`.''' ) return padding_strategy
67
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase ={ "configuration_time_series_transformer": [ "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimeSeriesTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase =[ "TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimeSeriesTransformerForPrediction", "TimeSeriesTransformerModel", "TimeSeriesTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys __UpperCAmelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
67
1
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from importlib import import_module from typing import Dict, List, Optional, Tuple import numpy as np from seqeval.metrics import accuracy_score, fa_score, precision_score, recall_score from torch import nn from utils_ner import Split, TokenClassificationDataset, TokenClassificationTask import transformers from transformers import ( AutoConfig, AutoModelForTokenClassification, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process __UpperCAmelCase =logging.getLogger(__name__) @dataclass class a__ : lowerCamelCase : str =field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) lowerCamelCase : Optional[str] =field( default=UpperCAmelCase__ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) lowerCamelCase : Optional[str] =field( default="NER" , metadata={"help": "Task type to fine tune in training (e.g. NER, POS, etc)"} ) lowerCamelCase : Optional[str] =field( default=UpperCAmelCase__ , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) lowerCamelCase : bool =field(default=UpperCAmelCase__ , metadata={"help": "Set this flag to use fast tokenization."} ) # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script, # or just modify its tokenizer_config.json. lowerCamelCase : Optional[str] =field( default=UpperCAmelCase__ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) @dataclass class a__ : lowerCamelCase : str =field( metadata={"help": "The input data dir. Should contain the .txt files for a CoNLL-2003-formatted task."} ) lowerCamelCase : Optional[str] =field( default=UpperCAmelCase__ , metadata={"help": "Path to a file containing all labels. If not specified, CoNLL-2003 labels are used."} , ) lowerCamelCase : int =field( default=1_2_8 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) lowerCamelCase : bool =field( default=UpperCAmelCase__ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def __lowerCAmelCase ( ) -> int: # 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. __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() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. Use""" ''' --overwrite_output_dir to overcome.''' ) __lowerCamelCase = import_module('''tasks''' ) try: __lowerCamelCase = getattr(UpperCamelCase__ , model_args.task_type ) __lowerCamelCase = token_classification_task_clazz() except AttributeError: raise ValueError( f"""Task {model_args.task_type} needs to be defined as a TokenClassificationTask subclass in {module}. """ f"""Available tasks classes are: {TokenClassificationTask.__subclasses__()}""" ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( '''Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s''' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('''Training/evaluation parameters %s''' , UpperCamelCase__ ) # Set seed set_seed(training_args.seed ) # Prepare CONLL-2003 task __lowerCamelCase = token_classification_task.get_labels(data_args.labels ) __lowerCamelCase = dict(enumerate(UpperCamelCase__ ) ) __lowerCamelCase = len(UpperCamelCase__ ) # Load pretrained model and tokenizer # # 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=UpperCamelCase__ , idalabel=UpperCamelCase__ , labelaid={label: i for i, label in enumerate(UpperCamelCase__ )} , cache_dir=model_args.cache_dir , ) __lowerCamelCase = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast , ) __lowerCamelCase = AutoModelForTokenClassification.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 , ) # Get datasets __lowerCamelCase = ( TokenClassificationDataset( token_classification_task=UpperCamelCase__ , data_dir=data_args.data_dir , tokenizer=UpperCamelCase__ , labels=UpperCamelCase__ , model_type=config.model_type , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) __lowerCamelCase = ( TokenClassificationDataset( token_classification_task=UpperCamelCase__ , data_dir=data_args.data_dir , tokenizer=UpperCamelCase__ , labels=UpperCamelCase__ , model_type=config.model_type , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.dev , ) if training_args.do_eval else None ) def align_predictions(UpperCamelCase__ , UpperCamelCase__ ) -> Tuple[List[int], List[int]]: __lowerCamelCase = np.argmax(UpperCamelCase__ , axis=2 ) __lowerCamelCase , __lowerCamelCase = preds.shape __lowerCamelCase = [[] for _ in range(UpperCamelCase__ )] __lowerCamelCase = [[] for _ in range(UpperCamelCase__ )] for i in range(UpperCamelCase__ ): for j in range(UpperCamelCase__ ): if label_ids[i, j] != nn.CrossEntropyLoss().ignore_index: out_label_list[i].append(label_map[label_ids[i][j]] ) preds_list[i].append(label_map[preds[i][j]] ) return preds_list, out_label_list def compute_metrics(UpperCamelCase__ ) -> Dict: __lowerCamelCase , __lowerCamelCase = align_predictions(p.predictions , p.label_ids ) return { "accuracy_score": accuracy_score(UpperCamelCase__ , UpperCamelCase__ ), "precision": precision_score(UpperCamelCase__ , UpperCamelCase__ ), "recall": recall_score(UpperCamelCase__ , UpperCamelCase__ ), "f1": fa_score(UpperCamelCase__ , UpperCamelCase__ ), } # Data collator __lowerCamelCase = DataCollatorWithPadding(UpperCamelCase__ , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer __lowerCamelCase = Trainer( model=UpperCamelCase__ , args=UpperCamelCase__ , train_dataset=UpperCamelCase__ , eval_dataset=UpperCamelCase__ , compute_metrics=UpperCamelCase__ , data_collator=UpperCamelCase__ , ) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_process_zero(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation __lowerCamelCase = {} if training_args.do_eval: logger.info('''*** Evaluate ***''' ) __lowerCamelCase = trainer.evaluate() __lowerCamelCase = os.path.join(training_args.output_dir , '''eval_results.txt''' ) if trainer.is_world_process_zero(): with open(UpperCamelCase__ , '''w''' ) as writer: logger.info('''***** Eval results *****''' ) for key, value in result.items(): logger.info(''' %s = %s''' , UpperCamelCase__ , UpperCamelCase__ ) writer.write('''%s = %s\n''' % (key, value) ) results.update(UpperCamelCase__ ) # Predict if training_args.do_predict: __lowerCamelCase = TokenClassificationDataset( token_classification_task=UpperCamelCase__ , data_dir=data_args.data_dir , tokenizer=UpperCamelCase__ , labels=UpperCamelCase__ , model_type=config.model_type , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.test , ) __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = trainer.predict(UpperCamelCase__ ) __lowerCamelCase , __lowerCamelCase = align_predictions(UpperCamelCase__ , UpperCamelCase__ ) __lowerCamelCase = os.path.join(training_args.output_dir , '''test_results.txt''' ) if trainer.is_world_process_zero(): with open(UpperCamelCase__ , '''w''' ) as writer: for key, value in metrics.items(): logger.info(''' %s = %s''' , UpperCamelCase__ , UpperCamelCase__ ) writer.write('''%s = %s\n''' % (key, value) ) # Save predictions __lowerCamelCase = os.path.join(training_args.output_dir , '''test_predictions.txt''' ) if trainer.is_world_process_zero(): with open(UpperCamelCase__ , '''w''' ) as writer: with open(os.path.join(data_args.data_dir , '''test.txt''' ) , '''r''' ) as f: token_classification_task.write_predictions_to_file(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) return results def __lowerCAmelCase ( UpperCamelCase__ ) -> str: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
67
'''simple docstring''' import warnings from pathlib import Path from typing import List, Tuple, Union import fire from torch import nn from transformers import AutoModelForSeqaSeqLM, AutoTokenizer, PreTrainedModel from transformers.utils import logging __UpperCAmelCase =logging.get_logger(__name__) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> None: __lowerCamelCase = nn.ModuleList([src_layers[i] for i in layers_to_copy] ) assert len(UpperCamelCase__ ) == len(UpperCamelCase__ ), f"""{len(UpperCamelCase__ )} != {len(UpperCamelCase__ )}""" dest_layers.load_state_dict(layers_to_copy.state_dict() ) __UpperCAmelCase ={ # maps num layers in teacher -> num_layers in student -> which teacher layers to copy. # 12: bart, 16: pegasus, 6: marian/Helsinki-NLP 1_2: { 1: [0], # This says that if the teacher has 12 layers and the student has 1, copy layer 0 of the teacher 2: [0, 6], 3: [0, 6, 1_1], 4: [0, 4, 8, 1_1], 6: [0, 2, 4, 7, 9, 1_1], 9: [0, 1, 2, 4, 5, 7, 9, 1_0, 1_1], 1_2: list(range(1_2)), }, 1_6: { # maps num layers in student -> which teacher layers to copy 1: [0], 2: [0, 1_5], 3: [0, 8, 1_5], 4: [0, 5, 1_0, 1_5], 6: [0, 3, 6, 9, 1_2, 1_5], 8: [0, 2, 4, 6, 8, 1_0, 1_2, 1_5], 9: [0, 1, 3, 5, 7, 9, 1_1, 1_3, 1_5], 1_2: [0, 1, 2, 3, 4, 5, 6, 7, 9, 1_1, 1_3, 1_5], 1_6: list(range(1_6)), }, 6: {1: [0], 2: [0, 5], 3: [0, 2, 5], 4: [0, 1, 3, 5], 6: list(range(6))}, } __UpperCAmelCase ={ # maps num layers in student -> which teacher layers to copy. 6: {1: [5], 2: [3, 5], 3: [1, 4, 5], 4: [1, 2, 4, 5]}, 1_2: {1: [1_1], 2: [5, 1_1], 3: [3, 7, 1_1], 6: [1, 3, 5, 8, 1_0, 1_1]}, 1_6: {1: [1_5], 4: [4, 9, 1_2, 1_5], 8: [1, 3, 5, 7, 9, 1_1, 1_3, 1_5]}, } def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Union[str, Any]: try: __lowerCamelCase = LAYERS_TO_COPY[n_teacher][n_student] return val except KeyError: if n_student != n_teacher: warnings.warn( f"""no hardcoded layers to copy for teacher {n_teacher} -> student {n_student}, defaulting to first""" f""" {n_student}""" ) return list(range(UpperCamelCase__ ) ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> List[int]: if n_student > n_teacher: raise ValueError(f"""Cannot perform intermediate supervision for student {n_student} > teacher {n_teacher}""" ) elif n_teacher == n_student: return list(range(UpperCamelCase__ ) ) elif n_student == 1: return [n_teacher - 1] else: return LAYERS_TO_SUPERVISE[n_teacher][n_student] def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ = "student" , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__=False , UpperCamelCase__=None , UpperCamelCase__=None , **UpperCamelCase__ , ) -> Tuple[PreTrainedModel, List[int], List[int]]: __lowerCamelCase = '''encoder_layers and decoder_layers cannot be both None-- you would just have an identical teacher.''' assert (e is not None) or (d is not None), _msg if isinstance(UpperCamelCase__ , UpperCamelCase__ ): AutoTokenizer.from_pretrained(UpperCamelCase__ ).save_pretrained(UpperCamelCase__ ) # purely for convenience __lowerCamelCase = AutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__ ).eval() else: assert isinstance(UpperCamelCase__ , UpperCamelCase__ ), f"""teacher must be a model or string got type {type(UpperCamelCase__ )}""" __lowerCamelCase = teacher.config.to_diff_dict() try: __lowerCamelCase , __lowerCamelCase = teacher.config.encoder_layers, teacher.config.decoder_layers if e is None: __lowerCamelCase = teacher_e if d is None: __lowerCamelCase = teacher_d init_kwargs.update({'''encoder_layers''': e, '''decoder_layers''': d} ) except AttributeError: # T5 if hasattr(teacher.config , '''num_encoder_layers''' ): __lowerCamelCase , __lowerCamelCase = teacher.config.num_encoder_layers, teacher.config.num_decoder_layers else: __lowerCamelCase , __lowerCamelCase = teacher.config.num_layers, teacher.config.num_decoder_layers if e is None: __lowerCamelCase = teacher_e if d is None: __lowerCamelCase = teacher_d if hasattr(teacher.config , '''num_encoder_layers''' ): init_kwargs.update({'''num_encoder_layers''': e, '''num_decoder_layers''': d} ) else: init_kwargs.update({'''num_layers''': e, '''num_decoder_layers''': d} ) # Kwargs to instantiate student: teacher kwargs with updated layer numbers + **extra_config_kwargs init_kwargs.update(UpperCamelCase__ ) # Copy weights __lowerCamelCase = teacher.config_class(**UpperCamelCase__ ) __lowerCamelCase = AutoModelForSeqaSeqLM.from_config(UpperCamelCase__ ) # Start by copying the full teacher state dict this will copy the first N teacher layers to the student. __lowerCamelCase = student.load_state_dict(teacher.state_dict() , strict=UpperCamelCase__ ) assert info.missing_keys == [], info.missing_keys # every student key should have a teacher keys. if copy_first_teacher_layers: # Our copying is done. We just log and save __lowerCamelCase , __lowerCamelCase = list(range(UpperCamelCase__ ) ), list(range(UpperCamelCase__ ) ) logger.info( f"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to""" f""" {save_path}""" ) student.save_pretrained(UpperCamelCase__ ) return student, e_layers_to_copy, d_layers_to_copy # Decide which layers of the teacher to copy. Not exactly alternating -- we try to keep first and last layer. if e_layers_to_copy is None: __lowerCamelCase = pick_layers_to_copy(UpperCamelCase__ , UpperCamelCase__ ) if d_layers_to_copy is None: __lowerCamelCase = pick_layers_to_copy(UpperCamelCase__ , UpperCamelCase__ ) try: if hasattr( UpperCamelCase__ , '''prophetnet''' ): # For ProphetNet, student.model.encoder.layers is called student.prophetnet.encoder.layers copy_layers(teacher.prophetnet.encoder.layers , student.prophetnet.encoder.layers , UpperCamelCase__ ) copy_layers(teacher.prophetnet.decoder.layers , student.prophetnet.decoder.layers , UpperCamelCase__ ) else: copy_layers(teacher.model.encoder.layers , student.model.encoder.layers , UpperCamelCase__ ) copy_layers(teacher.model.decoder.layers , student.model.decoder.layers , UpperCamelCase__ ) except AttributeError: # For t5, student.model.encoder.layers is called student.encoder.block copy_layers(teacher.encoder.block , student.encoder.block , UpperCamelCase__ ) copy_layers(teacher.decoder.block , student.decoder.block , UpperCamelCase__ ) logger.info( f"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to {save_path}""" ) __lowerCamelCase = { '''teacher_type''': teacher.config.model_type, '''copied_encoder_layers''': e_layers_to_copy, '''copied_decoder_layers''': d_layers_to_copy, } student.save_pretrained(UpperCamelCase__ ) # Save information about copying for easier reproducibility return student, e_layers_to_copy, d_layers_to_copy if __name__ == "__main__": fire.Fire(create_student_by_copying_alternating_layers)
67
1
'''simple docstring''' import PIL.Image import PIL.ImageOps from packaging import version from PIL import Image if version.parse(version.parse(PIL.__version__).base_version) >= version.parse("9.1.0"): __UpperCAmelCase ={ "linear": PIL.Image.Resampling.BILINEAR, "bilinear": PIL.Image.Resampling.BILINEAR, "bicubic": PIL.Image.Resampling.BICUBIC, "lanczos": PIL.Image.Resampling.LANCZOS, "nearest": PIL.Image.Resampling.NEAREST, } else: __UpperCAmelCase ={ "linear": PIL.Image.LINEAR, "bilinear": PIL.Image.BILINEAR, "bicubic": PIL.Image.BICUBIC, "lanczos": PIL.Image.LANCZOS, "nearest": PIL.Image.NEAREST, } def __lowerCAmelCase ( UpperCamelCase__ ) -> Tuple: __lowerCamelCase = (images / 2 + 0.5).clamp(0 , 1 ) __lowerCamelCase = images.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() __lowerCamelCase = numpy_to_pil(UpperCamelCase__ ) return images def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[Any]: if images.ndim == 3: __lowerCamelCase = images[None, ...] __lowerCamelCase = (images * 2_55).round().astype('''uint8''' ) if images.shape[-1] == 1: # special case for grayscale (single channel) images __lowerCamelCase = [Image.fromarray(image.squeeze() , mode='''L''' ) for image in images] else: __lowerCamelCase = [Image.fromarray(UpperCamelCase__ ) for image in images] return pil_images
67
'''simple docstring''' import unittest from pathlib import Path from tempfile import TemporaryDirectory from transformers import AutoConfig, TFGPTaLMHeadModel, is_keras_nlp_available, is_tf_available from transformers.models.gpta.tokenization_gpta import GPTaTokenizer from transformers.testing_utils import require_keras_nlp, require_tf, slow if is_tf_available(): import tensorflow as tf if is_keras_nlp_available(): from transformers.models.gpta import TFGPTaTokenizer __UpperCAmelCase =["gpt2"] __UpperCAmelCase ="gpt2" if is_tf_available(): class a__ ( tf.Module ): def __init__( self : str , a : Union[str, Any] ): """simple docstring""" super().__init__() __lowerCamelCase = tokenizer __lowerCamelCase = AutoConfig.from_pretrained(a ) __lowerCamelCase = TFGPTaLMHeadModel.from_config(a ) @tf.function(input_signature=(tf.TensorSpec((None,) , tf.string , name='''text''' ),) ) def SCREAMING_SNAKE_CASE__ ( self : str , a : Tuple ): """simple docstring""" __lowerCamelCase = self.tokenizer(a ) __lowerCamelCase = tokenized['''input_ids'''].to_tensor() __lowerCamelCase = tf.cast(input_ids_dense > 0 , tf.intaa ) # input_mask = tf.reshape(input_mask, [-1, MAX_SEQ_LEN]) __lowerCamelCase = self.model(input_ids=a , attention_mask=a )['''logits'''] return outputs @require_tf @require_keras_nlp class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" super().setUp() __lowerCamelCase = [GPTaTokenizer.from_pretrained(a ) for checkpoint in (TOKENIZER_CHECKPOINTS)] __lowerCamelCase = [TFGPTaTokenizer.from_pretrained(a ) for checkpoint in TOKENIZER_CHECKPOINTS] assert len(self.tokenizers ) == len(self.tf_tokenizers ) __lowerCamelCase = [ '''This is a straightforward English test sentence.''', '''This one has some weird characters\rto\nsee\r\nif those\u00E9break things.''', '''Now we\'re going to add some Chinese: 一 二 三 一二三''', '''And some much more rare Chinese: 齉 堃 齉堃''', '''Je vais aussi écrire en français pour tester les accents''', '''Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ''', ] __lowerCamelCase = list(zip(self.test_sentences , self.test_sentences[::-1] ) ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" for tokenizer, tf_tokenizer in zip(self.tokenizers , self.tf_tokenizers ): for test_inputs in self.test_sentences: __lowerCamelCase = tokenizer([test_inputs] , return_tensors='''tf''' ) __lowerCamelCase = tf_tokenizer([test_inputs] ) for key in python_outputs.keys(): # convert them to numpy to avoid messing with ragged tensors __lowerCamelCase = python_outputs[key].numpy() __lowerCamelCase = tf_outputs[key].numpy() self.assertTrue(tf.reduce_all(python_outputs_values.shape == tf_outputs_values.shape ) ) self.assertTrue(tf.reduce_all(tf.cast(a , tf.intaa ) == tf_outputs_values ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: __lowerCamelCase = tf.function(a ) for test_inputs in self.test_sentences: __lowerCamelCase = tf.constant(a ) __lowerCamelCase = compiled_tokenizer(a ) __lowerCamelCase = tf_tokenizer(a ) for key in eager_outputs.keys(): self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key] ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: __lowerCamelCase = ModelToSave(tokenizer=a ) __lowerCamelCase = tf.convert_to_tensor([self.test_sentences[0]] ) __lowerCamelCase = model.serving(a ) # Build model with some sample inputs with TemporaryDirectory() as tempdir: __lowerCamelCase = Path(a ) / '''saved.model''' tf.saved_model.save(a , a , signatures={'''serving_default''': model.serving} ) __lowerCamelCase = tf.saved_model.load(a ) __lowerCamelCase = loaded_model.signatures['''serving_default'''](a )['''output_0'''] # We may see small differences because the loaded model is compiled, so we need an epsilon for the test self.assertTrue(tf.reduce_all(out == loaded_output ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: __lowerCamelCase = tf.convert_to_tensor([self.test_sentences[0]] ) __lowerCamelCase = tf_tokenizer(a ) # Build model with some sample inputs __lowerCamelCase = tf_tokenizer.get_config() __lowerCamelCase = TFGPTaTokenizer.from_config(a ) __lowerCamelCase = model_from_config(a ) for key in from_config_output.keys(): self.assertTrue(tf.reduce_all(from_config_output[key] == out[key] ) ) @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" for tf_tokenizer in self.tf_tokenizers: # for the test to run __lowerCamelCase = 12_31_23 for max_length in [3, 5, 10_24]: __lowerCamelCase = tf.convert_to_tensor([self.test_sentences[0]] ) __lowerCamelCase = tf_tokenizer(a , max_length=a ) __lowerCamelCase = out['''input_ids'''].numpy().shape[1] assert out_length == max_length
67
1
'''simple docstring''' import warnings from typing import Dict, List, Optional, Tuple from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __UpperCAmelCase =logging.get_logger(__name__) class a__ ( UpperCAmelCase__ ): lowerCamelCase : List[Any] =["input_ids", "attention_mask"] def __init__( self : Tuple , a : Optional[Any]="</s>" , a : Dict="<unk>" , a : List[Any]="<pad>" , a : List[str]=1_25 , a : Optional[Any]=None , **a : Any , ): """simple docstring""" if extra_ids > 0 and additional_special_tokens is None: __lowerCamelCase = [f"""<extra_id_{i}>""" for i in range(a )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra_id special tokens __lowerCamelCase = len(set(filter(lambda a : bool('''extra_id''' in str(a ) ) , a ) ) ) if extra_tokens != extra_ids: raise ValueError( f"""Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are""" ''' provided to ByT5Tokenizer. In this case the additional_special_tokens must include the''' ''' extra_ids tokens''' ) __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else pad_token __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else eos_token __lowerCamelCase = AddedToken(a , lstrip=a , rstrip=a ) if isinstance(a , a ) else unk_token super().__init__( eos_token=a , unk_token=a , pad_token=a , extra_ids=a , additional_special_tokens=a , **a , ) __lowerCamelCase = extra_ids __lowerCamelCase = 2**8 # utf is 8 bits # define special tokens dict __lowerCamelCase = { self.pad_token: 0, self.eos_token: 1, self.unk_token: 2, } __lowerCamelCase = len(self.special_tokens_encoder ) __lowerCamelCase = len(a ) for i, token in enumerate(a ): __lowerCamelCase = self.vocab_size + i - n __lowerCamelCase = {v: k for k, v in self.special_tokens_encoder.items()} @property def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" return self._utf_vocab_size + self._num_special_tokens + self._extra_ids def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : List[int] , a : Optional[List[int]] = None , a : bool = False ): """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=a , token_ids_a=a , already_has_special_tokens=a ) # normal case: some special tokens if token_ids_a is None: return ([0] * len(a )) + [1] return ([0] * len(a )) + [1] + ([0] * len(a )) + [1] def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : List[int] ): """simple docstring""" if len(a ) > 0 and token_ids[-1] == self.eos_token_id: warnings.warn( f"""This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated""" ''' eos tokens being added.''' ) return token_ids else: return token_ids + [self.eos_token_id] def SCREAMING_SNAKE_CASE__ ( self : Any , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" __lowerCamelCase = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos ) * [0] return len(token_ids_a + eos + token_ids_a + eos ) * [0] def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , a : List[int] , a : Optional[List[int]] = None ): """simple docstring""" __lowerCamelCase = self._add_eos_if_not_present(a ) if token_ids_a is None: return token_ids_a else: __lowerCamelCase = self._add_eos_if_not_present(a ) return token_ids_a + token_ids_a def SCREAMING_SNAKE_CASE__ ( self : str , a : str ): """simple docstring""" __lowerCamelCase = [chr(a ) for i in text.encode('''utf-8''' )] return tokens def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , a : Tuple ): """simple docstring""" if token in self.special_tokens_encoder: __lowerCamelCase = self.special_tokens_encoder[token] elif token in self.added_tokens_encoder: __lowerCamelCase = self.added_tokens_encoder[token] elif len(a ) != 1: __lowerCamelCase = self.unk_token_id else: __lowerCamelCase = ord(a ) + self._num_special_tokens return token_id def SCREAMING_SNAKE_CASE__ ( self : int , a : Dict ): """simple docstring""" if index in self.special_tokens_decoder: __lowerCamelCase = self.special_tokens_decoder[index] else: __lowerCamelCase = chr(index - self._num_special_tokens ) return token def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , a : Dict ): """simple docstring""" __lowerCamelCase = b'''''' for token in tokens: if token in self.special_tokens_decoder: __lowerCamelCase = self.special_tokens_decoder[token].encode('''utf-8''' ) elif token in self.added_tokens_decoder: __lowerCamelCase = self.special_tokens_decoder[token].encode('''utf-8''' ) elif token in self.special_tokens_encoder: __lowerCamelCase = token.encode('''utf-8''' ) elif token in self.added_tokens_encoder: __lowerCamelCase = token.encode('''utf-8''' ) else: __lowerCamelCase = bytes([ord(a )] ) bstring += tok_string __lowerCamelCase = bstring.decode('''utf-8''' , errors='''ignore''' ) return string def SCREAMING_SNAKE_CASE__ ( self : Tuple , a : str , a : Optional[str] = None ): """simple docstring""" return ()
67
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Optional[int]: if b == 0: return 1 if (b % 2) == 0: return actual_power(UpperCamelCase__ , int(b / 2 ) ) * actual_power(UpperCamelCase__ , int(b / 2 ) ) else: return a * actual_power(UpperCamelCase__ , int(b / 2 ) ) * actual_power(UpperCamelCase__ , int(b / 2 ) ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> float: if b < 0: return 1 / actual_power(UpperCamelCase__ , UpperCamelCase__ ) return actual_power(UpperCamelCase__ , UpperCamelCase__ ) if __name__ == "__main__": print(power(-2, -3))
67
1
'''simple docstring''' def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> int: while second != 0: __lowerCamelCase = first & second first ^= second __lowerCamelCase = c << 1 return first if __name__ == "__main__": import doctest doctest.testmod() __UpperCAmelCase =int(input("Enter the first number: ").strip()) __UpperCAmelCase =int(input("Enter the second number: ").strip()) print(f'{add(first, second) = }')
67
'''simple docstring''' import logging import os from .state import PartialState class a__ ( logging.LoggerAdapter ): @staticmethod def SCREAMING_SNAKE_CASE__ ( a : Optional[Any] ): """simple docstring""" __lowerCamelCase = PartialState() return not main_process_only or (main_process_only and state.is_main_process) def SCREAMING_SNAKE_CASE__ ( self : int , a : Optional[int] , a : str , *a : Optional[int] , **a : List[Any] ): """simple docstring""" if PartialState._shared_state == {}: raise RuntimeError( '''You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.''' ) __lowerCamelCase = kwargs.pop('''main_process_only''' , a ) __lowerCamelCase = kwargs.pop('''in_order''' , a ) if self.isEnabledFor(a ): if self._should_log(a ): __lowerCamelCase , __lowerCamelCase = self.process(a , a ) self.logger.log(a , a , *a , **a ) elif in_order: __lowerCamelCase = PartialState() for i in range(state.num_processes ): if i == state.process_index: __lowerCamelCase , __lowerCamelCase = self.process(a , a ) self.logger.log(a , a , *a , **a ) state.wait_for_everyone() def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ = None ) -> Optional[int]: if log_level is None: __lowerCamelCase = os.environ.get('''ACCELERATE_LOG_LEVEL''' , UpperCamelCase__ ) __lowerCamelCase = logging.getLogger(UpperCamelCase__ ) if log_level is not None: logger.setLevel(log_level.upper() ) logger.root.setLevel(log_level.upper() ) return MultiProcessAdapter(UpperCamelCase__ , {} )
67
1