code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
import unittest from transformers import JukeboxTokenizer from transformers.testing_utils import require_torch class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = JukeboxTokenizer _lowerCAmelCase = { "artist": "Zac Brown Band", "genres": "Country", "lyrics": "I met a traveller from an antique land,\n Who said \"Two vast and trunkless legs of stone\n Stand in the desert. . . . Near them, on the sand,\n Half sunk a shattered visage lies, whose frown,\n And wrinkled lip, and sneer of cold command,\n Tell that its sculptor well those passions read\n Which yet survive, stamped on these lifeless things,\n The hand that mocked them, and the heart that fed;\n And on the pedestal, these words appear:\n My name is Ozymandias, King of Kings;\n Look on my Works, ye Mighty, and despair!\n Nothing beside remains. Round the decay\n Of that colossal Wreck, boundless and bare\n The lone and level sands stretch far away\n ", } @require_torch def __snake_case ( self ): import torch A__ : List[str] = JukeboxTokenizer.from_pretrained('''openai/jukebox-1b-lyrics''' ) A__ : List[Any] = tokenizer(**self.metas )['''input_ids'''] # fmt: off A__ : List[Any] = [ torch.tensor([[ 0, 0, 0, 7169, 507, 9, 76, 39, 31, 46, 76, 27, 76, 46, 44, 27, 48, 31, 38, 38, 31, 44, 76, 32, 44, 41, 39, 76, 27, 40, 76, 27, 40, 46, 35, 43, 47, 31, 76, 38, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 41, 76, 45, 27, 35, 30, 76, 71, 20, 49, 41, 76, 48, 27, 45, 46, 76, 27, 40, 30, 76, 46, 44, 47, 40, 37, 38, 31, 45, 45, 76, 38, 31, 33, 45, 76, 41, 32, 76, 45, 46, 41, 40, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 19, 46, 27, 40, 30, 76, 35, 40, 76, 46, 34, 31, 76, 30, 31, 45, 31, 44, 46, 63, 76, 63, 76, 63, 76, 63, 76, 14, 31, 27, 44, 76, 46, 34, 31, 39, 64, 76, 41, 40, 76, 46, 34, 31, 76, 45, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 8, 27, 38, 32, 76, 45, 47, 40, 37, 76, 27, 76, 45, 34, 27, 46, 46, 31, 44, 31, 30, 76, 48, 35, 45, 27, 33, 31, 76, 38, 35, 31, 45, 64, 76, 49, 34, 41, 45, 31, 76, 32, 44, 41, 49, 40, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 49, 44, 35, 40, 37, 38, 31, 30, 76, 38, 35, 42, 64, 76, 27, 40, 30, 76, 45, 40, 31, 31, 44, 76, 41, 32, 76, 29, 41, 38, 30, 76, 29, 41, 39, 39, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 31, 38, 38, 76, 46, 34, 27, 46, 76, 35, 46, 45, 76, 45, 29, 47, 38, 42, 46, 41, 44, 76, 49, 31, 38, 38, 76, 46, 34, 41, 45, 31, 76, 42, 27, 45, 45, 35, 41, 40, 45, 76, 44, 31, 27, 30, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 35, 29, 34, 76, 51, 31, 46, 76, 45, 47, 44, 48, 35, 48, 31, 64, 76, 45, 46, 27, 39, 42, 31, 30, 76, 41, 40, 76, 46, 34, 31, 45, 31, 76, 38, 35, 32, 31, 38, 31, 45, 45, 76, 46, 34, 35, 40, 33, 45, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 34, 27, 40, 30, 76, 46, 34, 27, 46, 76, 39, 41, 29, 37, 31, 30, 76, 46, 34, 31, 39, 64, 76, 27, 40, 30, 76, 46, 34, 31, 76, 34, 31, 27, 44, 46, 76, 46, 34, 27, 46, 76, 32, 31, 30, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 41, 40, 76, 46, 34, 31, 76, 42, 31, 30, 31, 45, 46, 27, 38, 64, 76, 46, 34, 31, 45, 31, 76, 49, 41, 44, 30, 45, 76, 27, 42, 42, 31, 27, 44, 65, 78, 76, 76, 76, 76, 76, 76, 76, 76, 13, 51, 76, 40, 27, 39, 31, 76, 35, 45, 76, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 76, 11, 35, 40, 33, 76, 41, 32, 76, 11, 35, 40, 33, 45, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 12, 41, 41, 37, 76, 41, 40, 76, 39, 51, 76, 23, 41, 44, 37, 45, 64, 76, 51, 31, 76, 13, 35, 33, 34, 46, 51, 64, 76, 27, 40, 30, 76, 30, 31, 45, 42, 27, 35, 44, 67, 78, 76, 76, 76, 76, 76, 76, 76, 76, 14, 41, 46, 34, 35, 40, 33, 76, 28, 31, 45, 35, 30, 31, 76, 44, 31, 39, 27, 35, 40, 45, 63, 76, 18, 41, 47, 40, 30, 76, 46, 34, 31, 76, 30, 31, 29, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76, 15, 32, 76, 46, 34, 27, 46, 76, 29, 41, 38, 41, 45, 45, 27, 38, 76, 23, 44, 31, 29, 37, 64, 76, 28, 41, 47, 40, 30, 38, 31, 45, 45, 76, 27, 40, 30, 76, 28, 27, 44, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 38, 41, 40, 31, 76, 27, 40, 30, 76, 38, 31, 48, 31, 38, 76, 45, 27, 40, 30, 45, 76, 45, 46, 44, 31, 46, 29, 34, 76, 32, 27, 44, 76, 27, 49, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76]] ), torch.tensor([[0, 0, 0, 1069, 11]] ), torch.tensor([[0, 0, 0, 1069, 11]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) ) @require_torch def __snake_case ( self ): import torch A__ : Optional[Any] = JukeboxTokenizer.from_pretrained('''openai/jukebox-5b-lyrics''' ) A__ : Any = tokenizer(**self.metas )['''input_ids'''] # fmt: off A__ : Optional[int] = [ torch.tensor([[ 0, 0, 0, 1069, 11, -1, -1, -1, -1, 9, 77, 39, 31, 46, 77, 27, 77, 46, 44, 27, 48, 31, 38, 38, 31, 44, 77, 32, 44, 41, 39, 77, 27, 40, 77, 27, 40, 46, 35, 43, 47, 31, 77, 38, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 41, 77, 45, 27, 35, 30, 77, 72, 20, 49, 41, 77, 48, 27, 45, 46, 77, 27, 40, 30, 77, 46, 44, 47, 40, 37, 38, 31, 45, 45, 77, 38, 31, 33, 45, 77, 41, 32, 77, 45, 46, 41, 40, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 19, 46, 27, 40, 30, 77, 35, 40, 77, 46, 34, 31, 77, 30, 31, 45, 31, 44, 46, 63, 77, 63, 77, 63, 77, 63, 77, 14, 31, 27, 44, 77, 46, 34, 31, 39, 64, 77, 41, 40, 77, 46, 34, 31, 77, 45, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 8, 27, 38, 32, 77, 45, 47, 40, 37, 77, 27, 77, 45, 34, 27, 46, 46, 31, 44, 31, 30, 77, 48, 35, 45, 27, 33, 31, 77, 38, 35, 31, 45, 64, 77, 49, 34, 41, 45, 31, 77, 32, 44, 41, 49, 40, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 49, 44, 35, 40, 37, 38, 31, 30, 77, 38, 35, 42, 64, 77, 27, 40, 30, 77, 45, 40, 31, 31, 44, 77, 41, 32, 77, 29, 41, 38, 30, 77, 29, 41, 39, 39, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 31, 38, 38, 77, 46, 34, 27, 46, 77, 35, 46, 45, 77, 45, 29, 47, 38, 42, 46, 41, 44, 77, 49, 31, 38, 38, 77, 46, 34, 41, 45, 31, 77, 42, 27, 45, 45, 35, 41, 40, 45, 77, 44, 31, 27, 30, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 35, 29, 34, 77, 51, 31, 46, 77, 45, 47, 44, 48, 35, 48, 31, 64, 77, 45, 46, 27, 39, 42, 31, 30, 77, 41, 40, 77, 46, 34, 31, 45, 31, 77, 38, 35, 32, 31, 38, 31, 45, 45, 77, 46, 34, 35, 40, 33, 45, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 34, 27, 40, 30, 77, 46, 34, 27, 46, 77, 39, 41, 29, 37, 31, 30, 77, 46, 34, 31, 39, 64, 77, 27, 40, 30, 77, 46, 34, 31, 77, 34, 31, 27, 44, 46, 77, 46, 34, 27, 46, 77, 32, 31, 30, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 41, 40, 77, 46, 34, 31, 77, 42, 31, 30, 31, 45, 46, 27, 38, 64, 77, 46, 34, 31, 45, 31, 77, 49, 41, 44, 30, 45, 77, 27, 42, 42, 31, 27, 44, 65, 79, 77, 77, 77, 77, 77, 77, 77, 77, 13, 51, 77, 40, 27, 39, 31, 77, 35, 45, 77, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 77, 11, 35, 40, 33, 77, 41, 32, 77, 11, 35, 40, 33, 45, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 12, 41, 41, 37, 77, 41, 40, 77, 39, 51, 77, 23, 41, 44, 37, 45, 64, 77, 51, 31, 77, 13, 35, 33, 34, 46, 51, 64, 77, 27, 40, 30, 77, 30, 31, 45, 42, 27, 35, 44, 67, 79, 77, 77, 77, 77, 77, 77, 77, 77, 14, 41, 46, 34, 35, 40, 33, 77, 28, 31, 45, 35, 30, 31, 77, 44, 31, 39, 27, 35, 40, 45, 63, 77, 18, 41, 47, 40, 30, 77, 46, 34, 31, 77, 30, 31, 29, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77, 15, 32, 77, 46, 34, 27, 46, 77, 29, 41, 38, 41, 45, 45, 27, 38, 77, 23, 44, 31, 29, 37, 64, 77, 28, 41, 47, 40, 30, 38, 31, 45, 45, 77, 27, 40, 30, 77, 28, 27, 44, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 38, 41, 40, 31, 77, 27, 40, 30, 77, 38, 31, 48, 31, 38, 77, 45, 27, 40, 30, 45, 77, 45, 46, 44, 31, 46, 29, 34, 77, 32, 27, 44, 77, 27, 49, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77]] ), torch.tensor([[0, 0, 0, 1069, 11, -1, -1, -1, -1]] ), torch.tensor([[0, 0, 0, 1069, 11, -1, -1, -1, -1]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) )
55
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline else: from .pipeline_kandinsky import KandinskyPipeline from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput from .text_encoder import MultilingualCLIP
55
1
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : list[int] ) -> list[int]: """simple docstring""" A__ : int = len(__UpperCamelCase ) for i in range(__UpperCamelCase ): for j in range(i + 1 , __UpperCamelCase ): if numbers[j] < numbers[i]: A__ , A__ : Optional[int] = numbers[j], numbers[i] return numbers if __name__ == "__main__": _SCREAMING_SNAKE_CASE : Optional[Any] = input('Enter numbers separated by a comma:\n').strip() _SCREAMING_SNAKE_CASE : int = [int(item) for item in user_input.split(',')] print(exchange_sort(unsorted))
55
import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to properly calculate the metrics on the # validation dataset when in a distributed system, and builds off the # `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _SCREAMING_SNAKE_CASE : str = 1_6 _SCREAMING_SNAKE_CASE : Tuple = 3_2 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Accelerator , __UpperCamelCase : int = 16 ) -> Optional[int]: """simple docstring""" A__ : List[str] = AutoTokenizer.from_pretrained('''bert-base-cased''' ) A__ : Optional[int] = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__UpperCamelCase : Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) A__ : int = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=__UpperCamelCase , max_length=__UpperCamelCase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): A__ : Optional[int] = datasets.map( __UpperCamelCase , batched=__UpperCamelCase , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library A__ : List[Any] = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__UpperCamelCase : Any ): # On TPU it's best to pad everything to the same length or training will be very slow. A__ : Optional[Any] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": A__ : int = 16 elif accelerator.mixed_precision != "no": A__ : Any = 8 else: A__ : Union[str, Any] = None return tokenizer.pad( __UpperCamelCase , padding='''longest''' , max_length=__UpperCamelCase , pad_to_multiple_of=__UpperCamelCase , return_tensors='''pt''' , ) # Instantiate dataloaders. A__ : Optional[int] = DataLoader( tokenized_datasets['''train'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) A__ : Tuple = DataLoader( tokenized_datasets['''validation'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('TESTING_MOCKED_DATALOADERS', None) == "1": from accelerate.test_utils.training import mocked_dataloaders _SCREAMING_SNAKE_CASE : Dict = mocked_dataloaders # noqa: F811 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : List[Any] ) -> Optional[Any]: """simple docstring""" if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , __UpperCamelCase ) == "1": A__ : List[str] = 2 # Initialize accelerator A__ : Optional[Any] = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs A__ : Tuple = config['''lr'''] A__ : Dict = int(config['''num_epochs'''] ) A__ : int = int(config['''seed'''] ) A__ : Optional[Any] = int(config['''batch_size'''] ) A__ : int = evaluate.load('''glue''' , '''mrpc''' ) # If the batch size is too big we use gradient accumulation A__ : Union[str, Any] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: A__ : List[Any] = batch_size // MAX_GPU_BATCH_SIZE A__ : Dict = MAX_GPU_BATCH_SIZE set_seed(__UpperCamelCase ) A__ , A__ : int = get_dataloaders(__UpperCamelCase , __UpperCamelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) A__ : Optional[int] = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=__UpperCamelCase ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). A__ : Tuple = model.to(accelerator.device ) # Instantiate optimizer A__ : Optional[int] = AdamW(params=model.parameters() , lr=__UpperCamelCase ) # Instantiate scheduler A__ : Any = get_linear_schedule_with_warmup( optimizer=__UpperCamelCase , num_warmup_steps=1_00 , num_training_steps=(len(__UpperCamelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. A__ , A__ , A__ , A__ , A__ : Dict = accelerator.prepare( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # Now we train the model for epoch in range(__UpperCamelCase ): model.train() for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) A__ : Dict = model(**__UpperCamelCase ) A__ : Dict = outputs.loss A__ : List[str] = loss / gradient_accumulation_steps accelerator.backward(__UpperCamelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() A__ : Optional[int] = 0 for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): A__ : Union[str, Any] = model(**__UpperCamelCase ) A__ : int = outputs.logits.argmax(dim=-1 ) A__ , A__ : Optional[Any] = accelerator.gather((predictions, batch['''labels''']) ) # New Code # # First we check if it's a distributed system if accelerator.use_distributed: # Then see if we're on the last batch of our eval dataloader if step == len(__UpperCamelCase ) - 1: # Last batch needs to be truncated on distributed systems as it contains additional samples A__ : Tuple = predictions[: len(eval_dataloader.dataset ) - samples_seen] A__ : int = references[: len(eval_dataloader.dataset ) - samples_seen] else: # Otherwise we add the number of samples seen samples_seen += references.shape[0] # All of this can be avoided if you use `Accelerator.gather_for_metrics` instead of `Accelerator.gather`: # accelerator.gather_for_metrics((predictions, batch["labels"])) metric.add_batch( predictions=__UpperCamelCase , references=__UpperCamelCase , ) A__ : Union[str, Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}:" , __UpperCamelCase ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: """simple docstring""" A__ : Tuple = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=__UpperCamelCase , default=__UpperCamelCase , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) A__ : Dict = parser.parse_args() A__ : Any = {'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": main()
55
1
import argparse import json import logging import os import shutil import sys import tempfile import unittest from unittest import mock import torch from accelerate.utils import write_basic_config from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device from transformers.utils import is_apex_available logging.basicConfig(level=logging.DEBUG) _SCREAMING_SNAKE_CASE : str = logging.getLogger() def SCREAMING_SNAKE_CASE ( ) -> List[Any]: """simple docstring""" A__ : Optional[Any] = argparse.ArgumentParser() parser.add_argument('''-f''' ) A__ : Dict = parser.parse_args() return args.f def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> Optional[Any]: """simple docstring""" A__ : Tuple = {} A__ : Optional[int] = os.path.join(__UpperCamelCase , '''all_results.json''' ) if os.path.exists(__UpperCamelCase ): with open(__UpperCamelCase , '''r''' ) as f: A__ : Optional[int] = json.load(__UpperCamelCase ) else: raise ValueError(F"can't find {path}" ) return results def SCREAMING_SNAKE_CASE ( ) -> Dict: """simple docstring""" A__ : str = torch.cuda.is_available() and torch_device == '''cuda''' return is_using_cuda and is_apex_available() _SCREAMING_SNAKE_CASE : Tuple = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' @classmethod def __snake_case ( cls ): # Write Accelerate config, will pick up on CPU, GPU, and multi-GPU A__ : Any = tempfile.mkdtemp() A__ : List[Any] = os.path.join(cls.tmpdir , '''default_config.yml''' ) write_basic_config(save_location=cls.configPath ) A__ : int = ['''accelerate''', '''launch''', '''--config_file''', cls.configPath] @classmethod def __snake_case ( cls ): shutil.rmtree(cls.tmpdir ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __snake_case ( self ): A__ : Dict = self.get_auto_remove_tmp_dir() A__ : Optional[int] = F"\n {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.py\n --model_name_or_path distilbert-base-uncased\n --output_dir {tmp_dir}\n --train_file ./tests/fixtures/tests_samples/MRPC/train.csv\n --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --learning_rate=1e-4\n --seed=42\n --checkpointing_steps epoch\n --with_tracking\n ".split() if is_cuda_and_apex_available(): testargs.append('''--fp16''' ) run_command(self._launch_args + testargs ) A__ : Tuple = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.7_5 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''glue_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __snake_case ( self ): A__ : List[Any] = self.get_auto_remove_tmp_dir() A__ : int = F"\n {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py\n --model_name_or_path distilgpt2\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --block_size 128\n --per_device_train_batch_size 5\n --per_device_eval_batch_size 5\n --num_train_epochs 2\n --output_dir {tmp_dir}\n --checkpointing_steps epoch\n --with_tracking\n ".split() if torch.cuda.device_count() > 1: # Skipping because there are not enough batches to train the model + would need a drop_last to work. return run_command(self._launch_args + testargs ) A__ : Optional[Any] = get_results(UpperCamelCase__ ) self.assertLess(result['''perplexity'''] , 100 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''clm_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __snake_case ( self ): A__ : Tuple = self.get_auto_remove_tmp_dir() A__ : str = F"\n {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.py\n --model_name_or_path distilroberta-base\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --output_dir {tmp_dir}\n --num_train_epochs=1\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) A__ : Optional[int] = get_results(UpperCamelCase__ ) self.assertLess(result['''perplexity'''] , 42 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''mlm_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __snake_case ( self ): # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu A__ : Optional[int] = 7 if get_gpu_count() > 1 else 2 A__ : Dict = self.get_auto_remove_tmp_dir() A__ : Dict = F"\n {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.py\n --model_name_or_path bert-base-uncased\n --train_file tests/fixtures/tests_samples/conll/sample.json\n --validation_file tests/fixtures/tests_samples/conll/sample.json\n --output_dir {tmp_dir}\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=2\n --num_train_epochs={epochs}\n --seed 7\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) A__ : str = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.7_5 ) self.assertLess(result['''train_loss'''] , 0.5 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''ner_no_trainer''' ) ) ) @unittest.skip(reason='''Fix me @muellerzr''' ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __snake_case ( self ): A__ : List[Any] = self.get_auto_remove_tmp_dir() A__ : Any = F"\n {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.py\n --model_name_or_path bert-base-uncased\n --version_2_with_negative\n --train_file tests/fixtures/tests_samples/SQUAD/sample.json\n --validation_file tests/fixtures/tests_samples/SQUAD/sample.json\n --output_dir {tmp_dir}\n --seed=42\n --max_train_steps=10\n --num_warmup_steps=2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) A__ : Tuple = get_results(UpperCamelCase__ ) # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics. self.assertGreaterEqual(result['''eval_f1'''] , 28 ) self.assertGreaterEqual(result['''eval_exact'''] , 28 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''qa_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __snake_case ( self ): A__ : Optional[int] = self.get_auto_remove_tmp_dir() A__ : Dict = F"\n {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py\n --model_name_or_path bert-base-uncased\n --train_file tests/fixtures/tests_samples/swag/sample.json\n --validation_file tests/fixtures/tests_samples/swag/sample.json\n --output_dir {tmp_dir}\n --max_train_steps=20\n --num_warmup_steps=2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) A__ : List[Any] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.8 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''swag_no_trainer''' ) ) ) @slow @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __snake_case ( self ): A__ : List[Any] = self.get_auto_remove_tmp_dir() A__ : Tuple = F"\n {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.py\n --model_name_or_path t5-small\n --train_file tests/fixtures/tests_samples/xsum/sample.json\n --validation_file tests/fixtures/tests_samples/xsum/sample.json\n --output_dir {tmp_dir}\n --max_train_steps=50\n --num_warmup_steps=8\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) A__ : Tuple = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_rouge1'''] , 10 ) self.assertGreaterEqual(result['''eval_rouge2'''] , 2 ) self.assertGreaterEqual(result['''eval_rougeL'''] , 7 ) self.assertGreaterEqual(result['''eval_rougeLsum'''] , 7 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''summarization_no_trainer''' ) ) ) @slow @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __snake_case ( self ): A__ : Any = self.get_auto_remove_tmp_dir() A__ : Optional[int] = F"\n {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py\n --model_name_or_path sshleifer/student_marian_en_ro_6_1\n --source_lang en\n --target_lang ro\n --train_file tests/fixtures/tests_samples/wmt16/sample.json\n --validation_file tests/fixtures/tests_samples/wmt16/sample.json\n --output_dir {tmp_dir}\n --max_train_steps=50\n --num_warmup_steps=8\n --num_beams=6\n --learning_rate=3e-3\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --source_lang en_XX\n --target_lang ro_RO\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) A__ : Optional[int] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_bleu'''] , 30 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''translation_no_trainer''' ) ) ) @slow def __snake_case ( self ): A__ : List[Any] = logging.StreamHandler(sys.stdout ) logger.addHandler(UpperCamelCase__ ) A__ : Optional[int] = self.get_auto_remove_tmp_dir() A__ : List[Any] = F"\n {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py\n --dataset_name huggingface/semantic-segmentation-test-sample\n --output_dir {tmp_dir}\n --max_train_steps=10\n --num_warmup_steps=2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --checkpointing_steps epoch\n ".split() run_command(self._launch_args + testargs ) A__ : Dict = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_overall_accuracy'''] , 0.1_0 ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __snake_case ( self ): A__ : Optional[Any] = self.get_auto_remove_tmp_dir() A__ : List[Any] = F"\n {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py\n --model_name_or_path google/vit-base-patch16-224-in21k\n --dataset_name hf-internal-testing/cats_vs_dogs_sample\n --learning_rate 1e-4\n --per_device_train_batch_size 2\n --per_device_eval_batch_size 1\n --max_train_steps 2\n --train_val_split 0.1\n --seed 42\n --output_dir {tmp_dir}\n --with_tracking\n --checkpointing_steps 1\n ".split() if is_cuda_and_apex_available(): testargs.append('''--fp16''' ) run_command(self._launch_args + testargs ) A__ : Dict = get_results(UpperCamelCase__ ) # The base model scores a 25% self.assertGreaterEqual(result['''eval_accuracy'''] , 0.6 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''step_1''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''image_classification_no_trainer''' ) ) )
55
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor from ..utils import is_datasets_available from .base import PipelineTool if is_datasets_available(): from datasets import load_dataset class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "microsoft/speecht5_tts" _lowerCAmelCase = ( "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the " "text to read (in English) and returns a waveform object containing the sound." ) _lowerCAmelCase = "text_reader" _lowerCAmelCase = SpeechTaProcessor _lowerCAmelCase = SpeechTaForTextToSpeech _lowerCAmelCase = SpeechTaHifiGan _lowerCAmelCase = ["text"] _lowerCAmelCase = ["audio"] def __snake_case ( self ): if self.post_processor is None: A__ : int = '''microsoft/speecht5_hifigan''' super().setup() def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__=None ): A__ : List[Any] = self.pre_processor(text=UpperCamelCase__ , return_tensors='''pt''' , truncation=UpperCamelCase__ ) if speaker_embeddings is None: if not is_datasets_available(): raise ImportError('''Datasets needs to be installed if not passing speaker embeddings.''' ) A__ : List[Any] = load_dataset('''Matthijs/cmu-arctic-xvectors''' , split='''validation''' ) A__ : Dict = torch.tensor(embeddings_dataset[7305]['''xvector'''] ).unsqueeze(0 ) return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings} def __snake_case ( self , UpperCamelCase__ ): with torch.no_grad(): return self.model.generate_speech(**UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): with torch.no_grad(): return self.post_processor(UpperCamelCase__ ).cpu().detach()
55
1
from abc import ABC, abstractmethod from argparse import ArgumentParser class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' @staticmethod @abstractmethod def __snake_case ( UpperCamelCase__ ): raise NotImplementedError() @abstractmethod def __snake_case ( self ): raise NotImplementedError()
55
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation _SCREAMING_SNAKE_CASE : Optional[int] = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Optional[Any] = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} _SCREAMING_SNAKE_CASE : List[str] = { 'tokenizer_file': { 'EleutherAI/gpt-neox-20b': 'https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/tokenizer.json', }, } _SCREAMING_SNAKE_CASE : Dict = { 'gpt-neox-20b': 2_0_4_8, } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = VOCAB_FILES_NAMES _lowerCAmelCase = PRETRAINED_VOCAB_FILES_MAP _lowerCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCAmelCase = ["input_ids", "attention_mask"] def __init__( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__="<|endoftext|>" , UpperCamelCase__="<|endoftext|>" , UpperCamelCase__="<|endoftext|>" , UpperCamelCase__=False , **UpperCamelCase__ , ): super().__init__( UpperCamelCase__ , UpperCamelCase__ , tokenizer_file=UpperCamelCase__ , unk_token=UpperCamelCase__ , bos_token=UpperCamelCase__ , eos_token=UpperCamelCase__ , add_prefix_space=UpperCamelCase__ , **UpperCamelCase__ , ) A__ : Optional[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , UpperCamelCase__ ) != add_prefix_space: A__ : Union[str, Any] = getattr(UpperCamelCase__ , pre_tok_state.pop('''type''' ) ) A__ : List[Any] = add_prefix_space A__ : Any = pre_tok_class(**UpperCamelCase__ ) A__ : List[Any] = add_prefix_space def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ = None ): A__ : Any = self._tokenizer.model.save(UpperCamelCase__ , name=UpperCamelCase__ ) return tuple(UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): A__ : List[str] = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) + [self.eos_token_id] ) if len(UpperCamelCase__ ) > self.model_max_length: A__ : Tuple = input_ids[-self.model_max_length :] return input_ids
55
1
_SCREAMING_SNAKE_CASE : Union[str, Any] = '\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n' _SCREAMING_SNAKE_CASE : Optional[int] = [{'type': 'code', 'content': INSTALL_CONTENT}] _SCREAMING_SNAKE_CASE : Dict = { '{processor_class}': 'FakeProcessorClass', '{model_class}': 'FakeModelClass', '{object_class}': 'FakeObjectClass', }
55
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING _SCREAMING_SNAKE_CASE : Union[str, Any] = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : int = { '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__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "deformable_detr" _lowerCAmelCase = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=3 , UpperCamelCase__=300 , UpperCamelCase__=1024 , UpperCamelCase__=6 , UpperCamelCase__=1024 , UpperCamelCase__=8 , UpperCamelCase__=6 , UpperCamelCase__=1024 , UpperCamelCase__=8 , UpperCamelCase__=0.0 , UpperCamelCase__=True , UpperCamelCase__="relu" , UpperCamelCase__=256 , UpperCamelCase__=0.1 , UpperCamelCase__=0.0 , UpperCamelCase__=0.0 , UpperCamelCase__=0.0_2 , UpperCamelCase__=1.0 , UpperCamelCase__=True , UpperCamelCase__=False , UpperCamelCase__="sine" , UpperCamelCase__="resnet50" , UpperCamelCase__=True , UpperCamelCase__=False , UpperCamelCase__=4 , UpperCamelCase__=4 , UpperCamelCase__=4 , UpperCamelCase__=False , UpperCamelCase__=300 , UpperCamelCase__=False , UpperCamelCase__=1 , UpperCamelCase__=5 , UpperCamelCase__=2 , UpperCamelCase__=1 , UpperCamelCase__=1 , UpperCamelCase__=5 , UpperCamelCase__=2 , UpperCamelCase__=0.1 , UpperCamelCase__=0.2_5 , UpperCamelCase__=False , **UpperCamelCase__ , ): if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) A__ : int = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Union[str, Any] = backbone_config.get('''model_type''' ) A__ : Union[str, Any] = CONFIG_MAPPING[backbone_model_type] A__ : Optional[int] = config_class.from_dict(UpperCamelCase__ ) A__ : Tuple = use_timm_backbone A__ : int = backbone_config A__ : List[Any] = num_channels A__ : List[Any] = num_queries A__ : str = max_position_embeddings A__ : Tuple = d_model A__ : int = encoder_ffn_dim A__ : Union[str, Any] = encoder_layers A__ : Optional[Any] = encoder_attention_heads A__ : List[Any] = decoder_ffn_dim A__ : Tuple = decoder_layers A__ : Optional[Any] = decoder_attention_heads A__ : List[str] = dropout A__ : str = attention_dropout A__ : List[Any] = activation_dropout A__ : Any = activation_function A__ : Optional[Any] = init_std A__ : Union[str, Any] = init_xavier_std A__ : Union[str, Any] = encoder_layerdrop A__ : Optional[int] = auxiliary_loss A__ : str = position_embedding_type A__ : List[Any] = backbone A__ : Optional[Any] = use_pretrained_backbone A__ : Any = dilation # deformable attributes A__ : List[Any] = num_feature_levels A__ : List[str] = encoder_n_points A__ : int = decoder_n_points A__ : List[Any] = two_stage A__ : Dict = two_stage_num_proposals A__ : Optional[int] = 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 A__ : List[str] = class_cost A__ : List[Any] = bbox_cost A__ : Any = giou_cost # Loss coefficients A__ : List[str] = mask_loss_coefficient A__ : Union[str, Any] = dice_loss_coefficient A__ : List[Any] = bbox_loss_coefficient A__ : Tuple = giou_loss_coefficient A__ : Optional[Any] = eos_coefficient A__ : List[Any] = focal_alpha A__ : List[str] = disable_custom_kernels super().__init__(is_encoder_decoder=UpperCamelCase__ , **UpperCamelCase__ ) @property def __snake_case ( self ): return self.encoder_attention_heads @property def __snake_case ( self ): return self.d_model def __snake_case ( self ): A__ : List[str] = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: A__ : Tuple = self.backbone_config.to_dict() A__ : Optional[int] = self.__class__.model_type return output
55
1
import torch from diffusers import CMStochasticIterativeScheduler from .test_schedulers import SchedulerCommonTest class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = (CMStochasticIterativeScheduler,) _lowerCAmelCase = 10 def __snake_case ( self , **UpperCamelCase__ ): A__ : str = { '''num_train_timesteps''': 201, '''sigma_min''': 0.0_0_2, '''sigma_max''': 8_0.0, } config.update(**UpperCamelCase__ ) return config def __snake_case ( self ): A__ : Tuple = 10 A__ : Any = self.get_scheduler_config() A__ : Tuple = self.scheduler_classes[0](**UpperCamelCase__ ) scheduler.set_timesteps(UpperCamelCase__ ) A__ : Dict = scheduler.timesteps[0] A__ : Dict = scheduler.timesteps[1] A__ : List[str] = self.dummy_sample A__ : str = 0.1 * sample A__ : Dict = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ).prev_sample A__ : int = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def __snake_case ( self ): for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=UpperCamelCase__ ) def __snake_case ( self ): for clip_denoised in [True, False]: self.check_over_configs(clip_denoised=UpperCamelCase__ ) def __snake_case ( self ): A__ : List[Any] = self.scheduler_classes[0] A__ : Dict = self.get_scheduler_config() A__ : List[Any] = scheduler_class(**UpperCamelCase__ ) A__ : Optional[Any] = 1 scheduler.set_timesteps(UpperCamelCase__ ) A__ : Optional[int] = scheduler.timesteps A__ : Tuple = torch.manual_seed(0 ) A__ : Optional[int] = self.dummy_model() A__ : Dict = self.dummy_sample_deter * scheduler.init_noise_sigma for i, t in enumerate(UpperCamelCase__ ): # 1. scale model input A__ : Tuple = scheduler.scale_model_input(UpperCamelCase__ , UpperCamelCase__ ) # 2. predict noise residual A__ : Dict = model(UpperCamelCase__ , UpperCamelCase__ ) # 3. predict previous sample x_t-1 A__ : Optional[int] = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , generator=UpperCamelCase__ ).prev_sample A__ : Any = pred_prev_sample A__ : Tuple = torch.sum(torch.abs(UpperCamelCase__ ) ) A__ : str = torch.mean(torch.abs(UpperCamelCase__ ) ) assert abs(result_sum.item() - 1_9_2.7_6_1_4 ) < 1e-2 assert abs(result_mean.item() - 0.2_5_1_0 ) < 1e-3 def __snake_case ( self ): A__ : Optional[int] = self.scheduler_classes[0] A__ : Any = self.get_scheduler_config() A__ : str = scheduler_class(**UpperCamelCase__ ) A__ : Any = [106, 0] scheduler.set_timesteps(timesteps=UpperCamelCase__ ) A__ : int = scheduler.timesteps A__ : Optional[int] = torch.manual_seed(0 ) A__ : int = self.dummy_model() A__ : str = self.dummy_sample_deter * scheduler.init_noise_sigma for t in timesteps: # 1. scale model input A__ : Any = scheduler.scale_model_input(UpperCamelCase__ , UpperCamelCase__ ) # 2. predict noise residual A__ : Optional[Any] = model(UpperCamelCase__ , UpperCamelCase__ ) # 3. predict previous sample x_t-1 A__ : Union[str, Any] = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , generator=UpperCamelCase__ ).prev_sample A__ : Tuple = pred_prev_sample A__ : List[Any] = torch.sum(torch.abs(UpperCamelCase__ ) ) A__ : str = torch.mean(torch.abs(UpperCamelCase__ ) ) assert abs(result_sum.item() - 3_4_7.6_3_5_7 ) < 1e-2 assert abs(result_mean.item() - 0.4_5_2_7 ) < 1e-3 def __snake_case ( self ): A__ : Optional[int] = self.scheduler_classes[0] A__ : str = self.get_scheduler_config() A__ : int = scheduler_class(**UpperCamelCase__ ) A__ : Optional[Any] = [39, 30, 12, 15, 0] with self.assertRaises(UpperCamelCase__ , msg='''`timesteps` must be in descending order.''' ): scheduler.set_timesteps(timesteps=UpperCamelCase__ ) def __snake_case ( self ): A__ : List[Any] = self.scheduler_classes[0] A__ : Tuple = self.get_scheduler_config() A__ : Optional[int] = scheduler_class(**UpperCamelCase__ ) A__ : Optional[int] = [39, 30, 12, 1, 0] A__ : Tuple = len(UpperCamelCase__ ) with self.assertRaises(UpperCamelCase__ , msg='''Can only pass one of `num_inference_steps` or `timesteps`.''' ): scheduler.set_timesteps(num_inference_steps=UpperCamelCase__ , timesteps=UpperCamelCase__ ) def __snake_case ( self ): A__ : List[str] = self.scheduler_classes[0] A__ : int = self.get_scheduler_config() A__ : Union[str, Any] = scheduler_class(**UpperCamelCase__ ) A__ : str = [scheduler.config.num_train_timesteps] with self.assertRaises( UpperCamelCase__ , msg='''`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}''' , ): scheduler.set_timesteps(timesteps=UpperCamelCase__ )
55
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> List[Any]: """simple docstring""" A__ : Optional[Any] = 0 A__ : Optional[Any] = len(__UpperCamelCase ) for i in range(n - 1 ): for j in range(i + 1 , __UpperCamelCase ): if arr[i] > arr[j]: num_inversions += 1 return num_inversions def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Tuple: """simple docstring""" if len(__UpperCamelCase ) <= 1: return arr, 0 A__ : Optional[int] = len(__UpperCamelCase ) // 2 A__ : List[str] = arr[0:mid] A__ : Union[str, Any] = arr[mid:] A__ , A__ : List[Any] = count_inversions_recursive(__UpperCamelCase ) A__ , A__ : int = count_inversions_recursive(__UpperCamelCase ) A__ , A__ : Dict = _count_cross_inversions(__UpperCamelCase , __UpperCamelCase ) A__ : Any = inversion_p + inversions_q + cross_inversions return c, num_inversions def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] , __UpperCamelCase : List[Any] ) -> Dict: """simple docstring""" A__ : str = [] A__ : Tuple = 0 while i < len(__UpperCamelCase ) and j < len(__UpperCamelCase ): if p[i] > q[j]: # if P[1] > Q[j], then P[k] > Q[k] for all i < k <= len(P) # These are all inversions. The claim emerges from the # property that P is sorted. num_inversion += len(__UpperCamelCase ) - i r.append(q[j] ) j += 1 else: r.append(p[i] ) i += 1 if i < len(__UpperCamelCase ): r.extend(p[i:] ) else: r.extend(q[j:] ) return r, num_inversion def SCREAMING_SNAKE_CASE ( ) -> Tuple: """simple docstring""" A__ : List[str] = [10, 2, 1, 5, 5, 2, 11] # this arr has 8 inversions: # (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2) A__ : int = count_inversions_bf(__UpperCamelCase ) A__ , A__ : int = count_inversions_recursive(__UpperCamelCase ) assert num_inversions_bf == num_inversions_recursive == 8 print('''number of inversions = ''' , __UpperCamelCase ) # testing an array with zero inversion (a sorted arr_1) arr_a.sort() A__ : Optional[Any] = count_inversions_bf(__UpperCamelCase ) A__ , A__ : Dict = count_inversions_recursive(__UpperCamelCase ) assert num_inversions_bf == num_inversions_recursive == 0 print('''number of inversions = ''' , __UpperCamelCase ) # an empty list should also have zero inversions A__ : Union[str, Any] = [] A__ : Union[str, Any] = count_inversions_bf(__UpperCamelCase ) A__ , A__ : Any = count_inversions_recursive(__UpperCamelCase ) assert num_inversions_bf == num_inversions_recursive == 0 print('''number of inversions = ''' , __UpperCamelCase ) if __name__ == "__main__": main()
55
1
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> int: """simple docstring""" A__ : Optional[int] = abs(__UpperCamelCase ) A__ : int = 0 while n > 0: res += n % 10 n //= 10 return res def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> int: """simple docstring""" A__ : int = abs(__UpperCamelCase ) return n if n < 10 else n % 10 + sum_of_digits(n // 10 ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> int: """simple docstring""" return sum(int(__UpperCamelCase ) for c in str(abs(__UpperCamelCase ) ) ) def SCREAMING_SNAKE_CASE ( ) -> None: """simple docstring""" from collections.abc import Callable from timeit import timeit def benchmark_a_function(__UpperCamelCase : Callable , __UpperCamelCase : int ) -> None: A__ : Dict = F"{func.__name__}({value})" A__ : Any = timeit(F"__main__.{call}" , setup='''import __main__''' ) print(F"{call:56} = {func(__UpperCamelCase )} -- {timing:.4f} seconds" ) for value in (26_21_44, 11_25_89_99_06_84_26_24, 1_26_76_50_60_02_28_22_94_01_49_67_03_20_53_76): for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact): benchmark_a_function(__UpperCamelCase , __UpperCamelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
55
from PIL import Image def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Image , __UpperCamelCase : float ) -> Image: """simple docstring""" def brightness(__UpperCamelCase : int ) -> float: return 1_28 + level + (c - 1_28) if not -2_5_5.0 <= level <= 2_5_5.0: raise ValueError('''level must be between -255.0 (black) and 255.0 (white)''' ) return img.point(__UpperCamelCase ) if __name__ == "__main__": # Load image with Image.open('image_data/lena.jpg') as img: # Change brightness to 100 _SCREAMING_SNAKE_CASE : Dict = change_brightness(img, 1_0_0) brigt_img.save('image_data/lena_brightness.png', format='png')
55
1
from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def SCREAMING_SNAKE_CASE ( __UpperCamelCase : NDArray[floataa] , __UpperCamelCase : NDArray[floataa] , __UpperCamelCase : list[int] , __UpperCamelCase : int , ) -> list[float]: """simple docstring""" A__ , A__ : Tuple = coefficient_matrix.shape A__ , A__ : List[str] = constant_matrix.shape if rowsa != colsa: A__ : Optional[Any] = F"Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}" raise ValueError(__UpperCamelCase ) if colsa != 1: A__ : Any = F"Constant matrix must be nx1 but received {rowsa}x{colsa}" raise ValueError(__UpperCamelCase ) if rowsa != rowsa: A__ : Union[str, Any] = ( '''Coefficient and constant matrices dimensions must be nxn and nx1 but ''' F"received {rowsa}x{colsa} and {rowsa}x{colsa}" ) raise ValueError(__UpperCamelCase ) if len(__UpperCamelCase ) != rowsa: A__ : Dict = ( '''Number of initial values must be equal to number of rows in coefficient ''' F"matrix but received {len(__UpperCamelCase )} and {rowsa}" ) raise ValueError(__UpperCamelCase ) if iterations <= 0: raise ValueError('''Iterations must be at least 1''' ) A__ : NDArray[floataa] = np.concatenate( (coefficient_matrix, constant_matrix) , axis=1 ) A__ , A__ : Union[str, Any] = table.shape strictly_diagonally_dominant(__UpperCamelCase ) # Iterates the whole matrix for given number of times for _ in range(__UpperCamelCase ): A__ : str = [] for row in range(__UpperCamelCase ): A__ : Any = 0 for col in range(__UpperCamelCase ): if col == row: A__ : List[str] = table[row][col] elif col == cols - 1: A__ : Union[str, Any] = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] A__ : List[Any] = (temp + val) / denom new_val.append(__UpperCamelCase ) A__ : Any = new_val return [float(__UpperCamelCase ) for i in new_val] def SCREAMING_SNAKE_CASE ( __UpperCamelCase : NDArray[floataa] ) -> bool: """simple docstring""" A__ , A__ : Union[str, Any] = table.shape A__ : Union[str, Any] = True for i in range(0 , __UpperCamelCase ): A__ : Union[str, Any] = 0 for j in range(0 , cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError('''Coefficient matrix is not strictly diagonally dominant''' ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
55
import json import os import tempfile from transformers.testing_utils import check_json_file_has_correct_format class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = None def __snake_case ( self ): A__ : Dict = self.feature_extraction_class(**self.feat_extract_dict ) A__ : Tuple = json.loads(feat_extract.to_json_string() ) for key, value in self.feat_extract_dict.items(): self.assertEqual(obj[key] , UpperCamelCase__ ) def __snake_case ( self ): A__ : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: A__ : Any = os.path.join(UpperCamelCase__ , '''feat_extract.json''' ) feat_extract_first.to_json_file(UpperCamelCase__ ) A__ : Dict = self.feature_extraction_class.from_json_file(UpperCamelCase__ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def __snake_case ( self ): A__ : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: A__ : Any = feat_extract_first.save_pretrained(UpperCamelCase__ )[0] check_json_file_has_correct_format(UpperCamelCase__ ) A__ : Optional[int] = self.feature_extraction_class.from_pretrained(UpperCamelCase__ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def __snake_case ( self ): A__ : str = self.feature_extraction_class() self.assertIsNotNone(UpperCamelCase__ )
55
1
from __future__ import annotations from collections import deque from collections.abc import Iterator from dataclasses import dataclass @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = 42 _lowerCAmelCase = 42 class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ ): A__ : list[list[Edge]] = [[] for _ in range(UpperCamelCase__ )] A__ : Tuple = size def __getitem__( self , UpperCamelCase__ ): return iter(self._graph[vertex] ) @property def __snake_case ( self ): return self._size def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): if weight not in (0, 1): raise ValueError('''Edge weight must be either 0 or 1.''' ) if to_vertex < 0 or to_vertex >= self.size: raise ValueError('''Vertex indexes must be in [0; size).''' ) self._graph[from_vertex].append(Edge(UpperCamelCase__ , UpperCamelCase__ ) ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ ): A__ : Union[str, Any] = deque([start_vertex] ) A__ : list[int | None] = [None] * self.size A__ : Tuple = 0 while queue: A__ : Optional[int] = queue.popleft() A__ : Union[str, Any] = distances[current_vertex] if current_distance is None: continue for edge in self[current_vertex]: A__ : List[str] = current_distance + edge.weight A__ : Optional[Any] = distances[edge.destination_vertex] if ( isinstance(UpperCamelCase__ , UpperCamelCase__ ) and new_distance >= dest_vertex_distance ): continue A__ : Tuple = new_distance if edge.weight == 0: queue.appendleft(edge.destination_vertex ) else: queue.append(edge.destination_vertex ) if distances[finish_vertex] is None: raise ValueError('''No path from start_vertex to finish_vertex.''' ) return distances[finish_vertex] if __name__ == "__main__": import doctest doctest.testmod()
55
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets _SCREAMING_SNAKE_CASE : Union[str, Any] = '\\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 : Tuple = '\\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 : Optional[Any] = '\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 UpperCamelCase__ ( datasets.Metric ): '''simple docstring''' def __snake_case ( self ): 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 , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = False , UpperCamelCase__ = False , UpperCamelCase__ = False , UpperCamelCase__ = False , ): A__ : List[Any] = len(references[0] ) if any(len(UpperCamelCase__ ) != references_per_prediction for refs in references ): raise ValueError('''Sacrebleu requires the same number of references for each prediction''' ) A__ : Dict = [[refs[i] for refs in references] for i in range(UpperCamelCase__ )] A__ : Optional[Any] = TER( normalized=UpperCamelCase__ , no_punct=UpperCamelCase__ , asian_support=UpperCamelCase__ , case_sensitive=UpperCamelCase__ , ) A__ : str = sb_ter.corpus_score(UpperCamelCase__ , UpperCamelCase__ ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
55
1
from __future__ import annotations import inspect import unittest import numpy as np from transformers import DeiTConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, ) from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DeiTImageProcessor class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__=13 , UpperCamelCase__=30 , UpperCamelCase__=2 , UpperCamelCase__=3 , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=32 , UpperCamelCase__=2 , UpperCamelCase__=4 , UpperCamelCase__=37 , UpperCamelCase__="gelu" , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=10 , UpperCamelCase__=0.0_2 , UpperCamelCase__=3 , UpperCamelCase__=None , UpperCamelCase__=2 , ): A__ : Any = parent A__ : str = batch_size A__ : Optional[int] = image_size A__ : Tuple = patch_size A__ : Dict = num_channels A__ : Optional[Any] = is_training A__ : List[str] = use_labels A__ : int = hidden_size A__ : Optional[Any] = num_hidden_layers A__ : Union[str, Any] = num_attention_heads A__ : str = intermediate_size A__ : Any = hidden_act A__ : List[str] = hidden_dropout_prob A__ : int = attention_probs_dropout_prob A__ : List[str] = type_sequence_label_size A__ : Dict = initializer_range A__ : Any = scope A__ : Dict = encoder_stride # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens) A__ : Dict = (image_size // patch_size) ** 2 A__ : Dict = num_patches + 2 def __snake_case ( self ): A__ : int = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ : Optional[Any] = None if self.use_labels: A__ : Tuple = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ : List[str] = self.get_config() return config, pixel_values, labels def __snake_case ( self ): 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=UpperCamelCase__ , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : Dict = TFDeiTModel(config=UpperCamelCase__ ) A__ : int = model(UpperCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : List[Any] = TFDeiTForMaskedImageModeling(config=UpperCamelCase__ ) A__ : Tuple = model(UpperCamelCase__ ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images A__ : Optional[Any] = 1 A__ : Any = TFDeiTForMaskedImageModeling(UpperCamelCase__ ) A__ : Dict = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A__ : List[Any] = model(UpperCamelCase__ ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : List[Any] = self.type_sequence_label_size A__ : str = TFDeiTForImageClassification(UpperCamelCase__ ) A__ : Any = model(UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images A__ : Optional[Any] = 1 A__ : Optional[int] = TFDeiTForImageClassification(UpperCamelCase__ ) A__ : Any = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A__ : Optional[int] = model(UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __snake_case ( self ): A__ : Dict = self.prepare_config_and_inputs() A__ , A__ , A__ : Tuple = config_and_inputs A__ : Optional[Any] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = ( ( TFDeiTModel, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, ) if is_tf_available() else () ) _lowerCAmelCase = ( { "feature-extraction": TFDeiTModel, "image-classification": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher), } if is_tf_available() else {} ) _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False def __snake_case ( self ): A__ : int = TFDeiTModelTester(self ) A__ : List[Any] = ConfigTester(self , config_class=UpperCamelCase__ , has_text_modality=UpperCamelCase__ , hidden_size=37 ) def __snake_case ( self ): self.config_tester.run_common_tests() @unittest.skip(reason='''DeiT does not use inputs_embeds''' ) def __snake_case ( self ): pass def __snake_case ( self ): A__ , A__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : List[Any] = model_class(UpperCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) A__ : List[Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCamelCase__ , tf.keras.layers.Dense ) ) def __snake_case ( self ): A__ , A__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : List[Any] = model_class(UpperCamelCase__ ) A__ : Optional[Any] = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ : List[str] = [*signature.parameters.keys()] A__ : Dict = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , UpperCamelCase__ ) def __snake_case ( self ): A__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase__ ) def __snake_case ( self ): A__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase__ ) def __snake_case ( self ): A__ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=False ): A__ : Any = super()._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ , return_labels=UpperCamelCase__ ) if return_labels: if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call ).parameters: del inputs_dict["labels"] return inputs_dict @slow def __snake_case ( self ): for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ : List[str] = TFDeiTModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) def SCREAMING_SNAKE_CASE ( ) -> str: """simple docstring""" A__ : List[Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf @require_vision class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' @cached_property def __snake_case ( self ): return ( DeiTImageProcessor.from_pretrained('''facebook/deit-base-distilled-patch16-224''' ) if is_vision_available() else None ) @slow def __snake_case ( self ): A__ : Any = TFDeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224''' ) A__ : Optional[int] = self.default_image_processor A__ : Dict = prepare_img() A__ : Any = image_processor(images=UpperCamelCase__ , return_tensors='''tf''' ) # forward pass A__ : str = model(**UpperCamelCase__ ) # verify the logits A__ : Dict = tf.TensorShape((1, 1000) ) self.assertEqual(outputs.logits.shape , UpperCamelCase__ ) A__ : List[str] = tf.constant([-1.0_2_6_6, 0.1_9_1_2, -1.2_8_6_1] ) self.assertTrue(np.allclose(outputs.logits[0, :3] , UpperCamelCase__ , atol=1e-4 ) )
55
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging _SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) # TODO Update this _SCREAMING_SNAKE_CASE : Optional[int] = { 'facebook/esm-1b': 'https://huggingface.co/facebook/esm-1b/resolve/main/config.json', # See all ESM models at https://huggingface.co/models?filter=esm } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "esm" def __init__( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=768 , UpperCamelCase__=12 , UpperCamelCase__=12 , UpperCamelCase__=3072 , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=1026 , UpperCamelCase__=0.0_2 , UpperCamelCase__=1e-12 , UpperCamelCase__="absolute" , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=False , UpperCamelCase__=False , UpperCamelCase__=None , UpperCamelCase__=None , **UpperCamelCase__ , ): super().__init__(pad_token_id=UpperCamelCase__ , mask_token_id=UpperCamelCase__ , **UpperCamelCase__ ) A__ : Optional[Any] = vocab_size A__ : int = hidden_size A__ : List[str] = num_hidden_layers A__ : Tuple = num_attention_heads A__ : str = intermediate_size A__ : List[str] = hidden_dropout_prob A__ : Optional[Any] = attention_probs_dropout_prob A__ : int = max_position_embeddings A__ : List[str] = initializer_range A__ : List[Any] = layer_norm_eps A__ : int = position_embedding_type A__ : Optional[Any] = use_cache A__ : Optional[int] = emb_layer_norm_before A__ : List[str] = token_dropout A__ : Tuple = is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) A__ : List[Any] = EsmFoldConfig() elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Optional[int] = EsmFoldConfig(**UpperCamelCase__ ) A__ : int = esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) A__ : Any = get_default_vocab_list() else: A__ : Dict = vocab_list else: A__ : Optional[Any] = None A__ : Tuple = None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCamelCase__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __snake_case ( self ): A__ : Optional[int] = super().to_dict() if isinstance(self.esmfold_config , UpperCamelCase__ ): A__ : Dict = self.esmfold_config.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = None _lowerCAmelCase = True _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = 0 _lowerCAmelCase = True _lowerCAmelCase = False _lowerCAmelCase = 128 _lowerCAmelCase = None def __snake_case ( self ): if self.trunk is None: A__ : Tuple = TrunkConfig() elif isinstance(self.trunk , UpperCamelCase__ ): A__ : List[Any] = TrunkConfig(**self.trunk ) def __snake_case ( self ): A__ : Optional[int] = asdict(self ) A__ : int = self.trunk.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = 48 _lowerCAmelCase = 1_024 _lowerCAmelCase = 128 _lowerCAmelCase = 32 _lowerCAmelCase = 32 _lowerCAmelCase = 32 _lowerCAmelCase = 0 _lowerCAmelCase = 0 _lowerCAmelCase = False _lowerCAmelCase = 4 _lowerCAmelCase = 128 _lowerCAmelCase = None def __snake_case ( self ): if self.structure_module is None: A__ : str = StructureModuleConfig() elif isinstance(self.structure_module , UpperCamelCase__ ): A__ : str = StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(F"`max_recycles` should be positive, got {self.max_recycles}." ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' F" {self.sequence_state_dim} and {self.sequence_state_dim}." ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' F" {self.pairwise_state_dim} and {self.pairwise_state_dim}." ) A__ : Tuple = self.sequence_state_dim // self.sequence_head_width A__ : int = self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' F" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}." ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' F" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}." ) if self.pairwise_state_dim % 2 != 0: raise ValueError(F"`pairwise_state_dim` should be even, got {self.pairwise_state_dim}." ) if self.dropout >= 0.4: raise ValueError(F"`dropout` should not be greater than 0.4, got {self.dropout}." ) def __snake_case ( self ): A__ : List[Any] = asdict(self ) A__ : Optional[int] = self.structure_module.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = 384 _lowerCAmelCase = 128 _lowerCAmelCase = 16 _lowerCAmelCase = 128 _lowerCAmelCase = 12 _lowerCAmelCase = 4 _lowerCAmelCase = 8 _lowerCAmelCase = 0.1 _lowerCAmelCase = 8 _lowerCAmelCase = 1 _lowerCAmelCase = 2 _lowerCAmelCase = 7 _lowerCAmelCase = 10 _lowerCAmelCase = 1e-8 _lowerCAmelCase = 1e5 def __snake_case ( self ): return asdict(self ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: """simple docstring""" return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
55
1
import numpy as np def SCREAMING_SNAKE_CASE ( __UpperCamelCase : np.array ) -> np.array: """simple docstring""" return 1 / (1 + np.exp(-vector )) if __name__ == "__main__": import doctest doctest.testmod()
55
import logging import torch from accelerate import Accelerator from arguments import EvaluationArguments from datasets import load_dataset from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, set_seed class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=1024 , UpperCamelCase__=1024 , UpperCamelCase__=3.6 ): A__ : str = tokenizer A__ : int = tokenizer.bos_token_id A__ : List[Any] = dataset A__ : Tuple = seq_length A__ : Any = seq_length * chars_per_token * num_of_sequences def __iter__( self ): A__ : Dict = iter(self.dataset ) A__ : Tuple = True while more_examples: A__ , A__ : Optional[Any] = [], 0 while True: if buffer_len >= self.input_characters: break try: buffer.append(next(UpperCamelCase__ )['''content'''] ) buffer_len += len(buffer[-1] ) except StopIteration: A__ : Dict = False break A__ : str = tokenizer(UpperCamelCase__ , truncation=UpperCamelCase__ )['''input_ids'''] A__ : Optional[int] = [] for tokenized_input in tokenized_inputs: all_token_ids.extend(tokenized_input + [self.concat_token_id] ) for i in range(0 , len(UpperCamelCase__ ) , self.seq_length ): A__ : Optional[int] = all_token_ids[i : i + self.seq_length] if len(UpperCamelCase__ ) == self.seq_length: yield torch.tensor(UpperCamelCase__ ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> Any: """simple docstring""" A__ : Any = {'''streaming''': True} A__ : List[str] = load_dataset(args.dataset_name , split='''train''' , **__UpperCamelCase ) A__ : List[str] = ConstantLengthDataset(__UpperCamelCase , __UpperCamelCase , seq_length=args.seq_length ) A__ : int = DataLoader(__UpperCamelCase , batch_size=args.batch_size ) return eval_dataloader def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] ) -> Dict: """simple docstring""" model.eval() A__ : Dict = [] for step, batch in enumerate(__UpperCamelCase ): with torch.no_grad(): A__ : Any = model(__UpperCamelCase , labels=__UpperCamelCase ) A__ : Tuple = outputs.loss.repeat(args.batch_size ) losses.append(accelerator.gather(__UpperCamelCase ) ) if args.max_eval_steps > 0 and step >= args.max_eval_steps: break A__ : Tuple = torch.mean(torch.cat(__UpperCamelCase ) ) try: A__ : Optional[Any] = torch.exp(__UpperCamelCase ) except OverflowError: A__ : Union[str, Any] = float('''inf''' ) return loss.item(), perplexity.item() # Setup Accelerator _SCREAMING_SNAKE_CASE : List[Any] = Accelerator() # Parse configuration _SCREAMING_SNAKE_CASE : Optional[int] = HfArgumentParser(EvaluationArguments) _SCREAMING_SNAKE_CASE : Union[str, Any] = parser.parse_args() set_seed(args.seed) # Logging _SCREAMING_SNAKE_CASE : Dict = logging.getLogger(__name__) logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO ) # Load model and tokenizer _SCREAMING_SNAKE_CASE : Optional[int] = AutoModelForCausalLM.from_pretrained(args.model_ckpt) _SCREAMING_SNAKE_CASE : List[str] = AutoTokenizer.from_pretrained(args.model_ckpt) # Load dataset and dataloader _SCREAMING_SNAKE_CASE : Optional[Any] = create_dataloader(args) # Prepare everything with our `accelerator`. _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Optional[Any] = accelerator.prepare(model, eval_dataloader) # Evaluate and save the last checkpoint logger.info('Evaluating and saving model after training') _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Optional[int] = evaluate(args) logger.info(f"""loss/eval: {eval_loss}, perplexity: {perplexity}""")
55
1
from __future__ import annotations from typing import Any class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ = 6 ): A__ : Node | None = None A__ : Node | None = None self.create_linked_list(UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): A__ : int = Node() A__ : str = current_node A__ : List[str] = current_node A__ : int = current_node for _ in range(1 , UpperCamelCase__ ): A__ : Optional[Any] = Node() A__ : Optional[Any] = current_node A__ : int = previous_node A__ : Dict = current_node A__ : Dict = self.front A__ : str = previous_node def __snake_case ( self ): return ( self.front == self.rear and self.front is not None and self.front.data is None ) def __snake_case ( self ): self.check_can_perform_operation() return self.front.data if self.front else None def __snake_case ( self , UpperCamelCase__ ): if self.rear is None: return self.check_is_full() if not self.is_empty(): A__ : int = self.rear.next if self.rear: A__ : Dict = data def __snake_case ( self ): self.check_can_perform_operation() if self.rear is None or self.front is None: return None if self.front == self.rear: A__ : Union[str, Any] = self.front.data A__ : Optional[int] = None return data A__ : int = self.front A__ : List[str] = old_front.next A__ : List[Any] = old_front.data A__ : int = None return data def __snake_case ( self ): if self.is_empty(): raise Exception('''Empty Queue''' ) def __snake_case ( self ): if self.rear and self.rear.next == self.front: raise Exception('''Full Queue''' ) class UpperCamelCase__ : '''simple docstring''' def __init__( self ): A__ : Any | None = None A__ : Node | None = None A__ : Node | None = None if __name__ == "__main__": import doctest doctest.testmod()
55
def SCREAMING_SNAKE_CASE ( ) -> Optional[int]: """simple docstring""" A__ : Optional[Any] = 0 for i in range(1 , 10_01 ): total += i**i return str(__UpperCamelCase )[-10:] if __name__ == "__main__": print(solution())
55
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _SCREAMING_SNAKE_CASE : Union[str, Any] = {'configuration_vit_msn': ['VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ViTMSNConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : List[str] = [ 'VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST', 'ViTMSNModel', 'ViTMSNForImageClassification', 'ViTMSNPreTrainedModel', ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE : Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
55
import inspect import os import unittest import torch import accelerate from accelerate import debug_launcher from accelerate.test_utils import ( execute_subprocess_async, require_cpu, require_huggingface_suite, require_multi_gpu, require_single_gpu, ) from accelerate.utils import patch_environment @require_huggingface_suite class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): A__ : Dict = inspect.getfile(accelerate.test_utils ) A__ : Any = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''external_deps''', '''test_metrics.py'''] ) from accelerate.test_utils.scripts.external_deps import test_metrics # noqa: F401 A__ : Tuple = test_metrics @require_cpu def __snake_case ( self ): debug_launcher(self.test_metrics.main , num_processes=1 ) @require_cpu def __snake_case ( self ): debug_launcher(self.test_metrics.main ) @require_single_gpu def __snake_case ( self ): self.test_metrics.main() @require_multi_gpu def __snake_case ( self ): print(F"Found {torch.cuda.device_count()} devices." ) A__ : int = ['''torchrun''', F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(UpperCamelCase__ , env=os.environ.copy() )
55
1
from ....configuration_utils import PretrainedConfig from ....utils import logging _SCREAMING_SNAKE_CASE : Optional[Any] = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Union[str, Any] = { 'Visual-Attention-Network/van-base': ( 'https://huggingface.co/Visual-Attention-Network/van-base/blob/main/config.json' ), } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "van" def __init__( self , UpperCamelCase__=224 , UpperCamelCase__=3 , UpperCamelCase__=[7, 3, 3, 3] , UpperCamelCase__=[4, 2, 2, 2] , UpperCamelCase__=[64, 128, 320, 512] , UpperCamelCase__=[3, 3, 12, 3] , UpperCamelCase__=[8, 8, 4, 4] , UpperCamelCase__="gelu" , UpperCamelCase__=0.0_2 , UpperCamelCase__=1e-6 , UpperCamelCase__=1e-2 , UpperCamelCase__=0.0 , UpperCamelCase__=0.0 , **UpperCamelCase__ , ): super().__init__(**UpperCamelCase__ ) A__ : List[str] = image_size A__ : Any = num_channels A__ : str = patch_sizes A__ : Dict = strides A__ : str = hidden_sizes A__ : str = depths A__ : int = mlp_ratios A__ : int = hidden_act A__ : Any = initializer_range A__ : List[str] = layer_norm_eps A__ : Union[str, Any] = layer_scale_init_value A__ : str = drop_path_rate A__ : Optional[Any] = dropout_rate
55
from numpy import exp, pi, sqrt def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict , __UpperCamelCase : float = 0.0 , __UpperCamelCase : float = 1.0 ) -> int: """simple docstring""" return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
55
1
from maths.prime_factors import prime_factors def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> int: """simple docstring""" if not isinstance(__UpperCamelCase , __UpperCamelCase ): A__ : Optional[Any] = F"Input value of [number={number}] must be an integer" raise TypeError(__UpperCamelCase ) if number < 1: raise ValueError('''Input must be a positive integer''' ) return -1 if len(prime_factors(__UpperCamelCase ) ) % 2 else 1 if __name__ == "__main__": import doctest doctest.testmod()
55
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tensorflow_text_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _SCREAMING_SNAKE_CASE : int = { 'configuration_bert': ['BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BertConfig', 'BertOnnxConfig'], 'tokenization_bert': ['BasicTokenizer', 'BertTokenizer', 'WordpieceTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Optional[Any] = ['BertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Union[str, Any] = [ 'BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BertForMaskedLM', 'BertForMultipleChoice', 'BertForNextSentencePrediction', 'BertForPreTraining', 'BertForQuestionAnswering', 'BertForSequenceClassification', 'BertForTokenClassification', 'BertLayer', 'BertLMHeadModel', 'BertModel', 'BertPreTrainedModel', 'load_tf_weights_in_bert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Tuple = [ 'TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFBertEmbeddings', 'TFBertForMaskedLM', 'TFBertForMultipleChoice', 'TFBertForNextSentencePrediction', 'TFBertForPreTraining', 'TFBertForQuestionAnswering', 'TFBertForSequenceClassification', 'TFBertForTokenClassification', 'TFBertLMHeadModel', 'TFBertMainLayer', 'TFBertModel', 'TFBertPreTrainedModel', ] try: if not is_tensorflow_text_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Dict = ['TFBertTokenizer'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Optional[int] = [ 'FlaxBertForCausalLM', 'FlaxBertForMaskedLM', 'FlaxBertForMultipleChoice', 'FlaxBertForNextSentencePrediction', 'FlaxBertForPreTraining', 'FlaxBertForQuestionAnswering', 'FlaxBertForSequenceClassification', 'FlaxBertForTokenClassification', 'FlaxBertModel', 'FlaxBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig, BertOnnxConfig from .tokenization_bert import BasicTokenizer, BertTokenizer, WordpieceTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bert_fast import BertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bert import ( BERT_PRETRAINED_MODEL_ARCHIVE_LIST, BertForMaskedLM, BertForMultipleChoice, BertForNextSentencePrediction, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, BertForTokenClassification, BertLayer, BertLMHeadModel, BertModel, BertPreTrainedModel, load_tf_weights_in_bert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_bert import ( TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFBertEmbeddings, TFBertForMaskedLM, TFBertForMultipleChoice, TFBertForNextSentencePrediction, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertForTokenClassification, TFBertLMHeadModel, TFBertMainLayer, TFBertModel, TFBertPreTrainedModel, ) try: if not is_tensorflow_text_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bert_tf import TFBertTokenizer try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_bert import ( FlaxBertForCausalLM, FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForNextSentencePrediction, FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertModel, FlaxBertPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE : Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
55
1
import importlib.util import os import platform from argparse import ArgumentParser import huggingface_hub from .. import __version__ as version from ..utils import ( is_accelerate_available, is_flax_available, is_safetensors_available, is_tf_available, is_torch_available, ) from . import BaseTransformersCLICommand def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> Optional[Any]: """simple docstring""" return EnvironmentCommand() def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> Optional[int]: """simple docstring""" return EnvironmentCommand(args.accelerate_config_file ) class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' @staticmethod def __snake_case ( UpperCamelCase__ ): A__ : Any = parser.add_parser('''env''' ) download_parser.set_defaults(func=UpperCamelCase__ ) download_parser.add_argument( '''--accelerate-config_file''' , default=UpperCamelCase__ , help='''The accelerate config file to use for the default values in the launching script.''' , ) download_parser.set_defaults(func=UpperCamelCase__ ) def __init__( self , UpperCamelCase__ , *UpperCamelCase__ ): A__ : List[Any] = accelerate_config_file def __snake_case ( self ): A__ : Any = '''not installed''' if is_safetensors_available(): import safetensors A__ : str = safetensors.__version__ elif importlib.util.find_spec('''safetensors''' ) is not None: import safetensors A__ : int = F"{safetensors.__version__} but is ignored because of PyTorch version too old." A__ : List[Any] = '''not installed''' A__ : Union[str, Any] = '''not found''' if is_accelerate_available(): import accelerate from accelerate.commands.config import default_config_file, load_config_from_file A__ : Union[str, Any] = accelerate.__version__ # Get the default from the config file. if self._accelerate_config_file is not None or os.path.isfile(UpperCamelCase__ ): A__ : Dict = load_config_from_file(self._accelerate_config_file ).to_dict() A__ : Optional[Any] = ( '''\n'''.join([F"\t- {prop}: {val}" for prop, val in accelerate_config.items()] ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else F"\t{accelerate_config}" ) A__ : List[str] = '''not installed''' A__ : List[str] = '''NA''' if is_torch_available(): import torch A__ : Optional[Any] = torch.__version__ A__ : List[str] = torch.cuda.is_available() A__ : int = '''not installed''' A__ : str = '''NA''' if is_tf_available(): import tensorflow as tf A__ : Dict = tf.__version__ try: # deprecated in v2.1 A__ : List[str] = tf.test.is_gpu_available() except AttributeError: # returns list of devices, convert to bool A__ : str = bool(tf.config.list_physical_devices('''GPU''' ) ) A__ : int = '''not installed''' A__ : Tuple = '''not installed''' A__ : str = '''not installed''' A__ : Optional[int] = '''NA''' if is_flax_available(): import flax import jax import jaxlib A__ : Optional[Any] = flax.__version__ A__ : int = jax.__version__ A__ : Any = jaxlib.__version__ A__ : List[Any] = jax.lib.xla_bridge.get_backend().platform A__ : Tuple = { '''`transformers` version''': version, '''Platform''': platform.platform(), '''Python version''': platform.python_version(), '''Huggingface_hub version''': huggingface_hub.__version__, '''Safetensors version''': F"{safetensors_version}", '''Accelerate version''': F"{accelerate_version}", '''Accelerate config''': F"{accelerate_config_str}", '''PyTorch version (GPU?)''': F"{pt_version} ({pt_cuda_available})", '''Tensorflow version (GPU?)''': F"{tf_version} ({tf_cuda_available})", '''Flax version (CPU?/GPU?/TPU?)''': F"{flax_version} ({jax_backend})", '''Jax version''': F"{jax_version}", '''JaxLib version''': F"{jaxlib_version}", '''Using GPU in script?''': '''<fill in>''', '''Using distributed or parallel set-up in script?''': '''<fill in>''', } print('''\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n''' ) print(self.format_dict(UpperCamelCase__ ) ) return info @staticmethod def __snake_case ( UpperCamelCase__ ): return "\n".join([F"- {prop}: {val}" for prop, val in d.items()] ) + "\n"
55
import json import os import sys import tempfile import unittest from pathlib import Path from shutil import copyfile from huggingface_hub import HfFolder, Repository, create_repo, delete_repo from requests.exceptions import HTTPError import transformers from transformers import ( CONFIG_MAPPING, FEATURE_EXTRACTOR_MAPPING, PROCESSOR_MAPPING, TOKENIZER_MAPPING, AutoConfig, AutoFeatureExtractor, AutoProcessor, AutoTokenizer, BertTokenizer, ProcessorMixin, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaProcessor, ) from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test from transformers.tokenization_utils import TOKENIZER_CONFIG_FILE from transformers.utils import FEATURE_EXTRACTOR_NAME, is_tokenizers_available sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 from test_module.custom_processing import CustomProcessor # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 _SCREAMING_SNAKE_CASE : List[Any] = get_tests_dir('fixtures/dummy_feature_extractor_config.json') _SCREAMING_SNAKE_CASE : int = get_tests_dir('fixtures/vocab.json') _SCREAMING_SNAKE_CASE : Tuple = get_tests_dir('fixtures') class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"] def __snake_case ( self ): A__ : List[Any] = 0 def __snake_case ( self ): A__ : Dict = AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h''' ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Optional[Any] = WavaVecaConfig() A__ : Dict = AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h''' ) # save in new folder model_config.save_pretrained(UpperCamelCase__ ) processor.save_pretrained(UpperCamelCase__ ) A__ : Any = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: # copy relevant files copyfile(UpperCamelCase__ , os.path.join(UpperCamelCase__ , UpperCamelCase__ ) ) copyfile(UpperCamelCase__ , os.path.join(UpperCamelCase__ , '''vocab.json''' ) ) A__ : List[Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Dict = WavaVecaFeatureExtractor() A__ : Union[str, Any] = AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h''' ) A__ : Optional[int] = WavaVecaProcessor(UpperCamelCase__ , UpperCamelCase__ ) # save in new folder processor.save_pretrained(UpperCamelCase__ ) # drop `processor_class` in tokenizer with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''r''' ) as f: A__ : str = json.load(UpperCamelCase__ ) config_dict.pop('''processor_class''' ) with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''w''' ) as f: f.write(json.dumps(UpperCamelCase__ ) ) A__ : Optional[int] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Optional[int] = WavaVecaFeatureExtractor() A__ : List[Any] = AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h''' ) A__ : str = WavaVecaProcessor(UpperCamelCase__ , UpperCamelCase__ ) # save in new folder processor.save_pretrained(UpperCamelCase__ ) # drop `processor_class` in feature extractor with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''r''' ) as f: A__ : List[Any] = json.load(UpperCamelCase__ ) config_dict.pop('''processor_class''' ) with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''w''' ) as f: f.write(json.dumps(UpperCamelCase__ ) ) A__ : List[Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Any = WavaVecaConfig(processor_class='''Wav2Vec2Processor''' ) model_config.save_pretrained(UpperCamelCase__ ) # copy relevant files copyfile(UpperCamelCase__ , os.path.join(UpperCamelCase__ , '''vocab.json''' ) ) # create emtpy sample processor with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''w''' ) as f: f.write('''{}''' ) A__ : Union[str, Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(UpperCamelCase__ ): A__ : Union[str, Any] = AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(UpperCamelCase__ ): A__ : str = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) A__ : int = AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) self.assertTrue(processor.special_attribute_present ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) A__ : List[Any] = processor.feature_extractor self.assertTrue(feature_extractor.special_attribute_present ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) A__ : List[Any] = processor.tokenizer self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) # Test we can also load the slow version A__ : Dict = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ , use_fast=UpperCamelCase__ ) A__ : int = new_processor.tokenizer self.assertTrue(new_tokenizer.special_attribute_present ) self.assertEqual(new_tokenizer.__class__.__name__ , '''NewTokenizer''' ) else: self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) def __snake_case ( self ): try: AutoConfig.register('''custom''' , UpperCamelCase__ ) AutoFeatureExtractor.register(UpperCamelCase__ , UpperCamelCase__ ) AutoTokenizer.register(UpperCamelCase__ , slow_tokenizer_class=UpperCamelCase__ ) AutoProcessor.register(UpperCamelCase__ , UpperCamelCase__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(UpperCamelCase__ ): AutoProcessor.register(UpperCamelCase__ , UpperCamelCase__ ) # Now that the config is registered, it can be used as any other config with the auto-API A__ : Any = CustomFeatureExtractor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: A__ : str = os.path.join(UpperCamelCase__ , '''vocab.txt''' ) with open(UpperCamelCase__ , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens] ) ) A__ : str = CustomTokenizer(UpperCamelCase__ ) A__ : Optional[Any] = CustomProcessor(UpperCamelCase__ , UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained(UpperCamelCase__ ) A__ : Union[str, Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] if CustomConfig in PROCESSOR_MAPPING._extra_content: del PROCESSOR_MAPPING._extra_content[CustomConfig] def __snake_case ( self ): class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = False class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = False class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "AutoFeatureExtractor" _lowerCAmelCase = "AutoTokenizer" _lowerCAmelCase = False try: AutoConfig.register('''custom''' , UpperCamelCase__ ) AutoFeatureExtractor.register(UpperCamelCase__ , UpperCamelCase__ ) AutoTokenizer.register(UpperCamelCase__ , slow_tokenizer_class=UpperCamelCase__ ) AutoProcessor.register(UpperCamelCase__ , UpperCamelCase__ ) # If remote code is not set, the default is to use local classes. A__ : List[Any] = AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor''' ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) self.assertFalse(processor.special_attribute_present ) self.assertFalse(processor.feature_extractor.special_attribute_present ) self.assertFalse(processor.tokenizer.special_attribute_present ) # If remote code is disabled, we load the local ones. A__ : Any = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) self.assertFalse(processor.special_attribute_present ) self.assertFalse(processor.feature_extractor.special_attribute_present ) self.assertFalse(processor.tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub. A__ : Union[str, Any] = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) self.assertTrue(processor.special_attribute_present ) self.assertTrue(processor.feature_extractor.special_attribute_present ) self.assertTrue(processor.tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] if CustomConfig in PROCESSOR_MAPPING._extra_content: del PROCESSOR_MAPPING._extra_content[CustomConfig] def __snake_case ( self ): A__ : str = AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) self.assertEqual(processor.__class__.__name__ , '''BertTokenizerFast''' ) def __snake_case ( self ): A__ : Union[str, Any] = AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-convnext''' ) self.assertEqual(processor.__class__.__name__ , '''ConvNextImageProcessor''' ) @is_staging_test class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"] @classmethod def __snake_case ( cls ): A__ : List[str] = TOKEN HfFolder.save_token(UpperCamelCase__ ) @classmethod def __snake_case ( cls ): try: delete_repo(token=cls._token , repo_id='''test-processor''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-processor-org''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''test-dynamic-processor''' ) except HTTPError: pass def __snake_case ( self ): A__ : Optional[Any] = WavaVecaProcessor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained( os.path.join(UpperCamelCase__ , '''test-processor''' ) , push_to_hub=UpperCamelCase__ , use_auth_token=self._token ) A__ : List[Any] = WavaVecaProcessor.from_pretrained(F"{USER}/test-processor" ) for k, v in processor.feature_extractor.__dict__.items(): self.assertEqual(UpperCamelCase__ , getattr(new_processor.feature_extractor , UpperCamelCase__ ) ) self.assertDictEqual(new_processor.tokenizer.get_vocab() , processor.tokenizer.get_vocab() ) def __snake_case ( self ): A__ : int = WavaVecaProcessor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained( os.path.join(UpperCamelCase__ , '''test-processor-org''' ) , push_to_hub=UpperCamelCase__ , use_auth_token=self._token , organization='''valid_org''' , ) A__ : List[str] = WavaVecaProcessor.from_pretrained('''valid_org/test-processor-org''' ) for k, v in processor.feature_extractor.__dict__.items(): self.assertEqual(UpperCamelCase__ , getattr(new_processor.feature_extractor , UpperCamelCase__ ) ) self.assertDictEqual(new_processor.tokenizer.get_vocab() , processor.tokenizer.get_vocab() ) def __snake_case ( self ): CustomFeatureExtractor.register_for_auto_class() CustomTokenizer.register_for_auto_class() CustomProcessor.register_for_auto_class() A__ : Optional[Any] = CustomFeatureExtractor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: A__ : List[Any] = os.path.join(UpperCamelCase__ , '''vocab.txt''' ) with open(UpperCamelCase__ , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens] ) ) A__ : Union[str, Any] = CustomTokenizer(UpperCamelCase__ ) A__ : List[Any] = CustomProcessor(UpperCamelCase__ , UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: create_repo(F"{USER}/test-dynamic-processor" , token=self._token ) A__ : Union[str, Any] = Repository(UpperCamelCase__ , clone_from=F"{USER}/test-dynamic-processor" , token=self._token ) processor.save_pretrained(UpperCamelCase__ ) # This has added the proper auto_map field to the feature extractor config self.assertDictEqual( processor.feature_extractor.auto_map , { '''AutoFeatureExtractor''': '''custom_feature_extraction.CustomFeatureExtractor''', '''AutoProcessor''': '''custom_processing.CustomProcessor''', } , ) # This has added the proper auto_map field to the tokenizer config with open(os.path.join(UpperCamelCase__ , '''tokenizer_config.json''' ) ) as f: A__ : Optional[int] = json.load(UpperCamelCase__ ) self.assertDictEqual( tokenizer_config['''auto_map'''] , { '''AutoTokenizer''': ['''custom_tokenization.CustomTokenizer''', None], '''AutoProcessor''': '''custom_processing.CustomProcessor''', } , ) # The code has been copied from fixtures self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase__ , '''custom_feature_extraction.py''' ) ) ) self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase__ , '''custom_tokenization.py''' ) ) ) self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase__ , '''custom_processing.py''' ) ) ) repo.push_to_hub() A__ : Tuple = AutoProcessor.from_pretrained(F"{USER}/test-dynamic-processor" , trust_remote_code=UpperCamelCase__ ) # Can't make an isinstance check because the new_processor is from the CustomProcessor class of a dynamic module self.assertEqual(new_processor.__class__.__name__ , '''CustomProcessor''' )
55
1
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : int ) -> str: """simple docstring""" if a < 0 or b < 0: raise ValueError('''the value of both inputs must be positive''' ) A__ : Any = str(bin(__UpperCamelCase ) )[2:] # remove the leading "0b" A__ : Dict = str(bin(__UpperCamelCase ) )[2:] # remove the leading "0b" A__ : Tuple = max(len(__UpperCamelCase ) , len(__UpperCamelCase ) ) return "0b" + "".join( str(int(char_a == '''1''' and char_b == '''1''' ) ) for char_a, char_b in zip(a_binary.zfill(__UpperCamelCase ) , b_binary.zfill(__UpperCamelCase ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
55
from abc import ABC, abstractmethod from argparse import ArgumentParser class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' @staticmethod @abstractmethod def __snake_case ( UpperCamelCase__ ): raise NotImplementedError() @abstractmethod def __snake_case ( self ): raise NotImplementedError()
55
1
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 ): '''simple docstring''' def __snake_case ( self ): A__ : str = tempfile.mkdtemp() A__ : List[str] = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''的''', '''价''', '''格''', '''是''', '''15''', '''便''', '''alex''', '''##andra''', ''',''', '''。''', '''-''', '''t''', '''shirt''', ] A__ : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) A__ : Union[str, Any] = { '''do_resize''': True, '''size''': {'''height''': 224, '''width''': 224}, '''do_center_crop''': True, '''crop_size''': {'''height''': 18, '''width''': 18}, '''do_normalize''': True, '''image_mean''': [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3], '''image_std''': [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1], '''do_convert_rgb''': True, } A__ : Any = os.path.join(self.tmpdirname , UpperCamelCase__ ) with open(self.image_processor_file , '''w''' , encoding='''utf-8''' ) as fp: json.dump(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self , **UpperCamelCase__ ): return BertTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __snake_case ( self , **UpperCamelCase__ ): return BertTokenizerFast.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __snake_case ( self , **UpperCamelCase__ ): return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __snake_case ( self ): shutil.rmtree(self.tmpdirname ) def __snake_case ( self ): A__ : List[Any] = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] A__ : Dict = [Image.fromarray(np.moveaxis(UpperCamelCase__ , 0 , -1 ) ) for x in image_inputs] return image_inputs def __snake_case ( self ): A__ : Optional[Any] = self.get_tokenizer() A__ : Optional[int] = self.get_rust_tokenizer() A__ : Optional[Any] = self.get_image_processor() A__ : Optional[int] = ChineseCLIPProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) processor_slow.save_pretrained(self.tmpdirname ) A__ : Dict = ChineseCLIPProcessor.from_pretrained(self.tmpdirname , use_fast=UpperCamelCase__ ) A__ : List[str] = ChineseCLIPProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) processor_fast.save_pretrained(self.tmpdirname ) A__ : str = 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 , UpperCamelCase__ ) self.assertIsInstance(processor_fast.tokenizer , UpperCamelCase__ ) 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 , UpperCamelCase__ ) self.assertIsInstance(processor_fast.image_processor , UpperCamelCase__ ) def __snake_case ( self ): A__ : Dict = ChineseCLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ : Tuple = self.get_tokenizer(cls_token='''(CLS)''' , sep_token='''(SEP)''' ) A__ : Optional[int] = self.get_image_processor(do_normalize=UpperCamelCase__ ) A__ : Tuple = ChineseCLIPProcessor.from_pretrained( self.tmpdirname , cls_token='''(CLS)''' , sep_token='''(SEP)''' , do_normalize=UpperCamelCase__ ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , UpperCamelCase__ ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase__ ) def __snake_case ( self ): A__ : int = self.get_image_processor() A__ : Tuple = self.get_tokenizer() A__ : int = ChineseCLIPProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) A__ : str = self.prepare_image_inputs() A__ : int = image_processor(UpperCamelCase__ , return_tensors='''np''' ) A__ : List[Any] = processor(images=UpperCamelCase__ , 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 ): A__ : int = self.get_image_processor() A__ : List[Any] = self.get_tokenizer() A__ : Union[str, Any] = ChineseCLIPProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) A__ : int = '''Alexandra,T-shirt的价格是15便士。''' A__ : List[str] = processor(text=UpperCamelCase__ ) A__ : Union[str, Any] = tokenizer(UpperCamelCase__ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __snake_case ( self ): A__ : str = self.get_image_processor() A__ : str = self.get_tokenizer() A__ : List[Any] = ChineseCLIPProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) A__ : Optional[Any] = '''Alexandra,T-shirt的价格是15便士。''' A__ : List[Any] = self.prepare_image_inputs() A__ : str = processor(text=UpperCamelCase__ , images=UpperCamelCase__ ) 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(UpperCamelCase__ ): processor() def __snake_case ( self ): A__ : List[str] = self.get_image_processor() A__ : Tuple = self.get_tokenizer() A__ : Any = ChineseCLIPProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) A__ : List[str] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A__ : Tuple = processor.batch_decode(UpperCamelCase__ ) A__ : Optional[Any] = tokenizer.batch_decode(UpperCamelCase__ ) self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): A__ : Union[str, Any] = self.get_image_processor() A__ : int = self.get_tokenizer() A__ : Union[str, Any] = ChineseCLIPProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) A__ : Union[str, Any] = '''Alexandra,T-shirt的价格是15便士。''' A__ : Tuple = self.prepare_image_inputs() A__ : int = processor(text=UpperCamelCase__ , images=UpperCamelCase__ ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
55
import inspect import unittest from transformers import YolosConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import YolosForObjectDetection, YolosModel from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__=13 , UpperCamelCase__=[30, 30] , UpperCamelCase__=2 , UpperCamelCase__=3 , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=32 , UpperCamelCase__=5 , UpperCamelCase__=4 , UpperCamelCase__=37 , UpperCamelCase__="gelu" , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=10 , UpperCamelCase__=0.0_2 , UpperCamelCase__=3 , UpperCamelCase__=None , UpperCamelCase__=8 , UpperCamelCase__=10 , ): A__ : Optional[int] = parent A__ : List[Any] = batch_size A__ : Dict = image_size A__ : Any = patch_size A__ : Dict = num_channels A__ : List[Any] = is_training A__ : int = use_labels A__ : Any = hidden_size A__ : List[str] = num_hidden_layers A__ : Optional[int] = num_attention_heads A__ : Optional[Any] = intermediate_size A__ : str = hidden_act A__ : str = hidden_dropout_prob A__ : Optional[int] = attention_probs_dropout_prob A__ : Optional[int] = type_sequence_label_size A__ : Any = initializer_range A__ : Optional[int] = num_labels A__ : Union[str, Any] = scope A__ : Union[str, Any] = n_targets A__ : Dict = num_detection_tokens # we set the expected sequence length (which is used in several tests) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens A__ : int = (image_size[1] // patch_size) * (image_size[0] // patch_size) A__ : List[str] = num_patches + 1 + self.num_detection_tokens def __snake_case ( self ): A__ : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]] ) A__ : int = None if self.use_labels: # labels is a list of Dict (each Dict being the labels for a given example in the batch) A__ : Tuple = [] for i in range(self.batch_size ): A__ : List[Any] = {} A__ : Tuple = torch.randint( high=self.num_labels , size=(self.n_targets,) , device=UpperCamelCase__ ) A__ : Any = torch.rand(self.n_targets , 4 , device=UpperCamelCase__ ) labels.append(UpperCamelCase__ ) A__ : Union[str, Any] = self.get_config() return config, pixel_values, labels def __snake_case ( self ): return YolosConfig( 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=UpperCamelCase__ , initializer_range=self.initializer_range , num_detection_tokens=self.num_detection_tokens , num_labels=self.num_labels , ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : Tuple = YolosModel(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : Optional[Any] = model(UpperCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.expected_seq_len, self.hidden_size) ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : Any = YolosForObjectDetection(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : Union[str, Any] = model(pixel_values=UpperCamelCase__ ) A__ : Optional[int] = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape , (self.batch_size, self.num_detection_tokens, 4) ) A__ : Union[str, Any] = model(pixel_values=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape , (self.batch_size, self.num_detection_tokens, 4) ) def __snake_case ( self ): A__ : Optional[int] = self.prepare_config_and_inputs() A__ , A__ , A__ : Optional[Any] = config_and_inputs A__ : Optional[int] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = (YolosModel, YolosForObjectDetection) if is_torch_available() else () _lowerCAmelCase = ( {"feature-extraction": YolosModel, "object-detection": YolosForObjectDetection} if is_torch_available() else {} ) _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=False ): A__ : Optional[int] = super()._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ , return_labels=UpperCamelCase__ ) if return_labels: if model_class.__name__ == "YolosForObjectDetection": A__ : str = [] for i in range(self.model_tester.batch_size ): A__ : int = {} A__ : Dict = torch.ones( size=(self.model_tester.n_targets,) , device=UpperCamelCase__ , dtype=torch.long ) A__ : Dict = torch.ones( self.model_tester.n_targets , 4 , device=UpperCamelCase__ , dtype=torch.float ) labels.append(UpperCamelCase__ ) A__ : Dict = labels return inputs_dict def __snake_case ( self ): A__ : List[Any] = YolosModelTester(self ) A__ : List[str] = ConfigTester(self , config_class=UpperCamelCase__ , has_text_modality=UpperCamelCase__ , hidden_size=37 ) def __snake_case ( self ): self.config_tester.run_common_tests() def __snake_case ( self ): # YOLOS does not use inputs_embeds pass def __snake_case ( self ): A__ , A__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : Any = model_class(UpperCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A__ : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCamelCase__ , nn.Linear ) ) def __snake_case ( self ): A__ , A__ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : List[str] = model_class(UpperCamelCase__ ) A__ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ : Optional[int] = [*signature.parameters.keys()] A__ : Optional[Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , UpperCamelCase__ ) def __snake_case ( self ): A__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase__ ) def __snake_case ( self ): A__ , A__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() A__ : Tuple = True # in YOLOS, the seq_len is different A__ : List[Any] = self.model_tester.expected_seq_len for model_class in self.all_model_classes: A__ : Any = True A__ : Optional[int] = False A__ : Optional[Any] = True A__ : int = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : List[str] = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Optional[int] = outputs.attentions self.assertEqual(len(UpperCamelCase__ ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] A__ : Tuple = True A__ : Optional[Any] = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : Tuple = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Tuple = outputs.attentions self.assertEqual(len(UpperCamelCase__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) A__ : List[Any] = len(UpperCamelCase__ ) # Check attention is always last and order is fine A__ : List[str] = True A__ : List[Any] = True A__ : int = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : Tuple = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Tuple = 1 self.assertEqual(out_len + added_hidden_states , len(UpperCamelCase__ ) ) A__ : List[str] = outputs.attentions self.assertEqual(len(UpperCamelCase__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) def __snake_case ( self ): def check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : str = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : int = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Optional[Any] = outputs.hidden_states A__ : int = getattr( self.model_tester , '''expected_num_hidden_layers''' , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(UpperCamelCase__ ) , UpperCamelCase__ ) # YOLOS has a different seq_length A__ : Union[str, Any] = self.model_tester.expected_seq_len self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) A__ , A__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : int = True check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ : Optional[int] = True check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): A__ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_object_detection(*UpperCamelCase__ ) @slow def __snake_case ( self ): for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ : Union[str, Any] = YolosModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) def SCREAMING_SNAKE_CASE ( ) -> List[str]: """simple docstring""" A__ : 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 __snake_case ( self ): return AutoImageProcessor.from_pretrained('''hustvl/yolos-small''' ) if is_vision_available() else None @slow def __snake_case ( self ): A__ : Tuple = YolosForObjectDetection.from_pretrained('''hustvl/yolos-small''' ).to(UpperCamelCase__ ) A__ : str = self.default_image_processor A__ : Tuple = prepare_img() A__ : Tuple = image_processor(images=UpperCamelCase__ , return_tensors='''pt''' ).to(UpperCamelCase__ ) # forward pass with torch.no_grad(): A__ : Any = model(inputs.pixel_values ) # verify outputs A__ : List[Any] = torch.Size((1, 100, 92) ) self.assertEqual(outputs.logits.shape , UpperCamelCase__ ) A__ : Optional[int] = torch.tensor( [[-2_4.0_2_4_8, -1_0.3_0_2_4, -1_4.8_2_9_0], [-4_2.0_3_9_2, -1_6.8_2_0_0, -2_7.4_3_3_4], [-2_7.2_7_4_3, -1_1.8_1_5_4, -1_8.7_1_4_8]] , device=UpperCamelCase__ , ) A__ : Optional[int] = torch.tensor( [[0.2_5_5_9, 0.5_4_5_5, 0.4_7_0_6], [0.2_9_8_9, 0.7_2_7_9, 0.1_8_7_5], [0.7_7_3_2, 0.4_0_1_7, 0.4_4_6_2]] , device=UpperCamelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , UpperCamelCase__ , atol=1e-4 ) ) self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3] , UpperCamelCase__ , atol=1e-4 ) ) # verify postprocessing A__ : Dict = image_processor.post_process_object_detection( UpperCamelCase__ , threshold=0.3 , target_sizes=[image.size[::-1]] )[0] A__ : int = torch.tensor([0.9_9_9_4, 0.9_7_9_0, 0.9_9_6_4, 0.9_9_7_2, 0.9_8_6_1] ).to(UpperCamelCase__ ) A__ : str = [75, 75, 17, 63, 17] A__ : Tuple = torch.tensor([3_3_5.0_6_0_9, 7_9.3_8_4_8, 3_7_5.4_2_1_6, 1_8_7.2_4_9_5] ).to(UpperCamelCase__ ) self.assertEqual(len(results['''scores'''] ) , 5 ) self.assertTrue(torch.allclose(results['''scores'''] , UpperCamelCase__ , atol=1e-4 ) ) self.assertSequenceEqual(results['''labels'''].tolist() , UpperCamelCase__ ) self.assertTrue(torch.allclose(results['''boxes'''][0, :] , UpperCamelCase__ ) )
55
1
_SCREAMING_SNAKE_CASE : Any = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] _SCREAMING_SNAKE_CASE : Union[str, Any] = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] _SCREAMING_SNAKE_CASE : List[Any] = { 0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday', } def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : int , __UpperCamelCase : int ) -> str: """simple docstring""" assert len(str(__UpperCamelCase ) ) > 2, "year should be in YYYY format" assert 1 <= month <= 12, "month should be between 1 to 12" assert 1 <= day <= 31, "day should be between 1 to 31" # Doomsday algorithm: A__ : str = year // 1_00 A__ : Union[str, Any] = (5 * (century % 4) + 2) % 7 A__ : List[Any] = year % 1_00 A__ : List[str] = centurian % 12 A__ : Optional[Any] = ( (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor ) % 7 A__ : Dict = ( DOOMSDAY_NOT_LEAP[month - 1] if (year % 4 != 0) or (centurian == 0 and (year % 4_00) == 0) else DOOMSDAY_LEAP[month - 1] ) A__ : Union[str, Any] = (dooms_day + day - day_anchor) % 7 return WEEK_DAY_NAMES[week_day] if __name__ == "__main__": import doctest doctest.testmod()
55
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> int: """simple docstring""" if n == 1 or not isinstance(__UpperCamelCase , __UpperCamelCase ): return 0 elif n == 2: return 1 else: A__ : Any = [0, 1] for i in range(2 , n + 1 ): sequence.append(sequence[i - 1] + sequence[i - 2] ) return sequence[n] def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> int: """simple docstring""" A__ : Dict = 0 A__ : Optional[int] = 2 while digits < n: index += 1 A__ : Dict = len(str(fibonacci(__UpperCamelCase ) ) ) return index def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int = 10_00 ) -> int: """simple docstring""" return fibonacci_digits_index(__UpperCamelCase ) if __name__ == "__main__": print(solution(int(str(input()).strip())))
55
1
import math def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> bool: """simple docstring""" assert isinstance(__UpperCamelCase , __UpperCamelCase ) and ( number >= 0 ), "'number' must been an int and positive" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or not number % 2: # Negatives, 0, 1 and all even numbers are not primes return False A__ : Optional[Any] = range(3 , int(math.sqrt(__UpperCamelCase ) + 1 ) , 2 ) return not any(not number % i for i in odd_numbers ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str , __UpperCamelCase : List[Any]=1 , **__UpperCamelCase : str ) -> str: """simple docstring""" A__ : Union[str, Any] = factor * value A__ : str = value while not is_prime(__UpperCamelCase ): value += 1 if not ("desc" in kwargs and kwargs["desc"] is True) else -1 if value == first_value_val: return next_prime(value + 1 , **__UpperCamelCase ) return value
55
_SCREAMING_SNAKE_CASE : List[str] = range(2, 2_0 + 1) _SCREAMING_SNAKE_CASE : Optional[Any] = [1_0**k for k in range(ks[-1] + 1)] _SCREAMING_SNAKE_CASE : dict[int, dict[int, list[list[int]]]] = {} def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any , __UpperCamelCase : Dict , __UpperCamelCase : Any , __UpperCamelCase : Optional[Any] ) -> int: """simple docstring""" A__ : Tuple = sum(a_i[j] for j in range(__UpperCamelCase , len(__UpperCamelCase ) ) ) A__ : Tuple = sum(a_i[j] * base[j] for j in range(min(len(__UpperCamelCase ) , __UpperCamelCase ) ) ) A__ , A__ : Optional[int] = 0, 0 A__ : List[Any] = n - i A__ : Any = memo.get(__UpperCamelCase ) if sub_memo is not None: A__ : Optional[int] = sub_memo.get(__UpperCamelCase ) if jumps is not None and len(__UpperCamelCase ) > 0: # find and make the largest jump without going over A__ : List[Any] = -1 for _k in range(len(__UpperCamelCase ) - 1 , -1 , -1 ): if jumps[_k][2] <= k and jumps[_k][1] <= max_dn: A__ : List[str] = _k break if max_jump >= 0: A__ , A__ , A__ : List[Any] = jumps[max_jump] # since the difference between jumps is cached, add c A__ : int = diff + c for j in range(min(__UpperCamelCase , len(__UpperCamelCase ) ) ): A__ , A__ : List[str] = divmod(__UpperCamelCase , 10 ) if new_c > 0: add(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else: A__ : List[Any] = [] else: A__ : Optional[Any] = {c: []} A__ : int = sub_memo if dn >= max_dn or c + diff >= base[k]: return diff, dn if k > ks[0]: while True: # keep doing smaller jumps A__ , A__ : str = next_term(__UpperCamelCase , k - 1 , i + dn , __UpperCamelCase ) diff += _diff dn += terms_jumped if dn >= max_dn or c + diff >= base[k]: break else: # would be too small a jump, just compute sequential terms instead A__ , A__ : str = compute(__UpperCamelCase , __UpperCamelCase , i + dn , __UpperCamelCase ) diff += _diff dn += terms_jumped A__ : str = sub_memo[c] # keep jumps sorted by # of terms skipped A__ : List[Any] = 0 while j < len(__UpperCamelCase ): if jumps[j][1] > dn: break j += 1 # cache the jump for this value digitsum(b) and c sub_memo[c].insert(__UpperCamelCase , (diff, dn, k) ) return (diff, dn) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : Optional[int] , __UpperCamelCase : List[str] , __UpperCamelCase : int ) -> Any: """simple docstring""" if i >= n: return 0, i if k > len(__UpperCamelCase ): a_i.extend([0 for _ in range(k - len(__UpperCamelCase ) )] ) # note: a_i -> b * 10^k + c # ds_b -> digitsum(b) # ds_c -> digitsum(c) A__ : Optional[Any] = i A__ , A__ , A__ : Dict = 0, 0, 0 for j in range(len(__UpperCamelCase ) ): if j >= k: ds_b += a_i[j] else: ds_c += a_i[j] while i < n: i += 1 A__ : int = ds_c + ds_b diff += addend A__ : List[Any] = 0 for j in range(__UpperCamelCase ): A__ : Optional[Any] = a_i[j] + addend A__ , A__ : List[str] = divmod(__UpperCamelCase , 10 ) ds_c += a_i[j] if addend > 0: break if addend > 0: add(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) return diff, i - start_i def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict , __UpperCamelCase : List[Any] , __UpperCamelCase : int ) -> Tuple: """simple docstring""" for j in range(__UpperCamelCase , len(__UpperCamelCase ) ): A__ : Any = digits[j] + addend if s >= 10: A__ , A__ : Union[str, Any] = divmod(__UpperCamelCase , 10 ) A__ : Optional[int] = addend // 10 + quotient else: A__ : Any = s A__ : Dict = addend // 10 if addend == 0: break while addend > 0: A__ , A__ : Dict = divmod(__UpperCamelCase , 10 ) digits.append(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int = 10**15 ) -> int: """simple docstring""" A__ : List[Any] = [1] A__ : Dict = 1 A__ : Tuple = 0 while True: A__ , A__ : List[str] = next_term(__UpperCamelCase , 20 , i + dn , __UpperCamelCase ) dn += terms_jumped if dn == n - i: break A__ : List[str] = 0 for j in range(len(__UpperCamelCase ) ): a_n += digits[j] * 10**j return a_n if __name__ == "__main__": print(f"""{solution() = }""")
55
1
from typing import List, Optional, Union import numpy as np import PIL import torch from PIL import Image 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, ) _SCREAMING_SNAKE_CASE : Union[str, Any] = logging.get_logger(__name__) # pylint: disable=invalid-name _SCREAMING_SNAKE_CASE : Any = '\n Examples:\n ```py\n >>> from diffusers import KandinskyV22Img2ImgPipeline, KandinskyV22PriorPipeline\n >>> from diffusers.utils import load_image\n >>> import torch\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\n ... "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16\n ... )\n >>> pipe_prior.to("cuda")\n\n >>> prompt = "A red cartoon frog, 4k"\n >>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)\n\n >>> pipe = KandinskyV22Img2ImgPipeline.from_pretrained(\n ... "kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16\n ... )\n >>> pipe.to("cuda")\n\n >>> init_image = load_image(\n ... "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"\n ... "/kandinsky/frog.png"\n ... )\n\n >>> image = pipe(\n ... image=init_image,\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... height=768,\n ... width=768,\n ... num_inference_steps=100,\n ... strength=0.2,\n ... ).images\n\n >>> image[0].save("red_frog.png")\n ```\n' def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] , __UpperCamelCase : Tuple , __UpperCamelCase : Optional[int]=8 ) -> List[str]: """simple docstring""" A__ : Union[str, Any] = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 A__ : List[str] = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any , __UpperCamelCase : int=5_12 , __UpperCamelCase : Optional[Any]=5_12 ) -> List[str]: """simple docstring""" A__ : str = pil_image.resize((w, h) , resample=Image.BICUBIC , reducing_gap=1 ) A__ : str = np.array(pil_image.convert('''RGB''' ) ) A__ : str = arr.astype(np.floataa ) / 1_2_7.5 - 1 A__ : List[str] = np.transpose(__UpperCamelCase , [2, 0, 1] ) A__ : List[str] = torch.from_numpy(__UpperCamelCase ).unsqueeze(0 ) return image class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ): super().__init__() self.register_modules( unet=UpperCamelCase__ , scheduler=UpperCamelCase__ , movq=UpperCamelCase__ , ) A__ : Dict = 2 ** (len(self.movq.config.block_out_channels ) - 1) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): # get the original timestep using init_timestep A__ : Tuple = min(int(num_inference_steps * strength ) , UpperCamelCase__ ) A__ : Optional[Any] = max(num_inference_steps - init_timestep , 0 ) A__ : int = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None ): 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__ )}" ) A__ : List[str] = image.to(device=UpperCamelCase__ , dtype=UpperCamelCase__ ) A__ : Dict = batch_size * num_images_per_prompt if image.shape[1] == 4: A__ : int = image else: 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." ) elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Dict = [ self.movq.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(UpperCamelCase__ ) ] A__ : int = torch.cat(UpperCamelCase__ , dim=0 ) else: A__ : str = self.movq.encode(UpperCamelCase__ ).latent_dist.sample(UpperCamelCase__ ) A__ : int = self.movq.config.scaling_factor * init_latents A__ : List[Any] = torch.cat([init_latents] , dim=0 ) A__ : str = init_latents.shape A__ : str = randn_tensor(UpperCamelCase__ , generator=UpperCamelCase__ , device=UpperCamelCase__ , dtype=UpperCamelCase__ ) # get latents A__ : str = self.scheduler.add_noise(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) A__ : str = init_latents return latents def __snake_case ( self , UpperCamelCase__=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) A__ : Dict = torch.device(F"cuda:{gpu_id}" ) A__ : List[Any] = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__=0 ): if is_accelerate_available() and is_accelerate_version('''>=''' , '''0.17.0.dev0''' ): from accelerate import cpu_offload_with_hook else: raise ImportError('''`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.''' ) A__ : Any = torch.device(F"cuda:{gpu_id}" ) if self.device.type != "cpu": self.to('''cpu''' , silence_dtype_warnings=UpperCamelCase__ ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) A__ : Tuple = None for cpu_offloaded_model in [self.unet, self.movq]: A__ , A__ : Tuple = cpu_offload_with_hook(UpperCamelCase__ , UpperCamelCase__ , prev_module_hook=UpperCamelCase__ ) # We'll offload the last model manually. A__ : int = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def __snake_case ( self ): if not hasattr(self.unet , '''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(UpperCamelCase__ , '''_hf_hook''' ) and hasattr(module._hf_hook , '''execution_device''' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(UpperCamelCase__ ) def __call__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = 512 , UpperCamelCase__ = 512 , UpperCamelCase__ = 100 , UpperCamelCase__ = 4.0 , UpperCamelCase__ = 0.3 , UpperCamelCase__ = 1 , UpperCamelCase__ = None , UpperCamelCase__ = "pil" , UpperCamelCase__ = True , ): A__ : Any = self._execution_device A__ : str = guidance_scale > 1.0 if isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Tuple = torch.cat(UpperCamelCase__ , dim=0 ) A__ : Tuple = image_embeds.shape[0] if isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : List[Any] = torch.cat(UpperCamelCase__ , dim=0 ) if do_classifier_free_guidance: A__ : List[str] = image_embeds.repeat_interleave(UpperCamelCase__ , dim=0 ) A__ : int = negative_image_embeds.repeat_interleave(UpperCamelCase__ , dim=0 ) A__ : Any = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=UpperCamelCase__ ) if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Optional[Any] = [image] if not all(isinstance(UpperCamelCase__ , (PIL.Image.Image, torch.Tensor) ) for i in image ): raise ValueError( F"Input is in incorrect format: {[type(UpperCamelCase__ ) for i in image]}. Currently, we only support PIL image and pytorch tensor" ) A__ : str = torch.cat([prepare_image(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) for i in image] , dim=0 ) A__ : List[Any] = image.to(dtype=image_embeds.dtype , device=UpperCamelCase__ ) A__ : List[str] = self.movq.encode(UpperCamelCase__ )['''latents'''] A__ : Dict = latents.repeat_interleave(UpperCamelCase__ , dim=0 ) self.scheduler.set_timesteps(UpperCamelCase__ , device=UpperCamelCase__ ) A__ , A__ : Optional[int] = self.get_timesteps(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) A__ : List[Any] = timesteps[:1].repeat(batch_size * num_images_per_prompt ) A__ , A__ : Any = downscale_height_and_width(UpperCamelCase__ , UpperCamelCase__ , self.movq_scale_factor ) A__ : List[str] = self.prepare_latents( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , image_embeds.dtype , UpperCamelCase__ , UpperCamelCase__ ) for i, t in enumerate(self.progress_bar(UpperCamelCase__ ) ): # expand the latents if we are doing classifier free guidance A__ : Tuple = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents A__ : List[str] = {'''image_embeds''': image_embeds} A__ : Optional[int] = self.unet( sample=UpperCamelCase__ , timestep=UpperCamelCase__ , encoder_hidden_states=UpperCamelCase__ , added_cond_kwargs=UpperCamelCase__ , return_dict=UpperCamelCase__ , )[0] if do_classifier_free_guidance: A__ , A__ : Any = noise_pred.split(latents.shape[1] , dim=1 ) A__ , A__ : Optional[int] = noise_pred.chunk(2 ) A__ , A__ : List[str] = variance_pred.chunk(2 ) A__ : List[Any] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) A__ : Optional[int] = 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"] ): A__ , A__ : Optional[int] = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 A__ : str = self.scheduler.step( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , generator=UpperCamelCase__ , )[0] # post-processing A__ : List[Any] = self.movq.decode(UpperCamelCase__ , force_not_quantize=UpperCamelCase__ )['''sample'''] if output_type not in ["pt", "np", "pil"]: raise ValueError(F"Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}" ) if output_type in ["np", "pil"]: A__ : Any = image * 0.5 + 0.5 A__ : List[str] = image.clamp(0 , 1 ) A__ : Dict = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": A__ : Tuple = self.numpy_to_pil(UpperCamelCase__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=UpperCamelCase__ )
55
import asyncio import os import shutil import subprocess import sys import tempfile import unittest from distutils.util import strtobool from functools import partial from pathlib import Path from typing import List, Union from unittest import mock import torch from ..state import AcceleratorState, PartialState from ..utils import ( gather, is_bnb_available, is_comet_ml_available, is_datasets_available, is_deepspeed_available, is_mps_available, is_safetensors_available, is_tensorboard_available, is_torch_version, is_tpu_available, is_transformers_available, is_wandb_available, is_xpu_available, ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] , __UpperCamelCase : int=False ) -> Tuple: """simple docstring""" try: A__ : Dict = os.environ[key] except KeyError: # KEY isn't set, default to `default`. A__ : Tuple = default else: # KEY is set, convert it to True or False. try: A__ : Union[str, Any] = strtobool(__UpperCamelCase ) except ValueError: # More values are supported, but let's keep the message simple. raise ValueError(F"If set, {key} must be yes or no." ) return _value _SCREAMING_SNAKE_CASE : Union[str, Any] = parse_flag_from_env('RUN_SLOW', default=False) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] ) -> Any: """simple docstring""" return unittest.skip('''Test was skipped''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> Union[str, Any]: """simple docstring""" return unittest.skipUnless(_run_slow_tests , '''test is slow''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> int: """simple docstring""" return unittest.skipUnless(not torch.cuda.is_available() , '''test requires only a CPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] ) -> Tuple: """simple docstring""" return unittest.skipUnless(torch.cuda.is_available() , '''test requires a GPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict ) -> List[str]: """simple docstring""" return unittest.skipUnless(is_xpu_available() , '''test requires a XPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict ) -> Any: """simple docstring""" return unittest.skipUnless(is_mps_available() , '''test requires a `mps` backend support in `torch`''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Optional[Any]: """simple docstring""" return unittest.skipUnless( is_transformers_available() and is_datasets_available() , '''test requires the Hugging Face suite''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> Tuple: """simple docstring""" return unittest.skipUnless(is_bnb_available() , '''test requires the bitsandbytes library''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> List[Any]: """simple docstring""" return unittest.skipUnless(is_tpu_available() , '''test requires TPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Tuple: """simple docstring""" return unittest.skipUnless(torch.cuda.device_count() == 1 , '''test requires a GPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Dict: """simple docstring""" return unittest.skipUnless(torch.xpu.device_count() == 1 , '''test requires a XPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> str: """simple docstring""" return unittest.skipUnless(torch.cuda.device_count() > 1 , '''test requires multiple GPUs''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Any: """simple docstring""" return unittest.skipUnless(torch.xpu.device_count() > 1 , '''test requires multiple XPUs''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> int: """simple docstring""" return unittest.skipUnless(is_safetensors_available() , '''test requires safetensors''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> Optional[Any]: """simple docstring""" return unittest.skipUnless(is_deepspeed_available() , '''test requires DeepSpeed''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> List[Any]: """simple docstring""" return unittest.skipUnless(is_torch_version('''>=''' , '''1.12.0''' ) , '''test requires torch version >= 1.12.0''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[int]=None , __UpperCamelCase : List[Any]=None ) -> Optional[Any]: """simple docstring""" if test_case is None: return partial(__UpperCamelCase , version=__UpperCamelCase ) return unittest.skipUnless(is_torch_version('''>=''' , __UpperCamelCase ) , F"test requires torch version >= {version}" )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] ) -> Optional[int]: """simple docstring""" return unittest.skipUnless(is_tensorboard_available() , '''test requires Tensorboard''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> Tuple: """simple docstring""" return unittest.skipUnless(is_wandb_available() , '''test requires wandb''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> Any: """simple docstring""" return unittest.skipUnless(is_comet_ml_available() , '''test requires comet_ml''' )(__UpperCamelCase ) _SCREAMING_SNAKE_CASE : Union[str, Any] = ( any([is_wandb_available(), is_tensorboard_available()]) and not is_comet_ml_available() ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[int] ) -> Optional[Any]: """simple docstring""" return unittest.skipUnless( _atleast_one_tracker_available , '''test requires at least one tracker to be available and for `comet_ml` to not be installed''' , )(__UpperCamelCase ) class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = True @classmethod def __snake_case ( cls ): A__ : Tuple = tempfile.mkdtemp() @classmethod def __snake_case ( cls ): if os.path.exists(cls.tmpdir ): shutil.rmtree(cls.tmpdir ) def __snake_case ( self ): if self.clear_on_setup: for path in Path(self.tmpdir ).glob('''**/*''' ): if path.is_file(): path.unlink() elif path.is_dir(): shutil.rmtree(UpperCamelCase__ ) class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): super().tearDown() # Reset the state of the AcceleratorState singleton. AcceleratorState._reset_state() PartialState._reset_state() class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self , UpperCamelCase__ ): A__ : Tuple = mocks if isinstance(UpperCamelCase__ , (tuple, list) ) else [mocks] for m in self.mocks: m.start() self.addCleanup(m.stop ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> Any: """simple docstring""" A__ : int = AcceleratorState() A__ : Any = tensor[None].clone().to(state.device ) A__ : Optional[int] = gather(__UpperCamelCase ).cpu() A__ : Any = tensor[0].cpu() for i in range(tensors.shape[0] ): if not torch.equal(tensors[i] , __UpperCamelCase ): return False return True class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : List[Any] = returncode A__ : Union[str, Any] = stdout A__ : Dict = stderr async def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str , __UpperCamelCase : Optional[Any] ) -> Any: """simple docstring""" while True: A__ : Tuple = await stream.readline() if line: callback(__UpperCamelCase ) else: break async def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] , __UpperCamelCase : Optional[Any]=None , __UpperCamelCase : List[Any]=None , __UpperCamelCase : Tuple=None , __UpperCamelCase : Tuple=False , __UpperCamelCase : List[Any]=False ) -> _RunOutput: """simple docstring""" if echo: print('''\nRunning: ''' , ''' '''.join(__UpperCamelCase ) ) A__ : int = await asyncio.create_subprocess_exec( cmd[0] , *cmd[1:] , stdin=__UpperCamelCase , stdout=asyncio.subprocess.PIPE , stderr=asyncio.subprocess.PIPE , env=__UpperCamelCase , ) # note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe # https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait # # If it starts hanging, will need to switch to the following code. The problem is that no data # will be seen until it's done and if it hangs for example there will be no debug info. # out, err = await p.communicate() # return _RunOutput(p.returncode, out, err) A__ : List[Any] = [] A__ : str = [] def tee(__UpperCamelCase : Optional[Any] , __UpperCamelCase : Union[str, Any] , __UpperCamelCase : Dict , __UpperCamelCase : List[Any]="" ): A__ : Optional[Any] = line.decode('''utf-8''' ).rstrip() sink.append(__UpperCamelCase ) if not quiet: print(__UpperCamelCase , __UpperCamelCase , file=__UpperCamelCase ) # XXX: the timeout doesn't seem to make any difference here await asyncio.wait( [ asyncio.create_task(_read_stream(p.stdout , lambda __UpperCamelCase : tee(__UpperCamelCase , __UpperCamelCase , sys.stdout , label='''stdout:''' ) ) ), asyncio.create_task(_read_stream(p.stderr , lambda __UpperCamelCase : tee(__UpperCamelCase , __UpperCamelCase , sys.stderr , label='''stderr:''' ) ) ), ] , timeout=__UpperCamelCase , ) return _RunOutput(await p.wait() , __UpperCamelCase , __UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] , __UpperCamelCase : Any=None , __UpperCamelCase : List[Any]=None , __UpperCamelCase : List[str]=1_80 , __UpperCamelCase : List[str]=False , __UpperCamelCase : Dict=True ) -> _RunOutput: """simple docstring""" A__ : Dict = asyncio.get_event_loop() A__ : Optional[Any] = loop.run_until_complete( _stream_subprocess(__UpperCamelCase , env=__UpperCamelCase , stdin=__UpperCamelCase , timeout=__UpperCamelCase , quiet=__UpperCamelCase , echo=__UpperCamelCase ) ) A__ : Union[str, Any] = ''' '''.join(__UpperCamelCase ) if result.returncode > 0: A__ : Optional[Any] = '''\n'''.join(result.stderr ) raise RuntimeError( F"'{cmd_str}' failed with returncode {result.returncode}\n\n" F"The combined stderr from workers follows:\n{stderr}" ) return result class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] , __UpperCamelCase : List[Any]=False ) -> Dict: """simple docstring""" try: A__ : List[Any] = subprocess.check_output(__UpperCamelCase , stderr=subprocess.STDOUT ) if return_stdout: if hasattr(__UpperCamelCase , '''decode''' ): A__ : Any = output.decode('''utf-8''' ) return output except subprocess.CalledProcessError as e: raise SubprocessCallException( F"Command `{' '.join(__UpperCamelCase )}` failed with the following error:\n\n{e.output.decode()}" ) from e
55
1
from json import JSONDecodeError # Workaround for requests.exceptions.JSONDecodeError import requests def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str = "isbn/0140328726" ) -> dict: """simple docstring""" A__ : List[str] = olid.strip().strip('''/''' ) # Remove leading/trailing whitespace & slashes if new_olid.count('''/''' ) != 1: A__ : str = F"{olid} is not a valid Open Library olid" raise ValueError(__UpperCamelCase ) return requests.get(F"https://openlibrary.org/{new_olid}.json" ).json() def SCREAMING_SNAKE_CASE ( __UpperCamelCase : dict ) -> dict: """simple docstring""" A__ : int = { '''title''': '''Title''', '''publish_date''': '''Publish date''', '''authors''': '''Authors''', '''number_of_pages''': '''Number of pages:''', '''first_sentence''': '''First sentence''', '''isbn_10''': '''ISBN (10)''', '''isbn_13''': '''ISBN (13)''', } A__ : Union[str, Any] = {better_key: ol_book_data[key] for key, better_key in desired_keys.items()} A__ : int = [ get_openlibrary_data(author['''key'''] )['''name'''] for author in data['''Authors'''] ] A__ : int = data['''First sentence''']['''value'''] for key, value in data.items(): if isinstance(__UpperCamelCase , __UpperCamelCase ): A__ : Union[str, Any] = ''', '''.join(__UpperCamelCase ) return data if __name__ == "__main__": import doctest doctest.testmod() while True: _SCREAMING_SNAKE_CASE : Optional[int] = input('\nEnter the ISBN code to search (or \'quit\' to stop): ').strip() if isbn.lower() in ("", "q", "quit", "exit", "stop"): break if len(isbn) not in (1_0, 1_3) or not isbn.isdigit(): print(f"""Sorry, {isbn} is not a valid ISBN. Please, input a valid ISBN.""") continue print(f"""\nSearching Open Library for ISBN: {isbn}...\n""") try: _SCREAMING_SNAKE_CASE : Dict = summarize_book(get_openlibrary_data(f"""isbn/{isbn}""")) print('\n'.join(f"""{key}: {value}""" for key, value in book_summary.items())) except JSONDecodeError: # Workaround for requests.exceptions.RequestException: print(f"""Sorry, there are no results for ISBN: {isbn}.""")
55
import numpy as np _SCREAMING_SNAKE_CASE : Any = [ ['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'k'], ['l', 'm', 'n', 'o', 'p'], ['q', 'r', 's', 't', 'u'], ['v', 'w', 'x', 'y', 'z'], ] class UpperCamelCase__ : '''simple docstring''' def __init__( self ): A__ : List[Any] = np.array(UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): A__ , A__ : Any = np.where(letter == self.SQUARE ) A__ : int = np.concatenate([indexa + 1, indexa + 1] ) return indexes def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ ): A__ : Union[str, Any] = self.SQUARE[indexa - 1, indexa - 1] return letter def __snake_case ( self , UpperCamelCase__ ): A__ : List[str] = message.lower() A__ : str = message.replace(''' ''' , '''''' ) A__ : Union[str, Any] = message.replace('''j''' , '''i''' ) A__ : List[Any] = np.empty((2, len(UpperCamelCase__ )) ) for letter_index in range(len(UpperCamelCase__ ) ): A__ : Any = self.letter_to_numbers(message[letter_index] ) A__ : Optional[Any] = numbers[0] A__ : List[str] = numbers[1] A__ : List[str] = first_step.reshape(2 * len(UpperCamelCase__ ) ) A__ : List[Any] = '''''' for numbers_index in range(len(UpperCamelCase__ ) ): A__ : Dict = int(second_step[numbers_index * 2] ) A__ : List[str] = int(second_step[(numbers_index * 2) + 1] ) A__ : Dict = self.numbers_to_letter(UpperCamelCase__ , UpperCamelCase__ ) A__ : Tuple = encoded_message + letter return encoded_message def __snake_case ( self , UpperCamelCase__ ): A__ : str = message.lower() message.replace(''' ''' , '''''' ) A__ : List[Any] = np.empty(2 * len(UpperCamelCase__ ) ) for letter_index in range(len(UpperCamelCase__ ) ): A__ : List[str] = self.letter_to_numbers(message[letter_index] ) A__ : Dict = numbers[0] A__ : int = numbers[1] A__ : Optional[Any] = first_step.reshape((2, len(UpperCamelCase__ )) ) A__ : int = '''''' for numbers_index in range(len(UpperCamelCase__ ) ): A__ : Tuple = int(second_step[0, numbers_index] ) A__ : Dict = int(second_step[1, numbers_index] ) A__ : List[str] = self.numbers_to_letter(UpperCamelCase__ , UpperCamelCase__ ) A__ : Tuple = decoded_message + letter return decoded_message
55
1
import argparse import json import os from collections import OrderedDict import numpy as np import tensorflow as tf import torch def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Union[str, Any] ) -> Optional[Any]: """simple docstring""" A__ : Optional[Any] = os.path.join(args.tf_model_dir , '''parameters.json''' ) A__ : List[str] = json.loads(open(__UpperCamelCase ).read() ) if not params: raise ValueError( F"It seems that the json file at {parameter_file} is empty. Make sure you have a correct json file." ) if not args.output.endswith('''.pt''' ): A__ : Dict = args.output + '''.pt''' A__ : int = OrderedDict() with tf.device('''/CPU:0''' ): A__ : Optional[Any] = tf.train.load_checkpoint(args.tf_model_dir ) A__ : List[str] = reader.get_variable_to_shape_map() for key_name in shapes.keys(): A__ : int = reader.get_tensor(__UpperCamelCase ).astype(np.floataa ) if key_name.endswith('''/adam_m''' ) or key_name.endswith('''/adam_v''' ): continue if key_name.startswith('''pasts/''' ): if key_name.startswith('''pasts/mlp''' ): A__ : Optional[Any] = int(key_name[9] ) elif key_name.startswith('''pasts/out''' ): A__ : Tuple = 8 A__ : Dict = '''model.sqout.%d.weight''' % (player * 2) # enter to nn.Sequencial with Tanh, so 2 at a time A__ : Dict = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix A__ : Dict = torch.tensor(__UpperCamelCase ) elif key_name.startswith('''model/moe''' ): A__ : Union[str, Any] = int(key_name[9:].split('''/''' )[0] ) if key_name.endswith('''/switch_gating/kernel''' ): A__ : Optional[int] = '''model.blocks.%d.feed_forward.mlp.router.classifier.weight''' % player A__ : Union[str, Any] = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix A__ : List[str] = torch.tensor(__UpperCamelCase ) elif key_name.endswith('''/softmlp/kernel''' ): A__ : List[Any] = '''model.blocks.%d.feed_forward.soft_bypass_mlp.weight''' % player A__ : List[Any] = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix A__ : Union[str, Any] = torch.tensor(__UpperCamelCase ) elif key_name.endswith('''/wo/kernel''' ) or key_name.endswith('''/wi/kernel''' ): A__ : List[str] = key_name[-9:-7] for i in range(16 ): A__ : Dict = '''model.blocks.%d.feed_forward.mlp.experts.expert_%d.%s.weight''' % (player, i, nlayer) A__ : List[Any] = ( vnp[i].transpose([1, 0] ).copy() ) # In Mesh-Tensorflow, it is one array, so it is divided A__ : List[Any] = torch.tensor(__UpperCamelCase ) elif key_name.startswith('''model/mlp''' ): A__ : Tuple = int(key_name[9:].split('''/''' )[0] ) if key_name.endswith('''/p1/kernel''' ): A__ : List[str] = '''model.blocks.%d.feed_forward.mlp.wi.weight''' % player A__ : Optional[Any] = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix A__ : List[Any] = torch.tensor(__UpperCamelCase ) elif key_name.endswith('''/p1/bias''' ): A__ : List[Any] = '''model.blocks.%d.feed_forward.mlp.wi.bias''' % player A__ : Any = vnp.copy() # same because it is one dimensional A__ : Tuple = torch.tensor(__UpperCamelCase ) elif key_name.endswith('''/p2/kernel''' ): A__ : str = '''model.blocks.%d.feed_forward.mlp.wo.weight''' % player A__ : Dict = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix A__ : Union[str, Any] = torch.tensor(__UpperCamelCase ) elif key_name.endswith('''/p2/bias''' ): A__ : List[Any] = '''model.blocks.%d.feed_forward.mlp.wo.bias''' % player A__ : List[str] = vnp.copy() # same because it is one dimensional A__ : Union[str, Any] = torch.tensor(__UpperCamelCase ) elif key_name.startswith('''model/ln''' ): A__ : Optional[Any] = int(key_name[8:].split('''/''' )[0] ) if key_name.endswith('''/b''' ): A__ : Optional[Any] = '''model.blocks.%d.feed_forward.norm.bias''' % player A__ : Union[str, Any] = vnp.copy() # same because it is one dimensional A__ : Optional[Any] = torch.tensor(__UpperCamelCase ) elif key_name.endswith('''/g''' ): A__ : List[str] = '''model.blocks.%d.feed_forward.norm.weight''' % player A__ : List[Any] = vnp.copy() # same because it is one dimensional A__ : Tuple = torch.tensor(__UpperCamelCase ) elif key_name.startswith('''model/att''' ): A__ : Any = int(key_name[9:].split('''/''' )[0] ) if key_name.endswith('''/qkv/kernel''' ): A__ : Union[str, Any] = vnp.copy() # Compute same dimension as Mesh-tensorflow using einsum A__ : List[Any] = state[:, 0, :, :] A__ : str = state[:, 1, :, :] A__ : Optional[Any] = state[:, 2, :, :] A__ : List[Any] = ( state_q.reshape([state_q.shape[0], state_q.shape[1] * state_q.shape[2]] ) .transpose([1, 0] ) .copy() ) # Mesh-Tensorflow is a diagonal matrix A__ : Optional[int] = ( state_k.reshape([state_k.shape[0], state_k.shape[1] * state_k.shape[2]] ) .transpose([1, 0] ) .copy() ) # Mesh-Tensorflow is a diagonal matrix A__ : Optional[int] = ( state_v.reshape([state_v.shape[0], state_v.shape[1] * state_v.shape[2]] ) .transpose([1, 0] ) .copy() ) # Mesh-Tensorflow is a diagonal matrix A__ : Optional[Any] = '''model.blocks.%d.self_attn.self_attn.q_proj.weight''' % player A__ : Optional[Any] = torch.tensor(__UpperCamelCase ) A__ : Optional[int] = '''model.blocks.%d.self_attn.self_attn.k_proj.weight''' % player A__ : Optional[int] = torch.tensor(__UpperCamelCase ) A__ : Dict = '''model.blocks.%d.self_attn.self_attn.v_proj.weight''' % player A__ : Tuple = torch.tensor(__UpperCamelCase ) elif key_name.endswith('''/o/kernel''' ): A__ : Optional[int] = '''model.blocks.%d.self_attn.self_attn.out_proj.weight''' % player A__ : Union[str, Any] = ( vnp.reshape([vnp.shape[0] * vnp.shape[1], vnp.shape[2]] ).transpose([1, 0] ).copy() ) # Mesh-Tensorflow is a diagonal matrix A__ : List[Any] = torch.tensor(__UpperCamelCase ) elif key_name.startswith('''model/an''' ): A__ : Optional[Any] = int(key_name[8:].split('''/''' )[0] ) if key_name.endswith('''/b''' ): A__ : int = '''model.blocks.%d.self_attn.norm.bias''' % player A__ : Union[str, Any] = vnp.copy() # same because it is one dimensional A__ : Union[str, Any] = torch.tensor(__UpperCamelCase ) elif key_name.endswith('''/g''' ): A__ : int = '''model.blocks.%d.self_attn.norm.weight''' % player A__ : int = vnp.copy() # same because it is one dimensional A__ : str = torch.tensor(__UpperCamelCase ) elif ( key_name.startswith('''model/wte''' ) or key_name.startswith('''model/wpe''' ) or key_name.startswith('''model/ete''' ) ): A__ : List[str] = {'''wte''': '''embed_tokens''', '''wpe''': '''position_embeddings''', '''ete''': '''extra_position_embeddings'''}[ key_name[-3:] ] A__ : Optional[Any] = '''model.%s.weight''' % nlayer A__ : List[str] = vnp.copy() # same in embedded A__ : int = torch.tensor(__UpperCamelCase ) if key_name.startswith('''model/wte''' ): A__ : Optional[int] = '''lm_head.weight''' A__ : Any = vnp.copy() # same in embedded A__ : str = torch.tensor(__UpperCamelCase ) elif key_name.startswith('''model/wob''' ): A__ : Any = '''final_logits_bias''' A__ : Union[str, Any] = vnp.copy() # same in embedded A__ : Dict = state.reshape((1, -1) ) A__ : Optional[Any] = torch.tensor(__UpperCamelCase ) elif key_name == "model/dense/kernel": A__ : Any = '''model.last_project.weight''' A__ : str = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix A__ : int = torch.tensor(__UpperCamelCase ) elif key_name == "model/dense_1/bias": A__ : List[Any] = '''model.last_project.bias''' A__ : Any = vnp.copy() # same because it is one dimensional A__ : int = torch.tensor(__UpperCamelCase ) torch.save(__UpperCamelCase , args.output ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE : Optional[int] = argparse.ArgumentParser( description='model converter.', formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument('--tf_model_dir', metavar='PATH', type=str, required=True, help='import model') parser.add_argument('--output', metavar='PATH', type=str, required=True, help='output model') _SCREAMING_SNAKE_CASE : List[str] = parser.parse_args() convert_tf_gptsan_to_pt(args)
55
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline else: from .pipeline_kandinsky import KandinskyPipeline from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput from .text_encoder import MultilingualCLIP
55
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) _SCREAMING_SNAKE_CASE : Tuple = { 'configuration_swiftformer': [ 'SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'SwiftFormerConfig', 'SwiftFormerOnnxConfig', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Any = [ 'SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'SwiftFormerForImageClassification', 'SwiftFormerModel', 'SwiftFormerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_swiftformer import ( SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, SwiftFormerConfig, SwiftFormerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swiftformer import ( SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, SwiftFormerForImageClassification, SwiftFormerModel, SwiftFormerPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
55
import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to properly calculate the metrics on the # validation dataset when in a distributed system, and builds off the # `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _SCREAMING_SNAKE_CASE : str = 1_6 _SCREAMING_SNAKE_CASE : Tuple = 3_2 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Accelerator , __UpperCamelCase : int = 16 ) -> Optional[int]: """simple docstring""" A__ : List[str] = AutoTokenizer.from_pretrained('''bert-base-cased''' ) A__ : Optional[int] = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__UpperCamelCase : Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) A__ : int = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=__UpperCamelCase , max_length=__UpperCamelCase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): A__ : Optional[int] = datasets.map( __UpperCamelCase , batched=__UpperCamelCase , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library A__ : List[Any] = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__UpperCamelCase : Any ): # On TPU it's best to pad everything to the same length or training will be very slow. A__ : Optional[Any] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": A__ : int = 16 elif accelerator.mixed_precision != "no": A__ : Any = 8 else: A__ : Union[str, Any] = None return tokenizer.pad( __UpperCamelCase , padding='''longest''' , max_length=__UpperCamelCase , pad_to_multiple_of=__UpperCamelCase , return_tensors='''pt''' , ) # Instantiate dataloaders. A__ : Optional[int] = DataLoader( tokenized_datasets['''train'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) A__ : Tuple = DataLoader( tokenized_datasets['''validation'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('TESTING_MOCKED_DATALOADERS', None) == "1": from accelerate.test_utils.training import mocked_dataloaders _SCREAMING_SNAKE_CASE : Dict = mocked_dataloaders # noqa: F811 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : List[Any] ) -> Optional[Any]: """simple docstring""" if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , __UpperCamelCase ) == "1": A__ : List[str] = 2 # Initialize accelerator A__ : Optional[Any] = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs A__ : Tuple = config['''lr'''] A__ : Dict = int(config['''num_epochs'''] ) A__ : int = int(config['''seed'''] ) A__ : Optional[Any] = int(config['''batch_size'''] ) A__ : int = evaluate.load('''glue''' , '''mrpc''' ) # If the batch size is too big we use gradient accumulation A__ : Union[str, Any] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: A__ : List[Any] = batch_size // MAX_GPU_BATCH_SIZE A__ : Dict = MAX_GPU_BATCH_SIZE set_seed(__UpperCamelCase ) A__ , A__ : int = get_dataloaders(__UpperCamelCase , __UpperCamelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) A__ : Optional[int] = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=__UpperCamelCase ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). A__ : Tuple = model.to(accelerator.device ) # Instantiate optimizer A__ : Optional[int] = AdamW(params=model.parameters() , lr=__UpperCamelCase ) # Instantiate scheduler A__ : Any = get_linear_schedule_with_warmup( optimizer=__UpperCamelCase , num_warmup_steps=1_00 , num_training_steps=(len(__UpperCamelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. A__ , A__ , A__ , A__ , A__ : Dict = accelerator.prepare( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # Now we train the model for epoch in range(__UpperCamelCase ): model.train() for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) A__ : Dict = model(**__UpperCamelCase ) A__ : Dict = outputs.loss A__ : List[str] = loss / gradient_accumulation_steps accelerator.backward(__UpperCamelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() A__ : Optional[int] = 0 for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): A__ : Union[str, Any] = model(**__UpperCamelCase ) A__ : int = outputs.logits.argmax(dim=-1 ) A__ , A__ : Optional[Any] = accelerator.gather((predictions, batch['''labels''']) ) # New Code # # First we check if it's a distributed system if accelerator.use_distributed: # Then see if we're on the last batch of our eval dataloader if step == len(__UpperCamelCase ) - 1: # Last batch needs to be truncated on distributed systems as it contains additional samples A__ : Tuple = predictions[: len(eval_dataloader.dataset ) - samples_seen] A__ : int = references[: len(eval_dataloader.dataset ) - samples_seen] else: # Otherwise we add the number of samples seen samples_seen += references.shape[0] # All of this can be avoided if you use `Accelerator.gather_for_metrics` instead of `Accelerator.gather`: # accelerator.gather_for_metrics((predictions, batch["labels"])) metric.add_batch( predictions=__UpperCamelCase , references=__UpperCamelCase , ) A__ : Union[str, Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}:" , __UpperCamelCase ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: """simple docstring""" A__ : Tuple = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=__UpperCamelCase , default=__UpperCamelCase , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) A__ : Dict = parser.parse_args() A__ : Any = {'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": main()
55
1
from .imports import is_rich_available if is_rich_available(): from rich.traceback import install install(show_locals=False) else: raise ModuleNotFoundError('To use the rich extension, install rich with `pip install rich`')
55
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor from ..utils import is_datasets_available from .base import PipelineTool if is_datasets_available(): from datasets import load_dataset class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "microsoft/speecht5_tts" _lowerCAmelCase = ( "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the " "text to read (in English) and returns a waveform object containing the sound." ) _lowerCAmelCase = "text_reader" _lowerCAmelCase = SpeechTaProcessor _lowerCAmelCase = SpeechTaForTextToSpeech _lowerCAmelCase = SpeechTaHifiGan _lowerCAmelCase = ["text"] _lowerCAmelCase = ["audio"] def __snake_case ( self ): if self.post_processor is None: A__ : int = '''microsoft/speecht5_hifigan''' super().setup() def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__=None ): A__ : List[Any] = self.pre_processor(text=UpperCamelCase__ , return_tensors='''pt''' , truncation=UpperCamelCase__ ) if speaker_embeddings is None: if not is_datasets_available(): raise ImportError('''Datasets needs to be installed if not passing speaker embeddings.''' ) A__ : List[Any] = load_dataset('''Matthijs/cmu-arctic-xvectors''' , split='''validation''' ) A__ : Dict = torch.tensor(embeddings_dataset[7305]['''xvector'''] ).unsqueeze(0 ) return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings} def __snake_case ( self , UpperCamelCase__ ): with torch.no_grad(): return self.model.generate_speech(**UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): with torch.no_grad(): return self.post_processor(UpperCamelCase__ ).cpu().detach()
55
1
import uuid from typing import Any, Dict, List, Optional, Union from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch _SCREAMING_SNAKE_CASE : List[Any] = logging.get_logger(__name__) class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__=None , UpperCamelCase__=None ): if not conversation_id: A__ : Any = uuid.uuida() if past_user_inputs is None: A__ : List[str] = [] if generated_responses is None: A__ : Union[str, Any] = [] A__ : uuid.UUID = conversation_id A__ : List[str] = past_user_inputs A__ : List[str] = generated_responses A__ : Optional[str] = text def __eq__( self , UpperCamelCase__ ): if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): return False if self.uuid == other.uuid: return True return ( self.new_user_input == other.new_user_input and self.past_user_inputs == other.past_user_inputs and self.generated_responses == other.generated_responses ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ = False ): if self.new_user_input: if overwrite: logger.warning( F"User input added while unprocessed input was existing: \"{self.new_user_input}\" was overwritten " F"with: \"{text}\"." ) A__ : Dict = text else: logger.warning( F"User input added while unprocessed input was existing: \"{self.new_user_input}\" new input " F"ignored: \"{text}\". Set `overwrite` to True to overwrite unprocessed user input" ) else: A__ : Optional[int] = text def __snake_case ( self ): if self.new_user_input: self.past_user_inputs.append(self.new_user_input ) A__ : List[str] = None def __snake_case ( self , UpperCamelCase__ ): self.generated_responses.append(UpperCamelCase__ ) def __snake_case ( self ): for user_input, generated_response in zip(self.past_user_inputs , self.generated_responses ): yield True, user_input yield False, generated_response if self.new_user_input: yield True, self.new_user_input def __repr__( self ): A__ : Tuple = F"Conversation id: {self.uuid} \n" for is_user, text in self.iter_texts(): A__ : str = '''user''' if is_user else '''bot''' output += F"{name} >> {text} \n" return output @add_end_docstrings( SCREAMING_SNAKE_CASE_, R"\n min_length_for_response (`int`, *optional*, defaults to 32):\n The minimum length (in number of tokens) for a response.\n minimum_tokens (`int`, *optional*, defaults to 10):\n The minimum length of tokens to leave for a response.\n ", ) class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ): super().__init__(*UpperCamelCase__ , **UpperCamelCase__ ) if self.tokenizer.pad_token_id is None: A__ : Tuple = self.tokenizer.eos_token def __snake_case ( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , **UpperCamelCase__ ): A__ : str = {} A__ : Optional[int] = {} A__ : Optional[int] = {} if min_length_for_response is not None: A__ : Dict = min_length_for_response if minimum_tokens is not None: A__ : Union[str, Any] = minimum_tokens if "max_length" in generate_kwargs: A__ : Union[str, Any] = generate_kwargs['''max_length'''] # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length) if clean_up_tokenization_spaces is not None: A__ : Optional[Any] = clean_up_tokenization_spaces if generate_kwargs: forward_params.update(UpperCamelCase__ ) return preprocess_params, forward_params, postprocess_params def __call__( self , UpperCamelCase__ , UpperCamelCase__=0 , **UpperCamelCase__ ): A__ : List[str] = super().__call__(UpperCamelCase__ , num_workers=UpperCamelCase__ , **UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) and len(UpperCamelCase__ ) == 1: return outputs[0] return outputs def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__=32 ): if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): raise ValueError('''ConversationalPipeline, expects Conversation as inputs''' ) if conversation.new_user_input is None: raise ValueError( F"Conversation with UUID {type(conversation.uuid )} does not contain new user input to process. " '''Add user inputs with the conversation\'s `add_user_input` method''' ) if hasattr(self.tokenizer , '''_build_conversation_input_ids''' ): A__ : List[Any] = self.tokenizer._build_conversation_input_ids(UpperCamelCase__ ) else: # If the tokenizer cannot handle conversations, we default to only the old version A__ : Any = self._legacy_parse_and_tokenize(UpperCamelCase__ ) if self.framework == "pt": A__ : Optional[Any] = torch.LongTensor([input_ids] ) elif self.framework == "tf": A__ : Optional[Any] = tf.constant([input_ids] ) return {"input_ids": input_ids, "conversation": conversation} def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__=10 , **UpperCamelCase__ ): A__ : str = generate_kwargs.get('''max_length''' , self.model.config.max_length ) A__ : List[Any] = model_inputs['''input_ids'''].shape[1] if max_length - minimum_tokens < n: logger.warning(F"Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})" ) A__ : Any = max_length - minimum_tokens A__ : List[Any] = model_inputs['''input_ids'''][:, -trim:] if "attention_mask" in model_inputs: A__ : List[str] = model_inputs['''attention_mask'''][:, -trim:] A__ : List[str] = model_inputs.pop('''conversation''' ) A__ : List[str] = max_length A__ : Any = self.model.generate(**UpperCamelCase__ , **UpperCamelCase__ ) if self.model.config.is_encoder_decoder: A__ : Any = 1 else: A__ : List[str] = n return {"output_ids": output_ids[:, start_position:], "conversation": conversation} def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__=True ): A__ : Dict = model_outputs['''output_ids'''] A__ : List[Any] = self.tokenizer.decode( output_ids[0] , skip_special_tokens=UpperCamelCase__ , clean_up_tokenization_spaces=UpperCamelCase__ , ) A__ : Union[str, Any] = model_outputs['''conversation'''] conversation.mark_processed() conversation.append_response(UpperCamelCase__ ) return conversation def __snake_case ( self , UpperCamelCase__ ): A__ : List[str] = self.tokenizer.eos_token_id A__ : str = [] for is_user, text in conversation.iter_texts(): if eos_token_id is not None: input_ids.extend(self.tokenizer.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) + [eos_token_id] ) else: input_ids.extend(self.tokenizer.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) ) if len(UpperCamelCase__ ) > self.tokenizer.model_max_length: A__ : Dict = input_ids[-self.tokenizer.model_max_length :] return input_ids
55
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation _SCREAMING_SNAKE_CASE : Optional[int] = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Optional[Any] = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} _SCREAMING_SNAKE_CASE : List[str] = { 'tokenizer_file': { 'EleutherAI/gpt-neox-20b': 'https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/tokenizer.json', }, } _SCREAMING_SNAKE_CASE : Dict = { 'gpt-neox-20b': 2_0_4_8, } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = VOCAB_FILES_NAMES _lowerCAmelCase = PRETRAINED_VOCAB_FILES_MAP _lowerCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCAmelCase = ["input_ids", "attention_mask"] def __init__( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__="<|endoftext|>" , UpperCamelCase__="<|endoftext|>" , UpperCamelCase__="<|endoftext|>" , UpperCamelCase__=False , **UpperCamelCase__ , ): super().__init__( UpperCamelCase__ , UpperCamelCase__ , tokenizer_file=UpperCamelCase__ , unk_token=UpperCamelCase__ , bos_token=UpperCamelCase__ , eos_token=UpperCamelCase__ , add_prefix_space=UpperCamelCase__ , **UpperCamelCase__ , ) A__ : Optional[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , UpperCamelCase__ ) != add_prefix_space: A__ : Union[str, Any] = getattr(UpperCamelCase__ , pre_tok_state.pop('''type''' ) ) A__ : List[Any] = add_prefix_space A__ : Any = pre_tok_class(**UpperCamelCase__ ) A__ : List[Any] = add_prefix_space def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ = None ): A__ : Any = self._tokenizer.model.save(UpperCamelCase__ , name=UpperCamelCase__ ) return tuple(UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): A__ : List[str] = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) + [self.eos_token_id] ) if len(UpperCamelCase__ ) > self.model_max_length: A__ : Tuple = input_ids[-self.model_max_length :] return input_ids
55
1
from manim import * class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def __snake_case ( self ): A__ : int = Rectangle(height=0.5 , width=0.5 ) A__ : List[Any] = Rectangle(height=0.4_6 , width=0.4_6 ).set_stroke(width=0 ) A__ : int = [mem.copy() for i in range(6 )] A__ : Optional[int] = [mem.copy() for i in range(6 )] A__ : List[str] = VGroup(*UpperCamelCase__ ).arrange(UpperCamelCase__ , buff=0 ) A__ : int = VGroup(*UpperCamelCase__ ).arrange(UpperCamelCase__ , buff=0 ) A__ : List[str] = VGroup(UpperCamelCase__ , UpperCamelCase__ ).arrange(UpperCamelCase__ , buff=0 ) A__ : Tuple = Text('''CPU''' , font_size=24 ) A__ : Any = Group(UpperCamelCase__ , UpperCamelCase__ ).arrange(UpperCamelCase__ , buff=0.5 , aligned_edge=UpperCamelCase__ ) cpu.move_to([-2.5, -0.5, 0] ) self.add(UpperCamelCase__ ) A__ : Dict = [mem.copy() for i in range(1 )] A__ : Union[str, Any] = VGroup(*UpperCamelCase__ ).arrange(UpperCamelCase__ , buff=0 ) A__ : int = Text('''GPU''' , font_size=24 ) A__ : Tuple = 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__ ) A__ : str = [mem.copy() for i in range(6 )] A__ : Optional[Any] = VGroup(*UpperCamelCase__ ).arrange(UpperCamelCase__ , buff=0 ) A__ : Optional[Any] = Text('''Model''' , font_size=24 ) A__ : str = 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 ) , ) A__ : Tuple = MarkupText( F"First, an empty model skeleton is loaded\ninto <span fgcolor='{YELLOW}'>memory</span> without using much RAM." , font_size=24 , ) A__ : Dict = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) A__ : List[Any] = 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__ ) A__ : str = [] A__ : Dict = [] A__ : Union[str, Any] = [] for i, rect in enumerate(UpperCamelCase__ ): A__ : List[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() A__ : Optional[int] = 0.4_6 / 4 A__ : 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()
55
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING _SCREAMING_SNAKE_CASE : Union[str, Any] = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : int = { '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__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "deformable_detr" _lowerCAmelCase = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=3 , UpperCamelCase__=300 , UpperCamelCase__=1024 , UpperCamelCase__=6 , UpperCamelCase__=1024 , UpperCamelCase__=8 , UpperCamelCase__=6 , UpperCamelCase__=1024 , UpperCamelCase__=8 , UpperCamelCase__=0.0 , UpperCamelCase__=True , UpperCamelCase__="relu" , UpperCamelCase__=256 , UpperCamelCase__=0.1 , UpperCamelCase__=0.0 , UpperCamelCase__=0.0 , UpperCamelCase__=0.0_2 , UpperCamelCase__=1.0 , UpperCamelCase__=True , UpperCamelCase__=False , UpperCamelCase__="sine" , UpperCamelCase__="resnet50" , UpperCamelCase__=True , UpperCamelCase__=False , UpperCamelCase__=4 , UpperCamelCase__=4 , UpperCamelCase__=4 , UpperCamelCase__=False , UpperCamelCase__=300 , UpperCamelCase__=False , UpperCamelCase__=1 , UpperCamelCase__=5 , UpperCamelCase__=2 , UpperCamelCase__=1 , UpperCamelCase__=1 , UpperCamelCase__=5 , UpperCamelCase__=2 , UpperCamelCase__=0.1 , UpperCamelCase__=0.2_5 , UpperCamelCase__=False , **UpperCamelCase__ , ): if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) A__ : int = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Union[str, Any] = backbone_config.get('''model_type''' ) A__ : Union[str, Any] = CONFIG_MAPPING[backbone_model_type] A__ : Optional[int] = config_class.from_dict(UpperCamelCase__ ) A__ : Tuple = use_timm_backbone A__ : int = backbone_config A__ : List[Any] = num_channels A__ : List[Any] = num_queries A__ : str = max_position_embeddings A__ : Tuple = d_model A__ : int = encoder_ffn_dim A__ : Union[str, Any] = encoder_layers A__ : Optional[Any] = encoder_attention_heads A__ : List[Any] = decoder_ffn_dim A__ : Tuple = decoder_layers A__ : Optional[Any] = decoder_attention_heads A__ : List[str] = dropout A__ : str = attention_dropout A__ : List[Any] = activation_dropout A__ : Any = activation_function A__ : Optional[Any] = init_std A__ : Union[str, Any] = init_xavier_std A__ : Union[str, Any] = encoder_layerdrop A__ : Optional[int] = auxiliary_loss A__ : str = position_embedding_type A__ : List[Any] = backbone A__ : Optional[Any] = use_pretrained_backbone A__ : Any = dilation # deformable attributes A__ : List[Any] = num_feature_levels A__ : List[str] = encoder_n_points A__ : int = decoder_n_points A__ : List[Any] = two_stage A__ : Dict = two_stage_num_proposals A__ : Optional[int] = 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 A__ : List[str] = class_cost A__ : List[Any] = bbox_cost A__ : Any = giou_cost # Loss coefficients A__ : List[str] = mask_loss_coefficient A__ : Union[str, Any] = dice_loss_coefficient A__ : List[Any] = bbox_loss_coefficient A__ : Tuple = giou_loss_coefficient A__ : Optional[Any] = eos_coefficient A__ : List[Any] = focal_alpha A__ : List[str] = disable_custom_kernels super().__init__(is_encoder_decoder=UpperCamelCase__ , **UpperCamelCase__ ) @property def __snake_case ( self ): return self.encoder_attention_heads @property def __snake_case ( self ): return self.d_model def __snake_case ( self ): A__ : List[str] = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: A__ : Tuple = self.backbone_config.to_dict() A__ : Optional[int] = self.__class__.model_type return output
55
1
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__=7 , UpperCamelCase__=3 , UpperCamelCase__=18 , UpperCamelCase__=30 , UpperCamelCase__=400 , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=True , UpperCamelCase__=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3] , UpperCamelCase__=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1] , UpperCamelCase__=True , ): A__ : Any = size if size is not None else {'''height''': 224, '''width''': 224} A__ : Optional[Any] = crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} A__ : Optional[Any] = parent A__ : Optional[Any] = batch_size A__ : List[str] = num_channels A__ : Tuple = image_size A__ : Union[str, Any] = min_resolution A__ : List[str] = max_resolution A__ : Union[str, Any] = do_resize A__ : int = size A__ : Tuple = do_center_crop A__ : List[Any] = crop_size A__ : List[str] = do_normalize A__ : List[Any] = image_mean A__ : Any = image_std A__ : Union[str, Any] = do_convert_rgb def __snake_case ( self ): return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_convert_rgb": self.do_convert_rgb, } def __snake_case ( self , UpperCamelCase__=False , UpperCamelCase__=False , UpperCamelCase__=False ): assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time" if equal_resolution: A__ : Union[str, Any] = [] for i in range(self.batch_size ): image_inputs.append( np.random.randint( 255 , size=(self.num_channels, self.max_resolution, self.max_resolution) , dtype=np.uinta ) ) else: A__ : int = [] for i in range(self.batch_size ): A__ , A__ : Optional[int] = np.random.choice(np.arange(self.min_resolution , self.max_resolution ) , 2 ) image_inputs.append(np.random.randint(255 , size=(self.num_channels, width, height) , dtype=np.uinta ) ) if not numpify and not torchify: # PIL expects the channel dimension as last dimension A__ : Tuple = [Image.fromarray(np.moveaxis(UpperCamelCase__ , 0 , -1 ) ) for x in image_inputs] if torchify: A__ : int = [torch.from_numpy(UpperCamelCase__ ) for x in image_inputs] return image_inputs @require_torch @require_vision class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = ChineseCLIPImageProcessor if is_vision_available() else None def __snake_case ( self ): A__ : Dict = ChineseCLIPImageProcessingTester(self , do_center_crop=UpperCamelCase__ ) @property def __snake_case ( self ): return self.image_processor_tester.prepare_image_processor_dict() def __snake_case ( self ): A__ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCamelCase__ , '''do_resize''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''size''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''do_center_crop''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''center_crop''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''do_normalize''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''image_mean''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''image_std''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''do_convert_rgb''' ) ) def __snake_case ( self ): A__ : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 224, '''width''': 224} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) A__ : int = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42} ) self.assertEqual(image_processor.crop_size , {'''height''': 84, '''width''': 84} ) def __snake_case ( self ): pass def __snake_case ( self ): # Initialize image_processing A__ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ : str = self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase__ , Image.Image ) # Test not batched input A__ : Dict = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched A__ : Optional[Any] = image_processing(UpperCamelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __snake_case ( self ): # Initialize image_processing A__ : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A__ : Dict = self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__ , numpify=UpperCamelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase__ , np.ndarray ) # Test not batched input A__ : Optional[int] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched A__ : int = image_processing(UpperCamelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __snake_case ( self ): # Initialize image_processing A__ : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A__ : Dict = self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__ , torchify=UpperCamelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase__ , torch.Tensor ) # Test not batched input A__ : Optional[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched A__ : Optional[Any] = image_processing(UpperCamelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) @require_torch @require_vision class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = ChineseCLIPImageProcessor if is_vision_available() else None def __snake_case ( self ): A__ : int = ChineseCLIPImageProcessingTester(self , num_channels=4 , do_center_crop=UpperCamelCase__ ) A__ : Optional[Any] = 3 @property def __snake_case ( self ): return self.image_processor_tester.prepare_image_processor_dict() def __snake_case ( self ): A__ : Any = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCamelCase__ , '''do_resize''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''size''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''do_center_crop''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''center_crop''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''do_normalize''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''image_mean''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''image_std''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''do_convert_rgb''' ) ) def __snake_case ( self ): pass def __snake_case ( self ): # Initialize image_processing A__ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ : List[Any] = self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase__ , Image.Image ) # Test not batched input A__ : Optional[int] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched A__ : Tuple = image_processing(UpperCamelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , )
55
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> List[Any]: """simple docstring""" A__ : Optional[Any] = 0 A__ : Optional[Any] = len(__UpperCamelCase ) for i in range(n - 1 ): for j in range(i + 1 , __UpperCamelCase ): if arr[i] > arr[j]: num_inversions += 1 return num_inversions def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Tuple: """simple docstring""" if len(__UpperCamelCase ) <= 1: return arr, 0 A__ : Optional[int] = len(__UpperCamelCase ) // 2 A__ : List[str] = arr[0:mid] A__ : Union[str, Any] = arr[mid:] A__ , A__ : List[Any] = count_inversions_recursive(__UpperCamelCase ) A__ , A__ : int = count_inversions_recursive(__UpperCamelCase ) A__ , A__ : Dict = _count_cross_inversions(__UpperCamelCase , __UpperCamelCase ) A__ : Any = inversion_p + inversions_q + cross_inversions return c, num_inversions def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] , __UpperCamelCase : List[Any] ) -> Dict: """simple docstring""" A__ : str = [] A__ : Tuple = 0 while i < len(__UpperCamelCase ) and j < len(__UpperCamelCase ): if p[i] > q[j]: # if P[1] > Q[j], then P[k] > Q[k] for all i < k <= len(P) # These are all inversions. The claim emerges from the # property that P is sorted. num_inversion += len(__UpperCamelCase ) - i r.append(q[j] ) j += 1 else: r.append(p[i] ) i += 1 if i < len(__UpperCamelCase ): r.extend(p[i:] ) else: r.extend(q[j:] ) return r, num_inversion def SCREAMING_SNAKE_CASE ( ) -> Tuple: """simple docstring""" A__ : List[str] = [10, 2, 1, 5, 5, 2, 11] # this arr has 8 inversions: # (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2) A__ : int = count_inversions_bf(__UpperCamelCase ) A__ , A__ : int = count_inversions_recursive(__UpperCamelCase ) assert num_inversions_bf == num_inversions_recursive == 8 print('''number of inversions = ''' , __UpperCamelCase ) # testing an array with zero inversion (a sorted arr_1) arr_a.sort() A__ : Optional[Any] = count_inversions_bf(__UpperCamelCase ) A__ , A__ : Dict = count_inversions_recursive(__UpperCamelCase ) assert num_inversions_bf == num_inversions_recursive == 0 print('''number of inversions = ''' , __UpperCamelCase ) # an empty list should also have zero inversions A__ : Union[str, Any] = [] A__ : Union[str, Any] = count_inversions_bf(__UpperCamelCase ) A__ , A__ : Any = count_inversions_recursive(__UpperCamelCase ) assert num_inversions_bf == num_inversions_recursive == 0 print('''number of inversions = ''' , __UpperCamelCase ) if __name__ == "__main__": main()
55
1
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline else: from .pipeline_kandinsky import KandinskyPipeline from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput from .text_encoder import MultilingualCLIP
55
from PIL import Image def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Image , __UpperCamelCase : float ) -> Image: """simple docstring""" def brightness(__UpperCamelCase : int ) -> float: return 1_28 + level + (c - 1_28) if not -2_5_5.0 <= level <= 2_5_5.0: raise ValueError('''level must be between -255.0 (black) and 255.0 (white)''' ) return img.point(__UpperCamelCase ) if __name__ == "__main__": # Load image with Image.open('image_data/lena.jpg') as img: # Change brightness to 100 _SCREAMING_SNAKE_CASE : Dict = change_brightness(img, 1_0_0) brigt_img.save('image_data/lena_brightness.png', format='png')
55
1
import logging import os import sys from dataclasses import dataclass, field from itertools import chain from typing import Optional, Union import datasets import numpy as np import torch from datasets import load_dataset import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, HfArgumentParser, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.tokenization_utils_base import PreTrainedTokenizerBase from transformers.trainer_utils import get_last_checkpoint from transformers.utils import PaddingStrategy, check_min_version, send_example_telemetry # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version('4.31.0') _SCREAMING_SNAKE_CASE : List[str] = logging.getLogger(__name__) @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) _lowerCAmelCase = field( default=SCREAMING_SNAKE_CASE_, metadata={"help": "Pretrained config name or path if not the same as model_name"} ) _lowerCAmelCase = field( default=SCREAMING_SNAKE_CASE_, metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) _lowerCAmelCase = field( default=SCREAMING_SNAKE_CASE_, metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"}, ) _lowerCAmelCase = field( default=SCREAMING_SNAKE_CASE_, metadata={"help": "Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."}, ) _lowerCAmelCase = field( default="main", metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."}, ) _lowerCAmelCase = field( default=SCREAMING_SNAKE_CASE_, metadata={ "help": ( "Will use the token generated when running `huggingface-cli login` (necessary to use this script " "with private models)." ) }, ) @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = field(default=SCREAMING_SNAKE_CASE_, metadata={"help": "The input training data file (a text file)."} ) _lowerCAmelCase = field( default=SCREAMING_SNAKE_CASE_, metadata={"help": "An optional input evaluation data file to evaluate the perplexity on (a text file)."}, ) _lowerCAmelCase = field( default=SCREAMING_SNAKE_CASE_, metadata={"help": "Overwrite the cached training and evaluation sets"} ) _lowerCAmelCase = field( default=SCREAMING_SNAKE_CASE_, metadata={"help": "The number of processes to use for the preprocessing."}, ) _lowerCAmelCase = field( default=SCREAMING_SNAKE_CASE_, metadata={ "help": ( "The maximum total input sequence length after tokenization. If passed, sequences longer " "than this will be truncated, sequences shorter will be padded." ) }, ) _lowerCAmelCase = field( default=SCREAMING_SNAKE_CASE_, metadata={ "help": ( "Whether to pad all samples to the maximum sentence length. " "If False, will pad the samples dynamically when batching to the maximum length in the batch. More " "efficient on GPU but very bad for TPU." ) }, ) _lowerCAmelCase = field( default=SCREAMING_SNAKE_CASE_, metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of training examples to this " "value if set." ) }, ) _lowerCAmelCase = field( default=SCREAMING_SNAKE_CASE_, metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of evaluation examples to this " "value if set." ) }, ) def __snake_case ( self ): if self.train_file is not None: A__ : List[str] = self.train_file.split('''.''' )[-1] assert extension in ["csv", "json"], "`train_file` should be a csv or a json file." if self.validation_file is not None: A__ : Optional[int] = self.validation_file.split('''.''' )[-1] assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file." @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = 42 _lowerCAmelCase = True _lowerCAmelCase = None _lowerCAmelCase = None def __call__( self , UpperCamelCase__ ): A__ : int = '''label''' if '''label''' in features[0].keys() else '''labels''' A__ : List[str] = [feature.pop(UpperCamelCase__ ) for feature in features] A__ : Optional[Any] = len(UpperCamelCase__ ) A__ : int = len(features[0]['''input_ids'''] ) A__ : Optional[Any] = [ [{k: v[i] for k, v in feature.items()} for i in range(UpperCamelCase__ )] for feature in features ] A__ : Tuple = list(chain(*UpperCamelCase__ ) ) A__ : List[str] = self.tokenizer.pad( UpperCamelCase__ , padding=self.padding , max_length=self.max_length , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors='''pt''' , ) # Un-flatten A__ : Optional[int] = {k: v.view(UpperCamelCase__ , UpperCamelCase__ , -1 ) for k, v in batch.items()} # Add back labels A__ : str = torch.tensor(UpperCamelCase__ , dtype=torch.intaa ) return batch def SCREAMING_SNAKE_CASE ( ) -> int: """simple docstring""" A__ : str = 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. A__ , A__ , A__ : Optional[int] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: A__ , A__ , A__ : List[Any] = 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_swag''' , __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() A__ : int = training_args.get_process_log_level() logger.setLevel(__UpperCamelCase ) datasets.utils.logging.set_verbosity(__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}" ) # Detecting last checkpoint. A__ : int = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: A__ : Dict = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"Output directory ({training_args.output_dir}) already exists and is not empty. " '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change " '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.train_file is not None or data_args.validation_file is not None: A__ : List[str] = {} if data_args.train_file is not None: A__ : Optional[Any] = data_args.train_file if data_args.validation_file is not None: A__ : Tuple = data_args.validation_file A__ : Dict = data_args.train_file.split('''.''' )[-1] A__ : Optional[Any] = load_dataset( __UpperCamelCase , data_files=__UpperCamelCase , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) else: # Downloading and loading the swag dataset from the hub. A__ : Optional[Any] = load_dataset( '''swag''' , '''regular''' , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Load pretrained model and tokenizer # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. A__ : str = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) A__ : Any = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) A__ : str = AutoModelForMultipleChoice.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 , ) # When using your own dataset or a different dataset from swag, you will probably need to change this. A__ : Union[str, Any] = [F"ending{i}" for i in range(4 )] A__ : Any = '''sent1''' A__ : int = '''sent2''' if data_args.max_seq_length is None: A__ : List[Any] = tokenizer.model_max_length if max_seq_length > 10_24: logger.warning( '''The chosen tokenizer supports a `model_max_length` that is longer than the default `block_size` value''' ''' of 1024. If you would like to use a longer `block_size` up to `tokenizer.model_max_length` you can''' ''' override this default with `--block_size xxx`.''' ) A__ : Optional[Any] = 10_24 else: if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( F"The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the" F"model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}." ) A__ : Dict = min(data_args.max_seq_length , tokenizer.model_max_length ) # Preprocessing the datasets. def preprocess_function(__UpperCamelCase : int ): A__ : Tuple = [[context] * 4 for context in examples[context_name]] A__ : Optional[int] = examples[question_header_name] A__ : Any = [ [F"{header} {examples[end][i]}" for end in ending_names] for i, header in enumerate(__UpperCamelCase ) ] # Flatten out A__ : Tuple = list(chain(*__UpperCamelCase ) ) A__ : Dict = list(chain(*__UpperCamelCase ) ) # Tokenize A__ : Any = tokenizer( __UpperCamelCase , __UpperCamelCase , truncation=__UpperCamelCase , max_length=__UpperCamelCase , padding='''max_length''' if data_args.pad_to_max_length else False , ) # Un-flatten return {k: [v[i : i + 4] for i in range(0 , len(__UpperCamelCase ) , 4 )] for k, v in tokenized_examples.items()} if training_args.do_train: if "train" not in raw_datasets: raise ValueError('''--do_train requires a train dataset''' ) A__ : Dict = raw_datasets['''train'''] if data_args.max_train_samples is not None: A__ : Dict = min(len(__UpperCamelCase ) , data_args.max_train_samples ) A__ : List[str] = train_dataset.select(range(__UpperCamelCase ) ) with training_args.main_process_first(desc='''train dataset map pre-processing''' ): A__ : str = train_dataset.map( __UpperCamelCase , batched=__UpperCamelCase , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , ) if training_args.do_eval: if "validation" not in raw_datasets: raise ValueError('''--do_eval requires a validation dataset''' ) A__ : Optional[int] = raw_datasets['''validation'''] if data_args.max_eval_samples is not None: A__ : Any = min(len(__UpperCamelCase ) , data_args.max_eval_samples ) A__ : str = eval_dataset.select(range(__UpperCamelCase ) ) with training_args.main_process_first(desc='''validation dataset map pre-processing''' ): A__ : Optional[Any] = eval_dataset.map( __UpperCamelCase , batched=__UpperCamelCase , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , ) # Data collator A__ : List[Any] = ( default_data_collator if data_args.pad_to_max_length else DataCollatorForMultipleChoice(tokenizer=__UpperCamelCase , pad_to_multiple_of=8 if training_args.fpaa else None ) ) # Metric def compute_metrics(__UpperCamelCase : List[Any] ): A__ , A__ : int = eval_predictions A__ : Tuple = np.argmax(__UpperCamelCase , axis=1 ) return {"accuracy": (preds == label_ids).astype(np.floataa ).mean().item()} # Initialize our Trainer A__ : Optional[Any] = Trainer( model=__UpperCamelCase , args=__UpperCamelCase , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , tokenizer=__UpperCamelCase , data_collator=__UpperCamelCase , compute_metrics=__UpperCamelCase , ) # Training if training_args.do_train: A__ : Union[str, Any] = None if training_args.resume_from_checkpoint is not None: A__ : int = training_args.resume_from_checkpoint elif last_checkpoint is not None: A__ : Dict = last_checkpoint A__ : List[str] = trainer.train(resume_from_checkpoint=__UpperCamelCase ) trainer.save_model() # Saves the tokenizer too for easy upload A__ : str = train_result.metrics A__ : Union[str, Any] = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(__UpperCamelCase ) ) A__ : int = min(__UpperCamelCase , len(__UpperCamelCase ) ) trainer.log_metrics('''train''' , __UpperCamelCase ) trainer.save_metrics('''train''' , __UpperCamelCase ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info('''*** Evaluate ***''' ) A__ : Any = trainer.evaluate() A__ : Optional[Any] = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(__UpperCamelCase ) A__ : str = min(__UpperCamelCase , len(__UpperCamelCase ) ) trainer.log_metrics('''eval''' , __UpperCamelCase ) trainer.save_metrics('''eval''' , __UpperCamelCase ) A__ : Any = { '''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''multiple-choice''', '''dataset_tags''': '''swag''', '''dataset_args''': '''regular''', '''dataset''': '''SWAG''', '''language''': '''en''', } if training_args.push_to_hub: trainer.push_to_hub(**__UpperCamelCase ) else: trainer.create_model_card(**__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Tuple: """simple docstring""" main() if __name__ == "__main__": main()
55
import json import os import tempfile from transformers.testing_utils import check_json_file_has_correct_format class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = None def __snake_case ( self ): A__ : Dict = self.feature_extraction_class(**self.feat_extract_dict ) A__ : Tuple = json.loads(feat_extract.to_json_string() ) for key, value in self.feat_extract_dict.items(): self.assertEqual(obj[key] , UpperCamelCase__ ) def __snake_case ( self ): A__ : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: A__ : Any = os.path.join(UpperCamelCase__ , '''feat_extract.json''' ) feat_extract_first.to_json_file(UpperCamelCase__ ) A__ : Dict = self.feature_extraction_class.from_json_file(UpperCamelCase__ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def __snake_case ( self ): A__ : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: A__ : Any = feat_extract_first.save_pretrained(UpperCamelCase__ )[0] check_json_file_has_correct_format(UpperCamelCase__ ) A__ : Optional[int] = self.feature_extraction_class.from_pretrained(UpperCamelCase__ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def __snake_case ( self ): A__ : str = self.feature_extraction_class() self.assertIsNotNone(UpperCamelCase__ )
55
1
from __future__ import annotations from collections.abc import Generator import requests from bsa import BeautifulSoup _SCREAMING_SNAKE_CASE : int = 'https://www.indeed.co.in/jobs?q=mobile+app+development&l=' def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str = "mumbai" ) -> Generator[tuple[str, str], None, None]: """simple docstring""" A__ : Tuple = BeautifulSoup(requests.get(url + location ).content , '''html.parser''' ) # This attribute finds out all the specifics listed in a job for job in soup.find_all('''div''' , attrs={'''data-tn-component''': '''organicJob'''} ): A__ : int = job.find('''a''' , attrs={'''data-tn-element''': '''jobTitle'''} ).text.strip() A__ : Union[str, Any] = job.find('''span''' , {'''class''': '''company'''} ).text.strip() yield job_title, company_name if __name__ == "__main__": for i, job in enumerate(fetch_jobs('Bangalore'), 1): print(f"""Job {i:>2} is {job[0]} at {job[1]}""")
55
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets _SCREAMING_SNAKE_CASE : Union[str, Any] = '\\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 : Tuple = '\\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 : Optional[Any] = '\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 UpperCamelCase__ ( datasets.Metric ): '''simple docstring''' def __snake_case ( self ): 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 , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = False , UpperCamelCase__ = False , UpperCamelCase__ = False , UpperCamelCase__ = False , ): A__ : List[Any] = len(references[0] ) if any(len(UpperCamelCase__ ) != references_per_prediction for refs in references ): raise ValueError('''Sacrebleu requires the same number of references for each prediction''' ) A__ : Dict = [[refs[i] for refs in references] for i in range(UpperCamelCase__ )] A__ : Optional[Any] = TER( normalized=UpperCamelCase__ , no_punct=UpperCamelCase__ , asian_support=UpperCamelCase__ , case_sensitive=UpperCamelCase__ , ) A__ : str = sb_ter.corpus_score(UpperCamelCase__ , UpperCamelCase__ ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
55
1
from math import factorial class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__ ): A__ : Dict = real if isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : int = [1] * rank else: A__ : Optional[Any] = rank def __repr__( self ): return ( F"{self.real}+" F"{'+'.join(str(UpperCamelCase__ )+'E'+str(n+1 )for n,dual in enumerate(self.duals ) )}" ) def __snake_case ( self ): A__ : Optional[Any] = self.duals.copy() while cur[-1] == 0: cur.pop(-1 ) return Dual(self.real , UpperCamelCase__ ) def __add__( self , UpperCamelCase__ ): if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): return Dual(self.real + other , self.duals ) A__ : int = self.duals.copy() A__ : Dict = other.duals.copy() if len(UpperCamelCase__ ) > len(UpperCamelCase__ ): o_dual.extend([1] * (len(UpperCamelCase__ ) - len(UpperCamelCase__ )) ) elif len(UpperCamelCase__ ) < len(UpperCamelCase__ ): s_dual.extend([1] * (len(UpperCamelCase__ ) - len(UpperCamelCase__ )) ) A__ : Optional[Any] = [] for i in range(len(UpperCamelCase__ ) ): new_duals.append(s_dual[i] + o_dual[i] ) return Dual(self.real + other.real , UpperCamelCase__ ) _lowerCAmelCase = __add__ def __sub__( self , UpperCamelCase__ ): return self + other * -1 def __mul__( self , UpperCamelCase__ ): if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : int = [] for i in self.duals: new_duals.append(i * other ) return Dual(self.real * other , UpperCamelCase__ ) A__ : Optional[int] = [0] * (len(self.duals ) + len(other.duals ) + 1) for i, item in enumerate(self.duals ): for j, jtem in enumerate(other.duals ): new_duals[i + j + 1] += item * jtem for k in range(len(self.duals ) ): new_duals[k] += self.duals[k] * other.real for index in range(len(other.duals ) ): new_duals[index] += other.duals[index] * self.real return Dual(self.real * other.real , UpperCamelCase__ ) _lowerCAmelCase = __mul__ def __truediv__( self , UpperCamelCase__ ): if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Tuple = [] for i in self.duals: new_duals.append(i / other ) return Dual(self.real / other , UpperCamelCase__ ) raise ValueError def __floordiv__( self , UpperCamelCase__ ): if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Any = [] for i in self.duals: new_duals.append(i // other ) return Dual(self.real // other , UpperCamelCase__ ) raise ValueError def __pow__( self , UpperCamelCase__ ): if n < 0 or isinstance(UpperCamelCase__ , UpperCamelCase__ ): raise ValueError('''power must be a positive integer''' ) if n == 0: return 1 if n == 1: return self A__ : Dict = self for _ in range(n - 1 ): x *= self return x def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] , __UpperCamelCase : int , __UpperCamelCase : List[str] ) -> Dict: """simple docstring""" if not callable(__UpperCamelCase ): raise ValueError('''differentiate() requires a function as input for func''' ) if not isinstance(__UpperCamelCase , (float, int) ): raise ValueError('''differentiate() requires a float as input for position''' ) if not isinstance(__UpperCamelCase , __UpperCamelCase ): raise ValueError('''differentiate() requires an int as input for order''' ) A__ : Dict = Dual(__UpperCamelCase , 1 ) A__ : Any = func(__UpperCamelCase ) if order == 0: return result.real return result.duals[order - 1] * factorial(__UpperCamelCase ) if __name__ == "__main__": import doctest doctest.testmod() def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Union[str, Any] ) -> Dict: """simple docstring""" return y**2 * y**4 print(differentiate(f, 9, 2))
55
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging _SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) # TODO Update this _SCREAMING_SNAKE_CASE : Optional[int] = { 'facebook/esm-1b': 'https://huggingface.co/facebook/esm-1b/resolve/main/config.json', # See all ESM models at https://huggingface.co/models?filter=esm } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "esm" def __init__( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=768 , UpperCamelCase__=12 , UpperCamelCase__=12 , UpperCamelCase__=3072 , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=1026 , UpperCamelCase__=0.0_2 , UpperCamelCase__=1e-12 , UpperCamelCase__="absolute" , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=False , UpperCamelCase__=False , UpperCamelCase__=None , UpperCamelCase__=None , **UpperCamelCase__ , ): super().__init__(pad_token_id=UpperCamelCase__ , mask_token_id=UpperCamelCase__ , **UpperCamelCase__ ) A__ : Optional[Any] = vocab_size A__ : int = hidden_size A__ : List[str] = num_hidden_layers A__ : Tuple = num_attention_heads A__ : str = intermediate_size A__ : List[str] = hidden_dropout_prob A__ : Optional[Any] = attention_probs_dropout_prob A__ : int = max_position_embeddings A__ : List[str] = initializer_range A__ : List[Any] = layer_norm_eps A__ : int = position_embedding_type A__ : Optional[Any] = use_cache A__ : Optional[int] = emb_layer_norm_before A__ : List[str] = token_dropout A__ : Tuple = is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) A__ : List[Any] = EsmFoldConfig() elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Optional[int] = EsmFoldConfig(**UpperCamelCase__ ) A__ : int = esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) A__ : Any = get_default_vocab_list() else: A__ : Dict = vocab_list else: A__ : Optional[Any] = None A__ : Tuple = None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCamelCase__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __snake_case ( self ): A__ : Optional[int] = super().to_dict() if isinstance(self.esmfold_config , UpperCamelCase__ ): A__ : Dict = self.esmfold_config.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = None _lowerCAmelCase = True _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = 0 _lowerCAmelCase = True _lowerCAmelCase = False _lowerCAmelCase = 128 _lowerCAmelCase = None def __snake_case ( self ): if self.trunk is None: A__ : Tuple = TrunkConfig() elif isinstance(self.trunk , UpperCamelCase__ ): A__ : List[Any] = TrunkConfig(**self.trunk ) def __snake_case ( self ): A__ : Optional[int] = asdict(self ) A__ : int = self.trunk.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = 48 _lowerCAmelCase = 1_024 _lowerCAmelCase = 128 _lowerCAmelCase = 32 _lowerCAmelCase = 32 _lowerCAmelCase = 32 _lowerCAmelCase = 0 _lowerCAmelCase = 0 _lowerCAmelCase = False _lowerCAmelCase = 4 _lowerCAmelCase = 128 _lowerCAmelCase = None def __snake_case ( self ): if self.structure_module is None: A__ : str = StructureModuleConfig() elif isinstance(self.structure_module , UpperCamelCase__ ): A__ : str = StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(F"`max_recycles` should be positive, got {self.max_recycles}." ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' F" {self.sequence_state_dim} and {self.sequence_state_dim}." ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' F" {self.pairwise_state_dim} and {self.pairwise_state_dim}." ) A__ : Tuple = self.sequence_state_dim // self.sequence_head_width A__ : int = self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' F" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}." ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' F" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}." ) if self.pairwise_state_dim % 2 != 0: raise ValueError(F"`pairwise_state_dim` should be even, got {self.pairwise_state_dim}." ) if self.dropout >= 0.4: raise ValueError(F"`dropout` should not be greater than 0.4, got {self.dropout}." ) def __snake_case ( self ): A__ : List[Any] = asdict(self ) A__ : Optional[int] = self.structure_module.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = 384 _lowerCAmelCase = 128 _lowerCAmelCase = 16 _lowerCAmelCase = 128 _lowerCAmelCase = 12 _lowerCAmelCase = 4 _lowerCAmelCase = 8 _lowerCAmelCase = 0.1 _lowerCAmelCase = 8 _lowerCAmelCase = 1 _lowerCAmelCase = 2 _lowerCAmelCase = 7 _lowerCAmelCase = 10 _lowerCAmelCase = 1e-8 _lowerCAmelCase = 1e5 def __snake_case ( self ): return asdict(self ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: """simple docstring""" return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
55
1
import unittest from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available from transformers.pipelines import pipeline from transformers.pipelines.document_question_answering import apply_tesseract from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_detectrona, require_pytesseract, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image from transformers.image_utils import load_image else: class UpperCamelCase__ : '''simple docstring''' @staticmethod def __snake_case ( *UpperCamelCase__ , **UpperCamelCase__ ): pass def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] ) -> Optional[int]: """simple docstring""" return None # This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace, # so we can expect it to be available. _SCREAMING_SNAKE_CASE : Dict = ( 'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png' ) @is_pipeline_test @require_torch @require_vision class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING @require_pytesseract @require_vision def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : Dict = pipeline( '''document-question-answering''' , model=UpperCamelCase__ , tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) A__ : int = INVOICE_URL A__ : Union[str, Any] = list(zip(*apply_tesseract(load_image(UpperCamelCase__ ) , UpperCamelCase__ , '''''' ) ) ) A__ : Dict = '''What is the placebo?''' A__ : int = [ { '''image''': load_image(UpperCamelCase__ ), '''question''': question, }, { '''image''': image, '''question''': question, }, { '''image''': image, '''question''': question, '''word_boxes''': word_boxes, }, ] return dqa_pipeline, examples def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ ): A__ : List[Any] = dqa_pipeline(UpperCamelCase__ , top_k=2 ) self.assertEqual( UpperCamelCase__ , [ [ {'''score''': ANY(UpperCamelCase__ ), '''answer''': ANY(UpperCamelCase__ ), '''start''': ANY(UpperCamelCase__ ), '''end''': ANY(UpperCamelCase__ )}, {'''score''': ANY(UpperCamelCase__ ), '''answer''': ANY(UpperCamelCase__ ), '''start''': ANY(UpperCamelCase__ ), '''end''': ANY(UpperCamelCase__ )}, ] ] * 3 , ) @require_torch @require_detectrona @require_pytesseract def __snake_case ( self ): A__ : Tuple = pipeline('''document-question-answering''' , model='''hf-internal-testing/tiny-random-layoutlmv2''' ) A__ : str = INVOICE_URL A__ : Union[str, Any] = '''How many cats are there?''' A__ : str = [ {'''score''': 0.0_0_0_1, '''answer''': '''oy 2312/2019''', '''start''': 38, '''end''': 39}, {'''score''': 0.0_0_0_1, '''answer''': '''oy 2312/2019 DUE''', '''start''': 38, '''end''': 40}, ] A__ : str = dqa_pipeline(image=UpperCamelCase__ , question=UpperCamelCase__ , top_k=2 ) self.assertEqual(nested_simplify(UpperCamelCase__ , decimals=4 ) , UpperCamelCase__ ) A__ : List[str] = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual(nested_simplify(UpperCamelCase__ , decimals=4 ) , UpperCamelCase__ ) # This image does not detect ANY text in it, meaning layoutlmv2 should fail. # Empty answer probably A__ : int = '''./tests/fixtures/tests_samples/COCO/000000039769.png''' A__ : Dict = dqa_pipeline(image=UpperCamelCase__ , question=UpperCamelCase__ , top_k=2 ) self.assertEqual(UpperCamelCase__ , [] ) # We can optionnally pass directly the words and bounding boxes A__ : Optional[Any] = '''./tests/fixtures/tests_samples/COCO/000000039769.png''' A__ : int = [] A__ : Dict = [] A__ : Dict = dqa_pipeline(image=UpperCamelCase__ , question=UpperCamelCase__ , words=UpperCamelCase__ , boxes=UpperCamelCase__ , top_k=2 ) self.assertEqual(UpperCamelCase__ , [] ) @slow @require_torch @require_detectrona @require_pytesseract def __snake_case ( self ): A__ : Any = pipeline( '''document-question-answering''' , model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa''' , revision='''9977165''' , ) A__ : Any = INVOICE_URL A__ : Dict = '''What is the invoice number?''' A__ : Any = dqa_pipeline(image=UpperCamelCase__ , question=UpperCamelCase__ , top_k=2 ) self.assertEqual( nested_simplify(UpperCamelCase__ , decimals=4 ) , [ {'''score''': 0.9_9_4_4, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_0_0_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) A__ : int = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(UpperCamelCase__ , decimals=4 ) , [ {'''score''': 0.9_9_4_4, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_0_0_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) A__ : List[str] = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(UpperCamelCase__ , decimals=4 ) , [ [ {'''score''': 0.9_9_4_4, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_0_0_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ], ] * 2 , ) @slow @require_torch @require_detectrona @require_pytesseract def __snake_case ( self ): A__ : str = pipeline( '''document-question-answering''' , model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa''' , revision='''9977165''' , max_seq_len=50 , ) A__ : Dict = INVOICE_URL A__ : int = '''What is the invoice number?''' A__ : List[Any] = dqa_pipeline(image=UpperCamelCase__ , question=UpperCamelCase__ , top_k=2 ) self.assertEqual( nested_simplify(UpperCamelCase__ , decimals=4 ) , [ {'''score''': 0.9_9_7_4, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9_9_4_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) A__ : Union[str, Any] = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(UpperCamelCase__ , decimals=4 ) , [ {'''score''': 0.9_9_7_4, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9_9_4_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) A__ : Optional[int] = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(UpperCamelCase__ , decimals=4 ) , [ [ {'''score''': 0.9_9_7_4, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9_9_4_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] ] * 2 , ) @slow @require_torch @require_pytesseract @require_vision def __snake_case ( self ): A__ : Tuple = AutoTokenizer.from_pretrained( '''impira/layoutlm-document-qa''' , revision='''3dc6de3''' , add_prefix_space=UpperCamelCase__ ) A__ : Dict = pipeline( '''document-question-answering''' , model='''impira/layoutlm-document-qa''' , tokenizer=UpperCamelCase__ , revision='''3dc6de3''' , ) A__ : Tuple = INVOICE_URL A__ : Tuple = '''What is the invoice number?''' A__ : Union[str, Any] = dqa_pipeline(image=UpperCamelCase__ , question=UpperCamelCase__ , top_k=2 ) self.assertEqual( nested_simplify(UpperCamelCase__ , decimals=4 ) , [ {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) A__ : str = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(UpperCamelCase__ , decimals=4 ) , [ {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) A__ : Optional[int] = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(UpperCamelCase__ , decimals=4 ) , [ [ {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] ] * 2 , ) A__ : Union[str, Any] = list(zip(*apply_tesseract(load_image(UpperCamelCase__ ) , UpperCamelCase__ , '''''' ) ) ) # This model should also work if `image` is set to None A__ : Tuple = dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(UpperCamelCase__ , decimals=4 ) , [ {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) @slow @require_torch @require_pytesseract @require_vision def __snake_case ( self ): A__ : int = AutoTokenizer.from_pretrained( '''impira/layoutlm-document-qa''' , revision='''3dc6de3''' , add_prefix_space=UpperCamelCase__ ) A__ : Optional[int] = pipeline( '''document-question-answering''' , model='''impira/layoutlm-document-qa''' , tokenizer=UpperCamelCase__ , revision='''3dc6de3''' , max_seq_len=50 , ) A__ : List[Any] = INVOICE_URL A__ : Union[str, Any] = '''What is the invoice number?''' A__ : Optional[int] = dqa_pipeline(image=UpperCamelCase__ , question=UpperCamelCase__ , top_k=2 ) self.assertEqual( nested_simplify(UpperCamelCase__ , decimals=4 ) , [ {'''score''': 0.9_9_9_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9_9_9_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) A__ : List[str] = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(UpperCamelCase__ , decimals=4 ) , [ [ {'''score''': 0.9_9_9_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9_9_9_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] ] * 2 , ) A__ : int = list(zip(*apply_tesseract(load_image(UpperCamelCase__ ) , UpperCamelCase__ , '''''' ) ) ) # This model should also work if `image` is set to None A__ : int = dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(UpperCamelCase__ , decimals=4 ) , [ {'''score''': 0.9_9_9_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9_9_9_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) @slow @require_torch def __snake_case ( self ): A__ : Any = pipeline( '''document-question-answering''' , model='''naver-clova-ix/donut-base-finetuned-docvqa''' , tokenizer=AutoTokenizer.from_pretrained('''naver-clova-ix/donut-base-finetuned-docvqa''' ) , feature_extractor='''naver-clova-ix/donut-base-finetuned-docvqa''' , ) A__ : Dict = INVOICE_URL A__ : Optional[Any] = '''What is the invoice number?''' A__ : Optional[int] = dqa_pipeline(image=UpperCamelCase__ , question=UpperCamelCase__ , top_k=2 ) self.assertEqual(nested_simplify(UpperCamelCase__ , decimals=4 ) , [{'''answer''': '''us-001'''}] ) @require_tf @unittest.skip('''Document question answering not implemented in TF''' ) def __snake_case ( self ): pass
55
import logging import torch from accelerate import Accelerator from arguments import EvaluationArguments from datasets import load_dataset from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, set_seed class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=1024 , UpperCamelCase__=1024 , UpperCamelCase__=3.6 ): A__ : str = tokenizer A__ : int = tokenizer.bos_token_id A__ : List[Any] = dataset A__ : Tuple = seq_length A__ : Any = seq_length * chars_per_token * num_of_sequences def __iter__( self ): A__ : Dict = iter(self.dataset ) A__ : Tuple = True while more_examples: A__ , A__ : Optional[Any] = [], 0 while True: if buffer_len >= self.input_characters: break try: buffer.append(next(UpperCamelCase__ )['''content'''] ) buffer_len += len(buffer[-1] ) except StopIteration: A__ : Dict = False break A__ : str = tokenizer(UpperCamelCase__ , truncation=UpperCamelCase__ )['''input_ids'''] A__ : Optional[int] = [] for tokenized_input in tokenized_inputs: all_token_ids.extend(tokenized_input + [self.concat_token_id] ) for i in range(0 , len(UpperCamelCase__ ) , self.seq_length ): A__ : Optional[int] = all_token_ids[i : i + self.seq_length] if len(UpperCamelCase__ ) == self.seq_length: yield torch.tensor(UpperCamelCase__ ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> Any: """simple docstring""" A__ : Any = {'''streaming''': True} A__ : List[str] = load_dataset(args.dataset_name , split='''train''' , **__UpperCamelCase ) A__ : List[str] = ConstantLengthDataset(__UpperCamelCase , __UpperCamelCase , seq_length=args.seq_length ) A__ : int = DataLoader(__UpperCamelCase , batch_size=args.batch_size ) return eval_dataloader def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] ) -> Dict: """simple docstring""" model.eval() A__ : Dict = [] for step, batch in enumerate(__UpperCamelCase ): with torch.no_grad(): A__ : Any = model(__UpperCamelCase , labels=__UpperCamelCase ) A__ : Tuple = outputs.loss.repeat(args.batch_size ) losses.append(accelerator.gather(__UpperCamelCase ) ) if args.max_eval_steps > 0 and step >= args.max_eval_steps: break A__ : Tuple = torch.mean(torch.cat(__UpperCamelCase ) ) try: A__ : Optional[Any] = torch.exp(__UpperCamelCase ) except OverflowError: A__ : Union[str, Any] = float('''inf''' ) return loss.item(), perplexity.item() # Setup Accelerator _SCREAMING_SNAKE_CASE : List[Any] = Accelerator() # Parse configuration _SCREAMING_SNAKE_CASE : Optional[int] = HfArgumentParser(EvaluationArguments) _SCREAMING_SNAKE_CASE : Union[str, Any] = parser.parse_args() set_seed(args.seed) # Logging _SCREAMING_SNAKE_CASE : Dict = logging.getLogger(__name__) logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO ) # Load model and tokenizer _SCREAMING_SNAKE_CASE : Optional[int] = AutoModelForCausalLM.from_pretrained(args.model_ckpt) _SCREAMING_SNAKE_CASE : List[str] = AutoTokenizer.from_pretrained(args.model_ckpt) # Load dataset and dataloader _SCREAMING_SNAKE_CASE : Optional[Any] = create_dataloader(args) # Prepare everything with our `accelerator`. _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Optional[Any] = accelerator.prepare(model, eval_dataloader) # Evaluate and save the last checkpoint logger.info('Evaluating and saving model after training') _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Optional[int] = evaluate(args) logger.info(f"""loss/eval: {eval_loss}, perplexity: {perplexity}""")
55
1
import argparse import glob import logging import os import time from argparse import Namespace import numpy as np import torch from lightning_base import BaseTransformer, add_generic_args, generic_train from torch.utils.data import DataLoader, TensorDataset from transformers import glue_compute_metrics as compute_metrics from transformers import glue_convert_examples_to_features as convert_examples_to_features from transformers import glue_output_modes, glue_tasks_num_labels from transformers import glue_processors as processors _SCREAMING_SNAKE_CASE : int = logging.getLogger(__name__) class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "sequence-classification" def __init__( self , UpperCamelCase__ ): if type(UpperCamelCase__ ) == dict: A__ : Any = Namespace(**UpperCamelCase__ ) A__ : int = glue_output_modes[hparams.task] A__ : int = glue_tasks_num_labels[hparams.task] super().__init__(UpperCamelCase__ , UpperCamelCase__ , self.mode ) def __snake_case ( self , **UpperCamelCase__ ): return self.model(**UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ ): A__ : Dict = {'''input_ids''': batch[0], '''attention_mask''': batch[1], '''labels''': batch[3]} if self.config.model_type not in ["distilbert", "bart"]: A__ : Optional[int] = batch[2] if self.config.model_type in ['''bert''', '''xlnet''', '''albert'''] else None A__ : Optional[int] = self(**UpperCamelCase__ ) A__ : str = outputs[0] A__ : List[Any] = self.trainer.lr_schedulers[0]['''scheduler'''] A__ : Optional[Any] = {'''loss''': loss, '''rate''': lr_scheduler.get_last_lr()[-1]} return {"loss": loss, "log": tensorboard_logs} def __snake_case ( self ): A__ : Dict = self.hparams A__ : Dict = processors[args.task]() A__ : Any = processor.get_labels() for mode in ["train", "dev"]: A__ : Union[str, Any] = self._feature_file(UpperCamelCase__ ) if os.path.exists(UpperCamelCase__ ) and not args.overwrite_cache: logger.info('''Loading features from cached file %s''' , UpperCamelCase__ ) else: logger.info('''Creating features from dataset file at %s''' , args.data_dir ) A__ : str = ( processor.get_dev_examples(args.data_dir ) if mode == '''dev''' else processor.get_train_examples(args.data_dir ) ) A__ : List[str] = convert_examples_to_features( UpperCamelCase__ , self.tokenizer , max_length=args.max_seq_length , label_list=self.labels , output_mode=args.glue_output_mode , ) logger.info('''Saving features into cached file %s''' , UpperCamelCase__ ) torch.save(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = False ): A__ : Tuple = '''dev''' if mode == '''test''' else mode A__ : Tuple = self._feature_file(UpperCamelCase__ ) logger.info('''Loading features from cached file %s''' , UpperCamelCase__ ) A__ : List[str] = torch.load(UpperCamelCase__ ) A__ : Dict = torch.tensor([f.input_ids for f in features] , dtype=torch.long ) A__ : Optional[int] = torch.tensor([f.attention_mask for f in features] , dtype=torch.long ) A__ : List[Any] = torch.tensor([f.token_type_ids for f in features] , dtype=torch.long ) if self.hparams.glue_output_mode == "classification": A__ : int = torch.tensor([f.label for f in features] , dtype=torch.long ) elif self.hparams.glue_output_mode == "regression": A__ : Tuple = torch.tensor([f.label for f in features] , dtype=torch.float ) return DataLoader( TensorDataset(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) , batch_size=UpperCamelCase__ , shuffle=UpperCamelCase__ , ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ ): A__ : Optional[int] = {'''input_ids''': batch[0], '''attention_mask''': batch[1], '''labels''': batch[3]} if self.config.model_type not in ["distilbert", "bart"]: A__ : int = batch[2] if self.config.model_type in ['''bert''', '''xlnet''', '''albert'''] else None A__ : Union[str, Any] = self(**UpperCamelCase__ ) A__ , A__ : int = outputs[:2] A__ : Optional[Any] = logits.detach().cpu().numpy() A__ : Optional[int] = inputs['''labels'''].detach().cpu().numpy() return {"val_loss": tmp_eval_loss.detach().cpu(), "pred": preds, "target": out_label_ids} def __snake_case ( self , UpperCamelCase__ ): A__ : List[Any] = torch.stack([x['''val_loss'''] for x in outputs] ).mean().detach().cpu().item() A__ : Union[str, Any] = np.concatenate([x['''pred'''] for x in outputs] , axis=0 ) if self.hparams.glue_output_mode == "classification": A__ : Optional[Any] = np.argmax(UpperCamelCase__ , axis=1 ) elif self.hparams.glue_output_mode == "regression": A__ : List[Any] = np.squeeze(UpperCamelCase__ ) A__ : int = np.concatenate([x['''target'''] for x in outputs] , axis=0 ) A__ : Optional[int] = [[] for _ in range(out_label_ids.shape[0] )] A__ : Optional[int] = [[] for _ in range(out_label_ids.shape[0] )] A__ : Optional[Any] = {**{'''val_loss''': val_loss_mean}, **compute_metrics(self.hparams.task , UpperCamelCase__ , UpperCamelCase__ )} A__ : List[Any] = dict(results.items() ) A__ : Optional[Any] = results return ret, preds_list, out_label_list def __snake_case ( self , UpperCamelCase__ ): A__ , A__ , A__ : Tuple = self._eval_end(UpperCamelCase__ ) A__ : Optional[int] = ret['''log'''] return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs} def __snake_case ( self , UpperCamelCase__ ): A__ , A__ , A__ : Union[str, Any] = self._eval_end(UpperCamelCase__ ) A__ : Dict = ret['''log'''] # `val_loss` is the key returned by `self._eval_end()` but actually refers to `test_loss` return {"avg_test_loss": logs["val_loss"], "log": logs, "progress_bar": logs} @staticmethod def __snake_case ( UpperCamelCase__ , UpperCamelCase__ ): BaseTransformer.add_model_specific_args(UpperCamelCase__ , UpperCamelCase__ ) parser.add_argument( '''--max_seq_length''' , default=128 , type=UpperCamelCase__ , help=( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) , ) parser.add_argument( '''--task''' , default='''''' , type=UpperCamelCase__ , required=UpperCamelCase__ , help='''The GLUE task to run''' , ) parser.add_argument( '''--gpus''' , default=0 , type=UpperCamelCase__ , help='''The number of GPUs allocated for this, it is by default 0 meaning none''' , ) parser.add_argument( '''--overwrite_cache''' , action='''store_true''' , help='''Overwrite the cached training and evaluation sets''' ) return parser def SCREAMING_SNAKE_CASE ( ) -> Dict: """simple docstring""" A__ : List[Any] = argparse.ArgumentParser() add_generic_args(__UpperCamelCase , os.getcwd() ) A__ : Any = GLUETransformer.add_model_specific_args(__UpperCamelCase , os.getcwd() ) A__ : Tuple = parser.parse_args() # If output_dir not provided, a folder will be generated in pwd if args.output_dir is None: A__ : List[str] = os.path.join( '''./results''' , F"{args.task}_{time.strftime('%Y%m%d_%H%M%S' )}" , ) os.makedirs(args.output_dir ) A__ : List[Any] = GLUETransformer(__UpperCamelCase ) A__ : List[str] = generic_train(__UpperCamelCase , __UpperCamelCase ) # Optionally, predict on dev set and write to output_dir if args.do_predict: A__ : Any = sorted(glob.glob(os.path.join(args.output_dir , '''checkpoint-epoch=*.ckpt''' ) , recursive=__UpperCamelCase ) ) A__ : int = model.load_from_checkpoint(checkpoints[-1] ) return trainer.test(__UpperCamelCase ) if __name__ == "__main__": main()
55
def SCREAMING_SNAKE_CASE ( ) -> Optional[int]: """simple docstring""" A__ : Optional[Any] = 0 for i in range(1 , 10_01 ): total += i**i return str(__UpperCamelCase )[-10:] if __name__ == "__main__": print(solution())
55
1
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor from ..utils import is_datasets_available from .base import PipelineTool if is_datasets_available(): from datasets import load_dataset class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "microsoft/speecht5_tts" _lowerCAmelCase = ( "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the " "text to read (in English) and returns a waveform object containing the sound." ) _lowerCAmelCase = "text_reader" _lowerCAmelCase = SpeechTaProcessor _lowerCAmelCase = SpeechTaForTextToSpeech _lowerCAmelCase = SpeechTaHifiGan _lowerCAmelCase = ["text"] _lowerCAmelCase = ["audio"] def __snake_case ( self ): if self.post_processor is None: A__ : int = '''microsoft/speecht5_hifigan''' super().setup() def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__=None ): A__ : List[Any] = self.pre_processor(text=UpperCamelCase__ , return_tensors='''pt''' , truncation=UpperCamelCase__ ) if speaker_embeddings is None: if not is_datasets_available(): raise ImportError('''Datasets needs to be installed if not passing speaker embeddings.''' ) A__ : List[Any] = load_dataset('''Matthijs/cmu-arctic-xvectors''' , split='''validation''' ) A__ : Dict = torch.tensor(embeddings_dataset[7305]['''xvector'''] ).unsqueeze(0 ) return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings} def __snake_case ( self , UpperCamelCase__ ): with torch.no_grad(): return self.model.generate_speech(**UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): with torch.no_grad(): return self.post_processor(UpperCamelCase__ ).cpu().detach()
55
import inspect import os import unittest import torch import accelerate from accelerate import debug_launcher from accelerate.test_utils import ( execute_subprocess_async, require_cpu, require_huggingface_suite, require_multi_gpu, require_single_gpu, ) from accelerate.utils import patch_environment @require_huggingface_suite class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): A__ : Dict = inspect.getfile(accelerate.test_utils ) A__ : Any = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''external_deps''', '''test_metrics.py'''] ) from accelerate.test_utils.scripts.external_deps import test_metrics # noqa: F401 A__ : Tuple = test_metrics @require_cpu def __snake_case ( self ): debug_launcher(self.test_metrics.main , num_processes=1 ) @require_cpu def __snake_case ( self ): debug_launcher(self.test_metrics.main ) @require_single_gpu def __snake_case ( self ): self.test_metrics.main() @require_multi_gpu def __snake_case ( self ): print(F"Found {torch.cuda.device_count()} devices." ) A__ : int = ['''torchrun''', F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(UpperCamelCase__ , env=os.environ.copy() )
55
1
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Union[str, Any] ) -> str: """simple docstring""" A__ : Any = [] A__ : List[Any] = set({'''(''', '''[''', '''{'''} ) A__ : int = set({''')''', ''']''', '''}'''} ) A__ : Dict = {'''{''': '''}''', '''[''': ''']''', '''(''': ''')'''} for i in range(len(__UpperCamelCase ) ): if s[i] in open_brackets: stack.append(s[i] ) elif s[i] in closed_brackets and ( len(__UpperCamelCase ) == 0 or (len(__UpperCamelCase ) > 0 and open_to_closed[stack.pop()] != s[i]) ): return False return len(__UpperCamelCase ) == 0 def SCREAMING_SNAKE_CASE ( ) -> Optional[int]: """simple docstring""" A__ : Optional[Any] = input('''Enter sequence of brackets: ''' ) if is_balanced(__UpperCamelCase ): print(__UpperCamelCase , '''is balanced''' ) else: print(__UpperCamelCase , '''is not balanced''' ) if __name__ == "__main__": main()
55
from numpy import exp, pi, sqrt def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict , __UpperCamelCase : float = 0.0 , __UpperCamelCase : float = 1.0 ) -> int: """simple docstring""" return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
55
1
import unittest import numpy as np from datasets import load_dataset from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import BeitImageProcessor class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__=7 , UpperCamelCase__=3 , UpperCamelCase__=18 , UpperCamelCase__=30 , UpperCamelCase__=400 , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=True , UpperCamelCase__=[0.5, 0.5, 0.5] , UpperCamelCase__=[0.5, 0.5, 0.5] , UpperCamelCase__=False , ): A__ : Union[str, Any] = size if size is not None else {'''height''': 20, '''width''': 20} A__ : Any = crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} A__ : Any = parent A__ : List[Any] = batch_size A__ : Tuple = num_channels A__ : int = image_size A__ : List[Any] = min_resolution A__ : List[Any] = max_resolution A__ : List[Any] = do_resize A__ : int = size A__ : int = do_center_crop A__ : int = crop_size A__ : Tuple = do_normalize A__ : List[str] = image_mean A__ : Any = image_std A__ : Union[str, Any] = do_reduce_labels def __snake_case ( self ): return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_reduce_labels": self.do_reduce_labels, } def SCREAMING_SNAKE_CASE ( ) -> Any: """simple docstring""" A__ : int = load_dataset('''hf-internal-testing/fixtures_ade20k''' , split='''test''' ) A__ : Any = Image.open(dataset[0]['''file'''] ) A__ : List[Any] = Image.open(dataset[1]['''file'''] ) return image, map def SCREAMING_SNAKE_CASE ( ) -> Tuple: """simple docstring""" A__ : Tuple = load_dataset('''hf-internal-testing/fixtures_ade20k''' , split='''test''' ) A__ : List[str] = Image.open(ds[0]['''file'''] ) A__ : int = Image.open(ds[1]['''file'''] ) A__ : Any = Image.open(ds[2]['''file'''] ) A__ : Dict = Image.open(ds[3]['''file'''] ) return [imagea, imagea], [mapa, mapa] @require_torch @require_vision class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = BeitImageProcessor if is_vision_available() else None def __snake_case ( self ): A__ : str = BeitImageProcessingTester(self ) @property def __snake_case ( self ): return self.image_processor_tester.prepare_image_processor_dict() def __snake_case ( self ): A__ : str = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCamelCase__ , '''do_resize''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''size''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''do_center_crop''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''center_crop''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''do_normalize''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''image_mean''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''image_std''' ) ) def __snake_case ( self ): A__ : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 20, '''width''': 20} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) self.assertEqual(image_processor.do_reduce_labels , UpperCamelCase__ ) A__ : Optional[Any] = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , crop_size=84 , reduce_labels=UpperCamelCase__ ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) self.assertEqual(image_processor.crop_size , {'''height''': 84, '''width''': 84} ) self.assertEqual(image_processor.do_reduce_labels , UpperCamelCase__ ) def __snake_case ( self ): pass def __snake_case ( self ): # Initialize image_processing A__ : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ : Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase__ , Image.Image ) # Test not batched input A__ : Any = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched A__ : List[Any] = image_processing(UpperCamelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __snake_case ( self ): # Initialize image_processing A__ : str = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A__ : Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase__ , numpify=UpperCamelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase__ , np.ndarray ) # Test not batched input A__ : List[Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched A__ : Dict = image_processing(UpperCamelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __snake_case ( self ): # Initialize image_processing A__ : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A__ : Union[str, Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase__ , torchify=UpperCamelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase__ , torch.Tensor ) # Test not batched input A__ : Tuple = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched A__ : Tuple = image_processing(UpperCamelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __snake_case ( self ): # Initialize image_processing A__ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A__ : List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase__ , torchify=UpperCamelCase__ ) A__ : int = [] for image in image_inputs: self.assertIsInstance(UpperCamelCase__ , torch.Tensor ) maps.append(torch.zeros(image.shape[-2:] ).long() ) # Test not batched input A__ : Tuple = image_processing(image_inputs[0] , maps[0] , return_tensors='''pt''' ) self.assertEqual( encoding['''pixel_values'''].shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) self.assertEqual( encoding['''labels'''].shape , ( 1, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) self.assertEqual(encoding['''labels'''].dtype , torch.long ) self.assertTrue(encoding['''labels'''].min().item() >= 0 ) self.assertTrue(encoding['''labels'''].max().item() <= 255 ) # Test batched A__ : Any = image_processing(UpperCamelCase__ , UpperCamelCase__ , return_tensors='''pt''' ) self.assertEqual( encoding['''pixel_values'''].shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) self.assertEqual( encoding['''labels'''].shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) self.assertEqual(encoding['''labels'''].dtype , torch.long ) self.assertTrue(encoding['''labels'''].min().item() >= 0 ) self.assertTrue(encoding['''labels'''].max().item() <= 255 ) # Test not batched input (PIL images) A__ , A__ : Optional[Any] = prepare_semantic_single_inputs() A__ : Dict = image_processing(UpperCamelCase__ , UpperCamelCase__ , return_tensors='''pt''' ) self.assertEqual( encoding['''pixel_values'''].shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) self.assertEqual( encoding['''labels'''].shape , ( 1, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) self.assertEqual(encoding['''labels'''].dtype , torch.long ) self.assertTrue(encoding['''labels'''].min().item() >= 0 ) self.assertTrue(encoding['''labels'''].max().item() <= 255 ) # Test batched input (PIL images) A__ , A__ : List[str] = prepare_semantic_batch_inputs() A__ : List[str] = image_processing(UpperCamelCase__ , UpperCamelCase__ , return_tensors='''pt''' ) self.assertEqual( encoding['''pixel_values'''].shape , ( 2, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) self.assertEqual( encoding['''labels'''].shape , ( 2, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) self.assertEqual(encoding['''labels'''].dtype , torch.long ) self.assertTrue(encoding['''labels'''].min().item() >= 0 ) self.assertTrue(encoding['''labels'''].max().item() <= 255 ) def __snake_case ( self ): # Initialize image_processing A__ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150 A__ , A__ : List[Any] = prepare_semantic_single_inputs() A__ : int = image_processing(UpperCamelCase__ , UpperCamelCase__ , return_tensors='''pt''' ) self.assertTrue(encoding['''labels'''].min().item() >= 0 ) self.assertTrue(encoding['''labels'''].max().item() <= 150 ) A__ : Optional[int] = True A__ : str = image_processing(UpperCamelCase__ , UpperCamelCase__ , return_tensors='''pt''' ) self.assertTrue(encoding['''labels'''].min().item() >= 0 ) self.assertTrue(encoding['''labels'''].max().item() <= 255 )
55
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tensorflow_text_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _SCREAMING_SNAKE_CASE : int = { 'configuration_bert': ['BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BertConfig', 'BertOnnxConfig'], 'tokenization_bert': ['BasicTokenizer', 'BertTokenizer', 'WordpieceTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Optional[Any] = ['BertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Union[str, Any] = [ 'BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BertForMaskedLM', 'BertForMultipleChoice', 'BertForNextSentencePrediction', 'BertForPreTraining', 'BertForQuestionAnswering', 'BertForSequenceClassification', 'BertForTokenClassification', 'BertLayer', 'BertLMHeadModel', 'BertModel', 'BertPreTrainedModel', 'load_tf_weights_in_bert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Tuple = [ 'TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFBertEmbeddings', 'TFBertForMaskedLM', 'TFBertForMultipleChoice', 'TFBertForNextSentencePrediction', 'TFBertForPreTraining', 'TFBertForQuestionAnswering', 'TFBertForSequenceClassification', 'TFBertForTokenClassification', 'TFBertLMHeadModel', 'TFBertMainLayer', 'TFBertModel', 'TFBertPreTrainedModel', ] try: if not is_tensorflow_text_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Dict = ['TFBertTokenizer'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Optional[int] = [ 'FlaxBertForCausalLM', 'FlaxBertForMaskedLM', 'FlaxBertForMultipleChoice', 'FlaxBertForNextSentencePrediction', 'FlaxBertForPreTraining', 'FlaxBertForQuestionAnswering', 'FlaxBertForSequenceClassification', 'FlaxBertForTokenClassification', 'FlaxBertModel', 'FlaxBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig, BertOnnxConfig from .tokenization_bert import BasicTokenizer, BertTokenizer, WordpieceTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bert_fast import BertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bert import ( BERT_PRETRAINED_MODEL_ARCHIVE_LIST, BertForMaskedLM, BertForMultipleChoice, BertForNextSentencePrediction, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, BertForTokenClassification, BertLayer, BertLMHeadModel, BertModel, BertPreTrainedModel, load_tf_weights_in_bert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_bert import ( TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFBertEmbeddings, TFBertForMaskedLM, TFBertForMultipleChoice, TFBertForNextSentencePrediction, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertForTokenClassification, TFBertLMHeadModel, TFBertMainLayer, TFBertModel, TFBertPreTrainedModel, ) try: if not is_tensorflow_text_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bert_tf import TFBertTokenizer try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_bert import ( FlaxBertForCausalLM, FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForNextSentencePrediction, FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertModel, FlaxBertPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE : Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
55
1
import fire from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoTokenizer from utils import SeqaSeqDataset, pickle_save def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] , __UpperCamelCase : Optional[Any] , __UpperCamelCase : str=10_24 , __UpperCamelCase : int=10_24 , __UpperCamelCase : Optional[int]=False , **__UpperCamelCase : Any ) -> List[Any]: """simple docstring""" A__ : Optional[Any] = AutoTokenizer.from_pretrained(__UpperCamelCase ) A__ : Tuple = SeqaSeqDataset(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , type_path='''train''' , **__UpperCamelCase ) A__ : Any = tok.pad_token_id def get_lens(__UpperCamelCase : Optional[Any] ): A__ : int = tqdm( DataLoader(__UpperCamelCase , batch_size=5_12 , num_workers=8 , shuffle=__UpperCamelCase , collate_fn=ds.collate_fn ) , desc=str(ds.len_file ) , ) A__ : List[str] = [] for batch in dl: A__ : str = batch['''input_ids'''].ne(__UpperCamelCase ).sum(1 ).tolist() A__ : Any = batch['''labels'''].ne(__UpperCamelCase ).sum(1 ).tolist() if consider_target: for src, tgt in zip(__UpperCamelCase , __UpperCamelCase ): max_lens.append(max(__UpperCamelCase , __UpperCamelCase ) ) else: max_lens.extend(__UpperCamelCase ) return max_lens A__ : Dict = get_lens(__UpperCamelCase ) A__ : Tuple = SeqaSeqDataset(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , type_path='''val''' , **__UpperCamelCase ) A__ : Any = get_lens(__UpperCamelCase ) pickle_save(__UpperCamelCase , train_ds.len_file ) pickle_save(__UpperCamelCase , val_ds.len_file ) if __name__ == "__main__": fire.Fire(save_len_file)
55
import json import os import sys import tempfile import unittest from pathlib import Path from shutil import copyfile from huggingface_hub import HfFolder, Repository, create_repo, delete_repo from requests.exceptions import HTTPError import transformers from transformers import ( CONFIG_MAPPING, FEATURE_EXTRACTOR_MAPPING, PROCESSOR_MAPPING, TOKENIZER_MAPPING, AutoConfig, AutoFeatureExtractor, AutoProcessor, AutoTokenizer, BertTokenizer, ProcessorMixin, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaProcessor, ) from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test from transformers.tokenization_utils import TOKENIZER_CONFIG_FILE from transformers.utils import FEATURE_EXTRACTOR_NAME, is_tokenizers_available sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 from test_module.custom_processing import CustomProcessor # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 _SCREAMING_SNAKE_CASE : List[Any] = get_tests_dir('fixtures/dummy_feature_extractor_config.json') _SCREAMING_SNAKE_CASE : int = get_tests_dir('fixtures/vocab.json') _SCREAMING_SNAKE_CASE : Tuple = get_tests_dir('fixtures') class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"] def __snake_case ( self ): A__ : List[Any] = 0 def __snake_case ( self ): A__ : Dict = AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h''' ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Optional[Any] = WavaVecaConfig() A__ : Dict = AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h''' ) # save in new folder model_config.save_pretrained(UpperCamelCase__ ) processor.save_pretrained(UpperCamelCase__ ) A__ : Any = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: # copy relevant files copyfile(UpperCamelCase__ , os.path.join(UpperCamelCase__ , UpperCamelCase__ ) ) copyfile(UpperCamelCase__ , os.path.join(UpperCamelCase__ , '''vocab.json''' ) ) A__ : List[Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Dict = WavaVecaFeatureExtractor() A__ : Union[str, Any] = AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h''' ) A__ : Optional[int] = WavaVecaProcessor(UpperCamelCase__ , UpperCamelCase__ ) # save in new folder processor.save_pretrained(UpperCamelCase__ ) # drop `processor_class` in tokenizer with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''r''' ) as f: A__ : str = json.load(UpperCamelCase__ ) config_dict.pop('''processor_class''' ) with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''w''' ) as f: f.write(json.dumps(UpperCamelCase__ ) ) A__ : Optional[int] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Optional[int] = WavaVecaFeatureExtractor() A__ : List[Any] = AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h''' ) A__ : str = WavaVecaProcessor(UpperCamelCase__ , UpperCamelCase__ ) # save in new folder processor.save_pretrained(UpperCamelCase__ ) # drop `processor_class` in feature extractor with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''r''' ) as f: A__ : List[Any] = json.load(UpperCamelCase__ ) config_dict.pop('''processor_class''' ) with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''w''' ) as f: f.write(json.dumps(UpperCamelCase__ ) ) A__ : List[Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Any = WavaVecaConfig(processor_class='''Wav2Vec2Processor''' ) model_config.save_pretrained(UpperCamelCase__ ) # copy relevant files copyfile(UpperCamelCase__ , os.path.join(UpperCamelCase__ , '''vocab.json''' ) ) # create emtpy sample processor with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''w''' ) as f: f.write('''{}''' ) A__ : Union[str, Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(UpperCamelCase__ ): A__ : Union[str, Any] = AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(UpperCamelCase__ ): A__ : str = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) A__ : int = AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) self.assertTrue(processor.special_attribute_present ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) A__ : List[Any] = processor.feature_extractor self.assertTrue(feature_extractor.special_attribute_present ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) A__ : List[Any] = processor.tokenizer self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) # Test we can also load the slow version A__ : Dict = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ , use_fast=UpperCamelCase__ ) A__ : int = new_processor.tokenizer self.assertTrue(new_tokenizer.special_attribute_present ) self.assertEqual(new_tokenizer.__class__.__name__ , '''NewTokenizer''' ) else: self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) def __snake_case ( self ): try: AutoConfig.register('''custom''' , UpperCamelCase__ ) AutoFeatureExtractor.register(UpperCamelCase__ , UpperCamelCase__ ) AutoTokenizer.register(UpperCamelCase__ , slow_tokenizer_class=UpperCamelCase__ ) AutoProcessor.register(UpperCamelCase__ , UpperCamelCase__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(UpperCamelCase__ ): AutoProcessor.register(UpperCamelCase__ , UpperCamelCase__ ) # Now that the config is registered, it can be used as any other config with the auto-API A__ : Any = CustomFeatureExtractor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: A__ : str = os.path.join(UpperCamelCase__ , '''vocab.txt''' ) with open(UpperCamelCase__ , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens] ) ) A__ : str = CustomTokenizer(UpperCamelCase__ ) A__ : Optional[Any] = CustomProcessor(UpperCamelCase__ , UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained(UpperCamelCase__ ) A__ : Union[str, Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] if CustomConfig in PROCESSOR_MAPPING._extra_content: del PROCESSOR_MAPPING._extra_content[CustomConfig] def __snake_case ( self ): class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = False class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = False class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "AutoFeatureExtractor" _lowerCAmelCase = "AutoTokenizer" _lowerCAmelCase = False try: AutoConfig.register('''custom''' , UpperCamelCase__ ) AutoFeatureExtractor.register(UpperCamelCase__ , UpperCamelCase__ ) AutoTokenizer.register(UpperCamelCase__ , slow_tokenizer_class=UpperCamelCase__ ) AutoProcessor.register(UpperCamelCase__ , UpperCamelCase__ ) # If remote code is not set, the default is to use local classes. A__ : List[Any] = AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor''' ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) self.assertFalse(processor.special_attribute_present ) self.assertFalse(processor.feature_extractor.special_attribute_present ) self.assertFalse(processor.tokenizer.special_attribute_present ) # If remote code is disabled, we load the local ones. A__ : Any = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) self.assertFalse(processor.special_attribute_present ) self.assertFalse(processor.feature_extractor.special_attribute_present ) self.assertFalse(processor.tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub. A__ : Union[str, Any] = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) self.assertTrue(processor.special_attribute_present ) self.assertTrue(processor.feature_extractor.special_attribute_present ) self.assertTrue(processor.tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] if CustomConfig in PROCESSOR_MAPPING._extra_content: del PROCESSOR_MAPPING._extra_content[CustomConfig] def __snake_case ( self ): A__ : str = AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) self.assertEqual(processor.__class__.__name__ , '''BertTokenizerFast''' ) def __snake_case ( self ): A__ : Union[str, Any] = AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-convnext''' ) self.assertEqual(processor.__class__.__name__ , '''ConvNextImageProcessor''' ) @is_staging_test class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"] @classmethod def __snake_case ( cls ): A__ : List[str] = TOKEN HfFolder.save_token(UpperCamelCase__ ) @classmethod def __snake_case ( cls ): try: delete_repo(token=cls._token , repo_id='''test-processor''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-processor-org''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''test-dynamic-processor''' ) except HTTPError: pass def __snake_case ( self ): A__ : Optional[Any] = WavaVecaProcessor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained( os.path.join(UpperCamelCase__ , '''test-processor''' ) , push_to_hub=UpperCamelCase__ , use_auth_token=self._token ) A__ : List[Any] = WavaVecaProcessor.from_pretrained(F"{USER}/test-processor" ) for k, v in processor.feature_extractor.__dict__.items(): self.assertEqual(UpperCamelCase__ , getattr(new_processor.feature_extractor , UpperCamelCase__ ) ) self.assertDictEqual(new_processor.tokenizer.get_vocab() , processor.tokenizer.get_vocab() ) def __snake_case ( self ): A__ : int = WavaVecaProcessor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained( os.path.join(UpperCamelCase__ , '''test-processor-org''' ) , push_to_hub=UpperCamelCase__ , use_auth_token=self._token , organization='''valid_org''' , ) A__ : List[str] = WavaVecaProcessor.from_pretrained('''valid_org/test-processor-org''' ) for k, v in processor.feature_extractor.__dict__.items(): self.assertEqual(UpperCamelCase__ , getattr(new_processor.feature_extractor , UpperCamelCase__ ) ) self.assertDictEqual(new_processor.tokenizer.get_vocab() , processor.tokenizer.get_vocab() ) def __snake_case ( self ): CustomFeatureExtractor.register_for_auto_class() CustomTokenizer.register_for_auto_class() CustomProcessor.register_for_auto_class() A__ : Optional[Any] = CustomFeatureExtractor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: A__ : List[Any] = os.path.join(UpperCamelCase__ , '''vocab.txt''' ) with open(UpperCamelCase__ , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens] ) ) A__ : Union[str, Any] = CustomTokenizer(UpperCamelCase__ ) A__ : List[Any] = CustomProcessor(UpperCamelCase__ , UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: create_repo(F"{USER}/test-dynamic-processor" , token=self._token ) A__ : Union[str, Any] = Repository(UpperCamelCase__ , clone_from=F"{USER}/test-dynamic-processor" , token=self._token ) processor.save_pretrained(UpperCamelCase__ ) # This has added the proper auto_map field to the feature extractor config self.assertDictEqual( processor.feature_extractor.auto_map , { '''AutoFeatureExtractor''': '''custom_feature_extraction.CustomFeatureExtractor''', '''AutoProcessor''': '''custom_processing.CustomProcessor''', } , ) # This has added the proper auto_map field to the tokenizer config with open(os.path.join(UpperCamelCase__ , '''tokenizer_config.json''' ) ) as f: A__ : Optional[int] = json.load(UpperCamelCase__ ) self.assertDictEqual( tokenizer_config['''auto_map'''] , { '''AutoTokenizer''': ['''custom_tokenization.CustomTokenizer''', None], '''AutoProcessor''': '''custom_processing.CustomProcessor''', } , ) # The code has been copied from fixtures self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase__ , '''custom_feature_extraction.py''' ) ) ) self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase__ , '''custom_tokenization.py''' ) ) ) self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase__ , '''custom_processing.py''' ) ) ) repo.push_to_hub() A__ : Tuple = AutoProcessor.from_pretrained(F"{USER}/test-dynamic-processor" , trust_remote_code=UpperCamelCase__ ) # Can't make an isinstance check because the new_processor is from the CustomProcessor class of a dynamic module self.assertEqual(new_processor.__class__.__name__ , '''CustomProcessor''' )
55
1
import tempfile import numpy as np import torch from transformers import AutoTokenizer, TaEncoderModel from diffusers import DDPMScheduler, UNetaDConditionModel from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.pipelines.deepfloyd_if import IFWatermarker from diffusers.utils.testing_utils import torch_device from ..test_pipelines_common import to_np class UpperCamelCase__ : '''simple docstring''' def __snake_case ( self ): torch.manual_seed(0 ) A__ : int = TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) A__ : Optional[int] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) A__ : Tuple = UNetaDConditionModel( sample_size=32 , layers_per_block=1 , block_out_channels=[32, 64] , down_block_types=[ '''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D''', ] , mid_block_type='''UNetMidBlock2DSimpleCrossAttn''' , up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''] , in_channels=3 , out_channels=6 , cross_attention_dim=32 , encoder_hid_dim=32 , attention_head_dim=8 , addition_embed_type='''text''' , addition_embed_type_num_heads=2 , cross_attention_norm='''group_norm''' , resnet_time_scale_shift='''scale_shift''' , act_fn='''gelu''' , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) A__ : Any = DDPMScheduler( num_train_timesteps=1000 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.0_0_0_1 , beta_end=0.0_2 , thresholding=UpperCamelCase__ , dynamic_thresholding_ratio=0.9_5 , sample_max_value=1.0 , prediction_type='''epsilon''' , variance_type='''learned_range''' , ) torch.manual_seed(0 ) A__ : Optional[int] = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def __snake_case ( self ): torch.manual_seed(0 ) A__ : Dict = TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) A__ : int = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) A__ : str = UNetaDConditionModel( sample_size=32 , layers_per_block=[1, 2] , block_out_channels=[32, 64] , down_block_types=[ '''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D''', ] , mid_block_type='''UNetMidBlock2DSimpleCrossAttn''' , up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''] , in_channels=6 , out_channels=6 , cross_attention_dim=32 , encoder_hid_dim=32 , attention_head_dim=8 , addition_embed_type='''text''' , addition_embed_type_num_heads=2 , cross_attention_norm='''group_norm''' , resnet_time_scale_shift='''scale_shift''' , act_fn='''gelu''' , class_embed_type='''timestep''' , mid_block_scale_factor=1.4_1_4 , time_embedding_act_fn='''gelu''' , time_embedding_dim=32 , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) A__ : int = DDPMScheduler( num_train_timesteps=1000 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.0_0_0_1 , beta_end=0.0_2 , thresholding=UpperCamelCase__ , dynamic_thresholding_ratio=0.9_5 , sample_max_value=1.0 , prediction_type='''epsilon''' , variance_type='''learned_range''' , ) torch.manual_seed(0 ) A__ : List[Any] = DDPMScheduler( num_train_timesteps=1000 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.0_0_0_1 , beta_end=0.0_2 , ) torch.manual_seed(0 ) A__ : str = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "image_noising_scheduler": image_noising_scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def __snake_case ( self ): A__ : int = self.get_dummy_components() A__ : Tuple = self.pipeline_class(**UpperCamelCase__ ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) A__ : List[str] = self.get_dummy_inputs(UpperCamelCase__ ) A__ : Optional[int] = inputs['''prompt'''] A__ : Optional[int] = inputs['''generator'''] A__ : int = inputs['''num_inference_steps'''] A__ : Optional[Any] = inputs['''output_type'''] if "image" in inputs: A__ : Optional[Any] = inputs['''image'''] else: A__ : Dict = None if "mask_image" in inputs: A__ : List[str] = inputs['''mask_image'''] else: A__ : List[str] = None if "original_image" in inputs: A__ : Optional[int] = inputs['''original_image'''] else: A__ : Tuple = None A__ , A__ : int = pipe.encode_prompt(UpperCamelCase__ ) # inputs with prompt converted to embeddings A__ : List[str] = { '''prompt_embeds''': prompt_embeds, '''negative_prompt_embeds''': negative_prompt_embeds, '''generator''': generator, '''num_inference_steps''': num_inference_steps, '''output_type''': output_type, } if image is not None: A__ : Dict = image if mask_image is not None: A__ : str = mask_image if original_image is not None: A__ : List[str] = original_image # set all optional components to None for optional_component in pipe._optional_components: setattr(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) A__ : int = pipe(**UpperCamelCase__ )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(UpperCamelCase__ ) A__ : Union[str, Any] = self.pipeline_class.from_pretrained(UpperCamelCase__ ) pipe_loaded.to(UpperCamelCase__ ) pipe_loaded.set_progress_bar_config(disable=UpperCamelCase__ ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests for optional_component in pipe._optional_components: self.assertTrue( getattr(UpperCamelCase__ , UpperCamelCase__ ) is None , F"`{optional_component}` did not stay set to None after loading." , ) A__ : int = self.get_dummy_inputs(UpperCamelCase__ ) A__ : Optional[int] = inputs['''generator'''] A__ : str = inputs['''num_inference_steps'''] A__ : Optional[int] = inputs['''output_type'''] # inputs with prompt converted to embeddings A__ : List[str] = { '''prompt_embeds''': prompt_embeds, '''negative_prompt_embeds''': negative_prompt_embeds, '''generator''': generator, '''num_inference_steps''': num_inference_steps, '''output_type''': output_type, } if image is not None: A__ : List[Any] = image if mask_image is not None: A__ : Union[str, Any] = mask_image if original_image is not None: A__ : List[str] = original_image A__ : Any = pipe_loaded(**UpperCamelCase__ )[0] A__ : int = np.abs(to_np(UpperCamelCase__ ) - to_np(UpperCamelCase__ ) ).max() self.assertLess(UpperCamelCase__ , 1e-4 ) def __snake_case ( self ): A__ : Optional[Any] = self.get_dummy_components() A__ : List[Any] = self.pipeline_class(**UpperCamelCase__ ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) A__ : int = self.get_dummy_inputs(UpperCamelCase__ ) A__ : str = pipe(**UpperCamelCase__ )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(UpperCamelCase__ ) A__ : Optional[int] = self.pipeline_class.from_pretrained(UpperCamelCase__ ) pipe_loaded.to(UpperCamelCase__ ) pipe_loaded.set_progress_bar_config(disable=UpperCamelCase__ ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests A__ : List[Any] = self.get_dummy_inputs(UpperCamelCase__ ) A__ : str = pipe_loaded(**UpperCamelCase__ )[0] A__ : Optional[int] = np.abs(to_np(UpperCamelCase__ ) - to_np(UpperCamelCase__ ) ).max() self.assertLess(UpperCamelCase__ , 1e-4 )
55
from abc import ABC, abstractmethod from argparse import ArgumentParser class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' @staticmethod @abstractmethod def __snake_case ( UpperCamelCase__ ): raise NotImplementedError() @abstractmethod def __snake_case ( self ): raise NotImplementedError()
55
1
import inspect from typing import Callable, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import DiffusionPipeline from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import logging _SCREAMING_SNAKE_CASE : List[str] = logging.get_logger(__name__) # pylint: disable=invalid-name class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ): super().__init__() self.register_modules( vae=UpperCamelCase__ , text_encoder=UpperCamelCase__ , tokenizer=UpperCamelCase__ , unet=UpperCamelCase__ , scheduler=UpperCamelCase__ , safety_checker=UpperCamelCase__ , feature_extractor=UpperCamelCase__ , ) def __snake_case ( self , UpperCamelCase__ = "auto" ): if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory A__ : Union[str, Any] = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(UpperCamelCase__ ) def __snake_case ( self ): self.enable_attention_slicing(UpperCamelCase__ ) @torch.no_grad() def __call__( self , UpperCamelCase__ , UpperCamelCase__ = 512 , UpperCamelCase__ = 512 , UpperCamelCase__ = 50 , UpperCamelCase__ = 7.5 , UpperCamelCase__ = None , UpperCamelCase__ = 1 , UpperCamelCase__ = 0.0 , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = "pil" , UpperCamelCase__ = True , UpperCamelCase__ = None , UpperCamelCase__ = 1 , UpperCamelCase__ = None , **UpperCamelCase__ , ): if isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Tuple = 1 elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Dict = len(UpperCamelCase__ ) else: raise ValueError(F"`prompt` has to be of type `str` or `list` but is {type(UpperCamelCase__ )}" ) if height % 8 != 0 or width % 8 != 0: raise ValueError(F"`height` and `width` have to be divisible by 8 but are {height} and {width}." ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(UpperCamelCase__ , UpperCamelCase__ ) or callback_steps <= 0) ): raise ValueError( F"`callback_steps` has to be a positive integer but is {callback_steps} of type" F" {type(UpperCamelCase__ )}." ) # get prompt text embeddings A__ : Any = self.tokenizer( UpperCamelCase__ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) A__ : Union[str, Any] = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: A__ : Union[str, Any] = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' F" {self.tokenizer.model_max_length} tokens: {removed_text}" ) A__ : Any = text_input_ids[:, : self.tokenizer.model_max_length] if text_embeddings is None: A__ : str = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method A__ , A__ , A__ : Dict = text_embeddings.shape A__ : Any = text_embeddings.repeat(1 , UpperCamelCase__ , 1 ) A__ : Any = text_embeddings.view(bs_embed * num_images_per_prompt , UpperCamelCase__ , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. A__ : int = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: A__ : List[str] if negative_prompt is None: A__ : Dict = [''''''] elif type(UpperCamelCase__ ) is not type(UpperCamelCase__ ): raise TypeError( F"`negative_prompt` should be the same type to `prompt`, but got {type(UpperCamelCase__ )} !=" F" {type(UpperCamelCase__ )}." ) elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Tuple = [negative_prompt] elif batch_size != len(UpperCamelCase__ ): raise ValueError( F"`negative_prompt`: {negative_prompt} has batch size {len(UpperCamelCase__ )}, but `prompt`:" F" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches" ''' the batch size of `prompt`.''' ) else: A__ : Optional[Any] = negative_prompt A__ : int = text_input_ids.shape[-1] A__ : Dict = self.tokenizer( UpperCamelCase__ , padding='''max_length''' , max_length=UpperCamelCase__ , truncation=UpperCamelCase__ , return_tensors='''pt''' , ) A__ : Optional[Any] = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method A__ : int = uncond_embeddings.shape[1] A__ : List[str] = uncond_embeddings.repeat(UpperCamelCase__ , UpperCamelCase__ , 1 ) A__ : Optional[Any] = uncond_embeddings.view(batch_size * num_images_per_prompt , UpperCamelCase__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes A__ : List[Any] = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. A__ : List[Any] = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) A__ : Any = (batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64) A__ : List[str] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps A__ : Any = torch.randn( UpperCamelCase__ , generator=UpperCamelCase__ , device='''cpu''' , dtype=UpperCamelCase__ ).to(self.device ) A__ : Dict = torch.randn(UpperCamelCase__ , generator=UpperCamelCase__ , device='''cpu''' , dtype=UpperCamelCase__ ).to( self.device ) else: A__ : Any = torch.randn( UpperCamelCase__ , generator=UpperCamelCase__ , device=self.device , dtype=UpperCamelCase__ ) A__ : Optional[Any] = torch.randn(UpperCamelCase__ , generator=UpperCamelCase__ , device=self.device , dtype=UpperCamelCase__ ) else: if latents_reference.shape != latents_shape: raise ValueError(F"Unexpected latents shape, got {latents.shape}, expected {latents_shape}" ) A__ : Union[str, Any] = latents_reference.to(self.device ) A__ : List[Any] = latents.to(self.device ) # This is the key part of the pipeline where we # try to ensure that the generated images w/ the same seed # but different sizes actually result in similar images A__ : Union[str, Any] = (latents_shape[3] - latents_shape_reference[3]) // 2 A__ : Dict = (latents_shape[2] - latents_shape_reference[2]) // 2 A__ : List[Any] = latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx A__ : Optional[Any] = latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy A__ : Dict = 0 if dx < 0 else dx A__ : Optional[Any] = 0 if dy < 0 else dy A__ : Union[str, Any] = max(-dx , 0 ) A__ : str = max(-dy , 0 ) # import pdb # pdb.set_trace() A__ : Union[str, Any] = latents_reference[:, :, dy : dy + h, dx : dx + w] # set timesteps self.scheduler.set_timesteps(UpperCamelCase__ ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand A__ : Union[str, Any] = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler A__ : Any = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] A__ : Dict = '''eta''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) A__ : int = {} if accepts_eta: A__ : Optional[Any] = eta for i, t in enumerate(self.progress_bar(UpperCamelCase__ ) ): # expand the latents if we are doing classifier free guidance A__ : List[str] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents A__ : List[str] = self.scheduler.scale_model_input(UpperCamelCase__ , UpperCamelCase__ ) # predict the noise residual A__ : List[Any] = self.unet(UpperCamelCase__ , UpperCamelCase__ , encoder_hidden_states=UpperCamelCase__ ).sample # perform guidance if do_classifier_free_guidance: A__ , A__ : List[Any] = noise_pred.chunk(2 ) A__ : Any = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 A__ : List[Any] = self.scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) A__ : Optional[Any] = 1 / 0.1_8_2_1_5 * latents A__ : List[Any] = self.vae.decode(UpperCamelCase__ ).sample A__ : Any = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 A__ : List[Any] = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if self.safety_checker is not None: A__ : Optional[int] = self.feature_extractor(self.numpy_to_pil(UpperCamelCase__ ) , return_tensors='''pt''' ).to( self.device ) A__ , A__ : List[Any] = self.safety_checker( images=UpperCamelCase__ , clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype ) ) else: A__ : List[Any] = None if output_type == "pil": A__ : Any = self.numpy_to_pil(UpperCamelCase__ ) if not return_dict: return (image, has_nsfw_concept) return StableDiffusionPipelineOutput(images=UpperCamelCase__ , nsfw_content_detected=UpperCamelCase__ )
55
import inspect import unittest from transformers import YolosConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import YolosForObjectDetection, YolosModel from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__=13 , UpperCamelCase__=[30, 30] , UpperCamelCase__=2 , UpperCamelCase__=3 , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=32 , UpperCamelCase__=5 , UpperCamelCase__=4 , UpperCamelCase__=37 , UpperCamelCase__="gelu" , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=10 , UpperCamelCase__=0.0_2 , UpperCamelCase__=3 , UpperCamelCase__=None , UpperCamelCase__=8 , UpperCamelCase__=10 , ): A__ : Optional[int] = parent A__ : List[Any] = batch_size A__ : Dict = image_size A__ : Any = patch_size A__ : Dict = num_channels A__ : List[Any] = is_training A__ : int = use_labels A__ : Any = hidden_size A__ : List[str] = num_hidden_layers A__ : Optional[int] = num_attention_heads A__ : Optional[Any] = intermediate_size A__ : str = hidden_act A__ : str = hidden_dropout_prob A__ : Optional[int] = attention_probs_dropout_prob A__ : Optional[int] = type_sequence_label_size A__ : Any = initializer_range A__ : Optional[int] = num_labels A__ : Union[str, Any] = scope A__ : Union[str, Any] = n_targets A__ : Dict = num_detection_tokens # we set the expected sequence length (which is used in several tests) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens A__ : int = (image_size[1] // patch_size) * (image_size[0] // patch_size) A__ : List[str] = num_patches + 1 + self.num_detection_tokens def __snake_case ( self ): A__ : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]] ) A__ : int = None if self.use_labels: # labels is a list of Dict (each Dict being the labels for a given example in the batch) A__ : Tuple = [] for i in range(self.batch_size ): A__ : List[Any] = {} A__ : Tuple = torch.randint( high=self.num_labels , size=(self.n_targets,) , device=UpperCamelCase__ ) A__ : Any = torch.rand(self.n_targets , 4 , device=UpperCamelCase__ ) labels.append(UpperCamelCase__ ) A__ : Union[str, Any] = self.get_config() return config, pixel_values, labels def __snake_case ( self ): return YolosConfig( 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=UpperCamelCase__ , initializer_range=self.initializer_range , num_detection_tokens=self.num_detection_tokens , num_labels=self.num_labels , ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : Tuple = YolosModel(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : Optional[Any] = model(UpperCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.expected_seq_len, self.hidden_size) ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : Any = YolosForObjectDetection(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : Union[str, Any] = model(pixel_values=UpperCamelCase__ ) A__ : Optional[int] = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape , (self.batch_size, self.num_detection_tokens, 4) ) A__ : Union[str, Any] = model(pixel_values=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape , (self.batch_size, self.num_detection_tokens, 4) ) def __snake_case ( self ): A__ : Optional[int] = self.prepare_config_and_inputs() A__ , A__ , A__ : Optional[Any] = config_and_inputs A__ : Optional[int] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = (YolosModel, YolosForObjectDetection) if is_torch_available() else () _lowerCAmelCase = ( {"feature-extraction": YolosModel, "object-detection": YolosForObjectDetection} if is_torch_available() else {} ) _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=False ): A__ : Optional[int] = super()._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ , return_labels=UpperCamelCase__ ) if return_labels: if model_class.__name__ == "YolosForObjectDetection": A__ : str = [] for i in range(self.model_tester.batch_size ): A__ : int = {} A__ : Dict = torch.ones( size=(self.model_tester.n_targets,) , device=UpperCamelCase__ , dtype=torch.long ) A__ : Dict = torch.ones( self.model_tester.n_targets , 4 , device=UpperCamelCase__ , dtype=torch.float ) labels.append(UpperCamelCase__ ) A__ : Dict = labels return inputs_dict def __snake_case ( self ): A__ : List[Any] = YolosModelTester(self ) A__ : List[str] = ConfigTester(self , config_class=UpperCamelCase__ , has_text_modality=UpperCamelCase__ , hidden_size=37 ) def __snake_case ( self ): self.config_tester.run_common_tests() def __snake_case ( self ): # YOLOS does not use inputs_embeds pass def __snake_case ( self ): A__ , A__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : Any = model_class(UpperCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A__ : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCamelCase__ , nn.Linear ) ) def __snake_case ( self ): A__ , A__ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : List[str] = model_class(UpperCamelCase__ ) A__ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ : Optional[int] = [*signature.parameters.keys()] A__ : Optional[Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , UpperCamelCase__ ) def __snake_case ( self ): A__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase__ ) def __snake_case ( self ): A__ , A__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() A__ : Tuple = True # in YOLOS, the seq_len is different A__ : List[Any] = self.model_tester.expected_seq_len for model_class in self.all_model_classes: A__ : Any = True A__ : Optional[int] = False A__ : Optional[Any] = True A__ : int = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : List[str] = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Optional[int] = outputs.attentions self.assertEqual(len(UpperCamelCase__ ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] A__ : Tuple = True A__ : Optional[Any] = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : Tuple = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Tuple = outputs.attentions self.assertEqual(len(UpperCamelCase__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) A__ : List[Any] = len(UpperCamelCase__ ) # Check attention is always last and order is fine A__ : List[str] = True A__ : List[Any] = True A__ : int = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : Tuple = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Tuple = 1 self.assertEqual(out_len + added_hidden_states , len(UpperCamelCase__ ) ) A__ : List[str] = outputs.attentions self.assertEqual(len(UpperCamelCase__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) def __snake_case ( self ): def check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : str = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : int = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Optional[Any] = outputs.hidden_states A__ : int = getattr( self.model_tester , '''expected_num_hidden_layers''' , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(UpperCamelCase__ ) , UpperCamelCase__ ) # YOLOS has a different seq_length A__ : Union[str, Any] = self.model_tester.expected_seq_len self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) A__ , A__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : int = True check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ : Optional[int] = True check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): A__ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_object_detection(*UpperCamelCase__ ) @slow def __snake_case ( self ): for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ : Union[str, Any] = YolosModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) def SCREAMING_SNAKE_CASE ( ) -> List[str]: """simple docstring""" A__ : 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 __snake_case ( self ): return AutoImageProcessor.from_pretrained('''hustvl/yolos-small''' ) if is_vision_available() else None @slow def __snake_case ( self ): A__ : Tuple = YolosForObjectDetection.from_pretrained('''hustvl/yolos-small''' ).to(UpperCamelCase__ ) A__ : str = self.default_image_processor A__ : Tuple = prepare_img() A__ : Tuple = image_processor(images=UpperCamelCase__ , return_tensors='''pt''' ).to(UpperCamelCase__ ) # forward pass with torch.no_grad(): A__ : Any = model(inputs.pixel_values ) # verify outputs A__ : List[Any] = torch.Size((1, 100, 92) ) self.assertEqual(outputs.logits.shape , UpperCamelCase__ ) A__ : Optional[int] = torch.tensor( [[-2_4.0_2_4_8, -1_0.3_0_2_4, -1_4.8_2_9_0], [-4_2.0_3_9_2, -1_6.8_2_0_0, -2_7.4_3_3_4], [-2_7.2_7_4_3, -1_1.8_1_5_4, -1_8.7_1_4_8]] , device=UpperCamelCase__ , ) A__ : Optional[int] = torch.tensor( [[0.2_5_5_9, 0.5_4_5_5, 0.4_7_0_6], [0.2_9_8_9, 0.7_2_7_9, 0.1_8_7_5], [0.7_7_3_2, 0.4_0_1_7, 0.4_4_6_2]] , device=UpperCamelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , UpperCamelCase__ , atol=1e-4 ) ) self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3] , UpperCamelCase__ , atol=1e-4 ) ) # verify postprocessing A__ : Dict = image_processor.post_process_object_detection( UpperCamelCase__ , threshold=0.3 , target_sizes=[image.size[::-1]] )[0] A__ : int = torch.tensor([0.9_9_9_4, 0.9_7_9_0, 0.9_9_6_4, 0.9_9_7_2, 0.9_8_6_1] ).to(UpperCamelCase__ ) A__ : str = [75, 75, 17, 63, 17] A__ : Tuple = torch.tensor([3_3_5.0_6_0_9, 7_9.3_8_4_8, 3_7_5.4_2_1_6, 1_8_7.2_4_9_5] ).to(UpperCamelCase__ ) self.assertEqual(len(results['''scores'''] ) , 5 ) self.assertTrue(torch.allclose(results['''scores'''] , UpperCamelCase__ , atol=1e-4 ) ) self.assertSequenceEqual(results['''labels'''].tolist() , UpperCamelCase__ ) self.assertTrue(torch.allclose(results['''boxes'''][0, :] , UpperCamelCase__ ) )
55
1
import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to properly calculate the metrics on the # validation dataset when in a distributed system, and builds off the # `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _SCREAMING_SNAKE_CASE : str = 1_6 _SCREAMING_SNAKE_CASE : Tuple = 3_2 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Accelerator , __UpperCamelCase : int = 16 ) -> Optional[int]: """simple docstring""" A__ : List[str] = AutoTokenizer.from_pretrained('''bert-base-cased''' ) A__ : Optional[int] = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__UpperCamelCase : Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) A__ : int = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=__UpperCamelCase , max_length=__UpperCamelCase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): A__ : Optional[int] = datasets.map( __UpperCamelCase , batched=__UpperCamelCase , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library A__ : List[Any] = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__UpperCamelCase : Any ): # On TPU it's best to pad everything to the same length or training will be very slow. A__ : Optional[Any] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": A__ : int = 16 elif accelerator.mixed_precision != "no": A__ : Any = 8 else: A__ : Union[str, Any] = None return tokenizer.pad( __UpperCamelCase , padding='''longest''' , max_length=__UpperCamelCase , pad_to_multiple_of=__UpperCamelCase , return_tensors='''pt''' , ) # Instantiate dataloaders. A__ : Optional[int] = DataLoader( tokenized_datasets['''train'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) A__ : Tuple = DataLoader( tokenized_datasets['''validation'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('TESTING_MOCKED_DATALOADERS', None) == "1": from accelerate.test_utils.training import mocked_dataloaders _SCREAMING_SNAKE_CASE : Dict = mocked_dataloaders # noqa: F811 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : List[Any] ) -> Optional[Any]: """simple docstring""" if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , __UpperCamelCase ) == "1": A__ : List[str] = 2 # Initialize accelerator A__ : Optional[Any] = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs A__ : Tuple = config['''lr'''] A__ : Dict = int(config['''num_epochs'''] ) A__ : int = int(config['''seed'''] ) A__ : Optional[Any] = int(config['''batch_size'''] ) A__ : int = evaluate.load('''glue''' , '''mrpc''' ) # If the batch size is too big we use gradient accumulation A__ : Union[str, Any] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: A__ : List[Any] = batch_size // MAX_GPU_BATCH_SIZE A__ : Dict = MAX_GPU_BATCH_SIZE set_seed(__UpperCamelCase ) A__ , A__ : int = get_dataloaders(__UpperCamelCase , __UpperCamelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) A__ : Optional[int] = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=__UpperCamelCase ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). A__ : Tuple = model.to(accelerator.device ) # Instantiate optimizer A__ : Optional[int] = AdamW(params=model.parameters() , lr=__UpperCamelCase ) # Instantiate scheduler A__ : Any = get_linear_schedule_with_warmup( optimizer=__UpperCamelCase , num_warmup_steps=1_00 , num_training_steps=(len(__UpperCamelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. A__ , A__ , A__ , A__ , A__ : Dict = accelerator.prepare( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # Now we train the model for epoch in range(__UpperCamelCase ): model.train() for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) A__ : Dict = model(**__UpperCamelCase ) A__ : Dict = outputs.loss A__ : List[str] = loss / gradient_accumulation_steps accelerator.backward(__UpperCamelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() A__ : Optional[int] = 0 for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): A__ : Union[str, Any] = model(**__UpperCamelCase ) A__ : int = outputs.logits.argmax(dim=-1 ) A__ , A__ : Optional[Any] = accelerator.gather((predictions, batch['''labels''']) ) # New Code # # First we check if it's a distributed system if accelerator.use_distributed: # Then see if we're on the last batch of our eval dataloader if step == len(__UpperCamelCase ) - 1: # Last batch needs to be truncated on distributed systems as it contains additional samples A__ : Tuple = predictions[: len(eval_dataloader.dataset ) - samples_seen] A__ : int = references[: len(eval_dataloader.dataset ) - samples_seen] else: # Otherwise we add the number of samples seen samples_seen += references.shape[0] # All of this can be avoided if you use `Accelerator.gather_for_metrics` instead of `Accelerator.gather`: # accelerator.gather_for_metrics((predictions, batch["labels"])) metric.add_batch( predictions=__UpperCamelCase , references=__UpperCamelCase , ) A__ : Union[str, Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}:" , __UpperCamelCase ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: """simple docstring""" A__ : Tuple = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=__UpperCamelCase , default=__UpperCamelCase , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) A__ : Dict = parser.parse_args() A__ : Any = {'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": main()
55
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> int: """simple docstring""" if n == 1 or not isinstance(__UpperCamelCase , __UpperCamelCase ): return 0 elif n == 2: return 1 else: A__ : Any = [0, 1] for i in range(2 , n + 1 ): sequence.append(sequence[i - 1] + sequence[i - 2] ) return sequence[n] def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> int: """simple docstring""" A__ : Dict = 0 A__ : Optional[int] = 2 while digits < n: index += 1 A__ : Dict = len(str(fibonacci(__UpperCamelCase ) ) ) return index def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int = 10_00 ) -> int: """simple docstring""" return fibonacci_digits_index(__UpperCamelCase ) if __name__ == "__main__": print(solution(int(str(input()).strip())))
55
1
from math import sqrt def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> bool: """simple docstring""" assert isinstance(__UpperCamelCase , __UpperCamelCase ) and ( number >= 0 ), "'number' must been an int and positive" A__ : str = True # 0 and 1 are none primes. if number <= 1: A__ : Tuple = False for divisor in range(2 , int(round(sqrt(__UpperCamelCase ) ) ) + 1 ): # if 'number' divisible by 'divisor' then sets 'status' # of false and break up the loop. if number % divisor == 0: A__ : Optional[Any] = False break # precondition assert isinstance(__UpperCamelCase , __UpperCamelCase ), "'status' must been from type bool" return status def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> List[Any]: """simple docstring""" assert isinstance(__UpperCamelCase , __UpperCamelCase ) and (n > 2), "'N' must been an int and > 2" # beginList: contains all natural numbers from 2 up to N A__ : Any = list(range(2 , n + 1 ) ) A__ : int = [] # this list will be returns. # actual sieve of erathostenes for i in range(len(__UpperCamelCase ) ): for j in range(i + 1 , len(__UpperCamelCase ) ): if (begin_list[i] != 0) and (begin_list[j] % begin_list[i] == 0): A__ : int = 0 # filters actual prime numbers. A__ : Optional[int] = [x for x in begin_list if x != 0] # precondition assert isinstance(__UpperCamelCase , __UpperCamelCase ), "'ans' must been from type list" return ans def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> Union[str, Any]: """simple docstring""" assert isinstance(__UpperCamelCase , __UpperCamelCase ) and (n > 2), "'N' must been an int and > 2" A__ : Tuple = [] # iterates over all numbers between 2 up to N+1 # if a number is prime then appends to list 'ans' for number in range(2 , n + 1 ): if is_prime(__UpperCamelCase ): ans.append(__UpperCamelCase ) # precondition assert isinstance(__UpperCamelCase , __UpperCamelCase ), "'ans' must been from type list" return ans def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict ) -> Any: """simple docstring""" assert isinstance(__UpperCamelCase , __UpperCamelCase ) and number >= 0, "'number' must been an int and >= 0" A__ : int = [] # this list will be returns of the function. # potential prime number factors. A__ : Tuple = 2 A__ : Optional[int] = number if number == 0 or number == 1: ans.append(__UpperCamelCase ) # if 'number' not prime then builds the prime factorization of 'number' elif not is_prime(__UpperCamelCase ): while quotient != 1: if is_prime(__UpperCamelCase ) and (quotient % factor == 0): ans.append(__UpperCamelCase ) quotient /= factor else: factor += 1 else: ans.append(__UpperCamelCase ) # precondition assert isinstance(__UpperCamelCase , __UpperCamelCase ), "'ans' must been from type list" return ans def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Dict: """simple docstring""" assert isinstance(__UpperCamelCase , __UpperCamelCase ) and ( number >= 0 ), "'number' bust been an int and >= 0" A__ : Any = 0 # prime factorization of 'number' A__ : Optional[int] = prime_factorization(__UpperCamelCase ) A__ : str = max(__UpperCamelCase ) # precondition assert isinstance(__UpperCamelCase , __UpperCamelCase ), "'ans' must been from type int" return ans def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> Any: """simple docstring""" assert isinstance(__UpperCamelCase , __UpperCamelCase ) and ( number >= 0 ), "'number' bust been an int and >= 0" A__ : Union[str, Any] = 0 # prime factorization of 'number' A__ : Optional[Any] = prime_factorization(__UpperCamelCase ) A__ : Optional[int] = min(__UpperCamelCase ) # precondition assert isinstance(__UpperCamelCase , __UpperCamelCase ), "'ans' must been from type int" return ans def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" assert isinstance(__UpperCamelCase , __UpperCamelCase ), "'number' must been an int" assert isinstance(number % 2 == 0 , __UpperCamelCase ), "compare bust been from type bool" return number % 2 == 0 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> Union[str, Any]: """simple docstring""" assert isinstance(__UpperCamelCase , __UpperCamelCase ), "'number' must been an int" assert isinstance(number % 2 != 0 , __UpperCamelCase ), "compare bust been from type bool" return number % 2 != 0 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> int: """simple docstring""" assert ( isinstance(__UpperCamelCase , __UpperCamelCase ) and (number > 2) and is_even(__UpperCamelCase ) ), "'number' must been an int, even and > 2" A__ : Dict = [] # this list will returned # creates a list of prime numbers between 2 up to 'number' A__ : List[str] = get_prime_numbers(__UpperCamelCase ) A__ : int = len(__UpperCamelCase ) # run variable for while-loops. A__ : List[str] = 0 A__ : List[str] = None # exit variable. for break up the loops A__ : Any = True while i < len_pn and loop: A__ : str = i + 1 while j < len_pn and loop: if prime_numbers[i] + prime_numbers[j] == number: A__ : str = False ans.append(prime_numbers[i] ) ans.append(prime_numbers[j] ) j += 1 i += 1 # precondition assert ( isinstance(__UpperCamelCase , __UpperCamelCase ) and (len(__UpperCamelCase ) == 2) and (ans[0] + ans[1] == number) and is_prime(ans[0] ) and is_prime(ans[1] ) ), "'ans' must contains two primes. And sum of elements must been eq 'number'" return ans def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple , __UpperCamelCase : int ) -> List[Any]: """simple docstring""" assert ( isinstance(__UpperCamelCase , __UpperCamelCase ) and isinstance(__UpperCamelCase , __UpperCamelCase ) and (numbera >= 0) and (numbera >= 0) ), "'number1' and 'number2' must been positive integer." A__ : int = 0 while numbera != 0: A__ : Dict = numbera % numbera A__ : List[str] = numbera A__ : int = rest # precondition assert isinstance(__UpperCamelCase , __UpperCamelCase ) and ( numbera >= 0 ), "'number' must been from type int and positive" return numbera def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[int] , __UpperCamelCase : Any ) -> Dict: """simple docstring""" assert ( isinstance(__UpperCamelCase , __UpperCamelCase ) and isinstance(__UpperCamelCase , __UpperCamelCase ) and (numbera >= 1) and (numbera >= 1) ), "'number1' and 'number2' must been positive integer." A__ : Any = 1 # actual answer that will be return. # for kgV (x,1) if numbera > 1 and numbera > 1: # builds the prime factorization of 'number1' and 'number2' A__ : List[str] = prime_factorization(__UpperCamelCase ) A__ : Dict = prime_factorization(__UpperCamelCase ) elif numbera == 1 or numbera == 1: A__ : Any = [] A__ : Dict = [] A__ : Optional[int] = max(__UpperCamelCase , __UpperCamelCase ) A__ : str = 0 A__ : List[Any] = 0 A__ : List[str] = [] # captured numbers int both 'primeFac1' and 'primeFac2' # iterates through primeFac1 for n in prime_fac_a: if n not in done: if n in prime_fac_a: A__ : Union[str, Any] = prime_fac_a.count(__UpperCamelCase ) A__ : List[str] = prime_fac_a.count(__UpperCamelCase ) for _ in range(max(__UpperCamelCase , __UpperCamelCase ) ): ans *= n else: A__ : Any = prime_fac_a.count(__UpperCamelCase ) for _ in range(__UpperCamelCase ): ans *= n done.append(__UpperCamelCase ) # iterates through primeFac2 for n in prime_fac_a: if n not in done: A__ : List[Any] = prime_fac_a.count(__UpperCamelCase ) for _ in range(__UpperCamelCase ): ans *= n done.append(__UpperCamelCase ) # precondition assert isinstance(__UpperCamelCase , __UpperCamelCase ) and ( ans >= 0 ), "'ans' must been from type int and positive" return ans def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> str: """simple docstring""" assert isinstance(__UpperCamelCase , __UpperCamelCase ) and (n >= 0), "'number' must been a positive int" A__ : Tuple = 0 A__ : int = 2 # this variable holds the answer while index < n: index += 1 ans += 1 # counts to the next number # if ans not prime then # runs to the next prime number. while not is_prime(__UpperCamelCase ): ans += 1 # precondition assert isinstance(__UpperCamelCase , __UpperCamelCase ) and is_prime( __UpperCamelCase ), "'ans' must been a prime number and from type int" return ans def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] , __UpperCamelCase : Optional[Any] ) -> Union[str, Any]: """simple docstring""" assert ( is_prime(__UpperCamelCase ) and is_prime(__UpperCamelCase ) and (p_number_a < p_number_a) ), "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" A__ : List[Any] = p_number_a + 1 # jump to the next number A__ : Any = [] # this list will be returns. # if number is not prime then # fetch the next prime number. while not is_prime(__UpperCamelCase ): number += 1 while number < p_number_a: ans.append(__UpperCamelCase ) number += 1 # fetch the next prime number. while not is_prime(__UpperCamelCase ): number += 1 # precondition assert ( isinstance(__UpperCamelCase , __UpperCamelCase ) and ans[0] != p_number_a and ans[len(__UpperCamelCase ) - 1] != p_number_a ), "'ans' must been a list without the arguments" # 'ans' contains not 'pNumber1' and 'pNumber2' ! return ans def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] ) -> Optional[int]: """simple docstring""" assert isinstance(__UpperCamelCase , __UpperCamelCase ) and (n >= 1), "'n' must been int and >= 1" A__ : str = [] # will be returned. for divisor in range(1 , n + 1 ): if n % divisor == 0: ans.append(__UpperCamelCase ) # precondition assert ans[0] == 1 and ans[len(__UpperCamelCase ) - 1] == n, "Error in function getDivisiors(...)" return ans def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> Union[str, Any]: """simple docstring""" assert isinstance(__UpperCamelCase , __UpperCamelCase ) and ( number > 1 ), "'number' must been an int and >= 1" A__ : List[str] = get_divisors(__UpperCamelCase ) # precondition assert ( isinstance(__UpperCamelCase , __UpperCamelCase ) and (divisors[0] == 1) and (divisors[len(__UpperCamelCase ) - 1] == number) ), "Error in help-function getDivisiors(...)" # summed all divisors up to 'number' (exclusive), hence [:-1] return sum(divisors[:-1] ) == number def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple , __UpperCamelCase : Optional[int] ) -> List[str]: """simple docstring""" assert ( isinstance(__UpperCamelCase , __UpperCamelCase ) and isinstance(__UpperCamelCase , __UpperCamelCase ) and (denominator != 0) ), "The arguments must been from type int and 'denominator' != 0" # build the greatest common divisor of numerator and denominator. A__ : Union[str, Any] = gcd(abs(__UpperCamelCase ) , abs(__UpperCamelCase ) ) # precondition assert ( isinstance(__UpperCamelCase , __UpperCamelCase ) and (numerator % gcd_of_fraction == 0) and (denominator % gcd_of_fraction == 0) ), "Error in function gcd(...,...)" return (numerator // gcd_of_fraction, denominator // gcd_of_fraction) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> Optional[Any]: """simple docstring""" assert isinstance(__UpperCamelCase , __UpperCamelCase ) and (n >= 0), "'n' must been a int and >= 0" A__ : Tuple = 1 # this will be return. for factor in range(1 , n + 1 ): ans *= factor return ans def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> List[Any]: """simple docstring""" assert isinstance(__UpperCamelCase , __UpperCamelCase ) and (n >= 0), "'n' must been an int and >= 0" A__ : List[Any] = 0 A__ : Optional[int] = 1 A__ : List[Any] = 1 # this will be return for _ in range(n - 1 ): A__ : str = ans ans += fiba A__ : List[Any] = tmp return ans
55
_SCREAMING_SNAKE_CASE : List[str] = range(2, 2_0 + 1) _SCREAMING_SNAKE_CASE : Optional[Any] = [1_0**k for k in range(ks[-1] + 1)] _SCREAMING_SNAKE_CASE : dict[int, dict[int, list[list[int]]]] = {} def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any , __UpperCamelCase : Dict , __UpperCamelCase : Any , __UpperCamelCase : Optional[Any] ) -> int: """simple docstring""" A__ : Tuple = sum(a_i[j] for j in range(__UpperCamelCase , len(__UpperCamelCase ) ) ) A__ : Tuple = sum(a_i[j] * base[j] for j in range(min(len(__UpperCamelCase ) , __UpperCamelCase ) ) ) A__ , A__ : Optional[int] = 0, 0 A__ : List[Any] = n - i A__ : Any = memo.get(__UpperCamelCase ) if sub_memo is not None: A__ : Optional[int] = sub_memo.get(__UpperCamelCase ) if jumps is not None and len(__UpperCamelCase ) > 0: # find and make the largest jump without going over A__ : List[Any] = -1 for _k in range(len(__UpperCamelCase ) - 1 , -1 , -1 ): if jumps[_k][2] <= k and jumps[_k][1] <= max_dn: A__ : List[str] = _k break if max_jump >= 0: A__ , A__ , A__ : List[Any] = jumps[max_jump] # since the difference between jumps is cached, add c A__ : int = diff + c for j in range(min(__UpperCamelCase , len(__UpperCamelCase ) ) ): A__ , A__ : List[str] = divmod(__UpperCamelCase , 10 ) if new_c > 0: add(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else: A__ : List[Any] = [] else: A__ : Optional[Any] = {c: []} A__ : int = sub_memo if dn >= max_dn or c + diff >= base[k]: return diff, dn if k > ks[0]: while True: # keep doing smaller jumps A__ , A__ : str = next_term(__UpperCamelCase , k - 1 , i + dn , __UpperCamelCase ) diff += _diff dn += terms_jumped if dn >= max_dn or c + diff >= base[k]: break else: # would be too small a jump, just compute sequential terms instead A__ , A__ : str = compute(__UpperCamelCase , __UpperCamelCase , i + dn , __UpperCamelCase ) diff += _diff dn += terms_jumped A__ : str = sub_memo[c] # keep jumps sorted by # of terms skipped A__ : List[Any] = 0 while j < len(__UpperCamelCase ): if jumps[j][1] > dn: break j += 1 # cache the jump for this value digitsum(b) and c sub_memo[c].insert(__UpperCamelCase , (diff, dn, k) ) return (diff, dn) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : Optional[int] , __UpperCamelCase : List[str] , __UpperCamelCase : int ) -> Any: """simple docstring""" if i >= n: return 0, i if k > len(__UpperCamelCase ): a_i.extend([0 for _ in range(k - len(__UpperCamelCase ) )] ) # note: a_i -> b * 10^k + c # ds_b -> digitsum(b) # ds_c -> digitsum(c) A__ : Optional[Any] = i A__ , A__ , A__ : Dict = 0, 0, 0 for j in range(len(__UpperCamelCase ) ): if j >= k: ds_b += a_i[j] else: ds_c += a_i[j] while i < n: i += 1 A__ : int = ds_c + ds_b diff += addend A__ : List[Any] = 0 for j in range(__UpperCamelCase ): A__ : Optional[Any] = a_i[j] + addend A__ , A__ : List[str] = divmod(__UpperCamelCase , 10 ) ds_c += a_i[j] if addend > 0: break if addend > 0: add(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) return diff, i - start_i def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict , __UpperCamelCase : List[Any] , __UpperCamelCase : int ) -> Tuple: """simple docstring""" for j in range(__UpperCamelCase , len(__UpperCamelCase ) ): A__ : Any = digits[j] + addend if s >= 10: A__ , A__ : Union[str, Any] = divmod(__UpperCamelCase , 10 ) A__ : Optional[int] = addend // 10 + quotient else: A__ : Any = s A__ : Dict = addend // 10 if addend == 0: break while addend > 0: A__ , A__ : Dict = divmod(__UpperCamelCase , 10 ) digits.append(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int = 10**15 ) -> int: """simple docstring""" A__ : List[Any] = [1] A__ : Dict = 1 A__ : Tuple = 0 while True: A__ , A__ : List[str] = next_term(__UpperCamelCase , 20 , i + dn , __UpperCamelCase ) dn += terms_jumped if dn == n - i: break A__ : List[str] = 0 for j in range(len(__UpperCamelCase ) ): a_n += digits[j] * 10**j return a_n if __name__ == "__main__": print(f"""{solution() = }""")
55
1
import warnings from typing import Dict import numpy as np from ..utils import ExplicitEnum, add_end_docstrings, is_tf_available, is_torch_available from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> Union[str, Any]: """simple docstring""" return 1.0 / (1.0 + np.exp(-_outputs )) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> int: """simple docstring""" A__ : List[Any] = np.max(_outputs , axis=-1 , keepdims=__UpperCamelCase ) A__ : Dict = np.exp(_outputs - maxes ) return shifted_exp / shifted_exp.sum(axis=-1 , keepdims=__UpperCamelCase ) class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "sigmoid" _lowerCAmelCase = "softmax" _lowerCAmelCase = "none" @add_end_docstrings( SCREAMING_SNAKE_CASE_, R"\n return_all_scores (`bool`, *optional*, defaults to `False`):\n Whether to return all prediction scores or just the one of the predicted class.\n function_to_apply (`str`, *optional*, defaults to `\"default\"`):\n The function to apply to the model outputs in order to retrieve the scores. Accepts four different values:\n\n - `\"default\"`: if the model has a single label, will apply the sigmoid function on the output. If the model\n has several labels, will apply the softmax function on the output.\n - `\"sigmoid\"`: Applies the sigmoid function on the output.\n - `\"softmax\"`: Applies the softmax function on the output.\n - `\"none\"`: Does not apply any function on the output.\n ", ) class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = False _lowerCAmelCase = ClassificationFunction.NONE def __init__( self , **UpperCamelCase__ ): super().__init__(**UpperCamelCase__ ) self.check_model_type( TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if self.framework == '''tf''' else MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING ) def __snake_case ( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__="" , **UpperCamelCase__ ): # Using "" as default argument because we're going to use `top_k=None` in user code to declare # "No top_k" A__ : List[str] = tokenizer_kwargs A__ : int = {} if hasattr(self.model.config , '''return_all_scores''' ) and return_all_scores is None: A__ : Union[str, Any] = self.model.config.return_all_scores if isinstance(UpperCamelCase__ , UpperCamelCase__ ) or top_k is None: A__ : Any = top_k A__ : List[Any] = False elif return_all_scores is not None: warnings.warn( '''`return_all_scores` is now deprecated, if want a similar functionality use `top_k=None` instead of''' ''' `return_all_scores=True` or `top_k=1` instead of `return_all_scores=False`.''' , UpperCamelCase__ , ) if return_all_scores: A__ : Optional[int] = None else: A__ : Dict = 1 if isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : List[Any] = ClassificationFunction[function_to_apply.upper()] if function_to_apply is not None: A__ : Optional[Any] = function_to_apply return preprocess_params, {}, postprocess_params def __call__( self , *UpperCamelCase__ , **UpperCamelCase__ ): A__ : Optional[int] = super().__call__(*UpperCamelCase__ , **UpperCamelCase__ ) # TODO try and retrieve it in a nicer way from _sanitize_parameters. A__ : Any = '''top_k''' not in kwargs if isinstance(args[0] , UpperCamelCase__ ) and _legacy: # This pipeline is odd, and return a list when single item is run return [result] else: return result def __snake_case ( self , UpperCamelCase__ , **UpperCamelCase__ ): A__ : Union[str, Any] = self.framework if isinstance(UpperCamelCase__ , UpperCamelCase__ ): return self.tokenizer(**UpperCamelCase__ , return_tensors=UpperCamelCase__ , **UpperCamelCase__ ) elif isinstance(UpperCamelCase__ , UpperCamelCase__ ) and len(UpperCamelCase__ ) == 1 and isinstance(inputs[0] , UpperCamelCase__ ) and len(inputs[0] ) == 2: # It used to be valid to use a list of list of list for text pairs, keeping this path for BC return self.tokenizer( text=inputs[0][0] , text_pair=inputs[0][1] , return_tensors=UpperCamelCase__ , **UpperCamelCase__ ) elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): # This is likely an invalid usage of the pipeline attempting to pass text pairs. raise ValueError( '''The pipeline received invalid inputs, if you are trying to send text pairs, you can try to send a''' ''' dictionary `{"text": "My text", "text_pair": "My pair"}` in order to send a text pair.''' ) return self.tokenizer(UpperCamelCase__ , return_tensors=UpperCamelCase__ , **UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): return self.model(**UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__=1 , UpperCamelCase__=True ): # `_legacy` is used to determine if we're running the naked pipeline and in backward # compatibility mode, or if running the pipeline with `pipeline(..., top_k=1)` we're running # the more natural result containing the list. # Default value before `set_parameters` if function_to_apply is None: if self.model.config.problem_type == "multi_label_classification" or self.model.config.num_labels == 1: A__ : Union[str, Any] = ClassificationFunction.SIGMOID elif self.model.config.problem_type == "single_label_classification" or self.model.config.num_labels > 1: A__ : Any = ClassificationFunction.SOFTMAX elif hasattr(self.model.config , '''function_to_apply''' ) and function_to_apply is None: A__ : Tuple = self.model.config.function_to_apply else: A__ : Optional[int] = ClassificationFunction.NONE A__ : Optional[Any] = model_outputs['''logits'''][0] A__ : Optional[Any] = outputs.numpy() if function_to_apply == ClassificationFunction.SIGMOID: A__ : Optional[Any] = sigmoid(UpperCamelCase__ ) elif function_to_apply == ClassificationFunction.SOFTMAX: A__ : Tuple = softmax(UpperCamelCase__ ) elif function_to_apply == ClassificationFunction.NONE: A__ : Any = outputs else: raise ValueError(F"Unrecognized `function_to_apply` argument: {function_to_apply}" ) if top_k == 1 and _legacy: return {"label": self.model.config.idalabel[scores.argmax().item()], "score": scores.max().item()} A__ : Optional[Any] = [ {'''label''': self.model.config.idalabel[i], '''score''': score.item()} for i, score in enumerate(UpperCamelCase__ ) ] if not _legacy: dict_scores.sort(key=lambda UpperCamelCase__ : x["score"] , reverse=UpperCamelCase__ ) if top_k is not None: A__ : List[Any] = dict_scores[:top_k] return dict_scores
55
import asyncio import os import shutil import subprocess import sys import tempfile import unittest from distutils.util import strtobool from functools import partial from pathlib import Path from typing import List, Union from unittest import mock import torch from ..state import AcceleratorState, PartialState from ..utils import ( gather, is_bnb_available, is_comet_ml_available, is_datasets_available, is_deepspeed_available, is_mps_available, is_safetensors_available, is_tensorboard_available, is_torch_version, is_tpu_available, is_transformers_available, is_wandb_available, is_xpu_available, ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] , __UpperCamelCase : int=False ) -> Tuple: """simple docstring""" try: A__ : Dict = os.environ[key] except KeyError: # KEY isn't set, default to `default`. A__ : Tuple = default else: # KEY is set, convert it to True or False. try: A__ : Union[str, Any] = strtobool(__UpperCamelCase ) except ValueError: # More values are supported, but let's keep the message simple. raise ValueError(F"If set, {key} must be yes or no." ) return _value _SCREAMING_SNAKE_CASE : Union[str, Any] = parse_flag_from_env('RUN_SLOW', default=False) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] ) -> Any: """simple docstring""" return unittest.skip('''Test was skipped''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> Union[str, Any]: """simple docstring""" return unittest.skipUnless(_run_slow_tests , '''test is slow''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> int: """simple docstring""" return unittest.skipUnless(not torch.cuda.is_available() , '''test requires only a CPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] ) -> Tuple: """simple docstring""" return unittest.skipUnless(torch.cuda.is_available() , '''test requires a GPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict ) -> List[str]: """simple docstring""" return unittest.skipUnless(is_xpu_available() , '''test requires a XPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict ) -> Any: """simple docstring""" return unittest.skipUnless(is_mps_available() , '''test requires a `mps` backend support in `torch`''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Optional[Any]: """simple docstring""" return unittest.skipUnless( is_transformers_available() and is_datasets_available() , '''test requires the Hugging Face suite''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> Tuple: """simple docstring""" return unittest.skipUnless(is_bnb_available() , '''test requires the bitsandbytes library''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> List[Any]: """simple docstring""" return unittest.skipUnless(is_tpu_available() , '''test requires TPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Tuple: """simple docstring""" return unittest.skipUnless(torch.cuda.device_count() == 1 , '''test requires a GPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Dict: """simple docstring""" return unittest.skipUnless(torch.xpu.device_count() == 1 , '''test requires a XPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> str: """simple docstring""" return unittest.skipUnless(torch.cuda.device_count() > 1 , '''test requires multiple GPUs''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Any: """simple docstring""" return unittest.skipUnless(torch.xpu.device_count() > 1 , '''test requires multiple XPUs''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> int: """simple docstring""" return unittest.skipUnless(is_safetensors_available() , '''test requires safetensors''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> Optional[Any]: """simple docstring""" return unittest.skipUnless(is_deepspeed_available() , '''test requires DeepSpeed''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> List[Any]: """simple docstring""" return unittest.skipUnless(is_torch_version('''>=''' , '''1.12.0''' ) , '''test requires torch version >= 1.12.0''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[int]=None , __UpperCamelCase : List[Any]=None ) -> Optional[Any]: """simple docstring""" if test_case is None: return partial(__UpperCamelCase , version=__UpperCamelCase ) return unittest.skipUnless(is_torch_version('''>=''' , __UpperCamelCase ) , F"test requires torch version >= {version}" )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] ) -> Optional[int]: """simple docstring""" return unittest.skipUnless(is_tensorboard_available() , '''test requires Tensorboard''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> Tuple: """simple docstring""" return unittest.skipUnless(is_wandb_available() , '''test requires wandb''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> Any: """simple docstring""" return unittest.skipUnless(is_comet_ml_available() , '''test requires comet_ml''' )(__UpperCamelCase ) _SCREAMING_SNAKE_CASE : Union[str, Any] = ( any([is_wandb_available(), is_tensorboard_available()]) and not is_comet_ml_available() ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[int] ) -> Optional[Any]: """simple docstring""" return unittest.skipUnless( _atleast_one_tracker_available , '''test requires at least one tracker to be available and for `comet_ml` to not be installed''' , )(__UpperCamelCase ) class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = True @classmethod def __snake_case ( cls ): A__ : Tuple = tempfile.mkdtemp() @classmethod def __snake_case ( cls ): if os.path.exists(cls.tmpdir ): shutil.rmtree(cls.tmpdir ) def __snake_case ( self ): if self.clear_on_setup: for path in Path(self.tmpdir ).glob('''**/*''' ): if path.is_file(): path.unlink() elif path.is_dir(): shutil.rmtree(UpperCamelCase__ ) class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): super().tearDown() # Reset the state of the AcceleratorState singleton. AcceleratorState._reset_state() PartialState._reset_state() class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self , UpperCamelCase__ ): A__ : Tuple = mocks if isinstance(UpperCamelCase__ , (tuple, list) ) else [mocks] for m in self.mocks: m.start() self.addCleanup(m.stop ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> Any: """simple docstring""" A__ : int = AcceleratorState() A__ : Any = tensor[None].clone().to(state.device ) A__ : Optional[int] = gather(__UpperCamelCase ).cpu() A__ : Any = tensor[0].cpu() for i in range(tensors.shape[0] ): if not torch.equal(tensors[i] , __UpperCamelCase ): return False return True class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : List[Any] = returncode A__ : Union[str, Any] = stdout A__ : Dict = stderr async def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str , __UpperCamelCase : Optional[Any] ) -> Any: """simple docstring""" while True: A__ : Tuple = await stream.readline() if line: callback(__UpperCamelCase ) else: break async def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] , __UpperCamelCase : Optional[Any]=None , __UpperCamelCase : List[Any]=None , __UpperCamelCase : Tuple=None , __UpperCamelCase : Tuple=False , __UpperCamelCase : List[Any]=False ) -> _RunOutput: """simple docstring""" if echo: print('''\nRunning: ''' , ''' '''.join(__UpperCamelCase ) ) A__ : int = await asyncio.create_subprocess_exec( cmd[0] , *cmd[1:] , stdin=__UpperCamelCase , stdout=asyncio.subprocess.PIPE , stderr=asyncio.subprocess.PIPE , env=__UpperCamelCase , ) # note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe # https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait # # If it starts hanging, will need to switch to the following code. The problem is that no data # will be seen until it's done and if it hangs for example there will be no debug info. # out, err = await p.communicate() # return _RunOutput(p.returncode, out, err) A__ : List[Any] = [] A__ : str = [] def tee(__UpperCamelCase : Optional[Any] , __UpperCamelCase : Union[str, Any] , __UpperCamelCase : Dict , __UpperCamelCase : List[Any]="" ): A__ : Optional[Any] = line.decode('''utf-8''' ).rstrip() sink.append(__UpperCamelCase ) if not quiet: print(__UpperCamelCase , __UpperCamelCase , file=__UpperCamelCase ) # XXX: the timeout doesn't seem to make any difference here await asyncio.wait( [ asyncio.create_task(_read_stream(p.stdout , lambda __UpperCamelCase : tee(__UpperCamelCase , __UpperCamelCase , sys.stdout , label='''stdout:''' ) ) ), asyncio.create_task(_read_stream(p.stderr , lambda __UpperCamelCase : tee(__UpperCamelCase , __UpperCamelCase , sys.stderr , label='''stderr:''' ) ) ), ] , timeout=__UpperCamelCase , ) return _RunOutput(await p.wait() , __UpperCamelCase , __UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] , __UpperCamelCase : Any=None , __UpperCamelCase : List[Any]=None , __UpperCamelCase : List[str]=1_80 , __UpperCamelCase : List[str]=False , __UpperCamelCase : Dict=True ) -> _RunOutput: """simple docstring""" A__ : Dict = asyncio.get_event_loop() A__ : Optional[Any] = loop.run_until_complete( _stream_subprocess(__UpperCamelCase , env=__UpperCamelCase , stdin=__UpperCamelCase , timeout=__UpperCamelCase , quiet=__UpperCamelCase , echo=__UpperCamelCase ) ) A__ : Union[str, Any] = ''' '''.join(__UpperCamelCase ) if result.returncode > 0: A__ : Optional[Any] = '''\n'''.join(result.stderr ) raise RuntimeError( F"'{cmd_str}' failed with returncode {result.returncode}\n\n" F"The combined stderr from workers follows:\n{stderr}" ) return result class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] , __UpperCamelCase : List[Any]=False ) -> Dict: """simple docstring""" try: A__ : List[Any] = subprocess.check_output(__UpperCamelCase , stderr=subprocess.STDOUT ) if return_stdout: if hasattr(__UpperCamelCase , '''decode''' ): A__ : Any = output.decode('''utf-8''' ) return output except subprocess.CalledProcessError as e: raise SubprocessCallException( F"Command `{' '.join(__UpperCamelCase )}` failed with the following error:\n\n{e.output.decode()}" ) from e
55
1
from ...configuration_utils import PretrainedConfig from ...utils import logging _SCREAMING_SNAKE_CASE : Optional[int] = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Any = { 'facebook/nllb-moe-54B': 'https://huggingface.co/facebook/nllb-moe-54b/resolve/main/config.json', } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "nllb-moe" _lowerCAmelCase = ["past_key_values"] _lowerCAmelCase = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"} def __init__( self , UpperCamelCase__=12_8112 , UpperCamelCase__=1024 , UpperCamelCase__=12 , UpperCamelCase__=4096 , UpperCamelCase__=16 , UpperCamelCase__=12 , UpperCamelCase__=4096 , UpperCamelCase__=16 , UpperCamelCase__=0.0_5 , UpperCamelCase__=0.0_5 , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__="relu" , UpperCamelCase__=1024 , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=0.0 , UpperCamelCase__=0.0_2 , UpperCamelCase__=2 , UpperCamelCase__=True , UpperCamelCase__=False , UpperCamelCase__="float32" , UpperCamelCase__=False , UpperCamelCase__=128 , UpperCamelCase__=64 , UpperCamelCase__=4 , UpperCamelCase__=4 , UpperCamelCase__=0.0_0_1 , UpperCamelCase__=0.0_0_1 , UpperCamelCase__="all" , UpperCamelCase__=False , UpperCamelCase__=False , UpperCamelCase__=1.0 , UpperCamelCase__=0.2 , UpperCamelCase__=1 , UpperCamelCase__=0 , UpperCamelCase__=2 , UpperCamelCase__=False , **UpperCamelCase__ , ): A__ : List[str] = vocab_size A__ : str = max_position_embeddings A__ : Dict = d_model A__ : int = encoder_ffn_dim A__ : Optional[Any] = encoder_layers A__ : Optional[Any] = encoder_attention_heads A__ : int = decoder_ffn_dim A__ : Dict = decoder_layers A__ : Optional[int] = decoder_attention_heads A__ : Dict = dropout A__ : int = attention_dropout A__ : List[str] = activation_dropout A__ : Union[str, Any] = activation_function A__ : int = init_std A__ : Optional[Any] = encoder_layerdrop A__ : Optional[Any] = decoder_layerdrop A__ : int = use_cache A__ : int = encoder_layers A__ : str = scale_embedding # scale factor will be sqrt(d_model) if True A__ : Any = router_z_loss_coef A__ : str = router_aux_loss_coef A__ : Dict = decoder_sparse_step A__ : int = encoder_sparse_step A__ : Any = num_experts A__ : Tuple = expert_capacity A__ : Any = router_bias if router_dtype not in ["float32", "float16", "bfloat16"]: raise ValueError(F"`router_dtype` must be one of 'float32', 'float16' or 'bfloat16', got {router_dtype}" ) A__ : Dict = router_dtype A__ : Optional[Any] = router_ignore_padding_tokens A__ : Optional[Any] = batch_prioritized_routing A__ : Optional[int] = second_expert_policy A__ : List[Any] = normalize_router_prob_before_dropping A__ : Optional[int] = moe_eval_capacity_token_fraction A__ : Tuple = moe_token_dropout A__ : Optional[Any] = output_router_logits super().__init__( pad_token_id=UpperCamelCase__ , bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , is_encoder_decoder=UpperCamelCase__ , decoder_start_token_id=UpperCamelCase__ , **UpperCamelCase__ , )
55
import numpy as np _SCREAMING_SNAKE_CASE : Any = [ ['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'k'], ['l', 'm', 'n', 'o', 'p'], ['q', 'r', 's', 't', 'u'], ['v', 'w', 'x', 'y', 'z'], ] class UpperCamelCase__ : '''simple docstring''' def __init__( self ): A__ : List[Any] = np.array(UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): A__ , A__ : Any = np.where(letter == self.SQUARE ) A__ : int = np.concatenate([indexa + 1, indexa + 1] ) return indexes def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ ): A__ : Union[str, Any] = self.SQUARE[indexa - 1, indexa - 1] return letter def __snake_case ( self , UpperCamelCase__ ): A__ : List[str] = message.lower() A__ : str = message.replace(''' ''' , '''''' ) A__ : Union[str, Any] = message.replace('''j''' , '''i''' ) A__ : List[Any] = np.empty((2, len(UpperCamelCase__ )) ) for letter_index in range(len(UpperCamelCase__ ) ): A__ : Any = self.letter_to_numbers(message[letter_index] ) A__ : Optional[Any] = numbers[0] A__ : List[str] = numbers[1] A__ : List[str] = first_step.reshape(2 * len(UpperCamelCase__ ) ) A__ : List[Any] = '''''' for numbers_index in range(len(UpperCamelCase__ ) ): A__ : Dict = int(second_step[numbers_index * 2] ) A__ : List[str] = int(second_step[(numbers_index * 2) + 1] ) A__ : Dict = self.numbers_to_letter(UpperCamelCase__ , UpperCamelCase__ ) A__ : Tuple = encoded_message + letter return encoded_message def __snake_case ( self , UpperCamelCase__ ): A__ : str = message.lower() message.replace(''' ''' , '''''' ) A__ : List[Any] = np.empty(2 * len(UpperCamelCase__ ) ) for letter_index in range(len(UpperCamelCase__ ) ): A__ : List[str] = self.letter_to_numbers(message[letter_index] ) A__ : Dict = numbers[0] A__ : int = numbers[1] A__ : Optional[Any] = first_step.reshape((2, len(UpperCamelCase__ )) ) A__ : int = '''''' for numbers_index in range(len(UpperCamelCase__ ) ): A__ : Tuple = int(second_step[0, numbers_index] ) A__ : Dict = int(second_step[1, numbers_index] ) A__ : List[str] = self.numbers_to_letter(UpperCamelCase__ , UpperCamelCase__ ) A__ : Tuple = decoded_message + letter return decoded_message
55
1
import copy import re class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = "hp" _lowerCAmelCase = {} _lowerCAmelCase = None @classmethod def __snake_case ( cls , UpperCamelCase__ , UpperCamelCase__ ): A__ : Optional[int] = prefix A__ : str = defaults cls.build_naming_info() @staticmethod def __snake_case ( UpperCamelCase__ , UpperCamelCase__ ): if len(UpperCamelCase__ ) == 0: return "" A__ : str = None if any(char.isdigit() for char in word ): raise Exception(F"Parameters should not contain numbers: '{word}' contains a number" ) if word in info["short_word"]: return info["short_word"][word] for prefix_len in range(1 , len(UpperCamelCase__ ) + 1 ): A__ : List[str] = word[:prefix_len] if prefix in info["reverse_short_word"]: continue else: A__ : List[Any] = prefix break if short_word is None: # Paranoid fallback def int_to_alphabetic(UpperCamelCase__ ): A__ : Dict = '''''' while integer != 0: A__ : Dict = chr(ord('''A''' ) + integer % 10 ) + s integer //= 10 return s A__ : Union[str, Any] = 0 while True: A__ : Optional[Any] = word + '''#''' + int_to_alphabetic(UpperCamelCase__ ) if sword in info["reverse_short_word"]: continue else: A__ : str = sword break A__ : List[Any] = short_word A__ : str = word return short_word @staticmethod def __snake_case ( UpperCamelCase__ , UpperCamelCase__ ): A__ : Union[str, Any] = param_name.split('''_''' ) A__ : List[Any] = [TrialShortNamer.shortname_for_word(UpperCamelCase__ , UpperCamelCase__ ) for word in words] # We try to create a separatorless short name, but if there is a collision we have to fallback # to a separated short name A__ : Optional[int] = ['''''', '''_'''] for separator in separators: A__ : Optional[int] = separator.join(UpperCamelCase__ ) if shortname not in info["reverse_short_param"]: A__ : List[Any] = shortname A__ : Union[str, Any] = param_name return shortname return param_name @staticmethod def __snake_case ( UpperCamelCase__ , UpperCamelCase__ ): A__ : Any = TrialShortNamer.shortname_for_key(UpperCamelCase__ , UpperCamelCase__ ) A__ : List[Any] = short_name A__ : Tuple = param_name @classmethod def __snake_case ( cls ): if cls.NAMING_INFO is not None: return A__ : Optional[Any] = { '''short_word''': {}, '''reverse_short_word''': {}, '''short_param''': {}, '''reverse_short_param''': {}, } A__ : Union[str, Any] = list(cls.DEFAULTS.keys() ) for k in field_keys: cls.add_new_param_name(UpperCamelCase__ , UpperCamelCase__ ) A__ : Tuple = info @classmethod def __snake_case ( cls , UpperCamelCase__ ): cls.build_naming_info() assert cls.PREFIX is not None A__ : Any = [copy.copy(cls.PREFIX )] for k, v in params.items(): if k not in cls.DEFAULTS: raise Exception(F"You should provide a default value for the param name {k} with value {v}" ) if v == cls.DEFAULTS[k]: # The default value is not added to the name continue A__ : Union[str, Any] = cls.NAMING_INFO['''short_param'''][k] if isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Tuple = 1 if v else 0 A__ : List[Any] = '''''' if isinstance(UpperCamelCase__ , (int, float) ) else '''-''' A__ : Any = F"{key}{sep}{v}" name.append(UpperCamelCase__ ) return "_".join(UpperCamelCase__ ) @classmethod def __snake_case ( cls , UpperCamelCase__ ): A__ : Optional[Any] = repr[len(cls.PREFIX ) + 1 :] if repr == "": A__ : Any = [] else: A__ : int = repr.split('''_''' ) A__ : Dict = {} for value in values: if "-" in value: A__ , A__ : Dict = value.split('''-''' ) else: A__ : Optional[int] = re.sub('''[0-9.]''' , '''''' , UpperCamelCase__ ) A__ : str = float(re.sub('''[^0-9.]''' , '''''' , UpperCamelCase__ ) ) A__ : Union[str, Any] = cls.NAMING_INFO['''reverse_short_param'''][p_k] A__ : List[Any] = p_v for k in cls.DEFAULTS: if k not in parameters: A__ : List[Any] = cls.DEFAULTS[k] return parameters
55
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline else: from .pipeline_kandinsky import KandinskyPipeline from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput from .text_encoder import MultilingualCLIP
55
1
import argparse import os from pathlib import Path import torch from bark.generation import _load_model as _bark_load_model from huggingface_hub import hf_hub_download from transformers import EncodecConfig, EncodecModel, set_seed from transformers.models.bark.configuration_bark import ( BarkCoarseConfig, BarkConfig, BarkFineConfig, BarkSemanticConfig, ) from transformers.models.bark.generation_configuration_bark import ( BarkCoarseGenerationConfig, BarkFineGenerationConfig, BarkGenerationConfig, BarkSemanticGenerationConfig, ) from transformers.models.bark.modeling_bark import BarkCoarseModel, BarkFineModel, BarkModel, BarkSemanticModel from transformers.utils import logging logging.set_verbosity_info() _SCREAMING_SNAKE_CASE : Any = logging.get_logger(__name__) set_seed(7_7_0) _SCREAMING_SNAKE_CASE : Optional[Any] = { 'c_attn': 'att_proj', 'c_proj': 'out_proj', 'c_fc': 'in_proj', 'transformer.': '', 'h.': 'layers.', 'ln_1': 'layernorm_1', 'ln_2': 'layernorm_2', 'ln_f': 'layernorm_final', 'wpe': 'position_embeds_layer', 'wte': 'input_embeds_layer', } _SCREAMING_SNAKE_CASE : Optional[int] = { 'text_small': { 'repo_id': 'suno/bark', 'file_name': 'text.pt', }, 'coarse_small': { 'repo_id': 'suno/bark', 'file_name': 'coarse.pt', }, 'fine_small': { 'repo_id': 'suno/bark', 'file_name': 'fine.pt', }, 'text': { 'repo_id': 'suno/bark', 'file_name': 'text_2.pt', }, 'coarse': { 'repo_id': 'suno/bark', 'file_name': 'coarse_2.pt', }, 'fine': { 'repo_id': 'suno/bark', 'file_name': 'fine_2.pt', }, } _SCREAMING_SNAKE_CASE : Tuple = os.path.dirname(os.path.abspath(__file__)) _SCREAMING_SNAKE_CASE : List[str] = os.path.join(os.path.expanduser('~'), '.cache') _SCREAMING_SNAKE_CASE : Tuple = os.path.join(os.getenv('XDG_CACHE_HOME', default_cache_dir), 'suno', 'bark_v0') def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any , __UpperCamelCase : Union[str, Any]=False ) -> Optional[Any]: """simple docstring""" A__ : Optional[Any] = model_type if use_small: key += "_small" return os.path.join(__UpperCamelCase , REMOTE_MODEL_PATHS[key]['''file_name'''] ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] , __UpperCamelCase : Dict ) -> Any: """simple docstring""" os.makedirs(__UpperCamelCase , exist_ok=__UpperCamelCase ) hf_hub_download(repo_id=__UpperCamelCase , filename=__UpperCamelCase , local_dir=__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] , __UpperCamelCase : Any , __UpperCamelCase : int=False , __UpperCamelCase : Optional[int]="text" ) -> Union[str, Any]: """simple docstring""" if model_type == "text": A__ : Optional[int] = BarkSemanticModel A__ : str = BarkSemanticConfig A__ : int = BarkSemanticGenerationConfig elif model_type == "coarse": A__ : Optional[int] = BarkCoarseModel A__ : Tuple = BarkCoarseConfig A__ : Optional[int] = BarkCoarseGenerationConfig elif model_type == "fine": A__ : str = BarkFineModel A__ : List[Any] = BarkFineConfig A__ : Optional[Any] = BarkFineGenerationConfig else: raise NotImplementedError() A__ : Optional[Any] = F"{model_type}_small" if use_small else model_type A__ : str = REMOTE_MODEL_PATHS[model_key] if not os.path.exists(__UpperCamelCase ): logger.info(F"{model_type} model not found, downloading into `{CACHE_DIR}`." ) _download(model_info['''repo_id'''] , model_info['''file_name'''] ) A__ : int = torch.load(__UpperCamelCase , map_location=__UpperCamelCase ) # this is a hack A__ : Union[str, Any] = checkpoint['''model_args'''] if "input_vocab_size" not in model_args: A__ : Any = model_args['''vocab_size'''] A__ : int = model_args['''vocab_size'''] del model_args["vocab_size"] # convert Bark model arguments to HF Bark model arguments A__ : Optional[Any] = model_args.pop('''n_head''' ) A__ : Tuple = model_args.pop('''n_embd''' ) A__ : Tuple = model_args.pop('''n_layer''' ) A__ : Any = ConfigClass(**checkpoint['''model_args'''] ) A__ : Tuple = ModelClass(config=__UpperCamelCase ) A__ : Any = GenerationConfigClass() A__ : Optional[int] = model_generation_config A__ : Union[str, Any] = checkpoint['''model'''] # fixup checkpoint A__ : List[str] = '''_orig_mod.''' for k, v in list(state_dict.items() ): if k.startswith(__UpperCamelCase ): # replace part of the key with corresponding layer name in HF implementation A__ : List[Any] = k[len(__UpperCamelCase ) :] for old_layer_name in new_layer_name_dict: A__ : List[Any] = new_k.replace(__UpperCamelCase , new_layer_name_dict[old_layer_name] ) A__ : Optional[int] = state_dict.pop(__UpperCamelCase ) A__ : List[str] = set(state_dict.keys() ) - set(model.state_dict().keys() ) A__ : Optional[int] = {k for k in extra_keys if not k.endswith('''.attn.bias''' )} A__ : Optional[int] = set(model.state_dict().keys() ) - set(state_dict.keys() ) A__ : Any = {k for k in missing_keys if not k.endswith('''.attn.bias''' )} if len(__UpperCamelCase ) != 0: raise ValueError(F"extra keys found: {extra_keys}" ) if len(__UpperCamelCase ) != 0: raise ValueError(F"missing keys: {missing_keys}" ) model.load_state_dict(__UpperCamelCase , strict=__UpperCamelCase ) A__ : Optional[Any] = model.num_parameters(exclude_embeddings=__UpperCamelCase ) A__ : str = checkpoint['''best_val_loss'''].item() logger.info(F"model loaded: {round(n_params/1e6 , 1 )}M params, {round(__UpperCamelCase , 3 )} loss" ) model.eval() model.to(__UpperCamelCase ) del checkpoint, state_dict return model def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] , __UpperCamelCase : Optional[Any]=False , __UpperCamelCase : Tuple="text" ) -> int: """simple docstring""" if model_type not in ("text", "coarse", "fine"): raise NotImplementedError() A__ : str = '''cpu''' # do conversion on cpu A__ : int = _get_ckpt_path(__UpperCamelCase , use_small=__UpperCamelCase ) A__ : Optional[int] = _load_model(__UpperCamelCase , __UpperCamelCase , model_type=__UpperCamelCase , use_small=__UpperCamelCase ) # load bark initial model A__ : List[Any] = _bark_load_model(__UpperCamelCase , '''cpu''' , model_type=__UpperCamelCase , use_small=__UpperCamelCase ) if model_type == "text": A__ : Optional[int] = bark_model['''model'''] if model.num_parameters(exclude_embeddings=__UpperCamelCase ) != bark_model.get_num_params(): raise ValueError('''initial and new models don\'t have the same number of parameters''' ) # check if same output as the bark model A__ : Optional[Any] = 5 A__ : Dict = 10 if model_type in ["text", "coarse"]: A__ : Tuple = torch.randint(2_56 , (batch_size, sequence_length) , dtype=torch.int ) A__ : Tuple = bark_model(__UpperCamelCase )[0] A__ : int = model(__UpperCamelCase ) # take last logits A__ : int = output_new_model_total.logits[:, [-1], :] else: A__ : str = 3 A__ : List[str] = 8 A__ : List[Any] = torch.randint(2_56 , (batch_size, sequence_length, n_codes_total) , dtype=torch.int ) A__ : List[Any] = model(__UpperCamelCase , __UpperCamelCase ) A__ : Union[str, Any] = bark_model(__UpperCamelCase , __UpperCamelCase ) A__ : Optional[Any] = output_new_model_total.logits # output difference should come from the difference of self-attention implementation design if output_new_model.shape != output_old_model.shape: raise ValueError('''initial and new outputs don\'t have the same shape''' ) if (output_new_model - output_old_model).abs().max().item() > 1e-3: raise ValueError('''initial and new outputs are not equal''' ) Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) model.save_pretrained(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict , __UpperCamelCase : Optional[Any] , __UpperCamelCase : Union[str, Any] , __UpperCamelCase : Optional[Any] , __UpperCamelCase : Dict , __UpperCamelCase : Optional[int] , ) -> Dict: """simple docstring""" A__ : int = os.path.join(__UpperCamelCase , __UpperCamelCase ) A__ : List[str] = BarkSemanticConfig.from_pretrained(os.path.join(__UpperCamelCase , '''config.json''' ) ) A__ : Optional[Any] = BarkCoarseConfig.from_pretrained(os.path.join(__UpperCamelCase , '''config.json''' ) ) A__ : str = BarkFineConfig.from_pretrained(os.path.join(__UpperCamelCase , '''config.json''' ) ) A__ : int = EncodecConfig.from_pretrained('''facebook/encodec_24khz''' ) A__ : Dict = BarkSemanticModel.from_pretrained(__UpperCamelCase ) A__ : str = BarkCoarseModel.from_pretrained(__UpperCamelCase ) A__ : Optional[int] = BarkFineModel.from_pretrained(__UpperCamelCase ) A__ : int = EncodecModel.from_pretrained('''facebook/encodec_24khz''' ) A__ : str = BarkConfig.from_sub_model_configs( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) A__ : Any = BarkGenerationConfig.from_sub_model_configs( semantic.generation_config , coarseAcoustic.generation_config , fineAcoustic.generation_config ) A__ : List[Any] = BarkModel(__UpperCamelCase ) A__ : List[Any] = semantic A__ : Tuple = coarseAcoustic A__ : Union[str, Any] = fineAcoustic A__ : Any = codec A__ : Union[str, Any] = bark_generation_config Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) bark.save_pretrained(__UpperCamelCase , repo_id=__UpperCamelCase , push_to_hub=__UpperCamelCase ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE : int = argparse.ArgumentParser() # Required parameters parser.add_argument('model_type', type=str, help='text, coarse or fine.') parser.add_argument('pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--is_small', action='store_true', help='convert the small version instead of the large.') _SCREAMING_SNAKE_CASE : Tuple = parser.parse_args() load_model(args.pytorch_dump_folder_path, model_type=args.model_type, use_small=args.is_small)
55
import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to properly calculate the metrics on the # validation dataset when in a distributed system, and builds off the # `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _SCREAMING_SNAKE_CASE : str = 1_6 _SCREAMING_SNAKE_CASE : Tuple = 3_2 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Accelerator , __UpperCamelCase : int = 16 ) -> Optional[int]: """simple docstring""" A__ : List[str] = AutoTokenizer.from_pretrained('''bert-base-cased''' ) A__ : Optional[int] = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__UpperCamelCase : Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) A__ : int = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=__UpperCamelCase , max_length=__UpperCamelCase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): A__ : Optional[int] = datasets.map( __UpperCamelCase , batched=__UpperCamelCase , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library A__ : List[Any] = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__UpperCamelCase : Any ): # On TPU it's best to pad everything to the same length or training will be very slow. A__ : Optional[Any] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": A__ : int = 16 elif accelerator.mixed_precision != "no": A__ : Any = 8 else: A__ : Union[str, Any] = None return tokenizer.pad( __UpperCamelCase , padding='''longest''' , max_length=__UpperCamelCase , pad_to_multiple_of=__UpperCamelCase , return_tensors='''pt''' , ) # Instantiate dataloaders. A__ : Optional[int] = DataLoader( tokenized_datasets['''train'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) A__ : Tuple = DataLoader( tokenized_datasets['''validation'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('TESTING_MOCKED_DATALOADERS', None) == "1": from accelerate.test_utils.training import mocked_dataloaders _SCREAMING_SNAKE_CASE : Dict = mocked_dataloaders # noqa: F811 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : List[Any] ) -> Optional[Any]: """simple docstring""" if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , __UpperCamelCase ) == "1": A__ : List[str] = 2 # Initialize accelerator A__ : Optional[Any] = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs A__ : Tuple = config['''lr'''] A__ : Dict = int(config['''num_epochs'''] ) A__ : int = int(config['''seed'''] ) A__ : Optional[Any] = int(config['''batch_size'''] ) A__ : int = evaluate.load('''glue''' , '''mrpc''' ) # If the batch size is too big we use gradient accumulation A__ : Union[str, Any] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: A__ : List[Any] = batch_size // MAX_GPU_BATCH_SIZE A__ : Dict = MAX_GPU_BATCH_SIZE set_seed(__UpperCamelCase ) A__ , A__ : int = get_dataloaders(__UpperCamelCase , __UpperCamelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) A__ : Optional[int] = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=__UpperCamelCase ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). A__ : Tuple = model.to(accelerator.device ) # Instantiate optimizer A__ : Optional[int] = AdamW(params=model.parameters() , lr=__UpperCamelCase ) # Instantiate scheduler A__ : Any = get_linear_schedule_with_warmup( optimizer=__UpperCamelCase , num_warmup_steps=1_00 , num_training_steps=(len(__UpperCamelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. A__ , A__ , A__ , A__ , A__ : Dict = accelerator.prepare( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # Now we train the model for epoch in range(__UpperCamelCase ): model.train() for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) A__ : Dict = model(**__UpperCamelCase ) A__ : Dict = outputs.loss A__ : List[str] = loss / gradient_accumulation_steps accelerator.backward(__UpperCamelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() A__ : Optional[int] = 0 for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): A__ : Union[str, Any] = model(**__UpperCamelCase ) A__ : int = outputs.logits.argmax(dim=-1 ) A__ , A__ : Optional[Any] = accelerator.gather((predictions, batch['''labels''']) ) # New Code # # First we check if it's a distributed system if accelerator.use_distributed: # Then see if we're on the last batch of our eval dataloader if step == len(__UpperCamelCase ) - 1: # Last batch needs to be truncated on distributed systems as it contains additional samples A__ : Tuple = predictions[: len(eval_dataloader.dataset ) - samples_seen] A__ : int = references[: len(eval_dataloader.dataset ) - samples_seen] else: # Otherwise we add the number of samples seen samples_seen += references.shape[0] # All of this can be avoided if you use `Accelerator.gather_for_metrics` instead of `Accelerator.gather`: # accelerator.gather_for_metrics((predictions, batch["labels"])) metric.add_batch( predictions=__UpperCamelCase , references=__UpperCamelCase , ) A__ : Union[str, Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}:" , __UpperCamelCase ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: """simple docstring""" A__ : Tuple = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=__UpperCamelCase , default=__UpperCamelCase , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) A__ : Dict = parser.parse_args() A__ : Any = {'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": main()
55
1
import argparse import tensorflow as tf import torch from transformers import BertConfig, BertForMaskedLM from transformers.models.bert.modeling_bert import ( BertIntermediate, BertLayer, BertOutput, BertPooler, BertSelfAttention, BertSelfOutput, ) from transformers.utils import logging logging.set_verbosity_info() def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str , __UpperCamelCase : str , __UpperCamelCase : str ) -> int: """simple docstring""" def get_masked_lm_array(__UpperCamelCase : str ): A__ : Any = F"masked_lm/{name}/.ATTRIBUTES/VARIABLE_VALUE" A__ : Union[str, Any] = tf.train.load_variable(__UpperCamelCase , __UpperCamelCase ) if "kernel" in name: A__ : str = array.transpose() return torch.from_numpy(__UpperCamelCase ) def get_encoder_array(__UpperCamelCase : str ): A__ : Dict = F"encoder/{name}/.ATTRIBUTES/VARIABLE_VALUE" A__ : List[str] = tf.train.load_variable(__UpperCamelCase , __UpperCamelCase ) if "kernel" in name: A__ : Any = array.transpose() return torch.from_numpy(__UpperCamelCase ) def get_encoder_layer_array(__UpperCamelCase : int , __UpperCamelCase : str ): A__ : Dict = F"encoder/_transformer_layers/{layer_index}/{name}/.ATTRIBUTES/VARIABLE_VALUE" A__ : List[str] = tf.train.load_variable(__UpperCamelCase , __UpperCamelCase ) if "kernel" in name: A__ : List[str] = array.transpose() return torch.from_numpy(__UpperCamelCase ) def get_encoder_attention_layer_array(__UpperCamelCase : int , __UpperCamelCase : str , __UpperCamelCase : str ): A__ : Any = F"encoder/_transformer_layers/{layer_index}/_attention_layer/{name}/.ATTRIBUTES/VARIABLE_VALUE" A__ : List[Any] = tf.train.load_variable(__UpperCamelCase , __UpperCamelCase ) A__ : Optional[int] = array.reshape(__UpperCamelCase ) if "kernel" in name: A__ : Optional[Any] = array.transpose() return torch.from_numpy(__UpperCamelCase ) print(F"Loading model based on config from {config_path}..." ) A__ : List[Any] = BertConfig.from_json_file(__UpperCamelCase ) A__ : Tuple = BertForMaskedLM(__UpperCamelCase ) # Layers for layer_index in range(0 , config.num_hidden_layers ): A__ : BertLayer = model.bert.encoder.layer[layer_index] # Self-attention A__ : BertSelfAttention = layer.attention.self A__ : str = get_encoder_attention_layer_array( __UpperCamelCase , '''_query_dense/kernel''' , self_attn.query.weight.data.shape ) A__ : Optional[Any] = get_encoder_attention_layer_array( __UpperCamelCase , '''_query_dense/bias''' , self_attn.query.bias.data.shape ) A__ : Dict = get_encoder_attention_layer_array( __UpperCamelCase , '''_key_dense/kernel''' , self_attn.key.weight.data.shape ) A__ : Optional[Any] = get_encoder_attention_layer_array( __UpperCamelCase , '''_key_dense/bias''' , self_attn.key.bias.data.shape ) A__ : Dict = get_encoder_attention_layer_array( __UpperCamelCase , '''_value_dense/kernel''' , self_attn.value.weight.data.shape ) A__ : int = get_encoder_attention_layer_array( __UpperCamelCase , '''_value_dense/bias''' , self_attn.value.bias.data.shape ) # Self-attention Output A__ : BertSelfOutput = layer.attention.output A__ : Optional[int] = get_encoder_attention_layer_array( __UpperCamelCase , '''_output_dense/kernel''' , self_output.dense.weight.data.shape ) A__ : Tuple = get_encoder_attention_layer_array( __UpperCamelCase , '''_output_dense/bias''' , self_output.dense.bias.data.shape ) A__ : Tuple = get_encoder_layer_array(__UpperCamelCase , '''_attention_layer_norm/gamma''' ) A__ : Union[str, Any] = get_encoder_layer_array(__UpperCamelCase , '''_attention_layer_norm/beta''' ) # Intermediate A__ : BertIntermediate = layer.intermediate A__ : Tuple = get_encoder_layer_array(__UpperCamelCase , '''_intermediate_dense/kernel''' ) A__ : int = get_encoder_layer_array(__UpperCamelCase , '''_intermediate_dense/bias''' ) # Output A__ : BertOutput = layer.output A__ : Any = get_encoder_layer_array(__UpperCamelCase , '''_output_dense/kernel''' ) A__ : Union[str, Any] = get_encoder_layer_array(__UpperCamelCase , '''_output_dense/bias''' ) A__ : Optional[Any] = get_encoder_layer_array(__UpperCamelCase , '''_output_layer_norm/gamma''' ) A__ : Union[str, Any] = get_encoder_layer_array(__UpperCamelCase , '''_output_layer_norm/beta''' ) # Embeddings A__ : Dict = get_encoder_array('''_position_embedding_layer/embeddings''' ) A__ : int = get_encoder_array('''_type_embedding_layer/embeddings''' ) A__ : Any = get_encoder_array('''_embedding_norm_layer/gamma''' ) A__ : Tuple = get_encoder_array('''_embedding_norm_layer/beta''' ) # LM Head A__ : str = model.cls.predictions.transform A__ : List[Any] = get_masked_lm_array('''dense/kernel''' ) A__ : Tuple = get_masked_lm_array('''dense/bias''' ) A__ : List[Any] = get_masked_lm_array('''layer_norm/gamma''' ) A__ : str = get_masked_lm_array('''layer_norm/beta''' ) A__ : Any = get_masked_lm_array('''embedding_table''' ) # Pooling A__ : Optional[int] = BertPooler(config=__UpperCamelCase ) A__ : BertPooler = get_encoder_array('''_pooler_layer/kernel''' ) A__ : BertPooler = get_encoder_array('''_pooler_layer/bias''' ) # Export final model model.save_pretrained(__UpperCamelCase ) # Integration test - should load without any errors ;) A__ : Any = BertForMaskedLM.from_pretrained(__UpperCamelCase ) print(new_model.eval() ) print('''Model conversion was done sucessfully!''' ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE : List[Any] = argparse.ArgumentParser() parser.add_argument( '--tf_checkpoint_path', type=str, required=True, help='Path to the TensorFlow Token Dropping checkpoint path.' ) parser.add_argument( '--bert_config_file', type=str, required=True, help='The config json file corresponding to the BERT model. This specifies the model architecture.', ) parser.add_argument( '--pytorch_dump_path', type=str, required=True, help='Path to the output PyTorch model.', ) _SCREAMING_SNAKE_CASE : Tuple = parser.parse_args() convert_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)
55
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor from ..utils import is_datasets_available from .base import PipelineTool if is_datasets_available(): from datasets import load_dataset class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "microsoft/speecht5_tts" _lowerCAmelCase = ( "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the " "text to read (in English) and returns a waveform object containing the sound." ) _lowerCAmelCase = "text_reader" _lowerCAmelCase = SpeechTaProcessor _lowerCAmelCase = SpeechTaForTextToSpeech _lowerCAmelCase = SpeechTaHifiGan _lowerCAmelCase = ["text"] _lowerCAmelCase = ["audio"] def __snake_case ( self ): if self.post_processor is None: A__ : int = '''microsoft/speecht5_hifigan''' super().setup() def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__=None ): A__ : List[Any] = self.pre_processor(text=UpperCamelCase__ , return_tensors='''pt''' , truncation=UpperCamelCase__ ) if speaker_embeddings is None: if not is_datasets_available(): raise ImportError('''Datasets needs to be installed if not passing speaker embeddings.''' ) A__ : List[Any] = load_dataset('''Matthijs/cmu-arctic-xvectors''' , split='''validation''' ) A__ : Dict = torch.tensor(embeddings_dataset[7305]['''xvector'''] ).unsqueeze(0 ) return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings} def __snake_case ( self , UpperCamelCase__ ): with torch.no_grad(): return self.model.generate_speech(**UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): with torch.no_grad(): return self.post_processor(UpperCamelCase__ ).cpu().detach()
55
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) _SCREAMING_SNAKE_CASE : Union[str, Any] = { 'configuration_mobilebert': [ 'MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MobileBertConfig', 'MobileBertOnnxConfig', ], 'tokenization_mobilebert': ['MobileBertTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Optional[int] = ['MobileBertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Optional[int] = [ 'MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'MobileBertForMaskedLM', 'MobileBertForMultipleChoice', 'MobileBertForNextSentencePrediction', 'MobileBertForPreTraining', 'MobileBertForQuestionAnswering', 'MobileBertForSequenceClassification', 'MobileBertForTokenClassification', 'MobileBertLayer', 'MobileBertModel', 'MobileBertPreTrainedModel', 'load_tf_weights_in_mobilebert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : str = [ 'TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFMobileBertForMaskedLM', 'TFMobileBertForMultipleChoice', 'TFMobileBertForNextSentencePrediction', 'TFMobileBertForPreTraining', 'TFMobileBertForQuestionAnswering', 'TFMobileBertForSequenceClassification', 'TFMobileBertForTokenClassification', 'TFMobileBertMainLayer', 'TFMobileBertModel', 'TFMobileBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_mobilebert import ( MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileBertConfig, MobileBertOnnxConfig, ) from .tokenization_mobilebert import MobileBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mobilebert_fast import MobileBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilebert import ( MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, MobileBertForMaskedLM, MobileBertForMultipleChoice, MobileBertForNextSentencePrediction, MobileBertForPreTraining, MobileBertForQuestionAnswering, MobileBertForSequenceClassification, MobileBertForTokenClassification, MobileBertLayer, MobileBertModel, MobileBertPreTrainedModel, load_tf_weights_in_mobilebert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mobilebert import ( TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFMobileBertForMaskedLM, TFMobileBertForMultipleChoice, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertMainLayer, TFMobileBertModel, TFMobileBertPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
55
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation _SCREAMING_SNAKE_CASE : Optional[int] = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Optional[Any] = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} _SCREAMING_SNAKE_CASE : List[str] = { 'tokenizer_file': { 'EleutherAI/gpt-neox-20b': 'https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/tokenizer.json', }, } _SCREAMING_SNAKE_CASE : Dict = { 'gpt-neox-20b': 2_0_4_8, } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = VOCAB_FILES_NAMES _lowerCAmelCase = PRETRAINED_VOCAB_FILES_MAP _lowerCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCAmelCase = ["input_ids", "attention_mask"] def __init__( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__="<|endoftext|>" , UpperCamelCase__="<|endoftext|>" , UpperCamelCase__="<|endoftext|>" , UpperCamelCase__=False , **UpperCamelCase__ , ): super().__init__( UpperCamelCase__ , UpperCamelCase__ , tokenizer_file=UpperCamelCase__ , unk_token=UpperCamelCase__ , bos_token=UpperCamelCase__ , eos_token=UpperCamelCase__ , add_prefix_space=UpperCamelCase__ , **UpperCamelCase__ , ) A__ : Optional[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , UpperCamelCase__ ) != add_prefix_space: A__ : Union[str, Any] = getattr(UpperCamelCase__ , pre_tok_state.pop('''type''' ) ) A__ : List[Any] = add_prefix_space A__ : Any = pre_tok_class(**UpperCamelCase__ ) A__ : List[Any] = add_prefix_space def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ = None ): A__ : Any = self._tokenizer.model.save(UpperCamelCase__ , name=UpperCamelCase__ ) return tuple(UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): A__ : List[str] = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) + [self.eos_token_id] ) if len(UpperCamelCase__ ) > self.model_max_length: A__ : Tuple = input_ids[-self.model_max_length :] return input_ids
55
1
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int = 60_08_51_47_51_43 ) -> int: """simple docstring""" try: A__ : Any = int(__UpperCamelCase ) except (TypeError, ValueError): raise TypeError('''Parameter n must be int or castable to int.''' ) if n <= 0: raise ValueError('''Parameter n must be greater than or equal to one.''' ) A__ : Tuple = 2 A__ : Dict = 0 if n == 2: return 2 while n > 2: while n % i != 0: i += 1 A__ : List[Any] = i while n % i == 0: A__ : Optional[int] = n // i i += 1 return int(__UpperCamelCase ) if __name__ == "__main__": print(f"""{solution() = }""")
55
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING _SCREAMING_SNAKE_CASE : Union[str, Any] = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : int = { '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__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "deformable_detr" _lowerCAmelCase = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=3 , UpperCamelCase__=300 , UpperCamelCase__=1024 , UpperCamelCase__=6 , UpperCamelCase__=1024 , UpperCamelCase__=8 , UpperCamelCase__=6 , UpperCamelCase__=1024 , UpperCamelCase__=8 , UpperCamelCase__=0.0 , UpperCamelCase__=True , UpperCamelCase__="relu" , UpperCamelCase__=256 , UpperCamelCase__=0.1 , UpperCamelCase__=0.0 , UpperCamelCase__=0.0 , UpperCamelCase__=0.0_2 , UpperCamelCase__=1.0 , UpperCamelCase__=True , UpperCamelCase__=False , UpperCamelCase__="sine" , UpperCamelCase__="resnet50" , UpperCamelCase__=True , UpperCamelCase__=False , UpperCamelCase__=4 , UpperCamelCase__=4 , UpperCamelCase__=4 , UpperCamelCase__=False , UpperCamelCase__=300 , UpperCamelCase__=False , UpperCamelCase__=1 , UpperCamelCase__=5 , UpperCamelCase__=2 , UpperCamelCase__=1 , UpperCamelCase__=1 , UpperCamelCase__=5 , UpperCamelCase__=2 , UpperCamelCase__=0.1 , UpperCamelCase__=0.2_5 , UpperCamelCase__=False , **UpperCamelCase__ , ): if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) A__ : int = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Union[str, Any] = backbone_config.get('''model_type''' ) A__ : Union[str, Any] = CONFIG_MAPPING[backbone_model_type] A__ : Optional[int] = config_class.from_dict(UpperCamelCase__ ) A__ : Tuple = use_timm_backbone A__ : int = backbone_config A__ : List[Any] = num_channels A__ : List[Any] = num_queries A__ : str = max_position_embeddings A__ : Tuple = d_model A__ : int = encoder_ffn_dim A__ : Union[str, Any] = encoder_layers A__ : Optional[Any] = encoder_attention_heads A__ : List[Any] = decoder_ffn_dim A__ : Tuple = decoder_layers A__ : Optional[Any] = decoder_attention_heads A__ : List[str] = dropout A__ : str = attention_dropout A__ : List[Any] = activation_dropout A__ : Any = activation_function A__ : Optional[Any] = init_std A__ : Union[str, Any] = init_xavier_std A__ : Union[str, Any] = encoder_layerdrop A__ : Optional[int] = auxiliary_loss A__ : str = position_embedding_type A__ : List[Any] = backbone A__ : Optional[Any] = use_pretrained_backbone A__ : Any = dilation # deformable attributes A__ : List[Any] = num_feature_levels A__ : List[str] = encoder_n_points A__ : int = decoder_n_points A__ : List[Any] = two_stage A__ : Dict = two_stage_num_proposals A__ : Optional[int] = 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 A__ : List[str] = class_cost A__ : List[Any] = bbox_cost A__ : Any = giou_cost # Loss coefficients A__ : List[str] = mask_loss_coefficient A__ : Union[str, Any] = dice_loss_coefficient A__ : List[Any] = bbox_loss_coefficient A__ : Tuple = giou_loss_coefficient A__ : Optional[Any] = eos_coefficient A__ : List[Any] = focal_alpha A__ : List[str] = disable_custom_kernels super().__init__(is_encoder_decoder=UpperCamelCase__ , **UpperCamelCase__ ) @property def __snake_case ( self ): return self.encoder_attention_heads @property def __snake_case ( self ): return self.d_model def __snake_case ( self ): A__ : List[str] = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: A__ : Tuple = self.backbone_config.to_dict() A__ : Optional[int] = self.__class__.model_type return output
55
1
import requests _SCREAMING_SNAKE_CASE : int = 'YOUR API KEY' def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str , __UpperCamelCase : str = giphy_api_key ) -> list: """simple docstring""" A__ : Any = '''+'''.join(query.split() ) A__ : Optional[int] = F"https://api.giphy.com/v1/gifs/search?q={formatted_query}&api_key={api_key}" A__ : Optional[Any] = requests.get(__UpperCamelCase ).json()['''data'''] return [gif["url"] for gif in gifs] if __name__ == "__main__": print('\n'.join(get_gifs('space ship')))
55
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> List[Any]: """simple docstring""" A__ : Optional[Any] = 0 A__ : Optional[Any] = len(__UpperCamelCase ) for i in range(n - 1 ): for j in range(i + 1 , __UpperCamelCase ): if arr[i] > arr[j]: num_inversions += 1 return num_inversions def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Tuple: """simple docstring""" if len(__UpperCamelCase ) <= 1: return arr, 0 A__ : Optional[int] = len(__UpperCamelCase ) // 2 A__ : List[str] = arr[0:mid] A__ : Union[str, Any] = arr[mid:] A__ , A__ : List[Any] = count_inversions_recursive(__UpperCamelCase ) A__ , A__ : int = count_inversions_recursive(__UpperCamelCase ) A__ , A__ : Dict = _count_cross_inversions(__UpperCamelCase , __UpperCamelCase ) A__ : Any = inversion_p + inversions_q + cross_inversions return c, num_inversions def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] , __UpperCamelCase : List[Any] ) -> Dict: """simple docstring""" A__ : str = [] A__ : Tuple = 0 while i < len(__UpperCamelCase ) and j < len(__UpperCamelCase ): if p[i] > q[j]: # if P[1] > Q[j], then P[k] > Q[k] for all i < k <= len(P) # These are all inversions. The claim emerges from the # property that P is sorted. num_inversion += len(__UpperCamelCase ) - i r.append(q[j] ) j += 1 else: r.append(p[i] ) i += 1 if i < len(__UpperCamelCase ): r.extend(p[i:] ) else: r.extend(q[j:] ) return r, num_inversion def SCREAMING_SNAKE_CASE ( ) -> Tuple: """simple docstring""" A__ : List[str] = [10, 2, 1, 5, 5, 2, 11] # this arr has 8 inversions: # (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2) A__ : int = count_inversions_bf(__UpperCamelCase ) A__ , A__ : int = count_inversions_recursive(__UpperCamelCase ) assert num_inversions_bf == num_inversions_recursive == 8 print('''number of inversions = ''' , __UpperCamelCase ) # testing an array with zero inversion (a sorted arr_1) arr_a.sort() A__ : Optional[Any] = count_inversions_bf(__UpperCamelCase ) A__ , A__ : Dict = count_inversions_recursive(__UpperCamelCase ) assert num_inversions_bf == num_inversions_recursive == 0 print('''number of inversions = ''' , __UpperCamelCase ) # an empty list should also have zero inversions A__ : Union[str, Any] = [] A__ : Union[str, Any] = count_inversions_bf(__UpperCamelCase ) A__ , A__ : Any = count_inversions_recursive(__UpperCamelCase ) assert num_inversions_bf == num_inversions_recursive == 0 print('''number of inversions = ''' , __UpperCamelCase ) if __name__ == "__main__": main()
55
1
import shutil import tempfile import unittest import numpy as np from transformers.testing_utils import ( is_pt_tf_cross_test, require_tf, require_torch, require_torchvision, require_vision, ) from transformers.utils import is_tf_available, is_torch_available, is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, SamImageProcessor, SamProcessor if is_torch_available(): import torch if is_tf_available(): import tensorflow as tf @require_vision @require_torchvision class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): A__ : Union[str, Any] = tempfile.mkdtemp() A__ : Optional[Any] = SamImageProcessor() A__ : List[str] = SamProcessor(UpperCamelCase__ ) processor.save_pretrained(self.tmpdirname ) def __snake_case ( self , **UpperCamelCase__ ): return AutoProcessor.from_pretrained(self.tmpdirname , **UpperCamelCase__ ).image_processor def __snake_case ( self ): shutil.rmtree(self.tmpdirname ) def __snake_case ( self ): A__ : Optional[Any] = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] A__ : Optional[int] = [Image.fromarray(np.moveaxis(UpperCamelCase__ , 0 , -1 ) ) for x in image_inputs] return image_inputs def __snake_case ( self ): A__ : Union[str, Any] = SamProcessor(image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ : str = self.get_image_processor(do_normalize=UpperCamelCase__ , padding_value=1.0 ) A__ : List[str] = SamProcessor.from_pretrained(self.tmpdirname , do_normalize=UpperCamelCase__ , padding_value=1.0 ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase__ ) def __snake_case ( self ): A__ : int = self.get_image_processor() A__ : Optional[Any] = SamProcessor(image_processor=UpperCamelCase__ ) A__ : str = self.prepare_image_inputs() A__ : Dict = image_processor(UpperCamelCase__ , return_tensors='''np''' ) A__ : Dict = processor(images=UpperCamelCase__ , return_tensors='''np''' ) input_feat_extract.pop('''original_sizes''' ) # pop original_sizes as it is popped in the processor input_feat_extract.pop('''reshaped_input_sizes''' ) # pop original_sizes as it is popped in the processor for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) @require_torch def __snake_case ( self ): A__ : str = self.get_image_processor() A__ : Tuple = SamProcessor(image_processor=UpperCamelCase__ ) A__ : Any = [torch.ones((1, 3, 5, 5) )] A__ : Union[str, Any] = [[1764, 2646]] A__ : Optional[int] = [[683, 1024]] A__ : int = processor.post_process_masks(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) self.assertEqual(masks[0].shape , (1, 3, 1764, 2646) ) A__ : List[Any] = processor.post_process_masks( UpperCamelCase__ , torch.tensor(UpperCamelCase__ ) , torch.tensor(UpperCamelCase__ ) ) self.assertEqual(masks[0].shape , (1, 3, 1764, 2646) ) # should also work with np A__ : Optional[Any] = [np.ones((1, 3, 5, 5) )] A__ : Optional[int] = processor.post_process_masks(UpperCamelCase__ , np.array(UpperCamelCase__ ) , np.array(UpperCamelCase__ ) ) self.assertEqual(masks[0].shape , (1, 3, 1764, 2646) ) A__ : List[str] = [[1, 0], [0, 1]] with self.assertRaises(UpperCamelCase__ ): A__ : int = processor.post_process_masks(UpperCamelCase__ , np.array(UpperCamelCase__ ) , np.array(UpperCamelCase__ ) ) @require_vision @require_tf class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): A__ : Union[str, Any] = tempfile.mkdtemp() A__ : List[str] = SamImageProcessor() A__ : str = SamProcessor(UpperCamelCase__ ) processor.save_pretrained(self.tmpdirname ) def __snake_case ( self , **UpperCamelCase__ ): return AutoProcessor.from_pretrained(self.tmpdirname , **UpperCamelCase__ ).image_processor def __snake_case ( self ): shutil.rmtree(self.tmpdirname ) def __snake_case ( self ): A__ : Tuple = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] A__ : Any = [Image.fromarray(np.moveaxis(UpperCamelCase__ , 0 , -1 ) ) for x in image_inputs] return image_inputs def __snake_case ( self ): A__ : Tuple = SamProcessor(image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ : Any = self.get_image_processor(do_normalize=UpperCamelCase__ , padding_value=1.0 ) A__ : List[Any] = SamProcessor.from_pretrained(self.tmpdirname , do_normalize=UpperCamelCase__ , padding_value=1.0 ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase__ ) def __snake_case ( self ): A__ : Optional[int] = self.get_image_processor() A__ : Optional[int] = SamProcessor(image_processor=UpperCamelCase__ ) A__ : Dict = self.prepare_image_inputs() A__ : List[str] = image_processor(UpperCamelCase__ , return_tensors='''np''' ) A__ : Optional[int] = processor(images=UpperCamelCase__ , return_tensors='''np''' ) input_feat_extract.pop('''original_sizes''' ) # pop original_sizes as it is popped in the processor input_feat_extract.pop('''reshaped_input_sizes''' ) # pop reshaped_input_sizes as it is popped in the processor for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) @require_tf def __snake_case ( self ): A__ : Optional[Any] = self.get_image_processor() A__ : Optional[int] = SamProcessor(image_processor=UpperCamelCase__ ) A__ : str = [tf.ones((1, 3, 5, 5) )] A__ : List[Any] = [[1764, 2646]] A__ : Tuple = [[683, 1024]] A__ : Optional[int] = processor.post_process_masks(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , return_tensors='''tf''' ) self.assertEqual(masks[0].shape , (1, 3, 1764, 2646) ) A__ : Dict = processor.post_process_masks( UpperCamelCase__ , tf.convert_to_tensor(UpperCamelCase__ ) , tf.convert_to_tensor(UpperCamelCase__ ) , return_tensors='''tf''' , ) self.assertEqual(masks[0].shape , (1, 3, 1764, 2646) ) # should also work with np A__ : str = [np.ones((1, 3, 5, 5) )] A__ : str = processor.post_process_masks( UpperCamelCase__ , np.array(UpperCamelCase__ ) , np.array(UpperCamelCase__ ) , return_tensors='''tf''' ) self.assertEqual(masks[0].shape , (1, 3, 1764, 2646) ) A__ : Tuple = [[1, 0], [0, 1]] with self.assertRaises(tf.errors.InvalidArgumentError ): A__ : str = processor.post_process_masks( UpperCamelCase__ , np.array(UpperCamelCase__ ) , np.array(UpperCamelCase__ ) , return_tensors='''tf''' ) @require_vision @require_torchvision class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): A__ : Optional[int] = tempfile.mkdtemp() A__ : Any = SamImageProcessor() A__ : Union[str, Any] = SamProcessor(UpperCamelCase__ ) processor.save_pretrained(self.tmpdirname ) def __snake_case ( self , **UpperCamelCase__ ): return AutoProcessor.from_pretrained(self.tmpdirname , **UpperCamelCase__ ).image_processor def __snake_case ( self ): shutil.rmtree(self.tmpdirname ) def __snake_case ( self ): A__ : str = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] A__ : Dict = [Image.fromarray(np.moveaxis(UpperCamelCase__ , 0 , -1 ) ) for x in image_inputs] return image_inputs @is_pt_tf_cross_test def __snake_case ( self ): A__ : Any = self.get_image_processor() A__ : List[Any] = SamProcessor(image_processor=UpperCamelCase__ ) A__ : List[Any] = np.random.randint(0 , 2 , size=(1, 3, 5, 5) ).astype(np.floataa ) A__ : Tuple = [tf.convert_to_tensor(UpperCamelCase__ )] A__ : Any = [torch.tensor(UpperCamelCase__ )] A__ : Optional[int] = [[1764, 2646]] A__ : Any = [[683, 1024]] A__ : Optional[int] = processor.post_process_masks( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , return_tensors='''tf''' ) A__ : List[Any] = processor.post_process_masks( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , return_tensors='''pt''' ) self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy() ) ) @is_pt_tf_cross_test def __snake_case ( self ): A__ : Dict = self.get_image_processor() A__ : Dict = SamProcessor(image_processor=UpperCamelCase__ ) A__ : Union[str, Any] = self.prepare_image_inputs() A__ : Dict = image_processor(UpperCamelCase__ , return_tensors='''pt''' )['''pixel_values'''].numpy() A__ : List[str] = processor(images=UpperCamelCase__ , return_tensors='''pt''' )['''pixel_values'''].numpy() A__ : Union[str, Any] = image_processor(UpperCamelCase__ , return_tensors='''tf''' )['''pixel_values'''].numpy() A__ : Union[str, Any] = processor(images=UpperCamelCase__ , return_tensors='''tf''' )['''pixel_values'''].numpy() self.assertTrue(np.allclose(UpperCamelCase__ , UpperCamelCase__ ) ) self.assertTrue(np.allclose(UpperCamelCase__ , UpperCamelCase__ ) ) self.assertTrue(np.allclose(UpperCamelCase__ , UpperCamelCase__ ) )
55
from PIL import Image def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Image , __UpperCamelCase : float ) -> Image: """simple docstring""" def brightness(__UpperCamelCase : int ) -> float: return 1_28 + level + (c - 1_28) if not -2_5_5.0 <= level <= 2_5_5.0: raise ValueError('''level must be between -255.0 (black) and 255.0 (white)''' ) return img.point(__UpperCamelCase ) if __name__ == "__main__": # Load image with Image.open('image_data/lena.jpg') as img: # Change brightness to 100 _SCREAMING_SNAKE_CASE : Dict = change_brightness(img, 1_0_0) brigt_img.save('image_data/lena_brightness.png', format='png')
55
1
import numpy as np _SCREAMING_SNAKE_CASE : Any = [ ['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'k'], ['l', 'm', 'n', 'o', 'p'], ['q', 'r', 's', 't', 'u'], ['v', 'w', 'x', 'y', 'z'], ] class UpperCamelCase__ : '''simple docstring''' def __init__( self ): A__ : List[Any] = np.array(UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): A__ , A__ : Any = np.where(letter == self.SQUARE ) A__ : int = np.concatenate([indexa + 1, indexa + 1] ) return indexes def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ ): A__ : Union[str, Any] = self.SQUARE[indexa - 1, indexa - 1] return letter def __snake_case ( self , UpperCamelCase__ ): A__ : List[str] = message.lower() A__ : str = message.replace(''' ''' , '''''' ) A__ : Union[str, Any] = message.replace('''j''' , '''i''' ) A__ : List[Any] = np.empty((2, len(UpperCamelCase__ )) ) for letter_index in range(len(UpperCamelCase__ ) ): A__ : Any = self.letter_to_numbers(message[letter_index] ) A__ : Optional[Any] = numbers[0] A__ : List[str] = numbers[1] A__ : List[str] = first_step.reshape(2 * len(UpperCamelCase__ ) ) A__ : List[Any] = '''''' for numbers_index in range(len(UpperCamelCase__ ) ): A__ : Dict = int(second_step[numbers_index * 2] ) A__ : List[str] = int(second_step[(numbers_index * 2) + 1] ) A__ : Dict = self.numbers_to_letter(UpperCamelCase__ , UpperCamelCase__ ) A__ : Tuple = encoded_message + letter return encoded_message def __snake_case ( self , UpperCamelCase__ ): A__ : str = message.lower() message.replace(''' ''' , '''''' ) A__ : List[Any] = np.empty(2 * len(UpperCamelCase__ ) ) for letter_index in range(len(UpperCamelCase__ ) ): A__ : List[str] = self.letter_to_numbers(message[letter_index] ) A__ : Dict = numbers[0] A__ : int = numbers[1] A__ : Optional[Any] = first_step.reshape((2, len(UpperCamelCase__ )) ) A__ : int = '''''' for numbers_index in range(len(UpperCamelCase__ ) ): A__ : Tuple = int(second_step[0, numbers_index] ) A__ : Dict = int(second_step[1, numbers_index] ) A__ : List[str] = self.numbers_to_letter(UpperCamelCase__ , UpperCamelCase__ ) A__ : Tuple = decoded_message + letter return decoded_message
55
import json import os import tempfile from transformers.testing_utils import check_json_file_has_correct_format class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = None def __snake_case ( self ): A__ : Dict = self.feature_extraction_class(**self.feat_extract_dict ) A__ : Tuple = json.loads(feat_extract.to_json_string() ) for key, value in self.feat_extract_dict.items(): self.assertEqual(obj[key] , UpperCamelCase__ ) def __snake_case ( self ): A__ : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: A__ : Any = os.path.join(UpperCamelCase__ , '''feat_extract.json''' ) feat_extract_first.to_json_file(UpperCamelCase__ ) A__ : Dict = self.feature_extraction_class.from_json_file(UpperCamelCase__ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def __snake_case ( self ): A__ : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: A__ : Any = feat_extract_first.save_pretrained(UpperCamelCase__ )[0] check_json_file_has_correct_format(UpperCamelCase__ ) A__ : Optional[int] = self.feature_extraction_class.from_pretrained(UpperCamelCase__ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def __snake_case ( self ): A__ : str = self.feature_extraction_class() self.assertIsNotNone(UpperCamelCase__ )
55
1
import csv import tweepy # Twitter API credentials _SCREAMING_SNAKE_CASE : Tuple = '' _SCREAMING_SNAKE_CASE : Any = '' _SCREAMING_SNAKE_CASE : Tuple = '' _SCREAMING_SNAKE_CASE : Dict = '' def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> None: """simple docstring""" A__ : Union[str, Any] = tweepy.OAuthHandler(__UpperCamelCase , __UpperCamelCase ) auth.set_access_token(__UpperCamelCase , __UpperCamelCase ) A__ : Tuple = tweepy.API(__UpperCamelCase ) # initialize a list to hold all the tweepy Tweets A__ : List[Any] = [] # make initial request for most recent tweets (200 is the maximum allowed count) A__ : List[str] = api.user_timeline(screen_name=__UpperCamelCase , count=2_00 ) # save most recent tweets alltweets.extend(__UpperCamelCase ) # save the id of the oldest tweet less one A__ : Tuple = alltweets[-1].id - 1 # keep grabbing tweets until there are no tweets left to grab while len(__UpperCamelCase ) > 0: print(F"getting tweets before {oldest}" ) # all subsequent requests use the max_id param to prevent duplicates A__ : Union[str, Any] = api.user_timeline( screen_name=__UpperCamelCase , count=2_00 , max_id=__UpperCamelCase ) # save most recent tweets alltweets.extend(__UpperCamelCase ) # update the id of the oldest tweet less one A__ : Optional[int] = alltweets[-1].id - 1 print(F"...{len(__UpperCamelCase )} tweets downloaded so far" ) # transform the tweepy tweets into a 2D array that will populate the csv A__ : Any = [[tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets] # write the csv with open(F"new_{screen_name}_tweets.csv" , '''w''' ) as f: A__ : Optional[Any] = csv.writer(__UpperCamelCase ) writer.writerow(['''id''', '''created_at''', '''text'''] ) writer.writerows(__UpperCamelCase ) if __name__ == "__main__": # pass in the username of the account you want to download get_all_tweets('FirePing32')
55
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets _SCREAMING_SNAKE_CASE : Union[str, Any] = '\\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 : Tuple = '\\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 : Optional[Any] = '\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 UpperCamelCase__ ( datasets.Metric ): '''simple docstring''' def __snake_case ( self ): 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 , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = False , UpperCamelCase__ = False , UpperCamelCase__ = False , UpperCamelCase__ = False , ): A__ : List[Any] = len(references[0] ) if any(len(UpperCamelCase__ ) != references_per_prediction for refs in references ): raise ValueError('''Sacrebleu requires the same number of references for each prediction''' ) A__ : Dict = [[refs[i] for refs in references] for i in range(UpperCamelCase__ )] A__ : Optional[Any] = TER( normalized=UpperCamelCase__ , no_punct=UpperCamelCase__ , asian_support=UpperCamelCase__ , case_sensitive=UpperCamelCase__ , ) A__ : str = sb_ter.corpus_score(UpperCamelCase__ , UpperCamelCase__ ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
55
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _SCREAMING_SNAKE_CASE : Any = {'configuration_xglm': ['XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XGLMConfig']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : str = ['XGLMTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Dict = ['XGLMTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Optional[int] = [ 'XGLM_PRETRAINED_MODEL_ARCHIVE_LIST', 'XGLMForCausalLM', 'XGLMModel', 'XGLMPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Optional[Any] = [ 'FlaxXGLMForCausalLM', 'FlaxXGLMModel', 'FlaxXGLMPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : str = [ 'TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFXGLMForCausalLM', 'TFXGLMModel', 'TFXGLMPreTrainedModel', ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure)
55
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging _SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) # TODO Update this _SCREAMING_SNAKE_CASE : Optional[int] = { 'facebook/esm-1b': 'https://huggingface.co/facebook/esm-1b/resolve/main/config.json', # See all ESM models at https://huggingface.co/models?filter=esm } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "esm" def __init__( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=768 , UpperCamelCase__=12 , UpperCamelCase__=12 , UpperCamelCase__=3072 , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=1026 , UpperCamelCase__=0.0_2 , UpperCamelCase__=1e-12 , UpperCamelCase__="absolute" , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=False , UpperCamelCase__=False , UpperCamelCase__=None , UpperCamelCase__=None , **UpperCamelCase__ , ): super().__init__(pad_token_id=UpperCamelCase__ , mask_token_id=UpperCamelCase__ , **UpperCamelCase__ ) A__ : Optional[Any] = vocab_size A__ : int = hidden_size A__ : List[str] = num_hidden_layers A__ : Tuple = num_attention_heads A__ : str = intermediate_size A__ : List[str] = hidden_dropout_prob A__ : Optional[Any] = attention_probs_dropout_prob A__ : int = max_position_embeddings A__ : List[str] = initializer_range A__ : List[Any] = layer_norm_eps A__ : int = position_embedding_type A__ : Optional[Any] = use_cache A__ : Optional[int] = emb_layer_norm_before A__ : List[str] = token_dropout A__ : Tuple = is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) A__ : List[Any] = EsmFoldConfig() elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Optional[int] = EsmFoldConfig(**UpperCamelCase__ ) A__ : int = esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) A__ : Any = get_default_vocab_list() else: A__ : Dict = vocab_list else: A__ : Optional[Any] = None A__ : Tuple = None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCamelCase__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __snake_case ( self ): A__ : Optional[int] = super().to_dict() if isinstance(self.esmfold_config , UpperCamelCase__ ): A__ : Dict = self.esmfold_config.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = None _lowerCAmelCase = True _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = 0 _lowerCAmelCase = True _lowerCAmelCase = False _lowerCAmelCase = 128 _lowerCAmelCase = None def __snake_case ( self ): if self.trunk is None: A__ : Tuple = TrunkConfig() elif isinstance(self.trunk , UpperCamelCase__ ): A__ : List[Any] = TrunkConfig(**self.trunk ) def __snake_case ( self ): A__ : Optional[int] = asdict(self ) A__ : int = self.trunk.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = 48 _lowerCAmelCase = 1_024 _lowerCAmelCase = 128 _lowerCAmelCase = 32 _lowerCAmelCase = 32 _lowerCAmelCase = 32 _lowerCAmelCase = 0 _lowerCAmelCase = 0 _lowerCAmelCase = False _lowerCAmelCase = 4 _lowerCAmelCase = 128 _lowerCAmelCase = None def __snake_case ( self ): if self.structure_module is None: A__ : str = StructureModuleConfig() elif isinstance(self.structure_module , UpperCamelCase__ ): A__ : str = StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(F"`max_recycles` should be positive, got {self.max_recycles}." ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' F" {self.sequence_state_dim} and {self.sequence_state_dim}." ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' F" {self.pairwise_state_dim} and {self.pairwise_state_dim}." ) A__ : Tuple = self.sequence_state_dim // self.sequence_head_width A__ : int = self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' F" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}." ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' F" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}." ) if self.pairwise_state_dim % 2 != 0: raise ValueError(F"`pairwise_state_dim` should be even, got {self.pairwise_state_dim}." ) if self.dropout >= 0.4: raise ValueError(F"`dropout` should not be greater than 0.4, got {self.dropout}." ) def __snake_case ( self ): A__ : List[Any] = asdict(self ) A__ : Optional[int] = self.structure_module.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = 384 _lowerCAmelCase = 128 _lowerCAmelCase = 16 _lowerCAmelCase = 128 _lowerCAmelCase = 12 _lowerCAmelCase = 4 _lowerCAmelCase = 8 _lowerCAmelCase = 0.1 _lowerCAmelCase = 8 _lowerCAmelCase = 1 _lowerCAmelCase = 2 _lowerCAmelCase = 7 _lowerCAmelCase = 10 _lowerCAmelCase = 1e-8 _lowerCAmelCase = 1e5 def __snake_case ( self ): return asdict(self ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: """simple docstring""" return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
55
1
import importlib import os from dataclasses import dataclass from enum import Enum from typing import Any, Dict, Optional, Union import torch from ..utils import BaseOutput _SCREAMING_SNAKE_CASE : Dict = 'scheduler_config.json' class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = 1 _lowerCAmelCase = 2 _lowerCAmelCase = 3 _lowerCAmelCase = 4 _lowerCAmelCase = 5 _lowerCAmelCase = 6 _lowerCAmelCase = 7 _lowerCAmelCase = 8 _lowerCAmelCase = 9 _lowerCAmelCase = 10 _lowerCAmelCase = 11 _lowerCAmelCase = 12 _lowerCAmelCase = 13 _lowerCAmelCase = 14 @dataclass class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = 42 class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = SCHEDULER_CONFIG_NAME _lowerCAmelCase = [] _lowerCAmelCase = True @classmethod def __snake_case ( cls , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__=False , **UpperCamelCase__ , ): A__ , A__ , A__ : List[Any] = cls.load_config( pretrained_model_name_or_path=UpperCamelCase__ , subfolder=UpperCamelCase__ , return_unused_kwargs=UpperCamelCase__ , return_commit_hash=UpperCamelCase__ , **UpperCamelCase__ , ) return cls.from_config(UpperCamelCase__ , return_unused_kwargs=UpperCamelCase__ , **UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ = False , **UpperCamelCase__ ): self.save_config(save_directory=UpperCamelCase__ , push_to_hub=UpperCamelCase__ , **UpperCamelCase__ ) @property def __snake_case ( self ): return self._get_compatibles() @classmethod def __snake_case ( cls ): A__ : Optional[int] = list(set([cls.__name__] + cls._compatibles ) ) A__ : List[Any] = importlib.import_module(__name__.split('''.''' )[0] ) A__ : Tuple = [ getattr(UpperCamelCase__ , UpperCamelCase__ ) for c in compatible_classes_str if hasattr(UpperCamelCase__ , UpperCamelCase__ ) ] return compatible_classes
55
import logging import torch from accelerate import Accelerator from arguments import EvaluationArguments from datasets import load_dataset from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, set_seed class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=1024 , UpperCamelCase__=1024 , UpperCamelCase__=3.6 ): A__ : str = tokenizer A__ : int = tokenizer.bos_token_id A__ : List[Any] = dataset A__ : Tuple = seq_length A__ : Any = seq_length * chars_per_token * num_of_sequences def __iter__( self ): A__ : Dict = iter(self.dataset ) A__ : Tuple = True while more_examples: A__ , A__ : Optional[Any] = [], 0 while True: if buffer_len >= self.input_characters: break try: buffer.append(next(UpperCamelCase__ )['''content'''] ) buffer_len += len(buffer[-1] ) except StopIteration: A__ : Dict = False break A__ : str = tokenizer(UpperCamelCase__ , truncation=UpperCamelCase__ )['''input_ids'''] A__ : Optional[int] = [] for tokenized_input in tokenized_inputs: all_token_ids.extend(tokenized_input + [self.concat_token_id] ) for i in range(0 , len(UpperCamelCase__ ) , self.seq_length ): A__ : Optional[int] = all_token_ids[i : i + self.seq_length] if len(UpperCamelCase__ ) == self.seq_length: yield torch.tensor(UpperCamelCase__ ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> Any: """simple docstring""" A__ : Any = {'''streaming''': True} A__ : List[str] = load_dataset(args.dataset_name , split='''train''' , **__UpperCamelCase ) A__ : List[str] = ConstantLengthDataset(__UpperCamelCase , __UpperCamelCase , seq_length=args.seq_length ) A__ : int = DataLoader(__UpperCamelCase , batch_size=args.batch_size ) return eval_dataloader def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] ) -> Dict: """simple docstring""" model.eval() A__ : Dict = [] for step, batch in enumerate(__UpperCamelCase ): with torch.no_grad(): A__ : Any = model(__UpperCamelCase , labels=__UpperCamelCase ) A__ : Tuple = outputs.loss.repeat(args.batch_size ) losses.append(accelerator.gather(__UpperCamelCase ) ) if args.max_eval_steps > 0 and step >= args.max_eval_steps: break A__ : Tuple = torch.mean(torch.cat(__UpperCamelCase ) ) try: A__ : Optional[Any] = torch.exp(__UpperCamelCase ) except OverflowError: A__ : Union[str, Any] = float('''inf''' ) return loss.item(), perplexity.item() # Setup Accelerator _SCREAMING_SNAKE_CASE : List[Any] = Accelerator() # Parse configuration _SCREAMING_SNAKE_CASE : Optional[int] = HfArgumentParser(EvaluationArguments) _SCREAMING_SNAKE_CASE : Union[str, Any] = parser.parse_args() set_seed(args.seed) # Logging _SCREAMING_SNAKE_CASE : Dict = logging.getLogger(__name__) logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO ) # Load model and tokenizer _SCREAMING_SNAKE_CASE : Optional[int] = AutoModelForCausalLM.from_pretrained(args.model_ckpt) _SCREAMING_SNAKE_CASE : List[str] = AutoTokenizer.from_pretrained(args.model_ckpt) # Load dataset and dataloader _SCREAMING_SNAKE_CASE : Optional[Any] = create_dataloader(args) # Prepare everything with our `accelerator`. _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Optional[Any] = accelerator.prepare(model, eval_dataloader) # Evaluate and save the last checkpoint logger.info('Evaluating and saving model after training') _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Optional[int] = evaluate(args) logger.info(f"""loss/eval: {eval_loss}, perplexity: {perplexity}""")
55
1
from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef import datasets _SCREAMING_SNAKE_CASE : List[Any] = '\\n@inproceedings{wang2019glue,\n title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding},\n author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.},\n note={In the Proceedings of ICLR.},\n year={2019}\n}\n' _SCREAMING_SNAKE_CASE : List[str] = '\\nGLUE, the General Language Understanding Evaluation benchmark\n(https://gluebenchmark.com/) is a collection of resources for training,\nevaluating, and analyzing natural language understanding systems.\n' _SCREAMING_SNAKE_CASE : str = '\nCompute GLUE evaluation metric associated to each GLUE dataset.\nArgs:\n predictions: list of predictions to score.\n Each translation should be tokenized into a list of tokens.\n references: list of lists of references for each translation.\n Each reference should be tokenized into a list of tokens.\nReturns: depending on the GLUE subset, one or several of:\n "accuracy": Accuracy\n "f1": F1 score\n "pearson": Pearson Correlation\n "spearmanr": Spearman Correlation\n "matthews_correlation": Matthew Correlation\nExamples:\n\n >>> glue_metric = datasets.load_metric(\'glue\', \'sst2\') # \'sst2\' or any of ["mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0}\n\n >>> glue_metric = datasets.load_metric(\'glue\', \'mrpc\') # \'mrpc\' or \'qqp\'\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0, \'f1\': 1.0}\n\n >>> glue_metric = datasets.load_metric(\'glue\', \'stsb\')\n >>> references = [0., 1., 2., 3., 4., 5.]\n >>> predictions = [0., 1., 2., 3., 4., 5.]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print({"pearson": round(results["pearson"], 2), "spearmanr": round(results["spearmanr"], 2)})\n {\'pearson\': 1.0, \'spearmanr\': 1.0}\n\n >>> glue_metric = datasets.load_metric(\'glue\', \'cola\')\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'matthews_correlation\': 1.0}\n' def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] , __UpperCamelCase : Optional[Any] ) -> Union[str, Any]: """simple docstring""" return float((preds == labels).mean() ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple , __UpperCamelCase : int ) -> Union[str, Any]: """simple docstring""" A__ : Optional[int] = simple_accuracy(__UpperCamelCase , __UpperCamelCase ) A__ : Optional[Any] = float(fa_score(y_true=__UpperCamelCase , y_pred=__UpperCamelCase ) ) return { "accuracy": acc, "f1": fa, } def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str , __UpperCamelCase : Tuple ) -> int: """simple docstring""" A__ : List[str] = float(pearsonr(__UpperCamelCase , __UpperCamelCase )[0] ) A__ : Optional[int] = float(spearmanr(__UpperCamelCase , __UpperCamelCase )[0] ) return { "pearson": pearson_corr, "spearmanr": spearman_corr, } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class UpperCamelCase__ ( datasets.Metric ): '''simple docstring''' def __snake_case ( self ): if self.config_name not in [ "sst2", "mnli", "mnli_mismatched", "mnli_matched", "cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans", ]: raise KeyError( '''You should supply a configuration name selected in ''' '''["sst2", "mnli", "mnli_mismatched", "mnli_matched", ''' '''"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]''' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''int64''' if self.config_name != '''stsb''' else '''float32''' ), '''references''': datasets.Value('''int64''' if self.config_name != '''stsb''' else '''float32''' ), } ) , codebase_urls=[] , reference_urls=[] , format='''numpy''' , ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ ): if self.config_name == "cola": return {"matthews_correlation": matthews_corrcoef(UpperCamelCase__ , UpperCamelCase__ )} elif self.config_name == "stsb": return pearson_and_spearman(UpperCamelCase__ , UpperCamelCase__ ) elif self.config_name in ["mrpc", "qqp"]: return acc_and_fa(UpperCamelCase__ , UpperCamelCase__ ) elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]: return {"accuracy": simple_accuracy(UpperCamelCase__ , UpperCamelCase__ )} else: raise KeyError( '''You should supply a configuration name selected in ''' '''["sst2", "mnli", "mnli_mismatched", "mnli_matched", ''' '''"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]''' )
55
def SCREAMING_SNAKE_CASE ( ) -> Optional[int]: """simple docstring""" A__ : Optional[Any] = 0 for i in range(1 , 10_01 ): total += i**i return str(__UpperCamelCase )[-10:] if __name__ == "__main__": print(solution())
55
1
import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartTokenizer, MBartTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin _SCREAMING_SNAKE_CASE : str = get_tests_dir('fixtures/test_sentencepiece.model') if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right _SCREAMING_SNAKE_CASE : Optional[Any] = 2_5_0_0_0_4 _SCREAMING_SNAKE_CASE : Optional[Any] = 2_5_0_0_2_0 @require_sentencepiece @require_tokenizers class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = MBartTokenizer _lowerCAmelCase = MBartTokenizerFast _lowerCAmelCase = True _lowerCAmelCase = True def __snake_case ( self ): super().setUp() # We have a SentencePiece fixture for testing A__ : str = MBartTokenizer(UpperCamelCase__ , keep_accents=UpperCamelCase__ ) tokenizer.save_pretrained(self.tmpdirname ) def __snake_case ( self ): A__ : List[str] = MBartTokenizer(UpperCamelCase__ , keep_accents=UpperCamelCase__ ) A__ : Optional[Any] = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(UpperCamelCase__ , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(UpperCamelCase__ ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) A__ : List[Any] = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( UpperCamelCase__ , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) A__ : Optional[int] = tokenizer.convert_tokens_to_ids(UpperCamelCase__ ) self.assertListEqual( UpperCamelCase__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] , ) A__ : Optional[Any] = tokenizer.convert_ids_to_tokens(UpperCamelCase__ ) self.assertListEqual( UpperCamelCase__ , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) def __snake_case ( self ): if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return A__ : Optional[int] = (self.rust_tokenizer_class, '''hf-internal-testing/tiny-random-mbart''', {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): A__ : Tuple = self.rust_tokenizer_class.from_pretrained(UpperCamelCase__ , **UpperCamelCase__ ) A__ : Tuple = self.tokenizer_class.from_pretrained(UpperCamelCase__ , **UpperCamelCase__ ) A__ : int = tempfile.mkdtemp() A__ : List[str] = tokenizer_r.save_pretrained(UpperCamelCase__ ) A__ : str = tokenizer_p.save_pretrained(UpperCamelCase__ ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) A__ : List[Any] = tuple(f for f in tokenizer_r_files if '''tokenizer.json''' not in f ) self.assertSequenceEqual(UpperCamelCase__ , UpperCamelCase__ ) # Checks everything loads correctly in the same way A__ : Optional[Any] = tokenizer_r.from_pretrained(UpperCamelCase__ ) A__ : Dict = tokenizer_p.from_pretrained(UpperCamelCase__ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(UpperCamelCase__ , UpperCamelCase__ ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(UpperCamelCase__ ) # Save tokenizer rust, legacy_format=True A__ : List[str] = tempfile.mkdtemp() A__ : List[Any] = tokenizer_r.save_pretrained(UpperCamelCase__ , legacy_format=UpperCamelCase__ ) A__ : Any = tokenizer_p.save_pretrained(UpperCamelCase__ ) # Checks it save with the same files self.assertSequenceEqual(UpperCamelCase__ , UpperCamelCase__ ) # Checks everything loads correctly in the same way A__ : str = tokenizer_r.from_pretrained(UpperCamelCase__ ) A__ : Tuple = tokenizer_p.from_pretrained(UpperCamelCase__ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(UpperCamelCase__ , UpperCamelCase__ ) ) shutil.rmtree(UpperCamelCase__ ) # Save tokenizer rust, legacy_format=False A__ : Optional[Any] = tempfile.mkdtemp() A__ : Dict = tokenizer_r.save_pretrained(UpperCamelCase__ , legacy_format=UpperCamelCase__ ) A__ : List[str] = tokenizer_p.save_pretrained(UpperCamelCase__ ) # Checks it saved the tokenizer.json file self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way A__ : List[Any] = tokenizer_r.from_pretrained(UpperCamelCase__ ) A__ : List[Any] = tokenizer_p.from_pretrained(UpperCamelCase__ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(UpperCamelCase__ , UpperCamelCase__ ) ) shutil.rmtree(UpperCamelCase__ ) @require_torch @require_sentencepiece @require_tokenizers class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = "facebook/mbart-large-en-ro" _lowerCAmelCase = [ " UN Chief Says There Is No Military Solution in Syria", " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.", ] _lowerCAmelCase = [ "Şeful ONU declară că nu există o soluţie militară în Siria", "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei" " pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor" " face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.", ] _lowerCAmelCase = [8_274, 127_873, 25_916, 7, 8_622, 2_071, 438, 67_485, 53, 187_895, 23, 51_712, 2, EN_CODE] @classmethod def __snake_case ( cls ): A__ : MBartTokenizer = MBartTokenizer.from_pretrained( cls.checkpoint_name , src_lang='''en_XX''' , tgt_lang='''ro_RO''' ) A__ : List[Any] = 1 return cls def __snake_case ( self ): self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''ar_AR'''] , 25_0001 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''en_EN'''] , 25_0004 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''ro_RO'''] , 25_0020 ) def __snake_case ( self ): A__ : Any = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , UpperCamelCase__ ) def __snake_case ( self ): self.assertIn(UpperCamelCase__ , self.tokenizer.all_special_ids ) A__ : List[str] = [RO_CODE, 884, 9019, 96, 9, 916, 8_6792, 36, 1_8743, 1_5596, 5, 2] A__ : Union[str, Any] = self.tokenizer.decode(UpperCamelCase__ , skip_special_tokens=UpperCamelCase__ ) A__ : Tuple = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=UpperCamelCase__ ) self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) self.assertNotIn(self.tokenizer.eos_token , UpperCamelCase__ ) def __snake_case ( self ): A__ : Tuple = ['''this is gunna be a long sentence ''' * 20] assert isinstance(src_text[0] , UpperCamelCase__ ) A__ : List[str] = 10 A__ : Optional[Any] = self.tokenizer(UpperCamelCase__ , max_length=UpperCamelCase__ , truncation=UpperCamelCase__ ).input_ids[0] self.assertEqual(ids[-2] , 2 ) self.assertEqual(ids[-1] , UpperCamelCase__ ) self.assertEqual(len(UpperCamelCase__ ) , UpperCamelCase__ ) def __snake_case ( self ): self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['''<mask>''', '''ar_AR'''] ) , [25_0026, 25_0001] ) def __snake_case ( self ): A__ : Optional[int] = tempfile.mkdtemp() A__ : Any = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(UpperCamelCase__ ) A__ : Any = MBartTokenizer.from_pretrained(UpperCamelCase__ ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , UpperCamelCase__ ) @require_torch def __snake_case ( self ): A__ : Tuple = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=UpperCamelCase__ , return_tensors='''pt''' ) A__ : Any = shift_tokens_right(batch['''labels'''] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][-2:].tolist() == [2, EN_CODE] assert batch.decoder_input_ids[1][0].tolist() == RO_CODE assert batch.decoder_input_ids[1][-1] == 2 assert batch.labels[1][-2:].tolist() == [2, RO_CODE] @require_torch def __snake_case ( self ): A__ : int = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , max_length=len(self.expected_src_tokens ) , return_tensors='''pt''' , ) A__ : List[str] = shift_tokens_right(batch['''labels'''] , self.tokenizer.pad_token_id ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) self.assertEqual((2, 14) , batch.input_ids.shape ) self.assertEqual((2, 14) , batch.attention_mask.shape ) A__ : Optional[Any] = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , UpperCamelCase__ ) self.assertEqual(2 , batch.decoder_input_ids[0, -1] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id, EN_CODE] ) def __snake_case ( self ): A__ : Union[str, Any] = self.tokenizer(self.src_text , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , max_length=3 , return_tensors='''pt''' ) A__ : Tuple = self.tokenizer( text_target=self.tgt_text , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , max_length=10 , return_tensors='''pt''' ) A__ : Union[str, Any] = targets['''input_ids'''] A__ : Any = shift_tokens_right(UpperCamelCase__ , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def __snake_case ( self ): A__ : Optional[int] = self.tokenizer._build_translation_inputs( '''A test''' , return_tensors='''pt''' , src_lang='''en_XX''' , tgt_lang='''ar_AR''' ) self.assertEqual( nested_simplify(UpperCamelCase__ ) , { # A, test, EOS, en_XX '''input_ids''': [[62, 3034, 2, 25_0004]], '''attention_mask''': [[1, 1, 1, 1]], # ar_AR '''forced_bos_token_id''': 25_0001, } , )
55
import inspect import os import unittest import torch import accelerate from accelerate import debug_launcher from accelerate.test_utils import ( execute_subprocess_async, require_cpu, require_huggingface_suite, require_multi_gpu, require_single_gpu, ) from accelerate.utils import patch_environment @require_huggingface_suite class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): A__ : Dict = inspect.getfile(accelerate.test_utils ) A__ : Any = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''external_deps''', '''test_metrics.py'''] ) from accelerate.test_utils.scripts.external_deps import test_metrics # noqa: F401 A__ : Tuple = test_metrics @require_cpu def __snake_case ( self ): debug_launcher(self.test_metrics.main , num_processes=1 ) @require_cpu def __snake_case ( self ): debug_launcher(self.test_metrics.main ) @require_single_gpu def __snake_case ( self ): self.test_metrics.main() @require_multi_gpu def __snake_case ( self ): print(F"Found {torch.cuda.device_count()} devices." ) A__ : int = ['''torchrun''', F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(UpperCamelCase__ , env=os.environ.copy() )
55
1
import inspect import unittest from transformers import MobileNetVaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MobileNetVaForImageClassification, MobileNetVaModel from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def __snake_case ( self ): A__ : Union[str, Any] = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(UpperCamelCase__ , '''tf_padding''' ) ) self.parent.assertTrue(hasattr(UpperCamelCase__ , '''depth_multiplier''' ) ) class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__=13 , UpperCamelCase__=3 , UpperCamelCase__=32 , UpperCamelCase__=0.2_5 , UpperCamelCase__=8 , UpperCamelCase__=True , UpperCamelCase__=1024 , UpperCamelCase__=32 , UpperCamelCase__="relu6" , UpperCamelCase__=0.1 , UpperCamelCase__=0.0_2 , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=10 , UpperCamelCase__=None , ): A__ : Any = parent A__ : Dict = batch_size A__ : Union[str, Any] = num_channels A__ : Any = image_size A__ : Optional[int] = depth_multiplier A__ : List[str] = min_depth A__ : Union[str, Any] = tf_padding A__ : Dict = int(last_hidden_size * depth_multiplier ) A__ : int = output_stride A__ : List[Any] = hidden_act A__ : Dict = classifier_dropout_prob A__ : List[Any] = use_labels A__ : int = is_training A__ : Tuple = num_labels A__ : Tuple = initializer_range A__ : int = scope def __snake_case ( self ): A__ : int = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ : str = None A__ : Union[str, Any] = None if self.use_labels: A__ : Optional[int] = ids_tensor([self.batch_size] , self.num_labels ) A__ : Optional[Any] = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) A__ : str = self.get_config() return config, pixel_values, labels, pixel_labels def __snake_case ( self ): return MobileNetVaConfig( num_channels=self.num_channels , image_size=self.image_size , depth_multiplier=self.depth_multiplier , min_depth=self.min_depth , tf_padding=self.tf_padding , hidden_act=self.hidden_act , classifier_dropout_prob=self.classifier_dropout_prob , initializer_range=self.initializer_range , ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : Dict = MobileNetVaModel(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : int = model(UpperCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , ( self.batch_size, self.last_hidden_size, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : str = self.num_labels A__ : int = MobileNetVaForImageClassification(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : Union[str, Any] = model(UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __snake_case ( self ): A__ : List[str] = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ : Any = config_and_inputs A__ : Tuple = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else () _lowerCAmelCase = ( {"feature-extraction": MobileNetVaModel, "image-classification": MobileNetVaForImageClassification} if is_torch_available() else {} ) _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False def __snake_case ( self ): A__ : List[Any] = MobileNetVaModelTester(self ) A__ : Dict = MobileNetVaConfigTester(self , config_class=UpperCamelCase__ , has_text_modality=UpperCamelCase__ ) def __snake_case ( self ): self.config_tester.run_common_tests() @unittest.skip(reason='''MobileNetV1 does not use inputs_embeds''' ) def __snake_case ( self ): pass @unittest.skip(reason='''MobileNetV1 does not support input and output embeddings''' ) def __snake_case ( self ): pass @unittest.skip(reason='''MobileNetV1 does not output attentions''' ) def __snake_case ( self ): pass def __snake_case ( self ): A__ , A__ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : Optional[Any] = model_class(UpperCamelCase__ ) A__ : Optional[int] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ : Any = [*signature.parameters.keys()] A__ : Tuple = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , UpperCamelCase__ ) def __snake_case ( self ): A__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase__ ) def __snake_case ( self ): def check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : List[str] = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : List[str] = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Union[str, Any] = outputs.hidden_states A__ : Optional[int] = 26 self.assertEqual(len(UpperCamelCase__ ) , UpperCamelCase__ ) A__ , A__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : List[str] = True check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ : Union[str, Any] = True check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): A__ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__ ) @slow def __snake_case ( self ): for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ : Optional[int] = MobileNetVaModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) def SCREAMING_SNAKE_CASE ( ) -> Tuple: """simple docstring""" A__ : Union[str, Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' @cached_property def __snake_case ( self ): return ( MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v1_1.0_224''' ) if is_vision_available() else None ) @slow def __snake_case ( self ): A__ : Optional[int] = MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v1_1.0_224''' ).to(UpperCamelCase__ ) A__ : Optional[int] = self.default_image_processor A__ : Optional[int] = prepare_img() A__ : int = image_processor(images=UpperCamelCase__ , return_tensors='''pt''' ).to(UpperCamelCase__ ) # forward pass with torch.no_grad(): A__ : List[str] = model(**UpperCamelCase__ ) # verify the logits A__ : Union[str, Any] = torch.Size((1, 1001) ) self.assertEqual(outputs.logits.shape , UpperCamelCase__ ) A__ : List[str] = torch.tensor([-4.1_7_3_9, -1.1_2_3_3, 3.1_2_0_5] ).to(UpperCamelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , UpperCamelCase__ , atol=1e-4 ) )
55
from numpy import exp, pi, sqrt def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict , __UpperCamelCase : float = 0.0 , __UpperCamelCase : float = 1.0 ) -> int: """simple docstring""" return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
55
1
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> bool: """simple docstring""" if not isinstance(__UpperCamelCase , __UpperCamelCase ): A__ : Dict = F"Input value of [number={number}] must be an integer" raise TypeError(__UpperCamelCase ) if number < 0: return False A__ : List[str] = number * number while number > 0: if number % 10 != number_square % 10: return False number //= 10 number_square //= 10 return True if __name__ == "__main__": import doctest doctest.testmod()
55
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tensorflow_text_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _SCREAMING_SNAKE_CASE : int = { 'configuration_bert': ['BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BertConfig', 'BertOnnxConfig'], 'tokenization_bert': ['BasicTokenizer', 'BertTokenizer', 'WordpieceTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Optional[Any] = ['BertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Union[str, Any] = [ 'BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BertForMaskedLM', 'BertForMultipleChoice', 'BertForNextSentencePrediction', 'BertForPreTraining', 'BertForQuestionAnswering', 'BertForSequenceClassification', 'BertForTokenClassification', 'BertLayer', 'BertLMHeadModel', 'BertModel', 'BertPreTrainedModel', 'load_tf_weights_in_bert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Tuple = [ 'TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFBertEmbeddings', 'TFBertForMaskedLM', 'TFBertForMultipleChoice', 'TFBertForNextSentencePrediction', 'TFBertForPreTraining', 'TFBertForQuestionAnswering', 'TFBertForSequenceClassification', 'TFBertForTokenClassification', 'TFBertLMHeadModel', 'TFBertMainLayer', 'TFBertModel', 'TFBertPreTrainedModel', ] try: if not is_tensorflow_text_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Dict = ['TFBertTokenizer'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Optional[int] = [ 'FlaxBertForCausalLM', 'FlaxBertForMaskedLM', 'FlaxBertForMultipleChoice', 'FlaxBertForNextSentencePrediction', 'FlaxBertForPreTraining', 'FlaxBertForQuestionAnswering', 'FlaxBertForSequenceClassification', 'FlaxBertForTokenClassification', 'FlaxBertModel', 'FlaxBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig, BertOnnxConfig from .tokenization_bert import BasicTokenizer, BertTokenizer, WordpieceTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bert_fast import BertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bert import ( BERT_PRETRAINED_MODEL_ARCHIVE_LIST, BertForMaskedLM, BertForMultipleChoice, BertForNextSentencePrediction, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, BertForTokenClassification, BertLayer, BertLMHeadModel, BertModel, BertPreTrainedModel, load_tf_weights_in_bert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_bert import ( TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFBertEmbeddings, TFBertForMaskedLM, TFBertForMultipleChoice, TFBertForNextSentencePrediction, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertForTokenClassification, TFBertLMHeadModel, TFBertMainLayer, TFBertModel, TFBertPreTrainedModel, ) try: if not is_tensorflow_text_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bert_tf import TFBertTokenizer try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_bert import ( FlaxBertForCausalLM, FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForNextSentencePrediction, FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertModel, FlaxBertPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE : Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
55
1
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging _SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) # TODO Update this _SCREAMING_SNAKE_CASE : Optional[int] = { 'facebook/esm-1b': 'https://huggingface.co/facebook/esm-1b/resolve/main/config.json', # See all ESM models at https://huggingface.co/models?filter=esm } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "esm" def __init__( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=768 , UpperCamelCase__=12 , UpperCamelCase__=12 , UpperCamelCase__=3072 , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=1026 , UpperCamelCase__=0.0_2 , UpperCamelCase__=1e-12 , UpperCamelCase__="absolute" , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=False , UpperCamelCase__=False , UpperCamelCase__=None , UpperCamelCase__=None , **UpperCamelCase__ , ): super().__init__(pad_token_id=UpperCamelCase__ , mask_token_id=UpperCamelCase__ , **UpperCamelCase__ ) A__ : Optional[Any] = vocab_size A__ : int = hidden_size A__ : List[str] = num_hidden_layers A__ : Tuple = num_attention_heads A__ : str = intermediate_size A__ : List[str] = hidden_dropout_prob A__ : Optional[Any] = attention_probs_dropout_prob A__ : int = max_position_embeddings A__ : List[str] = initializer_range A__ : List[Any] = layer_norm_eps A__ : int = position_embedding_type A__ : Optional[Any] = use_cache A__ : Optional[int] = emb_layer_norm_before A__ : List[str] = token_dropout A__ : Tuple = is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) A__ : List[Any] = EsmFoldConfig() elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Optional[int] = EsmFoldConfig(**UpperCamelCase__ ) A__ : int = esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) A__ : Any = get_default_vocab_list() else: A__ : Dict = vocab_list else: A__ : Optional[Any] = None A__ : Tuple = None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCamelCase__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __snake_case ( self ): A__ : Optional[int] = super().to_dict() if isinstance(self.esmfold_config , UpperCamelCase__ ): A__ : Dict = self.esmfold_config.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = None _lowerCAmelCase = True _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = 0 _lowerCAmelCase = True _lowerCAmelCase = False _lowerCAmelCase = 128 _lowerCAmelCase = None def __snake_case ( self ): if self.trunk is None: A__ : Tuple = TrunkConfig() elif isinstance(self.trunk , UpperCamelCase__ ): A__ : List[Any] = TrunkConfig(**self.trunk ) def __snake_case ( self ): A__ : Optional[int] = asdict(self ) A__ : int = self.trunk.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = 48 _lowerCAmelCase = 1_024 _lowerCAmelCase = 128 _lowerCAmelCase = 32 _lowerCAmelCase = 32 _lowerCAmelCase = 32 _lowerCAmelCase = 0 _lowerCAmelCase = 0 _lowerCAmelCase = False _lowerCAmelCase = 4 _lowerCAmelCase = 128 _lowerCAmelCase = None def __snake_case ( self ): if self.structure_module is None: A__ : str = StructureModuleConfig() elif isinstance(self.structure_module , UpperCamelCase__ ): A__ : str = StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(F"`max_recycles` should be positive, got {self.max_recycles}." ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' F" {self.sequence_state_dim} and {self.sequence_state_dim}." ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' F" {self.pairwise_state_dim} and {self.pairwise_state_dim}." ) A__ : Tuple = self.sequence_state_dim // self.sequence_head_width A__ : int = self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' F" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}." ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' F" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}." ) if self.pairwise_state_dim % 2 != 0: raise ValueError(F"`pairwise_state_dim` should be even, got {self.pairwise_state_dim}." ) if self.dropout >= 0.4: raise ValueError(F"`dropout` should not be greater than 0.4, got {self.dropout}." ) def __snake_case ( self ): A__ : List[Any] = asdict(self ) A__ : Optional[int] = self.structure_module.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = 384 _lowerCAmelCase = 128 _lowerCAmelCase = 16 _lowerCAmelCase = 128 _lowerCAmelCase = 12 _lowerCAmelCase = 4 _lowerCAmelCase = 8 _lowerCAmelCase = 0.1 _lowerCAmelCase = 8 _lowerCAmelCase = 1 _lowerCAmelCase = 2 _lowerCAmelCase = 7 _lowerCAmelCase = 10 _lowerCAmelCase = 1e-8 _lowerCAmelCase = 1e5 def __snake_case ( self ): return asdict(self ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: """simple docstring""" return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
55
import json import os import sys import tempfile import unittest from pathlib import Path from shutil import copyfile from huggingface_hub import HfFolder, Repository, create_repo, delete_repo from requests.exceptions import HTTPError import transformers from transformers import ( CONFIG_MAPPING, FEATURE_EXTRACTOR_MAPPING, PROCESSOR_MAPPING, TOKENIZER_MAPPING, AutoConfig, AutoFeatureExtractor, AutoProcessor, AutoTokenizer, BertTokenizer, ProcessorMixin, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaProcessor, ) from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test from transformers.tokenization_utils import TOKENIZER_CONFIG_FILE from transformers.utils import FEATURE_EXTRACTOR_NAME, is_tokenizers_available sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 from test_module.custom_processing import CustomProcessor # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 _SCREAMING_SNAKE_CASE : List[Any] = get_tests_dir('fixtures/dummy_feature_extractor_config.json') _SCREAMING_SNAKE_CASE : int = get_tests_dir('fixtures/vocab.json') _SCREAMING_SNAKE_CASE : Tuple = get_tests_dir('fixtures') class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"] def __snake_case ( self ): A__ : List[Any] = 0 def __snake_case ( self ): A__ : Dict = AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h''' ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Optional[Any] = WavaVecaConfig() A__ : Dict = AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h''' ) # save in new folder model_config.save_pretrained(UpperCamelCase__ ) processor.save_pretrained(UpperCamelCase__ ) A__ : Any = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: # copy relevant files copyfile(UpperCamelCase__ , os.path.join(UpperCamelCase__ , UpperCamelCase__ ) ) copyfile(UpperCamelCase__ , os.path.join(UpperCamelCase__ , '''vocab.json''' ) ) A__ : List[Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Dict = WavaVecaFeatureExtractor() A__ : Union[str, Any] = AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h''' ) A__ : Optional[int] = WavaVecaProcessor(UpperCamelCase__ , UpperCamelCase__ ) # save in new folder processor.save_pretrained(UpperCamelCase__ ) # drop `processor_class` in tokenizer with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''r''' ) as f: A__ : str = json.load(UpperCamelCase__ ) config_dict.pop('''processor_class''' ) with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''w''' ) as f: f.write(json.dumps(UpperCamelCase__ ) ) A__ : Optional[int] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Optional[int] = WavaVecaFeatureExtractor() A__ : List[Any] = AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h''' ) A__ : str = WavaVecaProcessor(UpperCamelCase__ , UpperCamelCase__ ) # save in new folder processor.save_pretrained(UpperCamelCase__ ) # drop `processor_class` in feature extractor with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''r''' ) as f: A__ : List[Any] = json.load(UpperCamelCase__ ) config_dict.pop('''processor_class''' ) with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''w''' ) as f: f.write(json.dumps(UpperCamelCase__ ) ) A__ : List[Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Any = WavaVecaConfig(processor_class='''Wav2Vec2Processor''' ) model_config.save_pretrained(UpperCamelCase__ ) # copy relevant files copyfile(UpperCamelCase__ , os.path.join(UpperCamelCase__ , '''vocab.json''' ) ) # create emtpy sample processor with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''w''' ) as f: f.write('''{}''' ) A__ : Union[str, Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(UpperCamelCase__ ): A__ : Union[str, Any] = AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(UpperCamelCase__ ): A__ : str = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) A__ : int = AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) self.assertTrue(processor.special_attribute_present ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) A__ : List[Any] = processor.feature_extractor self.assertTrue(feature_extractor.special_attribute_present ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) A__ : List[Any] = processor.tokenizer self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) # Test we can also load the slow version A__ : Dict = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ , use_fast=UpperCamelCase__ ) A__ : int = new_processor.tokenizer self.assertTrue(new_tokenizer.special_attribute_present ) self.assertEqual(new_tokenizer.__class__.__name__ , '''NewTokenizer''' ) else: self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) def __snake_case ( self ): try: AutoConfig.register('''custom''' , UpperCamelCase__ ) AutoFeatureExtractor.register(UpperCamelCase__ , UpperCamelCase__ ) AutoTokenizer.register(UpperCamelCase__ , slow_tokenizer_class=UpperCamelCase__ ) AutoProcessor.register(UpperCamelCase__ , UpperCamelCase__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(UpperCamelCase__ ): AutoProcessor.register(UpperCamelCase__ , UpperCamelCase__ ) # Now that the config is registered, it can be used as any other config with the auto-API A__ : Any = CustomFeatureExtractor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: A__ : str = os.path.join(UpperCamelCase__ , '''vocab.txt''' ) with open(UpperCamelCase__ , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens] ) ) A__ : str = CustomTokenizer(UpperCamelCase__ ) A__ : Optional[Any] = CustomProcessor(UpperCamelCase__ , UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained(UpperCamelCase__ ) A__ : Union[str, Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] if CustomConfig in PROCESSOR_MAPPING._extra_content: del PROCESSOR_MAPPING._extra_content[CustomConfig] def __snake_case ( self ): class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = False class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = False class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "AutoFeatureExtractor" _lowerCAmelCase = "AutoTokenizer" _lowerCAmelCase = False try: AutoConfig.register('''custom''' , UpperCamelCase__ ) AutoFeatureExtractor.register(UpperCamelCase__ , UpperCamelCase__ ) AutoTokenizer.register(UpperCamelCase__ , slow_tokenizer_class=UpperCamelCase__ ) AutoProcessor.register(UpperCamelCase__ , UpperCamelCase__ ) # If remote code is not set, the default is to use local classes. A__ : List[Any] = AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor''' ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) self.assertFalse(processor.special_attribute_present ) self.assertFalse(processor.feature_extractor.special_attribute_present ) self.assertFalse(processor.tokenizer.special_attribute_present ) # If remote code is disabled, we load the local ones. A__ : Any = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) self.assertFalse(processor.special_attribute_present ) self.assertFalse(processor.feature_extractor.special_attribute_present ) self.assertFalse(processor.tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub. A__ : Union[str, Any] = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) self.assertTrue(processor.special_attribute_present ) self.assertTrue(processor.feature_extractor.special_attribute_present ) self.assertTrue(processor.tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] if CustomConfig in PROCESSOR_MAPPING._extra_content: del PROCESSOR_MAPPING._extra_content[CustomConfig] def __snake_case ( self ): A__ : str = AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) self.assertEqual(processor.__class__.__name__ , '''BertTokenizerFast''' ) def __snake_case ( self ): A__ : Union[str, Any] = AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-convnext''' ) self.assertEqual(processor.__class__.__name__ , '''ConvNextImageProcessor''' ) @is_staging_test class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"] @classmethod def __snake_case ( cls ): A__ : List[str] = TOKEN HfFolder.save_token(UpperCamelCase__ ) @classmethod def __snake_case ( cls ): try: delete_repo(token=cls._token , repo_id='''test-processor''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-processor-org''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''test-dynamic-processor''' ) except HTTPError: pass def __snake_case ( self ): A__ : Optional[Any] = WavaVecaProcessor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained( os.path.join(UpperCamelCase__ , '''test-processor''' ) , push_to_hub=UpperCamelCase__ , use_auth_token=self._token ) A__ : List[Any] = WavaVecaProcessor.from_pretrained(F"{USER}/test-processor" ) for k, v in processor.feature_extractor.__dict__.items(): self.assertEqual(UpperCamelCase__ , getattr(new_processor.feature_extractor , UpperCamelCase__ ) ) self.assertDictEqual(new_processor.tokenizer.get_vocab() , processor.tokenizer.get_vocab() ) def __snake_case ( self ): A__ : int = WavaVecaProcessor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained( os.path.join(UpperCamelCase__ , '''test-processor-org''' ) , push_to_hub=UpperCamelCase__ , use_auth_token=self._token , organization='''valid_org''' , ) A__ : List[str] = WavaVecaProcessor.from_pretrained('''valid_org/test-processor-org''' ) for k, v in processor.feature_extractor.__dict__.items(): self.assertEqual(UpperCamelCase__ , getattr(new_processor.feature_extractor , UpperCamelCase__ ) ) self.assertDictEqual(new_processor.tokenizer.get_vocab() , processor.tokenizer.get_vocab() ) def __snake_case ( self ): CustomFeatureExtractor.register_for_auto_class() CustomTokenizer.register_for_auto_class() CustomProcessor.register_for_auto_class() A__ : Optional[Any] = CustomFeatureExtractor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: A__ : List[Any] = os.path.join(UpperCamelCase__ , '''vocab.txt''' ) with open(UpperCamelCase__ , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens] ) ) A__ : Union[str, Any] = CustomTokenizer(UpperCamelCase__ ) A__ : List[Any] = CustomProcessor(UpperCamelCase__ , UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: create_repo(F"{USER}/test-dynamic-processor" , token=self._token ) A__ : Union[str, Any] = Repository(UpperCamelCase__ , clone_from=F"{USER}/test-dynamic-processor" , token=self._token ) processor.save_pretrained(UpperCamelCase__ ) # This has added the proper auto_map field to the feature extractor config self.assertDictEqual( processor.feature_extractor.auto_map , { '''AutoFeatureExtractor''': '''custom_feature_extraction.CustomFeatureExtractor''', '''AutoProcessor''': '''custom_processing.CustomProcessor''', } , ) # This has added the proper auto_map field to the tokenizer config with open(os.path.join(UpperCamelCase__ , '''tokenizer_config.json''' ) ) as f: A__ : Optional[int] = json.load(UpperCamelCase__ ) self.assertDictEqual( tokenizer_config['''auto_map'''] , { '''AutoTokenizer''': ['''custom_tokenization.CustomTokenizer''', None], '''AutoProcessor''': '''custom_processing.CustomProcessor''', } , ) # The code has been copied from fixtures self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase__ , '''custom_feature_extraction.py''' ) ) ) self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase__ , '''custom_tokenization.py''' ) ) ) self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase__ , '''custom_processing.py''' ) ) ) repo.push_to_hub() A__ : Tuple = AutoProcessor.from_pretrained(F"{USER}/test-dynamic-processor" , trust_remote_code=UpperCamelCase__ ) # Can't make an isinstance check because the new_processor is from the CustomProcessor class of a dynamic module self.assertEqual(new_processor.__class__.__name__ , '''CustomProcessor''' )
55
1
from timeit import timeit def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> int: """simple docstring""" if number < 0: raise ValueError('''the value of input must not be negative''' ) A__ : int = 0 while number: number &= number - 1 result += 1 return result def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> int: """simple docstring""" if number < 0: raise ValueError('''the value of input must not be negative''' ) A__ : Optional[Any] = 0 while number: if number % 2 == 1: result += 1 number >>= 1 return result def SCREAMING_SNAKE_CASE ( ) -> None: """simple docstring""" def do_benchmark(__UpperCamelCase : int ) -> None: A__ : List[str] = '''import __main__ as z''' print(F"Benchmark when {number = }:" ) print(F"{get_set_bits_count_using_modulo_operator(__UpperCamelCase ) = }" ) A__ : int = timeit('''z.get_set_bits_count_using_modulo_operator(25)''' , setup=__UpperCamelCase ) print(F"timeit() runs in {timing} seconds" ) print(F"{get_set_bits_count_using_brian_kernighans_algorithm(__UpperCamelCase ) = }" ) A__ : Any = timeit( '''z.get_set_bits_count_using_brian_kernighans_algorithm(25)''' , setup=__UpperCamelCase , ) print(F"timeit() runs in {timing} seconds" ) for number in (25, 37, 58, 0): do_benchmark(__UpperCamelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
55
from abc import ABC, abstractmethod from argparse import ArgumentParser class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' @staticmethod @abstractmethod def __snake_case ( UpperCamelCase__ ): raise NotImplementedError() @abstractmethod def __snake_case ( self ): raise NotImplementedError()
55
1
from collections import OrderedDict from typing import List, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _SCREAMING_SNAKE_CASE : Tuple = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : List[str] = { 'google/efficientnet-b7': 'https://huggingface.co/google/efficientnet-b7/resolve/main/config.json', } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "efficientnet" def __init__( self , UpperCamelCase__ = 3 , UpperCamelCase__ = 600 , UpperCamelCase__ = 2.0 , UpperCamelCase__ = 3.1 , UpperCamelCase__ = 8 , UpperCamelCase__ = [3, 3, 5, 3, 5, 5, 3] , UpperCamelCase__ = [32, 16, 24, 40, 80, 112, 192] , UpperCamelCase__ = [16, 24, 40, 80, 112, 192, 320] , UpperCamelCase__ = [] , UpperCamelCase__ = [1, 2, 2, 2, 1, 2, 1] , UpperCamelCase__ = [1, 2, 2, 3, 3, 4, 1] , UpperCamelCase__ = [1, 6, 6, 6, 6, 6, 6] , UpperCamelCase__ = 0.2_5 , UpperCamelCase__ = "swish" , UpperCamelCase__ = 2560 , UpperCamelCase__ = "mean" , UpperCamelCase__ = 0.0_2 , UpperCamelCase__ = 0.0_0_1 , UpperCamelCase__ = 0.9_9 , UpperCamelCase__ = 0.5 , UpperCamelCase__ = 0.2 , **UpperCamelCase__ , ): super().__init__(**UpperCamelCase__ ) A__ : Optional[Any] = num_channels A__ : str = image_size A__ : int = width_coefficient A__ : Optional[int] = depth_coefficient A__ : str = depth_divisor A__ : Tuple = kernel_sizes A__ : List[str] = in_channels A__ : List[Any] = out_channels A__ : Optional[Any] = depthwise_padding A__ : Union[str, Any] = strides A__ : Optional[int] = num_block_repeats A__ : Any = expand_ratios A__ : Optional[Any] = squeeze_expansion_ratio A__ : Optional[Any] = hidden_act A__ : Union[str, Any] = hidden_dim A__ : Any = pooling_type A__ : Union[str, Any] = initializer_range A__ : Tuple = batch_norm_eps A__ : Union[str, Any] = batch_norm_momentum A__ : Union[str, Any] = dropout_rate A__ : Dict = drop_connect_rate A__ : Tuple = sum(UpperCamelCase__ ) * 4 class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = version.parse("1.11" ) @property def __snake_case ( self ): return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __snake_case ( self ): return 1e-5
55
import inspect import unittest from transformers import YolosConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import YolosForObjectDetection, YolosModel from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__=13 , UpperCamelCase__=[30, 30] , UpperCamelCase__=2 , UpperCamelCase__=3 , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=32 , UpperCamelCase__=5 , UpperCamelCase__=4 , UpperCamelCase__=37 , UpperCamelCase__="gelu" , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=10 , UpperCamelCase__=0.0_2 , UpperCamelCase__=3 , UpperCamelCase__=None , UpperCamelCase__=8 , UpperCamelCase__=10 , ): A__ : Optional[int] = parent A__ : List[Any] = batch_size A__ : Dict = image_size A__ : Any = patch_size A__ : Dict = num_channels A__ : List[Any] = is_training A__ : int = use_labels A__ : Any = hidden_size A__ : List[str] = num_hidden_layers A__ : Optional[int] = num_attention_heads A__ : Optional[Any] = intermediate_size A__ : str = hidden_act A__ : str = hidden_dropout_prob A__ : Optional[int] = attention_probs_dropout_prob A__ : Optional[int] = type_sequence_label_size A__ : Any = initializer_range A__ : Optional[int] = num_labels A__ : Union[str, Any] = scope A__ : Union[str, Any] = n_targets A__ : Dict = num_detection_tokens # we set the expected sequence length (which is used in several tests) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens A__ : int = (image_size[1] // patch_size) * (image_size[0] // patch_size) A__ : List[str] = num_patches + 1 + self.num_detection_tokens def __snake_case ( self ): A__ : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]] ) A__ : int = None if self.use_labels: # labels is a list of Dict (each Dict being the labels for a given example in the batch) A__ : Tuple = [] for i in range(self.batch_size ): A__ : List[Any] = {} A__ : Tuple = torch.randint( high=self.num_labels , size=(self.n_targets,) , device=UpperCamelCase__ ) A__ : Any = torch.rand(self.n_targets , 4 , device=UpperCamelCase__ ) labels.append(UpperCamelCase__ ) A__ : Union[str, Any] = self.get_config() return config, pixel_values, labels def __snake_case ( self ): return YolosConfig( 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=UpperCamelCase__ , initializer_range=self.initializer_range , num_detection_tokens=self.num_detection_tokens , num_labels=self.num_labels , ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : Tuple = YolosModel(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : Optional[Any] = model(UpperCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.expected_seq_len, self.hidden_size) ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : Any = YolosForObjectDetection(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : Union[str, Any] = model(pixel_values=UpperCamelCase__ ) A__ : Optional[int] = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape , (self.batch_size, self.num_detection_tokens, 4) ) A__ : Union[str, Any] = model(pixel_values=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape , (self.batch_size, self.num_detection_tokens, 4) ) def __snake_case ( self ): A__ : Optional[int] = self.prepare_config_and_inputs() A__ , A__ , A__ : Optional[Any] = config_and_inputs A__ : Optional[int] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = (YolosModel, YolosForObjectDetection) if is_torch_available() else () _lowerCAmelCase = ( {"feature-extraction": YolosModel, "object-detection": YolosForObjectDetection} if is_torch_available() else {} ) _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=False ): A__ : Optional[int] = super()._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ , return_labels=UpperCamelCase__ ) if return_labels: if model_class.__name__ == "YolosForObjectDetection": A__ : str = [] for i in range(self.model_tester.batch_size ): A__ : int = {} A__ : Dict = torch.ones( size=(self.model_tester.n_targets,) , device=UpperCamelCase__ , dtype=torch.long ) A__ : Dict = torch.ones( self.model_tester.n_targets , 4 , device=UpperCamelCase__ , dtype=torch.float ) labels.append(UpperCamelCase__ ) A__ : Dict = labels return inputs_dict def __snake_case ( self ): A__ : List[Any] = YolosModelTester(self ) A__ : List[str] = ConfigTester(self , config_class=UpperCamelCase__ , has_text_modality=UpperCamelCase__ , hidden_size=37 ) def __snake_case ( self ): self.config_tester.run_common_tests() def __snake_case ( self ): # YOLOS does not use inputs_embeds pass def __snake_case ( self ): A__ , A__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : Any = model_class(UpperCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A__ : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCamelCase__ , nn.Linear ) ) def __snake_case ( self ): A__ , A__ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : List[str] = model_class(UpperCamelCase__ ) A__ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ : Optional[int] = [*signature.parameters.keys()] A__ : Optional[Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , UpperCamelCase__ ) def __snake_case ( self ): A__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase__ ) def __snake_case ( self ): A__ , A__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() A__ : Tuple = True # in YOLOS, the seq_len is different A__ : List[Any] = self.model_tester.expected_seq_len for model_class in self.all_model_classes: A__ : Any = True A__ : Optional[int] = False A__ : Optional[Any] = True A__ : int = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : List[str] = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Optional[int] = outputs.attentions self.assertEqual(len(UpperCamelCase__ ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] A__ : Tuple = True A__ : Optional[Any] = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : Tuple = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Tuple = outputs.attentions self.assertEqual(len(UpperCamelCase__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) A__ : List[Any] = len(UpperCamelCase__ ) # Check attention is always last and order is fine A__ : List[str] = True A__ : List[Any] = True A__ : int = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : Tuple = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Tuple = 1 self.assertEqual(out_len + added_hidden_states , len(UpperCamelCase__ ) ) A__ : List[str] = outputs.attentions self.assertEqual(len(UpperCamelCase__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) def __snake_case ( self ): def check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : str = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : int = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Optional[Any] = outputs.hidden_states A__ : int = getattr( self.model_tester , '''expected_num_hidden_layers''' , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(UpperCamelCase__ ) , UpperCamelCase__ ) # YOLOS has a different seq_length A__ : Union[str, Any] = self.model_tester.expected_seq_len self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) A__ , A__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : int = True check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ : Optional[int] = True check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): A__ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_object_detection(*UpperCamelCase__ ) @slow def __snake_case ( self ): for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ : Union[str, Any] = YolosModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) def SCREAMING_SNAKE_CASE ( ) -> List[str]: """simple docstring""" A__ : 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 __snake_case ( self ): return AutoImageProcessor.from_pretrained('''hustvl/yolos-small''' ) if is_vision_available() else None @slow def __snake_case ( self ): A__ : Tuple = YolosForObjectDetection.from_pretrained('''hustvl/yolos-small''' ).to(UpperCamelCase__ ) A__ : str = self.default_image_processor A__ : Tuple = prepare_img() A__ : Tuple = image_processor(images=UpperCamelCase__ , return_tensors='''pt''' ).to(UpperCamelCase__ ) # forward pass with torch.no_grad(): A__ : Any = model(inputs.pixel_values ) # verify outputs A__ : List[Any] = torch.Size((1, 100, 92) ) self.assertEqual(outputs.logits.shape , UpperCamelCase__ ) A__ : Optional[int] = torch.tensor( [[-2_4.0_2_4_8, -1_0.3_0_2_4, -1_4.8_2_9_0], [-4_2.0_3_9_2, -1_6.8_2_0_0, -2_7.4_3_3_4], [-2_7.2_7_4_3, -1_1.8_1_5_4, -1_8.7_1_4_8]] , device=UpperCamelCase__ , ) A__ : Optional[int] = torch.tensor( [[0.2_5_5_9, 0.5_4_5_5, 0.4_7_0_6], [0.2_9_8_9, 0.7_2_7_9, 0.1_8_7_5], [0.7_7_3_2, 0.4_0_1_7, 0.4_4_6_2]] , device=UpperCamelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , UpperCamelCase__ , atol=1e-4 ) ) self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3] , UpperCamelCase__ , atol=1e-4 ) ) # verify postprocessing A__ : Dict = image_processor.post_process_object_detection( UpperCamelCase__ , threshold=0.3 , target_sizes=[image.size[::-1]] )[0] A__ : int = torch.tensor([0.9_9_9_4, 0.9_7_9_0, 0.9_9_6_4, 0.9_9_7_2, 0.9_8_6_1] ).to(UpperCamelCase__ ) A__ : str = [75, 75, 17, 63, 17] A__ : Tuple = torch.tensor([3_3_5.0_6_0_9, 7_9.3_8_4_8, 3_7_5.4_2_1_6, 1_8_7.2_4_9_5] ).to(UpperCamelCase__ ) self.assertEqual(len(results['''scores'''] ) , 5 ) self.assertTrue(torch.allclose(results['''scores'''] , UpperCamelCase__ , atol=1e-4 ) ) self.assertSequenceEqual(results['''labels'''].tolist() , UpperCamelCase__ ) self.assertTrue(torch.allclose(results['''boxes'''][0, :] , UpperCamelCase__ ) )
55
1
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import json import os from ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES from ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType from ...utils.imports import is_botoa_available from .config_args import SageMakerConfig from .config_utils import ( DYNAMO_BACKENDS, _ask_field, _ask_options, _convert_dynamo_backend, _convert_mixed_precision, _convert_sagemaker_distributed_mode, _convert_yes_no_to_bool, ) if is_botoa_available(): import botoa # noqa: F401 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> int: """simple docstring""" A__ : List[str] = botoa.client('''iam''' ) A__ : str = { '''Version''': '''2012-10-17''', '''Statement''': [ {'''Effect''': '''Allow''', '''Principal''': {'''Service''': '''sagemaker.amazonaws.com'''}, '''Action''': '''sts:AssumeRole'''} ], } try: # create the role, associated with the chosen trust policy iam_client.create_role( RoleName=__UpperCamelCase , AssumeRolePolicyDocument=json.dumps(__UpperCamelCase , indent=2 ) ) A__ : str = { '''Version''': '''2012-10-17''', '''Statement''': [ { '''Effect''': '''Allow''', '''Action''': [ '''sagemaker:*''', '''ecr:GetDownloadUrlForLayer''', '''ecr:BatchGetImage''', '''ecr:BatchCheckLayerAvailability''', '''ecr:GetAuthorizationToken''', '''cloudwatch:PutMetricData''', '''cloudwatch:GetMetricData''', '''cloudwatch:GetMetricStatistics''', '''cloudwatch:ListMetrics''', '''logs:CreateLogGroup''', '''logs:CreateLogStream''', '''logs:DescribeLogStreams''', '''logs:PutLogEvents''', '''logs:GetLogEvents''', '''s3:CreateBucket''', '''s3:ListBucket''', '''s3:GetBucketLocation''', '''s3:GetObject''', '''s3:PutObject''', ], '''Resource''': '''*''', } ], } # attach policy to role iam_client.put_role_policy( RoleName=__UpperCamelCase , PolicyName=F"{role_name}_policy_permission" , PolicyDocument=json.dumps(__UpperCamelCase , indent=2 ) , ) except iam_client.exceptions.EntityAlreadyExistsException: print(F"role {role_name} already exists. Using existing one" ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> List[str]: """simple docstring""" A__ : Tuple = botoa.client('''iam''' ) return iam_client.get_role(RoleName=__UpperCamelCase )["Role"]["Arn"] def SCREAMING_SNAKE_CASE ( ) -> int: """simple docstring""" A__ : str = _ask_options( '''How do you want to authorize?''' , ['''AWS Profile''', '''Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) '''] , __UpperCamelCase , ) A__ : Optional[int] = None if credentials_configuration == 0: A__ : Optional[int] = _ask_field('''Enter your AWS Profile name: [default] ''' , default='''default''' ) A__ : Any = aws_profile else: print( '''Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,''' '''`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`''' ) A__ : Any = _ask_field('''AWS Access Key ID: ''' ) A__ : Optional[int] = aws_access_key_id A__ : str = _ask_field('''AWS Secret Access Key: ''' ) A__ : Optional[Any] = aws_secret_access_key A__ : List[Any] = _ask_field('''Enter your AWS Region: [us-east-1]''' , default='''us-east-1''' ) A__ : List[str] = aws_region A__ : List[str] = _ask_options( '''Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?''' , ['''Provide IAM Role name''', '''Create new IAM role using credentials'''] , __UpperCamelCase , ) if role_management == 0: A__ : Tuple = _ask_field('''Enter your IAM role name: ''' ) else: A__ : str = '''accelerate_sagemaker_execution_role''' print(F"Accelerate will create an iam role \"{iam_role_name}\" using the provided credentials" ) _create_iam_role_for_sagemaker(__UpperCamelCase ) A__ : List[str] = _ask_field( '''Do you want to use custom Docker image? [yes/NO]: ''' , _convert_yes_no_to_bool , default=__UpperCamelCase , error_message='''Please enter yes or no.''' , ) A__ : str = None if is_custom_docker_image: A__ : Union[str, Any] = _ask_field('''Enter your Docker image: ''' , lambda __UpperCamelCase : str(__UpperCamelCase ).lower() ) A__ : str = _ask_field( '''Do you want to provide SageMaker input channels with data locations? [yes/NO]: ''' , _convert_yes_no_to_bool , default=__UpperCamelCase , error_message='''Please enter yes or no.''' , ) A__ : Optional[Any] = None if is_sagemaker_inputs_enabled: A__ : Dict = _ask_field( '''Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): ''' , lambda __UpperCamelCase : str(__UpperCamelCase ).lower() , ) A__ : Dict = _ask_field( '''Do you want to enable SageMaker metrics? [yes/NO]: ''' , _convert_yes_no_to_bool , default=__UpperCamelCase , error_message='''Please enter yes or no.''' , ) A__ : int = None if is_sagemaker_metrics_enabled: A__ : Any = _ask_field( '''Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): ''' , lambda __UpperCamelCase : str(__UpperCamelCase ).lower() , ) A__ : Any = _ask_options( '''What is the distributed mode?''' , ['''No distributed training''', '''Data parallelism'''] , _convert_sagemaker_distributed_mode , ) A__ : List[str] = {} A__ : Tuple = _ask_field( '''Do you wish to optimize your script with torch dynamo?[yes/NO]:''' , _convert_yes_no_to_bool , default=__UpperCamelCase , error_message='''Please enter yes or no.''' , ) if use_dynamo: A__ : Any = '''dynamo_''' A__ : List[str] = _ask_options( '''Which dynamo backend would you like to use?''' , [x.lower() for x in DYNAMO_BACKENDS] , _convert_dynamo_backend , default=2 , ) A__ : List[Any] = _ask_field( '''Do you want to customize the defaults sent to torch.compile? [yes/NO]: ''' , _convert_yes_no_to_bool , default=__UpperCamelCase , error_message='''Please enter yes or no.''' , ) if use_custom_options: A__ : Union[str, Any] = _ask_options( '''Which mode do you want to use?''' , __UpperCamelCase , lambda __UpperCamelCase : TORCH_DYNAMO_MODES[int(__UpperCamelCase )] , default='''default''' , ) A__ : Optional[Any] = _ask_field( '''Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: ''' , _convert_yes_no_to_bool , default=__UpperCamelCase , error_message='''Please enter yes or no.''' , ) A__ : Optional[int] = _ask_field( '''Do you want to enable dynamic shape tracing? [yes/NO]: ''' , _convert_yes_no_to_bool , default=__UpperCamelCase , error_message='''Please enter yes or no.''' , ) A__ : List[Any] = '''Which EC2 instance type you want to use for your training?''' if distributed_type != SageMakerDistributedType.NO: A__ : Optional[int] = _ask_options( __UpperCamelCase , __UpperCamelCase , lambda __UpperCamelCase : SAGEMAKER_PARALLEL_EC2_INSTANCES[int(__UpperCamelCase )] ) else: eca_instance_query += "? [ml.p3.2xlarge]:" A__ : Union[str, Any] = _ask_field(__UpperCamelCase , lambda __UpperCamelCase : str(__UpperCamelCase ).lower() , default='''ml.p3.2xlarge''' ) A__ : Dict = 1 if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL): A__ : int = _ask_field( '''How many machines do you want use? [1]: ''' , __UpperCamelCase , default=1 , ) A__ : Union[str, Any] = _ask_options( '''Do you wish to use FP16 or BF16 (mixed precision)?''' , ['''no''', '''fp16''', '''bf16''', '''fp8'''] , _convert_mixed_precision , ) if use_dynamo and mixed_precision == "no": print( '''Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts.''' ) return SageMakerConfig( image_uri=__UpperCamelCase , compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER , distributed_type=__UpperCamelCase , use_cpu=__UpperCamelCase , dynamo_config=__UpperCamelCase , eca_instance_type=__UpperCamelCase , profile=__UpperCamelCase , region=__UpperCamelCase , iam_role_name=__UpperCamelCase , mixed_precision=__UpperCamelCase , num_machines=__UpperCamelCase , sagemaker_inputs_file=__UpperCamelCase , sagemaker_metrics_file=__UpperCamelCase , )
55
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> int: """simple docstring""" if n == 1 or not isinstance(__UpperCamelCase , __UpperCamelCase ): return 0 elif n == 2: return 1 else: A__ : Any = [0, 1] for i in range(2 , n + 1 ): sequence.append(sequence[i - 1] + sequence[i - 2] ) return sequence[n] def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> int: """simple docstring""" A__ : Dict = 0 A__ : Optional[int] = 2 while digits < n: index += 1 A__ : Dict = len(str(fibonacci(__UpperCamelCase ) ) ) return index def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int = 10_00 ) -> int: """simple docstring""" return fibonacci_digits_index(__UpperCamelCase ) if __name__ == "__main__": print(solution(int(str(input()).strip())))
55
1
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto.configuration_auto import CONFIG_MAPPING _SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "upernet" def __init__( self , UpperCamelCase__=None , UpperCamelCase__=512 , UpperCamelCase__=0.0_2 , UpperCamelCase__=[1, 2, 3, 6] , UpperCamelCase__=True , UpperCamelCase__=0.4 , UpperCamelCase__=384 , UpperCamelCase__=256 , UpperCamelCase__=1 , UpperCamelCase__=False , UpperCamelCase__=255 , **UpperCamelCase__ , ): super().__init__(**UpperCamelCase__ ) if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) A__ : Optional[int] = CONFIG_MAPPING['''resnet'''](out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] ) elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Optional[int] = backbone_config.get('''model_type''' ) A__ : Dict = CONFIG_MAPPING[backbone_model_type] A__ : Optional[Any] = config_class.from_dict(UpperCamelCase__ ) A__ : Union[str, Any] = backbone_config A__ : List[str] = hidden_size A__ : Tuple = initializer_range A__ : Optional[int] = pool_scales A__ : Tuple = use_auxiliary_head A__ : Optional[Any] = auxiliary_loss_weight A__ : Optional[int] = auxiliary_in_channels A__ : Union[str, Any] = auxiliary_channels A__ : Tuple = auxiliary_num_convs A__ : List[Any] = auxiliary_concat_input A__ : Optional[int] = loss_ignore_index def __snake_case ( self ): A__ : Optional[int] = copy.deepcopy(self.__dict__ ) A__ : Optional[Any] = self.backbone_config.to_dict() A__ : Any = self.__class__.model_type return output
55
_SCREAMING_SNAKE_CASE : List[str] = range(2, 2_0 + 1) _SCREAMING_SNAKE_CASE : Optional[Any] = [1_0**k for k in range(ks[-1] + 1)] _SCREAMING_SNAKE_CASE : dict[int, dict[int, list[list[int]]]] = {} def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any , __UpperCamelCase : Dict , __UpperCamelCase : Any , __UpperCamelCase : Optional[Any] ) -> int: """simple docstring""" A__ : Tuple = sum(a_i[j] for j in range(__UpperCamelCase , len(__UpperCamelCase ) ) ) A__ : Tuple = sum(a_i[j] * base[j] for j in range(min(len(__UpperCamelCase ) , __UpperCamelCase ) ) ) A__ , A__ : Optional[int] = 0, 0 A__ : List[Any] = n - i A__ : Any = memo.get(__UpperCamelCase ) if sub_memo is not None: A__ : Optional[int] = sub_memo.get(__UpperCamelCase ) if jumps is not None and len(__UpperCamelCase ) > 0: # find and make the largest jump without going over A__ : List[Any] = -1 for _k in range(len(__UpperCamelCase ) - 1 , -1 , -1 ): if jumps[_k][2] <= k and jumps[_k][1] <= max_dn: A__ : List[str] = _k break if max_jump >= 0: A__ , A__ , A__ : List[Any] = jumps[max_jump] # since the difference between jumps is cached, add c A__ : int = diff + c for j in range(min(__UpperCamelCase , len(__UpperCamelCase ) ) ): A__ , A__ : List[str] = divmod(__UpperCamelCase , 10 ) if new_c > 0: add(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else: A__ : List[Any] = [] else: A__ : Optional[Any] = {c: []} A__ : int = sub_memo if dn >= max_dn or c + diff >= base[k]: return diff, dn if k > ks[0]: while True: # keep doing smaller jumps A__ , A__ : str = next_term(__UpperCamelCase , k - 1 , i + dn , __UpperCamelCase ) diff += _diff dn += terms_jumped if dn >= max_dn or c + diff >= base[k]: break else: # would be too small a jump, just compute sequential terms instead A__ , A__ : str = compute(__UpperCamelCase , __UpperCamelCase , i + dn , __UpperCamelCase ) diff += _diff dn += terms_jumped A__ : str = sub_memo[c] # keep jumps sorted by # of terms skipped A__ : List[Any] = 0 while j < len(__UpperCamelCase ): if jumps[j][1] > dn: break j += 1 # cache the jump for this value digitsum(b) and c sub_memo[c].insert(__UpperCamelCase , (diff, dn, k) ) return (diff, dn) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : Optional[int] , __UpperCamelCase : List[str] , __UpperCamelCase : int ) -> Any: """simple docstring""" if i >= n: return 0, i if k > len(__UpperCamelCase ): a_i.extend([0 for _ in range(k - len(__UpperCamelCase ) )] ) # note: a_i -> b * 10^k + c # ds_b -> digitsum(b) # ds_c -> digitsum(c) A__ : Optional[Any] = i A__ , A__ , A__ : Dict = 0, 0, 0 for j in range(len(__UpperCamelCase ) ): if j >= k: ds_b += a_i[j] else: ds_c += a_i[j] while i < n: i += 1 A__ : int = ds_c + ds_b diff += addend A__ : List[Any] = 0 for j in range(__UpperCamelCase ): A__ : Optional[Any] = a_i[j] + addend A__ , A__ : List[str] = divmod(__UpperCamelCase , 10 ) ds_c += a_i[j] if addend > 0: break if addend > 0: add(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) return diff, i - start_i def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict , __UpperCamelCase : List[Any] , __UpperCamelCase : int ) -> Tuple: """simple docstring""" for j in range(__UpperCamelCase , len(__UpperCamelCase ) ): A__ : Any = digits[j] + addend if s >= 10: A__ , A__ : Union[str, Any] = divmod(__UpperCamelCase , 10 ) A__ : Optional[int] = addend // 10 + quotient else: A__ : Any = s A__ : Dict = addend // 10 if addend == 0: break while addend > 0: A__ , A__ : Dict = divmod(__UpperCamelCase , 10 ) digits.append(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int = 10**15 ) -> int: """simple docstring""" A__ : List[Any] = [1] A__ : Dict = 1 A__ : Tuple = 0 while True: A__ , A__ : List[str] = next_term(__UpperCamelCase , 20 , i + dn , __UpperCamelCase ) dn += terms_jumped if dn == n - i: break A__ : List[str] = 0 for j in range(len(__UpperCamelCase ) ): a_n += digits[j] * 10**j return a_n if __name__ == "__main__": print(f"""{solution() = }""")
55
1
import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__=13 , UpperCamelCase__=7 , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=99 , UpperCamelCase__=32 , UpperCamelCase__=5 , UpperCamelCase__=4 , UpperCamelCase__=37 , UpperCamelCase__="gelu" , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=512 , UpperCamelCase__=16 , UpperCamelCase__=2 , UpperCamelCase__=0.0_2 , UpperCamelCase__=4 , ): A__ : Optional[int] = parent A__ : str = batch_size A__ : str = seq_length A__ : str = is_training A__ : Optional[int] = use_attention_mask A__ : Dict = use_token_type_ids A__ : Any = use_labels A__ : int = vocab_size A__ : str = hidden_size A__ : List[str] = num_hidden_layers A__ : int = num_attention_heads A__ : Optional[int] = intermediate_size A__ : Tuple = hidden_act A__ : List[str] = hidden_dropout_prob A__ : Optional[int] = attention_probs_dropout_prob A__ : Optional[int] = max_position_embeddings A__ : Dict = type_vocab_size A__ : Optional[int] = type_sequence_label_size A__ : List[str] = initializer_range A__ : Union[str, Any] = num_choices def __snake_case ( self ): A__ : Any = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ : List[str] = None if self.use_attention_mask: A__ : Dict = random_attention_mask([self.batch_size, self.seq_length] ) A__ : List[Any] = None if self.use_token_type_ids: A__ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ : str = AlbertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=UpperCamelCase__ , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def __snake_case ( self ): A__ : str = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ : Tuple = config_and_inputs A__ : Any = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_flax class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def __snake_case ( self ): A__ : str = FlaxAlbertModelTester(self ) @slow def __snake_case ( self ): for model_class_name in self.all_model_classes: A__ : int = model_class_name.from_pretrained('''albert-base-v2''' ) A__ : Optional[Any] = model(np.ones((1, 1) ) ) self.assertIsNotNone(UpperCamelCase__ ) @require_flax class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' @slow def __snake_case ( self ): A__ : Union[str, Any] = FlaxAlbertModel.from_pretrained('''albert-base-v2''' ) A__ : Any = np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) A__ : Dict = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) A__ : str = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ )[0] A__ : Dict = (1, 11, 768) self.assertEqual(output.shape , UpperCamelCase__ ) A__ : str = np.array( [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , UpperCamelCase__ , atol=1e-4 ) )
55
import asyncio import os import shutil import subprocess import sys import tempfile import unittest from distutils.util import strtobool from functools import partial from pathlib import Path from typing import List, Union from unittest import mock import torch from ..state import AcceleratorState, PartialState from ..utils import ( gather, is_bnb_available, is_comet_ml_available, is_datasets_available, is_deepspeed_available, is_mps_available, is_safetensors_available, is_tensorboard_available, is_torch_version, is_tpu_available, is_transformers_available, is_wandb_available, is_xpu_available, ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] , __UpperCamelCase : int=False ) -> Tuple: """simple docstring""" try: A__ : Dict = os.environ[key] except KeyError: # KEY isn't set, default to `default`. A__ : Tuple = default else: # KEY is set, convert it to True or False. try: A__ : Union[str, Any] = strtobool(__UpperCamelCase ) except ValueError: # More values are supported, but let's keep the message simple. raise ValueError(F"If set, {key} must be yes or no." ) return _value _SCREAMING_SNAKE_CASE : Union[str, Any] = parse_flag_from_env('RUN_SLOW', default=False) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] ) -> Any: """simple docstring""" return unittest.skip('''Test was skipped''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> Union[str, Any]: """simple docstring""" return unittest.skipUnless(_run_slow_tests , '''test is slow''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> int: """simple docstring""" return unittest.skipUnless(not torch.cuda.is_available() , '''test requires only a CPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] ) -> Tuple: """simple docstring""" return unittest.skipUnless(torch.cuda.is_available() , '''test requires a GPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict ) -> List[str]: """simple docstring""" return unittest.skipUnless(is_xpu_available() , '''test requires a XPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict ) -> Any: """simple docstring""" return unittest.skipUnless(is_mps_available() , '''test requires a `mps` backend support in `torch`''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Optional[Any]: """simple docstring""" return unittest.skipUnless( is_transformers_available() and is_datasets_available() , '''test requires the Hugging Face suite''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> Tuple: """simple docstring""" return unittest.skipUnless(is_bnb_available() , '''test requires the bitsandbytes library''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> List[Any]: """simple docstring""" return unittest.skipUnless(is_tpu_available() , '''test requires TPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Tuple: """simple docstring""" return unittest.skipUnless(torch.cuda.device_count() == 1 , '''test requires a GPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Dict: """simple docstring""" return unittest.skipUnless(torch.xpu.device_count() == 1 , '''test requires a XPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> str: """simple docstring""" return unittest.skipUnless(torch.cuda.device_count() > 1 , '''test requires multiple GPUs''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Any: """simple docstring""" return unittest.skipUnless(torch.xpu.device_count() > 1 , '''test requires multiple XPUs''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> int: """simple docstring""" return unittest.skipUnless(is_safetensors_available() , '''test requires safetensors''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> Optional[Any]: """simple docstring""" return unittest.skipUnless(is_deepspeed_available() , '''test requires DeepSpeed''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> List[Any]: """simple docstring""" return unittest.skipUnless(is_torch_version('''>=''' , '''1.12.0''' ) , '''test requires torch version >= 1.12.0''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[int]=None , __UpperCamelCase : List[Any]=None ) -> Optional[Any]: """simple docstring""" if test_case is None: return partial(__UpperCamelCase , version=__UpperCamelCase ) return unittest.skipUnless(is_torch_version('''>=''' , __UpperCamelCase ) , F"test requires torch version >= {version}" )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] ) -> Optional[int]: """simple docstring""" return unittest.skipUnless(is_tensorboard_available() , '''test requires Tensorboard''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> Tuple: """simple docstring""" return unittest.skipUnless(is_wandb_available() , '''test requires wandb''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> Any: """simple docstring""" return unittest.skipUnless(is_comet_ml_available() , '''test requires comet_ml''' )(__UpperCamelCase ) _SCREAMING_SNAKE_CASE : Union[str, Any] = ( any([is_wandb_available(), is_tensorboard_available()]) and not is_comet_ml_available() ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[int] ) -> Optional[Any]: """simple docstring""" return unittest.skipUnless( _atleast_one_tracker_available , '''test requires at least one tracker to be available and for `comet_ml` to not be installed''' , )(__UpperCamelCase ) class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = True @classmethod def __snake_case ( cls ): A__ : Tuple = tempfile.mkdtemp() @classmethod def __snake_case ( cls ): if os.path.exists(cls.tmpdir ): shutil.rmtree(cls.tmpdir ) def __snake_case ( self ): if self.clear_on_setup: for path in Path(self.tmpdir ).glob('''**/*''' ): if path.is_file(): path.unlink() elif path.is_dir(): shutil.rmtree(UpperCamelCase__ ) class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): super().tearDown() # Reset the state of the AcceleratorState singleton. AcceleratorState._reset_state() PartialState._reset_state() class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self , UpperCamelCase__ ): A__ : Tuple = mocks if isinstance(UpperCamelCase__ , (tuple, list) ) else [mocks] for m in self.mocks: m.start() self.addCleanup(m.stop ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> Any: """simple docstring""" A__ : int = AcceleratorState() A__ : Any = tensor[None].clone().to(state.device ) A__ : Optional[int] = gather(__UpperCamelCase ).cpu() A__ : Any = tensor[0].cpu() for i in range(tensors.shape[0] ): if not torch.equal(tensors[i] , __UpperCamelCase ): return False return True class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : List[Any] = returncode A__ : Union[str, Any] = stdout A__ : Dict = stderr async def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str , __UpperCamelCase : Optional[Any] ) -> Any: """simple docstring""" while True: A__ : Tuple = await stream.readline() if line: callback(__UpperCamelCase ) else: break async def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] , __UpperCamelCase : Optional[Any]=None , __UpperCamelCase : List[Any]=None , __UpperCamelCase : Tuple=None , __UpperCamelCase : Tuple=False , __UpperCamelCase : List[Any]=False ) -> _RunOutput: """simple docstring""" if echo: print('''\nRunning: ''' , ''' '''.join(__UpperCamelCase ) ) A__ : int = await asyncio.create_subprocess_exec( cmd[0] , *cmd[1:] , stdin=__UpperCamelCase , stdout=asyncio.subprocess.PIPE , stderr=asyncio.subprocess.PIPE , env=__UpperCamelCase , ) # note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe # https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait # # If it starts hanging, will need to switch to the following code. The problem is that no data # will be seen until it's done and if it hangs for example there will be no debug info. # out, err = await p.communicate() # return _RunOutput(p.returncode, out, err) A__ : List[Any] = [] A__ : str = [] def tee(__UpperCamelCase : Optional[Any] , __UpperCamelCase : Union[str, Any] , __UpperCamelCase : Dict , __UpperCamelCase : List[Any]="" ): A__ : Optional[Any] = line.decode('''utf-8''' ).rstrip() sink.append(__UpperCamelCase ) if not quiet: print(__UpperCamelCase , __UpperCamelCase , file=__UpperCamelCase ) # XXX: the timeout doesn't seem to make any difference here await asyncio.wait( [ asyncio.create_task(_read_stream(p.stdout , lambda __UpperCamelCase : tee(__UpperCamelCase , __UpperCamelCase , sys.stdout , label='''stdout:''' ) ) ), asyncio.create_task(_read_stream(p.stderr , lambda __UpperCamelCase : tee(__UpperCamelCase , __UpperCamelCase , sys.stderr , label='''stderr:''' ) ) ), ] , timeout=__UpperCamelCase , ) return _RunOutput(await p.wait() , __UpperCamelCase , __UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] , __UpperCamelCase : Any=None , __UpperCamelCase : List[Any]=None , __UpperCamelCase : List[str]=1_80 , __UpperCamelCase : List[str]=False , __UpperCamelCase : Dict=True ) -> _RunOutput: """simple docstring""" A__ : Dict = asyncio.get_event_loop() A__ : Optional[Any] = loop.run_until_complete( _stream_subprocess(__UpperCamelCase , env=__UpperCamelCase , stdin=__UpperCamelCase , timeout=__UpperCamelCase , quiet=__UpperCamelCase , echo=__UpperCamelCase ) ) A__ : Union[str, Any] = ''' '''.join(__UpperCamelCase ) if result.returncode > 0: A__ : Optional[Any] = '''\n'''.join(result.stderr ) raise RuntimeError( F"'{cmd_str}' failed with returncode {result.returncode}\n\n" F"The combined stderr from workers follows:\n{stderr}" ) return result class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] , __UpperCamelCase : List[Any]=False ) -> Dict: """simple docstring""" try: A__ : List[Any] = subprocess.check_output(__UpperCamelCase , stderr=subprocess.STDOUT ) if return_stdout: if hasattr(__UpperCamelCase , '''decode''' ): A__ : Any = output.decode('''utf-8''' ) return output except subprocess.CalledProcessError as e: raise SubprocessCallException( F"Command `{' '.join(__UpperCamelCase )}` failed with the following error:\n\n{e.output.decode()}" ) from e
55
1
import argparse import os import transformers from .convert_slow_tokenizer import SLOW_TO_FAST_CONVERTERS from .utils import logging logging.set_verbosity_info() _SCREAMING_SNAKE_CASE : int = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Any = {name: getattr(transformers, name + 'Fast') for name in SLOW_TO_FAST_CONVERTERS} def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] , __UpperCamelCase : List[Any] , __UpperCamelCase : Optional[int] , __UpperCamelCase : Union[str, Any] ) -> List[str]: """simple docstring""" if tokenizer_name is not None and tokenizer_name not in TOKENIZER_CLASSES: raise ValueError(F"Unrecognized tokenizer name, should be one of {list(TOKENIZER_CLASSES.keys() )}." ) if tokenizer_name is None: A__ : Union[str, Any] = TOKENIZER_CLASSES else: A__ : Dict = {tokenizer_name: getattr(__UpperCamelCase , tokenizer_name + '''Fast''' )} logger.info(F"Loading tokenizer classes: {tokenizer_names}" ) for tokenizer_name in tokenizer_names: A__ : Union[str, Any] = TOKENIZER_CLASSES[tokenizer_name] A__ : int = True if checkpoint_name is None: A__ : Tuple = list(tokenizer_class.max_model_input_sizes.keys() ) else: A__ : List[Any] = [checkpoint_name] logger.info(F"For tokenizer {tokenizer_class.__class__.__name__} loading checkpoints: {checkpoint_names}" ) for checkpoint in checkpoint_names: logger.info(F"Loading {tokenizer_class.__class__.__name__} {checkpoint}" ) # Load tokenizer A__ : Optional[int] = tokenizer_class.from_pretrained(__UpperCamelCase , force_download=__UpperCamelCase ) # Save fast tokenizer logger.info(F"Save fast tokenizer to {dump_path} with prefix {checkpoint} add_prefix {add_prefix}" ) # For organization names we create sub-directories if "/" in checkpoint: A__ , A__ : Union[str, Any] = checkpoint.split('''/''' ) A__ : Any = os.path.join(__UpperCamelCase , __UpperCamelCase ) elif add_prefix: A__ : Tuple = checkpoint A__ : Union[str, Any] = dump_path else: A__ : str = None A__ : Any = dump_path logger.info(F"=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}" ) if checkpoint in list(tokenizer.pretrained_vocab_files_map.values() )[0]: A__ : int = list(tokenizer.pretrained_vocab_files_map.values() )[0][checkpoint] A__ : Union[str, Any] = file_path.split(__UpperCamelCase )[-1][0] if next_char == "/": A__ : Union[str, Any] = os.path.join(__UpperCamelCase , __UpperCamelCase ) A__ : str = None logger.info(F"=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}" ) A__ : Optional[Any] = tokenizer.save_pretrained( __UpperCamelCase , legacy_format=__UpperCamelCase , filename_prefix=__UpperCamelCase ) logger.info(F"=> File names {file_names}" ) for file_name in file_names: if not file_name.endswith('''tokenizer.json''' ): os.remove(__UpperCamelCase ) logger.info(F"=> removing {file_name}" ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE : str = argparse.ArgumentParser() # Required parameters parser.add_argument( '--dump_path', default=None, type=str, required=True, help='Path to output generated fast tokenizer files.' ) parser.add_argument( '--tokenizer_name', default=None, type=str, help=( f"""Optional tokenizer type selected in the list of {list(TOKENIZER_CLASSES.keys())}. If not given, will """ 'download and convert all the checkpoints from AWS.' ), ) parser.add_argument( '--checkpoint_name', default=None, type=str, help='Optional checkpoint name. If not given, will download and convert the canonical checkpoints from AWS.', ) parser.add_argument( '--force_download', action='store_true', help='Re-download checkpoints.', ) _SCREAMING_SNAKE_CASE : Optional[Any] = parser.parse_args() convert_slow_checkpoint_to_fast(args.tokenizer_name, args.checkpoint_name, args.dump_path, args.force_download)
55
import numpy as np _SCREAMING_SNAKE_CASE : Any = [ ['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'k'], ['l', 'm', 'n', 'o', 'p'], ['q', 'r', 's', 't', 'u'], ['v', 'w', 'x', 'y', 'z'], ] class UpperCamelCase__ : '''simple docstring''' def __init__( self ): A__ : List[Any] = np.array(UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): A__ , A__ : Any = np.where(letter == self.SQUARE ) A__ : int = np.concatenate([indexa + 1, indexa + 1] ) return indexes def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ ): A__ : Union[str, Any] = self.SQUARE[indexa - 1, indexa - 1] return letter def __snake_case ( self , UpperCamelCase__ ): A__ : List[str] = message.lower() A__ : str = message.replace(''' ''' , '''''' ) A__ : Union[str, Any] = message.replace('''j''' , '''i''' ) A__ : List[Any] = np.empty((2, len(UpperCamelCase__ )) ) for letter_index in range(len(UpperCamelCase__ ) ): A__ : Any = self.letter_to_numbers(message[letter_index] ) A__ : Optional[Any] = numbers[0] A__ : List[str] = numbers[1] A__ : List[str] = first_step.reshape(2 * len(UpperCamelCase__ ) ) A__ : List[Any] = '''''' for numbers_index in range(len(UpperCamelCase__ ) ): A__ : Dict = int(second_step[numbers_index * 2] ) A__ : List[str] = int(second_step[(numbers_index * 2) + 1] ) A__ : Dict = self.numbers_to_letter(UpperCamelCase__ , UpperCamelCase__ ) A__ : Tuple = encoded_message + letter return encoded_message def __snake_case ( self , UpperCamelCase__ ): A__ : str = message.lower() message.replace(''' ''' , '''''' ) A__ : List[Any] = np.empty(2 * len(UpperCamelCase__ ) ) for letter_index in range(len(UpperCamelCase__ ) ): A__ : List[str] = self.letter_to_numbers(message[letter_index] ) A__ : Dict = numbers[0] A__ : int = numbers[1] A__ : Optional[Any] = first_step.reshape((2, len(UpperCamelCase__ )) ) A__ : int = '''''' for numbers_index in range(len(UpperCamelCase__ ) ): A__ : Tuple = int(second_step[0, numbers_index] ) A__ : Dict = int(second_step[1, numbers_index] ) A__ : List[str] = self.numbers_to_letter(UpperCamelCase__ , UpperCamelCase__ ) A__ : Tuple = decoded_message + letter return decoded_message
55
1
from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, flip_channel_order, get_resize_output_image_size, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging if is_vision_available(): import PIL if is_torch_available(): import torch _SCREAMING_SNAKE_CASE : Optional[Any] = logging.get_logger(__name__) class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = ["pixel_values"] def __init__( self , UpperCamelCase__ = True , UpperCamelCase__ = None , UpperCamelCase__ = PILImageResampling.BILINEAR , UpperCamelCase__ = True , UpperCamelCase__ = 1 / 255 , UpperCamelCase__ = True , UpperCamelCase__ = None , UpperCamelCase__ = True , **UpperCamelCase__ , ): super().__init__(**UpperCamelCase__ ) A__ : Optional[Any] = size if size is not None else {'''shortest_edge''': 224} A__ : Optional[int] = get_size_dict(UpperCamelCase__ , default_to_square=UpperCamelCase__ ) A__ : Union[str, Any] = crop_size if crop_size is not None else {'''height''': 256, '''width''': 256} A__ : int = get_size_dict(UpperCamelCase__ , param_name='''crop_size''' ) A__ : Union[str, Any] = do_resize A__ : str = size A__ : List[str] = resample A__ : Dict = do_rescale A__ : Optional[Any] = rescale_factor A__ : List[str] = do_center_crop A__ : Tuple = crop_size A__ : Dict = do_flip_channel_order def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = PIL.Image.BILINEAR , UpperCamelCase__ = None , **UpperCamelCase__ , ): A__ : Tuple = get_size_dict(UpperCamelCase__ , default_to_square=UpperCamelCase__ ) if "shortest_edge" not in size: raise ValueError(F"The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}" ) A__ : Any = get_resize_output_image_size(UpperCamelCase__ , size=size['''shortest_edge'''] , default_to_square=UpperCamelCase__ ) return resize(UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = None , **UpperCamelCase__ , ): A__ : str = get_size_dict(UpperCamelCase__ ) 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()}" ) return center_crop(UpperCamelCase__ , size=(size['''height'''], size['''width''']) , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = None , **UpperCamelCase__ , ): return rescale(UpperCamelCase__ , scale=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ = None ): return flip_channel_order(UpperCamelCase__ , data_format=UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = ChannelDimension.FIRST , **UpperCamelCase__ , ): A__ : Optional[Any] = do_resize if do_resize is not None else self.do_resize A__ : List[Any] = resample if resample is not None else self.resample A__ : List[str] = do_rescale if do_rescale is not None else self.do_rescale A__ : List[str] = rescale_factor if rescale_factor is not None else self.rescale_factor A__ : Optional[int] = do_center_crop if do_center_crop is not None else self.do_center_crop A__ : Union[str, Any] = ( do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order ) A__ : List[Any] = size if size is not None else self.size A__ : Dict = get_size_dict(UpperCamelCase__ , default_to_square=UpperCamelCase__ ) A__ : Any = crop_size if crop_size is not None else self.crop_size A__ : Optional[Any] = get_size_dict(UpperCamelCase__ , param_name='''crop_size''' ) A__ : Optional[int] = make_list_of_images(UpperCamelCase__ ) if not valid_images(UpperCamelCase__ ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None: raise ValueError('''Size 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_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) # All transformations expect numpy arrays. A__ : List[Any] = [to_numpy_array(UpperCamelCase__ ) for image in images] if do_resize: A__ : List[Any] = [self.resize(image=UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ ) for image in images] if do_center_crop: A__ : Optional[Any] = [self.center_crop(image=UpperCamelCase__ , size=UpperCamelCase__ ) for image in images] if do_rescale: A__ : Union[str, Any] = [self.rescale(image=UpperCamelCase__ , scale=UpperCamelCase__ ) for image in images] # the pretrained checkpoints assume images are BGR, not RGB if do_flip_channel_order: A__ : Dict = [self.flip_channel_order(image=UpperCamelCase__ ) for image in images] A__ : Optional[Any] = [to_channel_dimension_format(UpperCamelCase__ , UpperCamelCase__ ) for image in images] A__ : Tuple = {'''pixel_values''': images} return BatchFeature(data=UpperCamelCase__ , tensor_type=UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ = None ): A__ : List[str] = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(UpperCamelCase__ ) != len(UpperCamelCase__ ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(UpperCamelCase__ ): A__ : List[str] = target_sizes.numpy() A__ : int = [] for idx in range(len(UpperCamelCase__ ) ): A__ : List[str] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='''bilinear''' , align_corners=UpperCamelCase__ ) A__ : Union[str, Any] = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(UpperCamelCase__ ) else: A__ : Dict = logits.argmax(dim=1 ) A__ : int = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
55
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline else: from .pipeline_kandinsky import KandinskyPipeline from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput from .text_encoder import MultilingualCLIP
55
1
import unittest from transformers import PegasusTokenizer, PegasusTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin _SCREAMING_SNAKE_CASE : Any = get_tests_dir('fixtures/test_sentencepiece_no_bos.model') @require_sentencepiece @require_tokenizers class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = PegasusTokenizer _lowerCAmelCase = PegasusTokenizerFast _lowerCAmelCase = True _lowerCAmelCase = True def __snake_case ( self ): super().setUp() # We have a SentencePiece fixture for testing A__ : List[str] = PegasusTokenizer(UpperCamelCase__ ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def __snake_case ( self ): return PegasusTokenizer.from_pretrained('''google/pegasus-large''' ) def __snake_case ( self , **UpperCamelCase__ ): return PegasusTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): return ("This is a test", "This is a test") def __snake_case ( self ): A__ : List[Any] = '''</s>''' A__ : List[Any] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCamelCase__ ) , UpperCamelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCamelCase__ ) , UpperCamelCase__ ) def __snake_case ( self ): A__ : Optional[int] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<pad>''' ) self.assertEqual(vocab_keys[1] , '''</s>''' ) self.assertEqual(vocab_keys[-1] , '''v''' ) self.assertEqual(len(UpperCamelCase__ ) , 1103 ) def __snake_case ( self ): self.assertEqual(self.get_tokenizer().vocab_size , 1103 ) def __snake_case ( self ): A__ : Tuple = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) A__ : List[str] = self.tokenizer_class.from_pretrained(self.tmpdirname ) A__ : List[Any] = ( '''Let\'s see which <unk> is the better <unk_token_11> one <mask_1> It seems like this <mask_2> was important''' ''' </s> <pad> <pad> <pad>''' ) A__ : str = rust_tokenizer([raw_input_str] , return_tensors=UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ).input_ids[0] A__ : List[str] = py_tokenizer([raw_input_str] , return_tensors=UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ).input_ids[0] self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): A__ : List[str] = self._large_tokenizer # <mask_1> masks whole sentence while <mask_2> masks single word A__ : List[str] = '''<mask_1> To ensure a <mask_2> flow of bank resolutions.''' A__ : List[Any] = [2, 413, 615, 114, 3, 1971, 113, 1679, 1_0710, 107, 1] A__ : Union[str, Any] = tokenizer([raw_input_str] , return_tensors=UpperCamelCase__ ).input_ids[0] self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): A__ : Any = self._large_tokenizer # The tracebacks for the following asserts are **better** without messages or self.assertEqual assert tokenizer.vocab_size == 9_6103 assert tokenizer.pad_token_id == 0 assert tokenizer.eos_token_id == 1 assert tokenizer.offset == 103 assert tokenizer.unk_token_id == tokenizer.offset + 2 == 105 assert tokenizer.unk_token == "<unk>" assert tokenizer.model_max_length == 1024 A__ : List[Any] = '''To ensure a smooth flow of bank resolutions.''' A__ : Union[str, Any] = [413, 615, 114, 2291, 1971, 113, 1679, 1_0710, 107, 1] A__ : List[str] = tokenizer([raw_input_str] , return_tensors=UpperCamelCase__ ).input_ids[0] self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) assert tokenizer.convert_ids_to_tokens([0, 1, 2, 3] ) == ["<pad>", "</s>", "<mask_1>", "<mask_2>"] @require_torch def __snake_case ( self ): A__ : Union[str, Any] = ['''This is going to be way too long.''' * 150, '''short example'''] A__ : Tuple = ['''not super long but more than 5 tokens''', '''tiny'''] A__ : Tuple = self._large_tokenizer(UpperCamelCase__ , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , return_tensors='''pt''' ) A__ : Any = self._large_tokenizer( text_target=UpperCamelCase__ , max_length=5 , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , return_tensors='''pt''' ) assert batch.input_ids.shape == (2, 1024) assert batch.attention_mask.shape == (2, 1024) assert targets["input_ids"].shape == (2, 5) assert len(UpperCamelCase__ ) == 2 # input_ids, attention_mask. @slow def __snake_case ( self ): # fmt: off A__ : Optional[Any] = {'''input_ids''': [[3_8979, 143, 1_8485, 606, 130, 2_6669, 8_7686, 121, 5_4189, 1129, 111, 2_6669, 8_7686, 121, 9114, 1_4787, 121, 1_3249, 158, 592, 956, 121, 1_4621, 3_1576, 143, 6_2613, 108, 9688, 930, 4_3430, 1_1562, 6_2613, 304, 108, 1_1443, 897, 108, 9314, 1_7415, 6_3399, 108, 1_1443, 7614, 1_8316, 118, 4284, 7148, 1_2430, 143, 1400, 2_5703, 158, 111, 4284, 7148, 1_1772, 143, 2_1297, 1064, 158, 122, 204, 3506, 1754, 1133, 1_4787, 1581, 115, 3_3224, 4482, 111, 1355, 110, 2_9173, 317, 5_0833, 108, 2_0147, 9_4665, 111, 7_7198, 107, 1], [110, 6_2613, 117, 638, 112, 1133, 121, 2_0098, 1355, 7_9050, 1_3872, 135, 1596, 5_3541, 1352, 141, 1_3039, 5542, 124, 302, 518, 111, 268, 2956, 115, 149, 4427, 107, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [139, 1235, 2799, 1_8289, 1_7780, 204, 109, 9474, 1296, 107, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=UpperCamelCase__ , model_name='''google/bigbird-pegasus-large-arxiv''' , revision='''ba85d0851d708441f91440d509690f1ab6353415''' , ) @require_sentencepiece @require_tokenizers class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = PegasusTokenizer _lowerCAmelCase = PegasusTokenizerFast _lowerCAmelCase = True _lowerCAmelCase = True def __snake_case ( self ): super().setUp() # We have a SentencePiece fixture for testing A__ : Optional[int] = PegasusTokenizer(UpperCamelCase__ , offset=0 , mask_token_sent=UpperCamelCase__ , mask_token='''[MASK]''' ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def __snake_case ( self ): return PegasusTokenizer.from_pretrained('''google/bigbird-pegasus-large-arxiv''' ) def __snake_case ( self , **UpperCamelCase__ ): return PegasusTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): return ("This is a test", "This is a test") def __snake_case ( self ): A__ : Any = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) A__ : Tuple = self.tokenizer_class.from_pretrained(self.tmpdirname ) A__ : Tuple = ( '''Let\'s see which <unk> is the better <unk_token> one [MASK] It seems like this [MASK] was important </s>''' ''' <pad> <pad> <pad>''' ) A__ : List[str] = rust_tokenizer([raw_input_str] , return_tensors=UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ).input_ids[0] A__ : str = py_tokenizer([raw_input_str] , return_tensors=UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ).input_ids[0] self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) @require_torch def __snake_case ( self ): A__ : Dict = ['''This is going to be way too long.''' * 1000, '''short example'''] A__ : str = ['''not super long but more than 5 tokens''', '''tiny'''] A__ : List[Any] = self._large_tokenizer(UpperCamelCase__ , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , return_tensors='''pt''' ) A__ : Dict = self._large_tokenizer( text_target=UpperCamelCase__ , max_length=5 , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , return_tensors='''pt''' ) assert batch.input_ids.shape == (2, 4096) assert batch.attention_mask.shape == (2, 4096) assert targets["input_ids"].shape == (2, 5) assert len(UpperCamelCase__ ) == 2 # input_ids, attention_mask. def __snake_case ( self ): A__ : Tuple = ( '''This is an example string that is used to test the original TF implementation against the HF''' ''' implementation''' ) A__ : List[str] = self._large_tokenizer(UpperCamelCase__ ).input_ids self.assertListEqual( UpperCamelCase__ , [182, 117, 142, 587, 4211, 120, 117, 263, 112, 804, 109, 856, 2_5016, 3137, 464, 109, 2_6955, 3137, 1] , )
55
import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to properly calculate the metrics on the # validation dataset when in a distributed system, and builds off the # `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _SCREAMING_SNAKE_CASE : str = 1_6 _SCREAMING_SNAKE_CASE : Tuple = 3_2 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Accelerator , __UpperCamelCase : int = 16 ) -> Optional[int]: """simple docstring""" A__ : List[str] = AutoTokenizer.from_pretrained('''bert-base-cased''' ) A__ : Optional[int] = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__UpperCamelCase : Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) A__ : int = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=__UpperCamelCase , max_length=__UpperCamelCase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): A__ : Optional[int] = datasets.map( __UpperCamelCase , batched=__UpperCamelCase , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library A__ : List[Any] = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__UpperCamelCase : Any ): # On TPU it's best to pad everything to the same length or training will be very slow. A__ : Optional[Any] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": A__ : int = 16 elif accelerator.mixed_precision != "no": A__ : Any = 8 else: A__ : Union[str, Any] = None return tokenizer.pad( __UpperCamelCase , padding='''longest''' , max_length=__UpperCamelCase , pad_to_multiple_of=__UpperCamelCase , return_tensors='''pt''' , ) # Instantiate dataloaders. A__ : Optional[int] = DataLoader( tokenized_datasets['''train'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) A__ : Tuple = DataLoader( tokenized_datasets['''validation'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('TESTING_MOCKED_DATALOADERS', None) == "1": from accelerate.test_utils.training import mocked_dataloaders _SCREAMING_SNAKE_CASE : Dict = mocked_dataloaders # noqa: F811 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : List[Any] ) -> Optional[Any]: """simple docstring""" if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , __UpperCamelCase ) == "1": A__ : List[str] = 2 # Initialize accelerator A__ : Optional[Any] = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs A__ : Tuple = config['''lr'''] A__ : Dict = int(config['''num_epochs'''] ) A__ : int = int(config['''seed'''] ) A__ : Optional[Any] = int(config['''batch_size'''] ) A__ : int = evaluate.load('''glue''' , '''mrpc''' ) # If the batch size is too big we use gradient accumulation A__ : Union[str, Any] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: A__ : List[Any] = batch_size // MAX_GPU_BATCH_SIZE A__ : Dict = MAX_GPU_BATCH_SIZE set_seed(__UpperCamelCase ) A__ , A__ : int = get_dataloaders(__UpperCamelCase , __UpperCamelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) A__ : Optional[int] = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=__UpperCamelCase ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). A__ : Tuple = model.to(accelerator.device ) # Instantiate optimizer A__ : Optional[int] = AdamW(params=model.parameters() , lr=__UpperCamelCase ) # Instantiate scheduler A__ : Any = get_linear_schedule_with_warmup( optimizer=__UpperCamelCase , num_warmup_steps=1_00 , num_training_steps=(len(__UpperCamelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. A__ , A__ , A__ , A__ , A__ : Dict = accelerator.prepare( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # Now we train the model for epoch in range(__UpperCamelCase ): model.train() for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) A__ : Dict = model(**__UpperCamelCase ) A__ : Dict = outputs.loss A__ : List[str] = loss / gradient_accumulation_steps accelerator.backward(__UpperCamelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() A__ : Optional[int] = 0 for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): A__ : Union[str, Any] = model(**__UpperCamelCase ) A__ : int = outputs.logits.argmax(dim=-1 ) A__ , A__ : Optional[Any] = accelerator.gather((predictions, batch['''labels''']) ) # New Code # # First we check if it's a distributed system if accelerator.use_distributed: # Then see if we're on the last batch of our eval dataloader if step == len(__UpperCamelCase ) - 1: # Last batch needs to be truncated on distributed systems as it contains additional samples A__ : Tuple = predictions[: len(eval_dataloader.dataset ) - samples_seen] A__ : int = references[: len(eval_dataloader.dataset ) - samples_seen] else: # Otherwise we add the number of samples seen samples_seen += references.shape[0] # All of this can be avoided if you use `Accelerator.gather_for_metrics` instead of `Accelerator.gather`: # accelerator.gather_for_metrics((predictions, batch["labels"])) metric.add_batch( predictions=__UpperCamelCase , references=__UpperCamelCase , ) A__ : Union[str, Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}:" , __UpperCamelCase ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: """simple docstring""" A__ : Tuple = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=__UpperCamelCase , default=__UpperCamelCase , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) A__ : Dict = parser.parse_args() A__ : Any = {'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": main()
55
1
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets _SCREAMING_SNAKE_CASE : Union[str, Any] = '\\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 : Tuple = '\\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 : Optional[Any] = '\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 UpperCamelCase__ ( datasets.Metric ): '''simple docstring''' def __snake_case ( self ): 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 , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = False , UpperCamelCase__ = False , UpperCamelCase__ = False , UpperCamelCase__ = False , ): A__ : List[Any] = len(references[0] ) if any(len(UpperCamelCase__ ) != references_per_prediction for refs in references ): raise ValueError('''Sacrebleu requires the same number of references for each prediction''' ) A__ : Dict = [[refs[i] for refs in references] for i in range(UpperCamelCase__ )] A__ : Optional[Any] = TER( normalized=UpperCamelCase__ , no_punct=UpperCamelCase__ , asian_support=UpperCamelCase__ , case_sensitive=UpperCamelCase__ , ) A__ : str = sb_ter.corpus_score(UpperCamelCase__ , UpperCamelCase__ ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
55
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor from ..utils import is_datasets_available from .base import PipelineTool if is_datasets_available(): from datasets import load_dataset class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "microsoft/speecht5_tts" _lowerCAmelCase = ( "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the " "text to read (in English) and returns a waveform object containing the sound." ) _lowerCAmelCase = "text_reader" _lowerCAmelCase = SpeechTaProcessor _lowerCAmelCase = SpeechTaForTextToSpeech _lowerCAmelCase = SpeechTaHifiGan _lowerCAmelCase = ["text"] _lowerCAmelCase = ["audio"] def __snake_case ( self ): if self.post_processor is None: A__ : int = '''microsoft/speecht5_hifigan''' super().setup() def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__=None ): A__ : List[Any] = self.pre_processor(text=UpperCamelCase__ , return_tensors='''pt''' , truncation=UpperCamelCase__ ) if speaker_embeddings is None: if not is_datasets_available(): raise ImportError('''Datasets needs to be installed if not passing speaker embeddings.''' ) A__ : List[Any] = load_dataset('''Matthijs/cmu-arctic-xvectors''' , split='''validation''' ) A__ : Dict = torch.tensor(embeddings_dataset[7305]['''xvector'''] ).unsqueeze(0 ) return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings} def __snake_case ( self , UpperCamelCase__ ): with torch.no_grad(): return self.model.generate_speech(**UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): with torch.no_grad(): return self.post_processor(UpperCamelCase__ ).cpu().detach()
55
1
import inspect import os import unittest import torch import accelerate from accelerate import debug_launcher from accelerate.test_utils import ( execute_subprocess_async, require_cpu, require_huggingface_suite, require_multi_gpu, require_single_gpu, ) from accelerate.utils import patch_environment @require_huggingface_suite class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): A__ : Dict = inspect.getfile(accelerate.test_utils ) A__ : Any = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''external_deps''', '''test_metrics.py'''] ) from accelerate.test_utils.scripts.external_deps import test_metrics # noqa: F401 A__ : Tuple = test_metrics @require_cpu def __snake_case ( self ): debug_launcher(self.test_metrics.main , num_processes=1 ) @require_cpu def __snake_case ( self ): debug_launcher(self.test_metrics.main ) @require_single_gpu def __snake_case ( self ): self.test_metrics.main() @require_multi_gpu def __snake_case ( self ): print(F"Found {torch.cuda.device_count()} devices." ) A__ : int = ['''torchrun''', F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(UpperCamelCase__ , env=os.environ.copy() )
55
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation _SCREAMING_SNAKE_CASE : Optional[int] = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Optional[Any] = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} _SCREAMING_SNAKE_CASE : List[str] = { 'tokenizer_file': { 'EleutherAI/gpt-neox-20b': 'https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/tokenizer.json', }, } _SCREAMING_SNAKE_CASE : Dict = { 'gpt-neox-20b': 2_0_4_8, } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = VOCAB_FILES_NAMES _lowerCAmelCase = PRETRAINED_VOCAB_FILES_MAP _lowerCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCAmelCase = ["input_ids", "attention_mask"] def __init__( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__="<|endoftext|>" , UpperCamelCase__="<|endoftext|>" , UpperCamelCase__="<|endoftext|>" , UpperCamelCase__=False , **UpperCamelCase__ , ): super().__init__( UpperCamelCase__ , UpperCamelCase__ , tokenizer_file=UpperCamelCase__ , unk_token=UpperCamelCase__ , bos_token=UpperCamelCase__ , eos_token=UpperCamelCase__ , add_prefix_space=UpperCamelCase__ , **UpperCamelCase__ , ) A__ : Optional[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , UpperCamelCase__ ) != add_prefix_space: A__ : Union[str, Any] = getattr(UpperCamelCase__ , pre_tok_state.pop('''type''' ) ) A__ : List[Any] = add_prefix_space A__ : Any = pre_tok_class(**UpperCamelCase__ ) A__ : List[Any] = add_prefix_space def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ = None ): A__ : Any = self._tokenizer.model.save(UpperCamelCase__ , name=UpperCamelCase__ ) return tuple(UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): A__ : List[str] = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) + [self.eos_token_id] ) if len(UpperCamelCase__ ) > self.model_max_length: A__ : Tuple = input_ids[-self.model_max_length :] return input_ids
55
1
def SCREAMING_SNAKE_CASE ( ) -> Optional[int]: """simple docstring""" A__ : Optional[Any] = 0 for i in range(1 , 10_01 ): total += i**i return str(__UpperCamelCase )[-10:] if __name__ == "__main__": print(solution())
55
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING _SCREAMING_SNAKE_CASE : Union[str, Any] = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : int = { '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__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "deformable_detr" _lowerCAmelCase = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=3 , UpperCamelCase__=300 , UpperCamelCase__=1024 , UpperCamelCase__=6 , UpperCamelCase__=1024 , UpperCamelCase__=8 , UpperCamelCase__=6 , UpperCamelCase__=1024 , UpperCamelCase__=8 , UpperCamelCase__=0.0 , UpperCamelCase__=True , UpperCamelCase__="relu" , UpperCamelCase__=256 , UpperCamelCase__=0.1 , UpperCamelCase__=0.0 , UpperCamelCase__=0.0 , UpperCamelCase__=0.0_2 , UpperCamelCase__=1.0 , UpperCamelCase__=True , UpperCamelCase__=False , UpperCamelCase__="sine" , UpperCamelCase__="resnet50" , UpperCamelCase__=True , UpperCamelCase__=False , UpperCamelCase__=4 , UpperCamelCase__=4 , UpperCamelCase__=4 , UpperCamelCase__=False , UpperCamelCase__=300 , UpperCamelCase__=False , UpperCamelCase__=1 , UpperCamelCase__=5 , UpperCamelCase__=2 , UpperCamelCase__=1 , UpperCamelCase__=1 , UpperCamelCase__=5 , UpperCamelCase__=2 , UpperCamelCase__=0.1 , UpperCamelCase__=0.2_5 , UpperCamelCase__=False , **UpperCamelCase__ , ): if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) A__ : int = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Union[str, Any] = backbone_config.get('''model_type''' ) A__ : Union[str, Any] = CONFIG_MAPPING[backbone_model_type] A__ : Optional[int] = config_class.from_dict(UpperCamelCase__ ) A__ : Tuple = use_timm_backbone A__ : int = backbone_config A__ : List[Any] = num_channels A__ : List[Any] = num_queries A__ : str = max_position_embeddings A__ : Tuple = d_model A__ : int = encoder_ffn_dim A__ : Union[str, Any] = encoder_layers A__ : Optional[Any] = encoder_attention_heads A__ : List[Any] = decoder_ffn_dim A__ : Tuple = decoder_layers A__ : Optional[Any] = decoder_attention_heads A__ : List[str] = dropout A__ : str = attention_dropout A__ : List[Any] = activation_dropout A__ : Any = activation_function A__ : Optional[Any] = init_std A__ : Union[str, Any] = init_xavier_std A__ : Union[str, Any] = encoder_layerdrop A__ : Optional[int] = auxiliary_loss A__ : str = position_embedding_type A__ : List[Any] = backbone A__ : Optional[Any] = use_pretrained_backbone A__ : Any = dilation # deformable attributes A__ : List[Any] = num_feature_levels A__ : List[str] = encoder_n_points A__ : int = decoder_n_points A__ : List[Any] = two_stage A__ : Dict = two_stage_num_proposals A__ : Optional[int] = 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 A__ : List[str] = class_cost A__ : List[Any] = bbox_cost A__ : Any = giou_cost # Loss coefficients A__ : List[str] = mask_loss_coefficient A__ : Union[str, Any] = dice_loss_coefficient A__ : List[Any] = bbox_loss_coefficient A__ : Tuple = giou_loss_coefficient A__ : Optional[Any] = eos_coefficient A__ : List[Any] = focal_alpha A__ : List[str] = disable_custom_kernels super().__init__(is_encoder_decoder=UpperCamelCase__ , **UpperCamelCase__ ) @property def __snake_case ( self ): return self.encoder_attention_heads @property def __snake_case ( self ): return self.d_model def __snake_case ( self ): A__ : List[str] = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: A__ : Tuple = self.backbone_config.to_dict() A__ : Optional[int] = self.__class__.model_type return output
55
1
from .constants import ( MODEL_NAME, OPTIMIZER_NAME, RNG_STATE_NAME, SAFE_WEIGHTS_INDEX_NAME, SAFE_WEIGHTS_NAME, SCALER_NAME, SCHEDULER_NAME, TORCH_LAUNCH_PARAMS, WEIGHTS_INDEX_NAME, WEIGHTS_NAME, ) from .dataclasses import ( BnbQuantizationConfig, ComputeEnvironment, CustomDtype, DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, DynamoBackend, FPaRecipeKwargs, FullyShardedDataParallelPlugin, GradientAccumulationPlugin, GradScalerKwargs, InitProcessGroupKwargs, KwargsHandler, LoggerType, MegatronLMPlugin, PrecisionType, ProjectConfiguration, RNGType, SageMakerDistributedType, TensorInformation, TorchDynamoPlugin, ) from .environment import get_int_from_env, parse_choice_from_env, parse_flag_from_env from .imports import ( get_ccl_version, is_abit_bnb_available, is_abit_bnb_available, is_aim_available, is_bfaa_available, is_bnb_available, is_botoa_available, is_ccl_available, is_comet_ml_available, is_datasets_available, is_deepspeed_available, is_fpa_available, is_ipex_available, is_megatron_lm_available, is_mlflow_available, is_mps_available, is_npu_available, is_rich_available, is_safetensors_available, is_sagemaker_available, is_tensorboard_available, is_tpu_available, is_transformers_available, is_wandb_available, is_xpu_available, ) from .modeling import ( check_device_map, check_tied_parameters_in_config, check_tied_parameters_on_same_device, compute_module_sizes, convert_file_size_to_int, dtype_byte_size, find_tied_parameters, get_balanced_memory, get_max_layer_size, get_max_memory, get_mixed_precision_context_manager, id_tensor_storage, infer_auto_device_map, load_checkpoint_in_model, load_offloaded_weights, load_state_dict, named_module_tensors, retie_parameters, set_module_tensor_to_device, shard_checkpoint, ) from .offload import ( OffloadedWeightsLoader, PrefixedDataset, extract_submodules_state_dict, load_offloaded_weight, offload_state_dict, offload_weight, save_offload_index, ) from .operations import ( broadcast, broadcast_object_list, concatenate, convert_outputs_to_fpaa, convert_to_fpaa, find_batch_size, find_device, gather, gather_object, get_data_structure, honor_type, initialize_tensors, is_namedtuple, is_tensor_information, is_torch_tensor, listify, pad_across_processes, recursively_apply, reduce, send_to_device, slice_tensors, ) from .versions import compare_versions, is_torch_version if is_deepspeed_available(): from .deepspeed import ( DeepSpeedEngineWrapper, DeepSpeedOptimizerWrapper, DeepSpeedSchedulerWrapper, DummyOptim, DummyScheduler, HfDeepSpeedConfig, ) from .bnb import has_abit_bnb_layers, load_and_quantize_model from .fsdp_utils import load_fsdp_model, load_fsdp_optimizer, save_fsdp_model, save_fsdp_optimizer from .launch import ( PrepareForLaunch, _filter_args, prepare_deepspeed_cmd_env, prepare_multi_gpu_env, prepare_sagemager_args_inputs, prepare_simple_launcher_cmd_env, prepare_tpu, ) from .megatron_lm import ( AbstractTrainStep, BertTrainStep, GPTTrainStep, MegatronEngine, MegatronLMDummyDataLoader, MegatronLMDummyScheduler, MegatronLMOptimizerWrapper, MegatronLMSchedulerWrapper, TaTrainStep, avg_losses_across_data_parallel_group, gather_across_data_parallel_groups, ) from .megatron_lm import initialize as megatron_lm_initialize from .megatron_lm import prepare_data_loader as megatron_lm_prepare_data_loader from .megatron_lm import prepare_model as megatron_lm_prepare_model from .megatron_lm import prepare_optimizer as megatron_lm_prepare_optimizer from .megatron_lm import prepare_scheduler as megatron_lm_prepare_scheduler from .memory import find_executable_batch_size, release_memory from .other import ( extract_model_from_parallel, get_pretty_name, is_port_in_use, merge_dicts, patch_environment, save, wait_for_everyone, write_basic_config, ) from .random import set_seed, synchronize_rng_state, synchronize_rng_states from .torch_xla import install_xla from .tqdm import tqdm from .transformer_engine import convert_model, has_transformer_engine_layers
55
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> List[Any]: """simple docstring""" A__ : Optional[Any] = 0 A__ : Optional[Any] = len(__UpperCamelCase ) for i in range(n - 1 ): for j in range(i + 1 , __UpperCamelCase ): if arr[i] > arr[j]: num_inversions += 1 return num_inversions def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Tuple: """simple docstring""" if len(__UpperCamelCase ) <= 1: return arr, 0 A__ : Optional[int] = len(__UpperCamelCase ) // 2 A__ : List[str] = arr[0:mid] A__ : Union[str, Any] = arr[mid:] A__ , A__ : List[Any] = count_inversions_recursive(__UpperCamelCase ) A__ , A__ : int = count_inversions_recursive(__UpperCamelCase ) A__ , A__ : Dict = _count_cross_inversions(__UpperCamelCase , __UpperCamelCase ) A__ : Any = inversion_p + inversions_q + cross_inversions return c, num_inversions def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] , __UpperCamelCase : List[Any] ) -> Dict: """simple docstring""" A__ : str = [] A__ : Tuple = 0 while i < len(__UpperCamelCase ) and j < len(__UpperCamelCase ): if p[i] > q[j]: # if P[1] > Q[j], then P[k] > Q[k] for all i < k <= len(P) # These are all inversions. The claim emerges from the # property that P is sorted. num_inversion += len(__UpperCamelCase ) - i r.append(q[j] ) j += 1 else: r.append(p[i] ) i += 1 if i < len(__UpperCamelCase ): r.extend(p[i:] ) else: r.extend(q[j:] ) return r, num_inversion def SCREAMING_SNAKE_CASE ( ) -> Tuple: """simple docstring""" A__ : List[str] = [10, 2, 1, 5, 5, 2, 11] # this arr has 8 inversions: # (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2) A__ : int = count_inversions_bf(__UpperCamelCase ) A__ , A__ : int = count_inversions_recursive(__UpperCamelCase ) assert num_inversions_bf == num_inversions_recursive == 8 print('''number of inversions = ''' , __UpperCamelCase ) # testing an array with zero inversion (a sorted arr_1) arr_a.sort() A__ : Optional[Any] = count_inversions_bf(__UpperCamelCase ) A__ , A__ : Dict = count_inversions_recursive(__UpperCamelCase ) assert num_inversions_bf == num_inversions_recursive == 0 print('''number of inversions = ''' , __UpperCamelCase ) # an empty list should also have zero inversions A__ : Union[str, Any] = [] A__ : Union[str, Any] = count_inversions_bf(__UpperCamelCase ) A__ , A__ : Any = count_inversions_recursive(__UpperCamelCase ) assert num_inversions_bf == num_inversions_recursive == 0 print('''number of inversions = ''' , __UpperCamelCase ) if __name__ == "__main__": main()
55
1
import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.local_sgd import LocalSGD ######################################################################## # This is a fully working simple example to use Accelerate # with LocalSGD, which is a method to synchronize model # parameters every K batches. It is different, but complementary # to gradient accumulation. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _SCREAMING_SNAKE_CASE : Tuple = 1_6 _SCREAMING_SNAKE_CASE : Optional[Any] = 3_2 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Accelerator , __UpperCamelCase : int = 16 ) -> Optional[Any]: """simple docstring""" A__ : Union[str, Any] = AutoTokenizer.from_pretrained('''bert-base-cased''' ) A__ : Optional[Any] = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__UpperCamelCase : Optional[int] ): # max_length=None => use the model max length (it's actually the default) A__ : int = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=__UpperCamelCase , max_length=__UpperCamelCase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): A__ : str = datasets.map( __UpperCamelCase , batched=__UpperCamelCase , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library A__ : Union[str, Any] = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__UpperCamelCase : Union[str, Any] ): # On TPU it's best to pad everything to the same length or training will be very slow. A__ : Union[str, Any] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": A__ : Union[str, Any] = 16 elif accelerator.mixed_precision != "no": A__ : Dict = 8 else: A__ : List[Any] = None return tokenizer.pad( __UpperCamelCase , padding='''longest''' , max_length=__UpperCamelCase , pad_to_multiple_of=__UpperCamelCase , return_tensors='''pt''' , ) # Instantiate dataloaders. A__ : Any = DataLoader( tokenized_datasets['''train'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) A__ : str = DataLoader( tokenized_datasets['''validation'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('TESTING_MOCKED_DATALOADERS', None) == "1": from accelerate.test_utils.training import mocked_dataloaders _SCREAMING_SNAKE_CASE : Optional[int] = mocked_dataloaders # noqa: F811 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple , __UpperCamelCase : Optional[int] ) -> int: """simple docstring""" if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , __UpperCamelCase ) == "1": A__ : Tuple = 2 # New Code # A__ : Optional[int] = int(args.gradient_accumulation_steps ) A__ : List[Any] = int(args.local_sgd_steps ) # Initialize accelerator A__ : Tuple = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , gradient_accumulation_steps=__UpperCamelCase ) if accelerator.distributed_type not in [DistributedType.NO, DistributedType.MULTI_CPU, DistributedType.MULTI_GPU]: raise NotImplementedError('''LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)''' ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs A__ : Tuple = config['''lr'''] A__ : List[Any] = int(config['''num_epochs'''] ) A__ : int = int(config['''seed'''] ) A__ : List[Any] = int(config['''batch_size'''] ) A__ : List[Any] = evaluate.load('''glue''' , '''mrpc''' ) set_seed(__UpperCamelCase ) A__ , A__ : List[Any] = get_dataloaders(__UpperCamelCase , __UpperCamelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) A__ : Dict = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=__UpperCamelCase ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). A__ : List[Any] = model.to(accelerator.device ) # Instantiate optimizer A__ : Dict = AdamW(params=model.parameters() , lr=__UpperCamelCase ) # Instantiate scheduler A__ : Optional[int] = get_linear_schedule_with_warmup( optimizer=__UpperCamelCase , num_warmup_steps=1_00 , num_training_steps=(len(__UpperCamelCase ) * num_epochs) , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. A__ , A__ , A__ , A__ , A__ : Dict = accelerator.prepare( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # Now we train the model for epoch in range(__UpperCamelCase ): model.train() with LocalSGD( accelerator=__UpperCamelCase , model=__UpperCamelCase , local_sgd_steps=__UpperCamelCase , enabled=local_sgd_steps is not None ) as local_sgd: for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) # New code # # We use the new `accumulate` context manager to perform gradient accumulation # We also currently do not support TPUs nor advise it as bugs were found on the XLA side when running our tests. with accelerator.accumulate(__UpperCamelCase ): A__ : Tuple = model(**__UpperCamelCase ) A__ : str = output.loss accelerator.backward(__UpperCamelCase ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() # LocalSGD-specific line local_sgd.step() model.eval() for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): A__ : str = model(**__UpperCamelCase ) A__ : Dict = outputs.logits.argmax(dim=-1 ) A__ , A__ : int = accelerator.gather_for_metrics((predictions, batch['''labels''']) ) metric.add_batch( predictions=__UpperCamelCase , references=__UpperCamelCase , ) A__ : List[str] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}:" , __UpperCamelCase ) def SCREAMING_SNAKE_CASE ( ) -> int: """simple docstring""" A__ : List[Any] = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=__UpperCamelCase , default=__UpperCamelCase , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) # New Code # parser.add_argument( '''--gradient_accumulation_steps''' , type=__UpperCamelCase , default=1 , help='''The number of minibatches to be ran before gradients are accumulated.''' , ) parser.add_argument( '''--local_sgd_steps''' , type=__UpperCamelCase , default=8 , help='''Number of local SGD steps or None to disable local SGD''' ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) A__ : Optional[int] = parser.parse_args() A__ : Optional[Any] = {'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": main()
55
from PIL import Image def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Image , __UpperCamelCase : float ) -> Image: """simple docstring""" def brightness(__UpperCamelCase : int ) -> float: return 1_28 + level + (c - 1_28) if not -2_5_5.0 <= level <= 2_5_5.0: raise ValueError('''level must be between -255.0 (black) and 255.0 (white)''' ) return img.point(__UpperCamelCase ) if __name__ == "__main__": # Load image with Image.open('image_data/lena.jpg') as img: # Change brightness to 100 _SCREAMING_SNAKE_CASE : Dict = change_brightness(img, 1_0_0) brigt_img.save('image_data/lena_brightness.png', format='png')
55
1
_SCREAMING_SNAKE_CASE : List[str] = 8.31_4462 # Unit - J mol-1 K-1 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : float , __UpperCamelCase : float , __UpperCamelCase : float ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or volume < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def SCREAMING_SNAKE_CASE ( __UpperCamelCase : float , __UpperCamelCase : float , __UpperCamelCase : float ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
55
import json import os import tempfile from transformers.testing_utils import check_json_file_has_correct_format class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = None def __snake_case ( self ): A__ : Dict = self.feature_extraction_class(**self.feat_extract_dict ) A__ : Tuple = json.loads(feat_extract.to_json_string() ) for key, value in self.feat_extract_dict.items(): self.assertEqual(obj[key] , UpperCamelCase__ ) def __snake_case ( self ): A__ : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: A__ : Any = os.path.join(UpperCamelCase__ , '''feat_extract.json''' ) feat_extract_first.to_json_file(UpperCamelCase__ ) A__ : Dict = self.feature_extraction_class.from_json_file(UpperCamelCase__ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def __snake_case ( self ): A__ : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: A__ : Any = feat_extract_first.save_pretrained(UpperCamelCase__ )[0] check_json_file_has_correct_format(UpperCamelCase__ ) A__ : Optional[int] = self.feature_extraction_class.from_pretrained(UpperCamelCase__ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def __snake_case ( self ): A__ : str = self.feature_extraction_class() self.assertIsNotNone(UpperCamelCase__ )
55
1
from pickle import UnpicklingError import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict from ..utils import logging _SCREAMING_SNAKE_CASE : Optional[Any] = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] , __UpperCamelCase : List[Any] ) -> Any: """simple docstring""" try: with open(__UpperCamelCase , '''rb''' ) as flax_state_f: A__ : str = from_bytes(__UpperCamelCase , flax_state_f.read() ) except UnpicklingError as e: try: with open(__UpperCamelCase ) as f: if f.read().startswith('''version''' ): raise OSError( '''You seem to have cloned a repository without having git-lfs installed. Please''' ''' install git-lfs and run `git lfs install` followed by `git lfs pull` in the''' ''' folder you cloned.''' ) else: raise ValueError from e except (UnicodeDecodeError, ValueError): raise EnvironmentError(F"Unable to convert {model_file} to Flax deserializable object. " ) return load_flax_weights_in_pytorch_model(__UpperCamelCase , __UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple , __UpperCamelCase : Union[str, Any] ) -> Optional[Any]: """simple docstring""" try: import torch # noqa: F401 except ImportError: logger.error( '''Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see''' ''' https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation''' ''' instructions.''' ) raise # check if we have bf16 weights A__ : Optional[Any] = flatten_dict(jax.tree_util.tree_map(lambda __UpperCamelCase : x.dtype == jnp.bfloataa , __UpperCamelCase ) ).values() if any(__UpperCamelCase ): # convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( '''Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` ''' '''before loading those in PyTorch model.''' ) A__ : List[str] = jax.tree_util.tree_map( lambda __UpperCamelCase : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , __UpperCamelCase ) A__ : str = '''''' A__ : Any = flatten_dict(__UpperCamelCase , sep='''.''' ) A__ : List[Any] = pt_model.state_dict() # keep track of unexpected & missing keys A__ : str = [] A__ : Optional[int] = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): A__ : Union[str, Any] = flax_key_tuple.split('''.''' ) if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4: A__ : Optional[int] = flax_key_tuple_array[:-1] + ['''weight'''] A__ : Tuple = jnp.transpose(__UpperCamelCase , (3, 2, 0, 1) ) elif flax_key_tuple_array[-1] == "kernel": A__ : Dict = flax_key_tuple_array[:-1] + ['''weight'''] A__ : str = flax_tensor.T elif flax_key_tuple_array[-1] == "scale": A__ : Optional[int] = flax_key_tuple_array[:-1] + ['''weight'''] if "time_embedding" not in flax_key_tuple_array: for i, flax_key_tuple_string in enumerate(__UpperCamelCase ): A__ : Union[str, Any] = ( flax_key_tuple_string.replace('''_0''' , '''.0''' ) .replace('''_1''' , '''.1''' ) .replace('''_2''' , '''.2''' ) .replace('''_3''' , '''.3''' ) .replace('''_4''' , '''.4''' ) .replace('''_5''' , '''.5''' ) .replace('''_6''' , '''.6''' ) .replace('''_7''' , '''.7''' ) .replace('''_8''' , '''.8''' ) .replace('''_9''' , '''.9''' ) ) A__ : str = '''.'''.join(__UpperCamelCase ) if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( F"Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected " F"to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}." ) else: # add weight to pytorch dict A__ : str = np.asarray(__UpperCamelCase ) if not isinstance(__UpperCamelCase , np.ndarray ) else flax_tensor A__ : Dict = torch.from_numpy(__UpperCamelCase ) # remove from missing keys missing_keys.remove(__UpperCamelCase ) else: # weight is not expected by PyTorch model unexpected_keys.append(__UpperCamelCase ) pt_model.load_state_dict(__UpperCamelCase ) # re-transform missing_keys to list A__ : str = list(__UpperCamelCase ) if len(__UpperCamelCase ) > 0: logger.warning( '''Some weights of the Flax model were not used when initializing the PyTorch model''' F" {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing" F" {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture" ''' (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This''' F" IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect" ''' to be exactly identical (e.g. initializing a BertForSequenceClassification model from a''' ''' FlaxBertForSequenceClassification model).''' ) if len(__UpperCamelCase ) > 0: logger.warning( F"Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly" F" initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to" ''' use it for predictions and inference.''' ) return pt_model
55
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets _SCREAMING_SNAKE_CASE : Union[str, Any] = '\\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 : Tuple = '\\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 : Optional[Any] = '\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 UpperCamelCase__ ( datasets.Metric ): '''simple docstring''' def __snake_case ( self ): 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 , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = False , UpperCamelCase__ = False , UpperCamelCase__ = False , UpperCamelCase__ = False , ): A__ : List[Any] = len(references[0] ) if any(len(UpperCamelCase__ ) != references_per_prediction for refs in references ): raise ValueError('''Sacrebleu requires the same number of references for each prediction''' ) A__ : Dict = [[refs[i] for refs in references] for i in range(UpperCamelCase__ )] A__ : Optional[Any] = TER( normalized=UpperCamelCase__ , no_punct=UpperCamelCase__ , asian_support=UpperCamelCase__ , case_sensitive=UpperCamelCase__ , ) A__ : str = sb_ter.corpus_score(UpperCamelCase__ , UpperCamelCase__ ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
55
1
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int = 10_00 ) -> int: """simple docstring""" A__ : Tuple = 3 A__ : Optional[int] = 0 while a < n: if a % 3 == 0 or a % 5 == 0: result += a elif a % 15 == 0: result -= a a += 1 return result if __name__ == "__main__": print(f"""{solution() = }""")
55
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging _SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) # TODO Update this _SCREAMING_SNAKE_CASE : Optional[int] = { 'facebook/esm-1b': 'https://huggingface.co/facebook/esm-1b/resolve/main/config.json', # See all ESM models at https://huggingface.co/models?filter=esm } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "esm" def __init__( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=768 , UpperCamelCase__=12 , UpperCamelCase__=12 , UpperCamelCase__=3072 , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=1026 , UpperCamelCase__=0.0_2 , UpperCamelCase__=1e-12 , UpperCamelCase__="absolute" , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=False , UpperCamelCase__=False , UpperCamelCase__=None , UpperCamelCase__=None , **UpperCamelCase__ , ): super().__init__(pad_token_id=UpperCamelCase__ , mask_token_id=UpperCamelCase__ , **UpperCamelCase__ ) A__ : Optional[Any] = vocab_size A__ : int = hidden_size A__ : List[str] = num_hidden_layers A__ : Tuple = num_attention_heads A__ : str = intermediate_size A__ : List[str] = hidden_dropout_prob A__ : Optional[Any] = attention_probs_dropout_prob A__ : int = max_position_embeddings A__ : List[str] = initializer_range A__ : List[Any] = layer_norm_eps A__ : int = position_embedding_type A__ : Optional[Any] = use_cache A__ : Optional[int] = emb_layer_norm_before A__ : List[str] = token_dropout A__ : Tuple = is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) A__ : List[Any] = EsmFoldConfig() elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Optional[int] = EsmFoldConfig(**UpperCamelCase__ ) A__ : int = esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) A__ : Any = get_default_vocab_list() else: A__ : Dict = vocab_list else: A__ : Optional[Any] = None A__ : Tuple = None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCamelCase__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __snake_case ( self ): A__ : Optional[int] = super().to_dict() if isinstance(self.esmfold_config , UpperCamelCase__ ): A__ : Dict = self.esmfold_config.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = None _lowerCAmelCase = True _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = 0 _lowerCAmelCase = True _lowerCAmelCase = False _lowerCAmelCase = 128 _lowerCAmelCase = None def __snake_case ( self ): if self.trunk is None: A__ : Tuple = TrunkConfig() elif isinstance(self.trunk , UpperCamelCase__ ): A__ : List[Any] = TrunkConfig(**self.trunk ) def __snake_case ( self ): A__ : Optional[int] = asdict(self ) A__ : int = self.trunk.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = 48 _lowerCAmelCase = 1_024 _lowerCAmelCase = 128 _lowerCAmelCase = 32 _lowerCAmelCase = 32 _lowerCAmelCase = 32 _lowerCAmelCase = 0 _lowerCAmelCase = 0 _lowerCAmelCase = False _lowerCAmelCase = 4 _lowerCAmelCase = 128 _lowerCAmelCase = None def __snake_case ( self ): if self.structure_module is None: A__ : str = StructureModuleConfig() elif isinstance(self.structure_module , UpperCamelCase__ ): A__ : str = StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(F"`max_recycles` should be positive, got {self.max_recycles}." ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' F" {self.sequence_state_dim} and {self.sequence_state_dim}." ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' F" {self.pairwise_state_dim} and {self.pairwise_state_dim}." ) A__ : Tuple = self.sequence_state_dim // self.sequence_head_width A__ : int = self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' F" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}." ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' F" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}." ) if self.pairwise_state_dim % 2 != 0: raise ValueError(F"`pairwise_state_dim` should be even, got {self.pairwise_state_dim}." ) if self.dropout >= 0.4: raise ValueError(F"`dropout` should not be greater than 0.4, got {self.dropout}." ) def __snake_case ( self ): A__ : List[Any] = asdict(self ) A__ : Optional[int] = self.structure_module.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = 384 _lowerCAmelCase = 128 _lowerCAmelCase = 16 _lowerCAmelCase = 128 _lowerCAmelCase = 12 _lowerCAmelCase = 4 _lowerCAmelCase = 8 _lowerCAmelCase = 0.1 _lowerCAmelCase = 8 _lowerCAmelCase = 1 _lowerCAmelCase = 2 _lowerCAmelCase = 7 _lowerCAmelCase = 10 _lowerCAmelCase = 1e-8 _lowerCAmelCase = 1e5 def __snake_case ( self ): return asdict(self ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: """simple docstring""" return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
55
1
import os import pickle import unittest from transformers import AutoTokenizer from transformers.models.bert.tokenization_bert import BertTokenizer from transformers.models.bert_japanese.tokenization_bert_japanese import ( VOCAB_FILES_NAMES, BertJapaneseTokenizer, CharacterTokenizer, JumanppTokenizer, MecabTokenizer, SudachiTokenizer, WordpieceTokenizer, ) from transformers.testing_utils import custom_tokenizers, require_jumanpp, require_sudachi from ...test_tokenization_common import TokenizerTesterMixin @custom_tokenizers class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = BertJapaneseTokenizer _lowerCAmelCase = False _lowerCAmelCase = True def __snake_case ( self ): super().setUp() A__ : Dict = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''こんにちは''', '''こん''', '''にちは''', '''ばんは''', '''##こん''', '''##にちは''', '''##ばんは''', '''世界''', '''##世界''', '''、''', '''##、''', '''。''', '''##。''', ] A__ : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) def __snake_case ( self , UpperCamelCase__ ): A__ : Any = '''こんにちは、世界。 \nこんばんは、世界。''' A__ : Dict = '''こんにちは 、 世界 。 こんばんは 、 世界 。''' return input_text, output_text def __snake_case ( self , UpperCamelCase__ ): A__ , A__ : Dict = self.get_input_output_texts(UpperCamelCase__ ) A__ : List[str] = tokenizer.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) A__ : Dict = tokenizer.decode(UpperCamelCase__ , clean_up_tokenization_spaces=UpperCamelCase__ ) return text, ids def __snake_case ( self ): pass # TODO add if relevant def __snake_case ( self ): pass # TODO add if relevant def __snake_case ( self ): pass # TODO add if relevant def __snake_case ( self ): A__ : int = self.tokenizer_class(self.vocab_file ) A__ : Any = tokenizer.tokenize('''こんにちは、世界。\nこんばんは、世界。''' ) self.assertListEqual(UpperCamelCase__ , ['''こんにちは''', '''、''', '''世界''', '''。''', '''こん''', '''##ばんは''', '''、''', '''世界''', '''。'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__ ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) def __snake_case ( self ): A__ : Any = self.tokenizer_class(self.vocab_file , word_tokenizer_type='''mecab''' ) self.assertIsNotNone(UpperCamelCase__ ) A__ : Dict = '''こんにちは、世界。\nこんばんは、世界。''' A__ : Dict = tokenizer.tokenize(UpperCamelCase__ ) self.assertListEqual(UpperCamelCase__ , ['''こんにちは''', '''、''', '''世界''', '''。''', '''こん''', '''##ばんは''', '''、''', '''世界''', '''。'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__ ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) A__ : Optional[int] = os.path.join(self.tmpdirname , '''tokenizer.bin''' ) with open(UpperCamelCase__ , '''wb''' ) as handle: pickle.dump(UpperCamelCase__ , UpperCamelCase__ ) with open(UpperCamelCase__ , '''rb''' ) as handle: A__ : Tuple = pickle.load(UpperCamelCase__ ) A__ : str = tokenizer_new.tokenize(UpperCamelCase__ ) self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): A__ : Dict = MecabTokenizer(mecab_dic='''ipadic''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップルストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', '''。'''] , ) def __snake_case ( self ): try: A__ : Tuple = MecabTokenizer(mecab_dic='''unidic_lite''' ) except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', '''。'''] , ) def __snake_case ( self ): try: A__ : Optional[int] = MecabTokenizer(mecab_dic='''unidic''' ) except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', '''。'''] , ) def __snake_case ( self ): A__ : Tuple = MecabTokenizer(do_lower_case=UpperCamelCase__ , mecab_dic='''ipadic''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップルストア''', '''で''', '''iphone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', '''。'''] , ) def __snake_case ( self ): try: A__ : Dict = MecabTokenizer( do_lower_case=UpperCamelCase__ , normalize_text=UpperCamelCase__ , mecab_option='''-d /usr/local/lib/mecab/dic/jumandic''' ) except RuntimeError: # if dict doesn't exist in the system, previous code raises this error. return self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップルストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れた''', '''\u3000''', '''。'''] , ) def __snake_case ( self ): A__ : Optional[Any] = MecabTokenizer(normalize_text=UpperCamelCase__ , mecab_dic='''ipadic''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップルストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', ''' ''', '''。'''] , ) @require_sudachi def __snake_case ( self ): A__ : Any = self.tokenizer_class(self.vocab_file , word_tokenizer_type='''sudachi''' ) self.assertIsNotNone(UpperCamelCase__ ) A__ : int = '''こんにちは、世界。\nこんばんは、世界。''' A__ : Any = tokenizer.tokenize(UpperCamelCase__ ) self.assertListEqual(UpperCamelCase__ , ['''こんにちは''', '''、''', '''世界''', '''。''', '''こん''', '''##ばんは''', '''、''', '''世界''', '''。'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__ ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) A__ : str = os.path.join(self.tmpdirname , '''tokenizer.bin''' ) with open(UpperCamelCase__ , '''wb''' ) as handle: pickle.dump(UpperCamelCase__ , UpperCamelCase__ ) with open(UpperCamelCase__ , '''rb''' ) as handle: A__ : Optional[int] = pickle.load(UpperCamelCase__ ) A__ : Any = tokenizer_new.tokenize(UpperCamelCase__ ) self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) @require_sudachi def __snake_case ( self ): A__ : str = SudachiTokenizer(sudachi_dict_type='''core''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , [''' ''', '''\t''', '''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', ''' ''', '''が''', ''' ''', ''' ''', '''\n ''', '''発売''', '''さ''', '''れ''', '''た''', ''' ''', '''。''', ''' ''', ''' '''] , ) @require_sudachi def __snake_case ( self ): A__ : Optional[Any] = SudachiTokenizer(sudachi_dict_type='''core''' , sudachi_split_mode='''A''' ) self.assertListEqual(tokenizer.tokenize('''外国人参政権''' ) , ['''外国''', '''人''', '''参政''', '''権'''] ) @require_sudachi def __snake_case ( self ): A__ : Tuple = SudachiTokenizer(sudachi_dict_type='''core''' , sudachi_split_mode='''B''' ) self.assertListEqual(tokenizer.tokenize('''外国人参政権''' ) , ['''外国人''', '''参政権'''] ) @require_sudachi def __snake_case ( self ): A__ : str = SudachiTokenizer(sudachi_dict_type='''core''' , sudachi_split_mode='''C''' ) self.assertListEqual(tokenizer.tokenize('''外国人参政権''' ) , ['''外国人参政権'''] ) @require_sudachi def __snake_case ( self ): A__ : Optional[int] = SudachiTokenizer(do_lower_case=UpperCamelCase__ , sudachi_dict_type='''core''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , [''' ''', '''\t''', '''アップル''', '''ストア''', '''で''', '''iphone''', '''8''', ''' ''', '''が''', ''' ''', ''' ''', '''\n ''', '''発売''', '''さ''', '''れ''', '''た''', ''' ''', '''。''', ''' ''', ''' '''] , ) @require_sudachi def __snake_case ( self ): A__ : Optional[int] = SudachiTokenizer(normalize_text=UpperCamelCase__ , sudachi_dict_type='''core''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , [''' ''', '''\t''', '''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', ''' ''', '''が''', ''' ''', ''' ''', '''\n ''', '''発売''', '''さ''', '''れ''', '''た''', '''\u3000''', '''。''', ''' ''', ''' '''] , ) @require_sudachi def __snake_case ( self ): A__ : Optional[int] = SudachiTokenizer(trim_whitespace=UpperCamelCase__ , sudachi_dict_type='''core''' ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れ''', '''た''', '''。'''] , ) @require_jumanpp def __snake_case ( self ): A__ : Tuple = self.tokenizer_class(self.vocab_file , word_tokenizer_type='''jumanpp''' ) self.assertIsNotNone(UpperCamelCase__ ) A__ : Dict = '''こんにちは、世界。\nこんばんは、世界。''' A__ : List[str] = tokenizer.tokenize(UpperCamelCase__ ) self.assertListEqual(UpperCamelCase__ , ['''こんにちは''', '''、''', '''世界''', '''。''', '''こん''', '''##ばんは''', '''、''', '''世界''', '''。'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__ ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) A__ : Dict = os.path.join(self.tmpdirname , '''tokenizer.bin''' ) with open(UpperCamelCase__ , '''wb''' ) as handle: pickle.dump(UpperCamelCase__ , UpperCamelCase__ ) with open(UpperCamelCase__ , '''rb''' ) as handle: A__ : Dict = pickle.load(UpperCamelCase__ ) A__ : Any = tokenizer_new.tokenize(UpperCamelCase__ ) self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) @require_jumanpp def __snake_case ( self ): A__ : Any = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''\u3000''', '''が''', '''\u3000''', '''\u3000''', '''\u3000''', '''発売''', '''さ''', '''れた''', '''\u3000''', '''。'''] , ) @require_jumanpp def __snake_case ( self ): A__ : Union[str, Any] = JumanppTokenizer(do_lower_case=UpperCamelCase__ ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iphone''', '''8''', '''\u3000''', '''が''', '''\u3000''', '''\u3000''', '''\u3000''', '''発売''', '''さ''', '''れた''', '''\u3000''', '''。'''] , ) @require_jumanpp def __snake_case ( self ): A__ : int = JumanppTokenizer(normalize_text=UpperCamelCase__ ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''ア''', '''ッ''', '''フ''', '''゚''', '''ル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''\u3000''', '''が''', '''\u3000''', '''\u3000''', '''\u3000''', '''発売''', '''さ''', '''れた''', '''\u3000''', '''。'''] , ) @require_jumanpp def __snake_case ( self ): A__ : Dict = JumanppTokenizer(trim_whitespace=UpperCamelCase__ ) self.assertListEqual( tokenizer.tokenize(''' \tアップルストアでiPhone8 が \n 発売された 。 ''' ) , ['''アップル''', '''ストア''', '''で''', '''iPhone''', '''8''', '''が''', '''発売''', '''さ''', '''れた''', '''。'''] , ) @require_jumanpp def __snake_case ( self ): A__ : str = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize('''ありがとうございますm(_ _)m見つけるのが大変です。''' ) , ['''ありがとう''', '''ございます''', '''m(_ _)m''', '''見つける''', '''の''', '''が''', '''大変です''', '''。'''] , ) def __snake_case ( self ): A__ : Tuple = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''こんにちは''', '''こん''', '''にちは''', '''ばんは''', '''##こん''', '''##にちは''', '''##ばんは'''] A__ : str = {} for i, token in enumerate(UpperCamelCase__ ): A__ : str = i A__ : Union[str, Any] = WordpieceTokenizer(vocab=UpperCamelCase__ , unk_token='''[UNK]''' ) self.assertListEqual(tokenizer.tokenize('''''' ) , [] ) self.assertListEqual(tokenizer.tokenize('''こんにちは''' ) , ['''こんにちは'''] ) self.assertListEqual(tokenizer.tokenize('''こんばんは''' ) , ['''こん''', '''##ばんは'''] ) self.assertListEqual(tokenizer.tokenize('''こんばんは こんばんにちは こんにちは''' ) , ['''こん''', '''##ばんは''', '''[UNK]''', '''こんにちは'''] ) def __snake_case ( self ): A__ : Optional[Any] = BertJapaneseTokenizer.from_pretrained('''nlp-waseda/roberta-base-japanese-with-auto-jumanpp''' ) A__ : str = tokenizer.subword_tokenizer A__ : Tuple = subword_tokenizer.tokenize('''国境 の 長い トンネル を 抜ける と 雪国 であった 。''' ) self.assertListEqual(UpperCamelCase__ , ['''▁国境''', '''▁の''', '''▁長い''', '''▁トンネル''', '''▁を''', '''▁抜ける''', '''▁と''', '''▁雪''', '''国''', '''▁であった''', '''▁。'''] ) A__ : Tuple = subword_tokenizer.tokenize('''こんばんは こんばん にち は こんにちは''' ) self.assertListEqual(UpperCamelCase__ , ['''▁こん''', '''ばん''', '''は''', '''▁こん''', '''ばん''', '''▁に''', '''ち''', '''▁は''', '''▁こんにちは'''] ) def __snake_case ( self ): A__ : Dict = self.tokenizer_class.from_pretrained('''cl-tohoku/bert-base-japanese''' ) A__ : Optional[int] = tokenizer.encode('''ありがとう。''' , add_special_tokens=UpperCamelCase__ ) A__ : Any = tokenizer.encode('''どういたしまして。''' , add_special_tokens=UpperCamelCase__ ) A__ : List[str] = tokenizer.build_inputs_with_special_tokens(UpperCamelCase__ ) A__ : List[str] = tokenizer.build_inputs_with_special_tokens(UpperCamelCase__ , UpperCamelCase__ ) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = BertJapaneseTokenizer _lowerCAmelCase = False def __snake_case ( self ): super().setUp() A__ : Optional[Any] = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''こ''', '''ん''', '''に''', '''ち''', '''は''', '''ば''', '''世''', '''界''', '''、''', '''。'''] 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 __snake_case ( self , **UpperCamelCase__ ): return BertJapaneseTokenizer.from_pretrained(self.tmpdirname , subword_tokenizer_type='''character''' , **UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): A__ : int = '''こんにちは、世界。 \nこんばんは、世界。''' A__ : Dict = '''こ ん に ち は 、 世 界 。 こ ん ば ん は 、 世 界 。''' return input_text, output_text def __snake_case ( self ): pass # TODO add if relevant def __snake_case ( self ): pass # TODO add if relevant def __snake_case ( self ): pass # TODO add if relevant def __snake_case ( self ): A__ : Tuple = self.tokenizer_class(self.vocab_file , subword_tokenizer_type='''character''' ) A__ : str = tokenizer.tokenize('''こんにちは、世界。 \nこんばんは、世界。''' ) self.assertListEqual( UpperCamelCase__ , ['''こ''', '''ん''', '''に''', '''ち''', '''は''', '''、''', '''世''', '''界''', '''。''', '''こ''', '''ん''', '''ば''', '''ん''', '''は''', '''、''', '''世''', '''界''', '''。'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(UpperCamelCase__ ) , [3, 4, 5, 6, 7, 11, 9, 10, 12, 3, 4, 8, 4, 7, 11, 9, 10, 12] ) def __snake_case ( self ): A__ : List[str] = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''こ''', '''ん''', '''に''', '''ち''', '''は''', '''ば''', '''世''', '''界''', '''、''', '''。'''] A__ : Optional[int] = {} for i, token in enumerate(UpperCamelCase__ ): A__ : Optional[Any] = i A__ : Dict = CharacterTokenizer(vocab=UpperCamelCase__ , unk_token='''[UNK]''' ) self.assertListEqual(tokenizer.tokenize('''''' ) , [] ) self.assertListEqual(tokenizer.tokenize('''こんにちは''' ) , ['''こ''', '''ん''', '''に''', '''ち''', '''は'''] ) self.assertListEqual(tokenizer.tokenize('''こんにちほ''' ) , ['''こ''', '''ん''', '''に''', '''ち''', '''[UNK]'''] ) def __snake_case ( self ): A__ : List[Any] = self.tokenizer_class.from_pretrained('''cl-tohoku/bert-base-japanese-char''' ) A__ : Any = tokenizer.encode('''ありがとう。''' , add_special_tokens=UpperCamelCase__ ) A__ : str = tokenizer.encode('''どういたしまして。''' , add_special_tokens=UpperCamelCase__ ) A__ : List[Any] = tokenizer.build_inputs_with_special_tokens(UpperCamelCase__ ) A__ : int = tokenizer.build_inputs_with_special_tokens(UpperCamelCase__ , UpperCamelCase__ ) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): A__ : Tuple = '''cl-tohoku/bert-base-japanese''' A__ : str = AutoTokenizer.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): A__ : str = '''cl-tohoku/bert-base-japanese''' with self.assertLogs('''transformers''' , level='''WARNING''' ) as cm: BertTokenizer.from_pretrained(UpperCamelCase__ ) self.assertTrue( cm.records[0].message.startswith( '''The tokenizer class you load from this checkpoint is not the same type as the class this function''' ''' is called from.''' ) ) A__ : Any = '''bert-base-cased''' with self.assertLogs('''transformers''' , level='''WARNING''' ) as cm: BertJapaneseTokenizer.from_pretrained(UpperCamelCase__ ) self.assertTrue( cm.records[0].message.startswith( '''The tokenizer class you load from this checkpoint is not the same type as the class this function''' ''' is called from.''' ) )
55
import logging import torch from accelerate import Accelerator from arguments import EvaluationArguments from datasets import load_dataset from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, set_seed class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=1024 , UpperCamelCase__=1024 , UpperCamelCase__=3.6 ): A__ : str = tokenizer A__ : int = tokenizer.bos_token_id A__ : List[Any] = dataset A__ : Tuple = seq_length A__ : Any = seq_length * chars_per_token * num_of_sequences def __iter__( self ): A__ : Dict = iter(self.dataset ) A__ : Tuple = True while more_examples: A__ , A__ : Optional[Any] = [], 0 while True: if buffer_len >= self.input_characters: break try: buffer.append(next(UpperCamelCase__ )['''content'''] ) buffer_len += len(buffer[-1] ) except StopIteration: A__ : Dict = False break A__ : str = tokenizer(UpperCamelCase__ , truncation=UpperCamelCase__ )['''input_ids'''] A__ : Optional[int] = [] for tokenized_input in tokenized_inputs: all_token_ids.extend(tokenized_input + [self.concat_token_id] ) for i in range(0 , len(UpperCamelCase__ ) , self.seq_length ): A__ : Optional[int] = all_token_ids[i : i + self.seq_length] if len(UpperCamelCase__ ) == self.seq_length: yield torch.tensor(UpperCamelCase__ ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> Any: """simple docstring""" A__ : Any = {'''streaming''': True} A__ : List[str] = load_dataset(args.dataset_name , split='''train''' , **__UpperCamelCase ) A__ : List[str] = ConstantLengthDataset(__UpperCamelCase , __UpperCamelCase , seq_length=args.seq_length ) A__ : int = DataLoader(__UpperCamelCase , batch_size=args.batch_size ) return eval_dataloader def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] ) -> Dict: """simple docstring""" model.eval() A__ : Dict = [] for step, batch in enumerate(__UpperCamelCase ): with torch.no_grad(): A__ : Any = model(__UpperCamelCase , labels=__UpperCamelCase ) A__ : Tuple = outputs.loss.repeat(args.batch_size ) losses.append(accelerator.gather(__UpperCamelCase ) ) if args.max_eval_steps > 0 and step >= args.max_eval_steps: break A__ : Tuple = torch.mean(torch.cat(__UpperCamelCase ) ) try: A__ : Optional[Any] = torch.exp(__UpperCamelCase ) except OverflowError: A__ : Union[str, Any] = float('''inf''' ) return loss.item(), perplexity.item() # Setup Accelerator _SCREAMING_SNAKE_CASE : List[Any] = Accelerator() # Parse configuration _SCREAMING_SNAKE_CASE : Optional[int] = HfArgumentParser(EvaluationArguments) _SCREAMING_SNAKE_CASE : Union[str, Any] = parser.parse_args() set_seed(args.seed) # Logging _SCREAMING_SNAKE_CASE : Dict = logging.getLogger(__name__) logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO ) # Load model and tokenizer _SCREAMING_SNAKE_CASE : Optional[int] = AutoModelForCausalLM.from_pretrained(args.model_ckpt) _SCREAMING_SNAKE_CASE : List[str] = AutoTokenizer.from_pretrained(args.model_ckpt) # Load dataset and dataloader _SCREAMING_SNAKE_CASE : Optional[Any] = create_dataloader(args) # Prepare everything with our `accelerator`. _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Optional[Any] = accelerator.prepare(model, eval_dataloader) # Evaluate and save the last checkpoint logger.info('Evaluating and saving model after training') _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Optional[int] = evaluate(args) logger.info(f"""loss/eval: {eval_loss}, perplexity: {perplexity}""")
55
1
import subprocess import sys from transformers import BertConfig, BertModel, BertTokenizer, pipeline from transformers.testing_utils import TestCasePlus, require_torch class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' @require_torch def __snake_case ( self ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched A__ : Optional[Any] = ''' from transformers import BertConfig, BertModel, BertTokenizer, pipeline ''' A__ : int = ''' mname = "hf-internal-testing/tiny-random-bert" BertConfig.from_pretrained(mname) BertModel.from_pretrained(mname) BertTokenizer.from_pretrained(mname) pipe = pipeline(task="fill-mask", model=mname) print("success") ''' A__ : Dict = ''' import socket def offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet") socket.socket = offline_socket ''' # Force fetching the files so that we can use the cache A__ : Any = '''hf-internal-testing/tiny-random-bert''' BertConfig.from_pretrained(UpperCamelCase__ ) BertModel.from_pretrained(UpperCamelCase__ ) BertTokenizer.from_pretrained(UpperCamelCase__ ) pipeline(task='''fill-mask''' , model=UpperCamelCase__ ) # baseline - just load from_pretrained with normal network A__ : List[Any] = [sys.executable, '''-c''', '''\n'''.join([load, run, mock] )] # should succeed A__ : List[Any] = self.get_env() # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files A__ : Any = '''1''' A__ : List[str] = subprocess.run(UpperCamelCase__ , env=UpperCamelCase__ , check=UpperCamelCase__ , capture_output=UpperCamelCase__ ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('''success''' , result.stdout.decode() ) @require_torch def __snake_case ( self ): # python one-liner segments # this must be loaded before socket.socket is monkey-patched A__ : str = ''' from transformers import BertConfig, BertModel, BertTokenizer, pipeline ''' A__ : List[str] = ''' mname = "hf-internal-testing/tiny-random-bert" BertConfig.from_pretrained(mname) BertModel.from_pretrained(mname) BertTokenizer.from_pretrained(mname) pipe = pipeline(task="fill-mask", model=mname) print("success") ''' A__ : str = ''' import socket def offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet") socket.socket = offline_socket ''' # Force fetching the files so that we can use the cache A__ : Any = '''hf-internal-testing/tiny-random-bert''' BertConfig.from_pretrained(UpperCamelCase__ ) BertModel.from_pretrained(UpperCamelCase__ ) BertTokenizer.from_pretrained(UpperCamelCase__ ) pipeline(task='''fill-mask''' , model=UpperCamelCase__ ) # baseline - just load from_pretrained with normal network A__ : int = [sys.executable, '''-c''', '''\n'''.join([load, run, mock] )] # should succeed A__ : Optional[int] = self.get_env() A__ : int = subprocess.run(UpperCamelCase__ , env=UpperCamelCase__ , check=UpperCamelCase__ , capture_output=UpperCamelCase__ ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('''success''' , result.stdout.decode() ) @require_torch def __snake_case ( self ): # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched A__ : List[str] = ''' from transformers import BertConfig, BertModel, BertTokenizer ''' A__ : Union[str, Any] = ''' mname = "hf-internal-testing/tiny-random-bert-sharded" BertConfig.from_pretrained(mname) BertModel.from_pretrained(mname) print("success") ''' A__ : Dict = ''' import socket def offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled") socket.socket = offline_socket ''' # baseline - just load from_pretrained with normal network A__ : int = [sys.executable, '''-c''', '''\n'''.join([load, run] )] # should succeed A__ : Dict = self.get_env() A__ : Optional[int] = subprocess.run(UpperCamelCase__ , env=UpperCamelCase__ , check=UpperCamelCase__ , capture_output=UpperCamelCase__ ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('''success''' , result.stdout.decode() ) # next emulate no network A__ : List[Any] = [sys.executable, '''-c''', '''\n'''.join([load, mock, run] )] # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this. # env["TRANSFORMERS_OFFLINE"] = "0" # result = subprocess.run(cmd, env=env, check=False, capture_output=True) # self.assertEqual(result.returncode, 1, result.stderr) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files A__ : List[Any] = '''1''' A__ : Dict = subprocess.run(UpperCamelCase__ , env=UpperCamelCase__ , check=UpperCamelCase__ , capture_output=UpperCamelCase__ ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('''success''' , result.stdout.decode() ) @require_torch def __snake_case ( self ): A__ : int = ''' from transformers import pipeline ''' A__ : str = ''' mname = "hf-internal-testing/tiny-random-bert" pipe = pipeline(model=mname) ''' A__ : Optional[int] = ''' import socket def offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled") socket.socket = offline_socket ''' A__ : int = self.get_env() A__ : Optional[int] = '''1''' A__ : int = [sys.executable, '''-c''', '''\n'''.join([load, mock, run] )] A__ : Dict = subprocess.run(UpperCamelCase__ , env=UpperCamelCase__ , check=UpperCamelCase__ , capture_output=UpperCamelCase__ ) self.assertEqual(result.returncode , 1 , result.stderr ) self.assertIn( '''You cannot infer task automatically within `pipeline` when using offline mode''' , result.stderr.decode().replace('''\n''' , '''''' ) , ) @require_torch def __snake_case ( self ): A__ : str = ''' from transformers import AutoModel ''' A__ : List[Any] = ''' mname = "hf-internal-testing/test_dynamic_model" AutoModel.from_pretrained(mname, trust_remote_code=True) print("success") ''' # baseline - just load from_pretrained with normal network A__ : int = [sys.executable, '''-c''', '''\n'''.join([load, run] )] # should succeed A__ : Tuple = self.get_env() A__ : str = subprocess.run(UpperCamelCase__ , env=UpperCamelCase__ , check=UpperCamelCase__ , capture_output=UpperCamelCase__ ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('''success''' , result.stdout.decode() ) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files A__ : Any = '''1''' A__ : Any = subprocess.run(UpperCamelCase__ , env=UpperCamelCase__ , check=UpperCamelCase__ , capture_output=UpperCamelCase__ ) self.assertEqual(result.returncode , 0 , result.stderr ) self.assertIn('''success''' , result.stdout.decode() )
55
def SCREAMING_SNAKE_CASE ( ) -> Optional[int]: """simple docstring""" A__ : Optional[Any] = 0 for i in range(1 , 10_01 ): total += i**i return str(__UpperCamelCase )[-10:] if __name__ == "__main__": print(solution())
55
1
import string def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> None: """simple docstring""" for key in range(len(string.ascii_uppercase ) ): A__ : Tuple = '''''' for symbol in message: if symbol in string.ascii_uppercase: A__ : List[Any] = string.ascii_uppercase.find(__UpperCamelCase ) A__ : Any = num - key if num < 0: A__ : Union[str, Any] = num + len(string.ascii_uppercase ) A__ : Optional[int] = translated + string.ascii_uppercase[num] else: A__ : Dict = translated + symbol print(F"Decryption using Key #{key}: {translated}" ) def SCREAMING_SNAKE_CASE ( ) -> None: """simple docstring""" A__ : Optional[Any] = input('''Encrypted message: ''' ) A__ : Tuple = message.upper() decrypt(__UpperCamelCase ) if __name__ == "__main__": import doctest doctest.testmod() main()
55
import inspect import os import unittest import torch import accelerate from accelerate import debug_launcher from accelerate.test_utils import ( execute_subprocess_async, require_cpu, require_huggingface_suite, require_multi_gpu, require_single_gpu, ) from accelerate.utils import patch_environment @require_huggingface_suite class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): A__ : Dict = inspect.getfile(accelerate.test_utils ) A__ : Any = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''external_deps''', '''test_metrics.py'''] ) from accelerate.test_utils.scripts.external_deps import test_metrics # noqa: F401 A__ : Tuple = test_metrics @require_cpu def __snake_case ( self ): debug_launcher(self.test_metrics.main , num_processes=1 ) @require_cpu def __snake_case ( self ): debug_launcher(self.test_metrics.main ) @require_single_gpu def __snake_case ( self ): self.test_metrics.main() @require_multi_gpu def __snake_case ( self ): print(F"Found {torch.cuda.device_count()} devices." ) A__ : int = ['''torchrun''', F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(UpperCamelCase__ , env=os.environ.copy() )
55
1
import unittest from accelerate import debug_launcher from accelerate.test_utils import require_cpu, test_ops, test_script @require_cpu class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): debug_launcher(test_script.main ) def __snake_case ( self ): debug_launcher(test_ops.main )
55
from numpy import exp, pi, sqrt def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict , __UpperCamelCase : float = 0.0 , __UpperCamelCase : float = 1.0 ) -> int: """simple docstring""" return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
55
1
import unittest from transformers import XLMConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMWithLMHeadModel, ) from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__=13 , UpperCamelCase__=7 , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=False , UpperCamelCase__=False , UpperCamelCase__=False , UpperCamelCase__=2 , UpperCamelCase__=99 , UpperCamelCase__=0 , UpperCamelCase__=32 , UpperCamelCase__=5 , UpperCamelCase__=4 , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=512 , UpperCamelCase__=2 , UpperCamelCase__=0.0_2 , UpperCamelCase__=2 , UpperCamelCase__=4 , UpperCamelCase__="last" , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=0 , ): A__ : str = parent A__ : Tuple = batch_size A__ : str = seq_length A__ : List[str] = is_training A__ : int = use_input_lengths A__ : Optional[Any] = use_token_type_ids A__ : Tuple = use_labels A__ : Union[str, Any] = gelu_activation A__ : List[Any] = sinusoidal_embeddings A__ : List[str] = causal A__ : Optional[int] = asm A__ : Dict = n_langs A__ : Optional[int] = vocab_size A__ : Optional[Any] = n_special A__ : str = hidden_size A__ : int = num_hidden_layers A__ : Union[str, Any] = num_attention_heads A__ : Union[str, Any] = hidden_dropout_prob A__ : str = attention_probs_dropout_prob A__ : Dict = max_position_embeddings A__ : int = type_sequence_label_size A__ : Optional[Any] = initializer_range A__ : Dict = num_labels A__ : Optional[Any] = num_choices A__ : Tuple = summary_type A__ : Any = use_proj A__ : List[Any] = scope A__ : List[str] = bos_token_id def __snake_case ( self ): A__ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ : List[str] = random_attention_mask([self.batch_size, self.seq_length] ) A__ : List[str] = None if self.use_input_lengths: A__ : Optional[int] = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length A__ : Union[str, Any] = None if self.use_token_type_ids: A__ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) A__ : Any = None A__ : Any = None A__ : str = None if self.use_labels: A__ : List[str] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A__ : Tuple = ids_tensor([self.batch_size] , 2 ).float() A__ : Any = ids_tensor([self.batch_size] , self.num_choices ) A__ : Any = self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def __snake_case ( self ): return XLMConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , num_labels=self.num_labels , bos_token_id=self.bos_token_id , ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ): A__ : int = XLMModel(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : Optional[Any] = model(UpperCamelCase__ , lengths=UpperCamelCase__ , langs=UpperCamelCase__ ) A__ : str = model(UpperCamelCase__ , langs=UpperCamelCase__ ) A__ : str = model(UpperCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ): A__ : Optional[Any] = XLMWithLMHeadModel(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : Tuple = model(UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ): A__ : List[str] = XLMForQuestionAnsweringSimple(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : Optional[int] = model(UpperCamelCase__ ) A__ : Optional[Any] = model(UpperCamelCase__ , start_positions=UpperCamelCase__ , end_positions=UpperCamelCase__ ) A__ : Any = outputs self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ): A__ : List[str] = XLMForQuestionAnswering(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : Optional[Any] = model(UpperCamelCase__ ) A__ : List[Any] = model( UpperCamelCase__ , start_positions=UpperCamelCase__ , end_positions=UpperCamelCase__ , cls_index=UpperCamelCase__ , is_impossible=UpperCamelCase__ , p_mask=UpperCamelCase__ , ) A__ : int = model( UpperCamelCase__ , start_positions=UpperCamelCase__ , end_positions=UpperCamelCase__ , cls_index=UpperCamelCase__ , is_impossible=UpperCamelCase__ , ) ((A__) , ) : Union[str, Any] = result_with_labels.to_tuple() A__ : str = model(UpperCamelCase__ , start_positions=UpperCamelCase__ , end_positions=UpperCamelCase__ ) ((A__) , ) : List[str] = result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape , () ) self.parent.assertEqual(result.start_top_log_probs.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual(result.start_top_index.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual( result.end_top_log_probs.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual( result.end_top_index.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual(result.cls_logits.shape , (self.batch_size,) ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ): A__ : Any = XLMForSequenceClassification(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : Tuple = model(UpperCamelCase__ ) A__ : Any = model(UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ): A__ : List[str] = self.num_labels A__ : Union[str, Any] = XLMForTokenClassification(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : List[Any] = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ): A__ : int = self.num_choices A__ : Any = XLMForMultipleChoice(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : Any = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() A__ : Optional[Any] = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() A__ : List[Any] = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() A__ : Optional[int] = model( UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __snake_case ( self ): A__ : Dict = self.prepare_config_and_inputs() ( ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ) : Optional[int] = config_and_inputs A__ : Any = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''lengths''': input_lengths} return config, inputs_dict @require_torch class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = ( ( XLMModel, XLMWithLMHeadModel, XLMForQuestionAnswering, XLMForSequenceClassification, XLMForQuestionAnsweringSimple, XLMForTokenClassification, XLMForMultipleChoice, ) if is_torch_available() else () ) _lowerCAmelCase = ( (XLMWithLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable _lowerCAmelCase = ( { "feature-extraction": XLMModel, "fill-mask": XLMWithLMHeadModel, "question-answering": XLMForQuestionAnsweringSimple, "text-classification": XLMForSequenceClassification, "text-generation": XLMWithLMHeadModel, "token-classification": XLMForTokenClassification, "zero-shot": XLMForSequenceClassification, } if is_torch_available() else {} ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('''Fast''' ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=False ): A__ : Union[str, Any] = super()._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ , return_labels=UpperCamelCase__ ) if return_labels: if model_class.__name__ == "XLMForQuestionAnswering": A__ : Optional[int] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCamelCase__ ) A__ : Optional[Any] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCamelCase__ ) return inputs_dict def __snake_case ( self ): A__ : Optional[Any] = XLMModelTester(self ) A__ : Optional[Any] = ConfigTester(self , config_class=UpperCamelCase__ , emb_dim=37 ) def __snake_case ( self ): self.config_tester.run_common_tests() def __snake_case ( self ): A__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_model(*UpperCamelCase__ ) def __snake_case ( self ): A__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_lm_head(*UpperCamelCase__ ) def __snake_case ( self ): A__ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_simple_qa(*UpperCamelCase__ ) def __snake_case ( self ): A__ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_qa(*UpperCamelCase__ ) def __snake_case ( self ): A__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_sequence_classif(*UpperCamelCase__ ) def __snake_case ( self ): A__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_token_classif(*UpperCamelCase__ ) def __snake_case ( self ): A__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_for_multiple_choice(*UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=False , UpperCamelCase__=1 ): self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) self.assertListEqual( [isinstance(UpperCamelCase__ , UpperCamelCase__ ) for iter_attentions in attentions] , [True] * len(UpperCamelCase__ ) ) self.assertEqual(len(UpperCamelCase__ ) , (max_length - min_length) * num_beam_groups ) for idx, iter_attentions in enumerate(UpperCamelCase__ ): # adds PAD dummy token A__ : str = min_length + idx + 1 A__ : Any = min_length + idx + 1 A__ : str = ( batch_size * num_beam_groups, config.num_attention_heads, tgt_len, src_len, ) # check attn size self.assertListEqual( [layer_attention.shape for layer_attention in iter_attentions] , [expected_shape] * len(UpperCamelCase__ ) ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=False , UpperCamelCase__=1 ): self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) self.assertListEqual( [isinstance(UpperCamelCase__ , UpperCamelCase__ ) for iter_hidden_states in hidden_states] , [True] * len(UpperCamelCase__ ) , ) self.assertEqual(len(UpperCamelCase__ ) , (max_length - min_length) * num_beam_groups ) for idx, iter_hidden_states in enumerate(UpperCamelCase__ ): # adds PAD dummy token A__ : List[Any] = min_length + idx + 1 A__ : str = (batch_size * num_beam_groups, seq_len, config.hidden_size) # check hidden size self.assertListEqual( [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states] , [expected_shape] * len(UpperCamelCase__ ) , ) pass @slow def __snake_case ( self ): for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ : Dict = XLMModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) @require_torch class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' @slow def __snake_case ( self ): A__ : str = XLMWithLMHeadModel.from_pretrained('''xlm-mlm-en-2048''' ) model.to(UpperCamelCase__ ) A__ : Union[str, Any] = torch.tensor([[14, 447]] , dtype=torch.long , device=UpperCamelCase__ ) # the president A__ : Union[str, Any] = [ 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, ] # the president the president the president the president the president the president the president the president the president the president # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference A__ : Tuple = model.generate(UpperCamelCase__ , do_sample=UpperCamelCase__ ) self.assertListEqual(output_ids[0].cpu().numpy().tolist() , UpperCamelCase__ )
55
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tensorflow_text_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _SCREAMING_SNAKE_CASE : int = { 'configuration_bert': ['BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BertConfig', 'BertOnnxConfig'], 'tokenization_bert': ['BasicTokenizer', 'BertTokenizer', 'WordpieceTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Optional[Any] = ['BertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Union[str, Any] = [ 'BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BertForMaskedLM', 'BertForMultipleChoice', 'BertForNextSentencePrediction', 'BertForPreTraining', 'BertForQuestionAnswering', 'BertForSequenceClassification', 'BertForTokenClassification', 'BertLayer', 'BertLMHeadModel', 'BertModel', 'BertPreTrainedModel', 'load_tf_weights_in_bert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Tuple = [ 'TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFBertEmbeddings', 'TFBertForMaskedLM', 'TFBertForMultipleChoice', 'TFBertForNextSentencePrediction', 'TFBertForPreTraining', 'TFBertForQuestionAnswering', 'TFBertForSequenceClassification', 'TFBertForTokenClassification', 'TFBertLMHeadModel', 'TFBertMainLayer', 'TFBertModel', 'TFBertPreTrainedModel', ] try: if not is_tensorflow_text_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Dict = ['TFBertTokenizer'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Optional[int] = [ 'FlaxBertForCausalLM', 'FlaxBertForMaskedLM', 'FlaxBertForMultipleChoice', 'FlaxBertForNextSentencePrediction', 'FlaxBertForPreTraining', 'FlaxBertForQuestionAnswering', 'FlaxBertForSequenceClassification', 'FlaxBertForTokenClassification', 'FlaxBertModel', 'FlaxBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig, BertOnnxConfig from .tokenization_bert import BasicTokenizer, BertTokenizer, WordpieceTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bert_fast import BertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bert import ( BERT_PRETRAINED_MODEL_ARCHIVE_LIST, BertForMaskedLM, BertForMultipleChoice, BertForNextSentencePrediction, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, BertForTokenClassification, BertLayer, BertLMHeadModel, BertModel, BertPreTrainedModel, load_tf_weights_in_bert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_bert import ( TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFBertEmbeddings, TFBertForMaskedLM, TFBertForMultipleChoice, TFBertForNextSentencePrediction, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertForTokenClassification, TFBertLMHeadModel, TFBertMainLayer, TFBertModel, TFBertPreTrainedModel, ) try: if not is_tensorflow_text_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bert_tf import TFBertTokenizer try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_bert import ( FlaxBertForCausalLM, FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForNextSentencePrediction, FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertModel, FlaxBertPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE : Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
55
1
from math import isqrt def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> list[int]: """simple docstring""" A__ : Optional[int] = [True] * max_number for i in range(2 , isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2 , __UpperCamelCase , __UpperCamelCase ): A__ : List[str] = False return [i for i in range(2 , __UpperCamelCase ) if is_prime[i]] def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int = 10**8 ) -> int: """simple docstring""" A__ : Optional[int] = calculate_prime_numbers(max_number // 2 ) A__ : str = 0 A__ : str = 0 A__ : List[Any] = len(__UpperCamelCase ) - 1 while left <= right: while prime_numbers[left] * prime_numbers[right] >= max_number: right -= 1 semiprimes_count += right - left + 1 left += 1 return semiprimes_count if __name__ == "__main__": print(f"""{solution() = }""")
55
import json import os import sys import tempfile import unittest from pathlib import Path from shutil import copyfile from huggingface_hub import HfFolder, Repository, create_repo, delete_repo from requests.exceptions import HTTPError import transformers from transformers import ( CONFIG_MAPPING, FEATURE_EXTRACTOR_MAPPING, PROCESSOR_MAPPING, TOKENIZER_MAPPING, AutoConfig, AutoFeatureExtractor, AutoProcessor, AutoTokenizer, BertTokenizer, ProcessorMixin, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaProcessor, ) from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test from transformers.tokenization_utils import TOKENIZER_CONFIG_FILE from transformers.utils import FEATURE_EXTRACTOR_NAME, is_tokenizers_available sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 from test_module.custom_processing import CustomProcessor # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 _SCREAMING_SNAKE_CASE : List[Any] = get_tests_dir('fixtures/dummy_feature_extractor_config.json') _SCREAMING_SNAKE_CASE : int = get_tests_dir('fixtures/vocab.json') _SCREAMING_SNAKE_CASE : Tuple = get_tests_dir('fixtures') class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"] def __snake_case ( self ): A__ : List[Any] = 0 def __snake_case ( self ): A__ : Dict = AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h''' ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Optional[Any] = WavaVecaConfig() A__ : Dict = AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h''' ) # save in new folder model_config.save_pretrained(UpperCamelCase__ ) processor.save_pretrained(UpperCamelCase__ ) A__ : Any = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: # copy relevant files copyfile(UpperCamelCase__ , os.path.join(UpperCamelCase__ , UpperCamelCase__ ) ) copyfile(UpperCamelCase__ , os.path.join(UpperCamelCase__ , '''vocab.json''' ) ) A__ : List[Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Dict = WavaVecaFeatureExtractor() A__ : Union[str, Any] = AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h''' ) A__ : Optional[int] = WavaVecaProcessor(UpperCamelCase__ , UpperCamelCase__ ) # save in new folder processor.save_pretrained(UpperCamelCase__ ) # drop `processor_class` in tokenizer with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''r''' ) as f: A__ : str = json.load(UpperCamelCase__ ) config_dict.pop('''processor_class''' ) with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''w''' ) as f: f.write(json.dumps(UpperCamelCase__ ) ) A__ : Optional[int] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Optional[int] = WavaVecaFeatureExtractor() A__ : List[Any] = AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h''' ) A__ : str = WavaVecaProcessor(UpperCamelCase__ , UpperCamelCase__ ) # save in new folder processor.save_pretrained(UpperCamelCase__ ) # drop `processor_class` in feature extractor with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''r''' ) as f: A__ : List[Any] = json.load(UpperCamelCase__ ) config_dict.pop('''processor_class''' ) with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''w''' ) as f: f.write(json.dumps(UpperCamelCase__ ) ) A__ : List[Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): with tempfile.TemporaryDirectory() as tmpdirname: A__ : Any = WavaVecaConfig(processor_class='''Wav2Vec2Processor''' ) model_config.save_pretrained(UpperCamelCase__ ) # copy relevant files copyfile(UpperCamelCase__ , os.path.join(UpperCamelCase__ , '''vocab.json''' ) ) # create emtpy sample processor with open(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , '''w''' ) as f: f.write('''{}''' ) A__ : Union[str, Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(UpperCamelCase__ ): A__ : Union[str, Any] = AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(UpperCamelCase__ ): A__ : str = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) A__ : int = AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) self.assertTrue(processor.special_attribute_present ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) A__ : List[Any] = processor.feature_extractor self.assertTrue(feature_extractor.special_attribute_present ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) A__ : List[Any] = processor.tokenizer self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) # Test we can also load the slow version A__ : Dict = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ , use_fast=UpperCamelCase__ ) A__ : int = new_processor.tokenizer self.assertTrue(new_tokenizer.special_attribute_present ) self.assertEqual(new_tokenizer.__class__.__name__ , '''NewTokenizer''' ) else: self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) def __snake_case ( self ): try: AutoConfig.register('''custom''' , UpperCamelCase__ ) AutoFeatureExtractor.register(UpperCamelCase__ , UpperCamelCase__ ) AutoTokenizer.register(UpperCamelCase__ , slow_tokenizer_class=UpperCamelCase__ ) AutoProcessor.register(UpperCamelCase__ , UpperCamelCase__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(UpperCamelCase__ ): AutoProcessor.register(UpperCamelCase__ , UpperCamelCase__ ) # Now that the config is registered, it can be used as any other config with the auto-API A__ : Any = CustomFeatureExtractor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: A__ : str = os.path.join(UpperCamelCase__ , '''vocab.txt''' ) with open(UpperCamelCase__ , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens] ) ) A__ : str = CustomTokenizer(UpperCamelCase__ ) A__ : Optional[Any] = CustomProcessor(UpperCamelCase__ , UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained(UpperCamelCase__ ) A__ : Union[str, Any] = AutoProcessor.from_pretrained(UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] if CustomConfig in PROCESSOR_MAPPING._extra_content: del PROCESSOR_MAPPING._extra_content[CustomConfig] def __snake_case ( self ): class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = False class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = False class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "AutoFeatureExtractor" _lowerCAmelCase = "AutoTokenizer" _lowerCAmelCase = False try: AutoConfig.register('''custom''' , UpperCamelCase__ ) AutoFeatureExtractor.register(UpperCamelCase__ , UpperCamelCase__ ) AutoTokenizer.register(UpperCamelCase__ , slow_tokenizer_class=UpperCamelCase__ ) AutoProcessor.register(UpperCamelCase__ , UpperCamelCase__ ) # If remote code is not set, the default is to use local classes. A__ : List[Any] = AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor''' ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) self.assertFalse(processor.special_attribute_present ) self.assertFalse(processor.feature_extractor.special_attribute_present ) self.assertFalse(processor.tokenizer.special_attribute_present ) # If remote code is disabled, we load the local ones. A__ : Any = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) self.assertFalse(processor.special_attribute_present ) self.assertFalse(processor.feature_extractor.special_attribute_present ) self.assertFalse(processor.tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub. A__ : Union[str, Any] = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=UpperCamelCase__ ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) self.assertTrue(processor.special_attribute_present ) self.assertTrue(processor.feature_extractor.special_attribute_present ) self.assertTrue(processor.tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] if CustomConfig in PROCESSOR_MAPPING._extra_content: del PROCESSOR_MAPPING._extra_content[CustomConfig] def __snake_case ( self ): A__ : str = AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) self.assertEqual(processor.__class__.__name__ , '''BertTokenizerFast''' ) def __snake_case ( self ): A__ : Union[str, Any] = AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-convnext''' ) self.assertEqual(processor.__class__.__name__ , '''ConvNextImageProcessor''' ) @is_staging_test class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"] @classmethod def __snake_case ( cls ): A__ : List[str] = TOKEN HfFolder.save_token(UpperCamelCase__ ) @classmethod def __snake_case ( cls ): try: delete_repo(token=cls._token , repo_id='''test-processor''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-processor-org''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''test-dynamic-processor''' ) except HTTPError: pass def __snake_case ( self ): A__ : Optional[Any] = WavaVecaProcessor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained( os.path.join(UpperCamelCase__ , '''test-processor''' ) , push_to_hub=UpperCamelCase__ , use_auth_token=self._token ) A__ : List[Any] = WavaVecaProcessor.from_pretrained(F"{USER}/test-processor" ) for k, v in processor.feature_extractor.__dict__.items(): self.assertEqual(UpperCamelCase__ , getattr(new_processor.feature_extractor , UpperCamelCase__ ) ) self.assertDictEqual(new_processor.tokenizer.get_vocab() , processor.tokenizer.get_vocab() ) def __snake_case ( self ): A__ : int = WavaVecaProcessor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained( os.path.join(UpperCamelCase__ , '''test-processor-org''' ) , push_to_hub=UpperCamelCase__ , use_auth_token=self._token , organization='''valid_org''' , ) A__ : List[str] = WavaVecaProcessor.from_pretrained('''valid_org/test-processor-org''' ) for k, v in processor.feature_extractor.__dict__.items(): self.assertEqual(UpperCamelCase__ , getattr(new_processor.feature_extractor , UpperCamelCase__ ) ) self.assertDictEqual(new_processor.tokenizer.get_vocab() , processor.tokenizer.get_vocab() ) def __snake_case ( self ): CustomFeatureExtractor.register_for_auto_class() CustomTokenizer.register_for_auto_class() CustomProcessor.register_for_auto_class() A__ : Optional[Any] = CustomFeatureExtractor.from_pretrained(UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: A__ : List[Any] = os.path.join(UpperCamelCase__ , '''vocab.txt''' ) with open(UpperCamelCase__ , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens] ) ) A__ : Union[str, Any] = CustomTokenizer(UpperCamelCase__ ) A__ : List[Any] = CustomProcessor(UpperCamelCase__ , UpperCamelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: create_repo(F"{USER}/test-dynamic-processor" , token=self._token ) A__ : Union[str, Any] = Repository(UpperCamelCase__ , clone_from=F"{USER}/test-dynamic-processor" , token=self._token ) processor.save_pretrained(UpperCamelCase__ ) # This has added the proper auto_map field to the feature extractor config self.assertDictEqual( processor.feature_extractor.auto_map , { '''AutoFeatureExtractor''': '''custom_feature_extraction.CustomFeatureExtractor''', '''AutoProcessor''': '''custom_processing.CustomProcessor''', } , ) # This has added the proper auto_map field to the tokenizer config with open(os.path.join(UpperCamelCase__ , '''tokenizer_config.json''' ) ) as f: A__ : Optional[int] = json.load(UpperCamelCase__ ) self.assertDictEqual( tokenizer_config['''auto_map'''] , { '''AutoTokenizer''': ['''custom_tokenization.CustomTokenizer''', None], '''AutoProcessor''': '''custom_processing.CustomProcessor''', } , ) # The code has been copied from fixtures self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase__ , '''custom_feature_extraction.py''' ) ) ) self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase__ , '''custom_tokenization.py''' ) ) ) self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase__ , '''custom_processing.py''' ) ) ) repo.push_to_hub() A__ : Tuple = AutoProcessor.from_pretrained(F"{USER}/test-dynamic-processor" , trust_remote_code=UpperCamelCase__ ) # Can't make an isinstance check because the new_processor is from the CustomProcessor class of a dynamic module self.assertEqual(new_processor.__class__.__name__ , '''CustomProcessor''' )
55
1
import os import warnings from typing import List, Optional from ...tokenization_utils_base import BatchEncoding from ...utils import logging from .configuration_rag import RagConfig _SCREAMING_SNAKE_CASE : List[Any] = logging.get_logger(__name__) class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__ ): A__ : int = question_encoder A__ : int = generator A__ : Optional[int] = self.question_encoder def __snake_case ( self , UpperCamelCase__ ): if os.path.isfile(UpperCamelCase__ ): raise ValueError(F"Provided path ({save_directory}) should be a directory, not a file" ) os.makedirs(UpperCamelCase__ , exist_ok=UpperCamelCase__ ) A__ : Optional[Any] = os.path.join(UpperCamelCase__ , '''question_encoder_tokenizer''' ) A__ : List[Any] = os.path.join(UpperCamelCase__ , '''generator_tokenizer''' ) self.question_encoder.save_pretrained(UpperCamelCase__ ) self.generator.save_pretrained(UpperCamelCase__ ) @classmethod def __snake_case ( cls , UpperCamelCase__ , **UpperCamelCase__ ): # dynamically import AutoTokenizer from ..auto.tokenization_auto import AutoTokenizer A__ : int = kwargs.pop('''config''' , UpperCamelCase__ ) if config is None: A__ : Any = RagConfig.from_pretrained(UpperCamelCase__ ) A__ : Optional[int] = AutoTokenizer.from_pretrained( UpperCamelCase__ , config=config.question_encoder , subfolder='''question_encoder_tokenizer''' ) A__ : List[Any] = AutoTokenizer.from_pretrained( UpperCamelCase__ , config=config.generator , subfolder='''generator_tokenizer''' ) return cls(question_encoder=UpperCamelCase__ , generator=UpperCamelCase__ ) def __call__( self , *UpperCamelCase__ , **UpperCamelCase__ ): return self.current_tokenizer(*UpperCamelCase__ , **UpperCamelCase__ ) def __snake_case ( self , *UpperCamelCase__ , **UpperCamelCase__ ): return self.generator.batch_decode(*UpperCamelCase__ , **UpperCamelCase__ ) def __snake_case ( self , *UpperCamelCase__ , **UpperCamelCase__ ): return self.generator.decode(*UpperCamelCase__ , **UpperCamelCase__ ) def __snake_case ( self ): A__ : Dict = self.question_encoder def __snake_case ( self ): A__ : Optional[int] = self.generator def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = "longest" , UpperCamelCase__ = None , UpperCamelCase__ = True , **UpperCamelCase__ , ): warnings.warn( '''`prepare_seq2seq_batch` is deprecated and will be removed in version 5 of 🤗 Transformers. Use the ''' '''regular `__call__` method to prepare your inputs and the tokenizer under the `with_target_tokenizer` ''' '''context manager to prepare your targets. See the documentation of your specific tokenizer for more ''' '''details''' , UpperCamelCase__ , ) if max_length is None: A__ : str = self.current_tokenizer.model_max_length A__ : Any = self( UpperCamelCase__ , add_special_tokens=UpperCamelCase__ , return_tensors=UpperCamelCase__ , max_length=UpperCamelCase__ , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , **UpperCamelCase__ , ) if tgt_texts is None: return model_inputs # Process tgt_texts if max_target_length is None: A__ : List[Any] = self.current_tokenizer.model_max_length A__ : Dict = self( text_target=UpperCamelCase__ , add_special_tokens=UpperCamelCase__ , return_tensors=UpperCamelCase__ , padding=UpperCamelCase__ , max_length=UpperCamelCase__ , truncation=UpperCamelCase__ , **UpperCamelCase__ , ) A__ : Any = labels['''input_ids'''] return model_inputs
55
from abc import ABC, abstractmethod from argparse import ArgumentParser class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' @staticmethod @abstractmethod def __snake_case ( UpperCamelCase__ ): raise NotImplementedError() @abstractmethod def __snake_case ( self ): raise NotImplementedError()
55
1
import pytest import datasets # Import fixture modules as plugins _SCREAMING_SNAKE_CASE : Dict = ['tests.fixtures.files', 'tests.fixtures.hub', 'tests.fixtures.fsspec'] def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : Optional[Any] ) -> List[str]: """simple docstring""" for item in items: if any(marker in item.keywords for marker in ['''integration''', '''unit'''] ): continue item.add_marker(pytest.mark.unit ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Union[str, Any] ) -> List[str]: """simple docstring""" config.addinivalue_line('''markers''' , '''torchaudio_latest: mark test to run with torchaudio>=0.12''' ) @pytest.fixture(autouse=__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] , __UpperCamelCase : List[str] ) -> Tuple: """simple docstring""" A__ : str = tmp_path_factory.getbasetemp() / '''cache''' A__ : str = test_hf_cache_home / '''datasets''' A__ : str = test_hf_cache_home / '''metrics''' A__ : Optional[Any] = test_hf_cache_home / '''modules''' monkeypatch.setattr('''datasets.config.HF_DATASETS_CACHE''' , str(__UpperCamelCase ) ) monkeypatch.setattr('''datasets.config.HF_METRICS_CACHE''' , str(__UpperCamelCase ) ) monkeypatch.setattr('''datasets.config.HF_MODULES_CACHE''' , str(__UpperCamelCase ) ) A__ : int = test_hf_datasets_cache / '''downloads''' monkeypatch.setattr('''datasets.config.DOWNLOADED_DATASETS_PATH''' , str(__UpperCamelCase ) ) A__ : str = test_hf_datasets_cache / '''downloads''' / '''extracted''' monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_PATH''' , str(__UpperCamelCase ) ) @pytest.fixture(autouse=__UpperCamelCase , scope='''session''' ) def SCREAMING_SNAKE_CASE ( ) -> Optional[int]: """simple docstring""" datasets.disable_progress_bar() @pytest.fixture(autouse=__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> Union[str, Any]: """simple docstring""" monkeypatch.setattr('''datasets.config.HF_UPDATE_DOWNLOAD_COUNTS''' , __UpperCamelCase ) @pytest.fixture def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> int: """simple docstring""" monkeypatch.setattr('''sqlalchemy.util.deprecations.SILENCE_UBER_WARNING''' , __UpperCamelCase )
55
import inspect import unittest from transformers import YolosConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import YolosForObjectDetection, YolosModel from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__=13 , UpperCamelCase__=[30, 30] , UpperCamelCase__=2 , UpperCamelCase__=3 , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=32 , UpperCamelCase__=5 , UpperCamelCase__=4 , UpperCamelCase__=37 , UpperCamelCase__="gelu" , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=10 , UpperCamelCase__=0.0_2 , UpperCamelCase__=3 , UpperCamelCase__=None , UpperCamelCase__=8 , UpperCamelCase__=10 , ): A__ : Optional[int] = parent A__ : List[Any] = batch_size A__ : Dict = image_size A__ : Any = patch_size A__ : Dict = num_channels A__ : List[Any] = is_training A__ : int = use_labels A__ : Any = hidden_size A__ : List[str] = num_hidden_layers A__ : Optional[int] = num_attention_heads A__ : Optional[Any] = intermediate_size A__ : str = hidden_act A__ : str = hidden_dropout_prob A__ : Optional[int] = attention_probs_dropout_prob A__ : Optional[int] = type_sequence_label_size A__ : Any = initializer_range A__ : Optional[int] = num_labels A__ : Union[str, Any] = scope A__ : Union[str, Any] = n_targets A__ : Dict = num_detection_tokens # we set the expected sequence length (which is used in several tests) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens A__ : int = (image_size[1] // patch_size) * (image_size[0] // patch_size) A__ : List[str] = num_patches + 1 + self.num_detection_tokens def __snake_case ( self ): A__ : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]] ) A__ : int = None if self.use_labels: # labels is a list of Dict (each Dict being the labels for a given example in the batch) A__ : Tuple = [] for i in range(self.batch_size ): A__ : List[Any] = {} A__ : Tuple = torch.randint( high=self.num_labels , size=(self.n_targets,) , device=UpperCamelCase__ ) A__ : Any = torch.rand(self.n_targets , 4 , device=UpperCamelCase__ ) labels.append(UpperCamelCase__ ) A__ : Union[str, Any] = self.get_config() return config, pixel_values, labels def __snake_case ( self ): return YolosConfig( 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=UpperCamelCase__ , initializer_range=self.initializer_range , num_detection_tokens=self.num_detection_tokens , num_labels=self.num_labels , ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : Tuple = YolosModel(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : Optional[Any] = model(UpperCamelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.expected_seq_len, self.hidden_size) ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : Any = YolosForObjectDetection(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() A__ : Union[str, Any] = model(pixel_values=UpperCamelCase__ ) A__ : Optional[int] = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape , (self.batch_size, self.num_detection_tokens, 4) ) A__ : Union[str, Any] = model(pixel_values=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape , (self.batch_size, self.num_detection_tokens, 4) ) def __snake_case ( self ): A__ : Optional[int] = self.prepare_config_and_inputs() A__ , A__ , A__ : Optional[Any] = config_and_inputs A__ : Optional[int] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = (YolosModel, YolosForObjectDetection) if is_torch_available() else () _lowerCAmelCase = ( {"feature-extraction": YolosModel, "object-detection": YolosForObjectDetection} if is_torch_available() else {} ) _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=False ): A__ : Optional[int] = super()._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ , return_labels=UpperCamelCase__ ) if return_labels: if model_class.__name__ == "YolosForObjectDetection": A__ : str = [] for i in range(self.model_tester.batch_size ): A__ : int = {} A__ : Dict = torch.ones( size=(self.model_tester.n_targets,) , device=UpperCamelCase__ , dtype=torch.long ) A__ : Dict = torch.ones( self.model_tester.n_targets , 4 , device=UpperCamelCase__ , dtype=torch.float ) labels.append(UpperCamelCase__ ) A__ : Dict = labels return inputs_dict def __snake_case ( self ): A__ : List[Any] = YolosModelTester(self ) A__ : List[str] = ConfigTester(self , config_class=UpperCamelCase__ , has_text_modality=UpperCamelCase__ , hidden_size=37 ) def __snake_case ( self ): self.config_tester.run_common_tests() def __snake_case ( self ): # YOLOS does not use inputs_embeds pass def __snake_case ( self ): A__ , A__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : Any = model_class(UpperCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A__ : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCamelCase__ , nn.Linear ) ) def __snake_case ( self ): A__ , A__ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : List[str] = model_class(UpperCamelCase__ ) A__ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ : Optional[int] = [*signature.parameters.keys()] A__ : Optional[Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , UpperCamelCase__ ) def __snake_case ( self ): A__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase__ ) def __snake_case ( self ): A__ , A__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() A__ : Tuple = True # in YOLOS, the seq_len is different A__ : List[Any] = self.model_tester.expected_seq_len for model_class in self.all_model_classes: A__ : Any = True A__ : Optional[int] = False A__ : Optional[Any] = True A__ : int = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : List[str] = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Optional[int] = outputs.attentions self.assertEqual(len(UpperCamelCase__ ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] A__ : Tuple = True A__ : Optional[Any] = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : Tuple = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Tuple = outputs.attentions self.assertEqual(len(UpperCamelCase__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) A__ : List[Any] = len(UpperCamelCase__ ) # Check attention is always last and order is fine A__ : List[str] = True A__ : List[Any] = True A__ : int = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : Tuple = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Tuple = 1 self.assertEqual(out_len + added_hidden_states , len(UpperCamelCase__ ) ) A__ : List[str] = outputs.attentions self.assertEqual(len(UpperCamelCase__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) def __snake_case ( self ): def check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : str = model_class(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() with torch.no_grad(): A__ : int = model(**self._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ ) ) A__ : Optional[Any] = outputs.hidden_states A__ : int = getattr( self.model_tester , '''expected_num_hidden_layers''' , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(UpperCamelCase__ ) , UpperCamelCase__ ) # YOLOS has a different seq_length A__ : Union[str, Any] = self.model_tester.expected_seq_len self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) A__ , A__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : int = True check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ : Optional[int] = True check_hidden_states_output(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __snake_case ( self ): A__ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_object_detection(*UpperCamelCase__ ) @slow def __snake_case ( self ): for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ : Union[str, Any] = YolosModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) def SCREAMING_SNAKE_CASE ( ) -> List[str]: """simple docstring""" A__ : 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 __snake_case ( self ): return AutoImageProcessor.from_pretrained('''hustvl/yolos-small''' ) if is_vision_available() else None @slow def __snake_case ( self ): A__ : Tuple = YolosForObjectDetection.from_pretrained('''hustvl/yolos-small''' ).to(UpperCamelCase__ ) A__ : str = self.default_image_processor A__ : Tuple = prepare_img() A__ : Tuple = image_processor(images=UpperCamelCase__ , return_tensors='''pt''' ).to(UpperCamelCase__ ) # forward pass with torch.no_grad(): A__ : Any = model(inputs.pixel_values ) # verify outputs A__ : List[Any] = torch.Size((1, 100, 92) ) self.assertEqual(outputs.logits.shape , UpperCamelCase__ ) A__ : Optional[int] = torch.tensor( [[-2_4.0_2_4_8, -1_0.3_0_2_4, -1_4.8_2_9_0], [-4_2.0_3_9_2, -1_6.8_2_0_0, -2_7.4_3_3_4], [-2_7.2_7_4_3, -1_1.8_1_5_4, -1_8.7_1_4_8]] , device=UpperCamelCase__ , ) A__ : Optional[int] = torch.tensor( [[0.2_5_5_9, 0.5_4_5_5, 0.4_7_0_6], [0.2_9_8_9, 0.7_2_7_9, 0.1_8_7_5], [0.7_7_3_2, 0.4_0_1_7, 0.4_4_6_2]] , device=UpperCamelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , UpperCamelCase__ , atol=1e-4 ) ) self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3] , UpperCamelCase__ , atol=1e-4 ) ) # verify postprocessing A__ : Dict = image_processor.post_process_object_detection( UpperCamelCase__ , threshold=0.3 , target_sizes=[image.size[::-1]] )[0] A__ : int = torch.tensor([0.9_9_9_4, 0.9_7_9_0, 0.9_9_6_4, 0.9_9_7_2, 0.9_8_6_1] ).to(UpperCamelCase__ ) A__ : str = [75, 75, 17, 63, 17] A__ : Tuple = torch.tensor([3_3_5.0_6_0_9, 7_9.3_8_4_8, 3_7_5.4_2_1_6, 1_8_7.2_4_9_5] ).to(UpperCamelCase__ ) self.assertEqual(len(results['''scores'''] ) , 5 ) self.assertTrue(torch.allclose(results['''scores'''] , UpperCamelCase__ , atol=1e-4 ) ) self.assertSequenceEqual(results['''labels'''].tolist() , UpperCamelCase__ ) self.assertTrue(torch.allclose(results['''boxes'''][0, :] , UpperCamelCase__ ) )
55
1
import functools def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str , __UpperCamelCase : str ) -> int: """simple docstring""" A__ : Dict = len(__UpperCamelCase ) A__ : str = len(__UpperCamelCase ) @functools.cache def min_distance(__UpperCamelCase : int , __UpperCamelCase : int ) -> 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 A__ : Union[str, Any] = int(worda[indexa] != worda[indexa] ) # current letters not identical return min( 1 + min_distance(indexa + 1 , __UpperCamelCase ) , 1 + min_distance(__UpperCamelCase , indexa + 1 ) , diff + min_distance(indexa + 1 , indexa + 1 ) , ) return min_distance(0 , 0 ) if __name__ == "__main__": import doctest doctest.testmod()
55
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> int: """simple docstring""" if n == 1 or not isinstance(__UpperCamelCase , __UpperCamelCase ): return 0 elif n == 2: return 1 else: A__ : Any = [0, 1] for i in range(2 , n + 1 ): sequence.append(sequence[i - 1] + sequence[i - 2] ) return sequence[n] def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> int: """simple docstring""" A__ : Dict = 0 A__ : Optional[int] = 2 while digits < n: index += 1 A__ : Dict = len(str(fibonacci(__UpperCamelCase ) ) ) return index def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int = 10_00 ) -> int: """simple docstring""" return fibonacci_digits_index(__UpperCamelCase ) if __name__ == "__main__": print(solution(int(str(input()).strip())))
55
1
from __future__ import annotations import random import unittest from transformers import TransfoXLConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, TFTransfoXLForSequenceClassification, TFTransfoXLLMHeadModel, TFTransfoXLModel, ) class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ , ): A__ : Optional[Any] = parent A__ : int = 13 A__ : List[str] = 7 A__ : int = 30 A__ : int = self.seq_length + self.mem_len A__ : int = 15 A__ : Union[str, Any] = True A__ : List[str] = True A__ : Optional[int] = 99 A__ : Optional[Any] = [10, 50, 80] A__ : Optional[Any] = 32 A__ : str = 32 A__ : Union[str, Any] = 4 A__ : Tuple = 8 A__ : Optional[Any] = 128 A__ : Tuple = 2 A__ : Any = 2 A__ : List[str] = None A__ : int = 1 A__ : Optional[int] = 0 A__ : int = 3 A__ : Optional[int] = self.vocab_size - 1 A__ : int = 0.0_1 def __snake_case ( self ): A__ : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ : str = None if self.use_labels: A__ : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ : Dict = TransfoXLConfig( vocab_size=self.vocab_size , mem_len=self.mem_len , clamp_len=self.clamp_len , cutoffs=self.cutoffs , d_model=self.hidden_size , d_embed=self.d_embed , n_head=self.num_attention_heads , d_head=self.d_head , d_inner=self.d_inner , div_val=self.div_val , n_layer=self.num_hidden_layers , eos_token_id=self.eos_token_id , pad_token_id=self.vocab_size - 1 , init_range=self.init_range , num_labels=self.num_labels , ) return (config, input_ids_a, input_ids_a, lm_labels) def __snake_case ( self ): random.seed(self.seed ) tf.random.set_seed(self.seed ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : Union[str, Any] = TFTransfoXLModel(UpperCamelCase__ ) A__ , A__ : Tuple = model(UpperCamelCase__ ).to_tuple() A__ : Optional[Any] = {'''input_ids''': input_ids_a, '''mems''': mems_a} A__ , A__ : int = model(UpperCamelCase__ ).to_tuple() self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : Optional[Any] = TFTransfoXLLMHeadModel(UpperCamelCase__ ) A__ , A__ : Optional[int] = model(UpperCamelCase__ ).to_tuple() A__ : List[Any] = {'''input_ids''': input_ids_a, '''labels''': lm_labels} A__ , A__ : Tuple = model(UpperCamelCase__ ).to_tuple() A__ , A__ : Optional[int] = model([input_ids_a, mems_a] ).to_tuple() A__ : Any = {'''input_ids''': input_ids_a, '''mems''': mems_a, '''labels''': lm_labels} A__ , A__ : List[str] = model(UpperCamelCase__ ).to_tuple() self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : Tuple = TFTransfoXLForSequenceClassification(UpperCamelCase__ ) A__ : Tuple = model(UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __snake_case ( self ): A__ : int = self.prepare_config_and_inputs() ((A__) , (A__) , (A__) , (A__)) : Dict = config_and_inputs A__ : List[str] = {'''input_ids''': input_ids_a} return config, inputs_dict @require_tf class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = ( (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else () ) _lowerCAmelCase = () if is_tf_available() else () _lowerCAmelCase = ( { "feature-extraction": TFTransfoXLModel, "text-classification": TFTransfoXLForSequenceClassification, "text-generation": TFTransfoXLLMHeadModel, "zero-shot": TFTransfoXLForSequenceClassification, } if is_tf_available() else {} ) # TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): if pipeline_test_casse_name == "TextGenerationPipelineTests": # Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`. # `TransfoXLConfig` was never used in pipeline tests: cannot create a simple # tokenizer. return True return False def __snake_case ( self ): A__ : str = TFTransfoXLModelTester(self ) A__ : List[Any] = ConfigTester(self , config_class=UpperCamelCase__ , d_embed=37 ) def __snake_case ( self ): self.config_tester.run_common_tests() def __snake_case ( self ): self.model_tester.set_seed() A__ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_model(*UpperCamelCase__ ) def __snake_case ( self ): self.model_tester.set_seed() A__ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_lm_head(*UpperCamelCase__ ) def __snake_case ( self ): A__ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*UpperCamelCase__ ) def __snake_case ( self ): A__ , A__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() A__ : Tuple = [TFTransfoXLForSequenceClassification] for model_class in self.all_model_classes: A__ : Any = model_class(UpperCamelCase__ ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class in list_other_models_with_output_ebd: A__ : Any = model.get_output_embeddings() assert isinstance(UpperCamelCase__ , tf.keras.layers.Layer ) A__ : Union[str, Any] = model.get_bias() assert name is None else: A__ : Any = model.get_output_embeddings() assert x is None A__ : Optional[Any] = model.get_bias() assert name is None def __snake_case ( self ): # TODO JP: Make TransfoXL XLA compliant pass @slow def __snake_case ( self ): for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ : Optional[Any] = TFTransfoXLModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) @unittest.skip(reason='''This model doesn\'t play well with fit() due to not returning a single loss.''' ) def __snake_case ( self ): pass @require_tf class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' @unittest.skip('''Skip test until #12651 is resolved.''' ) @slow def __snake_case ( self ): A__ : Any = TFTransfoXLLMHeadModel.from_pretrained('''transfo-xl-wt103''' ) # fmt: off A__ : List[str] = tf.convert_to_tensor([[33,1297,2,1,1009,4,1109,1_1739,4762,358,5,25,245,22,1706,17,2_0098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,7_1477,2_0098,10_4447,2,2_0961,1,2604,4,1,329,3,6224,831,1_6002,2,8,603,7_8967,2_9546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,2_9546,54,8,3609,5,5_7211,49,4,1,277,18,8,1755,1_5691,3,341,25,416,693,4_2573,71,17,401,94,31,1_7919,2,2_9546,7873,18,1,435,23,1_1011,755,5,5167,3,7983,98,84,2,2_9546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,2_9546,824,1400,1868,2,19,160,2,311,8,5496,2,2_0920,17,25,1_5097,3,24,24,0]] , dtype=tf.intaa ) # noqa: E231 # fmt: on # In 1991 , the remains of Russian Tsar Nicholas II and his family # ( except for Alexei and Maria ) are discovered . # The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the # remainder of the story . 1883 Western Siberia , # a young Grigori Rasputin is asked by his father and a group of men to perform magic . # Rasputin has a vision and denounces one of the men as a horse thief . Although his # father initially slaps him for making such an accusation , Rasputin watches as the # man is chased outside and beaten . Twenty years later , Rasputin sees a vision of # the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous , # with people , even a bishop , begging for his blessing . <eod> </s> <eos> # fmt: off A__ : Dict = [33,1297,2,1,1009,4,1109,1_1739,4762,358,5,25,245,22,1706,17,2_0098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,7_1477,2_0098,10_4447,2,2_0961,1,2604,4,1,329,3,6224,831,1_6002,2,8,603,7_8967,2_9546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,2_9546,54,8,3609,5,5_7211,49,4,1,277,18,8,1755,1_5691,3,341,25,416,693,4_2573,71,17,401,94,31,1_7919,2,2_9546,7873,18,1,435,23,1_1011,755,5,5167,3,7983,98,84,2,2_9546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,2_9546,824,1400,1868,2,19,160,2,311,8,5496,2,2_0920,17,25,1_5097,3,24,24,0,33,1,1857,2,1,1009,4,1109,1_1739,4762,358,5,25,245,28,1110,3,13,1041,4,24,603,490,2,7_1477,2_0098,10_4447,2,2_0961,1,2604,4,1,329,3,0] # noqa: E231 # fmt: on # In 1991, the remains of Russian Tsar Nicholas II and his family ( # except for Alexei and Maria ) are discovered. The voice of young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story. # 1883 Western Siberia, a young Grigori Rasputin is asked by his father # and a group of men to perform magic. Rasputin has a vision and # denounces one of the men as a horse thief. Although his father initially # slaps him for making such an accusation, Rasputin watches as the man # is chased outside and beaten. Twenty years later, Rasputin sees a vision # of the Virgin Mary, prompting him to become a priest. # Rasputin quickly becomes famous, with people, even a bishop, begging for # his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar # Nicholas II and his family were discovered. The voice of <unk> young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos> A__ : Union[str, Any] = model.generate(UpperCamelCase__ , max_length=200 , do_sample=UpperCamelCase__ ) self.assertListEqual(output_ids[0].numpy().tolist() , UpperCamelCase__ )
55
_SCREAMING_SNAKE_CASE : List[str] = range(2, 2_0 + 1) _SCREAMING_SNAKE_CASE : Optional[Any] = [1_0**k for k in range(ks[-1] + 1)] _SCREAMING_SNAKE_CASE : dict[int, dict[int, list[list[int]]]] = {} def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any , __UpperCamelCase : Dict , __UpperCamelCase : Any , __UpperCamelCase : Optional[Any] ) -> int: """simple docstring""" A__ : Tuple = sum(a_i[j] for j in range(__UpperCamelCase , len(__UpperCamelCase ) ) ) A__ : Tuple = sum(a_i[j] * base[j] for j in range(min(len(__UpperCamelCase ) , __UpperCamelCase ) ) ) A__ , A__ : Optional[int] = 0, 0 A__ : List[Any] = n - i A__ : Any = memo.get(__UpperCamelCase ) if sub_memo is not None: A__ : Optional[int] = sub_memo.get(__UpperCamelCase ) if jumps is not None and len(__UpperCamelCase ) > 0: # find and make the largest jump without going over A__ : List[Any] = -1 for _k in range(len(__UpperCamelCase ) - 1 , -1 , -1 ): if jumps[_k][2] <= k and jumps[_k][1] <= max_dn: A__ : List[str] = _k break if max_jump >= 0: A__ , A__ , A__ : List[Any] = jumps[max_jump] # since the difference between jumps is cached, add c A__ : int = diff + c for j in range(min(__UpperCamelCase , len(__UpperCamelCase ) ) ): A__ , A__ : List[str] = divmod(__UpperCamelCase , 10 ) if new_c > 0: add(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else: A__ : List[Any] = [] else: A__ : Optional[Any] = {c: []} A__ : int = sub_memo if dn >= max_dn or c + diff >= base[k]: return diff, dn if k > ks[0]: while True: # keep doing smaller jumps A__ , A__ : str = next_term(__UpperCamelCase , k - 1 , i + dn , __UpperCamelCase ) diff += _diff dn += terms_jumped if dn >= max_dn or c + diff >= base[k]: break else: # would be too small a jump, just compute sequential terms instead A__ , A__ : str = compute(__UpperCamelCase , __UpperCamelCase , i + dn , __UpperCamelCase ) diff += _diff dn += terms_jumped A__ : str = sub_memo[c] # keep jumps sorted by # of terms skipped A__ : List[Any] = 0 while j < len(__UpperCamelCase ): if jumps[j][1] > dn: break j += 1 # cache the jump for this value digitsum(b) and c sub_memo[c].insert(__UpperCamelCase , (diff, dn, k) ) return (diff, dn) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : Optional[int] , __UpperCamelCase : List[str] , __UpperCamelCase : int ) -> Any: """simple docstring""" if i >= n: return 0, i if k > len(__UpperCamelCase ): a_i.extend([0 for _ in range(k - len(__UpperCamelCase ) )] ) # note: a_i -> b * 10^k + c # ds_b -> digitsum(b) # ds_c -> digitsum(c) A__ : Optional[Any] = i A__ , A__ , A__ : Dict = 0, 0, 0 for j in range(len(__UpperCamelCase ) ): if j >= k: ds_b += a_i[j] else: ds_c += a_i[j] while i < n: i += 1 A__ : int = ds_c + ds_b diff += addend A__ : List[Any] = 0 for j in range(__UpperCamelCase ): A__ : Optional[Any] = a_i[j] + addend A__ , A__ : List[str] = divmod(__UpperCamelCase , 10 ) ds_c += a_i[j] if addend > 0: break if addend > 0: add(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) return diff, i - start_i def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict , __UpperCamelCase : List[Any] , __UpperCamelCase : int ) -> Tuple: """simple docstring""" for j in range(__UpperCamelCase , len(__UpperCamelCase ) ): A__ : Any = digits[j] + addend if s >= 10: A__ , A__ : Union[str, Any] = divmod(__UpperCamelCase , 10 ) A__ : Optional[int] = addend // 10 + quotient else: A__ : Any = s A__ : Dict = addend // 10 if addend == 0: break while addend > 0: A__ , A__ : Dict = divmod(__UpperCamelCase , 10 ) digits.append(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int = 10**15 ) -> int: """simple docstring""" A__ : List[Any] = [1] A__ : Dict = 1 A__ : Tuple = 0 while True: A__ , A__ : List[str] = next_term(__UpperCamelCase , 20 , i + dn , __UpperCamelCase ) dn += terms_jumped if dn == n - i: break A__ : List[str] = 0 for j in range(len(__UpperCamelCase ) ): a_n += digits[j] * 10**j return a_n if __name__ == "__main__": print(f"""{solution() = }""")
55
1
class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ ): A__ : int = val A__ : Optional[Any] = None A__ : str = None def __snake_case ( self , UpperCamelCase__ ): if self.val: if val < self.val: if self.left is None: A__ : str = Node(UpperCamelCase__ ) else: self.left.insert(UpperCamelCase__ ) elif val > self.val: if self.right is None: A__ : Dict = Node(UpperCamelCase__ ) else: self.right.insert(UpperCamelCase__ ) else: A__ : List[str] = val def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] , __UpperCamelCase : Optional[Any] ) -> int: """simple docstring""" if root: inorder(root.left , __UpperCamelCase ) res.append(root.val ) inorder(root.right , __UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] ) -> Tuple: """simple docstring""" if len(__UpperCamelCase ) == 0: return arr A__ : Any = Node(arr[0] ) for i in range(1 , len(__UpperCamelCase ) ): root.insert(arr[i] ) # Traverse BST in order. A__ : Optional[Any] = [] inorder(__UpperCamelCase , __UpperCamelCase ) return res if __name__ == "__main__": print(tree_sort([1_0, 1, 3, 2, 9, 1_4, 1_3]))
55
import asyncio import os import shutil import subprocess import sys import tempfile import unittest from distutils.util import strtobool from functools import partial from pathlib import Path from typing import List, Union from unittest import mock import torch from ..state import AcceleratorState, PartialState from ..utils import ( gather, is_bnb_available, is_comet_ml_available, is_datasets_available, is_deepspeed_available, is_mps_available, is_safetensors_available, is_tensorboard_available, is_torch_version, is_tpu_available, is_transformers_available, is_wandb_available, is_xpu_available, ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] , __UpperCamelCase : int=False ) -> Tuple: """simple docstring""" try: A__ : Dict = os.environ[key] except KeyError: # KEY isn't set, default to `default`. A__ : Tuple = default else: # KEY is set, convert it to True or False. try: A__ : Union[str, Any] = strtobool(__UpperCamelCase ) except ValueError: # More values are supported, but let's keep the message simple. raise ValueError(F"If set, {key} must be yes or no." ) return _value _SCREAMING_SNAKE_CASE : Union[str, Any] = parse_flag_from_env('RUN_SLOW', default=False) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] ) -> Any: """simple docstring""" return unittest.skip('''Test was skipped''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> Union[str, Any]: """simple docstring""" return unittest.skipUnless(_run_slow_tests , '''test is slow''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> int: """simple docstring""" return unittest.skipUnless(not torch.cuda.is_available() , '''test requires only a CPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] ) -> Tuple: """simple docstring""" return unittest.skipUnless(torch.cuda.is_available() , '''test requires a GPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict ) -> List[str]: """simple docstring""" return unittest.skipUnless(is_xpu_available() , '''test requires a XPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Dict ) -> Any: """simple docstring""" return unittest.skipUnless(is_mps_available() , '''test requires a `mps` backend support in `torch`''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Optional[Any]: """simple docstring""" return unittest.skipUnless( is_transformers_available() and is_datasets_available() , '''test requires the Hugging Face suite''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> Tuple: """simple docstring""" return unittest.skipUnless(is_bnb_available() , '''test requires the bitsandbytes library''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> List[Any]: """simple docstring""" return unittest.skipUnless(is_tpu_available() , '''test requires TPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Tuple: """simple docstring""" return unittest.skipUnless(torch.cuda.device_count() == 1 , '''test requires a GPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Dict: """simple docstring""" return unittest.skipUnless(torch.xpu.device_count() == 1 , '''test requires a XPU''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> str: """simple docstring""" return unittest.skipUnless(torch.cuda.device_count() > 1 , '''test requires multiple GPUs''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Any: """simple docstring""" return unittest.skipUnless(torch.xpu.device_count() > 1 , '''test requires multiple XPUs''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> int: """simple docstring""" return unittest.skipUnless(is_safetensors_available() , '''test requires safetensors''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> Optional[Any]: """simple docstring""" return unittest.skipUnless(is_deepspeed_available() , '''test requires DeepSpeed''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> List[Any]: """simple docstring""" return unittest.skipUnless(is_torch_version('''>=''' , '''1.12.0''' ) , '''test requires torch version >= 1.12.0''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[int]=None , __UpperCamelCase : List[Any]=None ) -> Optional[Any]: """simple docstring""" if test_case is None: return partial(__UpperCamelCase , version=__UpperCamelCase ) return unittest.skipUnless(is_torch_version('''>=''' , __UpperCamelCase ) , F"test requires torch version >= {version}" )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] ) -> Optional[int]: """simple docstring""" return unittest.skipUnless(is_tensorboard_available() , '''test requires Tensorboard''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> Tuple: """simple docstring""" return unittest.skipUnless(is_wandb_available() , '''test requires wandb''' )(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Tuple ) -> Any: """simple docstring""" return unittest.skipUnless(is_comet_ml_available() , '''test requires comet_ml''' )(__UpperCamelCase ) _SCREAMING_SNAKE_CASE : Union[str, Any] = ( any([is_wandb_available(), is_tensorboard_available()]) and not is_comet_ml_available() ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[int] ) -> Optional[Any]: """simple docstring""" return unittest.skipUnless( _atleast_one_tracker_available , '''test requires at least one tracker to be available and for `comet_ml` to not be installed''' , )(__UpperCamelCase ) class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = True @classmethod def __snake_case ( cls ): A__ : Tuple = tempfile.mkdtemp() @classmethod def __snake_case ( cls ): if os.path.exists(cls.tmpdir ): shutil.rmtree(cls.tmpdir ) def __snake_case ( self ): if self.clear_on_setup: for path in Path(self.tmpdir ).glob('''**/*''' ): if path.is_file(): path.unlink() elif path.is_dir(): shutil.rmtree(UpperCamelCase__ ) class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): super().tearDown() # Reset the state of the AcceleratorState singleton. AcceleratorState._reset_state() PartialState._reset_state() class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self , UpperCamelCase__ ): A__ : Tuple = mocks if isinstance(UpperCamelCase__ , (tuple, list) ) else [mocks] for m in self.mocks: m.start() self.addCleanup(m.stop ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> Any: """simple docstring""" A__ : int = AcceleratorState() A__ : Any = tensor[None].clone().to(state.device ) A__ : Optional[int] = gather(__UpperCamelCase ).cpu() A__ : Any = tensor[0].cpu() for i in range(tensors.shape[0] ): if not torch.equal(tensors[i] , __UpperCamelCase ): return False return True class UpperCamelCase__ : '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): A__ : List[Any] = returncode A__ : Union[str, Any] = stdout A__ : Dict = stderr async def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str , __UpperCamelCase : Optional[Any] ) -> Any: """simple docstring""" while True: A__ : Tuple = await stream.readline() if line: callback(__UpperCamelCase ) else: break async def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] , __UpperCamelCase : Optional[Any]=None , __UpperCamelCase : List[Any]=None , __UpperCamelCase : Tuple=None , __UpperCamelCase : Tuple=False , __UpperCamelCase : List[Any]=False ) -> _RunOutput: """simple docstring""" if echo: print('''\nRunning: ''' , ''' '''.join(__UpperCamelCase ) ) A__ : int = await asyncio.create_subprocess_exec( cmd[0] , *cmd[1:] , stdin=__UpperCamelCase , stdout=asyncio.subprocess.PIPE , stderr=asyncio.subprocess.PIPE , env=__UpperCamelCase , ) # note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe # https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait # # If it starts hanging, will need to switch to the following code. The problem is that no data # will be seen until it's done and if it hangs for example there will be no debug info. # out, err = await p.communicate() # return _RunOutput(p.returncode, out, err) A__ : List[Any] = [] A__ : str = [] def tee(__UpperCamelCase : Optional[Any] , __UpperCamelCase : Union[str, Any] , __UpperCamelCase : Dict , __UpperCamelCase : List[Any]="" ): A__ : Optional[Any] = line.decode('''utf-8''' ).rstrip() sink.append(__UpperCamelCase ) if not quiet: print(__UpperCamelCase , __UpperCamelCase , file=__UpperCamelCase ) # XXX: the timeout doesn't seem to make any difference here await asyncio.wait( [ asyncio.create_task(_read_stream(p.stdout , lambda __UpperCamelCase : tee(__UpperCamelCase , __UpperCamelCase , sys.stdout , label='''stdout:''' ) ) ), asyncio.create_task(_read_stream(p.stderr , lambda __UpperCamelCase : tee(__UpperCamelCase , __UpperCamelCase , sys.stderr , label='''stderr:''' ) ) ), ] , timeout=__UpperCamelCase , ) return _RunOutput(await p.wait() , __UpperCamelCase , __UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] , __UpperCamelCase : Any=None , __UpperCamelCase : List[Any]=None , __UpperCamelCase : List[str]=1_80 , __UpperCamelCase : List[str]=False , __UpperCamelCase : Dict=True ) -> _RunOutput: """simple docstring""" A__ : Dict = asyncio.get_event_loop() A__ : Optional[Any] = loop.run_until_complete( _stream_subprocess(__UpperCamelCase , env=__UpperCamelCase , stdin=__UpperCamelCase , timeout=__UpperCamelCase , quiet=__UpperCamelCase , echo=__UpperCamelCase ) ) A__ : Union[str, Any] = ''' '''.join(__UpperCamelCase ) if result.returncode > 0: A__ : Optional[Any] = '''\n'''.join(result.stderr ) raise RuntimeError( F"'{cmd_str}' failed with returncode {result.returncode}\n\n" F"The combined stderr from workers follows:\n{stderr}" ) return result class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] , __UpperCamelCase : List[Any]=False ) -> Dict: """simple docstring""" try: A__ : List[Any] = subprocess.check_output(__UpperCamelCase , stderr=subprocess.STDOUT ) if return_stdout: if hasattr(__UpperCamelCase , '''decode''' ): A__ : Any = output.decode('''utf-8''' ) return output except subprocess.CalledProcessError as e: raise SubprocessCallException( F"Command `{' '.join(__UpperCamelCase )}` failed with the following error:\n\n{e.output.decode()}" ) from e
55
1
import json import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from transformers import OneFormerImageProcessor from transformers.models.oneformer.image_processing_oneformer import binary_mask_to_rle from transformers.models.oneformer.modeling_oneformer import OneFormerForUniversalSegmentationOutput if is_vision_available(): from PIL import Image def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] , __UpperCamelCase : Tuple="shi-labs/oneformer_demo" ) -> Dict: """simple docstring""" with open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type='''dataset''' ) , '''r''' ) as f: A__ : List[str] = json.load(__UpperCamelCase ) A__ : int = {} A__ : Dict = [] A__ : Tuple = [] for key, info in class_info.items(): A__ : Dict = info['''name'''] class_names.append(info['''name'''] ) if info["isthing"]: thing_ids.append(int(__UpperCamelCase ) ) A__ : Union[str, Any] = thing_ids A__ : Dict = class_names return metadata class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__=7 , UpperCamelCase__=3 , UpperCamelCase__=30 , UpperCamelCase__=400 , UpperCamelCase__=None , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=[0.5, 0.5, 0.5] , UpperCamelCase__=[0.5, 0.5, 0.5] , UpperCamelCase__=10 , UpperCamelCase__=False , UpperCamelCase__=255 , UpperCamelCase__="shi-labs/oneformer_demo" , UpperCamelCase__="ade20k_panoptic.json" , UpperCamelCase__=10 , ): A__ : Any = parent A__ : Union[str, Any] = batch_size A__ : Any = num_channels A__ : Optional[int] = min_resolution A__ : Optional[int] = max_resolution A__ : Optional[int] = do_resize A__ : List[Any] = {'''shortest_edge''': 32, '''longest_edge''': 1333} if size is None else size A__ : List[str] = do_normalize A__ : Any = image_mean A__ : Union[str, Any] = image_std A__ : int = class_info_file A__ : Optional[Any] = prepare_metadata(UpperCamelCase__ , UpperCamelCase__ ) A__ : Optional[int] = num_text A__ : Optional[int] = repo_path # for the post_process_functions A__ : Dict = 2 A__ : Any = 10 A__ : Tuple = 10 A__ : List[Any] = 3 A__ : Dict = 4 A__ : int = num_labels A__ : Union[str, Any] = do_reduce_labels A__ : List[str] = ignore_index def __snake_case ( self ): return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "num_labels": self.num_labels, "do_reduce_labels": self.do_reduce_labels, "ignore_index": self.ignore_index, "class_info_file": self.class_info_file, "metadata": self.metadata, "num_text": self.num_text, } def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__=False ): if not batched: A__ : List[str] = image_inputs[0] if isinstance(UpperCamelCase__ , Image.Image ): A__ , A__ : Any = image.size else: A__ , A__ : int = image.shape[1], image.shape[2] if w < h: A__ : List[Any] = int(self.size['''shortest_edge'''] * h / w ) A__ : str = self.size['''shortest_edge'''] elif w > h: A__ : List[Any] = self.size['''shortest_edge'''] A__ : Dict = int(self.size['''shortest_edge'''] * w / h ) else: A__ : List[str] = self.size['''shortest_edge'''] A__ : Optional[int] = self.size['''shortest_edge'''] else: A__ : Tuple = [] for image in image_inputs: A__ , A__ : Tuple = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) A__ : List[Any] = max(UpperCamelCase__ , key=lambda UpperCamelCase__ : item[0] )[0] A__ : Union[str, Any] = max(UpperCamelCase__ , key=lambda UpperCamelCase__ : item[1] )[1] return expected_height, expected_width def __snake_case ( self ): return OneFormerForUniversalSegmentationOutput( # +1 for null class class_queries_logits=torch.randn((self.batch_size, self.num_queries, self.num_classes + 1) ) , masks_queries_logits=torch.randn((self.batch_size, self.num_queries, self.height, self.width) ) , ) @require_torch @require_vision class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, unittest.TestCase ): '''simple docstring''' _lowerCAmelCase = OneFormerImageProcessor if (is_vision_available() and is_torch_available()) else None # only for test_image_processing_common.test_image_proc_to_json_string _lowerCAmelCase = image_processing_class def __snake_case ( self ): A__ : int = OneFormerImageProcessorTester(self ) @property def __snake_case ( self ): return self.image_processing_tester.prepare_image_processor_dict() def __snake_case ( self ): A__ : Tuple = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCamelCase__ , '''image_mean''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''image_std''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''do_normalize''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''do_resize''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''size''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''ignore_index''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''class_info_file''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''num_text''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''repo_path''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''metadata''' ) ) self.assertTrue(hasattr(UpperCamelCase__ , '''do_reduce_labels''' ) ) def __snake_case ( self ): pass def __snake_case ( self ): # Initialize image_processor A__ : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ : Dict = prepare_image_inputs(self.image_processing_tester , equal_resolution=UpperCamelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase__ , Image.Image ) # Test not batched input A__ : int = image_processor(image_inputs[0] , ['''semantic'''] , return_tensors='''pt''' ).pixel_values A__ , A__ : Any = self.image_processing_tester.get_expected_values(UpperCamelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ , A__ : str = self.image_processing_tester.get_expected_values(UpperCamelCase__ , batched=UpperCamelCase__ ) A__ : Optional[Any] = image_processor( UpperCamelCase__ , ['''semantic'''] * len(UpperCamelCase__ ) , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case ( self ): # Initialize image_processor A__ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A__ : Dict = prepare_image_inputs(self.image_processing_tester , equal_resolution=UpperCamelCase__ , numpify=UpperCamelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase__ , np.ndarray ) # Test not batched input A__ : int = image_processor(image_inputs[0] , ['''semantic'''] , return_tensors='''pt''' ).pixel_values A__ , A__ : Optional[Any] = self.image_processing_tester.get_expected_values(UpperCamelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ , A__ : List[str] = self.image_processing_tester.get_expected_values(UpperCamelCase__ , batched=UpperCamelCase__ ) A__ : Optional[Any] = image_processor( UpperCamelCase__ , ['''semantic'''] * len(UpperCamelCase__ ) , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case ( self ): # Initialize image_processor A__ : str = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A__ : Union[str, Any] = prepare_image_inputs(self.image_processing_tester , equal_resolution=UpperCamelCase__ , torchify=UpperCamelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase__ , torch.Tensor ) # Test not batched input A__ : Optional[int] = image_processor(image_inputs[0] , ['''semantic'''] , return_tensors='''pt''' ).pixel_values A__ , A__ : Union[str, Any] = self.image_processing_tester.get_expected_values(UpperCamelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ , A__ : int = self.image_processing_tester.get_expected_values(UpperCamelCase__ , batched=UpperCamelCase__ ) A__ : Any = image_processor( UpperCamelCase__ , ['''semantic'''] * len(UpperCamelCase__ ) , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case ( self , UpperCamelCase__=False , UpperCamelCase__=False , UpperCamelCase__="np" ): A__ : Any = self.image_processing_class(**self.image_processor_dict ) # prepare image and target A__ : Optional[int] = self.image_processing_tester.num_labels A__ : Tuple = None A__ : Optional[int] = None A__ : List[str] = prepare_image_inputs(self.image_processing_tester , equal_resolution=UpperCamelCase__ ) if with_segmentation_maps: A__ : Dict = num_labels if is_instance_map: A__ : Any = list(range(UpperCamelCase__ ) ) * 2 A__ : List[Any] = dict(enumerate(UpperCamelCase__ ) ) A__ : Tuple = [ np.random.randint(0 , high * 2 , (img.size[1], img.size[0]) ).astype(np.uinta ) for img in image_inputs ] if segmentation_type == "pil": A__ : Optional[int] = [Image.fromarray(UpperCamelCase__ ) for annotation in annotations] A__ : Optional[int] = image_processor( UpperCamelCase__ , ['''semantic'''] * len(UpperCamelCase__ ) , UpperCamelCase__ , return_tensors='''pt''' , instance_id_to_semantic_id=UpperCamelCase__ , pad_and_return_pixel_mask=UpperCamelCase__ , ) return inputs def __snake_case ( self ): pass def __snake_case ( self ): def common(UpperCamelCase__=False , UpperCamelCase__=None ): A__ : List[Any] = self.comm_get_image_processor_inputs( with_segmentation_maps=UpperCamelCase__ , is_instance_map=UpperCamelCase__ , segmentation_type=UpperCamelCase__ ) A__ : Optional[int] = inputs['''mask_labels'''] A__ : Union[str, Any] = inputs['''class_labels'''] A__ : Dict = inputs['''pixel_values'''] A__ : Union[str, Any] = inputs['''text_inputs'''] # check the batch_size for mask_label, class_label, text_input in zip(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): self.assertEqual(mask_label.shape[0] , class_label.shape[0] ) # this ensure padding has happened self.assertEqual(mask_label.shape[1:] , pixel_values.shape[2:] ) self.assertEqual(len(UpperCamelCase__ ) , self.image_processing_tester.num_text ) common() common(is_instance_map=UpperCamelCase__ ) common(is_instance_map=UpperCamelCase__ , segmentation_type='''pil''' ) common(is_instance_map=UpperCamelCase__ , segmentation_type='''pil''' ) def __snake_case ( self ): A__ : Union[str, Any] = np.zeros((20, 50) ) A__ : Any = 1 A__ : str = 1 A__ : Dict = 1 A__ : Tuple = binary_mask_to_rle(UpperCamelCase__ ) self.assertEqual(len(UpperCamelCase__ ) , 4 ) self.assertEqual(rle[0] , 21 ) self.assertEqual(rle[1] , 45 ) def __snake_case ( self ): A__ : Union[str, Any] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file='''ade20k_panoptic.json''' , num_text=self.image_processing_tester.num_text , repo_path='''shi-labs/oneformer_demo''' , ) A__ : List[Any] = self.image_processing_tester.get_fake_oneformer_outputs() A__ : Union[str, Any] = fature_extractor.post_process_semantic_segmentation(UpperCamelCase__ ) self.assertEqual(len(UpperCamelCase__ ) , self.image_processing_tester.batch_size ) self.assertEqual( segmentation[0].shape , ( self.image_processing_tester.height, self.image_processing_tester.width, ) , ) A__ : Dict = [(1, 4) for i in range(self.image_processing_tester.batch_size )] A__ : Optional[int] = fature_extractor.post_process_semantic_segmentation(UpperCamelCase__ , target_sizes=UpperCamelCase__ ) self.assertEqual(segmentation[0].shape , target_sizes[0] ) def __snake_case ( self ): A__ : Union[str, Any] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file='''ade20k_panoptic.json''' , num_text=self.image_processing_tester.num_text , repo_path='''shi-labs/oneformer_demo''' , ) A__ : int = self.image_processing_tester.get_fake_oneformer_outputs() A__ : str = image_processor.post_process_instance_segmentation(UpperCamelCase__ , threshold=0 ) self.assertTrue(len(UpperCamelCase__ ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue('''segmentation''' in el ) self.assertTrue('''segments_info''' in el ) self.assertEqual(type(el['''segments_info'''] ) , UpperCamelCase__ ) self.assertEqual( el['''segmentation'''].shape , (self.image_processing_tester.height, self.image_processing_tester.width) ) def __snake_case ( self ): A__ : Tuple = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file='''ade20k_panoptic.json''' , num_text=self.image_processing_tester.num_text , repo_path='''shi-labs/oneformer_demo''' , ) A__ : Tuple = self.image_processing_tester.get_fake_oneformer_outputs() A__ : int = image_processor.post_process_panoptic_segmentation(UpperCamelCase__ , threshold=0 ) self.assertTrue(len(UpperCamelCase__ ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue('''segmentation''' in el ) self.assertTrue('''segments_info''' in el ) self.assertEqual(type(el['''segments_info'''] ) , UpperCamelCase__ ) self.assertEqual( el['''segmentation'''].shape , (self.image_processing_tester.height, self.image_processing_tester.width) )
55
import numpy as np _SCREAMING_SNAKE_CASE : Any = [ ['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'k'], ['l', 'm', 'n', 'o', 'p'], ['q', 'r', 's', 't', 'u'], ['v', 'w', 'x', 'y', 'z'], ] class UpperCamelCase__ : '''simple docstring''' def __init__( self ): A__ : List[Any] = np.array(UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): A__ , A__ : Any = np.where(letter == self.SQUARE ) A__ : int = np.concatenate([indexa + 1, indexa + 1] ) return indexes def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ ): A__ : Union[str, Any] = self.SQUARE[indexa - 1, indexa - 1] return letter def __snake_case ( self , UpperCamelCase__ ): A__ : List[str] = message.lower() A__ : str = message.replace(''' ''' , '''''' ) A__ : Union[str, Any] = message.replace('''j''' , '''i''' ) A__ : List[Any] = np.empty((2, len(UpperCamelCase__ )) ) for letter_index in range(len(UpperCamelCase__ ) ): A__ : Any = self.letter_to_numbers(message[letter_index] ) A__ : Optional[Any] = numbers[0] A__ : List[str] = numbers[1] A__ : List[str] = first_step.reshape(2 * len(UpperCamelCase__ ) ) A__ : List[Any] = '''''' for numbers_index in range(len(UpperCamelCase__ ) ): A__ : Dict = int(second_step[numbers_index * 2] ) A__ : List[str] = int(second_step[(numbers_index * 2) + 1] ) A__ : Dict = self.numbers_to_letter(UpperCamelCase__ , UpperCamelCase__ ) A__ : Tuple = encoded_message + letter return encoded_message def __snake_case ( self , UpperCamelCase__ ): A__ : str = message.lower() message.replace(''' ''' , '''''' ) A__ : List[Any] = np.empty(2 * len(UpperCamelCase__ ) ) for letter_index in range(len(UpperCamelCase__ ) ): A__ : List[str] = self.letter_to_numbers(message[letter_index] ) A__ : Dict = numbers[0] A__ : int = numbers[1] A__ : Optional[Any] = first_step.reshape((2, len(UpperCamelCase__ )) ) A__ : int = '''''' for numbers_index in range(len(UpperCamelCase__ ) ): A__ : Tuple = int(second_step[0, numbers_index] ) A__ : Dict = int(second_step[1, numbers_index] ) A__ : List[str] = self.numbers_to_letter(UpperCamelCase__ , UpperCamelCase__ ) A__ : Tuple = decoded_message + letter return decoded_message
55
1
import gc import unittest from diffusers import FlaxStableDiffusionInpaintPipeline from diffusers.utils import is_flax_available, load_image, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class UpperCamelCase__ ( unittest.TestCase ): '''simple docstring''' def __snake_case ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() def __snake_case ( self ): A__ : Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) A__ : Optional[int] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) A__ : List[Any] = '''xvjiarui/stable-diffusion-2-inpainting''' A__ , A__ : Dict = FlaxStableDiffusionInpaintPipeline.from_pretrained(UpperCamelCase__ , safety_checker=UpperCamelCase__ ) A__ : Union[str, Any] = '''Face of a yellow cat, high resolution, sitting on a park bench''' A__ : List[str] = jax.random.PRNGKey(0 ) A__ : Dict = 50 A__ : Any = jax.device_count() A__ : str = num_samples * [prompt] A__ : int = num_samples * [init_image] A__ : Tuple = num_samples * [mask_image] A__ , A__ , A__ : int = pipeline.prepare_inputs(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # shard inputs and rng A__ : Optional[Any] = replicate(UpperCamelCase__ ) A__ : Any = jax.random.split(UpperCamelCase__ , jax.device_count() ) A__ : Any = shard(UpperCamelCase__ ) A__ : Optional[int] = shard(UpperCamelCase__ ) A__ : Union[str, Any] = shard(UpperCamelCase__ ) A__ : int = pipeline( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , jit=UpperCamelCase__ ) A__ : int = output.images.reshape(UpperCamelCase__ , 512 , 512 , 3 ) A__ : Union[str, Any] = images[0, 253:256, 253:256, -1] A__ : Dict = jnp.asarray(jax.device_get(image_slice.flatten() ) ) A__ : Union[str, Any] = jnp.array( [0.3_6_1_1_3_0_7, 0.3_7_6_4_9_7_3_6, 0.3_7_5_7_4_0_8, 0.3_8_2_1_3_9_5_3, 0.3_9_2_9_5_1_6_7, 0.3_8_4_1_6_3_1, 0.4_1_5_5_4_9_7_8, 0.4_1_3_7_4_7_5, 0.4_2_1_7_0_8_4] ) print(F"output_slice: {output_slice}" ) assert jnp.abs(output_slice - expected_slice ).max() < 1e-2
55
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline else: from .pipeline_kandinsky import KandinskyPipeline from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput from .text_encoder import MultilingualCLIP
55
1
# 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 UpperCamelCase__ ( SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = 1 @register_to_config def __init__( self , UpperCamelCase__=2000 , UpperCamelCase__=0.1 , UpperCamelCase__=20 , UpperCamelCase__=1e-3 ): A__ : int = None A__ : List[str] = None A__ : Union[str, Any] = None def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ = None ): A__ : Optional[int] = torch.linspace(1 , self.config.sampling_eps , UpperCamelCase__ , device=UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None ): 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 A__ : Optional[Any] = ( -0.2_5 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) A__ : str = torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) A__ : int = std.flatten() while len(std.shape ) < len(score.shape ): A__ : Tuple = std.unsqueeze(-1 ) A__ : List[Any] = -score / std # compute A__ : Dict = -1.0 / len(self.timesteps ) A__ : Dict = self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) A__ : Union[str, Any] = beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): A__ : Optional[int] = beta_t.unsqueeze(-1 ) A__ : Dict = -0.5 * beta_t * x A__ : Optional[int] = torch.sqrt(UpperCamelCase__ ) A__ : Union[str, Any] = drift - diffusion**2 * score A__ : List[str] = x + drift * dt # add noise A__ : Dict = randn_tensor(x.shape , layout=x.layout , generator=UpperCamelCase__ , device=x.device , dtype=x.dtype ) A__ : List[str] = x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__( self ): return self.config.num_train_timesteps
55
import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to properly calculate the metrics on the # validation dataset when in a distributed system, and builds off the # `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _SCREAMING_SNAKE_CASE : str = 1_6 _SCREAMING_SNAKE_CASE : Tuple = 3_2 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Accelerator , __UpperCamelCase : int = 16 ) -> Optional[int]: """simple docstring""" A__ : List[str] = AutoTokenizer.from_pretrained('''bert-base-cased''' ) A__ : Optional[int] = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__UpperCamelCase : Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) A__ : int = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=__UpperCamelCase , max_length=__UpperCamelCase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): A__ : Optional[int] = datasets.map( __UpperCamelCase , batched=__UpperCamelCase , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library A__ : List[Any] = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__UpperCamelCase : Any ): # On TPU it's best to pad everything to the same length or training will be very slow. A__ : Optional[Any] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": A__ : int = 16 elif accelerator.mixed_precision != "no": A__ : Any = 8 else: A__ : Union[str, Any] = None return tokenizer.pad( __UpperCamelCase , padding='''longest''' , max_length=__UpperCamelCase , pad_to_multiple_of=__UpperCamelCase , return_tensors='''pt''' , ) # Instantiate dataloaders. A__ : Optional[int] = DataLoader( tokenized_datasets['''train'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) A__ : Tuple = DataLoader( tokenized_datasets['''validation'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('TESTING_MOCKED_DATALOADERS', None) == "1": from accelerate.test_utils.training import mocked_dataloaders _SCREAMING_SNAKE_CASE : Dict = mocked_dataloaders # noqa: F811 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : List[Any] ) -> Optional[Any]: """simple docstring""" if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , __UpperCamelCase ) == "1": A__ : List[str] = 2 # Initialize accelerator A__ : Optional[Any] = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs A__ : Tuple = config['''lr'''] A__ : Dict = int(config['''num_epochs'''] ) A__ : int = int(config['''seed'''] ) A__ : Optional[Any] = int(config['''batch_size'''] ) A__ : int = evaluate.load('''glue''' , '''mrpc''' ) # If the batch size is too big we use gradient accumulation A__ : Union[str, Any] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: A__ : List[Any] = batch_size // MAX_GPU_BATCH_SIZE A__ : Dict = MAX_GPU_BATCH_SIZE set_seed(__UpperCamelCase ) A__ , A__ : int = get_dataloaders(__UpperCamelCase , __UpperCamelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) A__ : Optional[int] = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=__UpperCamelCase ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). A__ : Tuple = model.to(accelerator.device ) # Instantiate optimizer A__ : Optional[int] = AdamW(params=model.parameters() , lr=__UpperCamelCase ) # Instantiate scheduler A__ : Any = get_linear_schedule_with_warmup( optimizer=__UpperCamelCase , num_warmup_steps=1_00 , num_training_steps=(len(__UpperCamelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. A__ , A__ , A__ , A__ , A__ : Dict = accelerator.prepare( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # Now we train the model for epoch in range(__UpperCamelCase ): model.train() for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) A__ : Dict = model(**__UpperCamelCase ) A__ : Dict = outputs.loss A__ : List[str] = loss / gradient_accumulation_steps accelerator.backward(__UpperCamelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() A__ : Optional[int] = 0 for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): A__ : Union[str, Any] = model(**__UpperCamelCase ) A__ : int = outputs.logits.argmax(dim=-1 ) A__ , A__ : Optional[Any] = accelerator.gather((predictions, batch['''labels''']) ) # New Code # # First we check if it's a distributed system if accelerator.use_distributed: # Then see if we're on the last batch of our eval dataloader if step == len(__UpperCamelCase ) - 1: # Last batch needs to be truncated on distributed systems as it contains additional samples A__ : Tuple = predictions[: len(eval_dataloader.dataset ) - samples_seen] A__ : int = references[: len(eval_dataloader.dataset ) - samples_seen] else: # Otherwise we add the number of samples seen samples_seen += references.shape[0] # All of this can be avoided if you use `Accelerator.gather_for_metrics` instead of `Accelerator.gather`: # accelerator.gather_for_metrics((predictions, batch["labels"])) metric.add_batch( predictions=__UpperCamelCase , references=__UpperCamelCase , ) A__ : Union[str, Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}:" , __UpperCamelCase ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: """simple docstring""" A__ : Tuple = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=__UpperCamelCase , default=__UpperCamelCase , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) A__ : Dict = parser.parse_args() A__ : Any = {'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": main()
55
1
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Dict = { 'kssteven/ibert-roberta-base': 'https://huggingface.co/kssteven/ibert-roberta-base/resolve/main/config.json', 'kssteven/ibert-roberta-large': 'https://huggingface.co/kssteven/ibert-roberta-large/resolve/main/config.json', 'kssteven/ibert-roberta-large-mnli': ( 'https://huggingface.co/kssteven/ibert-roberta-large-mnli/resolve/main/config.json' ), } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "ibert" def __init__( self , UpperCamelCase__=3_0522 , UpperCamelCase__=768 , UpperCamelCase__=12 , UpperCamelCase__=12 , UpperCamelCase__=3072 , UpperCamelCase__="gelu" , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=512 , UpperCamelCase__=2 , UpperCamelCase__=0.0_2 , UpperCamelCase__=1e-12 , UpperCamelCase__=1 , UpperCamelCase__=0 , UpperCamelCase__=2 , UpperCamelCase__="absolute" , UpperCamelCase__=False , UpperCamelCase__="none" , **UpperCamelCase__ , ): super().__init__(pad_token_id=UpperCamelCase__ , bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , **UpperCamelCase__ ) A__ : Optional[Any] = vocab_size A__ : Optional[int] = hidden_size A__ : Optional[Any] = num_hidden_layers A__ : Any = num_attention_heads A__ : str = hidden_act A__ : Tuple = intermediate_size A__ : int = hidden_dropout_prob A__ : int = attention_probs_dropout_prob A__ : List[str] = max_position_embeddings A__ : Union[str, Any] = type_vocab_size A__ : Union[str, Any] = initializer_range A__ : Any = layer_norm_eps A__ : List[Any] = position_embedding_type A__ : List[str] = quant_mode A__ : Tuple = force_dequant class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' @property def __snake_case ( self ): if self.task == "multiple-choice": A__ : List[Any] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: A__ : List[Any] = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
55
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor from ..utils import is_datasets_available from .base import PipelineTool if is_datasets_available(): from datasets import load_dataset class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "microsoft/speecht5_tts" _lowerCAmelCase = ( "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the " "text to read (in English) and returns a waveform object containing the sound." ) _lowerCAmelCase = "text_reader" _lowerCAmelCase = SpeechTaProcessor _lowerCAmelCase = SpeechTaForTextToSpeech _lowerCAmelCase = SpeechTaHifiGan _lowerCAmelCase = ["text"] _lowerCAmelCase = ["audio"] def __snake_case ( self ): if self.post_processor is None: A__ : int = '''microsoft/speecht5_hifigan''' super().setup() def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__=None ): A__ : List[Any] = self.pre_processor(text=UpperCamelCase__ , return_tensors='''pt''' , truncation=UpperCamelCase__ ) if speaker_embeddings is None: if not is_datasets_available(): raise ImportError('''Datasets needs to be installed if not passing speaker embeddings.''' ) A__ : List[Any] = load_dataset('''Matthijs/cmu-arctic-xvectors''' , split='''validation''' ) A__ : Dict = torch.tensor(embeddings_dataset[7305]['''xvector'''] ).unsqueeze(0 ) return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings} def __snake_case ( self , UpperCamelCase__ ): with torch.no_grad(): return self.model.generate_speech(**UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): with torch.no_grad(): return self.post_processor(UpperCamelCase__ ).cpu().detach()
55
1
import inspect import os import re from transformers.configuration_utils import PretrainedConfig from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py _SCREAMING_SNAKE_CASE : Any = 'src/transformers' # This is to make sure the transformers module imported is the one in the repo. _SCREAMING_SNAKE_CASE : Any = direct_transformers_import(PATH_TO_TRANSFORMERS) _SCREAMING_SNAKE_CASE : str = transformers.models.auto.configuration_auto.CONFIG_MAPPING _SCREAMING_SNAKE_CASE : Tuple = { # used to compute the property `self.chunk_length` 'EncodecConfig': ['overlap'], # used as `self.bert_model = BertModel(config, ...)` 'DPRConfig': True, # not used in modeling files, but it's an important information 'FSMTConfig': ['langs'], # used internally in the configuration class file 'GPTNeoConfig': ['attention_types'], # used internally in the configuration class file 'EsmConfig': ['is_folding_model'], # used during training (despite we don't have training script for these models yet) 'Mask2FormerConfig': ['ignore_value'], # `ignore_value` used during training (despite we don't have training script for these models yet) # `norm` used in conversion script (despite not using in the modeling file) 'OneFormerConfig': ['ignore_value', 'norm'], # used during preprocessing and collation, see `collating_graphormer.py` 'GraphormerConfig': ['spatial_pos_max'], # used internally in the configuration class file 'T5Config': ['feed_forward_proj'], # used internally in the configuration class file # `tokenizer_class` get default value `T5Tokenizer` intentionally 'MT5Config': ['feed_forward_proj', 'tokenizer_class'], 'UMT5Config': ['feed_forward_proj', 'tokenizer_class'], # used internally in the configuration class file 'LongT5Config': ['feed_forward_proj'], # used internally in the configuration class file 'SwitchTransformersConfig': ['feed_forward_proj'], # having default values other than `1e-5` - we can't fix them without breaking 'BioGptConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'GLPNConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'SegformerConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'CvtConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'PerceiverConfig': ['layer_norm_eps'], # used internally to calculate the feature size 'InformerConfig': ['num_static_real_features', 'num_time_features'], # used internally to calculate the feature size 'TimeSeriesTransformerConfig': ['num_static_real_features', 'num_time_features'], # used internally to calculate the feature size 'AutoformerConfig': ['num_static_real_features', 'num_time_features'], # used internally to calculate `mlp_dim` 'SamVisionConfig': ['mlp_ratio'], # For (head) training, but so far not implemented 'ClapAudioConfig': ['num_classes'], # Not used, but providing useful information to users 'SpeechT5HifiGanConfig': ['sampling_rate'], } # TODO (ydshieh): Check the failing cases, try to fix them or move some cases to the above block once we are sure SPECIAL_CASES_TO_ALLOW.update( { 'CLIPSegConfig': True, 'DeformableDetrConfig': True, 'DetaConfig': True, 'DinatConfig': True, 'DonutSwinConfig': True, 'EfficientFormerConfig': True, 'FSMTConfig': True, 'JukeboxConfig': True, 'LayoutLMv2Config': True, 'MaskFormerSwinConfig': True, 'MT5Config': True, 'NatConfig': True, 'OneFormerConfig': True, 'PerceiverConfig': True, 'RagConfig': True, 'SpeechT5Config': True, 'SwinConfig': True, 'Swin2SRConfig': True, 'Swinv2Config': True, 'SwitchTransformersConfig': True, 'TableTransformerConfig': True, 'TapasConfig': True, 'TransfoXLConfig': True, 'UniSpeechConfig': True, 'UniSpeechSatConfig': True, 'WavLMConfig': True, 'WhisperConfig': True, # TODO: @Arthur (for `alignment_head` and `alignment_layer`) 'JukeboxPriorConfig': True, # TODO: @Younes (for `is_decoder`) 'Pix2StructTextConfig': True, } ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : Optional[Any] , __UpperCamelCase : Dict , __UpperCamelCase : List[Any] ) -> Optional[Any]: """simple docstring""" A__ : str = False for attribute in attributes: for modeling_source in source_strings: # check if we can find `config.xxx`, `getattr(config, "xxx", ...)` or `getattr(self.config, "xxx", ...)` if ( F"config.{attribute}" in modeling_source or F"getattr(config, \"{attribute}\"" in modeling_source or F"getattr(self.config, \"{attribute}\"" in modeling_source ): A__ : int = True # Deal with multi-line cases elif ( re.search( RF"getattr[ \t\v\n\r\f]*\([ \t\v\n\r\f]*(self\.)?config,[ \t\v\n\r\f]*\"{attribute}\"" , __UpperCamelCase , ) is not None ): A__ : Any = True # `SequenceSummary` is called with `SequenceSummary(config)` elif attribute in [ "summary_type", "summary_use_proj", "summary_activation", "summary_last_dropout", "summary_proj_to_labels", "summary_first_dropout", ]: if "SequenceSummary" in modeling_source: A__ : Any = True if attribute_used: break if attribute_used: break # common and important attributes, even if they do not always appear in the modeling files A__ : List[Any] = [ '''bos_index''', '''eos_index''', '''pad_index''', '''unk_index''', '''mask_index''', '''image_size''', '''use_cache''', '''out_features''', '''out_indices''', ] A__ : List[str] = ['''encoder_no_repeat_ngram_size'''] # Special cases to be allowed A__ : Optional[Any] = True if not attribute_used: A__ : int = False for attribute in attributes: # Allow if the default value in the configuration class is different from the one in `PretrainedConfig` if attribute in ["is_encoder_decoder"] and default_value is True: A__ : Any = True elif attribute in ["tie_word_embeddings"] and default_value is False: A__ : Dict = True # Allow cases without checking the default value in the configuration class elif attribute in attributes_to_allow + attributes_used_in_generation: A__ : Optional[int] = True elif attribute.endswith('''_token_id''' ): A__ : Union[str, Any] = True # configuration class specific cases if not case_allowed: A__ : Tuple = SPECIAL_CASES_TO_ALLOW.get(config_class.__name__ , [] ) A__ : Dict = allowed_cases is True or attribute in allowed_cases return attribute_used or case_allowed def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Any ) -> Optional[int]: """simple docstring""" A__ : List[str] = dict(inspect.signature(config_class.__init__ ).parameters ) A__ : Optional[Any] = [x for x in list(signature.keys() ) if x not in ['''self''', '''kwargs''']] A__ : int = [signature[param].default for param in parameter_names] # If `attribute_map` exists, an attribute can have different names to be used in the modeling files, and as long # as one variant is used, the test should pass A__ : Optional[Any] = {} if len(config_class.attribute_map ) > 0: A__ : List[Any] = {v: k for k, v in config_class.attribute_map.items()} # Get the path to modeling source files A__ : Optional[int] = inspect.getsourcefile(__UpperCamelCase ) A__ : int = os.path.dirname(__UpperCamelCase ) # Let's check against all frameworks: as long as one framework uses an attribute, we are good. A__ : Union[str, Any] = [os.path.join(__UpperCamelCase , __UpperCamelCase ) for fn in os.listdir(__UpperCamelCase ) if fn.startswith('''modeling_''' )] # Get the source code strings A__ : int = [] for path in modeling_paths: if os.path.isfile(__UpperCamelCase ): with open(__UpperCamelCase ) as fp: modeling_sources.append(fp.read() ) A__ : Any = [] for config_param, default_value in zip(__UpperCamelCase , __UpperCamelCase ): # `attributes` here is all the variant names for `config_param` A__ : int = [config_param] # some configuration classes have non-empty `attribute_map`, and both names could be used in the # corresponding modeling files. As long as one of them appears, it is fine. if config_param in reversed_attribute_map: attributes.append(reversed_attribute_map[config_param] ) if not check_attribute_being_used(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): unused_attributes.append(attributes[0] ) return sorted(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: """simple docstring""" A__ : Dict = {} for _config_class in list(CONFIG_MAPPING.values() ): # Skip deprecated models if "models.deprecated" in _config_class.__module__: continue # Some config classes are not in `CONFIG_MAPPING` (e.g. `CLIPVisionConfig`, `Blip2VisionConfig`, etc.) A__ : Tuple = [ cls for name, cls in inspect.getmembers( inspect.getmodule(_config_class ) , lambda __UpperCamelCase : inspect.isclass(__UpperCamelCase ) and issubclass(__UpperCamelCase , __UpperCamelCase ) and inspect.getmodule(__UpperCamelCase ) == inspect.getmodule(_config_class ) , ) ] for config_class in config_classes_in_module: A__ : List[str] = check_config_attributes_being_used(__UpperCamelCase ) if len(__UpperCamelCase ) > 0: A__ : Union[str, Any] = unused_attributes if len(__UpperCamelCase ) > 0: A__ : Optional[Any] = '''The following configuration classes contain unused attributes in the corresponding modeling files:\n''' for name, attributes in configs_with_unused_attributes.items(): error += F"{name}: {attributes}\n" raise ValueError(__UpperCamelCase ) if __name__ == "__main__": check_config_attributes()
55
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation _SCREAMING_SNAKE_CASE : Optional[int] = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Optional[Any] = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} _SCREAMING_SNAKE_CASE : List[str] = { 'tokenizer_file': { 'EleutherAI/gpt-neox-20b': 'https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/tokenizer.json', }, } _SCREAMING_SNAKE_CASE : Dict = { 'gpt-neox-20b': 2_0_4_8, } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = VOCAB_FILES_NAMES _lowerCAmelCase = PRETRAINED_VOCAB_FILES_MAP _lowerCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCAmelCase = ["input_ids", "attention_mask"] def __init__( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__="<|endoftext|>" , UpperCamelCase__="<|endoftext|>" , UpperCamelCase__="<|endoftext|>" , UpperCamelCase__=False , **UpperCamelCase__ , ): super().__init__( UpperCamelCase__ , UpperCamelCase__ , tokenizer_file=UpperCamelCase__ , unk_token=UpperCamelCase__ , bos_token=UpperCamelCase__ , eos_token=UpperCamelCase__ , add_prefix_space=UpperCamelCase__ , **UpperCamelCase__ , ) A__ : Optional[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , UpperCamelCase__ ) != add_prefix_space: A__ : Union[str, Any] = getattr(UpperCamelCase__ , pre_tok_state.pop('''type''' ) ) A__ : List[Any] = add_prefix_space A__ : Any = pre_tok_class(**UpperCamelCase__ ) A__ : List[Any] = add_prefix_space def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ = None ): A__ : Any = self._tokenizer.model.save(UpperCamelCase__ , name=UpperCamelCase__ ) return tuple(UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): A__ : List[str] = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ) + [self.eos_token_id] ) if len(UpperCamelCase__ ) > self.model_max_length: A__ : Tuple = input_ids[-self.model_max_length :] return input_ids
55
1
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : list ) -> list: """simple docstring""" if any(not isinstance(__UpperCamelCase , __UpperCamelCase ) or x < 0 for x in sequence ): raise TypeError('''Sequence must be list of non-negative integers''' ) for _ in range(len(__UpperCamelCase ) ): for i, (rod_upper, rod_lower) in enumerate(zip(__UpperCamelCase , sequence[1:] ) ): if rod_upper > rod_lower: sequence[i] -= rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower return sequence if __name__ == "__main__": assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9]
55
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING _SCREAMING_SNAKE_CASE : Union[str, Any] = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : int = { '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__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "deformable_detr" _lowerCAmelCase = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=3 , UpperCamelCase__=300 , UpperCamelCase__=1024 , UpperCamelCase__=6 , UpperCamelCase__=1024 , UpperCamelCase__=8 , UpperCamelCase__=6 , UpperCamelCase__=1024 , UpperCamelCase__=8 , UpperCamelCase__=0.0 , UpperCamelCase__=True , UpperCamelCase__="relu" , UpperCamelCase__=256 , UpperCamelCase__=0.1 , UpperCamelCase__=0.0 , UpperCamelCase__=0.0 , UpperCamelCase__=0.0_2 , UpperCamelCase__=1.0 , UpperCamelCase__=True , UpperCamelCase__=False , UpperCamelCase__="sine" , UpperCamelCase__="resnet50" , UpperCamelCase__=True , UpperCamelCase__=False , UpperCamelCase__=4 , UpperCamelCase__=4 , UpperCamelCase__=4 , UpperCamelCase__=False , UpperCamelCase__=300 , UpperCamelCase__=False , UpperCamelCase__=1 , UpperCamelCase__=5 , UpperCamelCase__=2 , UpperCamelCase__=1 , UpperCamelCase__=1 , UpperCamelCase__=5 , UpperCamelCase__=2 , UpperCamelCase__=0.1 , UpperCamelCase__=0.2_5 , UpperCamelCase__=False , **UpperCamelCase__ , ): if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) A__ : int = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Union[str, Any] = backbone_config.get('''model_type''' ) A__ : Union[str, Any] = CONFIG_MAPPING[backbone_model_type] A__ : Optional[int] = config_class.from_dict(UpperCamelCase__ ) A__ : Tuple = use_timm_backbone A__ : int = backbone_config A__ : List[Any] = num_channels A__ : List[Any] = num_queries A__ : str = max_position_embeddings A__ : Tuple = d_model A__ : int = encoder_ffn_dim A__ : Union[str, Any] = encoder_layers A__ : Optional[Any] = encoder_attention_heads A__ : List[Any] = decoder_ffn_dim A__ : Tuple = decoder_layers A__ : Optional[Any] = decoder_attention_heads A__ : List[str] = dropout A__ : str = attention_dropout A__ : List[Any] = activation_dropout A__ : Any = activation_function A__ : Optional[Any] = init_std A__ : Union[str, Any] = init_xavier_std A__ : Union[str, Any] = encoder_layerdrop A__ : Optional[int] = auxiliary_loss A__ : str = position_embedding_type A__ : List[Any] = backbone A__ : Optional[Any] = use_pretrained_backbone A__ : Any = dilation # deformable attributes A__ : List[Any] = num_feature_levels A__ : List[str] = encoder_n_points A__ : int = decoder_n_points A__ : List[Any] = two_stage A__ : Dict = two_stage_num_proposals A__ : Optional[int] = 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 A__ : List[str] = class_cost A__ : List[Any] = bbox_cost A__ : Any = giou_cost # Loss coefficients A__ : List[str] = mask_loss_coefficient A__ : Union[str, Any] = dice_loss_coefficient A__ : List[Any] = bbox_loss_coefficient A__ : Tuple = giou_loss_coefficient A__ : Optional[Any] = eos_coefficient A__ : List[Any] = focal_alpha A__ : List[str] = disable_custom_kernels super().__init__(is_encoder_decoder=UpperCamelCase__ , **UpperCamelCase__ ) @property def __snake_case ( self ): return self.encoder_attention_heads @property def __snake_case ( self ): return self.d_model def __snake_case ( self ): A__ : List[str] = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: A__ : Tuple = self.backbone_config.to_dict() A__ : Optional[int] = self.__class__.model_type return output
55
1
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> list: """simple docstring""" if bit_count < 0: raise ValueError('''The given input must be positive''' ) # get the generated string sequence A__ : List[Any] = gray_code_sequence_string(__UpperCamelCase ) # # convert them to integers for i in range(len(__UpperCamelCase ) ): A__ : str = int(sequence[i] , 2 ) return sequence def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> list: """simple docstring""" if bit_count == 0: return ["0"] if bit_count == 1: return ["0", "1"] A__ : Tuple = 1 << bit_count # defines the length of the sequence # 1<< n is equivalent to 2^n # recursive answer will generate answer for n-1 bits A__ : List[str] = gray_code_sequence_string(bit_count - 1 ) A__ : Optional[Any] = [] # append 0 to first half of the smaller sequence generated for i in range(seq_len // 2 ): A__ : int = '''0''' + smaller_sequence[i] sequence.append(__UpperCamelCase ) # append 1 to second half ... start from the end of the list for i in reversed(range(seq_len // 2 ) ): A__ : Tuple = '''1''' + smaller_sequence[i] sequence.append(__UpperCamelCase ) return sequence if __name__ == "__main__": import doctest doctest.testmod()
55
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> List[Any]: """simple docstring""" A__ : Optional[Any] = 0 A__ : Optional[Any] = len(__UpperCamelCase ) for i in range(n - 1 ): for j in range(i + 1 , __UpperCamelCase ): if arr[i] > arr[j]: num_inversions += 1 return num_inversions def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int ) -> Tuple: """simple docstring""" if len(__UpperCamelCase ) <= 1: return arr, 0 A__ : Optional[int] = len(__UpperCamelCase ) // 2 A__ : List[str] = arr[0:mid] A__ : Union[str, Any] = arr[mid:] A__ , A__ : List[Any] = count_inversions_recursive(__UpperCamelCase ) A__ , A__ : int = count_inversions_recursive(__UpperCamelCase ) A__ , A__ : Dict = _count_cross_inversions(__UpperCamelCase , __UpperCamelCase ) A__ : Any = inversion_p + inversions_q + cross_inversions return c, num_inversions def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] , __UpperCamelCase : List[Any] ) -> Dict: """simple docstring""" A__ : str = [] A__ : Tuple = 0 while i < len(__UpperCamelCase ) and j < len(__UpperCamelCase ): if p[i] > q[j]: # if P[1] > Q[j], then P[k] > Q[k] for all i < k <= len(P) # These are all inversions. The claim emerges from the # property that P is sorted. num_inversion += len(__UpperCamelCase ) - i r.append(q[j] ) j += 1 else: r.append(p[i] ) i += 1 if i < len(__UpperCamelCase ): r.extend(p[i:] ) else: r.extend(q[j:] ) return r, num_inversion def SCREAMING_SNAKE_CASE ( ) -> Tuple: """simple docstring""" A__ : List[str] = [10, 2, 1, 5, 5, 2, 11] # this arr has 8 inversions: # (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2) A__ : int = count_inversions_bf(__UpperCamelCase ) A__ , A__ : int = count_inversions_recursive(__UpperCamelCase ) assert num_inversions_bf == num_inversions_recursive == 8 print('''number of inversions = ''' , __UpperCamelCase ) # testing an array with zero inversion (a sorted arr_1) arr_a.sort() A__ : Optional[Any] = count_inversions_bf(__UpperCamelCase ) A__ , A__ : Dict = count_inversions_recursive(__UpperCamelCase ) assert num_inversions_bf == num_inversions_recursive == 0 print('''number of inversions = ''' , __UpperCamelCase ) # an empty list should also have zero inversions A__ : Union[str, Any] = [] A__ : Union[str, Any] = count_inversions_bf(__UpperCamelCase ) A__ , A__ : Any = count_inversions_recursive(__UpperCamelCase ) assert num_inversions_bf == num_inversions_recursive == 0 print('''number of inversions = ''' , __UpperCamelCase ) if __name__ == "__main__": main()
55
1
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE_ ) class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = field(default="image-classification", metadata={"include_in_asdict_even_if_is_default": True} ) _lowerCAmelCase = Features({"image": Image()} ) _lowerCAmelCase = Features({"labels": ClassLabel} ) _lowerCAmelCase = "image" _lowerCAmelCase = "labels" def __snake_case ( self , UpperCamelCase__ ): if self.label_column not in features: raise ValueError(F"Column {self.label_column} is not present in features." ) if not isinstance(features[self.label_column] , UpperCamelCase__ ): raise ValueError(F"Column {self.label_column} is not a ClassLabel." ) A__ : Optional[Any] = copy.deepcopy(self ) A__ : Tuple = self.label_schema.copy() A__ : Any = features[self.label_column] A__ : Dict = label_schema return task_template @property def __snake_case ( self ): return { self.image_column: "image", self.label_column: "labels", }
55
from PIL import Image def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Image , __UpperCamelCase : float ) -> Image: """simple docstring""" def brightness(__UpperCamelCase : int ) -> float: return 1_28 + level + (c - 1_28) if not -2_5_5.0 <= level <= 2_5_5.0: raise ValueError('''level must be between -255.0 (black) and 255.0 (white)''' ) return img.point(__UpperCamelCase ) if __name__ == "__main__": # Load image with Image.open('image_data/lena.jpg') as img: # Change brightness to 100 _SCREAMING_SNAKE_CASE : Dict = change_brightness(img, 1_0_0) brigt_img.save('image_data/lena_brightness.png', format='png')
55
1
import os from typing import Dict, List, Tuple, TypeVar, Union _SCREAMING_SNAKE_CASE : List[str] = TypeVar('T') _SCREAMING_SNAKE_CASE : int = Union[List[T], Tuple[T, ...]] _SCREAMING_SNAKE_CASE : Any = Union[T, List[T], Dict[str, T]] _SCREAMING_SNAKE_CASE : int = Union[str, bytes, os.PathLike]
55
import json import os import tempfile from transformers.testing_utils import check_json_file_has_correct_format class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = None def __snake_case ( self ): A__ : Dict = self.feature_extraction_class(**self.feat_extract_dict ) A__ : Tuple = json.loads(feat_extract.to_json_string() ) for key, value in self.feat_extract_dict.items(): self.assertEqual(obj[key] , UpperCamelCase__ ) def __snake_case ( self ): A__ : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: A__ : Any = os.path.join(UpperCamelCase__ , '''feat_extract.json''' ) feat_extract_first.to_json_file(UpperCamelCase__ ) A__ : Dict = self.feature_extraction_class.from_json_file(UpperCamelCase__ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def __snake_case ( self ): A__ : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: A__ : Any = feat_extract_first.save_pretrained(UpperCamelCase__ )[0] check_json_file_has_correct_format(UpperCamelCase__ ) A__ : Optional[int] = self.feature_extraction_class.from_pretrained(UpperCamelCase__ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def __snake_case ( self ): A__ : str = self.feature_extraction_class() self.assertIsNotNone(UpperCamelCase__ )
55
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 PreTrainedTokenizer from ...utils import logging _SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE : Dict = '▁' _SCREAMING_SNAKE_CASE : Optional[int] = { 'vocab_file': 'vocab.json', 'spm_file': 'sentencepiece.bpe.model', } _SCREAMING_SNAKE_CASE : List[Any] = { 'vocab_file': { 'facebook/s2t-small-librispeech-asr': ( 'https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/vocab.json' ), }, 'spm_file': { 'facebook/s2t-small-librispeech-asr': ( 'https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/sentencepiece.bpe.model' ) }, } _SCREAMING_SNAKE_CASE : Any = { 'facebook/s2t-small-librispeech-asr': 1_0_2_4, } _SCREAMING_SNAKE_CASE : Union[str, Any] = ['pt', 'fr', 'ru', 'nl', 'ro', 'it', 'es', 'de'] _SCREAMING_SNAKE_CASE : Dict = {'mustc': MUSTC_LANGS} class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = VOCAB_FILES_NAMES _lowerCAmelCase = PRETRAINED_VOCAB_FILES_MAP _lowerCAmelCase = MAX_MODEL_INPUT_SIZES _lowerCAmelCase = ["input_ids", "attention_mask"] _lowerCAmelCase = [] def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__="<s>" , UpperCamelCase__="</s>" , UpperCamelCase__="<pad>" , UpperCamelCase__="<unk>" , UpperCamelCase__=False , UpperCamelCase__=False , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__ = None , **UpperCamelCase__ , ): A__ : Optional[Any] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=UpperCamelCase__ , eos_token=UpperCamelCase__ , unk_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , do_upper_case=UpperCamelCase__ , do_lower_case=UpperCamelCase__ , tgt_lang=UpperCamelCase__ , lang_codes=UpperCamelCase__ , sp_model_kwargs=self.sp_model_kwargs , **UpperCamelCase__ , ) A__ : int = do_upper_case A__ : List[str] = do_lower_case A__ : List[Any] = load_json(UpperCamelCase__ ) A__ : str = {v: k for k, v in self.encoder.items()} A__ : Union[str, Any] = spm_file A__ : int = load_spm(UpperCamelCase__ , self.sp_model_kwargs ) if lang_codes is not None: A__ : Tuple = lang_codes A__ : Any = LANGUAGES[lang_codes] A__ : Any = [F"<lang:{lang}>" for lang in self.langs] A__ : Optional[int] = {lang: self.sp_model.PieceToId(F"<lang:{lang}>" ) for lang in self.langs} A__ : Any = self.lang_tokens A__ : str = tgt_lang if tgt_lang is not None else self.langs[0] self.set_tgt_lang_special_tokens(self._tgt_lang ) else: A__ : str = {} @property def __snake_case ( self ): return len(self.encoder ) @property def __snake_case ( self ): return self._tgt_lang @tgt_lang.setter def __snake_case ( self , UpperCamelCase__ ): A__ : List[Any] = new_tgt_lang self.set_tgt_lang_special_tokens(UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): A__ : Optional[Any] = self.lang_code_to_id[tgt_lang] A__ : Optional[Any] = [lang_code_id] def __snake_case ( self , UpperCamelCase__ ): return self.sp_model.encode(UpperCamelCase__ , out_type=UpperCamelCase__ ) def __snake_case ( self , UpperCamelCase__ ): return self.encoder.get(UpperCamelCase__ , self.encoder[self.unk_token] ) def __snake_case ( self , UpperCamelCase__ ): return self.decoder.get(UpperCamelCase__ , self.unk_token ) def __snake_case ( self , UpperCamelCase__ ): A__ : Tuple = [] A__ : Optional[int] = '''''' for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: A__ : Any = self.sp_model.decode(UpperCamelCase__ ) out_string += (decoded.upper() if self.do_upper_case else decoded) + token + " " A__ : List[Any] = [] else: current_sub_tokens.append(UpperCamelCase__ ) A__ : List[Any] = self.sp_model.decode(UpperCamelCase__ ) out_string += decoded.upper() if self.do_upper_case else decoded return out_string.strip() def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__=None ): if token_ids_a is None: return self.prefix_tokens + token_ids_a + [self.eos_token_id] # 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.eos_token_id] def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ = None , UpperCamelCase__ = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=UpperCamelCase__ , token_ids_a=UpperCamelCase__ , already_has_special_tokens=UpperCamelCase__ ) A__ : Optional[Any] = [1] * len(self.prefix_tokens ) A__ : Optional[Any] = [1] if token_ids_a is None: return prefix_ones + ([0] * len(UpperCamelCase__ )) + suffix_ones return prefix_ones + ([0] * len(UpperCamelCase__ )) + ([0] * len(UpperCamelCase__ )) + suffix_ones def __snake_case ( self ): A__ : Optional[int] = self.encoder.copy() vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ): A__ : Optional[int] = self.__dict__.copy() A__ : Any = None return state def __setstate__( self , UpperCamelCase__ ): A__ : str = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): A__ : int = {} A__ : str = load_spm(self.spm_file , self.sp_model_kwargs ) def __snake_case ( self , UpperCamelCase__ , UpperCamelCase__ = None ): A__ : int = Path(UpperCamelCase__ ) assert save_dir.is_dir(), F"{save_directory} should be a directory" A__ : Optional[Any] = save_dir / ( (filename_prefix + '''-''' if filename_prefix else '''''') + self.vocab_files_names['''vocab_file'''] ) A__ : List[Any] = save_dir / ( (filename_prefix + '''-''' if filename_prefix else '''''') + self.vocab_files_names['''spm_file'''] ) save_json(self.encoder , UpperCamelCase__ ) if os.path.abspath(self.spm_file ) != os.path.abspath(UpperCamelCase__ ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , UpperCamelCase__ ) elif not os.path.isfile(self.spm_file ): with open(UpperCamelCase__ , '''wb''' ) as fi: A__ : str = self.sp_model.serialized_model_proto() fi.write(UpperCamelCase__ ) return (str(UpperCamelCase__ ), str(UpperCamelCase__ )) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str , __UpperCamelCase : Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: """simple docstring""" A__ : List[Any] = sentencepiece.SentencePieceProcessor(**__UpperCamelCase ) spm.Load(str(__UpperCamelCase ) ) return spm def SCREAMING_SNAKE_CASE ( __UpperCamelCase : str ) -> Union[Dict, List]: """simple docstring""" with open(__UpperCamelCase , '''r''' ) as f: return json.load(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] , __UpperCamelCase : str ) -> None: """simple docstring""" with open(__UpperCamelCase , '''w''' ) as f: json.dump(__UpperCamelCase , __UpperCamelCase , indent=2 )
55
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets _SCREAMING_SNAKE_CASE : Union[str, Any] = '\\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 : Tuple = '\\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 : Optional[Any] = '\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 UpperCamelCase__ ( datasets.Metric ): '''simple docstring''' def __snake_case ( self ): 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 , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = False , UpperCamelCase__ = False , UpperCamelCase__ = False , UpperCamelCase__ = False , ): A__ : List[Any] = len(references[0] ) if any(len(UpperCamelCase__ ) != references_per_prediction for refs in references ): raise ValueError('''Sacrebleu requires the same number of references for each prediction''' ) A__ : Dict = [[refs[i] for refs in references] for i in range(UpperCamelCase__ )] A__ : Optional[Any] = TER( normalized=UpperCamelCase__ , no_punct=UpperCamelCase__ , asian_support=UpperCamelCase__ , case_sensitive=UpperCamelCase__ , ) A__ : str = sb_ter.corpus_score(UpperCamelCase__ , UpperCamelCase__ ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
55
1
from __future__ import annotations import math from collections.abc import Callable def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Callable[[int | float], int | float] , __UpperCamelCase : int | float , __UpperCamelCase : int | float , __UpperCamelCase : int = 1_00 , ) -> float: """simple docstring""" A__ : Optional[int] = x_start A__ : Tuple = fnc(__UpperCamelCase ) A__ : Any = 0.0 for _ in range(__UpperCamelCase ): # Approximates curve as a sequence of linear lines and sums their length A__ : Optional[int] = (x_end - x_start) / steps + xa A__ : Optional[Any] = fnc(__UpperCamelCase ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step A__ : Union[str, Any] = xa A__ : Tuple = fxa return length if __name__ == "__main__": def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[int] ) -> str: """simple docstring""" return math.sin(10 * x ) print('f(x) = sin(10 * x)') print('The length of the curve from x = -10 to x = 10 is:') _SCREAMING_SNAKE_CASE : str = 1_0 while i <= 1_0_0_0_0_0: print(f"""With {i} steps: {line_length(f, -1_0, 1_0, i)}""") i *= 1_0
55
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging _SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) # TODO Update this _SCREAMING_SNAKE_CASE : Optional[int] = { 'facebook/esm-1b': 'https://huggingface.co/facebook/esm-1b/resolve/main/config.json', # See all ESM models at https://huggingface.co/models?filter=esm } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' _lowerCAmelCase = "esm" def __init__( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=768 , UpperCamelCase__=12 , UpperCamelCase__=12 , UpperCamelCase__=3072 , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=1026 , UpperCamelCase__=0.0_2 , UpperCamelCase__=1e-12 , UpperCamelCase__="absolute" , UpperCamelCase__=True , UpperCamelCase__=None , UpperCamelCase__=False , UpperCamelCase__=False , UpperCamelCase__=None , UpperCamelCase__=None , **UpperCamelCase__ , ): super().__init__(pad_token_id=UpperCamelCase__ , mask_token_id=UpperCamelCase__ , **UpperCamelCase__ ) A__ : Optional[Any] = vocab_size A__ : int = hidden_size A__ : List[str] = num_hidden_layers A__ : Tuple = num_attention_heads A__ : str = intermediate_size A__ : List[str] = hidden_dropout_prob A__ : Optional[Any] = attention_probs_dropout_prob A__ : int = max_position_embeddings A__ : List[str] = initializer_range A__ : List[Any] = layer_norm_eps A__ : int = position_embedding_type A__ : Optional[Any] = use_cache A__ : Optional[int] = emb_layer_norm_before A__ : List[str] = token_dropout A__ : Tuple = is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) A__ : List[Any] = EsmFoldConfig() elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : Optional[int] = EsmFoldConfig(**UpperCamelCase__ ) A__ : int = esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) A__ : Any = get_default_vocab_list() else: A__ : Dict = vocab_list else: A__ : Optional[Any] = None A__ : Tuple = None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCamelCase__ ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __snake_case ( self ): A__ : Optional[int] = super().to_dict() if isinstance(self.esmfold_config , UpperCamelCase__ ): A__ : Dict = self.esmfold_config.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = None _lowerCAmelCase = True _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = 0 _lowerCAmelCase = True _lowerCAmelCase = False _lowerCAmelCase = 128 _lowerCAmelCase = None def __snake_case ( self ): if self.trunk is None: A__ : Tuple = TrunkConfig() elif isinstance(self.trunk , UpperCamelCase__ ): A__ : List[Any] = TrunkConfig(**self.trunk ) def __snake_case ( self ): A__ : Optional[int] = asdict(self ) A__ : int = self.trunk.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = 48 _lowerCAmelCase = 1_024 _lowerCAmelCase = 128 _lowerCAmelCase = 32 _lowerCAmelCase = 32 _lowerCAmelCase = 32 _lowerCAmelCase = 0 _lowerCAmelCase = 0 _lowerCAmelCase = False _lowerCAmelCase = 4 _lowerCAmelCase = 128 _lowerCAmelCase = None def __snake_case ( self ): if self.structure_module is None: A__ : str = StructureModuleConfig() elif isinstance(self.structure_module , UpperCamelCase__ ): A__ : str = StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(F"`max_recycles` should be positive, got {self.max_recycles}." ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' F" {self.sequence_state_dim} and {self.sequence_state_dim}." ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' F" {self.pairwise_state_dim} and {self.pairwise_state_dim}." ) A__ : Tuple = self.sequence_state_dim // self.sequence_head_width A__ : int = self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' F" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}." ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' F" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}." ) if self.pairwise_state_dim % 2 != 0: raise ValueError(F"`pairwise_state_dim` should be even, got {self.pairwise_state_dim}." ) if self.dropout >= 0.4: raise ValueError(F"`dropout` should not be greater than 0.4, got {self.dropout}." ) def __snake_case ( self ): A__ : List[Any] = asdict(self ) A__ : Optional[int] = self.structure_module.to_dict() return output @dataclass class UpperCamelCase__ : '''simple docstring''' _lowerCAmelCase = 384 _lowerCAmelCase = 128 _lowerCAmelCase = 16 _lowerCAmelCase = 128 _lowerCAmelCase = 12 _lowerCAmelCase = 4 _lowerCAmelCase = 8 _lowerCAmelCase = 0.1 _lowerCAmelCase = 8 _lowerCAmelCase = 1 _lowerCAmelCase = 2 _lowerCAmelCase = 7 _lowerCAmelCase = 10 _lowerCAmelCase = 1e-8 _lowerCAmelCase = 1e5 def __snake_case ( self ): return asdict(self ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: """simple docstring""" return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
55
1
import importlib import inspect import json import os import re import shutil import sys from pathlib import Path from typing import Dict, Optional, Union from urllib import request from huggingface_hub import HfFolder, cached_download, hf_hub_download, model_info from packaging import version from .. import __version__ from . import DIFFUSERS_DYNAMIC_MODULE_NAME, HF_MODULES_CACHE, logging _SCREAMING_SNAKE_CASE : Dict = ( 'https://raw.githubusercontent.com/huggingface/diffusers/{revision}/examples/community/{pipeline}.py' ) _SCREAMING_SNAKE_CASE : Any = logging.get_logger(__name__) # pylint: disable=invalid-name def SCREAMING_SNAKE_CASE ( ) -> Dict: """simple docstring""" A__ : List[str] = '''https://pypi.org/pypi/diffusers/json''' A__ : Optional[int] = json.loads(request.urlopen(__UpperCamelCase ).read() )['''releases'''].keys() return sorted(__UpperCamelCase , key=lambda __UpperCamelCase : version.Version(__UpperCamelCase ) ) def SCREAMING_SNAKE_CASE ( ) -> Optional[int]: """simple docstring""" if HF_MODULES_CACHE in sys.path: return sys.path.append(__UpperCamelCase ) os.makedirs(__UpperCamelCase , exist_ok=__UpperCamelCase ) A__ : int = Path(__UpperCamelCase ) / '''__init__.py''' if not init_path.exists(): init_path.touch() def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Union[str, os.PathLike] ) -> Any: """simple docstring""" init_hf_modules() A__ : Tuple = Path(__UpperCamelCase ) / name # If the parent module does not exist yet, recursively create it. if not dynamic_module_path.parent.exists(): create_dynamic_module(dynamic_module_path.parent ) os.makedirs(__UpperCamelCase , exist_ok=__UpperCamelCase ) A__ : Optional[int] = dynamic_module_path / '''__init__.py''' if not init_path.exists(): init_path.touch() def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] ) -> str: """simple docstring""" with open(__UpperCamelCase , '''r''' , encoding='''utf-8''' ) as f: A__ : Union[str, Any] = f.read() # Imports of the form `import .xxx` A__ : int = re.findall('''^\s*import\s+\.(\S+)\s*$''' , __UpperCamelCase , flags=re.MULTILINE ) # Imports of the form `from .xxx import yyy` relative_imports += re.findall('''^\s*from\s+\.(\S+)\s+import''' , __UpperCamelCase , flags=re.MULTILINE ) # Unique-ify return list(set(__UpperCamelCase ) ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] ) -> Dict: """simple docstring""" A__ : List[Any] = False A__ : Any = [module_file] A__ : List[Any] = [] # Let's recurse through all relative imports while not no_change: A__ : Optional[Any] = [] for f in files_to_check: new_imports.extend(get_relative_imports(__UpperCamelCase ) ) A__ : Any = Path(__UpperCamelCase ).parent A__ : Optional[int] = [str(module_path / m ) for m in new_imports] A__ : str = [f for f in new_import_files if f not in all_relative_imports] A__ : Union[str, Any] = [F"{f}.py" for f in new_import_files] A__ : Any = len(__UpperCamelCase ) == 0 all_relative_imports.extend(__UpperCamelCase ) return all_relative_imports def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] ) -> str: """simple docstring""" with open(__UpperCamelCase , '''r''' , encoding='''utf-8''' ) as f: A__ : str = f.read() # Imports of the form `import xxx` A__ : List[Any] = re.findall('''^\s*import\s+(\S+)\s*$''' , __UpperCamelCase , flags=re.MULTILINE ) # Imports of the form `from xxx import yyy` imports += re.findall('''^\s*from\s+(\S+)\s+import''' , __UpperCamelCase , flags=re.MULTILINE ) # Only keep the top-level module A__ : Optional[int] = [imp.split('''.''' )[0] for imp in imports if not imp.startswith('''.''' )] # Unique-ify and test we got them all A__ : Union[str, Any] = list(set(__UpperCamelCase ) ) A__ : List[Any] = [] for imp in imports: try: importlib.import_module(__UpperCamelCase ) except ImportError: missing_packages.append(__UpperCamelCase ) if len(__UpperCamelCase ) > 0: raise ImportError( '''This modeling file requires the following packages that were not found in your environment: ''' F"{', '.join(__UpperCamelCase )}. Run `pip install {' '.join(__UpperCamelCase )}`" ) return get_relative_imports(__UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : str ) -> Any: """simple docstring""" A__ : Any = module_path.replace(os.path.sep , '''.''' ) A__ : List[str] = importlib.import_module(__UpperCamelCase ) if class_name is None: return find_pipeline_class(__UpperCamelCase ) return getattr(__UpperCamelCase , __UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Union[str, Any] ) -> str: """simple docstring""" from ..pipelines import DiffusionPipeline A__ : int = dict(inspect.getmembers(__UpperCamelCase , inspect.isclass ) ) A__ : Any = None for cls_name, cls in cls_members.items(): if ( cls_name != DiffusionPipeline.__name__ and issubclass(cls , __UpperCamelCase ) and cls.__module__.split('''.''' )[0] != "diffusers" ): if pipeline_class is not None: raise ValueError( F"Multiple classes that inherit from {DiffusionPipeline.__name__} have been found:" F" {pipeline_class.__name__}, and {cls_name}. Please make sure to define only one in" F" {loaded_module}." ) A__ : Dict = cls return pipeline_class def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Union[str, os.PathLike] , __UpperCamelCase : str , __UpperCamelCase : Optional[Union[str, os.PathLike]] = None , __UpperCamelCase : bool = False , __UpperCamelCase : bool = False , __UpperCamelCase : Optional[Dict[str, str]] = None , __UpperCamelCase : Optional[Union[bool, str]] = None , __UpperCamelCase : Optional[str] = None , __UpperCamelCase : bool = False , ) -> Union[str, Any]: """simple docstring""" A__ : str = str(__UpperCamelCase ) A__ : Any = os.path.join(__UpperCamelCase , __UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): A__ : Tuple = module_file_or_url A__ : List[Any] = '''local''' elif pretrained_model_name_or_path.count('''/''' ) == 0: A__ : int = get_diffusers_versions() # cut ".dev0" A__ : Union[str, Any] = '''v''' + '''.'''.join(__version__.split('''.''' )[:3] ) # retrieve github version that matches if revision is None: A__ : Dict = latest_version if latest_version[1:] in available_versions else '''main''' logger.info(F"Defaulting to latest_version: {revision}." ) elif revision in available_versions: A__ : Dict = F"v{revision}" elif revision == "main": A__ : Dict = revision else: raise ValueError( F"`custom_revision`: {revision} does not exist. Please make sure to choose one of" F" {', '.join(available_versions + ['main'] )}." ) # community pipeline on GitHub A__ : int = COMMUNITY_PIPELINES_URL.format(revision=__UpperCamelCase , pipeline=__UpperCamelCase ) try: A__ : int = cached_download( __UpperCamelCase , cache_dir=__UpperCamelCase , force_download=__UpperCamelCase , proxies=__UpperCamelCase , resume_download=__UpperCamelCase , local_files_only=__UpperCamelCase , use_auth_token=__UpperCamelCase , ) A__ : List[str] = '''git''' A__ : List[str] = pretrained_model_name_or_path + '''.py''' except EnvironmentError: logger.error(F"Could not locate the {module_file} inside {pretrained_model_name_or_path}." ) raise else: try: # Load from URL or cache if already cached A__ : Tuple = hf_hub_download( __UpperCamelCase , __UpperCamelCase , cache_dir=__UpperCamelCase , force_download=__UpperCamelCase , proxies=__UpperCamelCase , resume_download=__UpperCamelCase , local_files_only=__UpperCamelCase , use_auth_token=__UpperCamelCase , ) A__ : Any = os.path.join('''local''' , '''--'''.join(pretrained_model_name_or_path.split('''/''' ) ) ) except EnvironmentError: logger.error(F"Could not locate the {module_file} inside {pretrained_model_name_or_path}." ) raise # Check we have all the requirements in our environment A__ : Optional[int] = check_imports(__UpperCamelCase ) # Now we move the module inside our cached dynamic modules. A__ : List[Any] = DIFFUSERS_DYNAMIC_MODULE_NAME + os.path.sep + submodule create_dynamic_module(__UpperCamelCase ) A__ : int = Path(__UpperCamelCase ) / full_submodule if submodule == "local" or submodule == "git": # We always copy local files (we could hash the file to see if there was a change, and give them the name of # that hash, to only copy when there is a modification but it seems overkill for now). # The only reason we do the copy is to avoid putting too many folders in sys.path. shutil.copy(__UpperCamelCase , submodule_path / module_file ) for module_needed in modules_needed: A__ : Any = F"{module_needed}.py" shutil.copy(os.path.join(__UpperCamelCase , __UpperCamelCase ) , submodule_path / module_needed ) else: # Get the commit hash # TODO: we will get this info in the etag soon, so retrieve it from there and not here. if isinstance(__UpperCamelCase , __UpperCamelCase ): A__ : List[Any] = use_auth_token elif use_auth_token is True: A__ : Dict = HfFolder.get_token() else: A__ : Optional[int] = None A__ : List[str] = model_info(__UpperCamelCase , revision=__UpperCamelCase , token=__UpperCamelCase ).sha # The module file will end up being placed in a subfolder with the git hash of the repo. This way we get the # benefit of versioning. A__ : Tuple = submodule_path / commit_hash A__ : Optional[Any] = full_submodule + os.path.sep + commit_hash create_dynamic_module(__UpperCamelCase ) if not (submodule_path / module_file).exists(): shutil.copy(__UpperCamelCase , submodule_path / module_file ) # Make sure we also have every file with relative for module_needed in modules_needed: if not (submodule_path / module_needed).exists(): get_cached_module_file( __UpperCamelCase , F"{module_needed}.py" , cache_dir=__UpperCamelCase , force_download=__UpperCamelCase , resume_download=__UpperCamelCase , proxies=__UpperCamelCase , use_auth_token=__UpperCamelCase , revision=__UpperCamelCase , local_files_only=__UpperCamelCase , ) return os.path.join(__UpperCamelCase , __UpperCamelCase ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Union[str, os.PathLike] , __UpperCamelCase : str , __UpperCamelCase : Optional[str] = None , __UpperCamelCase : Optional[Union[str, os.PathLike]] = None , __UpperCamelCase : bool = False , __UpperCamelCase : bool = False , __UpperCamelCase : Optional[Dict[str, str]] = None , __UpperCamelCase : Optional[Union[bool, str]] = None , __UpperCamelCase : Optional[str] = None , __UpperCamelCase : bool = False , **__UpperCamelCase : str , ) -> str: """simple docstring""" A__ : int = get_cached_module_file( __UpperCamelCase , __UpperCamelCase , cache_dir=__UpperCamelCase , force_download=__UpperCamelCase , resume_download=__UpperCamelCase , proxies=__UpperCamelCase , use_auth_token=__UpperCamelCase , revision=__UpperCamelCase , local_files_only=__UpperCamelCase , ) return get_class_in_module(__UpperCamelCase , final_module.replace('''.py''' , '''''' ) )
55
import logging import torch from accelerate import Accelerator from arguments import EvaluationArguments from datasets import load_dataset from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, set_seed class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=1024 , UpperCamelCase__=1024 , UpperCamelCase__=3.6 ): A__ : str = tokenizer A__ : int = tokenizer.bos_token_id A__ : List[Any] = dataset A__ : Tuple = seq_length A__ : Any = seq_length * chars_per_token * num_of_sequences def __iter__( self ): A__ : Dict = iter(self.dataset ) A__ : Tuple = True while more_examples: A__ , A__ : Optional[Any] = [], 0 while True: if buffer_len >= self.input_characters: break try: buffer.append(next(UpperCamelCase__ )['''content'''] ) buffer_len += len(buffer[-1] ) except StopIteration: A__ : Dict = False break A__ : str = tokenizer(UpperCamelCase__ , truncation=UpperCamelCase__ )['''input_ids'''] A__ : Optional[int] = [] for tokenized_input in tokenized_inputs: all_token_ids.extend(tokenized_input + [self.concat_token_id] ) for i in range(0 , len(UpperCamelCase__ ) , self.seq_length ): A__ : Optional[int] = all_token_ids[i : i + self.seq_length] if len(UpperCamelCase__ ) == self.seq_length: yield torch.tensor(UpperCamelCase__ ) def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[Any] ) -> Any: """simple docstring""" A__ : Any = {'''streaming''': True} A__ : List[str] = load_dataset(args.dataset_name , split='''train''' , **__UpperCamelCase ) A__ : List[str] = ConstantLengthDataset(__UpperCamelCase , __UpperCamelCase , seq_length=args.seq_length ) A__ : int = DataLoader(__UpperCamelCase , batch_size=args.batch_size ) return eval_dataloader def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] ) -> Dict: """simple docstring""" model.eval() A__ : Dict = [] for step, batch in enumerate(__UpperCamelCase ): with torch.no_grad(): A__ : Any = model(__UpperCamelCase , labels=__UpperCamelCase ) A__ : Tuple = outputs.loss.repeat(args.batch_size ) losses.append(accelerator.gather(__UpperCamelCase ) ) if args.max_eval_steps > 0 and step >= args.max_eval_steps: break A__ : Tuple = torch.mean(torch.cat(__UpperCamelCase ) ) try: A__ : Optional[Any] = torch.exp(__UpperCamelCase ) except OverflowError: A__ : Union[str, Any] = float('''inf''' ) return loss.item(), perplexity.item() # Setup Accelerator _SCREAMING_SNAKE_CASE : List[Any] = Accelerator() # Parse configuration _SCREAMING_SNAKE_CASE : Optional[int] = HfArgumentParser(EvaluationArguments) _SCREAMING_SNAKE_CASE : Union[str, Any] = parser.parse_args() set_seed(args.seed) # Logging _SCREAMING_SNAKE_CASE : Dict = logging.getLogger(__name__) logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO ) # Load model and tokenizer _SCREAMING_SNAKE_CASE : Optional[int] = AutoModelForCausalLM.from_pretrained(args.model_ckpt) _SCREAMING_SNAKE_CASE : List[str] = AutoTokenizer.from_pretrained(args.model_ckpt) # Load dataset and dataloader _SCREAMING_SNAKE_CASE : Optional[Any] = create_dataloader(args) # Prepare everything with our `accelerator`. _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Optional[Any] = accelerator.prepare(model, eval_dataloader) # Evaluate and save the last checkpoint logger.info('Evaluating and saving model after training') _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Optional[int] = evaluate(args) logger.info(f"""loss/eval: {eval_loss}, perplexity: {perplexity}""")
55
1
from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image _SCREAMING_SNAKE_CASE : int = ['text', 'image', 'audio'] def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] ) -> Tuple: """simple docstring""" A__ : Union[str, Any] = [] for input_type in input_types: if input_type == "text": inputs.append('''Text input''' ) elif input_type == "image": inputs.append( Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO''' ) ) / '''000000039769.png''' ).resize((5_12, 5_12) ) ) elif input_type == "audio": inputs.append(torch.ones(30_00 ) ) elif isinstance(__UpperCamelCase , __UpperCamelCase ): inputs.append(create_inputs(__UpperCamelCase ) ) else: raise ValueError(F"Invalid type requested: {input_type}" ) return inputs def SCREAMING_SNAKE_CASE ( __UpperCamelCase : List ) -> Tuple: """simple docstring""" A__ : Any = [] for output in outputs: if isinstance(__UpperCamelCase , (str, AgentText) ): output_types.append('''text''' ) elif isinstance(__UpperCamelCase , (Image.Image, AgentImage) ): output_types.append('''image''' ) elif isinstance(__UpperCamelCase , (torch.Tensor, AgentAudio) ): output_types.append('''audio''' ) else: raise ValueError(F"Invalid output: {output}" ) return output_types @is_tool_test class UpperCamelCase__ : '''simple docstring''' def __snake_case ( self ): self.assertTrue(hasattr(self.tool , '''inputs''' ) ) self.assertTrue(hasattr(self.tool , '''outputs''' ) ) A__ : Dict = self.tool.inputs for _input in inputs: if isinstance(_input , UpperCamelCase__ ): for __input in _input: self.assertTrue(__input in authorized_types ) else: self.assertTrue(_input in authorized_types ) A__ : int = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types ) def __snake_case ( self ): A__ : Optional[Any] = create_inputs(self.tool.inputs ) A__ : Tuple = self.tool(*UpperCamelCase__ ) # There is a single output if len(self.tool.outputs ) == 1: A__ : Union[str, Any] = [outputs] self.assertListEqual(output_types(UpperCamelCase__ ) , self.tool.outputs ) def __snake_case ( self ): self.assertTrue(hasattr(self.tool , '''description''' ) ) self.assertTrue(hasattr(self.tool , '''default_checkpoint''' ) ) self.assertTrue(self.tool.description.startswith('''This is a tool that''' ) ) def __snake_case ( self ): A__ : Any = create_inputs(self.tool.inputs ) A__ : Optional[Any] = self.tool(*UpperCamelCase__ ) if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : List[Any] = [outputs] self.assertEqual(len(UpperCamelCase__ ) , len(self.tool.outputs ) ) for output, output_type in zip(UpperCamelCase__ , self.tool.outputs ): A__ : List[Any] = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(UpperCamelCase__ , UpperCamelCase__ ) ) def __snake_case ( self ): A__ : Dict = create_inputs(self.tool.inputs ) A__ : str = [] for _input, input_type in zip(UpperCamelCase__ , self.tool.inputs ): if isinstance(UpperCamelCase__ , UpperCamelCase__ ): _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input ) for _input_type in input_type] ) else: _inputs.append(AGENT_TYPE_MAPPING[input_type](_input ) ) # Should not raise an error A__ : Union[str, Any] = self.tool(*UpperCamelCase__ ) if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ : List[Any] = [outputs] self.assertEqual(len(UpperCamelCase__ ) , len(self.tool.outputs ) )
55
def SCREAMING_SNAKE_CASE ( ) -> Optional[int]: """simple docstring""" A__ : Optional[Any] = 0 for i in range(1 , 10_01 ): total += i**i return str(__UpperCamelCase )[-10:] if __name__ == "__main__": print(solution())
55
1