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
import os import unittest from tempfile import TemporaryDirectory import torch import torch.nn as nn from accelerate.utils import ( OffloadedWeightsLoader, extract_submodules_state_dict, load_offloaded_weight, offload_state_dict, offload_weight, ) class __UpperCamelCase ( nn.Module ): """simple docstring""" def __init__( self : Optional[int] ): """simple docstring""" super().__init__() __SCREAMING_SNAKE_CASE : int = nn.Linear(3 , 4 ) __SCREAMING_SNAKE_CASE : int = nn.BatchNormad(4 ) __SCREAMING_SNAKE_CASE : Tuple = nn.Linear(4 , 5 ) def UpperCAmelCase__ ( self : Tuple , _A : Union[str, Any] ): """simple docstring""" return self.lineara(self.batchnorm(self.lineara(_A ) ) ) class __UpperCamelCase ( unittest.TestCase ): """simple docstring""" def UpperCAmelCase__ ( self : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = ModelForTest() with TemporaryDirectory() as tmp_dir: offload_state_dict(_A , model.state_dict() ) __SCREAMING_SNAKE_CASE : Optional[int] = os.path.join(_A , '''index.json''' ) self.assertTrue(os.path.isfile(_A ) ) # TODO: add tests on what is inside the index for key in ["linear1.weight", "linear1.bias", "linear2.weight", "linear2.bias"]: __SCREAMING_SNAKE_CASE : Any = os.path.join(_A , F'''{key}.dat''' ) self.assertTrue(os.path.isfile(_A ) ) # TODO: add tests on the fact weights are properly loaded def UpperCAmelCase__ ( self : Tuple ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = [torch.floataa, torch.floataa, torch.bfloataa] for dtype in dtypes: __SCREAMING_SNAKE_CASE : Optional[int] = torch.randn(2 , 3 , dtype=_A ) with TemporaryDirectory() as tmp_dir: __SCREAMING_SNAKE_CASE : Any = offload_weight(_A , '''weight''' , _A , {} ) __SCREAMING_SNAKE_CASE : Dict = os.path.join(_A , '''weight.dat''' ) self.assertTrue(os.path.isfile(_A ) ) self.assertDictEqual(_A , {'''weight''': {'''shape''': [2, 3], '''dtype''': str(_A ).split('''.''' )[1]}} ) __SCREAMING_SNAKE_CASE : str = load_offloaded_weight(_A , index['''weight'''] ) self.assertTrue(torch.equal(_A , _A ) ) def UpperCAmelCase__ ( self : List[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = ModelForTest() __SCREAMING_SNAKE_CASE : Dict = model.state_dict() __SCREAMING_SNAKE_CASE : str = {k: v for k, v in state_dict.items() if '''linear2''' not in k} __SCREAMING_SNAKE_CASE : int = {k: v for k, v in state_dict.items() if '''linear2''' in k} with TemporaryDirectory() as tmp_dir: offload_state_dict(_A , _A ) __SCREAMING_SNAKE_CASE : List[Any] = OffloadedWeightsLoader(state_dict=_A , save_folder=_A ) # Every key is there with the right value self.assertEqual(sorted(_A ) , sorted(state_dict.keys() ) ) for key, param in state_dict.items(): self.assertTrue(torch.allclose(_A , weight_map[key] ) ) __SCREAMING_SNAKE_CASE : int = {k: v for k, v in state_dict.items() if '''weight''' in k} __SCREAMING_SNAKE_CASE : str = {k: v for k, v in state_dict.items() if '''weight''' not in k} with TemporaryDirectory() as tmp_dir: offload_state_dict(_A , _A ) __SCREAMING_SNAKE_CASE : Dict = OffloadedWeightsLoader(state_dict=_A , save_folder=_A ) # Every key is there with the right value self.assertEqual(sorted(_A ) , sorted(state_dict.keys() ) ) for key, param in state_dict.items(): self.assertTrue(torch.allclose(_A , weight_map[key] ) ) with TemporaryDirectory() as tmp_dir: offload_state_dict(_A , _A ) # Duplicates are removed __SCREAMING_SNAKE_CASE : Dict = OffloadedWeightsLoader(state_dict=_A , save_folder=_A ) # Every key is there with the right value self.assertEqual(sorted(_A ) , sorted(state_dict.keys() ) ) for key, param in state_dict.items(): self.assertTrue(torch.allclose(_A , weight_map[key] ) ) def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = {'''a.1''': 0, '''a.10''': 1, '''a.2''': 2} __SCREAMING_SNAKE_CASE : Tuple = extract_submodules_state_dict(_A , ['''a.1''', '''a.2'''] ) self.assertDictEqual(_A , {'''a.1''': 0, '''a.2''': 2} ) __SCREAMING_SNAKE_CASE : Union[str, Any] = {'''a.1.a''': 0, '''a.10.a''': 1, '''a.2.a''': 2} __SCREAMING_SNAKE_CASE : Union[str, Any] = extract_submodules_state_dict(_A , ['''a.1''', '''a.2'''] ) self.assertDictEqual(_A , {'''a.1.a''': 0, '''a.2.a''': 2} )
303
import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem lowercase_ = importlib.util.find_spec("""s3fs""") is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 lowercase_ = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(f'''A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.''') fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def a__ ( snake_case ): """simple docstring""" if "://" in dataset_path: __SCREAMING_SNAKE_CASE : Any = dataset_path.split('''://''' )[1] return dataset_path def a__ ( snake_case ): """simple docstring""" if fs is not None and fs.protocol != "file": return True else: return False def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = not is_remote_filesystem(snake_case ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(snake_case ) , fs._strip_protocol(snake_case ) ) else: fs.mv(snake_case , snake_case , recursive=snake_case ) def a__ ( ): """simple docstring""" if hasattr(fsspec.asyn , '''reset_lock''' ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: __SCREAMING_SNAKE_CASE : int = None __SCREAMING_SNAKE_CASE : Union[str, Any] = None __SCREAMING_SNAKE_CASE : Union[str, Any] = threading.Lock()
303
1
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 lowercase_ = collections.namedtuple("""_Datasets""", ["""train""", """validation""", """test"""]) # CVDF mirror of http://yann.lecun.com/exdb/mnist/ lowercase_ = """https://storage.googleapis.com/cvdf-datasets/mnist/""" def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = numpy.dtype(numpy.uintaa ).newbyteorder('''>''' ) return numpy.frombuffer(bytestream.read(4 ) , dtype=snake_case )[0] @deprecated(snake_case , '''Please use tf.data to implement this functionality.''' ) def a__ ( snake_case ): """simple docstring""" print('''Extracting''' , f.name ) with gzip.GzipFile(fileobj=snake_case ) as bytestream: __SCREAMING_SNAKE_CASE : List[str] = _readaa(snake_case ) if magic != 2_051: raise ValueError( '''Invalid magic number %d in MNIST image file: %s''' % (magic, f.name) ) __SCREAMING_SNAKE_CASE : str = _readaa(snake_case ) __SCREAMING_SNAKE_CASE : int = _readaa(snake_case ) __SCREAMING_SNAKE_CASE : Tuple = _readaa(snake_case ) __SCREAMING_SNAKE_CASE : List[str] = bytestream.read(rows * cols * num_images ) __SCREAMING_SNAKE_CASE : Union[str, Any] = numpy.frombuffer(snake_case , dtype=numpy.uinta ) __SCREAMING_SNAKE_CASE : Optional[Any] = data.reshape(snake_case , snake_case , snake_case , 1 ) return data @deprecated(snake_case , '''Please use tf.one_hot on tensors.''' ) def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = labels_dense.shape[0] __SCREAMING_SNAKE_CASE : Any = numpy.arange(snake_case ) * num_classes __SCREAMING_SNAKE_CASE : Union[str, Any] = numpy.zeros((num_labels, num_classes) ) __SCREAMING_SNAKE_CASE : Union[str, Any] = 1 return labels_one_hot @deprecated(snake_case , '''Please use tf.data to implement this functionality.''' ) def a__ ( snake_case , snake_case=False , snake_case=10 ): """simple docstring""" print('''Extracting''' , f.name ) with gzip.GzipFile(fileobj=snake_case ) as bytestream: __SCREAMING_SNAKE_CASE : str = _readaa(snake_case ) if magic != 2_049: raise ValueError( '''Invalid magic number %d in MNIST label file: %s''' % (magic, f.name) ) __SCREAMING_SNAKE_CASE : Union[str, Any] = _readaa(snake_case ) __SCREAMING_SNAKE_CASE : Optional[int] = bytestream.read(snake_case ) __SCREAMING_SNAKE_CASE : str = numpy.frombuffer(snake_case , dtype=numpy.uinta ) if one_hot: return _dense_to_one_hot(snake_case , snake_case ) return labels class __UpperCamelCase : """simple docstring""" @deprecated( _A , '''Please use alternatives such as official/mnist/_DataSet.py''' ''' from tensorflow/models.''' , ) def __init__( self : Optional[int] , _A : List[Any] , _A : List[str] , _A : List[Any]=False , _A : str=False , _A : int=dtypes.floataa , _A : Dict=True , _A : Dict=None , ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = 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 ) __SCREAMING_SNAKE_CASE : str = 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: __SCREAMING_SNAKE_CASE : Optional[int] = 1_0000 __SCREAMING_SNAKE_CASE : Tuple = one_hot else: assert ( images.shape[0] == labels.shape[0] ), F'''images.shape: {images.shape} labels.shape: {labels.shape}''' __SCREAMING_SNAKE_CASE : Any = 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 __SCREAMING_SNAKE_CASE : Any = images.reshape( images.shape[0] , images.shape[1] * images.shape[2] ) if dtype == dtypes.floataa: # Convert from [0, 255] -> [0.0, 1.0]. __SCREAMING_SNAKE_CASE : Tuple = images.astype(numpy.floataa ) __SCREAMING_SNAKE_CASE : List[Any] = numpy.multiply(_A , 1.0 / 2_55.0 ) __SCREAMING_SNAKE_CASE : int = images __SCREAMING_SNAKE_CASE : List[str] = labels __SCREAMING_SNAKE_CASE : Any = 0 __SCREAMING_SNAKE_CASE : int = 0 @property def UpperCAmelCase__ ( self : Optional[Any] ): """simple docstring""" return self._images @property def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" return self._labels @property def UpperCAmelCase__ ( self : Dict ): """simple docstring""" return self._num_examples @property def UpperCAmelCase__ ( self : Optional[Any] ): """simple docstring""" return self._epochs_completed def UpperCAmelCase__ ( self : Union[str, Any] , _A : List[Any] , _A : Optional[int]=False , _A : Dict=True ): """simple docstring""" if fake_data: __SCREAMING_SNAKE_CASE : Union[str, Any] = [1] * 784 __SCREAMING_SNAKE_CASE : List[str] = [1] + [0] * 9 if self.one_hot else 0 return ( [fake_image for _ in range(_A )], [fake_label for _ in range(_A )], ) __SCREAMING_SNAKE_CASE : Optional[int] = self._index_in_epoch # Shuffle for the first epoch if self._epochs_completed == 0 and start == 0 and shuffle: __SCREAMING_SNAKE_CASE : Optional[int] = numpy.arange(self._num_examples ) numpy.random.shuffle(_A ) __SCREAMING_SNAKE_CASE : Dict = self.images[perma] __SCREAMING_SNAKE_CASE : Any = 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 __SCREAMING_SNAKE_CASE : Optional[int] = self._num_examples - start __SCREAMING_SNAKE_CASE : List[str] = self._images[start : self._num_examples] __SCREAMING_SNAKE_CASE : Dict = self._labels[start : self._num_examples] # Shuffle the data if shuffle: __SCREAMING_SNAKE_CASE : int = numpy.arange(self._num_examples ) numpy.random.shuffle(_A ) __SCREAMING_SNAKE_CASE : int = self.images[perm] __SCREAMING_SNAKE_CASE : List[Any] = self.labels[perm] # Start next epoch __SCREAMING_SNAKE_CASE : Union[str, Any] = 0 __SCREAMING_SNAKE_CASE : Union[str, Any] = batch_size - rest_num_examples __SCREAMING_SNAKE_CASE : Optional[Any] = self._index_in_epoch __SCREAMING_SNAKE_CASE : List[Any] = self._images[start:end] __SCREAMING_SNAKE_CASE : List[Any] = 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 __SCREAMING_SNAKE_CASE : List[Any] = self._index_in_epoch return self._images[start:end], self._labels[start:end] @deprecated(snake_case , '''Please write your own downloading logic.''' ) def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" if not gfile.Exists(snake_case ): gfile.MakeDirs(snake_case ) __SCREAMING_SNAKE_CASE : Any = os.path.join(snake_case , snake_case ) if not gfile.Exists(snake_case ): urllib.request.urlretrieve(snake_case , snake_case ) # noqa: S310 with gfile.GFile(snake_case ) as f: __SCREAMING_SNAKE_CASE : Optional[Any] = f.size() print('''Successfully downloaded''' , snake_case , snake_case , '''bytes.''' ) return filepath @deprecated( snake_case , '''Please use alternatives such as:''' ''' tensorflow_datasets.load(\'mnist\')''' ) def a__ ( snake_case , snake_case=False , snake_case=False , snake_case=dtypes.floataa , snake_case=True , snake_case=5_000 , snake_case=None , snake_case=DEFAULT_SOURCE_URL , ): """simple docstring""" if fake_data: def fake(): return _DataSet( [] , [] , fake_data=snake_case , one_hot=snake_case , dtype=snake_case , seed=snake_case ) __SCREAMING_SNAKE_CASE : List[str] = fake() __SCREAMING_SNAKE_CASE : List[str] = fake() __SCREAMING_SNAKE_CASE : List[str] = fake() return _Datasets(train=snake_case , validation=snake_case , test=snake_case ) if not source_url: # empty string check __SCREAMING_SNAKE_CASE : int = DEFAULT_SOURCE_URL __SCREAMING_SNAKE_CASE : int = '''train-images-idx3-ubyte.gz''' __SCREAMING_SNAKE_CASE : int = '''train-labels-idx1-ubyte.gz''' __SCREAMING_SNAKE_CASE : Tuple = '''t10k-images-idx3-ubyte.gz''' __SCREAMING_SNAKE_CASE : List[Any] = '''t10k-labels-idx1-ubyte.gz''' __SCREAMING_SNAKE_CASE : Optional[int] = _maybe_download( snake_case , snake_case , source_url + train_images_file ) with gfile.Open(snake_case , '''rb''' ) as f: __SCREAMING_SNAKE_CASE : Any = _extract_images(snake_case ) __SCREAMING_SNAKE_CASE : int = _maybe_download( snake_case , snake_case , source_url + train_labels_file ) with gfile.Open(snake_case , '''rb''' ) as f: __SCREAMING_SNAKE_CASE : List[str] = _extract_labels(snake_case , one_hot=snake_case ) __SCREAMING_SNAKE_CASE : str = _maybe_download( snake_case , snake_case , source_url + test_images_file ) with gfile.Open(snake_case , '''rb''' ) as f: __SCREAMING_SNAKE_CASE : Optional[int] = _extract_images(snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = _maybe_download( snake_case , snake_case , source_url + test_labels_file ) with gfile.Open(snake_case , '''rb''' ) as f: __SCREAMING_SNAKE_CASE : str = _extract_labels(snake_case , one_hot=snake_case ) if not 0 <= validation_size <= len(snake_case ): __SCREAMING_SNAKE_CASE : Optional[int] = ( '''Validation size should be between 0 and ''' F'''{len(snake_case )}. Received: {validation_size}.''' ) raise ValueError(snake_case ) __SCREAMING_SNAKE_CASE : Tuple = train_images[:validation_size] __SCREAMING_SNAKE_CASE : Tuple = train_labels[:validation_size] __SCREAMING_SNAKE_CASE : List[Any] = train_images[validation_size:] __SCREAMING_SNAKE_CASE : Tuple = train_labels[validation_size:] __SCREAMING_SNAKE_CASE : Dict = {'''dtype''': dtype, '''reshape''': reshape, '''seed''': seed} __SCREAMING_SNAKE_CASE : List[Any] = _DataSet(snake_case , snake_case , **snake_case ) __SCREAMING_SNAKE_CASE : Union[str, Any] = _DataSet(snake_case , snake_case , **snake_case ) __SCREAMING_SNAKE_CASE : List[str] = _DataSet(snake_case , snake_case , **snake_case ) return _Datasets(train=snake_case , validation=snake_case , test=snake_case )
303
from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels
303
1
import os import unittest from transformers import LayoutLMTokenizer, LayoutLMTokenizerFast from transformers.models.layoutlm.tokenization_layoutlm import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __UpperCamelCase ( lowerCAmelCase__ , unittest.TestCase ): """simple docstring""" lowerCAmelCase_ = LayoutLMTokenizer lowerCAmelCase_ = LayoutLMTokenizerFast lowerCAmelCase_ = True lowerCAmelCase_ = True def UpperCAmelCase__ ( self : str ): """simple docstring""" super().setUp() __SCREAMING_SNAKE_CASE : Dict = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] __SCREAMING_SNAKE_CASE : Tuple = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) def UpperCAmelCase__ ( self : Any , **_A : Any ): """simple docstring""" return LayoutLMTokenizer.from_pretrained(self.tmpdirname , **_A ) def UpperCAmelCase__ ( self : List[Any] , _A : Dict ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = '''UNwant\u00E9d,running''' __SCREAMING_SNAKE_CASE : Optional[int] = '''unwanted, running''' return input_text, output_text def UpperCAmelCase__ ( self : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = self.tokenizer_class(self.vocab_file ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(_A , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_A ) , [7, 4, 5, 10, 8, 9] ) def UpperCAmelCase__ ( self : Tuple ): """simple docstring""" pass
303
from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { """uw-madison/mra-base-512-4""": """https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json""", } class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''mra''' def __init__( self : str , _A : List[str]=5_0265 , _A : int=768 , _A : Union[str, Any]=12 , _A : Union[str, Any]=12 , _A : Union[str, Any]=3072 , _A : Any="gelu" , _A : List[Any]=0.1 , _A : List[Any]=0.1 , _A : List[str]=512 , _A : Tuple=1 , _A : List[str]=0.02 , _A : Union[str, Any]=1e-5 , _A : Optional[int]="absolute" , _A : Union[str, Any]=4 , _A : List[Any]="full" , _A : Union[str, Any]=0 , _A : Union[str, Any]=0 , _A : Optional[Any]=1 , _A : Union[str, Any]=0 , _A : Any=2 , **_A : List[str] , ): """simple docstring""" super().__init__(pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , **_A ) __SCREAMING_SNAKE_CASE : Dict = vocab_size __SCREAMING_SNAKE_CASE : str = max_position_embeddings __SCREAMING_SNAKE_CASE : Optional[int] = hidden_size __SCREAMING_SNAKE_CASE : str = num_hidden_layers __SCREAMING_SNAKE_CASE : Tuple = num_attention_heads __SCREAMING_SNAKE_CASE : str = intermediate_size __SCREAMING_SNAKE_CASE : Tuple = hidden_act __SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob __SCREAMING_SNAKE_CASE : int = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : Tuple = initializer_range __SCREAMING_SNAKE_CASE : Any = type_vocab_size __SCREAMING_SNAKE_CASE : str = layer_norm_eps __SCREAMING_SNAKE_CASE : Union[str, Any] = position_embedding_type __SCREAMING_SNAKE_CASE : str = block_per_row __SCREAMING_SNAKE_CASE : Union[str, Any] = approx_mode __SCREAMING_SNAKE_CASE : Optional[int] = initial_prior_first_n_blocks __SCREAMING_SNAKE_CASE : List[Any] = initial_prior_diagonal_n_blocks
303
1
def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" def count_of_possible_combinations(snake_case ) -> int: if target < 0: return 0 if target == 0: return 1 return sum(count_of_possible_combinations(target - item ) for item in array ) return count_of_possible_combinations(snake_case ) def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" def count_of_possible_combinations_with_dp_array( snake_case , snake_case ) -> int: if target < 0: return 0 if target == 0: return 1 if dp_array[target] != -1: return dp_array[target] __SCREAMING_SNAKE_CASE : Any = sum( count_of_possible_combinations_with_dp_array(target - item , snake_case ) for item in array ) __SCREAMING_SNAKE_CASE : List[str] = answer return answer __SCREAMING_SNAKE_CASE : List[str] = [-1] * (target + 1) return count_of_possible_combinations_with_dp_array(snake_case , snake_case ) def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = [0] * (target + 1) __SCREAMING_SNAKE_CASE : Optional[int] = 1 for i in range(1 , target + 1 ): for j in range(snake_case ): if i - array[j] >= 0: dp_array[i] += dp_array[i - array[j]] return dp_array[target] if __name__ == "__main__": import doctest doctest.testmod() lowercase_ = 3 lowercase_ = 5 lowercase_ = [1, 2, 5] print(combination_sum_iv(n, array, target))
303
from typing import List, Optional, Tuple, Union import torch from ...utils import logging, randn_tensor from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline lowercase_ = logging.get_logger(__name__) # pylint: disable=invalid-name class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" def __init__( self : List[str] , _A : Dict , _A : List[Any] ): """simple docstring""" super().__init__() self.register_modules(unet=_A , scheduler=_A ) @torch.no_grad() def __call__( self : List[str] , _A : int = 1 , _A : int = 100 , _A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _A : Optional[float] = None , _A : bool = True , ): """simple docstring""" if audio_length_in_s is None: __SCREAMING_SNAKE_CASE : Optional[Any] = self.unet.config.sample_size / self.unet.config.sample_rate __SCREAMING_SNAKE_CASE : List[Any] = audio_length_in_s * self.unet.config.sample_rate __SCREAMING_SNAKE_CASE : Any = 2 ** len(self.unet.up_blocks ) if sample_size < 3 * down_scale_factor: raise ValueError( F'''{audio_length_in_s} is too small. Make sure it\'s bigger or equal to''' F''' {3 * down_scale_factor / self.unet.config.sample_rate}.''' ) __SCREAMING_SNAKE_CASE : int = int(_A ) if sample_size % down_scale_factor != 0: __SCREAMING_SNAKE_CASE : Optional[int] = ( (audio_length_in_s * self.unet.config.sample_rate) // down_scale_factor + 1 ) * down_scale_factor logger.info( F'''{audio_length_in_s} is increased to {sample_size / self.unet.config.sample_rate} so that it can be handled''' F''' by the model. It will be cut to {original_sample_size / self.unet.config.sample_rate} after the denoising''' ''' process.''' ) __SCREAMING_SNAKE_CASE : List[Any] = int(_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = next(iter(self.unet.parameters() ) ).dtype __SCREAMING_SNAKE_CASE : int = (batch_size, self.unet.config.in_channels, sample_size) if isinstance(_A , _A ) and len(_A ) != batch_size: raise ValueError( F'''You have passed a list of generators of length {len(_A )}, but requested an effective batch''' F''' size of {batch_size}. Make sure the batch size matches the length of the generators.''' ) __SCREAMING_SNAKE_CASE : Dict = randn_tensor(_A , generator=_A , device=self.device , dtype=_A ) # set step values self.scheduler.set_timesteps(_A , device=audio.device ) __SCREAMING_SNAKE_CASE : Dict = self.scheduler.timesteps.to(_A ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output __SCREAMING_SNAKE_CASE : List[Any] = self.unet(_A , _A ).sample # 2. compute previous image: x_t -> t_t-1 __SCREAMING_SNAKE_CASE : Optional[int] = self.scheduler.step(_A , _A , _A ).prev_sample __SCREAMING_SNAKE_CASE : str = audio.clamp(-1 , 1 ).float().cpu().numpy() __SCREAMING_SNAKE_CASE : str = audio[:, :, :original_sample_size] if not return_dict: return (audio,) return AudioPipelineOutput(audios=_A )
303
1
import pytest from datasets.splits import SplitDict, SplitInfo from datasets.utils.py_utils import asdict @pytest.mark.parametrize( '''split_dict''' , [ SplitDict(), SplitDict({'''train''': SplitInfo(name='''train''' , num_bytes=1_337 , num_examples=42 , dataset_name='''my_dataset''' )} ), SplitDict({'''train''': SplitInfo(name='''train''' , num_bytes=1_337 , num_examples=42 )} ), SplitDict({'''train''': SplitInfo()} ), ] , ) def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = split_dict._to_yaml_list() assert len(snake_case ) == len(snake_case ) __SCREAMING_SNAKE_CASE : str = SplitDict._from_yaml_list(snake_case ) for split_name, split_info in split_dict.items(): # dataset_name field is deprecated, and is therefore not part of the YAML dump __SCREAMING_SNAKE_CASE : List[Any] = None # the split name of split_dict takes over the name of the split info object __SCREAMING_SNAKE_CASE : Tuple = split_name assert split_dict == reloaded @pytest.mark.parametrize( '''split_info''' , [SplitInfo(), SplitInfo(dataset_name=snake_case ), SplitInfo(dataset_name='''my_dataset''' )] ) def a__ ( snake_case ): """simple docstring""" # For backward compatibility, we need asdict(split_dict) to return split info dictrionaries with the "dataset_name" # field even if it's deprecated. This way old versionso of `datasets` can still reload dataset_infos.json files __SCREAMING_SNAKE_CASE : Optional[int] = asdict(SplitDict({'''train''': split_info} ) ) assert "dataset_name" in split_dict_asdict["train"] assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name
303
import importlib import torch import yaml from omegaconf import OmegaConf from taming.models.vqgan import VQModel def a__ ( snake_case , snake_case=False ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = OmegaConf.load(snake_case ) if display: print(yaml.dump(OmegaConf.to_container(snake_case ) ) ) return config def a__ ( snake_case , snake_case=None , snake_case=None ): """simple docstring""" if conf_path is None: __SCREAMING_SNAKE_CASE : Any = '''./model_checkpoints/vqgan_only.yaml''' __SCREAMING_SNAKE_CASE : List[str] = load_config(snake_case , display=snake_case ) __SCREAMING_SNAKE_CASE : str = VQModel(**config.model.params ) if ckpt_path is None: __SCREAMING_SNAKE_CASE : Optional[Any] = '''./model_checkpoints/vqgan_only.pt''' __SCREAMING_SNAKE_CASE : Optional[Any] = torch.load(snake_case , map_location=snake_case ) if ".ckpt" in ckpt_path: __SCREAMING_SNAKE_CASE : Optional[Any] = sd['''state_dict'''] model.load_state_dict(snake_case , strict=snake_case ) model.to(snake_case ) del sd return model def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Any = model.encode(snake_case ) print(F'''VQGAN --- {model.__class__.__name__}: latent shape: {z.shape[2:]}''' ) __SCREAMING_SNAKE_CASE : Any = model.decode(snake_case ) return xrec def a__ ( snake_case , snake_case=False ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = string.rsplit('''.''' , 1 ) if reload: __SCREAMING_SNAKE_CASE : Union[str, Any] = importlib.import_module(snake_case ) importlib.reload(snake_case ) return getattr(importlib.import_module(snake_case , package=snake_case ) , cls ) def a__ ( snake_case ): """simple docstring""" if "target" not in config: raise KeyError('''Expected key `target` to instantiate.''' ) return get_obj_from_str(config['''target'''] )(**config.get('''params''' , {} ) ) def a__ ( snake_case , snake_case , snake_case=True , snake_case=True ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = instantiate_from_config(snake_case ) if sd is not None: model.load_state_dict(snake_case ) if gpu: model.cuda() if eval_mode: model.eval() return {"model": model} def a__ ( snake_case , snake_case , snake_case , snake_case ): """simple docstring""" # load the specified checkpoint if ckpt: __SCREAMING_SNAKE_CASE : Dict = torch.load(snake_case , map_location='''cpu''' ) __SCREAMING_SNAKE_CASE : List[Any] = pl_sd['''global_step'''] print(F'''loaded model from global step {global_step}.''' ) else: __SCREAMING_SNAKE_CASE : Optional[Any] = {'''state_dict''': None} __SCREAMING_SNAKE_CASE : Optional[Any] = None __SCREAMING_SNAKE_CASE : Dict = load_model_from_config(config.model , pl_sd['''state_dict'''] , gpu=snake_case , eval_mode=snake_case )['''model'''] return model, global_step
303
1
import unittest from transformers import MobileBertConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, MobileBertForMaskedLM, MobileBertForMultipleChoice, MobileBertForNextSentencePrediction, MobileBertForPreTraining, MobileBertForQuestionAnswering, MobileBertForSequenceClassification, MobileBertForTokenClassification, MobileBertModel, ) class __UpperCamelCase : """simple docstring""" def __init__( self : str , _A : Any , _A : List[Any]=13 , _A : Union[str, Any]=7 , _A : Any=True , _A : Optional[int]=True , _A : List[Any]=True , _A : str=True , _A : List[Any]=99 , _A : List[Any]=64 , _A : Optional[Any]=32 , _A : Optional[int]=5 , _A : str=4 , _A : List[Any]=37 , _A : Union[str, Any]="gelu" , _A : int=0.1 , _A : Tuple=0.1 , _A : str=512 , _A : List[Any]=16 , _A : Union[str, Any]=2 , _A : int=0.02 , _A : Any=3 , _A : Optional[Any]=4 , _A : Any=None , ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = parent __SCREAMING_SNAKE_CASE : Optional[int] = batch_size __SCREAMING_SNAKE_CASE : Any = seq_length __SCREAMING_SNAKE_CASE : str = is_training __SCREAMING_SNAKE_CASE : Optional[Any] = use_input_mask __SCREAMING_SNAKE_CASE : Optional[Any] = use_token_type_ids __SCREAMING_SNAKE_CASE : List[str] = use_labels __SCREAMING_SNAKE_CASE : List[str] = vocab_size __SCREAMING_SNAKE_CASE : Tuple = hidden_size __SCREAMING_SNAKE_CASE : int = embedding_size __SCREAMING_SNAKE_CASE : Optional[int] = num_hidden_layers __SCREAMING_SNAKE_CASE : Dict = num_attention_heads __SCREAMING_SNAKE_CASE : int = intermediate_size __SCREAMING_SNAKE_CASE : str = hidden_act __SCREAMING_SNAKE_CASE : List[str] = hidden_dropout_prob __SCREAMING_SNAKE_CASE : List[Any] = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : Any = max_position_embeddings __SCREAMING_SNAKE_CASE : Optional[int] = type_vocab_size __SCREAMING_SNAKE_CASE : Any = type_sequence_label_size __SCREAMING_SNAKE_CASE : List[str] = initializer_range __SCREAMING_SNAKE_CASE : str = num_labels __SCREAMING_SNAKE_CASE : List[Any] = num_choices __SCREAMING_SNAKE_CASE : List[str] = scope def UpperCAmelCase__ ( self : List[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __SCREAMING_SNAKE_CASE : Any = None if self.use_input_mask: __SCREAMING_SNAKE_CASE : Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) __SCREAMING_SNAKE_CASE : Any = None if self.use_token_type_ids: __SCREAMING_SNAKE_CASE : Any = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __SCREAMING_SNAKE_CASE : Union[str, Any] = None __SCREAMING_SNAKE_CASE : Tuple = None __SCREAMING_SNAKE_CASE : str = None if self.use_labels: __SCREAMING_SNAKE_CASE : Optional[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __SCREAMING_SNAKE_CASE : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size] , self.num_choices ) __SCREAMING_SNAKE_CASE : Union[str, Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCAmelCase__ ( self : Any ): """simple docstring""" return MobileBertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , embedding_size=self.embedding_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_A , initializer_range=self.initializer_range , ) def UpperCAmelCase__ ( self : str , _A : Any , _A : Optional[Any] , _A : Union[str, Any] , _A : int , _A : Tuple , _A : Optional[int] , _A : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = MobileBertModel(config=_A ) model.to(_A ) model.eval() __SCREAMING_SNAKE_CASE : Optional[int] = model(_A , attention_mask=_A , token_type_ids=_A ) __SCREAMING_SNAKE_CASE : int = model(_A , token_type_ids=_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase__ ( self : Any , _A : Dict , _A : List[str] , _A : Optional[int] , _A : List[Any] , _A : str , _A : Dict , _A : List[str] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = MobileBertForMaskedLM(config=_A ) model.to(_A ) model.eval() __SCREAMING_SNAKE_CASE : Any = model(_A , attention_mask=_A , token_type_ids=_A , labels=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCAmelCase__ ( self : Optional[Any] , _A : int , _A : str , _A : Tuple , _A : List[Any] , _A : Optional[int] , _A : str , _A : Optional[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = MobileBertForNextSentencePrediction(config=_A ) model.to(_A ) model.eval() __SCREAMING_SNAKE_CASE : int = model( _A , attention_mask=_A , token_type_ids=_A , labels=_A , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def UpperCAmelCase__ ( self : Union[str, Any] , _A : List[str] , _A : Optional[Any] , _A : List[str] , _A : Tuple , _A : Dict , _A : int , _A : Optional[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = MobileBertForPreTraining(config=_A ) model.to(_A ) model.eval() __SCREAMING_SNAKE_CASE : int = model( _A , attention_mask=_A , token_type_ids=_A , labels=_A , next_sentence_label=_A , ) self.parent.assertEqual(result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.seq_relationship_logits.shape , (self.batch_size, 2) ) def UpperCAmelCase__ ( self : Union[str, Any] , _A : int , _A : int , _A : Optional[Any] , _A : str , _A : Optional[Any] , _A : str , _A : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = MobileBertForQuestionAnswering(config=_A ) model.to(_A ) model.eval() __SCREAMING_SNAKE_CASE : Optional[int] = model( _A , attention_mask=_A , token_type_ids=_A , start_positions=_A , end_positions=_A , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCAmelCase__ ( self : int , _A : List[str] , _A : List[Any] , _A : Optional[int] , _A : Any , _A : Union[str, Any] , _A : Tuple , _A : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = self.num_labels __SCREAMING_SNAKE_CASE : Optional[int] = MobileBertForSequenceClassification(_A ) model.to(_A ) model.eval() __SCREAMING_SNAKE_CASE : int = model(_A , attention_mask=_A , token_type_ids=_A , labels=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCAmelCase__ ( self : List[str] , _A : str , _A : List[Any] , _A : Optional[Any] , _A : Optional[int] , _A : Optional[int] , _A : Union[str, Any] , _A : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = self.num_labels __SCREAMING_SNAKE_CASE : Optional[int] = MobileBertForTokenClassification(config=_A ) model.to(_A ) model.eval() __SCREAMING_SNAKE_CASE : Optional[Any] = model(_A , attention_mask=_A , token_type_ids=_A , labels=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCAmelCase__ ( self : List[Any] , _A : Optional[int] , _A : int , _A : Optional[Any] , _A : Tuple , _A : Dict , _A : str , _A : List[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = self.num_choices __SCREAMING_SNAKE_CASE : Tuple = MobileBertForMultipleChoice(config=_A ) model.to(_A ) model.eval() __SCREAMING_SNAKE_CASE : Optional[int] = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __SCREAMING_SNAKE_CASE : Optional[int] = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __SCREAMING_SNAKE_CASE : List[Any] = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __SCREAMING_SNAKE_CASE : List[Any] = model( _A , attention_mask=_A , token_type_ids=_A , labels=_A , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = self.prepare_config_and_inputs() ( ( __SCREAMING_SNAKE_CASE ), ( __SCREAMING_SNAKE_CASE ), ( __SCREAMING_SNAKE_CASE ), ( __SCREAMING_SNAKE_CASE ), ( __SCREAMING_SNAKE_CASE ), ( __SCREAMING_SNAKE_CASE ), ( __SCREAMING_SNAKE_CASE ), ) : Optional[Any] = config_and_inputs __SCREAMING_SNAKE_CASE : Dict = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __UpperCamelCase ( lowerCAmelCase__ , lowerCAmelCase__ , unittest.TestCase ): """simple docstring""" lowerCAmelCase_ = ( ( MobileBertModel, MobileBertForMaskedLM, MobileBertForMultipleChoice, MobileBertForNextSentencePrediction, MobileBertForPreTraining, MobileBertForQuestionAnswering, MobileBertForSequenceClassification, MobileBertForTokenClassification, ) if is_torch_available() else () ) lowerCAmelCase_ = ( { '''feature-extraction''': MobileBertModel, '''fill-mask''': MobileBertForMaskedLM, '''question-answering''': MobileBertForQuestionAnswering, '''text-classification''': MobileBertForSequenceClassification, '''token-classification''': MobileBertForTokenClassification, '''zero-shot''': MobileBertForSequenceClassification, } if is_torch_available() else {} ) lowerCAmelCase_ = True def UpperCAmelCase__ ( self : str , _A : List[str] , _A : str , _A : Any=False ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = super()._prepare_for_class(_A , _A , return_labels=_A ) if return_labels: if model_class in get_values(_A ): __SCREAMING_SNAKE_CASE : List[Any] = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) , dtype=torch.long , device=_A ) __SCREAMING_SNAKE_CASE : Dict = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_A ) return inputs_dict def UpperCAmelCase__ ( self : Dict ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = MobileBertModelTester(self ) __SCREAMING_SNAKE_CASE : Any = ConfigTester(self , config_class=_A , hidden_size=37 ) def UpperCAmelCase__ ( self : Optional[Any] ): """simple docstring""" self.config_tester.run_common_tests() def UpperCAmelCase__ ( self : List[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_model(*_A ) def UpperCAmelCase__ ( self : Dict ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_masked_lm(*_A ) def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_A ) def UpperCAmelCase__ ( self : Dict ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_A ) def UpperCAmelCase__ ( self : int ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_pretraining(*_A ) def UpperCAmelCase__ ( self : List[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_question_answering(*_A ) def UpperCAmelCase__ ( self : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_A ) def UpperCAmelCase__ ( self : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_token_classification(*_A ) def a__ ( snake_case ): """simple docstring""" return torch.tensor( snake_case , dtype=torch.long , device=snake_case , ) lowercase_ = 1e-3 @require_torch @require_sentencepiece @require_tokenizers class __UpperCamelCase ( unittest.TestCase ): """simple docstring""" @slow def UpperCAmelCase__ ( self : Tuple ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = MobileBertModel.from_pretrained('''google/mobilebert-uncased''' ).to(_A ) __SCREAMING_SNAKE_CASE : int = _long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]] ) with torch.no_grad(): __SCREAMING_SNAKE_CASE : str = model(_A )[0] __SCREAMING_SNAKE_CASE : str = torch.Size((1, 9, 512) ) self.assertEqual(output.shape , _A ) __SCREAMING_SNAKE_CASE : Optional[int] = torch.tensor( [ [ [-2.4_73_65_26e07, 8.2_69_16_56e04, 1.6_52_18_38e05], [-5.7_54_17_04e-01, 3.9_05_60_22e00, 4.4_01_15_07e00], [2.6_04_73_59e00, 1.5_67_76_52e00, -1.7_32_41_88e-01], ] ] , device=_A , ) # MobileBERT results range from 10e0 to 10e8. Even a 0.0000001% difference with a value of 10e8 results in a # ~1 difference, it's therefore not a good idea to measure using addition. # Here, we instead divide the expected result with the result in order to obtain ~1. We then check that the # result is held between bounds: 1 - TOLERANCE < expected_result / result < 1 + TOLERANCE __SCREAMING_SNAKE_CASE : int = torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE ) __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE ) self.assertTrue(lower_bound and upper_bound )
303
from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { """studio-ousia/luke-base""": """https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json""", """studio-ousia/luke-large""": """https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json""", } class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''luke''' def __init__( self : Any , _A : int=5_0267 , _A : str=50_0000 , _A : Dict=768 , _A : int=256 , _A : Tuple=12 , _A : Optional[Any]=12 , _A : Any=3072 , _A : Tuple="gelu" , _A : Any=0.1 , _A : Dict=0.1 , _A : Any=512 , _A : Tuple=2 , _A : int=0.02 , _A : Any=1e-12 , _A : Dict=True , _A : Optional[Any]=None , _A : List[str]=1 , _A : List[str]=0 , _A : Dict=2 , **_A : List[str] , ): """simple docstring""" super().__init__(pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , **_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = vocab_size __SCREAMING_SNAKE_CASE : Any = entity_vocab_size __SCREAMING_SNAKE_CASE : int = hidden_size __SCREAMING_SNAKE_CASE : List[Any] = entity_emb_size __SCREAMING_SNAKE_CASE : Union[str, Any] = num_hidden_layers __SCREAMING_SNAKE_CASE : Tuple = num_attention_heads __SCREAMING_SNAKE_CASE : Dict = hidden_act __SCREAMING_SNAKE_CASE : Dict = intermediate_size __SCREAMING_SNAKE_CASE : Optional[int] = hidden_dropout_prob __SCREAMING_SNAKE_CASE : Any = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : List[Any] = max_position_embeddings __SCREAMING_SNAKE_CASE : List[str] = type_vocab_size __SCREAMING_SNAKE_CASE : Dict = initializer_range __SCREAMING_SNAKE_CASE : Tuple = layer_norm_eps __SCREAMING_SNAKE_CASE : int = use_entity_aware_attention __SCREAMING_SNAKE_CASE : Any = classifier_dropout
303
1
from ...utils import is_torch_available, is_transformers_available if is_transformers_available() and is_torch_available(): from .pipeline_vq_diffusion import LearnedClassifierFreeSamplingEmbeddings, VQDiffusionPipeline
303
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase_ = { """configuration_electra""": ["""ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ElectraConfig""", """ElectraOnnxConfig"""], """tokenization_electra""": ["""ElectraTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""ElectraTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST""", """ElectraForCausalLM""", """ElectraForMaskedLM""", """ElectraForMultipleChoice""", """ElectraForPreTraining""", """ElectraForQuestionAnswering""", """ElectraForSequenceClassification""", """ElectraForTokenClassification""", """ElectraModel""", """ElectraPreTrainedModel""", """load_tf_weights_in_electra""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFElectraForMaskedLM""", """TFElectraForMultipleChoice""", """TFElectraForPreTraining""", """TFElectraForQuestionAnswering""", """TFElectraForSequenceClassification""", """TFElectraForTokenClassification""", """TFElectraModel""", """TFElectraPreTrainedModel""", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """FlaxElectraForCausalLM""", """FlaxElectraForMaskedLM""", """FlaxElectraForMultipleChoice""", """FlaxElectraForPreTraining""", """FlaxElectraForQuestionAnswering""", """FlaxElectraForSequenceClassification""", """FlaxElectraForTokenClassification""", """FlaxElectraModel""", """FlaxElectraPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_electra import ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ElectraConfig, ElectraOnnxConfig from .tokenization_electra import ElectraTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_electra_fast import ElectraTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_electra import ( ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, ElectraForCausalLM, ElectraForMaskedLM, ElectraForMultipleChoice, ElectraForPreTraining, ElectraForQuestionAnswering, ElectraForSequenceClassification, ElectraForTokenClassification, ElectraModel, ElectraPreTrainedModel, load_tf_weights_in_electra, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_electra import ( TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, TFElectraForMaskedLM, TFElectraForMultipleChoice, TFElectraForPreTraining, TFElectraForQuestionAnswering, TFElectraForSequenceClassification, TFElectraForTokenClassification, TFElectraModel, TFElectraPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_electra import ( FlaxElectraForCausalLM, FlaxElectraForMaskedLM, FlaxElectraForMultipleChoice, FlaxElectraForPreTraining, FlaxElectraForQuestionAnswering, FlaxElectraForSequenceClassification, FlaxElectraForTokenClassification, FlaxElectraModel, FlaxElectraPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
303
1
import inspect import unittest from transformers import ConvNextConfig 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_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ConvNextBackbone, ConvNextForImageClassification, ConvNextModel from transformers.models.convnext.modeling_convnext import CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class __UpperCamelCase : """simple docstring""" def __init__( self : Dict , _A : Dict , _A : List[str]=13 , _A : List[Any]=32 , _A : int=3 , _A : Optional[Any]=4 , _A : List[Any]=[10, 20, 30, 40] , _A : str=[2, 2, 3, 2] , _A : Dict=True , _A : str=True , _A : int=37 , _A : str="gelu" , _A : Optional[Any]=10 , _A : Any=0.02 , _A : int=["stage2", "stage3", "stage4"] , _A : Tuple=[2, 3, 4] , _A : Tuple=None , ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = parent __SCREAMING_SNAKE_CASE : Optional[int] = batch_size __SCREAMING_SNAKE_CASE : Dict = image_size __SCREAMING_SNAKE_CASE : Any = num_channels __SCREAMING_SNAKE_CASE : Union[str, Any] = num_stages __SCREAMING_SNAKE_CASE : Tuple = hidden_sizes __SCREAMING_SNAKE_CASE : List[str] = depths __SCREAMING_SNAKE_CASE : Dict = is_training __SCREAMING_SNAKE_CASE : int = use_labels __SCREAMING_SNAKE_CASE : Optional[int] = intermediate_size __SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_act __SCREAMING_SNAKE_CASE : Dict = num_labels __SCREAMING_SNAKE_CASE : List[Any] = initializer_range __SCREAMING_SNAKE_CASE : Dict = out_features __SCREAMING_SNAKE_CASE : List[Any] = out_indices __SCREAMING_SNAKE_CASE : str = scope def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __SCREAMING_SNAKE_CASE : List[str] = None if self.use_labels: __SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size] , self.num_labels ) __SCREAMING_SNAKE_CASE : Optional[int] = self.get_config() return config, pixel_values, labels def UpperCAmelCase__ ( self : str ): """simple docstring""" return ConvNextConfig( num_channels=self.num_channels , hidden_sizes=self.hidden_sizes , depths=self.depths , num_stages=self.num_stages , hidden_act=self.hidden_act , is_decoder=_A , initializer_range=self.initializer_range , out_features=self.out_features , out_indices=self.out_indices , num_labels=self.num_labels , ) def UpperCAmelCase__ ( self : Optional[Any] , _A : Union[str, Any] , _A : Optional[Any] , _A : Union[str, Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = ConvNextModel(config=_A ) model.to(_A ) model.eval() __SCREAMING_SNAKE_CASE : List[str] = model(_A ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def UpperCAmelCase__ ( self : int , _A : List[str] , _A : Tuple , _A : List[str] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = ConvNextForImageClassification(_A ) model.to(_A ) model.eval() __SCREAMING_SNAKE_CASE : int = model(_A , labels=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCAmelCase__ ( self : List[Any] , _A : Optional[Any] , _A : Union[str, Any] , _A : List[str] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = ConvNextBackbone(config=_A ) model.to(_A ) model.eval() __SCREAMING_SNAKE_CASE : Optional[int] = model(_A ) # verify hidden states self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[1], 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , config.hidden_sizes[1:] ) # verify backbone works with out_features=None __SCREAMING_SNAKE_CASE : List[str] = None __SCREAMING_SNAKE_CASE : List[str] = ConvNextBackbone(config=_A ) model.to(_A ) model.eval() __SCREAMING_SNAKE_CASE : str = model(_A ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , 1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[-1], 1, 1] ) # verify channels self.parent.assertEqual(len(model.channels ) , 1 ) self.parent.assertListEqual(model.channels , [config.hidden_sizes[-1]] ) def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = self.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = config_and_inputs __SCREAMING_SNAKE_CASE : Dict = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __UpperCamelCase ( lowerCAmelCase__ , lowerCAmelCase__ , unittest.TestCase ): """simple docstring""" lowerCAmelCase_ = ( ( ConvNextModel, ConvNextForImageClassification, ConvNextBackbone, ) if is_torch_available() else () ) lowerCAmelCase_ = ( {'''feature-extraction''': ConvNextModel, '''image-classification''': ConvNextForImageClassification} if is_torch_available() else {} ) lowerCAmelCase_ = True lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False def UpperCAmelCase__ ( self : Optional[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = ConvNextModelTester(self ) __SCREAMING_SNAKE_CASE : Union[str, Any] = ConfigTester(self , config_class=_A , has_text_modality=_A , hidden_size=37 ) def UpperCAmelCase__ ( self : int ): """simple docstring""" self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" return @unittest.skip(reason='''ConvNext does not use inputs_embeds''' ) def UpperCAmelCase__ ( self : Dict ): """simple docstring""" pass @unittest.skip(reason='''ConvNext does not support input and output embeddings''' ) def UpperCAmelCase__ ( self : List[str] ): """simple docstring""" pass @unittest.skip(reason='''ConvNext does not use feedforward chunking''' ) def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" pass def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __SCREAMING_SNAKE_CASE : Optional[int] = model_class(_A ) __SCREAMING_SNAKE_CASE : int = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __SCREAMING_SNAKE_CASE : str = [*signature.parameters.keys()] __SCREAMING_SNAKE_CASE : Optional[Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _A ) def UpperCAmelCase__ ( self : List[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_A ) def UpperCAmelCase__ ( self : int ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*_A ) def UpperCAmelCase__ ( self : Any ): """simple docstring""" def check_hidden_states_output(_A : str , _A : Dict , _A : int ): __SCREAMING_SNAKE_CASE : Any = model_class(_A ) model.to(_A ) model.eval() with torch.no_grad(): __SCREAMING_SNAKE_CASE : Union[str, Any] = model(**self._prepare_for_class(_A , _A ) ) __SCREAMING_SNAKE_CASE : Optional[Any] = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states __SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.num_stages self.assertEqual(len(_A ) , expected_num_stages + 1 ) # ConvNext's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __SCREAMING_SNAKE_CASE : str = True check_hidden_states_output(_A , _A , _A ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __SCREAMING_SNAKE_CASE : Any = True check_hidden_states_output(_A , _A , _A ) def UpperCAmelCase__ ( self : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_A ) @slow def UpperCAmelCase__ ( self : str ): """simple docstring""" for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __SCREAMING_SNAKE_CASE : List[str] = ConvNextModel.from_pretrained(_A ) self.assertIsNotNone(_A ) def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class __UpperCamelCase ( unittest.TestCase ): """simple docstring""" @cached_property def UpperCAmelCase__ ( self : Dict ): """simple docstring""" return AutoImageProcessor.from_pretrained('''facebook/convnext-tiny-224''' ) if is_vision_available() else None @slow def UpperCAmelCase__ ( self : Tuple ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = ConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224''' ).to(_A ) __SCREAMING_SNAKE_CASE : int = self.default_image_processor __SCREAMING_SNAKE_CASE : Dict = prepare_img() __SCREAMING_SNAKE_CASE : int = image_processor(images=_A , return_tensors='''pt''' ).to(_A ) # forward pass with torch.no_grad(): __SCREAMING_SNAKE_CASE : Optional[int] = model(**_A ) # verify the logits __SCREAMING_SNAKE_CASE : Dict = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , _A ) __SCREAMING_SNAKE_CASE : Optional[int] = torch.tensor([-0.02_60, -0.47_39, 0.19_11] ).to(_A ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _A , atol=1e-4 ) ) @require_torch class __UpperCamelCase ( unittest.TestCase , lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = (ConvNextBackbone,) if is_torch_available() else () lowerCAmelCase_ = ConvNextConfig lowerCAmelCase_ = False def UpperCAmelCase__ ( self : int ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = ConvNextModelTester(self )
303
from typing import Dict, List, Optional, Tuple, Union import torch from ...models import AutoencoderKL, TransformeraDModel from ...schedulers import KarrasDiffusionSchedulers from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" def __init__( self : List[Any] , _A : TransformeraDModel , _A : AutoencoderKL , _A : KarrasDiffusionSchedulers , _A : Optional[Dict[int, str]] = None , ): """simple docstring""" super().__init__() self.register_modules(transformer=_A , vae=_A , scheduler=_A ) # create a imagenet -> id dictionary for easier use __SCREAMING_SNAKE_CASE : Optional[int] = {} if idalabel is not None: for key, value in idalabel.items(): for label in value.split(''',''' ): __SCREAMING_SNAKE_CASE : Optional[Any] = int(_A ) __SCREAMING_SNAKE_CASE : List[str] = dict(sorted(self.labels.items() ) ) def UpperCAmelCase__ ( self : List[Any] , _A : Union[str, List[str]] ): """simple docstring""" if not isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : Union[str, Any] = list(_A ) for l in label: if l not in self.labels: raise ValueError( F'''{l} does not exist. Please make sure to select one of the following labels: \n {self.labels}.''' ) return [self.labels[l] for l in label] @torch.no_grad() def __call__( self : Dict , _A : List[int] , _A : float = 4.0 , _A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _A : int = 50 , _A : Optional[str] = "pil" , _A : bool = True , ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = len(_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = self.transformer.config.sample_size __SCREAMING_SNAKE_CASE : List[Any] = self.transformer.config.in_channels __SCREAMING_SNAKE_CASE : Optional[int] = randn_tensor( shape=(batch_size, latent_channels, latent_size, latent_size) , generator=_A , device=self.device , dtype=self.transformer.dtype , ) __SCREAMING_SNAKE_CASE : Tuple = torch.cat([latents] * 2 ) if guidance_scale > 1 else latents __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor(_A , device=self.device ).reshape(-1 ) __SCREAMING_SNAKE_CASE : Any = torch.tensor([1000] * batch_size , device=self.device ) __SCREAMING_SNAKE_CASE : Any = torch.cat([class_labels, class_null] , 0 ) if guidance_scale > 1 else class_labels # set step values self.scheduler.set_timesteps(_A ) for t in self.progress_bar(self.scheduler.timesteps ): if guidance_scale > 1: __SCREAMING_SNAKE_CASE : Optional[Any] = latent_model_input[: len(_A ) // 2] __SCREAMING_SNAKE_CASE : List[Any] = torch.cat([half, half] , dim=0 ) __SCREAMING_SNAKE_CASE : int = self.scheduler.scale_model_input(_A , _A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = t if not torch.is_tensor(_A ): # TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can # This would be a good case for the `match` statement (Python 3.10+) __SCREAMING_SNAKE_CASE : Any = latent_model_input.device.type == '''mps''' if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : List[Any] = torch.floataa if is_mps else torch.floataa else: __SCREAMING_SNAKE_CASE : int = torch.intaa if is_mps else torch.intaa __SCREAMING_SNAKE_CASE : int = torch.tensor([timesteps] , dtype=_A , device=latent_model_input.device ) elif len(timesteps.shape ) == 0: __SCREAMING_SNAKE_CASE : Optional[Any] = timesteps[None].to(latent_model_input.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML __SCREAMING_SNAKE_CASE : Optional[int] = timesteps.expand(latent_model_input.shape[0] ) # predict noise model_output __SCREAMING_SNAKE_CASE : Union[str, Any] = self.transformer( _A , timestep=_A , class_labels=_A ).sample # perform guidance if guidance_scale > 1: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = noise_pred[:, :latent_channels], noise_pred[:, latent_channels:] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = torch.split(_A , len(_A ) // 2 , dim=0 ) __SCREAMING_SNAKE_CASE : str = uncond_eps + guidance_scale * (cond_eps - uncond_eps) __SCREAMING_SNAKE_CASE : List[Any] = torch.cat([half_eps, half_eps] , dim=0 ) __SCREAMING_SNAKE_CASE : List[str] = torch.cat([eps, rest] , dim=1 ) # learned sigma if self.transformer.config.out_channels // 2 == latent_channels: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = torch.split(_A , _A , dim=1 ) else: __SCREAMING_SNAKE_CASE : List[Any] = noise_pred # compute previous image: x_t -> x_t-1 __SCREAMING_SNAKE_CASE : str = self.scheduler.step(_A , _A , _A ).prev_sample if guidance_scale > 1: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = latent_model_input.chunk(2 , dim=0 ) else: __SCREAMING_SNAKE_CASE : Optional[Any] = latent_model_input __SCREAMING_SNAKE_CASE : List[Any] = 1 / self.vae.config.scaling_factor * latents __SCREAMING_SNAKE_CASE : List[str] = self.vae.decode(_A ).sample __SCREAMING_SNAKE_CASE : Any = (samples / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 __SCREAMING_SNAKE_CASE : int = samples.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": __SCREAMING_SNAKE_CASE : str = self.numpy_to_pil(_A ) if not return_dict: return (samples,) return ImagePipelineOutput(images=_A )
303
1
import functools def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = len(snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = len(snake_case ) @functools.cache def min_distance(snake_case , snake_case ) -> int: # if first word index is overflow - delete all from the second word if indexa >= len_worda: return len_worda - indexa # if second word index is overflow - delete all from the first word if indexa >= len_worda: return len_worda - indexa __SCREAMING_SNAKE_CASE : List[str] = int(worda[indexa] != worda[indexa] ) # current letters not identical return min( 1 + min_distance(indexa + 1 , snake_case ) , 1 + min_distance(snake_case , indexa + 1 ) , diff + min_distance(indexa + 1 , indexa + 1 ) , ) return min_distance(0 , 0 ) if __name__ == "__main__": import doctest doctest.testmod()
303
import os import sys lowercase_ = os.path.join(os.path.dirname(__file__), """src""") sys.path.append(SRC_DIR) from transformers import ( AutoConfig, AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoTokenizer, add_start_docstrings, ) lowercase_ = [ """torch""", """numpy""", """tokenizers""", """filelock""", """requests""", """tqdm""", """regex""", """sentencepiece""", """sacremoses""", """importlib_metadata""", """huggingface_hub""", ] @add_start_docstrings(AutoConfig.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoConfig.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoTokenizer.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoTokenizer.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModel.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModel.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForCausalLM.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForCausalLM.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForMaskedLM.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForMaskedLM.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForSequenceClassification.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForSequenceClassification.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForQuestionAnswering.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForQuestionAnswering.from_pretrained(*snake_case , **snake_case )
303
1
import os import pytest from attr import dataclass lowercase_ = """us-east-1""" # defaults region @dataclass class __UpperCamelCase : """simple docstring""" lowerCAmelCase_ = 42 lowerCAmelCase_ = '''arn:aws:iam::558105141721:role/sagemaker_execution_role''' lowerCAmelCase_ = { '''task_name''': '''mnli''', '''per_device_train_batch_size''': 16, '''per_device_eval_batch_size''': 16, '''do_train''': True, '''do_eval''': True, '''do_predict''': True, '''output_dir''': '''/opt/ml/model''', '''overwrite_output_dir''': True, '''max_steps''': 5_00, '''save_steps''': 55_00, } lowerCAmelCase_ = {**hyperparameters, '''max_steps''': 10_00} @property def UpperCAmelCase__ ( self : Dict ): """simple docstring""" if self.framework == "pytorch": return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"}, {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"}, ] else: return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"}, {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"}, ] @property def UpperCAmelCase__ ( self : Dict ): """simple docstring""" return F'''{self.framework}-transfromers-test''' @property def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" return F'''./tests/sagemaker/scripts/{self.framework}''' @property def UpperCAmelCase__ ( self : Optional[Any] ): """simple docstring""" if self.framework == "pytorch": return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04" else: return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04" @pytest.fixture(scope='''class''' ) def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = SageMakerTestEnvironment(framework=request.cls.framework )
303
from __future__ import annotations import numpy as np def a__ ( snake_case ): """simple docstring""" return np.maximum(0 , snake_case ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
303
1
from __future__ import annotations from random import random from typing import Generic, TypeVar lowercase_ = TypeVar("""KT""") lowercase_ = TypeVar("""VT""") class __UpperCamelCase ( Generic[KT, VT] ): """simple docstring""" def __init__( self : Optional[Any] , _A : KT | str = "root" , _A : VT | None = None ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = key __SCREAMING_SNAKE_CASE : Union[str, Any] = value __SCREAMING_SNAKE_CASE : list[Node[KT, VT]] = [] def __repr__( self : List[str] ): """simple docstring""" return F'''Node({self.key}: {self.value})''' @property def UpperCAmelCase__ ( self : List[Any] ): """simple docstring""" return len(self.forward ) class __UpperCamelCase ( Generic[KT, VT] ): """simple docstring""" def __init__( self : Optional[Any] , _A : float = 0.5 , _A : int = 16 ): """simple docstring""" __SCREAMING_SNAKE_CASE : Node[KT, VT] = Node[KT, VT]() __SCREAMING_SNAKE_CASE : Optional[int] = 0 __SCREAMING_SNAKE_CASE : Tuple = p __SCREAMING_SNAKE_CASE : List[Any] = max_level def __str__( self : int ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = list(self ) if len(_A ) == 0: return F'''SkipList(level={self.level})''' __SCREAMING_SNAKE_CASE : Optional[int] = max((len(str(_A ) ) for item in items) , default=4 ) __SCREAMING_SNAKE_CASE : int = max(_A , 4 ) + 4 __SCREAMING_SNAKE_CASE : str = self.head __SCREAMING_SNAKE_CASE : List[str] = [] __SCREAMING_SNAKE_CASE : str = node.forward.copy() lines.append(F'''[{node.key}]'''.ljust(_A , '''-''' ) + '''* ''' * len(_A ) ) lines.append(''' ''' * label_size + '''| ''' * len(_A ) ) while len(node.forward ) != 0: __SCREAMING_SNAKE_CASE : Optional[Any] = node.forward[0] lines.append( F'''[{node.key}]'''.ljust(_A , '''-''' ) + ''' '''.join(str(n.key ) if n.key == node.key else '''|''' for n in forwards ) ) lines.append(''' ''' * label_size + '''| ''' * len(_A ) ) __SCREAMING_SNAKE_CASE : int = node.forward lines.append('''None'''.ljust(_A ) + '''* ''' * len(_A ) ) return F'''SkipList(level={self.level})\n''' + "\n".join(_A ) def __iter__( self : List[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = self.head while len(node.forward ) != 0: yield node.forward[0].key __SCREAMING_SNAKE_CASE : Tuple = node.forward[0] def UpperCAmelCase__ ( self : Tuple ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = 1 while random() < self.p and level < self.max_level: level += 1 return level def UpperCAmelCase__ ( self : Tuple , _A : Dict ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = [] __SCREAMING_SNAKE_CASE : str = self.head for i in reversed(range(self.level ) ): # i < node.level - When node level is lesser than `i` decrement `i`. # node.forward[i].key < key - Jumping to node with key value higher # or equal to searched key would result # in skipping searched key. while i < node.level and node.forward[i].key < key: __SCREAMING_SNAKE_CASE : str = node.forward[i] # Each leftmost node (relative to searched node) will potentially have to # be updated. update_vector.append(_A ) update_vector.reverse() # Note that we were inserting values in reverse order. # len(node.forward) != 0 - If current node doesn't contain any further # references then searched key is not present. # node.forward[0].key == key - Next node key should be equal to search key # if key is present. if len(node.forward ) != 0 and node.forward[0].key == key: return node.forward[0], update_vector else: return None, update_vector def UpperCAmelCase__ ( self : Union[str, Any] , _A : KT ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = self._locate_node(_A ) if node is not None: for i, update_node in enumerate(_A ): # Remove or replace all references to removed node. if update_node.level > i and update_node.forward[i].key == key: if node.level > i: __SCREAMING_SNAKE_CASE : Tuple = node.forward[i] else: __SCREAMING_SNAKE_CASE : List[str] = update_node.forward[:i] def UpperCAmelCase__ ( self : List[Any] , _A : KT , _A : VT ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : List[Any] = self._locate_node(_A ) if node is not None: __SCREAMING_SNAKE_CASE : Union[str, Any] = value else: __SCREAMING_SNAKE_CASE : List[str] = self.random_level() if level > self.level: # After level increase we have to add additional nodes to head. for _ in range(self.level - 1 , _A ): update_vector.append(self.head ) __SCREAMING_SNAKE_CASE : Union[str, Any] = level __SCREAMING_SNAKE_CASE : Any = Node(_A , _A ) for i, update_node in enumerate(update_vector[:level] ): # Change references to pass through new node. if update_node.level > i: new_node.forward.append(update_node.forward[i] ) if update_node.level < i + 1: update_node.forward.append(_A ) else: __SCREAMING_SNAKE_CASE : int = new_node def UpperCAmelCase__ ( self : str , _A : VT ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : int = self._locate_node(_A ) if node is not None: return node.value return None def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = SkipList() skip_list.insert('''Key1''' , 3 ) skip_list.insert('''Key2''' , 12 ) skip_list.insert('''Key3''' , 41 ) skip_list.insert('''Key4''' , -19 ) __SCREAMING_SNAKE_CASE : str = skip_list.head __SCREAMING_SNAKE_CASE : int = {} while node.level != 0: __SCREAMING_SNAKE_CASE : Tuple = node.forward[0] __SCREAMING_SNAKE_CASE : Dict = node.value assert len(snake_case ) == 4 assert all_values["Key1"] == 3 assert all_values["Key2"] == 12 assert all_values["Key3"] == 41 assert all_values["Key4"] == -19 def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = SkipList() skip_list.insert('''Key1''' , 10 ) skip_list.insert('''Key1''' , 12 ) skip_list.insert('''Key5''' , 7 ) skip_list.insert('''Key7''' , 10 ) skip_list.insert('''Key10''' , 5 ) skip_list.insert('''Key7''' , 7 ) skip_list.insert('''Key5''' , 5 ) skip_list.insert('''Key10''' , 10 ) __SCREAMING_SNAKE_CASE : int = skip_list.head __SCREAMING_SNAKE_CASE : List[str] = {} while node.level != 0: __SCREAMING_SNAKE_CASE : str = node.forward[0] __SCREAMING_SNAKE_CASE : str = node.value if len(snake_case ) != 4: print() assert len(snake_case ) == 4 assert all_values["Key1"] == 12 assert all_values["Key7"] == 7 assert all_values["Key5"] == 5 assert all_values["Key10"] == 10 def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = SkipList() assert skip_list.find('''Some key''' ) is None def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = SkipList() skip_list.insert('''Key2''' , 20 ) assert skip_list.find('''Key2''' ) == 20 skip_list.insert('''Some Key''' , 10 ) skip_list.insert('''Key2''' , 8 ) skip_list.insert('''V''' , 13 ) assert skip_list.find('''Y''' ) is None assert skip_list.find('''Key2''' ) == 8 assert skip_list.find('''Some Key''' ) == 10 assert skip_list.find('''V''' ) == 13 def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = SkipList() skip_list.delete('''Some key''' ) assert len(skip_list.head.forward ) == 0 def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = SkipList() skip_list.insert('''Key1''' , 12 ) skip_list.insert('''V''' , 13 ) skip_list.insert('''X''' , 14 ) skip_list.insert('''Key2''' , 15 ) skip_list.delete('''V''' ) skip_list.delete('''Key2''' ) assert skip_list.find('''V''' ) is None assert skip_list.find('''Key2''' ) is None def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = SkipList() skip_list.insert('''Key1''' , 12 ) skip_list.insert('''V''' , 13 ) skip_list.insert('''X''' , 14 ) skip_list.insert('''Key2''' , 15 ) skip_list.delete('''V''' ) assert skip_list.find('''V''' ) is None assert skip_list.find('''X''' ) == 14 assert skip_list.find('''Key1''' ) == 12 assert skip_list.find('''Key2''' ) == 15 skip_list.delete('''X''' ) assert skip_list.find('''V''' ) is None assert skip_list.find('''X''' ) is None assert skip_list.find('''Key1''' ) == 12 assert skip_list.find('''Key2''' ) == 15 skip_list.delete('''Key1''' ) assert skip_list.find('''V''' ) is None assert skip_list.find('''X''' ) is None assert skip_list.find('''Key1''' ) is None assert skip_list.find('''Key2''' ) == 15 skip_list.delete('''Key2''' ) assert skip_list.find('''V''' ) is None assert skip_list.find('''X''' ) is None assert skip_list.find('''Key1''' ) is None assert skip_list.find('''Key2''' ) is None def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = SkipList() skip_list.insert('''Key1''' , 12 ) skip_list.insert('''V''' , 13 ) skip_list.insert('''X''' , 142 ) skip_list.insert('''Key2''' , 15 ) skip_list.delete('''X''' ) def traverse_keys(snake_case ): yield node.key for forward_node in node.forward: yield from traverse_keys(snake_case ) assert len(set(traverse_keys(skip_list.head ) ) ) == 4 def a__ ( ): """simple docstring""" def is_sorted(snake_case ): return all(next_item >= item for item, next_item in zip(snake_case , lst[1:] ) ) __SCREAMING_SNAKE_CASE : str = SkipList() for i in range(10 ): skip_list.insert(snake_case , snake_case ) assert is_sorted(list(snake_case ) ) skip_list.delete(5 ) skip_list.delete(8 ) skip_list.delete(2 ) assert is_sorted(list(snake_case ) ) skip_list.insert(-12 , -12 ) skip_list.insert(77 , 77 ) assert is_sorted(list(snake_case ) ) def a__ ( ): """simple docstring""" for _ in range(100 ): # Repeat test 100 times due to the probabilistic nature of skip list # random values == random bugs test_insert() test_insert_overrides_existing_value() test_searching_empty_list_returns_none() test_search() test_deleting_item_from_empty_list_do_nothing() test_deleted_items_are_not_founded_by_find_method() test_delete_removes_only_given_key() test_delete_doesnt_leave_dead_nodes() test_iter_always_yields_sorted_values() def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = SkipList() skip_list.insert(2 , '''2''' ) skip_list.insert(4 , '''4''' ) skip_list.insert(6 , '''4''' ) skip_list.insert(4 , '''5''' ) skip_list.insert(8 , '''4''' ) skip_list.insert(9 , '''4''' ) skip_list.delete(4 ) print(snake_case ) if __name__ == "__main__": import doctest doctest.testmod() main()
303
def a__ ( snake_case = 1_000_000 ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = 1 __SCREAMING_SNAKE_CASE : Optional[Any] = 1 __SCREAMING_SNAKE_CASE : Optional[int] = {1: 1} for inputa in range(2 , snake_case ): __SCREAMING_SNAKE_CASE : Tuple = 0 __SCREAMING_SNAKE_CASE : Optional[Any] = inputa while True: if number in counters: counter += counters[number] break if number % 2 == 0: number //= 2 counter += 1 else: __SCREAMING_SNAKE_CASE : List[Any] = (3 * number) + 1 counter += 1 if inputa not in counters: __SCREAMING_SNAKE_CASE : str = counter if counter > pre_counter: __SCREAMING_SNAKE_CASE : Optional[int] = inputa __SCREAMING_SNAKE_CASE : str = counter return largest_number if __name__ == "__main__": print(solution(int(input().strip())))
303
1
import inspect import re from hashlib import shaaaa from typing import Dict, List from .arrow import arrow from .audiofolder import audiofolder from .csv import csv from .imagefolder import imagefolder from .json import json from .pandas import pandas from .parquet import parquet from .sql import sql # noqa F401 from .text import text def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = [] for line in lines: __SCREAMING_SNAKE_CASE : Dict = re.sub(R'''#.*''' , '''''' , snake_case ) # remove comments if line: filtered_lines.append(snake_case ) __SCREAMING_SNAKE_CASE : Tuple = '''\n'''.join(snake_case ) # Make a hash from all this code __SCREAMING_SNAKE_CASE : List[Any] = full_str.encode('''utf-8''' ) return shaaaa(snake_case ).hexdigest() # get importable module names and hash for caching lowercase_ = { """csv""": (csv.__name__, _hash_python_lines(inspect.getsource(csv).splitlines())), """json""": (json.__name__, _hash_python_lines(inspect.getsource(json).splitlines())), """pandas""": (pandas.__name__, _hash_python_lines(inspect.getsource(pandas).splitlines())), """parquet""": (parquet.__name__, _hash_python_lines(inspect.getsource(parquet).splitlines())), """arrow""": (arrow.__name__, _hash_python_lines(inspect.getsource(arrow).splitlines())), """text""": (text.__name__, _hash_python_lines(inspect.getsource(text).splitlines())), """imagefolder""": (imagefolder.__name__, _hash_python_lines(inspect.getsource(imagefolder).splitlines())), """audiofolder""": (audiofolder.__name__, _hash_python_lines(inspect.getsource(audiofolder).splitlines())), } # Used to infer the module to use based on the data files extensions lowercase_ = { """.csv""": ("""csv""", {}), """.tsv""": ("""csv""", {"""sep""": """\t"""}), """.json""": ("""json""", {}), """.jsonl""": ("""json""", {}), """.parquet""": ("""parquet""", {}), """.arrow""": ("""arrow""", {}), """.txt""": ("""text""", {}), } _EXTENSION_TO_MODULE.update({ext: ("""imagefolder""", {}) for ext in imagefolder.ImageFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext.upper(): ("""imagefolder""", {}) for ext in imagefolder.ImageFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext: ("""audiofolder""", {}) for ext in audiofolder.AudioFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext.upper(): ("""audiofolder""", {}) for ext in audiofolder.AudioFolder.EXTENSIONS}) lowercase_ = {"""imagefolder""", """audiofolder"""} # Used to filter data files based on extensions given a module name lowercase_ = {} for _ext, (_module, _) in _EXTENSION_TO_MODULE.items(): _MODULE_TO_EXTENSIONS.setdefault(_module, []).append(_ext) _MODULE_TO_EXTENSIONS["imagefolder"].append(""".zip""") _MODULE_TO_EXTENSIONS["audiofolder"].append(""".zip""")
303
import argparse import json import os import pickle import shutil import numpy as np import torch from distiller import Distiller from lm_seqs_dataset import LmSeqsDataset from transformers import ( BertConfig, BertForMaskedLM, BertTokenizer, DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer, GPTaConfig, GPTaLMHeadModel, GPTaTokenizer, RobertaConfig, RobertaForMaskedLM, RobertaTokenizer, ) from utils import git_log, init_gpu_params, logger, set_seed lowercase_ = { """distilbert""": (DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer), """roberta""": (RobertaConfig, RobertaForMaskedLM, RobertaTokenizer), """bert""": (BertConfig, BertForMaskedLM, BertTokenizer), """gpt2""": (GPTaConfig, GPTaLMHeadModel, GPTaTokenizer), } def a__ ( snake_case ): """simple docstring""" assert (args.mlm and args.alpha_mlm > 0.0) or (not args.mlm and args.alpha_mlm == 0.0) assert (args.alpha_mlm > 0.0 and args.alpha_clm == 0.0) or (args.alpha_mlm == 0.0 and args.alpha_clm > 0.0) if args.mlm: assert os.path.isfile(args.token_counts ) assert (args.student_type in ["roberta", "distilbert"]) and (args.teacher_type in ["roberta", "bert"]) else: assert (args.student_type in ["gpt2"]) and (args.teacher_type in ["gpt2"]) assert args.teacher_type == args.student_type or ( args.student_type == "distilbert" and args.teacher_type == "bert" ) assert os.path.isfile(args.student_config ) if args.student_pretrained_weights is not None: assert os.path.isfile(args.student_pretrained_weights ) if args.freeze_token_type_embds: assert args.student_type in ["roberta"] assert args.alpha_ce >= 0.0 assert args.alpha_mlm >= 0.0 assert args.alpha_clm >= 0.0 assert args.alpha_mse >= 0.0 assert args.alpha_cos >= 0.0 assert args.alpha_ce + args.alpha_mlm + args.alpha_clm + args.alpha_mse + args.alpha_cos > 0.0 def a__ ( snake_case , snake_case ): """simple docstring""" if args.student_type == "roberta": __SCREAMING_SNAKE_CASE : int = False elif args.student_type == "gpt2": __SCREAMING_SNAKE_CASE : Optional[int] = False def a__ ( snake_case , snake_case ): """simple docstring""" if args.student_type == "roberta": __SCREAMING_SNAKE_CASE : Dict = False def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = argparse.ArgumentParser(description='''Training''' ) parser.add_argument('''--force''' , action='''store_true''' , help='''Overwrite dump_path if it already exists.''' ) parser.add_argument( '''--dump_path''' , type=snake_case , required=snake_case , help='''The output directory (log, checkpoints, parameters, etc.)''' ) parser.add_argument( '''--data_file''' , type=snake_case , required=snake_case , help='''The binarized file (tokenized + tokens_to_ids) and grouped by sequence.''' , ) parser.add_argument( '''--student_type''' , type=snake_case , choices=['''distilbert''', '''roberta''', '''gpt2'''] , required=snake_case , help='''The student type (DistilBERT, RoBERTa).''' , ) parser.add_argument('''--student_config''' , type=snake_case , required=snake_case , help='''Path to the student configuration.''' ) parser.add_argument( '''--student_pretrained_weights''' , default=snake_case , type=snake_case , help='''Load student initialization checkpoint.''' ) parser.add_argument( '''--teacher_type''' , choices=['''bert''', '''roberta''', '''gpt2'''] , required=snake_case , help='''Teacher type (BERT, RoBERTa).''' ) parser.add_argument('''--teacher_name''' , type=snake_case , required=snake_case , help='''The teacher model.''' ) parser.add_argument('''--temperature''' , default=2.0 , type=snake_case , help='''Temperature for the softmax temperature.''' ) parser.add_argument( '''--alpha_ce''' , default=0.5 , type=snake_case , help='''Linear weight for the distillation loss. Must be >=0.''' ) parser.add_argument( '''--alpha_mlm''' , default=0.0 , type=snake_case , help='''Linear weight for the MLM loss. Must be >=0. Should be used in conjunction with `mlm` flag.''' , ) parser.add_argument('''--alpha_clm''' , default=0.5 , type=snake_case , help='''Linear weight for the CLM loss. Must be >=0.''' ) parser.add_argument('''--alpha_mse''' , default=0.0 , type=snake_case , help='''Linear weight of the MSE loss. Must be >=0.''' ) parser.add_argument( '''--alpha_cos''' , default=0.0 , type=snake_case , help='''Linear weight of the cosine embedding loss. Must be >=0.''' ) parser.add_argument( '''--mlm''' , action='''store_true''' , help='''The LM step: MLM or CLM. If `mlm` is True, the MLM is used over CLM.''' ) parser.add_argument( '''--mlm_mask_prop''' , default=0.15 , type=snake_case , help='''Proportion of tokens for which we need to make a prediction.''' , ) parser.add_argument('''--word_mask''' , default=0.8 , type=snake_case , help='''Proportion of tokens to mask out.''' ) parser.add_argument('''--word_keep''' , default=0.1 , type=snake_case , help='''Proportion of tokens to keep.''' ) parser.add_argument('''--word_rand''' , default=0.1 , type=snake_case , help='''Proportion of tokens to randomly replace.''' ) parser.add_argument( '''--mlm_smoothing''' , default=0.7 , type=snake_case , help='''Smoothing parameter to emphasize more rare tokens (see XLM, similar to word2vec).''' , ) parser.add_argument('''--token_counts''' , type=snake_case , help='''The token counts in the data_file for MLM.''' ) parser.add_argument( '''--restrict_ce_to_mask''' , action='''store_true''' , help='''If true, compute the distillation loss only the [MLM] prediction distribution.''' , ) parser.add_argument( '''--freeze_pos_embs''' , action='''store_true''' , help='''Freeze positional embeddings during distillation. For student_type in [\'roberta\', \'gpt2\'] only.''' , ) parser.add_argument( '''--freeze_token_type_embds''' , action='''store_true''' , help='''Freeze token type embeddings during distillation if existent. For student_type in [\'roberta\'] only.''' , ) parser.add_argument('''--n_epoch''' , type=snake_case , default=3 , help='''Number of pass on the whole dataset.''' ) parser.add_argument('''--batch_size''' , type=snake_case , default=5 , help='''Batch size (for each process).''' ) parser.add_argument( '''--group_by_size''' , action='''store_false''' , help='''If true, group sequences that have similar length into the same batch. Default is true.''' , ) parser.add_argument( '''--gradient_accumulation_steps''' , type=snake_case , default=50 , help='''Gradient accumulation for larger training batches.''' , ) parser.add_argument('''--warmup_prop''' , default=0.05 , type=snake_case , help='''Linear warmup proportion.''' ) parser.add_argument('''--weight_decay''' , default=0.0 , type=snake_case , help='''Weight decay if we apply some.''' ) parser.add_argument('''--learning_rate''' , default=5E-4 , type=snake_case , help='''The initial learning rate for Adam.''' ) parser.add_argument('''--adam_epsilon''' , default=1E-6 , type=snake_case , help='''Epsilon for Adam optimizer.''' ) parser.add_argument('''--max_grad_norm''' , default=5.0 , type=snake_case , help='''Max gradient norm.''' ) parser.add_argument('''--initializer_range''' , default=0.02 , type=snake_case , help='''Random initialization range.''' ) parser.add_argument( '''--fp16''' , action='''store_true''' , help='''Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit''' , ) parser.add_argument( '''--fp16_opt_level''' , type=snake_case , default='''O1''' , help=( '''For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\'].''' '''See details at https://nvidia.github.io/apex/amp.html''' ) , ) parser.add_argument('''--n_gpu''' , type=snake_case , default=1 , help='''Number of GPUs in the node.''' ) parser.add_argument('''--local_rank''' , type=snake_case , default=-1 , help='''Distributed training - Local rank''' ) parser.add_argument('''--seed''' , type=snake_case , default=56 , help='''Random seed''' ) parser.add_argument('''--log_interval''' , type=snake_case , default=500 , help='''Tensorboard logging interval.''' ) parser.add_argument('''--checkpoint_interval''' , type=snake_case , default=4_000 , help='''Checkpoint interval.''' ) __SCREAMING_SNAKE_CASE : Optional[int] = parser.parse_args() sanity_checks(snake_case ) # ARGS # init_gpu_params(snake_case ) set_seed(snake_case ) if args.is_master: if os.path.exists(args.dump_path ): if not args.force: raise ValueError( F'''Serialization dir {args.dump_path} already exists, but you have not precised wheter to overwrite''' ''' itUse `--force` if you want to overwrite it''' ) else: shutil.rmtree(args.dump_path ) if not os.path.exists(args.dump_path ): os.makedirs(args.dump_path ) logger.info(F'''Experiment will be dumped and logged in {args.dump_path}''' ) # SAVE PARAMS # logger.info(F'''Param: {args}''' ) with open(os.path.join(args.dump_path , '''parameters.json''' ) , '''w''' ) as f: json.dump(vars(snake_case ) , snake_case , indent=4 ) git_log(args.dump_path ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = MODEL_CLASSES[args.student_type] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = MODEL_CLASSES[args.teacher_type] # TOKENIZER # __SCREAMING_SNAKE_CASE : Optional[int] = teacher_tokenizer_class.from_pretrained(args.teacher_name ) __SCREAMING_SNAKE_CASE : Optional[Any] = {} for tok_name, tok_symbol in tokenizer.special_tokens_map.items(): __SCREAMING_SNAKE_CASE : Any = tokenizer.all_special_tokens.index(snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.all_special_ids[idx] logger.info(F'''Special tokens {special_tok_ids}''' ) __SCREAMING_SNAKE_CASE : Any = special_tok_ids __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.max_model_input_sizes[args.teacher_name] # DATA LOADER # logger.info(F'''Loading data from {args.data_file}''' ) with open(args.data_file , '''rb''' ) as fp: __SCREAMING_SNAKE_CASE : List[str] = pickle.load(snake_case ) if args.mlm: logger.info(F'''Loading token counts from {args.token_counts} (already pre-computed)''' ) with open(args.token_counts , '''rb''' ) as fp: __SCREAMING_SNAKE_CASE : Optional[Any] = pickle.load(snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = np.maximum(snake_case , 1 ) ** -args.mlm_smoothing for idx in special_tok_ids.values(): __SCREAMING_SNAKE_CASE : Any = 0.0 # do not predict special tokens __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.from_numpy(snake_case ) else: __SCREAMING_SNAKE_CASE : Optional[int] = None __SCREAMING_SNAKE_CASE : Optional[Any] = LmSeqsDataset(params=snake_case , data=snake_case ) logger.info('''Data loader created.''' ) # STUDENT # logger.info(F'''Loading student config from {args.student_config}''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = student_config_class.from_pretrained(args.student_config ) __SCREAMING_SNAKE_CASE : Dict = True if args.student_pretrained_weights is not None: logger.info(F'''Loading pretrained weights from {args.student_pretrained_weights}''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = student_model_class.from_pretrained(args.student_pretrained_weights , config=snake_case ) else: __SCREAMING_SNAKE_CASE : str = student_model_class(snake_case ) if args.n_gpu > 0: student.to(F'''cuda:{args.local_rank}''' ) logger.info('''Student loaded.''' ) # TEACHER # __SCREAMING_SNAKE_CASE : List[str] = teacher_model_class.from_pretrained(args.teacher_name , output_hidden_states=snake_case ) if args.n_gpu > 0: teacher.to(F'''cuda:{args.local_rank}''' ) logger.info(F'''Teacher loaded from {args.teacher_name}.''' ) # FREEZING # if args.freeze_pos_embs: freeze_pos_embeddings(snake_case , snake_case ) if args.freeze_token_type_embds: freeze_token_type_embeddings(snake_case , snake_case ) # SANITY CHECKS # assert student.config.vocab_size == teacher.config.vocab_size assert student.config.hidden_size == teacher.config.hidden_size assert student.config.max_position_embeddings == teacher.config.max_position_embeddings if args.mlm: assert token_probs.size(0 ) == stu_architecture_config.vocab_size # DISTILLER # torch.cuda.empty_cache() __SCREAMING_SNAKE_CASE : int = Distiller( params=snake_case , dataset=snake_case , token_probs=snake_case , student=snake_case , teacher=snake_case ) distiller.train() logger.info('''Let\'s go get some drinks.''' ) if __name__ == "__main__": main()
303
1
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = """▁""" lowercase_ = { """vocab_file""": """vocab.json""", """spm_file""": """sentencepiece.bpe.model""", """tokenizer_config_file""": """tokenizer_config.json""", } lowercase_ = { """vocab_file""": { """facebook/m2m100_418M""": """https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json""", """facebook/m2m100_1.2B""": """https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json""", }, """spm_file""": { """facebook/m2m100_418M""": """https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model""", """facebook/m2m100_1.2B""": """https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model""", }, """tokenizer_config_file""": { """facebook/m2m100_418M""": """https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json""", """facebook/m2m100_1.2B""": """https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json""", }, } lowercase_ = { """facebook/m2m100_418M""": 1_024, } # fmt: off lowercase_ = { """m2m100""": ["""af""", """am""", """ar""", """ast""", """az""", """ba""", """be""", """bg""", """bn""", """br""", """bs""", """ca""", """ceb""", """cs""", """cy""", """da""", """de""", """el""", """en""", """es""", """et""", """fa""", """ff""", """fi""", """fr""", """fy""", """ga""", """gd""", """gl""", """gu""", """ha""", """he""", """hi""", """hr""", """ht""", """hu""", """hy""", """id""", """ig""", """ilo""", """is""", """it""", """ja""", """jv""", """ka""", """kk""", """km""", """kn""", """ko""", """lb""", """lg""", """ln""", """lo""", """lt""", """lv""", """mg""", """mk""", """ml""", """mn""", """mr""", """ms""", """my""", """ne""", """nl""", """no""", """ns""", """oc""", """or""", """pa""", """pl""", """ps""", """pt""", """ro""", """ru""", """sd""", """si""", """sk""", """sl""", """so""", """sq""", """sr""", """ss""", """su""", """sv""", """sw""", """ta""", """th""", """tl""", """tn""", """tr""", """uk""", """ur""", """uz""", """vi""", """wo""", """xh""", """yi""", """yo""", """zh""", """zu"""], """wmt21""": ["""en""", """ha""", """is""", """ja""", """cs""", """ru""", """zh""", """de"""] } class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = VOCAB_FILES_NAMES lowerCAmelCase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase_ = PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase_ = ['''input_ids''', '''attention_mask'''] lowerCAmelCase_ = [] lowerCAmelCase_ = [] def __init__( self : Optional[Any] , _A : Optional[int] , _A : Any , _A : Any=None , _A : Union[str, Any]=None , _A : Dict="<s>" , _A : Dict="</s>" , _A : Optional[int]="</s>" , _A : Optional[int]="<pad>" , _A : Tuple="<unk>" , _A : Tuple="m2m100" , _A : Optional[Dict[str, Any]] = None , _A : int=8 , **_A : Dict , ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = {} if sp_model_kwargs is None else sp_model_kwargs __SCREAMING_SNAKE_CASE : int = language_codes __SCREAMING_SNAKE_CASE : List[str] = FAIRSEQ_LANGUAGE_CODES[language_codes] __SCREAMING_SNAKE_CASE : Dict = {lang_code: F'''__{lang_code}__''' for lang_code in fairseq_language_code} __SCREAMING_SNAKE_CASE : Optional[Any] = kwargs.get('''additional_special_tokens''' , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(_A ) for lang_code in fairseq_language_code if self.get_lang_token(_A ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=_A , tgt_lang=_A , bos_token=_A , eos_token=_A , sep_token=_A , unk_token=_A , pad_token=_A , language_codes=_A , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=_A , **_A , ) __SCREAMING_SNAKE_CASE : List[Any] = vocab_file __SCREAMING_SNAKE_CASE : Tuple = load_json(_A ) __SCREAMING_SNAKE_CASE : List[str] = {v: k for k, v in self.encoder.items()} __SCREAMING_SNAKE_CASE : int = spm_file __SCREAMING_SNAKE_CASE : Optional[int] = load_spm(_A , self.sp_model_kwargs ) __SCREAMING_SNAKE_CASE : Optional[int] = len(self.encoder ) __SCREAMING_SNAKE_CASE : Optional[int] = { self.get_lang_token(_A ): self.encoder_size + i for i, lang_code in enumerate(_A ) } __SCREAMING_SNAKE_CASE : Tuple = {lang_code: self.encoder_size + i for i, lang_code in enumerate(_A )} __SCREAMING_SNAKE_CASE : Optional[Any] = {v: k for k, v in self.lang_token_to_id.items()} __SCREAMING_SNAKE_CASE : str = src_lang if src_lang is not None else '''en''' __SCREAMING_SNAKE_CASE : str = tgt_lang __SCREAMING_SNAKE_CASE : List[str] = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) __SCREAMING_SNAKE_CASE : int = num_madeup_words @property def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def UpperCAmelCase__ ( self : int ): """simple docstring""" return self._src_lang @src_lang.setter def UpperCAmelCase__ ( self : List[str] , _A : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def UpperCAmelCase__ ( self : int , _A : str ): """simple docstring""" return self.sp_model.encode(_A , out_type=_A ) def UpperCAmelCase__ ( self : Tuple , _A : Tuple ): """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(_A , self.encoder[self.unk_token] ) def UpperCAmelCase__ ( self : List[str] , _A : int ): """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(_A , self.unk_token ) def UpperCAmelCase__ ( self : Optional[Any] , _A : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = [] __SCREAMING_SNAKE_CASE : List[Any] = '''''' 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 __SCREAMING_SNAKE_CASE : Any = [] else: current_sub_tokens.append(_A ) out_string += self.sp_model.decode(_A ) return out_string.strip() def UpperCAmelCase__ ( 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 ) __SCREAMING_SNAKE_CASE : List[Any] = [1] * len(self.prefix_tokens ) __SCREAMING_SNAKE_CASE : int = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(_A )) + suffix_ones return prefix_ones + ([0] * len(_A )) + ([0] * len(_A )) + suffix_ones def UpperCAmelCase__ ( self : List[str] , _A : List[int] , _A : Optional[List[int]] = None ): """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def UpperCAmelCase__ ( self : Dict ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = {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 : List[str] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = self.__dict__.copy() __SCREAMING_SNAKE_CASE : Dict = None return state def __setstate__( self : Union[str, Any] , _A : Dict ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): __SCREAMING_SNAKE_CASE : List[Any] = {} __SCREAMING_SNAKE_CASE : List[Any] = load_spm(self.spm_file , self.sp_model_kwargs ) def UpperCAmelCase__ ( self : Union[str, Any] , _A : str , _A : Optional[str] = None ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = Path(_A ) if not save_dir.is_dir(): raise OSError(F'''{save_directory} should be a directory''' ) __SCREAMING_SNAKE_CASE : Union[str, Any] = save_dir / ( (filename_prefix + '''-''' if filename_prefix else '''''') + self.vocab_files_names['''vocab_file'''] ) __SCREAMING_SNAKE_CASE : Tuple = save_dir / ( (filename_prefix + '''-''' if filename_prefix else '''''') + self.vocab_files_names['''spm_file'''] ) save_json(self.encoder , _A ) if os.path.abspath(self.spm_file ) != os.path.abspath(_A ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , _A ) elif not os.path.isfile(self.spm_file ): with open(_A , '''wb''' ) as fi: __SCREAMING_SNAKE_CASE : Optional[Any] = self.sp_model.serialized_model_proto() fi.write(_A ) return (str(_A ), str(_A )) def UpperCAmelCase__ ( self : int , _A : List[str] , _A : str = "en" , _A : Optional[List[str]] = None , _A : str = "ro" , **_A : Union[str, Any] , ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = src_lang __SCREAMING_SNAKE_CASE : Optional[Any] = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(_A , _A , **_A ) def UpperCAmelCase__ ( self : str , _A : List[Any] , _A : Optional[str] , _A : Optional[str] , **_A : Tuple ): """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError('''Translation requires a `src_lang` and a `tgt_lang` for this model''' ) __SCREAMING_SNAKE_CASE : Dict = src_lang __SCREAMING_SNAKE_CASE : List[Any] = self(_A , add_special_tokens=_A , **_A ) __SCREAMING_SNAKE_CASE : Dict = self.get_lang_id(_A ) __SCREAMING_SNAKE_CASE : str = tgt_lang_id return inputs def UpperCAmelCase__ ( self : Optional[Any] ): """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def UpperCAmelCase__ ( self : str ): """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def UpperCAmelCase__ ( self : Tuple , _A : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = self.get_lang_token(_A ) __SCREAMING_SNAKE_CASE : Dict = self.lang_token_to_id[lang_token] __SCREAMING_SNAKE_CASE : Dict = [self.cur_lang_id] __SCREAMING_SNAKE_CASE : int = [self.eos_token_id] def UpperCAmelCase__ ( self : Optional[Any] , _A : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = self.get_lang_token(_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = self.lang_token_to_id[lang_token] __SCREAMING_SNAKE_CASE : Tuple = [self.cur_lang_id] __SCREAMING_SNAKE_CASE : Dict = [self.eos_token_id] def UpperCAmelCase__ ( self : Optional[int] , _A : str ): """simple docstring""" return self.lang_code_to_token[lang] def UpperCAmelCase__ ( self : Dict , _A : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = self.get_lang_token(_A ) return self.lang_token_to_id[lang_token] def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = sentencepiece.SentencePieceProcessor(**snake_case ) spm.Load(str(snake_case ) ) return spm def a__ ( snake_case ): """simple docstring""" with open(snake_case , '''r''' ) as f: return json.load(snake_case ) def a__ ( snake_case , snake_case ): """simple docstring""" with open(snake_case , '''w''' ) as f: json.dump(snake_case , snake_case , indent=2 )
303
import math import os import sys def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = '''''' try: with open(snake_case , '''rb''' ) as binary_file: __SCREAMING_SNAKE_CASE : int = binary_file.read() for dat in data: __SCREAMING_SNAKE_CASE : Optional[Any] = F'''{dat:08b}''' result += curr_byte return result except OSError: print('''File not accessible''' ) sys.exit() def a__ ( snake_case , snake_case , snake_case , snake_case ): """simple docstring""" lexicon.pop(snake_case ) __SCREAMING_SNAKE_CASE : List[str] = last_match_id if math.loga(snake_case ).is_integer(): for curr_key in lexicon: __SCREAMING_SNAKE_CASE : int = '''0''' + lexicon[curr_key] __SCREAMING_SNAKE_CASE : List[str] = bin(snake_case )[2:] def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = {'''0''': '''0''', '''1''': '''1'''} __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Any = '''''', '''''' __SCREAMING_SNAKE_CASE : Optional[Any] = len(snake_case ) for i in range(len(snake_case ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue __SCREAMING_SNAKE_CASE : Any = lexicon[curr_string] result += last_match_id add_key_to_lexicon(snake_case , snake_case , snake_case , snake_case ) index += 1 __SCREAMING_SNAKE_CASE : Tuple = '''''' while curr_string != "" and curr_string not in lexicon: curr_string += "0" if curr_string != "": __SCREAMING_SNAKE_CASE : Dict = lexicon[curr_string] result += last_match_id return result def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = os.path.getsize(snake_case ) __SCREAMING_SNAKE_CASE : Union[str, Any] = bin(snake_case )[2:] __SCREAMING_SNAKE_CASE : int = len(snake_case ) return "0" * (length_length - 1) + file_length_binary + compressed def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = 8 try: with open(snake_case , '''wb''' ) as opened_file: __SCREAMING_SNAKE_CASE : Optional[int] = [ to_write[i : i + byte_length] for i in range(0 , len(snake_case ) , snake_case ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append('''10000000''' ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array: opened_file.write(int(snake_case , 2 ).to_bytes(1 , byteorder='''big''' ) ) except OSError: print('''File not accessible''' ) sys.exit() def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = read_file_binary(snake_case ) __SCREAMING_SNAKE_CASE : Optional[int] = compress_data(snake_case ) __SCREAMING_SNAKE_CASE : Dict = add_file_length(snake_case , snake_case ) write_file_binary(snake_case , snake_case ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
303
1
from typing import Tuple, Union from ...modeling_outputs import BackboneOutput from ...modeling_utils import PreTrainedModel from ...utils import is_timm_available, is_torch_available, requires_backends from ...utils.backbone_utils import BackboneMixin from .configuration_timm_backbone import TimmBackboneConfig if is_timm_available(): import timm if is_torch_available(): from torch import Tensor class __UpperCamelCase ( lowerCAmelCase__ , lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''pixel_values''' lowerCAmelCase_ = False lowerCAmelCase_ = TimmBackboneConfig def __init__( self : str , _A : Dict , **_A : Optional[Any] ): """simple docstring""" requires_backends(self , '''timm''' ) super().__init__(_A ) __SCREAMING_SNAKE_CASE : List[str] = config if config.backbone is None: raise ValueError('''backbone is not set in the config. Please set it to a timm model name.''' ) if config.backbone not in timm.list_models(): raise ValueError(F'''backbone {config.backbone} is not supported by timm.''' ) if hasattr(_A , '''out_features''' ) and config.out_features is not None: raise ValueError('''out_features is not supported by TimmBackbone. Please use out_indices instead.''' ) __SCREAMING_SNAKE_CASE : List[Any] = getattr(_A , '''use_pretrained_backbone''' , _A ) if pretrained is None: raise ValueError('''use_pretrained_backbone is not set in the config. Please set it to True or False.''' ) # We just take the final layer by default. This matches the default for the transformers models. __SCREAMING_SNAKE_CASE : Optional[int] = config.out_indices if getattr(_A , '''out_indices''' , _A ) is not None else (-1,) __SCREAMING_SNAKE_CASE : Optional[int] = timm.create_model( config.backbone , pretrained=_A , features_only=config.features_only , in_chans=config.num_channels , out_indices=_A , **_A , ) # These are used to control the output of the model when called. If output_hidden_states is True, then # return_layers is modified to include all layers. __SCREAMING_SNAKE_CASE : Optional[int] = self._backbone.return_layers __SCREAMING_SNAKE_CASE : Tuple = {layer['''module''']: str(_A ) for i, layer in enumerate(self._backbone.feature_info.info )} super()._init_backbone(_A ) @classmethod def UpperCAmelCase__ ( cls : Union[str, Any] , _A : List[str] , *_A : Any , **_A : List[str] ): """simple docstring""" requires_backends(cls , ['''vision''', '''timm'''] ) from ...models.timm_backbone import TimmBackboneConfig __SCREAMING_SNAKE_CASE : Union[str, Any] = kwargs.pop('''config''' , TimmBackboneConfig() ) __SCREAMING_SNAKE_CASE : List[str] = kwargs.pop('''use_timm_backbone''' , _A ) if not use_timm: raise ValueError('''use_timm_backbone must be True for timm backbones''' ) __SCREAMING_SNAKE_CASE : Tuple = kwargs.pop('''num_channels''' , config.num_channels ) __SCREAMING_SNAKE_CASE : Any = kwargs.pop('''features_only''' , config.features_only ) __SCREAMING_SNAKE_CASE : List[str] = kwargs.pop('''use_pretrained_backbone''' , config.use_pretrained_backbone ) __SCREAMING_SNAKE_CASE : List[Any] = kwargs.pop('''out_indices''' , config.out_indices ) __SCREAMING_SNAKE_CASE : List[Any] = TimmBackboneConfig( backbone=_A , num_channels=_A , features_only=_A , use_pretrained_backbone=_A , out_indices=_A , ) return super()._from_config(_A , **_A ) def UpperCAmelCase__ ( self : Union[str, Any] , _A : Optional[int] ): """simple docstring""" pass def UpperCAmelCase__ ( self : Tuple , _A : int , _A : int=None , _A : List[str]=None , _A : Optional[int]=None , **_A : Union[str, Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = return_dict if return_dict is not None else self.config.use_return_dict __SCREAMING_SNAKE_CASE : List[Any] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __SCREAMING_SNAKE_CASE : Optional[Any] = output_attentions if output_attentions is not None else self.config.output_attentions if output_attentions: raise ValueError('''Cannot output attentions for timm backbones at the moment''' ) if output_hidden_states: # We modify the return layers to include all the stages of the backbone __SCREAMING_SNAKE_CASE : int = self._all_layers __SCREAMING_SNAKE_CASE : Optional[Any] = self._backbone(_A , **_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = self._return_layers __SCREAMING_SNAKE_CASE : Union[str, Any] = tuple(hidden_states[i] for i in self.out_indices ) else: __SCREAMING_SNAKE_CASE : Optional[int] = self._backbone(_A , **_A ) __SCREAMING_SNAKE_CASE : List[str] = None __SCREAMING_SNAKE_CASE : str = tuple(_A ) __SCREAMING_SNAKE_CASE : Dict = tuple(_A ) if hidden_states is not None else None if not return_dict: __SCREAMING_SNAKE_CASE : int = (feature_maps,) if output_hidden_states: __SCREAMING_SNAKE_CASE : int = output + (hidden_states,) return output return BackboneOutput(feature_maps=_A , hidden_states=_A , attentions=_A )
303
import unittest import numpy as np import torch from torch import nn from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import enable_full_determinism, skip_mps from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __UpperCamelCase ( lowerCAmelCase__ , unittest.TestCase ): """simple docstring""" lowerCAmelCase_ = KandinskyVaaPriorPipeline lowerCAmelCase_ = ['''prompt'''] lowerCAmelCase_ = ['''prompt''', '''negative_prompt'''] lowerCAmelCase_ = [ '''num_images_per_prompt''', '''generator''', '''num_inference_steps''', '''latents''', '''negative_prompt''', '''guidance_scale''', '''output_type''', '''return_dict''', ] lowerCAmelCase_ = False @property def UpperCAmelCase__ ( self : int ): """simple docstring""" return 32 @property def UpperCAmelCase__ ( self : List[str] ): """simple docstring""" return 32 @property def UpperCAmelCase__ ( self : Dict ): """simple docstring""" return self.time_input_dim @property def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" return self.time_input_dim * 4 @property def UpperCAmelCase__ ( self : List[Any] ): """simple docstring""" return 100 @property def UpperCAmelCase__ ( self : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) return tokenizer @property def UpperCAmelCase__ ( self : List[Any] ): """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : List[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(_A ) @property def UpperCAmelCase__ ( self : str ): """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : Dict = { '''num_attention_heads''': 2, '''attention_head_dim''': 12, '''embedding_dim''': self.text_embedder_hidden_size, '''num_layers''': 1, } __SCREAMING_SNAKE_CASE : Optional[Any] = PriorTransformer(**_A ) # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0 __SCREAMING_SNAKE_CASE : List[Any] = nn.Parameter(torch.ones(model.clip_std.shape ) ) return model @property def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : int = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , image_size=224 , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_channels=3 , num_hidden_layers=5 , patch_size=14 , ) __SCREAMING_SNAKE_CASE : str = CLIPVisionModelWithProjection(_A ) return model @property def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = CLIPImageProcessor( crop_size=224 , do_center_crop=_A , do_normalize=_A , do_resize=_A , image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73] , image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11] , resample=3 , size=224 , ) return image_processor def UpperCAmelCase__ ( self : Tuple ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = self.dummy_prior __SCREAMING_SNAKE_CASE : str = self.dummy_image_encoder __SCREAMING_SNAKE_CASE : Optional[int] = self.dummy_text_encoder __SCREAMING_SNAKE_CASE : List[Any] = self.dummy_tokenizer __SCREAMING_SNAKE_CASE : Union[str, Any] = self.dummy_image_processor __SCREAMING_SNAKE_CASE : str = UnCLIPScheduler( variance_type='''fixed_small_log''' , prediction_type='''sample''' , num_train_timesteps=1000 , clip_sample=_A , clip_sample_range=10.0 , ) __SCREAMING_SNAKE_CASE : int = { '''prior''': prior, '''image_encoder''': image_encoder, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''scheduler''': scheduler, '''image_processor''': image_processor, } return components def UpperCAmelCase__ ( self : Union[str, Any] , _A : int , _A : Dict=0 ): """simple docstring""" if str(_A ).startswith('''mps''' ): __SCREAMING_SNAKE_CASE : Tuple = torch.manual_seed(_A ) else: __SCREAMING_SNAKE_CASE : str = torch.Generator(device=_A ).manual_seed(_A ) __SCREAMING_SNAKE_CASE : List[str] = { '''prompt''': '''horse''', '''generator''': generator, '''guidance_scale''': 4.0, '''num_inference_steps''': 2, '''output_type''': '''np''', } return inputs def UpperCAmelCase__ ( self : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = '''cpu''' __SCREAMING_SNAKE_CASE : Any = self.get_dummy_components() __SCREAMING_SNAKE_CASE : Any = self.pipeline_class(**_A ) __SCREAMING_SNAKE_CASE : List[Any] = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) __SCREAMING_SNAKE_CASE : int = pipe(**self.get_dummy_inputs(_A ) ) __SCREAMING_SNAKE_CASE : Tuple = output.image_embeds __SCREAMING_SNAKE_CASE : Optional[Any] = pipe( **self.get_dummy_inputs(_A ) , return_dict=_A , )[0] __SCREAMING_SNAKE_CASE : Tuple = image[0, -10:] __SCREAMING_SNAKE_CASE : List[Any] = image_from_tuple[0, -10:] assert image.shape == (1, 32) __SCREAMING_SNAKE_CASE : List[str] = np.array( [-0.05_32, 1.71_20, 0.36_56, -1.08_52, -0.89_46, -1.17_56, 0.43_48, 0.24_82, 0.51_46, -0.11_56] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @skip_mps def UpperCAmelCase__ ( self : List[str] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = torch_device == '''cpu''' __SCREAMING_SNAKE_CASE : Any = True __SCREAMING_SNAKE_CASE : int = False self._test_inference_batch_single_identical( test_max_difference=_A , relax_max_difference=_A , test_mean_pixel_difference=_A , ) @skip_mps def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = torch_device == '''cpu''' __SCREAMING_SNAKE_CASE : List[Any] = False self._test_attention_slicing_forward_pass( test_max_difference=_A , test_mean_pixel_difference=_A , )
303
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowercase_ = { """configuration_graphormer""": ["""GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GraphormerConfig"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """GraphormerForGraphClassification""", """GraphormerModel""", """GraphormerPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_graphormer import GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, GraphormerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_graphormer import ( GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST, GraphormerForGraphClassification, GraphormerModel, GraphormerPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
303
import argparse import logging import pickle import random import time import numpy as np from transformers import BertTokenizer, GPTaTokenizer, RobertaTokenizer logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""", datefmt="""%m/%d/%Y %H:%M:%S""", level=logging.INFO ) lowercase_ = logging.getLogger(__name__) def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = argparse.ArgumentParser( description='''Preprocess the data to avoid re-doing it several times by (tokenization + token_to_ids).''' ) parser.add_argument('''--file_path''' , type=snake_case , default='''data/dump.txt''' , help='''The path to the data.''' ) parser.add_argument('''--tokenizer_type''' , type=snake_case , default='''bert''' , choices=['''bert''', '''roberta''', '''gpt2'''] ) parser.add_argument('''--tokenizer_name''' , type=snake_case , default='''bert-base-uncased''' , help='''The tokenizer to use.''' ) parser.add_argument('''--dump_file''' , type=snake_case , default='''data/dump''' , help='''The dump file prefix.''' ) __SCREAMING_SNAKE_CASE : List[Any] = parser.parse_args() logger.info(F'''Loading Tokenizer ({args.tokenizer_name})''' ) if args.tokenizer_type == "bert": __SCREAMING_SNAKE_CASE : Union[str, Any] = BertTokenizer.from_pretrained(args.tokenizer_name ) __SCREAMING_SNAKE_CASE : List[str] = tokenizer.special_tokens_map['''cls_token'''] # `[CLS]` __SCREAMING_SNAKE_CASE : List[str] = tokenizer.special_tokens_map['''sep_token'''] # `[SEP]` elif args.tokenizer_type == "roberta": __SCREAMING_SNAKE_CASE : List[str] = RobertaTokenizer.from_pretrained(args.tokenizer_name ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.special_tokens_map['''cls_token'''] # `<s>` __SCREAMING_SNAKE_CASE : Optional[int] = tokenizer.special_tokens_map['''sep_token'''] # `</s>` elif args.tokenizer_type == "gpt2": __SCREAMING_SNAKE_CASE : Dict = GPTaTokenizer.from_pretrained(args.tokenizer_name ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.special_tokens_map['''bos_token'''] # `<|endoftext|>` __SCREAMING_SNAKE_CASE : str = tokenizer.special_tokens_map['''eos_token'''] # `<|endoftext|>` logger.info(F'''Loading text from {args.file_path}''' ) with open(args.file_path , '''r''' , encoding='''utf8''' ) as fp: __SCREAMING_SNAKE_CASE : str = fp.readlines() logger.info('''Start encoding''' ) logger.info(F'''{len(snake_case )} examples to process.''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = [] __SCREAMING_SNAKE_CASE : Dict = 0 __SCREAMING_SNAKE_CASE : List[str] = 10_000 __SCREAMING_SNAKE_CASE : Dict = time.time() for text in data: __SCREAMING_SNAKE_CASE : Optional[int] = F'''{bos} {text.strip()} {sep}''' __SCREAMING_SNAKE_CASE : Tuple = tokenizer.encode(snake_case , add_special_tokens=snake_case ) rslt.append(snake_case ) iter += 1 if iter % interval == 0: __SCREAMING_SNAKE_CASE : List[str] = time.time() logger.info(F'''{iter} examples processed. - {(end-start):.2f}s/{interval}expl''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = time.time() logger.info('''Finished binarization''' ) logger.info(F'''{len(snake_case )} examples processed.''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = F'''{args.dump_file}.{args.tokenizer_name}.pickle''' __SCREAMING_SNAKE_CASE : str = tokenizer.vocab_size if vocab_size < (1 << 16): __SCREAMING_SNAKE_CASE : List[str] = [np.uintaa(snake_case ) for d in rslt] else: __SCREAMING_SNAKE_CASE : Optional[int] = [np.intaa(snake_case ) for d in rslt] random.shuffle(rslt_ ) logger.info(F'''Dump to {dp_file}''' ) with open(snake_case , '''wb''' ) as handle: pickle.dump(rslt_ , snake_case , protocol=pickle.HIGHEST_PROTOCOL ) if __name__ == "__main__": main()
303
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowercase_ = { """configuration_ctrl""": ["""CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP""", """CTRLConfig"""], """tokenization_ctrl""": ["""CTRLTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """CTRL_PRETRAINED_MODEL_ARCHIVE_LIST""", """CTRLForSequenceClassification""", """CTRLLMHeadModel""", """CTRLModel""", """CTRLPreTrainedModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFCTRLForSequenceClassification""", """TFCTRLLMHeadModel""", """TFCTRLModel""", """TFCTRLPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig from .tokenization_ctrl import CTRLTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ctrl import ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, CTRLPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_ctrl import ( TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, TFCTRLForSequenceClassification, TFCTRLLMHeadModel, TFCTRLModel, TFCTRLPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
303
import numpy as np import torch from imwatermark import WatermarkEncoder # Copied from https://github.com/Stability-AI/generative-models/blob/613af104c6b85184091d42d374fef420eddb356d/scripts/demo/streamlit_helpers.py#L66 lowercase_ = 0b1011_0011_1110_1100_1001_0000_0111_1011_1011_0001_1001_1110 # bin(x)[2:] gives bits of x as str, use int to convert them to 0/1 lowercase_ = [int(bit) for bit in bin(WATERMARK_MESSAGE)[2:]] class __UpperCamelCase : """simple docstring""" def __init__( self : Dict ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = WATERMARK_BITS __SCREAMING_SNAKE_CASE : Optional[int] = WatermarkEncoder() self.encoder.set_watermark('''bits''' , self.watermark ) def UpperCAmelCase__ ( self : List[Any] , _A : torch.FloatTensor ): """simple docstring""" if images.shape[-1] < 256: return images __SCREAMING_SNAKE_CASE : Union[str, Any] = (255 * (images / 2 + 0.5)).cpu().permute(0 , 2 , 3 , 1 ).float().numpy() __SCREAMING_SNAKE_CASE : Dict = [self.encoder.encode(_A , '''dwtDct''' ) for image in images] __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.from_numpy(np.array(_A ) ).permute(0 , 3 , 1 , 2 ) __SCREAMING_SNAKE_CASE : Optional[Any] = torch.clamp(2 * (images / 255 - 0.5) , min=-1.0 , max=1.0 ) return images
303
1
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL lowercase_ = logging.get_logger(__name__) class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = ['''pixel_values'''] def __init__( self : int , _A : bool = True , _A : Dict[str, int] = None , _A : PILImageResampling = PILImageResampling.BICUBIC , _A : bool = True , _A : Union[int, float] = 1 / 255 , _A : bool = True , _A : Optional[Union[float, List[float]]] = None , _A : Optional[Union[float, List[float]]] = None , _A : bool = True , **_A : str , ): """simple docstring""" super().__init__(**_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = size if size is not None else {'''height''': 384, '''width''': 384} __SCREAMING_SNAKE_CASE : Union[str, Any] = get_size_dict(_A , default_to_square=_A ) __SCREAMING_SNAKE_CASE : Any = do_resize __SCREAMING_SNAKE_CASE : Union[str, Any] = size __SCREAMING_SNAKE_CASE : List[Any] = resample __SCREAMING_SNAKE_CASE : int = do_rescale __SCREAMING_SNAKE_CASE : Union[str, Any] = rescale_factor __SCREAMING_SNAKE_CASE : Union[str, Any] = do_normalize __SCREAMING_SNAKE_CASE : List[Any] = image_mean if image_mean is not None else OPENAI_CLIP_MEAN __SCREAMING_SNAKE_CASE : int = image_std if image_std is not None else OPENAI_CLIP_STD __SCREAMING_SNAKE_CASE : Any = do_convert_rgb def UpperCAmelCase__ ( self : Any , _A : np.ndarray , _A : Dict[str, int] , _A : PILImageResampling = PILImageResampling.BICUBIC , _A : Optional[Union[str, ChannelDimension]] = None , **_A : Any , ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = get_size_dict(_A , default_to_square=_A ) if "height" not in size or "width" not in size: raise ValueError(F'''The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}''' ) __SCREAMING_SNAKE_CASE : Dict = (size['''height'''], size['''width''']) return resize(_A , size=_A , resample=_A , data_format=_A , **_A ) def UpperCAmelCase__ ( self : Optional[int] , _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 UpperCAmelCase__ ( self : List[str] , _A : np.ndarray , _A : Union[float, List[float]] , _A : Union[float, List[float]] , _A : Optional[Union[str, ChannelDimension]] = None , **_A : List[Any] , ): """simple docstring""" return normalize(_A , mean=_A , std=_A , data_format=_A , **_A ) def UpperCAmelCase__ ( self : str , _A : ImageInput , _A : Optional[bool] = None , _A : Optional[Dict[str, int]] = None , _A : PILImageResampling = None , _A : Optional[bool] = None , _A : Optional[float] = None , _A : Optional[bool] = None , _A : Optional[Union[float, List[float]]] = None , _A : Optional[Union[float, List[float]]] = None , _A : Optional[Union[str, TensorType]] = None , _A : bool = None , _A : ChannelDimension = ChannelDimension.FIRST , **_A : str , ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = do_resize if do_resize is not None else self.do_resize __SCREAMING_SNAKE_CASE : Any = resample if resample is not None else self.resample __SCREAMING_SNAKE_CASE : Dict = do_rescale if do_rescale is not None else self.do_rescale __SCREAMING_SNAKE_CASE : Optional[Any] = rescale_factor if rescale_factor is not None else self.rescale_factor __SCREAMING_SNAKE_CASE : List[Any] = do_normalize if do_normalize is not None else self.do_normalize __SCREAMING_SNAKE_CASE : Union[str, Any] = image_mean if image_mean is not None else self.image_mean __SCREAMING_SNAKE_CASE : int = image_std if image_std is not None else self.image_std __SCREAMING_SNAKE_CASE : Tuple = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb __SCREAMING_SNAKE_CASE : Any = size if size is not None else self.size __SCREAMING_SNAKE_CASE : Dict = get_size_dict(_A , default_to_square=_A ) __SCREAMING_SNAKE_CASE : Tuple = 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 or resample is None: raise ValueError('''Size and resample must be specified if do_resize 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.''' ) # PIL RGBA images are converted to RGB if do_convert_rgb: __SCREAMING_SNAKE_CASE : str = [convert_to_rgb(_A ) for image in images] # All transformations expect numpy arrays. __SCREAMING_SNAKE_CASE : Optional[int] = [to_numpy_array(_A ) for image in images] if do_resize: __SCREAMING_SNAKE_CASE : List[Any] = [self.resize(image=_A , size=_A , resample=_A ) for image in images] if do_rescale: __SCREAMING_SNAKE_CASE : List[str] = [self.rescale(image=_A , scale=_A ) for image in images] if do_normalize: __SCREAMING_SNAKE_CASE : List[Any] = [self.normalize(image=_A , mean=_A , std=_A ) for image in images] __SCREAMING_SNAKE_CASE : Optional[int] = [to_channel_dimension_format(_A , _A ) for image in images] __SCREAMING_SNAKE_CASE : Any = BatchFeature(data={'''pixel_values''': images} , tensor_type=_A ) return encoded_outputs
303
from heapq import heappop, heappush import numpy as np def a__ ( snake_case , snake_case , snake_case , snake_case , ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : int = grid.shape __SCREAMING_SNAKE_CASE : Tuple = [-1, 1, 0, 0] __SCREAMING_SNAKE_CASE : List[str] = [0, 0, -1, 1] if allow_diagonal: dx += [-1, -1, 1, 1] dy += [-1, 1, -1, 1] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Union[str, Any] = [(0, source)], set() __SCREAMING_SNAKE_CASE : Union[str, Any] = np.full((rows, cols) , np.inf ) __SCREAMING_SNAKE_CASE : Union[str, Any] = 0 __SCREAMING_SNAKE_CASE : Union[str, Any] = np.empty((rows, cols) , dtype=snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = None while queue: ((__SCREAMING_SNAKE_CASE), (__SCREAMING_SNAKE_CASE)) : Any = heappop(snake_case ) if (x, y) in visited: continue visited.add((x, y) ) if (x, y) == destination: __SCREAMING_SNAKE_CASE : int = [] while (x, y) != source: path.append((x, y) ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[Any] = predecessors[x, y] path.append(snake_case ) # add the source manually path.reverse() return matrix[destination], path for i in range(len(snake_case ) ): __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = x + dx[i], y + dy[i] if 0 <= nx < rows and 0 <= ny < cols: __SCREAMING_SNAKE_CASE : Optional[int] = grid[nx][ny] if next_node == 1 and matrix[nx, ny] > dist + 1: heappush(snake_case , (dist + 1, (nx, ny)) ) __SCREAMING_SNAKE_CASE : int = dist + 1 __SCREAMING_SNAKE_CASE : Dict = (x, y) return np.inf, [] if __name__ == "__main__": import doctest doctest.testmod()
303
1
lowercase_ = """0.21.0""" from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
303
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = {"""configuration_ibert""": ["""IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """IBertConfig""", """IBertOnnxConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """IBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """IBertForMaskedLM""", """IBertForMultipleChoice""", """IBertForQuestionAnswering""", """IBertForSequenceClassification""", """IBertForTokenClassification""", """IBertModel""", """IBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_ibert import IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, IBertConfig, IBertOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ibert import ( IBERT_PRETRAINED_MODEL_ARCHIVE_LIST, IBertForMaskedLM, IBertForMultipleChoice, IBertForQuestionAnswering, IBertForSequenceClassification, IBertForTokenClassification, IBertModel, IBertPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
303
1
from typing import Any class __UpperCamelCase : """simple docstring""" def __init__( self : Optional[int] , _A : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = data __SCREAMING_SNAKE_CASE : List[Any] = None def __repr__( self : Any ): """simple docstring""" return F'''Node({self.data})''' class __UpperCamelCase : """simple docstring""" def __init__( self : Tuple ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = None def __iter__( self : int ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = self.head while node: yield node.data __SCREAMING_SNAKE_CASE : Tuple = node.next def __len__( self : Optional[int] ): """simple docstring""" return sum(1 for _ in self ) def __repr__( self : List[str] ): """simple docstring""" return "->".join([str(_A ) for item in self] ) def __getitem__( self : Optional[int] , _A : int ): """simple docstring""" if not 0 <= index < len(self ): raise ValueError('''list index out of range.''' ) for i, node in enumerate(self ): if i == index: return node return None def __setitem__( self : str , _A : int , _A : Any ): """simple docstring""" if not 0 <= index < len(self ): raise ValueError('''list index out of range.''' ) __SCREAMING_SNAKE_CASE : int = self.head for _ in range(_A ): __SCREAMING_SNAKE_CASE : Union[str, Any] = current.next __SCREAMING_SNAKE_CASE : Dict = data def UpperCAmelCase__ ( self : List[str] , _A : Any ): """simple docstring""" self.insert_nth(len(self ) , _A ) def UpperCAmelCase__ ( self : List[Any] , _A : Any ): """simple docstring""" self.insert_nth(0 , _A ) def UpperCAmelCase__ ( self : List[str] , _A : int , _A : Any ): """simple docstring""" if not 0 <= index <= len(self ): raise IndexError('''list index out of range''' ) __SCREAMING_SNAKE_CASE : Dict = Node(_A ) if self.head is None: __SCREAMING_SNAKE_CASE : Union[str, Any] = new_node elif index == 0: __SCREAMING_SNAKE_CASE : int = self.head # link new_node to head __SCREAMING_SNAKE_CASE : int = new_node else: __SCREAMING_SNAKE_CASE : Any = self.head for _ in range(index - 1 ): __SCREAMING_SNAKE_CASE : List[str] = temp.next __SCREAMING_SNAKE_CASE : Optional[int] = temp.next __SCREAMING_SNAKE_CASE : Any = new_node def UpperCAmelCase__ ( self : Tuple ): # print every node data """simple docstring""" print(self ) def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" return self.delete_nth(0 ) def UpperCAmelCase__ ( self : str ): # delete from tail """simple docstring""" return self.delete_nth(len(self ) - 1 ) def UpperCAmelCase__ ( self : Any , _A : int = 0 ): """simple docstring""" if not 0 <= index <= len(self ) - 1: # test if index is valid raise IndexError('''List index out of range.''' ) __SCREAMING_SNAKE_CASE : str = self.head # default first node if index == 0: __SCREAMING_SNAKE_CASE : int = self.head.next else: __SCREAMING_SNAKE_CASE : List[str] = self.head for _ in range(index - 1 ): __SCREAMING_SNAKE_CASE : List[str] = temp.next __SCREAMING_SNAKE_CASE : Any = temp.next __SCREAMING_SNAKE_CASE : Union[str, Any] = temp.next.next return delete_node.data def UpperCAmelCase__ ( self : Tuple ): """simple docstring""" return self.head is None def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = None __SCREAMING_SNAKE_CASE : int = self.head while current: # Store the current node's next node. __SCREAMING_SNAKE_CASE : Dict = current.next # Make the current node's next point backwards __SCREAMING_SNAKE_CASE : Union[str, Any] = prev # Make the previous node be the current node __SCREAMING_SNAKE_CASE : List[str] = current # Make the current node the next node (to progress iteration) __SCREAMING_SNAKE_CASE : List[str] = next_node # Return prev in order to put the head at the end __SCREAMING_SNAKE_CASE : Union[str, Any] = prev def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = LinkedList() assert linked_list.is_empty() is True assert str(snake_case ) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(10 ): assert len(snake_case ) == i linked_list.insert_nth(snake_case , i + 1 ) assert str(snake_case ) == "->".join(str(snake_case ) for i in range(1 , 11 ) ) linked_list.insert_head(0 ) linked_list.insert_tail(11 ) assert str(snake_case ) == "->".join(str(snake_case ) for i in range(0 , 12 ) ) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9 ) == 10 assert linked_list.delete_tail() == 11 assert len(snake_case ) == 9 assert str(snake_case ) == "->".join(str(snake_case ) for i in range(1 , 10 ) ) assert all(linked_list[i] == i + 1 for i in range(0 , 9 ) ) is True for i in range(0 , 9 ): __SCREAMING_SNAKE_CASE : List[str] = -i assert all(linked_list[i] == -i for i in range(0 , 9 ) ) is True linked_list.reverse() assert str(snake_case ) == "->".join(str(snake_case ) for i in range(-8 , 1 ) ) def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = [ -9, 100, Node(77_345_112 ), '''dlrow olleH''', 7, 5_555, 0, -192.5_5555, '''Hello, world!''', 77.9, Node(10 ), None, None, 12.20, ] __SCREAMING_SNAKE_CASE : Any = LinkedList() for i in test_input: linked_list.insert_tail(snake_case ) # Check if it's empty or not assert linked_list.is_empty() is False assert ( str(snake_case ) == "-9->100->Node(77345112)->dlrow olleH->7->5555->0->" "-192.55555->Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the head __SCREAMING_SNAKE_CASE : int = linked_list.delete_head() assert result == -9 assert ( str(snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the tail __SCREAMING_SNAKE_CASE : Optional[int] = linked_list.delete_tail() assert result == 12.2 assert ( str(snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None" ) # Delete a node in specific location in linked list __SCREAMING_SNAKE_CASE : Dict = linked_list.delete_nth(10 ) assert result is None assert ( str(snake_case ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None" ) # Add a Node instance to its head linked_list.insert_head(Node('''Hello again, world!''' ) ) assert ( str(snake_case ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None" ) # Add None to its tail linked_list.insert_tail(snake_case ) assert ( str(snake_case ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None->None" ) # Reverse the linked list linked_list.reverse() assert ( str(snake_case ) == "None->None->Node(10)->77.9->Hello, world!->-192.55555->0->5555->" "7->dlrow olleH->Node(77345112)->100->Node(Hello again, world!)" ) def a__ ( ): """simple docstring""" from doctest import testmod testmod() __SCREAMING_SNAKE_CASE : str = LinkedList() linked_list.insert_head(input('''Inserting 1st at head ''' ).strip() ) linked_list.insert_head(input('''Inserting 2nd at head ''' ).strip() ) print('''\nPrint list:''' ) linked_list.print_list() linked_list.insert_tail(input('''\nInserting 1st at tail ''' ).strip() ) linked_list.insert_tail(input('''Inserting 2nd at tail ''' ).strip() ) print('''\nPrint list:''' ) linked_list.print_list() print('''\nDelete head''' ) linked_list.delete_head() print('''Delete tail''' ) linked_list.delete_tail() print('''\nPrint list:''' ) linked_list.print_list() print('''\nReverse linked list''' ) linked_list.reverse() print('''\nPrint list:''' ) linked_list.print_list() print('''\nString representation of linked list:''' ) print(snake_case ) print('''\nReading/changing Node data using indexing:''' ) print(F'''Element at Position 1: {linked_list[1]}''' ) __SCREAMING_SNAKE_CASE : Optional[int] = input('''Enter New Value: ''' ).strip() print('''New list:''' ) print(snake_case ) print(F'''length of linked_list is : {len(snake_case )}''' ) if __name__ == "__main__": main()
303
from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
303
1
from ....utils import logging lowercase_ = logging.get_logger(__name__) class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" def __init__( self : List[str] , _A : int , _A : List[str]=None , _A : List[Any]=2048 ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = config.__dict__ __SCREAMING_SNAKE_CASE : Union[str, Any] = modal_hidden_size if num_labels: __SCREAMING_SNAKE_CASE : Tuple = num_labels
303
import sys from collections import defaultdict class __UpperCamelCase : """simple docstring""" def __init__( self : Union[str, Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = [] def UpperCAmelCase__ ( self : List[str] , _A : str ): """simple docstring""" return self.node_position[vertex] def UpperCAmelCase__ ( self : Dict , _A : List[str] , _A : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = pos def UpperCAmelCase__ ( self : List[Any] , _A : Union[str, Any] , _A : List[Any] , _A : List[str] , _A : Union[str, Any] ): """simple docstring""" if start > size // 2 - 1: return else: if 2 * start + 2 >= size: __SCREAMING_SNAKE_CASE : Union[str, Any] = 2 * start + 1 else: if heap[2 * start + 1] < heap[2 * start + 2]: __SCREAMING_SNAKE_CASE : List[Any] = 2 * start + 1 else: __SCREAMING_SNAKE_CASE : Dict = 2 * start + 2 if heap[smallest_child] < heap[start]: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Union[str, Any] = heap[smallest_child], positions[smallest_child] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : List[str] = ( heap[start], positions[start], ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = temp, tempa __SCREAMING_SNAKE_CASE : Any = self.get_position(positions[smallest_child] ) self.set_position( positions[smallest_child] , self.get_position(positions[start] ) ) self.set_position(positions[start] , _A ) self.top_to_bottom(_A , _A , _A , _A ) def UpperCAmelCase__ ( self : Any , _A : Union[str, Any] , _A : Dict , _A : Optional[Any] , _A : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = position[index] while index != 0: __SCREAMING_SNAKE_CASE : Optional[Any] = int((index - 2) / 2 ) if index % 2 == 0 else int((index - 1) / 2 ) if val < heap[parent]: __SCREAMING_SNAKE_CASE : Optional[Any] = heap[parent] __SCREAMING_SNAKE_CASE : str = position[parent] self.set_position(position[parent] , _A ) else: __SCREAMING_SNAKE_CASE : List[str] = val __SCREAMING_SNAKE_CASE : List[str] = temp self.set_position(_A , _A ) break __SCREAMING_SNAKE_CASE : List[Any] = parent else: __SCREAMING_SNAKE_CASE : Tuple = val __SCREAMING_SNAKE_CASE : List[str] = temp self.set_position(_A , 0 ) def UpperCAmelCase__ ( self : List[str] , _A : Tuple , _A : Tuple ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = len(_A ) // 2 - 1 for i in range(_A , -1 , -1 ): self.top_to_bottom(_A , _A , len(_A ) , _A ) def UpperCAmelCase__ ( self : List[str] , _A : Dict , _A : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = positions[0] __SCREAMING_SNAKE_CASE : Tuple = sys.maxsize self.top_to_bottom(_A , 0 , len(_A ) , _A ) return temp def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = Heap() __SCREAMING_SNAKE_CASE : int = [0] * len(snake_case ) __SCREAMING_SNAKE_CASE : Dict = [-1] * len(snake_case ) # Neighboring Tree Vertex of selected vertex # Minimum Distance of explored vertex with neighboring vertex of partial tree # formed in graph __SCREAMING_SNAKE_CASE : Dict = [] # Heap of Distance of vertices from their neighboring vertex __SCREAMING_SNAKE_CASE : Optional[int] = [] for vertex in range(len(snake_case ) ): distance_tv.append(sys.maxsize ) positions.append(snake_case ) heap.node_position.append(snake_case ) __SCREAMING_SNAKE_CASE : Union[str, Any] = [] __SCREAMING_SNAKE_CASE : str = 1 __SCREAMING_SNAKE_CASE : int = sys.maxsize for neighbor, distance in adjacency_list[0]: __SCREAMING_SNAKE_CASE : Optional[Any] = 0 __SCREAMING_SNAKE_CASE : Dict = distance heap.heapify(snake_case , snake_case ) for _ in range(1 , len(snake_case ) ): __SCREAMING_SNAKE_CASE : Tuple = heap.delete_minimum(snake_case , snake_case ) if visited[vertex] == 0: tree_edges.append((nbr_tv[vertex], vertex) ) __SCREAMING_SNAKE_CASE : List[Any] = 1 for neighbor, distance in adjacency_list[vertex]: if ( visited[neighbor] == 0 and distance < distance_tv[heap.get_position(snake_case )] ): __SCREAMING_SNAKE_CASE : int = distance heap.bottom_to_top( snake_case , heap.get_position(snake_case ) , snake_case , snake_case ) __SCREAMING_SNAKE_CASE : Any = vertex return tree_edges if __name__ == "__main__": # pragma: no cover # < --------- Prims Algorithm --------- > lowercase_ = int(input("""Enter number of edges: """).strip()) lowercase_ = defaultdict(list) for _ in range(edges_number): lowercase_ = [int(x) for x in input().strip().split()] adjacency_list[edge[0]].append([edge[1], edge[2]]) adjacency_list[edge[1]].append([edge[0], edge[2]]) print(prisms_algorithm(adjacency_list))
303
1
from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( '''The RoBERTa Model transformer with early exiting (DeeRoBERTa). ''' , lowerCAmelCase__ , ) class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = RobertaConfig lowerCAmelCase_ = '''roberta''' def __init__( self : Any , _A : List[str] ): """simple docstring""" super().__init__(_A ) __SCREAMING_SNAKE_CASE : List[str] = RobertaEmbeddings(_A ) self.init_weights() @add_start_docstrings( '''RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top, also takes care of multi-layer training. ''' , lowerCAmelCase__ , ) class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = RobertaConfig lowerCAmelCase_ = '''roberta''' def __init__( self : Any , _A : str ): """simple docstring""" super().__init__(_A ) __SCREAMING_SNAKE_CASE : Any = config.num_labels __SCREAMING_SNAKE_CASE : Dict = config.num_hidden_layers __SCREAMING_SNAKE_CASE : Optional[Any] = DeeRobertaModel(_A ) __SCREAMING_SNAKE_CASE : List[Any] = nn.Dropout(config.hidden_dropout_prob ) __SCREAMING_SNAKE_CASE : Optional[int] = nn.Linear(config.hidden_size , self.config.num_labels ) @add_start_docstrings_to_model_forward(_A ) def UpperCAmelCase__ ( self : int , _A : Dict=None , _A : List[Any]=None , _A : Optional[int]=None , _A : Tuple=None , _A : Any=None , _A : Tuple=None , _A : Dict=None , _A : Any=-1 , _A : List[str]=False , ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = self.num_layers try: __SCREAMING_SNAKE_CASE : Union[str, Any] = self.roberta( _A , attention_mask=_A , token_type_ids=_A , position_ids=_A , head_mask=_A , inputs_embeds=_A , ) __SCREAMING_SNAKE_CASE : Any = outputs[1] __SCREAMING_SNAKE_CASE : Tuple = self.dropout(_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = self.classifier(_A ) __SCREAMING_SNAKE_CASE : List[str] = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: __SCREAMING_SNAKE_CASE : str = e.message __SCREAMING_SNAKE_CASE : Tuple = e.exit_layer __SCREAMING_SNAKE_CASE : str = outputs[0] if not self.training: __SCREAMING_SNAKE_CASE : int = entropy(_A ) __SCREAMING_SNAKE_CASE : str = [] __SCREAMING_SNAKE_CASE : int = [] if labels is not None: if self.num_labels == 1: # We are doing regression __SCREAMING_SNAKE_CASE : str = MSELoss() __SCREAMING_SNAKE_CASE : Any = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: __SCREAMING_SNAKE_CASE : Optional[Any] = CrossEntropyLoss() __SCREAMING_SNAKE_CASE : Optional[Any] = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits __SCREAMING_SNAKE_CASE : Any = [] for highway_exit in outputs[-1]: __SCREAMING_SNAKE_CASE : Optional[Any] = highway_exit[0] if not self.training: highway_logits_all.append(_A ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression __SCREAMING_SNAKE_CASE : List[str] = MSELoss() __SCREAMING_SNAKE_CASE : Union[str, Any] = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: __SCREAMING_SNAKE_CASE : Any = CrossEntropyLoss() __SCREAMING_SNAKE_CASE : List[Any] = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(_A ) if train_highway: __SCREAMING_SNAKE_CASE : List[str] = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: __SCREAMING_SNAKE_CASE : Union[str, Any] = (loss,) + outputs if not self.training: __SCREAMING_SNAKE_CASE : Tuple = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: __SCREAMING_SNAKE_CASE : Optional[int] = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
303
from __future__ import annotations import matplotlib.pyplot as plt # type: ignore import numpy # initial triangle of Koch snowflake lowercase_ = numpy.array([0, 0]) lowercase_ = numpy.array([0.5, 0.866_0254]) lowercase_ = numpy.array([1, 0]) lowercase_ = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1] def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = initial_vectors for _ in range(snake_case ): __SCREAMING_SNAKE_CASE : Dict = iteration_step(snake_case ) return vectors def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = [] for i, start_vector in enumerate(vectors[:-1] ): __SCREAMING_SNAKE_CASE : str = vectors[i + 1] new_vectors.append(snake_case ) __SCREAMING_SNAKE_CASE : List[str] = end_vector - start_vector new_vectors.append(start_vector + difference_vector / 3 ) new_vectors.append( start_vector + difference_vector / 3 + rotate(difference_vector / 3 , 60 ) ) new_vectors.append(start_vector + difference_vector * 2 / 3 ) new_vectors.append(vectors[-1] ) return new_vectors def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = numpy.radians(snake_case ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = numpy.cos(snake_case ), numpy.sin(snake_case ) __SCREAMING_SNAKE_CASE : Any = numpy.array(((c, -s), (s, c)) ) return numpy.dot(snake_case , snake_case ) def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = plt.gca() axes.set_aspect('''equal''' ) # matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all # y-coordinates as inputs, which are constructed from the vector-list using # zip() __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = zip(*snake_case ) plt.plot(snake_case , snake_case ) plt.show() if __name__ == "__main__": import doctest doctest.testmod() lowercase_ = iterate(INITIAL_VECTORS, 5) plot(processed_vectors)
303
1
import argparse import pathlib import fairseq import torch from fairseq.models.roberta import RobertaModel as FairseqRobertaModel from fairseq.modules import TransformerSentenceEncoderLayer from packaging import version from transformers import XLMRobertaConfig, XLMRobertaXLForMaskedLM, XLMRobertaXLForSequenceClassification from transformers.models.bert.modeling_bert import ( BertIntermediate, BertLayer, BertOutput, BertSelfAttention, BertSelfOutput, ) from transformers.models.roberta.modeling_roberta import RobertaAttention from transformers.utils import logging if version.parse(fairseq.__version__) < version.parse("""1.0.0a"""): raise Exception("""requires fairseq >= 1.0.0a""") logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) lowercase_ = """Hello world! cécé herlolip""" def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = FairseqRobertaModel.from_pretrained(snake_case ) roberta.eval() # disable dropout __SCREAMING_SNAKE_CASE : Optional[Any] = roberta.model.encoder.sentence_encoder __SCREAMING_SNAKE_CASE : List[str] = XLMRobertaConfig( vocab_size=roberta_sent_encoder.embed_tokens.num_embeddings , hidden_size=roberta.cfg.model.encoder_embed_dim , num_hidden_layers=roberta.cfg.model.encoder_layers , num_attention_heads=roberta.cfg.model.encoder_attention_heads , intermediate_size=roberta.cfg.model.encoder_ffn_embed_dim , max_position_embeddings=514 , type_vocab_size=1 , layer_norm_eps=1E-5 , ) if classification_head: __SCREAMING_SNAKE_CASE : Union[str, Any] = roberta.model.classification_heads['''mnli'''].out_proj.weight.shape[0] print('''Our RoBERTa config:''' , snake_case ) __SCREAMING_SNAKE_CASE : int = XLMRobertaXLForSequenceClassification(snake_case ) if classification_head else XLMRobertaXLForMaskedLM(snake_case ) model.eval() # Now let's copy all the weights. # Embeddings __SCREAMING_SNAKE_CASE : Tuple = roberta_sent_encoder.embed_tokens.weight __SCREAMING_SNAKE_CASE : Dict = roberta_sent_encoder.embed_positions.weight __SCREAMING_SNAKE_CASE : List[Any] = torch.zeros_like( model.roberta.embeddings.token_type_embeddings.weight ) # just zero them out b/c RoBERTa doesn't use them. __SCREAMING_SNAKE_CASE : str = roberta_sent_encoder.layer_norm.weight __SCREAMING_SNAKE_CASE : int = roberta_sent_encoder.layer_norm.bias for i in range(config.num_hidden_layers ): # Encoder: start of layer __SCREAMING_SNAKE_CASE : BertLayer = model.roberta.encoder.layer[i] __SCREAMING_SNAKE_CASE : TransformerSentenceEncoderLayer = roberta_sent_encoder.layers[i] __SCREAMING_SNAKE_CASE : RobertaAttention = layer.attention __SCREAMING_SNAKE_CASE : Optional[int] = roberta_layer.self_attn_layer_norm.weight __SCREAMING_SNAKE_CASE : Optional[Any] = roberta_layer.self_attn_layer_norm.bias # self attention __SCREAMING_SNAKE_CASE : BertSelfAttention = layer.attention.self assert ( roberta_layer.self_attn.k_proj.weight.data.shape == roberta_layer.self_attn.q_proj.weight.data.shape == roberta_layer.self_attn.v_proj.weight.data.shape == torch.Size((config.hidden_size, config.hidden_size) ) ) __SCREAMING_SNAKE_CASE : Dict = roberta_layer.self_attn.q_proj.weight __SCREAMING_SNAKE_CASE : Any = roberta_layer.self_attn.q_proj.bias __SCREAMING_SNAKE_CASE : Optional[Any] = roberta_layer.self_attn.k_proj.weight __SCREAMING_SNAKE_CASE : int = roberta_layer.self_attn.k_proj.bias __SCREAMING_SNAKE_CASE : Optional[int] = roberta_layer.self_attn.v_proj.weight __SCREAMING_SNAKE_CASE : str = roberta_layer.self_attn.v_proj.bias # self-attention output __SCREAMING_SNAKE_CASE : BertSelfOutput = layer.attention.output assert self_output.dense.weight.shape == roberta_layer.self_attn.out_proj.weight.shape __SCREAMING_SNAKE_CASE : str = roberta_layer.self_attn.out_proj.weight __SCREAMING_SNAKE_CASE : Optional[int] = roberta_layer.self_attn.out_proj.bias # this one is final layer norm __SCREAMING_SNAKE_CASE : Optional[int] = roberta_layer.final_layer_norm.weight __SCREAMING_SNAKE_CASE : List[str] = roberta_layer.final_layer_norm.bias # intermediate __SCREAMING_SNAKE_CASE : BertIntermediate = layer.intermediate assert intermediate.dense.weight.shape == roberta_layer.fca.weight.shape __SCREAMING_SNAKE_CASE : Tuple = roberta_layer.fca.weight __SCREAMING_SNAKE_CASE : Dict = roberta_layer.fca.bias # output __SCREAMING_SNAKE_CASE : BertOutput = layer.output assert bert_output.dense.weight.shape == roberta_layer.fca.weight.shape __SCREAMING_SNAKE_CASE : Optional[Any] = roberta_layer.fca.weight __SCREAMING_SNAKE_CASE : Dict = roberta_layer.fca.bias # end of layer if classification_head: __SCREAMING_SNAKE_CASE : List[Any] = roberta.model.classification_heads['''mnli'''].dense.weight __SCREAMING_SNAKE_CASE : str = roberta.model.classification_heads['''mnli'''].dense.bias __SCREAMING_SNAKE_CASE : List[Any] = roberta.model.classification_heads['''mnli'''].out_proj.weight __SCREAMING_SNAKE_CASE : Dict = roberta.model.classification_heads['''mnli'''].out_proj.bias else: # LM Head __SCREAMING_SNAKE_CASE : Optional[Any] = roberta.model.encoder.lm_head.dense.weight __SCREAMING_SNAKE_CASE : str = roberta.model.encoder.lm_head.dense.bias __SCREAMING_SNAKE_CASE : str = roberta.model.encoder.lm_head.layer_norm.weight __SCREAMING_SNAKE_CASE : int = roberta.model.encoder.lm_head.layer_norm.bias __SCREAMING_SNAKE_CASE : str = roberta.model.encoder.lm_head.weight __SCREAMING_SNAKE_CASE : Dict = roberta.model.encoder.lm_head.bias # Let's check that we get the same results. __SCREAMING_SNAKE_CASE : torch.Tensor = roberta.encode(snake_case ).unsqueeze(0 ) # batch of size 1 __SCREAMING_SNAKE_CASE : Optional[Any] = model(snake_case )[0] if classification_head: __SCREAMING_SNAKE_CASE : Dict = roberta.model.classification_heads['''mnli'''](roberta.extract_features(snake_case ) ) else: __SCREAMING_SNAKE_CASE : Dict = roberta.model(snake_case )[0] print(our_output.shape , their_output.shape ) __SCREAMING_SNAKE_CASE : Dict = torch.max(torch.abs(our_output - their_output ) ).item() print(F'''max_absolute_diff = {max_absolute_diff}''' ) # ~ 1e-7 __SCREAMING_SNAKE_CASE : Any = torch.allclose(snake_case , snake_case , atol=1E-3 ) print('''Do both models output the same tensors?''' , '''🔥''' if success else '''💩''' ) if not success: raise Exception('''Something went wRoNg''' ) pathlib.Path(snake_case ).mkdir(parents=snake_case , exist_ok=snake_case ) print(F'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(snake_case ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--roberta_checkpoint_path""", default=None, type=str, required=True, help="""Path the official PyTorch dump.""" ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) parser.add_argument( """--classification_head""", action="""store_true""", help="""Whether to convert a final classification head.""" ) lowercase_ = parser.parse_args() convert_xlm_roberta_xl_checkpoint_to_pytorch( args.roberta_checkpoint_path, args.pytorch_dump_folder_path, args.classification_head )
303
from typing import List, Optional, Union import torch from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) lowercase_ = logging.get_logger(__name__) # pylint: disable=invalid-name lowercase_ = """ Examples: ```py >>> import torch >>> import numpy as np >>> from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline >>> from transformers import pipeline >>> from diffusers.utils import load_image >>> def make_hint(image, depth_estimator): ... image = depth_estimator(image)[\"depth\"] ... image = np.array(image) ... image = image[:, :, None] ... image = np.concatenate([image, image, image], axis=2) ... detected_map = torch.from_numpy(image).float() / 255.0 ... hint = detected_map.permute(2, 0, 1) ... return hint >>> depth_estimator = pipeline(\"depth-estimation\") >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained( ... \"kandinsky-community/kandinsky-2-2-prior\", torch_dtype=torch.float16 ... ) >>> pipe_prior = pipe_prior.to(\"cuda\") >>> pipe = KandinskyV22ControlnetPipeline.from_pretrained( ... \"kandinsky-community/kandinsky-2-2-controlnet-depth\", torch_dtype=torch.float16 ... ) >>> pipe = pipe.to(\"cuda\") >>> img = load_image( ... \"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main\" ... \"/kandinsky/cat.png\" ... ).resize((768, 768)) >>> hint = make_hint(img, depth_estimator).unsqueeze(0).half().to(\"cuda\") >>> prompt = \"A robot, 4k photo\" >>> negative_prior_prompt = \"lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature\" >>> generator = torch.Generator(device=\"cuda\").manual_seed(43) >>> image_emb, zero_image_emb = pipe_prior( ... prompt=prompt, negative_prompt=negative_prior_prompt, generator=generator ... ).to_tuple() >>> images = pipe( ... image_embeds=image_emb, ... negative_image_embeds=zero_image_emb, ... hint=hint, ... num_inference_steps=50, ... generator=generator, ... height=768, ... width=768, ... ).images >>> images[0].save(\"robot_cat.png\") ``` """ def a__ ( snake_case , snake_case , snake_case=8 ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 __SCREAMING_SNAKE_CASE : Optional[int] = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" def __init__( self : int , _A : UNetaDConditionModel , _A : DDPMScheduler , _A : VQModel , ): """simple docstring""" super().__init__() self.register_modules( unet=_A , scheduler=_A , movq=_A , ) __SCREAMING_SNAKE_CASE : Optional[Any] = 2 ** (len(self.movq.config.block_out_channels ) - 1) def UpperCAmelCase__ ( self : Union[str, Any] , _A : Dict , _A : Optional[Any] , _A : Tuple , _A : List[Any] , _A : Optional[Any] , _A : List[Any] ): """simple docstring""" if latents is None: __SCREAMING_SNAKE_CASE : Optional[Any] = randn_tensor(_A , generator=_A , device=_A , dtype=_A ) else: if latents.shape != shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {shape}''' ) __SCREAMING_SNAKE_CASE : Tuple = latents.to(_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = latents * scheduler.init_noise_sigma return latents def UpperCAmelCase__ ( self : Tuple , _A : List[str]=0 ): """simple docstring""" if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) __SCREAMING_SNAKE_CASE : List[Any] = torch.device(F'''cuda:{gpu_id}''' ) __SCREAMING_SNAKE_CASE : Dict = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_A , _A ) def UpperCAmelCase__ ( self : int , _A : Tuple=0 ): """simple docstring""" if is_accelerate_available() and is_accelerate_version('''>=''' , '''0.17.0.dev0''' ): from accelerate import cpu_offload_with_hook else: raise ImportError('''`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.''' ) __SCREAMING_SNAKE_CASE : str = torch.device(F'''cuda:{gpu_id}''' ) if self.device.type != "cpu": self.to('''cpu''' , silence_dtype_warnings=_A ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) __SCREAMING_SNAKE_CASE : Optional[Any] = None for cpu_offloaded_model in [self.unet, self.movq]: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Dict = cpu_offload_with_hook(_A , _A , prev_module_hook=_A ) # We'll offload the last model manually. __SCREAMING_SNAKE_CASE : Optional[int] = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" if not hasattr(self.unet , '''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(_A , '''_hf_hook''' ) and hasattr(module._hf_hook , '''execution_device''' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(_A ) def __call__( self : Dict , _A : Union[torch.FloatTensor, List[torch.FloatTensor]] , _A : Union[torch.FloatTensor, List[torch.FloatTensor]] , _A : torch.FloatTensor , _A : int = 512 , _A : int = 512 , _A : int = 100 , _A : float = 4.0 , _A : int = 1 , _A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _A : Optional[torch.FloatTensor] = None , _A : Optional[str] = "pil" , _A : bool = True , ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = self._execution_device __SCREAMING_SNAKE_CASE : Optional[Any] = guidance_scale > 1.0 if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : Optional[Any] = torch.cat(_A , dim=0 ) if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : List[Any] = torch.cat(_A , dim=0 ) if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : List[str] = torch.cat(_A , dim=0 ) __SCREAMING_SNAKE_CASE : List[Any] = image_embeds.shape[0] * num_images_per_prompt if do_classifier_free_guidance: __SCREAMING_SNAKE_CASE : Dict = image_embeds.repeat_interleave(_A , dim=0 ) __SCREAMING_SNAKE_CASE : Any = negative_image_embeds.repeat_interleave(_A , dim=0 ) __SCREAMING_SNAKE_CASE : Union[str, Any] = hint.repeat_interleave(_A , dim=0 ) __SCREAMING_SNAKE_CASE : int = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=_A ) __SCREAMING_SNAKE_CASE : Optional[int] = torch.cat([hint, hint] , dim=0 ).to(dtype=self.unet.dtype , device=_A ) self.scheduler.set_timesteps(_A , device=_A ) __SCREAMING_SNAKE_CASE : Optional[int] = self.scheduler.timesteps __SCREAMING_SNAKE_CASE : Tuple = self.movq.config.latent_channels __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Dict = downscale_height_and_width(_A , _A , self.movq_scale_factor ) # create initial latent __SCREAMING_SNAKE_CASE : Tuple = self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , _A , _A , _A , self.scheduler , ) for i, t in enumerate(self.progress_bar(_A ) ): # expand the latents if we are doing classifier free guidance __SCREAMING_SNAKE_CASE : str = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents __SCREAMING_SNAKE_CASE : Dict = {'''image_embeds''': image_embeds, '''hint''': hint} __SCREAMING_SNAKE_CASE : Union[str, Any] = self.unet( sample=_A , timestep=_A , encoder_hidden_states=_A , added_cond_kwargs=_A , return_dict=_A , )[0] if do_classifier_free_guidance: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = noise_pred.split(latents.shape[1] , dim=1 ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Dict = noise_pred.chunk(2 ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[Any] = variance_pred.chunk(2 ) __SCREAMING_SNAKE_CASE : List[Any] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) __SCREAMING_SNAKE_CASE : Tuple = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , '''variance_type''' ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : List[str] = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 __SCREAMING_SNAKE_CASE : Any = self.scheduler.step( _A , _A , _A , generator=_A , )[0] # post-processing __SCREAMING_SNAKE_CASE : Any = self.movq.decode(_A , force_not_quantize=_A )['''sample'''] if output_type not in ["pt", "np", "pil"]: raise ValueError(F'''Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}''' ) if output_type in ["np", "pil"]: __SCREAMING_SNAKE_CASE : str = image * 0.5 + 0.5 __SCREAMING_SNAKE_CASE : Tuple = image.clamp(0 , 1 ) __SCREAMING_SNAKE_CASE : Dict = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": __SCREAMING_SNAKE_CASE : List[str] = self.numpy_to_pil(_A ) if not return_dict: return (image,) return ImagePipelineOutput(images=_A )
303
1
import json import os import tempfile import datasets from utils import generate_example_dataset, get_duration lowercase_ = 50_000 lowercase_ = 5_000 lowercase_ , lowercase_ = os.path.split(__file__) lowercase_ = os.path.join(RESULTS_BASEPATH, """results""", RESULTS_FILENAME.replace(""".py""", """.json""")) @get_duration def a__ ( snake_case , snake_case ): """simple docstring""" for i in range(snake_case ): __SCREAMING_SNAKE_CASE : int = dataset[i] @get_duration def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" for i in range(0 , len(snake_case ) , snake_case ): __SCREAMING_SNAKE_CASE : List[str] = dataset[i : i + batch_size] @get_duration def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" with dataset.formatted_as(type=snake_case ): for i in range(snake_case ): __SCREAMING_SNAKE_CASE : List[str] = dataset[i] @get_duration def a__ ( snake_case , snake_case , snake_case , snake_case ): """simple docstring""" with dataset.formatted_as(type=snake_case ): for i in range(0 , snake_case , snake_case ): __SCREAMING_SNAKE_CASE : int = dataset[i : i + batch_size] def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = {'''num examples''': SPEED_TEST_N_EXAMPLES} __SCREAMING_SNAKE_CASE : Optional[int] = [ (read, {'''length''': SMALL_TEST}), (read, {'''length''': SPEED_TEST_N_EXAMPLES}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 10}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 100}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 1_000}), (read_formatted, {'''type''': '''numpy''', '''length''': SMALL_TEST}), (read_formatted, {'''type''': '''pandas''', '''length''': SMALL_TEST}), (read_formatted, {'''type''': '''torch''', '''length''': SMALL_TEST}), (read_formatted, {'''type''': '''tensorflow''', '''length''': SMALL_TEST}), (read_formatted_batch, {'''type''': '''numpy''', '''length''': SMALL_TEST, '''batch_size''': 10}), (read_formatted_batch, {'''type''': '''numpy''', '''length''': SMALL_TEST, '''batch_size''': 1_000}), ] __SCREAMING_SNAKE_CASE : Union[str, Any] = [ (read, {'''length''': SMALL_TEST}), (read, {'''length''': SPEED_TEST_N_EXAMPLES}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 10}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 100}), (read_batch, {'''length''': SPEED_TEST_N_EXAMPLES, '''batch_size''': 1_000}), (read_formatted, {'''type''': '''numpy''', '''length''': SMALL_TEST}), (read_formatted_batch, {'''type''': '''numpy''', '''length''': SMALL_TEST, '''batch_size''': 10}), (read_formatted_batch, {'''type''': '''numpy''', '''length''': SMALL_TEST, '''batch_size''': 1_000}), ] with tempfile.TemporaryDirectory() as tmp_dir: print('''generating dataset''' ) __SCREAMING_SNAKE_CASE : List[str] = datasets.Features( {'''list''': datasets.Sequence(datasets.Value('''float32''' ) ), '''numbers''': datasets.Value('''float32''' )} ) __SCREAMING_SNAKE_CASE : str = generate_example_dataset( os.path.join(snake_case , '''dataset.arrow''' ) , snake_case , num_examples=snake_case , seq_shapes={'''list''': (100,)} , ) print('''first set of iterations''' ) for func, kwargs in functions: print(func.__name__ , str(snake_case ) ) __SCREAMING_SNAKE_CASE : Tuple = func(snake_case , **snake_case ) print('''shuffling dataset''' ) __SCREAMING_SNAKE_CASE : Union[str, Any] = dataset.shuffle() print('''Second set of iterations (after shuffling''' ) for func, kwargs in functions_shuffled: print('''shuffled ''' , func.__name__ , str(snake_case ) ) __SCREAMING_SNAKE_CASE : Optional[int] = func( snake_case , **snake_case ) with open(snake_case , '''wb''' ) as f: f.write(json.dumps(snake_case ).encode('''utf-8''' ) ) if __name__ == "__main__": # useful to run the profiler benchmark_iterating()
303
import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem lowercase_ = importlib.util.find_spec("""s3fs""") is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 lowercase_ = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(f'''A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.''') fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def a__ ( snake_case ): """simple docstring""" if "://" in dataset_path: __SCREAMING_SNAKE_CASE : Any = dataset_path.split('''://''' )[1] return dataset_path def a__ ( snake_case ): """simple docstring""" if fs is not None and fs.protocol != "file": return True else: return False def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = not is_remote_filesystem(snake_case ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(snake_case ) , fs._strip_protocol(snake_case ) ) else: fs.mv(snake_case , snake_case , recursive=snake_case ) def a__ ( ): """simple docstring""" if hasattr(fsspec.asyn , '''reset_lock''' ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: __SCREAMING_SNAKE_CASE : int = None __SCREAMING_SNAKE_CASE : Union[str, Any] = None __SCREAMING_SNAKE_CASE : Union[str, Any] = threading.Lock()
303
1
from __future__ import annotations def a__ ( snake_case , snake_case ): """simple docstring""" if b == 0: return (1, 0) ((__SCREAMING_SNAKE_CASE), (__SCREAMING_SNAKE_CASE)) : str = extended_euclid(snake_case , a % b ) __SCREAMING_SNAKE_CASE : List[str] = a // b return (y, x - k * y) def a__ ( snake_case , snake_case , snake_case , snake_case ): """simple docstring""" ((__SCREAMING_SNAKE_CASE), (__SCREAMING_SNAKE_CASE)) : Union[str, Any] = extended_euclid(snake_case , snake_case ) __SCREAMING_SNAKE_CASE : Any = na * na __SCREAMING_SNAKE_CASE : int = ra * x * na + ra * y * na return (n % m + m) % m def a__ ( snake_case , snake_case ): """simple docstring""" ((__SCREAMING_SNAKE_CASE), (__SCREAMING_SNAKE_CASE)) : Optional[int] = extended_euclid(snake_case , snake_case ) if b < 0: __SCREAMING_SNAKE_CASE : Any = (b % n + n) % n return b def a__ ( snake_case , snake_case , snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = invert_modulo(snake_case , snake_case ), invert_modulo(snake_case , snake_case ) __SCREAMING_SNAKE_CASE : Union[str, Any] = na * na __SCREAMING_SNAKE_CASE : int = ra * x * na + ra * y * na return (n % m + m) % m if __name__ == "__main__": from doctest import testmod testmod(name="""chinese_remainder_theorem""", verbose=True) testmod(name="""chinese_remainder_theorem2""", verbose=True) testmod(name="""invert_modulo""", verbose=True) testmod(name="""extended_euclid""", verbose=True)
303
from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels
303
1
import os import numpy import onnx def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = a.name __SCREAMING_SNAKE_CASE : Union[str, Any] = b.name __SCREAMING_SNAKE_CASE : List[Any] = '''''' __SCREAMING_SNAKE_CASE : Union[str, Any] = '''''' __SCREAMING_SNAKE_CASE : List[Any] = a == b __SCREAMING_SNAKE_CASE : Dict = name_a __SCREAMING_SNAKE_CASE : str = name_b return res def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" for i, input_name in enumerate(node_proto.input ): if input_name == name: node_proto.input.insert(snake_case , snake_case ) node_proto.input.pop(i + 1 ) if node_proto.op_type == "If": _graph_replace_input_with(node_proto.attribute[0].g , snake_case , snake_case ) _graph_replace_input_with(node_proto.attribute[1].g , snake_case , snake_case ) if node_proto.op_type == "Loop": _graph_replace_input_with(node_proto.attribute[0].g , snake_case , snake_case ) def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" for n in graph_proto.node: _node_replace_input_with(snake_case , snake_case , snake_case ) def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = list(model.graph.initializer ) __SCREAMING_SNAKE_CASE : Dict = list(model_without_ext.graph.initializer ) for i, ref_i in ind_to_replace: assert inits_with_data[i].name == inits[i].name assert inits_with_data[ref_i].name == inits[ref_i].name assert i > ref_i __SCREAMING_SNAKE_CASE : Any = inits[i].name __SCREAMING_SNAKE_CASE : Dict = inits[ref_i].name model_without_ext.graph.initializer.remove(inits[i] ) # for n in model.graph.node: _graph_replace_input_with(model_without_ext.graph , snake_case , snake_case ) def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = os.path.dirname(snake_case ) __SCREAMING_SNAKE_CASE : Union[str, Any] = os.path.basename(snake_case ) __SCREAMING_SNAKE_CASE : Tuple = onnx.load(os.path.join(snake_case , snake_case ) ) __SCREAMING_SNAKE_CASE : Dict = list(model.graph.initializer ) __SCREAMING_SNAKE_CASE : Optional[Any] = set() __SCREAMING_SNAKE_CASE : Optional[int] = {} __SCREAMING_SNAKE_CASE : Optional[Any] = [] __SCREAMING_SNAKE_CASE : int = 0 for i in range(len(snake_case ) ): if i in dup_set: continue for j in range(i + 1 , len(snake_case ) ): if j in dup_set: continue if _is_equal_tensor_proto(inits[i] , inits[j] ): dup_set.add(snake_case ) dup_set.add(snake_case ) __SCREAMING_SNAKE_CASE : List[str] = inits[j].data_type __SCREAMING_SNAKE_CASE : List[Any] = numpy.prod(inits[j].dims ) if dtype == 1: mem_size *= 4 elif dtype == 6: mem_size *= 4 elif dtype == 7 or dtype == 11: mem_size *= 8 else: print('''unexpected data type: ''' , snake_case ) total_reduced_size += mem_size __SCREAMING_SNAKE_CASE : List[Any] = inits[i].name __SCREAMING_SNAKE_CASE : List[str] = inits[j].name if name_i in dup_map: dup_map[name_i].append(snake_case ) else: __SCREAMING_SNAKE_CASE : List[str] = [name_j] ind_to_replace.append((j, i) ) print('''total reduced size: ''' , total_reduced_size / 1_024 / 1_024 / 1_024 , '''GB''' ) __SCREAMING_SNAKE_CASE : Any = sorted(snake_case ) _remove_dup_initializers_from_model(snake_case , snake_case , snake_case ) __SCREAMING_SNAKE_CASE : Dict = '''optimized_''' + model_file_name __SCREAMING_SNAKE_CASE : List[str] = os.path.join(snake_case , snake_case ) onnx.save(snake_case , snake_case ) return new_model
303
from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { """uw-madison/mra-base-512-4""": """https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json""", } class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''mra''' def __init__( self : str , _A : List[str]=5_0265 , _A : int=768 , _A : Union[str, Any]=12 , _A : Union[str, Any]=12 , _A : Union[str, Any]=3072 , _A : Any="gelu" , _A : List[Any]=0.1 , _A : List[Any]=0.1 , _A : List[str]=512 , _A : Tuple=1 , _A : List[str]=0.02 , _A : Union[str, Any]=1e-5 , _A : Optional[int]="absolute" , _A : Union[str, Any]=4 , _A : List[Any]="full" , _A : Union[str, Any]=0 , _A : Union[str, Any]=0 , _A : Optional[Any]=1 , _A : Union[str, Any]=0 , _A : Any=2 , **_A : List[str] , ): """simple docstring""" super().__init__(pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , **_A ) __SCREAMING_SNAKE_CASE : Dict = vocab_size __SCREAMING_SNAKE_CASE : str = max_position_embeddings __SCREAMING_SNAKE_CASE : Optional[int] = hidden_size __SCREAMING_SNAKE_CASE : str = num_hidden_layers __SCREAMING_SNAKE_CASE : Tuple = num_attention_heads __SCREAMING_SNAKE_CASE : str = intermediate_size __SCREAMING_SNAKE_CASE : Tuple = hidden_act __SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob __SCREAMING_SNAKE_CASE : int = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : Tuple = initializer_range __SCREAMING_SNAKE_CASE : Any = type_vocab_size __SCREAMING_SNAKE_CASE : str = layer_norm_eps __SCREAMING_SNAKE_CASE : Union[str, Any] = position_embedding_type __SCREAMING_SNAKE_CASE : str = block_per_row __SCREAMING_SNAKE_CASE : Union[str, Any] = approx_mode __SCREAMING_SNAKE_CASE : Optional[int] = initial_prior_first_n_blocks __SCREAMING_SNAKE_CASE : List[Any] = initial_prior_diagonal_n_blocks
303
1
import importlib import torch import yaml from omegaconf import OmegaConf from taming.models.vqgan import VQModel def a__ ( snake_case , snake_case=False ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = OmegaConf.load(snake_case ) if display: print(yaml.dump(OmegaConf.to_container(snake_case ) ) ) return config def a__ ( snake_case , snake_case=None , snake_case=None ): """simple docstring""" if conf_path is None: __SCREAMING_SNAKE_CASE : Any = '''./model_checkpoints/vqgan_only.yaml''' __SCREAMING_SNAKE_CASE : List[str] = load_config(snake_case , display=snake_case ) __SCREAMING_SNAKE_CASE : str = VQModel(**config.model.params ) if ckpt_path is None: __SCREAMING_SNAKE_CASE : Optional[Any] = '''./model_checkpoints/vqgan_only.pt''' __SCREAMING_SNAKE_CASE : Optional[Any] = torch.load(snake_case , map_location=snake_case ) if ".ckpt" in ckpt_path: __SCREAMING_SNAKE_CASE : Optional[Any] = sd['''state_dict'''] model.load_state_dict(snake_case , strict=snake_case ) model.to(snake_case ) del sd return model def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Any = model.encode(snake_case ) print(F'''VQGAN --- {model.__class__.__name__}: latent shape: {z.shape[2:]}''' ) __SCREAMING_SNAKE_CASE : Any = model.decode(snake_case ) return xrec def a__ ( snake_case , snake_case=False ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = string.rsplit('''.''' , 1 ) if reload: __SCREAMING_SNAKE_CASE : Union[str, Any] = importlib.import_module(snake_case ) importlib.reload(snake_case ) return getattr(importlib.import_module(snake_case , package=snake_case ) , cls ) def a__ ( snake_case ): """simple docstring""" if "target" not in config: raise KeyError('''Expected key `target` to instantiate.''' ) return get_obj_from_str(config['''target'''] )(**config.get('''params''' , {} ) ) def a__ ( snake_case , snake_case , snake_case=True , snake_case=True ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = instantiate_from_config(snake_case ) if sd is not None: model.load_state_dict(snake_case ) if gpu: model.cuda() if eval_mode: model.eval() return {"model": model} def a__ ( snake_case , snake_case , snake_case , snake_case ): """simple docstring""" # load the specified checkpoint if ckpt: __SCREAMING_SNAKE_CASE : Dict = torch.load(snake_case , map_location='''cpu''' ) __SCREAMING_SNAKE_CASE : List[Any] = pl_sd['''global_step'''] print(F'''loaded model from global step {global_step}.''' ) else: __SCREAMING_SNAKE_CASE : Optional[Any] = {'''state_dict''': None} __SCREAMING_SNAKE_CASE : Optional[Any] = None __SCREAMING_SNAKE_CASE : Dict = load_model_from_config(config.model , pl_sd['''state_dict'''] , gpu=snake_case , eval_mode=snake_case )['''model'''] return model, global_step
303
from typing import List, Optional, Tuple, Union import torch from ...utils import logging, randn_tensor from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline lowercase_ = logging.get_logger(__name__) # pylint: disable=invalid-name class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" def __init__( self : List[str] , _A : Dict , _A : List[Any] ): """simple docstring""" super().__init__() self.register_modules(unet=_A , scheduler=_A ) @torch.no_grad() def __call__( self : List[str] , _A : int = 1 , _A : int = 100 , _A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _A : Optional[float] = None , _A : bool = True , ): """simple docstring""" if audio_length_in_s is None: __SCREAMING_SNAKE_CASE : Optional[Any] = self.unet.config.sample_size / self.unet.config.sample_rate __SCREAMING_SNAKE_CASE : List[Any] = audio_length_in_s * self.unet.config.sample_rate __SCREAMING_SNAKE_CASE : Any = 2 ** len(self.unet.up_blocks ) if sample_size < 3 * down_scale_factor: raise ValueError( F'''{audio_length_in_s} is too small. Make sure it\'s bigger or equal to''' F''' {3 * down_scale_factor / self.unet.config.sample_rate}.''' ) __SCREAMING_SNAKE_CASE : int = int(_A ) if sample_size % down_scale_factor != 0: __SCREAMING_SNAKE_CASE : Optional[int] = ( (audio_length_in_s * self.unet.config.sample_rate) // down_scale_factor + 1 ) * down_scale_factor logger.info( F'''{audio_length_in_s} is increased to {sample_size / self.unet.config.sample_rate} so that it can be handled''' F''' by the model. It will be cut to {original_sample_size / self.unet.config.sample_rate} after the denoising''' ''' process.''' ) __SCREAMING_SNAKE_CASE : List[Any] = int(_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = next(iter(self.unet.parameters() ) ).dtype __SCREAMING_SNAKE_CASE : int = (batch_size, self.unet.config.in_channels, sample_size) if isinstance(_A , _A ) and len(_A ) != batch_size: raise ValueError( F'''You have passed a list of generators of length {len(_A )}, but requested an effective batch''' F''' size of {batch_size}. Make sure the batch size matches the length of the generators.''' ) __SCREAMING_SNAKE_CASE : Dict = randn_tensor(_A , generator=_A , device=self.device , dtype=_A ) # set step values self.scheduler.set_timesteps(_A , device=audio.device ) __SCREAMING_SNAKE_CASE : Dict = self.scheduler.timesteps.to(_A ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output __SCREAMING_SNAKE_CASE : List[Any] = self.unet(_A , _A ).sample # 2. compute previous image: x_t -> t_t-1 __SCREAMING_SNAKE_CASE : Optional[int] = self.scheduler.step(_A , _A , _A ).prev_sample __SCREAMING_SNAKE_CASE : str = audio.clamp(-1 , 1 ).float().cpu().numpy() __SCREAMING_SNAKE_CASE : str = audio[:, :, :original_sample_size] if not return_dict: return (audio,) return AudioPipelineOutput(audios=_A )
303
1
import argparse import dataclasses import json import logging import os import shutil from typing import List, Optional import datasets from accelerate import Accelerator from datasets import load_dataset from finetuning import finetune from tqdm.auto import tqdm import transformers from transformers import AutoConfig, set_seed from transformers.trainer_utils import IntervalStrategy lowercase_ = logging.getLogger(__name__) lowercase_ = """pytorch_model.bin""" @dataclasses.dataclass class __UpperCamelCase : """simple docstring""" lowerCAmelCase_ = dataclasses.field( metadata={'''help''': '''Path to pretrained model or model identifier from huggingface.co/models.'''} ) lowerCAmelCase_ = dataclasses.field( default=lowerCAmelCase__ , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co.'''} , ) @dataclasses.dataclass class __UpperCamelCase : """simple docstring""" lowerCAmelCase_ = dataclasses.field(metadata={'''help''': '''A csv or a json file containing the training data.'''} ) lowerCAmelCase_ = dataclasses.field(metadata={'''help''': '''A csv or a json file containing the data to predict on.'''} ) lowerCAmelCase_ = dataclasses.field( default=lowerCAmelCase__ , metadata={'''help''': '''A csv or a json file containing the validation data.'''} ) lowerCAmelCase_ = dataclasses.field( default=lowerCAmelCase__ , metadata={'''help''': '''The name of the task to train on.'''} , ) lowerCAmelCase_ = dataclasses.field( default=lowerCAmelCase__ , metadata={'''help''': '''The list of labels for the task.'''} ) @dataclasses.dataclass class __UpperCamelCase : """simple docstring""" lowerCAmelCase_ = dataclasses.field( metadata={'''help''': '''The output directory where the model predictions and checkpoints will be written.'''} ) lowerCAmelCase_ = dataclasses.field( default='''accuracy''' , metadata={'''help''': '''The evaluation metric used for the task.'''} ) lowerCAmelCase_ = dataclasses.field( default='''no''' , metadata={ '''help''': '''The evaluation strategy to adopt during training. Possible values are: ["no", "step", "epoch]''' } , ) lowerCAmelCase_ = dataclasses.field( default=10 , metadata={'''help''': '''Number of evaluation calls with no improvement after which training will be stopped.'''} , ) lowerCAmelCase_ = dataclasses.field( default=0.0 , metadata={ '''help''': '''How much the specified evaluation metric must improve to satisfy early stopping conditions.''' } , ) lowerCAmelCase_ = dataclasses.field( default=lowerCAmelCase__ , metadata={'''help''': '''Whether to filter the pseudo-labeled data based on the confidence score.'''} , ) lowerCAmelCase_ = dataclasses.field( default=lowerCAmelCase__ , metadata={'''help''': '''Whether to filter the pseudo-labeled data based on the validation performance.'''} , ) lowerCAmelCase_ = dataclasses.field( default=lowerCAmelCase__ , metadata={'''help''': '''Whether to fine-tune on labeled data after pseudo training.'''} , ) lowerCAmelCase_ = dataclasses.field( default=0.0 , metadata={'''help''': '''Confidence threshold for pseudo-labeled data filtering.'''} , ) lowerCAmelCase_ = dataclasses.field( default=1_00 , metadata={'''help''': '''Number of evaluation calls with no improvement after which training will be stopped.'''} , ) lowerCAmelCase_ = dataclasses.field( default=lowerCAmelCase__ , metadata={'''help''': '''Random seed for initialization.'''} , ) def a__ ( snake_case , snake_case , snake_case , snake_case , snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = datasets.concatenate_datasets([infer_input, infer_output] , axis=1 ) if args.do_filter_by_confidence: __SCREAMING_SNAKE_CASE : Optional[int] = dataset.filter(lambda snake_case : example["probability"] > args.confidence_threshold ) if args.do_filter_by_val_performance: assert eval_result >= 0.0 and eval_result <= 1.0 __SCREAMING_SNAKE_CASE : List[Any] = int(eval_result * len(snake_case ) ) print(snake_case ) __SCREAMING_SNAKE_CASE : Tuple = dataset.sort('''probability''' , reverse=snake_case ) __SCREAMING_SNAKE_CASE : Any = dataset.select(range(snake_case ) ) __SCREAMING_SNAKE_CASE : int = dataset.remove_columns(['''label''', '''probability'''] ) __SCREAMING_SNAKE_CASE : List[Any] = dataset.rename_column('''prediction''' , '''label''' ) __SCREAMING_SNAKE_CASE : Dict = dataset.map(lambda snake_case : {"label": idalabel[example["label"]]} ) __SCREAMING_SNAKE_CASE : Any = dataset.shuffle(seed=args.seed ) __SCREAMING_SNAKE_CASE : Union[str, Any] = os.path.join(snake_case , F'''train_pseudo.{args.data_file_extension}''' ) if args.data_file_extension == "csv": dataset.to_csv(snake_case , index=snake_case ) else: dataset.to_json(snake_case ) def a__ ( snake_case , snake_case , snake_case , snake_case , **snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = Accelerator() # Make one log on every process with the configuration for debugging. logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , level=logging.INFO , ) logger.info(accelerator.state ) # Setup logging, we only want one process per machine to log things on the # screen. accelerator.is_local_main_process is only True for one process per # machine. logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() __SCREAMING_SNAKE_CASE : List[str] = STModelArguments(model_name_or_path=snake_case ) __SCREAMING_SNAKE_CASE : Dict = STDataArguments(train_file=snake_case , infer_file=snake_case ) __SCREAMING_SNAKE_CASE : Tuple = STTrainingArguments(output_dir=snake_case ) __SCREAMING_SNAKE_CASE : Optional[Any] = argparse.Namespace() for arg_class in (model_args, data_args, training_args): for key, value in vars(snake_case ).items(): setattr(snake_case , snake_case , snake_case ) for key, value in kwargs.items(): if hasattr(snake_case , snake_case ): setattr(snake_case , snake_case , snake_case ) # Sanity checks __SCREAMING_SNAKE_CASE : Any = {} __SCREAMING_SNAKE_CASE : Tuple = None # You need to provide the training data and the data to predict on assert args.train_file is not None assert args.infer_file is not None __SCREAMING_SNAKE_CASE : Union[str, Any] = args.train_file __SCREAMING_SNAKE_CASE : str = args.infer_file if args.evaluation_strategy != IntervalStrategy.NO.value: assert args.eval_file is not None __SCREAMING_SNAKE_CASE : Optional[Any] = args.eval_file for key in data_files: __SCREAMING_SNAKE_CASE : int = data_files[key].split('''.''' )[-1] assert extension in ["csv", "json"], F'''`{key}_file` should be a csv or a json file.''' if args.data_file_extension is None: __SCREAMING_SNAKE_CASE : List[Any] = extension else: assert extension == args.data_file_extension, F'''`{key}_file` should be a {args.data_file_extension} file`.''' assert ( args.eval_metric in datasets.list_metrics() ), F'''{args.eval_metric} not in the list of supported metrics {datasets.list_metrics()}.''' # If passed along, set the training seed now. if args.seed is not None: set_seed(args.seed ) logger.info('''Creating the initial data directory for self-training...''' ) __SCREAMING_SNAKE_CASE : Dict = F'''{args.output_dir}/self-train_iter-{{}}'''.format __SCREAMING_SNAKE_CASE : Union[str, Any] = data_dir_format(0 ) if accelerator.is_main_process: if args.output_dir is not None: os.makedirs(args.output_dir , exist_ok=snake_case ) os.makedirs(snake_case , exist_ok=snake_case ) accelerator.wait_for_everyone() __SCREAMING_SNAKE_CASE : List[str] = None __SCREAMING_SNAKE_CASE : List[str] = None __SCREAMING_SNAKE_CASE : Tuple = 0 __SCREAMING_SNAKE_CASE : str = False # Show the progress bar __SCREAMING_SNAKE_CASE : int = tqdm(range(args.max_selftrain_iterations ) , disable=not accelerator.is_local_main_process ) # Self-train for iteration in range(0 , int(args.max_selftrain_iterations ) ): __SCREAMING_SNAKE_CASE : Dict = data_dir_format(snake_case ) assert os.path.exists(snake_case ) # Stage 1: initial fine-tuning for iteration = 0 or pseudo-training for # iteration > 0 __SCREAMING_SNAKE_CASE : Union[str, Any] = os.path.join(snake_case , '''stage-1''' ) __SCREAMING_SNAKE_CASE : Dict = { '''accelerator''': accelerator, '''model_name_or_path''': args.model_name_or_path, '''cache_dir''': args.cache_dir, '''do_train''': True, '''train_file''': data_files['''train'''] if iteration == 0 else data_files['''train_pseudo'''], '''do_eval''': True if args.eval_file is not None else False, '''eval_file''': data_files['''eval'''], '''do_predict''': True, '''infer_file''': data_files['''infer'''], '''task_name''': args.task_name, '''label_list''': args.label_list, '''output_dir''': current_output_dir, '''eval_metric''': args.eval_metric, '''evaluation_strategy''': args.evaluation_strategy, '''early_stopping_patience''': args.early_stopping_patience, '''early_stopping_threshold''': args.early_stopping_threshold, '''seed''': args.seed, } # Add additional training arguments for key, value in kwargs.items(): if key not in arguments_dict and not hasattr(snake_case , snake_case ): arguments_dict.update({key: value} ) __SCREAMING_SNAKE_CASE : Union[str, Any] = os.path.join(snake_case , '''best-checkpoint''' , snake_case ) if os.path.exists(snake_case ): logger.info( '''Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 1.''' , snake_case , snake_case , ) else: logger.info('''***** Running self-training: iteration: %d, stage: 1 *****''' , snake_case ) finetune(**snake_case ) accelerator.wait_for_everyone() assert os.path.exists(snake_case ) logger.info('''Self-training job completed: iteration: %d, stage: 1.''' , snake_case ) if iteration > 0 and args.finetune_on_labeled_data: # Stage 2 (optional): fine-tuning on the original labeled data __SCREAMING_SNAKE_CASE : int = os.path.join(snake_case , '''best-checkpoint''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = os.path.join(snake_case , '''stage-2''' ) # Update arguments_dict __SCREAMING_SNAKE_CASE : Tuple = model_path __SCREAMING_SNAKE_CASE : str = data_files['''train'''] __SCREAMING_SNAKE_CASE : int = current_output_dir __SCREAMING_SNAKE_CASE : Any = os.path.join(snake_case , '''best-checkpoint''' , snake_case ) if os.path.exists(snake_case ): logger.info( '''Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 2.''' , snake_case , snake_case , ) else: logger.info('''***** Running self-training: iteration: %d, stage: 2 *****''' , snake_case ) finetune(**snake_case ) accelerator.wait_for_everyone() assert os.path.exists(snake_case ) logger.info('''Self-training job completed: iteration: %d, stage: 2.''' , snake_case ) __SCREAMING_SNAKE_CASE : Union[str, Any] = iteration __SCREAMING_SNAKE_CASE : Tuple = data_dir_format(iteration + 1 ) __SCREAMING_SNAKE_CASE : List[Any] = AutoConfig.from_pretrained(os.path.join(snake_case , '''best-checkpoint''' ) ) __SCREAMING_SNAKE_CASE : Optional[int] = config.idalabel __SCREAMING_SNAKE_CASE : Union[str, Any] = os.path.join(snake_case , '''eval_results_best-checkpoint.json''' ) __SCREAMING_SNAKE_CASE : Optional[int] = os.path.join(snake_case , '''test_results_best-checkpoint.json''' ) assert os.path.exists(snake_case ) with open(snake_case , '''r''' ) as f: __SCREAMING_SNAKE_CASE : Any = float(json.load(snake_case )[args.eval_metric] ) __SCREAMING_SNAKE_CASE : List[Any] = os.path.join(snake_case , '''infer_output_best-checkpoint.csv''' ) assert os.path.exists(snake_case ) # Loading the dataset from local csv or json files. __SCREAMING_SNAKE_CASE : Optional[Any] = load_dataset(args.data_file_extension , data_files={'''data''': data_files['''infer''']} )['''data'''] __SCREAMING_SNAKE_CASE : int = load_dataset('''csv''' , data_files={'''data''': infer_output_file} )['''data'''] if accelerator.is_main_process: os.makedirs(snake_case , exist_ok=snake_case ) shutil.copy(snake_case , os.path.join(snake_case , F'''eval_results_iter-{iteration}.json''' ) ) if os.path.exists(snake_case ): shutil.copy(snake_case , os.path.join(snake_case , F'''test_results_iter-{iteration}.json''' ) ) create_pseudo_labeled_data(snake_case , snake_case , snake_case , snake_case , snake_case , snake_case ) accelerator.wait_for_everyone() __SCREAMING_SNAKE_CASE : int = os.path.join(snake_case , F'''train_pseudo.{args.data_file_extension}''' ) if args.evaluation_strategy != IntervalStrategy.NO.value: __SCREAMING_SNAKE_CASE : Tuple = eval_result if best_iteration is None: __SCREAMING_SNAKE_CASE : Tuple = new_iteration __SCREAMING_SNAKE_CASE : Union[str, Any] = new_eval_result else: if new_eval_result - best_eval_result > args.early_stopping_threshold: __SCREAMING_SNAKE_CASE : int = new_iteration __SCREAMING_SNAKE_CASE : str = new_eval_result __SCREAMING_SNAKE_CASE : Optional[Any] = 0 else: if new_eval_result == best_eval_result: __SCREAMING_SNAKE_CASE : int = new_iteration __SCREAMING_SNAKE_CASE : Any = new_eval_result early_stopping_patience_counter += 1 if early_stopping_patience_counter >= args.early_stopping_patience: __SCREAMING_SNAKE_CASE : List[str] = True progress_bar.update(1 ) if should_training_stop: break if best_iteration is not None: # Save the best iteration logger.info('''Best iteration: %d''' , snake_case ) logger.info('''Best evaluation result: %s = %f''' , args.eval_metric , snake_case ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(snake_case , F'''eval_results_iter-{iteration}.json''' ) , os.path.join(snake_case , '''eval_results_best-iteration.json''' ) , ) else: # Assume that the last iteration is the best logger.info('''Best iteration: %d''' , args.max_selftrain_iterations - 1 ) logger.info('''Best evaluation result: %s = %f''' , args.eval_metric , snake_case ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(snake_case , F'''eval_results_iter-{args.max_selftrain_iterations - 1}.json''' ) , os.path.join(snake_case , '''eval_results_best-iteration.json''' ) , )
303
import importlib import torch import yaml from omegaconf import OmegaConf from taming.models.vqgan import VQModel def a__ ( snake_case , snake_case=False ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = OmegaConf.load(snake_case ) if display: print(yaml.dump(OmegaConf.to_container(snake_case ) ) ) return config def a__ ( snake_case , snake_case=None , snake_case=None ): """simple docstring""" if conf_path is None: __SCREAMING_SNAKE_CASE : Any = '''./model_checkpoints/vqgan_only.yaml''' __SCREAMING_SNAKE_CASE : List[str] = load_config(snake_case , display=snake_case ) __SCREAMING_SNAKE_CASE : str = VQModel(**config.model.params ) if ckpt_path is None: __SCREAMING_SNAKE_CASE : Optional[Any] = '''./model_checkpoints/vqgan_only.pt''' __SCREAMING_SNAKE_CASE : Optional[Any] = torch.load(snake_case , map_location=snake_case ) if ".ckpt" in ckpt_path: __SCREAMING_SNAKE_CASE : Optional[Any] = sd['''state_dict'''] model.load_state_dict(snake_case , strict=snake_case ) model.to(snake_case ) del sd return model def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Any = model.encode(snake_case ) print(F'''VQGAN --- {model.__class__.__name__}: latent shape: {z.shape[2:]}''' ) __SCREAMING_SNAKE_CASE : Any = model.decode(snake_case ) return xrec def a__ ( snake_case , snake_case=False ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = string.rsplit('''.''' , 1 ) if reload: __SCREAMING_SNAKE_CASE : Union[str, Any] = importlib.import_module(snake_case ) importlib.reload(snake_case ) return getattr(importlib.import_module(snake_case , package=snake_case ) , cls ) def a__ ( snake_case ): """simple docstring""" if "target" not in config: raise KeyError('''Expected key `target` to instantiate.''' ) return get_obj_from_str(config['''target'''] )(**config.get('''params''' , {} ) ) def a__ ( snake_case , snake_case , snake_case=True , snake_case=True ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = instantiate_from_config(snake_case ) if sd is not None: model.load_state_dict(snake_case ) if gpu: model.cuda() if eval_mode: model.eval() return {"model": model} def a__ ( snake_case , snake_case , snake_case , snake_case ): """simple docstring""" # load the specified checkpoint if ckpt: __SCREAMING_SNAKE_CASE : Dict = torch.load(snake_case , map_location='''cpu''' ) __SCREAMING_SNAKE_CASE : List[Any] = pl_sd['''global_step'''] print(F'''loaded model from global step {global_step}.''' ) else: __SCREAMING_SNAKE_CASE : Optional[Any] = {'''state_dict''': None} __SCREAMING_SNAKE_CASE : Optional[Any] = None __SCREAMING_SNAKE_CASE : Dict = load_model_from_config(config.model , pl_sd['''state_dict'''] , gpu=snake_case , eval_mode=snake_case )['''model'''] return model, global_step
303
1
from collections import Counter from timeit import timeit def a__ ( snake_case = "" , ): """simple docstring""" return sum(c % 2 for c in Counter(input_str.replace(''' ''' , '''''' ).lower() ).values() ) < 2 def a__ ( snake_case = "" ): """simple docstring""" if len(snake_case ) == 0: return True __SCREAMING_SNAKE_CASE : Tuple = input_str.replace(''' ''' , '''''' ).lower() # character_freq_dict: Stores the frequency of every character in the input string __SCREAMING_SNAKE_CASE : dict[str, int] = {} for character in lower_case_input_str: __SCREAMING_SNAKE_CASE : List[str] = character_freq_dict.get(snake_case , 0 ) + 1 __SCREAMING_SNAKE_CASE : str = 0 for character_count in character_freq_dict.values(): if character_count % 2: odd_char += 1 if odd_char > 1: return False return True def a__ ( snake_case = "" ): """simple docstring""" print('''\nFor string = ''' , snake_case , ''':''' ) print( '''> can_string_be_rearranged_as_palindrome_counter()''' , '''\tans =''' , can_string_be_rearranged_as_palindrome_counter(snake_case ) , '''\ttime =''' , timeit( '''z.can_string_be_rearranged_as_palindrome_counter(z.check_str)''' , setup='''import __main__ as z''' , ) , '''seconds''' , ) print( '''> can_string_be_rearranged_as_palindrome()''' , '''\tans =''' , can_string_be_rearranged_as_palindrome(snake_case ) , '''\ttime =''' , timeit( '''z.can_string_be_rearranged_as_palindrome(z.check_str)''' , setup='''import __main__ as z''' , ) , '''seconds''' , ) if __name__ == "__main__": lowercase_ = input( """Enter string to determine if it can be rearranged as a palindrome or not: """ ).strip() benchmark(check_str) lowercase_ = can_string_be_rearranged_as_palindrome_counter(check_str) print(f'''{check_str} can {'' if status else 'not '}be rearranged as a palindrome''')
303
from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { """studio-ousia/luke-base""": """https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json""", """studio-ousia/luke-large""": """https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json""", } class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''luke''' def __init__( self : Any , _A : int=5_0267 , _A : str=50_0000 , _A : Dict=768 , _A : int=256 , _A : Tuple=12 , _A : Optional[Any]=12 , _A : Any=3072 , _A : Tuple="gelu" , _A : Any=0.1 , _A : Dict=0.1 , _A : Any=512 , _A : Tuple=2 , _A : int=0.02 , _A : Any=1e-12 , _A : Dict=True , _A : Optional[Any]=None , _A : List[str]=1 , _A : List[str]=0 , _A : Dict=2 , **_A : List[str] , ): """simple docstring""" super().__init__(pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , **_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = vocab_size __SCREAMING_SNAKE_CASE : Any = entity_vocab_size __SCREAMING_SNAKE_CASE : int = hidden_size __SCREAMING_SNAKE_CASE : List[Any] = entity_emb_size __SCREAMING_SNAKE_CASE : Union[str, Any] = num_hidden_layers __SCREAMING_SNAKE_CASE : Tuple = num_attention_heads __SCREAMING_SNAKE_CASE : Dict = hidden_act __SCREAMING_SNAKE_CASE : Dict = intermediate_size __SCREAMING_SNAKE_CASE : Optional[int] = hidden_dropout_prob __SCREAMING_SNAKE_CASE : Any = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : List[Any] = max_position_embeddings __SCREAMING_SNAKE_CASE : List[str] = type_vocab_size __SCREAMING_SNAKE_CASE : Dict = initializer_range __SCREAMING_SNAKE_CASE : Tuple = layer_norm_eps __SCREAMING_SNAKE_CASE : int = use_entity_aware_attention __SCREAMING_SNAKE_CASE : Any = classifier_dropout
303
1
def a__ ( snake_case = 4_000_000 ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = [0, 1] __SCREAMING_SNAKE_CASE : int = 0 while fib[i] <= n: fib.append(fib[i] + fib[i + 1] ) if fib[i + 2] > n: break i += 1 __SCREAMING_SNAKE_CASE : Dict = 0 for j in range(len(snake_case ) - 1 ): if fib[j] % 2 == 0: total += fib[j] return total if __name__ == "__main__": print(f'''{solution() = }''')
303
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase_ = { """configuration_electra""": ["""ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ElectraConfig""", """ElectraOnnxConfig"""], """tokenization_electra""": ["""ElectraTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""ElectraTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST""", """ElectraForCausalLM""", """ElectraForMaskedLM""", """ElectraForMultipleChoice""", """ElectraForPreTraining""", """ElectraForQuestionAnswering""", """ElectraForSequenceClassification""", """ElectraForTokenClassification""", """ElectraModel""", """ElectraPreTrainedModel""", """load_tf_weights_in_electra""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFElectraForMaskedLM""", """TFElectraForMultipleChoice""", """TFElectraForPreTraining""", """TFElectraForQuestionAnswering""", """TFElectraForSequenceClassification""", """TFElectraForTokenClassification""", """TFElectraModel""", """TFElectraPreTrainedModel""", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """FlaxElectraForCausalLM""", """FlaxElectraForMaskedLM""", """FlaxElectraForMultipleChoice""", """FlaxElectraForPreTraining""", """FlaxElectraForQuestionAnswering""", """FlaxElectraForSequenceClassification""", """FlaxElectraForTokenClassification""", """FlaxElectraModel""", """FlaxElectraPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_electra import ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ElectraConfig, ElectraOnnxConfig from .tokenization_electra import ElectraTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_electra_fast import ElectraTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_electra import ( ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, ElectraForCausalLM, ElectraForMaskedLM, ElectraForMultipleChoice, ElectraForPreTraining, ElectraForQuestionAnswering, ElectraForSequenceClassification, ElectraForTokenClassification, ElectraModel, ElectraPreTrainedModel, load_tf_weights_in_electra, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_electra import ( TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, TFElectraForMaskedLM, TFElectraForMultipleChoice, TFElectraForPreTraining, TFElectraForQuestionAnswering, TFElectraForSequenceClassification, TFElectraForTokenClassification, TFElectraModel, TFElectraPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_electra import ( FlaxElectraForCausalLM, FlaxElectraForMaskedLM, FlaxElectraForMultipleChoice, FlaxElectraForPreTraining, FlaxElectraForQuestionAnswering, FlaxElectraForSequenceClassification, FlaxElectraForTokenClassification, FlaxElectraModel, FlaxElectraPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
303
1
import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class __UpperCamelCase ( unittest.TestCase ): """simple docstring""" def UpperCAmelCase__ ( self : int ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = inspect.getfile(accelerate.test_utils ) __SCREAMING_SNAKE_CASE : str = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) __SCREAMING_SNAKE_CASE : Optional[Any] = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_distributed_data_loop.py'''] ) __SCREAMING_SNAKE_CASE : Tuple = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_ops.py'''] ) @require_multi_gpu def UpperCAmelCase__ ( self : str ): """simple docstring""" print(F'''Found {torch.cuda.device_count()} devices.''' ) __SCREAMING_SNAKE_CASE : str = ['''torchrun''', F'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_A , env=os.environ.copy() ) @require_multi_gpu def UpperCAmelCase__ ( self : str ): """simple docstring""" print(F'''Found {torch.cuda.device_count()} devices.''' ) __SCREAMING_SNAKE_CASE : Any = ['''torchrun''', F'''--nproc_per_node={torch.cuda.device_count()}''', self.operation_file_path] print(F'''Command: {cmd}''' ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_A , env=os.environ.copy() ) @require_multi_gpu def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = ['''torchrun''', F'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_A , env=os.environ.copy() ) @require_multi_gpu def UpperCAmelCase__ ( self : Any ): """simple docstring""" print(F'''Found {torch.cuda.device_count()} devices, using 2 devices only''' ) __SCREAMING_SNAKE_CASE : Tuple = ['''torchrun''', F'''--nproc_per_node={torch.cuda.device_count()}''', self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices='''0,1''' ): execute_subprocess_async(_A , env=os.environ.copy() ) if __name__ == "__main__": lowercase_ = Accelerator() lowercase_ = (accelerator.state.process_index + 2, 10) lowercase_ = torch.randint(0, 10, shape).to(accelerator.device) lowercase_ = """""" lowercase_ = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." lowercase_ = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." lowercase_ = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
303
from typing import Dict, List, Optional, Tuple, Union import torch from ...models import AutoencoderKL, TransformeraDModel from ...schedulers import KarrasDiffusionSchedulers from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" def __init__( self : List[Any] , _A : TransformeraDModel , _A : AutoencoderKL , _A : KarrasDiffusionSchedulers , _A : Optional[Dict[int, str]] = None , ): """simple docstring""" super().__init__() self.register_modules(transformer=_A , vae=_A , scheduler=_A ) # create a imagenet -> id dictionary for easier use __SCREAMING_SNAKE_CASE : Optional[int] = {} if idalabel is not None: for key, value in idalabel.items(): for label in value.split(''',''' ): __SCREAMING_SNAKE_CASE : Optional[Any] = int(_A ) __SCREAMING_SNAKE_CASE : List[str] = dict(sorted(self.labels.items() ) ) def UpperCAmelCase__ ( self : List[Any] , _A : Union[str, List[str]] ): """simple docstring""" if not isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : Union[str, Any] = list(_A ) for l in label: if l not in self.labels: raise ValueError( F'''{l} does not exist. Please make sure to select one of the following labels: \n {self.labels}.''' ) return [self.labels[l] for l in label] @torch.no_grad() def __call__( self : Dict , _A : List[int] , _A : float = 4.0 , _A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _A : int = 50 , _A : Optional[str] = "pil" , _A : bool = True , ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = len(_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = self.transformer.config.sample_size __SCREAMING_SNAKE_CASE : List[Any] = self.transformer.config.in_channels __SCREAMING_SNAKE_CASE : Optional[int] = randn_tensor( shape=(batch_size, latent_channels, latent_size, latent_size) , generator=_A , device=self.device , dtype=self.transformer.dtype , ) __SCREAMING_SNAKE_CASE : Tuple = torch.cat([latents] * 2 ) if guidance_scale > 1 else latents __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor(_A , device=self.device ).reshape(-1 ) __SCREAMING_SNAKE_CASE : Any = torch.tensor([1000] * batch_size , device=self.device ) __SCREAMING_SNAKE_CASE : Any = torch.cat([class_labels, class_null] , 0 ) if guidance_scale > 1 else class_labels # set step values self.scheduler.set_timesteps(_A ) for t in self.progress_bar(self.scheduler.timesteps ): if guidance_scale > 1: __SCREAMING_SNAKE_CASE : Optional[Any] = latent_model_input[: len(_A ) // 2] __SCREAMING_SNAKE_CASE : List[Any] = torch.cat([half, half] , dim=0 ) __SCREAMING_SNAKE_CASE : int = self.scheduler.scale_model_input(_A , _A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = t if not torch.is_tensor(_A ): # TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can # This would be a good case for the `match` statement (Python 3.10+) __SCREAMING_SNAKE_CASE : Any = latent_model_input.device.type == '''mps''' if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : List[Any] = torch.floataa if is_mps else torch.floataa else: __SCREAMING_SNAKE_CASE : int = torch.intaa if is_mps else torch.intaa __SCREAMING_SNAKE_CASE : int = torch.tensor([timesteps] , dtype=_A , device=latent_model_input.device ) elif len(timesteps.shape ) == 0: __SCREAMING_SNAKE_CASE : Optional[Any] = timesteps[None].to(latent_model_input.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML __SCREAMING_SNAKE_CASE : Optional[int] = timesteps.expand(latent_model_input.shape[0] ) # predict noise model_output __SCREAMING_SNAKE_CASE : Union[str, Any] = self.transformer( _A , timestep=_A , class_labels=_A ).sample # perform guidance if guidance_scale > 1: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = noise_pred[:, :latent_channels], noise_pred[:, latent_channels:] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = torch.split(_A , len(_A ) // 2 , dim=0 ) __SCREAMING_SNAKE_CASE : str = uncond_eps + guidance_scale * (cond_eps - uncond_eps) __SCREAMING_SNAKE_CASE : List[Any] = torch.cat([half_eps, half_eps] , dim=0 ) __SCREAMING_SNAKE_CASE : List[str] = torch.cat([eps, rest] , dim=1 ) # learned sigma if self.transformer.config.out_channels // 2 == latent_channels: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = torch.split(_A , _A , dim=1 ) else: __SCREAMING_SNAKE_CASE : List[Any] = noise_pred # compute previous image: x_t -> x_t-1 __SCREAMING_SNAKE_CASE : str = self.scheduler.step(_A , _A , _A ).prev_sample if guidance_scale > 1: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = latent_model_input.chunk(2 , dim=0 ) else: __SCREAMING_SNAKE_CASE : Optional[Any] = latent_model_input __SCREAMING_SNAKE_CASE : List[Any] = 1 / self.vae.config.scaling_factor * latents __SCREAMING_SNAKE_CASE : List[str] = self.vae.decode(_A ).sample __SCREAMING_SNAKE_CASE : Any = (samples / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 __SCREAMING_SNAKE_CASE : int = samples.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": __SCREAMING_SNAKE_CASE : str = self.numpy_to_pil(_A ) if not return_dict: return (samples,) return ImagePipelineOutput(images=_A )
303
1
from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
303
import os import sys lowercase_ = os.path.join(os.path.dirname(__file__), """src""") sys.path.append(SRC_DIR) from transformers import ( AutoConfig, AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoTokenizer, add_start_docstrings, ) lowercase_ = [ """torch""", """numpy""", """tokenizers""", """filelock""", """requests""", """tqdm""", """regex""", """sentencepiece""", """sacremoses""", """importlib_metadata""", """huggingface_hub""", ] @add_start_docstrings(AutoConfig.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoConfig.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoTokenizer.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoTokenizer.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModel.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModel.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForCausalLM.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForCausalLM.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForMaskedLM.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForMaskedLM.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForSequenceClassification.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForSequenceClassification.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForQuestionAnswering.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForQuestionAnswering.from_pretrained(*snake_case , **snake_case )
303
1
from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels
303
from __future__ import annotations import numpy as np def a__ ( snake_case ): """simple docstring""" return np.maximum(0 , snake_case ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
303
1
import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''Speech2TextFeatureExtractor''' lowerCAmelCase_ = '''Speech2TextTokenizer''' def __init__( self : int , _A : Optional[Any] , _A : List[Any] ): """simple docstring""" super().__init__(_A , _A ) __SCREAMING_SNAKE_CASE : int = self.feature_extractor __SCREAMING_SNAKE_CASE : int = False def __call__( self : Optional[Any] , *_A : List[Any] , **_A : Tuple ): """simple docstring""" if self._in_target_context_manager: return self.current_processor(*_A , **_A ) if "raw_speech" in kwargs: warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.''' ) __SCREAMING_SNAKE_CASE : List[Any] = kwargs.pop('''raw_speech''' ) else: __SCREAMING_SNAKE_CASE : List[Any] = kwargs.pop('''audio''' , _A ) __SCREAMING_SNAKE_CASE : str = kwargs.pop('''sampling_rate''' , _A ) __SCREAMING_SNAKE_CASE : Tuple = kwargs.pop('''text''' , _A ) if len(_A ) > 0: __SCREAMING_SNAKE_CASE : Tuple = args[0] __SCREAMING_SNAKE_CASE : int = args[1:] if audio is None and text is None: raise ValueError('''You need to specify either an `audio` or `text` input to process.''' ) if audio is not None: __SCREAMING_SNAKE_CASE : List[str] = self.feature_extractor(_A , *_A , sampling_rate=_A , **_A ) if text is not None: __SCREAMING_SNAKE_CASE : Optional[Any] = self.tokenizer(_A , **_A ) if text is None: return inputs elif audio is None: return encodings else: __SCREAMING_SNAKE_CASE : Dict = encodings['''input_ids'''] return inputs def UpperCAmelCase__ ( self : List[Any] , *_A : Any , **_A : int ): """simple docstring""" return self.tokenizer.batch_decode(*_A , **_A ) def UpperCAmelCase__ ( self : Union[str, Any] , *_A : Optional[Any] , **_A : List[Any] ): """simple docstring""" return self.tokenizer.decode(*_A , **_A ) @contextmanager def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your audio inputs, or in a separate call.''' ) __SCREAMING_SNAKE_CASE : Optional[int] = True __SCREAMING_SNAKE_CASE : Union[str, Any] = self.tokenizer yield __SCREAMING_SNAKE_CASE : Optional[int] = self.feature_extractor __SCREAMING_SNAKE_CASE : int = False
303
def a__ ( snake_case = 1_000_000 ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = 1 __SCREAMING_SNAKE_CASE : Optional[Any] = 1 __SCREAMING_SNAKE_CASE : Optional[int] = {1: 1} for inputa in range(2 , snake_case ): __SCREAMING_SNAKE_CASE : Tuple = 0 __SCREAMING_SNAKE_CASE : Optional[Any] = inputa while True: if number in counters: counter += counters[number] break if number % 2 == 0: number //= 2 counter += 1 else: __SCREAMING_SNAKE_CASE : List[Any] = (3 * number) + 1 counter += 1 if inputa not in counters: __SCREAMING_SNAKE_CASE : str = counter if counter > pre_counter: __SCREAMING_SNAKE_CASE : Optional[int] = inputa __SCREAMING_SNAKE_CASE : str = counter return largest_number if __name__ == "__main__": print(solution(int(input().strip())))
303
1
import unittest from transformers import LiltConfig, 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 ( LiltForQuestionAnswering, LiltForSequenceClassification, LiltForTokenClassification, LiltModel, ) from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST class __UpperCamelCase : """simple docstring""" def __init__( self : int , _A : Union[str, Any] , _A : int=13 , _A : List[Any]=7 , _A : int=True , _A : Tuple=True , _A : Optional[Any]=True , _A : Any=True , _A : List[Any]=99 , _A : Any=24 , _A : str=2 , _A : Any=6 , _A : Optional[Any]=37 , _A : int="gelu" , _A : List[str]=0.1 , _A : Tuple=0.1 , _A : Tuple=512 , _A : Union[str, Any]=16 , _A : Any=2 , _A : str=0.02 , _A : Tuple=3 , _A : Any=None , _A : Dict=1000 , ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = parent __SCREAMING_SNAKE_CASE : Optional[int] = batch_size __SCREAMING_SNAKE_CASE : Union[str, Any] = seq_length __SCREAMING_SNAKE_CASE : Tuple = is_training __SCREAMING_SNAKE_CASE : int = use_input_mask __SCREAMING_SNAKE_CASE : Optional[int] = use_token_type_ids __SCREAMING_SNAKE_CASE : Optional[Any] = use_labels __SCREAMING_SNAKE_CASE : List[str] = vocab_size __SCREAMING_SNAKE_CASE : str = hidden_size __SCREAMING_SNAKE_CASE : Optional[Any] = num_hidden_layers __SCREAMING_SNAKE_CASE : List[str] = num_attention_heads __SCREAMING_SNAKE_CASE : Optional[int] = intermediate_size __SCREAMING_SNAKE_CASE : List[str] = hidden_act __SCREAMING_SNAKE_CASE : Dict = hidden_dropout_prob __SCREAMING_SNAKE_CASE : Any = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : Tuple = max_position_embeddings __SCREAMING_SNAKE_CASE : Dict = type_vocab_size __SCREAMING_SNAKE_CASE : List[str] = type_sequence_label_size __SCREAMING_SNAKE_CASE : str = initializer_range __SCREAMING_SNAKE_CASE : Optional[Any] = num_labels __SCREAMING_SNAKE_CASE : Dict = scope __SCREAMING_SNAKE_CASE : List[Any] = range_bbox def UpperCAmelCase__ ( self : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox ) # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: __SCREAMING_SNAKE_CASE : str = bbox[i, j, 3] __SCREAMING_SNAKE_CASE : Dict = bbox[i, j, 1] __SCREAMING_SNAKE_CASE : Dict = t if bbox[i, j, 2] < bbox[i, j, 0]: __SCREAMING_SNAKE_CASE : Dict = bbox[i, j, 2] __SCREAMING_SNAKE_CASE : Any = bbox[i, j, 0] __SCREAMING_SNAKE_CASE : Optional[Any] = t __SCREAMING_SNAKE_CASE : Dict = None if self.use_input_mask: __SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) __SCREAMING_SNAKE_CASE : Union[str, Any] = None if self.use_token_type_ids: __SCREAMING_SNAKE_CASE : str = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __SCREAMING_SNAKE_CASE : int = None __SCREAMING_SNAKE_CASE : int = None if self.use_labels: __SCREAMING_SNAKE_CASE : List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __SCREAMING_SNAKE_CASE : int = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __SCREAMING_SNAKE_CASE : Any = self.get_config() return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels def UpperCAmelCase__ ( self : Dict ): """simple docstring""" return LiltConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) def UpperCAmelCase__ ( self : List[Any] , _A : int , _A : Optional[int] , _A : Any , _A : Union[str, Any] , _A : int , _A : Optional[Any] , _A : List[Any] , ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = LiltModel(config=_A ) model.to(_A ) model.eval() __SCREAMING_SNAKE_CASE : int = model(_A , bbox=_A , attention_mask=_A , token_type_ids=_A ) __SCREAMING_SNAKE_CASE : Dict = model(_A , bbox=_A , token_type_ids=_A ) __SCREAMING_SNAKE_CASE : List[Any] = model(_A , bbox=_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase__ ( self : List[str] , _A : List[str] , _A : List[str] , _A : List[str] , _A : Optional[int] , _A : Optional[int] , _A : Optional[Any] , _A : Optional[int] , ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = self.num_labels __SCREAMING_SNAKE_CASE : Optional[Any] = LiltForTokenClassification(config=_A ) model.to(_A ) model.eval() __SCREAMING_SNAKE_CASE : Union[str, Any] = model( _A , bbox=_A , attention_mask=_A , token_type_ids=_A , labels=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCAmelCase__ ( self : List[str] , _A : List[Any] , _A : Optional[int] , _A : Dict , _A : List[str] , _A : Tuple , _A : List[str] , _A : List[Any] , ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = LiltForQuestionAnswering(config=_A ) model.to(_A ) model.eval() __SCREAMING_SNAKE_CASE : Optional[Any] = model( _A , bbox=_A , attention_mask=_A , token_type_ids=_A , start_positions=_A , end_positions=_A , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCAmelCase__ ( self : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = self.prepare_config_and_inputs() ( ( __SCREAMING_SNAKE_CASE ), ( __SCREAMING_SNAKE_CASE ), ( __SCREAMING_SNAKE_CASE ), ( __SCREAMING_SNAKE_CASE ), ( __SCREAMING_SNAKE_CASE ), ( __SCREAMING_SNAKE_CASE ), ( __SCREAMING_SNAKE_CASE ), ) : List[str] = config_and_inputs __SCREAMING_SNAKE_CASE : List[Any] = { '''input_ids''': input_ids, '''bbox''': bbox, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask, } return config, inputs_dict @require_torch class __UpperCamelCase ( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , unittest.TestCase ): """simple docstring""" lowerCAmelCase_ = ( ( LiltModel, LiltForSequenceClassification, LiltForTokenClassification, LiltForQuestionAnswering, ) if is_torch_available() else () ) lowerCAmelCase_ = ( { '''feature-extraction''': LiltModel, '''question-answering''': LiltForQuestionAnswering, '''text-classification''': LiltForSequenceClassification, '''token-classification''': LiltForTokenClassification, '''zero-shot''': LiltForSequenceClassification, } if is_torch_available() else {} ) lowerCAmelCase_ = False lowerCAmelCase_ = False def UpperCAmelCase__ ( self : int , _A : Optional[int] , _A : Any , _A : Union[str, Any] , _A : Tuple , _A : Optional[int] ): """simple docstring""" return True def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = LiltModelTester(self ) __SCREAMING_SNAKE_CASE : Optional[int] = ConfigTester(self , config_class=_A , hidden_size=37 ) def UpperCAmelCase__ ( self : Any ): """simple docstring""" self.config_tester.run_common_tests() def UpperCAmelCase__ ( self : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_A ) def UpperCAmelCase__ ( self : Optional[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: __SCREAMING_SNAKE_CASE : Optional[int] = type self.model_tester.create_and_check_model(*_A ) def UpperCAmelCase__ ( self : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_A ) def UpperCAmelCase__ ( self : List[str] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_A ) @slow def UpperCAmelCase__ ( self : Dict ): """simple docstring""" for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __SCREAMING_SNAKE_CASE : Optional[int] = LiltModel.from_pretrained(_A ) self.assertIsNotNone(_A ) @require_torch @slow class __UpperCamelCase ( unittest.TestCase ): """simple docstring""" def UpperCAmelCase__ ( self : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base''' ).to(_A ) __SCREAMING_SNAKE_CASE : int = torch.tensor([[1, 2]] , device=_A ) __SCREAMING_SNAKE_CASE : List[str] = torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]] , device=_A ) # forward pass with torch.no_grad(): __SCREAMING_SNAKE_CASE : Union[str, Any] = model(input_ids=_A , bbox=_A ) __SCREAMING_SNAKE_CASE : Any = torch.Size([1, 2, 768] ) __SCREAMING_SNAKE_CASE : Dict = torch.tensor( [[-0.06_53, 0.09_50, -0.00_61], [-0.05_45, 0.09_26, -0.03_24]] , device=_A , ) self.assertTrue(outputs.last_hidden_state.shape , _A ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :, :3] , _A , atol=1e-3 ) )
303
import argparse import json import os import pickle import shutil import numpy as np import torch from distiller import Distiller from lm_seqs_dataset import LmSeqsDataset from transformers import ( BertConfig, BertForMaskedLM, BertTokenizer, DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer, GPTaConfig, GPTaLMHeadModel, GPTaTokenizer, RobertaConfig, RobertaForMaskedLM, RobertaTokenizer, ) from utils import git_log, init_gpu_params, logger, set_seed lowercase_ = { """distilbert""": (DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer), """roberta""": (RobertaConfig, RobertaForMaskedLM, RobertaTokenizer), """bert""": (BertConfig, BertForMaskedLM, BertTokenizer), """gpt2""": (GPTaConfig, GPTaLMHeadModel, GPTaTokenizer), } def a__ ( snake_case ): """simple docstring""" assert (args.mlm and args.alpha_mlm > 0.0) or (not args.mlm and args.alpha_mlm == 0.0) assert (args.alpha_mlm > 0.0 and args.alpha_clm == 0.0) or (args.alpha_mlm == 0.0 and args.alpha_clm > 0.0) if args.mlm: assert os.path.isfile(args.token_counts ) assert (args.student_type in ["roberta", "distilbert"]) and (args.teacher_type in ["roberta", "bert"]) else: assert (args.student_type in ["gpt2"]) and (args.teacher_type in ["gpt2"]) assert args.teacher_type == args.student_type or ( args.student_type == "distilbert" and args.teacher_type == "bert" ) assert os.path.isfile(args.student_config ) if args.student_pretrained_weights is not None: assert os.path.isfile(args.student_pretrained_weights ) if args.freeze_token_type_embds: assert args.student_type in ["roberta"] assert args.alpha_ce >= 0.0 assert args.alpha_mlm >= 0.0 assert args.alpha_clm >= 0.0 assert args.alpha_mse >= 0.0 assert args.alpha_cos >= 0.0 assert args.alpha_ce + args.alpha_mlm + args.alpha_clm + args.alpha_mse + args.alpha_cos > 0.0 def a__ ( snake_case , snake_case ): """simple docstring""" if args.student_type == "roberta": __SCREAMING_SNAKE_CASE : int = False elif args.student_type == "gpt2": __SCREAMING_SNAKE_CASE : Optional[int] = False def a__ ( snake_case , snake_case ): """simple docstring""" if args.student_type == "roberta": __SCREAMING_SNAKE_CASE : Dict = False def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = argparse.ArgumentParser(description='''Training''' ) parser.add_argument('''--force''' , action='''store_true''' , help='''Overwrite dump_path if it already exists.''' ) parser.add_argument( '''--dump_path''' , type=snake_case , required=snake_case , help='''The output directory (log, checkpoints, parameters, etc.)''' ) parser.add_argument( '''--data_file''' , type=snake_case , required=snake_case , help='''The binarized file (tokenized + tokens_to_ids) and grouped by sequence.''' , ) parser.add_argument( '''--student_type''' , type=snake_case , choices=['''distilbert''', '''roberta''', '''gpt2'''] , required=snake_case , help='''The student type (DistilBERT, RoBERTa).''' , ) parser.add_argument('''--student_config''' , type=snake_case , required=snake_case , help='''Path to the student configuration.''' ) parser.add_argument( '''--student_pretrained_weights''' , default=snake_case , type=snake_case , help='''Load student initialization checkpoint.''' ) parser.add_argument( '''--teacher_type''' , choices=['''bert''', '''roberta''', '''gpt2'''] , required=snake_case , help='''Teacher type (BERT, RoBERTa).''' ) parser.add_argument('''--teacher_name''' , type=snake_case , required=snake_case , help='''The teacher model.''' ) parser.add_argument('''--temperature''' , default=2.0 , type=snake_case , help='''Temperature for the softmax temperature.''' ) parser.add_argument( '''--alpha_ce''' , default=0.5 , type=snake_case , help='''Linear weight for the distillation loss. Must be >=0.''' ) parser.add_argument( '''--alpha_mlm''' , default=0.0 , type=snake_case , help='''Linear weight for the MLM loss. Must be >=0. Should be used in conjunction with `mlm` flag.''' , ) parser.add_argument('''--alpha_clm''' , default=0.5 , type=snake_case , help='''Linear weight for the CLM loss. Must be >=0.''' ) parser.add_argument('''--alpha_mse''' , default=0.0 , type=snake_case , help='''Linear weight of the MSE loss. Must be >=0.''' ) parser.add_argument( '''--alpha_cos''' , default=0.0 , type=snake_case , help='''Linear weight of the cosine embedding loss. Must be >=0.''' ) parser.add_argument( '''--mlm''' , action='''store_true''' , help='''The LM step: MLM or CLM. If `mlm` is True, the MLM is used over CLM.''' ) parser.add_argument( '''--mlm_mask_prop''' , default=0.15 , type=snake_case , help='''Proportion of tokens for which we need to make a prediction.''' , ) parser.add_argument('''--word_mask''' , default=0.8 , type=snake_case , help='''Proportion of tokens to mask out.''' ) parser.add_argument('''--word_keep''' , default=0.1 , type=snake_case , help='''Proportion of tokens to keep.''' ) parser.add_argument('''--word_rand''' , default=0.1 , type=snake_case , help='''Proportion of tokens to randomly replace.''' ) parser.add_argument( '''--mlm_smoothing''' , default=0.7 , type=snake_case , help='''Smoothing parameter to emphasize more rare tokens (see XLM, similar to word2vec).''' , ) parser.add_argument('''--token_counts''' , type=snake_case , help='''The token counts in the data_file for MLM.''' ) parser.add_argument( '''--restrict_ce_to_mask''' , action='''store_true''' , help='''If true, compute the distillation loss only the [MLM] prediction distribution.''' , ) parser.add_argument( '''--freeze_pos_embs''' , action='''store_true''' , help='''Freeze positional embeddings during distillation. For student_type in [\'roberta\', \'gpt2\'] only.''' , ) parser.add_argument( '''--freeze_token_type_embds''' , action='''store_true''' , help='''Freeze token type embeddings during distillation if existent. For student_type in [\'roberta\'] only.''' , ) parser.add_argument('''--n_epoch''' , type=snake_case , default=3 , help='''Number of pass on the whole dataset.''' ) parser.add_argument('''--batch_size''' , type=snake_case , default=5 , help='''Batch size (for each process).''' ) parser.add_argument( '''--group_by_size''' , action='''store_false''' , help='''If true, group sequences that have similar length into the same batch. Default is true.''' , ) parser.add_argument( '''--gradient_accumulation_steps''' , type=snake_case , default=50 , help='''Gradient accumulation for larger training batches.''' , ) parser.add_argument('''--warmup_prop''' , default=0.05 , type=snake_case , help='''Linear warmup proportion.''' ) parser.add_argument('''--weight_decay''' , default=0.0 , type=snake_case , help='''Weight decay if we apply some.''' ) parser.add_argument('''--learning_rate''' , default=5E-4 , type=snake_case , help='''The initial learning rate for Adam.''' ) parser.add_argument('''--adam_epsilon''' , default=1E-6 , type=snake_case , help='''Epsilon for Adam optimizer.''' ) parser.add_argument('''--max_grad_norm''' , default=5.0 , type=snake_case , help='''Max gradient norm.''' ) parser.add_argument('''--initializer_range''' , default=0.02 , type=snake_case , help='''Random initialization range.''' ) parser.add_argument( '''--fp16''' , action='''store_true''' , help='''Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit''' , ) parser.add_argument( '''--fp16_opt_level''' , type=snake_case , default='''O1''' , help=( '''For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\'].''' '''See details at https://nvidia.github.io/apex/amp.html''' ) , ) parser.add_argument('''--n_gpu''' , type=snake_case , default=1 , help='''Number of GPUs in the node.''' ) parser.add_argument('''--local_rank''' , type=snake_case , default=-1 , help='''Distributed training - Local rank''' ) parser.add_argument('''--seed''' , type=snake_case , default=56 , help='''Random seed''' ) parser.add_argument('''--log_interval''' , type=snake_case , default=500 , help='''Tensorboard logging interval.''' ) parser.add_argument('''--checkpoint_interval''' , type=snake_case , default=4_000 , help='''Checkpoint interval.''' ) __SCREAMING_SNAKE_CASE : Optional[int] = parser.parse_args() sanity_checks(snake_case ) # ARGS # init_gpu_params(snake_case ) set_seed(snake_case ) if args.is_master: if os.path.exists(args.dump_path ): if not args.force: raise ValueError( F'''Serialization dir {args.dump_path} already exists, but you have not precised wheter to overwrite''' ''' itUse `--force` if you want to overwrite it''' ) else: shutil.rmtree(args.dump_path ) if not os.path.exists(args.dump_path ): os.makedirs(args.dump_path ) logger.info(F'''Experiment will be dumped and logged in {args.dump_path}''' ) # SAVE PARAMS # logger.info(F'''Param: {args}''' ) with open(os.path.join(args.dump_path , '''parameters.json''' ) , '''w''' ) as f: json.dump(vars(snake_case ) , snake_case , indent=4 ) git_log(args.dump_path ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = MODEL_CLASSES[args.student_type] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = MODEL_CLASSES[args.teacher_type] # TOKENIZER # __SCREAMING_SNAKE_CASE : Optional[int] = teacher_tokenizer_class.from_pretrained(args.teacher_name ) __SCREAMING_SNAKE_CASE : Optional[Any] = {} for tok_name, tok_symbol in tokenizer.special_tokens_map.items(): __SCREAMING_SNAKE_CASE : Any = tokenizer.all_special_tokens.index(snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.all_special_ids[idx] logger.info(F'''Special tokens {special_tok_ids}''' ) __SCREAMING_SNAKE_CASE : Any = special_tok_ids __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.max_model_input_sizes[args.teacher_name] # DATA LOADER # logger.info(F'''Loading data from {args.data_file}''' ) with open(args.data_file , '''rb''' ) as fp: __SCREAMING_SNAKE_CASE : List[str] = pickle.load(snake_case ) if args.mlm: logger.info(F'''Loading token counts from {args.token_counts} (already pre-computed)''' ) with open(args.token_counts , '''rb''' ) as fp: __SCREAMING_SNAKE_CASE : Optional[Any] = pickle.load(snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = np.maximum(snake_case , 1 ) ** -args.mlm_smoothing for idx in special_tok_ids.values(): __SCREAMING_SNAKE_CASE : Any = 0.0 # do not predict special tokens __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.from_numpy(snake_case ) else: __SCREAMING_SNAKE_CASE : Optional[int] = None __SCREAMING_SNAKE_CASE : Optional[Any] = LmSeqsDataset(params=snake_case , data=snake_case ) logger.info('''Data loader created.''' ) # STUDENT # logger.info(F'''Loading student config from {args.student_config}''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = student_config_class.from_pretrained(args.student_config ) __SCREAMING_SNAKE_CASE : Dict = True if args.student_pretrained_weights is not None: logger.info(F'''Loading pretrained weights from {args.student_pretrained_weights}''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = student_model_class.from_pretrained(args.student_pretrained_weights , config=snake_case ) else: __SCREAMING_SNAKE_CASE : str = student_model_class(snake_case ) if args.n_gpu > 0: student.to(F'''cuda:{args.local_rank}''' ) logger.info('''Student loaded.''' ) # TEACHER # __SCREAMING_SNAKE_CASE : List[str] = teacher_model_class.from_pretrained(args.teacher_name , output_hidden_states=snake_case ) if args.n_gpu > 0: teacher.to(F'''cuda:{args.local_rank}''' ) logger.info(F'''Teacher loaded from {args.teacher_name}.''' ) # FREEZING # if args.freeze_pos_embs: freeze_pos_embeddings(snake_case , snake_case ) if args.freeze_token_type_embds: freeze_token_type_embeddings(snake_case , snake_case ) # SANITY CHECKS # assert student.config.vocab_size == teacher.config.vocab_size assert student.config.hidden_size == teacher.config.hidden_size assert student.config.max_position_embeddings == teacher.config.max_position_embeddings if args.mlm: assert token_probs.size(0 ) == stu_architecture_config.vocab_size # DISTILLER # torch.cuda.empty_cache() __SCREAMING_SNAKE_CASE : int = Distiller( params=snake_case , dataset=snake_case , token_probs=snake_case , student=snake_case , teacher=snake_case ) distiller.train() logger.info('''Let\'s go get some drinks.''' ) if __name__ == "__main__": main()
303
1
class __UpperCamelCase : """simple docstring""" def __init__( self : str , _A : str = "" , _A : bool = False ): """simple docstring""" __SCREAMING_SNAKE_CASE : dict[str, RadixNode] = {} # A node will be a leaf if the tree contains its word __SCREAMING_SNAKE_CASE : Optional[Any] = is_leaf __SCREAMING_SNAKE_CASE : str = prefix def UpperCAmelCase__ ( self : Optional[Any] , _A : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = 0 for q, w in zip(self.prefix , _A ): if q != w: break x += 1 return self.prefix[:x], self.prefix[x:], word[x:] def UpperCAmelCase__ ( self : List[Any] , _A : list[str] ): """simple docstring""" for word in words: self.insert(_A ) def UpperCAmelCase__ ( self : Optional[int] , _A : str ): """simple docstring""" if self.prefix == word: __SCREAMING_SNAKE_CASE : Tuple = True # Case 2: The node has no edges that have a prefix to the word # Solution: We create an edge from the current node to a new one # containing the word elif word[0] not in self.nodes: __SCREAMING_SNAKE_CASE : str = RadixNode(prefix=_A , is_leaf=_A ) else: __SCREAMING_SNAKE_CASE : Dict = self.nodes[word[0]] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Union[str, Any] = incoming_node.match( _A ) # Case 3: The node prefix is equal to the matching # Solution: We insert remaining word on the next node if remaining_prefix == "": self.nodes[matching_string[0]].insert(_A ) # Case 4: The word is greater equal to the matching # Solution: Create a node in between both nodes, change # prefixes and add the new node for the remaining word else: __SCREAMING_SNAKE_CASE : List[str] = remaining_prefix __SCREAMING_SNAKE_CASE : List[str] = self.nodes[matching_string[0]] __SCREAMING_SNAKE_CASE : str = RadixNode(_A , _A ) __SCREAMING_SNAKE_CASE : int = aux_node if remaining_word == "": __SCREAMING_SNAKE_CASE : Optional[Any] = True else: self.nodes[matching_string[0]].insert(_A ) def UpperCAmelCase__ ( self : Any , _A : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = self.nodes.get(word[0] , _A ) if not incoming_node: return False else: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : int = incoming_node.match( _A ) # If there is remaining prefix, the word can't be on the tree if remaining_prefix != "": return False # This applies when the word and the prefix are equal elif remaining_word == "": return incoming_node.is_leaf # We have word remaining so we check the next node else: return incoming_node.find(_A ) def UpperCAmelCase__ ( self : Optional[int] , _A : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = self.nodes.get(word[0] , _A ) if not incoming_node: return False else: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[Any] = incoming_node.match( _A ) # If there is remaining prefix, the word can't be on the tree if remaining_prefix != "": return False # We have word remaining so we check the next node elif remaining_word != "": return incoming_node.delete(_A ) else: # If it is not a leaf, we don't have to delete if not incoming_node.is_leaf: return False else: # We delete the nodes if no edges go from it if len(incoming_node.nodes ) == 0: del self.nodes[word[0]] # We merge the current node with its only child if len(self.nodes ) == 1 and not self.is_leaf: __SCREAMING_SNAKE_CASE : Optional[Any] = list(self.nodes.values() )[0] __SCREAMING_SNAKE_CASE : Tuple = merging_node.is_leaf self.prefix += merging_node.prefix __SCREAMING_SNAKE_CASE : List[Any] = merging_node.nodes # If there is more than 1 edge, we just mark it as non-leaf elif len(incoming_node.nodes ) > 1: __SCREAMING_SNAKE_CASE : str = False # If there is 1 edge, we merge it with its child else: __SCREAMING_SNAKE_CASE : Union[str, Any] = list(incoming_node.nodes.values() )[0] __SCREAMING_SNAKE_CASE : Dict = merging_node.is_leaf incoming_node.prefix += merging_node.prefix __SCREAMING_SNAKE_CASE : Dict = merging_node.nodes return True def UpperCAmelCase__ ( self : List[str] , _A : int = 0 ): """simple docstring""" if self.prefix != "": print('''-''' * height , self.prefix , ''' (leaf)''' if self.is_leaf else '''''' ) for value in self.nodes.values(): value.print_tree(height + 1 ) def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = '''banana bananas bandana band apple all beast'''.split() __SCREAMING_SNAKE_CASE : Optional[Any] = RadixNode() root.insert_many(snake_case ) assert all(root.find(snake_case ) for word in words ) assert not root.find('''bandanas''' ) assert not root.find('''apps''' ) root.delete('''all''' ) assert not root.find('''all''' ) root.delete('''banana''' ) assert not root.find('''banana''' ) assert root.find('''bananas''' ) return True def a__ ( ): """simple docstring""" assert test_trie() def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = RadixNode() __SCREAMING_SNAKE_CASE : int = '''banana bananas bandanas bandana band apple all beast'''.split() root.insert_many(snake_case ) print('''Words:''' , snake_case ) print('''Tree:''' ) root.print_tree() if __name__ == "__main__": main()
303
import math import os import sys def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = '''''' try: with open(snake_case , '''rb''' ) as binary_file: __SCREAMING_SNAKE_CASE : int = binary_file.read() for dat in data: __SCREAMING_SNAKE_CASE : Optional[Any] = F'''{dat:08b}''' result += curr_byte return result except OSError: print('''File not accessible''' ) sys.exit() def a__ ( snake_case , snake_case , snake_case , snake_case ): """simple docstring""" lexicon.pop(snake_case ) __SCREAMING_SNAKE_CASE : List[str] = last_match_id if math.loga(snake_case ).is_integer(): for curr_key in lexicon: __SCREAMING_SNAKE_CASE : int = '''0''' + lexicon[curr_key] __SCREAMING_SNAKE_CASE : List[str] = bin(snake_case )[2:] def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = {'''0''': '''0''', '''1''': '''1'''} __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Any = '''''', '''''' __SCREAMING_SNAKE_CASE : Optional[Any] = len(snake_case ) for i in range(len(snake_case ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue __SCREAMING_SNAKE_CASE : Any = lexicon[curr_string] result += last_match_id add_key_to_lexicon(snake_case , snake_case , snake_case , snake_case ) index += 1 __SCREAMING_SNAKE_CASE : Tuple = '''''' while curr_string != "" and curr_string not in lexicon: curr_string += "0" if curr_string != "": __SCREAMING_SNAKE_CASE : Dict = lexicon[curr_string] result += last_match_id return result def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = os.path.getsize(snake_case ) __SCREAMING_SNAKE_CASE : Union[str, Any] = bin(snake_case )[2:] __SCREAMING_SNAKE_CASE : int = len(snake_case ) return "0" * (length_length - 1) + file_length_binary + compressed def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = 8 try: with open(snake_case , '''wb''' ) as opened_file: __SCREAMING_SNAKE_CASE : Optional[int] = [ to_write[i : i + byte_length] for i in range(0 , len(snake_case ) , snake_case ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append('''10000000''' ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array: opened_file.write(int(snake_case , 2 ).to_bytes(1 , byteorder='''big''' ) ) except OSError: print('''File not accessible''' ) sys.exit() def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = read_file_binary(snake_case ) __SCREAMING_SNAKE_CASE : Optional[int] = compress_data(snake_case ) __SCREAMING_SNAKE_CASE : Dict = add_file_length(snake_case , snake_case ) write_file_binary(snake_case , snake_case ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
303
1
import argparse import json from collections import OrderedDict from functools import partial from pathlib import Path import timm import torch from huggingface_hub import hf_hub_download from transformers import LevitConfig, LevitForImageClassificationWithTeacher, LevitImageProcessor from transformers.utils import logging logging.set_verbosity_info() lowercase_ = logging.get_logger() def a__ ( snake_case , snake_case , snake_case , snake_case , snake_case = True ): """simple docstring""" print(F'''Converting {name}...''' ) with torch.no_grad(): if hidden_sizes == 128: if name[-1] == "S": __SCREAMING_SNAKE_CASE : Optional[Any] = timm.create_model('''levit_128s''' , pretrained=snake_case ) else: __SCREAMING_SNAKE_CASE : Union[str, Any] = timm.create_model('''levit_128''' , pretrained=snake_case ) if hidden_sizes == 192: __SCREAMING_SNAKE_CASE : int = timm.create_model('''levit_192''' , pretrained=snake_case ) if hidden_sizes == 256: __SCREAMING_SNAKE_CASE : Optional[int] = timm.create_model('''levit_256''' , pretrained=snake_case ) if hidden_sizes == 384: __SCREAMING_SNAKE_CASE : str = timm.create_model('''levit_384''' , pretrained=snake_case ) from_model.eval() __SCREAMING_SNAKE_CASE : str = LevitForImageClassificationWithTeacher(snake_case ).eval() __SCREAMING_SNAKE_CASE : Union[str, Any] = OrderedDict() __SCREAMING_SNAKE_CASE : List[str] = from_model.state_dict() __SCREAMING_SNAKE_CASE : Tuple = list(from_model.state_dict().keys() ) __SCREAMING_SNAKE_CASE : Any = list(our_model.state_dict().keys() ) print(len(snake_case ) , len(snake_case ) ) for i in range(len(snake_case ) ): __SCREAMING_SNAKE_CASE : str = weights[og_keys[i]] our_model.load_state_dict(snake_case ) __SCREAMING_SNAKE_CASE : Dict = torch.randn((2, 3, 224, 224) ) __SCREAMING_SNAKE_CASE : Dict = from_model(snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = our_model(snake_case ).logits assert torch.allclose(snake_case , snake_case ), "The model logits don't match the original one." __SCREAMING_SNAKE_CASE : List[str] = name print(snake_case ) if push_to_hub: our_model.save_pretrained(save_directory / checkpoint_name ) __SCREAMING_SNAKE_CASE : List[str] = LevitImageProcessor() image_processor.save_pretrained(save_directory / checkpoint_name ) print(F'''Pushed {checkpoint_name}''' ) def a__ ( snake_case , snake_case = None , snake_case = True ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = '''imagenet-1k-id2label.json''' __SCREAMING_SNAKE_CASE : List[Any] = 1_000 __SCREAMING_SNAKE_CASE : int = (1, num_labels) __SCREAMING_SNAKE_CASE : Optional[Any] = '''huggingface/label-files''' __SCREAMING_SNAKE_CASE : Optional[Any] = num_labels __SCREAMING_SNAKE_CASE : Optional[int] = json.load(open(hf_hub_download(snake_case , snake_case , repo_type='''dataset''' ) , '''r''' ) ) __SCREAMING_SNAKE_CASE : Optional[int] = {int(snake_case ): v for k, v in idalabel.items()} __SCREAMING_SNAKE_CASE : Optional[int] = idalabel __SCREAMING_SNAKE_CASE : Optional[int] = {v: k for k, v in idalabel.items()} __SCREAMING_SNAKE_CASE : Any = partial(snake_case , num_labels=snake_case , idalabel=snake_case , labelaid=snake_case ) __SCREAMING_SNAKE_CASE : Any = { '''levit-128S''': 128, '''levit-128''': 128, '''levit-192''': 192, '''levit-256''': 256, '''levit-384''': 384, } __SCREAMING_SNAKE_CASE : List[Any] = { '''levit-128S''': ImageNetPreTrainedConfig( hidden_sizes=[128, 256, 384] , num_attention_heads=[4, 6, 8] , depths=[2, 3, 4] , key_dim=[16, 16, 16] , drop_path_rate=0 , ), '''levit-128''': ImageNetPreTrainedConfig( hidden_sizes=[128, 256, 384] , num_attention_heads=[4, 8, 12] , depths=[4, 4, 4] , key_dim=[16, 16, 16] , drop_path_rate=0 , ), '''levit-192''': ImageNetPreTrainedConfig( hidden_sizes=[192, 288, 384] , num_attention_heads=[3, 5, 6] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0 , ), '''levit-256''': ImageNetPreTrainedConfig( hidden_sizes=[256, 384, 512] , num_attention_heads=[4, 6, 8] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0 , ), '''levit-384''': ImageNetPreTrainedConfig( hidden_sizes=[384, 512, 768] , num_attention_heads=[6, 9, 12] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0.1 , ), } if model_name: convert_weight_and_push( names_to_hidden_sizes[model_name] , snake_case , names_to_config[model_name] , snake_case , snake_case ) else: for model_name, config in names_to_config.items(): convert_weight_and_push(names_to_hidden_sizes[model_name] , snake_case , snake_case , snake_case , snake_case ) return config, expected_shape if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default=None, type=str, help="""The name of the model you wish to convert, it must be one of the supported Levit* architecture,""", ) parser.add_argument( """--pytorch_dump_folder_path""", default="""levit-dump-folder/""", type=Path, required=False, help="""Path to the output PyTorch model directory.""", ) parser.add_argument("""--push_to_hub""", action="""store_true""", help="""Push model and image processor to the hub""") parser.add_argument( """--no-push_to_hub""", dest="""push_to_hub""", action="""store_false""", help="""Do not push model and image processor to the hub""", ) lowercase_ = parser.parse_args() lowercase_ = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
303
import unittest import numpy as np import torch from torch import nn from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import enable_full_determinism, skip_mps from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __UpperCamelCase ( lowerCAmelCase__ , unittest.TestCase ): """simple docstring""" lowerCAmelCase_ = KandinskyVaaPriorPipeline lowerCAmelCase_ = ['''prompt'''] lowerCAmelCase_ = ['''prompt''', '''negative_prompt'''] lowerCAmelCase_ = [ '''num_images_per_prompt''', '''generator''', '''num_inference_steps''', '''latents''', '''negative_prompt''', '''guidance_scale''', '''output_type''', '''return_dict''', ] lowerCAmelCase_ = False @property def UpperCAmelCase__ ( self : int ): """simple docstring""" return 32 @property def UpperCAmelCase__ ( self : List[str] ): """simple docstring""" return 32 @property def UpperCAmelCase__ ( self : Dict ): """simple docstring""" return self.time_input_dim @property def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" return self.time_input_dim * 4 @property def UpperCAmelCase__ ( self : List[Any] ): """simple docstring""" return 100 @property def UpperCAmelCase__ ( self : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) return tokenizer @property def UpperCAmelCase__ ( self : List[Any] ): """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : List[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(_A ) @property def UpperCAmelCase__ ( self : str ): """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : Dict = { '''num_attention_heads''': 2, '''attention_head_dim''': 12, '''embedding_dim''': self.text_embedder_hidden_size, '''num_layers''': 1, } __SCREAMING_SNAKE_CASE : Optional[Any] = PriorTransformer(**_A ) # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0 __SCREAMING_SNAKE_CASE : List[Any] = nn.Parameter(torch.ones(model.clip_std.shape ) ) return model @property def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : int = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , image_size=224 , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_channels=3 , num_hidden_layers=5 , patch_size=14 , ) __SCREAMING_SNAKE_CASE : str = CLIPVisionModelWithProjection(_A ) return model @property def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = CLIPImageProcessor( crop_size=224 , do_center_crop=_A , do_normalize=_A , do_resize=_A , image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73] , image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11] , resample=3 , size=224 , ) return image_processor def UpperCAmelCase__ ( self : Tuple ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = self.dummy_prior __SCREAMING_SNAKE_CASE : str = self.dummy_image_encoder __SCREAMING_SNAKE_CASE : Optional[int] = self.dummy_text_encoder __SCREAMING_SNAKE_CASE : List[Any] = self.dummy_tokenizer __SCREAMING_SNAKE_CASE : Union[str, Any] = self.dummy_image_processor __SCREAMING_SNAKE_CASE : str = UnCLIPScheduler( variance_type='''fixed_small_log''' , prediction_type='''sample''' , num_train_timesteps=1000 , clip_sample=_A , clip_sample_range=10.0 , ) __SCREAMING_SNAKE_CASE : int = { '''prior''': prior, '''image_encoder''': image_encoder, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''scheduler''': scheduler, '''image_processor''': image_processor, } return components def UpperCAmelCase__ ( self : Union[str, Any] , _A : int , _A : Dict=0 ): """simple docstring""" if str(_A ).startswith('''mps''' ): __SCREAMING_SNAKE_CASE : Tuple = torch.manual_seed(_A ) else: __SCREAMING_SNAKE_CASE : str = torch.Generator(device=_A ).manual_seed(_A ) __SCREAMING_SNAKE_CASE : List[str] = { '''prompt''': '''horse''', '''generator''': generator, '''guidance_scale''': 4.0, '''num_inference_steps''': 2, '''output_type''': '''np''', } return inputs def UpperCAmelCase__ ( self : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = '''cpu''' __SCREAMING_SNAKE_CASE : Any = self.get_dummy_components() __SCREAMING_SNAKE_CASE : Any = self.pipeline_class(**_A ) __SCREAMING_SNAKE_CASE : List[Any] = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) __SCREAMING_SNAKE_CASE : int = pipe(**self.get_dummy_inputs(_A ) ) __SCREAMING_SNAKE_CASE : Tuple = output.image_embeds __SCREAMING_SNAKE_CASE : Optional[Any] = pipe( **self.get_dummy_inputs(_A ) , return_dict=_A , )[0] __SCREAMING_SNAKE_CASE : Tuple = image[0, -10:] __SCREAMING_SNAKE_CASE : List[Any] = image_from_tuple[0, -10:] assert image.shape == (1, 32) __SCREAMING_SNAKE_CASE : List[str] = np.array( [-0.05_32, 1.71_20, 0.36_56, -1.08_52, -0.89_46, -1.17_56, 0.43_48, 0.24_82, 0.51_46, -0.11_56] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @skip_mps def UpperCAmelCase__ ( self : List[str] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = torch_device == '''cpu''' __SCREAMING_SNAKE_CASE : Any = True __SCREAMING_SNAKE_CASE : int = False self._test_inference_batch_single_identical( test_max_difference=_A , relax_max_difference=_A , test_mean_pixel_difference=_A , ) @skip_mps def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = torch_device == '''cpu''' __SCREAMING_SNAKE_CASE : List[Any] = False self._test_attention_slicing_forward_pass( test_max_difference=_A , test_mean_pixel_difference=_A , )
303
1
import copy import os from typing import TYPE_CHECKING, List, Union if TYPE_CHECKING: pass from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { """kakaobrain/align-base""": """https://huggingface.co/kakaobrain/align-base/resolve/main/config.json""", } class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''align_text_model''' def __init__( self : Optional[Any] , _A : str=3_0522 , _A : str=768 , _A : Optional[Any]=12 , _A : Union[str, Any]=12 , _A : List[str]=3072 , _A : Union[str, Any]="gelu" , _A : Optional[int]=0.1 , _A : Union[str, Any]=0.1 , _A : Optional[Any]=512 , _A : str=2 , _A : Optional[int]=0.02 , _A : Tuple=1e-12 , _A : Union[str, Any]=0 , _A : int="absolute" , _A : Dict=True , **_A : Optional[Any] , ): """simple docstring""" super().__init__(**_A ) __SCREAMING_SNAKE_CASE : List[str] = vocab_size __SCREAMING_SNAKE_CASE : List[str] = hidden_size __SCREAMING_SNAKE_CASE : Union[str, Any] = num_hidden_layers __SCREAMING_SNAKE_CASE : Optional[Any] = num_attention_heads __SCREAMING_SNAKE_CASE : Any = hidden_act __SCREAMING_SNAKE_CASE : Dict = intermediate_size __SCREAMING_SNAKE_CASE : List[str] = hidden_dropout_prob __SCREAMING_SNAKE_CASE : Dict = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : List[str] = max_position_embeddings __SCREAMING_SNAKE_CASE : List[Any] = type_vocab_size __SCREAMING_SNAKE_CASE : Union[str, Any] = initializer_range __SCREAMING_SNAKE_CASE : Union[str, Any] = layer_norm_eps __SCREAMING_SNAKE_CASE : List[str] = position_embedding_type __SCREAMING_SNAKE_CASE : str = use_cache __SCREAMING_SNAKE_CASE : List[str] = pad_token_id @classmethod def UpperCAmelCase__ ( cls : Dict , _A : Union[str, os.PathLike] , **_A : Optional[Any] ): """simple docstring""" cls._set_token_in_kwargs(_A ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Any = cls.get_config_dict(_A , **_A ) # get the text config dict if we are loading from AlignConfig if config_dict.get('''model_type''' ) == "align": __SCREAMING_SNAKE_CASE : List[Any] = config_dict['''text_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type ''' F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(_A , **_A ) class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''align_vision_model''' def __init__( self : List[str] , _A : int = 3 , _A : int = 600 , _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, 112, 192] , _A : List[int] = [16, 24, 40, 80, 112, 192, 320] , _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 = 2560 , _A : str = "mean" , _A : float = 0.02 , _A : float = 0.0_01 , _A : float = 0.99 , _A : float = 0.2 , **_A : Optional[int] , ): """simple docstring""" super().__init__(**_A ) __SCREAMING_SNAKE_CASE : Any = num_channels __SCREAMING_SNAKE_CASE : Any = image_size __SCREAMING_SNAKE_CASE : Dict = width_coefficient __SCREAMING_SNAKE_CASE : List[str] = depth_coefficient __SCREAMING_SNAKE_CASE : int = depth_divisor __SCREAMING_SNAKE_CASE : Optional[Any] = kernel_sizes __SCREAMING_SNAKE_CASE : str = in_channels __SCREAMING_SNAKE_CASE : Dict = out_channels __SCREAMING_SNAKE_CASE : Optional[Any] = depthwise_padding __SCREAMING_SNAKE_CASE : Tuple = strides __SCREAMING_SNAKE_CASE : List[str] = num_block_repeats __SCREAMING_SNAKE_CASE : Optional[int] = expand_ratios __SCREAMING_SNAKE_CASE : List[str] = squeeze_expansion_ratio __SCREAMING_SNAKE_CASE : Optional[Any] = hidden_act __SCREAMING_SNAKE_CASE : Optional[Any] = hidden_dim __SCREAMING_SNAKE_CASE : Any = pooling_type __SCREAMING_SNAKE_CASE : Union[str, Any] = initializer_range __SCREAMING_SNAKE_CASE : List[Any] = batch_norm_eps __SCREAMING_SNAKE_CASE : List[str] = batch_norm_momentum __SCREAMING_SNAKE_CASE : List[str] = drop_connect_rate __SCREAMING_SNAKE_CASE : int = sum(_A ) * 4 @classmethod def UpperCAmelCase__ ( cls : Any , _A : Union[str, os.PathLike] , **_A : int ): """simple docstring""" cls._set_token_in_kwargs(_A ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : List[Any] = cls.get_config_dict(_A , **_A ) # get the vision config dict if we are loading from AlignConfig if config_dict.get('''model_type''' ) == "align": __SCREAMING_SNAKE_CASE : Any = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type ''' F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(_A , **_A ) class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''align''' lowerCAmelCase_ = True def __init__( self : Union[str, Any] , _A : Any=None , _A : Any=None , _A : List[Any]=640 , _A : Union[str, Any]=1.0 , _A : Union[str, Any]=0.02 , **_A : Tuple , ): """simple docstring""" super().__init__(**_A ) if text_config is None: __SCREAMING_SNAKE_CASE : int = {} logger.info('''text_config is None. Initializing the AlignTextConfig with default values.''' ) if vision_config is None: __SCREAMING_SNAKE_CASE : List[str] = {} logger.info('''vision_config is None. Initializing the AlignVisionConfig with default values.''' ) __SCREAMING_SNAKE_CASE : List[str] = AlignTextConfig(**_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = AlignVisionConfig(**_A ) __SCREAMING_SNAKE_CASE : Tuple = projection_dim __SCREAMING_SNAKE_CASE : Optional[int] = temperature_init_value __SCREAMING_SNAKE_CASE : int = initializer_range @classmethod def UpperCAmelCase__ ( cls : List[Any] , _A : AlignTextConfig , _A : AlignVisionConfig , **_A : Union[str, Any] ): """simple docstring""" return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **_A ) def UpperCAmelCase__ ( self : int ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = copy.deepcopy(self.__dict__ ) __SCREAMING_SNAKE_CASE : Tuple = self.text_config.to_dict() __SCREAMING_SNAKE_CASE : int = self.vision_config.to_dict() __SCREAMING_SNAKE_CASE : List[str] = self.__class__.model_type return output
303
import argparse import logging import pickle import random import time import numpy as np from transformers import BertTokenizer, GPTaTokenizer, RobertaTokenizer logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""", datefmt="""%m/%d/%Y %H:%M:%S""", level=logging.INFO ) lowercase_ = logging.getLogger(__name__) def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = argparse.ArgumentParser( description='''Preprocess the data to avoid re-doing it several times by (tokenization + token_to_ids).''' ) parser.add_argument('''--file_path''' , type=snake_case , default='''data/dump.txt''' , help='''The path to the data.''' ) parser.add_argument('''--tokenizer_type''' , type=snake_case , default='''bert''' , choices=['''bert''', '''roberta''', '''gpt2'''] ) parser.add_argument('''--tokenizer_name''' , type=snake_case , default='''bert-base-uncased''' , help='''The tokenizer to use.''' ) parser.add_argument('''--dump_file''' , type=snake_case , default='''data/dump''' , help='''The dump file prefix.''' ) __SCREAMING_SNAKE_CASE : List[Any] = parser.parse_args() logger.info(F'''Loading Tokenizer ({args.tokenizer_name})''' ) if args.tokenizer_type == "bert": __SCREAMING_SNAKE_CASE : Union[str, Any] = BertTokenizer.from_pretrained(args.tokenizer_name ) __SCREAMING_SNAKE_CASE : List[str] = tokenizer.special_tokens_map['''cls_token'''] # `[CLS]` __SCREAMING_SNAKE_CASE : List[str] = tokenizer.special_tokens_map['''sep_token'''] # `[SEP]` elif args.tokenizer_type == "roberta": __SCREAMING_SNAKE_CASE : List[str] = RobertaTokenizer.from_pretrained(args.tokenizer_name ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.special_tokens_map['''cls_token'''] # `<s>` __SCREAMING_SNAKE_CASE : Optional[int] = tokenizer.special_tokens_map['''sep_token'''] # `</s>` elif args.tokenizer_type == "gpt2": __SCREAMING_SNAKE_CASE : Dict = GPTaTokenizer.from_pretrained(args.tokenizer_name ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.special_tokens_map['''bos_token'''] # `<|endoftext|>` __SCREAMING_SNAKE_CASE : str = tokenizer.special_tokens_map['''eos_token'''] # `<|endoftext|>` logger.info(F'''Loading text from {args.file_path}''' ) with open(args.file_path , '''r''' , encoding='''utf8''' ) as fp: __SCREAMING_SNAKE_CASE : str = fp.readlines() logger.info('''Start encoding''' ) logger.info(F'''{len(snake_case )} examples to process.''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = [] __SCREAMING_SNAKE_CASE : Dict = 0 __SCREAMING_SNAKE_CASE : List[str] = 10_000 __SCREAMING_SNAKE_CASE : Dict = time.time() for text in data: __SCREAMING_SNAKE_CASE : Optional[int] = F'''{bos} {text.strip()} {sep}''' __SCREAMING_SNAKE_CASE : Tuple = tokenizer.encode(snake_case , add_special_tokens=snake_case ) rslt.append(snake_case ) iter += 1 if iter % interval == 0: __SCREAMING_SNAKE_CASE : List[str] = time.time() logger.info(F'''{iter} examples processed. - {(end-start):.2f}s/{interval}expl''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = time.time() logger.info('''Finished binarization''' ) logger.info(F'''{len(snake_case )} examples processed.''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = F'''{args.dump_file}.{args.tokenizer_name}.pickle''' __SCREAMING_SNAKE_CASE : str = tokenizer.vocab_size if vocab_size < (1 << 16): __SCREAMING_SNAKE_CASE : List[str] = [np.uintaa(snake_case ) for d in rslt] else: __SCREAMING_SNAKE_CASE : Optional[int] = [np.intaa(snake_case ) for d in rslt] random.shuffle(rslt_ ) logger.info(F'''Dump to {dp_file}''' ) with open(snake_case , '''wb''' ) as handle: pickle.dump(rslt_ , snake_case , protocol=pickle.HIGHEST_PROTOCOL ) if __name__ == "__main__": main()
303
1
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING lowercase_ = logging.get_logger(__name__) lowercase_ = { """SenseTime/deformable-detr""": """https://huggingface.co/sensetime/deformable-detr/resolve/main/config.json""", # See all Deformable DETR models at https://huggingface.co/models?filter=deformable-detr } class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''deformable_detr''' lowerCAmelCase_ = { '''hidden_size''': '''d_model''', '''num_attention_heads''': '''encoder_attention_heads''', } def __init__( self : Optional[int] , _A : List[str]=True , _A : Union[str, Any]=None , _A : Optional[int]=3 , _A : Dict=300 , _A : List[str]=1024 , _A : Dict=6 , _A : Dict=1024 , _A : Tuple=8 , _A : Optional[int]=6 , _A : Optional[int]=1024 , _A : Any=8 , _A : Union[str, Any]=0.0 , _A : Dict=True , _A : Optional[Any]="relu" , _A : Tuple=256 , _A : int=0.1 , _A : Dict=0.0 , _A : Any=0.0 , _A : Optional[int]=0.02 , _A : Union[str, Any]=1.0 , _A : str=True , _A : Union[str, Any]=False , _A : Dict="sine" , _A : Tuple="resnet50" , _A : int=True , _A : str=False , _A : List[Any]=4 , _A : Dict=4 , _A : Optional[int]=4 , _A : Optional[Any]=False , _A : int=300 , _A : List[Any]=False , _A : Any=1 , _A : int=5 , _A : Optional[Any]=2 , _A : str=1 , _A : List[Any]=1 , _A : int=5 , _A : Optional[int]=2 , _A : int=0.1 , _A : List[str]=0.25 , _A : Any=False , **_A : int , ): """simple docstring""" if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) __SCREAMING_SNAKE_CASE : str = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : Dict = backbone_config.get('''model_type''' ) __SCREAMING_SNAKE_CASE : Dict = CONFIG_MAPPING[backbone_model_type] __SCREAMING_SNAKE_CASE : str = config_class.from_dict(_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = use_timm_backbone __SCREAMING_SNAKE_CASE : Optional[Any] = backbone_config __SCREAMING_SNAKE_CASE : Optional[int] = num_channels __SCREAMING_SNAKE_CASE : Optional[int] = num_queries __SCREAMING_SNAKE_CASE : Tuple = max_position_embeddings __SCREAMING_SNAKE_CASE : List[str] = d_model __SCREAMING_SNAKE_CASE : Tuple = encoder_ffn_dim __SCREAMING_SNAKE_CASE : Tuple = encoder_layers __SCREAMING_SNAKE_CASE : Union[str, Any] = encoder_attention_heads __SCREAMING_SNAKE_CASE : List[str] = decoder_ffn_dim __SCREAMING_SNAKE_CASE : Optional[int] = decoder_layers __SCREAMING_SNAKE_CASE : int = decoder_attention_heads __SCREAMING_SNAKE_CASE : List[Any] = dropout __SCREAMING_SNAKE_CASE : Any = attention_dropout __SCREAMING_SNAKE_CASE : List[Any] = activation_dropout __SCREAMING_SNAKE_CASE : str = activation_function __SCREAMING_SNAKE_CASE : Optional[Any] = init_std __SCREAMING_SNAKE_CASE : Optional[Any] = init_xavier_std __SCREAMING_SNAKE_CASE : Any = encoder_layerdrop __SCREAMING_SNAKE_CASE : int = auxiliary_loss __SCREAMING_SNAKE_CASE : Optional[int] = position_embedding_type __SCREAMING_SNAKE_CASE : Union[str, Any] = backbone __SCREAMING_SNAKE_CASE : Dict = use_pretrained_backbone __SCREAMING_SNAKE_CASE : Any = dilation # deformable attributes __SCREAMING_SNAKE_CASE : Any = num_feature_levels __SCREAMING_SNAKE_CASE : List[str] = encoder_n_points __SCREAMING_SNAKE_CASE : Tuple = decoder_n_points __SCREAMING_SNAKE_CASE : int = two_stage __SCREAMING_SNAKE_CASE : Dict = two_stage_num_proposals __SCREAMING_SNAKE_CASE : Dict = with_box_refine if two_stage is True and with_box_refine is False: raise ValueError('''If two_stage is True, with_box_refine must be True.''' ) # Hungarian matcher __SCREAMING_SNAKE_CASE : Optional[int] = class_cost __SCREAMING_SNAKE_CASE : Tuple = bbox_cost __SCREAMING_SNAKE_CASE : Tuple = giou_cost # Loss coefficients __SCREAMING_SNAKE_CASE : List[Any] = mask_loss_coefficient __SCREAMING_SNAKE_CASE : Any = dice_loss_coefficient __SCREAMING_SNAKE_CASE : Optional[int] = bbox_loss_coefficient __SCREAMING_SNAKE_CASE : Any = giou_loss_coefficient __SCREAMING_SNAKE_CASE : List[Any] = eos_coefficient __SCREAMING_SNAKE_CASE : Optional[int] = focal_alpha __SCREAMING_SNAKE_CASE : int = disable_custom_kernels super().__init__(is_encoder_decoder=_A , **_A ) @property def UpperCAmelCase__ ( self : str ): """simple docstring""" return self.encoder_attention_heads @property def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" return self.d_model def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: __SCREAMING_SNAKE_CASE : List[str] = self.backbone_config.to_dict() __SCREAMING_SNAKE_CASE : int = self.__class__.model_type return output
303
import numpy as np import torch from imwatermark import WatermarkEncoder # Copied from https://github.com/Stability-AI/generative-models/blob/613af104c6b85184091d42d374fef420eddb356d/scripts/demo/streamlit_helpers.py#L66 lowercase_ = 0b1011_0011_1110_1100_1001_0000_0111_1011_1011_0001_1001_1110 # bin(x)[2:] gives bits of x as str, use int to convert them to 0/1 lowercase_ = [int(bit) for bit in bin(WATERMARK_MESSAGE)[2:]] class __UpperCamelCase : """simple docstring""" def __init__( self : Dict ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = WATERMARK_BITS __SCREAMING_SNAKE_CASE : Optional[int] = WatermarkEncoder() self.encoder.set_watermark('''bits''' , self.watermark ) def UpperCAmelCase__ ( self : List[Any] , _A : torch.FloatTensor ): """simple docstring""" if images.shape[-1] < 256: return images __SCREAMING_SNAKE_CASE : Union[str, Any] = (255 * (images / 2 + 0.5)).cpu().permute(0 , 2 , 3 , 1 ).float().numpy() __SCREAMING_SNAKE_CASE : Dict = [self.encoder.encode(_A , '''dwtDct''' ) for image in images] __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.from_numpy(np.array(_A ) ).permute(0 , 3 , 1 , 2 ) __SCREAMING_SNAKE_CASE : Optional[Any] = torch.clamp(2 * (images / 255 - 0.5) , min=-1.0 , max=1.0 ) return images
303
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, is_vision_available, ) lowercase_ = {"""processing_layoutxlm""": ["""LayoutXLMProcessor"""]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""LayoutXLMTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""LayoutXLMTokenizerFast"""] if TYPE_CHECKING: from .processing_layoutxlm import LayoutXLMProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm import LayoutXLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutxlm_fast import LayoutXLMTokenizerFast else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
303
from heapq import heappop, heappush import numpy as np def a__ ( snake_case , snake_case , snake_case , snake_case , ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : int = grid.shape __SCREAMING_SNAKE_CASE : Tuple = [-1, 1, 0, 0] __SCREAMING_SNAKE_CASE : List[str] = [0, 0, -1, 1] if allow_diagonal: dx += [-1, -1, 1, 1] dy += [-1, 1, -1, 1] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Union[str, Any] = [(0, source)], set() __SCREAMING_SNAKE_CASE : Union[str, Any] = np.full((rows, cols) , np.inf ) __SCREAMING_SNAKE_CASE : Union[str, Any] = 0 __SCREAMING_SNAKE_CASE : Union[str, Any] = np.empty((rows, cols) , dtype=snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = None while queue: ((__SCREAMING_SNAKE_CASE), (__SCREAMING_SNAKE_CASE)) : Any = heappop(snake_case ) if (x, y) in visited: continue visited.add((x, y) ) if (x, y) == destination: __SCREAMING_SNAKE_CASE : int = [] while (x, y) != source: path.append((x, y) ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[Any] = predecessors[x, y] path.append(snake_case ) # add the source manually path.reverse() return matrix[destination], path for i in range(len(snake_case ) ): __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = x + dx[i], y + dy[i] if 0 <= nx < rows and 0 <= ny < cols: __SCREAMING_SNAKE_CASE : Optional[int] = grid[nx][ny] if next_node == 1 and matrix[nx, ny] > dist + 1: heappush(snake_case , (dist + 1, (nx, ny)) ) __SCREAMING_SNAKE_CASE : int = dist + 1 __SCREAMING_SNAKE_CASE : Dict = (x, y) return np.inf, [] if __name__ == "__main__": import doctest doctest.testmod()
303
1
def _a ( a :int = 50 ) -> int: a = [[0] * 3 for _ in range(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 ): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length] ) if __name__ == "__main__": print(f"""{solution() = }""")
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = {"""configuration_ibert""": ["""IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """IBertConfig""", """IBertOnnxConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """IBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """IBertForMaskedLM""", """IBertForMultipleChoice""", """IBertForQuestionAnswering""", """IBertForSequenceClassification""", """IBertForTokenClassification""", """IBertModel""", """IBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_ibert import IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, IBertConfig, IBertOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ibert import ( IBERT_PRETRAINED_MODEL_ARCHIVE_LIST, IBertForMaskedLM, IBertForMultipleChoice, IBertForQuestionAnswering, IBertForSequenceClassification, IBertForTokenClassification, IBertModel, IBertPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
303
0
'''simple docstring''' from __future__ import annotations import inspect import unittest import numpy as np from transformers import DeiTConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, ) from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DeiTImageProcessor class __A : def __init__(self : Any , __a : str , __a : List[str]=13 , __a : Optional[int]=30 , __a : Tuple=2 , __a : str=3 , __a : Tuple=True , __a : List[Any]=True , __a : Optional[int]=32 , __a : Optional[int]=2 , __a : int=4 , __a : Optional[Any]=37 , __a : Optional[Any]="gelu" , __a : Optional[Any]=0.1 , __a : int=0.1 , __a : int=10 , __a : Optional[int]=0.02 , __a : Dict=3 , __a : Optional[int]=None , __a : List[str]=2 , ): UpperCAmelCase_ = parent UpperCAmelCase_ = batch_size UpperCAmelCase_ = image_size UpperCAmelCase_ = patch_size UpperCAmelCase_ = num_channels UpperCAmelCase_ = is_training UpperCAmelCase_ = use_labels UpperCAmelCase_ = hidden_size UpperCAmelCase_ = num_hidden_layers UpperCAmelCase_ = num_attention_heads UpperCAmelCase_ = intermediate_size UpperCAmelCase_ = hidden_act UpperCAmelCase_ = hidden_dropout_prob UpperCAmelCase_ = attention_probs_dropout_prob UpperCAmelCase_ = type_sequence_label_size UpperCAmelCase_ = initializer_range UpperCAmelCase_ = scope UpperCAmelCase_ = encoder_stride # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens) UpperCAmelCase_ = (image_size // patch_size) ** 2 UpperCAmelCase_ = num_patches + 2 def _lowercase (self : Optional[Any] ): UpperCAmelCase_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCAmelCase_ = None if self.use_labels: UpperCAmelCase_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase_ = self.get_config() return config, pixel_values, labels def _lowercase (self : List[str] ): return DeiTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__a , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def _lowercase (self : Union[str, Any] , __a : str , __a : Any , __a : Dict ): UpperCAmelCase_ = TFDeiTModel(config=__a ) UpperCAmelCase_ = model(__a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowercase (self : str , __a : Optional[int] , __a : List[str] , __a : Tuple ): UpperCAmelCase_ = TFDeiTForMaskedImageModeling(config=__a ) UpperCAmelCase_ = model(__a ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images UpperCAmelCase_ = 1 UpperCAmelCase_ = TFDeiTForMaskedImageModeling(__a ) UpperCAmelCase_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCAmelCase_ = model(__a ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def _lowercase (self : List[Any] , __a : List[str] , __a : int , __a : int ): UpperCAmelCase_ = self.type_sequence_label_size UpperCAmelCase_ = TFDeiTForImageClassification(__a ) UpperCAmelCase_ = model(__a , labels=__a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images UpperCAmelCase_ = 1 UpperCAmelCase_ = TFDeiTForImageClassification(__a ) UpperCAmelCase_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCAmelCase_ = model(__a , labels=__a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _lowercase (self : Dict ): UpperCAmelCase_ = self.prepare_config_and_inputs() UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ = config_and_inputs UpperCAmelCase_ = {"pixel_values": pixel_values} return config, inputs_dict @require_tf class __A ( UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase ): a__ : Tuple = ( ( TFDeiTModel, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, ) if is_tf_available() else () ) a__ : Union[str, Any] = ( { """feature-extraction""": TFDeiTModel, """image-classification""": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher), } if is_tf_available() else {} ) a__ : Tuple = False a__ : List[Any] = False a__ : Any = False a__ : Dict = False def _lowercase (self : List[Any] ): UpperCAmelCase_ = TFDeiTModelTester(self ) UpperCAmelCase_ = ConfigTester(self , config_class=__a , has_text_modality=__a , hidden_size=37 ) def _lowercase (self : Union[str, Any] ): self.config_tester.run_common_tests() @unittest.skip(reason="DeiT does not use inputs_embeds" ) def _lowercase (self : List[str] ): pass def _lowercase (self : List[Any] ): UpperCAmelCase_ , UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase_ = model_class(__a ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) UpperCAmelCase_ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__a , tf.keras.layers.Dense ) ) def _lowercase (self : Optional[Any] ): UpperCAmelCase_ , UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase_ = model_class(__a ) UpperCAmelCase_ = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase_ = [*signature.parameters.keys()] UpperCAmelCase_ = ["pixel_values"] self.assertListEqual(arg_names[:1] , __a ) def _lowercase (self : Dict ): UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__a ) def _lowercase (self : Any ): UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*__a ) def _lowercase (self : Any ): UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__a ) def _lowercase (self : Optional[Any] , __a : Tuple , __a : Tuple , __a : Union[str, Any]=False ): UpperCAmelCase_ = super()._prepare_for_class(__a , __a , return_labels=__a ) if return_labels: if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call ).parameters: del inputs_dict["labels"] return inputs_dict @slow def _lowercase (self : int ): for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ = TFDeiTModel.from_pretrained(__a ) self.assertIsNotNone(__a ) def lowerCAmelCase_ ( ) -> Dict: '''simple docstring''' UpperCAmelCase_ = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_tf @require_vision class __A ( unittest.TestCase ): @cached_property def _lowercase (self : Union[str, Any] ): return ( DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224" ) if is_vision_available() else None ) @slow def _lowercase (self : Optional[Any] ): UpperCAmelCase_ = TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224" ) UpperCAmelCase_ = self.default_image_processor UpperCAmelCase_ = prepare_img() UpperCAmelCase_ = image_processor(images=__a , return_tensors="tf" ) # forward pass UpperCAmelCase_ = model(**__a ) # verify the logits UpperCAmelCase_ = tf.TensorShape((1, 1000) ) self.assertEqual(outputs.logits.shape , __a ) UpperCAmelCase_ = tf.constant([-1.02_66, 0.19_12, -1.28_61] ) self.assertTrue(np.allclose(outputs.logits[0, :3] , __a , atol=1E-4 ) )
1
from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
303
0
'''simple docstring''' from __future__ import annotations from fractions import Fraction from math import gcd, sqrt def _SCREAMING_SNAKE_CASE (A ) -> bool: """simple docstring""" lowercase__ = int(number**0.5 ) return number == sq * sq def _SCREAMING_SNAKE_CASE (A , A , A , A , A , A ) -> tuple[int, int]: """simple docstring""" lowercase__ = x_num * y_den * z_den + y_num * x_den * z_den + z_num * x_den * y_den lowercase__ = x_den * y_den * z_den lowercase__ = gcd(A , A ) top //= hcf bottom //= hcf return top, bottom def _SCREAMING_SNAKE_CASE (A = 35 ) -> int: """simple docstring""" lowercase__ = set() lowercase__ = 42 lowercase__ = Fraction(0 ) lowercase__ = 42 for x_num in range(1 , order + 1 ): for x_den in range(x_num + 1 , order + 1 ): for y_num in range(1 , order + 1 ): for y_den in range(y_num + 1 , order + 1 ): # n=1 lowercase__ = x_num * y_den + x_den * y_num lowercase__ = x_den * y_den lowercase__ = gcd(A , A ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: lowercase__ = add_three( A , A , A , A , A , A ) unique_s.add(A ) # n=2 lowercase__ = ( x_num * x_num * y_den * y_den + x_den * x_den * y_num * y_num ) lowercase__ = x_den * x_den * y_den * y_den if is_sq(A ) and is_sq(A ): lowercase__ = int(sqrt(A ) ) lowercase__ = int(sqrt(A ) ) lowercase__ = gcd(A , A ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: lowercase__ = add_three( A , A , A , A , A , A ) unique_s.add(A ) # n=-1 lowercase__ = x_num * y_num lowercase__ = x_den * y_num + x_num * y_den lowercase__ = gcd(A , A ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: lowercase__ = add_three( A , A , A , A , A , A ) unique_s.add(A ) # n=2 lowercase__ = x_num * x_num * y_num * y_num lowercase__ = ( x_den * x_den * y_num * y_num + x_num * x_num * y_den * y_den ) if is_sq(A ) and is_sq(A ): lowercase__ = int(sqrt(A ) ) lowercase__ = int(sqrt(A ) ) lowercase__ = gcd(A , A ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: lowercase__ = add_three( A , A , A , A , A , A ) unique_s.add(A ) for num, den in unique_s: total += Fraction(A , A ) return total.denominator + total.numerator if __name__ == "__main__": print(f"""{solution() = }""")
2
import sys from collections import defaultdict class __UpperCamelCase : """simple docstring""" def __init__( self : Union[str, Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = [] def UpperCAmelCase__ ( self : List[str] , _A : str ): """simple docstring""" return self.node_position[vertex] def UpperCAmelCase__ ( self : Dict , _A : List[str] , _A : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = pos def UpperCAmelCase__ ( self : List[Any] , _A : Union[str, Any] , _A : List[Any] , _A : List[str] , _A : Union[str, Any] ): """simple docstring""" if start > size // 2 - 1: return else: if 2 * start + 2 >= size: __SCREAMING_SNAKE_CASE : Union[str, Any] = 2 * start + 1 else: if heap[2 * start + 1] < heap[2 * start + 2]: __SCREAMING_SNAKE_CASE : List[Any] = 2 * start + 1 else: __SCREAMING_SNAKE_CASE : Dict = 2 * start + 2 if heap[smallest_child] < heap[start]: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Union[str, Any] = heap[smallest_child], positions[smallest_child] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : List[str] = ( heap[start], positions[start], ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = temp, tempa __SCREAMING_SNAKE_CASE : Any = self.get_position(positions[smallest_child] ) self.set_position( positions[smallest_child] , self.get_position(positions[start] ) ) self.set_position(positions[start] , _A ) self.top_to_bottom(_A , _A , _A , _A ) def UpperCAmelCase__ ( self : Any , _A : Union[str, Any] , _A : Dict , _A : Optional[Any] , _A : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = position[index] while index != 0: __SCREAMING_SNAKE_CASE : Optional[Any] = int((index - 2) / 2 ) if index % 2 == 0 else int((index - 1) / 2 ) if val < heap[parent]: __SCREAMING_SNAKE_CASE : Optional[Any] = heap[parent] __SCREAMING_SNAKE_CASE : str = position[parent] self.set_position(position[parent] , _A ) else: __SCREAMING_SNAKE_CASE : List[str] = val __SCREAMING_SNAKE_CASE : List[str] = temp self.set_position(_A , _A ) break __SCREAMING_SNAKE_CASE : List[Any] = parent else: __SCREAMING_SNAKE_CASE : Tuple = val __SCREAMING_SNAKE_CASE : List[str] = temp self.set_position(_A , 0 ) def UpperCAmelCase__ ( self : List[str] , _A : Tuple , _A : Tuple ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = len(_A ) // 2 - 1 for i in range(_A , -1 , -1 ): self.top_to_bottom(_A , _A , len(_A ) , _A ) def UpperCAmelCase__ ( self : List[str] , _A : Dict , _A : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = positions[0] __SCREAMING_SNAKE_CASE : Tuple = sys.maxsize self.top_to_bottom(_A , 0 , len(_A ) , _A ) return temp def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = Heap() __SCREAMING_SNAKE_CASE : int = [0] * len(snake_case ) __SCREAMING_SNAKE_CASE : Dict = [-1] * len(snake_case ) # Neighboring Tree Vertex of selected vertex # Minimum Distance of explored vertex with neighboring vertex of partial tree # formed in graph __SCREAMING_SNAKE_CASE : Dict = [] # Heap of Distance of vertices from their neighboring vertex __SCREAMING_SNAKE_CASE : Optional[int] = [] for vertex in range(len(snake_case ) ): distance_tv.append(sys.maxsize ) positions.append(snake_case ) heap.node_position.append(snake_case ) __SCREAMING_SNAKE_CASE : Union[str, Any] = [] __SCREAMING_SNAKE_CASE : str = 1 __SCREAMING_SNAKE_CASE : int = sys.maxsize for neighbor, distance in adjacency_list[0]: __SCREAMING_SNAKE_CASE : Optional[Any] = 0 __SCREAMING_SNAKE_CASE : Dict = distance heap.heapify(snake_case , snake_case ) for _ in range(1 , len(snake_case ) ): __SCREAMING_SNAKE_CASE : Tuple = heap.delete_minimum(snake_case , snake_case ) if visited[vertex] == 0: tree_edges.append((nbr_tv[vertex], vertex) ) __SCREAMING_SNAKE_CASE : List[Any] = 1 for neighbor, distance in adjacency_list[vertex]: if ( visited[neighbor] == 0 and distance < distance_tv[heap.get_position(snake_case )] ): __SCREAMING_SNAKE_CASE : int = distance heap.bottom_to_top( snake_case , heap.get_position(snake_case ) , snake_case , snake_case ) __SCREAMING_SNAKE_CASE : Any = vertex return tree_edges if __name__ == "__main__": # pragma: no cover # < --------- Prims Algorithm --------- > lowercase_ = int(input("""Enter number of edges: """).strip()) lowercase_ = defaultdict(list) for _ in range(edges_number): lowercase_ = [int(x) for x in input().strip().split()] adjacency_list[edge[0]].append([edge[1], edge[2]]) adjacency_list[edge[1]].append([edge[0], edge[2]]) print(prisms_algorithm(adjacency_list))
303
0
'''simple docstring''' import os import sys import unittest lowercase : Dict = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, 'utils')) import get_test_info # noqa: E402 from get_test_info import ( # noqa: E402 get_model_to_test_mapping, get_model_to_tester_mapping, get_test_to_tester_mapping, ) lowercase : Any = os.path.join('tests', 'models', 'bert', 'test_modeling_bert.py') lowercase : Optional[int] = os.path.join('tests', 'models', 'blip', 'test_modeling_blip.py') class A ( unittest.TestCase ): def __lowerCAmelCase ( self ) -> List[str]: """simple docstring""" A : Tuple = get_test_to_tester_mapping(SCREAMING_SNAKE_CASE ) A : Any = get_test_to_tester_mapping(SCREAMING_SNAKE_CASE ) A : List[Any] = {'''BertModelTest''': '''BertModelTester'''} A : int = { '''BlipModelTest''': '''BlipModelTester''', '''BlipTextImageModelTest''': '''BlipTextImageModelsModelTester''', '''BlipTextModelTest''': '''BlipTextModelTester''', '''BlipTextRetrievalModelTest''': '''BlipTextRetrievalModelTester''', '''BlipVQAModelTest''': '''BlipVQAModelTester''', '''BlipVisionModelTest''': '''BlipVisionModelTester''', } self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE ) , SCREAMING_SNAKE_CASE ) self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE ) , SCREAMING_SNAKE_CASE ) def __lowerCAmelCase ( self ) -> Any: """simple docstring""" A : Tuple = get_model_to_test_mapping(SCREAMING_SNAKE_CASE ) A : Tuple = get_model_to_test_mapping(SCREAMING_SNAKE_CASE ) A : List[str] = { '''BertForMaskedLM''': ['''BertModelTest'''], '''BertForMultipleChoice''': ['''BertModelTest'''], '''BertForNextSentencePrediction''': ['''BertModelTest'''], '''BertForPreTraining''': ['''BertModelTest'''], '''BertForQuestionAnswering''': ['''BertModelTest'''], '''BertForSequenceClassification''': ['''BertModelTest'''], '''BertForTokenClassification''': ['''BertModelTest'''], '''BertLMHeadModel''': ['''BertModelTest'''], '''BertModel''': ['''BertModelTest'''], } A : Union[str, Any] = { '''BlipForConditionalGeneration''': ['''BlipTextImageModelTest'''], '''BlipForImageTextRetrieval''': ['''BlipTextRetrievalModelTest'''], '''BlipForQuestionAnswering''': ['''BlipVQAModelTest'''], '''BlipModel''': ['''BlipModelTest'''], '''BlipTextModel''': ['''BlipTextModelTest'''], '''BlipVisionModel''': ['''BlipVisionModelTest'''], } self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE ) , SCREAMING_SNAKE_CASE ) self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE ) , SCREAMING_SNAKE_CASE ) def __lowerCAmelCase ( self ) -> Optional[int]: """simple docstring""" A : int = get_model_to_tester_mapping(SCREAMING_SNAKE_CASE ) A : Union[str, Any] = get_model_to_tester_mapping(SCREAMING_SNAKE_CASE ) A : Dict = { '''BertForMaskedLM''': ['''BertModelTester'''], '''BertForMultipleChoice''': ['''BertModelTester'''], '''BertForNextSentencePrediction''': ['''BertModelTester'''], '''BertForPreTraining''': ['''BertModelTester'''], '''BertForQuestionAnswering''': ['''BertModelTester'''], '''BertForSequenceClassification''': ['''BertModelTester'''], '''BertForTokenClassification''': ['''BertModelTester'''], '''BertLMHeadModel''': ['''BertModelTester'''], '''BertModel''': ['''BertModelTester'''], } A : str = { '''BlipForConditionalGeneration''': ['''BlipTextImageModelsModelTester'''], '''BlipForImageTextRetrieval''': ['''BlipTextRetrievalModelTester'''], '''BlipForQuestionAnswering''': ['''BlipVQAModelTester'''], '''BlipModel''': ['''BlipModelTester'''], '''BlipTextModel''': ['''BlipTextModelTester'''], '''BlipVisionModel''': ['''BlipVisionModelTester'''], } self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE ) , SCREAMING_SNAKE_CASE ) self.assertEqual(get_test_info.to_json(SCREAMING_SNAKE_CASE ) , SCREAMING_SNAKE_CASE )
3
from __future__ import annotations import matplotlib.pyplot as plt # type: ignore import numpy # initial triangle of Koch snowflake lowercase_ = numpy.array([0, 0]) lowercase_ = numpy.array([0.5, 0.866_0254]) lowercase_ = numpy.array([1, 0]) lowercase_ = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1] def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = initial_vectors for _ in range(snake_case ): __SCREAMING_SNAKE_CASE : Dict = iteration_step(snake_case ) return vectors def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = [] for i, start_vector in enumerate(vectors[:-1] ): __SCREAMING_SNAKE_CASE : str = vectors[i + 1] new_vectors.append(snake_case ) __SCREAMING_SNAKE_CASE : List[str] = end_vector - start_vector new_vectors.append(start_vector + difference_vector / 3 ) new_vectors.append( start_vector + difference_vector / 3 + rotate(difference_vector / 3 , 60 ) ) new_vectors.append(start_vector + difference_vector * 2 / 3 ) new_vectors.append(vectors[-1] ) return new_vectors def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = numpy.radians(snake_case ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = numpy.cos(snake_case ), numpy.sin(snake_case ) __SCREAMING_SNAKE_CASE : Any = numpy.array(((c, -s), (s, c)) ) return numpy.dot(snake_case , snake_case ) def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = plt.gca() axes.set_aspect('''equal''' ) # matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all # y-coordinates as inputs, which are constructed from the vector-list using # zip() __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = zip(*snake_case ) plt.plot(snake_case , snake_case ) plt.show() if __name__ == "__main__": import doctest doctest.testmod() lowercase_ = iterate(INITIAL_VECTORS, 5) plot(processed_vectors)
303
0
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto.configuration_auto import CONFIG_MAPPING __snake_case =logging.get_logger(__name__) class UpperCAmelCase_ ( __lowercase ): lowerCamelCase : Optional[Any] = '''upernet''' def __init__( self : Optional[int] , UpperCAmelCase__ : Optional[Any]=None , UpperCAmelCase__ : Tuple=5_1_2 , UpperCAmelCase__ : List[Any]=0.02 , UpperCAmelCase__ : str=[1, 2, 3, 6] , UpperCAmelCase__ : str=True , UpperCAmelCase__ : Dict=0.4 , UpperCAmelCase__ : int=3_8_4 , UpperCAmelCase__ : Dict=2_5_6 , UpperCAmelCase__ : Optional[int]=1 , UpperCAmelCase__ : List[str]=False , UpperCAmelCase__ : Any=2_5_5 , **UpperCAmelCase__ : Union[str, Any] , ) -> List[str]: super().__init__(**UpperCAmelCase__ ) if backbone_config is None: logger.info('`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.' ) lowerCAmelCase = CONFIG_MAPPING['resnet'](out_features=['stage1', 'stage2', 'stage3', 'stage4'] ) elif isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): lowerCAmelCase = backbone_config.get('model_type' ) lowerCAmelCase = CONFIG_MAPPING[backbone_model_type] lowerCAmelCase = config_class.from_dict(UpperCAmelCase__ ) lowerCAmelCase = backbone_config lowerCAmelCase = hidden_size lowerCAmelCase = initializer_range lowerCAmelCase = pool_scales lowerCAmelCase = use_auxiliary_head lowerCAmelCase = auxiliary_loss_weight lowerCAmelCase = auxiliary_in_channels lowerCAmelCase = auxiliary_channels lowerCAmelCase = auxiliary_num_convs lowerCAmelCase = auxiliary_concat_input lowerCAmelCase = loss_ignore_index def __UpperCAmelCase ( self : Any ) -> Tuple: lowerCAmelCase = copy.deepcopy(self.__dict__ ) lowerCAmelCase = self.backbone_config.to_dict() lowerCAmelCase = self.__class__.model_type return output
4
from typing import List, Optional, Union import torch from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) lowercase_ = logging.get_logger(__name__) # pylint: disable=invalid-name lowercase_ = """ Examples: ```py >>> import torch >>> import numpy as np >>> from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline >>> from transformers import pipeline >>> from diffusers.utils import load_image >>> def make_hint(image, depth_estimator): ... image = depth_estimator(image)[\"depth\"] ... image = np.array(image) ... image = image[:, :, None] ... image = np.concatenate([image, image, image], axis=2) ... detected_map = torch.from_numpy(image).float() / 255.0 ... hint = detected_map.permute(2, 0, 1) ... return hint >>> depth_estimator = pipeline(\"depth-estimation\") >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained( ... \"kandinsky-community/kandinsky-2-2-prior\", torch_dtype=torch.float16 ... ) >>> pipe_prior = pipe_prior.to(\"cuda\") >>> pipe = KandinskyV22ControlnetPipeline.from_pretrained( ... \"kandinsky-community/kandinsky-2-2-controlnet-depth\", torch_dtype=torch.float16 ... ) >>> pipe = pipe.to(\"cuda\") >>> img = load_image( ... \"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main\" ... \"/kandinsky/cat.png\" ... ).resize((768, 768)) >>> hint = make_hint(img, depth_estimator).unsqueeze(0).half().to(\"cuda\") >>> prompt = \"A robot, 4k photo\" >>> negative_prior_prompt = \"lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature\" >>> generator = torch.Generator(device=\"cuda\").manual_seed(43) >>> image_emb, zero_image_emb = pipe_prior( ... prompt=prompt, negative_prompt=negative_prior_prompt, generator=generator ... ).to_tuple() >>> images = pipe( ... image_embeds=image_emb, ... negative_image_embeds=zero_image_emb, ... hint=hint, ... num_inference_steps=50, ... generator=generator, ... height=768, ... width=768, ... ).images >>> images[0].save(\"robot_cat.png\") ``` """ def a__ ( snake_case , snake_case , snake_case=8 ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 __SCREAMING_SNAKE_CASE : Optional[int] = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" def __init__( self : int , _A : UNetaDConditionModel , _A : DDPMScheduler , _A : VQModel , ): """simple docstring""" super().__init__() self.register_modules( unet=_A , scheduler=_A , movq=_A , ) __SCREAMING_SNAKE_CASE : Optional[Any] = 2 ** (len(self.movq.config.block_out_channels ) - 1) def UpperCAmelCase__ ( self : Union[str, Any] , _A : Dict , _A : Optional[Any] , _A : Tuple , _A : List[Any] , _A : Optional[Any] , _A : List[Any] ): """simple docstring""" if latents is None: __SCREAMING_SNAKE_CASE : Optional[Any] = randn_tensor(_A , generator=_A , device=_A , dtype=_A ) else: if latents.shape != shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {shape}''' ) __SCREAMING_SNAKE_CASE : Tuple = latents.to(_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = latents * scheduler.init_noise_sigma return latents def UpperCAmelCase__ ( self : Tuple , _A : List[str]=0 ): """simple docstring""" if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) __SCREAMING_SNAKE_CASE : List[Any] = torch.device(F'''cuda:{gpu_id}''' ) __SCREAMING_SNAKE_CASE : Dict = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_A , _A ) def UpperCAmelCase__ ( self : int , _A : Tuple=0 ): """simple docstring""" if is_accelerate_available() and is_accelerate_version('''>=''' , '''0.17.0.dev0''' ): from accelerate import cpu_offload_with_hook else: raise ImportError('''`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.''' ) __SCREAMING_SNAKE_CASE : str = torch.device(F'''cuda:{gpu_id}''' ) if self.device.type != "cpu": self.to('''cpu''' , silence_dtype_warnings=_A ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) __SCREAMING_SNAKE_CASE : Optional[Any] = None for cpu_offloaded_model in [self.unet, self.movq]: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Dict = cpu_offload_with_hook(_A , _A , prev_module_hook=_A ) # We'll offload the last model manually. __SCREAMING_SNAKE_CASE : Optional[int] = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" if not hasattr(self.unet , '''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(_A , '''_hf_hook''' ) and hasattr(module._hf_hook , '''execution_device''' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(_A ) def __call__( self : Dict , _A : Union[torch.FloatTensor, List[torch.FloatTensor]] , _A : Union[torch.FloatTensor, List[torch.FloatTensor]] , _A : torch.FloatTensor , _A : int = 512 , _A : int = 512 , _A : int = 100 , _A : float = 4.0 , _A : int = 1 , _A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _A : Optional[torch.FloatTensor] = None , _A : Optional[str] = "pil" , _A : bool = True , ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = self._execution_device __SCREAMING_SNAKE_CASE : Optional[Any] = guidance_scale > 1.0 if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : Optional[Any] = torch.cat(_A , dim=0 ) if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : List[Any] = torch.cat(_A , dim=0 ) if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : List[str] = torch.cat(_A , dim=0 ) __SCREAMING_SNAKE_CASE : List[Any] = image_embeds.shape[0] * num_images_per_prompt if do_classifier_free_guidance: __SCREAMING_SNAKE_CASE : Dict = image_embeds.repeat_interleave(_A , dim=0 ) __SCREAMING_SNAKE_CASE : Any = negative_image_embeds.repeat_interleave(_A , dim=0 ) __SCREAMING_SNAKE_CASE : Union[str, Any] = hint.repeat_interleave(_A , dim=0 ) __SCREAMING_SNAKE_CASE : int = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=_A ) __SCREAMING_SNAKE_CASE : Optional[int] = torch.cat([hint, hint] , dim=0 ).to(dtype=self.unet.dtype , device=_A ) self.scheduler.set_timesteps(_A , device=_A ) __SCREAMING_SNAKE_CASE : Optional[int] = self.scheduler.timesteps __SCREAMING_SNAKE_CASE : Tuple = self.movq.config.latent_channels __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Dict = downscale_height_and_width(_A , _A , self.movq_scale_factor ) # create initial latent __SCREAMING_SNAKE_CASE : Tuple = self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , _A , _A , _A , self.scheduler , ) for i, t in enumerate(self.progress_bar(_A ) ): # expand the latents if we are doing classifier free guidance __SCREAMING_SNAKE_CASE : str = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents __SCREAMING_SNAKE_CASE : Dict = {'''image_embeds''': image_embeds, '''hint''': hint} __SCREAMING_SNAKE_CASE : Union[str, Any] = self.unet( sample=_A , timestep=_A , encoder_hidden_states=_A , added_cond_kwargs=_A , return_dict=_A , )[0] if do_classifier_free_guidance: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = noise_pred.split(latents.shape[1] , dim=1 ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Dict = noise_pred.chunk(2 ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[Any] = variance_pred.chunk(2 ) __SCREAMING_SNAKE_CASE : List[Any] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) __SCREAMING_SNAKE_CASE : Tuple = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , '''variance_type''' ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : List[str] = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 __SCREAMING_SNAKE_CASE : Any = self.scheduler.step( _A , _A , _A , generator=_A , )[0] # post-processing __SCREAMING_SNAKE_CASE : Any = self.movq.decode(_A , force_not_quantize=_A )['''sample'''] if output_type not in ["pt", "np", "pil"]: raise ValueError(F'''Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}''' ) if output_type in ["np", "pil"]: __SCREAMING_SNAKE_CASE : str = image * 0.5 + 0.5 __SCREAMING_SNAKE_CASE : Tuple = image.clamp(0 , 1 ) __SCREAMING_SNAKE_CASE : Dict = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": __SCREAMING_SNAKE_CASE : List[str] = self.numpy_to_pil(_A ) if not return_dict: return (image,) return ImagePipelineOutput(images=_A )
303
0
import argparse import fairseq import torch from transformers import UniSpeechSatConfig, UniSpeechSatForCTC, UniSpeechSatForPreTraining, logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.k_proj''': '''encoder.layers.*.attention.k_proj''', '''self_attn.v_proj''': '''encoder.layers.*.attention.v_proj''', '''self_attn.q_proj''': '''encoder.layers.*.attention.q_proj''', '''self_attn.out_proj''': '''encoder.layers.*.attention.out_proj''', '''self_attn_layer_norm''': '''encoder.layers.*.layer_norm''', '''fc1''': '''encoder.layers.*.feed_forward.intermediate_dense''', '''fc2''': '''encoder.layers.*.feed_forward.output_dense''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''encoder.layer_norm_for_extract''': '''layer_norm_for_extract''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''quantizer.weight_proj''': '''quantizer.weight_proj''', '''quantizer.vars''': '''quantizer.codevectors''', '''project_q''': '''project_q''', '''final_proj''': '''project_hid''', '''w2v_encoder.proj''': '''lm_head''', '''label_embs_concat''': '''label_embeddings_concat''', '''mask_emb''': '''masked_spec_embed''', '''spk_proj''': '''speaker_proj''', } UpperCAmelCase__ = [ '''lm_head''', '''quantizer.weight_proj''', '''quantizer.codevectors''', '''project_q''', '''project_hid''', '''label_embeddings_concat''', '''speaker_proj''', '''layer_norm_for_extract''', ] def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) -> List[str]: """simple docstring""" for attribute in key.split('''.''' ): _lowercase =getattr(__snake_case , __snake_case ) if weight_type is not None: _lowercase =getattr(__snake_case , __snake_case ).shape else: _lowercase =hf_pointer.shape if hf_shape != value.shape: raise ValueError( F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" F" {value.shape} for {full_name}" ) if weight_type == "weight": _lowercase =value elif weight_type == "weight_g": _lowercase =value elif weight_type == "weight_v": _lowercase =value elif weight_type == "bias": _lowercase =value else: _lowercase =value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> List[Any]: """simple docstring""" _lowercase =[] _lowercase =fairseq_model.state_dict() _lowercase =hf_model.unispeech_sat.feature_extractor for name, value in fairseq_dict.items(): _lowercase =False if "conv_layers" in name: load_conv_layer( __snake_case , __snake_case , __snake_case , __snake_case , hf_model.config.feat_extract_norm == '''group''' , ) _lowercase =True else: for key, mapped_key in MAPPING.items(): _lowercase ='''unispeech_sat.''' + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split('''w2v_model.''' )[-1] == name.split('''.''' )[0]: if "layer_norm_for_extract" in name and (".".join(name.split('''.''' )[:-1] ) != key): # special case since naming is very similar continue _lowercase =True if "*" in mapped_key: _lowercase =name.split(__snake_case )[0].split('''.''' )[-2] _lowercase =mapped_key.replace('''*''' , __snake_case ) if "weight_g" in name: _lowercase ='''weight_g''' elif "weight_v" in name: _lowercase ='''weight_v''' elif "bias" in name: _lowercase ='''bias''' elif "weight" in name: # TODO: don't match quantizer.weight_proj _lowercase ='''weight''' else: _lowercase =None set_recursively(__snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) continue if not is_used: unused_weights.append(__snake_case ) logger.warning(F"Unused weights: {unused_weights}" ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) -> int: """simple docstring""" _lowercase =full_name.split('''conv_layers.''' )[-1] _lowercase =name.split('''.''' ) _lowercase =int(items[0] ) _lowercase =int(items[1] ) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) _lowercase =value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) _lowercase =value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( F"{full_name} has size {value.shape}, but" F" {feature_extractor[layer_id].layer_norm.bias.data.shape} was found." ) _lowercase =value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( F"{full_name} has size {value.shape}, but" F" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) _lowercase =value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(__snake_case ) @torch.no_grad() def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case=None , __snake_case=None , __snake_case=True ) -> Any: """simple docstring""" if config_path is not None: _lowercase =UniSpeechSatConfig.from_pretrained(__snake_case ) else: _lowercase =UniSpeechSatConfig() _lowercase ='''''' if is_finetuned: _lowercase =UniSpeechSatForCTC(__snake_case ) else: _lowercase =UniSpeechSatForPreTraining(__snake_case ) _lowercase , _lowercase , _lowercase =fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'''data''': '''/'''.join(dict_path.split('''/''' )[:-1] )} ) _lowercase =model[0].eval() recursively_load_weights(__snake_case , __snake_case ) hf_wavavec.save_pretrained(__snake_case ) 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('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--not_finetuned''', action='''store_true''', help='''Whether the model to convert is a fine-tuned model or not''' ) UpperCAmelCase__ = parser.parse_args() convert_unispeech_sat_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
5
import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem lowercase_ = importlib.util.find_spec("""s3fs""") is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 lowercase_ = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(f'''A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.''') fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def a__ ( snake_case ): """simple docstring""" if "://" in dataset_path: __SCREAMING_SNAKE_CASE : Any = dataset_path.split('''://''' )[1] return dataset_path def a__ ( snake_case ): """simple docstring""" if fs is not None and fs.protocol != "file": return True else: return False def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = not is_remote_filesystem(snake_case ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(snake_case ) , fs._strip_protocol(snake_case ) ) else: fs.mv(snake_case , snake_case , recursive=snake_case ) def a__ ( ): """simple docstring""" if hasattr(fsspec.asyn , '''reset_lock''' ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: __SCREAMING_SNAKE_CASE : int = None __SCREAMING_SNAKE_CASE : Union[str, Any] = None __SCREAMING_SNAKE_CASE : Union[str, Any] = threading.Lock()
303
0
import html from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin from ...utils import is_bsa_available, logging, requires_backends if is_bsa_available(): import bsa from bsa import BeautifulSoup A : str = logging.get_logger(__name__) class __A( a ): def __init__( self , **_snake_case ) -> List[Any]: '''simple docstring''' requires_backends(self , ['''bs4'''] ) super().__init__(**_snake_case ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> int: '''simple docstring''' __a = [] __a = [] __a = element if element.name else element.parent for parent in child.parents: # type: bs4.element.Tag __a = parent.find_all(child.name , recursive=_snake_case ) xpath_tags.append(child.name ) xpath_subscripts.append( 0 if 1 == len(_snake_case ) else next(i for i, s in enumerate(_snake_case , 1 ) if s is child ) ) __a = parent xpath_tags.reverse() xpath_subscripts.reverse() return xpath_tags, xpath_subscripts def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> Optional[int]: '''simple docstring''' __a = BeautifulSoup(_snake_case , '''html.parser''' ) __a = [] __a = [] __a = [] for element in html_code.descendants: if type(_snake_case ) == bsa.element.NavigableString: if type(element.parent ) != bsa.element.Tag: continue __a = html.unescape(_snake_case ).strip() if not text_in_this_tag: continue all_doc_strings.append(_snake_case ) __a , __a = self.xpath_soup(_snake_case ) stringaxtag_seq.append(_snake_case ) stringaxsubs_seq.append(_snake_case ) if len(_snake_case ) != len(_snake_case ): raise ValueError('''Number of doc strings and xtags does not correspond''' ) if len(_snake_case ) != len(_snake_case ): raise ValueError('''Number of doc strings and xsubs does not correspond''' ) return all_doc_strings, stringaxtag_seq, stringaxsubs_seq def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case ) -> Optional[int]: '''simple docstring''' __a = '''''' for tagname, subs in zip(_snake_case , _snake_case ): xpath += F"""/{tagname}""" if subs != 0: xpath += F"""[{subs}]""" return xpath def __call__( self , _snake_case ) -> BatchFeature: '''simple docstring''' __a = False # Check that strings has a valid type if isinstance(_snake_case , _snake_case ): __a = True elif isinstance(_snake_case , (list, tuple) ): if len(_snake_case ) == 0 or isinstance(html_strings[0] , _snake_case ): __a = True if not valid_strings: raise ValueError( '''HTML strings must of type `str`, `List[str]` (batch of examples), ''' F"""but is of type {type(_snake_case )}.""" ) __a = bool(isinstance(_snake_case , (list, tuple) ) and (isinstance(html_strings[0] , _snake_case )) ) if not is_batched: __a = [html_strings] # Get nodes + xpaths __a = [] __a = [] for html_string in html_strings: __a , __a , __a = self.get_three_from_single(_snake_case ) nodes.append(_snake_case ) __a = [] for node, tag_list, sub_list in zip(_snake_case , _snake_case , _snake_case ): __a = self.construct_xpath(_snake_case , _snake_case ) xpath_strings.append(_snake_case ) xpaths.append(_snake_case ) # return as Dict __a = {'''nodes''': nodes, '''xpaths''': xpaths} __a = BatchFeature(data=_snake_case , tensor_type=_snake_case ) return encoded_inputs
6
from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels
303
0
import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _snake_case( *SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : Optional[Union[Dict, Any]] = None , SCREAMING_SNAKE_CASE__ : Union[str, Any]=True , SCREAMING_SNAKE_CASE__ : int=2 ) -> Optional[int]: '''simple docstring''' from .. import __version__ A__ = take_from A__ = () if not isinstance(args[0] , SCREAMING_SNAKE_CASE__ ): A__ = (args,) for attribute, version_name, message in args: if version.parse(version.parse(SCREAMING_SNAKE_CASE__ ).base_version ) >= version.parse(SCREAMING_SNAKE_CASE__ ): raise ValueError( f'The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\'' f' version {__version__} is >= {version_name}' ) A__ = None if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(SCREAMING_SNAKE_CASE__ ),) A__ = f'The `{attribute}` argument is deprecated and will be removed in version {version_name}.' elif hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): values += (getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ),) A__ = f'The `{attribute}` attribute is deprecated and will be removed in version {version_name}.' elif deprecated_kwargs is None: A__ = f'`{attribute}` is deprecated and will be removed in version {version_name}.' if warning is not None: A__ = warning + ' ' if standard_warn else '' warnings.warn(warning + message , SCREAMING_SNAKE_CASE__ , stacklevel=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and len(SCREAMING_SNAKE_CASE__ ) > 0: A__ = inspect.getouterframes(inspect.currentframe() )[1] A__ = call_frame.filename A__ = call_frame.lineno A__ = call_frame.function A__ , A__ = next(iter(deprecated_kwargs.items() ) ) raise TypeError(f'{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`' ) if len(SCREAMING_SNAKE_CASE__ ) == 0: return elif len(SCREAMING_SNAKE_CASE__ ) == 1: return values[0] return values
7
from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { """uw-madison/mra-base-512-4""": """https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json""", } class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''mra''' def __init__( self : str , _A : List[str]=5_0265 , _A : int=768 , _A : Union[str, Any]=12 , _A : Union[str, Any]=12 , _A : Union[str, Any]=3072 , _A : Any="gelu" , _A : List[Any]=0.1 , _A : List[Any]=0.1 , _A : List[str]=512 , _A : Tuple=1 , _A : List[str]=0.02 , _A : Union[str, Any]=1e-5 , _A : Optional[int]="absolute" , _A : Union[str, Any]=4 , _A : List[Any]="full" , _A : Union[str, Any]=0 , _A : Union[str, Any]=0 , _A : Optional[Any]=1 , _A : Union[str, Any]=0 , _A : Any=2 , **_A : List[str] , ): """simple docstring""" super().__init__(pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , **_A ) __SCREAMING_SNAKE_CASE : Dict = vocab_size __SCREAMING_SNAKE_CASE : str = max_position_embeddings __SCREAMING_SNAKE_CASE : Optional[int] = hidden_size __SCREAMING_SNAKE_CASE : str = num_hidden_layers __SCREAMING_SNAKE_CASE : Tuple = num_attention_heads __SCREAMING_SNAKE_CASE : str = intermediate_size __SCREAMING_SNAKE_CASE : Tuple = hidden_act __SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob __SCREAMING_SNAKE_CASE : int = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : Tuple = initializer_range __SCREAMING_SNAKE_CASE : Any = type_vocab_size __SCREAMING_SNAKE_CASE : str = layer_norm_eps __SCREAMING_SNAKE_CASE : Union[str, Any] = position_embedding_type __SCREAMING_SNAKE_CASE : str = block_per_row __SCREAMING_SNAKE_CASE : Union[str, Any] = approx_mode __SCREAMING_SNAKE_CASE : Optional[int] = initial_prior_first_n_blocks __SCREAMING_SNAKE_CASE : List[Any] = initial_prior_diagonal_n_blocks
303
0
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = (DPMSolverSDEScheduler,) SCREAMING_SNAKE_CASE : str = 10 def snake_case__( self : Optional[int] , **_UpperCamelCase : str ) ->Optional[Any]: snake_case_ = { '''num_train_timesteps''': 1_1_0_0, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', '''noise_sampler_seed''': 0, } config.update(**_UpperCamelCase ) return config def snake_case__( self : Optional[Any] ) ->Any: for timesteps in [1_0, 5_0, 1_0_0, 1_0_0_0]: self.check_over_configs(num_train_timesteps=_UpperCamelCase ) def snake_case__( self : List[str] ) ->int: for beta_start, beta_end in zip([0.00001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=_UpperCamelCase , beta_end=_UpperCamelCase ) def snake_case__( self : List[str] ) ->Union[str, Any]: for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_UpperCamelCase ) def snake_case__( self : List[Any] ) ->Optional[Any]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCamelCase ) def snake_case__( self : Optional[int] ) ->int: snake_case_ = self.scheduler_classes[0] snake_case_ = self.get_scheduler_config() snake_case_ = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) snake_case_ = self.dummy_model() snake_case_ = self.dummy_sample_deter * scheduler.init_noise_sigma snake_case_ = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): snake_case_ = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) snake_case_ = model(_UpperCamelCase , _UpperCamelCase ) snake_case_ = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) snake_case_ = output.prev_sample snake_case_ = torch.sum(torch.abs(_UpperCamelCase ) ) snake_case_ = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.47821044921875 ) < 1e-2 assert abs(result_mean.item() - 0.2178705964565277 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59352111816406 ) < 1e-2 assert abs(result_mean.item() - 0.22342906892299652 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def snake_case__( self : Optional[Any] ) ->str: snake_case_ = self.scheduler_classes[0] snake_case_ = self.get_scheduler_config(prediction_type='''v_prediction''' ) snake_case_ = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) snake_case_ = self.dummy_model() snake_case_ = self.dummy_sample_deter * scheduler.init_noise_sigma snake_case_ = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): snake_case_ = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) snake_case_ = model(_UpperCamelCase , _UpperCamelCase ) snake_case_ = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) snake_case_ = output.prev_sample snake_case_ = torch.sum(torch.abs(_UpperCamelCase ) ) snake_case_ = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 124.77149200439453 ) < 1e-2 assert abs(result_mean.item() - 0.16226289014816284 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 128.1663360595703 ) < 1e-2 assert abs(result_mean.item() - 0.16688326001167297 ) < 1e-3 else: assert abs(result_sum.item() - 119.8487548828125 ) < 1e-2 assert abs(result_mean.item() - 0.1560530662536621 ) < 1e-3 def snake_case__( self : List[str] ) ->Union[str, Any]: snake_case_ = self.scheduler_classes[0] snake_case_ = self.get_scheduler_config() snake_case_ = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps , device=_UpperCamelCase ) snake_case_ = self.dummy_model() snake_case_ = self.dummy_sample_deter.to(_UpperCamelCase ) * scheduler.init_noise_sigma for t in scheduler.timesteps: snake_case_ = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) snake_case_ = model(_UpperCamelCase , _UpperCamelCase ) snake_case_ = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) snake_case_ = output.prev_sample snake_case_ = torch.sum(torch.abs(_UpperCamelCase ) ) snake_case_ = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.46957397460938 ) < 1e-2 assert abs(result_mean.item() - 0.21805934607982635 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59353637695312 ) < 1e-2 assert abs(result_mean.item() - 0.22342908382415771 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def snake_case__( self : Optional[Any] ) ->int: snake_case_ = self.scheduler_classes[0] snake_case_ = self.get_scheduler_config() snake_case_ = scheduler_class(**_UpperCamelCase , use_karras_sigmas=_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps , device=_UpperCamelCase ) snake_case_ = self.dummy_model() snake_case_ = self.dummy_sample_deter.to(_UpperCamelCase ) * scheduler.init_noise_sigma snake_case_ = sample.to(_UpperCamelCase ) for t in scheduler.timesteps: snake_case_ = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) snake_case_ = model(_UpperCamelCase , _UpperCamelCase ) snake_case_ = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) snake_case_ = output.prev_sample snake_case_ = torch.sum(torch.abs(_UpperCamelCase ) ) snake_case_ = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 176.66974135742188 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 177.63653564453125 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 else: assert abs(result_sum.item() - 170.3135223388672 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2
8
from typing import List, Optional, Tuple, Union import torch from ...utils import logging, randn_tensor from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline lowercase_ = logging.get_logger(__name__) # pylint: disable=invalid-name class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" def __init__( self : List[str] , _A : Dict , _A : List[Any] ): """simple docstring""" super().__init__() self.register_modules(unet=_A , scheduler=_A ) @torch.no_grad() def __call__( self : List[str] , _A : int = 1 , _A : int = 100 , _A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _A : Optional[float] = None , _A : bool = True , ): """simple docstring""" if audio_length_in_s is None: __SCREAMING_SNAKE_CASE : Optional[Any] = self.unet.config.sample_size / self.unet.config.sample_rate __SCREAMING_SNAKE_CASE : List[Any] = audio_length_in_s * self.unet.config.sample_rate __SCREAMING_SNAKE_CASE : Any = 2 ** len(self.unet.up_blocks ) if sample_size < 3 * down_scale_factor: raise ValueError( F'''{audio_length_in_s} is too small. Make sure it\'s bigger or equal to''' F''' {3 * down_scale_factor / self.unet.config.sample_rate}.''' ) __SCREAMING_SNAKE_CASE : int = int(_A ) if sample_size % down_scale_factor != 0: __SCREAMING_SNAKE_CASE : Optional[int] = ( (audio_length_in_s * self.unet.config.sample_rate) // down_scale_factor + 1 ) * down_scale_factor logger.info( F'''{audio_length_in_s} is increased to {sample_size / self.unet.config.sample_rate} so that it can be handled''' F''' by the model. It will be cut to {original_sample_size / self.unet.config.sample_rate} after the denoising''' ''' process.''' ) __SCREAMING_SNAKE_CASE : List[Any] = int(_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = next(iter(self.unet.parameters() ) ).dtype __SCREAMING_SNAKE_CASE : int = (batch_size, self.unet.config.in_channels, sample_size) if isinstance(_A , _A ) and len(_A ) != batch_size: raise ValueError( F'''You have passed a list of generators of length {len(_A )}, but requested an effective batch''' F''' size of {batch_size}. Make sure the batch size matches the length of the generators.''' ) __SCREAMING_SNAKE_CASE : Dict = randn_tensor(_A , generator=_A , device=self.device , dtype=_A ) # set step values self.scheduler.set_timesteps(_A , device=audio.device ) __SCREAMING_SNAKE_CASE : Dict = self.scheduler.timesteps.to(_A ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output __SCREAMING_SNAKE_CASE : List[Any] = self.unet(_A , _A ).sample # 2. compute previous image: x_t -> t_t-1 __SCREAMING_SNAKE_CASE : Optional[int] = self.scheduler.step(_A , _A , _A ).prev_sample __SCREAMING_SNAKE_CASE : str = audio.clamp(-1 , 1 ).float().cpu().numpy() __SCREAMING_SNAKE_CASE : str = audio[:, :, :original_sample_size] if not return_dict: return (audio,) return AudioPipelineOutput(audios=_A )
303
0
import unittest from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow if is_flax_available(): import jax from transformers.models.auto.modeling_flax_auto import FlaxAutoModel from transformers.models.bert.modeling_flax_bert import FlaxBertModel from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel @require_flax class _lowercase ( unittest.TestCase ): '''simple docstring''' @slow def __magic_name__( self :str ) -> int: for model_name in ["bert-base-cased", "bert-large-uncased"]: with self.subTest(lowerCAmelCase__ ): __SCREAMING_SNAKE_CASE : Dict = AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : int = FlaxAutoModel.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def __magic_name__( self :Dict ) -> Dict: for model_name in ["roberta-base", "roberta-large"]: with self.subTest(lowerCAmelCase__ ): __SCREAMING_SNAKE_CASE : List[Any] = AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = FlaxAutoModel.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def __magic_name__( self :Optional[int] ) -> List[Any]: for model_name in ["bert-base-cased", "bert-large-uncased"]: __SCREAMING_SNAKE_CASE : Optional[int] = AutoTokenizer.from_pretrained(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = FlaxBertModel.from_pretrained(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer('''Do you support jax jitted function?''' , return_tensors=TensorType.JAX ) @jax.jit def eval(**lowerCAmelCase__ :List[Any] ): return model(**lowerCAmelCase__ ) eval(**lowerCAmelCase__ ).block_until_ready() @slow def __magic_name__( self :List[str] ) -> Optional[int]: for model_name in ["roberta-base", "roberta-large"]: __SCREAMING_SNAKE_CASE : str = AutoTokenizer.from_pretrained(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = FlaxRobertaModel.from_pretrained(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = tokenizer('''Do you support jax jitted function?''' , return_tensors=TensorType.JAX ) @jax.jit def eval(**lowerCAmelCase__ :List[Any] ): return model(**lowerCAmelCase__ ) eval(**lowerCAmelCase__ ).block_until_ready() def __magic_name__( self :Dict ) -> Union[str, Any]: with self.assertRaisesRegex( lowerCAmelCase__ , '''bert-base is not a local folder and is not a valid model identifier''' ): __SCREAMING_SNAKE_CASE : Union[str, Any] = FlaxAutoModel.from_pretrained('''bert-base''' ) def __magic_name__( self :int ) -> Optional[int]: with self.assertRaisesRegex( lowerCAmelCase__ , r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): __SCREAMING_SNAKE_CASE : Optional[int] = FlaxAutoModel.from_pretrained(lowerCAmelCase__ , revision='''aaaaaa''' ) def __magic_name__( self :str ) -> Tuple: with self.assertRaisesRegex( lowerCAmelCase__ , '''hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack''' , ): __SCREAMING_SNAKE_CASE : str = FlaxAutoModel.from_pretrained('''hf-internal-testing/config-no-model''' ) def __magic_name__( self :Dict ) -> int: with self.assertRaisesRegex(lowerCAmelCase__ , '''Use `from_pt=True` to load this model''' ): __SCREAMING_SNAKE_CASE : Dict = FlaxAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only''' )
9
import importlib import torch import yaml from omegaconf import OmegaConf from taming.models.vqgan import VQModel def a__ ( snake_case , snake_case=False ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = OmegaConf.load(snake_case ) if display: print(yaml.dump(OmegaConf.to_container(snake_case ) ) ) return config def a__ ( snake_case , snake_case=None , snake_case=None ): """simple docstring""" if conf_path is None: __SCREAMING_SNAKE_CASE : Any = '''./model_checkpoints/vqgan_only.yaml''' __SCREAMING_SNAKE_CASE : List[str] = load_config(snake_case , display=snake_case ) __SCREAMING_SNAKE_CASE : str = VQModel(**config.model.params ) if ckpt_path is None: __SCREAMING_SNAKE_CASE : Optional[Any] = '''./model_checkpoints/vqgan_only.pt''' __SCREAMING_SNAKE_CASE : Optional[Any] = torch.load(snake_case , map_location=snake_case ) if ".ckpt" in ckpt_path: __SCREAMING_SNAKE_CASE : Optional[Any] = sd['''state_dict'''] model.load_state_dict(snake_case , strict=snake_case ) model.to(snake_case ) del sd return model def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Any = model.encode(snake_case ) print(F'''VQGAN --- {model.__class__.__name__}: latent shape: {z.shape[2:]}''' ) __SCREAMING_SNAKE_CASE : Any = model.decode(snake_case ) return xrec def a__ ( snake_case , snake_case=False ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = string.rsplit('''.''' , 1 ) if reload: __SCREAMING_SNAKE_CASE : Union[str, Any] = importlib.import_module(snake_case ) importlib.reload(snake_case ) return getattr(importlib.import_module(snake_case , package=snake_case ) , cls ) def a__ ( snake_case ): """simple docstring""" if "target" not in config: raise KeyError('''Expected key `target` to instantiate.''' ) return get_obj_from_str(config['''target'''] )(**config.get('''params''' , {} ) ) def a__ ( snake_case , snake_case , snake_case=True , snake_case=True ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = instantiate_from_config(snake_case ) if sd is not None: model.load_state_dict(snake_case ) if gpu: model.cuda() if eval_mode: model.eval() return {"model": model} def a__ ( snake_case , snake_case , snake_case , snake_case ): """simple docstring""" # load the specified checkpoint if ckpt: __SCREAMING_SNAKE_CASE : Dict = torch.load(snake_case , map_location='''cpu''' ) __SCREAMING_SNAKE_CASE : List[Any] = pl_sd['''global_step'''] print(F'''loaded model from global step {global_step}.''' ) else: __SCREAMING_SNAKE_CASE : Optional[Any] = {'''state_dict''': None} __SCREAMING_SNAKE_CASE : Optional[Any] = None __SCREAMING_SNAKE_CASE : Dict = load_model_from_config(config.model , pl_sd['''state_dict'''] , gpu=snake_case , eval_mode=snake_case )['''model'''] return model, global_step
303
0
def lowerCAmelCase_ ( __a , __a ) -> list: """simple docstring""" lowerCamelCase__: int =word.split() def justify(__a , __a , __a ) -> str: lowerCamelCase__: Tuple =max_width - width lowerCamelCase__: str =len(__a ) if len(__a ) == 1: # if there is only word in line # just insert overall_spaces_count for the remainder of line return line[0] + " " * overall_spaces_count else: lowerCamelCase__: List[Any] =words_count - 1 # num_spaces_between_words_list[i] : tells you to insert # num_spaces_between_words_list[i] spaces # after word on line[i] lowerCamelCase__: str =spaces_to_insert_between_words * [ overall_spaces_count // spaces_to_insert_between_words ] lowerCamelCase__: str =( overall_spaces_count % spaces_to_insert_between_words ) # distribute spaces via round robin to the left words for i in range(__a ): num_spaces_between_words_list[i] += 1 lowerCamelCase__: Union[str, Any] =[] for i in range(__a ): # add the word aligned_words_list.append(line[i] ) # add the spaces to insert aligned_words_list.append(num_spaces_between_words_list[i] * " " ) # just add the last word to the sentence aligned_words_list.append(line[-1] ) # join the aligned words list to form a justified line return "".join(__a ) lowerCamelCase__: int =[] lowerCamelCase__: list[str] =[] lowerCamelCase__: Any =0 for word in words: if width + len(__a ) + len(__a ) <= max_width: # keep adding words until we can fill out max_width # width = sum of length of all words (without overall_spaces_count) # len(word) = length of current word # len(line) = number of overall_spaces_count to insert between words line.append(__a ) width += len(__a ) else: # justify the line and add it to result answer.append(justify(__a , __a , __a ) ) # reset new line and new width lowerCamelCase__ , lowerCamelCase__: int =[word], len(__a ) lowerCamelCase__: str =max_width - width - len(__a ) answer.append(" ".join(__a ) + (remaining_spaces + 1) * " " ) return answer if __name__ == "__main__": from doctest import testmod testmod()
10
from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { """studio-ousia/luke-base""": """https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json""", """studio-ousia/luke-large""": """https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json""", } class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''luke''' def __init__( self : Any , _A : int=5_0267 , _A : str=50_0000 , _A : Dict=768 , _A : int=256 , _A : Tuple=12 , _A : Optional[Any]=12 , _A : Any=3072 , _A : Tuple="gelu" , _A : Any=0.1 , _A : Dict=0.1 , _A : Any=512 , _A : Tuple=2 , _A : int=0.02 , _A : Any=1e-12 , _A : Dict=True , _A : Optional[Any]=None , _A : List[str]=1 , _A : List[str]=0 , _A : Dict=2 , **_A : List[str] , ): """simple docstring""" super().__init__(pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , **_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = vocab_size __SCREAMING_SNAKE_CASE : Any = entity_vocab_size __SCREAMING_SNAKE_CASE : int = hidden_size __SCREAMING_SNAKE_CASE : List[Any] = entity_emb_size __SCREAMING_SNAKE_CASE : Union[str, Any] = num_hidden_layers __SCREAMING_SNAKE_CASE : Tuple = num_attention_heads __SCREAMING_SNAKE_CASE : Dict = hidden_act __SCREAMING_SNAKE_CASE : Dict = intermediate_size __SCREAMING_SNAKE_CASE : Optional[int] = hidden_dropout_prob __SCREAMING_SNAKE_CASE : Any = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : List[Any] = max_position_embeddings __SCREAMING_SNAKE_CASE : List[str] = type_vocab_size __SCREAMING_SNAKE_CASE : Dict = initializer_range __SCREAMING_SNAKE_CASE : Tuple = layer_norm_eps __SCREAMING_SNAKE_CASE : int = use_entity_aware_attention __SCREAMING_SNAKE_CASE : Any = classifier_dropout
303
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { 'facebook/vit-mae-base': 'https://huggingface.co/facebook/vit-mae-base/resolve/main/config.json', # See all ViT MAE models at https://huggingface.co/models?filter=vit-mae } class lowerCAmelCase__ ( a): '''simple docstring''' __SCREAMING_SNAKE_CASE = "vit_mae" def __init__( self , __lowerCamelCase=7_6_8 , __lowerCamelCase=1_2 , __lowerCamelCase=1_2 , __lowerCamelCase=3_0_7_2 , __lowerCamelCase="gelu" , __lowerCamelCase=0.0 , __lowerCamelCase=0.0 , __lowerCamelCase=0.0_2 , __lowerCamelCase=1e-12 , __lowerCamelCase=2_2_4 , __lowerCamelCase=1_6 , __lowerCamelCase=3 , __lowerCamelCase=True , __lowerCamelCase=1_6 , __lowerCamelCase=5_1_2 , __lowerCamelCase=8 , __lowerCamelCase=2_0_4_8 , __lowerCamelCase=0.7_5 , __lowerCamelCase=False , **__lowerCamelCase , ) -> int: super().__init__(**__lowerCamelCase) _A : int = hidden_size _A : List[str] = num_hidden_layers _A : List[Any] = num_attention_heads _A : Optional[Any] = intermediate_size _A : Optional[int] = hidden_act _A : List[Any] = hidden_dropout_prob _A : List[Any] = attention_probs_dropout_prob _A : Union[str, Any] = initializer_range _A : str = layer_norm_eps _A : Any = image_size _A : int = patch_size _A : int = num_channels _A : Dict = qkv_bias _A : Tuple = decoder_num_attention_heads _A : Tuple = decoder_hidden_size _A : List[str] = decoder_num_hidden_layers _A : Optional[Any] = decoder_intermediate_size _A : List[str] = mask_ratio _A : Union[str, Any] = norm_pix_loss
11
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase_ = { """configuration_electra""": ["""ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ElectraConfig""", """ElectraOnnxConfig"""], """tokenization_electra""": ["""ElectraTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""ElectraTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST""", """ElectraForCausalLM""", """ElectraForMaskedLM""", """ElectraForMultipleChoice""", """ElectraForPreTraining""", """ElectraForQuestionAnswering""", """ElectraForSequenceClassification""", """ElectraForTokenClassification""", """ElectraModel""", """ElectraPreTrainedModel""", """load_tf_weights_in_electra""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFElectraForMaskedLM""", """TFElectraForMultipleChoice""", """TFElectraForPreTraining""", """TFElectraForQuestionAnswering""", """TFElectraForSequenceClassification""", """TFElectraForTokenClassification""", """TFElectraModel""", """TFElectraPreTrainedModel""", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """FlaxElectraForCausalLM""", """FlaxElectraForMaskedLM""", """FlaxElectraForMultipleChoice""", """FlaxElectraForPreTraining""", """FlaxElectraForQuestionAnswering""", """FlaxElectraForSequenceClassification""", """FlaxElectraForTokenClassification""", """FlaxElectraModel""", """FlaxElectraPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_electra import ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ElectraConfig, ElectraOnnxConfig from .tokenization_electra import ElectraTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_electra_fast import ElectraTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_electra import ( ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, ElectraForCausalLM, ElectraForMaskedLM, ElectraForMultipleChoice, ElectraForPreTraining, ElectraForQuestionAnswering, ElectraForSequenceClassification, ElectraForTokenClassification, ElectraModel, ElectraPreTrainedModel, load_tf_weights_in_electra, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_electra import ( TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, TFElectraForMaskedLM, TFElectraForMultipleChoice, TFElectraForPreTraining, TFElectraForQuestionAnswering, TFElectraForSequenceClassification, TFElectraForTokenClassification, TFElectraModel, TFElectraPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_electra import ( FlaxElectraForCausalLM, FlaxElectraForMaskedLM, FlaxElectraForMultipleChoice, FlaxElectraForPreTraining, FlaxElectraForQuestionAnswering, FlaxElectraForSequenceClassification, FlaxElectraForTokenClassification, FlaxElectraModel, FlaxElectraPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
303
0
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 lowerCamelCase__( __lowerCamelCase): UpperCAmelCase__ : Tuple = ['image_processor', 'tokenizer'] UpperCAmelCase__ : Dict = 'Pix2StructImageProcessor' UpperCAmelCase__ : Any = ('T5Tokenizer', 'T5TokenizerFast') def __init__( self: Dict , UpperCamelCase_: Any , UpperCamelCase_: str ): __lowerCamelCase = False super().__init__(UpperCamelCase_ , UpperCamelCase_ ) def __call__( self: List[Any] , UpperCamelCase_: Tuple=None , UpperCamelCase_: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , UpperCamelCase_: bool = True , UpperCamelCase_: Union[bool, str, PaddingStrategy] = False , UpperCamelCase_: Union[bool, str, TruncationStrategy] = None , UpperCamelCase_: Optional[int] = None , UpperCamelCase_: Optional[int] = 20_48 , UpperCamelCase_: int = 0 , UpperCamelCase_: Optional[int] = None , UpperCamelCase_: Optional[bool] = None , UpperCamelCase_: bool = False , UpperCamelCase_: bool = False , UpperCamelCase_: bool = False , UpperCamelCase_: bool = False , UpperCamelCase_: bool = False , UpperCamelCase_: bool = True , UpperCamelCase_: Optional[Union[str, TensorType]] = None , **UpperCamelCase_: Tuple , ): if images is None and text is None: raise ValueError("""You have to specify either images or text.""" ) # Get only text if images is None and not self.image_processor.is_vqa: __lowerCamelCase = self.tokenizer __lowerCamelCase = self.tokenizer( text=UpperCamelCase_ , add_special_tokens=UpperCamelCase_ , padding=UpperCamelCase_ , truncation=UpperCamelCase_ , max_length=UpperCamelCase_ , stride=UpperCamelCase_ , pad_to_multiple_of=UpperCamelCase_ , return_attention_mask=UpperCamelCase_ , return_overflowing_tokens=UpperCamelCase_ , return_special_tokens_mask=UpperCamelCase_ , return_offsets_mapping=UpperCamelCase_ , return_token_type_ids=UpperCamelCase_ , return_length=UpperCamelCase_ , verbose=UpperCamelCase_ , return_tensors=UpperCamelCase_ , **UpperCamelCase_ , ) return text_encoding if not self.image_processor.is_vqa: # add pixel_values __lowerCamelCase = self.image_processor( UpperCamelCase_ , return_tensors=UpperCamelCase_ , max_patches=UpperCamelCase_ , **UpperCamelCase_ ) else: # add pixel_values and bbox __lowerCamelCase = self.image_processor( UpperCamelCase_ , return_tensors=UpperCamelCase_ , max_patches=UpperCamelCase_ , header_text=UpperCamelCase_ , **UpperCamelCase_ ) if text is not None and not self.image_processor.is_vqa: __lowerCamelCase = self.tokenizer( text=UpperCamelCase_ , add_special_tokens=UpperCamelCase_ , padding=UpperCamelCase_ , truncation=UpperCamelCase_ , max_length=UpperCamelCase_ , stride=UpperCamelCase_ , pad_to_multiple_of=UpperCamelCase_ , return_attention_mask=UpperCamelCase_ , return_overflowing_tokens=UpperCamelCase_ , return_special_tokens_mask=UpperCamelCase_ , return_offsets_mapping=UpperCamelCase_ , return_token_type_ids=UpperCamelCase_ , return_length=UpperCamelCase_ , verbose=UpperCamelCase_ , return_tensors=UpperCamelCase_ , **UpperCamelCase_ , ) if "attention_mask" in text_encoding: __lowerCamelCase = text_encoding.pop("""attention_mask""" ) if "input_ids" in text_encoding: __lowerCamelCase = text_encoding.pop("""input_ids""" ) else: __lowerCamelCase = None if text_encoding is not None: encoding_image_processor.update(UpperCamelCase_ ) return encoding_image_processor def lowerCAmelCase__ ( self: Optional[Any] , *UpperCamelCase_: List[str] , **UpperCamelCase_: List[Any] ): return self.tokenizer.batch_decode(*UpperCamelCase_ , **UpperCamelCase_ ) def lowerCAmelCase__ ( self: Union[str, Any] , *UpperCamelCase_: str , **UpperCamelCase_: int ): return self.tokenizer.decode(*UpperCamelCase_ , **UpperCamelCase_ ) @property def lowerCAmelCase__ ( self: Dict ): __lowerCamelCase = self.tokenizer.model_input_names __lowerCamelCase = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
12
from typing import Dict, List, Optional, Tuple, Union import torch from ...models import AutoencoderKL, TransformeraDModel from ...schedulers import KarrasDiffusionSchedulers from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" def __init__( self : List[Any] , _A : TransformeraDModel , _A : AutoencoderKL , _A : KarrasDiffusionSchedulers , _A : Optional[Dict[int, str]] = None , ): """simple docstring""" super().__init__() self.register_modules(transformer=_A , vae=_A , scheduler=_A ) # create a imagenet -> id dictionary for easier use __SCREAMING_SNAKE_CASE : Optional[int] = {} if idalabel is not None: for key, value in idalabel.items(): for label in value.split(''',''' ): __SCREAMING_SNAKE_CASE : Optional[Any] = int(_A ) __SCREAMING_SNAKE_CASE : List[str] = dict(sorted(self.labels.items() ) ) def UpperCAmelCase__ ( self : List[Any] , _A : Union[str, List[str]] ): """simple docstring""" if not isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : Union[str, Any] = list(_A ) for l in label: if l not in self.labels: raise ValueError( F'''{l} does not exist. Please make sure to select one of the following labels: \n {self.labels}.''' ) return [self.labels[l] for l in label] @torch.no_grad() def __call__( self : Dict , _A : List[int] , _A : float = 4.0 , _A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _A : int = 50 , _A : Optional[str] = "pil" , _A : bool = True , ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = len(_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = self.transformer.config.sample_size __SCREAMING_SNAKE_CASE : List[Any] = self.transformer.config.in_channels __SCREAMING_SNAKE_CASE : Optional[int] = randn_tensor( shape=(batch_size, latent_channels, latent_size, latent_size) , generator=_A , device=self.device , dtype=self.transformer.dtype , ) __SCREAMING_SNAKE_CASE : Tuple = torch.cat([latents] * 2 ) if guidance_scale > 1 else latents __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor(_A , device=self.device ).reshape(-1 ) __SCREAMING_SNAKE_CASE : Any = torch.tensor([1000] * batch_size , device=self.device ) __SCREAMING_SNAKE_CASE : Any = torch.cat([class_labels, class_null] , 0 ) if guidance_scale > 1 else class_labels # set step values self.scheduler.set_timesteps(_A ) for t in self.progress_bar(self.scheduler.timesteps ): if guidance_scale > 1: __SCREAMING_SNAKE_CASE : Optional[Any] = latent_model_input[: len(_A ) // 2] __SCREAMING_SNAKE_CASE : List[Any] = torch.cat([half, half] , dim=0 ) __SCREAMING_SNAKE_CASE : int = self.scheduler.scale_model_input(_A , _A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = t if not torch.is_tensor(_A ): # TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can # This would be a good case for the `match` statement (Python 3.10+) __SCREAMING_SNAKE_CASE : Any = latent_model_input.device.type == '''mps''' if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : List[Any] = torch.floataa if is_mps else torch.floataa else: __SCREAMING_SNAKE_CASE : int = torch.intaa if is_mps else torch.intaa __SCREAMING_SNAKE_CASE : int = torch.tensor([timesteps] , dtype=_A , device=latent_model_input.device ) elif len(timesteps.shape ) == 0: __SCREAMING_SNAKE_CASE : Optional[Any] = timesteps[None].to(latent_model_input.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML __SCREAMING_SNAKE_CASE : Optional[int] = timesteps.expand(latent_model_input.shape[0] ) # predict noise model_output __SCREAMING_SNAKE_CASE : Union[str, Any] = self.transformer( _A , timestep=_A , class_labels=_A ).sample # perform guidance if guidance_scale > 1: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = noise_pred[:, :latent_channels], noise_pred[:, latent_channels:] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = torch.split(_A , len(_A ) // 2 , dim=0 ) __SCREAMING_SNAKE_CASE : str = uncond_eps + guidance_scale * (cond_eps - uncond_eps) __SCREAMING_SNAKE_CASE : List[Any] = torch.cat([half_eps, half_eps] , dim=0 ) __SCREAMING_SNAKE_CASE : List[str] = torch.cat([eps, rest] , dim=1 ) # learned sigma if self.transformer.config.out_channels // 2 == latent_channels: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = torch.split(_A , _A , dim=1 ) else: __SCREAMING_SNAKE_CASE : List[Any] = noise_pred # compute previous image: x_t -> x_t-1 __SCREAMING_SNAKE_CASE : str = self.scheduler.step(_A , _A , _A ).prev_sample if guidance_scale > 1: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = latent_model_input.chunk(2 , dim=0 ) else: __SCREAMING_SNAKE_CASE : Optional[Any] = latent_model_input __SCREAMING_SNAKE_CASE : List[Any] = 1 / self.vae.config.scaling_factor * latents __SCREAMING_SNAKE_CASE : List[str] = self.vae.decode(_A ).sample __SCREAMING_SNAKE_CASE : Any = (samples / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 __SCREAMING_SNAKE_CASE : int = samples.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": __SCREAMING_SNAKE_CASE : str = self.numpy_to_pil(_A ) if not return_dict: return (samples,) return ImagePipelineOutput(images=_A )
303
0
import json import os from typing import Optional import numpy as np from ...feature_extraction_utils import BatchFeature from ...processing_utils import ProcessorMixin from ...utils import logging from ...utils.hub import get_file_from_repo from ..auto import AutoTokenizer lowerCAmelCase : str = logging.get_logger(__name__) class __lowercase ( UpperCAmelCase_ ): """simple docstring""" _UpperCAmelCase : Dict = '''AutoTokenizer''' _UpperCAmelCase : Optional[Any] = ['''tokenizer'''] _UpperCAmelCase : Dict = { '''semantic_prompt''': 1, '''coarse_prompt''': 2, '''fine_prompt''': 2, } def __init__( self : str , lowerCAmelCase__ : str , lowerCAmelCase__ : Dict=None): super().__init__(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = speaker_embeddings @classmethod def _SCREAMING_SNAKE_CASE ( cls : Tuple , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple="speaker_embeddings_path.json" , **lowerCAmelCase__ : int): if speaker_embeddings_dict_path is not None: SCREAMING_SNAKE_CASE_: int = get_file_from_repo( lowerCAmelCase__ , lowerCAmelCase__ , subfolder=kwargs.pop("subfolder" , lowerCAmelCase__) , cache_dir=kwargs.pop("cache_dir" , lowerCAmelCase__) , force_download=kwargs.pop("force_download" , lowerCAmelCase__) , proxies=kwargs.pop("proxies" , lowerCAmelCase__) , resume_download=kwargs.pop("resume_download" , lowerCAmelCase__) , local_files_only=kwargs.pop("local_files_only" , lowerCAmelCase__) , use_auth_token=kwargs.pop("use_auth_token" , lowerCAmelCase__) , revision=kwargs.pop("revision" , lowerCAmelCase__) , ) if speaker_embeddings_path is None: logger.warning( F"`{os.path.join(lowerCAmelCase__ , lowerCAmelCase__)}` does not exists\n , no preloaded speaker embeddings will be used - Make sure to provide a correct path to the json\n dictionnary if wanted, otherwise set `speaker_embeddings_dict_path=None`.") SCREAMING_SNAKE_CASE_: Any = None else: with open(lowerCAmelCase__) as speaker_embeddings_json: SCREAMING_SNAKE_CASE_: int = json.load(lowerCAmelCase__) else: SCREAMING_SNAKE_CASE_: Dict = None SCREAMING_SNAKE_CASE_: Optional[Any] = AutoTokenizer.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__) return cls(tokenizer=lowerCAmelCase__ , speaker_embeddings=lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Tuple , lowerCAmelCase__ : Any , lowerCAmelCase__ : int="speaker_embeddings_path.json" , lowerCAmelCase__ : Optional[int]="speaker_embeddings" , lowerCAmelCase__ : bool = False , **lowerCAmelCase__ : Any , ): if self.speaker_embeddings is not None: os.makedirs(os.path.join(lowerCAmelCase__ , lowerCAmelCase__ , "v2") , exist_ok=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = {} SCREAMING_SNAKE_CASE_: List[str] = save_directory for prompt_key in self.speaker_embeddings: if prompt_key != "repo_or_path": SCREAMING_SNAKE_CASE_: Tuple = self._load_voice_preset(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = {} for key in self.speaker_embeddings[prompt_key]: np.save( os.path.join( embeddings_dict["repo_or_path"] , lowerCAmelCase__ , F"{prompt_key}_{key}") , voice_preset[key] , allow_pickle=lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE_: Tuple = os.path.join(lowerCAmelCase__ , F"{prompt_key}_{key}.npy") SCREAMING_SNAKE_CASE_: int = tmp_dict with open(os.path.join(lowerCAmelCase__ , lowerCAmelCase__) , "w") as fp: json.dump(lowerCAmelCase__ , lowerCAmelCase__) super().save_pretrained(lowerCAmelCase__ , lowerCAmelCase__ , **lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : List[Any] , lowerCAmelCase__ : str = None , **lowerCAmelCase__ : Tuple): SCREAMING_SNAKE_CASE_: Optional[Any] = self.speaker_embeddings[voice_preset] SCREAMING_SNAKE_CASE_: int = {} for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset_paths: raise ValueError( F"Voice preset unrecognized, missing {key} as a key in self.speaker_embeddings[{voice_preset}].") SCREAMING_SNAKE_CASE_: Optional[int] = get_file_from_repo( self.speaker_embeddings.get("repo_or_path" , "/") , voice_preset_paths[key] , subfolder=kwargs.pop("subfolder" , lowerCAmelCase__) , cache_dir=kwargs.pop("cache_dir" , lowerCAmelCase__) , force_download=kwargs.pop("force_download" , lowerCAmelCase__) , proxies=kwargs.pop("proxies" , lowerCAmelCase__) , resume_download=kwargs.pop("resume_download" , lowerCAmelCase__) , local_files_only=kwargs.pop("local_files_only" , lowerCAmelCase__) , use_auth_token=kwargs.pop("use_auth_token" , lowerCAmelCase__) , revision=kwargs.pop("revision" , lowerCAmelCase__) , ) if path is None: raise ValueError( F"`{os.path.join(self.speaker_embeddings.get('repo_or_path' , '/') , voice_preset_paths[key])}` does not exists\n , no preloaded voice preset will be used - Make sure to provide correct paths to the {voice_preset}\n embeddings.") SCREAMING_SNAKE_CASE_: Optional[Any] = np.load(lowerCAmelCase__) return voice_preset_dict def _SCREAMING_SNAKE_CASE ( self : Any , lowerCAmelCase__ : Optional[dict] = None): for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset: raise ValueError(F"Voice preset unrecognized, missing {key} as a key.") if not isinstance(voice_preset[key] , np.ndarray): raise ValueError(F"{key} voice preset must be a {str(self.preset_shape[key])}D ndarray.") if len(voice_preset[key].shape) != self.preset_shape[key]: raise ValueError(F"{key} voice preset must be a {str(self.preset_shape[key])}D ndarray.") def __call__( self : Tuple , lowerCAmelCase__ : Dict=None , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : List[str]="pt" , lowerCAmelCase__ : Union[str, Any]=256 , lowerCAmelCase__ : Tuple=False , lowerCAmelCase__ : str=True , lowerCAmelCase__ : List[str]=False , **lowerCAmelCase__ : str , ): if voice_preset is not None and not isinstance(lowerCAmelCase__ , lowerCAmelCase__): if ( isinstance(lowerCAmelCase__ , lowerCAmelCase__) and self.speaker_embeddings is not None and voice_preset in self.speaker_embeddings ): SCREAMING_SNAKE_CASE_: Optional[int] = self._load_voice_preset(lowerCAmelCase__) else: if isinstance(lowerCAmelCase__ , lowerCAmelCase__) and not voice_preset.endswith(".npz"): SCREAMING_SNAKE_CASE_: List[str] = voice_preset + ".npz" SCREAMING_SNAKE_CASE_: Tuple = np.load(lowerCAmelCase__) if voice_preset is not None: self._validate_voice_preset_dict(lowerCAmelCase__ , **lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = BatchFeature(data=lowerCAmelCase__ , tensor_type=lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[str] = self.tokenizer( lowerCAmelCase__ , return_tensors=lowerCAmelCase__ , padding="max_length" , max_length=lowerCAmelCase__ , return_attention_mask=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , **lowerCAmelCase__ , ) if voice_preset is not None: SCREAMING_SNAKE_CASE_: int = voice_preset return encoded_text
13
import os import sys lowercase_ = os.path.join(os.path.dirname(__file__), """src""") sys.path.append(SRC_DIR) from transformers import ( AutoConfig, AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoTokenizer, add_start_docstrings, ) lowercase_ = [ """torch""", """numpy""", """tokenizers""", """filelock""", """requests""", """tqdm""", """regex""", """sentencepiece""", """sacremoses""", """importlib_metadata""", """huggingface_hub""", ] @add_start_docstrings(AutoConfig.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoConfig.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoTokenizer.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoTokenizer.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModel.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModel.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForCausalLM.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForCausalLM.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForMaskedLM.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForMaskedLM.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForSequenceClassification.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForSequenceClassification.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForQuestionAnswering.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForQuestionAnswering.from_pretrained(*snake_case , **snake_case )
303
0
import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert import BertTokenizer _lowerCamelCase : List[str] = logging.get_logger(__name__) _lowerCamelCase : Optional[int] = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} _lowerCamelCase : Optional[Any] = { """vocab_file""": { """facebook/dpr-ctx_encoder-single-nq-base""": ( """https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt""" ), """facebook/dpr-ctx_encoder-multiset-base""": ( """https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt""" ), }, """tokenizer_file""": { """facebook/dpr-ctx_encoder-single-nq-base""": ( """https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json""" ), """facebook/dpr-ctx_encoder-multiset-base""": ( """https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json""" ), }, } _lowerCamelCase : Dict = { """vocab_file""": { """facebook/dpr-question_encoder-single-nq-base""": ( """https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt""" ), """facebook/dpr-question_encoder-multiset-base""": ( """https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt""" ), }, """tokenizer_file""": { """facebook/dpr-question_encoder-single-nq-base""": ( """https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json""" ), """facebook/dpr-question_encoder-multiset-base""": ( """https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json""" ), }, } _lowerCamelCase : str = { """vocab_file""": { """facebook/dpr-reader-single-nq-base""": ( """https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt""" ), """facebook/dpr-reader-multiset-base""": ( """https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt""" ), }, """tokenizer_file""": { """facebook/dpr-reader-single-nq-base""": ( """https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json""" ), """facebook/dpr-reader-multiset-base""": ( """https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json""" ), }, } _lowerCamelCase : Any = { """facebook/dpr-ctx_encoder-single-nq-base""": 512, """facebook/dpr-ctx_encoder-multiset-base""": 512, } _lowerCamelCase : Dict = { """facebook/dpr-question_encoder-single-nq-base""": 512, """facebook/dpr-question_encoder-multiset-base""": 512, } _lowerCamelCase : Optional[int] = { """facebook/dpr-reader-single-nq-base""": 512, """facebook/dpr-reader-multiset-base""": 512, } _lowerCamelCase : Optional[Any] = { """facebook/dpr-ctx_encoder-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-ctx_encoder-multiset-base""": {"""do_lower_case""": True}, } _lowerCamelCase : List[str] = { """facebook/dpr-question_encoder-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-question_encoder-multiset-base""": {"""do_lower_case""": True}, } _lowerCamelCase : Tuple = { """facebook/dpr-reader-single-nq-base""": {"""do_lower_case""": True}, """facebook/dpr-reader-multiset-base""": {"""do_lower_case""": True}, } class UpperCamelCase_ ( UpperCAmelCase__ ): '''simple docstring''' UpperCAmelCase__ = VOCAB_FILES_NAMES UpperCAmelCase__ = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION class UpperCamelCase_ ( UpperCAmelCase__ ): '''simple docstring''' UpperCAmelCase__ = VOCAB_FILES_NAMES UpperCAmelCase__ = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION _lowerCamelCase : List[str] = collections.namedtuple( """DPRSpanPrediction""", ["""span_score""", """relevance_score""", """doc_id""", """start_index""", """end_index""", """text"""] ) _lowerCamelCase : str = collections.namedtuple("""DPRReaderOutput""", ["""start_logits""", """end_logits""", """relevance_logits"""]) _lowerCamelCase : Optional[int] = r""" Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`. It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers), using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)` with the format: ``` [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids> ``` Args: questions (`str` or `List[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. titles (`str` or `List[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. texts (`str` or `List[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different lengths). truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`): Activates and controls truncation. Accepts the following values: - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). max_length (`int`, *optional*): Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors instead of list of python integers. Acceptable values are: - `'tf'`: Return TensorFlow `tf.constant` objects. - `'pt'`: Return PyTorch `torch.Tensor` objects. - `'np'`: Return Numpy `np.ndarray` objects. return_attention_mask (`bool`, *optional*): Whether or not to return the attention mask. If not set, will return the attention mask according to the specific tokenizer's default, defined by the `return_outputs` attribute. [What are attention masks?](../glossary#attention-mask) Returns: `Dict[str, List[List[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. """ @add_start_docstrings(UpperCAmelCase__ ) class UpperCamelCase_ : '''simple docstring''' def __call__( self : Union[str, Any] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[str] = None , UpperCAmelCase__ : Optional[str] = None , UpperCAmelCase__ : Union[bool, str] = False , UpperCAmelCase__ : Union[bool, str] = False , UpperCAmelCase__ : Optional[int] = None , UpperCAmelCase__ : Optional[Union[str, TensorType]] = None , UpperCAmelCase__ : Optional[bool] = None , **UpperCAmelCase__ : List[str] , ) ->BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( UpperCAmelCase__ , padding=UpperCAmelCase__ , truncation=UpperCAmelCase__ , max_length=UpperCAmelCase__ , return_tensors=UpperCAmelCase__ , return_attention_mask=UpperCAmelCase__ , **UpperCAmelCase__ , ) elif titles is None or texts is None: A__ = titles if texts is None else texts return super().__call__( UpperCAmelCase__ , UpperCAmelCase__ , padding=UpperCAmelCase__ , truncation=UpperCAmelCase__ , max_length=UpperCAmelCase__ , return_tensors=UpperCAmelCase__ , return_attention_mask=UpperCAmelCase__ , **UpperCAmelCase__ , ) A__ = titles if not isinstance(UpperCAmelCase__ , UpperCAmelCase__) else [titles] A__ = texts if not isinstance(UpperCAmelCase__ , UpperCAmelCase__) else [texts] A__ = len(UpperCAmelCase__) A__ = questions if not isinstance(UpperCAmelCase__ , UpperCAmelCase__) else [questions] * n_passages if len(UpperCAmelCase__) != len(UpperCAmelCase__): raise ValueError( f"""There should be as many titles than texts but got {len(UpperCAmelCase__)} titles and {len(UpperCAmelCase__)} texts.""") A__ = super().__call__(UpperCAmelCase__ , UpperCAmelCase__ , padding=UpperCAmelCase__ , truncation=UpperCAmelCase__)['''input_ids'''] A__ = super().__call__(UpperCAmelCase__ , add_special_tokens=UpperCAmelCase__ , padding=UpperCAmelCase__ , truncation=UpperCAmelCase__)['''input_ids'''] A__ = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(UpperCAmelCase__ , UpperCAmelCase__) ] } if return_attention_mask is not False: A__ = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id) for input_id in input_ids]) A__ = attention_mask return self.pad(UpperCAmelCase__ , padding=UpperCAmelCase__ , max_length=UpperCAmelCase__ , return_tensors=UpperCAmelCase__) def SCREAMING_SNAKE_CASE ( self : Dict , UpperCAmelCase__ : BatchEncoding , UpperCAmelCase__ : DPRReaderOutput , UpperCAmelCase__ : int = 16 , UpperCAmelCase__ : int = 64 , UpperCAmelCase__ : int = 4 , ) ->List[DPRSpanPrediction]: '''simple docstring''' A__ = reader_input['''input_ids'''] A__ , A__ , A__ = reader_output[:3] A__ = len(UpperCAmelCase__) A__ = sorted(range(UpperCAmelCase__) , reverse=UpperCAmelCase__ , key=relevance_logits.__getitem__) A__ = [] for doc_id in sorted_docs: A__ = list(input_ids[doc_id]) # assuming question & title information is at the beginning of the sequence A__ = sequence_ids.index(self.sep_token_id , 2) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: A__ = sequence_ids.index(self.pad_token_id) else: A__ = len(UpperCAmelCase__) A__ = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=UpperCAmelCase__ , top_spans=UpperCAmelCase__ , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=UpperCAmelCase__ , start_index=UpperCAmelCase__ , end_index=UpperCAmelCase__ , text=self.decode(sequence_ids[start_index : end_index + 1]) , )) if len(UpperCAmelCase__) >= num_spans: break return nbest_spans_predictions[:num_spans] def SCREAMING_SNAKE_CASE ( self : Dict , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : int , ) ->List[DPRSpanPrediction]: '''simple docstring''' A__ = [] for start_index, start_score in enumerate(UpperCAmelCase__): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length]): scores.append(((start_index, start_index + answer_length), start_score + end_score)) A__ = sorted(UpperCAmelCase__ , key=lambda UpperCAmelCase__: x[1] , reverse=UpperCAmelCase__) A__ = [] for (start_index, end_index), score in scores: if start_index > end_index: raise ValueError(f"""Wrong span indices: [{start_index}:{end_index}]""") A__ = end_index - start_index + 1 if length > max_answer_length: raise ValueError(f"""Span is too long: {length} > {max_answer_length}""") if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals): continue chosen_span_intervals.append((start_index, end_index)) if len(UpperCAmelCase__) == top_spans: break return chosen_span_intervals @add_end_docstrings(UpperCAmelCase__ ) class UpperCamelCase_ ( UpperCAmelCase__ , UpperCAmelCase__ ): '''simple docstring''' UpperCAmelCase__ = VOCAB_FILES_NAMES UpperCAmelCase__ = READER_PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ = READER_PRETRAINED_INIT_CONFIGURATION UpperCAmelCase__ = ['''input_ids''', '''attention_mask''']
14
from __future__ import annotations import numpy as np def a__ ( snake_case ): """simple docstring""" return np.maximum(0 , snake_case ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
303
0
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_bart import BartTokenizer SCREAMING_SNAKE_CASE :Optional[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :int = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} # See all BART models at https://huggingface.co/models?filter=bart SCREAMING_SNAKE_CASE :Dict = { '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', }, 'tokenizer_file': { 'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/tokenizer.json', 'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/tokenizer.json', 'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/tokenizer.json', 'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/tokenizer.json', 'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/tokenizer.json', 'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/tokenizer.json', }, } SCREAMING_SNAKE_CASE :Tuple = { 'facebook/bart-base': 1024, 'facebook/bart-large': 1024, 'facebook/bart-large-mnli': 1024, 'facebook/bart-large-cnn': 1024, 'facebook/bart-large-xsum': 1024, 'yjernite/bart_eli5': 1024, } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = ["input_ids", "attention_mask"] snake_case_ = BartTokenizer def __init__( self : Any ,A : Optional[Any]=None ,A : str=None ,A : Optional[Any]=None ,A : Tuple="replace" ,A : List[Any]="<s>" ,A : Dict="</s>" ,A : str="</s>" ,A : Union[str, Any]="<s>" ,A : List[Any]="<unk>" ,A : str="<pad>" ,A : Union[str, Any]="<mask>" ,A : int=False ,A : Optional[int]=True ,**A : Any ,): super().__init__( A ,A ,tokenizer_file=A ,errors=A ,bos_token=A ,eos_token=A ,sep_token=A ,cls_token=A ,unk_token=A ,pad_token=A ,mask_token=A ,add_prefix_space=A ,trim_offsets=A ,**A ,) __A = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" ,A ) != add_prefix_space: __A = getattr(A ,pre_tok_state.pop("type" ) ) __A = add_prefix_space __A = pre_tok_class(**A ) __A = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` __A = "post_processor" __A = getattr(self.backend_tokenizer ,A ,A ) if tokenizer_component_instance: __A = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: __A = tuple(state["sep"] ) if "cls" in state: __A = tuple(state["cls"] ) __A = False if state.get("add_prefix_space" ,A ) != add_prefix_space: __A = add_prefix_space __A = True if state.get("trim_offsets" ,A ) != trim_offsets: __A = trim_offsets __A = True if changes_to_apply: __A = getattr(A ,state.pop("type" ) ) __A = component_class(**A ) setattr(self.backend_tokenizer ,A ,A ) @property def UpperCamelCase_ ( self : Tuple ): if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def UpperCamelCase_ ( self : str ,A : Dict ): __A = AddedToken(A ,lstrip=A ,rstrip=A ) if isinstance(A ,A ) else value __A = value def UpperCamelCase_ ( self : List[str] ,*A : List[Any] ,**A : List[str] ): __A = kwargs.get("is_split_into_words" ,A ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*A ,**A ) def UpperCamelCase_ ( self : Optional[Any] ,*A : Tuple ,**A : Any ): __A = kwargs.get("is_split_into_words" ,A ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*A ,**A ) def UpperCamelCase_ ( self : Tuple ,A : str ,A : Optional[str] = None ): __A = self._tokenizer.model.save(A ,name=A ) return tuple(A ) def UpperCamelCase_ ( self : Union[str, Any] ,A : Optional[Any] ,A : Union[str, Any]=None ): __A = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def UpperCamelCase_ ( self : int ,A : List[int] ,A : Optional[List[int]] = None ): __A = [self.sep_token_id] __A = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
15
def a__ ( snake_case = 1_000_000 ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = 1 __SCREAMING_SNAKE_CASE : Optional[Any] = 1 __SCREAMING_SNAKE_CASE : Optional[int] = {1: 1} for inputa in range(2 , snake_case ): __SCREAMING_SNAKE_CASE : Tuple = 0 __SCREAMING_SNAKE_CASE : Optional[Any] = inputa while True: if number in counters: counter += counters[number] break if number % 2 == 0: number //= 2 counter += 1 else: __SCREAMING_SNAKE_CASE : List[Any] = (3 * number) + 1 counter += 1 if inputa not in counters: __SCREAMING_SNAKE_CASE : str = counter if counter > pre_counter: __SCREAMING_SNAKE_CASE : Optional[int] = inputa __SCREAMING_SNAKE_CASE : str = counter return largest_number if __name__ == "__main__": print(solution(int(input().strip())))
303
0
"""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 lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = '▁' lowerCAmelCase_ = {'vocab_file': 'sentencepiece.bpe.model'} lowerCAmelCase_ = { 'vocab_file': { 'facebook/xglm-564M': 'https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model', } } lowerCAmelCase_ = { 'facebook/xglm-564M': 2_048, } class __A ( A_ ): '''simple docstring''' lowerCAmelCase : List[Any] = VOCAB_FILES_NAMES lowerCAmelCase : Any = PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase : int = ["input_ids", "attention_mask"] def __init__( self : int ,_snake_case : Dict ,_snake_case : Dict="<s>" ,_snake_case : Dict="</s>" ,_snake_case : str="</s>" ,_snake_case : Optional[Any]="<s>" ,_snake_case : Optional[Any]="<unk>" ,_snake_case : Optional[int]="<pad>" ,_snake_case : Optional[Dict[str, Any]] = None ,**_snake_case : str ,) -> None: """simple docstring""" lowercase__ : Any = {} if sp_model_kwargs is None else sp_model_kwargs # Compatibility with the original tokenizer lowercase__ : Any = 7 lowercase__ : Optional[int] = [f"""<madeupword{i}>""" for i in range(self.num_madeup_words )] lowercase__ : Dict = kwargs.get('''additional_special_tokens''' ,[] ) kwargs["additional_special_tokens"] += [ word for word in madeup_words if word not in kwargs["additional_special_tokens"] ] super().__init__( bos_token=_snake_case ,eos_token=_snake_case ,unk_token=_snake_case ,sep_token=_snake_case ,cls_token=_snake_case ,pad_token=_snake_case ,sp_model_kwargs=self.sp_model_kwargs ,**_snake_case ,) lowercase__ : List[str] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_snake_case ) ) lowercase__ : str = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab lowercase__ : Optional[int] = 1 # Mimic fairseq token-to-id alignment for the first 4 token lowercase__ : Optional[int] = {'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3} lowercase__ : List[str] = len(self.sp_model ) lowercase__ : Tuple = {f"""<madeupword{i}>""": sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words )} self.fairseq_tokens_to_ids.update(_snake_case ) lowercase__ : Union[str, Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self : int ) -> Optional[int]: """simple docstring""" lowercase__ : List[Any] = self.__dict__.copy() lowercase__ : Optional[int] = None lowercase__ : Any = self.sp_model.serialized_model_proto() return state def __setstate__( self : Dict ,_snake_case : List[str] ) -> Any: """simple docstring""" lowercase__ : int = d # for backward compatibility if not hasattr(self ,'''sp_model_kwargs''' ): lowercase__ : Dict = {} lowercase__ : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def UpperCAmelCase ( self : Any ,_snake_case : List[int] ,_snake_case : Optional[List[int]] = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return [self.sep_token_id] + token_ids_a lowercase__ : Optional[Any] = [self.sep_token_id] return sep + token_ids_a + sep + sep + token_ids_a def UpperCAmelCase ( self : Any ,_snake_case : List[int] ,_snake_case : Optional[List[int]] = None ,_snake_case : bool = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_snake_case ,token_ids_a=_snake_case ,already_has_special_tokens=_snake_case ) if token_ids_a is None: return [1] + ([0] * len(_snake_case )) return [1] + ([0] * len(_snake_case )) + [1, 1] + ([0] * len(_snake_case )) def UpperCAmelCase ( self : Union[str, Any] ,_snake_case : List[int] ,_snake_case : Optional[List[int]] = None ) -> List[int]: """simple docstring""" lowercase__ : List[Any] = [self.sep_token_id] if token_ids_a is None: return len(sep + token_ids_a ) * [0] return len(sep + token_ids_a + sep + sep + token_ids_a ) * [0] @property def UpperCAmelCase ( self : str ) -> Tuple: """simple docstring""" return len(self.sp_model ) + self.fairseq_offset + self.num_madeup_words def UpperCAmelCase ( self : List[str] ) -> Optional[Any]: """simple docstring""" lowercase__ : Union[str, Any] = {self.convert_ids_to_tokens(_snake_case ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCAmelCase ( self : List[Any] ,_snake_case : str ) -> List[str]: """simple docstring""" return self.sp_model.encode(_snake_case ,out_type=_snake_case ) def UpperCAmelCase ( self : int ,_snake_case : Optional[int] ) -> List[Any]: """simple docstring""" if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] lowercase__ : Tuple = self.sp_model.PieceToId(_snake_case ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def UpperCAmelCase ( self : Any ,_snake_case : List[str] ) -> Any: """simple docstring""" if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def UpperCAmelCase ( self : Tuple ,_snake_case : Tuple ) -> Union[str, Any]: """simple docstring""" lowercase__ : Optional[Any] = ''''''.join(_snake_case ).replace(_snake_case ,''' ''' ).strip() return out_string def UpperCAmelCase ( self : Any ,_snake_case : str ,_snake_case : Optional[str] = None ) -> Tuple[str]: """simple docstring""" if not os.path.isdir(_snake_case ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return lowercase__ : Any = os.path.join( _snake_case ,(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_snake_case ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,_snake_case ) elif not os.path.isfile(self.vocab_file ): with open(_snake_case ,'''wb''' ) as fi: lowercase__ : Dict = self.sp_model.serialized_model_proto() fi.write(_snake_case ) return (out_vocab_file,)
16
import argparse import json import os import pickle import shutil import numpy as np import torch from distiller import Distiller from lm_seqs_dataset import LmSeqsDataset from transformers import ( BertConfig, BertForMaskedLM, BertTokenizer, DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer, GPTaConfig, GPTaLMHeadModel, GPTaTokenizer, RobertaConfig, RobertaForMaskedLM, RobertaTokenizer, ) from utils import git_log, init_gpu_params, logger, set_seed lowercase_ = { """distilbert""": (DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer), """roberta""": (RobertaConfig, RobertaForMaskedLM, RobertaTokenizer), """bert""": (BertConfig, BertForMaskedLM, BertTokenizer), """gpt2""": (GPTaConfig, GPTaLMHeadModel, GPTaTokenizer), } def a__ ( snake_case ): """simple docstring""" assert (args.mlm and args.alpha_mlm > 0.0) or (not args.mlm and args.alpha_mlm == 0.0) assert (args.alpha_mlm > 0.0 and args.alpha_clm == 0.0) or (args.alpha_mlm == 0.0 and args.alpha_clm > 0.0) if args.mlm: assert os.path.isfile(args.token_counts ) assert (args.student_type in ["roberta", "distilbert"]) and (args.teacher_type in ["roberta", "bert"]) else: assert (args.student_type in ["gpt2"]) and (args.teacher_type in ["gpt2"]) assert args.teacher_type == args.student_type or ( args.student_type == "distilbert" and args.teacher_type == "bert" ) assert os.path.isfile(args.student_config ) if args.student_pretrained_weights is not None: assert os.path.isfile(args.student_pretrained_weights ) if args.freeze_token_type_embds: assert args.student_type in ["roberta"] assert args.alpha_ce >= 0.0 assert args.alpha_mlm >= 0.0 assert args.alpha_clm >= 0.0 assert args.alpha_mse >= 0.0 assert args.alpha_cos >= 0.0 assert args.alpha_ce + args.alpha_mlm + args.alpha_clm + args.alpha_mse + args.alpha_cos > 0.0 def a__ ( snake_case , snake_case ): """simple docstring""" if args.student_type == "roberta": __SCREAMING_SNAKE_CASE : int = False elif args.student_type == "gpt2": __SCREAMING_SNAKE_CASE : Optional[int] = False def a__ ( snake_case , snake_case ): """simple docstring""" if args.student_type == "roberta": __SCREAMING_SNAKE_CASE : Dict = False def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = argparse.ArgumentParser(description='''Training''' ) parser.add_argument('''--force''' , action='''store_true''' , help='''Overwrite dump_path if it already exists.''' ) parser.add_argument( '''--dump_path''' , type=snake_case , required=snake_case , help='''The output directory (log, checkpoints, parameters, etc.)''' ) parser.add_argument( '''--data_file''' , type=snake_case , required=snake_case , help='''The binarized file (tokenized + tokens_to_ids) and grouped by sequence.''' , ) parser.add_argument( '''--student_type''' , type=snake_case , choices=['''distilbert''', '''roberta''', '''gpt2'''] , required=snake_case , help='''The student type (DistilBERT, RoBERTa).''' , ) parser.add_argument('''--student_config''' , type=snake_case , required=snake_case , help='''Path to the student configuration.''' ) parser.add_argument( '''--student_pretrained_weights''' , default=snake_case , type=snake_case , help='''Load student initialization checkpoint.''' ) parser.add_argument( '''--teacher_type''' , choices=['''bert''', '''roberta''', '''gpt2'''] , required=snake_case , help='''Teacher type (BERT, RoBERTa).''' ) parser.add_argument('''--teacher_name''' , type=snake_case , required=snake_case , help='''The teacher model.''' ) parser.add_argument('''--temperature''' , default=2.0 , type=snake_case , help='''Temperature for the softmax temperature.''' ) parser.add_argument( '''--alpha_ce''' , default=0.5 , type=snake_case , help='''Linear weight for the distillation loss. Must be >=0.''' ) parser.add_argument( '''--alpha_mlm''' , default=0.0 , type=snake_case , help='''Linear weight for the MLM loss. Must be >=0. Should be used in conjunction with `mlm` flag.''' , ) parser.add_argument('''--alpha_clm''' , default=0.5 , type=snake_case , help='''Linear weight for the CLM loss. Must be >=0.''' ) parser.add_argument('''--alpha_mse''' , default=0.0 , type=snake_case , help='''Linear weight of the MSE loss. Must be >=0.''' ) parser.add_argument( '''--alpha_cos''' , default=0.0 , type=snake_case , help='''Linear weight of the cosine embedding loss. Must be >=0.''' ) parser.add_argument( '''--mlm''' , action='''store_true''' , help='''The LM step: MLM or CLM. If `mlm` is True, the MLM is used over CLM.''' ) parser.add_argument( '''--mlm_mask_prop''' , default=0.15 , type=snake_case , help='''Proportion of tokens for which we need to make a prediction.''' , ) parser.add_argument('''--word_mask''' , default=0.8 , type=snake_case , help='''Proportion of tokens to mask out.''' ) parser.add_argument('''--word_keep''' , default=0.1 , type=snake_case , help='''Proportion of tokens to keep.''' ) parser.add_argument('''--word_rand''' , default=0.1 , type=snake_case , help='''Proportion of tokens to randomly replace.''' ) parser.add_argument( '''--mlm_smoothing''' , default=0.7 , type=snake_case , help='''Smoothing parameter to emphasize more rare tokens (see XLM, similar to word2vec).''' , ) parser.add_argument('''--token_counts''' , type=snake_case , help='''The token counts in the data_file for MLM.''' ) parser.add_argument( '''--restrict_ce_to_mask''' , action='''store_true''' , help='''If true, compute the distillation loss only the [MLM] prediction distribution.''' , ) parser.add_argument( '''--freeze_pos_embs''' , action='''store_true''' , help='''Freeze positional embeddings during distillation. For student_type in [\'roberta\', \'gpt2\'] only.''' , ) parser.add_argument( '''--freeze_token_type_embds''' , action='''store_true''' , help='''Freeze token type embeddings during distillation if existent. For student_type in [\'roberta\'] only.''' , ) parser.add_argument('''--n_epoch''' , type=snake_case , default=3 , help='''Number of pass on the whole dataset.''' ) parser.add_argument('''--batch_size''' , type=snake_case , default=5 , help='''Batch size (for each process).''' ) parser.add_argument( '''--group_by_size''' , action='''store_false''' , help='''If true, group sequences that have similar length into the same batch. Default is true.''' , ) parser.add_argument( '''--gradient_accumulation_steps''' , type=snake_case , default=50 , help='''Gradient accumulation for larger training batches.''' , ) parser.add_argument('''--warmup_prop''' , default=0.05 , type=snake_case , help='''Linear warmup proportion.''' ) parser.add_argument('''--weight_decay''' , default=0.0 , type=snake_case , help='''Weight decay if we apply some.''' ) parser.add_argument('''--learning_rate''' , default=5E-4 , type=snake_case , help='''The initial learning rate for Adam.''' ) parser.add_argument('''--adam_epsilon''' , default=1E-6 , type=snake_case , help='''Epsilon for Adam optimizer.''' ) parser.add_argument('''--max_grad_norm''' , default=5.0 , type=snake_case , help='''Max gradient norm.''' ) parser.add_argument('''--initializer_range''' , default=0.02 , type=snake_case , help='''Random initialization range.''' ) parser.add_argument( '''--fp16''' , action='''store_true''' , help='''Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit''' , ) parser.add_argument( '''--fp16_opt_level''' , type=snake_case , default='''O1''' , help=( '''For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\'].''' '''See details at https://nvidia.github.io/apex/amp.html''' ) , ) parser.add_argument('''--n_gpu''' , type=snake_case , default=1 , help='''Number of GPUs in the node.''' ) parser.add_argument('''--local_rank''' , type=snake_case , default=-1 , help='''Distributed training - Local rank''' ) parser.add_argument('''--seed''' , type=snake_case , default=56 , help='''Random seed''' ) parser.add_argument('''--log_interval''' , type=snake_case , default=500 , help='''Tensorboard logging interval.''' ) parser.add_argument('''--checkpoint_interval''' , type=snake_case , default=4_000 , help='''Checkpoint interval.''' ) __SCREAMING_SNAKE_CASE : Optional[int] = parser.parse_args() sanity_checks(snake_case ) # ARGS # init_gpu_params(snake_case ) set_seed(snake_case ) if args.is_master: if os.path.exists(args.dump_path ): if not args.force: raise ValueError( F'''Serialization dir {args.dump_path} already exists, but you have not precised wheter to overwrite''' ''' itUse `--force` if you want to overwrite it''' ) else: shutil.rmtree(args.dump_path ) if not os.path.exists(args.dump_path ): os.makedirs(args.dump_path ) logger.info(F'''Experiment will be dumped and logged in {args.dump_path}''' ) # SAVE PARAMS # logger.info(F'''Param: {args}''' ) with open(os.path.join(args.dump_path , '''parameters.json''' ) , '''w''' ) as f: json.dump(vars(snake_case ) , snake_case , indent=4 ) git_log(args.dump_path ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = MODEL_CLASSES[args.student_type] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = MODEL_CLASSES[args.teacher_type] # TOKENIZER # __SCREAMING_SNAKE_CASE : Optional[int] = teacher_tokenizer_class.from_pretrained(args.teacher_name ) __SCREAMING_SNAKE_CASE : Optional[Any] = {} for tok_name, tok_symbol in tokenizer.special_tokens_map.items(): __SCREAMING_SNAKE_CASE : Any = tokenizer.all_special_tokens.index(snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.all_special_ids[idx] logger.info(F'''Special tokens {special_tok_ids}''' ) __SCREAMING_SNAKE_CASE : Any = special_tok_ids __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.max_model_input_sizes[args.teacher_name] # DATA LOADER # logger.info(F'''Loading data from {args.data_file}''' ) with open(args.data_file , '''rb''' ) as fp: __SCREAMING_SNAKE_CASE : List[str] = pickle.load(snake_case ) if args.mlm: logger.info(F'''Loading token counts from {args.token_counts} (already pre-computed)''' ) with open(args.token_counts , '''rb''' ) as fp: __SCREAMING_SNAKE_CASE : Optional[Any] = pickle.load(snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = np.maximum(snake_case , 1 ) ** -args.mlm_smoothing for idx in special_tok_ids.values(): __SCREAMING_SNAKE_CASE : Any = 0.0 # do not predict special tokens __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.from_numpy(snake_case ) else: __SCREAMING_SNAKE_CASE : Optional[int] = None __SCREAMING_SNAKE_CASE : Optional[Any] = LmSeqsDataset(params=snake_case , data=snake_case ) logger.info('''Data loader created.''' ) # STUDENT # logger.info(F'''Loading student config from {args.student_config}''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = student_config_class.from_pretrained(args.student_config ) __SCREAMING_SNAKE_CASE : Dict = True if args.student_pretrained_weights is not None: logger.info(F'''Loading pretrained weights from {args.student_pretrained_weights}''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = student_model_class.from_pretrained(args.student_pretrained_weights , config=snake_case ) else: __SCREAMING_SNAKE_CASE : str = student_model_class(snake_case ) if args.n_gpu > 0: student.to(F'''cuda:{args.local_rank}''' ) logger.info('''Student loaded.''' ) # TEACHER # __SCREAMING_SNAKE_CASE : List[str] = teacher_model_class.from_pretrained(args.teacher_name , output_hidden_states=snake_case ) if args.n_gpu > 0: teacher.to(F'''cuda:{args.local_rank}''' ) logger.info(F'''Teacher loaded from {args.teacher_name}.''' ) # FREEZING # if args.freeze_pos_embs: freeze_pos_embeddings(snake_case , snake_case ) if args.freeze_token_type_embds: freeze_token_type_embeddings(snake_case , snake_case ) # SANITY CHECKS # assert student.config.vocab_size == teacher.config.vocab_size assert student.config.hidden_size == teacher.config.hidden_size assert student.config.max_position_embeddings == teacher.config.max_position_embeddings if args.mlm: assert token_probs.size(0 ) == stu_architecture_config.vocab_size # DISTILLER # torch.cuda.empty_cache() __SCREAMING_SNAKE_CASE : int = Distiller( params=snake_case , dataset=snake_case , token_probs=snake_case , student=snake_case , teacher=snake_case ) distiller.train() logger.info('''Let\'s go get some drinks.''' ) if __name__ == "__main__": main()
303
0
"""simple docstring""" # coding=utf-8 # Copyright 2020 The HuggingFace Inc. team. # # 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. # this script dumps information about the environment import os import sys import transformers _a = '3' print('Python version:', sys.version) print('transformers version:', transformers.__version__) try: import torch print('Torch version:', torch.__version__) print('Cuda available:', torch.cuda.is_available()) print('Cuda version:', torch.version.cuda) print('CuDNN version:', torch.backends.cudnn.version()) print('Number of GPUs available:', torch.cuda.device_count()) print('NCCL version:', torch.cuda.nccl.version()) except ImportError: print('Torch version:', None) try: import deepspeed print('DeepSpeed version:', deepspeed.__version__) except ImportError: print('DeepSpeed version:', None) try: import tensorflow as tf print('TensorFlow version:', tf.__version__) print('TF GPUs available:', bool(tf.config.list_physical_devices('GPU'))) print('Number of TF GPUs available:', len(tf.config.list_physical_devices('GPU'))) except ImportError: print('TensorFlow version:', None)
17
import math import os import sys def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = '''''' try: with open(snake_case , '''rb''' ) as binary_file: __SCREAMING_SNAKE_CASE : int = binary_file.read() for dat in data: __SCREAMING_SNAKE_CASE : Optional[Any] = F'''{dat:08b}''' result += curr_byte return result except OSError: print('''File not accessible''' ) sys.exit() def a__ ( snake_case , snake_case , snake_case , snake_case ): """simple docstring""" lexicon.pop(snake_case ) __SCREAMING_SNAKE_CASE : List[str] = last_match_id if math.loga(snake_case ).is_integer(): for curr_key in lexicon: __SCREAMING_SNAKE_CASE : int = '''0''' + lexicon[curr_key] __SCREAMING_SNAKE_CASE : List[str] = bin(snake_case )[2:] def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = {'''0''': '''0''', '''1''': '''1'''} __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Any = '''''', '''''' __SCREAMING_SNAKE_CASE : Optional[Any] = len(snake_case ) for i in range(len(snake_case ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue __SCREAMING_SNAKE_CASE : Any = lexicon[curr_string] result += last_match_id add_key_to_lexicon(snake_case , snake_case , snake_case , snake_case ) index += 1 __SCREAMING_SNAKE_CASE : Tuple = '''''' while curr_string != "" and curr_string not in lexicon: curr_string += "0" if curr_string != "": __SCREAMING_SNAKE_CASE : Dict = lexicon[curr_string] result += last_match_id return result def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = os.path.getsize(snake_case ) __SCREAMING_SNAKE_CASE : Union[str, Any] = bin(snake_case )[2:] __SCREAMING_SNAKE_CASE : int = len(snake_case ) return "0" * (length_length - 1) + file_length_binary + compressed def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = 8 try: with open(snake_case , '''wb''' ) as opened_file: __SCREAMING_SNAKE_CASE : Optional[int] = [ to_write[i : i + byte_length] for i in range(0 , len(snake_case ) , snake_case ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append('''10000000''' ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array: opened_file.write(int(snake_case , 2 ).to_bytes(1 , byteorder='''big''' ) ) except OSError: print('''File not accessible''' ) sys.exit() def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = read_file_binary(snake_case ) __SCREAMING_SNAKE_CASE : Optional[int] = compress_data(snake_case ) __SCREAMING_SNAKE_CASE : Dict = add_file_length(snake_case , snake_case ) write_file_binary(snake_case , snake_case ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
303
0
__lowerCamelCase : List[str] = {str(digit): digit**5 for digit in range(10)} def _snake_case ( lowerCAmelCase : int ): """simple docstring""" return sum(DIGITS_FIFTH_POWER[digit] for digit in str(lowerCAmelCase ) ) def _snake_case ( ): """simple docstring""" return sum( number for number in range(1_0_0_0 , 1_0_0_0_0_0_0 ) if number == digits_fifth_powers_sum(lowerCAmelCase ) ) if __name__ == "__main__": print(solution())
18
import unittest import numpy as np import torch from torch import nn from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import enable_full_determinism, skip_mps from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __UpperCamelCase ( lowerCAmelCase__ , unittest.TestCase ): """simple docstring""" lowerCAmelCase_ = KandinskyVaaPriorPipeline lowerCAmelCase_ = ['''prompt'''] lowerCAmelCase_ = ['''prompt''', '''negative_prompt'''] lowerCAmelCase_ = [ '''num_images_per_prompt''', '''generator''', '''num_inference_steps''', '''latents''', '''negative_prompt''', '''guidance_scale''', '''output_type''', '''return_dict''', ] lowerCAmelCase_ = False @property def UpperCAmelCase__ ( self : int ): """simple docstring""" return 32 @property def UpperCAmelCase__ ( self : List[str] ): """simple docstring""" return 32 @property def UpperCAmelCase__ ( self : Dict ): """simple docstring""" return self.time_input_dim @property def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" return self.time_input_dim * 4 @property def UpperCAmelCase__ ( self : List[Any] ): """simple docstring""" return 100 @property def UpperCAmelCase__ ( self : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) return tokenizer @property def UpperCAmelCase__ ( self : List[Any] ): """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : List[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(_A ) @property def UpperCAmelCase__ ( self : str ): """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : Dict = { '''num_attention_heads''': 2, '''attention_head_dim''': 12, '''embedding_dim''': self.text_embedder_hidden_size, '''num_layers''': 1, } __SCREAMING_SNAKE_CASE : Optional[Any] = PriorTransformer(**_A ) # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0 __SCREAMING_SNAKE_CASE : List[Any] = nn.Parameter(torch.ones(model.clip_std.shape ) ) return model @property def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : int = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , image_size=224 , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_channels=3 , num_hidden_layers=5 , patch_size=14 , ) __SCREAMING_SNAKE_CASE : str = CLIPVisionModelWithProjection(_A ) return model @property def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = CLIPImageProcessor( crop_size=224 , do_center_crop=_A , do_normalize=_A , do_resize=_A , image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73] , image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11] , resample=3 , size=224 , ) return image_processor def UpperCAmelCase__ ( self : Tuple ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = self.dummy_prior __SCREAMING_SNAKE_CASE : str = self.dummy_image_encoder __SCREAMING_SNAKE_CASE : Optional[int] = self.dummy_text_encoder __SCREAMING_SNAKE_CASE : List[Any] = self.dummy_tokenizer __SCREAMING_SNAKE_CASE : Union[str, Any] = self.dummy_image_processor __SCREAMING_SNAKE_CASE : str = UnCLIPScheduler( variance_type='''fixed_small_log''' , prediction_type='''sample''' , num_train_timesteps=1000 , clip_sample=_A , clip_sample_range=10.0 , ) __SCREAMING_SNAKE_CASE : int = { '''prior''': prior, '''image_encoder''': image_encoder, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''scheduler''': scheduler, '''image_processor''': image_processor, } return components def UpperCAmelCase__ ( self : Union[str, Any] , _A : int , _A : Dict=0 ): """simple docstring""" if str(_A ).startswith('''mps''' ): __SCREAMING_SNAKE_CASE : Tuple = torch.manual_seed(_A ) else: __SCREAMING_SNAKE_CASE : str = torch.Generator(device=_A ).manual_seed(_A ) __SCREAMING_SNAKE_CASE : List[str] = { '''prompt''': '''horse''', '''generator''': generator, '''guidance_scale''': 4.0, '''num_inference_steps''': 2, '''output_type''': '''np''', } return inputs def UpperCAmelCase__ ( self : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = '''cpu''' __SCREAMING_SNAKE_CASE : Any = self.get_dummy_components() __SCREAMING_SNAKE_CASE : Any = self.pipeline_class(**_A ) __SCREAMING_SNAKE_CASE : List[Any] = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) __SCREAMING_SNAKE_CASE : int = pipe(**self.get_dummy_inputs(_A ) ) __SCREAMING_SNAKE_CASE : Tuple = output.image_embeds __SCREAMING_SNAKE_CASE : Optional[Any] = pipe( **self.get_dummy_inputs(_A ) , return_dict=_A , )[0] __SCREAMING_SNAKE_CASE : Tuple = image[0, -10:] __SCREAMING_SNAKE_CASE : List[Any] = image_from_tuple[0, -10:] assert image.shape == (1, 32) __SCREAMING_SNAKE_CASE : List[str] = np.array( [-0.05_32, 1.71_20, 0.36_56, -1.08_52, -0.89_46, -1.17_56, 0.43_48, 0.24_82, 0.51_46, -0.11_56] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @skip_mps def UpperCAmelCase__ ( self : List[str] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = torch_device == '''cpu''' __SCREAMING_SNAKE_CASE : Any = True __SCREAMING_SNAKE_CASE : int = False self._test_inference_batch_single_identical( test_max_difference=_A , relax_max_difference=_A , test_mean_pixel_difference=_A , ) @skip_mps def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = torch_device == '''cpu''' __SCREAMING_SNAKE_CASE : List[Any] = False self._test_attention_slicing_forward_pass( test_max_difference=_A , test_mean_pixel_difference=_A , )
303
0
from collections import deque class _SCREAMING_SNAKE_CASE : def __init__( self , lowercase , lowercase , lowercase ) -> None: lowerCamelCase_ = process_name # process name lowerCamelCase_ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time lowerCamelCase_ = arrival_time lowerCamelCase_ = burst_time # remaining burst time lowerCamelCase_ = 0 # total time of the process wait in ready queue lowerCamelCase_ = 0 # time from arrival time to completion time class _SCREAMING_SNAKE_CASE : def __init__( self , lowercase , lowercase , lowercase , lowercase , ) -> None: # total number of mlfq's queues lowerCamelCase_ = number_of_queues # time slice of queues that round robin algorithm applied lowerCamelCase_ = time_slices # unfinished process is in this ready_queue lowerCamelCase_ = queue # current time lowerCamelCase_ = current_time # finished process is in this sequence queue lowerCamelCase_ = deque() def SCREAMING_SNAKE_CASE_( self ) -> list[str]: lowerCamelCase_ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def SCREAMING_SNAKE_CASE_( self , lowercase ) -> list[int]: lowerCamelCase_ = [] for i in range(len(lowercase ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def SCREAMING_SNAKE_CASE_( self , lowercase ) -> list[int]: lowerCamelCase_ = [] for i in range(len(lowercase ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def SCREAMING_SNAKE_CASE_( self , lowercase ) -> list[int]: lowerCamelCase_ = [] for i in range(len(lowercase ) ): completion_times.append(queue[i].stop_time ) return completion_times def SCREAMING_SNAKE_CASE_( self , lowercase ) -> list[int]: return [q.burst_time for q in queue] def SCREAMING_SNAKE_CASE_( self , lowercase ) -> int: process.waiting_time += self.current_time - process.stop_time return process.waiting_time def SCREAMING_SNAKE_CASE_( self , lowercase ) -> deque[Process]: lowerCamelCase_ = deque() # sequence deque of finished process while len(lowercase ) != 0: lowerCamelCase_ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(lowercase ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 lowerCamelCase_ = 0 # set the process's turnaround time because it is finished lowerCamelCase_ = self.current_time - cp.arrival_time # set the completion time lowerCamelCase_ = self.current_time # add the process to queue that has finished queue finished.append(lowercase ) self.finish_queue.extend(lowercase ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def SCREAMING_SNAKE_CASE_( self , lowercase , lowercase ) -> tuple[deque[Process], deque[Process]]: lowerCamelCase_ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(lowercase ) ): lowerCamelCase_ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(lowercase ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time lowerCamelCase_ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(lowercase ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished lowerCamelCase_ = 0 # set the finish time lowerCamelCase_ = self.current_time # update the process' turnaround time because it is finished lowerCamelCase_ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(lowercase ) self.finish_queue.extend(lowercase ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def SCREAMING_SNAKE_CASE_( self ) -> deque[Process]: # all queues except last one have round_robin algorithm for i in range(self.number_of_queues - 1 ): lowerCamelCase_ , lowerCamelCase_ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest __A =Process('''P1''', 0, 5_3) __A =Process('''P2''', 0, 1_7) __A =Process('''P3''', 0, 6_8) __A =Process('''P4''', 0, 2_4) __A =3 __A =[1_7, 2_5] __A =deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) __A =Process('''P1''', 0, 5_3) __A =Process('''P2''', 0, 1_7) __A =Process('''P3''', 0, 6_8) __A =Process('''P4''', 0, 2_4) __A =3 __A =[1_7, 2_5] __A =deque([Pa, Pa, Pa, Pa]) __A =MLFQ(number_of_queues, time_slices, queue, 0) __A =mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( F"""waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}""" ) # print completion times of processes(P1, P2, P3, P4) print( F"""completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}""" ) # print total turnaround times of processes(P1, P2, P3, P4) print( F"""turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}""" ) # print sequence of finished processes print( F"""sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}""" )
19
import argparse import logging import pickle import random import time import numpy as np from transformers import BertTokenizer, GPTaTokenizer, RobertaTokenizer logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""", datefmt="""%m/%d/%Y %H:%M:%S""", level=logging.INFO ) lowercase_ = logging.getLogger(__name__) def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = argparse.ArgumentParser( description='''Preprocess the data to avoid re-doing it several times by (tokenization + token_to_ids).''' ) parser.add_argument('''--file_path''' , type=snake_case , default='''data/dump.txt''' , help='''The path to the data.''' ) parser.add_argument('''--tokenizer_type''' , type=snake_case , default='''bert''' , choices=['''bert''', '''roberta''', '''gpt2'''] ) parser.add_argument('''--tokenizer_name''' , type=snake_case , default='''bert-base-uncased''' , help='''The tokenizer to use.''' ) parser.add_argument('''--dump_file''' , type=snake_case , default='''data/dump''' , help='''The dump file prefix.''' ) __SCREAMING_SNAKE_CASE : List[Any] = parser.parse_args() logger.info(F'''Loading Tokenizer ({args.tokenizer_name})''' ) if args.tokenizer_type == "bert": __SCREAMING_SNAKE_CASE : Union[str, Any] = BertTokenizer.from_pretrained(args.tokenizer_name ) __SCREAMING_SNAKE_CASE : List[str] = tokenizer.special_tokens_map['''cls_token'''] # `[CLS]` __SCREAMING_SNAKE_CASE : List[str] = tokenizer.special_tokens_map['''sep_token'''] # `[SEP]` elif args.tokenizer_type == "roberta": __SCREAMING_SNAKE_CASE : List[str] = RobertaTokenizer.from_pretrained(args.tokenizer_name ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.special_tokens_map['''cls_token'''] # `<s>` __SCREAMING_SNAKE_CASE : Optional[int] = tokenizer.special_tokens_map['''sep_token'''] # `</s>` elif args.tokenizer_type == "gpt2": __SCREAMING_SNAKE_CASE : Dict = GPTaTokenizer.from_pretrained(args.tokenizer_name ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.special_tokens_map['''bos_token'''] # `<|endoftext|>` __SCREAMING_SNAKE_CASE : str = tokenizer.special_tokens_map['''eos_token'''] # `<|endoftext|>` logger.info(F'''Loading text from {args.file_path}''' ) with open(args.file_path , '''r''' , encoding='''utf8''' ) as fp: __SCREAMING_SNAKE_CASE : str = fp.readlines() logger.info('''Start encoding''' ) logger.info(F'''{len(snake_case )} examples to process.''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = [] __SCREAMING_SNAKE_CASE : Dict = 0 __SCREAMING_SNAKE_CASE : List[str] = 10_000 __SCREAMING_SNAKE_CASE : Dict = time.time() for text in data: __SCREAMING_SNAKE_CASE : Optional[int] = F'''{bos} {text.strip()} {sep}''' __SCREAMING_SNAKE_CASE : Tuple = tokenizer.encode(snake_case , add_special_tokens=snake_case ) rslt.append(snake_case ) iter += 1 if iter % interval == 0: __SCREAMING_SNAKE_CASE : List[str] = time.time() logger.info(F'''{iter} examples processed. - {(end-start):.2f}s/{interval}expl''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = time.time() logger.info('''Finished binarization''' ) logger.info(F'''{len(snake_case )} examples processed.''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = F'''{args.dump_file}.{args.tokenizer_name}.pickle''' __SCREAMING_SNAKE_CASE : str = tokenizer.vocab_size if vocab_size < (1 << 16): __SCREAMING_SNAKE_CASE : List[str] = [np.uintaa(snake_case ) for d in rslt] else: __SCREAMING_SNAKE_CASE : Optional[int] = [np.intaa(snake_case ) for d in rslt] random.shuffle(rslt_ ) logger.info(F'''Dump to {dp_file}''' ) with open(snake_case , '''wb''' ) as handle: pickle.dump(rslt_ , snake_case , protocol=pickle.HIGHEST_PROTOCOL ) if __name__ == "__main__": main()
303
0
import argparse import os import torch from transformers import FlavaConfig, FlavaForPreTraining from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint def _snake_case( SCREAMING_SNAKE_CASE__ ) -> Optional[int]: # encoder.embeddings are double copied in original FLAVA return sum(param.float().sum() if """encoder.embeddings""" not in key else 0 for key, param in state_dict.items() ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : Tuple = {} for key, value in state_dict.items(): if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key: continue lowercase : Optional[int] = key.replace("""heads.cmd.mim_head.cls.predictions""" , """mmm_image_head""" ) lowercase : Tuple = key.replace("""heads.cmd.mlm_head.cls.predictions""" , """mmm_text_head""" ) lowercase : Tuple = key.replace("""heads.cmd.itm_head.cls""" , """itm_head""" ) lowercase : List[str] = key.replace("""heads.cmd.itm_head.pooler""" , """itm_head.pooler""" ) lowercase : List[Any] = key.replace("""heads.cmd.clip_head.logit_scale""" , """flava.logit_scale""" ) lowercase : Optional[int] = key.replace("""heads.fairseq_mlm.cls.predictions""" , """mlm_head""" ) lowercase : int = key.replace("""heads.imagenet.mim_head.cls.predictions""" , """mim_head""" ) lowercase : List[Any] = key.replace("""mm_text_projection""" , """flava.text_to_mm_projection""" ) lowercase : List[str] = key.replace("""mm_image_projection""" , """flava.image_to_mm_projection""" ) lowercase : Optional[Any] = key.replace("""image_encoder.module""" , """flava.image_model""" ) lowercase : Dict = key.replace("""text_encoder.module""" , """flava.text_model""" ) lowercase : Dict = key.replace("""mm_encoder.module.encoder.cls_token""" , """flava.multimodal_model.cls_token""" ) lowercase : Any = key.replace("""mm_encoder.module""" , """flava.multimodal_model""" ) lowercase : Tuple = key.replace("""text_projection""" , """flava.text_projection""" ) lowercase : List[Any] = key.replace("""image_projection""" , """flava.image_projection""" ) lowercase : Union[str, Any] = value.float() for key, value in codebook_state_dict.items(): lowercase : Dict = value return upgrade @torch.no_grad() def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None ) -> Dict: if config_path is not None: lowercase : Optional[int] = FlavaConfig.from_pretrained(SCREAMING_SNAKE_CASE__ ) else: lowercase : Dict = FlavaConfig() lowercase : Any = FlavaForPreTraining(SCREAMING_SNAKE_CASE__ ).eval() lowercase : Optional[Any] = convert_dalle_checkpoint(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , save_checkpoint=SCREAMING_SNAKE_CASE__ ) if os.path.exists(SCREAMING_SNAKE_CASE__ ): lowercase : int = torch.load(SCREAMING_SNAKE_CASE__ , map_location="""cpu""" ) else: lowercase : List[str] = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE__ , map_location="""cpu""" ) lowercase : Union[str, Any] = upgrade_state_dict(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) hf_model.load_state_dict(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[int] = hf_model.state_dict() lowercase : Dict = count_parameters(SCREAMING_SNAKE_CASE__ ) lowercase : str = count_parameters(SCREAMING_SNAKE_CASE__ ) + count_parameters(SCREAMING_SNAKE_CASE__ ) assert torch.allclose(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , atol=1e-3 ) hf_model.save_pretrained(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": lowercase : Dict = argparse.ArgumentParser() parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to flava checkpoint""") parser.add_argument("""--codebook_path""", default=None, type=str, help="""Path to flava codebook checkpoint""") parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") lowercase : List[str] = parser.parse_args() convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)
20
import numpy as np import torch from imwatermark import WatermarkEncoder # Copied from https://github.com/Stability-AI/generative-models/blob/613af104c6b85184091d42d374fef420eddb356d/scripts/demo/streamlit_helpers.py#L66 lowercase_ = 0b1011_0011_1110_1100_1001_0000_0111_1011_1011_0001_1001_1110 # bin(x)[2:] gives bits of x as str, use int to convert them to 0/1 lowercase_ = [int(bit) for bit in bin(WATERMARK_MESSAGE)[2:]] class __UpperCamelCase : """simple docstring""" def __init__( self : Dict ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = WATERMARK_BITS __SCREAMING_SNAKE_CASE : Optional[int] = WatermarkEncoder() self.encoder.set_watermark('''bits''' , self.watermark ) def UpperCAmelCase__ ( self : List[Any] , _A : torch.FloatTensor ): """simple docstring""" if images.shape[-1] < 256: return images __SCREAMING_SNAKE_CASE : Union[str, Any] = (255 * (images / 2 + 0.5)).cpu().permute(0 , 2 , 3 , 1 ).float().numpy() __SCREAMING_SNAKE_CASE : Dict = [self.encoder.encode(_A , '''dwtDct''' ) for image in images] __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.from_numpy(np.array(_A ) ).permute(0 , 3 , 1 , 2 ) __SCREAMING_SNAKE_CASE : Optional[Any] = torch.clamp(2 * (images / 255 - 0.5) , min=-1.0 , max=1.0 ) return images
303
0
import argparse import json import os import tensorstore as ts import torch from flax import serialization from flax.traverse_util import flatten_dict, unflatten_dict from tensorflow.io import gfile from transformers.modeling_utils import dtype_byte_size from transformers.models.switch_transformers.convert_switch_transformers_original_flax_checkpoint_to_pytorch import ( rename_keys, ) from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME from transformers.utils.hub import convert_file_size_to_int def UpperCamelCase_( lowerCamelCase_ , lowerCamelCase_ ) -> Tuple: if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 3: # expert layer _lowercase : Tuple = flax_key_tuple[:-1] + ('weight',) _lowercase : int = torch.permute(lowerCamelCase_ , (0, 2, 1) ) elif flax_key_tuple[-1] == "kernel" and ".".join(lowerCamelCase_ ): # linear layer _lowercase : Any = flax_key_tuple[:-1] + ('weight',) _lowercase : int = flax_tensor.T elif flax_key_tuple[-1] in ["scale", "embedding"]: _lowercase : List[str] = flax_key_tuple[:-1] + ('weight',) return flax_key_tuple, flax_tensor def UpperCamelCase_( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) -> List[str]: if "metadata" in layer: _lowercase : Any = layer.split('metadata' ) _lowercase : Union[str, Any] = ''.join(split_layer[0] )[:-1] _lowercase : List[str] = [tuple(('metadata' + split_layer[1]).split('/' ) )] elif "kvstore" in layer: _lowercase : str = layer.split('kvstore' ) _lowercase : Union[str, Any] = ''.join(split_layer[0] )[:-1] _lowercase : Optional[Any] = [tuple(('kvstore' + split_layer[1]).split('/' ) )] else: _lowercase : int = layer.split('/' ) _lowercase : Tuple = '/'.join(split_layer[:-1] ) _lowercase : Optional[Any] = (split_layer[-1],) if "kvstore/path" in layer: _lowercase : str = F'''{switch_checkpoint_path}/{checkpoint_info[layer]}''' elif "kvstore/driver" in layer: _lowercase : List[str] = 'file' else: _lowercase : Optional[int] = checkpoint_info[layer] return curr_real_layer_name, split_layer, content def UpperCamelCase_( lowerCamelCase_ , lowerCamelCase_ ) -> Dict: _lowercase : Tuple = rename_keys(lowerCamelCase_ ) _lowercase : List[str] = {} for k, v in current_block.items(): _lowercase : Dict = v _lowercase : Any = new_current_block torch.save(lowerCamelCase_ , lowerCamelCase_ ) def UpperCamelCase_( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ = WEIGHTS_NAME ) -> Dict: _lowercase : Optional[Any] = convert_file_size_to_int(lowerCamelCase_ ) _lowercase : Any = [] _lowercase : Optional[Any] = {} _lowercase : Any = 0 _lowercase : str = 0 os.makedirs(lowerCamelCase_ , exist_ok=lowerCamelCase_ ) with gfile.GFile(switch_checkpoint_path + '/checkpoint' , 'rb' ) as fp: _lowercase : Union[str, Any] = serialization.msgpack_restore(fp.read() )['optimizer']['target'] _lowercase : Optional[int] = flatten_dict(lowerCamelCase_ , sep='/' ) _lowercase : str = {} for layer in checkpoint_info.keys(): _lowercase , _lowercase , _lowercase : List[Any] = get_key_and_tensorstore_dict( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) if curr_real_layer_name in all_layers: _lowercase : List[Any] = content else: _lowercase : Dict = {split_layer[-1]: content} for key in all_layers.keys(): # open tensorstore file _lowercase : Dict = ts.open(unflatten_dict(all_layers[key] ) ).result().read().result() _lowercase : Tuple = torch.tensor(lowerCamelCase_ ) _lowercase : Dict = raw_weights.numel() * dtype_byte_size(raw_weights.dtype ) # use the renaming pattern from the small conversion scripts _lowercase , _lowercase : str = rename_base_flax_keys(tuple(key.split('/' ) ) , lowerCamelCase_ ) _lowercase : Any = '/'.join(lowerCamelCase_ ) # If this weight is going to tip up over the maximal size, we split. if current_block_size + weight_size > max_shard_size: _lowercase : List[str] = os.path.join( lowerCamelCase_ , weights_name.replace('.bin' , F'''-{len(lowerCamelCase_ )+1:05d}-of-???.bin''' ) ) rename_and_save_block(lowerCamelCase_ , lowerCamelCase_ ) sharded_state_dicts.append(current_block.keys() ) del current_block _lowercase : Any = {} _lowercase : str = 0 _lowercase : List[str] = raw_weights.to(getattr(lowerCamelCase_ , lowerCamelCase_ ) ) current_block_size += weight_size total_size += weight_size # Add the last block _lowercase : List[Any] = os.path.join(lowerCamelCase_ , weights_name.replace('.bin' , F'''-{len(lowerCamelCase_ )+1:05d}-of-???.bin''' ) ) rename_and_save_block(lowerCamelCase_ , lowerCamelCase_ ) sharded_state_dicts.append(current_block.keys() ) # If we only have one shard, we return it if len(lowerCamelCase_ ) == 1: return {weights_name: sharded_state_dicts[0]}, None # Otherwise, let's build the index _lowercase : int = {} _lowercase : Optional[Any] = {} for idx, shard in enumerate(lowerCamelCase_ ): _lowercase : str = weights_name.replace( '.bin' , F'''-{idx+1:05d}-of-{len(lowerCamelCase_ ):05d}.bin''' ) # len(sharded_state_dicts):05d} _lowercase : List[str] = os.path.join(lowerCamelCase_ , weights_name.replace('.bin' , F'''-{idx+1:05d}-of-???.bin''' ) ) os.rename(lowerCamelCase_ , os.path.join(lowerCamelCase_ , lowerCamelCase_ ) ) _lowercase : int = shard for key in shard: _lowercase : Optional[int] = shard_file # Add the metadata _lowercase : Any = {'total_size': total_size} _lowercase : Tuple = {'metadata': metadata, 'weight_map': weight_map} with open(os.path.join(lowerCamelCase_ , lowerCamelCase_ ) , 'w' , encoding='utf-8' ) as f: _lowercase : Tuple = json.dumps(lowerCamelCase_ , indent=2 , sort_keys=lowerCamelCase_ ) + '\n' f.write(lowerCamelCase_ ) return metadata, index if __name__ == "__main__": SCREAMING_SNAKE_CASE : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--switch_t5x_checkpoint_path", default="/mnt/disks/disk_switch/original_checkpoints/switch-xxl-128/checkpoint_634600", type=str, required=False, help="Path to a directory containing a folder per layer. Follows the original Google format.", ) parser.add_argument("--max_shard_size", default="10GB", required=False, help="Max shard size") parser.add_argument("--dtype", default="bfloat16", type=str, required=False, help="dtype of the saved model") parser.add_argument( "--pytorch_dump_folder_path", default="/mnt/disks/disk_switch/original_checkpoints/switch-xxl-128-converted", type=str, required=False, help="Path to the output pytorch model.", ) SCREAMING_SNAKE_CASE : Dict = parser.parse_args() shard_on_the_fly( args.switch_tax_checkpoint_path, args.pytorch_dump_folder_path, args.max_shard_size, args.dtype, ) def UpperCamelCase_( ) -> str: from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration, TaTokenizer _lowercase : Optional[Any] = SwitchTransformersConfig.from_pretrained('google/switch-base-8' ) config.save_pretrained('/home/arthur_huggingface_co/transformers/switch_converted' ) _lowercase : str = SwitchTransformersForConditionalGeneration.from_pretrained( '/home/arthur_huggingface_co/transformers/switch_converted' , device_map='auto' ) _lowercase : str = TaTokenizer.from_pretrained('t5-small' ) _lowercase : Tuple = 'A <extra_id_0> walks into a bar a orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.' _lowercase : Optional[int] = tokenizer(lowerCamelCase_ , return_tensors='pt' ).input_ids _lowercase : Tuple = model.generate(lowerCamelCase_ , decoder_start_token_id=0 ) print(tokenizer.decode(out[0] ) )
21
from heapq import heappop, heappush import numpy as np def a__ ( snake_case , snake_case , snake_case , snake_case , ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : int = grid.shape __SCREAMING_SNAKE_CASE : Tuple = [-1, 1, 0, 0] __SCREAMING_SNAKE_CASE : List[str] = [0, 0, -1, 1] if allow_diagonal: dx += [-1, -1, 1, 1] dy += [-1, 1, -1, 1] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Union[str, Any] = [(0, source)], set() __SCREAMING_SNAKE_CASE : Union[str, Any] = np.full((rows, cols) , np.inf ) __SCREAMING_SNAKE_CASE : Union[str, Any] = 0 __SCREAMING_SNAKE_CASE : Union[str, Any] = np.empty((rows, cols) , dtype=snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = None while queue: ((__SCREAMING_SNAKE_CASE), (__SCREAMING_SNAKE_CASE)) : Any = heappop(snake_case ) if (x, y) in visited: continue visited.add((x, y) ) if (x, y) == destination: __SCREAMING_SNAKE_CASE : int = [] while (x, y) != source: path.append((x, y) ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[Any] = predecessors[x, y] path.append(snake_case ) # add the source manually path.reverse() return matrix[destination], path for i in range(len(snake_case ) ): __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = x + dx[i], y + dy[i] if 0 <= nx < rows and 0 <= ny < cols: __SCREAMING_SNAKE_CASE : Optional[int] = grid[nx][ny] if next_node == 1 and matrix[nx, ny] > dist + 1: heappush(snake_case , (dist + 1, (nx, ny)) ) __SCREAMING_SNAKE_CASE : int = dist + 1 __SCREAMING_SNAKE_CASE : Dict = (x, y) return np.inf, [] if __name__ == "__main__": import doctest doctest.testmod()
303
0
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import warnings from typing import List from unittest.mock import Mock import torch from torch.utils.data import DataLoader, IterableDataset, TensorDataset from accelerate.accelerator import Accelerator from accelerate.utils.dataclasses import DistributedType class A_ ( lowerCAmelCase_ ): def __init__( self : Union[str, Any] , snake_case_ : Union[str, Any] ): _UpperCAmelCase = data def __iter__( self : Any ): for element in self.data: yield element def UpperCAmelCase_ ( __lowercase : Optional[int]=True ) -> Optional[int]: '''simple docstring''' _UpperCAmelCase = Accelerator(even_batches=__lowercase ) assert accelerator.num_processes == 2, "this script expects that two GPUs are available" return accelerator def UpperCAmelCase_ ( __lowercase : Accelerator , __lowercase : int , __lowercase : int , __lowercase : bool = False ) -> Tuple: '''simple docstring''' if iterable: _UpperCAmelCase = DummyIterableDataset(torch.as_tensor(range(__lowercase ) ) ) else: _UpperCAmelCase = TensorDataset(torch.as_tensor(range(__lowercase ) ) ) _UpperCAmelCase = DataLoader(__lowercase , batch_size=__lowercase ) _UpperCAmelCase = accelerator.prepare(__lowercase ) return dl def UpperCAmelCase_ ( __lowercase : Accelerator , __lowercase : int , __lowercase : int , __lowercase : List[int] , __lowercase : List[int] , ) -> Tuple: '''simple docstring''' _UpperCAmelCase = create_dataloader(accelerator=__lowercase , dataset_size=__lowercase , batch_size=__lowercase ) _UpperCAmelCase = [len(batch[0] ) for batch in dl] if accelerator.process_index == 0: assert batch_sizes == process_0_expected_batch_sizes elif accelerator.process_index == 1: assert batch_sizes == process_1_expected_batch_sizes def UpperCAmelCase_ ( ) -> List[Any]: '''simple docstring''' _UpperCAmelCase = create_accelerator() # without padding, we would expect a different number of batches verify_dataloader_batch_sizes( __lowercase , dataset_size=3 , batch_size=1 , process_0_expected_batch_sizes=[1, 1] , process_1_expected_batch_sizes=[1, 1] , ) # without padding, we would expect the same number of batches, but different sizes verify_dataloader_batch_sizes( __lowercase , dataset_size=7 , batch_size=2 , process_0_expected_batch_sizes=[2, 2] , process_1_expected_batch_sizes=[2, 2] , ) def UpperCAmelCase_ ( ) -> int: '''simple docstring''' _UpperCAmelCase = create_accelerator(even_batches=__lowercase ) verify_dataloader_batch_sizes( __lowercase , dataset_size=3 , batch_size=1 , process_0_expected_batch_sizes=[1, 1] , process_1_expected_batch_sizes=[1] , ) verify_dataloader_batch_sizes( __lowercase , dataset_size=7 , batch_size=2 , process_0_expected_batch_sizes=[2, 2] , process_1_expected_batch_sizes=[2, 1] , ) def UpperCAmelCase_ ( ) -> int: '''simple docstring''' _UpperCAmelCase = create_accelerator(even_batches=__lowercase ) _UpperCAmelCase = torch.nn.Linear(1 , 1 ) _UpperCAmelCase = accelerator.prepare(__lowercase ) _UpperCAmelCase = create_dataloader(__lowercase , dataset_size=3 , batch_size=1 ) _UpperCAmelCase = [] with accelerator.join_uneven_inputs([ddp_model] ): for batch_idx, batch in enumerate(__lowercase ): _UpperCAmelCase = ddp_model(batch[0].float() ) _UpperCAmelCase = output.sum() loss.backward() batch_idxs.append(__lowercase ) accelerator.wait_for_everyone() if accelerator.process_index == 0: assert batch_idxs == [0, 1] elif accelerator.process_index == 1: assert batch_idxs == [0] def UpperCAmelCase_ ( __lowercase : List[Any] ) -> Tuple: '''simple docstring''' with warnings.catch_warnings(record=__lowercase ) as w: with accelerator.join_uneven_inputs([Mock()] ): pass assert issubclass(w[-1].category , __lowercase ) assert "only supported for multi-GPU" in str(w[-1].message ) def UpperCAmelCase_ ( ) -> Optional[Any]: '''simple docstring''' _UpperCAmelCase = True _UpperCAmelCase = False _UpperCAmelCase = create_accelerator(even_batches=__lowercase ) _UpperCAmelCase = torch.nn.Linear(1 , 1 ) _UpperCAmelCase = accelerator.prepare(__lowercase ) _UpperCAmelCase = create_dataloader(__lowercase , dataset_size=3 , batch_size=1 ) _UpperCAmelCase = create_dataloader(__lowercase , dataset_size=3 , batch_size=1 ) with accelerator.join_uneven_inputs([ddp_model] , even_batches=__lowercase ): _UpperCAmelCase = train_dl.batch_sampler.even_batches _UpperCAmelCase = valid_dl.batch_sampler.even_batches assert train_dl_overridden_value == overridden_even_batches assert valid_dl_overridden_value == overridden_even_batches assert train_dl.batch_sampler.even_batches == default_even_batches assert valid_dl.batch_sampler.even_batches == default_even_batches def UpperCAmelCase_ ( ) -> Dict: '''simple docstring''' _UpperCAmelCase = True _UpperCAmelCase = False _UpperCAmelCase = create_accelerator(even_batches=__lowercase ) _UpperCAmelCase = torch.nn.Linear(1 , 1 ) _UpperCAmelCase = accelerator.prepare(__lowercase ) create_dataloader(__lowercase , dataset_size=3 , batch_size=1 , iterable=__lowercase ) _UpperCAmelCase = create_dataloader(__lowercase , dataset_size=3 , batch_size=1 ) with warnings.catch_warnings(): warnings.filterwarnings("ignore" ) try: with accelerator.join_uneven_inputs([ddp_model] , even_batches=__lowercase ): _UpperCAmelCase = batch_dl.batch_sampler.even_batches except AttributeError: # ensure attribute error is not raised when processing iterable dl raise AssertionError assert batch_dl_overridden_value == overridden_even_batches assert batch_dl.batch_sampler.even_batches == default_even_batches def UpperCAmelCase_ ( ) -> List[str]: '''simple docstring''' _UpperCAmelCase = create_accelerator() _UpperCAmelCase = torch.nn.Linear(1 , 1 ) _UpperCAmelCase = accelerator.prepare(__lowercase ) create_dataloader(__lowercase , dataset_size=3 , batch_size=1 , iterable=__lowercase ) with warnings.catch_warnings(record=__lowercase ) as w: with accelerator.join_uneven_inputs([ddp_model] , even_batches=__lowercase ): pass assert issubclass(w[-1].category , __lowercase ) assert "only supported for map-style datasets" in str(w[-1].message ) def UpperCAmelCase_ ( ) -> Optional[int]: '''simple docstring''' _UpperCAmelCase = create_accelerator() accelerator.print("Test that even_batches variable ensures uniform batches across processes" ) test_default_ensures_even_batch_sizes() accelerator.print("Run tests with even_batches disabled" ) test_can_disable_even_batches() accelerator.print("Test joining uneven inputs" ) test_can_join_uneven_inputs() accelerator.print("Test overriding even_batches when joining uneven inputs" ) test_join_can_override_even_batches() accelerator.print("Test overriding even_batches for mixed dataloader types" ) test_join_can_override_for_mixed_type_dataloaders() accelerator.print("Test overriding even_batches raises a warning for iterable dataloaders" ) test_join_raises_warning_for_iterable_when_overriding_even_batches() accelerator.print("Test join with non DDP distributed raises warning" ) _UpperCAmelCase = accelerator.state.distributed_type _UpperCAmelCase = DistributedType.FSDP test_join_raises_warning_for_non_ddp_distributed(__lowercase ) _UpperCAmelCase = original_state if __name__ == "__main__": main()
22
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = {"""configuration_ibert""": ["""IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """IBertConfig""", """IBertOnnxConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """IBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """IBertForMaskedLM""", """IBertForMultipleChoice""", """IBertForQuestionAnswering""", """IBertForSequenceClassification""", """IBertForTokenClassification""", """IBertModel""", """IBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_ibert import IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, IBertConfig, IBertOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ibert import ( IBERT_PRETRAINED_MODEL_ARCHIVE_LIST, IBertForMaskedLM, IBertForMultipleChoice, IBertForQuestionAnswering, IBertForSequenceClassification, IBertForTokenClassification, IBertModel, IBertPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
303
0
'''simple docstring''' from math import loga def snake_case_ ( _lowerCAmelCase : int ) -> int: if a < 0: raise ValueError('''Input value must be a positive integer''' ) elif isinstance(_lowerCAmelCase , _lowerCAmelCase ): raise TypeError('''Input value must be a \'int\' type''' ) return 0 if (a == 0) else int(loga(a & -a ) ) if __name__ == "__main__": import doctest doctest.testmod()
23
from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
303
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) snake_case_ = { 'configuration_deberta': ['DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DebertaConfig', 'DebertaOnnxConfig'], 'tokenization_deberta': ['DebertaTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case_ = ['DebertaTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case_ = [ 'DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'DebertaForMaskedLM', 'DebertaForQuestionAnswering', 'DebertaForSequenceClassification', 'DebertaForTokenClassification', 'DebertaModel', 'DebertaPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case_ = [ 'TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFDebertaForMaskedLM', 'TFDebertaForQuestionAnswering', 'TFDebertaForSequenceClassification', 'TFDebertaForTokenClassification', 'TFDebertaModel', 'TFDebertaPreTrainedModel', ] if TYPE_CHECKING: from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig, DebertaOnnxConfig from .tokenization_deberta import DebertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_deberta_fast import DebertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deberta import ( DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, DebertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deberta import ( TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFDebertaForMaskedLM, TFDebertaForQuestionAnswering, TFDebertaForSequenceClassification, TFDebertaForTokenClassification, TFDebertaModel, TFDebertaPreTrainedModel, ) else: import sys snake_case_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
24
import sys from collections import defaultdict class __UpperCamelCase : """simple docstring""" def __init__( self : Union[str, Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = [] def UpperCAmelCase__ ( self : List[str] , _A : str ): """simple docstring""" return self.node_position[vertex] def UpperCAmelCase__ ( self : Dict , _A : List[str] , _A : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = pos def UpperCAmelCase__ ( self : List[Any] , _A : Union[str, Any] , _A : List[Any] , _A : List[str] , _A : Union[str, Any] ): """simple docstring""" if start > size // 2 - 1: return else: if 2 * start + 2 >= size: __SCREAMING_SNAKE_CASE : Union[str, Any] = 2 * start + 1 else: if heap[2 * start + 1] < heap[2 * start + 2]: __SCREAMING_SNAKE_CASE : List[Any] = 2 * start + 1 else: __SCREAMING_SNAKE_CASE : Dict = 2 * start + 2 if heap[smallest_child] < heap[start]: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Union[str, Any] = heap[smallest_child], positions[smallest_child] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : List[str] = ( heap[start], positions[start], ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = temp, tempa __SCREAMING_SNAKE_CASE : Any = self.get_position(positions[smallest_child] ) self.set_position( positions[smallest_child] , self.get_position(positions[start] ) ) self.set_position(positions[start] , _A ) self.top_to_bottom(_A , _A , _A , _A ) def UpperCAmelCase__ ( self : Any , _A : Union[str, Any] , _A : Dict , _A : Optional[Any] , _A : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = position[index] while index != 0: __SCREAMING_SNAKE_CASE : Optional[Any] = int((index - 2) / 2 ) if index % 2 == 0 else int((index - 1) / 2 ) if val < heap[parent]: __SCREAMING_SNAKE_CASE : Optional[Any] = heap[parent] __SCREAMING_SNAKE_CASE : str = position[parent] self.set_position(position[parent] , _A ) else: __SCREAMING_SNAKE_CASE : List[str] = val __SCREAMING_SNAKE_CASE : List[str] = temp self.set_position(_A , _A ) break __SCREAMING_SNAKE_CASE : List[Any] = parent else: __SCREAMING_SNAKE_CASE : Tuple = val __SCREAMING_SNAKE_CASE : List[str] = temp self.set_position(_A , 0 ) def UpperCAmelCase__ ( self : List[str] , _A : Tuple , _A : Tuple ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = len(_A ) // 2 - 1 for i in range(_A , -1 , -1 ): self.top_to_bottom(_A , _A , len(_A ) , _A ) def UpperCAmelCase__ ( self : List[str] , _A : Dict , _A : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = positions[0] __SCREAMING_SNAKE_CASE : Tuple = sys.maxsize self.top_to_bottom(_A , 0 , len(_A ) , _A ) return temp def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = Heap() __SCREAMING_SNAKE_CASE : int = [0] * len(snake_case ) __SCREAMING_SNAKE_CASE : Dict = [-1] * len(snake_case ) # Neighboring Tree Vertex of selected vertex # Minimum Distance of explored vertex with neighboring vertex of partial tree # formed in graph __SCREAMING_SNAKE_CASE : Dict = [] # Heap of Distance of vertices from their neighboring vertex __SCREAMING_SNAKE_CASE : Optional[int] = [] for vertex in range(len(snake_case ) ): distance_tv.append(sys.maxsize ) positions.append(snake_case ) heap.node_position.append(snake_case ) __SCREAMING_SNAKE_CASE : Union[str, Any] = [] __SCREAMING_SNAKE_CASE : str = 1 __SCREAMING_SNAKE_CASE : int = sys.maxsize for neighbor, distance in adjacency_list[0]: __SCREAMING_SNAKE_CASE : Optional[Any] = 0 __SCREAMING_SNAKE_CASE : Dict = distance heap.heapify(snake_case , snake_case ) for _ in range(1 , len(snake_case ) ): __SCREAMING_SNAKE_CASE : Tuple = heap.delete_minimum(snake_case , snake_case ) if visited[vertex] == 0: tree_edges.append((nbr_tv[vertex], vertex) ) __SCREAMING_SNAKE_CASE : List[Any] = 1 for neighbor, distance in adjacency_list[vertex]: if ( visited[neighbor] == 0 and distance < distance_tv[heap.get_position(snake_case )] ): __SCREAMING_SNAKE_CASE : int = distance heap.bottom_to_top( snake_case , heap.get_position(snake_case ) , snake_case , snake_case ) __SCREAMING_SNAKE_CASE : Any = vertex return tree_edges if __name__ == "__main__": # pragma: no cover # < --------- Prims Algorithm --------- > lowercase_ = int(input("""Enter number of edges: """).strip()) lowercase_ = defaultdict(list) for _ in range(edges_number): lowercase_ = [int(x) for x in input().strip().split()] adjacency_list[edge[0]].append([edge[1], edge[2]]) adjacency_list[edge[1]].append([edge[0], edge[2]]) print(prisms_algorithm(adjacency_list))
303
0
"""simple docstring""" from string import ascii_lowercase, ascii_uppercase def lowercase_ ( _snake_case ): if not sentence: return "" SCREAMING_SNAKE_CASE__ : int = dict(zip(_snake_case ,_snake_case ) ) return lower_to_upper.get(sentence[0] ,sentence[0] ) + sentence[1:] if __name__ == "__main__": from doctest import testmod testmod()
25
from __future__ import annotations import matplotlib.pyplot as plt # type: ignore import numpy # initial triangle of Koch snowflake lowercase_ = numpy.array([0, 0]) lowercase_ = numpy.array([0.5, 0.866_0254]) lowercase_ = numpy.array([1, 0]) lowercase_ = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1] def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = initial_vectors for _ in range(snake_case ): __SCREAMING_SNAKE_CASE : Dict = iteration_step(snake_case ) return vectors def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = [] for i, start_vector in enumerate(vectors[:-1] ): __SCREAMING_SNAKE_CASE : str = vectors[i + 1] new_vectors.append(snake_case ) __SCREAMING_SNAKE_CASE : List[str] = end_vector - start_vector new_vectors.append(start_vector + difference_vector / 3 ) new_vectors.append( start_vector + difference_vector / 3 + rotate(difference_vector / 3 , 60 ) ) new_vectors.append(start_vector + difference_vector * 2 / 3 ) new_vectors.append(vectors[-1] ) return new_vectors def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = numpy.radians(snake_case ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = numpy.cos(snake_case ), numpy.sin(snake_case ) __SCREAMING_SNAKE_CASE : Any = numpy.array(((c, -s), (s, c)) ) return numpy.dot(snake_case , snake_case ) def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = plt.gca() axes.set_aspect('''equal''' ) # matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all # y-coordinates as inputs, which are constructed from the vector-list using # zip() __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = zip(*snake_case ) plt.plot(snake_case , snake_case ) plt.show() if __name__ == "__main__": import doctest doctest.testmod() lowercase_ = iterate(INITIAL_VECTORS, 5) plot(processed_vectors)
303
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _snake_case = { "configuration_nllb_moe": [ "NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP", "NllbMoeConfig", ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST", "NllbMoeForConditionalGeneration", "NllbMoeModel", "NllbMoePreTrainedModel", "NllbMoeTop2Router", "NllbMoeSparseMLP", ] if TYPE_CHECKING: from .configuration_nllb_moe import ( NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP, NllbMoeConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_nllb_moe import ( NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST, NllbMoeForConditionalGeneration, NllbMoeModel, NllbMoePreTrainedModel, NllbMoeSparseMLP, NllbMoeTopaRouter, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
from typing import List, Optional, Union import torch from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) lowercase_ = logging.get_logger(__name__) # pylint: disable=invalid-name lowercase_ = """ Examples: ```py >>> import torch >>> import numpy as np >>> from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline >>> from transformers import pipeline >>> from diffusers.utils import load_image >>> def make_hint(image, depth_estimator): ... image = depth_estimator(image)[\"depth\"] ... image = np.array(image) ... image = image[:, :, None] ... image = np.concatenate([image, image, image], axis=2) ... detected_map = torch.from_numpy(image).float() / 255.0 ... hint = detected_map.permute(2, 0, 1) ... return hint >>> depth_estimator = pipeline(\"depth-estimation\") >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained( ... \"kandinsky-community/kandinsky-2-2-prior\", torch_dtype=torch.float16 ... ) >>> pipe_prior = pipe_prior.to(\"cuda\") >>> pipe = KandinskyV22ControlnetPipeline.from_pretrained( ... \"kandinsky-community/kandinsky-2-2-controlnet-depth\", torch_dtype=torch.float16 ... ) >>> pipe = pipe.to(\"cuda\") >>> img = load_image( ... \"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main\" ... \"/kandinsky/cat.png\" ... ).resize((768, 768)) >>> hint = make_hint(img, depth_estimator).unsqueeze(0).half().to(\"cuda\") >>> prompt = \"A robot, 4k photo\" >>> negative_prior_prompt = \"lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature\" >>> generator = torch.Generator(device=\"cuda\").manual_seed(43) >>> image_emb, zero_image_emb = pipe_prior( ... prompt=prompt, negative_prompt=negative_prior_prompt, generator=generator ... ).to_tuple() >>> images = pipe( ... image_embeds=image_emb, ... negative_image_embeds=zero_image_emb, ... hint=hint, ... num_inference_steps=50, ... generator=generator, ... height=768, ... width=768, ... ).images >>> images[0].save(\"robot_cat.png\") ``` """ def a__ ( snake_case , snake_case , snake_case=8 ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 __SCREAMING_SNAKE_CASE : Optional[int] = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" def __init__( self : int , _A : UNetaDConditionModel , _A : DDPMScheduler , _A : VQModel , ): """simple docstring""" super().__init__() self.register_modules( unet=_A , scheduler=_A , movq=_A , ) __SCREAMING_SNAKE_CASE : Optional[Any] = 2 ** (len(self.movq.config.block_out_channels ) - 1) def UpperCAmelCase__ ( self : Union[str, Any] , _A : Dict , _A : Optional[Any] , _A : Tuple , _A : List[Any] , _A : Optional[Any] , _A : List[Any] ): """simple docstring""" if latents is None: __SCREAMING_SNAKE_CASE : Optional[Any] = randn_tensor(_A , generator=_A , device=_A , dtype=_A ) else: if latents.shape != shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {shape}''' ) __SCREAMING_SNAKE_CASE : Tuple = latents.to(_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = latents * scheduler.init_noise_sigma return latents def UpperCAmelCase__ ( self : Tuple , _A : List[str]=0 ): """simple docstring""" if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) __SCREAMING_SNAKE_CASE : List[Any] = torch.device(F'''cuda:{gpu_id}''' ) __SCREAMING_SNAKE_CASE : Dict = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_A , _A ) def UpperCAmelCase__ ( self : int , _A : Tuple=0 ): """simple docstring""" if is_accelerate_available() and is_accelerate_version('''>=''' , '''0.17.0.dev0''' ): from accelerate import cpu_offload_with_hook else: raise ImportError('''`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.''' ) __SCREAMING_SNAKE_CASE : str = torch.device(F'''cuda:{gpu_id}''' ) if self.device.type != "cpu": self.to('''cpu''' , silence_dtype_warnings=_A ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) __SCREAMING_SNAKE_CASE : Optional[Any] = None for cpu_offloaded_model in [self.unet, self.movq]: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Dict = cpu_offload_with_hook(_A , _A , prev_module_hook=_A ) # We'll offload the last model manually. __SCREAMING_SNAKE_CASE : Optional[int] = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" if not hasattr(self.unet , '''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(_A , '''_hf_hook''' ) and hasattr(module._hf_hook , '''execution_device''' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(_A ) def __call__( self : Dict , _A : Union[torch.FloatTensor, List[torch.FloatTensor]] , _A : Union[torch.FloatTensor, List[torch.FloatTensor]] , _A : torch.FloatTensor , _A : int = 512 , _A : int = 512 , _A : int = 100 , _A : float = 4.0 , _A : int = 1 , _A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _A : Optional[torch.FloatTensor] = None , _A : Optional[str] = "pil" , _A : bool = True , ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = self._execution_device __SCREAMING_SNAKE_CASE : Optional[Any] = guidance_scale > 1.0 if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : Optional[Any] = torch.cat(_A , dim=0 ) if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : List[Any] = torch.cat(_A , dim=0 ) if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : List[str] = torch.cat(_A , dim=0 ) __SCREAMING_SNAKE_CASE : List[Any] = image_embeds.shape[0] * num_images_per_prompt if do_classifier_free_guidance: __SCREAMING_SNAKE_CASE : Dict = image_embeds.repeat_interleave(_A , dim=0 ) __SCREAMING_SNAKE_CASE : Any = negative_image_embeds.repeat_interleave(_A , dim=0 ) __SCREAMING_SNAKE_CASE : Union[str, Any] = hint.repeat_interleave(_A , dim=0 ) __SCREAMING_SNAKE_CASE : int = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=_A ) __SCREAMING_SNAKE_CASE : Optional[int] = torch.cat([hint, hint] , dim=0 ).to(dtype=self.unet.dtype , device=_A ) self.scheduler.set_timesteps(_A , device=_A ) __SCREAMING_SNAKE_CASE : Optional[int] = self.scheduler.timesteps __SCREAMING_SNAKE_CASE : Tuple = self.movq.config.latent_channels __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Dict = downscale_height_and_width(_A , _A , self.movq_scale_factor ) # create initial latent __SCREAMING_SNAKE_CASE : Tuple = self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , _A , _A , _A , self.scheduler , ) for i, t in enumerate(self.progress_bar(_A ) ): # expand the latents if we are doing classifier free guidance __SCREAMING_SNAKE_CASE : str = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents __SCREAMING_SNAKE_CASE : Dict = {'''image_embeds''': image_embeds, '''hint''': hint} __SCREAMING_SNAKE_CASE : Union[str, Any] = self.unet( sample=_A , timestep=_A , encoder_hidden_states=_A , added_cond_kwargs=_A , return_dict=_A , )[0] if do_classifier_free_guidance: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = noise_pred.split(latents.shape[1] , dim=1 ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Dict = noise_pred.chunk(2 ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[Any] = variance_pred.chunk(2 ) __SCREAMING_SNAKE_CASE : List[Any] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) __SCREAMING_SNAKE_CASE : Tuple = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , '''variance_type''' ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : List[str] = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 __SCREAMING_SNAKE_CASE : Any = self.scheduler.step( _A , _A , _A , generator=_A , )[0] # post-processing __SCREAMING_SNAKE_CASE : Any = self.movq.decode(_A , force_not_quantize=_A )['''sample'''] if output_type not in ["pt", "np", "pil"]: raise ValueError(F'''Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}''' ) if output_type in ["np", "pil"]: __SCREAMING_SNAKE_CASE : str = image * 0.5 + 0.5 __SCREAMING_SNAKE_CASE : Tuple = image.clamp(0 , 1 ) __SCREAMING_SNAKE_CASE : Dict = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": __SCREAMING_SNAKE_CASE : List[str] = self.numpy_to_pil(_A ) if not return_dict: return (image,) return ImagePipelineOutput(images=_A )
303
0
'''simple docstring''' import os import unittest from transformers import LayoutLMTokenizer, LayoutLMTokenizerFast from transformers.models.layoutlm.tokenization_layoutlm import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __UpperCamelCase ( lowerCAmelCase_ , unittest.TestCase ): A_ = LayoutLMTokenizer A_ = LayoutLMTokenizerFast A_ = True A_ = True def __UpperCAmelCase ( self ): '''simple docstring''' super().setUp() __a : List[Any] = [ '[UNK]', '[CLS]', '[SEP]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] __a : Optional[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def __UpperCAmelCase ( self , **__a ): '''simple docstring''' return LayoutLMTokenizer.from_pretrained(self.tmpdirname , **__a ) def __UpperCAmelCase ( self , __a ): '''simple docstring''' __a : Tuple = 'UNwant\u00E9d,running' __a : Tuple = 'unwanted, running' return input_text, output_text def __UpperCAmelCase ( self ): '''simple docstring''' __a : Any = self.tokenizer_class(self.vocab_file ) __a : Any = tokenizer.tokenize('UNwant\u00E9d,running' ) self.assertListEqual(__a , ['un', '##want', '##ed', ',', 'runn', '##ing'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(__a ) , [7, 4, 5, 10, 8, 9] ) def __UpperCAmelCase ( self ): '''simple docstring''' pass
27
import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem lowercase_ = importlib.util.find_spec("""s3fs""") is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 lowercase_ = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(f'''A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.''') fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def a__ ( snake_case ): """simple docstring""" if "://" in dataset_path: __SCREAMING_SNAKE_CASE : Any = dataset_path.split('''://''' )[1] return dataset_path def a__ ( snake_case ): """simple docstring""" if fs is not None and fs.protocol != "file": return True else: return False def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = not is_remote_filesystem(snake_case ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(snake_case ) , fs._strip_protocol(snake_case ) ) else: fs.mv(snake_case , snake_case , recursive=snake_case ) def a__ ( ): """simple docstring""" if hasattr(fsspec.asyn , '''reset_lock''' ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: __SCREAMING_SNAKE_CASE : int = None __SCREAMING_SNAKE_CASE : Union[str, Any] = None __SCREAMING_SNAKE_CASE : Union[str, Any] = threading.Lock()
303
0
'''simple docstring''' from typing import List, Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class SCREAMING_SNAKE_CASE ( _a ): """simple docstring""" _SCREAMING_SNAKE_CASE = ["""image_processor""", """tokenizer"""] _SCREAMING_SNAKE_CASE = """BlipImageProcessor""" _SCREAMING_SNAKE_CASE = ("""BertTokenizer""", """BertTokenizerFast""") def __init__( self : Optional[int] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Union[str, Any] ): """simple docstring""" UpperCamelCase = False super().__init__(UpperCamelCase__ , UpperCamelCase__ ) UpperCamelCase = self.image_processor def __call__( self : Union[str, Any] , UpperCamelCase__ : ImageInput = None , UpperCamelCase__ : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , UpperCamelCase__ : bool = True , UpperCamelCase__ : Union[bool, str, PaddingStrategy] = False , UpperCamelCase__ : Union[bool, str, TruncationStrategy] = None , UpperCamelCase__ : Optional[int] = None , UpperCamelCase__ : int = 0 , UpperCamelCase__ : Optional[int] = None , UpperCamelCase__ : Optional[bool] = None , UpperCamelCase__ : bool = False , UpperCamelCase__ : bool = False , UpperCamelCase__ : bool = False , UpperCamelCase__ : bool = False , UpperCamelCase__ : bool = False , UpperCamelCase__ : bool = True , UpperCamelCase__ : Optional[Union[str, TensorType]] = None , **UpperCamelCase__ : int , ): """simple docstring""" if images is None and text is None: raise ValueError('You have to specify either images or text.' ) # Get only text if images is None: UpperCamelCase = self.tokenizer UpperCamelCase = self.tokenizer( text=UpperCamelCase__ , add_special_tokens=UpperCamelCase__ , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , max_length=UpperCamelCase__ , stride=UpperCamelCase__ , pad_to_multiple_of=UpperCamelCase__ , return_attention_mask=UpperCamelCase__ , return_overflowing_tokens=UpperCamelCase__ , return_special_tokens_mask=UpperCamelCase__ , return_offsets_mapping=UpperCamelCase__ , return_token_type_ids=UpperCamelCase__ , return_length=UpperCamelCase__ , verbose=UpperCamelCase__ , return_tensors=UpperCamelCase__ , **UpperCamelCase__ , ) return text_encoding # add pixel_values UpperCamelCase = self.image_processor(UpperCamelCase__ , return_tensors=UpperCamelCase__ ) if text is not None: UpperCamelCase = self.tokenizer( text=UpperCamelCase__ , add_special_tokens=UpperCamelCase__ , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , max_length=UpperCamelCase__ , stride=UpperCamelCase__ , pad_to_multiple_of=UpperCamelCase__ , return_attention_mask=UpperCamelCase__ , return_overflowing_tokens=UpperCamelCase__ , return_special_tokens_mask=UpperCamelCase__ , return_offsets_mapping=UpperCamelCase__ , return_token_type_ids=UpperCamelCase__ , return_length=UpperCamelCase__ , verbose=UpperCamelCase__ , return_tensors=UpperCamelCase__ , **UpperCamelCase__ , ) else: UpperCamelCase = None if text_encoding is not None: encoding_image_processor.update(UpperCamelCase__ ) return encoding_image_processor def A ( self : Tuple , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Any ): """simple docstring""" return self.tokenizer.batch_decode(*UpperCamelCase__ , **UpperCamelCase__ ) def A ( self : Union[str, Any] , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Dict ): """simple docstring""" return self.tokenizer.decode(*UpperCamelCase__ , **UpperCamelCase__ ) @property def A ( self : str ): """simple docstring""" UpperCamelCase = self.tokenizer.model_input_names UpperCamelCase = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
28
from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels
303
0
import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class lowerCamelCase (_snake_case ): '''simple docstring''' _snake_case : int = 0 _snake_case : bool = False _snake_case : float = 3.0 class lowerCamelCase (unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ) -> Dict: # If no defaults are changed, `to_kwargs` returns an empty dict. self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'a': 2} ) self.assertDictEqual(MockClass(a=2 , b=_UpperCamelCase ).to_kwargs() , {'a': 2, 'b': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'a': 2, 'c': 2.25} ) @require_cuda def __UpperCAmelCase ( self ) -> Optional[Any]: # If no defaults are changed, `to_kwargs` returns an empty dict. UpperCAmelCase_ : Optional[Any] = GradScalerKwargs(init_scale=1_0_2_4 , growth_factor=2 ) AcceleratorState._reset_state() UpperCAmelCase_ : Union[str, Any] = Accelerator(mixed_precision='fp16' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) UpperCAmelCase_ : Any = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 10_24.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2_0_0_0 ) self.assertEqual(scaler._enabled , _UpperCamelCase ) @require_multi_gpu def __UpperCAmelCase ( self ) -> str: UpperCAmelCase_ : int = ['torchrun', f"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )] execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) if __name__ == "__main__": __UpperCAmelCase = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) __UpperCAmelCase = Accelerator(kwargs_handlers=[ddp_scaler]) __UpperCAmelCase = torch.nn.Linear(100, 200) __UpperCAmelCase = accelerator.prepare(model) # Check the values changed in kwargs __UpperCAmelCase = '' __UpperCAmelCase = model.bucket_bytes_cap // (1024 * 1024) if observed_bucket_cap_map != 15: error_msg += F"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += F"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += F"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += F"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += F"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
29
from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { """uw-madison/mra-base-512-4""": """https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json""", } class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''mra''' def __init__( self : str , _A : List[str]=5_0265 , _A : int=768 , _A : Union[str, Any]=12 , _A : Union[str, Any]=12 , _A : Union[str, Any]=3072 , _A : Any="gelu" , _A : List[Any]=0.1 , _A : List[Any]=0.1 , _A : List[str]=512 , _A : Tuple=1 , _A : List[str]=0.02 , _A : Union[str, Any]=1e-5 , _A : Optional[int]="absolute" , _A : Union[str, Any]=4 , _A : List[Any]="full" , _A : Union[str, Any]=0 , _A : Union[str, Any]=0 , _A : Optional[Any]=1 , _A : Union[str, Any]=0 , _A : Any=2 , **_A : List[str] , ): """simple docstring""" super().__init__(pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , **_A ) __SCREAMING_SNAKE_CASE : Dict = vocab_size __SCREAMING_SNAKE_CASE : str = max_position_embeddings __SCREAMING_SNAKE_CASE : Optional[int] = hidden_size __SCREAMING_SNAKE_CASE : str = num_hidden_layers __SCREAMING_SNAKE_CASE : Tuple = num_attention_heads __SCREAMING_SNAKE_CASE : str = intermediate_size __SCREAMING_SNAKE_CASE : Tuple = hidden_act __SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob __SCREAMING_SNAKE_CASE : int = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : Tuple = initializer_range __SCREAMING_SNAKE_CASE : Any = type_vocab_size __SCREAMING_SNAKE_CASE : str = layer_norm_eps __SCREAMING_SNAKE_CASE : Union[str, Any] = position_embedding_type __SCREAMING_SNAKE_CASE : str = block_per_row __SCREAMING_SNAKE_CASE : Union[str, Any] = approx_mode __SCREAMING_SNAKE_CASE : Optional[int] = initial_prior_first_n_blocks __SCREAMING_SNAKE_CASE : List[Any] = initial_prior_diagonal_n_blocks
303
0
def a ( snake_case__: str , snake_case__: str ): '''simple docstring''' lowercase_ = len(snake_case__ ) lowercase_ = len(snake_case__ ) lowercase_ = ( first_str_length if first_str_length > second_str_length else second_str_length ) lowercase_ = [] for char_count in range(snake_case__ ): if char_count < first_str_length: output_list.append(first_str[char_count] ) if char_count < second_str_length: output_list.append(second_str[char_count] ) return "".join(snake_case__ ) if __name__ == "__main__": print(alternative_string_arrange('AB', 'XYZ'), end=' ')
30
from typing import List, Optional, Tuple, Union import torch from ...utils import logging, randn_tensor from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline lowercase_ = logging.get_logger(__name__) # pylint: disable=invalid-name class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" def __init__( self : List[str] , _A : Dict , _A : List[Any] ): """simple docstring""" super().__init__() self.register_modules(unet=_A , scheduler=_A ) @torch.no_grad() def __call__( self : List[str] , _A : int = 1 , _A : int = 100 , _A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _A : Optional[float] = None , _A : bool = True , ): """simple docstring""" if audio_length_in_s is None: __SCREAMING_SNAKE_CASE : Optional[Any] = self.unet.config.sample_size / self.unet.config.sample_rate __SCREAMING_SNAKE_CASE : List[Any] = audio_length_in_s * self.unet.config.sample_rate __SCREAMING_SNAKE_CASE : Any = 2 ** len(self.unet.up_blocks ) if sample_size < 3 * down_scale_factor: raise ValueError( F'''{audio_length_in_s} is too small. Make sure it\'s bigger or equal to''' F''' {3 * down_scale_factor / self.unet.config.sample_rate}.''' ) __SCREAMING_SNAKE_CASE : int = int(_A ) if sample_size % down_scale_factor != 0: __SCREAMING_SNAKE_CASE : Optional[int] = ( (audio_length_in_s * self.unet.config.sample_rate) // down_scale_factor + 1 ) * down_scale_factor logger.info( F'''{audio_length_in_s} is increased to {sample_size / self.unet.config.sample_rate} so that it can be handled''' F''' by the model. It will be cut to {original_sample_size / self.unet.config.sample_rate} after the denoising''' ''' process.''' ) __SCREAMING_SNAKE_CASE : List[Any] = int(_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = next(iter(self.unet.parameters() ) ).dtype __SCREAMING_SNAKE_CASE : int = (batch_size, self.unet.config.in_channels, sample_size) if isinstance(_A , _A ) and len(_A ) != batch_size: raise ValueError( F'''You have passed a list of generators of length {len(_A )}, but requested an effective batch''' F''' size of {batch_size}. Make sure the batch size matches the length of the generators.''' ) __SCREAMING_SNAKE_CASE : Dict = randn_tensor(_A , generator=_A , device=self.device , dtype=_A ) # set step values self.scheduler.set_timesteps(_A , device=audio.device ) __SCREAMING_SNAKE_CASE : Dict = self.scheduler.timesteps.to(_A ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output __SCREAMING_SNAKE_CASE : List[Any] = self.unet(_A , _A ).sample # 2. compute previous image: x_t -> t_t-1 __SCREAMING_SNAKE_CASE : Optional[int] = self.scheduler.step(_A , _A , _A ).prev_sample __SCREAMING_SNAKE_CASE : str = audio.clamp(-1 , 1 ).float().cpu().numpy() __SCREAMING_SNAKE_CASE : str = audio[:, :, :original_sample_size] if not return_dict: return (audio,) return AudioPipelineOutput(audios=_A )
303
0
'''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 __SCREAMING_SNAKE_CASE : Optional[Any] = 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 UpperCamelCase_ ( _UpperCAmelCase : np.ndarray , _UpperCAmelCase : float , _UpperCAmelCase : int = 16_000 ) -> Any: """simple docstring""" _UpperCAmelCase : Union[str, Any] = int(round(sample_rate * max_length ) ) if len(_UpperCAmelCase ) <= sample_length: return wav _UpperCAmelCase : Optional[Any] = randint(0 , len(_UpperCAmelCase ) - sample_length - 1 ) return wav[random_offset : random_offset + sample_length] @dataclass class lowerCamelCase_ : '''simple docstring''' __UpperCamelCase: Optional[str] = field(default=snake_case__ , metadata={"help": "Name of a dataset from the datasets package"} ) __UpperCamelCase: Optional[str] = field( default=snake_case__ , metadata={"help": "The configuration name of the dataset to use (via the datasets library)."} ) __UpperCamelCase: Optional[str] = field( default=snake_case__ , metadata={"help": "A file containing the training audio paths and labels."} ) __UpperCamelCase: Optional[str] = field( default=snake_case__ , metadata={"help": "A file containing the validation audio paths and labels."} ) __UpperCamelCase: str = field( default="train" , metadata={ "help": "The name of the training data set split to use (via the datasets library). Defaults to 'train'" } , ) __UpperCamelCase: str = field( default="validation" , metadata={ "help": ( "The name of the training data set split to use (via the datasets library). Defaults to 'validation'" ) } , ) __UpperCamelCase: str = field( default="audio" , metadata={"help": "The name of the dataset column containing the audio data. Defaults to 'audio'"} , ) __UpperCamelCase: str = field( default="label" , metadata={"help": "The name of the dataset column containing the labels. Defaults to 'label'"} ) __UpperCamelCase: Optional[int] = field( default=snake_case__ , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of training examples to this " "value if set." ) } , ) __UpperCamelCase: Optional[int] = field( default=snake_case__ , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of evaluation examples to this " "value if set." ) } , ) __UpperCamelCase: 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 lowerCamelCase_ : '''simple docstring''' __UpperCamelCase: str = field( default="facebook/wav2vec2-base" , metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} , ) __UpperCamelCase: Optional[str] = field( default=snake_case__ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) __UpperCamelCase: Optional[str] = field( default=snake_case__ , metadata={"help": "Where do you want to store the pretrained models downloaded from the Hub"} ) __UpperCamelCase: str = field( default="main" , metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."} , ) __UpperCamelCase: Optional[str] = field( default=snake_case__ , metadata={"help": "Name or path of preprocessor config."} ) __UpperCamelCase: bool = field( default=snake_case__ , metadata={"help": "Whether to freeze the feature encoder layers of the model."} ) __UpperCamelCase: bool = field( default=snake_case__ , metadata={"help": "Whether to generate an attention mask in the feature extractor."} ) __UpperCamelCase: bool = field( default=snake_case__ , metadata={ "help": ( "Will use the token generated when running `huggingface-cli login` (necessary to use this script " "with private models)." ) } , ) __UpperCamelCase: Optional[bool] = field( default=snake_case__ , metadata={"help": "Whether to freeze the feature extractor layers of the model."} ) __UpperCamelCase: bool = field( default=snake_case__ , metadata={"help": "Will enable to load a pretrained model whose head dimensions are different."} , ) def _A ( self : str ): 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 UpperCamelCase_ ( ) -> List[Any]: """simple docstring""" _UpperCAmelCase : int = 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. _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : Dict = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : str = 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() _UpperCAmelCase : Optional[int] = 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. _UpperCAmelCase : Optional[int] = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: _UpperCAmelCase : Any = 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. _UpperCAmelCase : Union[str, Any] = DatasetDict() _UpperCAmelCase : Optional[Any] = 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 , ) _UpperCAmelCase : Tuple = 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 _UpperCAmelCase : Dict = 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. _UpperCAmelCase : Any = raw_datasets.cast_column( data_args.audio_column_name , datasets.features.Audio(sampling_rate=feature_extractor.sampling_rate ) ) _UpperCAmelCase : Optional[int] = feature_extractor.model_input_names[0] def train_transforms(_UpperCAmelCase : Optional[Any] ): _UpperCAmelCase : Optional[Any] = [] for audio in batch[data_args.audio_column_name]: _UpperCAmelCase : int = random_subsample( audio["array"] , max_length=data_args.max_length_seconds , sample_rate=feature_extractor.sampling_rate ) subsampled_wavs.append(_UpperCAmelCase ) _UpperCAmelCase : str = feature_extractor(_UpperCAmelCase , sampling_rate=feature_extractor.sampling_rate ) _UpperCAmelCase : Dict = {model_input_name: inputs.get(_UpperCAmelCase )} _UpperCAmelCase : Tuple = list(batch[data_args.label_column_name] ) return output_batch def val_transforms(_UpperCAmelCase : Any ): _UpperCAmelCase : str = [audio["array"] for audio in batch[data_args.audio_column_name]] _UpperCAmelCase : List[Any] = feature_extractor(_UpperCAmelCase , sampling_rate=feature_extractor.sampling_rate ) _UpperCAmelCase : str = {model_input_name: inputs.get(_UpperCAmelCase )} _UpperCAmelCase : List[str] = 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. _UpperCAmelCase : int = raw_datasets["train"].features[data_args.label_column_name].names _UpperCAmelCase , _UpperCAmelCase : Any = {}, {} for i, label in enumerate(_UpperCAmelCase ): _UpperCAmelCase : Union[str, Any] = str(_UpperCAmelCase ) _UpperCAmelCase : str = label # Load the accuracy metric from the datasets package _UpperCAmelCase : Dict = 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 : Optional[int] ): _UpperCAmelCase : str = np.argmax(eval_pred.predictions , axis=1 ) return metric.compute(predictions=_UpperCAmelCase , references=eval_pred.label_ids ) _UpperCAmelCase : int = 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 , ) _UpperCAmelCase : Dict = 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: _UpperCAmelCase : List[str] = ( 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: _UpperCAmelCase : Tuple = ( 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 _UpperCAmelCase : Dict = 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: _UpperCAmelCase : List[Any] = None if training_args.resume_from_checkpoint is not None: _UpperCAmelCase : Optional[Any] = training_args.resume_from_checkpoint elif last_checkpoint is not None: _UpperCAmelCase : int = last_checkpoint _UpperCAmelCase : Dict = 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: _UpperCAmelCase : Tuple = trainer.evaluate() trainer.log_metrics("eval" , _UpperCAmelCase ) trainer.save_metrics("eval" , _UpperCAmelCase ) # Write model card and (optionally) push to hub _UpperCAmelCase : Tuple = { "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()
31
import importlib import torch import yaml from omegaconf import OmegaConf from taming.models.vqgan import VQModel def a__ ( snake_case , snake_case=False ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = OmegaConf.load(snake_case ) if display: print(yaml.dump(OmegaConf.to_container(snake_case ) ) ) return config def a__ ( snake_case , snake_case=None , snake_case=None ): """simple docstring""" if conf_path is None: __SCREAMING_SNAKE_CASE : Any = '''./model_checkpoints/vqgan_only.yaml''' __SCREAMING_SNAKE_CASE : List[str] = load_config(snake_case , display=snake_case ) __SCREAMING_SNAKE_CASE : str = VQModel(**config.model.params ) if ckpt_path is None: __SCREAMING_SNAKE_CASE : Optional[Any] = '''./model_checkpoints/vqgan_only.pt''' __SCREAMING_SNAKE_CASE : Optional[Any] = torch.load(snake_case , map_location=snake_case ) if ".ckpt" in ckpt_path: __SCREAMING_SNAKE_CASE : Optional[Any] = sd['''state_dict'''] model.load_state_dict(snake_case , strict=snake_case ) model.to(snake_case ) del sd return model def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Any = model.encode(snake_case ) print(F'''VQGAN --- {model.__class__.__name__}: latent shape: {z.shape[2:]}''' ) __SCREAMING_SNAKE_CASE : Any = model.decode(snake_case ) return xrec def a__ ( snake_case , snake_case=False ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = string.rsplit('''.''' , 1 ) if reload: __SCREAMING_SNAKE_CASE : Union[str, Any] = importlib.import_module(snake_case ) importlib.reload(snake_case ) return getattr(importlib.import_module(snake_case , package=snake_case ) , cls ) def a__ ( snake_case ): """simple docstring""" if "target" not in config: raise KeyError('''Expected key `target` to instantiate.''' ) return get_obj_from_str(config['''target'''] )(**config.get('''params''' , {} ) ) def a__ ( snake_case , snake_case , snake_case=True , snake_case=True ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = instantiate_from_config(snake_case ) if sd is not None: model.load_state_dict(snake_case ) if gpu: model.cuda() if eval_mode: model.eval() return {"model": model} def a__ ( snake_case , snake_case , snake_case , snake_case ): """simple docstring""" # load the specified checkpoint if ckpt: __SCREAMING_SNAKE_CASE : Dict = torch.load(snake_case , map_location='''cpu''' ) __SCREAMING_SNAKE_CASE : List[Any] = pl_sd['''global_step'''] print(F'''loaded model from global step {global_step}.''' ) else: __SCREAMING_SNAKE_CASE : Optional[Any] = {'''state_dict''': None} __SCREAMING_SNAKE_CASE : Optional[Any] = None __SCREAMING_SNAKE_CASE : Dict = load_model_from_config(config.model , pl_sd['''state_dict'''] , gpu=snake_case , eval_mode=snake_case )['''model'''] return model, global_step
303
0
import enum import shutil import sys UpperCAmelCase_ , UpperCAmelCase_ : List[str] = shutil.get_terminal_size() UpperCAmelCase_ : List[Any] = {'UP': 'A', 'DOWN': 'B', 'RIGHT': 'C', 'LEFT': 'D'} class SCREAMING_SNAKE_CASE__ ( enum.Enum ): snake_case__ : Optional[Any] = 0 snake_case__ : Dict = 1 def SCREAMING_SNAKE_CASE_ ( __A : Tuple , __A : Optional[Any]="" ) -> List[str]: """simple docstring""" sys.stdout.write(str(__A ) + end ) sys.stdout.flush() def SCREAMING_SNAKE_CASE_ ( __A : List[str] , __A : int , __A : List[Any]="" ) -> int: """simple docstring""" forceWrite(F"""\u001b[{color}m{content}\u001b[0m""" , __A ) def SCREAMING_SNAKE_CASE_ ( ) -> Dict: """simple docstring""" forceWrite('\r' ) def SCREAMING_SNAKE_CASE_ ( __A : int , __A : str ) -> Optional[int]: """simple docstring""" forceWrite(F"""\033[{num_lines}{CURSOR_TO_CHAR[direction.upper()]}""" ) def SCREAMING_SNAKE_CASE_ ( ) -> Any: """simple docstring""" forceWrite(' ' * TERMINAL_WIDTH ) reset_cursor() def SCREAMING_SNAKE_CASE_ ( ) -> Dict: """simple docstring""" reset_cursor() forceWrite('-' * TERMINAL_WIDTH )
32
from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { """studio-ousia/luke-base""": """https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json""", """studio-ousia/luke-large""": """https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json""", } class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''luke''' def __init__( self : Any , _A : int=5_0267 , _A : str=50_0000 , _A : Dict=768 , _A : int=256 , _A : Tuple=12 , _A : Optional[Any]=12 , _A : Any=3072 , _A : Tuple="gelu" , _A : Any=0.1 , _A : Dict=0.1 , _A : Any=512 , _A : Tuple=2 , _A : int=0.02 , _A : Any=1e-12 , _A : Dict=True , _A : Optional[Any]=None , _A : List[str]=1 , _A : List[str]=0 , _A : Dict=2 , **_A : List[str] , ): """simple docstring""" super().__init__(pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , **_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = vocab_size __SCREAMING_SNAKE_CASE : Any = entity_vocab_size __SCREAMING_SNAKE_CASE : int = hidden_size __SCREAMING_SNAKE_CASE : List[Any] = entity_emb_size __SCREAMING_SNAKE_CASE : Union[str, Any] = num_hidden_layers __SCREAMING_SNAKE_CASE : Tuple = num_attention_heads __SCREAMING_SNAKE_CASE : Dict = hidden_act __SCREAMING_SNAKE_CASE : Dict = intermediate_size __SCREAMING_SNAKE_CASE : Optional[int] = hidden_dropout_prob __SCREAMING_SNAKE_CASE : Any = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : List[Any] = max_position_embeddings __SCREAMING_SNAKE_CASE : List[str] = type_vocab_size __SCREAMING_SNAKE_CASE : Dict = initializer_range __SCREAMING_SNAKE_CASE : Tuple = layer_norm_eps __SCREAMING_SNAKE_CASE : int = use_entity_aware_attention __SCREAMING_SNAKE_CASE : Any = classifier_dropout
303
0
"""simple docstring""" import argparse import os import re import packaging.version __A : List[str] = '''examples/''' __A : int = { '''examples''': (re.compile(R'''^check_min_version\("[^"]+"\)\s*$''', re.MULTILINE), '''check_min_version("VERSION")\n'''), '''init''': (re.compile(R'''^__version__\s+=\s+"([^"]+)"\s*$''', re.MULTILINE), '''__version__ = "VERSION"\n'''), '''setup''': (re.compile(R'''^(\s*)version\s*=\s*"[^"]+",''', re.MULTILINE), R'''\1version="VERSION",'''), '''doc''': (re.compile(R'''^(\s*)release\s*=\s*"[^"]+"$''', re.MULTILINE), '''release = "VERSION"\n'''), } __A : Dict = { '''init''': '''src/transformers/__init__.py''', '''setup''': '''setup.py''', } __A : Optional[int] = '''README.md''' def lowercase ( __snake_case : int , __snake_case : Any , __snake_case : int ): with open(__snake_case , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: lowercase_ : int = f.read() lowercase_ , lowercase_ : List[str] = REPLACE_PATTERNS[pattern] lowercase_ : Union[str, Any] = replace.replace('''VERSION''' , __snake_case ) lowercase_ : Optional[Any] = re_pattern.sub(__snake_case , __snake_case ) with open(__snake_case , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.write(__snake_case ) def lowercase ( __snake_case : int ): for folder, directories, fnames in os.walk(__snake_case ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove('''research_projects''' ) if "legacy" in directories: directories.remove('''legacy''' ) for fname in fnames: if fname.endswith('''.py''' ): update_version_in_file(os.path.join(__snake_case , __snake_case ) , __snake_case , pattern='''examples''' ) def lowercase ( __snake_case : Optional[Any] , __snake_case : Optional[Any]=False ): for pattern, fname in REPLACE_FILES.items(): update_version_in_file(__snake_case , __snake_case , __snake_case ) if not patch: update_version_in_examples(__snake_case ) def lowercase ( ): lowercase_ : Union[str, Any] = '''🤗 Transformers currently provides the following architectures''' lowercase_ : Union[str, Any] = '''1. Want to contribute a new model?''' with open(__snake_case , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: lowercase_ : List[str] = f.readlines() # Find the start of the list. lowercase_ : Optional[Any] = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 lowercase_ : str = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith('''1.''' ): lowercase_ : str = lines[index].replace( '''https://huggingface.co/docs/transformers/main/model_doc''' , '''https://huggingface.co/docs/transformers/model_doc''' , ) index += 1 with open(__snake_case , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.writelines(__snake_case ) def lowercase ( ): with open(REPLACE_FILES['''init'''] , '''r''' ) as f: lowercase_ : List[Any] = f.read() lowercase_ : List[str] = REPLACE_PATTERNS['''init'''][0].search(__snake_case ).groups()[0] return packaging.version.parse(__snake_case ) def lowercase ( __snake_case : Optional[Any]=False ): lowercase_ : str = get_version() if patch and default_version.is_devrelease: raise ValueError('''Can\'t create a patch version from the dev branch, checkout a released version!''' ) if default_version.is_devrelease: lowercase_ : Optional[Any] = default_version.base_version elif patch: lowercase_ : Optional[int] = F'''{default_version.major}.{default_version.minor}.{default_version.micro + 1}''' else: lowercase_ : Optional[int] = F'''{default_version.major}.{default_version.minor + 1}.0''' # Now let's ask nicely if that's the right one. lowercase_ : int = input(F'''Which version are you releasing? [{default_version}]''' ) if len(__snake_case ) == 0: lowercase_ : Dict = default_version print(F'''Updating version to {version}.''' ) global_version_update(__snake_case , patch=__snake_case ) if not patch: print('''Cleaning main README, don\'t forget to run `make fix-copies`.''' ) clean_main_ref_in_model_list() def lowercase ( ): lowercase_ : List[Any] = get_version() lowercase_ : List[str] = F'''{current_version.major}.{current_version.minor + 1}.0.dev0''' lowercase_ : Any = current_version.base_version # Check with the user we got that right. lowercase_ : Tuple = input(F'''Which version are we developing now? [{dev_version}]''' ) if len(__snake_case ) == 0: lowercase_ : str = dev_version print(F'''Updating version to {version}.''' ) global_version_update(__snake_case ) print('''Cleaning main README, don\'t forget to run `make fix-copies`.''' ) clean_main_ref_in_model_list() if __name__ == "__main__": __A : int = argparse.ArgumentParser() parser.add_argument('''--post_release''', action='''store_true''', help='''Whether this is pre or post release.''') parser.add_argument('''--patch''', action='''store_true''', help='''Whether or not this is a patch release.''') __A : Any = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print('''Nothing to do after a patch :-)''') else: post_release_work()
33
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase_ = { """configuration_electra""": ["""ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ElectraConfig""", """ElectraOnnxConfig"""], """tokenization_electra""": ["""ElectraTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""ElectraTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST""", """ElectraForCausalLM""", """ElectraForMaskedLM""", """ElectraForMultipleChoice""", """ElectraForPreTraining""", """ElectraForQuestionAnswering""", """ElectraForSequenceClassification""", """ElectraForTokenClassification""", """ElectraModel""", """ElectraPreTrainedModel""", """load_tf_weights_in_electra""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFElectraForMaskedLM""", """TFElectraForMultipleChoice""", """TFElectraForPreTraining""", """TFElectraForQuestionAnswering""", """TFElectraForSequenceClassification""", """TFElectraForTokenClassification""", """TFElectraModel""", """TFElectraPreTrainedModel""", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """FlaxElectraForCausalLM""", """FlaxElectraForMaskedLM""", """FlaxElectraForMultipleChoice""", """FlaxElectraForPreTraining""", """FlaxElectraForQuestionAnswering""", """FlaxElectraForSequenceClassification""", """FlaxElectraForTokenClassification""", """FlaxElectraModel""", """FlaxElectraPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_electra import ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ElectraConfig, ElectraOnnxConfig from .tokenization_electra import ElectraTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_electra_fast import ElectraTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_electra import ( ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, ElectraForCausalLM, ElectraForMaskedLM, ElectraForMultipleChoice, ElectraForPreTraining, ElectraForQuestionAnswering, ElectraForSequenceClassification, ElectraForTokenClassification, ElectraModel, ElectraPreTrainedModel, load_tf_weights_in_electra, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_electra import ( TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, TFElectraForMaskedLM, TFElectraForMultipleChoice, TFElectraForPreTraining, TFElectraForQuestionAnswering, TFElectraForSequenceClassification, TFElectraForTokenClassification, TFElectraModel, TFElectraPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_electra import ( FlaxElectraForCausalLM, FlaxElectraForMaskedLM, FlaxElectraForMultipleChoice, FlaxElectraForPreTraining, FlaxElectraForQuestionAnswering, FlaxElectraForSequenceClassification, FlaxElectraForTokenClassification, FlaxElectraModel, FlaxElectraPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
303
0
'''simple docstring''' from collections import deque from .hash_table import HashTable class _a ( __a ): def __init__( self : Union[str, Any] , *lowercase : Tuple , **lowercase : Optional[int] ): '''simple docstring''' super().__init__(*lowercase , **lowercase ) def A ( self : List[str] , lowercase : Dict , lowercase : Dict ): '''simple docstring''' UpperCAmelCase = deque([] ) if self.values[key] is None else self.values[key] self.values[key].appendleft(lowercase ) UpperCAmelCase = self.values[key] def A ( self : Dict ): '''simple docstring''' return ( sum(self.charge_factor - len(lowercase ) for slot in self.values ) / self.size_table * self.charge_factor ) def A ( self : str , lowercase : Dict , lowercase : Optional[int]=None ): '''simple docstring''' if not ( len(self.values[key] ) == self.charge_factor and self.values.count(lowercase ) == 0 ): return key return super()._collision_resolution(lowercase , lowercase )
34
from typing import Dict, List, Optional, Tuple, Union import torch from ...models import AutoencoderKL, TransformeraDModel from ...schedulers import KarrasDiffusionSchedulers from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" def __init__( self : List[Any] , _A : TransformeraDModel , _A : AutoencoderKL , _A : KarrasDiffusionSchedulers , _A : Optional[Dict[int, str]] = None , ): """simple docstring""" super().__init__() self.register_modules(transformer=_A , vae=_A , scheduler=_A ) # create a imagenet -> id dictionary for easier use __SCREAMING_SNAKE_CASE : Optional[int] = {} if idalabel is not None: for key, value in idalabel.items(): for label in value.split(''',''' ): __SCREAMING_SNAKE_CASE : Optional[Any] = int(_A ) __SCREAMING_SNAKE_CASE : List[str] = dict(sorted(self.labels.items() ) ) def UpperCAmelCase__ ( self : List[Any] , _A : Union[str, List[str]] ): """simple docstring""" if not isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : Union[str, Any] = list(_A ) for l in label: if l not in self.labels: raise ValueError( F'''{l} does not exist. Please make sure to select one of the following labels: \n {self.labels}.''' ) return [self.labels[l] for l in label] @torch.no_grad() def __call__( self : Dict , _A : List[int] , _A : float = 4.0 , _A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _A : int = 50 , _A : Optional[str] = "pil" , _A : bool = True , ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = len(_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = self.transformer.config.sample_size __SCREAMING_SNAKE_CASE : List[Any] = self.transformer.config.in_channels __SCREAMING_SNAKE_CASE : Optional[int] = randn_tensor( shape=(batch_size, latent_channels, latent_size, latent_size) , generator=_A , device=self.device , dtype=self.transformer.dtype , ) __SCREAMING_SNAKE_CASE : Tuple = torch.cat([latents] * 2 ) if guidance_scale > 1 else latents __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor(_A , device=self.device ).reshape(-1 ) __SCREAMING_SNAKE_CASE : Any = torch.tensor([1000] * batch_size , device=self.device ) __SCREAMING_SNAKE_CASE : Any = torch.cat([class_labels, class_null] , 0 ) if guidance_scale > 1 else class_labels # set step values self.scheduler.set_timesteps(_A ) for t in self.progress_bar(self.scheduler.timesteps ): if guidance_scale > 1: __SCREAMING_SNAKE_CASE : Optional[Any] = latent_model_input[: len(_A ) // 2] __SCREAMING_SNAKE_CASE : List[Any] = torch.cat([half, half] , dim=0 ) __SCREAMING_SNAKE_CASE : int = self.scheduler.scale_model_input(_A , _A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = t if not torch.is_tensor(_A ): # TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can # This would be a good case for the `match` statement (Python 3.10+) __SCREAMING_SNAKE_CASE : Any = latent_model_input.device.type == '''mps''' if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : List[Any] = torch.floataa if is_mps else torch.floataa else: __SCREAMING_SNAKE_CASE : int = torch.intaa if is_mps else torch.intaa __SCREAMING_SNAKE_CASE : int = torch.tensor([timesteps] , dtype=_A , device=latent_model_input.device ) elif len(timesteps.shape ) == 0: __SCREAMING_SNAKE_CASE : Optional[Any] = timesteps[None].to(latent_model_input.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML __SCREAMING_SNAKE_CASE : Optional[int] = timesteps.expand(latent_model_input.shape[0] ) # predict noise model_output __SCREAMING_SNAKE_CASE : Union[str, Any] = self.transformer( _A , timestep=_A , class_labels=_A ).sample # perform guidance if guidance_scale > 1: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = noise_pred[:, :latent_channels], noise_pred[:, latent_channels:] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = torch.split(_A , len(_A ) // 2 , dim=0 ) __SCREAMING_SNAKE_CASE : str = uncond_eps + guidance_scale * (cond_eps - uncond_eps) __SCREAMING_SNAKE_CASE : List[Any] = torch.cat([half_eps, half_eps] , dim=0 ) __SCREAMING_SNAKE_CASE : List[str] = torch.cat([eps, rest] , dim=1 ) # learned sigma if self.transformer.config.out_channels // 2 == latent_channels: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = torch.split(_A , _A , dim=1 ) else: __SCREAMING_SNAKE_CASE : List[Any] = noise_pred # compute previous image: x_t -> x_t-1 __SCREAMING_SNAKE_CASE : str = self.scheduler.step(_A , _A , _A ).prev_sample if guidance_scale > 1: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = latent_model_input.chunk(2 , dim=0 ) else: __SCREAMING_SNAKE_CASE : Optional[Any] = latent_model_input __SCREAMING_SNAKE_CASE : List[Any] = 1 / self.vae.config.scaling_factor * latents __SCREAMING_SNAKE_CASE : List[str] = self.vae.decode(_A ).sample __SCREAMING_SNAKE_CASE : Any = (samples / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 __SCREAMING_SNAKE_CASE : int = samples.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": __SCREAMING_SNAKE_CASE : str = self.numpy_to_pil(_A ) if not return_dict: return (samples,) return ImagePipelineOutput(images=_A )
303
0
'''simple docstring''' import os import sys import tempfile import torch from .state import AcceleratorState from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment def __snake_case( _lowerCAmelCase , _lowerCAmelCase=() , _lowerCAmelCase=None , _lowerCAmelCase="no" , _lowerCAmelCase="29500" ) -> int: snake_case__ : List[str] = False snake_case__ : Tuple = False if any(key.startswith("""KAGGLE""" ) for key in os.environ.keys() ): snake_case__ : List[str] = True elif "IPython" in sys.modules: snake_case__ : int = """google.colab""" in str(sys.modules["""IPython"""].get_ipython() ) try: snake_case__ : Any = PrecisionType(mixed_precision.lower() ) except ValueError: raise ValueError( f"Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}." ) if (in_colab or in_kaggle) and (os.environ.get("""TPU_NAME""" , _lowerCAmelCase ) is not None): # TPU launch import torch_xla.distributed.xla_multiprocessing as xmp if len(AcceleratorState._shared_state ) > 0: raise ValueError( """To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside """ """your training function. Restart your notebook and make sure no cells initializes an """ """`Accelerator`.""" ) if num_processes is None: snake_case__ : Union[str, Any] = 8 snake_case__ : str = PrepareForLaunch(_lowerCAmelCase , distributed_type="""TPU""" ) print(f"Launching a training on {num_processes} TPU cores." ) xmp.spawn(_lowerCAmelCase , args=_lowerCAmelCase , nprocs=_lowerCAmelCase , start_method="""fork""" ) elif in_colab: # No need for a distributed launch otherwise as it's either CPU or one GPU. if torch.cuda.is_available(): print("""Launching training on one GPU.""" ) else: print("""Launching training on one CPU.""" ) function(*_lowerCAmelCase ) else: if num_processes is None: raise ValueError( """You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call.""" ) if num_processes > 1: # Multi-GPU launch from torch.multiprocessing import start_processes from torch.multiprocessing.spawn import ProcessRaisedException if len(AcceleratorState._shared_state ) > 0: raise ValueError( """To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized """ """inside your training function. Restart your notebook and make sure no cells initializes an """ """`Accelerator`.""" ) if torch.cuda.is_initialized(): raise ValueError( """To launch a multi-GPU training from your notebook, you need to avoid running any instruction """ """using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA """ """function.""" ) # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=_lowerCAmelCase , master_addr="""127.0.01""" , master_port=_lowerCAmelCase , mixed_precision=_lowerCAmelCase ): snake_case__ : Optional[Any] = PrepareForLaunch(_lowerCAmelCase , distributed_type="""MULTI_GPU""" ) print(f"Launching training on {num_processes} GPUs." ) try: start_processes(_lowerCAmelCase , args=_lowerCAmelCase , nprocs=_lowerCAmelCase , start_method="""fork""" ) except ProcessRaisedException as e: if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]: raise RuntimeError( """CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. """ """This likely stems from an outside import causing issues once the `notebook_launcher()` is called. """ """Please review your imports and test them when running the `notebook_launcher()` to identify """ """which one is problematic.""" ) from e else: # No need for a distributed launch otherwise as it's either CPU, GPU or MPS. if is_mps_available(): snake_case__ : Tuple = """1""" print("""Launching training on MPS.""" ) elif torch.cuda.is_available(): print("""Launching training on one GPU.""" ) else: print("""Launching training on CPU.""" ) function(*_lowerCAmelCase ) def __snake_case( _lowerCAmelCase , _lowerCAmelCase=() , _lowerCAmelCase=2 ) -> Tuple: from torch.multiprocessing import start_processes with tempfile.NamedTemporaryFile() as tmp_file: # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=_lowerCAmelCase , master_addr="""127.0.01""" , master_port="""29500""" , accelerate_mixed_precision="""no""" , accelerate_debug_rdv_file=tmp_file.name , accelerate_use_cpu="""yes""" , ): snake_case__ : Tuple = PrepareForLaunch(_lowerCAmelCase , debug=_lowerCAmelCase ) start_processes(_lowerCAmelCase , args=_lowerCAmelCase , nprocs=_lowerCAmelCase , start_method="""fork""" )
35
import os import sys lowercase_ = os.path.join(os.path.dirname(__file__), """src""") sys.path.append(SRC_DIR) from transformers import ( AutoConfig, AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoTokenizer, add_start_docstrings, ) lowercase_ = [ """torch""", """numpy""", """tokenizers""", """filelock""", """requests""", """tqdm""", """regex""", """sentencepiece""", """sacremoses""", """importlib_metadata""", """huggingface_hub""", ] @add_start_docstrings(AutoConfig.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoConfig.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoTokenizer.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoTokenizer.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModel.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModel.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForCausalLM.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForCausalLM.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForMaskedLM.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForMaskedLM.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForSequenceClassification.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForSequenceClassification.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForQuestionAnswering.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForQuestionAnswering.from_pretrained(*snake_case , **snake_case )
303
0
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor @require_vision class UpperCAmelCase_ ( unittest.TestCase): def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : Dict = tempfile.mkdtemp() _lowerCAmelCase : List[Any] = [ "[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "的", "价", "格", "是", "15", "便", "alex", "##andra", ",", "。", "-", "t", "shirt", ] _lowerCAmelCase : Tuple = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"]) with open(self.vocab_file, "w", encoding="utf-8") as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens])) _lowerCAmelCase : str = { "do_resize": True, "size": {"height": 224, "width": 224}, "do_center_crop": True, "crop_size": {"height": 18, "width": 18}, "do_normalize": True, "image_mean": [0.48_145_466, 0.4_578_275, 0.40_821_073], "image_std": [0.26_862_954, 0.26_130_258, 0.27_577_711], "do_convert_rgb": True, } _lowerCAmelCase : Union[str, Any] = os.path.join(self.tmpdirname, __a) with open(self.image_processor_file, "w", encoding="utf-8") as fp: json.dump(__a, __a) def snake_case__ ( self, **__a): '''simple docstring''' return BertTokenizer.from_pretrained(self.tmpdirname, **__a) def snake_case__ ( self, **__a): '''simple docstring''' return BertTokenizerFast.from_pretrained(self.tmpdirname, **__a) def snake_case__ ( self, **__a): '''simple docstring''' return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname, **__a) def snake_case__ ( self): '''simple docstring''' shutil.rmtree(self.tmpdirname) def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : str = [np.random.randint(255, size=(3, 30, 400), dtype=np.uinta)] _lowerCAmelCase : Any = [Image.fromarray(np.moveaxis(__a, 0, -1)) for x in image_inputs] return image_inputs def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : List[Any] = self.get_tokenizer() _lowerCAmelCase : Dict = self.get_rust_tokenizer() _lowerCAmelCase : Optional[int] = self.get_image_processor() _lowerCAmelCase : Tuple = ChineseCLIPProcessor(tokenizer=__a, image_processor=__a) processor_slow.save_pretrained(self.tmpdirname) _lowerCAmelCase : Optional[Any] = ChineseCLIPProcessor.from_pretrained(self.tmpdirname, use_fast=__a) _lowerCAmelCase : Tuple = ChineseCLIPProcessor(tokenizer=__a, image_processor=__a) processor_fast.save_pretrained(self.tmpdirname) _lowerCAmelCase : Tuple = ChineseCLIPProcessor.from_pretrained(self.tmpdirname) self.assertEqual(processor_slow.tokenizer.get_vocab(), tokenizer_slow.get_vocab()) self.assertEqual(processor_fast.tokenizer.get_vocab(), tokenizer_fast.get_vocab()) self.assertEqual(tokenizer_slow.get_vocab(), tokenizer_fast.get_vocab()) self.assertIsInstance(processor_slow.tokenizer, __a) self.assertIsInstance(processor_fast.tokenizer, __a) self.assertEqual(processor_slow.image_processor.to_json_string(), image_processor.to_json_string()) self.assertEqual(processor_fast.image_processor.to_json_string(), image_processor.to_json_string()) self.assertIsInstance(processor_slow.image_processor, __a) self.assertIsInstance(processor_fast.image_processor, __a) def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : Union[str, Any] = ChineseCLIPProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) processor.save_pretrained(self.tmpdirname) _lowerCAmelCase : int = self.get_tokenizer(cls_token="(CLS)", sep_token="(SEP)") _lowerCAmelCase : Dict = self.get_image_processor(do_normalize=__a) _lowerCAmelCase : Tuple = ChineseCLIPProcessor.from_pretrained( self.tmpdirname, cls_token="(CLS)", sep_token="(SEP)", do_normalize=__a) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer, __a) self.assertEqual(processor.image_processor.to_json_string(), image_processor_add_kwargs.to_json_string()) self.assertIsInstance(processor.image_processor, __a) def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : List[str] = self.get_image_processor() _lowerCAmelCase : List[Any] = self.get_tokenizer() _lowerCAmelCase : List[Any] = ChineseCLIPProcessor(tokenizer=__a, image_processor=__a) _lowerCAmelCase : List[str] = self.prepare_image_inputs() _lowerCAmelCase : str = image_processor(__a, return_tensors="np") _lowerCAmelCase : Union[str, Any] = processor(images=__a, return_tensors="np") for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum(), input_processor[key].sum(), delta=1E-2) def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : Dict = self.get_image_processor() _lowerCAmelCase : int = self.get_tokenizer() _lowerCAmelCase : Optional[int] = ChineseCLIPProcessor(tokenizer=__a, image_processor=__a) _lowerCAmelCase : Optional[Any] = "Alexandra,T-shirt的价格是15便士。" _lowerCAmelCase : Union[str, Any] = processor(text=__a) _lowerCAmelCase : str = tokenizer(__a) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key], encoded_processor[key]) def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : List[Any] = self.get_image_processor() _lowerCAmelCase : int = self.get_tokenizer() _lowerCAmelCase : Dict = ChineseCLIPProcessor(tokenizer=__a, image_processor=__a) _lowerCAmelCase : int = "Alexandra,T-shirt的价格是15便士。" _lowerCAmelCase : Any = self.prepare_image_inputs() _lowerCAmelCase : Dict = processor(text=__a, images=__a) self.assertListEqual(list(inputs.keys()), ["input_ids", "token_type_ids", "attention_mask", "pixel_values"]) # test if it raises when no input is passed with pytest.raises(__a): processor() def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : int = self.get_image_processor() _lowerCAmelCase : List[str] = self.get_tokenizer() _lowerCAmelCase : Union[str, Any] = ChineseCLIPProcessor(tokenizer=__a, image_processor=__a) _lowerCAmelCase : Tuple = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] _lowerCAmelCase : Union[str, Any] = processor.batch_decode(__a) _lowerCAmelCase : Tuple = tokenizer.batch_decode(__a) self.assertListEqual(__a, __a) def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : List[Any] = self.get_image_processor() _lowerCAmelCase : Dict = self.get_tokenizer() _lowerCAmelCase : Dict = ChineseCLIPProcessor(tokenizer=__a, image_processor=__a) _lowerCAmelCase : Any = "Alexandra,T-shirt的价格是15便士。" _lowerCAmelCase : Optional[Any] = self.prepare_image_inputs() _lowerCAmelCase : Any = processor(text=__a, images=__a) self.assertListEqual(list(inputs.keys()), processor.model_input_names)
36
from __future__ import annotations import numpy as np def a__ ( snake_case ): """simple docstring""" return np.maximum(0 , snake_case ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
303
0
'''simple docstring''' import torch from diffusers import DiffusionPipeline class lowerCAmelCase_( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def __init__( self ,__UpperCAmelCase ,__UpperCAmelCase ) -> Tuple: super().__init__() self.register_modules(unet=__UpperCAmelCase ,scheduler=__UpperCAmelCase ) def __call__( self ) -> str: lowerCAmelCase__ : Tuple = torch.randn( (1, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size) ,) lowerCAmelCase__ : Union[str, Any] = 1 lowerCAmelCase__ : Union[str, Any] = self.unet(__UpperCAmelCase ,__UpperCAmelCase ).sample lowerCAmelCase__ : List[Any] = self.scheduler.step(__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ).prev_sample lowerCAmelCase__ : Optional[int] = scheduler_output - scheduler_output + torch.ones_like(__UpperCAmelCase ) return result
37
def a__ ( snake_case = 1_000_000 ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = 1 __SCREAMING_SNAKE_CASE : Optional[Any] = 1 __SCREAMING_SNAKE_CASE : Optional[int] = {1: 1} for inputa in range(2 , snake_case ): __SCREAMING_SNAKE_CASE : Tuple = 0 __SCREAMING_SNAKE_CASE : Optional[Any] = inputa while True: if number in counters: counter += counters[number] break if number % 2 == 0: number //= 2 counter += 1 else: __SCREAMING_SNAKE_CASE : List[Any] = (3 * number) + 1 counter += 1 if inputa not in counters: __SCREAMING_SNAKE_CASE : str = counter if counter > pre_counter: __SCREAMING_SNAKE_CASE : Optional[int] = inputa __SCREAMING_SNAKE_CASE : str = counter return largest_number if __name__ == "__main__": print(solution(int(input().strip())))
303
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCAmelCase_ : List[Any] = { '''configuration_xlm''': ['''XLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMConfig''', '''XLMOnnxConfig'''], '''tokenization_xlm''': ['''XLMTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : Optional[int] = [ '''XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XLMForMultipleChoice''', '''XLMForQuestionAnswering''', '''XLMForQuestionAnsweringSimple''', '''XLMForSequenceClassification''', '''XLMForTokenClassification''', '''XLMModel''', '''XLMPreTrainedModel''', '''XLMWithLMHeadModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : Dict = [ '''TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXLMForMultipleChoice''', '''TFXLMForQuestionAnsweringSimple''', '''TFXLMForSequenceClassification''', '''TFXLMForTokenClassification''', '''TFXLMMainLayer''', '''TFXLMModel''', '''TFXLMPreTrainedModel''', '''TFXLMWithLMHeadModel''', ] if TYPE_CHECKING: from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig from .tokenization_xlm import XLMTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm import ( XLM_PRETRAINED_MODEL_ARCHIVE_LIST, XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMPreTrainedModel, XLMWithLMHeadModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm import ( TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMForMultipleChoice, TFXLMForQuestionAnsweringSimple, TFXLMForSequenceClassification, TFXLMForTokenClassification, TFXLMMainLayer, TFXLMModel, TFXLMPreTrainedModel, TFXLMWithLMHeadModel, ) else: import sys UpperCAmelCase_ : List[Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
38
import argparse import json import os import pickle import shutil import numpy as np import torch from distiller import Distiller from lm_seqs_dataset import LmSeqsDataset from transformers import ( BertConfig, BertForMaskedLM, BertTokenizer, DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer, GPTaConfig, GPTaLMHeadModel, GPTaTokenizer, RobertaConfig, RobertaForMaskedLM, RobertaTokenizer, ) from utils import git_log, init_gpu_params, logger, set_seed lowercase_ = { """distilbert""": (DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer), """roberta""": (RobertaConfig, RobertaForMaskedLM, RobertaTokenizer), """bert""": (BertConfig, BertForMaskedLM, BertTokenizer), """gpt2""": (GPTaConfig, GPTaLMHeadModel, GPTaTokenizer), } def a__ ( snake_case ): """simple docstring""" assert (args.mlm and args.alpha_mlm > 0.0) or (not args.mlm and args.alpha_mlm == 0.0) assert (args.alpha_mlm > 0.0 and args.alpha_clm == 0.0) or (args.alpha_mlm == 0.0 and args.alpha_clm > 0.0) if args.mlm: assert os.path.isfile(args.token_counts ) assert (args.student_type in ["roberta", "distilbert"]) and (args.teacher_type in ["roberta", "bert"]) else: assert (args.student_type in ["gpt2"]) and (args.teacher_type in ["gpt2"]) assert args.teacher_type == args.student_type or ( args.student_type == "distilbert" and args.teacher_type == "bert" ) assert os.path.isfile(args.student_config ) if args.student_pretrained_weights is not None: assert os.path.isfile(args.student_pretrained_weights ) if args.freeze_token_type_embds: assert args.student_type in ["roberta"] assert args.alpha_ce >= 0.0 assert args.alpha_mlm >= 0.0 assert args.alpha_clm >= 0.0 assert args.alpha_mse >= 0.0 assert args.alpha_cos >= 0.0 assert args.alpha_ce + args.alpha_mlm + args.alpha_clm + args.alpha_mse + args.alpha_cos > 0.0 def a__ ( snake_case , snake_case ): """simple docstring""" if args.student_type == "roberta": __SCREAMING_SNAKE_CASE : int = False elif args.student_type == "gpt2": __SCREAMING_SNAKE_CASE : Optional[int] = False def a__ ( snake_case , snake_case ): """simple docstring""" if args.student_type == "roberta": __SCREAMING_SNAKE_CASE : Dict = False def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = argparse.ArgumentParser(description='''Training''' ) parser.add_argument('''--force''' , action='''store_true''' , help='''Overwrite dump_path if it already exists.''' ) parser.add_argument( '''--dump_path''' , type=snake_case , required=snake_case , help='''The output directory (log, checkpoints, parameters, etc.)''' ) parser.add_argument( '''--data_file''' , type=snake_case , required=snake_case , help='''The binarized file (tokenized + tokens_to_ids) and grouped by sequence.''' , ) parser.add_argument( '''--student_type''' , type=snake_case , choices=['''distilbert''', '''roberta''', '''gpt2'''] , required=snake_case , help='''The student type (DistilBERT, RoBERTa).''' , ) parser.add_argument('''--student_config''' , type=snake_case , required=snake_case , help='''Path to the student configuration.''' ) parser.add_argument( '''--student_pretrained_weights''' , default=snake_case , type=snake_case , help='''Load student initialization checkpoint.''' ) parser.add_argument( '''--teacher_type''' , choices=['''bert''', '''roberta''', '''gpt2'''] , required=snake_case , help='''Teacher type (BERT, RoBERTa).''' ) parser.add_argument('''--teacher_name''' , type=snake_case , required=snake_case , help='''The teacher model.''' ) parser.add_argument('''--temperature''' , default=2.0 , type=snake_case , help='''Temperature for the softmax temperature.''' ) parser.add_argument( '''--alpha_ce''' , default=0.5 , type=snake_case , help='''Linear weight for the distillation loss. Must be >=0.''' ) parser.add_argument( '''--alpha_mlm''' , default=0.0 , type=snake_case , help='''Linear weight for the MLM loss. Must be >=0. Should be used in conjunction with `mlm` flag.''' , ) parser.add_argument('''--alpha_clm''' , default=0.5 , type=snake_case , help='''Linear weight for the CLM loss. Must be >=0.''' ) parser.add_argument('''--alpha_mse''' , default=0.0 , type=snake_case , help='''Linear weight of the MSE loss. Must be >=0.''' ) parser.add_argument( '''--alpha_cos''' , default=0.0 , type=snake_case , help='''Linear weight of the cosine embedding loss. Must be >=0.''' ) parser.add_argument( '''--mlm''' , action='''store_true''' , help='''The LM step: MLM or CLM. If `mlm` is True, the MLM is used over CLM.''' ) parser.add_argument( '''--mlm_mask_prop''' , default=0.15 , type=snake_case , help='''Proportion of tokens for which we need to make a prediction.''' , ) parser.add_argument('''--word_mask''' , default=0.8 , type=snake_case , help='''Proportion of tokens to mask out.''' ) parser.add_argument('''--word_keep''' , default=0.1 , type=snake_case , help='''Proportion of tokens to keep.''' ) parser.add_argument('''--word_rand''' , default=0.1 , type=snake_case , help='''Proportion of tokens to randomly replace.''' ) parser.add_argument( '''--mlm_smoothing''' , default=0.7 , type=snake_case , help='''Smoothing parameter to emphasize more rare tokens (see XLM, similar to word2vec).''' , ) parser.add_argument('''--token_counts''' , type=snake_case , help='''The token counts in the data_file for MLM.''' ) parser.add_argument( '''--restrict_ce_to_mask''' , action='''store_true''' , help='''If true, compute the distillation loss only the [MLM] prediction distribution.''' , ) parser.add_argument( '''--freeze_pos_embs''' , action='''store_true''' , help='''Freeze positional embeddings during distillation. For student_type in [\'roberta\', \'gpt2\'] only.''' , ) parser.add_argument( '''--freeze_token_type_embds''' , action='''store_true''' , help='''Freeze token type embeddings during distillation if existent. For student_type in [\'roberta\'] only.''' , ) parser.add_argument('''--n_epoch''' , type=snake_case , default=3 , help='''Number of pass on the whole dataset.''' ) parser.add_argument('''--batch_size''' , type=snake_case , default=5 , help='''Batch size (for each process).''' ) parser.add_argument( '''--group_by_size''' , action='''store_false''' , help='''If true, group sequences that have similar length into the same batch. Default is true.''' , ) parser.add_argument( '''--gradient_accumulation_steps''' , type=snake_case , default=50 , help='''Gradient accumulation for larger training batches.''' , ) parser.add_argument('''--warmup_prop''' , default=0.05 , type=snake_case , help='''Linear warmup proportion.''' ) parser.add_argument('''--weight_decay''' , default=0.0 , type=snake_case , help='''Weight decay if we apply some.''' ) parser.add_argument('''--learning_rate''' , default=5E-4 , type=snake_case , help='''The initial learning rate for Adam.''' ) parser.add_argument('''--adam_epsilon''' , default=1E-6 , type=snake_case , help='''Epsilon for Adam optimizer.''' ) parser.add_argument('''--max_grad_norm''' , default=5.0 , type=snake_case , help='''Max gradient norm.''' ) parser.add_argument('''--initializer_range''' , default=0.02 , type=snake_case , help='''Random initialization range.''' ) parser.add_argument( '''--fp16''' , action='''store_true''' , help='''Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit''' , ) parser.add_argument( '''--fp16_opt_level''' , type=snake_case , default='''O1''' , help=( '''For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\'].''' '''See details at https://nvidia.github.io/apex/amp.html''' ) , ) parser.add_argument('''--n_gpu''' , type=snake_case , default=1 , help='''Number of GPUs in the node.''' ) parser.add_argument('''--local_rank''' , type=snake_case , default=-1 , help='''Distributed training - Local rank''' ) parser.add_argument('''--seed''' , type=snake_case , default=56 , help='''Random seed''' ) parser.add_argument('''--log_interval''' , type=snake_case , default=500 , help='''Tensorboard logging interval.''' ) parser.add_argument('''--checkpoint_interval''' , type=snake_case , default=4_000 , help='''Checkpoint interval.''' ) __SCREAMING_SNAKE_CASE : Optional[int] = parser.parse_args() sanity_checks(snake_case ) # ARGS # init_gpu_params(snake_case ) set_seed(snake_case ) if args.is_master: if os.path.exists(args.dump_path ): if not args.force: raise ValueError( F'''Serialization dir {args.dump_path} already exists, but you have not precised wheter to overwrite''' ''' itUse `--force` if you want to overwrite it''' ) else: shutil.rmtree(args.dump_path ) if not os.path.exists(args.dump_path ): os.makedirs(args.dump_path ) logger.info(F'''Experiment will be dumped and logged in {args.dump_path}''' ) # SAVE PARAMS # logger.info(F'''Param: {args}''' ) with open(os.path.join(args.dump_path , '''parameters.json''' ) , '''w''' ) as f: json.dump(vars(snake_case ) , snake_case , indent=4 ) git_log(args.dump_path ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = MODEL_CLASSES[args.student_type] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = MODEL_CLASSES[args.teacher_type] # TOKENIZER # __SCREAMING_SNAKE_CASE : Optional[int] = teacher_tokenizer_class.from_pretrained(args.teacher_name ) __SCREAMING_SNAKE_CASE : Optional[Any] = {} for tok_name, tok_symbol in tokenizer.special_tokens_map.items(): __SCREAMING_SNAKE_CASE : Any = tokenizer.all_special_tokens.index(snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.all_special_ids[idx] logger.info(F'''Special tokens {special_tok_ids}''' ) __SCREAMING_SNAKE_CASE : Any = special_tok_ids __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.max_model_input_sizes[args.teacher_name] # DATA LOADER # logger.info(F'''Loading data from {args.data_file}''' ) with open(args.data_file , '''rb''' ) as fp: __SCREAMING_SNAKE_CASE : List[str] = pickle.load(snake_case ) if args.mlm: logger.info(F'''Loading token counts from {args.token_counts} (already pre-computed)''' ) with open(args.token_counts , '''rb''' ) as fp: __SCREAMING_SNAKE_CASE : Optional[Any] = pickle.load(snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = np.maximum(snake_case , 1 ) ** -args.mlm_smoothing for idx in special_tok_ids.values(): __SCREAMING_SNAKE_CASE : Any = 0.0 # do not predict special tokens __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.from_numpy(snake_case ) else: __SCREAMING_SNAKE_CASE : Optional[int] = None __SCREAMING_SNAKE_CASE : Optional[Any] = LmSeqsDataset(params=snake_case , data=snake_case ) logger.info('''Data loader created.''' ) # STUDENT # logger.info(F'''Loading student config from {args.student_config}''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = student_config_class.from_pretrained(args.student_config ) __SCREAMING_SNAKE_CASE : Dict = True if args.student_pretrained_weights is not None: logger.info(F'''Loading pretrained weights from {args.student_pretrained_weights}''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = student_model_class.from_pretrained(args.student_pretrained_weights , config=snake_case ) else: __SCREAMING_SNAKE_CASE : str = student_model_class(snake_case ) if args.n_gpu > 0: student.to(F'''cuda:{args.local_rank}''' ) logger.info('''Student loaded.''' ) # TEACHER # __SCREAMING_SNAKE_CASE : List[str] = teacher_model_class.from_pretrained(args.teacher_name , output_hidden_states=snake_case ) if args.n_gpu > 0: teacher.to(F'''cuda:{args.local_rank}''' ) logger.info(F'''Teacher loaded from {args.teacher_name}.''' ) # FREEZING # if args.freeze_pos_embs: freeze_pos_embeddings(snake_case , snake_case ) if args.freeze_token_type_embds: freeze_token_type_embeddings(snake_case , snake_case ) # SANITY CHECKS # assert student.config.vocab_size == teacher.config.vocab_size assert student.config.hidden_size == teacher.config.hidden_size assert student.config.max_position_embeddings == teacher.config.max_position_embeddings if args.mlm: assert token_probs.size(0 ) == stu_architecture_config.vocab_size # DISTILLER # torch.cuda.empty_cache() __SCREAMING_SNAKE_CASE : int = Distiller( params=snake_case , dataset=snake_case , token_probs=snake_case , student=snake_case , teacher=snake_case ) distiller.train() logger.info('''Let\'s go get some drinks.''' ) if __name__ == "__main__": main()
303
0
from typing import List, Optional, Tuple, Union import PIL import torch from torchvision import transforms from diffusers.pipeline_utils import DiffusionPipeline, ImagePipelineOutput from diffusers.schedulers import DDIMScheduler from diffusers.utils import randn_tensor _a = transforms.Compose( [ transforms.Resize((256, 256)), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ] ) def __A ( __lowerCAmelCase )-> str: """simple docstring""" if isinstance(__lowerCAmelCase , torch.Tensor ): return image elif isinstance(__lowerCAmelCase , PIL.Image.Image ): _UpperCAmelCase = [image] _UpperCAmelCase = [trans(img.convert('RGB' ) ) for img in image] _UpperCAmelCase = torch.stack(__lowerCAmelCase ) return image class __lowerCamelCase ( snake_case__): """simple docstring""" def __init__( self , UpperCAmelCase , UpperCAmelCase ): """simple docstring""" super().__init__() # make sure scheduler can always be converted to DDIM _UpperCAmelCase = DDIMScheduler.from_config(scheduler.config ) self.register_modules(unet=UpperCAmelCase , scheduler=UpperCAmelCase ) def UpperCamelCase ( self , UpperCAmelCase ): """simple docstring""" if strength < 0 or strength > 1: raise ValueError(F"""The value of strength should in [0.0, 1.0] but is {strength}""" ) def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = min(int(num_inference_steps * strength ) , UpperCAmelCase ) _UpperCAmelCase = max(num_inference_steps - init_timestep , 0 ) _UpperCAmelCase = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None ): """simple docstring""" if not isinstance(UpperCAmelCase , (torch.Tensor, PIL.Image.Image, list) ): raise ValueError( F"""`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(UpperCAmelCase )}""" ) _UpperCAmelCase = image.to(device=UpperCAmelCase , dtype=UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ) and len(UpperCAmelCase ) != batch_size: raise ValueError( F"""You have passed a list of generators of length {len(UpperCAmelCase )}, but requested an effective batch""" F""" size of {batch_size}. Make sure the batch size matches the length of the generators.""" ) _UpperCAmelCase = init_latents.shape _UpperCAmelCase = randn_tensor(UpperCAmelCase , generator=UpperCAmelCase , device=UpperCAmelCase , dtype=UpperCAmelCase ) # get latents print('add noise to latents at timestep' , UpperCAmelCase ) _UpperCAmelCase = self.scheduler.add_noise(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) _UpperCAmelCase = init_latents return latents @torch.no_grad() def __call__( self , UpperCAmelCase = None , UpperCAmelCase = 0.8 , UpperCAmelCase = 1 , UpperCAmelCase = None , UpperCAmelCase = 0.0 , UpperCAmelCase = 50 , UpperCAmelCase = None , UpperCAmelCase = "pil" , UpperCAmelCase = True , ): """simple docstring""" self.check_inputs(UpperCAmelCase ) # 2. Preprocess image _UpperCAmelCase = preprocess(UpperCAmelCase ) # 3. set timesteps self.scheduler.set_timesteps(UpperCAmelCase , device=self.device ) _UpperCAmelCase , _UpperCAmelCase = self.get_timesteps(UpperCAmelCase , UpperCAmelCase , self.device ) _UpperCAmelCase = timesteps[:1].repeat(UpperCAmelCase ) # 4. Prepare latent variables _UpperCAmelCase = self.prepare_latents(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , self.unet.dtype , self.device , UpperCAmelCase ) _UpperCAmelCase = latents # 5. Denoising loop for t in self.progress_bar(UpperCAmelCase ): # 1. predict noise model_output _UpperCAmelCase = self.unet(UpperCAmelCase , UpperCAmelCase ).sample # 2. predict previous mean of image x_t-1 and add variance depending on eta # eta corresponds to η in paper and should be between [0, 1] # do x_t -> x_t-1 _UpperCAmelCase = self.scheduler.step( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , eta=UpperCAmelCase , use_clipped_model_output=UpperCAmelCase , generator=UpperCAmelCase , ).prev_sample _UpperCAmelCase = (image / 2 + 0.5).clamp(0 , 1 ) _UpperCAmelCase = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _UpperCAmelCase = self.numpy_to_pil(UpperCAmelCase ) if not return_dict: return (image, latent_timestep.item()) return ImagePipelineOutput(images=UpperCAmelCase )
39
import math import os import sys def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = '''''' try: with open(snake_case , '''rb''' ) as binary_file: __SCREAMING_SNAKE_CASE : int = binary_file.read() for dat in data: __SCREAMING_SNAKE_CASE : Optional[Any] = F'''{dat:08b}''' result += curr_byte return result except OSError: print('''File not accessible''' ) sys.exit() def a__ ( snake_case , snake_case , snake_case , snake_case ): """simple docstring""" lexicon.pop(snake_case ) __SCREAMING_SNAKE_CASE : List[str] = last_match_id if math.loga(snake_case ).is_integer(): for curr_key in lexicon: __SCREAMING_SNAKE_CASE : int = '''0''' + lexicon[curr_key] __SCREAMING_SNAKE_CASE : List[str] = bin(snake_case )[2:] def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = {'''0''': '''0''', '''1''': '''1'''} __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Any = '''''', '''''' __SCREAMING_SNAKE_CASE : Optional[Any] = len(snake_case ) for i in range(len(snake_case ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue __SCREAMING_SNAKE_CASE : Any = lexicon[curr_string] result += last_match_id add_key_to_lexicon(snake_case , snake_case , snake_case , snake_case ) index += 1 __SCREAMING_SNAKE_CASE : Tuple = '''''' while curr_string != "" and curr_string not in lexicon: curr_string += "0" if curr_string != "": __SCREAMING_SNAKE_CASE : Dict = lexicon[curr_string] result += last_match_id return result def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = os.path.getsize(snake_case ) __SCREAMING_SNAKE_CASE : Union[str, Any] = bin(snake_case )[2:] __SCREAMING_SNAKE_CASE : int = len(snake_case ) return "0" * (length_length - 1) + file_length_binary + compressed def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = 8 try: with open(snake_case , '''wb''' ) as opened_file: __SCREAMING_SNAKE_CASE : Optional[int] = [ to_write[i : i + byte_length] for i in range(0 , len(snake_case ) , snake_case ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append('''10000000''' ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array: opened_file.write(int(snake_case , 2 ).to_bytes(1 , byteorder='''big''' ) ) except OSError: print('''File not accessible''' ) sys.exit() def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = read_file_binary(snake_case ) __SCREAMING_SNAKE_CASE : Optional[int] = compress_data(snake_case ) __SCREAMING_SNAKE_CASE : Dict = add_file_length(snake_case , snake_case ) write_file_binary(snake_case , snake_case ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
303
0
"""simple docstring""" def lowercase ( A_ , A_ )-> int: '''simple docstring''' return int((input_a, input_a).count(0 ) == 0 ) def lowercase ( )-> None: '''simple docstring''' assert and_gate(0 , 0 ) == 0 assert and_gate(0 , 1 ) == 0 assert and_gate(1 , 0 ) == 0 assert and_gate(1 , 1 ) == 1 if __name__ == "__main__": test_and_gate() print(and_gate(1, 0)) print(and_gate(0, 0)) print(and_gate(0, 1)) print(and_gate(1, 1))
40
import unittest import numpy as np import torch from torch import nn from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import enable_full_determinism, skip_mps from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __UpperCamelCase ( lowerCAmelCase__ , unittest.TestCase ): """simple docstring""" lowerCAmelCase_ = KandinskyVaaPriorPipeline lowerCAmelCase_ = ['''prompt'''] lowerCAmelCase_ = ['''prompt''', '''negative_prompt'''] lowerCAmelCase_ = [ '''num_images_per_prompt''', '''generator''', '''num_inference_steps''', '''latents''', '''negative_prompt''', '''guidance_scale''', '''output_type''', '''return_dict''', ] lowerCAmelCase_ = False @property def UpperCAmelCase__ ( self : int ): """simple docstring""" return 32 @property def UpperCAmelCase__ ( self : List[str] ): """simple docstring""" return 32 @property def UpperCAmelCase__ ( self : Dict ): """simple docstring""" return self.time_input_dim @property def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" return self.time_input_dim * 4 @property def UpperCAmelCase__ ( self : List[Any] ): """simple docstring""" return 100 @property def UpperCAmelCase__ ( self : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) return tokenizer @property def UpperCAmelCase__ ( self : List[Any] ): """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : List[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(_A ) @property def UpperCAmelCase__ ( self : str ): """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : Dict = { '''num_attention_heads''': 2, '''attention_head_dim''': 12, '''embedding_dim''': self.text_embedder_hidden_size, '''num_layers''': 1, } __SCREAMING_SNAKE_CASE : Optional[Any] = PriorTransformer(**_A ) # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0 __SCREAMING_SNAKE_CASE : List[Any] = nn.Parameter(torch.ones(model.clip_std.shape ) ) return model @property def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : int = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , image_size=224 , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_channels=3 , num_hidden_layers=5 , patch_size=14 , ) __SCREAMING_SNAKE_CASE : str = CLIPVisionModelWithProjection(_A ) return model @property def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = CLIPImageProcessor( crop_size=224 , do_center_crop=_A , do_normalize=_A , do_resize=_A , image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73] , image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11] , resample=3 , size=224 , ) return image_processor def UpperCAmelCase__ ( self : Tuple ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = self.dummy_prior __SCREAMING_SNAKE_CASE : str = self.dummy_image_encoder __SCREAMING_SNAKE_CASE : Optional[int] = self.dummy_text_encoder __SCREAMING_SNAKE_CASE : List[Any] = self.dummy_tokenizer __SCREAMING_SNAKE_CASE : Union[str, Any] = self.dummy_image_processor __SCREAMING_SNAKE_CASE : str = UnCLIPScheduler( variance_type='''fixed_small_log''' , prediction_type='''sample''' , num_train_timesteps=1000 , clip_sample=_A , clip_sample_range=10.0 , ) __SCREAMING_SNAKE_CASE : int = { '''prior''': prior, '''image_encoder''': image_encoder, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''scheduler''': scheduler, '''image_processor''': image_processor, } return components def UpperCAmelCase__ ( self : Union[str, Any] , _A : int , _A : Dict=0 ): """simple docstring""" if str(_A ).startswith('''mps''' ): __SCREAMING_SNAKE_CASE : Tuple = torch.manual_seed(_A ) else: __SCREAMING_SNAKE_CASE : str = torch.Generator(device=_A ).manual_seed(_A ) __SCREAMING_SNAKE_CASE : List[str] = { '''prompt''': '''horse''', '''generator''': generator, '''guidance_scale''': 4.0, '''num_inference_steps''': 2, '''output_type''': '''np''', } return inputs def UpperCAmelCase__ ( self : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = '''cpu''' __SCREAMING_SNAKE_CASE : Any = self.get_dummy_components() __SCREAMING_SNAKE_CASE : Any = self.pipeline_class(**_A ) __SCREAMING_SNAKE_CASE : List[Any] = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) __SCREAMING_SNAKE_CASE : int = pipe(**self.get_dummy_inputs(_A ) ) __SCREAMING_SNAKE_CASE : Tuple = output.image_embeds __SCREAMING_SNAKE_CASE : Optional[Any] = pipe( **self.get_dummy_inputs(_A ) , return_dict=_A , )[0] __SCREAMING_SNAKE_CASE : Tuple = image[0, -10:] __SCREAMING_SNAKE_CASE : List[Any] = image_from_tuple[0, -10:] assert image.shape == (1, 32) __SCREAMING_SNAKE_CASE : List[str] = np.array( [-0.05_32, 1.71_20, 0.36_56, -1.08_52, -0.89_46, -1.17_56, 0.43_48, 0.24_82, 0.51_46, -0.11_56] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @skip_mps def UpperCAmelCase__ ( self : List[str] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = torch_device == '''cpu''' __SCREAMING_SNAKE_CASE : Any = True __SCREAMING_SNAKE_CASE : int = False self._test_inference_batch_single_identical( test_max_difference=_A , relax_max_difference=_A , test_mean_pixel_difference=_A , ) @skip_mps def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = torch_device == '''cpu''' __SCREAMING_SNAKE_CASE : List[Any] = False self._test_attention_slicing_forward_pass( test_max_difference=_A , test_mean_pixel_difference=_A , )
303
0
'''simple docstring''' import argparse import json import re from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileNetVaConfig, MobileNetVaForImageClassification, MobileNetVaImageProcessor, load_tf_weights_in_mobilenet_va, ) from transformers.utils import logging logging.set_verbosity_info() _A : Dict =logging.get_logger(__name__) def SCREAMING_SNAKE_CASE_ (UpperCamelCase ) -> int: lowerCamelCase__ : Union[str, Any] = MobileNetVaConfig(layer_norm_eps=0.001 ) if "_quant" in model_name: raise ValueError("""Quantized models are not supported.""" ) lowerCamelCase__ : Optional[int] = re.match(r"""^mobilenet_v1_([^_]*)_([^_]*)$""" , UpperCamelCase ) if matches: lowerCamelCase__ : Optional[int] = float(matches[1] ) lowerCamelCase__ : Optional[int] = int(matches[2] ) # The TensorFlow version of MobileNetV1 predicts 1001 classes instead of # the usual 1000. The first class (index 0) is "background". lowerCamelCase__ : List[Any] = 1001 lowerCamelCase__ : Any = """imagenet-1k-id2label.json""" lowerCamelCase__ : Union[str, Any] = """huggingface/label-files""" lowerCamelCase__ : List[Any] = json.load(open(hf_hub_download(UpperCamelCase , UpperCamelCase , repo_type="""dataset""" ) , """r""" ) ) lowerCamelCase__ : str = {int(UpperCamelCase ) + 1: v for k, v in idalabel.items()} lowerCamelCase__ : Dict = """background""" lowerCamelCase__ : Tuple = idalabel lowerCamelCase__ : Dict = {v: k for k, v in idalabel.items()} return config def SCREAMING_SNAKE_CASE_ () -> Union[str, Any]: lowerCamelCase__ : int = """http://images.cocodataset.org/val2017/000000039769.jpg""" lowerCamelCase__ : List[str] = Image.open(requests.get(UpperCamelCase , stream=UpperCamelCase ).raw ) return im @torch.no_grad() def SCREAMING_SNAKE_CASE_ (UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase=False ) -> Tuple: lowerCamelCase__ : Union[str, Any] = get_mobilenet_va_config(UpperCamelCase ) # Load 🤗 model lowerCamelCase__ : Optional[Any] = MobileNetVaForImageClassification(UpperCamelCase ).eval() # Load weights from TensorFlow checkpoint load_tf_weights_in_mobilenet_va(UpperCamelCase , UpperCamelCase , UpperCamelCase ) # Check outputs on an image, prepared by MobileNetV1ImageProcessor lowerCamelCase__ : Optional[Any] = MobileNetVaImageProcessor( crop_size={"""width""": config.image_size, """height""": config.image_size} , size={"""shortest_edge""": config.image_size + 32} , ) lowerCamelCase__ : Any = image_processor(images=prepare_img() , return_tensors="""pt""" ) lowerCamelCase__ : str = model(**UpperCamelCase ) lowerCamelCase__ : Dict = outputs.logits assert logits.shape == (1, 1001) if model_name == "mobilenet_v1_1.0_224": lowerCamelCase__ : Optional[Any] = torch.tensor([-4.1739, -1.1233, 3.1205] ) elif model_name == "mobilenet_v1_0.75_192": lowerCamelCase__ : Union[str, Any] = torch.tensor([-3.9440, -2.3141, -0.3333] ) else: lowerCamelCase__ : Tuple = None if expected_logits is not None: assert torch.allclose(logits[0, :3] , UpperCamelCase , atol=1E-4 ) Path(UpperCamelCase ).mkdir(exist_ok=UpperCamelCase ) print(f'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(UpperCamelCase ) if push_to_hub: print("""Pushing to the hub...""" ) lowerCamelCase__ : Optional[Any] = """google/""" + model_name image_processor.push_to_hub(UpperCamelCase ) model.push_to_hub(UpperCamelCase ) if __name__ == "__main__": _A : Any =argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''mobilenet_v1_1.0_224''', type=str, help='''Name of the MobileNetV1 model you\'d like to convert. Should in the form \'mobilenet_v1_<depth>_<size>\'.''', ) parser.add_argument( '''--checkpoint_path''', required=True, type=str, help='''Path to the original TensorFlow checkpoint (.ckpt file).''' ) parser.add_argument( '''--pytorch_dump_folder_path''', required=True, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) _A : Optional[Any] =parser.parse_args() convert_movilevit_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
41
import argparse import logging import pickle import random import time import numpy as np from transformers import BertTokenizer, GPTaTokenizer, RobertaTokenizer logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""", datefmt="""%m/%d/%Y %H:%M:%S""", level=logging.INFO ) lowercase_ = logging.getLogger(__name__) def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = argparse.ArgumentParser( description='''Preprocess the data to avoid re-doing it several times by (tokenization + token_to_ids).''' ) parser.add_argument('''--file_path''' , type=snake_case , default='''data/dump.txt''' , help='''The path to the data.''' ) parser.add_argument('''--tokenizer_type''' , type=snake_case , default='''bert''' , choices=['''bert''', '''roberta''', '''gpt2'''] ) parser.add_argument('''--tokenizer_name''' , type=snake_case , default='''bert-base-uncased''' , help='''The tokenizer to use.''' ) parser.add_argument('''--dump_file''' , type=snake_case , default='''data/dump''' , help='''The dump file prefix.''' ) __SCREAMING_SNAKE_CASE : List[Any] = parser.parse_args() logger.info(F'''Loading Tokenizer ({args.tokenizer_name})''' ) if args.tokenizer_type == "bert": __SCREAMING_SNAKE_CASE : Union[str, Any] = BertTokenizer.from_pretrained(args.tokenizer_name ) __SCREAMING_SNAKE_CASE : List[str] = tokenizer.special_tokens_map['''cls_token'''] # `[CLS]` __SCREAMING_SNAKE_CASE : List[str] = tokenizer.special_tokens_map['''sep_token'''] # `[SEP]` elif args.tokenizer_type == "roberta": __SCREAMING_SNAKE_CASE : List[str] = RobertaTokenizer.from_pretrained(args.tokenizer_name ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.special_tokens_map['''cls_token'''] # `<s>` __SCREAMING_SNAKE_CASE : Optional[int] = tokenizer.special_tokens_map['''sep_token'''] # `</s>` elif args.tokenizer_type == "gpt2": __SCREAMING_SNAKE_CASE : Dict = GPTaTokenizer.from_pretrained(args.tokenizer_name ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.special_tokens_map['''bos_token'''] # `<|endoftext|>` __SCREAMING_SNAKE_CASE : str = tokenizer.special_tokens_map['''eos_token'''] # `<|endoftext|>` logger.info(F'''Loading text from {args.file_path}''' ) with open(args.file_path , '''r''' , encoding='''utf8''' ) as fp: __SCREAMING_SNAKE_CASE : str = fp.readlines() logger.info('''Start encoding''' ) logger.info(F'''{len(snake_case )} examples to process.''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = [] __SCREAMING_SNAKE_CASE : Dict = 0 __SCREAMING_SNAKE_CASE : List[str] = 10_000 __SCREAMING_SNAKE_CASE : Dict = time.time() for text in data: __SCREAMING_SNAKE_CASE : Optional[int] = F'''{bos} {text.strip()} {sep}''' __SCREAMING_SNAKE_CASE : Tuple = tokenizer.encode(snake_case , add_special_tokens=snake_case ) rslt.append(snake_case ) iter += 1 if iter % interval == 0: __SCREAMING_SNAKE_CASE : List[str] = time.time() logger.info(F'''{iter} examples processed. - {(end-start):.2f}s/{interval}expl''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = time.time() logger.info('''Finished binarization''' ) logger.info(F'''{len(snake_case )} examples processed.''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = F'''{args.dump_file}.{args.tokenizer_name}.pickle''' __SCREAMING_SNAKE_CASE : str = tokenizer.vocab_size if vocab_size < (1 << 16): __SCREAMING_SNAKE_CASE : List[str] = [np.uintaa(snake_case ) for d in rslt] else: __SCREAMING_SNAKE_CASE : Optional[int] = [np.intaa(snake_case ) for d in rslt] random.shuffle(rslt_ ) logger.info(F'''Dump to {dp_file}''' ) with open(snake_case , '''wb''' ) as handle: pickle.dump(rslt_ , snake_case , protocol=pickle.HIGHEST_PROTOCOL ) if __name__ == "__main__": main()
303
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase : Dict = logging.get_logger(__name__) lowercase : Union[str, Any] = { "google/vivit-b-16x2-kinetics400": ( "https://huggingface.co/google/vivit-b-16x2-kinetics400/resolve/main/config.json" ), # See all Vivit models at https://huggingface.co/models?filter=vivit } class __UpperCAmelCase ( _lowerCamelCase ): __lowercase = """vivit""" def __init__( self , lowerCAmelCase_=2_24 , lowerCAmelCase_=32 , lowerCAmelCase_=[2, 16, 16] , lowerCAmelCase_=3 , lowerCAmelCase_=7_68 , lowerCAmelCase_=12 , lowerCAmelCase_=12 , lowerCAmelCase_=30_72 , lowerCAmelCase_="gelu_fast" , lowerCAmelCase_=0.0 , lowerCAmelCase_=0.0 , lowerCAmelCase_=0.02 , lowerCAmelCase_=1E-06 , lowerCAmelCase_=True , **lowerCAmelCase_ , ): """simple docstring""" _snake_case = hidden_size _snake_case = num_hidden_layers _snake_case = num_attention_heads _snake_case = intermediate_size _snake_case = hidden_act _snake_case = hidden_dropout_prob _snake_case = attention_probs_dropout_prob _snake_case = initializer_range _snake_case = layer_norm_eps _snake_case = image_size _snake_case = num_frames _snake_case = tubelet_size _snake_case = num_channels _snake_case = qkv_bias super().__init__(**lowerCAmelCase_ )
42
import numpy as np import torch from imwatermark import WatermarkEncoder # Copied from https://github.com/Stability-AI/generative-models/blob/613af104c6b85184091d42d374fef420eddb356d/scripts/demo/streamlit_helpers.py#L66 lowercase_ = 0b1011_0011_1110_1100_1001_0000_0111_1011_1011_0001_1001_1110 # bin(x)[2:] gives bits of x as str, use int to convert them to 0/1 lowercase_ = [int(bit) for bit in bin(WATERMARK_MESSAGE)[2:]] class __UpperCamelCase : """simple docstring""" def __init__( self : Dict ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = WATERMARK_BITS __SCREAMING_SNAKE_CASE : Optional[int] = WatermarkEncoder() self.encoder.set_watermark('''bits''' , self.watermark ) def UpperCAmelCase__ ( self : List[Any] , _A : torch.FloatTensor ): """simple docstring""" if images.shape[-1] < 256: return images __SCREAMING_SNAKE_CASE : Union[str, Any] = (255 * (images / 2 + 0.5)).cpu().permute(0 , 2 , 3 , 1 ).float().numpy() __SCREAMING_SNAKE_CASE : Dict = [self.encoder.encode(_A , '''dwtDct''' ) for image in images] __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.from_numpy(np.array(_A ) ).permute(0 , 3 , 1 , 2 ) __SCREAMING_SNAKE_CASE : Optional[Any] = torch.clamp(2 * (images / 255 - 0.5) , min=-1.0 , max=1.0 ) return images
303
0
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __lowercase = logging.get_logger(__name__) __lowercase = {'''tokenizer_file''': '''tokenizer.json'''} __lowercase = { '''tokenizer_file''': { '''bigscience/tokenizer''': '''https://huggingface.co/bigscience/tokenizer/blob/main/tokenizer.json''', '''bigscience/bloom-560m''': '''https://huggingface.co/bigscience/bloom-560m/blob/main/tokenizer.json''', '''bigscience/bloom-1b1''': '''https://huggingface.co/bigscience/bloom-1b1/blob/main/tokenizer.json''', '''bigscience/bloom-1b7''': '''https://huggingface.co/bigscience/bloom-1b7/blob/main/tokenizer.json''', '''bigscience/bloom-3b''': '''https://huggingface.co/bigscience/bloom-3b/blob/main/tokenizer.json''', '''bigscience/bloom-7b1''': '''https://huggingface.co/bigscience/bloom-7b1/blob/main/tokenizer.json''', '''bigscience/bloom''': '''https://huggingface.co/bigscience/bloom/blob/main/tokenizer.json''', }, } class lowerCamelCase_ ( UpperCAmelCase_ ): '''simple docstring''' a__ : int = VOCAB_FILES_NAMES a__ : Tuple = PRETRAINED_VOCAB_FILES_MAP a__ : List[str] = ["""input_ids""", """attention_mask"""] a__ : int = None def __init__( self , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase="<unk>" , __lowercase="<s>" , __lowercase="</s>" , __lowercase="<pad>" , __lowercase=False , __lowercase=False , **__lowercase , ) -> List[str]: super().__init__( __lowercase , __lowercase , tokenizer_file=__lowercase , unk_token=__lowercase , bos_token=__lowercase , eos_token=__lowercase , pad_token=__lowercase , add_prefix_space=__lowercase , clean_up_tokenization_spaces=__lowercase , **__lowercase , ) __UpperCamelCase :int = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()) if pre_tok_state.get('''add_prefix_space''' , __lowercase) != add_prefix_space: __UpperCamelCase :Any = getattr(__lowercase , pre_tok_state.pop('''type''')) __UpperCamelCase :str = add_prefix_space __UpperCamelCase :List[str] = pre_tok_class(**__lowercase) __UpperCamelCase :Tuple = add_prefix_space def UpperCamelCase__ ( self , *__lowercase , **__lowercase) -> BatchEncoding: __UpperCamelCase :Tuple = kwargs.get('''is_split_into_words''' , __lowercase) if not (self.add_prefix_space or not is_split_into_words): raise Exception( f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with""" ''' pretokenized inputs.''') return super()._batch_encode_plus(*__lowercase , **__lowercase) def UpperCamelCase__ ( self , *__lowercase , **__lowercase) -> BatchEncoding: __UpperCamelCase :List[str] = kwargs.get('''is_split_into_words''' , __lowercase) if not (self.add_prefix_space or not is_split_into_words): raise Exception( f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with""" ''' pretokenized inputs.''') return super()._encode_plus(*__lowercase , **__lowercase) def UpperCamelCase__ ( self , __lowercase , __lowercase = None) -> Tuple[str]: __UpperCamelCase :Optional[Any] = self._tokenizer.model.save(__lowercase , name=__lowercase) return tuple(__lowercase) def UpperCamelCase__ ( self , __lowercase) -> List[int]: __UpperCamelCase :str = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(__lowercase , add_special_tokens=__lowercase) + [self.eos_token_id]) if len(__lowercase) > self.model_max_length: __UpperCamelCase :Any = input_ids[-self.model_max_length :] return input_ids
43
from heapq import heappop, heappush import numpy as np def a__ ( snake_case , snake_case , snake_case , snake_case , ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : int = grid.shape __SCREAMING_SNAKE_CASE : Tuple = [-1, 1, 0, 0] __SCREAMING_SNAKE_CASE : List[str] = [0, 0, -1, 1] if allow_diagonal: dx += [-1, -1, 1, 1] dy += [-1, 1, -1, 1] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Union[str, Any] = [(0, source)], set() __SCREAMING_SNAKE_CASE : Union[str, Any] = np.full((rows, cols) , np.inf ) __SCREAMING_SNAKE_CASE : Union[str, Any] = 0 __SCREAMING_SNAKE_CASE : Union[str, Any] = np.empty((rows, cols) , dtype=snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = None while queue: ((__SCREAMING_SNAKE_CASE), (__SCREAMING_SNAKE_CASE)) : Any = heappop(snake_case ) if (x, y) in visited: continue visited.add((x, y) ) if (x, y) == destination: __SCREAMING_SNAKE_CASE : int = [] while (x, y) != source: path.append((x, y) ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[Any] = predecessors[x, y] path.append(snake_case ) # add the source manually path.reverse() return matrix[destination], path for i in range(len(snake_case ) ): __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = x + dx[i], y + dy[i] if 0 <= nx < rows and 0 <= ny < cols: __SCREAMING_SNAKE_CASE : Optional[int] = grid[nx][ny] if next_node == 1 and matrix[nx, ny] > dist + 1: heappush(snake_case , (dist + 1, (nx, ny)) ) __SCREAMING_SNAKE_CASE : int = dist + 1 __SCREAMING_SNAKE_CASE : Dict = (x, y) return np.inf, [] if __name__ == "__main__": import doctest doctest.testmod()
303
0
"""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 SCREAMING_SNAKE_CASE ( ) -> Tuple: _lowerCAmelCase : List[Any] = 10 _lowerCAmelCase : List[str] = 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 : Optional[Any] = datasets.Dataset.from_dict( { """tokens""": [["""foo"""] * 5] * n, """labels""": [[1] * 5] * n, """answers""": [{"""answer_start""": [97], """text""": ["""1976"""]}] * 10, """id""": list(range(_lowerCamelCase ) ), } ,features=_lowerCamelCase ,) return dataset @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Dict ,_lowerCamelCase : int ) -> List[Any]: _lowerCAmelCase : Tuple = str(tmp_path_factory.mktemp("""data""" ) / """file.arrow""" ) dataset.map(cache_file_name=_lowerCamelCase ) return filename # FILE_CONTENT + files _a : List[str] = '\\n Text data.\n Second line of data.' @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[str] ) -> Union[str, Any]: _lowerCAmelCase : Optional[Any] = tmp_path_factory.mktemp("""data""" ) / """file.txt""" _lowerCAmelCase : Union[str, Any] = FILE_CONTENT with open(_lowerCamelCase ,"""w""" ) as f: f.write(_lowerCamelCase ) return filename @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> Any: import bza _lowerCAmelCase : Optional[int] = tmp_path_factory.mktemp("""data""" ) / """file.txt.bz2""" _lowerCAmelCase : Optional[Any] = bytes(_lowerCamelCase ,"""utf-8""" ) with bza.open(_lowerCamelCase ,"""wb""" ) as f: f.write(_lowerCamelCase ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple ) -> Optional[Any]: import gzip _lowerCAmelCase : Dict = str(tmp_path_factory.mktemp("""data""" ) / """file.txt.gz""" ) _lowerCAmelCase : Dict = bytes(_lowerCamelCase ,"""utf-8""" ) with gzip.open(_lowerCamelCase ,"""wb""" ) as f: f.write(_lowerCamelCase ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Dict ) -> int: if datasets.config.LZ4_AVAILABLE: import lza.frame _lowerCAmelCase : List[str] = tmp_path_factory.mktemp("""data""" ) / """file.txt.lz4""" _lowerCAmelCase : str = bytes(_lowerCamelCase ,"""utf-8""" ) with lza.frame.open(_lowerCamelCase ,"""wb""" ) as f: f.write(_lowerCamelCase ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[Any] ,_lowerCamelCase : List[str] ) -> int: if datasets.config.PY7ZR_AVAILABLE: import pyazr _lowerCAmelCase : Dict = tmp_path_factory.mktemp("""data""" ) / """file.txt.7z""" with pyazr.SevenZipFile(_lowerCamelCase ,"""w""" ) as archive: archive.write(_lowerCamelCase ,arcname=os.path.basename(_lowerCamelCase ) ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ,_lowerCamelCase : Dict ) -> int: import tarfile _lowerCAmelCase : List[str] = tmp_path_factory.mktemp("""data""" ) / """file.txt.tar""" with tarfile.TarFile(_lowerCamelCase ,"""w""" ) as f: f.add(_lowerCamelCase ,arcname=os.path.basename(_lowerCamelCase ) ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[str] ) -> Tuple: import lzma _lowerCAmelCase : Union[str, Any] = tmp_path_factory.mktemp("""data""" ) / """file.txt.xz""" _lowerCAmelCase : Optional[int] = bytes(_lowerCamelCase ,"""utf-8""" ) with lzma.open(_lowerCamelCase ,"""wb""" ) as f: f.write(_lowerCamelCase ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[str] ,_lowerCamelCase : str ) -> Tuple: import zipfile _lowerCAmelCase : List[str] = tmp_path_factory.mktemp("""data""" ) / """file.txt.zip""" with zipfile.ZipFile(_lowerCamelCase ,"""w""" ) as f: f.write(_lowerCamelCase ,arcname=os.path.basename(_lowerCamelCase ) ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> List[str]: if datasets.config.ZSTANDARD_AVAILABLE: import zstandard as zstd _lowerCAmelCase : List[Any] = tmp_path_factory.mktemp("""data""" ) / """file.txt.zst""" _lowerCAmelCase : Tuple = bytes(_lowerCamelCase ,"""utf-8""" ) with zstd.open(_lowerCamelCase ,"""wb""" ) as f: f.write(_lowerCamelCase ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> Any: _lowerCAmelCase : Any = tmp_path_factory.mktemp("""data""" ) / """file.xml""" _lowerCAmelCase : Any = 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(_lowerCamelCase ,"""w""" ) as f: f.write(_lowerCamelCase ) return filename _a : Union[str, Any] = [ {'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}, ] _a : Any = [ {'col_1': '4', 'col_2': 4, 'col_3': 4.0}, {'col_1': '5', 'col_2': 5, 'col_3': 5.0}, ] _a : List[Any] = { 'col_1': ['0', '1', '2', '3'], 'col_2': [0, 1, 2, 3], 'col_3': [0.0, 1.0, 2.0, 3.0], } _a : Any = [ {'col_3': 0.0, 'col_1': '0', 'col_2': 0}, {'col_3': 1.0, 'col_1': '1', 'col_2': 1}, ] _a : Optional[Any] = [ {'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 SCREAMING_SNAKE_CASE ( ) -> int: return DATA_DICT_OF_LISTS @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Dict ) -> str: _lowerCAmelCase : Union[str, Any] = datasets.Dataset.from_dict(_lowerCamelCase ) _lowerCAmelCase : int = str(tmp_path_factory.mktemp("""data""" ) / """dataset.arrow""" ) dataset.map(cache_file_name=_lowerCamelCase ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[Any] ) -> Dict: _lowerCAmelCase : List[Any] = str(tmp_path_factory.mktemp("""data""" ) / """dataset.sqlite""" ) with contextlib.closing(sqlitea.connect(_lowerCamelCase ) ) as con: _lowerCAmelCase : int = 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 SCREAMING_SNAKE_CASE ( _lowerCamelCase : Any ) -> List[Any]: _lowerCAmelCase : Optional[Any] = str(tmp_path_factory.mktemp("""data""" ) / """dataset.csv""" ) with open(_lowerCamelCase ,"""w""" ,newline="""""" ) as f: _lowerCAmelCase : str = csv.DictWriter(_lowerCamelCase ,fieldnames=["""col_1""", """col_2""", """col_3"""] ) writer.writeheader() for item in DATA: writer.writerow(_lowerCamelCase ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Any ) -> List[str]: _lowerCAmelCase : str = str(tmp_path_factory.mktemp("""data""" ) / """dataset2.csv""" ) with open(_lowerCamelCase ,"""w""" ,newline="""""" ) as f: _lowerCAmelCase : Tuple = csv.DictWriter(_lowerCamelCase ,fieldnames=["""col_1""", """col_2""", """col_3"""] ) writer.writeheader() for item in DATA: writer.writerow(_lowerCamelCase ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ,_lowerCamelCase : List[Any] ) -> Optional[int]: import bza _lowerCAmelCase : Any = tmp_path_factory.mktemp("""data""" ) / """dataset.csv.bz2""" with open(_lowerCamelCase ,"""rb""" ) as f: _lowerCAmelCase : List[str] = f.read() # data = bytes(FILE_CONTENT, "utf-8") with bza.open(_lowerCamelCase ,"""wb""" ) as f: f.write(_lowerCamelCase ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple ,_lowerCamelCase : str ,_lowerCamelCase : Optional[Any] ) -> Dict: _lowerCAmelCase : Dict = tmp_path_factory.mktemp("""data""" ) / """dataset.csv.zip""" with zipfile.ZipFile(_lowerCamelCase ,"""w""" ) as f: f.write(_lowerCamelCase ,arcname=os.path.basename(_lowerCamelCase ) ) f.write(_lowerCamelCase ,arcname=os.path.basename(_lowerCamelCase ) ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[str] ,_lowerCamelCase : Tuple ,_lowerCamelCase : Tuple ) -> Tuple: _lowerCAmelCase : Any = tmp_path_factory.mktemp("""data""" ) / """dataset.csv.zip""" with zipfile.ZipFile(_lowerCamelCase ,"""w""" ) as f: f.write(_lowerCamelCase ,arcname=os.path.basename(csv_path.replace(""".csv""" ,""".CSV""" ) ) ) f.write(_lowerCamelCase ,arcname=os.path.basename(csva_path.replace(""".csv""" ,""".CSV""" ) ) ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ,_lowerCamelCase : Optional[Any] ,_lowerCamelCase : Union[str, Any] ) -> int: _lowerCAmelCase : List[Any] = tmp_path_factory.mktemp("""data""" ) / """dataset_with_dir.csv.zip""" with zipfile.ZipFile(_lowerCamelCase ,"""w""" ) as f: f.write(_lowerCamelCase ,arcname=os.path.join("""main_dir""" ,os.path.basename(_lowerCamelCase ) ) ) f.write(_lowerCamelCase ,arcname=os.path.join("""main_dir""" ,os.path.basename(_lowerCamelCase ) ) ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ) -> Union[str, Any]: _lowerCAmelCase : int = str(tmp_path_factory.mktemp("""data""" ) / """dataset.parquet""" ) _lowerCAmelCase : Any = pa.schema( { """col_1""": pa.string(), """col_2""": pa.intaa(), """col_3""": pa.floataa(), } ) with open(_lowerCamelCase ,"""wb""" ) as f: _lowerCAmelCase : List[str] = pq.ParquetWriter(_lowerCamelCase ,schema=_lowerCamelCase ) _lowerCAmelCase : Dict = pa.Table.from_pydict({k: [DATA[i][k] for i in range(len(_lowerCamelCase ) )] for k in DATA[0]} ,schema=_lowerCamelCase ) writer.write_table(_lowerCamelCase ) writer.close() return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[int] ) -> Union[str, Any]: _lowerCAmelCase : Any = str(tmp_path_factory.mktemp("""data""" ) / """dataset.json""" ) _lowerCAmelCase : List[Any] = {"""data""": DATA} with open(_lowerCamelCase ,"""w""" ) as f: json.dump(_lowerCamelCase ,_lowerCamelCase ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : str ) -> Optional[int]: _lowerCAmelCase : int = str(tmp_path_factory.mktemp("""data""" ) / """dataset.json""" ) _lowerCAmelCase : List[str] = {"""data""": DATA_DICT_OF_LISTS} with open(_lowerCamelCase ,"""w""" ) as f: json.dump(_lowerCamelCase ,_lowerCamelCase ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[int] ) -> Optional[Any]: _lowerCAmelCase : List[Any] = str(tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl""" ) with open(_lowerCamelCase ,"""w""" ) as f: for item in DATA: f.write(json.dumps(_lowerCamelCase ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Any ) -> Optional[int]: _lowerCAmelCase : List[str] = str(tmp_path_factory.mktemp("""data""" ) / """dataset2.jsonl""" ) with open(_lowerCamelCase ,"""w""" ) as f: for item in DATA: f.write(json.dumps(_lowerCamelCase ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[Any] ) -> str: _lowerCAmelCase : Optional[int] = str(tmp_path_factory.mktemp("""data""" ) / """dataset_312.jsonl""" ) with open(_lowerCamelCase ,"""w""" ) as f: for item in DATA_312: f.write(json.dumps(_lowerCamelCase ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ) -> str: _lowerCAmelCase : Optional[Any] = str(tmp_path_factory.mktemp("""data""" ) / """dataset-str.jsonl""" ) with open(_lowerCamelCase ,"""w""" ) as f: for item in DATA_STR: f.write(json.dumps(_lowerCamelCase ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple ,_lowerCamelCase : List[Any] ) -> int: import gzip _lowerCAmelCase : str = str(tmp_path_factory.mktemp("""data""" ) / """dataset.txt.gz""" ) with open(_lowerCamelCase ,"""rb""" ) as orig_file: with gzip.open(_lowerCamelCase ,"""wb""" ) as zipped_file: zipped_file.writelines(_lowerCamelCase ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : str ,_lowerCamelCase : Optional[int] ) -> Union[str, Any]: import gzip _lowerCAmelCase : str = str(tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl.gz""" ) with open(_lowerCamelCase ,"""rb""" ) as orig_file: with gzip.open(_lowerCamelCase ,"""wb""" ) as zipped_file: zipped_file.writelines(_lowerCamelCase ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[str] ,_lowerCamelCase : List[Any] ,_lowerCamelCase : Optional[int] ) -> Any: _lowerCAmelCase : Tuple = tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl.zip""" with zipfile.ZipFile(_lowerCamelCase ,"""w""" ) as f: f.write(_lowerCamelCase ,arcname=os.path.basename(_lowerCamelCase ) ) f.write(_lowerCamelCase ,arcname=os.path.basename(_lowerCamelCase ) ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : str ,_lowerCamelCase : Union[str, Any] ,_lowerCamelCase : int ,_lowerCamelCase : Union[str, Any] ) -> List[str]: _lowerCAmelCase : List[Any] = tmp_path_factory.mktemp("""data""" ) / """dataset_nested.jsonl.zip""" with zipfile.ZipFile(_lowerCamelCase ,"""w""" ) as f: f.write(_lowerCamelCase ,arcname=os.path.join("""nested""" ,os.path.basename(_lowerCamelCase ) ) ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[str] ,_lowerCamelCase : str ,_lowerCamelCase : List[Any] ) -> Union[str, Any]: _lowerCAmelCase : Dict = tmp_path_factory.mktemp("""data""" ) / """dataset_with_dir.jsonl.zip""" with zipfile.ZipFile(_lowerCamelCase ,"""w""" ) as f: f.write(_lowerCamelCase ,arcname=os.path.join("""main_dir""" ,os.path.basename(_lowerCamelCase ) ) ) f.write(_lowerCamelCase ,arcname=os.path.join("""main_dir""" ,os.path.basename(_lowerCamelCase ) ) ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Any ,_lowerCamelCase : Any ,_lowerCamelCase : List[str] ) -> Any: _lowerCAmelCase : str = tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl.tar""" with tarfile.TarFile(_lowerCamelCase ,"""w""" ) as f: f.add(_lowerCamelCase ,arcname=os.path.basename(_lowerCamelCase ) ) f.add(_lowerCamelCase ,arcname=os.path.basename(_lowerCamelCase ) ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Any ,_lowerCamelCase : int ,_lowerCamelCase : List[Any] ,_lowerCamelCase : Optional[int] ) -> str: _lowerCAmelCase : Any = tmp_path_factory.mktemp("""data""" ) / """dataset_nested.jsonl.tar""" with tarfile.TarFile(_lowerCamelCase ,"""w""" ) as f: f.add(_lowerCamelCase ,arcname=os.path.join("""nested""" ,os.path.basename(_lowerCamelCase ) ) ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[Any] ) -> Optional[Any]: _lowerCAmelCase : Any = ["""0""", """1""", """2""", """3"""] _lowerCAmelCase : Any = str(tmp_path_factory.mktemp("""data""" ) / """dataset.txt""" ) with open(_lowerCamelCase ,"""w""" ) as f: for item in data: f.write(item + """\n""" ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Any ) -> Union[str, Any]: _lowerCAmelCase : List[Any] = ["""0""", """1""", """2""", """3"""] _lowerCAmelCase : Optional[Any] = str(tmp_path_factory.mktemp("""data""" ) / """dataset2.txt""" ) with open(_lowerCamelCase ,"""w""" ) as f: for item in data: f.write(item + """\n""" ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[Any] ) -> Tuple: _lowerCAmelCase : int = ["""0""", """1""", """2""", """3"""] _lowerCAmelCase : Tuple = tmp_path_factory.mktemp("""data""" ) / """dataset.abc""" with open(_lowerCamelCase ,"""w""" ) as f: for item in data: f.write(item + """\n""" ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[int] ,_lowerCamelCase : int ,_lowerCamelCase : str ) -> Any: _lowerCAmelCase : Any = tmp_path_factory.mktemp("""data""" ) / """dataset.text.zip""" with zipfile.ZipFile(_lowerCamelCase ,"""w""" ) as f: f.write(_lowerCamelCase ,arcname=os.path.basename(_lowerCamelCase ) ) f.write(_lowerCamelCase ,arcname=os.path.basename(_lowerCamelCase ) ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : str ,_lowerCamelCase : Union[str, Any] ,_lowerCamelCase : List[Any] ) -> List[str]: _lowerCAmelCase : Any = tmp_path_factory.mktemp("""data""" ) / """dataset_with_dir.text.zip""" with zipfile.ZipFile(_lowerCamelCase ,"""w""" ) as f: f.write(_lowerCamelCase ,arcname=os.path.join("""main_dir""" ,os.path.basename(_lowerCamelCase ) ) ) f.write(_lowerCamelCase ,arcname=os.path.join("""main_dir""" ,os.path.basename(_lowerCamelCase ) ) ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ,_lowerCamelCase : int ,_lowerCamelCase : Tuple ) -> Dict: _lowerCAmelCase : Optional[int] = tmp_path_factory.mktemp("""data""" ) / """dataset.ext.zip""" with zipfile.ZipFile(_lowerCamelCase ,"""w""" ) as f: f.write(_lowerCamelCase ,arcname=os.path.basename("""unsupported.ext""" ) ) f.write(_lowerCamelCase ,arcname=os.path.basename("""unsupported_2.ext""" ) ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[str] ) -> Tuple: _lowerCAmelCase : List[str] = """\n""".join(["""First""", """Second\u2029with Unicode new line""", """Third"""] ) _lowerCAmelCase : Tuple = str(tmp_path_factory.mktemp("""data""" ) / """dataset_with_unicode_new_lines.txt""" ) with open(_lowerCamelCase ,"""w""" ,encoding="""utf-8""" ) as f: f.write(_lowerCamelCase ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( ) -> Dict: return os.path.join("""tests""" ,"""features""" ,"""data""" ,"""test_image_rgb.jpg""" ) @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( ) -> Optional[Any]: return os.path.join("""tests""" ,"""features""" ,"""data""" ,"""test_audio_44100.wav""" ) @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ,_lowerCamelCase : Optional[int] ) -> Dict: _lowerCAmelCase : Tuple = tmp_path_factory.mktemp("""data""" ) / """dataset.img.zip""" with zipfile.ZipFile(_lowerCamelCase ,"""w""" ) as f: f.write(_lowerCamelCase ,arcname=os.path.basename(_lowerCamelCase ) ) f.write(_lowerCamelCase ,arcname=os.path.basename(_lowerCamelCase ).replace(""".jpg""" ,"""2.jpg""" ) ) return path @pytest.fixture(scope="""session""" ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Any ) -> Optional[int]: _lowerCAmelCase : Tuple = 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
44
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = {"""configuration_ibert""": ["""IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """IBertConfig""", """IBertOnnxConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """IBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """IBertForMaskedLM""", """IBertForMultipleChoice""", """IBertForQuestionAnswering""", """IBertForSequenceClassification""", """IBertForTokenClassification""", """IBertModel""", """IBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_ibert import IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, IBertConfig, IBertOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ibert import ( IBERT_PRETRAINED_MODEL_ARCHIVE_LIST, IBertForMaskedLM, IBertForMultipleChoice, IBertForQuestionAnswering, IBertForSequenceClassification, IBertForTokenClassification, IBertModel, IBertPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
303
0
"""simple docstring""" from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block @dataclass class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : torch.FloatTensor class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' @register_to_config def __init__( self , _a = 65_536 , _a = None , _a = 2 , _a = 2 , _a = 0 , _a = "fourier" , _a = True , _a = False , _a = 0.0 , _a = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") , _a = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") , _a = "UNetMidBlock1D" , _a = None , _a = (32, 32, 64) , _a = None , _a = 8 , _a = 1 , _a = False , ): super().__init__() __a = sample_size # time if time_embedding_type == "fourier": __a = GaussianFourierProjection( embedding_size=8 , set_W_to_weight=_a , log=_a , flip_sin_to_cos=_a ) __a = 2 * block_out_channels[0] elif time_embedding_type == "positional": __a = Timesteps( block_out_channels[0] , flip_sin_to_cos=_a , downscale_freq_shift=_a ) __a = block_out_channels[0] if use_timestep_embedding: __a = block_out_channels[0] * 4 __a = TimestepEmbedding( in_channels=_a , time_embed_dim=_a , act_fn=_a , out_dim=block_out_channels[0] , ) __a = nn.ModuleList([] ) __a = None __a = nn.ModuleList([] ) __a = None # down __a = in_channels for i, down_block_type in enumerate(_a ): __a = output_channel __a = block_out_channels[i] if i == 0: input_channel += extra_in_channels __a = i == len(_a ) - 1 __a = get_down_block( _a , num_layers=_a , in_channels=_a , out_channels=_a , temb_channels=block_out_channels[0] , add_downsample=not is_final_block or downsample_each_block , ) self.down_blocks.append(_a ) # mid __a = get_mid_block( _a , in_channels=block_out_channels[-1] , mid_channels=block_out_channels[-1] , out_channels=block_out_channels[-1] , embed_dim=block_out_channels[0] , num_layers=_a , add_downsample=_a , ) # up __a = list(reversed(_a ) ) __a = reversed_block_out_channels[0] if out_block_type is None: __a = out_channels else: __a = block_out_channels[0] for i, up_block_type in enumerate(_a ): __a = output_channel __a = ( reversed_block_out_channels[i + 1] if i < len(_a ) - 1 else final_upsample_channels ) __a = i == len(_a ) - 1 __a = get_up_block( _a , num_layers=_a , in_channels=_a , out_channels=_a , temb_channels=block_out_channels[0] , add_upsample=not is_final_block , ) self.up_blocks.append(_a ) __a = output_channel # out __a = norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4 , 32 ) __a = get_out_block( out_block_type=_a , num_groups_out=_a , embed_dim=block_out_channels[0] , out_channels=_a , act_fn=_a , fc_dim=block_out_channels[-1] // 4 , ) def __UpperCAmelCase ( self , _a , _a , _a = True , ): __a = timestep if not torch.is_tensor(_a ): __a = torch.tensor([timesteps] , dtype=torch.long , device=sample.device ) elif torch.is_tensor(_a ) and len(timesteps.shape ) == 0: __a = timesteps[None].to(sample.device ) __a = self.time_proj(_a ) if self.config.use_timestep_embedding: __a = self.time_mlp(_a ) else: __a = timestep_embed[..., None] __a = timestep_embed.repeat([1, 1, sample.shape[2]] ).to(sample.dtype ) __a = timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]) ) # 2. down __a = () for downsample_block in self.down_blocks: __a , __a = downsample_block(hidden_states=_a , temb=_a ) down_block_res_samples += res_samples # 3. mid if self.mid_block: __a = self.mid_block(_a , _a ) # 4. up for i, upsample_block in enumerate(self.up_blocks ): __a = down_block_res_samples[-1:] __a = down_block_res_samples[:-1] __a = upsample_block(_a , res_hidden_states_tuple=_a , temb=_a ) # 5. post-process if self.out_block: __a = self.out_block(_a , _a ) if not return_dict: return (sample,) return UNetaDOutput(sample=_a )
45
from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
303
0
"""simple docstring""" import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets SCREAMING_SNAKE_CASE__ = "\\n@inproceedings{snover-etal-2006-study,\n title = \"A Study of Translation Edit Rate with Targeted Human Annotation\",\n author = \"Snover, Matthew and\n Dorr, Bonnie and\n Schwartz, Rich and\n Micciulla, Linnea and\n Makhoul, John\",\n booktitle = \"Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers\",\n month = aug # \" 8-12\",\n year = \"2006\",\n address = \"Cambridge, Massachusetts, USA\",\n publisher = \"Association for Machine Translation in the Americas\",\n url = \"https://aclanthology.org/2006.amta-papers.25\",\n pages = \"223--231\",\n}\n@inproceedings{post-2018-call,\n title = \"A Call for Clarity in Reporting {BLEU} Scores\",\n author = \"Post, Matt\",\n booktitle = \"Proceedings of the Third Conference on Machine Translation: Research Papers\",\n month = oct,\n year = \"2018\",\n address = \"Belgium, Brussels\",\n publisher = \"Association for Computational Linguistics\",\n url = \"https://www.aclweb.org/anthology/W18-6319\",\n pages = \"186--191\",\n}\n" SCREAMING_SNAKE_CASE__ = "\\nTER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a\nhypothesis requires to match a reference translation. We use the implementation that is already present in sacrebleu\n(https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the TERCOM implementation, which can be found\nhere: https://github.com/jhclark/tercom.\n\nThe implementation here is slightly different from sacrebleu in terms of the required input format. The length of\nthe references and hypotheses lists need to be the same, so you may need to transpose your references compared to\nsacrebleu's required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534\n\nSee the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information.\n" SCREAMING_SNAKE_CASE__ = "\nProduces TER scores alongside the number of edits and reference length.\n\nArgs:\n predictions (list of str): The system stream (a sequence of segments).\n references (list of list of str): A list of one or more reference streams (each a sequence of segments).\n normalized (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`.\n ignore_punct (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`.\n support_zh_ja_chars (boolean): If `True`, tokenization/normalization supports processing of Chinese characters,\n as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana.\n Only applies if `normalized = True`. Defaults to `False`.\n case_sensitive (boolean): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`.\n\nReturns:\n 'score' (float): TER score (num_edits / sum_ref_lengths * 100)\n 'num_edits' (int): The cumulative number of edits\n 'ref_length' (float): The cumulative average reference length\n\nExamples:\n Example 1:\n >>> predictions = [\"does this sentence match??\",\n ... \"what about this sentence?\",\n ... \"What did the TER metric user say to the developer?\"]\n >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"],\n ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"],\n ... [\"Your jokes are...\", \"...TERrible\"]]\n >>> ter = datasets.load_metric(\"ter\")\n >>> results = ter.compute(predictions=predictions,\n ... references=references,\n ... case_sensitive=True)\n >>> print(results)\n {'score': 150.0, 'num_edits': 15, 'ref_length': 10.0}\n\n Example 2:\n >>> predictions = [\"does this sentence match??\",\n ... \"what about this sentence?\"]\n >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"],\n ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"]]\n >>> ter = datasets.load_metric(\"ter\")\n >>> results = ter.compute(predictions=predictions,\n ... references=references,\n ... case_sensitive=True)\n >>> print(results)\n {'score': 62.5, 'num_edits': 5, 'ref_length': 8.0}\n\n Example 3:\n >>> predictions = [\"does this sentence match??\",\n ... \"what about this sentence?\"]\n >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"],\n ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"]]\n >>> ter = datasets.load_metric(\"ter\")\n >>> results = ter.compute(predictions=predictions,\n ... references=references,\n ... normalized=True,\n ... case_sensitive=True)\n >>> print(results)\n {'score': 57.14285714285714, 'num_edits': 6, 'ref_length': 10.5}\n\n Example 4:\n >>> predictions = [\"does this sentence match??\",\n ... \"what about this sentence?\"]\n >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"],\n ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"]]\n >>> ter = datasets.load_metric(\"ter\")\n >>> results = ter.compute(predictions=predictions,\n ... references=references,\n ... ignore_punct=True,\n ... case_sensitive=False)\n >>> print(results)\n {'score': 0.0, 'num_edits': 0, 'ref_length': 8.0}\n\n Example 5:\n >>> predictions = [\"does this sentence match??\",\n ... \"what about this sentence?\",\n ... \"What did the TER metric user say to the developer?\"]\n >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"],\n ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"],\n ... [\"Your jokes are...\", \"...TERrible\"]]\n >>> ter = datasets.load_metric(\"ter\")\n >>> results = ter.compute(predictions=predictions,\n ... references=references,\n ... ignore_punct=True,\n ... case_sensitive=False)\n >>> print(results)\n {'score': 100.0, 'num_edits': 10, 'ref_length': 10.0}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase ( datasets.Metric ): def _snake_case ( self ) -> Tuple: if version.parse(scb.__version__ ) < version.parse("""1.4.12""" ): raise ImportWarning( """To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n""" """You can install it with `pip install \"sacrebleu>=1.4.12\"`.""" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="""http://www.cs.umd.edu/~snover/tercom/""" , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""string""" , id="""sequence""" ), """references""": datasets.Sequence(datasets.Value("""string""" , id="""sequence""" ) , id="""references""" ), } ) , codebase_urls=["""https://github.com/mjpost/sacreBLEU#ter"""] , reference_urls=[ """https://github.com/jhclark/tercom""", ] , ) def _snake_case ( self , lowercase , lowercase , lowercase = False , lowercase = False , lowercase = False , lowercase = False , ) -> Optional[int]: lowerCAmelCase = len(references[0] ) if any(len(lowercase ) != references_per_prediction for refs in references ): raise ValueError("""Sacrebleu requires the same number of references for each prediction""" ) lowerCAmelCase = [[refs[i] for refs in references] for i in range(lowercase )] lowerCAmelCase = TER( normalized=lowercase , no_punct=lowercase , asian_support=lowercase , case_sensitive=lowercase , ) lowerCAmelCase = sb_ter.corpus_score(lowercase , lowercase ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
46
import sys from collections import defaultdict class __UpperCamelCase : """simple docstring""" def __init__( self : Union[str, Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = [] def UpperCAmelCase__ ( self : List[str] , _A : str ): """simple docstring""" return self.node_position[vertex] def UpperCAmelCase__ ( self : Dict , _A : List[str] , _A : Any ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = pos def UpperCAmelCase__ ( self : List[Any] , _A : Union[str, Any] , _A : List[Any] , _A : List[str] , _A : Union[str, Any] ): """simple docstring""" if start > size // 2 - 1: return else: if 2 * start + 2 >= size: __SCREAMING_SNAKE_CASE : Union[str, Any] = 2 * start + 1 else: if heap[2 * start + 1] < heap[2 * start + 2]: __SCREAMING_SNAKE_CASE : List[Any] = 2 * start + 1 else: __SCREAMING_SNAKE_CASE : Dict = 2 * start + 2 if heap[smallest_child] < heap[start]: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Union[str, Any] = heap[smallest_child], positions[smallest_child] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : List[str] = ( heap[start], positions[start], ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = temp, tempa __SCREAMING_SNAKE_CASE : Any = self.get_position(positions[smallest_child] ) self.set_position( positions[smallest_child] , self.get_position(positions[start] ) ) self.set_position(positions[start] , _A ) self.top_to_bottom(_A , _A , _A , _A ) def UpperCAmelCase__ ( self : Any , _A : Union[str, Any] , _A : Dict , _A : Optional[Any] , _A : str ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = position[index] while index != 0: __SCREAMING_SNAKE_CASE : Optional[Any] = int((index - 2) / 2 ) if index % 2 == 0 else int((index - 1) / 2 ) if val < heap[parent]: __SCREAMING_SNAKE_CASE : Optional[Any] = heap[parent] __SCREAMING_SNAKE_CASE : str = position[parent] self.set_position(position[parent] , _A ) else: __SCREAMING_SNAKE_CASE : List[str] = val __SCREAMING_SNAKE_CASE : List[str] = temp self.set_position(_A , _A ) break __SCREAMING_SNAKE_CASE : List[Any] = parent else: __SCREAMING_SNAKE_CASE : Tuple = val __SCREAMING_SNAKE_CASE : List[str] = temp self.set_position(_A , 0 ) def UpperCAmelCase__ ( self : List[str] , _A : Tuple , _A : Tuple ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = len(_A ) // 2 - 1 for i in range(_A , -1 , -1 ): self.top_to_bottom(_A , _A , len(_A ) , _A ) def UpperCAmelCase__ ( self : List[str] , _A : Dict , _A : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = positions[0] __SCREAMING_SNAKE_CASE : Tuple = sys.maxsize self.top_to_bottom(_A , 0 , len(_A ) , _A ) return temp def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = Heap() __SCREAMING_SNAKE_CASE : int = [0] * len(snake_case ) __SCREAMING_SNAKE_CASE : Dict = [-1] * len(snake_case ) # Neighboring Tree Vertex of selected vertex # Minimum Distance of explored vertex with neighboring vertex of partial tree # formed in graph __SCREAMING_SNAKE_CASE : Dict = [] # Heap of Distance of vertices from their neighboring vertex __SCREAMING_SNAKE_CASE : Optional[int] = [] for vertex in range(len(snake_case ) ): distance_tv.append(sys.maxsize ) positions.append(snake_case ) heap.node_position.append(snake_case ) __SCREAMING_SNAKE_CASE : Union[str, Any] = [] __SCREAMING_SNAKE_CASE : str = 1 __SCREAMING_SNAKE_CASE : int = sys.maxsize for neighbor, distance in adjacency_list[0]: __SCREAMING_SNAKE_CASE : Optional[Any] = 0 __SCREAMING_SNAKE_CASE : Dict = distance heap.heapify(snake_case , snake_case ) for _ in range(1 , len(snake_case ) ): __SCREAMING_SNAKE_CASE : Tuple = heap.delete_minimum(snake_case , snake_case ) if visited[vertex] == 0: tree_edges.append((nbr_tv[vertex], vertex) ) __SCREAMING_SNAKE_CASE : List[Any] = 1 for neighbor, distance in adjacency_list[vertex]: if ( visited[neighbor] == 0 and distance < distance_tv[heap.get_position(snake_case )] ): __SCREAMING_SNAKE_CASE : int = distance heap.bottom_to_top( snake_case , heap.get_position(snake_case ) , snake_case , snake_case ) __SCREAMING_SNAKE_CASE : Any = vertex return tree_edges if __name__ == "__main__": # pragma: no cover # < --------- Prims Algorithm --------- > lowercase_ = int(input("""Enter number of edges: """).strip()) lowercase_ = defaultdict(list) for _ in range(edges_number): lowercase_ = [int(x) for x in input().strip().split()] adjacency_list[edge[0]].append([edge[1], edge[2]]) adjacency_list[edge[1]].append([edge[0], edge[2]]) print(prisms_algorithm(adjacency_list))
303
0
'''simple docstring''' from typing import List, Optional, Tuple, Union import torch from ...schedulers import DDIMScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class A__ ( A__ ): def __init__( self : str , _a : List[Any] , _a : Dict ) -> Union[str, Any]: '''simple docstring''' super().__init__() # make sure scheduler can always be converted to DDIM _SCREAMING_SNAKE_CASE =DDIMScheduler.from_config(scheduler.config ) self.register_modules(unet=_a , scheduler=_a ) @torch.no_grad() def __call__( self : List[Any] , _a : int = 1 , _a : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _a : float = 0.0 , _a : int = 50 , _a : Optional[bool] = None , _a : Optional[str] = "pil" , _a : bool = True , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(self.unet.config.sample_size , _a ): _SCREAMING_SNAKE_CASE =( batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size, ) else: _SCREAMING_SNAKE_CASE =(batch_size, self.unet.config.in_channels, *self.unet.config.sample_size) if isinstance(_a , _a ) and len(_a ) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(_a )}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) _SCREAMING_SNAKE_CASE =randn_tensor(_a , generator=_a , device=self.device , dtype=self.unet.dtype ) # set step values self.scheduler.set_timesteps(_a ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output _SCREAMING_SNAKE_CASE =self.unet(_a , _a ).sample # 2. predict previous mean of image x_t-1 and add variance depending on eta # eta corresponds to η in paper and should be between [0, 1] # do x_t -> x_t-1 _SCREAMING_SNAKE_CASE =self.scheduler.step( _a , _a , _a , eta=_a , use_clipped_model_output=_a , generator=_a ).prev_sample _SCREAMING_SNAKE_CASE =(image / 2 + 0.5).clamp(0 , 1 ) _SCREAMING_SNAKE_CASE =image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _SCREAMING_SNAKE_CASE =self.numpy_to_pil(_a ) if not return_dict: return (image,) return ImagePipelineOutput(images=_a )
47
from __future__ import annotations import matplotlib.pyplot as plt # type: ignore import numpy # initial triangle of Koch snowflake lowercase_ = numpy.array([0, 0]) lowercase_ = numpy.array([0.5, 0.866_0254]) lowercase_ = numpy.array([1, 0]) lowercase_ = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1] def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = initial_vectors for _ in range(snake_case ): __SCREAMING_SNAKE_CASE : Dict = iteration_step(snake_case ) return vectors def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : str = [] for i, start_vector in enumerate(vectors[:-1] ): __SCREAMING_SNAKE_CASE : str = vectors[i + 1] new_vectors.append(snake_case ) __SCREAMING_SNAKE_CASE : List[str] = end_vector - start_vector new_vectors.append(start_vector + difference_vector / 3 ) new_vectors.append( start_vector + difference_vector / 3 + rotate(difference_vector / 3 , 60 ) ) new_vectors.append(start_vector + difference_vector * 2 / 3 ) new_vectors.append(vectors[-1] ) return new_vectors def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = numpy.radians(snake_case ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = numpy.cos(snake_case ), numpy.sin(snake_case ) __SCREAMING_SNAKE_CASE : Any = numpy.array(((c, -s), (s, c)) ) return numpy.dot(snake_case , snake_case ) def a__ ( snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : int = plt.gca() axes.set_aspect('''equal''' ) # matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all # y-coordinates as inputs, which are constructed from the vector-list using # zip() __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = zip(*snake_case ) plt.plot(snake_case , snake_case ) plt.show() if __name__ == "__main__": import doctest doctest.testmod() lowercase_ = iterate(INITIAL_VECTORS, 5) plot(processed_vectors)
303
0
import argparse import os import shutil from pathlib import Path import onnx import torch from packaging import version from torch.onnx import export from diffusers import OnnxRuntimeModel, OnnxStableDiffusionPipeline, StableDiffusionPipeline SCREAMING_SNAKE_CASE__ : Dict = version.parse(version.parse(torch.__version__).base_version) < version.parse('1.11') def A ( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE=False ,) -> List[Any]: output_path.parent.mkdir(parents=_SCREAMING_SNAKE_CASE ,exist_ok=_SCREAMING_SNAKE_CASE ) # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11, # so we check the torch version for backwards compatibility if is_torch_less_than_1_11: export( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,f=output_path.as_posix() ,input_names=_SCREAMING_SNAKE_CASE ,output_names=_SCREAMING_SNAKE_CASE ,dynamic_axes=_SCREAMING_SNAKE_CASE ,do_constant_folding=_SCREAMING_SNAKE_CASE ,use_external_data_format=_SCREAMING_SNAKE_CASE ,enable_onnx_checker=_SCREAMING_SNAKE_CASE ,opset_version=_SCREAMING_SNAKE_CASE ,) else: export( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,f=output_path.as_posix() ,input_names=_SCREAMING_SNAKE_CASE ,output_names=_SCREAMING_SNAKE_CASE ,dynamic_axes=_SCREAMING_SNAKE_CASE ,do_constant_folding=_SCREAMING_SNAKE_CASE ,opset_version=_SCREAMING_SNAKE_CASE ,) @torch.no_grad() def A ( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE = False ) -> List[Any]: lowerCamelCase : Union[str, Any] = torch.floataa if fpaa else torch.floataa if fpaa and torch.cuda.is_available(): lowerCamelCase : Union[str, Any] = "cuda" elif fpaa and not torch.cuda.is_available(): raise ValueError("`float16` model export is only supported on GPUs with CUDA" ) else: lowerCamelCase : Dict = "cpu" lowerCamelCase : int = StableDiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE ,torch_dtype=_SCREAMING_SNAKE_CASE ).to(_SCREAMING_SNAKE_CASE ) lowerCamelCase : Tuple = Path(_SCREAMING_SNAKE_CASE ) # TEXT ENCODER lowerCamelCase : Dict = pipeline.text_encoder.config.max_position_embeddings lowerCamelCase : Dict = pipeline.text_encoder.config.hidden_size lowerCamelCase : Dict = pipeline.tokenizer( "A sample prompt" ,padding="max_length" ,max_length=pipeline.tokenizer.model_max_length ,truncation=_SCREAMING_SNAKE_CASE ,return_tensors="pt" ,) onnx_export( pipeline.text_encoder ,model_args=(text_input.input_ids.to(device=_SCREAMING_SNAKE_CASE ,dtype=torch.intaa )) ,output_path=output_path / "text_encoder" / "model.onnx" ,ordered_input_names=["input_ids"] ,output_names=["last_hidden_state", "pooler_output"] ,dynamic_axes={ "input_ids": {0: "batch", 1: "sequence"}, } ,opset=_SCREAMING_SNAKE_CASE ,) del pipeline.text_encoder # UNET lowerCamelCase : int = pipeline.unet.config.in_channels lowerCamelCase : Any = pipeline.unet.config.sample_size lowerCamelCase : List[str] = output_path / "unet" / "model.onnx" onnx_export( pipeline.unet ,model_args=( torch.randn(2 ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ).to(device=_SCREAMING_SNAKE_CASE ,dtype=_SCREAMING_SNAKE_CASE ), torch.randn(2 ).to(device=_SCREAMING_SNAKE_CASE ,dtype=_SCREAMING_SNAKE_CASE ), torch.randn(2 ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ).to(device=_SCREAMING_SNAKE_CASE ,dtype=_SCREAMING_SNAKE_CASE ), False, ) ,output_path=_SCREAMING_SNAKE_CASE ,ordered_input_names=["sample", "timestep", "encoder_hidden_states", "return_dict"] ,output_names=["out_sample"] ,dynamic_axes={ "sample": {0: "batch", 1: "channels", 2: "height", 3: "width"}, "timestep": {0: "batch"}, "encoder_hidden_states": {0: "batch", 1: "sequence"}, } ,opset=_SCREAMING_SNAKE_CASE ,use_external_data_format=_SCREAMING_SNAKE_CASE ,) lowerCamelCase : List[str] = str(unet_path.absolute().as_posix() ) lowerCamelCase : Optional[int] = os.path.dirname(_SCREAMING_SNAKE_CASE ) lowerCamelCase : str = onnx.load(_SCREAMING_SNAKE_CASE ) # clean up existing tensor files shutil.rmtree(_SCREAMING_SNAKE_CASE ) os.mkdir(_SCREAMING_SNAKE_CASE ) # collate external tensor files into one onnx.save_model( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,save_as_external_data=_SCREAMING_SNAKE_CASE ,all_tensors_to_one_file=_SCREAMING_SNAKE_CASE ,location="weights.pb" ,convert_attribute=_SCREAMING_SNAKE_CASE ,) del pipeline.unet # VAE ENCODER lowerCamelCase : int = pipeline.vae lowerCamelCase : Optional[Any] = vae_encoder.config.in_channels lowerCamelCase : int = vae_encoder.config.sample_size # need to get the raw tensor output (sample) from the encoder lowerCamelCase : str = lambda _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE : vae_encoder.encode(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE )[0].sample() onnx_export( _SCREAMING_SNAKE_CASE ,model_args=( torch.randn(1 ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ).to(device=_SCREAMING_SNAKE_CASE ,dtype=_SCREAMING_SNAKE_CASE ), False, ) ,output_path=output_path / "vae_encoder" / "model.onnx" ,ordered_input_names=["sample", "return_dict"] ,output_names=["latent_sample"] ,dynamic_axes={ "sample": {0: "batch", 1: "channels", 2: "height", 3: "width"}, } ,opset=_SCREAMING_SNAKE_CASE ,) # VAE DECODER lowerCamelCase : int = pipeline.vae lowerCamelCase : Optional[int] = vae_decoder.config.latent_channels lowerCamelCase : str = vae_decoder.config.out_channels # forward only through the decoder part lowerCamelCase : str = vae_encoder.decode onnx_export( _SCREAMING_SNAKE_CASE ,model_args=( torch.randn(1 ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ).to(device=_SCREAMING_SNAKE_CASE ,dtype=_SCREAMING_SNAKE_CASE ), False, ) ,output_path=output_path / "vae_decoder" / "model.onnx" ,ordered_input_names=["latent_sample", "return_dict"] ,output_names=["sample"] ,dynamic_axes={ "latent_sample": {0: "batch", 1: "channels", 2: "height", 3: "width"}, } ,opset=_SCREAMING_SNAKE_CASE ,) del pipeline.vae # SAFETY CHECKER if pipeline.safety_checker is not None: lowerCamelCase : int = pipeline.safety_checker lowerCamelCase : str = safety_checker.config.vision_config.num_channels lowerCamelCase : Tuple = safety_checker.config.vision_config.image_size lowerCamelCase : int = safety_checker.forward_onnx onnx_export( pipeline.safety_checker ,model_args=( torch.randn( 1 ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,).to(device=_SCREAMING_SNAKE_CASE ,dtype=_SCREAMING_SNAKE_CASE ), torch.randn(1 ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ).to(device=_SCREAMING_SNAKE_CASE ,dtype=_SCREAMING_SNAKE_CASE ), ) ,output_path=output_path / "safety_checker" / "model.onnx" ,ordered_input_names=["clip_input", "images"] ,output_names=["out_images", "has_nsfw_concepts"] ,dynamic_axes={ "clip_input": {0: "batch", 1: "channels", 2: "height", 3: "width"}, "images": {0: "batch", 1: "height", 2: "width", 3: "channels"}, } ,opset=_SCREAMING_SNAKE_CASE ,) del pipeline.safety_checker lowerCamelCase : List[Any] = OnnxRuntimeModel.from_pretrained(output_path / "safety_checker" ) lowerCamelCase : Optional[Any] = pipeline.feature_extractor else: lowerCamelCase : List[Any] = None lowerCamelCase : Optional[int] = None lowerCamelCase : str = OnnxStableDiffusionPipeline( vae_encoder=OnnxRuntimeModel.from_pretrained(output_path / "vae_encoder" ) ,vae_decoder=OnnxRuntimeModel.from_pretrained(output_path / "vae_decoder" ) ,text_encoder=OnnxRuntimeModel.from_pretrained(output_path / "text_encoder" ) ,tokenizer=pipeline.tokenizer ,unet=OnnxRuntimeModel.from_pretrained(output_path / "unet" ) ,scheduler=pipeline.scheduler ,safety_checker=_SCREAMING_SNAKE_CASE ,feature_extractor=_SCREAMING_SNAKE_CASE ,requires_safety_checker=safety_checker is not None ,) onnx_pipeline.save_pretrained(_SCREAMING_SNAKE_CASE ) print("ONNX pipeline saved to" ,_SCREAMING_SNAKE_CASE ) del pipeline del onnx_pipeline lowerCamelCase : List[str] = OnnxStableDiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE ,provider="CPUExecutionProvider" ) print("ONNX pipeline is loadable" ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ : int = argparse.ArgumentParser() parser.add_argument( '--model_path', type=str, required=True, help='Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).', ) parser.add_argument('--output_path', type=str, required=True, help='Path to the output model.') parser.add_argument( '--opset', default=14, type=int, help='The version of the ONNX operator set to use.', ) parser.add_argument('--fp16', action='store_true', default=False, help='Export the models in `float16` mode') SCREAMING_SNAKE_CASE__ : Any = parser.parse_args() convert_models(args.model_path, args.output_path, args.opset, args.fpaa)
48
from typing import List, Optional, Union import torch from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) lowercase_ = logging.get_logger(__name__) # pylint: disable=invalid-name lowercase_ = """ Examples: ```py >>> import torch >>> import numpy as np >>> from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline >>> from transformers import pipeline >>> from diffusers.utils import load_image >>> def make_hint(image, depth_estimator): ... image = depth_estimator(image)[\"depth\"] ... image = np.array(image) ... image = image[:, :, None] ... image = np.concatenate([image, image, image], axis=2) ... detected_map = torch.from_numpy(image).float() / 255.0 ... hint = detected_map.permute(2, 0, 1) ... return hint >>> depth_estimator = pipeline(\"depth-estimation\") >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained( ... \"kandinsky-community/kandinsky-2-2-prior\", torch_dtype=torch.float16 ... ) >>> pipe_prior = pipe_prior.to(\"cuda\") >>> pipe = KandinskyV22ControlnetPipeline.from_pretrained( ... \"kandinsky-community/kandinsky-2-2-controlnet-depth\", torch_dtype=torch.float16 ... ) >>> pipe = pipe.to(\"cuda\") >>> img = load_image( ... \"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main\" ... \"/kandinsky/cat.png\" ... ).resize((768, 768)) >>> hint = make_hint(img, depth_estimator).unsqueeze(0).half().to(\"cuda\") >>> prompt = \"A robot, 4k photo\" >>> negative_prior_prompt = \"lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature\" >>> generator = torch.Generator(device=\"cuda\").manual_seed(43) >>> image_emb, zero_image_emb = pipe_prior( ... prompt=prompt, negative_prompt=negative_prior_prompt, generator=generator ... ).to_tuple() >>> images = pipe( ... image_embeds=image_emb, ... negative_image_embeds=zero_image_emb, ... hint=hint, ... num_inference_steps=50, ... generator=generator, ... height=768, ... width=768, ... ).images >>> images[0].save(\"robot_cat.png\") ``` """ def a__ ( snake_case , snake_case , snake_case=8 ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 __SCREAMING_SNAKE_CASE : Optional[int] = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" def __init__( self : int , _A : UNetaDConditionModel , _A : DDPMScheduler , _A : VQModel , ): """simple docstring""" super().__init__() self.register_modules( unet=_A , scheduler=_A , movq=_A , ) __SCREAMING_SNAKE_CASE : Optional[Any] = 2 ** (len(self.movq.config.block_out_channels ) - 1) def UpperCAmelCase__ ( self : Union[str, Any] , _A : Dict , _A : Optional[Any] , _A : Tuple , _A : List[Any] , _A : Optional[Any] , _A : List[Any] ): """simple docstring""" if latents is None: __SCREAMING_SNAKE_CASE : Optional[Any] = randn_tensor(_A , generator=_A , device=_A , dtype=_A ) else: if latents.shape != shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {shape}''' ) __SCREAMING_SNAKE_CASE : Tuple = latents.to(_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = latents * scheduler.init_noise_sigma return latents def UpperCAmelCase__ ( self : Tuple , _A : List[str]=0 ): """simple docstring""" if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) __SCREAMING_SNAKE_CASE : List[Any] = torch.device(F'''cuda:{gpu_id}''' ) __SCREAMING_SNAKE_CASE : Dict = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_A , _A ) def UpperCAmelCase__ ( self : int , _A : Tuple=0 ): """simple docstring""" if is_accelerate_available() and is_accelerate_version('''>=''' , '''0.17.0.dev0''' ): from accelerate import cpu_offload_with_hook else: raise ImportError('''`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.''' ) __SCREAMING_SNAKE_CASE : str = torch.device(F'''cuda:{gpu_id}''' ) if self.device.type != "cpu": self.to('''cpu''' , silence_dtype_warnings=_A ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) __SCREAMING_SNAKE_CASE : Optional[Any] = None for cpu_offloaded_model in [self.unet, self.movq]: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Dict = cpu_offload_with_hook(_A , _A , prev_module_hook=_A ) # We'll offload the last model manually. __SCREAMING_SNAKE_CASE : Optional[int] = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def UpperCAmelCase__ ( self : Union[str, Any] ): """simple docstring""" if not hasattr(self.unet , '''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(_A , '''_hf_hook''' ) and hasattr(module._hf_hook , '''execution_device''' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(_A ) def __call__( self : Dict , _A : Union[torch.FloatTensor, List[torch.FloatTensor]] , _A : Union[torch.FloatTensor, List[torch.FloatTensor]] , _A : torch.FloatTensor , _A : int = 512 , _A : int = 512 , _A : int = 100 , _A : float = 4.0 , _A : int = 1 , _A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _A : Optional[torch.FloatTensor] = None , _A : Optional[str] = "pil" , _A : bool = True , ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = self._execution_device __SCREAMING_SNAKE_CASE : Optional[Any] = guidance_scale > 1.0 if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : Optional[Any] = torch.cat(_A , dim=0 ) if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : List[Any] = torch.cat(_A , dim=0 ) if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : List[str] = torch.cat(_A , dim=0 ) __SCREAMING_SNAKE_CASE : List[Any] = image_embeds.shape[0] * num_images_per_prompt if do_classifier_free_guidance: __SCREAMING_SNAKE_CASE : Dict = image_embeds.repeat_interleave(_A , dim=0 ) __SCREAMING_SNAKE_CASE : Any = negative_image_embeds.repeat_interleave(_A , dim=0 ) __SCREAMING_SNAKE_CASE : Union[str, Any] = hint.repeat_interleave(_A , dim=0 ) __SCREAMING_SNAKE_CASE : int = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=_A ) __SCREAMING_SNAKE_CASE : Optional[int] = torch.cat([hint, hint] , dim=0 ).to(dtype=self.unet.dtype , device=_A ) self.scheduler.set_timesteps(_A , device=_A ) __SCREAMING_SNAKE_CASE : Optional[int] = self.scheduler.timesteps __SCREAMING_SNAKE_CASE : Tuple = self.movq.config.latent_channels __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Dict = downscale_height_and_width(_A , _A , self.movq_scale_factor ) # create initial latent __SCREAMING_SNAKE_CASE : Tuple = self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , _A , _A , _A , self.scheduler , ) for i, t in enumerate(self.progress_bar(_A ) ): # expand the latents if we are doing classifier free guidance __SCREAMING_SNAKE_CASE : str = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents __SCREAMING_SNAKE_CASE : Dict = {'''image_embeds''': image_embeds, '''hint''': hint} __SCREAMING_SNAKE_CASE : Union[str, Any] = self.unet( sample=_A , timestep=_A , encoder_hidden_states=_A , added_cond_kwargs=_A , return_dict=_A , )[0] if do_classifier_free_guidance: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = noise_pred.split(latents.shape[1] , dim=1 ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Dict = noise_pred.chunk(2 ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[Any] = variance_pred.chunk(2 ) __SCREAMING_SNAKE_CASE : List[Any] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) __SCREAMING_SNAKE_CASE : Tuple = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , '''variance_type''' ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : List[str] = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 __SCREAMING_SNAKE_CASE : Any = self.scheduler.step( _A , _A , _A , generator=_A , )[0] # post-processing __SCREAMING_SNAKE_CASE : Any = self.movq.decode(_A , force_not_quantize=_A )['''sample'''] if output_type not in ["pt", "np", "pil"]: raise ValueError(F'''Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}''' ) if output_type in ["np", "pil"]: __SCREAMING_SNAKE_CASE : str = image * 0.5 + 0.5 __SCREAMING_SNAKE_CASE : Tuple = image.clamp(0 , 1 ) __SCREAMING_SNAKE_CASE : Dict = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": __SCREAMING_SNAKE_CASE : List[str] = self.numpy_to_pil(_A ) if not return_dict: return (image,) return ImagePipelineOutput(images=_A )
303
0
import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def __snake_case ( _UpperCAmelCase ): __a = {} __a = tokenizer(example['''content'''] , truncation=_UpperCAmelCase )['''input_ids'''] __a = len(example['''content'''] ) / len(output['''input_ids'''] ) return output __snake_case :Any = HfArgumentParser(PretokenizationArguments) __snake_case :Dict = parser.parse_args() if args.num_workers is None: __snake_case :List[Any] = multiprocessing.cpu_count() __snake_case :Dict = AutoTokenizer.from_pretrained(args.tokenizer_dir) __snake_case :Optional[int] = time.time() __snake_case :Any = load_dataset(args.dataset_name, split='''train''') print(f'Dataset loaded in {time.time()-t_start:.2f}s') __snake_case :Optional[Any] = time.time() __snake_case :Tuple = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ '''repo_name''', '''path''', '''copies''', '''size''', '''content''', '''license''', '''hash''', '''line_mean''', '''line_max''', '''alpha_frac''', '''autogenerated''', ], ) print(f'Dataset tokenized in {time.time()-t_start:.2f}s') __snake_case :Tuple = time.time() ds.push_to_hub(args.tokenized_data_repo) print(f'Data pushed to the hub in {time.time()-t_start:.2f}s')
49
import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem lowercase_ = importlib.util.find_spec("""s3fs""") is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 lowercase_ = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(f'''A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.''') fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def a__ ( snake_case ): """simple docstring""" if "://" in dataset_path: __SCREAMING_SNAKE_CASE : Any = dataset_path.split('''://''' )[1] return dataset_path def a__ ( snake_case ): """simple docstring""" if fs is not None and fs.protocol != "file": return True else: return False def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = not is_remote_filesystem(snake_case ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(snake_case ) , fs._strip_protocol(snake_case ) ) else: fs.mv(snake_case , snake_case , recursive=snake_case ) def a__ ( ): """simple docstring""" if hasattr(fsspec.asyn , '''reset_lock''' ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: __SCREAMING_SNAKE_CASE : int = None __SCREAMING_SNAKE_CASE : Union[str, Any] = None __SCREAMING_SNAKE_CASE : Union[str, Any] = threading.Lock()
303
0
from manim import * class lowerCAmelCase ( __UpperCamelCase ): def A_ ( self : Dict ) -> List[str]: lowerCamelCase__ : List[Any] = Rectangle(height=0.5 , width=0.5 ) lowerCamelCase__ : Optional[Any] = Rectangle(height=0.4_6 , width=0.4_6 ).set_stroke(width=0 ) lowerCamelCase__ : Optional[int] = [mem.copy() for i in range(6 )] lowerCamelCase__ : Union[str, Any] = [mem.copy() for i in range(6 )] lowerCamelCase__ : Any = VGroup(*UpperCAmelCase ).arrange(UpperCAmelCase , buff=0 ) lowerCamelCase__ : Any = VGroup(*UpperCAmelCase ).arrange(UpperCAmelCase , buff=0 ) lowerCamelCase__ : int = VGroup(UpperCAmelCase , UpperCAmelCase ).arrange(UpperCAmelCase , buff=0 ) lowerCamelCase__ : List[Any] = Text('CPU' , font_size=24 ) lowerCamelCase__ : Union[str, Any] = Group(UpperCAmelCase , UpperCAmelCase ).arrange(UpperCAmelCase , buff=0.5 , aligned_edge=UpperCAmelCase ) cpu.move_to([-2.5, -0.5, 0] ) self.add(UpperCAmelCase ) lowerCamelCase__ : List[Any] = [mem.copy() for i in range(1 )] lowerCamelCase__ : str = VGroup(*UpperCAmelCase ).arrange(UpperCAmelCase , buff=0 ) lowerCamelCase__ : int = Text('GPU' , font_size=24 ) lowerCamelCase__ : Optional[Any] = Group(UpperCAmelCase , UpperCAmelCase ).arrange(UpperCAmelCase , buff=0.5 , aligned_edge=UpperCAmelCase ) gpu.align_to(UpperCAmelCase , UpperCAmelCase ) gpu.set_x(gpu.get_x() - 1 ) self.add(UpperCAmelCase ) lowerCamelCase__ : Optional[int] = [mem.copy() for i in range(6 )] lowerCamelCase__ : Tuple = VGroup(*UpperCAmelCase ).arrange(UpperCAmelCase , buff=0 ) lowerCamelCase__ : Dict = Text('Model' , font_size=24 ) lowerCamelCase__ : Dict = Group(UpperCAmelCase , UpperCAmelCase ).arrange(UpperCAmelCase , buff=0.5 , aligned_edge=UpperCAmelCase ) model.move_to([3, -1.0, 0] ) self.play( Create(UpperCAmelCase , run_time=1 ) , Create(UpperCAmelCase , run_time=1 ) , Create(UpperCAmelCase , run_time=1 ) , ) lowerCamelCase__ : Dict = MarkupText( F"""First, an empty model skeleton is loaded\ninto <span fgcolor='{YELLOW}'>memory</span> without using much RAM.""" , font_size=24 , ) lowerCamelCase__ : Any = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) lowerCamelCase__ : str = MarkupText( F"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model""" , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) step_a.move_to([2, 2, 0] ) self.play(Write(UpperCAmelCase , run_time=2.5 ) , Write(UpperCAmelCase ) , Write(UpperCAmelCase ) ) self.add(UpperCAmelCase ) lowerCamelCase__ : int = [] lowerCamelCase__ : str = [] lowerCamelCase__ : Dict = [] for i, rect in enumerate(UpperCAmelCase ): lowerCamelCase__ : Union[str, Any] = Rectangle(height=0.4_6 , width=0.4_6 ).set_stroke(width=0.0 ).set_fill(UpperCAmelCase , opacity=0.7 ) cpu_target.move_to(UpperCAmelCase ) cpu_target.generate_target() lowerCamelCase__ : Dict = 0.4_6 / 4 lowerCamelCase__ : List[str] = 0.4_6 / 3 if i == 0: cpu_target.target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.0_2 , direction=UpperCAmelCase ) cpu_target.target.set_x(cpu_target.target.get_x() + 0.1 ) elif i == 3: cpu_target.target.next_to(cpu_targs[0].target , direction=UpperCAmelCase , buff=0.0 ) else: cpu_target.target.next_to(cpu_targs[i - 1].target , direction=UpperCAmelCase , buff=0.0 ) cpu_targs.append(UpperCAmelCase ) first_animations.append(rect.animate(run_time=0.5 ).set_stroke(UpperCAmelCase ) ) second_animations.append(MoveToTarget(UpperCAmelCase , run_time=1.5 ) ) self.play(*UpperCAmelCase ) self.play(*UpperCAmelCase ) self.wait()
50
from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels
303
0
import gc import inspect import unittest import torch from parameterized import parameterized from diffusers import PriorTransformer from diffusers.utils import floats_tensor, slow, torch_all_close, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin enable_full_determinism() class __snake_case ( a , unittest.TestCase ): UpperCAmelCase__ : Tuple = PriorTransformer UpperCAmelCase__ : Tuple = '''hidden_states''' @property def lowerCamelCase ( self : Any): """simple docstring""" UpperCAmelCase_ = 4 UpperCAmelCase_ = 8 UpperCAmelCase_ = 7 UpperCAmelCase_ = floats_tensor((batch_size, embedding_dim)).to(_snake_case) UpperCAmelCase_ = floats_tensor((batch_size, embedding_dim)).to(_snake_case) UpperCAmelCase_ = floats_tensor((batch_size, num_embeddings, embedding_dim)).to(_snake_case) return { "hidden_states": hidden_states, "timestep": 2, "proj_embedding": proj_embedding, "encoder_hidden_states": encoder_hidden_states, } def lowerCamelCase ( self : int , _snake_case : List[str]=0): """simple docstring""" torch.manual_seed(_snake_case) UpperCAmelCase_ = 4 UpperCAmelCase_ = 8 UpperCAmelCase_ = 7 UpperCAmelCase_ = torch.randn((batch_size, embedding_dim)).to(_snake_case) UpperCAmelCase_ = torch.randn((batch_size, embedding_dim)).to(_snake_case) UpperCAmelCase_ = torch.randn((batch_size, num_embeddings, embedding_dim)).to(_snake_case) return { "hidden_states": hidden_states, "timestep": 2, "proj_embedding": proj_embedding, "encoder_hidden_states": encoder_hidden_states, } @property def lowerCamelCase ( self : List[Any]): """simple docstring""" return (4, 8) @property def lowerCamelCase ( self : str): """simple docstring""" return (4, 8) def lowerCamelCase ( self : List[Any]): """simple docstring""" UpperCAmelCase_ = { '''num_attention_heads''': 2, '''attention_head_dim''': 4, '''num_layers''': 2, '''embedding_dim''': 8, '''num_embeddings''': 7, '''additional_embeddings''': 4, } UpperCAmelCase_ = self.dummy_input return init_dict, inputs_dict def lowerCamelCase ( self : int): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = PriorTransformer.from_pretrained( '''hf-internal-testing/prior-dummy''' , output_loading_info=_snake_case) self.assertIsNotNone(_snake_case) self.assertEqual(len(loading_info['''missing_keys''']) , 0) model.to(_snake_case) UpperCAmelCase_ = model(**self.dummy_input)[0] assert hidden_states is not None, "Make sure output is not None" def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = self.prepare_init_args_and_inputs_for_common() UpperCAmelCase_ = self.model_class(**_snake_case) UpperCAmelCase_ = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase_ = [*signature.parameters.keys()] UpperCAmelCase_ = ['''hidden_states''', '''timestep'''] self.assertListEqual(arg_names[:2] , _snake_case) def lowerCamelCase ( self : str): """simple docstring""" UpperCAmelCase_ = PriorTransformer.from_pretrained('''hf-internal-testing/prior-dummy''') UpperCAmelCase_ = model.to(_snake_case) if hasattr(_snake_case , '''set_default_attn_processor'''): model.set_default_attn_processor() UpperCAmelCase_ = self.get_dummy_seed_input() with torch.no_grad(): UpperCAmelCase_ = model(**_snake_case)[0] UpperCAmelCase_ = output[0, :5].flatten().cpu() print(_snake_case) # Since the VAE Gaussian prior's generator is seeded on the appropriate device, # the expected output slices are not the same for CPU and GPU. UpperCAmelCase_ = torch.tensor([-1.3_4_3_6, -0.2_8_7_0, 0.7_5_3_8, 0.4_3_6_8, -0.0_2_3_9]) self.assertTrue(torch_all_close(_snake_case , _snake_case , rtol=1e-2)) @slow class __snake_case ( unittest.TestCase ): def lowerCamelCase ( self : Union[str, Any] , _snake_case : Optional[Any]=1 , _snake_case : Any=768 , _snake_case : Optional[Any]=77 , _snake_case : Optional[int]=0): """simple docstring""" torch.manual_seed(_snake_case) UpperCAmelCase_ = batch_size UpperCAmelCase_ = embedding_dim UpperCAmelCase_ = num_embeddings UpperCAmelCase_ = torch.randn((batch_size, embedding_dim)).to(_snake_case) UpperCAmelCase_ = torch.randn((batch_size, embedding_dim)).to(_snake_case) UpperCAmelCase_ = torch.randn((batch_size, num_embeddings, embedding_dim)).to(_snake_case) return { "hidden_states": hidden_states, "timestep": 2, "proj_embedding": proj_embedding, "encoder_hidden_states": encoder_hidden_states, } def lowerCamelCase ( self : str): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @parameterized.expand( [ # fmt: off [13, [-0.5_8_6_1, 0.1_2_8_3, -0.0_9_3_1, 0.0_8_8_2, 0.4_4_7_6, 0.1_3_2_9, -0.0_4_9_8, 0.0_6_4_0]], [37, [-0.4_9_1_3, 0.0_1_1_0, -0.0_4_8_3, 0.0_5_4_1, 0.4_9_5_4, -0.0_1_7_0, 0.0_3_5_4, 0.1_6_5_1]], # fmt: on ]) def lowerCamelCase ( self : List[str] , _snake_case : List[Any] , _snake_case : List[Any]): """simple docstring""" UpperCAmelCase_ = PriorTransformer.from_pretrained('''kandinsky-community/kandinsky-2-1-prior''' , subfolder='''prior''') model.to(_snake_case) UpperCAmelCase_ = self.get_dummy_seed_input(seed=_snake_case) with torch.no_grad(): UpperCAmelCase_ = model(**_snake_case)[0] assert list(sample.shape) == [1, 768] UpperCAmelCase_ = sample[0, :8].flatten().cpu() print(_snake_case) UpperCAmelCase_ = torch.tensor(_snake_case) assert torch_all_close(_snake_case , _snake_case , atol=1e-3)
51
from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { """uw-madison/mra-base-512-4""": """https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json""", } class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''mra''' def __init__( self : str , _A : List[str]=5_0265 , _A : int=768 , _A : Union[str, Any]=12 , _A : Union[str, Any]=12 , _A : Union[str, Any]=3072 , _A : Any="gelu" , _A : List[Any]=0.1 , _A : List[Any]=0.1 , _A : List[str]=512 , _A : Tuple=1 , _A : List[str]=0.02 , _A : Union[str, Any]=1e-5 , _A : Optional[int]="absolute" , _A : Union[str, Any]=4 , _A : List[Any]="full" , _A : Union[str, Any]=0 , _A : Union[str, Any]=0 , _A : Optional[Any]=1 , _A : Union[str, Any]=0 , _A : Any=2 , **_A : List[str] , ): """simple docstring""" super().__init__(pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , **_A ) __SCREAMING_SNAKE_CASE : Dict = vocab_size __SCREAMING_SNAKE_CASE : str = max_position_embeddings __SCREAMING_SNAKE_CASE : Optional[int] = hidden_size __SCREAMING_SNAKE_CASE : str = num_hidden_layers __SCREAMING_SNAKE_CASE : Tuple = num_attention_heads __SCREAMING_SNAKE_CASE : str = intermediate_size __SCREAMING_SNAKE_CASE : Tuple = hidden_act __SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob __SCREAMING_SNAKE_CASE : int = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : Tuple = initializer_range __SCREAMING_SNAKE_CASE : Any = type_vocab_size __SCREAMING_SNAKE_CASE : str = layer_norm_eps __SCREAMING_SNAKE_CASE : Union[str, Any] = position_embedding_type __SCREAMING_SNAKE_CASE : str = block_per_row __SCREAMING_SNAKE_CASE : Union[str, Any] = approx_mode __SCREAMING_SNAKE_CASE : Optional[int] = initial_prior_first_n_blocks __SCREAMING_SNAKE_CASE : List[Any] = initial_prior_diagonal_n_blocks
303
0
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class A__ ( __snake_case , __snake_case ): _UpperCAmelCase :Dict = 1 @register_to_config def __init__( self , A_=2000 , A_=0.1 , A_=20 , A_=1e-3 ): '''simple docstring''' UpperCamelCase : List[Any] = None UpperCamelCase : Any = None UpperCamelCase : Dict = None def __UpperCamelCase( self , A_ , A_ = None ): '''simple docstring''' UpperCamelCase : Dict = torch.linspace(1 , self.config.sampling_eps , A_ , device=A_ ) def __UpperCamelCase( self , A_ , A_ , A_ , A_=None ): '''simple docstring''' if self.timesteps is None: raise ValueError( "`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler" ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score UpperCamelCase : int = ( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) UpperCamelCase : List[str] = torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) UpperCamelCase : List[str] = std.flatten() while len(std.shape ) < len(score.shape ): UpperCamelCase : Optional[Any] = std.unsqueeze(-1 ) UpperCamelCase : Union[str, Any] = -score / std # compute UpperCamelCase : str = -1.0 / len(self.timesteps ) UpperCamelCase : Optional[int] = self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) UpperCamelCase : Tuple = beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): UpperCamelCase : Optional[int] = beta_t.unsqueeze(-1 ) UpperCamelCase : List[Any] = -0.5 * beta_t * x UpperCamelCase : List[Any] = torch.sqrt(A_ ) UpperCamelCase : Any = drift - diffusion**2 * score UpperCamelCase : List[Any] = x + drift * dt # add noise UpperCamelCase : str = randn_tensor(x.shape , layout=x.layout , generator=A_ , device=x.device , dtype=x.dtype ) UpperCamelCase : Union[str, Any] = x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__( self ): '''simple docstring''' return self.config.num_train_timesteps
52
from typing import List, Optional, Tuple, Union import torch from ...utils import logging, randn_tensor from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline lowercase_ = logging.get_logger(__name__) # pylint: disable=invalid-name class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" def __init__( self : List[str] , _A : Dict , _A : List[Any] ): """simple docstring""" super().__init__() self.register_modules(unet=_A , scheduler=_A ) @torch.no_grad() def __call__( self : List[str] , _A : int = 1 , _A : int = 100 , _A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _A : Optional[float] = None , _A : bool = True , ): """simple docstring""" if audio_length_in_s is None: __SCREAMING_SNAKE_CASE : Optional[Any] = self.unet.config.sample_size / self.unet.config.sample_rate __SCREAMING_SNAKE_CASE : List[Any] = audio_length_in_s * self.unet.config.sample_rate __SCREAMING_SNAKE_CASE : Any = 2 ** len(self.unet.up_blocks ) if sample_size < 3 * down_scale_factor: raise ValueError( F'''{audio_length_in_s} is too small. Make sure it\'s bigger or equal to''' F''' {3 * down_scale_factor / self.unet.config.sample_rate}.''' ) __SCREAMING_SNAKE_CASE : int = int(_A ) if sample_size % down_scale_factor != 0: __SCREAMING_SNAKE_CASE : Optional[int] = ( (audio_length_in_s * self.unet.config.sample_rate) // down_scale_factor + 1 ) * down_scale_factor logger.info( F'''{audio_length_in_s} is increased to {sample_size / self.unet.config.sample_rate} so that it can be handled''' F''' by the model. It will be cut to {original_sample_size / self.unet.config.sample_rate} after the denoising''' ''' process.''' ) __SCREAMING_SNAKE_CASE : List[Any] = int(_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = next(iter(self.unet.parameters() ) ).dtype __SCREAMING_SNAKE_CASE : int = (batch_size, self.unet.config.in_channels, sample_size) if isinstance(_A , _A ) and len(_A ) != batch_size: raise ValueError( F'''You have passed a list of generators of length {len(_A )}, but requested an effective batch''' F''' size of {batch_size}. Make sure the batch size matches the length of the generators.''' ) __SCREAMING_SNAKE_CASE : Dict = randn_tensor(_A , generator=_A , device=self.device , dtype=_A ) # set step values self.scheduler.set_timesteps(_A , device=audio.device ) __SCREAMING_SNAKE_CASE : Dict = self.scheduler.timesteps.to(_A ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output __SCREAMING_SNAKE_CASE : List[Any] = self.unet(_A , _A ).sample # 2. compute previous image: x_t -> t_t-1 __SCREAMING_SNAKE_CASE : Optional[int] = self.scheduler.step(_A , _A , _A ).prev_sample __SCREAMING_SNAKE_CASE : str = audio.clamp(-1 , 1 ).float().cpu().numpy() __SCREAMING_SNAKE_CASE : str = audio[:, :, :original_sample_size] if not return_dict: return (audio,) return AudioPipelineOutput(audios=_A )
303
0
'''simple docstring''' # Lint as: python3 import os import re import urllib.parse from pathlib import Path from typing import Callable, List, Optional, Union from zipfile import ZipFile from ..utils.file_utils import cached_path, hf_github_url from ..utils.logging import get_logger from ..utils.version import Version a__ : List[str] =get_logger(__name__) class snake_case : """simple docstring""" SCREAMING_SNAKE_CASE_ : str ="dummy_data" SCREAMING_SNAKE_CASE_ : List[str] ="datasets" SCREAMING_SNAKE_CASE_ : Dict =False def __init__( self : str , __A : str , __A : str , __A : Union[Version, str] , __A : Optional[str] = None , __A : bool = False , __A : bool = True , __A : Optional[List[Callable]] = None , ): __UpperCamelCase = 0 __UpperCamelCase = dataset_name __UpperCamelCase = cache_dir __UpperCamelCase = use_local_dummy_data __UpperCamelCase = config # download_callbacks take a single url as input __UpperCamelCase = download_callbacks or [] # if False, it doesn't load existing files and it returns the paths of the dummy files relative # to the dummy_data zip file root __UpperCamelCase = load_existing_dummy_data # TODO(PVP, QL) might need to make this more general __UpperCamelCase = str(__A ) # to be downloaded __UpperCamelCase = None __UpperCamelCase = None @property def _lowerCamelCase ( self : int ): if self._dummy_file is None: __UpperCamelCase = self.download_dummy_data() return self._dummy_file @property def _lowerCamelCase ( self : Optional[int] ): if self.config is not None: # structure is dummy / config_name / version_name return os.path.join('dummy' , self.config.name , self.version_name ) # structure is dummy / version_name return os.path.join('dummy' , self.version_name ) @property def _lowerCamelCase ( self : Union[str, Any] ): return os.path.join(self.dummy_data_folder , 'dummy_data.zip' ) def _lowerCamelCase ( self : Dict ): __UpperCamelCase = ( self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data ) __UpperCamelCase = cached_path( __A , cache_dir=self.cache_dir , extract_compressed_file=__A , force_extract=__A ) return os.path.join(__A , self.dummy_file_name ) @property def _lowerCamelCase ( self : List[str] ): return os.path.join(self.datasets_scripts_dir , self.dataset_name , self.dummy_zip_file ) @property def _lowerCamelCase ( self : Tuple ): if self._bucket_url is None: __UpperCamelCase = hf_github_url(self.dataset_name , self.dummy_zip_file.replace(os.sep , '/' ) ) return self._bucket_url @property def _lowerCamelCase ( self : Union[str, Any] ): # return full path if its a dir if os.path.isdir(self.dummy_file ): return self.dummy_file # else cut off path to file -> example `xsum`. return "/".join(self.dummy_file.replace(os.sep , '/' ).split('/' )[:-1] ) def _lowerCamelCase ( self : Optional[int] , __A : Optional[int] , *__A : Optional[int] ): if self.load_existing_dummy_data: # dummy data is downloaded and tested __UpperCamelCase = self.dummy_file else: # dummy data cannot be downloaded and only the path to dummy file is returned __UpperCamelCase = self.dummy_file_name # special case when data_url is a dict if isinstance(__A , __A ): return self.create_dummy_data_dict(__A , __A ) elif isinstance(__A , (list, tuple) ): return self.create_dummy_data_list(__A , __A ) else: return self.create_dummy_data_single(__A , __A ) def _lowerCamelCase ( self : Any , __A : Dict , *__A : Any ): return self.download_and_extract(__A ) def _lowerCamelCase ( self : Optional[Any] , __A : Tuple , __A : Dict ): return self.download_and_extract(__A ) def _lowerCamelCase ( self : int , __A : Any , *__A : List[str] , **__A : Optional[Any] ): return path def _lowerCamelCase ( self : str ): return {} def _lowerCamelCase ( self : Any , __A : int , __A : Any ): __UpperCamelCase = {} for key, single_urls in data_url.items(): for download_callback in self.download_callbacks: if isinstance(__A , __A ): for single_url in single_urls: download_callback(__A ) else: __UpperCamelCase = single_urls download_callback(__A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus if isinstance(__A , __A ): __UpperCamelCase = [os.path.join(__A , urllib.parse.quote_plus(Path(__A ).name ) ) for x in single_urls] else: __UpperCamelCase = single_urls __UpperCamelCase = os.path.join(__A , urllib.parse.quote_plus(Path(__A ).name ) ) __UpperCamelCase = value # make sure that values are unique if all(isinstance(__A , __A ) for i in dummy_data_dict.values() ) and len(set(dummy_data_dict.values() ) ) < len( dummy_data_dict.values() ): # append key to value to make its name unique __UpperCamelCase = {key: value + key for key, value in dummy_data_dict.items()} return dummy_data_dict def _lowerCamelCase ( self : int , __A : Dict , __A : Optional[Any] ): __UpperCamelCase = [] # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one __UpperCamelCase = all(bool(re.findall('[0-9]{3,}-of-[0-9]{3,}' , __A ) ) for url in data_url ) __UpperCamelCase = all( url.startswith('https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed' ) for url in data_url ) if data_url and (is_tf_records or is_pubmed_records): __UpperCamelCase = [data_url[0]] * len(__A ) for single_url in data_url: for download_callback in self.download_callbacks: download_callback(__A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __UpperCamelCase = os.path.join(__A , urllib.parse.quote_plus(single_url.split('/' )[-1] ) ) dummy_data_list.append(__A ) return dummy_data_list def _lowerCamelCase ( self : Optional[int] , __A : Any , __A : List[str] ): for download_callback in self.download_callbacks: download_callback(__A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __UpperCamelCase = os.path.join(__A , urllib.parse.quote_plus(data_url.split('/' )[-1] ) ) if os.path.exists(__A ) or not self.load_existing_dummy_data: return value else: # Backward compatibility, maybe deprecate at one point. # For many datasets with single url calls to dl_manager.download_and_extract, # the dummy_data.zip file is actually the zipped downloaded file # while now we expected the dummy_data.zip file to be a directory containing # the downloaded file. return path_to_dummy_data def _lowerCamelCase ( self : str ): pass def _lowerCamelCase ( self : List[Any] ): pass def _lowerCamelCase ( self : Tuple , __A : Dict ): def _iter_archive_members(__A : Tuple ): # this preserves the order of the members inside the ZIP archive __UpperCamelCase = Path(self.dummy_file ).parent __UpperCamelCase = path.relative_to(__A ) with ZipFile(self.local_path_to_dummy_data ) as zip_file: __UpperCamelCase = zip_file.namelist() for member in members: if member.startswith(relative_path.as_posix() ): yield dummy_parent_path.joinpath(__A ) __UpperCamelCase = Path(__A ) __UpperCamelCase = _iter_archive_members(__A ) if self.use_local_dummy_data else path.rglob('*' ) for file_path in file_paths: if file_path.is_file() and not file_path.name.startswith(('.', '__') ): yield file_path.relative_to(__A ).as_posix(), file_path.open('rb' ) def _lowerCamelCase ( self : str , __A : Optional[Any] ): if not isinstance(__A , __A ): __UpperCamelCase = [paths] for path in paths: if os.path.isfile(__A ): if os.path.basename(__A ).startswith(('.', '__') ): return yield path else: for dirpath, dirnames, filenames in os.walk(__A ): if os.path.basename(__A ).startswith(('.', '__') ): continue dirnames.sort() for filename in sorted(__A ): if filename.startswith(('.', '__') ): continue yield os.path.join(__A , __A )
53
import importlib import torch import yaml from omegaconf import OmegaConf from taming.models.vqgan import VQModel def a__ ( snake_case , snake_case=False ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = OmegaConf.load(snake_case ) if display: print(yaml.dump(OmegaConf.to_container(snake_case ) ) ) return config def a__ ( snake_case , snake_case=None , snake_case=None ): """simple docstring""" if conf_path is None: __SCREAMING_SNAKE_CASE : Any = '''./model_checkpoints/vqgan_only.yaml''' __SCREAMING_SNAKE_CASE : List[str] = load_config(snake_case , display=snake_case ) __SCREAMING_SNAKE_CASE : str = VQModel(**config.model.params ) if ckpt_path is None: __SCREAMING_SNAKE_CASE : Optional[Any] = '''./model_checkpoints/vqgan_only.pt''' __SCREAMING_SNAKE_CASE : Optional[Any] = torch.load(snake_case , map_location=snake_case ) if ".ckpt" in ckpt_path: __SCREAMING_SNAKE_CASE : Optional[Any] = sd['''state_dict'''] model.load_state_dict(snake_case , strict=snake_case ) model.to(snake_case ) del sd return model def a__ ( snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Any = model.encode(snake_case ) print(F'''VQGAN --- {model.__class__.__name__}: latent shape: {z.shape[2:]}''' ) __SCREAMING_SNAKE_CASE : Any = model.decode(snake_case ) return xrec def a__ ( snake_case , snake_case=False ): """simple docstring""" __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = string.rsplit('''.''' , 1 ) if reload: __SCREAMING_SNAKE_CASE : Union[str, Any] = importlib.import_module(snake_case ) importlib.reload(snake_case ) return getattr(importlib.import_module(snake_case , package=snake_case ) , cls ) def a__ ( snake_case ): """simple docstring""" if "target" not in config: raise KeyError('''Expected key `target` to instantiate.''' ) return get_obj_from_str(config['''target'''] )(**config.get('''params''' , {} ) ) def a__ ( snake_case , snake_case , snake_case=True , snake_case=True ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = instantiate_from_config(snake_case ) if sd is not None: model.load_state_dict(snake_case ) if gpu: model.cuda() if eval_mode: model.eval() return {"model": model} def a__ ( snake_case , snake_case , snake_case , snake_case ): """simple docstring""" # load the specified checkpoint if ckpt: __SCREAMING_SNAKE_CASE : Dict = torch.load(snake_case , map_location='''cpu''' ) __SCREAMING_SNAKE_CASE : List[Any] = pl_sd['''global_step'''] print(F'''loaded model from global step {global_step}.''' ) else: __SCREAMING_SNAKE_CASE : Optional[Any] = {'''state_dict''': None} __SCREAMING_SNAKE_CASE : Optional[Any] = None __SCREAMING_SNAKE_CASE : Dict = load_model_from_config(config.model , pl_sd['''state_dict'''] , gpu=snake_case , eval_mode=snake_case )['''model'''] return model, global_step
303
0
"""simple docstring""" from pathlib import Path import cva import numpy as np from matplotlib import pyplot as plt def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ): '''simple docstring''' __SCREAMING_SNAKE_CASE = cva.getAffineTransform(lowerCAmelCase_ , lowerCAmelCase_ ) return cva.warpAffine(lowerCAmelCase_ , lowerCAmelCase_ , (rows, cols) ) if __name__ == "__main__": # read original image a__ : Any = cva.imread( str(Path(__file__).resolve().parent.parent / '''image_data''' / '''lena.jpg''') ) # turn image in gray scale value a__ : Optional[Any] = cva.cvtColor(image, cva.COLOR_BGR2GRAY) # get image shape a__ , a__ : Union[str, Any] = gray_img.shape # set different points to rotate image a__ : Optional[Any] = np.array([[5_0, 5_0], [2_0_0, 5_0], [5_0, 2_0_0]], np.floataa) a__ : List[str] = np.array([[1_0, 1_0_0], [2_0_0, 5_0], [1_0_0, 2_5_0]], np.floataa) a__ : Optional[Any] = np.array([[5_0, 5_0], [1_5_0, 5_0], [1_2_0, 2_0_0]], np.floataa) a__ : Tuple = np.array([[1_0, 1_0_0], [8_0, 5_0], [1_8_0, 2_5_0]], np.floataa) # add all rotated images in a list a__ : Union[str, Any] = [ gray_img, get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), ] # plot different image rotations a__ : Any = plt.figure(1) a__ : Optional[int] = ['''Original''', '''Rotation 1''', '''Rotation 2''', '''Rotation 3'''] for i, image in enumerate(images): plt.subplot(2, 2, i + 1), plt.imshow(image, '''gray''') plt.title(titles[i]) plt.axis('''off''') plt.subplots_adjust(left=0.0, bottom=0.05, right=1.0, top=0.95) plt.show()
54
from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { """studio-ousia/luke-base""": """https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json""", """studio-ousia/luke-large""": """https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json""", } class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" lowerCAmelCase_ = '''luke''' def __init__( self : Any , _A : int=5_0267 , _A : str=50_0000 , _A : Dict=768 , _A : int=256 , _A : Tuple=12 , _A : Optional[Any]=12 , _A : Any=3072 , _A : Tuple="gelu" , _A : Any=0.1 , _A : Dict=0.1 , _A : Any=512 , _A : Tuple=2 , _A : int=0.02 , _A : Any=1e-12 , _A : Dict=True , _A : Optional[Any]=None , _A : List[str]=1 , _A : List[str]=0 , _A : Dict=2 , **_A : List[str] , ): """simple docstring""" super().__init__(pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , **_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = vocab_size __SCREAMING_SNAKE_CASE : Any = entity_vocab_size __SCREAMING_SNAKE_CASE : int = hidden_size __SCREAMING_SNAKE_CASE : List[Any] = entity_emb_size __SCREAMING_SNAKE_CASE : Union[str, Any] = num_hidden_layers __SCREAMING_SNAKE_CASE : Tuple = num_attention_heads __SCREAMING_SNAKE_CASE : Dict = hidden_act __SCREAMING_SNAKE_CASE : Dict = intermediate_size __SCREAMING_SNAKE_CASE : Optional[int] = hidden_dropout_prob __SCREAMING_SNAKE_CASE : Any = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : List[Any] = max_position_embeddings __SCREAMING_SNAKE_CASE : List[str] = type_vocab_size __SCREAMING_SNAKE_CASE : Dict = initializer_range __SCREAMING_SNAKE_CASE : Tuple = layer_norm_eps __SCREAMING_SNAKE_CASE : int = use_entity_aware_attention __SCREAMING_SNAKE_CASE : Any = classifier_dropout
303
0
'''simple docstring''' import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import DeiTImageProcessor, ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel from transformers.utils import logging logging.set_verbosity_info() a_ : int = logging.get_logger(__name__) def __snake_case ( UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Tuple=False ): lowerCamelCase_ = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F'''blocks.{i}.norm1.weight''', F'''vit.encoder.layer.{i}.layernorm_before.weight''') ) rename_keys.append((F'''blocks.{i}.norm1.bias''', F'''vit.encoder.layer.{i}.layernorm_before.bias''') ) rename_keys.append((F'''blocks.{i}.attn.proj.weight''', F'''vit.encoder.layer.{i}.attention.output.dense.weight''') ) rename_keys.append((F'''blocks.{i}.attn.proj.bias''', F'''vit.encoder.layer.{i}.attention.output.dense.bias''') ) rename_keys.append((F'''blocks.{i}.norm2.weight''', F'''vit.encoder.layer.{i}.layernorm_after.weight''') ) rename_keys.append((F'''blocks.{i}.norm2.bias''', F'''vit.encoder.layer.{i}.layernorm_after.bias''') ) rename_keys.append((F'''blocks.{i}.mlp.fc1.weight''', F'''vit.encoder.layer.{i}.intermediate.dense.weight''') ) rename_keys.append((F'''blocks.{i}.mlp.fc1.bias''', F'''vit.encoder.layer.{i}.intermediate.dense.bias''') ) rename_keys.append((F'''blocks.{i}.mlp.fc2.weight''', F'''vit.encoder.layer.{i}.output.dense.weight''') ) rename_keys.append((F'''blocks.{i}.mlp.fc2.bias''', F'''vit.encoder.layer.{i}.output.dense.bias''') ) # projection layer + position embeddings rename_keys.extend( [ ("cls_token", "vit.embeddings.cls_token"), ("patch_embed.proj.weight", "vit.embeddings.patch_embeddings.projection.weight"), ("patch_embed.proj.bias", "vit.embeddings.patch_embeddings.projection.bias"), ("pos_embed", "vit.embeddings.position_embeddings"), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ("norm.weight", "layernorm.weight"), ("norm.bias", "layernorm.bias"), ("pre_logits.fc.weight", "pooler.dense.weight"), ("pre_logits.fc.bias", "pooler.dense.bias"), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" lowerCamelCase_ = [(pair[0], pair[1][4:]) if pair[1].startswith("vit" ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ("norm.weight", "vit.layernorm.weight"), ("norm.bias", "vit.layernorm.bias"), ("head.weight", "classifier.weight"), ("head.bias", "classifier.bias"), ] ) return rename_keys def __snake_case ( UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Dict=False ): for i in range(config.num_hidden_layers ): if base_model: lowerCamelCase_ = "" else: lowerCamelCase_ = "vit." # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) lowerCamelCase_ = state_dict.pop(F'''blocks.{i}.attn.qkv.weight''' ) lowerCamelCase_ = state_dict.pop(F'''blocks.{i}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict lowerCamelCase_ = in_proj_weight[ : config.hidden_size, : ] lowerCamelCase_ = in_proj_bias[: config.hidden_size] lowerCamelCase_ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] lowerCamelCase_ = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] lowerCamelCase_ = in_proj_weight[ -config.hidden_size :, : ] lowerCamelCase_ = in_proj_bias[-config.hidden_size :] def __snake_case ( UpperCAmelCase_ : int ): lowerCamelCase_ = ["head.weight", "head.bias"] for k in ignore_keys: state_dict.pop(UpperCAmelCase_ , UpperCAmelCase_ ) def __snake_case ( UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : int ): lowerCamelCase_ = dct.pop(UpperCAmelCase_ ) lowerCamelCase_ = val def __snake_case ( ): lowerCamelCase_ = "http://images.cocodataset.org/val2017/000000039769.jpg" lowerCamelCase_ = Image.open(requests.get(UpperCAmelCase_ , stream=UpperCAmelCase_ ).raw ) return im @torch.no_grad() def __snake_case ( UpperCAmelCase_ : Any , UpperCAmelCase_ : Optional[int] ): lowerCamelCase_ = ViTConfig() lowerCamelCase_ = False # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size if vit_name[-5:] == "in21k": lowerCamelCase_ = True lowerCamelCase_ = int(vit_name[-12:-10] ) lowerCamelCase_ = int(vit_name[-9:-6] ) else: lowerCamelCase_ = 1000 lowerCamelCase_ = "huggingface/label-files" 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_ = idalabel lowerCamelCase_ = {v: k for k, v in idalabel.items()} lowerCamelCase_ = int(vit_name[-6:-4] ) lowerCamelCase_ = int(vit_name[-3:] ) # size of the architecture if "deit" in vit_name: if vit_name[9:].startswith("tiny" ): lowerCamelCase_ = 192 lowerCamelCase_ = 768 lowerCamelCase_ = 12 lowerCamelCase_ = 3 elif vit_name[9:].startswith("small" ): lowerCamelCase_ = 384 lowerCamelCase_ = 1536 lowerCamelCase_ = 12 lowerCamelCase_ = 6 else: pass else: if vit_name[4:].startswith("small" ): lowerCamelCase_ = 768 lowerCamelCase_ = 2304 lowerCamelCase_ = 8 lowerCamelCase_ = 8 elif vit_name[4:].startswith("base" ): pass elif vit_name[4:].startswith("large" ): lowerCamelCase_ = 1024 lowerCamelCase_ = 4096 lowerCamelCase_ = 24 lowerCamelCase_ = 16 elif vit_name[4:].startswith("huge" ): lowerCamelCase_ = 1280 lowerCamelCase_ = 5120 lowerCamelCase_ = 32 lowerCamelCase_ = 16 # load original model from timm lowerCamelCase_ = timm.create_model(UpperCAmelCase_ , pretrained=UpperCAmelCase_ ) timm_model.eval() # load state_dict of original model, remove and rename some keys lowerCamelCase_ = timm_model.state_dict() if base_model: remove_classification_head_(UpperCAmelCase_ ) lowerCamelCase_ = create_rename_keys(UpperCAmelCase_ , UpperCAmelCase_ ) for src, dest in rename_keys: rename_key(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) read_in_q_k_v(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) # load HuggingFace model if vit_name[-5:] == "in21k": lowerCamelCase_ = ViTModel(UpperCAmelCase_ ).eval() else: lowerCamelCase_ = ViTForImageClassification(UpperCAmelCase_ ).eval() model.load_state_dict(UpperCAmelCase_ ) # Check outputs on an image, prepared by ViTImageProcessor/DeiTImageProcessor if "deit" in vit_name: lowerCamelCase_ = DeiTImageProcessor(size=config.image_size ) else: lowerCamelCase_ = ViTImageProcessor(size=config.image_size ) lowerCamelCase_ = image_processor(images=prepare_img() , return_tensors="pt" ) lowerCamelCase_ = encoding["pixel_values"] lowerCamelCase_ = model(UpperCAmelCase_ ) if base_model: lowerCamelCase_ = timm_model.forward_features(UpperCAmelCase_ ) assert timm_pooled_output.shape == outputs.pooler_output.shape assert torch.allclose(UpperCAmelCase_ , outputs.pooler_output , atol=1E-3 ) else: lowerCamelCase_ = timm_model(UpperCAmelCase_ ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(UpperCAmelCase_ , outputs.logits , atol=1E-3 ) Path(UpperCAmelCase_ ).mkdir(exist_ok=UpperCAmelCase_ ) print(F'''Saving model {vit_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(UpperCAmelCase_ ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(UpperCAmelCase_ ) if __name__ == "__main__": a_ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--vit_name""", default="""vit_base_patch16_224""", type=str, help="""Name of the ViT timm model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) a_ : List[str] = parser.parse_args() convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path)
55
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase_ = { """configuration_electra""": ["""ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ElectraConfig""", """ElectraOnnxConfig"""], """tokenization_electra""": ["""ElectraTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["""ElectraTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST""", """ElectraForCausalLM""", """ElectraForMaskedLM""", """ElectraForMultipleChoice""", """ElectraForPreTraining""", """ElectraForQuestionAnswering""", """ElectraForSequenceClassification""", """ElectraForTokenClassification""", """ElectraModel""", """ElectraPreTrainedModel""", """load_tf_weights_in_electra""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFElectraForMaskedLM""", """TFElectraForMultipleChoice""", """TFElectraForPreTraining""", """TFElectraForQuestionAnswering""", """TFElectraForSequenceClassification""", """TFElectraForTokenClassification""", """TFElectraModel""", """TFElectraPreTrainedModel""", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ """FlaxElectraForCausalLM""", """FlaxElectraForMaskedLM""", """FlaxElectraForMultipleChoice""", """FlaxElectraForPreTraining""", """FlaxElectraForQuestionAnswering""", """FlaxElectraForSequenceClassification""", """FlaxElectraForTokenClassification""", """FlaxElectraModel""", """FlaxElectraPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_electra import ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ElectraConfig, ElectraOnnxConfig from .tokenization_electra import ElectraTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_electra_fast import ElectraTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_electra import ( ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, ElectraForCausalLM, ElectraForMaskedLM, ElectraForMultipleChoice, ElectraForPreTraining, ElectraForQuestionAnswering, ElectraForSequenceClassification, ElectraForTokenClassification, ElectraModel, ElectraPreTrainedModel, load_tf_weights_in_electra, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_electra import ( TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, TFElectraForMaskedLM, TFElectraForMultipleChoice, TFElectraForPreTraining, TFElectraForQuestionAnswering, TFElectraForSequenceClassification, TFElectraForTokenClassification, TFElectraModel, TFElectraPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_electra import ( FlaxElectraForCausalLM, FlaxElectraForMaskedLM, FlaxElectraForMultipleChoice, FlaxElectraForPreTraining, FlaxElectraForQuestionAnswering, FlaxElectraForSequenceClassification, FlaxElectraForTokenClassification, FlaxElectraModel, FlaxElectraPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
303
0
'''simple docstring''' import gc import unittest import torch from parameterized import parameterized from diffusers import AutoencoderKL from diffusers.utils import floats_tensor, load_hf_numpy, require_torch_gpu, slow, torch_all_close, torch_device from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin enable_full_determinism() class a ( _lowerCamelCase , _lowerCamelCase , unittest.TestCase ): snake_case_ = AutoencoderKL snake_case_ = "sample" snake_case_ = 1e-2 @property def A_ ( self : Dict ): snake_case_ = 4 snake_case_ = 3 snake_case_ = (32, 32) snake_case_ = floats_tensor((batch_size, num_channels) + sizes ).to(lowercase_ ) return {"sample": image} @property def A_ ( self : List[Any] ): return (3, 32, 32) @property def A_ ( self : Dict ): return (3, 32, 32) def A_ ( self : Union[str, Any] ): snake_case_ = { '''block_out_channels''': [32, 64], '''in_channels''': 3, '''out_channels''': 3, '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''], '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''], '''latent_channels''': 4, } snake_case_ = self.dummy_input return init_dict, inputs_dict def A_ ( self : Any ): pass def A_ ( self : str ): pass @unittest.skipIf(torch_device == '''mps''' , '''Gradient checkpointing skipped on MPS''' ) def A_ ( self : Dict ): # enable deterministic behavior for gradient checkpointing snake_case_ ,snake_case_ = self.prepare_init_args_and_inputs_for_common() snake_case_ = self.model_class(**lowercase_ ) model.to(lowercase_ ) assert not model.is_gradient_checkpointing and model.training snake_case_ = model(**lowercase_ ).sample # run the backwards pass on the model. For backwards pass, for simplicity purpose, # we won't calculate the loss and rather backprop on out.sum() model.zero_grad() snake_case_ = torch.randn_like(lowercase_ ) snake_case_ = (out - labels).mean() loss.backward() # re-instantiate the model now enabling gradient checkpointing snake_case_ = self.model_class(**lowercase_ ) # clone model model_a.load_state_dict(model.state_dict() ) model_a.to(lowercase_ ) model_a.enable_gradient_checkpointing() assert model_a.is_gradient_checkpointing and model_a.training snake_case_ = model_a(**lowercase_ ).sample # run the backwards pass on the model. For backwards pass, for simplicity purpose, # we won't calculate the loss and rather backprop on out.sum() model_a.zero_grad() snake_case_ = (out_a - labels).mean() loss_a.backward() # compare the output and parameters gradients self.assertTrue((loss - loss_a).abs() < 1e-5 ) snake_case_ = dict(model.named_parameters() ) snake_case_ = dict(model_a.named_parameters() ) for name, param in named_params.items(): self.assertTrue(torch_all_close(param.grad.data , named_params_a[name].grad.data , atol=5e-5 ) ) def A_ ( self : Tuple ): snake_case_ ,snake_case_ = AutoencoderKL.from_pretrained('''fusing/autoencoder-kl-dummy''' , output_loading_info=lowercase_ ) self.assertIsNotNone(lowercase_ ) self.assertEqual(len(loading_info['''missing_keys'''] ) , 0 ) model.to(lowercase_ ) snake_case_ = model(**self.dummy_input ) assert image is not None, "Make sure output is not None" def A_ ( self : Tuple ): snake_case_ = AutoencoderKL.from_pretrained('''fusing/autoencoder-kl-dummy''' ) snake_case_ = model.to(lowercase_ ) model.eval() if torch_device == "mps": snake_case_ = torch.manual_seed(0 ) else: snake_case_ = torch.Generator(device=lowercase_ ).manual_seed(0 ) snake_case_ = torch.randn( 1 , model.config.in_channels , model.config.sample_size , model.config.sample_size , generator=torch.manual_seed(0 ) , ) snake_case_ = image.to(lowercase_ ) with torch.no_grad(): snake_case_ = model(lowercase_ , sample_posterior=lowercase_ , generator=lowercase_ ).sample snake_case_ = output[0, -1, -3:, -3:].flatten().cpu() # Since the VAE Gaussian prior's generator is seeded on the appropriate device, # the expected output slices are not the same for CPU and GPU. if torch_device == "mps": snake_case_ = torch.tensor( [ -4.0_078e-01, -3.8_323e-04, -1.2_681e-01, -1.1_462e-01, 2.0_095e-01, 1.0_893e-01, -8.8_247e-02, -3.0_361e-01, -9.8_644e-03, ] ) elif torch_device == "cpu": snake_case_ = torch.tensor( [-0.1352, 0.0878, 0.0419, -0.0818, -0.1069, 0.0688, -0.1458, -0.4446, -0.0026] ) else: snake_case_ = torch.tensor( [-0.2421, 0.4642, 0.2507, -0.0438, 0.0682, 0.3160, -0.2018, -0.0727, 0.2485] ) self.assertTrue(torch_all_close(lowercase_ , lowercase_ , rtol=1e-2 ) ) @slow class a ( unittest.TestCase ): def A_ ( self : Union[str, Any] , lowercase_ : Optional[Any] , lowercase_ : Optional[int] ): return F"gaussian_noise_s={seed}_shape={'_'.join([str(lowercase_ ) for s in shape] )}.npy" def A_ ( self : Any ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def A_ ( self : Dict , lowercase_ : List[Any]=0 , lowercase_ : Union[str, Any]=(4, 3, 512, 512) , lowercase_ : Optional[Any]=False ): snake_case_ = torch.floataa if fpaa else torch.floataa snake_case_ = torch.from_numpy(load_hf_numpy(self.get_file_format(lowercase_ , lowercase_ ) ) ).to(lowercase_ ).to(lowercase_ ) return image def A_ ( self : Any , lowercase_ : Dict="CompVis/stable-diffusion-v1-4" , lowercase_ : List[str]=False ): snake_case_ = '''fp16''' if fpaa else None snake_case_ = torch.floataa if fpaa else torch.floataa snake_case_ = AutoencoderKL.from_pretrained( lowercase_ , subfolder='''vae''' , torch_dtype=lowercase_ , revision=lowercase_ , ) model.to(lowercase_ ).eval() return model def A_ ( self : Any , lowercase_ : int=0 ): if torch_device == "mps": return torch.manual_seed(lowercase_ ) return torch.Generator(device=lowercase_ ).manual_seed(lowercase_ ) @parameterized.expand( [ # fmt: off [33, [-0.1603, 0.9878, -0.0495, -0.0790, -0.2709, 0.8375, -0.2060, -0.0824], [-0.2395, 0.0098, 0.0102, -0.0709, -0.2840, -0.0274, -0.0718, -0.1824]], [47, [-0.2376, 0.1168, 0.1332, -0.4840, -0.2508, -0.0791, -0.0493, -0.4089], [0.0350, 0.0847, 0.0467, 0.0344, -0.0842, -0.0547, -0.0633, -0.1131]], # fmt: on ] ) def A_ ( self : Union[str, Any] , lowercase_ : Tuple , lowercase_ : Tuple , lowercase_ : Tuple ): snake_case_ = self.get_sd_vae_model() snake_case_ = self.get_sd_image(lowercase_ ) snake_case_ = self.get_generator(lowercase_ ) with torch.no_grad(): snake_case_ = model(lowercase_ , generator=lowercase_ , sample_posterior=lowercase_ ).sample assert sample.shape == image.shape snake_case_ = sample[-1, -2:, -2:, :2].flatten().float().cpu() snake_case_ = torch.tensor(expected_slice_mps if torch_device == '''mps''' else expected_slice ) assert torch_all_close(lowercase_ , lowercase_ , atol=3e-3 ) @parameterized.expand( [ # fmt: off [33, [-0.0513, 0.0289, 1.3799, 0.2166, -0.2573, -0.0871, 0.5103, -0.0999]], [47, [-0.4128, -0.1320, -0.3704, 0.1965, -0.4116, -0.2332, -0.3340, 0.2247]], # fmt: on ] ) @require_torch_gpu def A_ ( self : Optional[int] , lowercase_ : Union[str, Any] , lowercase_ : Dict ): snake_case_ = self.get_sd_vae_model(fpaa=lowercase_ ) snake_case_ = self.get_sd_image(lowercase_ , fpaa=lowercase_ ) snake_case_ = self.get_generator(lowercase_ ) with torch.no_grad(): snake_case_ = model(lowercase_ , generator=lowercase_ , sample_posterior=lowercase_ ).sample assert sample.shape == image.shape snake_case_ = sample[-1, -2:, :2, -2:].flatten().float().cpu() snake_case_ = torch.tensor(lowercase_ ) assert torch_all_close(lowercase_ , lowercase_ , atol=1e-2 ) @parameterized.expand( [ # fmt: off [33, [-0.1609, 0.9866, -0.0487, -0.0777, -0.2716, 0.8368, -0.2055, -0.0814], [-0.2395, 0.0098, 0.0102, -0.0709, -0.2840, -0.0274, -0.0718, -0.1824]], [47, [-0.2377, 0.1147, 0.1333, -0.4841, -0.2506, -0.0805, -0.0491, -0.4085], [0.0350, 0.0847, 0.0467, 0.0344, -0.0842, -0.0547, -0.0633, -0.1131]], # fmt: on ] ) def A_ ( self : Tuple , lowercase_ : Dict , lowercase_ : str , lowercase_ : Optional[int] ): snake_case_ = self.get_sd_vae_model() snake_case_ = self.get_sd_image(lowercase_ ) with torch.no_grad(): snake_case_ = model(lowercase_ ).sample assert sample.shape == image.shape snake_case_ = sample[-1, -2:, -2:, :2].flatten().float().cpu() snake_case_ = torch.tensor(expected_slice_mps if torch_device == '''mps''' else expected_slice ) assert torch_all_close(lowercase_ , lowercase_ , atol=3e-3 ) @parameterized.expand( [ # fmt: off [13, [-0.2051, -0.1803, -0.2311, -0.2114, -0.3292, -0.3574, -0.2953, -0.3323]], [37, [-0.2632, -0.2625, -0.2199, -0.2741, -0.4539, -0.4990, -0.3720, -0.4925]], # fmt: on ] ) @require_torch_gpu def A_ ( self : Dict , lowercase_ : Tuple , lowercase_ : Optional[int] ): snake_case_ = self.get_sd_vae_model() snake_case_ = self.get_sd_image(lowercase_ , shape=(3, 4, 64, 64) ) with torch.no_grad(): snake_case_ = model.decode(lowercase_ ).sample assert list(sample.shape ) == [3, 3, 512, 512] snake_case_ = sample[-1, -2:, :2, -2:].flatten().cpu() snake_case_ = torch.tensor(lowercase_ ) assert torch_all_close(lowercase_ , lowercase_ , atol=1e-3 ) @parameterized.expand( [ # fmt: off [27, [-0.0369, 0.0207, -0.0776, -0.0682, -0.1747, -0.1930, -0.1465, -0.2039]], [16, [-0.1628, -0.2134, -0.2747, -0.2642, -0.3774, -0.4404, -0.3687, -0.4277]], # fmt: on ] ) @require_torch_gpu def A_ ( self : Union[str, Any] , lowercase_ : Any , lowercase_ : Optional[Any] ): snake_case_ = self.get_sd_vae_model(fpaa=lowercase_ ) snake_case_ = self.get_sd_image(lowercase_ , shape=(3, 4, 64, 64) , fpaa=lowercase_ ) with torch.no_grad(): snake_case_ = model.decode(lowercase_ ).sample assert list(sample.shape ) == [3, 3, 512, 512] snake_case_ = sample[-1, -2:, :2, -2:].flatten().float().cpu() snake_case_ = torch.tensor(lowercase_ ) assert torch_all_close(lowercase_ , lowercase_ , atol=5e-3 ) @parameterized.expand([(13,), (16,), (27,)] ) @require_torch_gpu @unittest.skipIf(not is_xformers_available() , reason='''xformers is not required when using PyTorch 2.0.''' ) def A_ ( self : Optional[Any] , lowercase_ : List[str] ): snake_case_ = self.get_sd_vae_model(fpaa=lowercase_ ) snake_case_ = self.get_sd_image(lowercase_ , shape=(3, 4, 64, 64) , fpaa=lowercase_ ) with torch.no_grad(): snake_case_ = model.decode(lowercase_ ).sample model.enable_xformers_memory_efficient_attention() with torch.no_grad(): snake_case_ = model.decode(lowercase_ ).sample assert list(sample.shape ) == [3, 3, 512, 512] assert torch_all_close(lowercase_ , lowercase_ , atol=1e-1 ) @parameterized.expand([(13,), (16,), (37,)] ) @require_torch_gpu @unittest.skipIf(not is_xformers_available() , reason='''xformers is not required when using PyTorch 2.0.''' ) def A_ ( self : Optional[Any] , lowercase_ : Any ): snake_case_ = self.get_sd_vae_model() snake_case_ = self.get_sd_image(lowercase_ , shape=(3, 4, 64, 64) ) with torch.no_grad(): snake_case_ = model.decode(lowercase_ ).sample model.enable_xformers_memory_efficient_attention() with torch.no_grad(): snake_case_ = model.decode(lowercase_ ).sample assert list(sample.shape ) == [3, 3, 512, 512] assert torch_all_close(lowercase_ , lowercase_ , atol=1e-2 ) @parameterized.expand( [ # fmt: off [33, [-0.3001, 0.0918, -2.6984, -3.9720, -3.2099, -5.0353, 1.7338, -0.2065, 3.4267]], [47, [-1.5030, -4.3871, -6.0355, -9.1157, -1.6661, -2.7853, 2.1607, -5.0823, 2.5633]], # fmt: on ] ) def A_ ( self : str , lowercase_ : Optional[int] , lowercase_ : Tuple ): snake_case_ = self.get_sd_vae_model() snake_case_ = self.get_sd_image(lowercase_ ) snake_case_ = self.get_generator(lowercase_ ) with torch.no_grad(): snake_case_ = model.encode(lowercase_ ).latent_dist snake_case_ = dist.sample(generator=lowercase_ ) assert list(sample.shape ) == [image.shape[0], 4] + [i // 8 for i in image.shape[2:]] snake_case_ = sample[0, -1, -3:, -3:].flatten().cpu() snake_case_ = torch.tensor(lowercase_ ) snake_case_ = 3e-3 if torch_device != '''mps''' else 1e-2 assert torch_all_close(lowercase_ , lowercase_ , atol=lowercase_ )
56
from typing import Dict, List, Optional, Tuple, Union import torch from ...models import AutoencoderKL, TransformeraDModel from ...schedulers import KarrasDiffusionSchedulers from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class __UpperCamelCase ( lowerCAmelCase__ ): """simple docstring""" def __init__( self : List[Any] , _A : TransformeraDModel , _A : AutoencoderKL , _A : KarrasDiffusionSchedulers , _A : Optional[Dict[int, str]] = None , ): """simple docstring""" super().__init__() self.register_modules(transformer=_A , vae=_A , scheduler=_A ) # create a imagenet -> id dictionary for easier use __SCREAMING_SNAKE_CASE : Optional[int] = {} if idalabel is not None: for key, value in idalabel.items(): for label in value.split(''',''' ): __SCREAMING_SNAKE_CASE : Optional[Any] = int(_A ) __SCREAMING_SNAKE_CASE : List[str] = dict(sorted(self.labels.items() ) ) def UpperCAmelCase__ ( self : List[Any] , _A : Union[str, List[str]] ): """simple docstring""" if not isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : Union[str, Any] = list(_A ) for l in label: if l not in self.labels: raise ValueError( F'''{l} does not exist. Please make sure to select one of the following labels: \n {self.labels}.''' ) return [self.labels[l] for l in label] @torch.no_grad() def __call__( self : Dict , _A : List[int] , _A : float = 4.0 , _A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _A : int = 50 , _A : Optional[str] = "pil" , _A : bool = True , ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = len(_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = self.transformer.config.sample_size __SCREAMING_SNAKE_CASE : List[Any] = self.transformer.config.in_channels __SCREAMING_SNAKE_CASE : Optional[int] = randn_tensor( shape=(batch_size, latent_channels, latent_size, latent_size) , generator=_A , device=self.device , dtype=self.transformer.dtype , ) __SCREAMING_SNAKE_CASE : Tuple = torch.cat([latents] * 2 ) if guidance_scale > 1 else latents __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor(_A , device=self.device ).reshape(-1 ) __SCREAMING_SNAKE_CASE : Any = torch.tensor([1000] * batch_size , device=self.device ) __SCREAMING_SNAKE_CASE : Any = torch.cat([class_labels, class_null] , 0 ) if guidance_scale > 1 else class_labels # set step values self.scheduler.set_timesteps(_A ) for t in self.progress_bar(self.scheduler.timesteps ): if guidance_scale > 1: __SCREAMING_SNAKE_CASE : Optional[Any] = latent_model_input[: len(_A ) // 2] __SCREAMING_SNAKE_CASE : List[Any] = torch.cat([half, half] , dim=0 ) __SCREAMING_SNAKE_CASE : int = self.scheduler.scale_model_input(_A , _A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = t if not torch.is_tensor(_A ): # TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can # This would be a good case for the `match` statement (Python 3.10+) __SCREAMING_SNAKE_CASE : Any = latent_model_input.device.type == '''mps''' if isinstance(_A , _A ): __SCREAMING_SNAKE_CASE : List[Any] = torch.floataa if is_mps else torch.floataa else: __SCREAMING_SNAKE_CASE : int = torch.intaa if is_mps else torch.intaa __SCREAMING_SNAKE_CASE : int = torch.tensor([timesteps] , dtype=_A , device=latent_model_input.device ) elif len(timesteps.shape ) == 0: __SCREAMING_SNAKE_CASE : Optional[Any] = timesteps[None].to(latent_model_input.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML __SCREAMING_SNAKE_CASE : Optional[int] = timesteps.expand(latent_model_input.shape[0] ) # predict noise model_output __SCREAMING_SNAKE_CASE : Union[str, Any] = self.transformer( _A , timestep=_A , class_labels=_A ).sample # perform guidance if guidance_scale > 1: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = noise_pred[:, :latent_channels], noise_pred[:, latent_channels:] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = torch.split(_A , len(_A ) // 2 , dim=0 ) __SCREAMING_SNAKE_CASE : str = uncond_eps + guidance_scale * (cond_eps - uncond_eps) __SCREAMING_SNAKE_CASE : List[Any] = torch.cat([half_eps, half_eps] , dim=0 ) __SCREAMING_SNAKE_CASE : List[str] = torch.cat([eps, rest] , dim=1 ) # learned sigma if self.transformer.config.out_channels // 2 == latent_channels: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = torch.split(_A , _A , dim=1 ) else: __SCREAMING_SNAKE_CASE : List[Any] = noise_pred # compute previous image: x_t -> x_t-1 __SCREAMING_SNAKE_CASE : str = self.scheduler.step(_A , _A , _A ).prev_sample if guidance_scale > 1: __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Optional[int] = latent_model_input.chunk(2 , dim=0 ) else: __SCREAMING_SNAKE_CASE : Optional[Any] = latent_model_input __SCREAMING_SNAKE_CASE : List[Any] = 1 / self.vae.config.scaling_factor * latents __SCREAMING_SNAKE_CASE : List[str] = self.vae.decode(_A ).sample __SCREAMING_SNAKE_CASE : Any = (samples / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 __SCREAMING_SNAKE_CASE : int = samples.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": __SCREAMING_SNAKE_CASE : str = self.numpy_to_pil(_A ) if not return_dict: return (samples,) return ImagePipelineOutput(images=_A )
303
0
"""simple docstring""" from __future__ import annotations import time import numpy as np A : Union[str, Any] = [8, 5, 9, 7] A : Dict = [ [2, 0, 1, 1], [0, 1, 2, 1], [4, 0, 0, 3], [0, 2, 1, 0], [1, 0, 3, 0], ] A : Optional[Any] = [ [3, 2, 1, 4], [0, 2, 5, 2], [5, 1, 0, 5], [1, 5, 3, 0], [3, 0, 3, 3], ] class _UpperCamelCase : '''simple docstring''' def __init__( self , __a , __a , __a , ): __lowerCAmelCase = claim_vector __lowerCAmelCase = allocated_resources_table __lowerCAmelCase = maximum_claim_table def snake_case ( self ): return [ sum(p_item[i] for p_item in self.__allocated_resources_table ) for i in range(len(self.__allocated_resources_table[0] ) ) ] def snake_case ( self ): return np.array(self.__claim_vector ) - np.array( self.__processes_resource_summation() ) def snake_case ( self ): return [ list(np.array(self.__maximum_claim_table[i] ) - np.array(__a ) ) for i, allocated_resource in enumerate(self.__allocated_resources_table ) ] def snake_case ( self ): return {self.__need().index(__a ): i for i in self.__need()} def snake_case ( self , **__a ): __lowerCAmelCase = self.__need() __lowerCAmelCase = self.__allocated_resources_table __lowerCAmelCase = self.__available_resources() __lowerCAmelCase = self.__need_index_manager() for kw, val in kwargs.items(): if kw and val is True: self.__pretty_data() print("_" * 50 + "\n" ) while need_list: __lowerCAmelCase = False for each_need in need_list: __lowerCAmelCase = True for index, need in enumerate(__a ): if need > available_resources[index]: __lowerCAmelCase = False break if execution: __lowerCAmelCase = True # get the original index of the process from ind_ctrl db for original_need_index, need_clone in need_index_manager.items(): if each_need == need_clone: __lowerCAmelCase = original_need_index print(f"Process {process_number + 1} is executing." ) # remove the process run from stack need_list.remove(__a ) # update available/freed resources stack __lowerCAmelCase = np.array(__a ) + np.array( alloc_resources_table[process_number] ) print( "Updated available resource stack for processes: " + " ".join([str(__a ) for x in available_resources] ) ) break if safe: print("The process is in a safe state.\n" ) else: print("System in unsafe state. Aborting...\n" ) break def snake_case ( self ): print(" " * 9 + "Allocated Resource Table" ) for item in self.__allocated_resources_table: print( f"P{self.__allocated_resources_table.index(__a ) + 1}" + " ".join(f"{it:>8}" for it in item ) + "\n" ) print(" " * 9 + "System Resource Table" ) for item in self.__maximum_claim_table: print( f"P{self.__maximum_claim_table.index(__a ) + 1}" + " ".join(f"{it:>8}" for it in item ) + "\n" ) print( "Current Usage by Active Processes: " + " ".join(str(__a ) for x in self.__claim_vector ) ) print( "Initial Available Resources: " + " ".join(str(__a ) for x in self.__available_resources() ) ) time.sleep(1 ) if __name__ == "__main__": import doctest doctest.testmod()
57
import os import sys lowercase_ = os.path.join(os.path.dirname(__file__), """src""") sys.path.append(SRC_DIR) from transformers import ( AutoConfig, AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoTokenizer, add_start_docstrings, ) lowercase_ = [ """torch""", """numpy""", """tokenizers""", """filelock""", """requests""", """tqdm""", """regex""", """sentencepiece""", """sacremoses""", """importlib_metadata""", """huggingface_hub""", ] @add_start_docstrings(AutoConfig.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoConfig.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoTokenizer.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoTokenizer.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModel.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModel.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForCausalLM.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForCausalLM.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForMaskedLM.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForMaskedLM.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForSequenceClassification.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForSequenceClassification.from_pretrained(*snake_case , **snake_case ) @add_start_docstrings(AutoModelForQuestionAnswering.__doc__ ) def a__ ( *snake_case , **snake_case ): """simple docstring""" return AutoModelForQuestionAnswering.from_pretrained(*snake_case , **snake_case )
303
0
'''simple docstring''' import argparse import os import torch from transformers.utils import WEIGHTS_NAME lowercase_ = ["""small""", """medium""", """large"""] lowercase_ = """lm_head.decoder.weight""" lowercase_ = """lm_head.weight""" def lowerCamelCase ( __lowerCamelCase : str , __lowerCamelCase : str ) ->Any: _SCREAMING_SNAKE_CASE = torch.load(__lowerCamelCase ) _SCREAMING_SNAKE_CASE = d.pop(__lowerCamelCase ) os.makedirs(__lowerCamelCase , exist_ok=__lowerCamelCase ) torch.save(__lowerCamelCase , os.path.join(__lowerCamelCase , __lowerCamelCase ) ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument("""--dialogpt_path""", default=""".""", type=str) lowercase_ = parser.parse_args() for MODEL in DIALOGPT_MODELS: lowercase_ = os.path.join(args.dialogpt_path, f"""{MODEL}_ft.pkl""") lowercase_ = f"""./DialoGPT-{MODEL}""" convert_dialogpt_checkpoint( checkpoint_path, pytorch_dump_folder_path, )
58
from __future__ import annotations import numpy as np def a__ ( snake_case ): """simple docstring""" return np.maximum(0 , snake_case ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
303
0
import string # frequency taken from https://en.wikipedia.org/wiki/Letter_frequency __lowerCamelCase = { """E""": 12.70, """T""": 9.06, """A""": 8.17, """O""": 7.51, """I""": 6.97, """N""": 6.75, """S""": 6.33, """H""": 6.09, """R""": 5.99, """D""": 4.25, """L""": 4.03, """C""": 2.78, """U""": 2.76, """M""": 2.41, """W""": 2.36, """F""": 2.23, """G""": 2.02, """Y""": 1.97, """P""": 1.93, """B""": 1.29, """V""": 0.98, """K""": 0.77, """J""": 0.15, """X""": 0.15, """Q""": 0.10, """Z""": 0.07, } __lowerCamelCase = """ETAOINSHRDLCUMWFGYPBVKJXQZ""" __lowerCamelCase = """ABCDEFGHIJKLMNOPQRSTUVWXYZ""" def UpperCamelCase ( __lowerCamelCase : str ): snake_case : Any = {letter: 0 for letter in string.ascii_uppercase} for letter in message.upper(): if letter in LETTERS: letter_count[letter] += 1 return letter_count def UpperCamelCase ( __lowerCamelCase : tuple ): return x[0] def UpperCamelCase ( __lowerCamelCase : str ): snake_case : List[Any] = get_letter_count(__lowerCamelCase ) snake_case : dict[int, list[str]] = { freq: [] for letter, freq in letter_to_freq.items() } for letter in LETTERS: freq_to_letter[letter_to_freq[letter]].append(__lowerCamelCase ) snake_case : dict[int, str] = {} for freq in freq_to_letter: freq_to_letter[freq].sort(key=ETAOIN.find , reverse=__lowerCamelCase ) snake_case : Optional[Any] = "".join(freq_to_letter[freq] ) snake_case : Any = list(freq_to_letter_str.items() ) freq_pairs.sort(key=__lowerCamelCase , reverse=__lowerCamelCase ) snake_case : list[str] = [freq_pair[1] for freq_pair in freq_pairs] return "".join(__lowerCamelCase ) def UpperCamelCase ( __lowerCamelCase : str ): snake_case : Dict = get_frequency_order(__lowerCamelCase ) snake_case : List[Any] = 0 for common_letter in ETAOIN[:6]: if common_letter in freq_order[:6]: match_score += 1 for uncommon_letter in ETAOIN[-6:]: if uncommon_letter in freq_order[-6:]: match_score += 1 return match_score if __name__ == "__main__": import doctest doctest.testmod()
59
def a__ ( snake_case = 1_000_000 ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = 1 __SCREAMING_SNAKE_CASE : Optional[Any] = 1 __SCREAMING_SNAKE_CASE : Optional[int] = {1: 1} for inputa in range(2 , snake_case ): __SCREAMING_SNAKE_CASE : Tuple = 0 __SCREAMING_SNAKE_CASE : Optional[Any] = inputa while True: if number in counters: counter += counters[number] break if number % 2 == 0: number //= 2 counter += 1 else: __SCREAMING_SNAKE_CASE : List[Any] = (3 * number) + 1 counter += 1 if inputa not in counters: __SCREAMING_SNAKE_CASE : str = counter if counter > pre_counter: __SCREAMING_SNAKE_CASE : Optional[int] = inputa __SCREAMING_SNAKE_CASE : str = counter return largest_number if __name__ == "__main__": print(solution(int(input().strip())))
303
0
"""simple docstring""" # # This a `torch.distributed` diagnostics script that checks that all GPUs in the cluster (one or # many nodes) can talk to each other via nccl and allocate gpu memory. # # To run first adjust the number of processes and nodes: # # python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py # # You may need to add --master_addr $MASTER_ADDR --master_port $MASTER_PORT if using a custom addr:port # # You can also use the rdzv API: --rdzv_endpoint $MASTER_ADDR:$MASTER_PORT --rdzv_backend c10d # # use torch.distributed.launch instead of torch.distributed.run for torch < 1.9 # # If you get a hanging in `barrier` calls you have some network issues, you may try to debug this with: # # NCCL_DEBUG=INFO python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py # # which should tell you what's going on behind the scenes. # # # This script can be run via `srun` in the SLURM environment as well. Here is a SLURM script that # runs on 2 nodes of 4 gpus per node: # # #SBATCH --job-name=test-nodes # name # #SBATCH --nodes=2 # nodes # #SBATCH --ntasks-per-node=1 # crucial - only 1 task per dist per node! # #SBATCH --cpus-per-task=10 # number of cores per tasks # #SBATCH --gres=gpu:4 # number of gpus # #SBATCH --time 0:05:00 # maximum execution time (HH:MM:SS) # #SBATCH --output=%x-%j.out # output file name # # GPUS_PER_NODE=4 # MASTER_ADDR=$(scontrol show hostnames $SLURM_JOB_NODELIST | head -n 1) # MASTER_PORT=6000 # # srun --jobid $SLURM_JOBID bash -c 'python -m torch.distributed.run \ # --nproc_per_node $GPUS_PER_NODE --nnodes $SLURM_NNODES --node_rank $SLURM_PROCID \ # --master_addr $MASTER_ADDR --master_port $MASTER_PORT \ # torch-distributed-gpu-test.py' # import fcntl import os import socket import torch import torch.distributed as dist def _snake_case ( *_snake_case : Dict ): with open(_snake_case , '''r''' ) as fh: fcntl.flock(_snake_case , fcntl.LOCK_EX ) try: print(*_snake_case ) finally: fcntl.flock(_snake_case , fcntl.LOCK_UN ) snake_case__ : str = int(os.environ['''LOCAL_RANK''']) torch.cuda.set_device(local_rank) snake_case__ : int = torch.device('''cuda''', local_rank) snake_case__ : Union[str, Any] = socket.gethostname() snake_case__ : str = f"""[{hostname}-{local_rank}]""" try: # test distributed dist.init_process_group('''nccl''') dist.all_reduce(torch.ones(1).to(device), op=dist.ReduceOp.SUM) dist.barrier() # test cuda is available and can allocate memory torch.cuda.is_available() torch.ones(1).cuda(local_rank) # global rank snake_case__ : List[Any] = dist.get_rank() snake_case__ : List[str] = dist.get_world_size() printflock(f"""{gpu} is OK (global rank: {rank}/{world_size})""") dist.barrier() if rank == 0: printflock(f"""pt={torch.__version__}, cuda={torch.version.cuda}, nccl={torch.cuda.nccl.version()}""") except Exception: printflock(f"""{gpu} is broken""") raise
60
import argparse import json import os import pickle import shutil import numpy as np import torch from distiller import Distiller from lm_seqs_dataset import LmSeqsDataset from transformers import ( BertConfig, BertForMaskedLM, BertTokenizer, DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer, GPTaConfig, GPTaLMHeadModel, GPTaTokenizer, RobertaConfig, RobertaForMaskedLM, RobertaTokenizer, ) from utils import git_log, init_gpu_params, logger, set_seed lowercase_ = { """distilbert""": (DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer), """roberta""": (RobertaConfig, RobertaForMaskedLM, RobertaTokenizer), """bert""": (BertConfig, BertForMaskedLM, BertTokenizer), """gpt2""": (GPTaConfig, GPTaLMHeadModel, GPTaTokenizer), } def a__ ( snake_case ): """simple docstring""" assert (args.mlm and args.alpha_mlm > 0.0) or (not args.mlm and args.alpha_mlm == 0.0) assert (args.alpha_mlm > 0.0 and args.alpha_clm == 0.0) or (args.alpha_mlm == 0.0 and args.alpha_clm > 0.0) if args.mlm: assert os.path.isfile(args.token_counts ) assert (args.student_type in ["roberta", "distilbert"]) and (args.teacher_type in ["roberta", "bert"]) else: assert (args.student_type in ["gpt2"]) and (args.teacher_type in ["gpt2"]) assert args.teacher_type == args.student_type or ( args.student_type == "distilbert" and args.teacher_type == "bert" ) assert os.path.isfile(args.student_config ) if args.student_pretrained_weights is not None: assert os.path.isfile(args.student_pretrained_weights ) if args.freeze_token_type_embds: assert args.student_type in ["roberta"] assert args.alpha_ce >= 0.0 assert args.alpha_mlm >= 0.0 assert args.alpha_clm >= 0.0 assert args.alpha_mse >= 0.0 assert args.alpha_cos >= 0.0 assert args.alpha_ce + args.alpha_mlm + args.alpha_clm + args.alpha_mse + args.alpha_cos > 0.0 def a__ ( snake_case , snake_case ): """simple docstring""" if args.student_type == "roberta": __SCREAMING_SNAKE_CASE : int = False elif args.student_type == "gpt2": __SCREAMING_SNAKE_CASE : Optional[int] = False def a__ ( snake_case , snake_case ): """simple docstring""" if args.student_type == "roberta": __SCREAMING_SNAKE_CASE : Dict = False def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = argparse.ArgumentParser(description='''Training''' ) parser.add_argument('''--force''' , action='''store_true''' , help='''Overwrite dump_path if it already exists.''' ) parser.add_argument( '''--dump_path''' , type=snake_case , required=snake_case , help='''The output directory (log, checkpoints, parameters, etc.)''' ) parser.add_argument( '''--data_file''' , type=snake_case , required=snake_case , help='''The binarized file (tokenized + tokens_to_ids) and grouped by sequence.''' , ) parser.add_argument( '''--student_type''' , type=snake_case , choices=['''distilbert''', '''roberta''', '''gpt2'''] , required=snake_case , help='''The student type (DistilBERT, RoBERTa).''' , ) parser.add_argument('''--student_config''' , type=snake_case , required=snake_case , help='''Path to the student configuration.''' ) parser.add_argument( '''--student_pretrained_weights''' , default=snake_case , type=snake_case , help='''Load student initialization checkpoint.''' ) parser.add_argument( '''--teacher_type''' , choices=['''bert''', '''roberta''', '''gpt2'''] , required=snake_case , help='''Teacher type (BERT, RoBERTa).''' ) parser.add_argument('''--teacher_name''' , type=snake_case , required=snake_case , help='''The teacher model.''' ) parser.add_argument('''--temperature''' , default=2.0 , type=snake_case , help='''Temperature for the softmax temperature.''' ) parser.add_argument( '''--alpha_ce''' , default=0.5 , type=snake_case , help='''Linear weight for the distillation loss. Must be >=0.''' ) parser.add_argument( '''--alpha_mlm''' , default=0.0 , type=snake_case , help='''Linear weight for the MLM loss. Must be >=0. Should be used in conjunction with `mlm` flag.''' , ) parser.add_argument('''--alpha_clm''' , default=0.5 , type=snake_case , help='''Linear weight for the CLM loss. Must be >=0.''' ) parser.add_argument('''--alpha_mse''' , default=0.0 , type=snake_case , help='''Linear weight of the MSE loss. Must be >=0.''' ) parser.add_argument( '''--alpha_cos''' , default=0.0 , type=snake_case , help='''Linear weight of the cosine embedding loss. Must be >=0.''' ) parser.add_argument( '''--mlm''' , action='''store_true''' , help='''The LM step: MLM or CLM. If `mlm` is True, the MLM is used over CLM.''' ) parser.add_argument( '''--mlm_mask_prop''' , default=0.15 , type=snake_case , help='''Proportion of tokens for which we need to make a prediction.''' , ) parser.add_argument('''--word_mask''' , default=0.8 , type=snake_case , help='''Proportion of tokens to mask out.''' ) parser.add_argument('''--word_keep''' , default=0.1 , type=snake_case , help='''Proportion of tokens to keep.''' ) parser.add_argument('''--word_rand''' , default=0.1 , type=snake_case , help='''Proportion of tokens to randomly replace.''' ) parser.add_argument( '''--mlm_smoothing''' , default=0.7 , type=snake_case , help='''Smoothing parameter to emphasize more rare tokens (see XLM, similar to word2vec).''' , ) parser.add_argument('''--token_counts''' , type=snake_case , help='''The token counts in the data_file for MLM.''' ) parser.add_argument( '''--restrict_ce_to_mask''' , action='''store_true''' , help='''If true, compute the distillation loss only the [MLM] prediction distribution.''' , ) parser.add_argument( '''--freeze_pos_embs''' , action='''store_true''' , help='''Freeze positional embeddings during distillation. For student_type in [\'roberta\', \'gpt2\'] only.''' , ) parser.add_argument( '''--freeze_token_type_embds''' , action='''store_true''' , help='''Freeze token type embeddings during distillation if existent. For student_type in [\'roberta\'] only.''' , ) parser.add_argument('''--n_epoch''' , type=snake_case , default=3 , help='''Number of pass on the whole dataset.''' ) parser.add_argument('''--batch_size''' , type=snake_case , default=5 , help='''Batch size (for each process).''' ) parser.add_argument( '''--group_by_size''' , action='''store_false''' , help='''If true, group sequences that have similar length into the same batch. Default is true.''' , ) parser.add_argument( '''--gradient_accumulation_steps''' , type=snake_case , default=50 , help='''Gradient accumulation for larger training batches.''' , ) parser.add_argument('''--warmup_prop''' , default=0.05 , type=snake_case , help='''Linear warmup proportion.''' ) parser.add_argument('''--weight_decay''' , default=0.0 , type=snake_case , help='''Weight decay if we apply some.''' ) parser.add_argument('''--learning_rate''' , default=5E-4 , type=snake_case , help='''The initial learning rate for Adam.''' ) parser.add_argument('''--adam_epsilon''' , default=1E-6 , type=snake_case , help='''Epsilon for Adam optimizer.''' ) parser.add_argument('''--max_grad_norm''' , default=5.0 , type=snake_case , help='''Max gradient norm.''' ) parser.add_argument('''--initializer_range''' , default=0.02 , type=snake_case , help='''Random initialization range.''' ) parser.add_argument( '''--fp16''' , action='''store_true''' , help='''Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit''' , ) parser.add_argument( '''--fp16_opt_level''' , type=snake_case , default='''O1''' , help=( '''For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\'].''' '''See details at https://nvidia.github.io/apex/amp.html''' ) , ) parser.add_argument('''--n_gpu''' , type=snake_case , default=1 , help='''Number of GPUs in the node.''' ) parser.add_argument('''--local_rank''' , type=snake_case , default=-1 , help='''Distributed training - Local rank''' ) parser.add_argument('''--seed''' , type=snake_case , default=56 , help='''Random seed''' ) parser.add_argument('''--log_interval''' , type=snake_case , default=500 , help='''Tensorboard logging interval.''' ) parser.add_argument('''--checkpoint_interval''' , type=snake_case , default=4_000 , help='''Checkpoint interval.''' ) __SCREAMING_SNAKE_CASE : Optional[int] = parser.parse_args() sanity_checks(snake_case ) # ARGS # init_gpu_params(snake_case ) set_seed(snake_case ) if args.is_master: if os.path.exists(args.dump_path ): if not args.force: raise ValueError( F'''Serialization dir {args.dump_path} already exists, but you have not precised wheter to overwrite''' ''' itUse `--force` if you want to overwrite it''' ) else: shutil.rmtree(args.dump_path ) if not os.path.exists(args.dump_path ): os.makedirs(args.dump_path ) logger.info(F'''Experiment will be dumped and logged in {args.dump_path}''' ) # SAVE PARAMS # logger.info(F'''Param: {args}''' ) with open(os.path.join(args.dump_path , '''parameters.json''' ) , '''w''' ) as f: json.dump(vars(snake_case ) , snake_case , indent=4 ) git_log(args.dump_path ) __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : str = MODEL_CLASSES[args.student_type] __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE : Tuple = MODEL_CLASSES[args.teacher_type] # TOKENIZER # __SCREAMING_SNAKE_CASE : Optional[int] = teacher_tokenizer_class.from_pretrained(args.teacher_name ) __SCREAMING_SNAKE_CASE : Optional[Any] = {} for tok_name, tok_symbol in tokenizer.special_tokens_map.items(): __SCREAMING_SNAKE_CASE : Any = tokenizer.all_special_tokens.index(snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.all_special_ids[idx] logger.info(F'''Special tokens {special_tok_ids}''' ) __SCREAMING_SNAKE_CASE : Any = special_tok_ids __SCREAMING_SNAKE_CASE : List[Any] = tokenizer.max_model_input_sizes[args.teacher_name] # DATA LOADER # logger.info(F'''Loading data from {args.data_file}''' ) with open(args.data_file , '''rb''' ) as fp: __SCREAMING_SNAKE_CASE : List[str] = pickle.load(snake_case ) if args.mlm: logger.info(F'''Loading token counts from {args.token_counts} (already pre-computed)''' ) with open(args.token_counts , '''rb''' ) as fp: __SCREAMING_SNAKE_CASE : Optional[Any] = pickle.load(snake_case ) __SCREAMING_SNAKE_CASE : List[Any] = np.maximum(snake_case , 1 ) ** -args.mlm_smoothing for idx in special_tok_ids.values(): __SCREAMING_SNAKE_CASE : Any = 0.0 # do not predict special tokens __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.from_numpy(snake_case ) else: __SCREAMING_SNAKE_CASE : Optional[int] = None __SCREAMING_SNAKE_CASE : Optional[Any] = LmSeqsDataset(params=snake_case , data=snake_case ) logger.info('''Data loader created.''' ) # STUDENT # logger.info(F'''Loading student config from {args.student_config}''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = student_config_class.from_pretrained(args.student_config ) __SCREAMING_SNAKE_CASE : Dict = True if args.student_pretrained_weights is not None: logger.info(F'''Loading pretrained weights from {args.student_pretrained_weights}''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = student_model_class.from_pretrained(args.student_pretrained_weights , config=snake_case ) else: __SCREAMING_SNAKE_CASE : str = student_model_class(snake_case ) if args.n_gpu > 0: student.to(F'''cuda:{args.local_rank}''' ) logger.info('''Student loaded.''' ) # TEACHER # __SCREAMING_SNAKE_CASE : List[str] = teacher_model_class.from_pretrained(args.teacher_name , output_hidden_states=snake_case ) if args.n_gpu > 0: teacher.to(F'''cuda:{args.local_rank}''' ) logger.info(F'''Teacher loaded from {args.teacher_name}.''' ) # FREEZING # if args.freeze_pos_embs: freeze_pos_embeddings(snake_case , snake_case ) if args.freeze_token_type_embds: freeze_token_type_embeddings(snake_case , snake_case ) # SANITY CHECKS # assert student.config.vocab_size == teacher.config.vocab_size assert student.config.hidden_size == teacher.config.hidden_size assert student.config.max_position_embeddings == teacher.config.max_position_embeddings if args.mlm: assert token_probs.size(0 ) == stu_architecture_config.vocab_size # DISTILLER # torch.cuda.empty_cache() __SCREAMING_SNAKE_CASE : int = Distiller( params=snake_case , dataset=snake_case , token_probs=snake_case , student=snake_case , teacher=snake_case ) distiller.train() logger.info('''Let\'s go get some drinks.''' ) if __name__ == "__main__": main()
303
0