code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
import os import tempfile import unittest from transformers import DistilBertConfig, is_torch_available from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, DistilBertModel, ) class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Dict , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Optional[Any]=13 , __lowerCAmelCase : Any=7 , __lowerCAmelCase : Dict=True , __lowerCAmelCase : Any=True , __lowerCAmelCase : str=False , __lowerCAmelCase : Dict=True , __lowerCAmelCase : List[Any]=99 , __lowerCAmelCase : Optional[int]=32 , __lowerCAmelCase : int=5 , __lowerCAmelCase : Optional[Any]=4 , __lowerCAmelCase : List[str]=37 , __lowerCAmelCase : Any="gelu" , __lowerCAmelCase : Tuple=0.1 , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : Optional[int]=5_12 , __lowerCAmelCase : List[str]=16 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Tuple=0.0_2 , __lowerCAmelCase : str=3 , __lowerCAmelCase : Dict=4 , __lowerCAmelCase : str=None , ) -> Any: """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_input_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_labels A__ = num_choices A__ = scope def a_ ( self : int ) -> Tuple: """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_input_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None A__ = None A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A__ = ids_tensor([self.batch_size] , self.num_choices ) A__ = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" return DistilBertConfig( vocab_size=self.vocab_size , dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , hidden_dim=self.intermediate_size , hidden_act=self.hidden_act , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , ) def a_ ( self : Union[str, Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Tuple , __lowerCAmelCase : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : List[Any] ) -> Optional[Any]: """simple docstring""" A__ = DistilBertModel(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase , __lowerCAmelCase ) A__ = model(__lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a_ ( self : Optional[int] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : str , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[int] ) -> Optional[Any]: """simple docstring""" A__ = DistilBertForMaskedLM(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a_ ( self : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Tuple , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Tuple , __lowerCAmelCase : Tuple ) -> List[Any]: """simple docstring""" A__ = DistilBertForQuestionAnswering(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model( __lowerCAmelCase , attention_mask=__lowerCAmelCase , start_positions=__lowerCAmelCase , end_positions=__lowerCAmelCase ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def a_ ( self : Dict , __lowerCAmelCase : Any , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : List[str] , __lowerCAmelCase : List[str] ) -> Any: """simple docstring""" A__ = self.num_labels A__ = DistilBertForSequenceClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Any , __lowerCAmelCase : Tuple , __lowerCAmelCase : Optional[int] ) -> Dict: """simple docstring""" A__ = self.num_labels A__ = DistilBertForTokenClassification(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def a_ ( self : Optional[int] , __lowerCAmelCase : Dict , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Any , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[Any] ) -> List[str]: """simple docstring""" A__ = self.num_choices A__ = DistilBertForMultipleChoice(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() A__ = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() A__ = model( __lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def a_ ( self : List[Any] ) -> Optional[Any]: """simple docstring""" A__ = self.prepare_config_and_inputs() ((A__) , (A__) , (A__) , (A__) , (A__) , (A__)) = config_and_inputs A__ = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class A (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( ( DistilBertModel, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, ) if is_torch_available() else None ) __lowerCamelCase : Tuple = ( { '''feature-extraction''': DistilBertModel, '''fill-mask''': DistilBertForMaskedLM, '''question-answering''': DistilBertForQuestionAnswering, '''text-classification''': DistilBertForSequenceClassification, '''token-classification''': DistilBertForTokenClassification, '''zero-shot''': DistilBertForSequenceClassification, } if is_torch_available() else {} ) __lowerCamelCase : List[str] = True __lowerCamelCase : str = True __lowerCamelCase : int = True __lowerCamelCase : Optional[int] = True def a_ ( self : List[Any] ) -> Tuple: """simple docstring""" A__ = DistilBertModelTester(self ) A__ = ConfigTester(self , config_class=__lowerCAmelCase , dim=37 ) def a_ ( self : List[str] ) -> List[str]: """simple docstring""" self.config_tester.run_common_tests() def a_ ( self : str ) -> int: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_model(*__lowerCAmelCase ) def a_ ( self : Tuple ) -> Optional[Any]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_masked_lm(*__lowerCAmelCase ) def a_ ( self : Union[str, Any] ) -> List[Any]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_question_answering(*__lowerCAmelCase ) def a_ ( self : Any ) -> List[str]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_sequence_classification(*__lowerCAmelCase ) def a_ ( self : List[str] ) -> Tuple: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_token_classification(*__lowerCAmelCase ) def a_ ( self : int ) -> str: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_multiple_choice(*__lowerCAmelCase ) @slow def a_ ( self : List[Any] ) -> Tuple: """simple docstring""" for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = DistilBertModel.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) @slow @require_torch_gpu def a_ ( self : str ) -> List[str]: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # BertForMultipleChoice behaves incorrectly in JIT environments. if model_class == DistilBertForMultipleChoice: return A__ = True A__ = model_class(config=__lowerCAmelCase ) A__ = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) A__ = torch.jit.trace( __lowerCAmelCase , (inputs_dict["""input_ids"""].to("""cpu""" ), inputs_dict["""attention_mask"""].to("""cpu""" )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(__lowerCAmelCase , os.path.join(__lowerCAmelCase , """traced_model.pt""" ) ) A__ = torch.jit.load(os.path.join(__lowerCAmelCase , """traced_model.pt""" ) , map_location=__lowerCAmelCase ) loaded(inputs_dict["""input_ids"""].to(__lowerCAmelCase ) , inputs_dict["""attention_mask"""].to(__lowerCAmelCase ) ) @require_torch class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : Optional[int] ) -> Optional[Any]: """simple docstring""" A__ = DistilBertModel.from_pretrained("""distilbert-base-uncased""" ) A__ = torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] ) A__ = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase )[0] A__ = torch.Size((1, 11, 7_68) ) self.assertEqual(output.shape , __lowerCAmelCase ) A__ = torch.tensor( [[[-0.1_6_3_9, 0.3_2_9_9, 0.1_6_4_8], [-0.1_7_4_6, 0.3_2_8_9, 0.1_7_1_0], [-0.1_8_8_4, 0.3_3_5_7, 0.1_8_1_0]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , __lowerCAmelCase , atol=1e-4 ) )
274
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A : Tuple = logging.getLogger(__name__) def __lowerCamelCase ( __a :Optional[int] , __a :List[str] ) -> Tuple: """simple docstring""" A__ = np.argmax(__a , axis=1 ) return np.sum(outputs == labels ) def __lowerCamelCase ( __a :Tuple ) -> Dict: """simple docstring""" with open(__a , encoding="""utf_8""" ) as f: A__ = csv.reader(__a ) A__ = [] next(__a ) # skip the first line for line in tqdm(__a ): output.append((""" """.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def __lowerCamelCase ( __a :Optional[int] , __a :List[Any] , __a :Dict , __a :Optional[Any] , __a :Optional[Any] , __a :int ) -> Union[str, Any]: """simple docstring""" A__ = [] for dataset in encoded_datasets: A__ = len(__a ) A__ = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) A__ = np.zeros((n_batch, 2) , dtype=np.intaa ) A__ = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) A__ = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(__a ): A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = with_conta A__ = with_conta A__ = len(__a ) - 1 A__ = len(__a ) - 1 A__ = with_conta A__ = with_conta A__ = mc_label A__ = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(__a ) for t in all_inputs ) ) return tensor_datasets def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = argparse.ArgumentParser() parser.add_argument("""--model_name""" , type=__a , default="""openai-gpt""" , help="""pretrained model name""" ) parser.add_argument("""--do_train""" , action="""store_true""" , help="""Whether to run training.""" ) parser.add_argument("""--do_eval""" , action="""store_true""" , help="""Whether to run eval on the dev set.""" ) parser.add_argument( """--output_dir""" , default=__a , type=__a , required=__a , help="""The output directory where the model predictions and checkpoints will be written.""" , ) parser.add_argument("""--train_dataset""" , type=__a , default="""""" ) parser.add_argument("""--eval_dataset""" , type=__a , default="""""" ) parser.add_argument("""--seed""" , type=__a , default=4_2 ) parser.add_argument("""--num_train_epochs""" , type=__a , default=3 ) parser.add_argument("""--train_batch_size""" , type=__a , default=8 ) parser.add_argument("""--eval_batch_size""" , type=__a , default=1_6 ) parser.add_argument("""--adam_epsilon""" , default=1E-8 , type=__a , help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--max_grad_norm""" , type=__a , default=1 ) parser.add_argument( """--max_steps""" , default=-1 , type=__a , help=( """If > 0: set total number of training steps to perform. Override num_train_epochs.""" ) , ) parser.add_argument( """--gradient_accumulation_steps""" , type=__a , default=1 , help="""Number of updates steps to accumulate before performing a backward/update pass.""" , ) parser.add_argument("""--learning_rate""" , type=__a , default=6.25E-5 ) parser.add_argument("""--warmup_steps""" , default=0 , type=__a , help="""Linear warmup over warmup_steps.""" ) parser.add_argument("""--lr_schedule""" , type=__a , default="""warmup_linear""" ) parser.add_argument("""--weight_decay""" , type=__a , default=0.01 ) parser.add_argument("""--lm_coef""" , type=__a , default=0.9 ) parser.add_argument("""--n_valid""" , type=__a , default=3_7_4 ) parser.add_argument("""--server_ip""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) parser.add_argument("""--server_port""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) A__ = parser.parse_args() print(__a ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("""Waiting for debugger attach""" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__a ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) A__ = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) A__ = torch.cuda.device_count() logger.info("""device: {}, n_gpu {}""".format(__a , __a ) ) if not args.do_train and not args.do_eval: raise ValueError("""At least one of `do_train` or `do_eval` must be True.""" ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset A__ = ["""_start_""", """_delimiter_""", """_classify_"""] A__ = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(__a ) A__ = tokenizer.convert_tokens_to_ids(__a ) A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(__a ) ) model.to(__a ) # Load and encode the datasets def tokenize_and_encode(__a :Tuple ): if isinstance(__a , __a ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(__a ) ) elif isinstance(__a , __a ): return obj return [tokenize_and_encode(__a ) for o in obj] logger.info("""Encoding dataset...""" ) A__ = load_rocstories_dataset(args.train_dataset ) A__ = load_rocstories_dataset(args.eval_dataset ) A__ = (train_dataset, eval_dataset) A__ = tokenize_and_encode(__a ) # Compute the max input length for the Transformer A__ = model.config.n_positions // 2 - 2 A__ = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) A__ = min(__a , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders A__ = pre_process_datasets(__a , __a , __a , *__a ) A__ , A__ = tensor_datasets[0], tensor_datasets[1] A__ = TensorDataset(*__a ) A__ = RandomSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.train_batch_size ) A__ = TensorDataset(*__a ) A__ = SequentialSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: A__ = args.max_steps A__ = args.max_steps // (len(__a ) // args.gradient_accumulation_steps) + 1 else: A__ = len(__a ) // args.gradient_accumulation_steps * args.num_train_epochs A__ = list(model.named_parameters() ) A__ = ["""bias""", """LayerNorm.bias""", """LayerNorm.weight"""] A__ = [ { """params""": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], """weight_decay""": args.weight_decay, }, {"""params""": [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], """weight_decay""": 0.0}, ] A__ = AdamW(__a , lr=args.learning_rate , eps=args.adam_epsilon ) A__ = get_linear_schedule_with_warmup( __a , num_warmup_steps=args.warmup_steps , num_training_steps=__a ) if args.do_train: A__ , A__ , A__ = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="""Epoch""" ): A__ = 0 A__ = 0 A__ = tqdm(__a , desc="""Training""" ) for step, batch in enumerate(__a ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch A__ = model(__a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() A__ = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 A__ = """Training loss: {:.2e} lr: {:.2e}""".format(__a , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer A__ = model.module if hasattr(__a , """module""" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` A__ = os.path.join(args.output_dir , __a ) A__ = os.path.join(args.output_dir , __a ) torch.save(model_to_save.state_dict() , __a ) model_to_save.config.to_json_file(__a ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) A__ = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(__a ) if args.do_eval: model.eval() A__ , A__ = 0, 0 A__ , A__ = 0, 0 for batch in tqdm(__a , desc="""Evaluating""" ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch with torch.no_grad(): A__ , A__ , A__ , A__ = model( __a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = mc_logits.detach().cpu().numpy() A__ = mc_labels.to("""cpu""" ).numpy() A__ = accuracy(__a , __a ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 A__ = eval_loss / nb_eval_steps A__ = eval_accuracy / nb_eval_examples A__ = tr_loss / nb_tr_steps if args.do_train else None A__ = {"""eval_loss""": eval_loss, """eval_accuracy""": eval_accuracy, """train_loss""": train_loss} A__ = os.path.join(args.output_dir , """eval_results.txt""" ) with open(__a , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key in sorted(result.keys() ): logger.info(""" %s = %s""" , __a , str(result[key] ) ) writer.write("""%s = %s\n""" % (key, str(result[key] )) ) if __name__ == "__main__": main()
274
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 A : Optional[Any] = '''scheduler_config.json''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[Any] = 1 __lowerCamelCase : Optional[int] = 2 __lowerCamelCase : List[Any] = 3 __lowerCamelCase : Any = 4 __lowerCamelCase : List[str] = 5 __lowerCamelCase : Any = 6 __lowerCamelCase : List[str] = 7 __lowerCamelCase : int = 8 __lowerCamelCase : List[Any] = 9 __lowerCamelCase : Optional[int] = 10 __lowerCamelCase : int = 11 __lowerCamelCase : Union[str, Any] = 12 __lowerCamelCase : List[Any] = 13 __lowerCamelCase : str = 14 @dataclass class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : torch.FloatTensor class A : '''simple docstring''' __lowerCamelCase : Tuple = SCHEDULER_CONFIG_NAME __lowerCamelCase : Optional[Any] = [] __lowerCamelCase : Tuple = True @classmethod def a_ ( cls : Optional[int] , __lowerCAmelCase : Dict[str, Any] = None , __lowerCAmelCase : Optional[str] = None , __lowerCAmelCase : str=False , **__lowerCAmelCase : Optional[Any] , ) -> str: """simple docstring""" A__ , A__ , A__ = cls.load_config( pretrained_model_name_or_path=__lowerCAmelCase , subfolder=__lowerCAmelCase , return_unused_kwargs=__lowerCAmelCase , return_commit_hash=__lowerCAmelCase , **__lowerCAmelCase , ) return cls.from_config(__lowerCAmelCase , return_unused_kwargs=__lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Optional[int] , __lowerCAmelCase : Union[str, os.PathLike] , __lowerCAmelCase : bool = False , **__lowerCAmelCase : Dict ) -> Dict: """simple docstring""" self.save_config(save_directory=__lowerCAmelCase , push_to_hub=__lowerCAmelCase , **__lowerCAmelCase ) @property def a_ ( self : int ) -> str: """simple docstring""" return self._get_compatibles() @classmethod def a_ ( cls : Optional[Any] ) -> int: """simple docstring""" A__ = list(set([cls.__name__] + cls._compatibles ) ) A__ = importlib.import_module(__name__.split(""".""" )[0] ) A__ = [ getattr(__lowerCAmelCase , __lowerCAmelCase ) for c in compatible_classes_str if hasattr(__lowerCAmelCase , __lowerCAmelCase ) ] return compatible_classes
274
import argparse from collections import defaultdict import yaml A : str = '''docs/source/en/_toctree.yml''' def __lowerCamelCase ( __a :str ) -> List[Any]: """simple docstring""" A__ = defaultdict(__a ) A__ = [] A__ = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__a ) A__ = new_doc_list A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__a ) > 1: raise ValueError( F'{duplicate_key} is present several times in the documentation table of content at ' """`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the """ """others.""" ) # Only add this once new_doc.append({"""local""": duplicate_key, """title""": titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) A__ = sorted(__a , key=lambda __a : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__a ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__a ) # Sort return overview_doc def __lowerCamelCase ( __a :Any=False ) -> List[str]: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 A__ = api_doc[scheduler_idx]["""sections"""] A__ = clean_doc_toc(__a ) A__ = False if new_scheduler_doc != scheduler_doc: A__ = True if overwrite: A__ = new_scheduler_doc if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def __lowerCamelCase ( __a :Optional[int]=False ) -> Dict: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 A__ = False A__ = api_doc[pipeline_idx]["""sections"""] A__ = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: A__ = pipeline_doc["""section"""] A__ = clean_doc_toc(__a ) if overwrite: A__ = new_sub_pipeline_doc new_pipeline_docs.append(__a ) # sort overall pipeline doc A__ = clean_doc_toc(__a ) if new_pipeline_docs != pipeline_docs: A__ = True if overwrite: A__ = new_pipeline_docs if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) if __name__ == "__main__": A : Tuple = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') A : Optional[Any] = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
274
1
import requests A : List[Any] = '''''' # <-- Put your OpenWeatherMap appid here! A : Dict = '''https://api.openweathermap.org/data/2.5/''' def __lowerCamelCase ( __a :str = "Chicago" , __a :str = APPID ) -> dict: """simple docstring""" return requests.get(URL_BASE + """weather""" , params=locals() ).json() def __lowerCamelCase ( __a :str = "Kolkata, India" , __a :str = APPID ) -> dict: """simple docstring""" return requests.get(URL_BASE + """forecast""" , params=locals() ).json() def __lowerCamelCase ( __a :float = 55.68 , __a :float = 12.57 , __a :str = APPID ) -> dict: """simple docstring""" return requests.get(URL_BASE + """onecall""" , params=locals() ).json() if __name__ == "__main__": from pprint import pprint while True: A : int = input('''Enter a location:''').strip() if location: pprint(current_weather(location)) else: break
274
def __lowerCamelCase ( __a :str ) -> list: """simple docstring""" A__ = [0] * len(__a ) for i in range(1 , len(__a ) ): # use last results for better performance - dynamic programming A__ = prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: A__ = prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 A__ = j return prefix_result def __lowerCamelCase ( __a :str ) -> int: """simple docstring""" return max(prefix_function(__a ) ) if __name__ == "__main__": import doctest doctest.testmod()
274
1
import logging import os import threading import time try: import warnings except ImportError: A : Optional[Any] = None try: import msvcrt except ImportError: A : List[Any] = None try: import fcntl except ImportError: A : Optional[Any] = None # Backward compatibility # ------------------------------------------------ try: TimeoutError except NameError: A : Optional[Any] = OSError # Data # ------------------------------------------------ A : Optional[Any] = [ '''Timeout''', '''BaseFileLock''', '''WindowsFileLock''', '''UnixFileLock''', '''SoftFileLock''', '''FileLock''', ] A : Optional[Any] = '''3.0.12''' A : str = None def __lowerCamelCase ( ) -> Any: """simple docstring""" global _logger A__ = _logger or logging.getLogger(__name__ ) return _logger class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : List[Any] , __lowerCAmelCase : Optional[Any] ) -> List[str]: """simple docstring""" A__ = lock_file return None def __str__( self : Union[str, Any] ) -> Any: """simple docstring""" A__ = f'The file lock \'{self.lock_file}\' could not be acquired.' return temp class A : '''simple docstring''' def __init__( self : List[Any] , __lowerCAmelCase : Dict ) -> str: """simple docstring""" A__ = lock return None def __enter__( self : str ) -> Dict: """simple docstring""" return self.lock def __exit__( self : str , __lowerCAmelCase : int , __lowerCAmelCase : Any , __lowerCAmelCase : Dict ) -> Any: """simple docstring""" self.lock.release() return None class A : '''simple docstring''' def __init__( self : Optional[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[Any]=-1 , __lowerCAmelCase : Optional[int]=None ) -> Union[str, Any]: """simple docstring""" A__ = max_filename_length if max_filename_length is not None else 2_55 # Hash the filename if it's too long A__ = self.hash_filename_if_too_long(__lowerCAmelCase , __lowerCAmelCase ) # The path to the lock file. A__ = lock_file # The file descriptor for the *_lock_file* as it is returned by the # os.open() function. # This file lock is only NOT None, if the object currently holds the # lock. A__ = None # The default timeout value. A__ = timeout # We use this lock primarily for the lock counter. A__ = threading.Lock() # The lock counter is used for implementing the nested locking # mechanism. Whenever the lock is acquired, the counter is increased and # the lock is only released, when this value is 0 again. A__ = 0 return None @property def a_ ( self : List[Any] ) -> List[Any]: """simple docstring""" return self._lock_file @property def a_ ( self : Any ) -> int: """simple docstring""" return self._timeout @timeout.setter def a_ ( self : List[str] , __lowerCAmelCase : List[Any] ) -> Any: """simple docstring""" A__ = float(__lowerCAmelCase ) return None def a_ ( self : int ) -> int: """simple docstring""" raise NotImplementedError() def a_ ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" raise NotImplementedError() @property def a_ ( self : List[Any] ) -> List[str]: """simple docstring""" return self._lock_file_fd is not None def a_ ( self : int , __lowerCAmelCase : str=None , __lowerCAmelCase : Optional[Any]=0.0_5 ) -> List[str]: """simple docstring""" if timeout is None: A__ = self.timeout # Increment the number right at the beginning. # We can still undo it, if something fails. with self._thread_lock: self._lock_counter += 1 A__ = id(self ) A__ = self._lock_file A__ = time.time() try: while True: with self._thread_lock: if not self.is_locked: logger().debug(f'Attempting to acquire lock {lock_id} on {lock_filename}' ) self._acquire() if self.is_locked: logger().debug(f'Lock {lock_id} acquired on {lock_filename}' ) break elif timeout >= 0 and time.time() - start_time > timeout: logger().debug(f'Timeout on acquiring lock {lock_id} on {lock_filename}' ) raise Timeout(self._lock_file ) else: logger().debug( f'Lock {lock_id} not acquired on {lock_filename}, waiting {poll_intervall} seconds ...' ) time.sleep(__lowerCAmelCase ) except: # noqa # Something did go wrong, so decrement the counter. with self._thread_lock: A__ = max(0 , self._lock_counter - 1 ) raise return _Acquire_ReturnProxy(lock=self ) def a_ ( self : Union[str, Any] , __lowerCAmelCase : int=False ) -> Tuple: """simple docstring""" with self._thread_lock: if self.is_locked: self._lock_counter -= 1 if self._lock_counter == 0 or force: A__ = id(self ) A__ = self._lock_file logger().debug(f'Attempting to release lock {lock_id} on {lock_filename}' ) self._release() A__ = 0 logger().debug(f'Lock {lock_id} released on {lock_filename}' ) return None def __enter__( self : Dict ) -> Union[str, Any]: """simple docstring""" self.acquire() return self def __exit__( self : Optional[Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Any ) -> Any: """simple docstring""" self.release() return None def __del__( self : Tuple ) -> Union[str, Any]: """simple docstring""" self.release(force=__lowerCAmelCase ) return None def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : int ) -> str: """simple docstring""" A__ = os.path.basename(__lowerCAmelCase ) if len(__lowerCAmelCase ) > max_length and max_length > 0: A__ = os.path.dirname(__lowerCAmelCase ) A__ = str(hash(__lowerCAmelCase ) ) A__ = filename[: max_length - len(__lowerCAmelCase ) - 8] + """...""" + hashed_filename + """.lock""" return os.path.join(__lowerCAmelCase , __lowerCAmelCase ) else: return path class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Tuple , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Any=-1 , __lowerCAmelCase : List[Any]=None ) -> List[Any]: """simple docstring""" from .file_utils import relative_to_absolute_path super().__init__(__lowerCAmelCase , timeout=__lowerCAmelCase , max_filename_length=__lowerCAmelCase ) A__ = """\\\\?\\""" + relative_to_absolute_path(self.lock_file ) def a_ ( self : Tuple ) -> Optional[int]: """simple docstring""" A__ = os.O_RDWR | os.O_CREAT | os.O_TRUNC try: A__ = os.open(self._lock_file , __lowerCAmelCase ) except OSError: pass else: try: msvcrt.locking(__lowerCAmelCase , msvcrt.LK_NBLCK , 1 ) except OSError: os.close(__lowerCAmelCase ) else: A__ = fd return None def a_ ( self : Any ) -> Optional[int]: """simple docstring""" A__ = self._lock_file_fd A__ = None msvcrt.locking(__lowerCAmelCase , msvcrt.LK_UNLCK , 1 ) os.close(__lowerCAmelCase ) try: os.remove(self._lock_file ) # Probably another instance of the application # that acquired the file lock. except OSError: pass return None class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[int]=-1 , __lowerCAmelCase : Dict=None ) -> Union[str, Any]: """simple docstring""" A__ = os.statvfs(os.path.dirname(__lowerCAmelCase ) ).f_namemax super().__init__(__lowerCAmelCase , timeout=__lowerCAmelCase , max_filename_length=__lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" A__ = os.O_RDWR | os.O_CREAT | os.O_TRUNC A__ = os.open(self._lock_file , __lowerCAmelCase ) try: fcntl.flock(__lowerCAmelCase , fcntl.LOCK_EX | fcntl.LOCK_NB ) except OSError: os.close(__lowerCAmelCase ) else: A__ = fd return None def a_ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" A__ = self._lock_file_fd A__ = None fcntl.flock(__lowerCAmelCase , fcntl.LOCK_UN ) os.close(__lowerCAmelCase ) return None class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def a_ ( self : Optional[Any] ) -> str: """simple docstring""" A__ = os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_TRUNC try: A__ = os.open(self._lock_file , __lowerCAmelCase ) except OSError: pass else: A__ = fd return None def a_ ( self : Dict ) -> Tuple: """simple docstring""" os.close(self._lock_file_fd ) A__ = None try: os.remove(self._lock_file ) # The file is already deleted and that's what we want. except OSError: pass return None A : Optional[Any] = None if msvcrt: A : List[str] = WindowsFileLock elif fcntl: A : Tuple = UnixFileLock else: A : Tuple = SoftFileLock if warnings is not None: warnings.warn('''only soft file lock is available''')
274
def __lowerCamelCase ( __a :int = 1_0_0_0_0_0_0 ) -> int: """simple docstring""" A__ = limit + 1 A__ = [0] * limit for first_term in range(1 , __a ): for n in range(__a , __a , __a ): A__ = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a A__ = sum(1 for x in frequency[1:limit] if x == 1_0 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
274
1
import pytest from datasets.parallel import ParallelBackendConfig, parallel_backend from datasets.utils.py_utils import map_nested from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows def __lowerCamelCase ( __a :Optional[Any] ) -> str: # picklable for multiprocessing """simple docstring""" return i + 1 @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows def __lowerCamelCase ( ) -> Dict: """simple docstring""" with parallel_backend("""spark""" ): assert ParallelBackendConfig.backend_name == "spark" A__ = [1, 2, 3] with pytest.raises(__a ): with parallel_backend("""unsupported backend""" ): map_nested(__a , __a , num_proc=2 ) with pytest.raises(__a ): with parallel_backend("""unsupported backend""" ): map_nested(__a , __a , num_proc=-1 ) @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows @pytest.mark.parametrize("""num_proc""" , [2, -1] ) def __lowerCamelCase ( __a :str ) -> Tuple: """simple docstring""" A__ = [1, 2] A__ = {"""a""": 1, """b""": 2} A__ = {"""a""": [1, 2], """b""": [3, 4]} A__ = {"""a""": {"""1""": 1}, """b""": 2} A__ = {"""a""": 1, """b""": 2, """c""": 3, """d""": 4} A__ = [2, 3] A__ = {"""a""": 2, """b""": 3} A__ = {"""a""": [2, 3], """b""": [4, 5]} A__ = {"""a""": {"""1""": 2}, """b""": 3} A__ = {"""a""": 2, """b""": 3, """c""": 4, """d""": 5} with parallel_backend("""spark""" ): assert map_nested(__a , __a , num_proc=__a ) == expected_map_nested_sa assert map_nested(__a , __a , num_proc=__a ) == expected_map_nested_sa assert map_nested(__a , __a , num_proc=__a ) == expected_map_nested_sa assert map_nested(__a , __a , num_proc=__a ) == expected_map_nested_sa assert map_nested(__a , __a , num_proc=__a ) == expected_map_nested_sa
274
class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A : '''simple docstring''' def __init__( self : List[Any] ) -> str: """simple docstring""" A__ = [ [], [], [], ] def a_ ( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" try: if len(self.queues[priority] ) >= 1_00: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(__lowerCAmelCase ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def a_ ( self : Optional[Any] ) -> int: """simple docstring""" for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self : Tuple ) -> str: """simple docstring""" return "\n".join(f'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class A : '''simple docstring''' def __init__( self : int ) -> str: """simple docstring""" A__ = [] def a_ ( self : int , __lowerCAmelCase : int ) -> None: """simple docstring""" if len(self.queue ) == 1_00: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(__lowerCAmelCase ) def a_ ( self : List[str] ) -> int: """simple docstring""" if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: A__ = min(self.queue ) self.queue.remove(__lowerCAmelCase ) return data def __str__( self : List[Any] ) -> str: """simple docstring""" return str(self.queue ) def __lowerCamelCase ( ) -> Optional[Any]: """simple docstring""" A__ = FixedPriorityQueue() fpq.enqueue(0 , 1_0 ) fpq.enqueue(1 , 7_0 ) fpq.enqueue(0 , 1_0_0 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 6_4 ) fpq.enqueue(0 , 1_2_8 ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __lowerCamelCase ( ) -> int: """simple docstring""" A__ = ElementPriorityQueue() epq.enqueue(1_0 ) epq.enqueue(7_0 ) epq.enqueue(1_0_0 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(6_4 ) epq.enqueue(1_2_8 ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
274
1
import gc import unittest from parameterized import parameterized from diffusers import FlaxUNetaDConditionModel from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow if is_flax_available(): import jax import jax.numpy as jnp @slow @require_flax class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Tuple , __lowerCAmelCase : Any , __lowerCAmelCase : Union[str, Any] ) -> Any: """simple docstring""" return f'gaussian_noise_s={seed}_shape={"_".join([str(__lowerCAmelCase ) for s in shape] )}.npy' def a_ ( self : Any ) -> Tuple: """simple docstring""" super().tearDown() gc.collect() def a_ ( self : Union[str, Any] , __lowerCAmelCase : Optional[int]=0 , __lowerCAmelCase : str=(4, 4, 64, 64) , __lowerCAmelCase : Tuple=False ) -> Tuple: """simple docstring""" A__ = jnp.bfloataa if fpaa else jnp.floataa A__ = jnp.array(load_hf_numpy(self.get_file_format(__lowerCAmelCase , __lowerCAmelCase ) ) , dtype=__lowerCAmelCase ) return image def a_ ( self : List[Any] , __lowerCAmelCase : Tuple=False , __lowerCAmelCase : List[str]="CompVis/stable-diffusion-v1-4" ) -> Dict: """simple docstring""" A__ = jnp.bfloataa if fpaa else jnp.floataa A__ = """bf16""" if fpaa else None A__ , A__ = FlaxUNetaDConditionModel.from_pretrained( __lowerCAmelCase , subfolder="""unet""" , dtype=__lowerCAmelCase , revision=__lowerCAmelCase ) return model, params def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[str]=0 , __lowerCAmelCase : str=(4, 77, 7_68) , __lowerCAmelCase : int=False ) -> Optional[Any]: """simple docstring""" A__ = jnp.bfloataa if fpaa else jnp.floataa A__ = jnp.array(load_hf_numpy(self.get_file_format(__lowerCAmelCase , __lowerCAmelCase ) ) , dtype=__lowerCAmelCase ) return hidden_states @parameterized.expand( [ # fmt: off [83, 4, [-0.2_3_2_3, -0.1_3_0_4, 0.0_8_1_3, -0.3_0_9_3, -0.0_9_1_9, -0.1_5_7_1, -0.1_1_2_5, -0.5_8_0_6]], [17, 0.5_5, [-0.0_8_3_1, -0.2_4_4_3, 0.0_9_0_1, -0.0_9_1_9, 0.3_3_9_6, 0.0_1_0_3, -0.3_7_4_3, 0.0_7_0_1]], [8, 0.8_9, [-0.4_8_6_3, 0.0_8_5_9, 0.0_8_7_5, -0.1_6_5_8, 0.9_1_9_9, -0.0_1_1_4, 0.4_8_3_9, 0.4_6_3_9]], [3, 10_00, [-0.5_6_4_9, 0.2_4_0_2, -0.5_5_1_8, 0.1_2_4_8, 1.1_3_2_8, -0.2_4_4_3, -0.0_3_2_5, -1.0_0_7_8]], # fmt: on ] ) def a_ ( self : Optional[int] , __lowerCAmelCase : str , __lowerCAmelCase : str , __lowerCAmelCase : int ) -> Dict: """simple docstring""" A__ , A__ = self.get_unet_model(model_id="""CompVis/stable-diffusion-v1-4""" , fpaa=__lowerCAmelCase ) A__ = self.get_latents(__lowerCAmelCase , fpaa=__lowerCAmelCase ) A__ = self.get_encoder_hidden_states(__lowerCAmelCase , fpaa=__lowerCAmelCase ) A__ = model.apply( {"""params""": params} , __lowerCAmelCase , jnp.array(__lowerCAmelCase , dtype=jnp.intaa ) , encoder_hidden_states=__lowerCAmelCase , ).sample assert sample.shape == latents.shape A__ = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) A__ = jnp.array(__lowerCAmelCase , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware assert jnp.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-2 ) @parameterized.expand( [ # fmt: off [83, 4, [0.1_5_1_4, 0.0_8_0_7, 0.1_6_2_4, 0.1_0_1_6, -0.1_8_9_6, 0.0_2_6_3, 0.0_6_7_7, 0.2_3_1_0]], [17, 0.5_5, [0.1_1_6_4, -0.0_2_1_6, 0.0_1_7_0, 0.1_5_8_9, -0.3_1_2_0, 0.1_0_0_5, -0.0_5_8_1, -0.1_4_5_8]], [8, 0.8_9, [-0.1_7_5_8, -0.0_1_6_9, 0.1_0_0_4, -0.1_4_1_1, 0.1_3_1_2, 0.1_1_0_3, -0.1_9_9_6, 0.2_1_3_9]], [3, 10_00, [0.1_2_1_4, 0.0_3_5_2, -0.0_7_3_1, -0.1_5_6_2, -0.0_9_9_4, -0.0_9_0_6, -0.2_3_4_0, -0.0_5_3_9]], # fmt: on ] ) def a_ ( self : Dict , __lowerCAmelCase : Dict , __lowerCAmelCase : str , __lowerCAmelCase : Dict ) -> Optional[Any]: """simple docstring""" A__ , A__ = self.get_unet_model(model_id="""stabilityai/stable-diffusion-2""" , fpaa=__lowerCAmelCase ) A__ = self.get_latents(__lowerCAmelCase , shape=(4, 4, 96, 96) , fpaa=__lowerCAmelCase ) A__ = self.get_encoder_hidden_states(__lowerCAmelCase , shape=(4, 77, 10_24) , fpaa=__lowerCAmelCase ) A__ = model.apply( {"""params""": params} , __lowerCAmelCase , jnp.array(__lowerCAmelCase , dtype=jnp.intaa ) , encoder_hidden_states=__lowerCAmelCase , ).sample assert sample.shape == latents.shape A__ = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) A__ = jnp.array(__lowerCAmelCase , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware assert jnp.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-2 )
274
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = tempfile.mkdtemp() # fmt: off A__ = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on A__ = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) A__ = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""] A__ = {"""unk_token""": """<unk>"""} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(__lowerCAmelCase ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__lowerCAmelCase ) ) A__ = { """do_resize""": True, """size""": 20, """do_center_crop""": True, """crop_size""": 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], } A__ = os.path.join(self.tmpdirname , __lowerCAmelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Tuple , **__lowerCAmelCase : Dict ) -> str: """simple docstring""" return CLIPTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : Union[str, Any] , **__lowerCAmelCase : Dict ) -> List[str]: """simple docstring""" return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : List[str] , **__lowerCAmelCase : Optional[Any] ) -> Dict: """simple docstring""" return CLIPImageProcessor.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : str ) -> Dict: """simple docstring""" shutil.rmtree(self.tmpdirname ) def a_ ( self : str ) -> Any: """simple docstring""" A__ = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] A__ = [Image.fromarray(np.moveaxis(__lowerCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def a_ ( self : Optional[int] ) -> Tuple: """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_rust_tokenizer() A__ = self.get_image_processor() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_slow.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname , use_fast=__lowerCAmelCase ) A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_fast.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , __lowerCAmelCase ) self.assertIsInstance(processor_fast.tokenizer , __lowerCAmelCase ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , __lowerCAmelCase ) self.assertIsInstance(processor_fast.image_processor , __lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" A__ = CLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=__lowerCAmelCase , padding_value=1.0 ) A__ = CLIPProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__lowerCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowerCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCAmelCase ) def a_ ( self : List[Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(__lowerCAmelCase , return_tensors="""np""" ) A__ = processor(images=__lowerCAmelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def a_ ( self : Optional[Any] ) -> Any: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = processor(text=__lowerCAmelCase ) A__ = tokenizer(__lowerCAmelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(__lowerCAmelCase ): processor() def a_ ( self : Tuple ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.batch_decode(__lowerCAmelCase ) A__ = tokenizer.batch_decode(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Optional[int] ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
274
1
import argparse import json import os import tensorstore as ts import torch from flax import serialization from flax.traverse_util import flatten_dict, unflatten_dict from tensorflow.io import gfile from transformers.modeling_utils import dtype_byte_size from transformers.models.switch_transformers.convert_switch_transformers_original_flax_checkpoint_to_pytorch import ( rename_keys, ) from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME from transformers.utils.hub import convert_file_size_to_int def __lowerCamelCase ( __a :Union[str, Any] , __a :int ) -> Tuple: """simple docstring""" if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 3: # expert layer A__ = flax_key_tuple[:-1] + ("""weight""",) A__ = torch.permute(__a , (0, 2, 1) ) elif flax_key_tuple[-1] == "kernel" and ".".join(__a ): # linear layer A__ = flax_key_tuple[:-1] + ("""weight""",) A__ = flax_tensor.T elif flax_key_tuple[-1] in ["scale", "embedding"]: A__ = flax_key_tuple[:-1] + ("""weight""",) return flax_key_tuple, flax_tensor def __lowerCamelCase ( __a :str , __a :Tuple , __a :Tuple ) -> Optional[int]: """simple docstring""" if "metadata" in layer: A__ = layer.split("""metadata""" ) A__ = """""".join(split_layer[0] )[:-1] A__ = [tuple(("""metadata""" + split_layer[1]).split("""/""" ) )] elif "kvstore" in layer: A__ = layer.split("""kvstore""" ) A__ = """""".join(split_layer[0] )[:-1] A__ = [tuple(("""kvstore""" + split_layer[1]).split("""/""" ) )] else: A__ = layer.split("""/""" ) A__ = """/""".join(split_layer[:-1] ) A__ = (split_layer[-1],) if "kvstore/path" in layer: A__ = F'{switch_checkpoint_path}/{checkpoint_info[layer]}' elif "kvstore/driver" in layer: A__ = """file""" else: A__ = checkpoint_info[layer] return curr_real_layer_name, split_layer, content def __lowerCamelCase ( __a :Optional[int] , __a :Optional[int] ) -> Optional[int]: """simple docstring""" A__ = rename_keys(__a ) A__ = {} for k, v in current_block.items(): A__ = v A__ = new_current_block torch.save(__a , __a ) def __lowerCamelCase ( __a :Dict , __a :Optional[Any] , __a :Union[str, Any] , __a :Optional[Any] , __a :str = WEIGHTS_NAME ) -> int: """simple docstring""" A__ = convert_file_size_to_int(__a ) A__ = [] A__ = {} A__ = 0 A__ = 0 os.makedirs(__a , exist_ok=__a ) with gfile.GFile(switch_checkpoint_path + """/checkpoint""" , """rb""" ) as fp: A__ = serialization.msgpack_restore(fp.read() )["""optimizer"""]["""target"""] A__ = flatten_dict(__a , sep="""/""" ) A__ = {} for layer in checkpoint_info.keys(): A__ , A__ , A__ = get_key_and_tensorstore_dict( __a , __a , __a ) if curr_real_layer_name in all_layers: A__ = content else: A__ = {split_layer[-1]: content} for key in all_layers.keys(): # open tensorstore file A__ = ts.open(unflatten_dict(all_layers[key] ) ).result().read().result() A__ = torch.tensor(__a ) A__ = raw_weights.numel() * dtype_byte_size(raw_weights.dtype ) # use the renaming pattern from the small conversion scripts A__ , A__ = rename_base_flax_keys(tuple(key.split("""/""" ) ) , __a ) A__ = """/""".join(__a ) # If this weight is going to tip up over the maximal size, we split. if current_block_size + weight_size > max_shard_size: A__ = os.path.join( __a , weights_name.replace(""".bin""" , F'-{len(__a )+1:05d}-of-???.bin' ) ) rename_and_save_block(__a , __a ) sharded_state_dicts.append(current_block.keys() ) del current_block A__ = {} A__ = 0 A__ = raw_weights.to(getattr(__a , __a ) ) current_block_size += weight_size total_size += weight_size # Add the last block A__ = os.path.join(__a , weights_name.replace(""".bin""" , F'-{len(__a )+1:05d}-of-???.bin' ) ) rename_and_save_block(__a , __a ) sharded_state_dicts.append(current_block.keys() ) # If we only have one shard, we return it if len(__a ) == 1: return {weights_name: sharded_state_dicts[0]}, None # Otherwise, let's build the index A__ = {} A__ = {} for idx, shard in enumerate(__a ): A__ = weights_name.replace( """.bin""" , F'-{idx+1:05d}-of-{len(__a ):05d}.bin' ) # len(sharded_state_dicts):05d} A__ = os.path.join(__a , weights_name.replace(""".bin""" , F'-{idx+1:05d}-of-???.bin' ) ) os.rename(__a , os.path.join(__a , __a ) ) A__ = shard for key in shard: A__ = shard_file # Add the metadata A__ = {"""total_size""": total_size} A__ = {"""metadata""": metadata, """weight_map""": weight_map} with open(os.path.join(__a , __a ) , """w""" , encoding="""utf-8""" ) as f: A__ = json.dumps(__a , indent=2 , sort_keys=__a ) + """\n""" f.write(__a ) return metadata, index if __name__ == "__main__": A : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--switch_t5x_checkpoint_path''', default='''/mnt/disks/disk_switch/original_checkpoints/switch-xxl-128/checkpoint_634600''', type=str, required=False, help='''Path to a directory containing a folder per layer. Follows the original Google format.''', ) parser.add_argument('''--max_shard_size''', default='''10GB''', required=False, help='''Max shard size''') parser.add_argument('''--dtype''', default='''bfloat16''', type=str, required=False, help='''dtype of the saved model''') parser.add_argument( '''--pytorch_dump_folder_path''', default='''/mnt/disks/disk_switch/original_checkpoints/switch-xxl-128-converted''', type=str, required=False, help='''Path to the output pytorch model.''', ) A : List[Any] = parser.parse_args() shard_on_the_fly( args.switch_tax_checkpoint_path, args.pytorch_dump_folder_path, args.max_shard_size, args.dtype, ) def __lowerCamelCase ( ) -> int: """simple docstring""" from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration, TaTokenizer A__ = SwitchTransformersConfig.from_pretrained("""google/switch-base-8""" ) config.save_pretrained("""/home/arthur_huggingface_co/transformers/switch_converted""" ) A__ = SwitchTransformersForConditionalGeneration.from_pretrained( """/home/arthur_huggingface_co/transformers/switch_converted""" , device_map="""auto""" ) A__ = TaTokenizer.from_pretrained("""t5-small""" ) A__ = """A <extra_id_0> walks into a bar a orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.""" A__ = tokenizer(__a , return_tensors="""pt""" ).input_ids A__ = model.generate(__a , decoder_start_token_id=0 ) print(tokenizer.decode(out[0] ) )
274
from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time A : Dict = Lock() def __lowerCamelCase ( __a :Dict , __a :List[str] , __a :Optional[int] , __a :Optional[int] , __a :Optional[Any] , __a :Optional[int] , __a :int ) -> Dict: """simple docstring""" global process_lock # we perform n swaps since after n swaps we know we are sorted # we *could* stop early if we are sorted already, but it takes as long to # find out we are sorted as it does to sort the list with this algorithm for i in range(0 , 1_0 ): if (i + position) % 2 == 0 and r_send is not None: # send your value to your right neighbor process_lock.acquire() r_send[1].send(__a ) process_lock.release() # receive your right neighbor's value process_lock.acquire() A__ = rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left A__ = min(__a , __a ) elif (i + position) % 2 != 0 and l_send is not None: # send your value to your left neighbor process_lock.acquire() l_send[1].send(__a ) process_lock.release() # receive your left neighbor's value process_lock.acquire() A__ = lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right A__ = max(__a , __a ) # after all swaps are performed, send the values back to main result_pipe[1].send(__a ) def __lowerCamelCase ( __a :List[str] ) -> int: """simple docstring""" A__ = [] A__ = [] # initialize the list of pipes where the values will be retrieved for _ in arr: result_pipe.append(Pipe() ) # creates the processes # the first and last process only have one neighbor so they are made outside # of the loop A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) A__ = temp_rs A__ = temp_rr for i in range(1 , len(__a ) - 1 ): A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) A__ = temp_rs A__ = temp_rr process_array_.append( Process( target=__a , args=( len(__a ) - 1, arr[len(__a ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(__a ) - 1], ) , ) ) # start the processes for p in process_array_: p.start() # wait for the processes to end and write their values to the list for p in range(0 , len(__a ) ): A__ = result_pipe[p][0].recv() process_array_[p].join() return arr def __lowerCamelCase ( ) -> str: """simple docstring""" A__ = list(range(1_0 , 0 , -1 ) ) print("""Initial List""" ) print(*__a ) A__ = odd_even_transposition(__a ) print("""Sorted List\n""" ) print(*__a ) if __name__ == "__main__": main()
274
1
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import KarrasVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : UNetaDModel __lowerCamelCase : KarrasVeScheduler def __init__( self : Optional[Any] , __lowerCAmelCase : UNetaDModel , __lowerCAmelCase : KarrasVeScheduler ) -> Optional[Any]: """simple docstring""" super().__init__() self.register_modules(unet=__lowerCAmelCase , scheduler=__lowerCAmelCase ) @torch.no_grad() def __call__( self : Optional[int] , __lowerCAmelCase : int = 1 , __lowerCAmelCase : int = 50 , __lowerCAmelCase : Optional[Union[torch.Generator, List[torch.Generator]]] = None , __lowerCAmelCase : Optional[str] = "pil" , __lowerCAmelCase : bool = True , **__lowerCAmelCase : Union[str, Any] , ) -> Union[Tuple, ImagePipelineOutput]: """simple docstring""" A__ = self.unet.config.sample_size A__ = (batch_size, 3, img_size, img_size) A__ = self.unet # sample x_0 ~ N(0, sigma_0^2 * I) A__ = randn_tensor(__lowerCAmelCase , generator=__lowerCAmelCase , device=self.device ) * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(__lowerCAmelCase ) for t in self.progress_bar(self.scheduler.timesteps ): # here sigma_t == t_i from the paper A__ = self.scheduler.schedule[t] A__ = self.scheduler.schedule[t - 1] if t > 0 else 0 # 1. Select temporarily increased noise level sigma_hat # 2. Add new noise to move from sample_i to sample_hat A__ , A__ = self.scheduler.add_noise_to_input(__lowerCAmelCase , __lowerCAmelCase , generator=__lowerCAmelCase ) # 3. Predict the noise residual given the noise magnitude `sigma_hat` # The model inputs and output are adjusted by following eq. (213) in [1]. A__ = (sigma_hat / 2) * model((sample_hat + 1) / 2 , sigma_hat / 2 ).sample # 4. Evaluate dx/dt at sigma_hat # 5. Take Euler step from sigma to sigma_prev A__ = self.scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) if sigma_prev != 0: # 6. Apply 2nd order correction # The model inputs and output are adjusted by following eq. (213) in [1]. A__ = (sigma_prev / 2) * model((step_output.prev_sample + 1) / 2 , sigma_prev / 2 ).sample A__ = self.scheduler.step_correct( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , step_output.prev_sample , step_output["""derivative"""] , ) A__ = step_output.prev_sample A__ = (sample / 2 + 0.5).clamp(0 , 1 ) A__ = sample.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": A__ = self.numpy_to_pil(__lowerCAmelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=__lowerCAmelCase )
274
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def __lowerCamelCase ( __a :Dict ) -> Any: """simple docstring""" A__ = [ """decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(__a , __a ) def __lowerCamelCase ( __a :str ) -> Union[str, Any]: """simple docstring""" A__ , A__ = emb.weight.shape A__ = nn.Linear(__a , __a , bias=__a ) A__ = emb.weight.data return lin_layer def __lowerCamelCase ( __a :str ) -> List[str]: """simple docstring""" A__ = torch.load(__a , map_location="""cpu""" ) A__ = Namespace(**checkpoint["""cfg"""]["""model"""] ) A__ = checkpoint["""model"""] remove_ignore_keys_(__a ) A__ = state_dict["""decoder.embed_tokens.weight"""].shape[0] A__ = {key.replace("""decoder""" , """model""" ): val for key, val in state_dict.items()} A__ = XGLMConfig( vocab_size=__a , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""gelu""" , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) A__ = XGLMForCausalLM(__a ) A__ = model.load_state_dict(__a , strict=__a ) print(__a ) A__ = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": A : int = argparse.ArgumentParser() # Required parameters parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') A : str = parser.parse_args() A : str = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
274
1
import math from ...configuration_utils import PretrainedConfig from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) A : Tuple = { '''facebook/data2vec-base-960h''': '''https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json''', # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Tuple = '''data2vec-audio''' def __init__( self : Tuple , __lowerCAmelCase : str=32 , __lowerCAmelCase : str=7_68 , __lowerCAmelCase : Optional[int]=12 , __lowerCAmelCase : Tuple=12 , __lowerCAmelCase : str=30_72 , __lowerCAmelCase : Tuple="gelu" , __lowerCAmelCase : int=0.1 , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : Union[str, Any]=0.1 , __lowerCAmelCase : str=0.0 , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : str=0.1 , __lowerCAmelCase : str=0.0_2 , __lowerCAmelCase : int=1e-5 , __lowerCAmelCase : Dict="gelu" , __lowerCAmelCase : Optional[Any]=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12) , __lowerCAmelCase : Dict=(5, 2, 2, 2, 2, 2, 2) , __lowerCAmelCase : Optional[Any]=(10, 3, 3, 3, 3, 2, 2) , __lowerCAmelCase : List[Any]=False , __lowerCAmelCase : Optional[Any]=16 , __lowerCAmelCase : Union[str, Any]=19 , __lowerCAmelCase : Any=5 , __lowerCAmelCase : Tuple=0.0_5 , __lowerCAmelCase : Optional[Any]=10 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Union[str, Any]=0.0 , __lowerCAmelCase : str=10 , __lowerCAmelCase : List[str]=0 , __lowerCAmelCase : Optional[Any]="sum" , __lowerCAmelCase : Tuple=False , __lowerCAmelCase : Optional[Any]=False , __lowerCAmelCase : Any=2_56 , __lowerCAmelCase : Tuple=(5_12, 5_12, 5_12, 5_12, 15_00) , __lowerCAmelCase : Optional[Any]=(5, 3, 3, 1, 1) , __lowerCAmelCase : Optional[Any]=(1, 2, 3, 1, 1) , __lowerCAmelCase : Union[str, Any]=5_12 , __lowerCAmelCase : int=0 , __lowerCAmelCase : Optional[int]=1 , __lowerCAmelCase : Dict=2 , __lowerCAmelCase : Any=False , __lowerCAmelCase : int=3 , __lowerCAmelCase : List[Any]=2 , __lowerCAmelCase : Optional[int]=3 , __lowerCAmelCase : int=None , **__lowerCAmelCase : List[Any] , ) -> str: """simple docstring""" super().__init__(**__lowerCAmelCase , pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase ) A__ = hidden_size A__ = feat_extract_activation A__ = list(__lowerCAmelCase ) A__ = list(__lowerCAmelCase ) A__ = list(__lowerCAmelCase ) A__ = conv_bias A__ = num_conv_pos_embeddings A__ = num_conv_pos_embedding_groups A__ = conv_pos_kernel_size A__ = len(self.conv_dim ) A__ = num_hidden_layers A__ = intermediate_size A__ = hidden_act A__ = num_attention_heads A__ = hidden_dropout A__ = attention_dropout A__ = activation_dropout A__ = feat_proj_dropout A__ = final_dropout A__ = layerdrop A__ = layer_norm_eps A__ = initializer_range A__ = vocab_size A__ = use_weighted_layer_sum if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==""" """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =""" f' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,' f' `len(config.conv_kernel) = {len(self.conv_kernel )}`.' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 A__ = mask_time_prob A__ = mask_time_length A__ = mask_time_min_masks A__ = mask_feature_prob A__ = mask_feature_length A__ = mask_feature_min_masks # ctc loss A__ = ctc_loss_reduction A__ = ctc_zero_infinity # adapter A__ = add_adapter A__ = adapter_kernel_size A__ = adapter_stride A__ = num_adapter_layers A__ = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. A__ = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. A__ = list(__lowerCAmelCase ) A__ = list(__lowerCAmelCase ) A__ = list(__lowerCAmelCase ) A__ = xvector_output_dim @property def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" return math.prod(self.conv_stride )
274
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 A (unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : List[str]=13 , __lowerCAmelCase : int=7 , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : int=True , __lowerCAmelCase : Dict=True , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : List[Any]=99 , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Optional[Any]=5 , __lowerCAmelCase : Tuple=4 , __lowerCAmelCase : Any=37 , __lowerCAmelCase : str="gelu" , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : Union[str, Any]=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : int=16 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : List[Any]=0.0_2 , __lowerCAmelCase : Tuple=4 , ) -> Dict: """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_attention_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_choices def a_ ( self : Any ) -> str: """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_attention_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ = 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=__lowerCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a_ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class A (SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a_ ( self : str ) -> Optional[int]: """simple docstring""" A__ = FlaxAlbertModelTester(self ) @slow def a_ ( self : int ) -> Tuple: """simple docstring""" for model_class_name in self.all_model_classes: A__ = model_class_name.from_pretrained("""albert-base-v2""" ) A__ = model(np.ones((1, 1) ) ) self.assertIsNotNone(__lowerCAmelCase ) @require_flax class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : Dict ) -> List[Any]: """simple docstring""" A__ = FlaxAlbertModel.from_pretrained("""albert-base-v2""" ) A__ = np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] ) A__ = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase )[0] A__ = (1, 11, 7_68) self.assertEqual(output.shape , __lowerCAmelCase ) A__ = 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] , __lowerCAmelCase , atol=1e-4 ) )
274
1
import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class A : '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : Union[str, Any]=13 , __lowerCAmelCase : Any=30 , __lowerCAmelCase : Optional[Any]=2 , __lowerCAmelCase : str=3 , __lowerCAmelCase : str=True , __lowerCAmelCase : Dict=True , __lowerCAmelCase : Dict=32 , __lowerCAmelCase : Tuple=5 , __lowerCAmelCase : str=4 , __lowerCAmelCase : List[Any]=37 , __lowerCAmelCase : Optional[Any]="gelu" , __lowerCAmelCase : int=0.1 , __lowerCAmelCase : Union[str, Any]=0.1 , __lowerCAmelCase : int=10 , __lowerCAmelCase : str=0.0_2 , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : Tuple=2 , ) -> Union[str, Any]: """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = patch_size A__ = num_channels A__ = is_training A__ = use_labels A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = type_sequence_label_size A__ = initializer_range A__ = scope A__ = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) A__ = (image_size // patch_size) ** 2 A__ = num_patches + 1 def a_ ( self : Any ) -> Optional[int]: """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ = self.get_config() return config, pixel_values, labels def a_ ( self : int ) -> Union[str, Any]: """simple docstring""" return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__lowerCAmelCase , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def a_ ( self : Optional[int] , __lowerCAmelCase : int , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[str] ) -> Optional[Any]: """simple docstring""" A__ = ViTModel(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a_ ( self : Optional[Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : List[str] ) -> Union[str, Any]: """simple docstring""" A__ = ViTForMaskedImageModeling(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images A__ = 1 A__ = ViTForMaskedImageModeling(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A__ = model(__lowerCAmelCase ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def a_ ( self : Optional[int] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Union[str, Any] ) -> List[Any]: """simple docstring""" A__ = self.type_sequence_label_size A__ = ViTForImageClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase , labels=__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images A__ = 1 A__ = ViTForImageClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A__ = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def a_ ( self : str ) -> Union[str, Any]: """simple docstring""" A__ = self.prepare_config_and_inputs() ( ( A__ ) , ( A__ ) , ( A__ ) , ) = config_and_inputs A__ = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class A (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Any = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) __lowerCamelCase : List[str] = ( {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification} if is_torch_available() else {} ) __lowerCamelCase : Optional[Any] = True __lowerCamelCase : Optional[int] = False __lowerCamelCase : Any = False __lowerCamelCase : List[Any] = False def a_ ( self : str ) -> Dict: """simple docstring""" A__ = ViTModelTester(self ) A__ = ConfigTester(self , config_class=__lowerCAmelCase , has_text_modality=__lowerCAmelCase , hidden_size=37 ) def a_ ( self : List[Any] ) -> Dict: """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason="""ViT does not use inputs_embeds""" ) def a_ ( self : Any ) -> str: """simple docstring""" pass def a_ ( self : str ) -> Optional[Any]: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(__lowerCAmelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__lowerCAmelCase , nn.Linear ) ) def a_ ( self : Tuple ) -> int: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(__lowerCAmelCase ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , __lowerCAmelCase ) def a_ ( self : str ) -> int: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCAmelCase ) def a_ ( self : int ) -> Optional[int]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*__lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Optional[int]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase ) @slow def a_ ( self : Dict ) -> Optional[Any]: """simple docstring""" for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = ViTModel.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) def __lowerCamelCase ( ) -> List[Any]: """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class A (unittest.TestCase ): '''simple docstring''' @cached_property def a_ ( self : Tuple ) -> str: """simple docstring""" return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224""" ) if is_vision_available() else None @slow def a_ ( self : Tuple ) -> Dict: """simple docstring""" A__ = ViTForImageClassification.from_pretrained("""google/vit-base-patch16-224""" ).to(__lowerCAmelCase ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(images=__lowerCAmelCase , return_tensors="""pt""" ).to(__lowerCAmelCase ) # forward pass with torch.no_grad(): A__ = model(**__lowerCAmelCase ) # verify the logits A__ = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , __lowerCAmelCase ) A__ = torch.tensor([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6] ).to(__lowerCAmelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , __lowerCAmelCase , atol=1e-4 ) ) @slow def a_ ( self : List[str] ) -> List[Any]: """simple docstring""" A__ = ViTModel.from_pretrained("""facebook/dino-vits8""" ).to(__lowerCAmelCase ) A__ = ViTImageProcessor.from_pretrained("""facebook/dino-vits8""" , size=4_80 ) A__ = prepare_img() A__ = image_processor(images=__lowerCAmelCase , return_tensors="""pt""" ) A__ = inputs.pixel_values.to(__lowerCAmelCase ) # forward pass with torch.no_grad(): A__ = model(__lowerCAmelCase , interpolate_pos_encoding=__lowerCAmelCase ) # verify the logits A__ = torch.Size((1, 36_01, 3_84) ) self.assertEqual(outputs.last_hidden_state.shape , __lowerCAmelCase ) A__ = torch.tensor( [[4.2_3_4_0, 4.3_9_0_6, -6.6_6_9_2], [4.5_4_6_3, 1.8_9_2_8, -6.7_2_5_7], [4.4_4_2_9, 0.8_4_9_6, -5.8_5_8_5]] ).to(__lowerCAmelCase ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , __lowerCAmelCase , atol=1e-4 ) ) @slow @require_accelerate @require_torch_gpu def a_ ( self : int ) -> Any: """simple docstring""" A__ = ViTModel.from_pretrained("""facebook/dino-vits8""" , torch_dtype=torch.floataa , device_map="""auto""" ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(images=__lowerCAmelCase , return_tensors="""pt""" ) A__ = inputs.pixel_values.to(__lowerCAmelCase ) # forward pass to make sure inference works in fp16 with torch.no_grad(): A__ = model(__lowerCAmelCase )
274
from sklearn.metrics import fa_score import datasets A : Any = ''' The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation: F1 = 2 * (precision * recall) / (precision + recall) ''' A : List[Any] = ''' Args: predictions (`list` of `int`): Predicted labels. references (`list` of `int`): Ground truth labels. labels (`list` of `int`): The set of labels to include when `average` is not set to `\'binary\'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None. pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1. average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`. - \'binary\': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary. - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives. - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - \'weighted\': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. This option can result in an F-score that is not between precision and recall. - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). sample_weight (`list` of `float`): Sample weights Defaults to None. Returns: f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better. Examples: Example 1-A simple binary example >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0]) >>> print(results) {\'f1\': 0.5} Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0) >>> print(round(results[\'f1\'], 2)) 0.67 Example 3-The same simple binary example as in Example 1, but with `sample_weight` included. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3]) >>> print(round(results[\'f1\'], 2)) 0.35 Example 4-A multiclass example, with different values for the `average` input. >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro") >>> print(round(results[\'f1\'], 2)) 0.33 >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {\'f1\': array([0.8, 0. , 0. ])} ''' A : List[Any] = ''' @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A (datasets.Metric ): '''simple docstring''' def a_ ( self : Optional[int] ) -> List[Any]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html"""] , ) def a_ ( self : Any , __lowerCAmelCase : Tuple , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict=None , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : Any="binary" , __lowerCAmelCase : Optional[int]=None ) -> List[Any]: """simple docstring""" A__ = fa_score( __lowerCAmelCase , __lowerCAmelCase , labels=__lowerCAmelCase , pos_label=__lowerCAmelCase , average=__lowerCAmelCase , sample_weight=__lowerCAmelCase ) return {"f1": float(__lowerCAmelCase ) if score.size == 1 else score}
274
1
from ...configuration_utils import PretrainedConfig from ...utils import logging A : Dict = logging.get_logger(__name__) A : Tuple = { '''google/fnet-base''': '''https://huggingface.co/google/fnet-base/resolve/main/config.json''', '''google/fnet-large''': '''https://huggingface.co/google/fnet-large/resolve/main/config.json''' # See all FNet models at https://huggingface.co/models?filter=fnet } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[Any] = '''fnet''' def __init__( self : int , __lowerCAmelCase : Optional[Any]=3_20_00 , __lowerCAmelCase : str=7_68 , __lowerCAmelCase : List[str]=12 , __lowerCAmelCase : Tuple=30_72 , __lowerCAmelCase : Tuple="gelu_new" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : int=5_12 , __lowerCAmelCase : Dict=4 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : Union[str, Any]=1e-12 , __lowerCAmelCase : Dict=False , __lowerCAmelCase : Any=5_12 , __lowerCAmelCase : Union[str, Any]=3 , __lowerCAmelCase : Optional[Any]=1 , __lowerCAmelCase : str=2 , **__lowerCAmelCase : str , ) -> Optional[Any]: """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) A__ = vocab_size A__ = max_position_embeddings A__ = hidden_size A__ = num_hidden_layers A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = initializer_range A__ = type_vocab_size A__ = layer_norm_eps A__ = use_tpu_fourier_optimizations A__ = tpu_short_seq_length
274
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Any = '''xlm-roberta''' def __init__( self : Optional[Any] , __lowerCAmelCase : List[Any]=3_05_22 , __lowerCAmelCase : int=7_68 , __lowerCAmelCase : Tuple=12 , __lowerCAmelCase : str=12 , __lowerCAmelCase : Union[str, Any]=30_72 , __lowerCAmelCase : Union[str, Any]="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-12 , __lowerCAmelCase : Union[str, Any]=1 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Tuple="absolute" , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=None , **__lowerCAmelCase : str , ) -> Optional[Any]: """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": A__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A__ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
274
1
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionSAGPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class A (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : List[str] = StableDiffusionSAGPipeline __lowerCamelCase : Union[str, Any] = TEXT_TO_IMAGE_PARAMS __lowerCamelCase : List[Any] = TEXT_TO_IMAGE_BATCH_PARAMS __lowerCamelCase : List[Any] = TEXT_TO_IMAGE_IMAGE_PARAMS __lowerCamelCase : Tuple = TEXT_TO_IMAGE_IMAGE_PARAMS __lowerCamelCase : int = False def a_ ( self : Any ) -> Optional[int]: """simple docstring""" torch.manual_seed(0 ) A__ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , ) A__ = DDIMScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule="""scaled_linear""" , clip_sample=__lowerCAmelCase , set_alpha_to_one=__lowerCAmelCase , ) torch.manual_seed(0 ) A__ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=4 , ) torch.manual_seed(0 ) A__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) A__ = CLIPTextModel(__lowerCAmelCase ) A__ = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) A__ = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def a_ ( self : Optional[int] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : List[str]=0 ) -> Union[str, Any]: """simple docstring""" if str(__lowerCAmelCase ).startswith("""mps""" ): A__ = torch.manual_seed(__lowerCAmelCase ) else: A__ = torch.Generator(device=__lowerCAmelCase ).manual_seed(__lowerCAmelCase ) A__ = { """prompt""": """.""", """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 1.0, """sag_scale""": 1.0, """output_type""": """numpy""", } return inputs def a_ ( self : Optional[Any] ) -> int: """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : int ) -> Any: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def a_ ( self : Optional[Any] ) -> str: """simple docstring""" A__ = StableDiffusionSAGPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4""" ) A__ = sag_pipe.to(__lowerCAmelCase ) sag_pipe.set_progress_bar_config(disable=__lowerCAmelCase ) A__ = """.""" A__ = torch.manual_seed(0 ) A__ = sag_pipe( [prompt] , generator=__lowerCAmelCase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type="""np""" ) A__ = output.images A__ = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) A__ = np.array([0.1_5_6_8, 0.1_7_3_8, 0.1_6_9_5, 0.1_6_9_3, 0.1_5_0_7, 0.1_7_0_5, 0.1_5_4_7, 0.1_7_5_1, 0.1_9_4_9] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-2 def a_ ( self : Optional[int] ) -> str: """simple docstring""" A__ = StableDiffusionSAGPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base""" ) A__ = sag_pipe.to(__lowerCAmelCase ) sag_pipe.set_progress_bar_config(disable=__lowerCAmelCase ) A__ = """.""" A__ = torch.manual_seed(0 ) A__ = sag_pipe( [prompt] , generator=__lowerCAmelCase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type="""np""" ) A__ = output.images A__ = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) A__ = np.array([0.3_4_5_9, 0.2_8_7_6, 0.2_5_3_7, 0.3_0_0_2, 0.2_6_7_1, 0.2_1_6_0, 0.3_0_2_6, 0.2_2_6_2, 0.2_3_7_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-2 def a_ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" A__ = StableDiffusionSAGPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base""" ) A__ = sag_pipe.to(__lowerCAmelCase ) sag_pipe.set_progress_bar_config(disable=__lowerCAmelCase ) A__ = """.""" A__ = torch.manual_seed(0 ) A__ = sag_pipe( [prompt] , width=7_68 , height=5_12 , generator=__lowerCAmelCase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type="""np""" , ) A__ = output.images assert image.shape == (1, 5_12, 7_68, 3)
274
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean A : str = 0 A : Any = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] A : Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right A : Union[str, Any] = tuple[int, int] class A : '''simple docstring''' def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Node | None , ) -> None: """simple docstring""" A__ = pos_x A__ = pos_y A__ = (pos_y, pos_x) A__ = goal_x A__ = goal_y A__ = g_cost A__ = parent A__ = self.calculate_heuristic() A__ = self.g_cost + self.h_cost def a_ ( self : Dict ) -> float: """simple docstring""" A__ = self.pos_x - self.goal_x A__ = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(__lowerCAmelCase ) + abs(__lowerCAmelCase ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : int , __lowerCAmelCase : Node ) -> bool: """simple docstring""" return self.f_cost < other.f_cost class A : '''simple docstring''' def __init__( self : Union[str, Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> Tuple: """simple docstring""" A__ = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) A__ = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , __lowerCAmelCase ) A__ = [self.start] A__ = [] A__ = False def a_ ( self : List[str] ) -> list[TPosition]: """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() A__ = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) A__ = self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) return [self.start.pos] def a_ ( self : Optional[Any] , __lowerCAmelCase : Node ) -> list[Node]: """simple docstring""" A__ = [] for action in delta: A__ = parent.pos_x + action[1] A__ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def a_ ( self : List[Any] , __lowerCAmelCase : Node | None ) -> list[TPosition]: """simple docstring""" A__ = node A__ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A__ = current_node.parent path.reverse() return path class A : '''simple docstring''' def __init__( self : Optional[Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> None: """simple docstring""" A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = False def a_ ( self : int ) -> list[TPosition]: """simple docstring""" while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() A__ = self.fwd_astar.open_nodes.pop(0 ) A__ = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( __lowerCAmelCase , __lowerCAmelCase ) self.fwd_astar.closed_nodes.append(__lowerCAmelCase ) self.bwd_astar.closed_nodes.append(__lowerCAmelCase ) A__ = current_bwd_node A__ = current_fwd_node A__ = { self.fwd_astar: self.fwd_astar.get_successors(__lowerCAmelCase ), self.bwd_astar: self.bwd_astar.get_successors(__lowerCAmelCase ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = astar.open_nodes.pop( astar.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(__lowerCAmelCase ) else: astar.open_nodes.append(__lowerCAmelCase ) return [self.fwd_astar.start.pos] def a_ ( self : List[str] , __lowerCAmelCase : Node , __lowerCAmelCase : Node ) -> list[TPosition]: """simple docstring""" A__ = self.fwd_astar.retrace_path(__lowerCAmelCase ) A__ = self.bwd_astar.retrace_path(__lowerCAmelCase ) bwd_path.pop() bwd_path.reverse() A__ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] A : Optional[int] = (0, 0) A : int = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) A : Dict = time.time() A : Optional[Any] = AStar(init, goal) A : Optional[int] = a_star.search() A : Optional[int] = time.time() - start_time print(F'''AStar execution time = {end_time:f} seconds''') A : Dict = time.time() A : Tuple = BidirectionalAStar(init, goal) A : List[Any] = time.time() - bd_start_time print(F'''BidirectionalAStar execution time = {bd_end_time:f} seconds''')
274
1
from typing import List, Optional, Union import torch from transformers import ( XLMRobertaTokenizer, ) from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) from .text_encoder import MultilingualCLIP A : Optional[Any] = logging.get_logger(__name__) # pylint: disable=invalid-name A : List[str] = ''' Examples: ```py >>> from diffusers import KandinskyPipeline, KandinskyPriorPipeline >>> import torch >>> pipe_prior = KandinskyPriorPipeline.from_pretrained("kandinsky-community/Kandinsky-2-1-prior") >>> pipe_prior.to("cuda") >>> prompt = "red cat, 4k photo" >>> out = pipe_prior(prompt) >>> image_emb = out.image_embeds >>> negative_image_emb = out.negative_image_embeds >>> pipe = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1") >>> pipe.to("cuda") >>> image = pipe( ... prompt, ... image_embeds=image_emb, ... negative_image_embeds=negative_image_emb, ... height=768, ... width=768, ... num_inference_steps=100, ... ).images >>> image[0].save("cat.png") ``` ''' def __lowerCamelCase ( __a :str , __a :Any , __a :List[str]=8 ) -> Dict: """simple docstring""" A__ = h // scale_factor**2 if h % scale_factor**2 != 0: new_h += 1 A__ = w // scale_factor**2 if w % scale_factor**2 != 0: new_w += 1 return new_h * scale_factor, new_w * scale_factor class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Dict , __lowerCAmelCase : MultilingualCLIP , __lowerCAmelCase : XLMRobertaTokenizer , __lowerCAmelCase : UNetaDConditionModel , __lowerCAmelCase : Union[DDIMScheduler, DDPMScheduler] , __lowerCAmelCase : VQModel , ) -> Optional[int]: """simple docstring""" super().__init__() self.register_modules( text_encoder=__lowerCAmelCase , tokenizer=__lowerCAmelCase , unet=__lowerCAmelCase , scheduler=__lowerCAmelCase , movq=__lowerCAmelCase , ) A__ = 2 ** (len(self.movq.config.block_out_channels ) - 1) def a_ ( self : str , __lowerCAmelCase : Tuple , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : List[str] ) -> int: """simple docstring""" if latents is None: A__ = randn_tensor(__lowerCAmelCase , generator=__lowerCAmelCase , device=__lowerCAmelCase , dtype=__lowerCAmelCase ) else: if latents.shape != shape: raise ValueError(f'Unexpected latents shape, got {latents.shape}, expected {shape}' ) A__ = latents.to(__lowerCAmelCase ) A__ = latents * scheduler.init_noise_sigma return latents def a_ ( self : Any , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : int=None , ) -> Tuple: """simple docstring""" A__ = len(__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else 1 # get prompt text embeddings A__ = self.tokenizer( __lowerCAmelCase , padding="""max_length""" , truncation=__lowerCAmelCase , max_length=77 , return_attention_mask=__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , return_tensors="""pt""" , ) A__ = text_inputs.input_ids A__ = self.tokenizer(__lowerCAmelCase , padding="""longest""" , return_tensors="""pt""" ).input_ids if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(__lowerCAmelCase , __lowerCAmelCase ): A__ = self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1] ) 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__ = text_input_ids.to(__lowerCAmelCase ) A__ = text_inputs.attention_mask.to(__lowerCAmelCase ) A__ , A__ = self.text_encoder( input_ids=__lowerCAmelCase , attention_mask=__lowerCAmelCase ) A__ = prompt_embeds.repeat_interleave(__lowerCAmelCase , dim=0 ) A__ = text_encoder_hidden_states.repeat_interleave(__lowerCAmelCase , dim=0 ) A__ = text_mask.repeat_interleave(__lowerCAmelCase , dim=0 ) if do_classifier_free_guidance: A__ = 42 if negative_prompt is None: A__ = [""""""] * batch_size elif type(__lowerCAmelCase ) is not type(__lowerCAmelCase ): raise TypeError( f'`negative_prompt` should be the same type to `prompt`, but got {type(__lowerCAmelCase )} !=' f' {type(__lowerCAmelCase )}.' ) elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): A__ = [negative_prompt] elif batch_size != len(__lowerCAmelCase ): raise ValueError( f'`negative_prompt`: {negative_prompt} has batch size {len(__lowerCAmelCase )}, but `prompt`:' f' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches' """ the batch size of `prompt`.""" ) else: A__ = negative_prompt A__ = self.tokenizer( __lowerCAmelCase , padding="""max_length""" , max_length=77 , truncation=__lowerCAmelCase , return_attention_mask=__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , return_tensors="""pt""" , ) A__ = uncond_input.input_ids.to(__lowerCAmelCase ) A__ = uncond_input.attention_mask.to(__lowerCAmelCase ) A__ , A__ = self.text_encoder( input_ids=__lowerCAmelCase , attention_mask=__lowerCAmelCase ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method A__ = negative_prompt_embeds.shape[1] A__ = negative_prompt_embeds.repeat(1 , __lowerCAmelCase ) A__ = negative_prompt_embeds.view(batch_size * num_images_per_prompt , __lowerCAmelCase ) A__ = uncond_text_encoder_hidden_states.shape[1] A__ = uncond_text_encoder_hidden_states.repeat(1 , __lowerCAmelCase , 1 ) A__ = uncond_text_encoder_hidden_states.view( batch_size * num_images_per_prompt , __lowerCAmelCase , -1 ) A__ = uncond_text_mask.repeat_interleave(__lowerCAmelCase , dim=0 ) # done duplicates # 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__ = torch.cat([negative_prompt_embeds, prompt_embeds] ) A__ = torch.cat([uncond_text_encoder_hidden_states, text_encoder_hidden_states] ) A__ = torch.cat([uncond_text_mask, text_mask] ) return prompt_embeds, text_encoder_hidden_states, text_mask def a_ ( self : List[Any] , __lowerCAmelCase : List[Any]=0 ) -> List[Any]: """simple docstring""" if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("""Please install accelerate via `pip install accelerate`""" ) A__ = torch.device(f'cuda:{gpu_id}' ) A__ = [ self.unet, self.text_encoder, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : str , __lowerCAmelCase : Tuple=0 ) -> Optional[int]: """simple docstring""" if is_accelerate_available() and is_accelerate_version(""">=""" , """0.17.0.dev0""" ): from accelerate import cpu_offload_with_hook else: raise ImportError("""`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.""" ) A__ = torch.device(f'cuda:{gpu_id}' ) if self.device.type != "cpu": self.to("""cpu""" , silence_dtype_warnings=__lowerCAmelCase ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) A__ = None for cpu_offloaded_model in [self.text_encoder, self.unet, self.movq]: A__ , A__ = cpu_offload_with_hook(__lowerCAmelCase , __lowerCAmelCase , prev_module_hook=__lowerCAmelCase ) if self.safety_checker is not None: A__ , A__ = cpu_offload_with_hook(self.safety_checker , __lowerCAmelCase , prev_module_hook=__lowerCAmelCase ) # We'll offload the last model manually. A__ = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def a_ ( self : Optional[Any] ) -> List[str]: """simple docstring""" if not hasattr(self.unet , """_hf_hook""" ): return self.device for module in self.unet.modules(): if ( hasattr(__lowerCAmelCase , """_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(__lowerCAmelCase ) def __call__( self : List[Any] , __lowerCAmelCase : Union[str, List[str]] , __lowerCAmelCase : Union[torch.FloatTensor, List[torch.FloatTensor]] , __lowerCAmelCase : Union[torch.FloatTensor, List[torch.FloatTensor]] , __lowerCAmelCase : Optional[Union[str, List[str]]] = None , __lowerCAmelCase : int = 5_12 , __lowerCAmelCase : int = 5_12 , __lowerCAmelCase : int = 1_00 , __lowerCAmelCase : float = 4.0 , __lowerCAmelCase : int = 1 , __lowerCAmelCase : Optional[Union[torch.Generator, List[torch.Generator]]] = None , __lowerCAmelCase : Optional[torch.FloatTensor] = None , __lowerCAmelCase : Optional[str] = "pil" , __lowerCAmelCase : bool = True , ) -> Any: """simple docstring""" if isinstance(__lowerCAmelCase , __lowerCAmelCase ): A__ = 1 elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): A__ = len(__lowerCAmelCase ) else: raise ValueError(f'`prompt` has to be of type `str` or `list` but is {type(__lowerCAmelCase )}' ) A__ = self._execution_device A__ = batch_size * num_images_per_prompt A__ = guidance_scale > 1.0 A__ , A__ , A__ = self._encode_prompt( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ): A__ = torch.cat(__lowerCAmelCase , dim=0 ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ): A__ = torch.cat(__lowerCAmelCase , dim=0 ) if do_classifier_free_guidance: A__ = image_embeds.repeat_interleave(__lowerCAmelCase , dim=0 ) A__ = negative_image_embeds.repeat_interleave(__lowerCAmelCase , dim=0 ) A__ = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to( dtype=prompt_embeds.dtype , device=__lowerCAmelCase ) self.scheduler.set_timesteps(__lowerCAmelCase , device=__lowerCAmelCase ) A__ = self.scheduler.timesteps A__ = self.unet.config.in_channels A__ , A__ = get_new_h_w(__lowerCAmelCase , __lowerCAmelCase , self.movq_scale_factor ) # create initial latent A__ = self.prepare_latents( (batch_size, num_channels_latents, height, width) , text_encoder_hidden_states.dtype , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , self.scheduler , ) for i, t in enumerate(self.progress_bar(__lowerCAmelCase ) ): # expand the latents if we are doing classifier free guidance A__ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents A__ = {"""text_embeds""": prompt_embeds, """image_embeds""": image_embeds} A__ = self.unet( sample=__lowerCAmelCase , timestep=__lowerCAmelCase , encoder_hidden_states=__lowerCAmelCase , added_cond_kwargs=__lowerCAmelCase , return_dict=__lowerCAmelCase , )[0] if do_classifier_free_guidance: A__ , A__ = noise_pred.split(latents.shape[1] , dim=1 ) A__ , A__ = noise_pred.chunk(2 ) A__ , A__ = variance_pred.chunk(2 ) A__ = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) A__ = 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__ = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 A__ = self.scheduler.step( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , generator=__lowerCAmelCase , ).prev_sample # post-processing A__ = self.movq.decode(__lowerCAmelCase , force_not_quantize=__lowerCAmelCase )["""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__ = image * 0.5 + 0.5 A__ = image.clamp(0 , 1 ) A__ = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": A__ = self.numpy_to_pil(__lowerCAmelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=__lowerCAmelCase )
274
import unittest from transformers.utils.backbone_utils import ( BackboneMixin, get_aligned_output_features_output_indices, verify_out_features_out_indices, ) class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Any ) -> Union[str, Any]: """simple docstring""" A__ = ["""a""", """b""", """c"""] # Defaults to last layer if both are None A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""c"""] ) self.assertEqual(__lowerCAmelCase , [2] ) # Out indices set to match out features A__ , A__ = get_aligned_output_features_output_indices(["""a""", """c"""] , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features set to match out indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [0, 2] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features selected from negative indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [-3, -1] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [-3, -1] ) def a_ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , __lowerCAmelCase ) # Out features must be a list with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(("""a""", """b""") , (0, 1) , ["""a""", """b"""] ) # Out features must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , ["""a"""] ) # Out indices must be a list or tuple with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , 0 , ["""a""", """b"""] ) # Out indices must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , (0, 1) , ["""a"""] ) # Out features and out indices must be the same length with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0,) , ["""a""", """b""", """c"""] ) # Out features should match out indices with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 2) , ["""a""", """b""", """c"""] ) # Out features and out indices should be in order with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""b""", """a"""] , (0, 1) , ["""a""", """b"""] ) # Check passes with valid inputs verify_out_features_out_indices(["""a""", """b""", """d"""] , (0, 1, -1) , ["""a""", """b""", """c""", """d"""] ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" A__ = BackboneMixin() A__ = ["""a""", """b""", """c"""] A__ = ["""a""", """c"""] A__ = [0, 2] # Check that the output features and indices are set correctly self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [0, 2] ) # Check out features and indices are updated correctly A__ = ["""a""", """b"""] self.assertEqual(backbone.out_features , ["""a""", """b"""] ) self.assertEqual(backbone.out_indices , [0, 1] ) A__ = [-3, -1] self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [-3, -1] )
274
1
from typing import Dict, Optional import numpy as np import datasets A : str = ''' IoU is the area of overlap between the predicted segmentation and the ground truth divided by the area of union between the predicted segmentation and the ground truth. For binary (two classes) or multi-class segmentation, the mean IoU of the image is calculated by taking the IoU of each class and averaging them. ''' A : Any = ''' Args: predictions (`List[ndarray]`): List of predicted segmentation maps, each of shape (height, width). Each segmentation map can be of a different size. references (`List[ndarray]`): List of ground truth segmentation maps, each of shape (height, width). Each segmentation map can be of a different size. num_labels (`int`): Number of classes (categories). ignore_index (`int`): Index that will be ignored during evaluation. nan_to_num (`int`, *optional*): If specified, NaN values will be replaced by the number defined by the user. label_map (`dict`, *optional*): If specified, dictionary mapping old label indices to new label indices. reduce_labels (`bool`, *optional*, defaults to `False`): Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is used for background, and background itself is not included in all classes of a dataset (e.g. ADE20k). The background label will be replaced by 255. Returns: `Dict[str, float | ndarray]` comprising various elements: - *mean_iou* (`float`): Mean Intersection-over-Union (IoU averaged over all categories). - *mean_accuracy* (`float`): Mean accuracy (averaged over all categories). - *overall_accuracy* (`float`): Overall accuracy on all images. - *per_category_accuracy* (`ndarray` of shape `(num_labels,)`): Per category accuracy. - *per_category_iou* (`ndarray` of shape `(num_labels,)`): Per category IoU. Examples: >>> import numpy as np >>> mean_iou = datasets.load_metric("mean_iou") >>> # suppose one has 3 different segmentation maps predicted >>> predicted_1 = np.array([[1, 2], [3, 4], [5, 255]]) >>> actual_1 = np.array([[0, 3], [5, 4], [6, 255]]) >>> predicted_2 = np.array([[2, 7], [9, 2], [3, 6]]) >>> actual_2 = np.array([[1, 7], [9, 2], [3, 6]]) >>> predicted_3 = np.array([[2, 2, 3], [8, 2, 4], [3, 255, 2]]) >>> actual_3 = np.array([[1, 2, 2], [8, 2, 1], [3, 255, 1]]) >>> predicted = [predicted_1, predicted_2, predicted_3] >>> ground_truth = [actual_1, actual_2, actual_3] >>> results = mean_iou.compute(predictions=predicted, references=ground_truth, num_labels=10, ignore_index=255, reduce_labels=False) >>> print(results) # doctest: +NORMALIZE_WHITESPACE {\'mean_iou\': 0.47750000000000004, \'mean_accuracy\': 0.5916666666666666, \'overall_accuracy\': 0.5263157894736842, \'per_category_iou\': array([0. , 0. , 0.375, 0.4 , 0.5 , 0. , 0.5 , 1. , 1. , 1. ]), \'per_category_accuracy\': array([0. , 0. , 0.75 , 0.66666667, 1. , 0. , 0.5 , 1. , 1. , 1. ])} ''' A : Any = '''\ @software{MMSegmentation_Contributors_OpenMMLab_Semantic_Segmentation_2020, author = {{MMSegmentation Contributors}}, license = {Apache-2.0}, month = {7}, title = {{OpenMMLab Semantic Segmentation Toolbox and Benchmark}}, url = {https://github.com/open-mmlab/mmsegmentation}, year = {2020} }''' def __lowerCamelCase ( __a :Dict , __a :List[Any] , __a :List[Any] , __a :bool , __a :Optional[Dict[int, int]] = None , __a :bool = False , ) -> Dict: """simple docstring""" if label_map is not None: for old_id, new_id in label_map.items(): A__ = new_id # turn into Numpy arrays A__ = np.array(__a ) A__ = np.array(__a ) if reduce_labels: A__ = 2_5_5 A__ = label - 1 A__ = 2_5_5 A__ = label != ignore_index A__ = np.not_equal(__a , __a ) A__ = pred_label[mask] A__ = np.array(__a )[mask] A__ = pred_label[pred_label == label] A__ = np.histogram(__a , bins=__a , range=(0, num_labels - 1) )[0] A__ = np.histogram(__a , bins=__a , range=(0, num_labels - 1) )[0] A__ = np.histogram(__a , bins=__a , range=(0, num_labels - 1) )[0] A__ = area_pred_label + area_label - area_intersect return area_intersect, area_union, area_pred_label, area_label def __lowerCamelCase ( __a :Tuple , __a :int , __a :str , __a :bool , __a :Optional[Dict[int, int]] = None , __a :bool = False , ) -> List[str]: """simple docstring""" A__ = np.zeros((num_labels,) , dtype=np.floataa ) A__ = np.zeros((num_labels,) , dtype=np.floataa ) A__ = np.zeros((num_labels,) , dtype=np.floataa ) A__ = np.zeros((num_labels,) , dtype=np.floataa ) for result, gt_seg_map in zip(__a , __a ): A__ , A__ , A__ , A__ = intersect_and_union( __a , __a , __a , __a , __a , __a ) total_area_intersect += area_intersect total_area_union += area_union total_area_pred_label += area_pred_label total_area_label += area_label return total_area_intersect, total_area_union, total_area_pred_label, total_area_label def __lowerCamelCase ( __a :Tuple , __a :Union[str, Any] , __a :Tuple , __a :bool , __a :Optional[int] = None , __a :Optional[Dict[int, int]] = None , __a :bool = False , ) -> Union[str, Any]: """simple docstring""" A__ , A__ , A__ , A__ = total_intersect_and_union( __a , __a , __a , __a , __a , __a ) # compute metrics A__ = {} A__ = total_area_intersect.sum() / total_area_label.sum() A__ = total_area_intersect / total_area_union A__ = total_area_intersect / total_area_label A__ = np.nanmean(__a ) A__ = np.nanmean(__a ) A__ = all_acc A__ = iou A__ = acc if nan_to_num is not None: A__ = {metric: np.nan_to_num(__a , nan=__a ) for metric, metric_value in metrics.items()} return metrics @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A (datasets.Metric ): '''simple docstring''' def a_ ( self : Union[str, Any] ) -> Tuple: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( # 1st Seq - height dim, 2nd - width dim { """predictions""": datasets.Sequence(datasets.Sequence(datasets.Value("""uint16""" ) ) ), """references""": datasets.Sequence(datasets.Sequence(datasets.Value("""uint16""" ) ) ), } ) , reference_urls=[ """https://github.com/open-mmlab/mmsegmentation/blob/71c201b1813267d78764f306a297ca717827c4bf/mmseg/core/evaluation/metrics.py""" ] , ) def a_ ( self : Dict , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : bool , __lowerCAmelCase : Optional[int] = None , __lowerCAmelCase : Optional[Dict[int, int]] = None , __lowerCAmelCase : bool = False , ) -> List[str]: """simple docstring""" A__ = mean_iou( results=__lowerCAmelCase , gt_seg_maps=__lowerCAmelCase , num_labels=__lowerCAmelCase , ignore_index=__lowerCAmelCase , nan_to_num=__lowerCAmelCase , label_map=__lowerCAmelCase , reduce_labels=__lowerCAmelCase , ) return iou_result
274
from collections import deque class A : '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" A__ = process_name # process name A__ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time A__ = arrival_time A__ = burst_time # remaining burst time A__ = 0 # total time of the process wait in ready queue A__ = 0 # time from arrival time to completion time class A : '''simple docstring''' def __init__( self : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : list[int] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int , ) -> None: """simple docstring""" A__ = number_of_queues # time slice of queues that round robin algorithm applied A__ = time_slices # unfinished process is in this ready_queue A__ = queue # current time A__ = current_time # finished process is in this sequence queue A__ = deque() def a_ ( self : Dict ) -> list[str]: """simple docstring""" A__ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def a_ ( self : Tuple , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def a_ ( self : Optional[Any] , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def a_ ( self : Dict , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): completion_times.append(queue[i].stop_time ) return completion_times def a_ ( self : int , __lowerCAmelCase : deque[Process] ) -> list[int]: """simple docstring""" return [q.burst_time for q in queue] def a_ ( self : Any , __lowerCAmelCase : Process ) -> int: """simple docstring""" process.waiting_time += self.current_time - process.stop_time return process.waiting_time def a_ ( self : Union[str, Any] , __lowerCAmelCase : deque[Process] ) -> deque[Process]: """simple docstring""" A__ = deque() # sequence deque of finished process while len(__lowerCAmelCase ) != 0: A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__lowerCAmelCase ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 A__ = 0 # set the process's turnaround time because it is finished A__ = self.current_time - cp.arrival_time # set the completion time A__ = self.current_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def a_ ( self : Optional[Any] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int ) -> tuple[deque[Process], deque[Process]]: """simple docstring""" A__ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__lowerCAmelCase ) ): A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__lowerCAmelCase ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time A__ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__lowerCAmelCase ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished A__ = 0 # set the finish time A__ = self.current_time # update the process' turnaround time because it is finished A__ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def a_ ( self : List[Any] ) -> deque[Process]: """simple docstring""" for i in range(self.number_of_queues - 1 ): A__ , A__ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest A : Union[str, Any] = Process('''P1''', 0, 5_3) A : Optional[Any] = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : int = Process('''P4''', 0, 2_4) A : Any = 3 A : List[Any] = [1_7, 2_5] A : Optional[Any] = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) A : Optional[Any] = Process('''P1''', 0, 5_3) A : int = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : Tuple = Process('''P4''', 0, 2_4) A : Union[str, Any] = 3 A : Optional[Any] = [1_7, 2_5] A : Tuple = deque([Pa, Pa, Pa, Pa]) A : Optional[int] = MLFQ(number_of_queues, time_slices, queue, 0) A : Dict = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( F'''waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print completion times of processes(P1, P2, P3, P4) print( F'''completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print total turnaround times of processes(P1, P2, P3, P4) print( F'''turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print sequence of finished processes print( F'''sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}''' )
274
1
def __lowerCamelCase ( __a :int = 6_0_0_8_5_1_4_7_5_1_4_3 ) -> int: """simple docstring""" try: A__ = int(__a ) 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__ = 2 A__ = 0 if n == 2: return 2 while n > 2: while n % i != 0: i += 1 A__ = i while n % i == 0: A__ = n // i i += 1 return int(__a ) if __name__ == "__main__": print(F'''{solution() = }''')
274
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType A : str = logging.get_logger(__name__) A : Union[str, Any] = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Optional[Any] = '''layoutlmv3''' def __init__( self : Tuple , __lowerCAmelCase : Optional[int]=5_02_65 , __lowerCAmelCase : Tuple=7_68 , __lowerCAmelCase : Union[str, Any]=12 , __lowerCAmelCase : Any=12 , __lowerCAmelCase : List[Any]=30_72 , __lowerCAmelCase : Optional[int]="gelu" , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[Any]=0.1 , __lowerCAmelCase : Dict=5_12 , __lowerCAmelCase : Any=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-5 , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : int=0 , __lowerCAmelCase : Dict=2 , __lowerCAmelCase : Tuple=10_24 , __lowerCAmelCase : List[str]=1_28 , __lowerCAmelCase : Optional[int]=1_28 , __lowerCAmelCase : Any=True , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Any=1_28 , __lowerCAmelCase : str=64 , __lowerCAmelCase : Optional[int]=2_56 , __lowerCAmelCase : int=True , __lowerCAmelCase : int=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : int=2_24 , __lowerCAmelCase : Dict=3 , __lowerCAmelCase : List[Any]=16 , __lowerCAmelCase : Dict=None , **__lowerCAmelCase : Optional[Any] , ) -> Dict: """simple docstring""" super().__init__( vocab_size=__lowerCAmelCase , hidden_size=__lowerCAmelCase , num_hidden_layers=__lowerCAmelCase , num_attention_heads=__lowerCAmelCase , intermediate_size=__lowerCAmelCase , hidden_act=__lowerCAmelCase , hidden_dropout_prob=__lowerCAmelCase , attention_probs_dropout_prob=__lowerCAmelCase , max_position_embeddings=__lowerCAmelCase , type_vocab_size=__lowerCAmelCase , initializer_range=__lowerCAmelCase , layer_norm_eps=__lowerCAmelCase , pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = max_ad_position_embeddings A__ = coordinate_size A__ = shape_size A__ = has_relative_attention_bias A__ = rel_pos_bins A__ = max_rel_pos A__ = has_spatial_attention_bias A__ = rel_ad_pos_bins A__ = max_rel_ad_pos A__ = text_embed A__ = visual_embed A__ = input_size A__ = num_channels A__ = patch_size A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[str] = version.parse('''1.12''' ) @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def a_ ( self : Optional[int] ) -> float: """simple docstring""" return 1e-5 @property def a_ ( self : Tuple ) -> int: """simple docstring""" return 12 def a_ ( self : str , __lowerCAmelCase : "ProcessorMixin" , __lowerCAmelCase : int = -1 , __lowerCAmelCase : int = -1 , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional["TensorType"] = None , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 40 , __lowerCAmelCase : int = 40 , ) -> Mapping[str, Any]: """simple docstring""" setattr(processor.image_processor , """apply_ocr""" , __lowerCAmelCase ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX A__ = processor.tokenizer.num_special_tokens_to_add(__lowerCAmelCase ) A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__lowerCAmelCase ) # Generate dummy inputs according to compute batch and sequence A__ = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes A__ = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) A__ = self._generate_dummy_images(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = dict( processor( __lowerCAmelCase , text=__lowerCAmelCase , boxes=__lowerCAmelCase , return_tensors=__lowerCAmelCase , ) ) return inputs
274
1
from collections import deque from math import floor from random import random from time import time class A : '''simple docstring''' def __init__( self : Tuple ) -> Dict: """simple docstring""" A__ = {} def a_ ( self : str , __lowerCAmelCase : Tuple , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Any=1 ) -> Union[str, Any]: """simple docstring""" if self.graph.get(__lowerCAmelCase ): if self.graph[u].count([w, v] ) == 0: self.graph[u].append([w, v] ) else: A__ = [[w, v]] if not self.graph.get(__lowerCAmelCase ): A__ = [] def a_ ( self : Optional[Any] ) -> Any: """simple docstring""" return list(self.graph ) def a_ ( self : Dict , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Optional[Any] ) -> Tuple: """simple docstring""" if self.graph.get(__lowerCAmelCase ): for _ in self.graph[u]: if _[1] == v: self.graph[u].remove(__lowerCAmelCase ) def a_ ( self : List[Any] , __lowerCAmelCase : int=-2 , __lowerCAmelCase : Tuple=-1 ) -> Optional[int]: """simple docstring""" if s == d: return [] A__ = [] A__ = [] if s == -2: A__ = list(self.graph )[0] stack.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) A__ = s while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: A__ = s for node in self.graph[s]: if visited.count(node[1] ) < 1: if node[1] == d: visited.append(__lowerCAmelCase ) return visited else: stack.append(node[1] ) visited.append(node[1] ) A__ = node[1] break # check if all the children are visited if s == ss: stack.pop() if len(__lowerCAmelCase ) != 0: A__ = stack[len(__lowerCAmelCase ) - 1] else: A__ = ss # check if se have reached the starting point if len(__lowerCAmelCase ) == 0: return visited def a_ ( self : List[str] , __lowerCAmelCase : int=-1 ) -> Tuple: """simple docstring""" if c == -1: A__ = floor(random() * 1_00_00 ) + 10 for i in range(__lowerCAmelCase ): # every vertex has max 100 edges for _ in range(floor(random() * 1_02 ) + 1 ): A__ = floor(random() * c ) + 1 if n != i: self.add_pair(__lowerCAmelCase , __lowerCAmelCase , 1 ) def a_ ( self : List[Any] , __lowerCAmelCase : int=-2 ) -> Union[str, Any]: """simple docstring""" A__ = deque() A__ = [] if s == -2: A__ = list(self.graph )[0] d.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) while d: A__ = d.popleft() if len(self.graph[s] ) != 0: for node in self.graph[s]: if visited.count(node[1] ) < 1: d.append(node[1] ) visited.append(node[1] ) return visited def a_ ( self : Union[str, Any] , __lowerCAmelCase : Any ) -> str: """simple docstring""" A__ = 0 for x in self.graph: for y in self.graph[x]: if y[1] == u: count += 1 return count def a_ ( self : List[str] , __lowerCAmelCase : Tuple ) -> Optional[int]: """simple docstring""" return len(self.graph[u] ) def a_ ( self : List[Any] , __lowerCAmelCase : str=-2 ) -> List[Any]: """simple docstring""" A__ = [] A__ = [] if s == -2: A__ = list(self.graph )[0] stack.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) A__ = s A__ = [] while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: A__ = s for node in self.graph[s]: if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) A__ = node[1] break # check if all the children are visited if s == ss: sorted_nodes.append(stack.pop() ) if len(__lowerCAmelCase ) != 0: A__ = stack[len(__lowerCAmelCase ) - 1] else: A__ = ss # check if se have reached the starting point if len(__lowerCAmelCase ) == 0: return sorted_nodes def a_ ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" A__ = [] A__ = [] A__ = list(self.graph )[0] stack.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) A__ = -2 A__ = [] A__ = s A__ = False A__ = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: A__ = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): A__ = len(__lowerCAmelCase ) - 1 while len_stack >= 0: if stack[len_stack] == node[1]: anticipating_nodes.add(node[1] ) break else: anticipating_nodes.add(stack[len_stack] ) len_stack -= 1 if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) A__ = node[1] break # check if all the children are visited if s == ss: stack.pop() A__ = True if len(__lowerCAmelCase ) != 0: A__ = stack[len(__lowerCAmelCase ) - 1] else: A__ = False indirect_parents.append(__lowerCAmelCase ) A__ = s A__ = ss # check if se have reached the starting point if len(__lowerCAmelCase ) == 0: return list(__lowerCAmelCase ) def a_ ( self : Optional[int] ) -> Tuple: """simple docstring""" A__ = [] A__ = [] A__ = list(self.graph )[0] stack.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) A__ = -2 A__ = [] A__ = s A__ = False A__ = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: A__ = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): A__ = len(__lowerCAmelCase ) - 1 while len_stack_minus_one >= 0: if stack[len_stack_minus_one] == node[1]: anticipating_nodes.add(node[1] ) break else: return True if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) A__ = node[1] break # check if all the children are visited if s == ss: stack.pop() A__ = True if len(__lowerCAmelCase ) != 0: A__ = stack[len(__lowerCAmelCase ) - 1] else: A__ = False indirect_parents.append(__lowerCAmelCase ) A__ = s A__ = ss # check if se have reached the starting point if len(__lowerCAmelCase ) == 0: return False def a_ ( self : int , __lowerCAmelCase : int=-2 , __lowerCAmelCase : Optional[Any]=-1 ) -> Union[str, Any]: """simple docstring""" A__ = time() self.dfs(__lowerCAmelCase , __lowerCAmelCase ) A__ = time() return end - begin def a_ ( self : List[Any] , __lowerCAmelCase : Tuple=-2 ) -> Dict: """simple docstring""" A__ = time() self.bfs(__lowerCAmelCase ) A__ = time() return end - begin class A : '''simple docstring''' def __init__( self : Optional[Any] ) -> str: """simple docstring""" A__ = {} def a_ ( self : int , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Dict=1 ) -> Union[str, Any]: """simple docstring""" if self.graph.get(__lowerCAmelCase ): # if there already is a edge if self.graph[u].count([w, v] ) == 0: self.graph[u].append([w, v] ) else: # if u does not exist A__ = [[w, v]] # add the other way if self.graph.get(__lowerCAmelCase ): # if there already is a edge if self.graph[v].count([w, u] ) == 0: self.graph[v].append([w, u] ) else: # if u does not exist A__ = [[w, u]] def a_ ( self : Optional[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : List[str] ) -> Union[str, Any]: """simple docstring""" if self.graph.get(__lowerCAmelCase ): for _ in self.graph[u]: if _[1] == v: self.graph[u].remove(__lowerCAmelCase ) # the other way round if self.graph.get(__lowerCAmelCase ): for _ in self.graph[v]: if _[1] == u: self.graph[v].remove(__lowerCAmelCase ) def a_ ( self : Dict , __lowerCAmelCase : List[str]=-2 , __lowerCAmelCase : Optional[Any]=-1 ) -> List[Any]: """simple docstring""" if s == d: return [] A__ = [] A__ = [] if s == -2: A__ = list(self.graph )[0] stack.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) A__ = s while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: A__ = s for node in self.graph[s]: if visited.count(node[1] ) < 1: if node[1] == d: visited.append(__lowerCAmelCase ) return visited else: stack.append(node[1] ) visited.append(node[1] ) A__ = node[1] break # check if all the children are visited if s == ss: stack.pop() if len(__lowerCAmelCase ) != 0: A__ = stack[len(__lowerCAmelCase ) - 1] else: A__ = ss # check if se have reached the starting point if len(__lowerCAmelCase ) == 0: return visited def a_ ( self : Optional[Any] , __lowerCAmelCase : Any=-1 ) -> Tuple: """simple docstring""" if c == -1: A__ = floor(random() * 1_00_00 ) + 10 for i in range(__lowerCAmelCase ): # every vertex has max 100 edges for _ in range(floor(random() * 1_02 ) + 1 ): A__ = floor(random() * c ) + 1 if n != i: self.add_pair(__lowerCAmelCase , __lowerCAmelCase , 1 ) def a_ ( self : Any , __lowerCAmelCase : Optional[Any]=-2 ) -> List[Any]: """simple docstring""" A__ = deque() A__ = [] if s == -2: A__ = list(self.graph )[0] d.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) while d: A__ = d.popleft() if len(self.graph[s] ) != 0: for node in self.graph[s]: if visited.count(node[1] ) < 1: d.append(node[1] ) visited.append(node[1] ) return visited def a_ ( self : str , __lowerCAmelCase : List[str] ) -> Dict: """simple docstring""" return len(self.graph[u] ) def a_ ( self : int ) -> Any: """simple docstring""" A__ = [] A__ = [] A__ = list(self.graph )[0] stack.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) A__ = -2 A__ = [] A__ = s A__ = False A__ = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: A__ = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): A__ = len(__lowerCAmelCase ) - 1 while len_stack >= 0: if stack[len_stack] == node[1]: anticipating_nodes.add(node[1] ) break else: anticipating_nodes.add(stack[len_stack] ) len_stack -= 1 if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) A__ = node[1] break # check if all the children are visited if s == ss: stack.pop() A__ = True if len(__lowerCAmelCase ) != 0: A__ = stack[len(__lowerCAmelCase ) - 1] else: A__ = False indirect_parents.append(__lowerCAmelCase ) A__ = s A__ = ss # check if se have reached the starting point if len(__lowerCAmelCase ) == 0: return list(__lowerCAmelCase ) def a_ ( self : Dict ) -> int: """simple docstring""" A__ = [] A__ = [] A__ = list(self.graph )[0] stack.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) A__ = -2 A__ = [] A__ = s A__ = False A__ = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: A__ = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): A__ = len(__lowerCAmelCase ) - 1 while len_stack_minus_one >= 0: if stack[len_stack_minus_one] == node[1]: anticipating_nodes.add(node[1] ) break else: return True if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) A__ = node[1] break # check if all the children are visited if s == ss: stack.pop() A__ = True if len(__lowerCAmelCase ) != 0: A__ = stack[len(__lowerCAmelCase ) - 1] else: A__ = False indirect_parents.append(__lowerCAmelCase ) A__ = s A__ = ss # check if se have reached the starting point if len(__lowerCAmelCase ) == 0: return False def a_ ( self : List[Any] ) -> List[str]: """simple docstring""" return list(self.graph ) def a_ ( self : List[str] , __lowerCAmelCase : List[str]=-2 , __lowerCAmelCase : int=-1 ) -> Any: """simple docstring""" A__ = time() self.dfs(__lowerCAmelCase , __lowerCAmelCase ) A__ = time() return end - begin def a_ ( self : Optional[int] , __lowerCAmelCase : Tuple=-2 ) -> str: """simple docstring""" A__ = time() self.bfs(__lowerCAmelCase ) A__ = time() return end - begin
274
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) A : str = '''▁''' A : Any = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', '''tokenizer_config_file''': '''tokenizer_config.json''', } A : List[Any] = { '''vocab_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json''', }, '''spm_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_config_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json''', }, } A : Tuple = { '''facebook/m2m100_418M''': 1_0_2_4, } # fmt: off A : Optional[int] = { '''m2m100''': ['''af''', '''am''', '''ar''', '''ast''', '''az''', '''ba''', '''be''', '''bg''', '''bn''', '''br''', '''bs''', '''ca''', '''ceb''', '''cs''', '''cy''', '''da''', '''de''', '''el''', '''en''', '''es''', '''et''', '''fa''', '''ff''', '''fi''', '''fr''', '''fy''', '''ga''', '''gd''', '''gl''', '''gu''', '''ha''', '''he''', '''hi''', '''hr''', '''ht''', '''hu''', '''hy''', '''id''', '''ig''', '''ilo''', '''is''', '''it''', '''ja''', '''jv''', '''ka''', '''kk''', '''km''', '''kn''', '''ko''', '''lb''', '''lg''', '''ln''', '''lo''', '''lt''', '''lv''', '''mg''', '''mk''', '''ml''', '''mn''', '''mr''', '''ms''', '''my''', '''ne''', '''nl''', '''no''', '''ns''', '''oc''', '''or''', '''pa''', '''pl''', '''ps''', '''pt''', '''ro''', '''ru''', '''sd''', '''si''', '''sk''', '''sl''', '''so''', '''sq''', '''sr''', '''ss''', '''su''', '''sv''', '''sw''', '''ta''', '''th''', '''tl''', '''tn''', '''tr''', '''uk''', '''ur''', '''uz''', '''vi''', '''wo''', '''xh''', '''yi''', '''yo''', '''zh''', '''zu'''], '''wmt21''': ['''en''', '''ha''', '''is''', '''ja''', '''cs''', '''ru''', '''zh''', '''de'''] } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = VOCAB_FILES_NAMES __lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = ['''input_ids''', '''attention_mask'''] __lowerCamelCase : List[int] = [] __lowerCamelCase : List[int] = [] def __init__( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any]=None , __lowerCAmelCase : str=None , __lowerCAmelCase : List[Any]="<s>" , __lowerCAmelCase : List[Any]="</s>" , __lowerCAmelCase : Optional[int]="</s>" , __lowerCAmelCase : Optional[Any]="<pad>" , __lowerCAmelCase : Any="<unk>" , __lowerCAmelCase : Any="m2m100" , __lowerCAmelCase : Optional[Dict[str, Any]] = None , __lowerCAmelCase : Dict=8 , **__lowerCAmelCase : Tuple , ) -> None: """simple docstring""" A__ = {} if sp_model_kwargs is None else sp_model_kwargs A__ = language_codes A__ = FAIRSEQ_LANGUAGE_CODES[language_codes] A__ = {lang_code: f'__{lang_code}__' for lang_code in fairseq_language_code} A__ = kwargs.get("""additional_special_tokens""" , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(__lowerCAmelCase ) for lang_code in fairseq_language_code if self.get_lang_token(__lowerCAmelCase ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=__lowerCAmelCase , tgt_lang=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , language_codes=__lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = vocab_file A__ = load_json(__lowerCAmelCase ) A__ = {v: k for k, v in self.encoder.items()} A__ = spm_file A__ = load_spm(__lowerCAmelCase , self.sp_model_kwargs ) A__ = len(self.encoder ) A__ = { self.get_lang_token(__lowerCAmelCase ): self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase ) } A__ = {lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase )} A__ = {v: k for k, v in self.lang_token_to_id.items()} A__ = src_lang if src_lang is not None else """en""" A__ = tgt_lang A__ = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) A__ = num_madeup_words @property def a_ ( self : Optional[int] ) -> int: """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def a_ ( self : Optional[Any] ) -> str: """simple docstring""" return self._src_lang @src_lang.setter def a_ ( self : List[Any] , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def a_ ( self : Optional[int] , __lowerCAmelCase : str ) -> List[str]: """simple docstring""" return self.sp_model.encode(__lowerCAmelCase , out_type=__lowerCAmelCase ) def a_ ( self : Optional[Any] , __lowerCAmelCase : Dict ) -> Optional[Any]: """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(__lowerCAmelCase , self.encoder[self.unk_token] ) def a_ ( self : Optional[int] , __lowerCAmelCase : int ) -> str: """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(__lowerCAmelCase , self.unk_token ) def a_ ( self : Optional[int] , __lowerCAmelCase : Dict ) -> str: """simple docstring""" A__ = [] A__ = """""" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(__lowerCAmelCase ) + token A__ = [] else: current_sub_tokens.append(__lowerCAmelCase ) out_string += self.sp_model.decode(__lowerCAmelCase ) return out_string.strip() def a_ ( self : List[str] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None , __lowerCAmelCase : bool = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCAmelCase , token_ids_a=__lowerCAmelCase , already_has_special_tokens=__lowerCAmelCase ) A__ = [1] * len(self.prefix_tokens ) A__ = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(__lowerCAmelCase )) + suffix_ones return prefix_ones + ([0] * len(__lowerCAmelCase )) + ([0] * len(__lowerCAmelCase )) + suffix_ones def a_ ( self : Tuple , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def a_ ( self : int ) -> Dict: """simple docstring""" A__ = {self.convert_ids_to_tokens(__lowerCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.__dict__.copy() A__ = None return state def __setstate__( self : str , __lowerCAmelCase : Dict ) -> None: """simple docstring""" A__ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): A__ = {} A__ = load_spm(self.spm_file , self.sp_model_kwargs ) def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" A__ = Path(__lowerCAmelCase ) if not save_dir.is_dir(): raise OSError(f'{save_directory} should be a directory' ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , __lowerCAmelCase ) if os.path.abspath(self.spm_file ) != os.path.abspath(__lowerCAmelCase ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , __lowerCAmelCase ) elif not os.path.isfile(self.spm_file ): with open(__lowerCAmelCase , """wb""" ) as fi: A__ = self.sp_model.serialized_model_proto() fi.write(__lowerCAmelCase ) return (str(__lowerCAmelCase ), str(__lowerCAmelCase )) def a_ ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : str = "en" , __lowerCAmelCase : Optional[List[str]] = None , __lowerCAmelCase : str = "ro" , **__lowerCAmelCase : List[Any] , ) -> BatchEncoding: """simple docstring""" A__ = src_lang A__ = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(__lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[str] , __lowerCAmelCase : Optional[str] , **__lowerCAmelCase : Tuple ) -> Tuple: """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""" ) A__ = src_lang A__ = self(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , **__lowerCAmelCase ) A__ = self.get_lang_id(__lowerCAmelCase ) A__ = tgt_lang_id return inputs def a_ ( self : Dict ) -> int: """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def a_ ( self : str , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Tuple , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> str: """simple docstring""" return self.lang_code_to_token[lang] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> int: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) return self.lang_token_to_id[lang_token] def __lowerCamelCase ( __a :str , __a :Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: """simple docstring""" A__ = sentencepiece.SentencePieceProcessor(**__a ) spm.Load(str(__a ) ) return spm def __lowerCamelCase ( __a :str ) -> Union[Dict, List]: """simple docstring""" with open(__a , """r""" ) as f: return json.load(__a ) def __lowerCamelCase ( __a :List[Any] , __a :str ) -> None: """simple docstring""" with open(__a , """w""" ) as f: json.dump(__a , __a , indent=2 )
274
1
import argparse 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 # # 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 # ######################################################################## A : Optional[int] = 1_6 A : Optional[Any] = 3_2 def __lowerCamelCase ( __a :Accelerator , __a :int = 1_6 ) -> Optional[int]: """simple docstring""" A__ = AutoTokenizer.from_pretrained("""bert-base-cased""" ) A__ = load_dataset("""glue""" , """mrpc""" ) def tokenize_function(__a :Union[str, Any] ): # max_length=None => use the model max length (it's actually the default) A__ = tokenizer(examples["""sentence1"""] , examples["""sentence2"""] , truncation=__a , max_length=__a ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): A__ = datasets.map( __a , batched=__a , remove_columns=["""idx""", """sentence1""", """sentence2"""] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library A__ = tokenized_datasets.rename_column("""label""" , """labels""" ) def collate_fn(__a :Optional[int] ): # On TPU it's best to pad everything to the same length or training will be very slow. A__ = 1_2_8 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__ = 1_6 elif accelerator.mixed_precision != "no": A__ = 8 else: A__ = None return tokenizer.pad( __a , padding="""longest""" , max_length=__a , pad_to_multiple_of=__a , return_tensors="""pt""" , ) # Instantiate dataloaders. A__ = DataLoader( tokenized_datasets["""train"""] , shuffle=__a , collate_fn=__a , batch_size=__a , drop_last=__a ) A__ = DataLoader( tokenized_datasets["""validation"""] , shuffle=__a , collate_fn=__a , batch_size=__a , drop_last=(accelerator.mixed_precision == """fp8""") , ) return train_dataloader, eval_dataloader def __lowerCamelCase ( __a :List[str] , __a :Optional[Any] ) -> Dict: """simple docstring""" A__ = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs A__ = config["""lr"""] A__ = int(config["""num_epochs"""] ) A__ = int(config["""seed"""] ) A__ = int(config["""batch_size"""] ) A__ = evaluate.load("""glue""" , """mrpc""" ) # If the batch size is too big we use gradient accumulation A__ = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: A__ = batch_size // MAX_GPU_BATCH_SIZE A__ = MAX_GPU_BATCH_SIZE set_seed(__a ) A__ , A__ = get_dataloaders(__a , __a ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) A__ = AutoModelForSequenceClassification.from_pretrained("""bert-base-cased""" , return_dict=__a ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). A__ = model.to(accelerator.device ) # Instantiate optimizer A__ = AdamW(params=model.parameters() , lr=__a ) # Instantiate scheduler A__ = get_linear_schedule_with_warmup( optimizer=__a , num_warmup_steps=1_0_0 , num_training_steps=(len(__a ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. A__ , A__ , A__ , A__ , A__ = accelerator.prepare( __a , __a , __a , __a , __a ) # Now we train the model for epoch in range(__a ): model.train() for step, batch in enumerate(__a ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) A__ = model(**__a ) A__ = outputs.loss A__ = loss / gradient_accumulation_steps accelerator.backward(__a ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(__a ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): A__ = model(**__a ) A__ = outputs.logits.argmax(dim=-1 ) A__ , A__ = accelerator.gather_for_metrics((predictions, batch["""labels"""]) ) metric.add_batch( predictions=__a , references=__a , ) A__ = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'epoch {epoch}:' , __a ) def __lowerCamelCase ( ) -> List[str]: """simple docstring""" A__ = argparse.ArgumentParser(description="""Simple example of training script.""" ) parser.add_argument( """--mixed_precision""" , type=__a , default=__a , choices=["""no""", """fp16""", """bf16""", """fp8"""] , help="""Whether to use mixed precision. Choose""" """between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.""" """and an Nvidia Ampere GPU.""" , ) parser.add_argument("""--cpu""" , action="""store_true""" , help="""If passed, will train on the CPU.""" ) A__ = parser.parse_args() A__ = {"""lr""": 2E-5, """num_epochs""": 3, """seed""": 4_2, """batch_size""": 1_6} training_function(__a , __a ) if __name__ == "__main__": main()
274
from __future__ import annotations from PIL import Image # Define glider example A : Any = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [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], ] # Define blinker example A : Optional[Any] = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def __lowerCamelCase ( __a :list[list[int]] ) -> list[list[int]]: """simple docstring""" A__ = [] for i in range(len(__a ) ): A__ = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours A__ = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(__a ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(__a ) - 1: neighbour_count += cells[i + 1][j] if i < len(__a ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. A__ = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(__a ) return next_generation def __lowerCamelCase ( __a :list[list[int]] , __a :int ) -> list[Image.Image]: """simple docstring""" A__ = [] for _ in range(__a ): # Create output image A__ = Image.new("""RGB""" , (len(cells[0] ), len(__a )) ) A__ = img.load() # Save cells to image for x in range(len(__a ) ): for y in range(len(cells[0] ) ): A__ = 2_5_5 - cells[y][x] * 2_5_5 A__ = (colour, colour, colour) # Save image images.append(__a ) A__ = new_generation(__a ) return images if __name__ == "__main__": A : str = generate_images(GLIDER, 1_6) images[0].save('''out.gif''', save_all=True, append_images=images[1:])
274
1
def __lowerCamelCase ( __a :str ) -> str: """simple docstring""" A__ = """""" for ch in key: if ch == " " or ch not in key_no_dups and ch.isalpha(): key_no_dups += ch return key_no_dups def __lowerCamelCase ( __a :str ) -> dict[str, str]: """simple docstring""" A__ = [chr(i + 6_5 ) for i in range(2_6 )] # Remove duplicate characters from key A__ = remove_duplicates(key.upper() ) A__ = len(__a ) # First fill cipher with key characters A__ = {alphabet[i]: char for i, char in enumerate(__a )} # Then map remaining characters in alphabet to # the alphabet from the beginning for i in range(len(__a ) , 2_6 ): A__ = alphabet[i - offset] # Ensure we are not mapping letters to letters previously mapped while char in key: offset -= 1 A__ = alphabet[i - offset] A__ = char return cipher_alphabet def __lowerCamelCase ( __a :str , __a :dict[str, str] ) -> str: """simple docstring""" return "".join(cipher_map.get(__a , __a ) for ch in message.upper() ) def __lowerCamelCase ( __a :str , __a :dict[str, str] ) -> str: """simple docstring""" A__ = {v: k for k, v in cipher_map.items()} return "".join(rev_cipher_map.get(__a , __a ) for ch in message.upper() ) def __lowerCamelCase ( ) -> None: """simple docstring""" A__ = input("""Enter message to encode or decode: """ ).strip() A__ = input("""Enter keyword: """ ).strip() A__ = input("""Encipher or decipher? E/D:""" ).strip()[0].lower() try: A__ = {"""e""": encipher, """d""": decipher}[option] except KeyError: raise KeyError("""invalid input option""" ) A__ = create_cipher_map(__a ) print(func(__a , __a ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
274
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": A : List[str] = input('''Enter image url: ''').strip() print(F'''Downloading image from {url} ...''') A : Any = BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image A : List[Any] = soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] A : Dict = requests.get(image_url).content A : Tuple = F'''{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg''' with open(file_name, '''wb''') as fp: fp.write(image_data) print(F'''Done. Image saved to disk as {file_name}.''')
274
1
import argparse from collections import defaultdict import yaml A : str = '''docs/source/en/_toctree.yml''' def __lowerCamelCase ( __a :str ) -> List[Any]: """simple docstring""" A__ = defaultdict(__a ) A__ = [] A__ = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__a ) A__ = new_doc_list A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__a ) > 1: raise ValueError( F'{duplicate_key} is present several times in the documentation table of content at ' """`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the """ """others.""" ) # Only add this once new_doc.append({"""local""": duplicate_key, """title""": titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) A__ = sorted(__a , key=lambda __a : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__a ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__a ) # Sort return overview_doc def __lowerCamelCase ( __a :Any=False ) -> List[str]: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 A__ = api_doc[scheduler_idx]["""sections"""] A__ = clean_doc_toc(__a ) A__ = False if new_scheduler_doc != scheduler_doc: A__ = True if overwrite: A__ = new_scheduler_doc if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def __lowerCamelCase ( __a :Optional[int]=False ) -> Dict: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 A__ = False A__ = api_doc[pipeline_idx]["""sections"""] A__ = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: A__ = pipeline_doc["""section"""] A__ = clean_doc_toc(__a ) if overwrite: A__ = new_sub_pipeline_doc new_pipeline_docs.append(__a ) # sort overall pipeline doc A__ = clean_doc_toc(__a ) if new_pipeline_docs != pipeline_docs: A__ = True if overwrite: A__ = new_pipeline_docs if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) if __name__ == "__main__": A : Tuple = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') A : Optional[Any] = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
274
# This code is adapted from OpenAI's release # https://github.com/openai/human-eval/blob/master/human_eval/execution.py import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def __lowerCamelCase ( __a :List[str] , __a :List[Any] , __a :Union[str, Any] , __a :List[Any] ) -> Dict: """simple docstring""" A__ = multiprocessing.Manager() A__ = manager.list() A__ = multiprocessing.Process(target=__a , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append("""timed out""" ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def __lowerCamelCase ( __a :Optional[Any] , __a :Any , __a :List[Any] ) -> Union[str, Any]: """simple docstring""" with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil A__ = shutil.rmtree A__ = os.rmdir A__ = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: A__ = {} with swallow_io(): with time_limit(__a ): exec(__a , __a ) result.append("""passed""" ) except TimeoutException: result.append("""timed out""" ) except BaseException as e: result.append(F'failed: {e}' ) # Needed for cleaning up. A__ = rmtree A__ = rmdir A__ = chdir @contextlib.contextmanager def __lowerCamelCase ( __a :List[str] ) -> Dict: """simple docstring""" def signal_handler(__a :List[Any] , __a :Optional[Any] ): raise TimeoutException("""Timed out!""" ) signal.setitimer(signal.ITIMER_REAL , __a ) signal.signal(signal.SIGALRM , __a ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = WriteOnlyStringIO() with contextlib.redirect_stdout(__a ): with contextlib.redirect_stderr(__a ): with redirect_stdin(__a ): yield @contextlib.contextmanager def __lowerCamelCase ( ) -> Dict: """simple docstring""" with tempfile.TemporaryDirectory() as dirname: with chdir(__a ): yield dirname class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (io.StringIO ): '''simple docstring''' def a_ ( self : Any , *__lowerCAmelCase : List[str] , **__lowerCAmelCase : str ) -> Dict: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Optional[int] ) -> str: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Any ) -> int: """simple docstring""" raise OSError def a_ ( self : str , *__lowerCAmelCase : Any , **__lowerCAmelCase : Union[str, Any] ) -> int: """simple docstring""" return False class A (contextlib._RedirectStream ): # type: ignore '''simple docstring''' __lowerCamelCase : Union[str, Any] = '''stdin''' @contextlib.contextmanager def __lowerCamelCase ( __a :Union[str, Any] ) -> List[str]: """simple docstring""" if root == ".": yield return A__ = os.getcwd() os.chdir(__a ) try: yield except BaseException as exc: raise exc finally: os.chdir(__a ) def __lowerCamelCase ( __a :Union[str, Any]=None ) -> Dict: """simple docstring""" if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins A__ = None A__ = None import os A__ = """1""" A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None import shutil A__ = None A__ = None A__ = None import subprocess A__ = None # type: ignore A__ = None import sys A__ = None A__ = None A__ = None A__ = None A__ = None
274
1
from __future__ import annotations import numpy as np def __lowerCamelCase ( __a :np.ndarray ) -> tuple[np.ndarray, np.ndarray]: """simple docstring""" A__ , A__ = np.shape(__a ) if rows != columns: A__ = ( """'table' has to be of square shaped array but got a """ F'{rows}x{columns} array:\n{table}' ) raise ValueError(__a ) A__ = np.zeros((rows, columns) ) A__ = np.zeros((rows, columns) ) for i in range(__a ): for j in range(__a ): A__ = sum(lower[i][k] * upper[k][j] for k in range(__a ) ) if upper[j][j] == 0: raise ArithmeticError("""No LU decomposition exists""" ) A__ = (table[i][j] - total) / upper[j][j] A__ = 1 for j in range(__a , __a ): A__ = sum(lower[i][k] * upper[k][j] for k in range(__a ) ) A__ = table[i][j] - total return lower, upper if __name__ == "__main__": import doctest doctest.testmod()
274
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_albert import AlbertTokenizer else: A : Tuple = None A : Optional[Any] = logging.get_logger(__name__) A : Tuple = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A : List[str] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json''', }, } A : List[str] = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } A : Optional[int] = '''▁''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : str = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = AlbertTokenizer def __init__( self : Tuple , __lowerCAmelCase : Tuple=None , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : Optional[Any]=True , __lowerCAmelCase : List[Any]=True , __lowerCAmelCase : str=False , __lowerCAmelCase : Union[str, Any]="[CLS]" , __lowerCAmelCase : int="[SEP]" , __lowerCAmelCase : Dict="<unk>" , __lowerCAmelCase : Dict="[SEP]" , __lowerCAmelCase : Union[str, Any]="<pad>" , __lowerCAmelCase : str="[CLS]" , __lowerCAmelCase : int="[MASK]" , **__lowerCAmelCase : Optional[Any] , ) -> Optional[Any]: """simple docstring""" A__ = ( AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase , normalized=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token ) super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def a_ ( self : List[Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a_ ( self : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(__lowerCAmelCase ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return A__ = os.path.join( __lowerCAmelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCAmelCase ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
274
1
import math def __lowerCamelCase ( __a :int ) -> list[int]: """simple docstring""" A__ = [] A__ = 2 A__ = int(math.sqrt(__a ) ) # Size of every segment A__ = [True] * (end + 1) A__ = [] while start <= end: if temp[start] is True: in_prime.append(__a ) for i in range(start * start , end + 1 , __a ): A__ = False start += 1 prime += in_prime A__ = end + 1 A__ = min(2 * end , __a ) while low <= n: A__ = [True] * (high - low + 1) for each in in_prime: A__ = math.floor(low / each ) * each if t < low: t += each for j in range(__a , high + 1 , __a ): A__ = False for j in range(len(__a ) ): if temp[j] is True: prime.append(j + low ) A__ = high + 1 A__ = min(high + end , __a ) return prime print(sieve(1_0**6))
274
import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging A : Dict = logging.get_logger(__name__) def __lowerCamelCase ( __a :int=None , __a :Optional[Any]=None ) -> int: """simple docstring""" return field(default_factory=lambda: default , metadata=__a ) @dataclass class A : '''simple docstring''' __lowerCamelCase : List[str] = list_field( default=[] , metadata={ '''help''': ( '''Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version''' ''' of all available models''' ) } , ) __lowerCamelCase : List[int] = list_field( default=[8] , metadata={'''help''': '''List of batch sizes for which memory and time performance will be evaluated'''} ) __lowerCamelCase : List[int] = list_field( default=[8, 32, 128, 512] , metadata={'''help''': '''List of sequence lengths for which memory and time performance will be evaluated'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to benchmark inference of model. Inference can be disabled via --no-inference.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available cuda devices. Cuda can be disabled via --no-cuda.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available tpu devices. TPU can be disabled via --no-tpu.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Use FP16 to accelerate inference.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Benchmark training of model'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Verbose memory tracing'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to perform speed measurements. Speed measurements can be disabled via --no-speed.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': '''Whether to perform memory measurements. Memory measurements can be disabled via --no-memory''' } , ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Trace memory line by line'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save result to a CSV file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save all print statements in a log file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to print environment information'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use''' ''' multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled''' ''' for debugging / testing and on TPU.''' ) } , ) __lowerCamelCase : str = field( default=F'''inference_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''train_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''train_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''env_info_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving environment information.'''} , ) __lowerCamelCase : str = field( default=F'''log_{round(time() )}.csv''' , metadata={'''help''': '''Log filename used if print statements are saved in log.'''} , ) __lowerCamelCase : int = field(default=3 , metadata={'''help''': '''Times an experiment will be run.'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain''' ''' model weights.''' ) } , ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" warnings.warn( f'The class {self.__class__} is deprecated. Hugging Face Benchmarking utils' """ are deprecated in general and it is advised to use external Benchmarking libraries """ """ to benchmark Transformer models.""" , __lowerCAmelCase , ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def a_ ( self : Tuple ) -> List[str]: """simple docstring""" if len(self.models ) <= 0: raise ValueError( """Please make sure you provide at least one model name / model identifier, *e.g.* `--models""" """ bert-base-cased` or `args.models = ['bert-base-cased'].""" ) return self.models @property def a_ ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" if not self.multi_process: return False elif self.is_tpu: logger.info("""Multiprocessing is currently not possible on TPU.""" ) return False else: return True
274
1
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig A : Any = { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''', } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Optional[int] = '''albert''' def __init__( self : str , __lowerCAmelCase : Tuple=3_00_00 , __lowerCAmelCase : List[Any]=1_28 , __lowerCAmelCase : Union[str, Any]=40_96 , __lowerCAmelCase : Dict=12 , __lowerCAmelCase : Tuple=1 , __lowerCAmelCase : List[str]=64 , __lowerCAmelCase : List[Any]=1_63_84 , __lowerCAmelCase : Any=1 , __lowerCAmelCase : Any="gelu_new" , __lowerCAmelCase : Optional[Any]=0 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Dict=5_12 , __lowerCAmelCase : int=2 , __lowerCAmelCase : List[str]=0.0_2 , __lowerCAmelCase : int=1e-12 , __lowerCAmelCase : List[str]=0.1 , __lowerCAmelCase : Union[str, Any]="absolute" , __lowerCAmelCase : int=0 , __lowerCAmelCase : Optional[Any]=2 , __lowerCAmelCase : int=3 , **__lowerCAmelCase : Tuple , ) -> Tuple: """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) A__ = vocab_size A__ = embedding_size A__ = hidden_size A__ = num_hidden_layers A__ = num_hidden_groups A__ = num_attention_heads A__ = inner_group_num A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = classifier_dropout_prob A__ = position_embedding_type class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def a_ ( self : Any ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": A__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A__ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis), ] )
274
from math import ceil def __lowerCamelCase ( __a :int = 1_0_0_1 ) -> int: """simple docstring""" A__ = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): A__ = 2 * i + 1 A__ = 2 * i A__ = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: A : List[str] = int(sys.argv[1]) print(solution(n)) except ValueError: print('''Invalid entry - please enter a number''')
274
1
def __lowerCamelCase ( __a :str ) -> list: """simple docstring""" if n_term == "": return [] A__ = [] for temp in range(int(__a ) ): series.append(F'1/{temp + 1}' if series else """1""" ) return series if __name__ == "__main__": A : List[Any] = input('''Enter the last number (nth term) of the Harmonic Series''') print('''Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n''') print(harmonic_series(nth_term))
274
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A : Tuple = logging.getLogger(__name__) def __lowerCamelCase ( __a :Optional[int] , __a :List[str] ) -> Tuple: """simple docstring""" A__ = np.argmax(__a , axis=1 ) return np.sum(outputs == labels ) def __lowerCamelCase ( __a :Tuple ) -> Dict: """simple docstring""" with open(__a , encoding="""utf_8""" ) as f: A__ = csv.reader(__a ) A__ = [] next(__a ) # skip the first line for line in tqdm(__a ): output.append((""" """.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def __lowerCamelCase ( __a :Optional[int] , __a :List[Any] , __a :Dict , __a :Optional[Any] , __a :Optional[Any] , __a :int ) -> Union[str, Any]: """simple docstring""" A__ = [] for dataset in encoded_datasets: A__ = len(__a ) A__ = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) A__ = np.zeros((n_batch, 2) , dtype=np.intaa ) A__ = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) A__ = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(__a ): A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = with_conta A__ = with_conta A__ = len(__a ) - 1 A__ = len(__a ) - 1 A__ = with_conta A__ = with_conta A__ = mc_label A__ = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(__a ) for t in all_inputs ) ) return tensor_datasets def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = argparse.ArgumentParser() parser.add_argument("""--model_name""" , type=__a , default="""openai-gpt""" , help="""pretrained model name""" ) parser.add_argument("""--do_train""" , action="""store_true""" , help="""Whether to run training.""" ) parser.add_argument("""--do_eval""" , action="""store_true""" , help="""Whether to run eval on the dev set.""" ) parser.add_argument( """--output_dir""" , default=__a , type=__a , required=__a , help="""The output directory where the model predictions and checkpoints will be written.""" , ) parser.add_argument("""--train_dataset""" , type=__a , default="""""" ) parser.add_argument("""--eval_dataset""" , type=__a , default="""""" ) parser.add_argument("""--seed""" , type=__a , default=4_2 ) parser.add_argument("""--num_train_epochs""" , type=__a , default=3 ) parser.add_argument("""--train_batch_size""" , type=__a , default=8 ) parser.add_argument("""--eval_batch_size""" , type=__a , default=1_6 ) parser.add_argument("""--adam_epsilon""" , default=1E-8 , type=__a , help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--max_grad_norm""" , type=__a , default=1 ) parser.add_argument( """--max_steps""" , default=-1 , type=__a , help=( """If > 0: set total number of training steps to perform. Override num_train_epochs.""" ) , ) parser.add_argument( """--gradient_accumulation_steps""" , type=__a , default=1 , help="""Number of updates steps to accumulate before performing a backward/update pass.""" , ) parser.add_argument("""--learning_rate""" , type=__a , default=6.25E-5 ) parser.add_argument("""--warmup_steps""" , default=0 , type=__a , help="""Linear warmup over warmup_steps.""" ) parser.add_argument("""--lr_schedule""" , type=__a , default="""warmup_linear""" ) parser.add_argument("""--weight_decay""" , type=__a , default=0.01 ) parser.add_argument("""--lm_coef""" , type=__a , default=0.9 ) parser.add_argument("""--n_valid""" , type=__a , default=3_7_4 ) parser.add_argument("""--server_ip""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) parser.add_argument("""--server_port""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) A__ = parser.parse_args() print(__a ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("""Waiting for debugger attach""" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__a ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) A__ = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) A__ = torch.cuda.device_count() logger.info("""device: {}, n_gpu {}""".format(__a , __a ) ) if not args.do_train and not args.do_eval: raise ValueError("""At least one of `do_train` or `do_eval` must be True.""" ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset A__ = ["""_start_""", """_delimiter_""", """_classify_"""] A__ = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(__a ) A__ = tokenizer.convert_tokens_to_ids(__a ) A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(__a ) ) model.to(__a ) # Load and encode the datasets def tokenize_and_encode(__a :Tuple ): if isinstance(__a , __a ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(__a ) ) elif isinstance(__a , __a ): return obj return [tokenize_and_encode(__a ) for o in obj] logger.info("""Encoding dataset...""" ) A__ = load_rocstories_dataset(args.train_dataset ) A__ = load_rocstories_dataset(args.eval_dataset ) A__ = (train_dataset, eval_dataset) A__ = tokenize_and_encode(__a ) # Compute the max input length for the Transformer A__ = model.config.n_positions // 2 - 2 A__ = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) A__ = min(__a , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders A__ = pre_process_datasets(__a , __a , __a , *__a ) A__ , A__ = tensor_datasets[0], tensor_datasets[1] A__ = TensorDataset(*__a ) A__ = RandomSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.train_batch_size ) A__ = TensorDataset(*__a ) A__ = SequentialSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: A__ = args.max_steps A__ = args.max_steps // (len(__a ) // args.gradient_accumulation_steps) + 1 else: A__ = len(__a ) // args.gradient_accumulation_steps * args.num_train_epochs A__ = list(model.named_parameters() ) A__ = ["""bias""", """LayerNorm.bias""", """LayerNorm.weight"""] A__ = [ { """params""": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], """weight_decay""": args.weight_decay, }, {"""params""": [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], """weight_decay""": 0.0}, ] A__ = AdamW(__a , lr=args.learning_rate , eps=args.adam_epsilon ) A__ = get_linear_schedule_with_warmup( __a , num_warmup_steps=args.warmup_steps , num_training_steps=__a ) if args.do_train: A__ , A__ , A__ = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="""Epoch""" ): A__ = 0 A__ = 0 A__ = tqdm(__a , desc="""Training""" ) for step, batch in enumerate(__a ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch A__ = model(__a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() A__ = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 A__ = """Training loss: {:.2e} lr: {:.2e}""".format(__a , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer A__ = model.module if hasattr(__a , """module""" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` A__ = os.path.join(args.output_dir , __a ) A__ = os.path.join(args.output_dir , __a ) torch.save(model_to_save.state_dict() , __a ) model_to_save.config.to_json_file(__a ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) A__ = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(__a ) if args.do_eval: model.eval() A__ , A__ = 0, 0 A__ , A__ = 0, 0 for batch in tqdm(__a , desc="""Evaluating""" ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch with torch.no_grad(): A__ , A__ , A__ , A__ = model( __a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = mc_logits.detach().cpu().numpy() A__ = mc_labels.to("""cpu""" ).numpy() A__ = accuracy(__a , __a ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 A__ = eval_loss / nb_eval_steps A__ = eval_accuracy / nb_eval_examples A__ = tr_loss / nb_tr_steps if args.do_train else None A__ = {"""eval_loss""": eval_loss, """eval_accuracy""": eval_accuracy, """train_loss""": train_loss} A__ = os.path.join(args.output_dir , """eval_results.txt""" ) with open(__a , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key in sorted(result.keys() ): logger.info(""" %s = %s""" , __a , str(result[key] ) ) writer.write("""%s = %s\n""" % (key, str(result[key] )) ) if __name__ == "__main__": main()
274
1
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_mobilebert import MobileBertTokenizer A : List[str] = logging.get_logger(__name__) A : Any = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} A : Optional[Any] = { '''vocab_file''': {'''mobilebert-uncased''': '''https://huggingface.co/google/mobilebert-uncased/resolve/main/vocab.txt'''}, '''tokenizer_file''': { '''mobilebert-uncased''': '''https://huggingface.co/google/mobilebert-uncased/resolve/main/tokenizer.json''' }, } A : List[Any] = {'''mobilebert-uncased''': 5_1_2} A : List[str] = {} class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[str] = VOCAB_FILES_NAMES __lowerCamelCase : Any = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : int = PRETRAINED_INIT_CONFIGURATION __lowerCamelCase : int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : Union[str, Any] = MobileBertTokenizer def __init__( self : Dict , __lowerCAmelCase : Optional[int]=None , __lowerCAmelCase : Tuple=None , __lowerCAmelCase : Any=True , __lowerCAmelCase : Tuple="[UNK]" , __lowerCAmelCase : Optional[int]="[SEP]" , __lowerCAmelCase : int="[PAD]" , __lowerCAmelCase : Optional[int]="[CLS]" , __lowerCAmelCase : List[str]="[MASK]" , __lowerCAmelCase : Optional[Any]=True , __lowerCAmelCase : List[str]=None , **__lowerCAmelCase : List[str] , ) -> Dict: """simple docstring""" super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , tokenize_chinese_chars=__lowerCAmelCase , strip_accents=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("""lowercase""" , __lowerCAmelCase ) != do_lower_case or normalizer_state.get("""strip_accents""" , __lowerCAmelCase ) != strip_accents or normalizer_state.get("""handle_chinese_chars""" , __lowerCAmelCase ) != tokenize_chinese_chars ): A__ = getattr(__lowerCAmelCase , normalizer_state.pop("""type""" ) ) A__ = do_lower_case A__ = strip_accents A__ = tokenize_chinese_chars A__ = normalizer_class(**__lowerCAmelCase ) A__ = do_lower_case def a_ ( self : int , __lowerCAmelCase : Tuple , __lowerCAmelCase : List[str]=None ) -> Optional[int]: """simple docstring""" A__ = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def a_ ( self : Tuple , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" A__ = self._tokenizer.model.save(__lowerCAmelCase , name=__lowerCAmelCase ) return tuple(__lowerCAmelCase )
274
import argparse from collections import defaultdict import yaml A : str = '''docs/source/en/_toctree.yml''' def __lowerCamelCase ( __a :str ) -> List[Any]: """simple docstring""" A__ = defaultdict(__a ) A__ = [] A__ = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__a ) A__ = new_doc_list A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__a ) > 1: raise ValueError( F'{duplicate_key} is present several times in the documentation table of content at ' """`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the """ """others.""" ) # Only add this once new_doc.append({"""local""": duplicate_key, """title""": titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) A__ = sorted(__a , key=lambda __a : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__a ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__a ) # Sort return overview_doc def __lowerCamelCase ( __a :Any=False ) -> List[str]: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 A__ = api_doc[scheduler_idx]["""sections"""] A__ = clean_doc_toc(__a ) A__ = False if new_scheduler_doc != scheduler_doc: A__ = True if overwrite: A__ = new_scheduler_doc if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def __lowerCamelCase ( __a :Optional[int]=False ) -> Dict: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 A__ = False A__ = api_doc[pipeline_idx]["""sections"""] A__ = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: A__ = pipeline_doc["""section"""] A__ = clean_doc_toc(__a ) if overwrite: A__ = new_sub_pipeline_doc new_pipeline_docs.append(__a ) # sort overall pipeline doc A__ = clean_doc_toc(__a ) if new_pipeline_docs != pipeline_docs: A__ = True if overwrite: A__ = new_pipeline_docs if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) if __name__ == "__main__": A : Tuple = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') A : Optional[Any] = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
274
1
def __lowerCamelCase ( __a :int ) -> bool: """simple docstring""" return sum(i for i in range(1 , number // 2 + 1 ) if number % i == 0 ) == number if __name__ == "__main__": print('''Program to check whether a number is a Perfect number or not...''') A : Any = int(input('''Enter number: ''').strip()) print(F'''{number} is {"" if perfect(number) else "not "}a Perfect Number.''')
274
def __lowerCamelCase ( __a :str ) -> list: """simple docstring""" A__ = [0] * len(__a ) for i in range(1 , len(__a ) ): # use last results for better performance - dynamic programming A__ = prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: A__ = prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 A__ = j return prefix_result def __lowerCamelCase ( __a :str ) -> int: """simple docstring""" return max(prefix_function(__a ) ) if __name__ == "__main__": import doctest doctest.testmod()
274
1
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def __lowerCamelCase ( __a :Dict ) -> Any: """simple docstring""" A__ = [ """decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(__a , __a ) def __lowerCamelCase ( __a :str ) -> Union[str, Any]: """simple docstring""" A__ , A__ = emb.weight.shape A__ = nn.Linear(__a , __a , bias=__a ) A__ = emb.weight.data return lin_layer def __lowerCamelCase ( __a :str ) -> List[str]: """simple docstring""" A__ = torch.load(__a , map_location="""cpu""" ) A__ = Namespace(**checkpoint["""cfg"""]["""model"""] ) A__ = checkpoint["""model"""] remove_ignore_keys_(__a ) A__ = state_dict["""decoder.embed_tokens.weight"""].shape[0] A__ = {key.replace("""decoder""" , """model""" ): val for key, val in state_dict.items()} A__ = XGLMConfig( vocab_size=__a , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""gelu""" , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) A__ = XGLMForCausalLM(__a ) A__ = model.load_state_dict(__a , strict=__a ) print(__a ) A__ = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": A : int = argparse.ArgumentParser() # Required parameters parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') A : str = parser.parse_args() A : str = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
274
def __lowerCamelCase ( __a :int = 1_0_0_0_0_0_0 ) -> int: """simple docstring""" A__ = limit + 1 A__ = [0] * limit for first_term in range(1 , __a ): for n in range(__a , __a , __a ): A__ = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a A__ = sum(1 for x in frequency[1:limit] if x == 1_0 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
274
1
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available from ...utils import OptionalDependencyNotAvailable A : str = {'''configuration_gpt_neox''': ['''GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GPTNeoXConfig''']} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[Any] = ['''GPTNeoXTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[str] = [ '''GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST''', '''GPTNeoXForCausalLM''', '''GPTNeoXForQuestionAnswering''', '''GPTNeoXForSequenceClassification''', '''GPTNeoXForTokenClassification''', '''GPTNeoXLayer''', '''GPTNeoXModel''', '''GPTNeoXPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_gpt_neox import GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_neox_fast import GPTNeoXTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox import ( GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, GPTNeoXLayer, GPTNeoXModel, GPTNeoXPreTrainedModel, ) else: import sys A : List[Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
274
class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A : '''simple docstring''' def __init__( self : List[Any] ) -> str: """simple docstring""" A__ = [ [], [], [], ] def a_ ( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" try: if len(self.queues[priority] ) >= 1_00: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(__lowerCAmelCase ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def a_ ( self : Optional[Any] ) -> int: """simple docstring""" for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self : Tuple ) -> str: """simple docstring""" return "\n".join(f'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class A : '''simple docstring''' def __init__( self : int ) -> str: """simple docstring""" A__ = [] def a_ ( self : int , __lowerCAmelCase : int ) -> None: """simple docstring""" if len(self.queue ) == 1_00: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(__lowerCAmelCase ) def a_ ( self : List[str] ) -> int: """simple docstring""" if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: A__ = min(self.queue ) self.queue.remove(__lowerCAmelCase ) return data def __str__( self : List[Any] ) -> str: """simple docstring""" return str(self.queue ) def __lowerCamelCase ( ) -> Optional[Any]: """simple docstring""" A__ = FixedPriorityQueue() fpq.enqueue(0 , 1_0 ) fpq.enqueue(1 , 7_0 ) fpq.enqueue(0 , 1_0_0 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 6_4 ) fpq.enqueue(0 , 1_2_8 ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __lowerCamelCase ( ) -> int: """simple docstring""" A__ = ElementPriorityQueue() epq.enqueue(1_0 ) epq.enqueue(7_0 ) epq.enqueue(1_0_0 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(6_4 ) epq.enqueue(1_2_8 ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
274
1
def __lowerCamelCase ( __a :int = 1_0_0_0 ) -> int: """simple docstring""" return sum(e for e in range(3 , __a ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F'''{solution() = }''')
274
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = tempfile.mkdtemp() # fmt: off A__ = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on A__ = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) A__ = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""] A__ = {"""unk_token""": """<unk>"""} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(__lowerCAmelCase ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__lowerCAmelCase ) ) A__ = { """do_resize""": True, """size""": 20, """do_center_crop""": True, """crop_size""": 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], } A__ = os.path.join(self.tmpdirname , __lowerCAmelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Tuple , **__lowerCAmelCase : Dict ) -> str: """simple docstring""" return CLIPTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : Union[str, Any] , **__lowerCAmelCase : Dict ) -> List[str]: """simple docstring""" return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : List[str] , **__lowerCAmelCase : Optional[Any] ) -> Dict: """simple docstring""" return CLIPImageProcessor.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : str ) -> Dict: """simple docstring""" shutil.rmtree(self.tmpdirname ) def a_ ( self : str ) -> Any: """simple docstring""" A__ = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] A__ = [Image.fromarray(np.moveaxis(__lowerCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def a_ ( self : Optional[int] ) -> Tuple: """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_rust_tokenizer() A__ = self.get_image_processor() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_slow.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname , use_fast=__lowerCAmelCase ) A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_fast.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , __lowerCAmelCase ) self.assertIsInstance(processor_fast.tokenizer , __lowerCAmelCase ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , __lowerCAmelCase ) self.assertIsInstance(processor_fast.image_processor , __lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" A__ = CLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=__lowerCAmelCase , padding_value=1.0 ) A__ = CLIPProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__lowerCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowerCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCAmelCase ) def a_ ( self : List[Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(__lowerCAmelCase , return_tensors="""np""" ) A__ = processor(images=__lowerCAmelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def a_ ( self : Optional[Any] ) -> Any: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = processor(text=__lowerCAmelCase ) A__ = tokenizer(__lowerCAmelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(__lowerCAmelCase ): processor() def a_ ( self : Tuple ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.batch_decode(__lowerCAmelCase ) A__ = tokenizer.batch_decode(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Optional[int] ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
274
1
from random import randint, random def __lowerCamelCase ( __a :int , __a :int , __a :int , __a :bool = False , __a :bool = False , __a :int = 5 , ) -> list: """simple docstring""" A__ = [[-1] * number_of_cells] # Create a highway without any car A__ = 0 A__ = max(__a , 0 ) while i < number_of_cells: A__ = ( randint(0 , __a ) if random_speed else initial_speed ) # Place the cars i += ( randint(1 , max_speed * 2 ) if random_frequency else frequency ) # Arbitrary number, may need tuning return highway def __lowerCamelCase ( __a :list , __a :int ) -> int: """simple docstring""" A__ = 0 A__ = highway_now[car_index + 1 :] for cell in range(len(__a ) ): # May need a better name for this if cells[cell] != -1: # If the cell is not empty then return distance # we have the distance we wanted distance += 1 # Here if the car is near the end of the highway return distance + get_distance(__a , -1 ) def __lowerCamelCase ( __a :list , __a :float , __a :int ) -> list: """simple docstring""" A__ = len(__a ) # Beforce calculations, the highway is empty A__ = [-1] * number_of_cells for car_index in range(__a ): if highway_now[car_index] != -1: # Add 1 to the current speed of the car and cap the speed A__ = min(highway_now[car_index] + 1 , __a ) # Number of empty cell before the next car A__ = get_distance(__a , __a ) - 1 # We can't have the car causing an accident A__ = min(next_highway[car_index] , __a ) if random() < probability: # Randomly, a driver will slow down A__ = max(next_highway[car_index] - 1 , 0 ) return next_highway def __lowerCamelCase ( __a :list , __a :int , __a :float , __a :int ) -> list: """simple docstring""" A__ = len(highway[0] ) for i in range(__a ): A__ = update(highway[i] , __a , __a ) A__ = [-1] * number_of_cells for car_index in range(__a ): A__ = next_speeds_calculated[car_index] if speed != -1: # Change the position based on the speed (with % to create the loop) A__ = (car_index + speed) % number_of_cells # Commit the change of position A__ = speed highway.append(__a ) return highway if __name__ == "__main__": import doctest doctest.testmod()
274
from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time A : Dict = Lock() def __lowerCamelCase ( __a :Dict , __a :List[str] , __a :Optional[int] , __a :Optional[int] , __a :Optional[Any] , __a :Optional[int] , __a :int ) -> Dict: """simple docstring""" global process_lock # we perform n swaps since after n swaps we know we are sorted # we *could* stop early if we are sorted already, but it takes as long to # find out we are sorted as it does to sort the list with this algorithm for i in range(0 , 1_0 ): if (i + position) % 2 == 0 and r_send is not None: # send your value to your right neighbor process_lock.acquire() r_send[1].send(__a ) process_lock.release() # receive your right neighbor's value process_lock.acquire() A__ = rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left A__ = min(__a , __a ) elif (i + position) % 2 != 0 and l_send is not None: # send your value to your left neighbor process_lock.acquire() l_send[1].send(__a ) process_lock.release() # receive your left neighbor's value process_lock.acquire() A__ = lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right A__ = max(__a , __a ) # after all swaps are performed, send the values back to main result_pipe[1].send(__a ) def __lowerCamelCase ( __a :List[str] ) -> int: """simple docstring""" A__ = [] A__ = [] # initialize the list of pipes where the values will be retrieved for _ in arr: result_pipe.append(Pipe() ) # creates the processes # the first and last process only have one neighbor so they are made outside # of the loop A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) A__ = temp_rs A__ = temp_rr for i in range(1 , len(__a ) - 1 ): A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) A__ = temp_rs A__ = temp_rr process_array_.append( Process( target=__a , args=( len(__a ) - 1, arr[len(__a ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(__a ) - 1], ) , ) ) # start the processes for p in process_array_: p.start() # wait for the processes to end and write their values to the list for p in range(0 , len(__a ) ): A__ = result_pipe[p][0].recv() process_array_[p].join() return arr def __lowerCamelCase ( ) -> str: """simple docstring""" A__ = list(range(1_0 , 0 , -1 ) ) print("""Initial List""" ) print(*__a ) A__ = odd_even_transposition(__a ) print("""Sorted List\n""" ) print(*__a ) if __name__ == "__main__": main()
274
1
def __lowerCamelCase ( __a :list[int] , __a :str ) -> list[int]: """simple docstring""" A__ = int(__a ) # Initialize Result A__ = [] # Traverse through all denomination for denomination in reversed(__a ): # Find denominations while int(__a ) >= int(__a ): total_value -= int(__a ) answer.append(__a ) # Append the "answers" array return answer # Driver Code if __name__ == "__main__": A : Optional[Any] = [] A : Union[str, Any] = '''0''' if ( input('''Do you want to enter your denominations ? (yY/n): ''').strip().lower() == "y" ): A : List[str] = int(input('''Enter the number of denominations you want to add: ''').strip()) for i in range(0, n): denominations.append(int(input(F'''Denomination {i}: ''').strip())) A : Dict = input('''Enter the change you want to make in Indian Currency: ''').strip() else: # All denominations of Indian Currency if user does not enter A : List[str] = [1, 2, 5, 1_0, 2_0, 5_0, 1_0_0, 5_0_0, 2_0_0_0] A : List[str] = input('''Enter the change you want to make: ''').strip() if int(value) == 0 or int(value) < 0: print('''The total value cannot be zero or negative.''') else: print(F'''Following is minimal change for {value}: ''') A : Dict = find_minimum_change(denominations, value) # Print result for i in range(len(answer)): print(answer[i], end=''' ''')
274
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def __lowerCamelCase ( __a :Dict ) -> Any: """simple docstring""" A__ = [ """decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(__a , __a ) def __lowerCamelCase ( __a :str ) -> Union[str, Any]: """simple docstring""" A__ , A__ = emb.weight.shape A__ = nn.Linear(__a , __a , bias=__a ) A__ = emb.weight.data return lin_layer def __lowerCamelCase ( __a :str ) -> List[str]: """simple docstring""" A__ = torch.load(__a , map_location="""cpu""" ) A__ = Namespace(**checkpoint["""cfg"""]["""model"""] ) A__ = checkpoint["""model"""] remove_ignore_keys_(__a ) A__ = state_dict["""decoder.embed_tokens.weight"""].shape[0] A__ = {key.replace("""decoder""" , """model""" ): val for key, val in state_dict.items()} A__ = XGLMConfig( vocab_size=__a , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""gelu""" , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) A__ = XGLMForCausalLM(__a ) A__ = model.load_state_dict(__a , strict=__a ) print(__a ) A__ = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": A : int = argparse.ArgumentParser() # Required parameters parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') A : str = parser.parse_args() A : str = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
274
1
from typing import Union import fire import torch from tqdm import tqdm def __lowerCamelCase ( __a :str , __a :str = "cpu" , __a :Union[str, None] = None ) -> None: """simple docstring""" A__ = torch.load(__a , map_location=__a ) for k, v in tqdm(state_dict.items() ): if not isinstance(__a , torch.Tensor ): raise TypeError("""FP16 conversion only works on paths that are saved state dicts, like pytorch_model.bin""" ) A__ = v.half() if save_path is None: # overwrite src_path A__ = src_path torch.save(__a , __a ) if __name__ == "__main__": fire.Fire(convert)
274
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 A (unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : List[str]=13 , __lowerCAmelCase : int=7 , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : int=True , __lowerCAmelCase : Dict=True , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : List[Any]=99 , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Optional[Any]=5 , __lowerCAmelCase : Tuple=4 , __lowerCAmelCase : Any=37 , __lowerCAmelCase : str="gelu" , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : Union[str, Any]=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : int=16 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : List[Any]=0.0_2 , __lowerCAmelCase : Tuple=4 , ) -> Dict: """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_attention_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_choices def a_ ( self : Any ) -> str: """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_attention_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ = 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=__lowerCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a_ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class A (SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a_ ( self : str ) -> Optional[int]: """simple docstring""" A__ = FlaxAlbertModelTester(self ) @slow def a_ ( self : int ) -> Tuple: """simple docstring""" for model_class_name in self.all_model_classes: A__ = model_class_name.from_pretrained("""albert-base-v2""" ) A__ = model(np.ones((1, 1) ) ) self.assertIsNotNone(__lowerCAmelCase ) @require_flax class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : Dict ) -> List[Any]: """simple docstring""" A__ = FlaxAlbertModel.from_pretrained("""albert-base-v2""" ) A__ = np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] ) A__ = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase )[0] A__ = (1, 11, 7_68) self.assertEqual(output.shape , __lowerCAmelCase ) A__ = 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] , __lowerCAmelCase , atol=1e-4 ) )
274
1
import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class A : '''simple docstring''' @staticmethod def a_ ( *__lowerCAmelCase : Union[str, Any] , **__lowerCAmelCase : Optional[Any] ) -> Tuple: """simple docstring""" pass @is_pipeline_test @require_vision class A (unittest.TestCase ): '''simple docstring''' @require_torch def a_ ( self : str ) -> List[Any]: """simple docstring""" A__ = pipeline( model="""hf-internal-testing/tiny-random-clip-zero-shot-image-classification""" , ) A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) A__ = image_classifier(__lowerCAmelCase , candidate_labels=["""a""", """b""", """c"""] ) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(__lowerCAmelCase ) , [ [{"""score""": 0.3_3_3, """label""": """a"""}, {"""score""": 0.3_3_3, """label""": """b"""}, {"""score""": 0.3_3_3, """label""": """c"""}], [{"""score""": 0.3_3_3, """label""": """a"""}, {"""score""": 0.3_3_3, """label""": """c"""}, {"""score""": 0.3_3_3, """label""": """b"""}], ] , ) A__ = image_classifier([image] * 5 , candidate_labels=["""A""", """B""", """C"""] , batch_size=2 ) self.assertEqual( nested_simplify(__lowerCAmelCase ) , [ [ {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, ], [ {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, ], [ {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, ], [ {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, ], [ {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, ], ] , ) @require_tf def a_ ( self : Union[str, Any] ) -> str: """simple docstring""" A__ = pipeline( model="""hf-internal-testing/tiny-random-clip-zero-shot-image-classification""" , framework="""tf""" ) A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) A__ = image_classifier(__lowerCAmelCase , candidate_labels=["""a""", """b""", """c"""] ) self.assertEqual( nested_simplify(__lowerCAmelCase ) , [{"""score""": 0.3_3_3, """label""": """a"""}, {"""score""": 0.3_3_3, """label""": """b"""}, {"""score""": 0.3_3_3, """label""": """c"""}] , ) A__ = image_classifier([image] * 5 , candidate_labels=["""A""", """B""", """C"""] , batch_size=2 ) self.assertEqual( nested_simplify(__lowerCAmelCase ) , [ [ {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, ], [ {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, ], [ {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, ], [ {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, ], [ {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, {"""score""": 0.3_3_3, """label""": ANY(__lowerCAmelCase )}, ], ] , ) @slow @require_torch def a_ ( self : Any ) -> Any: """simple docstring""" A__ = pipeline( task="""zero-shot-image-classification""" , model="""openai/clip-vit-base-patch32""" , ) # This is an image of 2 cats with remotes and no planes A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) A__ = image_classifier(__lowerCAmelCase , candidate_labels=["""cat""", """plane""", """remote"""] ) self.assertEqual( nested_simplify(__lowerCAmelCase ) , [ {"""score""": 0.5_1_1, """label""": """remote"""}, {"""score""": 0.4_8_5, """label""": """cat"""}, {"""score""": 0.0_0_4, """label""": """plane"""}, ] , ) A__ = image_classifier([image] * 5 , candidate_labels=["""cat""", """plane""", """remote"""] , batch_size=2 ) self.assertEqual( nested_simplify(__lowerCAmelCase ) , [ [ {"""score""": 0.5_1_1, """label""": """remote"""}, {"""score""": 0.4_8_5, """label""": """cat"""}, {"""score""": 0.0_0_4, """label""": """plane"""}, ], ] * 5 , ) @slow @require_tf def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" A__ = pipeline( task="""zero-shot-image-classification""" , model="""openai/clip-vit-base-patch32""" , framework="""tf""" ) # This is an image of 2 cats with remotes and no planes A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) A__ = image_classifier(__lowerCAmelCase , candidate_labels=["""cat""", """plane""", """remote"""] ) self.assertEqual( nested_simplify(__lowerCAmelCase ) , [ {"""score""": 0.5_1_1, """label""": """remote"""}, {"""score""": 0.4_8_5, """label""": """cat"""}, {"""score""": 0.0_0_4, """label""": """plane"""}, ] , ) A__ = image_classifier([image] * 5 , candidate_labels=["""cat""", """plane""", """remote"""] , batch_size=2 ) self.assertEqual( nested_simplify(__lowerCAmelCase ) , [ [ {"""score""": 0.5_1_1, """label""": """remote"""}, {"""score""": 0.4_8_5, """label""": """cat"""}, {"""score""": 0.0_0_4, """label""": """plane"""}, ], ] * 5 , )
274
from sklearn.metrics import fa_score import datasets A : Any = ''' The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation: F1 = 2 * (precision * recall) / (precision + recall) ''' A : List[Any] = ''' Args: predictions (`list` of `int`): Predicted labels. references (`list` of `int`): Ground truth labels. labels (`list` of `int`): The set of labels to include when `average` is not set to `\'binary\'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None. pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1. average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`. - \'binary\': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary. - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives. - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - \'weighted\': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. This option can result in an F-score that is not between precision and recall. - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). sample_weight (`list` of `float`): Sample weights Defaults to None. Returns: f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better. Examples: Example 1-A simple binary example >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0]) >>> print(results) {\'f1\': 0.5} Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0) >>> print(round(results[\'f1\'], 2)) 0.67 Example 3-The same simple binary example as in Example 1, but with `sample_weight` included. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3]) >>> print(round(results[\'f1\'], 2)) 0.35 Example 4-A multiclass example, with different values for the `average` input. >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro") >>> print(round(results[\'f1\'], 2)) 0.33 >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {\'f1\': array([0.8, 0. , 0. ])} ''' A : List[Any] = ''' @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A (datasets.Metric ): '''simple docstring''' def a_ ( self : Optional[int] ) -> List[Any]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html"""] , ) def a_ ( self : Any , __lowerCAmelCase : Tuple , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict=None , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : Any="binary" , __lowerCAmelCase : Optional[int]=None ) -> List[Any]: """simple docstring""" A__ = fa_score( __lowerCAmelCase , __lowerCAmelCase , labels=__lowerCAmelCase , pos_label=__lowerCAmelCase , average=__lowerCAmelCase , sample_weight=__lowerCAmelCase ) return {"f1": float(__lowerCAmelCase ) if score.size == 1 else score}
274
1
from ...configuration_utils import PretrainedConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : Union[str, Any] = { '''microsoft/markuplm-base''': '''https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json''', '''microsoft/markuplm-large''': '''https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json''', } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Optional[int] = '''markuplm''' def __init__( self : Dict , __lowerCAmelCase : Union[str, Any]=3_05_22 , __lowerCAmelCase : List[str]=7_68 , __lowerCAmelCase : Optional[Any]=12 , __lowerCAmelCase : str=12 , __lowerCAmelCase : str=30_72 , __lowerCAmelCase : Dict="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : List[str]=0.1 , __lowerCAmelCase : Union[str, Any]=5_12 , __lowerCAmelCase : Optional[Any]=2 , __lowerCAmelCase : Optional[Any]=0.0_2 , __lowerCAmelCase : Dict=1e-12 , __lowerCAmelCase : Optional[int]=0 , __lowerCAmelCase : List[Any]=0 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : List[Any]=2_56 , __lowerCAmelCase : Dict=10_24 , __lowerCAmelCase : Tuple=2_16 , __lowerCAmelCase : Union[str, Any]=10_01 , __lowerCAmelCase : int=32 , __lowerCAmelCase : str=50 , __lowerCAmelCase : List[str]="absolute" , __lowerCAmelCase : Any=True , __lowerCAmelCase : Optional[int]=None , **__lowerCAmelCase : Any , ) -> Any: """simple docstring""" super().__init__( pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache A__ = classifier_dropout # additional properties A__ = max_depth A__ = max_xpath_tag_unit_embeddings A__ = max_xpath_subs_unit_embeddings A__ = tag_pad_id A__ = subs_pad_id A__ = xpath_unit_hidden_size
274
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Any = '''xlm-roberta''' def __init__( self : Optional[Any] , __lowerCAmelCase : List[Any]=3_05_22 , __lowerCAmelCase : int=7_68 , __lowerCAmelCase : Tuple=12 , __lowerCAmelCase : str=12 , __lowerCAmelCase : Union[str, Any]=30_72 , __lowerCAmelCase : Union[str, Any]="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-12 , __lowerCAmelCase : Union[str, Any]=1 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Tuple="absolute" , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=None , **__lowerCAmelCase : str , ) -> Optional[Any]: """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": A__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A__ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
274
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) A : Any = { '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig'''] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Dict = ['''VisionEncoderDecoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Union[str, Any] = ['''TFVisionEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[str] = ['''FlaxVisionEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys A : Union[str, Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
274
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean A : str = 0 A : Any = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] A : Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right A : Union[str, Any] = tuple[int, int] class A : '''simple docstring''' def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Node | None , ) -> None: """simple docstring""" A__ = pos_x A__ = pos_y A__ = (pos_y, pos_x) A__ = goal_x A__ = goal_y A__ = g_cost A__ = parent A__ = self.calculate_heuristic() A__ = self.g_cost + self.h_cost def a_ ( self : Dict ) -> float: """simple docstring""" A__ = self.pos_x - self.goal_x A__ = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(__lowerCAmelCase ) + abs(__lowerCAmelCase ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : int , __lowerCAmelCase : Node ) -> bool: """simple docstring""" return self.f_cost < other.f_cost class A : '''simple docstring''' def __init__( self : Union[str, Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> Tuple: """simple docstring""" A__ = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) A__ = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , __lowerCAmelCase ) A__ = [self.start] A__ = [] A__ = False def a_ ( self : List[str] ) -> list[TPosition]: """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() A__ = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) A__ = self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) return [self.start.pos] def a_ ( self : Optional[Any] , __lowerCAmelCase : Node ) -> list[Node]: """simple docstring""" A__ = [] for action in delta: A__ = parent.pos_x + action[1] A__ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def a_ ( self : List[Any] , __lowerCAmelCase : Node | None ) -> list[TPosition]: """simple docstring""" A__ = node A__ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A__ = current_node.parent path.reverse() return path class A : '''simple docstring''' def __init__( self : Optional[Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> None: """simple docstring""" A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = False def a_ ( self : int ) -> list[TPosition]: """simple docstring""" while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() A__ = self.fwd_astar.open_nodes.pop(0 ) A__ = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( __lowerCAmelCase , __lowerCAmelCase ) self.fwd_astar.closed_nodes.append(__lowerCAmelCase ) self.bwd_astar.closed_nodes.append(__lowerCAmelCase ) A__ = current_bwd_node A__ = current_fwd_node A__ = { self.fwd_astar: self.fwd_astar.get_successors(__lowerCAmelCase ), self.bwd_astar: self.bwd_astar.get_successors(__lowerCAmelCase ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = astar.open_nodes.pop( astar.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(__lowerCAmelCase ) else: astar.open_nodes.append(__lowerCAmelCase ) return [self.fwd_astar.start.pos] def a_ ( self : List[str] , __lowerCAmelCase : Node , __lowerCAmelCase : Node ) -> list[TPosition]: """simple docstring""" A__ = self.fwd_astar.retrace_path(__lowerCAmelCase ) A__ = self.bwd_astar.retrace_path(__lowerCAmelCase ) bwd_path.pop() bwd_path.reverse() A__ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] A : Optional[int] = (0, 0) A : int = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) A : Dict = time.time() A : Optional[Any] = AStar(init, goal) A : Optional[int] = a_star.search() A : Optional[int] = time.time() - start_time print(F'''AStar execution time = {end_time:f} seconds''') A : Dict = time.time() A : Tuple = BidirectionalAStar(init, goal) A : List[Any] = time.time() - bd_start_time print(F'''BidirectionalAStar execution time = {bd_end_time:f} seconds''')
274
1
A : Any = '''Tobias Carryer''' from time import time class A : '''simple docstring''' def __init__( self : Optional[int] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Any=int(time() ) ) -> List[Any]: # noqa: B008 """simple docstring""" A__ = multiplier A__ = increment A__ = modulo A__ = seed def a_ ( self : List[Any] ) -> List[str]: """simple docstring""" A__ = (self.multiplier * self.seed + self.increment) % self.modulo return self.seed if __name__ == "__main__": # Show the LCG in action. A : int = LinearCongruentialGenerator(1_6_6_4_5_2_5, 1_0_1_3_9_0_4_2_2_3, 2 << 3_1) while True: print(lcg.next_number())
274
import unittest from transformers.utils.backbone_utils import ( BackboneMixin, get_aligned_output_features_output_indices, verify_out_features_out_indices, ) class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Any ) -> Union[str, Any]: """simple docstring""" A__ = ["""a""", """b""", """c"""] # Defaults to last layer if both are None A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""c"""] ) self.assertEqual(__lowerCAmelCase , [2] ) # Out indices set to match out features A__ , A__ = get_aligned_output_features_output_indices(["""a""", """c"""] , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features set to match out indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [0, 2] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features selected from negative indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [-3, -1] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [-3, -1] ) def a_ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , __lowerCAmelCase ) # Out features must be a list with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(("""a""", """b""") , (0, 1) , ["""a""", """b"""] ) # Out features must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , ["""a"""] ) # Out indices must be a list or tuple with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , 0 , ["""a""", """b"""] ) # Out indices must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , (0, 1) , ["""a"""] ) # Out features and out indices must be the same length with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0,) , ["""a""", """b""", """c"""] ) # Out features should match out indices with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 2) , ["""a""", """b""", """c"""] ) # Out features and out indices should be in order with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""b""", """a"""] , (0, 1) , ["""a""", """b"""] ) # Check passes with valid inputs verify_out_features_out_indices(["""a""", """b""", """d"""] , (0, 1, -1) , ["""a""", """b""", """c""", """d"""] ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" A__ = BackboneMixin() A__ = ["""a""", """b""", """c"""] A__ = ["""a""", """c"""] A__ = [0, 2] # Check that the output features and indices are set correctly self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [0, 2] ) # Check out features and indices are updated correctly A__ = ["""a""", """b"""] self.assertEqual(backbone.out_features , ["""a""", """b"""] ) self.assertEqual(backbone.out_indices , [0, 1] ) A__ = [-3, -1] self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [-3, -1] )
274
1
import warnings from ...utils import logging from .image_processing_yolos import YolosImageProcessor A : List[Any] = logging.get_logger(__name__) class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Tuple , *__lowerCAmelCase : str , **__lowerCAmelCase : List[Any] ) -> None: """simple docstring""" warnings.warn( """The class YolosFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use YolosImageProcessor instead.""" , __lowerCAmelCase , ) super().__init__(*__lowerCAmelCase , **__lowerCAmelCase )
274
from collections import deque class A : '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" A__ = process_name # process name A__ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time A__ = arrival_time A__ = burst_time # remaining burst time A__ = 0 # total time of the process wait in ready queue A__ = 0 # time from arrival time to completion time class A : '''simple docstring''' def __init__( self : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : list[int] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int , ) -> None: """simple docstring""" A__ = number_of_queues # time slice of queues that round robin algorithm applied A__ = time_slices # unfinished process is in this ready_queue A__ = queue # current time A__ = current_time # finished process is in this sequence queue A__ = deque() def a_ ( self : Dict ) -> list[str]: """simple docstring""" A__ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def a_ ( self : Tuple , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def a_ ( self : Optional[Any] , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def a_ ( self : Dict , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): completion_times.append(queue[i].stop_time ) return completion_times def a_ ( self : int , __lowerCAmelCase : deque[Process] ) -> list[int]: """simple docstring""" return [q.burst_time for q in queue] def a_ ( self : Any , __lowerCAmelCase : Process ) -> int: """simple docstring""" process.waiting_time += self.current_time - process.stop_time return process.waiting_time def a_ ( self : Union[str, Any] , __lowerCAmelCase : deque[Process] ) -> deque[Process]: """simple docstring""" A__ = deque() # sequence deque of finished process while len(__lowerCAmelCase ) != 0: A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__lowerCAmelCase ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 A__ = 0 # set the process's turnaround time because it is finished A__ = self.current_time - cp.arrival_time # set the completion time A__ = self.current_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def a_ ( self : Optional[Any] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int ) -> tuple[deque[Process], deque[Process]]: """simple docstring""" A__ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__lowerCAmelCase ) ): A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__lowerCAmelCase ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time A__ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__lowerCAmelCase ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished A__ = 0 # set the finish time A__ = self.current_time # update the process' turnaround time because it is finished A__ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def a_ ( self : List[Any] ) -> deque[Process]: """simple docstring""" for i in range(self.number_of_queues - 1 ): A__ , A__ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest A : Union[str, Any] = Process('''P1''', 0, 5_3) A : Optional[Any] = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : int = Process('''P4''', 0, 2_4) A : Any = 3 A : List[Any] = [1_7, 2_5] A : Optional[Any] = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) A : Optional[Any] = Process('''P1''', 0, 5_3) A : int = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : Tuple = Process('''P4''', 0, 2_4) A : Union[str, Any] = 3 A : Optional[Any] = [1_7, 2_5] A : Tuple = deque([Pa, Pa, Pa, Pa]) A : Optional[int] = MLFQ(number_of_queues, time_slices, queue, 0) A : Dict = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( F'''waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print completion times of processes(P1, P2, P3, P4) print( F'''completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print total turnaround times of processes(P1, P2, P3, P4) print( F'''turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print sequence of finished processes print( F'''sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}''' )
274
1
import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer A : Optional[Any] = '''bart''' A : Optional[int] = True @st.cache(allow_output_mutation=__a ) def __lowerCamelCase ( ) -> Optional[int]: """simple docstring""" if LOAD_DENSE_INDEX: A__ = AutoTokenizer.from_pretrained("""yjernite/retribert-base-uncased""" ) A__ = AutoModel.from_pretrained("""yjernite/retribert-base-uncased""" ).to("""cuda:0""" ) A__ = qar_model.eval() else: A__ , A__ = (None, None) if MODEL_TYPE == "bart": A__ = AutoTokenizer.from_pretrained("""yjernite/bart_eli5""" ) A__ = AutoModelForSeqaSeqLM.from_pretrained("""yjernite/bart_eli5""" ).to("""cuda:0""" ) A__ = torch.load("""seq2seq_models/eli5_bart_model_blm_2.pth""" ) sas_model.load_state_dict(save_dict["""model"""] ) A__ = sas_model.eval() else: A__ , A__ = make_qa_sas_model( model_name="""t5-small""" , from_file="""seq2seq_models/eli5_t5_model_1024_4.pth""" , device="""cuda:0""" ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=__a ) def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" if LOAD_DENSE_INDEX: A__ = faiss.StandardGpuResources() A__ = datasets.load_dataset(path="""wiki_snippets""" , name="""wiki40b_en_100_0""" )["""train"""] A__ = np.memmap( """wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat""" , dtype="""float32""" , mode="""r""" , shape=(wikiaab_passages.num_rows, 1_2_8) , ) A__ = faiss.IndexFlatIP(1_2_8 ) A__ = faiss.index_cpu_to_gpu(__a , 1 , __a ) wikiaab_gpu_index_flat.add(__a ) # TODO fix for larger GPU else: A__ , A__ = (None, None) A__ = Elasticsearch([{"""host""": """localhost""", """port""": """9200"""}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=__a ) def __lowerCamelCase ( ) -> Any: """simple docstring""" A__ = datasets.load_dataset("""eli5""" , name="""LFQA_reddit""" ) A__ = elia["""train_eli5"""] A__ = np.memmap( """eli5_questions_reps.dat""" , dtype="""float32""" , mode="""r""" , shape=(elia_train.num_rows, 1_2_8) ) A__ = faiss.IndexFlatIP(1_2_8 ) eli5_train_q_index.add(__a ) return (elia_train, eli5_train_q_index) A, A, A : str = load_indexes() A, A, A, A : str = load_models() A, A : Optional[Any] = load_train_data() def __lowerCamelCase ( __a :int , __a :List[str]=1_0 ) -> Any: """simple docstring""" A__ = embed_questions_for_retrieval([question] , __a , __a ) A__ , A__ = eli5_train_q_index.search(__a , __a ) A__ = [elia_train[int(__a )] for i in I[0]] return nn_examples def __lowerCamelCase ( __a :Optional[Any] , __a :Any="wiki40b" , __a :int="dense" , __a :Any=1_0 ) -> Optional[int]: """simple docstring""" if source == "none": A__ , A__ = (""" <P> """.join(["""""" for _ in range(1_1 )] ).strip(), []) else: if method == "dense": A__ , A__ = query_qa_dense_index( __a , __a , __a , __a , __a , __a ) else: A__ , A__ = query_es_index( __a , __a , index_name="""english_wiki40b_snippets_100w""" , n_results=__a , ) A__ = [ (res["""article_title"""], res["""section_title"""].strip(), res["""score"""], res["""passage_text"""]) for res in hit_lst ] A__ = """question: {} context: {}""".format(__a , __a ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda __a : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda __a : None), } ) def __lowerCamelCase ( __a :List[str] , __a :List[Any] , __a :Optional[Any] , __a :Union[str, Any]=6_4 , __a :str=2_5_6 , __a :Union[str, Any]=False , __a :Union[str, Any]=2 , __a :int=0.95 , __a :Any=0.8 ) -> List[str]: """simple docstring""" with torch.no_grad(): A__ = qa_sas_generate( __a , __a , __a , num_answers=1 , num_beams=__a , min_len=__a , max_len=__a , do_sample=__a , temp=__a , top_p=__a , top_k=__a , max_input_length=1_0_2_4 , device="""cuda:0""" , )[0] return (answer, support_list) st.title('''Long Form Question Answering with ELI5''') # Start sidebar A : int = '''<img src=\'https://huggingface.co/front/assets/huggingface_logo.svg\'>''' A : Optional[int] = ''' <html> <head> <style> .img-container { padding-left: 90px; padding-right: 90px; padding-top: 50px; padding-bottom: 50px; background-color: #f0f3f9; } </style> </head> <body> <span class="img-container"> <!-- Inline parent element --> %s </span> </body> </html> ''' % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia A : List[str] = ''' This demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html). First, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset, a pre-processed fixed snapshot of Wikipedia. ''' st.sidebar.markdown(description, unsafe_allow_html=True) A : Optional[int] = [ '''Answer the question''', '''View the retrieved document only''', '''View the most similar ELI5 question and answer''', '''Show me everything, please!''', ] A : List[Any] = st.sidebar.checkbox('''Demo options''') if demo_options: A : Optional[Any] = st.sidebar.selectbox( '''''', action_list, index=3, ) A : Union[str, Any] = action_list.index(action_st) A : Union[str, Any] = st.sidebar.selectbox( '''''', ['''Show full text of passages''', '''Show passage section titles'''], index=0, ) A : Tuple = show_type == '''Show full text of passages''' else: A : str = 3 A : Tuple = True A : Union[str, Any] = st.sidebar.checkbox('''Retrieval options''') if retrieval_options: A : Tuple = ''' ### Information retriever options The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs. The answer is then generated by sequence to sequence model which takes the question and retrieved document as input. ''' st.sidebar.markdown(retriever_info) A : str = st.sidebar.selectbox('''Which Wikipedia format should the model use?''', ['''wiki40b''', '''none''']) A : int = st.sidebar.selectbox('''Which Wikipedia indexer should the model use?''', ['''dense''', '''sparse''', '''mixed''']) else: A : Optional[Any] = '''wiki40b''' A : Tuple = '''dense''' A : List[str] = '''beam''' A : Optional[Any] = 2 A : Tuple = 6_4 A : List[str] = 2_5_6 A : str = None A : int = None A : List[Any] = st.sidebar.checkbox('''Generation options''') if generate_options: A : Any = ''' ### Answer generation options The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large) weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with **beam** search, or **sample** from the decoder\'s output probabilities. ''' st.sidebar.markdown(generate_info) A : Any = st.sidebar.selectbox('''Would you like to use beam search or sample an answer?''', ['''beam''', '''sampled''']) A : Any = st.sidebar.slider( '''Minimum generation length''', min_value=8, max_value=2_5_6, value=6_4, step=8, format=None, key=None ) A : Any = st.sidebar.slider( '''Maximum generation length''', min_value=6_4, max_value=5_1_2, value=2_5_6, step=1_6, format=None, key=None ) if sampled == "beam": A : List[Any] = st.sidebar.slider('''Beam size''', min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: A : int = st.sidebar.slider( '''Nucleus sampling p''', min_value=0.1, max_value=1.0, value=0.95, step=0.01, format=None, key=None ) A : str = st.sidebar.slider( '''Temperature''', min_value=0.1, max_value=1.0, value=0.7, step=0.01, format=None, key=None ) A : Tuple = None # start main text A : Optional[Any] = [ '''<MY QUESTION>''', '''How do people make chocolate?''', '''Why do we get a fever when we are sick?''', '''How can different animals perceive different colors?''', '''What is natural language processing?''', '''What\'s the best way to treat a sunburn?''', '''What exactly are vitamins ?''', '''How does nuclear energy provide electricity?''', '''What\'s the difference between viruses and bacteria?''', '''Why are flutes classified as woodwinds when most of them are made out of metal ?''', '''Why do people like drinking coffee even though it tastes so bad?''', '''What happens when wine ages? How does it make the wine taste better?''', '''If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?''', '''How can we set a date to the beginning or end of an artistic period? Doesn\'t the change happen gradually?''', '''How does New Zealand have so many large bird predators?''', ] A : Tuple = st.selectbox( '''What would you like to ask? ---- select <MY QUESTION> to enter a new query''', questions_list, index=1, ) if question_s == "<MY QUESTION>": A : Any = st.text_input('''Enter your question here:''', '''''') else: A : List[Any] = question_s if st.button('''Show me!'''): if action in [0, 1, 3]: if index_type == "mixed": A, A : Dict = make_support(question, source=wiki_source, method='''dense''', n_results=1_0) A, A : int = make_support(question, source=wiki_source, method='''sparse''', n_results=1_0) A : Optional[Any] = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] A : Optional[Any] = support_list[:1_0] A : List[Any] = '''<P> ''' + ''' <P> '''.join([res[-1] for res in support_list]) else: A, A : Tuple = make_support(question, source=wiki_source, method=index_type, n_results=1_0) if action in [0, 3]: A, A : int = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == '''sampled'''), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown('''### The model generated answer is:''') st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown('''--- \n ### The model is drawing information from the following Wikipedia passages:''') for i, res in enumerate(support_list): A : Any = '''https://en.wikipedia.org/wiki/{}'''.format(res[0].replace(''' ''', '''_''')) A : Any = res[1].strip() if sec_titles == "": A : str = '''[{}]({})'''.format(res[0], wiki_url) else: A : Optional[Any] = sec_titles.split(''' & ''') A : str = ''' & '''.join( ['''[{}]({}#{})'''.format(sec.strip(), wiki_url, sec.strip().replace(''' ''', '''_''')) for sec in sec_list] ) st.markdown( '''{0:02d} - **Article**: {1:<18} <br> _Section_: {2}'''.format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( '''> <span style="font-family:arial; font-size:10pt;">''' + res[-1] + '''</span>''', unsafe_allow_html=True ) if action in [2, 3]: A : str = find_nearest_training(question) A : Union[str, Any] = nn_train_list[0] st.markdown( '''--- \n ### The most similar question in the ELI5 training set was: \n\n {}'''.format(train_exple['''title''']) ) A : Any = [ '''{}. {}'''.format(i + 1, ''' \n'''.join([line.strip() for line in ans.split('''\n''') if line.strip() != ''''''])) for i, (ans, sc) in enumerate(zip(train_exple['''answers''']['''text'''], train_exple['''answers''']['''score'''])) if i == 0 or sc > 2 ] st.markdown('''##### Its answers were: \n\n {}'''.format('''\n'''.join(answers_st))) A : Any = ''' --- **Disclaimer** *The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system. Evaluating biases of such a model and ensuring factual generations are still very much open research problems. Therefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.* ''' st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
274
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType A : str = logging.get_logger(__name__) A : Union[str, Any] = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Optional[Any] = '''layoutlmv3''' def __init__( self : Tuple , __lowerCAmelCase : Optional[int]=5_02_65 , __lowerCAmelCase : Tuple=7_68 , __lowerCAmelCase : Union[str, Any]=12 , __lowerCAmelCase : Any=12 , __lowerCAmelCase : List[Any]=30_72 , __lowerCAmelCase : Optional[int]="gelu" , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[Any]=0.1 , __lowerCAmelCase : Dict=5_12 , __lowerCAmelCase : Any=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-5 , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : int=0 , __lowerCAmelCase : Dict=2 , __lowerCAmelCase : Tuple=10_24 , __lowerCAmelCase : List[str]=1_28 , __lowerCAmelCase : Optional[int]=1_28 , __lowerCAmelCase : Any=True , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Any=1_28 , __lowerCAmelCase : str=64 , __lowerCAmelCase : Optional[int]=2_56 , __lowerCAmelCase : int=True , __lowerCAmelCase : int=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : int=2_24 , __lowerCAmelCase : Dict=3 , __lowerCAmelCase : List[Any]=16 , __lowerCAmelCase : Dict=None , **__lowerCAmelCase : Optional[Any] , ) -> Dict: """simple docstring""" super().__init__( vocab_size=__lowerCAmelCase , hidden_size=__lowerCAmelCase , num_hidden_layers=__lowerCAmelCase , num_attention_heads=__lowerCAmelCase , intermediate_size=__lowerCAmelCase , hidden_act=__lowerCAmelCase , hidden_dropout_prob=__lowerCAmelCase , attention_probs_dropout_prob=__lowerCAmelCase , max_position_embeddings=__lowerCAmelCase , type_vocab_size=__lowerCAmelCase , initializer_range=__lowerCAmelCase , layer_norm_eps=__lowerCAmelCase , pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = max_ad_position_embeddings A__ = coordinate_size A__ = shape_size A__ = has_relative_attention_bias A__ = rel_pos_bins A__ = max_rel_pos A__ = has_spatial_attention_bias A__ = rel_ad_pos_bins A__ = max_rel_ad_pos A__ = text_embed A__ = visual_embed A__ = input_size A__ = num_channels A__ = patch_size A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[str] = version.parse('''1.12''' ) @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def a_ ( self : Optional[int] ) -> float: """simple docstring""" return 1e-5 @property def a_ ( self : Tuple ) -> int: """simple docstring""" return 12 def a_ ( self : str , __lowerCAmelCase : "ProcessorMixin" , __lowerCAmelCase : int = -1 , __lowerCAmelCase : int = -1 , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional["TensorType"] = None , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 40 , __lowerCAmelCase : int = 40 , ) -> Mapping[str, Any]: """simple docstring""" setattr(processor.image_processor , """apply_ocr""" , __lowerCAmelCase ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX A__ = processor.tokenizer.num_special_tokens_to_add(__lowerCAmelCase ) A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__lowerCAmelCase ) # Generate dummy inputs according to compute batch and sequence A__ = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes A__ = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) A__ = self._generate_dummy_images(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = dict( processor( __lowerCAmelCase , text=__lowerCAmelCase , boxes=__lowerCAmelCase , return_tensors=__lowerCAmelCase , ) ) return inputs
274
1
from typing import List, Optional, Tuple, Union import torch from torch import nn from torch.nn import CrossEntropyLoss from ... import AutoBackbone from ...modeling_outputs import SemanticSegmenterOutput from ...modeling_utils import PreTrainedModel from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings from ...utils.backbone_utils import BackboneMixin from .configuration_upernet import UperNetConfig A : List[str] = [ '''openmmlab/upernet-convnext-tiny''', # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring A : List[Any] = '''UperNetConfig''' class A (nn.Module ): '''simple docstring''' def __init__( self : Optional[int] , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Union[int, Tuple[int, int]] , __lowerCAmelCase : Union[int, Tuple[int, int], str] = 0 , __lowerCAmelCase : bool = False , __lowerCAmelCase : Union[int, Tuple[int, int]] = 1 , ) -> None: """simple docstring""" super().__init__() A__ = nn.Convad( in_channels=__lowerCAmelCase , out_channels=__lowerCAmelCase , kernel_size=__lowerCAmelCase , padding=__lowerCAmelCase , bias=__lowerCAmelCase , dilation=__lowerCAmelCase , ) A__ = nn.BatchNormad(__lowerCAmelCase ) A__ = nn.ReLU() def a_ ( self : Tuple , __lowerCAmelCase : torch.Tensor ) -> torch.Tensor: """simple docstring""" A__ = self.conv(__lowerCAmelCase ) A__ = self.batch_norm(__lowerCAmelCase ) A__ = self.activation(__lowerCAmelCase ) return output class A (nn.Module ): '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" super().__init__() A__ = [ nn.AdaptiveAvgPoolad(__lowerCAmelCase ), UperNetConvModule(__lowerCAmelCase , __lowerCAmelCase , kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(__lowerCAmelCase ) , __lowerCAmelCase ) def a_ ( self : int , __lowerCAmelCase : torch.Tensor ) -> torch.Tensor: """simple docstring""" A__ = input for layer in self.layers: A__ = layer(__lowerCAmelCase ) return hidden_state class A (nn.Module ): '''simple docstring''' def __init__( self : List[Any] , __lowerCAmelCase : Tuple[int, ...] , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : bool ) -> None: """simple docstring""" super().__init__() A__ = pool_scales A__ = align_corners A__ = in_channels A__ = channels A__ = [] for i, pool_scale in enumerate(__lowerCAmelCase ): A__ = UperNetPyramidPoolingBlock(pool_scale=__lowerCAmelCase , in_channels=__lowerCAmelCase , channels=__lowerCAmelCase ) self.blocks.append(__lowerCAmelCase ) self.add_module(str(__lowerCAmelCase ) , __lowerCAmelCase ) def a_ ( self : Any , __lowerCAmelCase : torch.Tensor ) -> List[torch.Tensor]: """simple docstring""" A__ = [] for ppm in self.blocks: A__ = ppm(__lowerCAmelCase ) A__ = nn.functional.interpolate( __lowerCAmelCase , size=x.size()[2:] , mode="""bilinear""" , align_corners=self.align_corners ) ppm_outs.append(__lowerCAmelCase ) return ppm_outs class A (nn.Module ): '''simple docstring''' def __init__( self : Dict , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Tuple ) -> List[str]: """simple docstring""" super().__init__() A__ = config A__ = config.pool_scales # e.g. (1, 2, 3, 6) A__ = in_channels A__ = config.hidden_size A__ = False A__ = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) # PSP Module A__ = UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) A__ = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module A__ = nn.ModuleList() A__ = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer A__ = UperNetConvModule(__lowerCAmelCase , self.channels , kernel_size=1 ) A__ = UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1 ) self.lateral_convs.append(__lowerCAmelCase ) self.fpn_convs.append(__lowerCAmelCase ) A__ = UperNetConvModule( len(self.in_channels ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def a_ ( self : List[Any] ) -> Union[str, Any]: """simple docstring""" self.apply(self._init_weights ) def a_ ( self : Any , __lowerCAmelCase : List[str] ) -> Dict: """simple docstring""" if isinstance(__lowerCAmelCase , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def a_ ( self : List[Any] , __lowerCAmelCase : List[str] ) -> Dict: """simple docstring""" A__ = inputs[-1] A__ = [x] psp_outs.extend(self.psp_modules(__lowerCAmelCase ) ) A__ = torch.cat(__lowerCAmelCase , dim=1 ) A__ = self.bottleneck(__lowerCAmelCase ) return output def a_ ( self : Optional[int] , __lowerCAmelCase : torch.Tensor ) -> torch.Tensor: """simple docstring""" A__ = [lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(__lowerCAmelCase ) ) # build top-down path A__ = len(__lowerCAmelCase ) for i in range(used_backbone_levels - 1 , 0 , -1 ): A__ = laterals[i - 1].shape[2:] A__ = laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=__lowerCAmelCase , mode="""bilinear""" , align_corners=self.align_corners ) # build outputs A__ = [self.fpn_convs[i](laterals[i] ) for i in range(used_backbone_levels - 1 )] # append psp feature fpn_outs.append(laterals[-1] ) for i in range(used_backbone_levels - 1 , 0 , -1 ): A__ = nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode="""bilinear""" , align_corners=self.align_corners ) A__ = torch.cat(__lowerCAmelCase , dim=1 ) A__ = self.fpn_bottleneck(__lowerCAmelCase ) A__ = self.classifier(__lowerCAmelCase ) return output class A (nn.Module ): '''simple docstring''' def __init__( self : Optional[int] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : int = 2 , __lowerCAmelCase : int = 3 , __lowerCAmelCase : Union[int, Tuple[int, int]] = 1 ) -> None: """simple docstring""" super().__init__() A__ = config A__ = config.auxiliary_in_channels A__ = config.auxiliary_channels A__ = config.auxiliary_num_convs A__ = config.auxiliary_concat_input A__ = in_index A__ = (kernel_size // 2) * dilation A__ = [] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=__lowerCAmelCase , padding=__lowerCAmelCase , dilation=__lowerCAmelCase ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=__lowerCAmelCase , padding=__lowerCAmelCase , dilation=__lowerCAmelCase ) ) if self.num_convs == 0: A__ = nn.Identity() else: A__ = nn.Sequential(*__lowerCAmelCase ) if self.concat_input: A__ = UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=__lowerCAmelCase , padding=kernel_size // 2 ) A__ = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) def a_ ( self : List[Any] ) -> Tuple: """simple docstring""" self.apply(self._init_weights ) def a_ ( self : int , __lowerCAmelCase : int ) -> Union[str, Any]: """simple docstring""" if isinstance(__lowerCAmelCase , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def a_ ( self : Any , __lowerCAmelCase : torch.Tensor ) -> torch.Tensor: """simple docstring""" A__ = encoder_hidden_states[self.in_index] A__ = self.convs(__lowerCAmelCase ) if self.concat_input: A__ = self.conv_cat(torch.cat([hidden_states, output] , dim=1 ) ) A__ = self.classifier(__lowerCAmelCase ) return output class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : str = UperNetConfig __lowerCamelCase : List[Any] = '''pixel_values''' __lowerCamelCase : Optional[Any] = True def a_ ( self : int , __lowerCAmelCase : Union[str, Any] ) -> List[str]: """simple docstring""" if isinstance(__lowerCAmelCase , __lowerCAmelCase ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def a_ ( self : Any ) -> Tuple: """simple docstring""" self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[int]=False ) -> Any: """simple docstring""" if isinstance(__lowerCAmelCase , __lowerCAmelCase ): A__ = value A : Dict = R''' Parameters: This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. config ([`UperNetConfig`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. ''' A : Optional[Any] = R''' Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details. output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See `attentions` under returned tensors for more detail. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. ''' @add_start_docstrings( '''UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.''' , SCREAMING_SNAKE_CASE , ) class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : int , __lowerCAmelCase : List[str] ) -> List[Any]: """simple docstring""" super().__init__(__lowerCAmelCase ) A__ = AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) A__ = UperNetHead(__lowerCAmelCase , in_channels=self.backbone.channels ) A__ = UperNetFCNHead(__lowerCAmelCase ) if config.use_auxiliary_head else None # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format("""batch_size, sequence_length""" ) ) @replace_return_docstrings(output_type=__lowerCAmelCase , config_class=_CONFIG_FOR_DOC ) def a_ ( self : List[Any] , __lowerCAmelCase : Optional[torch.Tensor] = None , __lowerCAmelCase : Optional[bool] = None , __lowerCAmelCase : Optional[bool] = None , __lowerCAmelCase : Optional[torch.Tensor] = None , __lowerCAmelCase : Optional[bool] = None , ) -> Union[tuple, SemanticSegmenterOutput]: """simple docstring""" A__ = return_dict if return_dict is not None else self.config.use_return_dict A__ = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) A__ = output_attentions if output_attentions is not None else self.config.output_attentions A__ = self.backbone.forward_with_filtered_kwargs( __lowerCAmelCase , output_hidden_states=__lowerCAmelCase , output_attentions=__lowerCAmelCase ) A__ = outputs.feature_maps A__ = self.decode_head(__lowerCAmelCase ) A__ = nn.functional.interpolate(__lowerCAmelCase , size=pixel_values.shape[2:] , mode="""bilinear""" , align_corners=__lowerCAmelCase ) A__ = None if self.auxiliary_head is not None: A__ = self.auxiliary_head(__lowerCAmelCase ) A__ = nn.functional.interpolate( __lowerCAmelCase , size=pixel_values.shape[2:] , mode="""bilinear""" , align_corners=__lowerCAmelCase ) A__ = None if labels is not None: if self.config.num_labels == 1: raise ValueError("""The number of labels should be greater than one""" ) else: # compute weighted loss A__ = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) A__ = loss_fct(__lowerCAmelCase , __lowerCAmelCase ) A__ = loss_fct(__lowerCAmelCase , __lowerCAmelCase ) A__ = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: A__ = (logits,) + outputs[1:] else: A__ = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=__lowerCAmelCase , logits=__lowerCAmelCase , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
274
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) A : str = '''▁''' A : Any = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', '''tokenizer_config_file''': '''tokenizer_config.json''', } A : List[Any] = { '''vocab_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json''', }, '''spm_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_config_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json''', }, } A : Tuple = { '''facebook/m2m100_418M''': 1_0_2_4, } # fmt: off A : Optional[int] = { '''m2m100''': ['''af''', '''am''', '''ar''', '''ast''', '''az''', '''ba''', '''be''', '''bg''', '''bn''', '''br''', '''bs''', '''ca''', '''ceb''', '''cs''', '''cy''', '''da''', '''de''', '''el''', '''en''', '''es''', '''et''', '''fa''', '''ff''', '''fi''', '''fr''', '''fy''', '''ga''', '''gd''', '''gl''', '''gu''', '''ha''', '''he''', '''hi''', '''hr''', '''ht''', '''hu''', '''hy''', '''id''', '''ig''', '''ilo''', '''is''', '''it''', '''ja''', '''jv''', '''ka''', '''kk''', '''km''', '''kn''', '''ko''', '''lb''', '''lg''', '''ln''', '''lo''', '''lt''', '''lv''', '''mg''', '''mk''', '''ml''', '''mn''', '''mr''', '''ms''', '''my''', '''ne''', '''nl''', '''no''', '''ns''', '''oc''', '''or''', '''pa''', '''pl''', '''ps''', '''pt''', '''ro''', '''ru''', '''sd''', '''si''', '''sk''', '''sl''', '''so''', '''sq''', '''sr''', '''ss''', '''su''', '''sv''', '''sw''', '''ta''', '''th''', '''tl''', '''tn''', '''tr''', '''uk''', '''ur''', '''uz''', '''vi''', '''wo''', '''xh''', '''yi''', '''yo''', '''zh''', '''zu'''], '''wmt21''': ['''en''', '''ha''', '''is''', '''ja''', '''cs''', '''ru''', '''zh''', '''de'''] } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = VOCAB_FILES_NAMES __lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = ['''input_ids''', '''attention_mask'''] __lowerCamelCase : List[int] = [] __lowerCamelCase : List[int] = [] def __init__( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any]=None , __lowerCAmelCase : str=None , __lowerCAmelCase : List[Any]="<s>" , __lowerCAmelCase : List[Any]="</s>" , __lowerCAmelCase : Optional[int]="</s>" , __lowerCAmelCase : Optional[Any]="<pad>" , __lowerCAmelCase : Any="<unk>" , __lowerCAmelCase : Any="m2m100" , __lowerCAmelCase : Optional[Dict[str, Any]] = None , __lowerCAmelCase : Dict=8 , **__lowerCAmelCase : Tuple , ) -> None: """simple docstring""" A__ = {} if sp_model_kwargs is None else sp_model_kwargs A__ = language_codes A__ = FAIRSEQ_LANGUAGE_CODES[language_codes] A__ = {lang_code: f'__{lang_code}__' for lang_code in fairseq_language_code} A__ = kwargs.get("""additional_special_tokens""" , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(__lowerCAmelCase ) for lang_code in fairseq_language_code if self.get_lang_token(__lowerCAmelCase ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=__lowerCAmelCase , tgt_lang=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , language_codes=__lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = vocab_file A__ = load_json(__lowerCAmelCase ) A__ = {v: k for k, v in self.encoder.items()} A__ = spm_file A__ = load_spm(__lowerCAmelCase , self.sp_model_kwargs ) A__ = len(self.encoder ) A__ = { self.get_lang_token(__lowerCAmelCase ): self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase ) } A__ = {lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase )} A__ = {v: k for k, v in self.lang_token_to_id.items()} A__ = src_lang if src_lang is not None else """en""" A__ = tgt_lang A__ = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) A__ = num_madeup_words @property def a_ ( self : Optional[int] ) -> int: """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def a_ ( self : Optional[Any] ) -> str: """simple docstring""" return self._src_lang @src_lang.setter def a_ ( self : List[Any] , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def a_ ( self : Optional[int] , __lowerCAmelCase : str ) -> List[str]: """simple docstring""" return self.sp_model.encode(__lowerCAmelCase , out_type=__lowerCAmelCase ) def a_ ( self : Optional[Any] , __lowerCAmelCase : Dict ) -> Optional[Any]: """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(__lowerCAmelCase , self.encoder[self.unk_token] ) def a_ ( self : Optional[int] , __lowerCAmelCase : int ) -> str: """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(__lowerCAmelCase , self.unk_token ) def a_ ( self : Optional[int] , __lowerCAmelCase : Dict ) -> str: """simple docstring""" A__ = [] A__ = """""" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(__lowerCAmelCase ) + token A__ = [] else: current_sub_tokens.append(__lowerCAmelCase ) out_string += self.sp_model.decode(__lowerCAmelCase ) return out_string.strip() def a_ ( self : List[str] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None , __lowerCAmelCase : bool = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCAmelCase , token_ids_a=__lowerCAmelCase , already_has_special_tokens=__lowerCAmelCase ) A__ = [1] * len(self.prefix_tokens ) A__ = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(__lowerCAmelCase )) + suffix_ones return prefix_ones + ([0] * len(__lowerCAmelCase )) + ([0] * len(__lowerCAmelCase )) + suffix_ones def a_ ( self : Tuple , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def a_ ( self : int ) -> Dict: """simple docstring""" A__ = {self.convert_ids_to_tokens(__lowerCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.__dict__.copy() A__ = None return state def __setstate__( self : str , __lowerCAmelCase : Dict ) -> None: """simple docstring""" A__ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): A__ = {} A__ = load_spm(self.spm_file , self.sp_model_kwargs ) def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" A__ = Path(__lowerCAmelCase ) if not save_dir.is_dir(): raise OSError(f'{save_directory} should be a directory' ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , __lowerCAmelCase ) if os.path.abspath(self.spm_file ) != os.path.abspath(__lowerCAmelCase ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , __lowerCAmelCase ) elif not os.path.isfile(self.spm_file ): with open(__lowerCAmelCase , """wb""" ) as fi: A__ = self.sp_model.serialized_model_proto() fi.write(__lowerCAmelCase ) return (str(__lowerCAmelCase ), str(__lowerCAmelCase )) def a_ ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : str = "en" , __lowerCAmelCase : Optional[List[str]] = None , __lowerCAmelCase : str = "ro" , **__lowerCAmelCase : List[Any] , ) -> BatchEncoding: """simple docstring""" A__ = src_lang A__ = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(__lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[str] , __lowerCAmelCase : Optional[str] , **__lowerCAmelCase : Tuple ) -> Tuple: """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""" ) A__ = src_lang A__ = self(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , **__lowerCAmelCase ) A__ = self.get_lang_id(__lowerCAmelCase ) A__ = tgt_lang_id return inputs def a_ ( self : Dict ) -> int: """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def a_ ( self : str , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Tuple , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> str: """simple docstring""" return self.lang_code_to_token[lang] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> int: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) return self.lang_token_to_id[lang_token] def __lowerCamelCase ( __a :str , __a :Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: """simple docstring""" A__ = sentencepiece.SentencePieceProcessor(**__a ) spm.Load(str(__a ) ) return spm def __lowerCamelCase ( __a :str ) -> Union[Dict, List]: """simple docstring""" with open(__a , """r""" ) as f: return json.load(__a ) def __lowerCamelCase ( __a :List[Any] , __a :str ) -> None: """simple docstring""" with open(__a , """w""" ) as f: json.dump(__a , __a , indent=2 )
274
1
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) A : str = '''▁''' A : Any = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', '''tokenizer_config_file''': '''tokenizer_config.json''', } A : List[Any] = { '''vocab_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json''', }, '''spm_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_config_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json''', }, } A : Tuple = { '''facebook/m2m100_418M''': 1_0_2_4, } # fmt: off A : Optional[int] = { '''m2m100''': ['''af''', '''am''', '''ar''', '''ast''', '''az''', '''ba''', '''be''', '''bg''', '''bn''', '''br''', '''bs''', '''ca''', '''ceb''', '''cs''', '''cy''', '''da''', '''de''', '''el''', '''en''', '''es''', '''et''', '''fa''', '''ff''', '''fi''', '''fr''', '''fy''', '''ga''', '''gd''', '''gl''', '''gu''', '''ha''', '''he''', '''hi''', '''hr''', '''ht''', '''hu''', '''hy''', '''id''', '''ig''', '''ilo''', '''is''', '''it''', '''ja''', '''jv''', '''ka''', '''kk''', '''km''', '''kn''', '''ko''', '''lb''', '''lg''', '''ln''', '''lo''', '''lt''', '''lv''', '''mg''', '''mk''', '''ml''', '''mn''', '''mr''', '''ms''', '''my''', '''ne''', '''nl''', '''no''', '''ns''', '''oc''', '''or''', '''pa''', '''pl''', '''ps''', '''pt''', '''ro''', '''ru''', '''sd''', '''si''', '''sk''', '''sl''', '''so''', '''sq''', '''sr''', '''ss''', '''su''', '''sv''', '''sw''', '''ta''', '''th''', '''tl''', '''tn''', '''tr''', '''uk''', '''ur''', '''uz''', '''vi''', '''wo''', '''xh''', '''yi''', '''yo''', '''zh''', '''zu'''], '''wmt21''': ['''en''', '''ha''', '''is''', '''ja''', '''cs''', '''ru''', '''zh''', '''de'''] } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = VOCAB_FILES_NAMES __lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = ['''input_ids''', '''attention_mask'''] __lowerCamelCase : List[int] = [] __lowerCamelCase : List[int] = [] def __init__( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any]=None , __lowerCAmelCase : str=None , __lowerCAmelCase : List[Any]="<s>" , __lowerCAmelCase : List[Any]="</s>" , __lowerCAmelCase : Optional[int]="</s>" , __lowerCAmelCase : Optional[Any]="<pad>" , __lowerCAmelCase : Any="<unk>" , __lowerCAmelCase : Any="m2m100" , __lowerCAmelCase : Optional[Dict[str, Any]] = None , __lowerCAmelCase : Dict=8 , **__lowerCAmelCase : Tuple , ) -> None: """simple docstring""" A__ = {} if sp_model_kwargs is None else sp_model_kwargs A__ = language_codes A__ = FAIRSEQ_LANGUAGE_CODES[language_codes] A__ = {lang_code: f'__{lang_code}__' for lang_code in fairseq_language_code} A__ = kwargs.get("""additional_special_tokens""" , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(__lowerCAmelCase ) for lang_code in fairseq_language_code if self.get_lang_token(__lowerCAmelCase ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=__lowerCAmelCase , tgt_lang=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , language_codes=__lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = vocab_file A__ = load_json(__lowerCAmelCase ) A__ = {v: k for k, v in self.encoder.items()} A__ = spm_file A__ = load_spm(__lowerCAmelCase , self.sp_model_kwargs ) A__ = len(self.encoder ) A__ = { self.get_lang_token(__lowerCAmelCase ): self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase ) } A__ = {lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase )} A__ = {v: k for k, v in self.lang_token_to_id.items()} A__ = src_lang if src_lang is not None else """en""" A__ = tgt_lang A__ = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) A__ = num_madeup_words @property def a_ ( self : Optional[int] ) -> int: """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def a_ ( self : Optional[Any] ) -> str: """simple docstring""" return self._src_lang @src_lang.setter def a_ ( self : List[Any] , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def a_ ( self : Optional[int] , __lowerCAmelCase : str ) -> List[str]: """simple docstring""" return self.sp_model.encode(__lowerCAmelCase , out_type=__lowerCAmelCase ) def a_ ( self : Optional[Any] , __lowerCAmelCase : Dict ) -> Optional[Any]: """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(__lowerCAmelCase , self.encoder[self.unk_token] ) def a_ ( self : Optional[int] , __lowerCAmelCase : int ) -> str: """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(__lowerCAmelCase , self.unk_token ) def a_ ( self : Optional[int] , __lowerCAmelCase : Dict ) -> str: """simple docstring""" A__ = [] A__ = """""" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(__lowerCAmelCase ) + token A__ = [] else: current_sub_tokens.append(__lowerCAmelCase ) out_string += self.sp_model.decode(__lowerCAmelCase ) return out_string.strip() def a_ ( self : List[str] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None , __lowerCAmelCase : bool = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCAmelCase , token_ids_a=__lowerCAmelCase , already_has_special_tokens=__lowerCAmelCase ) A__ = [1] * len(self.prefix_tokens ) A__ = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(__lowerCAmelCase )) + suffix_ones return prefix_ones + ([0] * len(__lowerCAmelCase )) + ([0] * len(__lowerCAmelCase )) + suffix_ones def a_ ( self : Tuple , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def a_ ( self : int ) -> Dict: """simple docstring""" A__ = {self.convert_ids_to_tokens(__lowerCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.__dict__.copy() A__ = None return state def __setstate__( self : str , __lowerCAmelCase : Dict ) -> None: """simple docstring""" A__ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): A__ = {} A__ = load_spm(self.spm_file , self.sp_model_kwargs ) def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" A__ = Path(__lowerCAmelCase ) if not save_dir.is_dir(): raise OSError(f'{save_directory} should be a directory' ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , __lowerCAmelCase ) if os.path.abspath(self.spm_file ) != os.path.abspath(__lowerCAmelCase ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , __lowerCAmelCase ) elif not os.path.isfile(self.spm_file ): with open(__lowerCAmelCase , """wb""" ) as fi: A__ = self.sp_model.serialized_model_proto() fi.write(__lowerCAmelCase ) return (str(__lowerCAmelCase ), str(__lowerCAmelCase )) def a_ ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : str = "en" , __lowerCAmelCase : Optional[List[str]] = None , __lowerCAmelCase : str = "ro" , **__lowerCAmelCase : List[Any] , ) -> BatchEncoding: """simple docstring""" A__ = src_lang A__ = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(__lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[str] , __lowerCAmelCase : Optional[str] , **__lowerCAmelCase : Tuple ) -> Tuple: """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""" ) A__ = src_lang A__ = self(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , **__lowerCAmelCase ) A__ = self.get_lang_id(__lowerCAmelCase ) A__ = tgt_lang_id return inputs def a_ ( self : Dict ) -> int: """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def a_ ( self : str , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Tuple , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> str: """simple docstring""" return self.lang_code_to_token[lang] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> int: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) return self.lang_token_to_id[lang_token] def __lowerCamelCase ( __a :str , __a :Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: """simple docstring""" A__ = sentencepiece.SentencePieceProcessor(**__a ) spm.Load(str(__a ) ) return spm def __lowerCamelCase ( __a :str ) -> Union[Dict, List]: """simple docstring""" with open(__a , """r""" ) as f: return json.load(__a ) def __lowerCamelCase ( __a :List[Any] , __a :str ) -> None: """simple docstring""" with open(__a , """w""" ) as f: json.dump(__a , __a , indent=2 )
274
from __future__ import annotations from PIL import Image # Define glider example A : Any = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [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], ] # Define blinker example A : Optional[Any] = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def __lowerCamelCase ( __a :list[list[int]] ) -> list[list[int]]: """simple docstring""" A__ = [] for i in range(len(__a ) ): A__ = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours A__ = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(__a ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(__a ) - 1: neighbour_count += cells[i + 1][j] if i < len(__a ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. A__ = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(__a ) return next_generation def __lowerCamelCase ( __a :list[list[int]] , __a :int ) -> list[Image.Image]: """simple docstring""" A__ = [] for _ in range(__a ): # Create output image A__ = Image.new("""RGB""" , (len(cells[0] ), len(__a )) ) A__ = img.load() # Save cells to image for x in range(len(__a ) ): for y in range(len(cells[0] ) ): A__ = 2_5_5 - cells[y][x] * 2_5_5 A__ = (colour, colour, colour) # Save image images.append(__a ) A__ = new_generation(__a ) return images if __name__ == "__main__": A : str = generate_images(GLIDER, 1_6) images[0].save('''out.gif''', save_all=True, append_images=images[1:])
274
1
import os import tempfile from functools import partial from unittest import TestCase from unittest.mock import patch import datasets import datasets.config from .utils import require_beam class A (datasets.BeamBasedBuilder ): '''simple docstring''' def a_ ( self : str ) -> int: """simple docstring""" return datasets.DatasetInfo( features=datasets.Features({"""content""": datasets.Value("""string""" )} ) , supervised_keys=__lowerCAmelCase , ) def a_ ( self : Tuple , __lowerCAmelCase : Dict , __lowerCAmelCase : int ) -> Dict: """simple docstring""" return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={"""examples""": get_test_dummy_examples()} )] def a_ ( self : List[Any] , __lowerCAmelCase : str , __lowerCAmelCase : Any ) -> str: """simple docstring""" import apache_beam as beam return pipeline | "Load Examples" >> beam.Create(__lowerCAmelCase ) class A (datasets.BeamBasedBuilder ): '''simple docstring''' def a_ ( self : int ) -> Tuple: """simple docstring""" return datasets.DatasetInfo( features=datasets.Features({"""a""": datasets.Sequence({"""b""": datasets.Value("""string""" )} )} ) , supervised_keys=__lowerCAmelCase , ) def a_ ( self : List[str] , __lowerCAmelCase : List[str] , __lowerCAmelCase : List[str] ) -> Tuple: """simple docstring""" return [ datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={"""examples""": get_test_nested_examples()} ) ] def a_ ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : Tuple ) -> Dict: """simple docstring""" import apache_beam as beam return pipeline | "Load Examples" >> beam.Create(__lowerCAmelCase ) def __lowerCamelCase ( ) -> List[str]: """simple docstring""" return [(i, {"content": content}) for i, content in enumerate(["""foo""", """bar""", """foobar"""] )] def __lowerCamelCase ( ) -> Optional[int]: """simple docstring""" return [(i, {"a": {"b": [content]}}) for i, content in enumerate(["""foo""", """bar""", """foobar"""] )] class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @require_beam def a_ ( self : Optional[int] ) -> str: """simple docstring""" A__ = len(get_test_dummy_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: A__ = DummyBeamDataset(cache_dir=__lowerCAmelCase , beam_runner="""DirectRunner""" ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join(__lowerCAmelCase , builder.name , """default""" , """0.0.0""" , f'{builder.name}-train.arrow' ) ) ) self.assertDictEqual(builder.info.features , datasets.Features({"""content""": datasets.Value("""string""" )} ) ) A__ = builder.as_dataset() self.assertEqual(dset["""train"""].num_rows , __lowerCAmelCase ) self.assertEqual(dset["""train"""].info.splits["""train"""].num_examples , __lowerCAmelCase ) self.assertDictEqual(dset["""train"""][0] , get_test_dummy_examples()[0][1] ) self.assertDictEqual( dset["""train"""][expected_num_examples - 1] , get_test_dummy_examples()[expected_num_examples - 1][1] ) self.assertTrue( os.path.exists(os.path.join(__lowerCAmelCase , builder.name , """default""" , """0.0.0""" , """dataset_info.json""" ) ) ) del dset @require_beam def a_ ( self : Any ) -> int: """simple docstring""" import apache_beam as beam A__ = beam.io.parquetio.WriteToParquet A__ = len(get_test_dummy_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: A__ = DummyBeamDataset(cache_dir=__lowerCAmelCase , beam_runner="""DirectRunner""" ) with patch("""apache_beam.io.parquetio.WriteToParquet""" ) as write_parquet_mock: A__ = partial(__lowerCAmelCase , num_shards=2 ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join( __lowerCAmelCase , builder.name , """default""" , """0.0.0""" , f'{builder.name}-train-00000-of-00002.arrow' ) ) ) self.assertTrue( os.path.exists( os.path.join( __lowerCAmelCase , builder.name , """default""" , """0.0.0""" , f'{builder.name}-train-00000-of-00002.arrow' ) ) ) self.assertDictEqual(builder.info.features , datasets.Features({"""content""": datasets.Value("""string""" )} ) ) A__ = builder.as_dataset() self.assertEqual(dset["""train"""].num_rows , __lowerCAmelCase ) self.assertEqual(dset["""train"""].info.splits["""train"""].num_examples , __lowerCAmelCase ) # Order is not preserved when sharding, so we just check that all the elements are there self.assertListEqual(sorted(dset["""train"""]["""content"""] ) , sorted(["""foo""", """bar""", """foobar"""] ) ) self.assertTrue( os.path.exists(os.path.join(__lowerCAmelCase , builder.name , """default""" , """0.0.0""" , """dataset_info.json""" ) ) ) del dset @require_beam def a_ ( self : Dict ) -> Optional[Any]: """simple docstring""" with tempfile.TemporaryDirectory() as tmp_cache_dir: A__ = DummyBeamDataset(cache_dir=__lowerCAmelCase ) self.assertRaises(datasets.builder.MissingBeamOptions , builder.download_and_prepare ) @require_beam def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" A__ = len(get_test_nested_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: A__ = NestedBeamDataset(cache_dir=__lowerCAmelCase , beam_runner="""DirectRunner""" ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join(__lowerCAmelCase , builder.name , """default""" , """0.0.0""" , f'{builder.name}-train.arrow' ) ) ) self.assertDictEqual( builder.info.features , datasets.Features({"""a""": datasets.Sequence({"""b""": datasets.Value("""string""" )} )} ) ) A__ = builder.as_dataset() self.assertEqual(dset["""train"""].num_rows , __lowerCAmelCase ) self.assertEqual(dset["""train"""].info.splits["""train"""].num_examples , __lowerCAmelCase ) self.assertDictEqual(dset["""train"""][0] , get_test_nested_examples()[0][1] ) self.assertDictEqual( dset["""train"""][expected_num_examples - 1] , get_test_nested_examples()[expected_num_examples - 1][1] ) self.assertTrue( os.path.exists(os.path.join(__lowerCAmelCase , builder.name , """default""" , """0.0.0""" , """dataset_info.json""" ) ) ) del dset
274
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": A : List[str] = input('''Enter image url: ''').strip() print(F'''Downloading image from {url} ...''') A : Any = BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image A : List[Any] = soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] A : Dict = requests.get(image_url).content A : Tuple = F'''{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg''' with open(file_name, '''wb''') as fp: fp.write(image_data) print(F'''Done. Image saved to disk as {file_name}.''')
274
1
from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS A : Tuple = logging.get_logger(__name__) A : Dict = { '''linear''': get_linear_schedule_with_warmup, '''cosine''': get_cosine_schedule_with_warmup, '''cosine_w_restarts''': get_cosine_with_hard_restarts_schedule_with_warmup, '''polynomial''': get_polynomial_decay_schedule_with_warmup, '''constant''': get_constant_schedule, '''constant_w_warmup''': get_constant_schedule_with_warmup, } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : str , __lowerCAmelCase : str=None , __lowerCAmelCase : str=None , *__lowerCAmelCase : List[str] , **__lowerCAmelCase : Optional[int] ) -> Tuple: """simple docstring""" super().__init__(*__lowerCAmelCase , **__lowerCAmelCase ) if config is None: assert isinstance(self.model , __lowerCAmelCase ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" f' {self.model.__class__}' ) A__ = self.model.config else: A__ = config A__ = data_args A__ = self.config.tgt_vocab_size if isinstance(self.config , __lowerCAmelCase ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( f'The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for' """ padding..""" ) if self.args.label_smoothing == 0: A__ = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss A__ = label_smoothed_nll_loss def a_ ( self : Optional[int] , __lowerCAmelCase : int ) -> List[Any]: """simple docstring""" if self.optimizer is None: A__ = ["""bias""", """LayerNorm.weight"""] A__ = [ { """params""": [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], """weight_decay""": self.args.weight_decay, }, { """params""": [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], """weight_decay""": 0.0, }, ] A__ = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: A__ = Adafactor A__ = {"""scale_parameter""": False, """relative_step""": False} else: A__ = AdamW A__ = { """betas""": (self.args.adam_betaa, self.args.adam_betaa), """eps""": self.args.adam_epsilon, } A__ = self.args.learning_rate if self.sharded_ddp: A__ = OSS( params=__lowerCAmelCase , optim=__lowerCAmelCase , **__lowerCAmelCase , ) else: A__ = optimizer_cls(__lowerCAmelCase , **__lowerCAmelCase ) if self.lr_scheduler is None: A__ = self._get_lr_scheduler(__lowerCAmelCase ) else: # ignoring --lr_scheduler logger.warning("""scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.""" ) def a_ ( self : Dict , __lowerCAmelCase : str ) -> Tuple: """simple docstring""" A__ = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": A__ = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": A__ = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: A__ = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=__lowerCAmelCase ) return scheduler def a_ ( self : int ) -> Optional[torch.utils.data.Sampler]: """simple docstring""" if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : Optional[Any] ) -> Optional[Any]: """simple docstring""" if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token A__ = model(**__lowerCAmelCase , use_cache=__lowerCAmelCase )[0] A__ = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models A__ , A__ = model(**__lowerCAmelCase , labels=__lowerCAmelCase , use_cache=__lowerCAmelCase )[:2] else: # compute label smoothed loss A__ = model(**__lowerCAmelCase , use_cache=__lowerCAmelCase )[0] A__ = torch.nn.functional.log_softmax(__lowerCAmelCase , dim=-1 ) A__ , A__ = self.loss_fn(__lowerCAmelCase , __lowerCAmelCase , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def a_ ( self : Tuple , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : str ) -> List[Any]: """simple docstring""" A__ = inputs.pop("""labels""" ) A__ , A__ = self._compute_loss(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) return loss def a_ ( self : int , __lowerCAmelCase : nn.Module , __lowerCAmelCase : Dict[str, Union[torch.Tensor, Any]] , __lowerCAmelCase : bool , __lowerCAmelCase : Optional[List[str]] = None , ) -> Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: """simple docstring""" A__ = self._prepare_inputs(__lowerCAmelCase ) A__ = { """max_length""": self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, """num_beams""": self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: A__ = self.model.generate( inputs["""input_ids"""] , attention_mask=inputs["""attention_mask"""] , **__lowerCAmelCase , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: A__ = self._pad_tensors_to_max_len(__lowerCAmelCase , gen_kwargs["""max_length"""] ) A__ = inputs.pop("""labels""" ) with torch.no_grad(): # compute loss on predict data A__ , A__ = self._compute_loss(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) A__ = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: A__ = self._pad_tensors_to_max_len(__lowerCAmelCase , gen_kwargs["""max_length"""] ) return (loss, logits, labels) def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Optional[Any] ) -> List[str]: """simple docstring""" A__ = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( """Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be""" f' padded to `max_length`={max_length}' ) A__ = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) A__ = tensor return padded_tensor
274
# This code is adapted from OpenAI's release # https://github.com/openai/human-eval/blob/master/human_eval/execution.py import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def __lowerCamelCase ( __a :List[str] , __a :List[Any] , __a :Union[str, Any] , __a :List[Any] ) -> Dict: """simple docstring""" A__ = multiprocessing.Manager() A__ = manager.list() A__ = multiprocessing.Process(target=__a , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append("""timed out""" ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def __lowerCamelCase ( __a :Optional[Any] , __a :Any , __a :List[Any] ) -> Union[str, Any]: """simple docstring""" with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil A__ = shutil.rmtree A__ = os.rmdir A__ = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: A__ = {} with swallow_io(): with time_limit(__a ): exec(__a , __a ) result.append("""passed""" ) except TimeoutException: result.append("""timed out""" ) except BaseException as e: result.append(F'failed: {e}' ) # Needed for cleaning up. A__ = rmtree A__ = rmdir A__ = chdir @contextlib.contextmanager def __lowerCamelCase ( __a :List[str] ) -> Dict: """simple docstring""" def signal_handler(__a :List[Any] , __a :Optional[Any] ): raise TimeoutException("""Timed out!""" ) signal.setitimer(signal.ITIMER_REAL , __a ) signal.signal(signal.SIGALRM , __a ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = WriteOnlyStringIO() with contextlib.redirect_stdout(__a ): with contextlib.redirect_stderr(__a ): with redirect_stdin(__a ): yield @contextlib.contextmanager def __lowerCamelCase ( ) -> Dict: """simple docstring""" with tempfile.TemporaryDirectory() as dirname: with chdir(__a ): yield dirname class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (io.StringIO ): '''simple docstring''' def a_ ( self : Any , *__lowerCAmelCase : List[str] , **__lowerCAmelCase : str ) -> Dict: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Optional[int] ) -> str: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Any ) -> int: """simple docstring""" raise OSError def a_ ( self : str , *__lowerCAmelCase : Any , **__lowerCAmelCase : Union[str, Any] ) -> int: """simple docstring""" return False class A (contextlib._RedirectStream ): # type: ignore '''simple docstring''' __lowerCamelCase : Union[str, Any] = '''stdin''' @contextlib.contextmanager def __lowerCamelCase ( __a :Union[str, Any] ) -> List[str]: """simple docstring""" if root == ".": yield return A__ = os.getcwd() os.chdir(__a ) try: yield except BaseException as exc: raise exc finally: os.chdir(__a ) def __lowerCamelCase ( __a :Union[str, Any]=None ) -> Dict: """simple docstring""" if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins A__ = None A__ = None import os A__ = """1""" A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None import shutil A__ = None A__ = None A__ = None import subprocess A__ = None # type: ignore A__ = None import sys A__ = None A__ = None A__ = None A__ = None A__ = None
274
1
import platform from argparse import ArgumentParser import huggingface_hub from .. import __version__ as version from ..utils import is_accelerate_available, is_torch_available, is_transformers_available, is_xformers_available from . import BaseDiffusersCLICommand def __lowerCamelCase ( __a :Optional[Any] ) -> List[Any]: """simple docstring""" return EnvironmentCommand() class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @staticmethod def a_ ( __lowerCAmelCase : ArgumentParser ) -> List[str]: """simple docstring""" A__ = parser.add_parser("""env""" ) download_parser.set_defaults(func=__lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> str: """simple docstring""" A__ = huggingface_hub.__version__ A__ = """not installed""" A__ = """NA""" if is_torch_available(): import torch A__ = torch.__version__ A__ = torch.cuda.is_available() A__ = """not installed""" if is_transformers_available(): import transformers A__ = transformers.__version__ A__ = """not installed""" if is_accelerate_available(): import accelerate A__ = accelerate.__version__ A__ = """not installed""" if is_xformers_available(): import xformers A__ = xformers.__version__ A__ = { """`diffusers` version""": version, """Platform""": platform.platform(), """Python version""": platform.python_version(), """PyTorch version (GPU?)""": f'{pt_version} ({pt_cuda_available})', """Huggingface_hub version""": hub_version, """Transformers version""": transformers_version, """Accelerate version""": accelerate_version, """xFormers version""": xformers_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(__lowerCAmelCase ) ) return info @staticmethod def a_ ( __lowerCAmelCase : Dict ) -> List[Any]: """simple docstring""" return "\n".join([f'- {prop}: {val}' for prop, val in d.items()] ) + "\n"
274
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_albert import AlbertTokenizer else: A : Tuple = None A : Optional[Any] = logging.get_logger(__name__) A : Tuple = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A : List[str] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json''', }, } A : List[str] = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } A : Optional[int] = '''▁''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : str = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = AlbertTokenizer def __init__( self : Tuple , __lowerCAmelCase : Tuple=None , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : Optional[Any]=True , __lowerCAmelCase : List[Any]=True , __lowerCAmelCase : str=False , __lowerCAmelCase : Union[str, Any]="[CLS]" , __lowerCAmelCase : int="[SEP]" , __lowerCAmelCase : Dict="<unk>" , __lowerCAmelCase : Dict="[SEP]" , __lowerCAmelCase : Union[str, Any]="<pad>" , __lowerCAmelCase : str="[CLS]" , __lowerCAmelCase : int="[MASK]" , **__lowerCAmelCase : Optional[Any] , ) -> Optional[Any]: """simple docstring""" A__ = ( AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase , normalized=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token ) super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def a_ ( self : List[Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a_ ( self : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(__lowerCAmelCase ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return A__ = os.path.join( __lowerCAmelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCAmelCase ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
274
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A : str = { '''configuration_clipseg''': [ '''CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CLIPSegConfig''', '''CLIPSegTextConfig''', '''CLIPSegVisionConfig''', ], '''processing_clipseg''': ['''CLIPSegProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : str = [ '''CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST''', '''CLIPSegModel''', '''CLIPSegPreTrainedModel''', '''CLIPSegTextModel''', '''CLIPSegVisionModel''', '''CLIPSegForImageSegmentation''', ] if TYPE_CHECKING: from .configuration_clipseg import ( CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPSegConfig, CLIPSegTextConfig, CLIPSegVisionConfig, ) from .processing_clipseg import CLIPSegProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clipseg import ( CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPSegForImageSegmentation, CLIPSegModel, CLIPSegPreTrainedModel, CLIPSegTextModel, CLIPSegVisionModel, ) else: import sys A : List[Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
274
import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging A : Dict = logging.get_logger(__name__) def __lowerCamelCase ( __a :int=None , __a :Optional[Any]=None ) -> int: """simple docstring""" return field(default_factory=lambda: default , metadata=__a ) @dataclass class A : '''simple docstring''' __lowerCamelCase : List[str] = list_field( default=[] , metadata={ '''help''': ( '''Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version''' ''' of all available models''' ) } , ) __lowerCamelCase : List[int] = list_field( default=[8] , metadata={'''help''': '''List of batch sizes for which memory and time performance will be evaluated'''} ) __lowerCamelCase : List[int] = list_field( default=[8, 32, 128, 512] , metadata={'''help''': '''List of sequence lengths for which memory and time performance will be evaluated'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to benchmark inference of model. Inference can be disabled via --no-inference.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available cuda devices. Cuda can be disabled via --no-cuda.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available tpu devices. TPU can be disabled via --no-tpu.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Use FP16 to accelerate inference.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Benchmark training of model'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Verbose memory tracing'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to perform speed measurements. Speed measurements can be disabled via --no-speed.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': '''Whether to perform memory measurements. Memory measurements can be disabled via --no-memory''' } , ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Trace memory line by line'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save result to a CSV file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save all print statements in a log file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to print environment information'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use''' ''' multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled''' ''' for debugging / testing and on TPU.''' ) } , ) __lowerCamelCase : str = field( default=F'''inference_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''train_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''train_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''env_info_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving environment information.'''} , ) __lowerCamelCase : str = field( default=F'''log_{round(time() )}.csv''' , metadata={'''help''': '''Log filename used if print statements are saved in log.'''} , ) __lowerCamelCase : int = field(default=3 , metadata={'''help''': '''Times an experiment will be run.'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain''' ''' model weights.''' ) } , ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" warnings.warn( f'The class {self.__class__} is deprecated. Hugging Face Benchmarking utils' """ are deprecated in general and it is advised to use external Benchmarking libraries """ """ to benchmark Transformer models.""" , __lowerCAmelCase , ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def a_ ( self : Tuple ) -> List[str]: """simple docstring""" if len(self.models ) <= 0: raise ValueError( """Please make sure you provide at least one model name / model identifier, *e.g.* `--models""" """ bert-base-cased` or `args.models = ['bert-base-cased'].""" ) return self.models @property def a_ ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" if not self.multi_process: return False elif self.is_tpu: logger.info("""Multiprocessing is currently not possible on TPU.""" ) return False else: return True
274
1
import collections import inspect import unittest from typing import Dict, List, Tuple from transformers import MaskFormerSwinConfig from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device from transformers.utils import is_torch_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import MaskFormerSwinBackbone from transformers.models.maskformer import MaskFormerSwinModel class A : '''simple docstring''' def __init__( self : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Optional[Any]=13 , __lowerCAmelCase : Tuple=32 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Optional[Any]=3 , __lowerCAmelCase : List[Any]=16 , __lowerCAmelCase : Optional[Any]=[1, 2, 1] , __lowerCAmelCase : List[Any]=[2, 2, 4] , __lowerCAmelCase : Any=2 , __lowerCAmelCase : int=2.0 , __lowerCAmelCase : List[Any]=True , __lowerCAmelCase : List[str]=0.0 , __lowerCAmelCase : Tuple=0.0 , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : Optional[int]="gelu" , __lowerCAmelCase : Tuple=False , __lowerCAmelCase : Optional[int]=True , __lowerCAmelCase : Optional[Any]=0.0_2 , __lowerCAmelCase : List[str]=1e-5 , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : Union[str, Any]=None , __lowerCAmelCase : str=True , __lowerCAmelCase : str=10 , __lowerCAmelCase : Union[str, Any]=8 , __lowerCAmelCase : List[Any]=["stage1", "stage2", "stage3"] , __lowerCAmelCase : List[str]=[1, 2, 3] , ) -> int: """simple docstring""" A__ = parent A__ = batch_size A__ = image_size A__ = patch_size A__ = num_channels A__ = embed_dim A__ = depths A__ = num_heads A__ = window_size A__ = mlp_ratio A__ = qkv_bias A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = drop_path_rate A__ = hidden_act A__ = use_absolute_embeddings A__ = patch_norm A__ = layer_norm_eps A__ = initializer_range A__ = is_training A__ = scope A__ = use_labels A__ = type_sequence_label_size A__ = encoder_stride A__ = out_features A__ = out_indices def a_ ( self : str ) -> List[Any]: """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ = self.get_config() return config, pixel_values, labels def a_ ( self : Optional[int] ) -> Any: """simple docstring""" return MaskFormerSwinConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , out_features=self.out_features , out_indices=self.out_indices , ) def a_ ( self : Tuple , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Dict , __lowerCAmelCase : List[str] ) -> List[str]: """simple docstring""" A__ = MaskFormerSwinModel(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase ) A__ = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) A__ = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def a_ ( self : List[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Optional[Any] ) -> Any: """simple docstring""" A__ = MaskFormerSwinBackbone(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [13, 16, 16, 16] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , [16, 32, 64] ) # verify ValueError with self.parent.assertRaises(__lowerCAmelCase ): A__ = ["""stem"""] A__ = MaskFormerSwinBackbone(config=__lowerCAmelCase ) def a_ ( self : Any ) -> List[str]: """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class A (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : List[Any] = ( ( MaskFormerSwinModel, MaskFormerSwinBackbone, ) if is_torch_available() else () ) __lowerCamelCase : Tuple = {'''feature-extraction''': MaskFormerSwinModel} if is_torch_available() else {} __lowerCamelCase : Optional[int] = False __lowerCamelCase : List[str] = False __lowerCamelCase : Optional[Any] = False __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Tuple = False def a_ ( self : Any ) -> Any: """simple docstring""" A__ = MaskFormerSwinModelTester(self ) A__ = ConfigTester(self , config_class=__lowerCAmelCase , embed_dim=37 ) @require_torch_multi_gpu @unittest.skip( reason=( """`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn't work well with""" """ `nn.DataParallel`""" ) ) def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" pass def a_ ( self : Dict ) -> Optional[Any]: """simple docstring""" self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def a_ ( self : int ) -> Optional[Any]: """simple docstring""" return def a_ ( self : int ) -> str: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCAmelCase ) def a_ ( self : Optional[int] ) -> Any: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*__lowerCAmelCase ) @unittest.skip("""Swin does not use inputs_embeds""" ) def a_ ( self : List[Any] ) -> List[str]: """simple docstring""" pass @unittest.skip("""Swin does not support feedforward chunking""" ) def a_ ( self : Dict ) -> List[Any]: """simple docstring""" pass def a_ ( self : List[Any] ) -> Optional[Any]: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(__lowerCAmelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__lowerCAmelCase , nn.Linear ) ) def a_ ( self : Dict ) -> Any: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(__lowerCAmelCase ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , __lowerCAmelCase ) @unittest.skip(reason="""MaskFormerSwin is only used as backbone and doesn't support output_attentions""" ) def a_ ( self : int ) -> Union[str, Any]: """simple docstring""" pass @unittest.skip(reason="""MaskFormerSwin is only used as an internal backbone""" ) def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" pass def a_ ( self : List[str] , __lowerCAmelCase : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any] ) -> Tuple: """simple docstring""" A__ = model_class(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) ) A__ = outputs.hidden_states A__ = getattr( self.model_tester , """expected_num_hidden_layers""" , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(__lowerCAmelCase ) , __lowerCAmelCase ) # Swin has a different seq_length A__ = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) A__ = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def a_ ( self : List[str] ) -> Any: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: A__ = True self.check_hidden_states_output(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ = True self.check_hidden_states_output(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = 3 A__ = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) A__ = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) A__ = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) A__ = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: A__ = True self.check_hidden_states_output(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ = True self.check_hidden_states_output(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , (padded_height, padded_width) ) @unittest.skip(reason="""MaskFormerSwin doesn't have pretrained checkpoints""" ) def a_ ( self : str ) -> Optional[int]: """simple docstring""" pass @unittest.skip(reason="""This will be fixed once MaskFormerSwin is replaced by native Swin""" ) def a_ ( self : List[Any] ) -> Union[str, Any]: """simple docstring""" pass @unittest.skip(reason="""This will be fixed once MaskFormerSwin is replaced by native Swin""" ) def a_ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" pass def a_ ( self : Tuple ) -> Dict: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() def set_nan_tensor_to_zero(__lowerCAmelCase : str ): A__ = 0 return t def check_equivalence(__lowerCAmelCase : Any , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : str={} ): with torch.no_grad(): A__ = model(**__lowerCAmelCase , return_dict=__lowerCAmelCase , **__lowerCAmelCase ) A__ = model(**__lowerCAmelCase , return_dict=__lowerCAmelCase , **__lowerCAmelCase ).to_tuple() def recursive_check(__lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : int ): if isinstance(__lowerCAmelCase , (List, Tuple) ): for tuple_iterable_value, dict_iterable_value in zip(__lowerCAmelCase , __lowerCAmelCase ): recursive_check(__lowerCAmelCase , __lowerCAmelCase ) elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): for tuple_iterable_value, dict_iterable_value in zip( tuple_object.values() , dict_object.values() ): recursive_check(__lowerCAmelCase , __lowerCAmelCase ) elif tuple_object is None: return else: self.assertTrue( torch.allclose( set_nan_tensor_to_zero(__lowerCAmelCase ) , set_nan_tensor_to_zero(__lowerCAmelCase ) , atol=1e-5 ) , msg=( """Tuple and dict output are not equal. Difference:""" f' {torch.max(torch.abs(tuple_object - dict_object ) )}. Tuple has `nan`:' f' {torch.isnan(__lowerCAmelCase ).any()} and `inf`: {torch.isinf(__lowerCAmelCase )}. Dict has' f' `nan`: {torch.isnan(__lowerCAmelCase ).any()} and `inf`: {torch.isinf(__lowerCAmelCase )}.' ) , ) recursive_check(__lowerCAmelCase , __lowerCAmelCase ) for model_class in self.all_model_classes: A__ = model_class(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) A__ = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) check_equivalence(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase , return_labels=__lowerCAmelCase ) A__ = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase , return_labels=__lowerCAmelCase ) check_equivalence(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) A__ = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) check_equivalence(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , {"""output_hidden_states""": True} ) A__ = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase , return_labels=__lowerCAmelCase ) A__ = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase , return_labels=__lowerCAmelCase ) check_equivalence(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , {"""output_hidden_states""": True} ) @require_torch class A (unittest.TestCase , SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Tuple = (MaskFormerSwinBackbone,) if is_torch_available() else () __lowerCamelCase : Optional[Any] = MaskFormerSwinConfig def a_ ( self : Union[str, Any] ) -> Optional[int]: """simple docstring""" A__ = MaskFormerSwinModelTester(self ) def a_ ( self : Dict ) -> int: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = inputs_dict["""pixel_values"""].shape[0] for backbone_class in self.all_model_classes: A__ = backbone_class(__lowerCAmelCase ) backbone.to(__lowerCAmelCase ) backbone.eval() A__ = backbone(**__lowerCAmelCase ) # Test default outputs and verify feature maps self.assertIsInstance(outputs.feature_maps , __lowerCAmelCase ) self.assertTrue(len(outputs.feature_maps ) == len(backbone.channels ) ) for feature_map, n_channels in zip(outputs.feature_maps , backbone.channels ): self.assertTrue(feature_map.shape[:2] , (batch_size, n_channels) ) self.assertIsNone(outputs.hidden_states ) self.assertIsNone(outputs.attentions ) # Test output_hidden_states=True A__ = backbone(**__lowerCAmelCase , output_hidden_states=__lowerCAmelCase ) self.assertIsNotNone(outputs.hidden_states ) self.assertTrue(len(outputs.hidden_states ) , len(backbone.stage_names ) ) # We skip the stem layer for hidden_states, n_channels in zip(outputs.hidden_states[1:] , backbone.channels ): for hidden_state in hidden_states: # Hidden states are in the format (batch_size, (height * width), n_channels) A__ , A__ , A__ = hidden_state.shape self.assertTrue((h_batch_size, h_n_channels) , (batch_size, n_channels) ) # Test output_attentions=True if self.has_attentions: A__ = backbone(**__lowerCAmelCase , output_attentions=__lowerCAmelCase ) self.assertIsNotNone(outputs.attentions )
274
from math import ceil def __lowerCamelCase ( __a :int = 1_0_0_1 ) -> int: """simple docstring""" A__ = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): A__ = 2 * i + 1 A__ = 2 * i A__ = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: A : List[str] = int(sys.argv[1]) print(solution(n)) except ValueError: print('''Invalid entry - please enter a number''')
274
1
import math import tensorflow as tf from packaging import version def __lowerCamelCase ( __a :str ) -> Union[str, Any]: """simple docstring""" A__ = tf.convert_to_tensor(__a ) A__ = 0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0 ) , x.dtype ) )) return x * cdf def __lowerCamelCase ( __a :Dict ) -> int: """simple docstring""" A__ = tf.convert_to_tensor(__a ) A__ = tf.cast(math.pi , x.dtype ) A__ = tf.cast(0.044715 , x.dtype ) A__ = 0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi ) * (x + coeff * tf.pow(__a , 3 )) )) return x * cdf def __lowerCamelCase ( __a :Tuple ) -> Tuple: """simple docstring""" A__ = tf.convert_to_tensor(__a ) return x * tf.tanh(tf.math.softplus(__a ) ) def __lowerCamelCase ( __a :List[Any] ) -> Optional[Any]: """simple docstring""" A__ = tf.convert_to_tensor(__a ) A__ = tf.cast(0.044715 , x.dtype ) A__ = tf.cast(0.7978845608 , x.dtype ) return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x) )) def __lowerCamelCase ( __a :Tuple ) -> Optional[int]: """simple docstring""" A__ = tf.convert_to_tensor(__a ) A__ = tf.cast(1.702 , x.dtype ) return x * tf.math.sigmoid(coeff * x ) def __lowerCamelCase ( __a :Tuple ) -> List[Any]: """simple docstring""" return tf.clip_by_value(_gelu(__a ) , -1_0 , 1_0 ) def __lowerCamelCase ( __a :Union[str, Any] , __a :Dict=-1 ) -> List[Any]: """simple docstring""" A__ , A__ = tf.split(__a , 2 , axis=__a ) return a * tf.math.sigmoid(__a ) if version.parse(tf.version.VERSION) >= version.parse('''2.4'''): def __lowerCamelCase ( __a :Any ) -> List[str]: """simple docstring""" return tf.keras.activations.gelu(__a , approximate=__a ) A : Union[str, Any] = tf.keras.activations.gelu A : Union[str, Any] = approximate_gelu_wrap else: A : Tuple = _gelu A : str = _gelu_new A : Optional[int] = { '''gelu''': gelu, '''gelu_10''': gelu_aa, '''gelu_fast''': gelu_fast, '''gelu_new''': gelu_new, '''glu''': glu, '''mish''': mish, '''quick_gelu''': quick_gelu, '''relu''': tf.keras.activations.relu, '''sigmoid''': tf.keras.activations.sigmoid, '''silu''': tf.keras.activations.swish, '''swish''': tf.keras.activations.swish, '''tanh''': tf.keras.activations.tanh, } def __lowerCamelCase ( __a :Any ) -> Optional[int]: """simple docstring""" if activation_string in ACTaFN: return ACTaFN[activation_string] else: raise KeyError(F'function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys() )}' )
274
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A : Tuple = logging.getLogger(__name__) def __lowerCamelCase ( __a :Optional[int] , __a :List[str] ) -> Tuple: """simple docstring""" A__ = np.argmax(__a , axis=1 ) return np.sum(outputs == labels ) def __lowerCamelCase ( __a :Tuple ) -> Dict: """simple docstring""" with open(__a , encoding="""utf_8""" ) as f: A__ = csv.reader(__a ) A__ = [] next(__a ) # skip the first line for line in tqdm(__a ): output.append((""" """.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def __lowerCamelCase ( __a :Optional[int] , __a :List[Any] , __a :Dict , __a :Optional[Any] , __a :Optional[Any] , __a :int ) -> Union[str, Any]: """simple docstring""" A__ = [] for dataset in encoded_datasets: A__ = len(__a ) A__ = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) A__ = np.zeros((n_batch, 2) , dtype=np.intaa ) A__ = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) A__ = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(__a ): A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = with_conta A__ = with_conta A__ = len(__a ) - 1 A__ = len(__a ) - 1 A__ = with_conta A__ = with_conta A__ = mc_label A__ = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(__a ) for t in all_inputs ) ) return tensor_datasets def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = argparse.ArgumentParser() parser.add_argument("""--model_name""" , type=__a , default="""openai-gpt""" , help="""pretrained model name""" ) parser.add_argument("""--do_train""" , action="""store_true""" , help="""Whether to run training.""" ) parser.add_argument("""--do_eval""" , action="""store_true""" , help="""Whether to run eval on the dev set.""" ) parser.add_argument( """--output_dir""" , default=__a , type=__a , required=__a , help="""The output directory where the model predictions and checkpoints will be written.""" , ) parser.add_argument("""--train_dataset""" , type=__a , default="""""" ) parser.add_argument("""--eval_dataset""" , type=__a , default="""""" ) parser.add_argument("""--seed""" , type=__a , default=4_2 ) parser.add_argument("""--num_train_epochs""" , type=__a , default=3 ) parser.add_argument("""--train_batch_size""" , type=__a , default=8 ) parser.add_argument("""--eval_batch_size""" , type=__a , default=1_6 ) parser.add_argument("""--adam_epsilon""" , default=1E-8 , type=__a , help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--max_grad_norm""" , type=__a , default=1 ) parser.add_argument( """--max_steps""" , default=-1 , type=__a , help=( """If > 0: set total number of training steps to perform. Override num_train_epochs.""" ) , ) parser.add_argument( """--gradient_accumulation_steps""" , type=__a , default=1 , help="""Number of updates steps to accumulate before performing a backward/update pass.""" , ) parser.add_argument("""--learning_rate""" , type=__a , default=6.25E-5 ) parser.add_argument("""--warmup_steps""" , default=0 , type=__a , help="""Linear warmup over warmup_steps.""" ) parser.add_argument("""--lr_schedule""" , type=__a , default="""warmup_linear""" ) parser.add_argument("""--weight_decay""" , type=__a , default=0.01 ) parser.add_argument("""--lm_coef""" , type=__a , default=0.9 ) parser.add_argument("""--n_valid""" , type=__a , default=3_7_4 ) parser.add_argument("""--server_ip""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) parser.add_argument("""--server_port""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) A__ = parser.parse_args() print(__a ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("""Waiting for debugger attach""" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__a ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) A__ = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) A__ = torch.cuda.device_count() logger.info("""device: {}, n_gpu {}""".format(__a , __a ) ) if not args.do_train and not args.do_eval: raise ValueError("""At least one of `do_train` or `do_eval` must be True.""" ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset A__ = ["""_start_""", """_delimiter_""", """_classify_"""] A__ = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(__a ) A__ = tokenizer.convert_tokens_to_ids(__a ) A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(__a ) ) model.to(__a ) # Load and encode the datasets def tokenize_and_encode(__a :Tuple ): if isinstance(__a , __a ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(__a ) ) elif isinstance(__a , __a ): return obj return [tokenize_and_encode(__a ) for o in obj] logger.info("""Encoding dataset...""" ) A__ = load_rocstories_dataset(args.train_dataset ) A__ = load_rocstories_dataset(args.eval_dataset ) A__ = (train_dataset, eval_dataset) A__ = tokenize_and_encode(__a ) # Compute the max input length for the Transformer A__ = model.config.n_positions // 2 - 2 A__ = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) A__ = min(__a , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders A__ = pre_process_datasets(__a , __a , __a , *__a ) A__ , A__ = tensor_datasets[0], tensor_datasets[1] A__ = TensorDataset(*__a ) A__ = RandomSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.train_batch_size ) A__ = TensorDataset(*__a ) A__ = SequentialSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: A__ = args.max_steps A__ = args.max_steps // (len(__a ) // args.gradient_accumulation_steps) + 1 else: A__ = len(__a ) // args.gradient_accumulation_steps * args.num_train_epochs A__ = list(model.named_parameters() ) A__ = ["""bias""", """LayerNorm.bias""", """LayerNorm.weight"""] A__ = [ { """params""": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], """weight_decay""": args.weight_decay, }, {"""params""": [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], """weight_decay""": 0.0}, ] A__ = AdamW(__a , lr=args.learning_rate , eps=args.adam_epsilon ) A__ = get_linear_schedule_with_warmup( __a , num_warmup_steps=args.warmup_steps , num_training_steps=__a ) if args.do_train: A__ , A__ , A__ = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="""Epoch""" ): A__ = 0 A__ = 0 A__ = tqdm(__a , desc="""Training""" ) for step, batch in enumerate(__a ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch A__ = model(__a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() A__ = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 A__ = """Training loss: {:.2e} lr: {:.2e}""".format(__a , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer A__ = model.module if hasattr(__a , """module""" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` A__ = os.path.join(args.output_dir , __a ) A__ = os.path.join(args.output_dir , __a ) torch.save(model_to_save.state_dict() , __a ) model_to_save.config.to_json_file(__a ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) A__ = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(__a ) if args.do_eval: model.eval() A__ , A__ = 0, 0 A__ , A__ = 0, 0 for batch in tqdm(__a , desc="""Evaluating""" ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch with torch.no_grad(): A__ , A__ , A__ , A__ = model( __a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = mc_logits.detach().cpu().numpy() A__ = mc_labels.to("""cpu""" ).numpy() A__ = accuracy(__a , __a ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 A__ = eval_loss / nb_eval_steps A__ = eval_accuracy / nb_eval_examples A__ = tr_loss / nb_tr_steps if args.do_train else None A__ = {"""eval_loss""": eval_loss, """eval_accuracy""": eval_accuracy, """train_loss""": train_loss} A__ = os.path.join(args.output_dir , """eval_results.txt""" ) with open(__a , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key in sorted(result.keys() ): logger.info(""" %s = %s""" , __a , str(result[key] ) ) writer.write("""%s = %s\n""" % (key, str(result[key] )) ) if __name__ == "__main__": main()
274
1
from math import factorial def __lowerCamelCase ( __a :int , __a :int , __a :float ) -> float: """simple docstring""" if successes > trials: raise ValueError("""successes must be lower or equal to trials""" ) if trials < 0 or successes < 0: raise ValueError("""the function is defined for non-negative integers""" ) if not isinstance(__a , __a ) or not isinstance(__a , __a ): raise ValueError("""the function is defined for non-negative integers""" ) if not 0 < prob < 1: raise ValueError("""prob has to be in range of 1 - 0""" ) A__ = (prob**successes) * ((1 - prob) ** (trials - successes)) # Calculate the binomial coefficient: n! / k!(n-k)! A__ = float(factorial(__a ) ) coefficient /= factorial(__a ) * factorial(trials - successes ) return probability * coefficient if __name__ == "__main__": from doctest import testmod testmod() print('''Probability of 2 successes out of 4 trails''') print('''with probability of 0.75 is:''', end=''' ''') print(binomial_distribution(2, 4, 0.75))
274
import argparse from collections import defaultdict import yaml A : str = '''docs/source/en/_toctree.yml''' def __lowerCamelCase ( __a :str ) -> List[Any]: """simple docstring""" A__ = defaultdict(__a ) A__ = [] A__ = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__a ) A__ = new_doc_list A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__a ) > 1: raise ValueError( F'{duplicate_key} is present several times in the documentation table of content at ' """`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the """ """others.""" ) # Only add this once new_doc.append({"""local""": duplicate_key, """title""": titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) A__ = sorted(__a , key=lambda __a : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__a ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__a ) # Sort return overview_doc def __lowerCamelCase ( __a :Any=False ) -> List[str]: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 A__ = api_doc[scheduler_idx]["""sections"""] A__ = clean_doc_toc(__a ) A__ = False if new_scheduler_doc != scheduler_doc: A__ = True if overwrite: A__ = new_scheduler_doc if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def __lowerCamelCase ( __a :Optional[int]=False ) -> Dict: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 A__ = False A__ = api_doc[pipeline_idx]["""sections"""] A__ = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: A__ = pipeline_doc["""section"""] A__ = clean_doc_toc(__a ) if overwrite: A__ = new_sub_pipeline_doc new_pipeline_docs.append(__a ) # sort overall pipeline doc A__ = clean_doc_toc(__a ) if new_pipeline_docs != pipeline_docs: A__ = True if overwrite: A__ = new_pipeline_docs if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) if __name__ == "__main__": A : Tuple = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') A : Optional[Any] = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
274
1
from queue import PriorityQueue from typing import Any import numpy as np def __lowerCamelCase ( __a :dict , __a :str , __a :set , __a :set , __a :dict , __a :dict , __a :PriorityQueue , __a :dict , __a :float | int , ) -> float | int: """simple docstring""" for nxt, d in graph[v]: if nxt in visited_forward: continue A__ = cst_fwd.get(__a , np.inf ) A__ = cst_fwd[v] + d if new_cost_f < old_cost_f: queue.put((new_cost_f, nxt) ) A__ = new_cost_f A__ = v if nxt in visited_backward: if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance: A__ = cst_fwd[v] + d + cst_bwd[nxt] return shortest_distance def __lowerCamelCase ( __a :str , __a :str , __a :dict , __a :dict ) -> int: """simple docstring""" A__ = -1 A__ = set() A__ = set() A__ = {source: 0} A__ = {destination: 0} A__ = {source: None} A__ = {destination: None} A__ = PriorityQueue() A__ = PriorityQueue() A__ = np.inf queue_forward.put((0, source) ) queue_backward.put((0, destination) ) if source == destination: return 0 while not queue_forward.empty() and not queue_backward.empty(): A__ , A__ = queue_forward.get() visited_forward.add(__a ) A__ , A__ = queue_backward.get() visited_backward.add(__a ) A__ = pass_and_relaxation( __a , __a , __a , __a , __a , __a , __a , __a , __a , ) A__ = pass_and_relaxation( __a , __a , __a , __a , __a , __a , __a , __a , __a , ) if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance: break if shortest_distance != np.inf: A__ = shortest_distance return shortest_path_distance A : Dict = { '''B''': [['''C''', 1]], '''C''': [['''D''', 1]], '''D''': [['''F''', 1]], '''E''': [['''B''', 1], ['''G''', 2]], '''F''': [], '''G''': [['''F''', 1]], } A : Optional[int] = { '''B''': [['''E''', 1]], '''C''': [['''B''', 1]], '''D''': [['''C''', 1]], '''F''': [['''D''', 1], ['''G''', 1]], '''E''': [[None, np.inf]], '''G''': [['''E''', 2]], } if __name__ == "__main__": import doctest doctest.testmod()
274
def __lowerCamelCase ( __a :str ) -> list: """simple docstring""" A__ = [0] * len(__a ) for i in range(1 , len(__a ) ): # use last results for better performance - dynamic programming A__ = prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: A__ = prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 A__ = j return prefix_result def __lowerCamelCase ( __a :str ) -> int: """simple docstring""" return max(prefix_function(__a ) ) if __name__ == "__main__": import doctest doctest.testmod()
274
1
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL A : Any = logging.get_logger(__name__) class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Tuple = ['''pixel_values'''] def __init__( self : int , __lowerCAmelCase : bool = True , __lowerCAmelCase : Dict[str, int] = None , __lowerCAmelCase : float = None , __lowerCAmelCase : PILImageResampling = PILImageResampling.BILINEAR , __lowerCAmelCase : bool = True , __lowerCAmelCase : Union[int, float] = 1 / 2_55 , __lowerCAmelCase : bool = True , __lowerCAmelCase : Optional[Union[float, List[float]]] = None , __lowerCAmelCase : Optional[Union[float, List[float]]] = None , **__lowerCAmelCase : Optional[int] , ) -> None: """simple docstring""" super().__init__(**__lowerCAmelCase ) A__ = size if size is not None else {"""shortest_edge""": 3_84} A__ = get_size_dict(__lowerCAmelCase , default_to_square=__lowerCAmelCase ) A__ = do_resize A__ = size # Default value set here for backwards compatibility where the value in config is None A__ = crop_pct if crop_pct is not None else 2_24 / 2_56 A__ = resample A__ = do_rescale A__ = rescale_factor A__ = do_normalize A__ = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN A__ = image_std if image_std is not None else IMAGENET_STANDARD_STD def a_ ( self : Tuple , __lowerCAmelCase : np.ndarray , __lowerCAmelCase : Dict[str, int] , __lowerCAmelCase : float , __lowerCAmelCase : PILImageResampling = PILImageResampling.BICUBIC , __lowerCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__lowerCAmelCase : int , ) -> np.ndarray: """simple docstring""" A__ = get_size_dict(__lowerCAmelCase , default_to_square=__lowerCAmelCase ) if "shortest_edge" not in size: raise ValueError(f'Size dictionary must contain \'shortest_edge\' key. Got {size.keys()}' ) A__ = size["""shortest_edge"""] if shortest_edge < 3_84: # maintain same ratio, resizing shortest edge to shortest_edge/crop_pct A__ = int(shortest_edge / crop_pct ) A__ = get_resize_output_image_size(__lowerCAmelCase , size=__lowerCAmelCase , default_to_square=__lowerCAmelCase ) A__ = resize(image=__lowerCAmelCase , size=__lowerCAmelCase , resample=__lowerCAmelCase , data_format=__lowerCAmelCase , **__lowerCAmelCase ) # then crop to (shortest_edge, shortest_edge) return center_crop(image=__lowerCAmelCase , size=(shortest_edge, shortest_edge) , data_format=__lowerCAmelCase , **__lowerCAmelCase ) else: # warping (no cropping) when evaluated at 384 or larger return resize( __lowerCAmelCase , size=(shortest_edge, shortest_edge) , resample=__lowerCAmelCase , data_format=__lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : str , __lowerCAmelCase : np.ndarray , __lowerCAmelCase : Union[int, float] , __lowerCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__lowerCAmelCase : List[Any] , ) -> Union[str, Any]: """simple docstring""" return rescale(__lowerCAmelCase , scale=__lowerCAmelCase , data_format=__lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Optional[int] , __lowerCAmelCase : np.ndarray , __lowerCAmelCase : Union[float, List[float]] , __lowerCAmelCase : Union[float, List[float]] , __lowerCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__lowerCAmelCase : Tuple , ) -> np.ndarray: """simple docstring""" return normalize(__lowerCAmelCase , mean=__lowerCAmelCase , std=__lowerCAmelCase , data_format=__lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : int , __lowerCAmelCase : ImageInput , __lowerCAmelCase : bool = None , __lowerCAmelCase : Dict[str, int] = None , __lowerCAmelCase : float = None , __lowerCAmelCase : PILImageResampling = None , __lowerCAmelCase : bool = None , __lowerCAmelCase : float = None , __lowerCAmelCase : bool = None , __lowerCAmelCase : Optional[Union[float, List[float]]] = None , __lowerCAmelCase : Optional[Union[float, List[float]]] = None , __lowerCAmelCase : Optional[Union[str, TensorType]] = None , __lowerCAmelCase : ChannelDimension = ChannelDimension.FIRST , **__lowerCAmelCase : str , ) -> PIL.Image.Image: """simple docstring""" A__ = do_resize if do_resize is not None else self.do_resize A__ = crop_pct if crop_pct is not None else self.crop_pct A__ = resample if resample is not None else self.resample A__ = do_rescale if do_rescale is not None else self.do_rescale A__ = rescale_factor if rescale_factor is not None else self.rescale_factor A__ = do_normalize if do_normalize is not None else self.do_normalize A__ = image_mean if image_mean is not None else self.image_mean A__ = image_std if image_std is not None else self.image_std A__ = size if size is not None else self.size A__ = get_size_dict(__lowerCAmelCase , default_to_square=__lowerCAmelCase ) A__ = make_list_of_images(__lowerCAmelCase ) if not valid_images(__lowerCAmelCase ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None or resample is None: raise ValueError("""Size and resample must be specified if do_resize is True.""" ) if do_resize and size["shortest_edge"] < 3_84 and crop_pct is None: raise ValueError("""crop_pct must be specified if size < 384.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # All transformations expect numpy arrays. A__ = [to_numpy_array(__lowerCAmelCase ) for image in images] if do_resize: A__ = [self.resize(image=__lowerCAmelCase , size=__lowerCAmelCase , crop_pct=__lowerCAmelCase , resample=__lowerCAmelCase ) for image in images] if do_rescale: A__ = [self.rescale(image=__lowerCAmelCase , scale=__lowerCAmelCase ) for image in images] if do_normalize: A__ = [self.normalize(image=__lowerCAmelCase , mean=__lowerCAmelCase , std=__lowerCAmelCase ) for image in images] A__ = [to_channel_dimension_format(__lowerCAmelCase , __lowerCAmelCase ) for image in images] A__ = {"""pixel_values""": images} return BatchFeature(data=__lowerCAmelCase , tensor_type=__lowerCAmelCase )
274
def __lowerCamelCase ( __a :int = 1_0_0_0_0_0_0 ) -> int: """simple docstring""" A__ = limit + 1 A__ = [0] * limit for first_term in range(1 , __a ): for n in range(__a , __a , __a ): A__ = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a A__ = sum(1 for x in frequency[1:limit] if x == 1_0 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
274
1
import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device if is_torch_available(): from transformers import AutoModelForSeqaSeqLM, AutoTokenizer @require_torch @require_sentencepiece @require_tokenizers class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : Dict ) -> Dict: """simple docstring""" A__ = AutoModelForSeqaSeqLM.from_pretrained("""google/mt5-small""" , return_dict=__lowerCAmelCase ).to(__lowerCAmelCase ) A__ = AutoTokenizer.from_pretrained("""google/mt5-small""" ) A__ = tokenizer("""Hello there""" , return_tensors="""pt""" ).input_ids A__ = tokenizer("""Hi I am""" , return_tensors="""pt""" ).input_ids A__ = model(input_ids.to(__lowerCAmelCase ) , labels=labels.to(__lowerCAmelCase ) ).loss A__ = -(labels.shape[-1] * loss.item()) A__ = -8_4.9_1_2_7 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
274
class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A : '''simple docstring''' def __init__( self : List[Any] ) -> str: """simple docstring""" A__ = [ [], [], [], ] def a_ ( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" try: if len(self.queues[priority] ) >= 1_00: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(__lowerCAmelCase ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def a_ ( self : Optional[Any] ) -> int: """simple docstring""" for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self : Tuple ) -> str: """simple docstring""" return "\n".join(f'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class A : '''simple docstring''' def __init__( self : int ) -> str: """simple docstring""" A__ = [] def a_ ( self : int , __lowerCAmelCase : int ) -> None: """simple docstring""" if len(self.queue ) == 1_00: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(__lowerCAmelCase ) def a_ ( self : List[str] ) -> int: """simple docstring""" if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: A__ = min(self.queue ) self.queue.remove(__lowerCAmelCase ) return data def __str__( self : List[Any] ) -> str: """simple docstring""" return str(self.queue ) def __lowerCamelCase ( ) -> Optional[Any]: """simple docstring""" A__ = FixedPriorityQueue() fpq.enqueue(0 , 1_0 ) fpq.enqueue(1 , 7_0 ) fpq.enqueue(0 , 1_0_0 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 6_4 ) fpq.enqueue(0 , 1_2_8 ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __lowerCamelCase ( ) -> int: """simple docstring""" A__ = ElementPriorityQueue() epq.enqueue(1_0 ) epq.enqueue(7_0 ) epq.enqueue(1_0_0 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(6_4 ) epq.enqueue(1_2_8 ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
274
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 A : Union[str, Any] = logging.get_logger(__name__) # pylint: disable=invalid-name class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : str , __lowerCAmelCase : AutoencoderKL , __lowerCAmelCase : CLIPTextModel , __lowerCAmelCase : CLIPTokenizer , __lowerCAmelCase : UNetaDConditionModel , __lowerCAmelCase : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , __lowerCAmelCase : StableDiffusionSafetyChecker , __lowerCAmelCase : CLIPImageProcessor , ) -> int: """simple docstring""" super().__init__() self.register_modules( vae=__lowerCAmelCase , text_encoder=__lowerCAmelCase , tokenizer=__lowerCAmelCase , unet=__lowerCAmelCase , scheduler=__lowerCAmelCase , safety_checker=__lowerCAmelCase , feature_extractor=__lowerCAmelCase , ) def a_ ( self : List[Any] , __lowerCAmelCase : Optional[Union[str, int]] = "auto" ) -> Dict: """simple docstring""" if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory A__ = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(__lowerCAmelCase ) def a_ ( self : Optional[int] ) -> str: """simple docstring""" self.enable_attention_slicing(__lowerCAmelCase ) @torch.no_grad() def __call__( self : List[Any] , __lowerCAmelCase : Union[str, List[str]] , __lowerCAmelCase : int = 5_12 , __lowerCAmelCase : int = 5_12 , __lowerCAmelCase : int = 50 , __lowerCAmelCase : float = 7.5 , __lowerCAmelCase : Optional[Union[str, List[str]]] = None , __lowerCAmelCase : Optional[int] = 1 , __lowerCAmelCase : float = 0.0 , __lowerCAmelCase : Optional[torch.Generator] = None , __lowerCAmelCase : Optional[torch.FloatTensor] = None , __lowerCAmelCase : Optional[str] = "pil" , __lowerCAmelCase : bool = True , __lowerCAmelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , __lowerCAmelCase : int = 1 , __lowerCAmelCase : Optional[torch.FloatTensor] = None , **__lowerCAmelCase : Tuple , ) -> Optional[Any]: """simple docstring""" if isinstance(__lowerCAmelCase , __lowerCAmelCase ): A__ = 1 elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): A__ = len(__lowerCAmelCase ) else: raise ValueError(f'`prompt` has to be of type `str` or `list` but is {type(__lowerCAmelCase )}' ) 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(__lowerCAmelCase , __lowerCAmelCase ) or callback_steps <= 0) ): raise ValueError( f'`callback_steps` has to be a positive integer but is {callback_steps} of type' f' {type(__lowerCAmelCase )}.' ) # get prompt text embeddings A__ = self.tokenizer( __lowerCAmelCase , padding="""max_length""" , max_length=self.tokenizer.model_max_length , return_tensors="""pt""" , ) A__ = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: A__ = 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__ = text_input_ids[:, : self.tokenizer.model_max_length] if text_embeddings is None: A__ = 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__ = text_embeddings.shape A__ = text_embeddings.repeat(1 , __lowerCAmelCase , 1 ) A__ = text_embeddings.view(bs_embed * num_images_per_prompt , __lowerCAmelCase , -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__ = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: A__ = 42 if negative_prompt is None: A__ = [""""""] elif type(__lowerCAmelCase ) is not type(__lowerCAmelCase ): raise TypeError( f'`negative_prompt` should be the same type to `prompt`, but got {type(__lowerCAmelCase )} !=' f' {type(__lowerCAmelCase )}.' ) elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): A__ = [negative_prompt] elif batch_size != len(__lowerCAmelCase ): raise ValueError( f'`negative_prompt`: {negative_prompt} has batch size {len(__lowerCAmelCase )}, but `prompt`:' f' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches' """ the batch size of `prompt`.""" ) else: A__ = negative_prompt A__ = text_input_ids.shape[-1] A__ = self.tokenizer( __lowerCAmelCase , padding="""max_length""" , max_length=__lowerCAmelCase , truncation=__lowerCAmelCase , return_tensors="""pt""" , ) A__ = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method A__ = uncond_embeddings.shape[1] A__ = uncond_embeddings.repeat(__lowerCAmelCase , __lowerCAmelCase , 1 ) A__ = uncond_embeddings.view(batch_size * num_images_per_prompt , __lowerCAmelCase , -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__ = 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__ = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) A__ = (batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64) A__ = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps A__ = torch.randn( __lowerCAmelCase , generator=__lowerCAmelCase , device="""cpu""" , dtype=__lowerCAmelCase ).to(self.device ) A__ = torch.randn(__lowerCAmelCase , generator=__lowerCAmelCase , device="""cpu""" , dtype=__lowerCAmelCase ).to( self.device ) else: A__ = torch.randn( __lowerCAmelCase , generator=__lowerCAmelCase , device=self.device , dtype=__lowerCAmelCase ) A__ = torch.randn(__lowerCAmelCase , generator=__lowerCAmelCase , device=self.device , dtype=__lowerCAmelCase ) else: if latents_reference.shape != latents_shape: raise ValueError(f'Unexpected latents shape, got {latents.shape}, expected {latents_shape}' ) A__ = latents_reference.to(self.device ) A__ = 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__ = (latents_shape[3] - latents_shape_reference[3]) // 2 A__ = (latents_shape[2] - latents_shape_reference[2]) // 2 A__ = latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx A__ = latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy A__ = 0 if dx < 0 else dx A__ = 0 if dy < 0 else dy A__ = max(-dx , 0 ) A__ = max(-dy , 0 ) # import pdb # pdb.set_trace() A__ = latents_reference[:, :, dy : dy + h, dx : dx + w] # set timesteps self.scheduler.set_timesteps(__lowerCAmelCase ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand A__ = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler A__ = 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__ = """eta""" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) A__ = {} if accepts_eta: A__ = eta for i, t in enumerate(self.progress_bar(__lowerCAmelCase ) ): # expand the latents if we are doing classifier free guidance A__ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents A__ = self.scheduler.scale_model_input(__lowerCAmelCase , __lowerCAmelCase ) # predict the noise residual A__ = self.unet(__lowerCAmelCase , __lowerCAmelCase , encoder_hidden_states=__lowerCAmelCase ).sample # perform guidance if do_classifier_free_guidance: A__ , A__ = noise_pred.chunk(2 ) A__ = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 A__ = self.scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = 1 / 0.1_8_2_1_5 * latents A__ = self.vae.decode(__lowerCAmelCase ).sample A__ = (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__ = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if self.safety_checker is not None: A__ = self.feature_extractor(self.numpy_to_pil(__lowerCAmelCase ) , return_tensors="""pt""" ).to( self.device ) A__ , A__ = self.safety_checker( images=__lowerCAmelCase , clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype ) ) else: A__ = None if output_type == "pil": A__ = self.numpy_to_pil(__lowerCAmelCase ) if not return_dict: return (image, has_nsfw_concept) return StableDiffusionPipelineOutput(images=__lowerCAmelCase , nsfw_content_detected=__lowerCAmelCase )
274
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = tempfile.mkdtemp() # fmt: off A__ = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on A__ = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) A__ = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""] A__ = {"""unk_token""": """<unk>"""} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(__lowerCAmelCase ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__lowerCAmelCase ) ) A__ = { """do_resize""": True, """size""": 20, """do_center_crop""": True, """crop_size""": 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], } A__ = os.path.join(self.tmpdirname , __lowerCAmelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Tuple , **__lowerCAmelCase : Dict ) -> str: """simple docstring""" return CLIPTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : Union[str, Any] , **__lowerCAmelCase : Dict ) -> List[str]: """simple docstring""" return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : List[str] , **__lowerCAmelCase : Optional[Any] ) -> Dict: """simple docstring""" return CLIPImageProcessor.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : str ) -> Dict: """simple docstring""" shutil.rmtree(self.tmpdirname ) def a_ ( self : str ) -> Any: """simple docstring""" A__ = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] A__ = [Image.fromarray(np.moveaxis(__lowerCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def a_ ( self : Optional[int] ) -> Tuple: """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_rust_tokenizer() A__ = self.get_image_processor() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_slow.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname , use_fast=__lowerCAmelCase ) A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_fast.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , __lowerCAmelCase ) self.assertIsInstance(processor_fast.tokenizer , __lowerCAmelCase ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , __lowerCAmelCase ) self.assertIsInstance(processor_fast.image_processor , __lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" A__ = CLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=__lowerCAmelCase , padding_value=1.0 ) A__ = CLIPProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__lowerCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowerCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCAmelCase ) def a_ ( self : List[Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(__lowerCAmelCase , return_tensors="""np""" ) A__ = processor(images=__lowerCAmelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def a_ ( self : Optional[Any] ) -> Any: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = processor(text=__lowerCAmelCase ) A__ = tokenizer(__lowerCAmelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(__lowerCAmelCase ): processor() def a_ ( self : Tuple ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.batch_decode(__lowerCAmelCase ) A__ = tokenizer.batch_decode(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Optional[int] ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
274
1
import unittest from transformers.utils.backbone_utils import ( BackboneMixin, get_aligned_output_features_output_indices, verify_out_features_out_indices, ) class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Any ) -> Union[str, Any]: """simple docstring""" A__ = ["""a""", """b""", """c"""] # Defaults to last layer if both are None A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""c"""] ) self.assertEqual(__lowerCAmelCase , [2] ) # Out indices set to match out features A__ , A__ = get_aligned_output_features_output_indices(["""a""", """c"""] , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features set to match out indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [0, 2] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features selected from negative indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [-3, -1] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [-3, -1] ) def a_ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , __lowerCAmelCase ) # Out features must be a list with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(("""a""", """b""") , (0, 1) , ["""a""", """b"""] ) # Out features must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , ["""a"""] ) # Out indices must be a list or tuple with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , 0 , ["""a""", """b"""] ) # Out indices must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , (0, 1) , ["""a"""] ) # Out features and out indices must be the same length with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0,) , ["""a""", """b""", """c"""] ) # Out features should match out indices with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 2) , ["""a""", """b""", """c"""] ) # Out features and out indices should be in order with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""b""", """a"""] , (0, 1) , ["""a""", """b"""] ) # Check passes with valid inputs verify_out_features_out_indices(["""a""", """b""", """d"""] , (0, 1, -1) , ["""a""", """b""", """c""", """d"""] ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" A__ = BackboneMixin() A__ = ["""a""", """b""", """c"""] A__ = ["""a""", """c"""] A__ = [0, 2] # Check that the output features and indices are set correctly self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [0, 2] ) # Check out features and indices are updated correctly A__ = ["""a""", """b"""] self.assertEqual(backbone.out_features , ["""a""", """b"""] ) self.assertEqual(backbone.out_indices , [0, 1] ) A__ = [-3, -1] self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [-3, -1] )
274
from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time A : Dict = Lock() def __lowerCamelCase ( __a :Dict , __a :List[str] , __a :Optional[int] , __a :Optional[int] , __a :Optional[Any] , __a :Optional[int] , __a :int ) -> Dict: """simple docstring""" global process_lock # we perform n swaps since after n swaps we know we are sorted # we *could* stop early if we are sorted already, but it takes as long to # find out we are sorted as it does to sort the list with this algorithm for i in range(0 , 1_0 ): if (i + position) % 2 == 0 and r_send is not None: # send your value to your right neighbor process_lock.acquire() r_send[1].send(__a ) process_lock.release() # receive your right neighbor's value process_lock.acquire() A__ = rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left A__ = min(__a , __a ) elif (i + position) % 2 != 0 and l_send is not None: # send your value to your left neighbor process_lock.acquire() l_send[1].send(__a ) process_lock.release() # receive your left neighbor's value process_lock.acquire() A__ = lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right A__ = max(__a , __a ) # after all swaps are performed, send the values back to main result_pipe[1].send(__a ) def __lowerCamelCase ( __a :List[str] ) -> int: """simple docstring""" A__ = [] A__ = [] # initialize the list of pipes where the values will be retrieved for _ in arr: result_pipe.append(Pipe() ) # creates the processes # the first and last process only have one neighbor so they are made outside # of the loop A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) A__ = temp_rs A__ = temp_rr for i in range(1 , len(__a ) - 1 ): A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) A__ = temp_rs A__ = temp_rr process_array_.append( Process( target=__a , args=( len(__a ) - 1, arr[len(__a ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(__a ) - 1], ) , ) ) # start the processes for p in process_array_: p.start() # wait for the processes to end and write their values to the list for p in range(0 , len(__a ) ): A__ = result_pipe[p][0].recv() process_array_[p].join() return arr def __lowerCamelCase ( ) -> str: """simple docstring""" A__ = list(range(1_0 , 0 , -1 ) ) print("""Initial List""" ) print(*__a ) A__ = odd_even_transposition(__a ) print("""Sorted List\n""" ) print(*__a ) if __name__ == "__main__": main()
274
1
from ...configuration_utils import PretrainedConfig from ...utils import logging A : Tuple = logging.get_logger(__name__) A : Dict = { '''microsoft/cvt-13''': '''https://huggingface.co/microsoft/cvt-13/resolve/main/config.json''', # See all Cvt models at https://huggingface.co/models?filter=cvt } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Any = '''cvt''' def __init__( self : Optional[int] , __lowerCAmelCase : Optional[Any]=3 , __lowerCAmelCase : Optional[Any]=[7, 3, 3] , __lowerCAmelCase : Optional[int]=[4, 2, 2] , __lowerCAmelCase : List[Any]=[2, 1, 1] , __lowerCAmelCase : Tuple=[64, 1_92, 3_84] , __lowerCAmelCase : Dict=[1, 3, 6] , __lowerCAmelCase : Dict=[1, 2, 10] , __lowerCAmelCase : Any=[4.0, 4.0, 4.0] , __lowerCAmelCase : str=[0.0, 0.0, 0.0] , __lowerCAmelCase : List[Any]=[0.0, 0.0, 0.0] , __lowerCAmelCase : Dict=[0.0, 0.0, 0.1] , __lowerCAmelCase : Optional[int]=[True, True, True] , __lowerCAmelCase : Optional[int]=[False, False, True] , __lowerCAmelCase : str=["dw_bn", "dw_bn", "dw_bn"] , __lowerCAmelCase : int=[3, 3, 3] , __lowerCAmelCase : Dict=[1, 1, 1] , __lowerCAmelCase : Union[str, Any]=[2, 2, 2] , __lowerCAmelCase : List[str]=[1, 1, 1] , __lowerCAmelCase : Tuple=[1, 1, 1] , __lowerCAmelCase : Any=0.0_2 , __lowerCAmelCase : Any=1e-12 , **__lowerCAmelCase : List[Any] , ) -> Optional[Any]: """simple docstring""" super().__init__(**__lowerCAmelCase ) A__ = num_channels A__ = patch_sizes A__ = patch_stride A__ = patch_padding A__ = embed_dim A__ = num_heads A__ = depth A__ = mlp_ratio A__ = attention_drop_rate A__ = drop_rate A__ = drop_path_rate A__ = qkv_bias A__ = cls_token A__ = qkv_projection_method A__ = kernel_qkv A__ = padding_kv A__ = stride_kv A__ = padding_q A__ = stride_q A__ = initializer_range A__ = layer_norm_eps
274
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def __lowerCamelCase ( __a :Dict ) -> Any: """simple docstring""" A__ = [ """decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(__a , __a ) def __lowerCamelCase ( __a :str ) -> Union[str, Any]: """simple docstring""" A__ , A__ = emb.weight.shape A__ = nn.Linear(__a , __a , bias=__a ) A__ = emb.weight.data return lin_layer def __lowerCamelCase ( __a :str ) -> List[str]: """simple docstring""" A__ = torch.load(__a , map_location="""cpu""" ) A__ = Namespace(**checkpoint["""cfg"""]["""model"""] ) A__ = checkpoint["""model"""] remove_ignore_keys_(__a ) A__ = state_dict["""decoder.embed_tokens.weight"""].shape[0] A__ = {key.replace("""decoder""" , """model""" ): val for key, val in state_dict.items()} A__ = XGLMConfig( vocab_size=__a , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""gelu""" , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) A__ = XGLMForCausalLM(__a ) A__ = model.load_state_dict(__a , strict=__a ) print(__a ) A__ = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": A : int = argparse.ArgumentParser() # Required parameters parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') A : str = parser.parse_args() A : str = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
274
1
def __lowerCamelCase ( __a :int ) -> int: """simple docstring""" A__ = [1] A__ , A__ , A__ = 0, 0, 0 A__ = ugly_nums[ia] * 2 A__ = ugly_nums[ia] * 3 A__ = ugly_nums[ia] * 5 for _ in range(1 , __a ): A__ = min(__a , __a , __a ) ugly_nums.append(__a ) if next_num == next_a: ia += 1 A__ = ugly_nums[ia] * 2 if next_num == next_a: ia += 1 A__ = ugly_nums[ia] * 3 if next_num == next_a: ia += 1 A__ = ugly_nums[ia] * 5 return ugly_nums[-1] if __name__ == "__main__": from doctest import testmod testmod(verbose=True) print(F'''{ugly_numbers(2_0_0) = }''')
274
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 A (unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : List[str]=13 , __lowerCAmelCase : int=7 , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : int=True , __lowerCAmelCase : Dict=True , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : List[Any]=99 , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Optional[Any]=5 , __lowerCAmelCase : Tuple=4 , __lowerCAmelCase : Any=37 , __lowerCAmelCase : str="gelu" , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : Union[str, Any]=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : int=16 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : List[Any]=0.0_2 , __lowerCAmelCase : Tuple=4 , ) -> Dict: """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_attention_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_choices def a_ ( self : Any ) -> str: """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_attention_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ = 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=__lowerCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a_ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class A (SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a_ ( self : str ) -> Optional[int]: """simple docstring""" A__ = FlaxAlbertModelTester(self ) @slow def a_ ( self : int ) -> Tuple: """simple docstring""" for model_class_name in self.all_model_classes: A__ = model_class_name.from_pretrained("""albert-base-v2""" ) A__ = model(np.ones((1, 1) ) ) self.assertIsNotNone(__lowerCAmelCase ) @require_flax class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : Dict ) -> List[Any]: """simple docstring""" A__ = FlaxAlbertModel.from_pretrained("""albert-base-v2""" ) A__ = np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] ) A__ = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase )[0] A__ = (1, 11, 7_68) self.assertEqual(output.shape , __lowerCAmelCase ) A__ = 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] , __lowerCAmelCase , atol=1e-4 ) )
274
1
def __lowerCamelCase ( __a :int , __a :int ) -> float: """simple docstring""" return base * power(__a , (exponent - 1) ) if exponent else 1 if __name__ == "__main__": print('''Raise base to the power of exponent using recursion...''') A : str = int(input('''Enter the base: ''').strip()) A : List[str] = int(input('''Enter the exponent: ''').strip()) A : Optional[int] = power(base, abs(exponent)) if exponent < 0: # power() does not properly deal w/ negative exponents A : Optional[Any] = 1 / result print(F'''{base} to the power of {exponent} is {result}''')
274
from sklearn.metrics import fa_score import datasets A : Any = ''' The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation: F1 = 2 * (precision * recall) / (precision + recall) ''' A : List[Any] = ''' Args: predictions (`list` of `int`): Predicted labels. references (`list` of `int`): Ground truth labels. labels (`list` of `int`): The set of labels to include when `average` is not set to `\'binary\'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None. pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1. average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`. - \'binary\': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary. - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives. - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - \'weighted\': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. This option can result in an F-score that is not between precision and recall. - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). sample_weight (`list` of `float`): Sample weights Defaults to None. Returns: f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better. Examples: Example 1-A simple binary example >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0]) >>> print(results) {\'f1\': 0.5} Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0) >>> print(round(results[\'f1\'], 2)) 0.67 Example 3-The same simple binary example as in Example 1, but with `sample_weight` included. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3]) >>> print(round(results[\'f1\'], 2)) 0.35 Example 4-A multiclass example, with different values for the `average` input. >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro") >>> print(round(results[\'f1\'], 2)) 0.33 >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {\'f1\': array([0.8, 0. , 0. ])} ''' A : List[Any] = ''' @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A (datasets.Metric ): '''simple docstring''' def a_ ( self : Optional[int] ) -> List[Any]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html"""] , ) def a_ ( self : Any , __lowerCAmelCase : Tuple , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict=None , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : Any="binary" , __lowerCAmelCase : Optional[int]=None ) -> List[Any]: """simple docstring""" A__ = fa_score( __lowerCAmelCase , __lowerCAmelCase , labels=__lowerCAmelCase , pos_label=__lowerCAmelCase , average=__lowerCAmelCase , sample_weight=__lowerCAmelCase ) return {"f1": float(__lowerCAmelCase ) if score.size == 1 else score}
274
1
import math import sys def __lowerCamelCase ( __a :str ) -> str: """simple docstring""" A__ = """""" try: with open(__a , """rb""" ) as binary_file: A__ = binary_file.read() for dat in data: A__ = F'{dat:08b}' result += curr_byte return result except OSError: print("""File not accessible""" ) sys.exit() def __lowerCamelCase ( __a :str ) -> str: """simple docstring""" A__ = {"""0""": """0""", """1""": """1"""} A__ , A__ = """""", """""" A__ = len(__a ) for i in range(len(__a ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue A__ = lexicon[curr_string] result += last_match_id A__ = last_match_id + """0""" if math.loga(__a ).is_integer(): A__ = {} for curr_key in list(__a ): A__ = lexicon.pop(__a ) A__ = new_lex A__ = last_match_id + """1""" index += 1 A__ = """""" return result def __lowerCamelCase ( __a :str , __a :str ) -> None: """simple docstring""" A__ = 8 try: with open(__a , """wb""" ) as opened_file: A__ = [ to_write[i : i + byte_length] for i in range(0 , len(__a ) , __a ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append("""10000000""" ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array[:-1]: opened_file.write(int(__a , 2 ).to_bytes(1 , byteorder="""big""" ) ) except OSError: print("""File not accessible""" ) sys.exit() def __lowerCamelCase ( __a :str ) -> str: """simple docstring""" A__ = 0 for letter in data_bits: if letter == "1": break counter += 1 A__ = data_bits[counter:] A__ = data_bits[counter + 1 :] return data_bits def __lowerCamelCase ( __a :str , __a :str ) -> None: """simple docstring""" A__ = read_file_binary(__a ) A__ = remove_prefix(__a ) A__ = decompress_data(__a ) write_file_binary(__a , __a ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
274
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Any = '''xlm-roberta''' def __init__( self : Optional[Any] , __lowerCAmelCase : List[Any]=3_05_22 , __lowerCAmelCase : int=7_68 , __lowerCAmelCase : Tuple=12 , __lowerCAmelCase : str=12 , __lowerCAmelCase : Union[str, Any]=30_72 , __lowerCAmelCase : Union[str, Any]="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-12 , __lowerCAmelCase : Union[str, Any]=1 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Tuple="absolute" , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=None , **__lowerCAmelCase : str , ) -> Optional[Any]: """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": A__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A__ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
274
1
def __lowerCamelCase ( __a :int , __a :bool = False ) -> bool: """simple docstring""" if n == 2: return True if not n % 2 or n < 2: return False if n > 5 and n % 1_0 not in (1, 3, 7, 9): # can quickly check last digit return False if n > 3_3_1_7_0_4_4_0_6_4_6_7_9_8_8_7_3_8_5_9_6_1_9_8_1 and not allow_probable: raise ValueError( """Warning: upper bound of deterministic test is exceeded. """ """Pass allow_probable=True to allow probabilistic test. """ """A return value of True indicates a probable prime.""" ) # array bounds provided by analysis A__ = [ 2_0_4_7, 1_3_7_3_6_5_3, 2_5_3_2_6_0_0_1, 3_2_1_5_0_3_1_7_5_1, 2_1_5_2_3_0_2_8_9_8_7_4_7, 3_4_7_4_7_4_9_6_6_0_3_8_3, 3_4_1_5_5_0_0_7_1_7_2_8_3_2_1, 1, 3_8_2_5_1_2_3_0_5_6_5_4_6_4_1_3_0_5_1, 1, 1, 3_1_8_6_6_5_8_5_7_8_3_4_0_3_1_1_5_1_1_6_7_4_6_1, 3_3_1_7_0_4_4_0_6_4_6_7_9_8_8_7_3_8_5_9_6_1_9_8_1, ] A__ = [2, 3, 5, 7, 1_1, 1_3, 1_7, 1_9, 2_3, 2_9, 3_1, 3_7, 4_1] for idx, _p in enumerate(__a , 1 ): if n < _p: # then we have our last prime to check A__ = primes[:idx] break A__ , A__ = n - 1, 0 # break up n -1 into a power of 2 (s) and # remaining odd component # essentially, solve for d * 2 ** s == n - 1 while d % 2 == 0: d //= 2 s += 1 for prime in plist: A__ = False for r in range(__a ): A__ = pow(__a , d * 2**r , __a ) # see article for analysis explanation for m if (r == 0 and m == 1) or ((m + 1) % n == 0): A__ = True # this loop will not determine compositeness break if pr: continue # if pr is False, then the above loop never evaluated to true, # and the n MUST be composite return False return True def __lowerCamelCase ( ) -> None: """simple docstring""" assert not miller_rabin(5_6_1 ) assert miller_rabin(5_6_3 ) # 2047 assert not miller_rabin(8_3_8_2_0_1 ) assert miller_rabin(8_3_8_2_0_7 ) # 1_373_653 assert not miller_rabin(1_7_3_1_6_0_0_1 ) assert miller_rabin(1_7_3_1_6_0_1_7 ) # 25_326_001 assert not miller_rabin(3_0_7_8_3_8_6_6_4_1 ) assert miller_rabin(3_0_7_8_3_8_6_6_5_3 ) # 3_215_031_751 assert not miller_rabin(1_7_1_3_0_4_5_5_7_4_8_0_1 ) assert miller_rabin(1_7_1_3_0_4_5_5_7_4_8_1_9 ) # 2_152_302_898_747 assert not miller_rabin(2_7_7_9_7_9_9_7_2_8_3_0_7 ) assert miller_rabin(2_7_7_9_7_9_9_7_2_8_3_2_7 ) # 3_474_749_660_383 assert not miller_rabin(1_1_3_8_5_0_0_2_3_9_0_9_4_4_1 ) assert miller_rabin(1_1_3_8_5_0_0_2_3_9_0_9_5_2_7 ) # 341_550_071_728_321 assert not miller_rabin(1_2_7_5_0_4_1_0_1_8_8_4_8_8_0_4_3_5_1 ) assert miller_rabin(1_2_7_5_0_4_1_0_1_8_8_4_8_8_0_4_3_9_1 ) # 3_825_123_056_546_413_051 assert not miller_rabin(7_9_6_6_6_4_6_4_4_5_8_5_0_7_7_8_7_7_9_1_8_6_7 ) assert miller_rabin(7_9_6_6_6_4_6_4_4_5_8_5_0_7_7_8_7_7_9_1_9_5_1 ) # 318_665_857_834_031_151_167_461 assert not miller_rabin(5_5_2_8_4_0_6_7_7_4_4_6_6_4_7_8_9_7_6_6_0_3_3_3 ) assert miller_rabin(5_5_2_8_4_0_6_7_7_4_4_6_6_4_7_8_9_7_6_6_0_3_5_9 ) # 3_317_044_064_679_887_385_961_981 # upper limit for probabilistic test if __name__ == "__main__": test_miller_rabin()
274
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean A : str = 0 A : Any = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] A : Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right A : Union[str, Any] = tuple[int, int] class A : '''simple docstring''' def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Node | None , ) -> None: """simple docstring""" A__ = pos_x A__ = pos_y A__ = (pos_y, pos_x) A__ = goal_x A__ = goal_y A__ = g_cost A__ = parent A__ = self.calculate_heuristic() A__ = self.g_cost + self.h_cost def a_ ( self : Dict ) -> float: """simple docstring""" A__ = self.pos_x - self.goal_x A__ = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(__lowerCAmelCase ) + abs(__lowerCAmelCase ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : int , __lowerCAmelCase : Node ) -> bool: """simple docstring""" return self.f_cost < other.f_cost class A : '''simple docstring''' def __init__( self : Union[str, Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> Tuple: """simple docstring""" A__ = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) A__ = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , __lowerCAmelCase ) A__ = [self.start] A__ = [] A__ = False def a_ ( self : List[str] ) -> list[TPosition]: """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() A__ = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) A__ = self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) return [self.start.pos] def a_ ( self : Optional[Any] , __lowerCAmelCase : Node ) -> list[Node]: """simple docstring""" A__ = [] for action in delta: A__ = parent.pos_x + action[1] A__ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def a_ ( self : List[Any] , __lowerCAmelCase : Node | None ) -> list[TPosition]: """simple docstring""" A__ = node A__ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A__ = current_node.parent path.reverse() return path class A : '''simple docstring''' def __init__( self : Optional[Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> None: """simple docstring""" A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = False def a_ ( self : int ) -> list[TPosition]: """simple docstring""" while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() A__ = self.fwd_astar.open_nodes.pop(0 ) A__ = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( __lowerCAmelCase , __lowerCAmelCase ) self.fwd_astar.closed_nodes.append(__lowerCAmelCase ) self.bwd_astar.closed_nodes.append(__lowerCAmelCase ) A__ = current_bwd_node A__ = current_fwd_node A__ = { self.fwd_astar: self.fwd_astar.get_successors(__lowerCAmelCase ), self.bwd_astar: self.bwd_astar.get_successors(__lowerCAmelCase ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = astar.open_nodes.pop( astar.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(__lowerCAmelCase ) else: astar.open_nodes.append(__lowerCAmelCase ) return [self.fwd_astar.start.pos] def a_ ( self : List[str] , __lowerCAmelCase : Node , __lowerCAmelCase : Node ) -> list[TPosition]: """simple docstring""" A__ = self.fwd_astar.retrace_path(__lowerCAmelCase ) A__ = self.bwd_astar.retrace_path(__lowerCAmelCase ) bwd_path.pop() bwd_path.reverse() A__ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] A : Optional[int] = (0, 0) A : int = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) A : Dict = time.time() A : Optional[Any] = AStar(init, goal) A : Optional[int] = a_star.search() A : Optional[int] = time.time() - start_time print(F'''AStar execution time = {end_time:f} seconds''') A : Dict = time.time() A : Tuple = BidirectionalAStar(init, goal) A : List[Any] = time.time() - bd_start_time print(F'''BidirectionalAStar execution time = {bd_end_time:f} seconds''')
274
1
import numpy as np class A : '''simple docstring''' def __init__( self : Optional[Any] ) -> str: """simple docstring""" A__ = (0, 0) A__ = None A__ = 0 A__ = 0 A__ = 0 def __eq__( self : Any , __lowerCAmelCase : str ) -> Optional[int]: """simple docstring""" return self.position == cell.position def a_ ( self : List[str] ) -> Optional[Any]: """simple docstring""" print(self.position ) class A : '''simple docstring''' def __init__( self : str , __lowerCAmelCase : Optional[Any]=(5, 5) ) -> Optional[Any]: """simple docstring""" A__ = np.zeros(__lowerCAmelCase ) A__ = world_size[0] A__ = world_size[1] def a_ ( self : Tuple ) -> Optional[int]: """simple docstring""" print(self.w ) def a_ ( self : Any , __lowerCAmelCase : List[Any] ) -> Dict: """simple docstring""" A__ = [ (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1), ] A__ = cell.position[0] A__ = cell.position[1] A__ = [] for n in neughbour_cord: A__ = current_x + n[0] A__ = current_y + n[1] if 0 <= x < self.world_x_limit and 0 <= y < self.world_y_limit: A__ = Cell() A__ = (x, y) A__ = cell neighbours.append(__lowerCAmelCase ) return neighbours def __lowerCamelCase ( __a :Union[str, Any] , __a :str , __a :int ) -> Dict: """simple docstring""" A__ = [] A__ = [] _open.append(__a ) while _open: A__ = np.argmin([n.f for n in _open] ) A__ = _open[min_f] _closed.append(_open.pop(__a ) ) if current == goal: break for n in world.get_neigbours(__a ): for c in _closed: if c == n: continue A__ = current.g + 1 A__ , A__ = n.position A__ , A__ = goal.position A__ = (ya - ya) ** 2 + (xa - xa) ** 2 A__ = n.h + n.g for c in _open: if c == n and c.f < n.f: continue _open.append(__a ) A__ = [] while current.parent is not None: path.append(current.position ) A__ = current.parent path.append(current.position ) return path[::-1] if __name__ == "__main__": A : Dict = Gridworld() # Start position and goal A : Optional[Any] = Cell() A : str = (0, 0) A : Optional[int] = Cell() A : Optional[int] = (4, 4) print(F'''path from {start.position} to {goal.position}''') A : Dict = astar(world, start, goal) # Just for visual reasons. for i in s: A : str = 1 print(world.w)
274
import unittest from transformers.utils.backbone_utils import ( BackboneMixin, get_aligned_output_features_output_indices, verify_out_features_out_indices, ) class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Any ) -> Union[str, Any]: """simple docstring""" A__ = ["""a""", """b""", """c"""] # Defaults to last layer if both are None A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""c"""] ) self.assertEqual(__lowerCAmelCase , [2] ) # Out indices set to match out features A__ , A__ = get_aligned_output_features_output_indices(["""a""", """c"""] , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features set to match out indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [0, 2] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features selected from negative indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [-3, -1] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [-3, -1] ) def a_ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , __lowerCAmelCase ) # Out features must be a list with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(("""a""", """b""") , (0, 1) , ["""a""", """b"""] ) # Out features must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , ["""a"""] ) # Out indices must be a list or tuple with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , 0 , ["""a""", """b"""] ) # Out indices must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , (0, 1) , ["""a"""] ) # Out features and out indices must be the same length with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0,) , ["""a""", """b""", """c"""] ) # Out features should match out indices with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 2) , ["""a""", """b""", """c"""] ) # Out features and out indices should be in order with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""b""", """a"""] , (0, 1) , ["""a""", """b"""] ) # Check passes with valid inputs verify_out_features_out_indices(["""a""", """b""", """d"""] , (0, 1, -1) , ["""a""", """b""", """c""", """d"""] ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" A__ = BackboneMixin() A__ = ["""a""", """b""", """c"""] A__ = ["""a""", """c"""] A__ = [0, 2] # Check that the output features and indices are set correctly self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [0, 2] ) # Check out features and indices are updated correctly A__ = ["""a""", """b"""] self.assertEqual(backbone.out_features , ["""a""", """b"""] ) self.assertEqual(backbone.out_indices , [0, 1] ) A__ = [-3, -1] self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [-3, -1] )
274
1
A : Any = '''0.18.2''' from .configuration_utils import ConfigMixin from .utils import ( OptionalDependencyNotAvailable, is_flax_available, is_inflect_available, is_invisible_watermark_available, is_k_diffusion_available, is_k_diffusion_version, is_librosa_available, is_note_seq_available, is_onnx_available, is_scipy_available, is_torch_available, is_torchsde_available, is_transformers_available, is_transformers_version, is_unidecode_available, logging, ) try: if not is_onnx_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_onnx_objects import * # noqa F403 else: from .pipelines import OnnxRuntimeModel try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_pt_objects import * # noqa F403 else: from .models import ( AutoencoderKL, ControlNetModel, ModelMixin, PriorTransformer, TaFilmDecoder, TransformeraDModel, UNetaDModel, UNetaDConditionModel, UNetaDModel, UNetaDConditionModel, VQModel, ) from .optimization import ( get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, get_scheduler, ) from .pipelines import ( AudioPipelineOutput, ConsistencyModelPipeline, DanceDiffusionPipeline, DDIMPipeline, DDPMPipeline, DiffusionPipeline, DiTPipeline, ImagePipelineOutput, KarrasVePipeline, LDMPipeline, LDMSuperResolutionPipeline, PNDMPipeline, RePaintPipeline, ScoreSdeVePipeline, ) from .schedulers import ( CMStochasticIterativeScheduler, DDIMInverseScheduler, DDIMParallelScheduler, DDIMScheduler, DDPMParallelScheduler, DDPMScheduler, DEISMultistepScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, HeunDiscreteScheduler, IPNDMScheduler, KarrasVeScheduler, KDPMaAncestralDiscreteScheduler, KDPMaDiscreteScheduler, PNDMScheduler, RePaintScheduler, SchedulerMixin, ScoreSdeVeScheduler, UnCLIPScheduler, UniPCMultistepScheduler, VQDiffusionScheduler, ) from .training_utils import EMAModel try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .schedulers import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .schedulers import DPMSolverSDEScheduler try: if not (is_torch_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipelines import ( AltDiffusionImgaImgPipeline, AltDiffusionPipeline, AudioLDMPipeline, CycleDiffusionPipeline, IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ImageTextPipelineOutput, KandinskyImgaImgPipeline, KandinskyInpaintPipeline, KandinskyPipeline, KandinskyPriorPipeline, KandinskyVaaControlnetImgaImgPipeline, KandinskyVaaControlnetPipeline, KandinskyVaaImgaImgPipeline, KandinskyVaaInpaintPipeline, KandinskyVaaPipeline, KandinskyVaaPriorEmbaEmbPipeline, KandinskyVaaPriorPipeline, LDMTextToImagePipeline, PaintByExamplePipeline, SemanticStableDiffusionPipeline, ShapEImgaImgPipeline, ShapEPipeline, StableDiffusionAttendAndExcitePipeline, StableDiffusionControlNetImgaImgPipeline, StableDiffusionControlNetInpaintPipeline, StableDiffusionControlNetPipeline, StableDiffusionDepthaImgPipeline, StableDiffusionDiffEditPipeline, StableDiffusionImageVariationPipeline, StableDiffusionImgaImgPipeline, StableDiffusionInpaintPipeline, StableDiffusionInpaintPipelineLegacy, StableDiffusionInstructPixaPixPipeline, StableDiffusionLatentUpscalePipeline, StableDiffusionLDMaDPipeline, StableDiffusionModelEditingPipeline, StableDiffusionPanoramaPipeline, StableDiffusionParadigmsPipeline, StableDiffusionPipeline, StableDiffusionPipelineSafe, StableDiffusionPixaPixZeroPipeline, StableDiffusionSAGPipeline, StableDiffusionUpscalePipeline, StableUnCLIPImgaImgPipeline, StableUnCLIPPipeline, TextToVideoSDPipeline, TextToVideoZeroPipeline, UnCLIPImageVariationPipeline, UnCLIPPipeline, UniDiffuserModel, UniDiffuserPipeline, UniDiffuserTextDecoder, VersatileDiffusionDualGuidedPipeline, VersatileDiffusionImageVariationPipeline, VersatileDiffusionPipeline, VersatileDiffusionTextToImagePipeline, VideoToVideoSDPipeline, VQDiffusionPipeline, ) try: if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import * # noqa F403 else: from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline try: if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import * # noqa F403 else: from .pipelines import StableDiffusionKDiffusionPipeline try: if not (is_torch_available() and is_transformers_available() and is_onnx_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_onnx_objects import * # noqa F403 else: from .pipelines import ( OnnxStableDiffusionImgaImgPipeline, OnnxStableDiffusionInpaintPipeline, OnnxStableDiffusionInpaintPipelineLegacy, OnnxStableDiffusionPipeline, OnnxStableDiffusionUpscalePipeline, StableDiffusionOnnxPipeline, ) try: if not (is_torch_available() and is_librosa_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_librosa_objects import * # noqa F403 else: from .pipelines import AudioDiffusionPipeline, Mel try: if not (is_transformers_available() and is_torch_available() and is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_transformers_and_torch_and_note_seq_objects import * # noqa F403 else: from .pipelines import SpectrogramDiffusionPipeline try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_objects import * # noqa F403 else: from .models.controlnet_flax import FlaxControlNetModel from .models.modeling_flax_utils import FlaxModelMixin from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel from .models.vae_flax import FlaxAutoencoderKL from .pipelines import FlaxDiffusionPipeline from .schedulers import ( FlaxDDIMScheduler, FlaxDDPMScheduler, FlaxDPMSolverMultistepScheduler, FlaxKarrasVeScheduler, FlaxLMSDiscreteScheduler, FlaxPNDMScheduler, FlaxSchedulerMixin, FlaxScoreSdeVeScheduler, ) try: if not (is_flax_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_and_transformers_objects import * # noqa F403 else: from .pipelines import ( FlaxStableDiffusionControlNetPipeline, FlaxStableDiffusionImgaImgPipeline, FlaxStableDiffusionInpaintPipeline, FlaxStableDiffusionPipeline, ) try: if not (is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_note_seq_objects import * # noqa F403 else: from .pipelines import MidiProcessor
274
from collections import deque class A : '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" A__ = process_name # process name A__ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time A__ = arrival_time A__ = burst_time # remaining burst time A__ = 0 # total time of the process wait in ready queue A__ = 0 # time from arrival time to completion time class A : '''simple docstring''' def __init__( self : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : list[int] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int , ) -> None: """simple docstring""" A__ = number_of_queues # time slice of queues that round robin algorithm applied A__ = time_slices # unfinished process is in this ready_queue A__ = queue # current time A__ = current_time # finished process is in this sequence queue A__ = deque() def a_ ( self : Dict ) -> list[str]: """simple docstring""" A__ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def a_ ( self : Tuple , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def a_ ( self : Optional[Any] , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def a_ ( self : Dict , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): completion_times.append(queue[i].stop_time ) return completion_times def a_ ( self : int , __lowerCAmelCase : deque[Process] ) -> list[int]: """simple docstring""" return [q.burst_time for q in queue] def a_ ( self : Any , __lowerCAmelCase : Process ) -> int: """simple docstring""" process.waiting_time += self.current_time - process.stop_time return process.waiting_time def a_ ( self : Union[str, Any] , __lowerCAmelCase : deque[Process] ) -> deque[Process]: """simple docstring""" A__ = deque() # sequence deque of finished process while len(__lowerCAmelCase ) != 0: A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__lowerCAmelCase ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 A__ = 0 # set the process's turnaround time because it is finished A__ = self.current_time - cp.arrival_time # set the completion time A__ = self.current_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def a_ ( self : Optional[Any] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int ) -> tuple[deque[Process], deque[Process]]: """simple docstring""" A__ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__lowerCAmelCase ) ): A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__lowerCAmelCase ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time A__ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__lowerCAmelCase ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished A__ = 0 # set the finish time A__ = self.current_time # update the process' turnaround time because it is finished A__ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def a_ ( self : List[Any] ) -> deque[Process]: """simple docstring""" for i in range(self.number_of_queues - 1 ): A__ , A__ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest A : Union[str, Any] = Process('''P1''', 0, 5_3) A : Optional[Any] = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : int = Process('''P4''', 0, 2_4) A : Any = 3 A : List[Any] = [1_7, 2_5] A : Optional[Any] = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) A : Optional[Any] = Process('''P1''', 0, 5_3) A : int = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : Tuple = Process('''P4''', 0, 2_4) A : Union[str, Any] = 3 A : Optional[Any] = [1_7, 2_5] A : Tuple = deque([Pa, Pa, Pa, Pa]) A : Optional[int] = MLFQ(number_of_queues, time_slices, queue, 0) A : Dict = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( F'''waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print completion times of processes(P1, P2, P3, P4) print( F'''completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print total turnaround times of processes(P1, P2, P3, P4) print( F'''turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print sequence of finished processes print( F'''sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}''' )
274
1
import argparse import struct import unittest class A : '''simple docstring''' def __init__( self : Optional[int] , __lowerCAmelCase : bytes ) -> None: """simple docstring""" A__ = data # Initialize hash values A__ = [ 0X6A09E667, 0XBB67AE85, 0X3C6EF372, 0XA54FF53A, 0X510E527F, 0X9B05688C, 0X1F83D9AB, 0X5BE0CD19, ] # Initialize round constants A__ = [ 0X428A2F98, 0X71374491, 0XB5C0FBCF, 0XE9B5DBA5, 0X3956C25B, 0X59F111F1, 0X923F82A4, 0XAB1C5ED5, 0XD807AA98, 0X12835B01, 0X243185BE, 0X550C7DC3, 0X72BE5D74, 0X80DEB1FE, 0X9BDC06A7, 0XC19BF174, 0XE49B69C1, 0XEFBE4786, 0X0FC19DC6, 0X240CA1CC, 0X2DE92C6F, 0X4A7484AA, 0X5CB0A9DC, 0X76F988DA, 0X983E5152, 0XA831C66D, 0XB00327C8, 0XBF597FC7, 0XC6E00BF3, 0XD5A79147, 0X06CA6351, 0X14292967, 0X27B70A85, 0X2E1B2138, 0X4D2C6DFC, 0X53380D13, 0X650A7354, 0X766A0ABB, 0X81C2C92E, 0X92722C85, 0XA2BFE8A1, 0XA81A664B, 0XC24B8B70, 0XC76C51A3, 0XD192E819, 0XD6990624, 0XF40E3585, 0X106AA070, 0X19A4C116, 0X1E376C08, 0X2748774C, 0X34B0BCB5, 0X391C0CB3, 0X4ED8AA4A, 0X5B9CCA4F, 0X682E6FF3, 0X748F82EE, 0X78A5636F, 0X84C87814, 0X8CC70208, 0X90BEFFFA, 0XA4506CEB, 0XBEF9A3F7, 0XC67178F2, ] A__ = self.preprocessing(self.data ) self.final_hash() @staticmethod def a_ ( __lowerCAmelCase : bytes ) -> bytes: """simple docstring""" A__ = B"""\x80""" + (B"""\x00""" * (63 - (len(__lowerCAmelCase ) + 8) % 64)) A__ = struct.pack(""">Q""" , (len(__lowerCAmelCase ) * 8) ) return data + padding + big_endian_integer def a_ ( self : Optional[int] ) -> None: """simple docstring""" A__ = [ self.preprocessed_data[x : x + 64] for x in range(0 , len(self.preprocessed_data ) , 64 ) ] for block in self.blocks: # Convert the given block into a list of 4 byte integers A__ = list(struct.unpack(""">16L""" , __lowerCAmelCase ) ) # add 48 0-ed integers words += [0] * 48 A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ = self.hashes for index in range(0 , 64 ): if index > 15: # modify the zero-ed indexes at the end of the array A__ = ( self.ror(words[index - 15] , 7 ) ^ self.ror(words[index - 15] , 18 ) ^ (words[index - 15] >> 3) ) A__ = ( self.ror(words[index - 2] , 17 ) ^ self.ror(words[index - 2] , 19 ) ^ (words[index - 2] >> 10) ) A__ = ( words[index - 16] + sa + words[index - 7] + sa ) % 0X100000000 # Compression A__ = self.ror(__lowerCAmelCase , 6 ) ^ self.ror(__lowerCAmelCase , 11 ) ^ self.ror(__lowerCAmelCase , 25 ) A__ = (e & f) ^ ((~e & 0XFFFFFFFF) & g) A__ = ( h + sa + ch + self.round_constants[index] + words[index] ) % 0X100000000 A__ = self.ror(__lowerCAmelCase , 2 ) ^ self.ror(__lowerCAmelCase , 13 ) ^ self.ror(__lowerCAmelCase , 22 ) A__ = (a & b) ^ (a & c) ^ (b & c) A__ = (sa + maj) % 0X100000000 A__ , A__ , A__ , A__ , A__ , A__ , A__ , A__ = ( g, f, e, ((d + tempa) % 0X100000000), c, b, a, ((tempa + tempa) % 0X100000000), ) A__ = [a, b, c, d, e, f, g, h] # Modify final values A__ = [ ((element + mutated_hash_values[index]) % 0X100000000) for index, element in enumerate(self.hashes ) ] A__ = """""".join([hex(__lowerCAmelCase )[2:].zfill(8 ) for value in self.hashes] ) def a_ ( self : str , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> int: """simple docstring""" return 0XFFFFFFFF & (value << (32 - rotations)) | (value >> rotations) class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : List[Any] ) -> None: """simple docstring""" import hashlib A__ = bytes("""Test String""" , """utf-8""" ) self.assertEqual(SHAaaa(__lowerCAmelCase ).hash , hashlib.shaaaa(__lowerCAmelCase ).hexdigest() ) def __lowerCamelCase ( ) -> None: """simple docstring""" import doctest doctest.testmod() A__ = argparse.ArgumentParser() parser.add_argument( """-s""" , """--string""" , dest="""input_string""" , default="""Hello World!! Welcome to Cryptography""" , help="""Hash the string""" , ) parser.add_argument( """-f""" , """--file""" , dest="""input_file""" , help="""Hash contents of a file""" ) A__ = parser.parse_args() A__ = args.input_string # hash input should be a bytestring if args.input_file: with open(args.input_file , """rb""" ) as f: A__ = f.read() else: A__ = bytes(__a , """utf-8""" ) print(SHAaaa(__a ).hash ) if __name__ == "__main__": main()
274
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType A : str = logging.get_logger(__name__) A : Union[str, Any] = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Optional[Any] = '''layoutlmv3''' def __init__( self : Tuple , __lowerCAmelCase : Optional[int]=5_02_65 , __lowerCAmelCase : Tuple=7_68 , __lowerCAmelCase : Union[str, Any]=12 , __lowerCAmelCase : Any=12 , __lowerCAmelCase : List[Any]=30_72 , __lowerCAmelCase : Optional[int]="gelu" , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[Any]=0.1 , __lowerCAmelCase : Dict=5_12 , __lowerCAmelCase : Any=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-5 , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : int=0 , __lowerCAmelCase : Dict=2 , __lowerCAmelCase : Tuple=10_24 , __lowerCAmelCase : List[str]=1_28 , __lowerCAmelCase : Optional[int]=1_28 , __lowerCAmelCase : Any=True , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Any=1_28 , __lowerCAmelCase : str=64 , __lowerCAmelCase : Optional[int]=2_56 , __lowerCAmelCase : int=True , __lowerCAmelCase : int=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : int=2_24 , __lowerCAmelCase : Dict=3 , __lowerCAmelCase : List[Any]=16 , __lowerCAmelCase : Dict=None , **__lowerCAmelCase : Optional[Any] , ) -> Dict: """simple docstring""" super().__init__( vocab_size=__lowerCAmelCase , hidden_size=__lowerCAmelCase , num_hidden_layers=__lowerCAmelCase , num_attention_heads=__lowerCAmelCase , intermediate_size=__lowerCAmelCase , hidden_act=__lowerCAmelCase , hidden_dropout_prob=__lowerCAmelCase , attention_probs_dropout_prob=__lowerCAmelCase , max_position_embeddings=__lowerCAmelCase , type_vocab_size=__lowerCAmelCase , initializer_range=__lowerCAmelCase , layer_norm_eps=__lowerCAmelCase , pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = max_ad_position_embeddings A__ = coordinate_size A__ = shape_size A__ = has_relative_attention_bias A__ = rel_pos_bins A__ = max_rel_pos A__ = has_spatial_attention_bias A__ = rel_ad_pos_bins A__ = max_rel_ad_pos A__ = text_embed A__ = visual_embed A__ = input_size A__ = num_channels A__ = patch_size A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[str] = version.parse('''1.12''' ) @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def a_ ( self : Optional[int] ) -> float: """simple docstring""" return 1e-5 @property def a_ ( self : Tuple ) -> int: """simple docstring""" return 12 def a_ ( self : str , __lowerCAmelCase : "ProcessorMixin" , __lowerCAmelCase : int = -1 , __lowerCAmelCase : int = -1 , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional["TensorType"] = None , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 40 , __lowerCAmelCase : int = 40 , ) -> Mapping[str, Any]: """simple docstring""" setattr(processor.image_processor , """apply_ocr""" , __lowerCAmelCase ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX A__ = processor.tokenizer.num_special_tokens_to_add(__lowerCAmelCase ) A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__lowerCAmelCase ) # Generate dummy inputs according to compute batch and sequence A__ = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes A__ = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) A__ = self._generate_dummy_images(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = dict( processor( __lowerCAmelCase , text=__lowerCAmelCase , boxes=__lowerCAmelCase , return_tensors=__lowerCAmelCase , ) ) return inputs
274
1
from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = '''ClapFeatureExtractor''' __lowerCamelCase : Dict = ('''RobertaTokenizer''', '''RobertaTokenizerFast''') def __init__( self : List[Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[Any] ) -> str: """simple docstring""" super().__init__(__lowerCAmelCase , __lowerCAmelCase ) def __call__( self : List[str] , __lowerCAmelCase : Optional[Any]=None , __lowerCAmelCase : Any=None , __lowerCAmelCase : Tuple=None , **__lowerCAmelCase : int ) -> str: """simple docstring""" A__ = kwargs.pop("""sampling_rate""" , __lowerCAmelCase ) if text is None and audios is None: raise ValueError("""You have to specify either text or audios. Both cannot be none.""" ) if text is not None: A__ = self.tokenizer(__lowerCAmelCase , return_tensors=__lowerCAmelCase , **__lowerCAmelCase ) if audios is not None: A__ = self.feature_extractor( __lowerCAmelCase , sampling_rate=__lowerCAmelCase , return_tensors=__lowerCAmelCase , **__lowerCAmelCase ) if text is not None and audios is not None: A__ = audio_features.input_features return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__lowerCAmelCase ) , tensor_type=__lowerCAmelCase ) def a_ ( self : Optional[Any] , *__lowerCAmelCase : int , **__lowerCAmelCase : Union[str, Any] ) -> str: """simple docstring""" return self.tokenizer.batch_decode(*__lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : List[str] , *__lowerCAmelCase : Optional[int] , **__lowerCAmelCase : int ) -> List[Any]: """simple docstring""" return self.tokenizer.decode(*__lowerCAmelCase , **__lowerCAmelCase ) @property def a_ ( self : Any ) -> Optional[int]: """simple docstring""" A__ = self.tokenizer.model_input_names A__ = self.feature_extractor.model_input_names return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names ) )
274
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) A : str = '''▁''' A : Any = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', '''tokenizer_config_file''': '''tokenizer_config.json''', } A : List[Any] = { '''vocab_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json''', }, '''spm_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_config_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json''', }, } A : Tuple = { '''facebook/m2m100_418M''': 1_0_2_4, } # fmt: off A : Optional[int] = { '''m2m100''': ['''af''', '''am''', '''ar''', '''ast''', '''az''', '''ba''', '''be''', '''bg''', '''bn''', '''br''', '''bs''', '''ca''', '''ceb''', '''cs''', '''cy''', '''da''', '''de''', '''el''', '''en''', '''es''', '''et''', '''fa''', '''ff''', '''fi''', '''fr''', '''fy''', '''ga''', '''gd''', '''gl''', '''gu''', '''ha''', '''he''', '''hi''', '''hr''', '''ht''', '''hu''', '''hy''', '''id''', '''ig''', '''ilo''', '''is''', '''it''', '''ja''', '''jv''', '''ka''', '''kk''', '''km''', '''kn''', '''ko''', '''lb''', '''lg''', '''ln''', '''lo''', '''lt''', '''lv''', '''mg''', '''mk''', '''ml''', '''mn''', '''mr''', '''ms''', '''my''', '''ne''', '''nl''', '''no''', '''ns''', '''oc''', '''or''', '''pa''', '''pl''', '''ps''', '''pt''', '''ro''', '''ru''', '''sd''', '''si''', '''sk''', '''sl''', '''so''', '''sq''', '''sr''', '''ss''', '''su''', '''sv''', '''sw''', '''ta''', '''th''', '''tl''', '''tn''', '''tr''', '''uk''', '''ur''', '''uz''', '''vi''', '''wo''', '''xh''', '''yi''', '''yo''', '''zh''', '''zu'''], '''wmt21''': ['''en''', '''ha''', '''is''', '''ja''', '''cs''', '''ru''', '''zh''', '''de'''] } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = VOCAB_FILES_NAMES __lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = ['''input_ids''', '''attention_mask'''] __lowerCamelCase : List[int] = [] __lowerCamelCase : List[int] = [] def __init__( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any]=None , __lowerCAmelCase : str=None , __lowerCAmelCase : List[Any]="<s>" , __lowerCAmelCase : List[Any]="</s>" , __lowerCAmelCase : Optional[int]="</s>" , __lowerCAmelCase : Optional[Any]="<pad>" , __lowerCAmelCase : Any="<unk>" , __lowerCAmelCase : Any="m2m100" , __lowerCAmelCase : Optional[Dict[str, Any]] = None , __lowerCAmelCase : Dict=8 , **__lowerCAmelCase : Tuple , ) -> None: """simple docstring""" A__ = {} if sp_model_kwargs is None else sp_model_kwargs A__ = language_codes A__ = FAIRSEQ_LANGUAGE_CODES[language_codes] A__ = {lang_code: f'__{lang_code}__' for lang_code in fairseq_language_code} A__ = kwargs.get("""additional_special_tokens""" , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(__lowerCAmelCase ) for lang_code in fairseq_language_code if self.get_lang_token(__lowerCAmelCase ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=__lowerCAmelCase , tgt_lang=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , language_codes=__lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = vocab_file A__ = load_json(__lowerCAmelCase ) A__ = {v: k for k, v in self.encoder.items()} A__ = spm_file A__ = load_spm(__lowerCAmelCase , self.sp_model_kwargs ) A__ = len(self.encoder ) A__ = { self.get_lang_token(__lowerCAmelCase ): self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase ) } A__ = {lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase )} A__ = {v: k for k, v in self.lang_token_to_id.items()} A__ = src_lang if src_lang is not None else """en""" A__ = tgt_lang A__ = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) A__ = num_madeup_words @property def a_ ( self : Optional[int] ) -> int: """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def a_ ( self : Optional[Any] ) -> str: """simple docstring""" return self._src_lang @src_lang.setter def a_ ( self : List[Any] , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def a_ ( self : Optional[int] , __lowerCAmelCase : str ) -> List[str]: """simple docstring""" return self.sp_model.encode(__lowerCAmelCase , out_type=__lowerCAmelCase ) def a_ ( self : Optional[Any] , __lowerCAmelCase : Dict ) -> Optional[Any]: """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(__lowerCAmelCase , self.encoder[self.unk_token] ) def a_ ( self : Optional[int] , __lowerCAmelCase : int ) -> str: """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(__lowerCAmelCase , self.unk_token ) def a_ ( self : Optional[int] , __lowerCAmelCase : Dict ) -> str: """simple docstring""" A__ = [] A__ = """""" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(__lowerCAmelCase ) + token A__ = [] else: current_sub_tokens.append(__lowerCAmelCase ) out_string += self.sp_model.decode(__lowerCAmelCase ) return out_string.strip() def a_ ( self : List[str] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None , __lowerCAmelCase : bool = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCAmelCase , token_ids_a=__lowerCAmelCase , already_has_special_tokens=__lowerCAmelCase ) A__ = [1] * len(self.prefix_tokens ) A__ = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(__lowerCAmelCase )) + suffix_ones return prefix_ones + ([0] * len(__lowerCAmelCase )) + ([0] * len(__lowerCAmelCase )) + suffix_ones def a_ ( self : Tuple , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def a_ ( self : int ) -> Dict: """simple docstring""" A__ = {self.convert_ids_to_tokens(__lowerCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.__dict__.copy() A__ = None return state def __setstate__( self : str , __lowerCAmelCase : Dict ) -> None: """simple docstring""" A__ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): A__ = {} A__ = load_spm(self.spm_file , self.sp_model_kwargs ) def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" A__ = Path(__lowerCAmelCase ) if not save_dir.is_dir(): raise OSError(f'{save_directory} should be a directory' ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , __lowerCAmelCase ) if os.path.abspath(self.spm_file ) != os.path.abspath(__lowerCAmelCase ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , __lowerCAmelCase ) elif not os.path.isfile(self.spm_file ): with open(__lowerCAmelCase , """wb""" ) as fi: A__ = self.sp_model.serialized_model_proto() fi.write(__lowerCAmelCase ) return (str(__lowerCAmelCase ), str(__lowerCAmelCase )) def a_ ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : str = "en" , __lowerCAmelCase : Optional[List[str]] = None , __lowerCAmelCase : str = "ro" , **__lowerCAmelCase : List[Any] , ) -> BatchEncoding: """simple docstring""" A__ = src_lang A__ = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(__lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[str] , __lowerCAmelCase : Optional[str] , **__lowerCAmelCase : Tuple ) -> Tuple: """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""" ) A__ = src_lang A__ = self(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , **__lowerCAmelCase ) A__ = self.get_lang_id(__lowerCAmelCase ) A__ = tgt_lang_id return inputs def a_ ( self : Dict ) -> int: """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def a_ ( self : str , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Tuple , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> str: """simple docstring""" return self.lang_code_to_token[lang] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> int: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) return self.lang_token_to_id[lang_token] def __lowerCamelCase ( __a :str , __a :Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: """simple docstring""" A__ = sentencepiece.SentencePieceProcessor(**__a ) spm.Load(str(__a ) ) return spm def __lowerCamelCase ( __a :str ) -> Union[Dict, List]: """simple docstring""" with open(__a , """r""" ) as f: return json.load(__a ) def __lowerCamelCase ( __a :List[Any] , __a :str ) -> None: """simple docstring""" with open(__a , """w""" ) as f: json.dump(__a , __a , indent=2 )
274
1
from __future__ import annotations def __lowerCamelCase ( __a :str , __a :list[str] | None = None ) -> list[list[str]]: """simple docstring""" A__ = word_bank or [] # create a table A__ = len(__a ) + 1 A__ = [] for _ in range(__a ): table.append([] ) # seed value A__ = [[]] # because empty string has empty combination # iterate through the indices for i in range(__a ): # condition if table[i] != []: for word in word_bank: # slice condition if target[i : i + len(__a )] == word: A__ = [ [word, *way] for way in table[i] ] # adds the word to every combination the current position holds # now,push that combination to the table[i+len(word)] table[i + len(__a )] += new_combinations # combinations are in reverse order so reverse for better output for combination in table[len(__a )]: combination.reverse() return table[len(__a )] if __name__ == "__main__": print(all_construct('''jwajalapa''', ['''jwa''', '''j''', '''w''', '''a''', '''la''', '''lapa'''])) print(all_construct('''rajamati''', ['''s''', '''raj''', '''amat''', '''raja''', '''ma''', '''i''', '''t'''])) print( all_construct( '''hexagonosaurus''', ['''h''', '''ex''', '''hex''', '''ag''', '''ago''', '''ru''', '''auru''', '''rus''', '''go''', '''no''', '''o''', '''s'''], ) )
274
from __future__ import annotations from PIL import Image # Define glider example A : Any = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [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], ] # Define blinker example A : Optional[Any] = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def __lowerCamelCase ( __a :list[list[int]] ) -> list[list[int]]: """simple docstring""" A__ = [] for i in range(len(__a ) ): A__ = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours A__ = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(__a ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(__a ) - 1: neighbour_count += cells[i + 1][j] if i < len(__a ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. A__ = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(__a ) return next_generation def __lowerCamelCase ( __a :list[list[int]] , __a :int ) -> list[Image.Image]: """simple docstring""" A__ = [] for _ in range(__a ): # Create output image A__ = Image.new("""RGB""" , (len(cells[0] ), len(__a )) ) A__ = img.load() # Save cells to image for x in range(len(__a ) ): for y in range(len(cells[0] ) ): A__ = 2_5_5 - cells[y][x] * 2_5_5 A__ = (colour, colour, colour) # Save image images.append(__a ) A__ = new_generation(__a ) return images if __name__ == "__main__": A : str = generate_images(GLIDER, 1_6) images[0].save('''out.gif''', save_all=True, append_images=images[1:])
274
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) A : int = { '''configuration_swiftformer''': [ '''SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SwiftFormerConfig''', '''SwiftFormerOnnxConfig''', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[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 A : Optional[int] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
274
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": A : List[str] = input('''Enter image url: ''').strip() print(F'''Downloading image from {url} ...''') A : Any = BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image A : List[Any] = soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] A : Dict = requests.get(image_url).content A : Tuple = F'''{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg''' with open(file_name, '''wb''') as fp: fp.write(image_data) print(F'''Done. Image saved to disk as {file_name}.''')
274
1
import os from pickle import UnpicklingError from typing import Dict, Tuple import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict, unflatten_dict import transformers from .utils import logging A : Optional[int] = logging.get_logger(__name__) def __lowerCamelCase ( __a :int , __a :List[str] , __a :Optional[Any] , __a :Tuple=False ) -> int: """simple docstring""" try: import torch # noqa: F401 except ImportError: logger.error( """Loading a PyTorch model in Flax, 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 if not is_sharded: A__ = os.path.abspath(__a ) logger.info(F'Loading PyTorch weights from {pt_path}' ) A__ = torch.load(__a , map_location="""cpu""" ) logger.info(F'PyTorch checkpoint contains {sum(t.numel() for t in pt_state_dict.values() ):,} parameters.' ) A__ = convert_pytorch_state_dict_to_flax(__a , __a ) else: # model is sharded and pytorch_checkpoint_path already contains the list of .pt shard files A__ = convert_pytorch_sharded_state_dict_to_flax(__a , __a ) return flax_state_dict def __lowerCamelCase ( __a :Tuple[str] , __a :np.ndarray , __a :Dict[str, jnp.ndarray] , __a :str , ) -> (Tuple[str], np.ndarray): """simple docstring""" def is_key_or_prefix_key_in_dict(__a :Tuple[str] ) -> bool: return len(set(__a ) & {key, (model_prefix,) + key} ) > 0 # layer norm A__ = pt_tuple_key[:-1] + ("""scale""",) if pt_tuple_key[-1] in ["weight", "gamma"] and is_key_or_prefix_key_in_dict(__a ): return renamed_pt_tuple_key, pt_tensor # batch norm layer mean A__ = pt_tuple_key[:-1] + ("""mean""",) if pt_tuple_key[-1] == "running_mean" and not is_key_or_prefix_key_in_dict(__a ): return renamed_pt_tuple_key, pt_tensor # batch norm layer var A__ = pt_tuple_key[:-1] + ("""var""",) if pt_tuple_key[-1] == "running_var" and not is_key_or_prefix_key_in_dict(__a ): return renamed_pt_tuple_key, pt_tensor # embedding A__ = pt_tuple_key[:-1] + ("""embedding""",) if pt_tuple_key[-1] == "weight" and is_key_or_prefix_key_in_dict(__a ): return renamed_pt_tuple_key, pt_tensor # conv layer A__ = pt_tuple_key[:-1] + ("""kernel""",) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4 and not is_key_or_prefix_key_in_dict(__a ): A__ = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer A__ = pt_tuple_key[:-1] + ("""kernel""",) if pt_tuple_key[-1] == "weight" and not is_key_or_prefix_key_in_dict(__a ): A__ = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight A__ = pt_tuple_key[:-1] + ("""weight""",) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias A__ = pt_tuple_key[:-1] + ("""bias""",) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 A__ = None if pt_tuple_key[-3::2] == ("parametrizations", "original0"): A__ = pt_tuple_key[-2] + """_g""" elif pt_tuple_key[-3::2] == ("parametrizations", "original1"): A__ = pt_tuple_key[-2] + """_v""" if name is not None: A__ = pt_tuple_key[:-3] + (name,) return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def __lowerCamelCase ( __a :Dict , __a :Optional[Any] ) -> Union[str, Any]: """simple docstring""" A__ = {k: v.numpy() for k, v in pt_state_dict.items()} A__ = flax_model.base_model_prefix # use params dict if the model contains batch norm layers if "params" in flax_model.params: A__ = flax_model.params["""params"""] else: A__ = flax_model.params A__ = flatten_dict(__a ) # add batch_stats keys,values to dict if "batch_stats" in flax_model.params: A__ = flatten_dict(flax_model.params["""batch_stats"""] ) random_flax_state_dict.update(__a ) A__ = {} A__ = (model_prefix not in flax_model_params) and ( model_prefix in {k.split(""".""" )[0] for k in pt_state_dict.keys()} ) A__ = (model_prefix in flax_model_params) and ( model_prefix not in {k.split(""".""" )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): A__ = tuple(pt_key.split(""".""" ) ) # remove base model prefix if necessary A__ = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: A__ = pt_tuple_key[1:] # Correctly rename weight parameters A__ , A__ = rename_key_and_reshape_tensor( __a , __a , __a , __a ) # add model prefix if necessary A__ = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: A__ = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F'PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape ' F'{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1] or "var" in flax_key[-1]: A__ = jnp.asarray(__a ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(__a , __a ) continue # also add unexpected weight so that warning is thrown A__ = jnp.asarray(__a ) else: # also add unexpected weight so that warning is thrown A__ = jnp.asarray(__a ) return unflatten_dict(__a ) def __lowerCamelCase ( __a :Optional[int] , __a :int ) -> Union[str, Any]: """simple docstring""" import torch # Load the index A__ = {} for shard_file in shard_filenames: # load using msgpack utils A__ = torch.load(__a ) A__ = {k: v.numpy() for k, v in pt_state_dict.items()} A__ = flax_model.base_model_prefix # use params dict if the model contains batch norm layers and then add batch_stats keys,values to dict if "batch_stats" in flax_model.params: A__ = flax_model.params["""params"""] A__ = flatten_dict(__a ) random_flax_state_dict.update(flatten_dict(flax_model.params["""batch_stats"""] ) ) else: A__ = flax_model.params A__ = flatten_dict(__a ) A__ = (model_prefix not in flax_model_params) and ( model_prefix in {k.split(""".""" )[0] for k in pt_state_dict.keys()} ) A__ = (model_prefix in flax_model_params) and ( model_prefix not in {k.split(""".""" )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): A__ = tuple(pt_key.split(""".""" ) ) # remove base model prefix if necessary A__ = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: A__ = pt_tuple_key[1:] # Correctly rename weight parameters A__ , A__ = rename_key_and_reshape_tensor( __a , __a , __a , __a ) # add model prefix if necessary A__ = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: A__ = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F'PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape ' F'{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1]: A__ = jnp.asarray(__a ) continue if "var" in flax_key[-1]: A__ = jnp.asarray(__a ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(__a , __a ) continue # also add unexpected weight so that warning is thrown A__ = jnp.asarray(__a ) else: # also add unexpected weight so that warning is thrown A__ = jnp.asarray(__a ) return unflatten_dict(__a ) def __lowerCamelCase ( __a :Union[str, Any] , __a :Dict ) -> List[Any]: """simple docstring""" A__ = os.path.abspath(__a ) logger.info(F'Loading Flax weights from {flax_checkpoint_path}' ) # import correct flax class A__ = getattr(__a , """Flax""" + model.__class__.__name__ ) # load flax weight dict with open(__a , """rb""" ) as state_f: try: A__ = from_bytes(__a , state_f.read() ) except UnpicklingError: raise EnvironmentError(F'Unable to convert {flax_checkpoint_path} to Flax deserializable object. ' ) return load_flax_weights_in_pytorch_model(__a , __a ) def __lowerCamelCase ( __a :str , __a :Tuple ) -> int: """simple docstring""" try: import torch # noqa: F401 except ImportError: logger.error( """Loading a 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__ = flatten_dict(jax.tree_util.tree_map(lambda __a : x.dtype == jnp.bfloataa , __a ) ).values() if any(__a ): # convert all weights to fp32 if the 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__ = jax.tree_util.tree_map( lambda __a : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , __a ) A__ = flatten_dict(__a ) A__ = pt_model.state_dict() A__ = (pt_model.base_model_prefix in flax_state) and ( pt_model.base_model_prefix not in {k.split(""".""" )[0] for k in pt_model_dict.keys()} ) A__ = (pt_model.base_model_prefix not in flax_state) and ( pt_model.base_model_prefix in {k.split(""".""" )[0] for k in pt_model_dict.keys()} ) # keep track of unexpected & missing keys A__ = [] A__ = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): A__ = flax_key_tuple[0] == pt_model.base_model_prefix A__ = """.""".join((pt_model.base_model_prefix,) + flax_key_tuple ) in pt_model_dict # adapt flax_key to prepare for loading from/to base model only if load_model_with_head_into_base_model and has_base_model_prefix: A__ = flax_key_tuple[1:] elif load_base_model_into_model_with_head and require_base_model_prefix: A__ = (pt_model.base_model_prefix,) + flax_key_tuple # rename flax weights to PyTorch format if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 4 and ".".join(__a ) not in pt_model_dict: # conv layer A__ = flax_key_tuple[:-1] + ("""weight""",) A__ = jnp.transpose(__a , (3, 2, 0, 1) ) elif flax_key_tuple[-1] == "kernel" and ".".join(__a ) not in pt_model_dict: # linear layer A__ = flax_key_tuple[:-1] + ("""weight""",) A__ = flax_tensor.T elif flax_key_tuple[-1] in ["scale", "embedding"]: A__ = flax_key_tuple[:-1] + ("""weight""",) # adding batch stats from flax batch norm to pt elif "mean" in flax_key_tuple[-1]: A__ = flax_key_tuple[:-1] + ("""running_mean""",) elif "var" in flax_key_tuple[-1]: A__ = flax_key_tuple[:-1] + ("""running_var""",) if "batch_stats" in flax_state: A__ = """.""".join(flax_key_tuple[1:] ) # Remove the params/batch_stats header else: A__ = """.""".join(__a ) # We also need to look at `pt_model_dict` and see if there are keys requiring further transformation. A__ = {} # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 for key in pt_model_dict: A__ = key.split(""".""" ) A__ = None if key_components[-3::2] == ["parametrizations", "original0"]: A__ = key_components[-2] + """_g""" elif key_components[-3::2] == ["parametrizations", "original1"]: A__ = key_components[-2] + """_v""" if name is not None: A__ = key_components[:-3] + [name] A__ = """.""".join(__a ) A__ = key if flax_key in special_pt_names: A__ = special_pt_names[flax_key] 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__ = np.asarray(__a ) if not isinstance(__a , np.ndarray ) else flax_tensor A__ = torch.from_numpy(__a ) # remove from missing keys missing_keys.remove(__a ) else: # weight is not expected by PyTorch model unexpected_keys.append(__a ) pt_model.load_state_dict(__a ) # re-transform missing_keys to list A__ = list(__a ) if len(__a ) > 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).""" ) else: logger.warning(F'All Flax model weights were used when initializing {pt_model.__class__.__name__}.\n' ) if len(__a ) > 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.""" ) else: logger.warning( F'All the weights of {pt_model.__class__.__name__} were initialized from the Flax model.\n' """If your task is similar to the task the model of the checkpoint was trained on, """ F'you can already use {pt_model.__class__.__name__} for predictions without further training.' ) return pt_model
274
# This code is adapted from OpenAI's release # https://github.com/openai/human-eval/blob/master/human_eval/execution.py import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def __lowerCamelCase ( __a :List[str] , __a :List[Any] , __a :Union[str, Any] , __a :List[Any] ) -> Dict: """simple docstring""" A__ = multiprocessing.Manager() A__ = manager.list() A__ = multiprocessing.Process(target=__a , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append("""timed out""" ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def __lowerCamelCase ( __a :Optional[Any] , __a :Any , __a :List[Any] ) -> Union[str, Any]: """simple docstring""" with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil A__ = shutil.rmtree A__ = os.rmdir A__ = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: A__ = {} with swallow_io(): with time_limit(__a ): exec(__a , __a ) result.append("""passed""" ) except TimeoutException: result.append("""timed out""" ) except BaseException as e: result.append(F'failed: {e}' ) # Needed for cleaning up. A__ = rmtree A__ = rmdir A__ = chdir @contextlib.contextmanager def __lowerCamelCase ( __a :List[str] ) -> Dict: """simple docstring""" def signal_handler(__a :List[Any] , __a :Optional[Any] ): raise TimeoutException("""Timed out!""" ) signal.setitimer(signal.ITIMER_REAL , __a ) signal.signal(signal.SIGALRM , __a ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = WriteOnlyStringIO() with contextlib.redirect_stdout(__a ): with contextlib.redirect_stderr(__a ): with redirect_stdin(__a ): yield @contextlib.contextmanager def __lowerCamelCase ( ) -> Dict: """simple docstring""" with tempfile.TemporaryDirectory() as dirname: with chdir(__a ): yield dirname class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (io.StringIO ): '''simple docstring''' def a_ ( self : Any , *__lowerCAmelCase : List[str] , **__lowerCAmelCase : str ) -> Dict: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Optional[int] ) -> str: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Any ) -> int: """simple docstring""" raise OSError def a_ ( self : str , *__lowerCAmelCase : Any , **__lowerCAmelCase : Union[str, Any] ) -> int: """simple docstring""" return False class A (contextlib._RedirectStream ): # type: ignore '''simple docstring''' __lowerCamelCase : Union[str, Any] = '''stdin''' @contextlib.contextmanager def __lowerCamelCase ( __a :Union[str, Any] ) -> List[str]: """simple docstring""" if root == ".": yield return A__ = os.getcwd() os.chdir(__a ) try: yield except BaseException as exc: raise exc finally: os.chdir(__a ) def __lowerCamelCase ( __a :Union[str, Any]=None ) -> Dict: """simple docstring""" if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins A__ = None A__ = None import os A__ = """1""" A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None import shutil A__ = None A__ = None A__ = None import subprocess A__ = None # type: ignore A__ = None import sys A__ = None A__ = None A__ = None A__ = None A__ = None
274
1
from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time A : Dict = Lock() def __lowerCamelCase ( __a :Dict , __a :List[str] , __a :Optional[int] , __a :Optional[int] , __a :Optional[Any] , __a :Optional[int] , __a :int ) -> Dict: """simple docstring""" global process_lock # we perform n swaps since after n swaps we know we are sorted # we *could* stop early if we are sorted already, but it takes as long to # find out we are sorted as it does to sort the list with this algorithm for i in range(0 , 1_0 ): if (i + position) % 2 == 0 and r_send is not None: # send your value to your right neighbor process_lock.acquire() r_send[1].send(__a ) process_lock.release() # receive your right neighbor's value process_lock.acquire() A__ = rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left A__ = min(__a , __a ) elif (i + position) % 2 != 0 and l_send is not None: # send your value to your left neighbor process_lock.acquire() l_send[1].send(__a ) process_lock.release() # receive your left neighbor's value process_lock.acquire() A__ = lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right A__ = max(__a , __a ) # after all swaps are performed, send the values back to main result_pipe[1].send(__a ) def __lowerCamelCase ( __a :List[str] ) -> int: """simple docstring""" A__ = [] A__ = [] # initialize the list of pipes where the values will be retrieved for _ in arr: result_pipe.append(Pipe() ) # creates the processes # the first and last process only have one neighbor so they are made outside # of the loop A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) A__ = temp_rs A__ = temp_rr for i in range(1 , len(__a ) - 1 ): A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) A__ = temp_rs A__ = temp_rr process_array_.append( Process( target=__a , args=( len(__a ) - 1, arr[len(__a ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(__a ) - 1], ) , ) ) # start the processes for p in process_array_: p.start() # wait for the processes to end and write their values to the list for p in range(0 , len(__a ) ): A__ = result_pipe[p][0].recv() process_array_[p].join() return arr def __lowerCamelCase ( ) -> str: """simple docstring""" A__ = list(range(1_0 , 0 , -1 ) ) print("""Initial List""" ) print(*__a ) A__ = odd_even_transposition(__a ) print("""Sorted List\n""" ) print(*__a ) if __name__ == "__main__": main()
274
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_albert import AlbertTokenizer else: A : Tuple = None A : Optional[Any] = logging.get_logger(__name__) A : Tuple = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A : List[str] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json''', }, } A : List[str] = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } A : Optional[int] = '''▁''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : str = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = AlbertTokenizer def __init__( self : Tuple , __lowerCAmelCase : Tuple=None , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : Optional[Any]=True , __lowerCAmelCase : List[Any]=True , __lowerCAmelCase : str=False , __lowerCAmelCase : Union[str, Any]="[CLS]" , __lowerCAmelCase : int="[SEP]" , __lowerCAmelCase : Dict="<unk>" , __lowerCAmelCase : Dict="[SEP]" , __lowerCAmelCase : Union[str, Any]="<pad>" , __lowerCAmelCase : str="[CLS]" , __lowerCAmelCase : int="[MASK]" , **__lowerCAmelCase : Optional[Any] , ) -> Optional[Any]: """simple docstring""" A__ = ( AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase , normalized=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token ) super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def a_ ( self : List[Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a_ ( self : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(__lowerCAmelCase ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return A__ = os.path.join( __lowerCAmelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCAmelCase ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
274
1
import argparse import gc import json import os import shutil import warnings import torch from transformers import LlamaConfig, LlamaForCausalLM, LlamaTokenizer try: from transformers import LlamaTokenizerFast except ImportError as e: warnings.warn(e) warnings.warn( '''The converted tokenizer will be the `slow` tokenizer. To use the fast, update your `tokenizers` library and re-run the tokenizer conversion''' ) A : Union[str, Any] = None A : List[Any] = { '''7B''': 1_1_0_0_8, '''13B''': 1_3_8_2_4, '''30B''': 1_7_9_2_0, '''65B''': 2_2_0_1_6, '''70B''': 2_8_6_7_2, } A : List[Any] = { '''7B''': 1, '''7Bf''': 1, '''13B''': 2, '''13Bf''': 2, '''30B''': 4, '''65B''': 8, '''70B''': 8, '''70Bf''': 8, } def __lowerCamelCase ( __a :List[Any] , __a :List[Any]=1 , __a :Union[str, Any]=2_5_6 ) -> Any: """simple docstring""" return multiple_of * ((int(ffn_dim_multiplier * int(8 * n / 3 ) ) + multiple_of - 1) // multiple_of) def __lowerCamelCase ( __a :Optional[Any] ) -> List[str]: """simple docstring""" with open(__a , """r""" ) as f: return json.load(__a ) def __lowerCamelCase ( __a :Union[str, Any] , __a :Any ) -> Union[str, Any]: """simple docstring""" with open(__a , """w""" ) as f: json.dump(__a , __a ) def __lowerCamelCase ( __a :List[Any] , __a :Dict , __a :List[str] , __a :int=True ) -> str: """simple docstring""" os.makedirs(__a , exist_ok=__a ) A__ = os.path.join(__a , """tmp""" ) os.makedirs(__a , exist_ok=__a ) A__ = read_json(os.path.join(__a , """params.json""" ) ) A__ = NUM_SHARDS[model_size] A__ = params["""n_layers"""] A__ = params["""n_heads"""] A__ = n_heads // num_shards A__ = params["""dim"""] A__ = dim // n_heads A__ = 10000.0 A__ = 1.0 / (base ** (torch.arange(0 , __a , 2 ).float() / dims_per_head)) if "n_kv_heads" in params: A__ = params["""n_kv_heads"""] # for GQA / MQA A__ = n_heads_per_shard // num_key_value_heads A__ = dim // num_key_value_heads else: # compatibility with other checkpoints A__ = n_heads A__ = n_heads_per_shard A__ = dim # permute for sliced rotary def permute(__a :List[Any] , __a :Tuple=n_heads , __a :int=dim , __a :int=dim ): return w.view(__a , dima // n_heads // 2 , 2 , __a ).transpose(1 , 2 ).reshape(__a , __a ) print(F'Fetching all parameters from the checkpoint at {input_base_path}.' ) # Load weights if model_size == "7B": # Not sharded # (The sharded implementation would also work, but this is simpler.) A__ = torch.load(os.path.join(__a , """consolidated.00.pth""" ) , map_location="""cpu""" ) else: # Sharded A__ = [ torch.load(os.path.join(__a , F'consolidated.{i:02d}.pth' ) , map_location="""cpu""" ) for i in range(__a ) ] A__ = 0 A__ = {"""weight_map""": {}} for layer_i in range(__a ): A__ = F'pytorch_model-{layer_i + 1}-of-{n_layers + 1}.bin' if model_size == "7B": # Unsharded A__ = { F'model.layers.{layer_i}.self_attn.q_proj.weight': permute( loaded[F'layers.{layer_i}.attention.wq.weight'] ), F'model.layers.{layer_i}.self_attn.k_proj.weight': permute( loaded[F'layers.{layer_i}.attention.wk.weight'] ), F'model.layers.{layer_i}.self_attn.v_proj.weight': loaded[F'layers.{layer_i}.attention.wv.weight'], F'model.layers.{layer_i}.self_attn.o_proj.weight': loaded[F'layers.{layer_i}.attention.wo.weight'], F'model.layers.{layer_i}.mlp.gate_proj.weight': loaded[F'layers.{layer_i}.feed_forward.w1.weight'], F'model.layers.{layer_i}.mlp.down_proj.weight': loaded[F'layers.{layer_i}.feed_forward.w2.weight'], F'model.layers.{layer_i}.mlp.up_proj.weight': loaded[F'layers.{layer_i}.feed_forward.w3.weight'], F'model.layers.{layer_i}.input_layernorm.weight': loaded[F'layers.{layer_i}.attention_norm.weight'], F'model.layers.{layer_i}.post_attention_layernorm.weight': loaded[F'layers.{layer_i}.ffn_norm.weight'], } else: # Sharded # Note that attention.w{q,k,v,o}, feed_fordward.w[1,2,3], attention_norm.weight and ffn_norm.weight share # the same storage object, saving attention_norm and ffn_norm will save other weights too, which is # redundant as other weights will be stitched from multiple shards. To avoid that, they are cloned. A__ = { F'model.layers.{layer_i}.input_layernorm.weight': loaded[0][ F'layers.{layer_i}.attention_norm.weight' ].clone(), F'model.layers.{layer_i}.post_attention_layernorm.weight': loaded[0][ F'layers.{layer_i}.ffn_norm.weight' ].clone(), } A__ = permute( torch.cat( [ loaded[i][F'layers.{layer_i}.attention.wq.weight'].view(__a , __a , __a ) for i in range(__a ) ] , dim=0 , ).reshape(__a , __a ) ) A__ = permute( torch.cat( [ loaded[i][F'layers.{layer_i}.attention.wk.weight'].view( __a , __a , __a ) for i in range(__a ) ] , dim=0 , ).reshape(__a , __a ) , __a , __a , __a , ) A__ = torch.cat( [ loaded[i][F'layers.{layer_i}.attention.wv.weight'].view( __a , __a , __a ) for i in range(__a ) ] , dim=0 , ).reshape(__a , __a ) A__ = torch.cat( [loaded[i][F'layers.{layer_i}.attention.wo.weight'] for i in range(__a )] , dim=1 ) A__ = torch.cat( [loaded[i][F'layers.{layer_i}.feed_forward.w1.weight'] for i in range(__a )] , dim=0 ) A__ = torch.cat( [loaded[i][F'layers.{layer_i}.feed_forward.w2.weight'] for i in range(__a )] , dim=1 ) A__ = torch.cat( [loaded[i][F'layers.{layer_i}.feed_forward.w3.weight'] for i in range(__a )] , dim=0 ) A__ = inv_freq for k, v in state_dict.items(): A__ = filename param_count += v.numel() torch.save(__a , os.path.join(__a , __a ) ) A__ = F'pytorch_model-{n_layers + 1}-of-{n_layers + 1}.bin' if model_size == "7B": # Unsharded A__ = { """model.embed_tokens.weight""": loaded["""tok_embeddings.weight"""], """model.norm.weight""": loaded["""norm.weight"""], """lm_head.weight""": loaded["""output.weight"""], } else: A__ = { """model.norm.weight""": loaded[0]["""norm.weight"""], """model.embed_tokens.weight""": torch.cat( [loaded[i]["""tok_embeddings.weight"""] for i in range(__a )] , dim=1 ), """lm_head.weight""": torch.cat([loaded[i]["""output.weight"""] for i in range(__a )] , dim=0 ), } for k, v in state_dict.items(): A__ = filename param_count += v.numel() torch.save(__a , os.path.join(__a , __a ) ) # Write configs A__ = {"""total_size""": param_count * 2} write_json(__a , os.path.join(__a , """pytorch_model.bin.index.json""" ) ) A__ = params["""ffn_dim_multiplier"""] if """ffn_dim_multiplier""" in params else 1 A__ = params["""multiple_of"""] if """multiple_of""" in params else 2_5_6 A__ = LlamaConfig( hidden_size=__a , intermediate_size=compute_intermediate_size(__a , __a , __a ) , num_attention_heads=params["""n_heads"""] , num_hidden_layers=params["""n_layers"""] , rms_norm_eps=params["""norm_eps"""] , num_key_value_heads=__a , ) config.save_pretrained(__a ) # Make space so we can load the model properly now. del state_dict del loaded gc.collect() print("""Loading the checkpoint in a Llama model.""" ) A__ = LlamaForCausalLM.from_pretrained(__a , torch_dtype=torch.floataa , low_cpu_mem_usage=__a ) # Avoid saving this as part of the config. del model.config._name_or_path print("""Saving in the Transformers format.""" ) model.save_pretrained(__a , safe_serialization=__a ) shutil.rmtree(__a ) def __lowerCamelCase ( __a :Optional[int] , __a :List[str] ) -> Optional[Any]: """simple docstring""" A__ = LlamaTokenizer if LlamaTokenizerFast is None else LlamaTokenizerFast print(F'Saving a {tokenizer_class.__name__} to {tokenizer_path}.' ) A__ = tokenizer_class(__a ) tokenizer.save_pretrained(__a ) def __lowerCamelCase ( ) -> Optional[int]: """simple docstring""" A__ = argparse.ArgumentParser() parser.add_argument( """--input_dir""" , help="""Location of LLaMA weights, which contains tokenizer.model and model folders""" , ) parser.add_argument( """--model_size""" , choices=["""7B""", """7Bf""", """13B""", """13Bf""", """30B""", """65B""", """70B""", """70Bf""", """tokenizer_only"""] , ) parser.add_argument( """--output_dir""" , help="""Location to write HF model and tokenizer""" , ) parser.add_argument("""--safe_serialization""" , type=__a , help="""Whether or not to save using `safetensors`.""" ) A__ = parser.parse_args() if args.model_size != "tokenizer_only": write_model( model_path=args.output_dir , input_base_path=os.path.join(args.input_dir , args.model_size ) , model_size=args.model_size , safe_serialization=args.safe_serialization , ) A__ = os.path.join(args.input_dir , """tokenizer.model""" ) write_tokenizer(args.output_dir , __a ) if __name__ == "__main__": main()
274
import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging A : Dict = logging.get_logger(__name__) def __lowerCamelCase ( __a :int=None , __a :Optional[Any]=None ) -> int: """simple docstring""" return field(default_factory=lambda: default , metadata=__a ) @dataclass class A : '''simple docstring''' __lowerCamelCase : List[str] = list_field( default=[] , metadata={ '''help''': ( '''Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version''' ''' of all available models''' ) } , ) __lowerCamelCase : List[int] = list_field( default=[8] , metadata={'''help''': '''List of batch sizes for which memory and time performance will be evaluated'''} ) __lowerCamelCase : List[int] = list_field( default=[8, 32, 128, 512] , metadata={'''help''': '''List of sequence lengths for which memory and time performance will be evaluated'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to benchmark inference of model. Inference can be disabled via --no-inference.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available cuda devices. Cuda can be disabled via --no-cuda.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available tpu devices. TPU can be disabled via --no-tpu.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Use FP16 to accelerate inference.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Benchmark training of model'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Verbose memory tracing'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to perform speed measurements. Speed measurements can be disabled via --no-speed.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': '''Whether to perform memory measurements. Memory measurements can be disabled via --no-memory''' } , ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Trace memory line by line'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save result to a CSV file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save all print statements in a log file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to print environment information'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use''' ''' multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled''' ''' for debugging / testing and on TPU.''' ) } , ) __lowerCamelCase : str = field( default=F'''inference_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''train_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''train_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''env_info_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving environment information.'''} , ) __lowerCamelCase : str = field( default=F'''log_{round(time() )}.csv''' , metadata={'''help''': '''Log filename used if print statements are saved in log.'''} , ) __lowerCamelCase : int = field(default=3 , metadata={'''help''': '''Times an experiment will be run.'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain''' ''' model weights.''' ) } , ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" warnings.warn( f'The class {self.__class__} is deprecated. Hugging Face Benchmarking utils' """ are deprecated in general and it is advised to use external Benchmarking libraries """ """ to benchmark Transformer models.""" , __lowerCAmelCase , ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def a_ ( self : Tuple ) -> List[str]: """simple docstring""" if len(self.models ) <= 0: raise ValueError( """Please make sure you provide at least one model name / model identifier, *e.g.* `--models""" """ bert-base-cased` or `args.models = ['bert-base-cased'].""" ) return self.models @property def a_ ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" if not self.multi_process: return False elif self.is_tpu: logger.info("""Multiprocessing is currently not possible on TPU.""" ) return False else: return True
274
1
import argparse import logging import os import datasets import tensorflow as tf from transformers import AutoTokenizer A : Union[str, Any] = logging.getLogger(__name__) def __lowerCamelCase ( ) -> Tuple: """simple docstring""" A__ = argparse.ArgumentParser( description="""Prepare TFRecord shards from pre-tokenized samples of the wikitext dataset.""" ) parser.add_argument( """--dataset_name""" , type=__a , default="""wikitext""" , help="""Name of the training. Explore datasets at: hf.co/datasets.""" , ) parser.add_argument( """--dataset_config""" , type=__a , default="""wikitext-103-raw-v1""" , help="""Configuration name of the dataset.""" ) parser.add_argument( """--tokenizer_name_or_path""" , type=__a , default="""sayakpaul/unigram-tokenizer-wikitext""" , help="""Tokenizer identifier. Can be a local filepath or a Hub identifier.""" , ) parser.add_argument( """--shard_size""" , type=__a , default=1_0_0_0 , help="""Number of entries to go in a single shard.""" , ) parser.add_argument("""--split""" , type=__a , default="""train""" , choices=["""train""", """test""", """validation"""] ) parser.add_argument( """--limit""" , default=__a , type=__a , help="""Limit the number of shards (used for debugging).""" , ) parser.add_argument( """--max_length""" , type=__a , default=5_1_2 , help="""Maximum sequence length. For training on TPUs, it helps to have a maximum""" """ sequence length that is a multiple of 8.""" , ) parser.add_argument( """--output_dir""" , default="""tf-tpu""" , type=__a , help="""Output directory where the TFRecord shards will be saved. If the""" """ path is appended with `gs://` ('gs://tf-tpu', for example) then the TFRecord""" """ shards will be directly saved to a Google Cloud Storage bucket.""" , ) A__ = parser.parse_args() return args def __lowerCamelCase ( __a :List[str] ) -> Any: """simple docstring""" def fn(__a :Optional[int] ): return tokenizer(examples["""text"""] ) return fn def __lowerCamelCase ( __a :str ) -> Any: """simple docstring""" A__ = [] for i in range(len(tokenized_data["""input_ids"""] ) ): A__ = { """input_ids""": tf.train.Feature(intaa_list=tf.train.IntaaList(value=tokenized_data["""input_ids"""][i] ) ), """attention_mask""": tf.train.Feature( intaa_list=tf.train.IntaaList(value=tokenized_data["""attention_mask"""][i] ) ), } A__ = tf.train.Features(feature=__a ) A__ = tf.train.Example(features=__a ) A__ = example.SerializeToString() records.append(__a ) return records def __lowerCamelCase ( __a :str ) -> Tuple: """simple docstring""" A__ = datasets.load_dataset(args.dataset_name , args.dataset_config , split=args.split ) if args.limit is not None: A__ = min(len(__a ) , args.limit ) A__ = dataset.select(range(__a ) ) print(F'Limiting the dataset to {args.limit} entries.' ) A__ = AutoTokenizer.from_pretrained(args.tokenizer_name_or_path ) # Handle output directory creation. # For serializing into a Google Cloud Storage Bucket, one needs to first # create a bucket. if "gs" not in args.output_dir: if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) A__ = os.path.join(args.output_dir , args.split ) if not os.path.exists(__a ): os.makedirs(__a ) else: A__ = os.path.join(args.output_dir , args.split ) # Tokenize the whole dataset at once. A__ = tokenize_function(__a ) A__ = dataset.map(__a , batched=__a , num_proc=4 , remove_columns=["""text"""] ) # We need to concatenate all our texts together, and then split the result # into chunks of a fixed size, which we will call block_size. To do this, we # will use the map method again, with the option batched=True. When we use batched=True, # the function we pass to map() will be passed multiple inputs at once, allowing us # to group them into more or fewer examples than we had in the input. # This allows us to create our new fixed-length samples. The advantage of this # method is that we don't lose a whole lot of content from the dataset compared to the # case where we simply tokenize with a pre-defined max_length. def group_texts(__a :str ): # Concatenate all texts. A__ = {k: sum(examples[k] , [] ) for k in examples.keys()} A__ = len(concatenated_examples[list(examples.keys() )[0]] ) # We drop the small remainder, though you could add padding instead if the model supports it # In this, as in all things, we advise you to follow your heart 🫀 A__ = (total_length // args.max_length) * args.max_length # Split by chunks of max_len. A__ = { k: [t[i : i + args.max_length] for i in range(0 , __a , args.max_length )] for k, t in concatenated_examples.items() } return result A__ = dataset_tokenized.map(__a , batched=__a , batch_size=1_0_0_0 , num_proc=4 ) A__ = 0 A__ = 0 for shard in range(0 , len(__a ) , args.shard_size ): A__ = grouped_dataset[shard : shard + args.shard_size] A__ = len(dataset_snapshot["""input_ids"""] ) A__ = os.path.join(__a , F'dataset-{shard_count}-{records_containing}.tfrecord' ) A__ = get_serialized_examples(__a ) with tf.io.TFRecordWriter(__a ) as out_file: for i in range(len(__a ) ): A__ = serialized_examples[i] out_file.write(__a ) print("""Wrote file {} containing {} records""".format(__a , __a ) ) shard_count += 1 total_records += records_containing with open(F'split-{args.split}-records-count.txt' , """w""" ) as f: print(F'Total {args.split} records: {total_records}' , file=__a ) if __name__ == "__main__": A : str = parse_args() main(args)
274
from math import ceil def __lowerCamelCase ( __a :int = 1_0_0_1 ) -> int: """simple docstring""" A__ = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): A__ = 2 * i + 1 A__ = 2 * i A__ = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: A : List[str] = int(sys.argv[1]) print(solution(n)) except ValueError: print('''Invalid entry - please enter a number''')
274
1
import math from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import SchedulerMixin, SchedulerOutput class A (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : str = 1 @register_to_config def __init__( self : List[Any] , __lowerCAmelCase : int = 10_00 , __lowerCAmelCase : Optional[Union[np.ndarray, List[float]]] = None ) -> Any: """simple docstring""" self.set_timesteps(__lowerCAmelCase ) # standard deviation of the initial noise distribution A__ = 1.0 # For now we only support F-PNDM, i.e. the runge-kutta method # For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf # mainly at formula (9), (12), (13) and the Algorithm 2. A__ = 4 # running values A__ = [] def a_ ( self : Optional[int] , __lowerCAmelCase : int , __lowerCAmelCase : Union[str, torch.device] = None ) -> Optional[int]: """simple docstring""" A__ = num_inference_steps A__ = torch.linspace(1 , 0 , num_inference_steps + 1 )[:-1] A__ = torch.cat([steps, torch.tensor([0.0] )] ) if self.config.trained_betas is not None: A__ = torch.tensor(self.config.trained_betas , dtype=torch.floataa ) else: A__ = torch.sin(steps * math.pi / 2 ) ** 2 A__ = (1.0 - self.betas**2) ** 0.5 A__ = (torch.atana(self.betas , self.alphas ) / math.pi * 2)[:-1] A__ = timesteps.to(__lowerCAmelCase ) A__ = [] def a_ ( self : str , __lowerCAmelCase : torch.FloatTensor , __lowerCAmelCase : int , __lowerCAmelCase : torch.FloatTensor , __lowerCAmelCase : bool = True , ) -> Union[SchedulerOutput, Tuple]: """simple docstring""" if self.num_inference_steps is None: raise ValueError( """Number of inference steps is 'None', you need to run 'set_timesteps' after creating the scheduler""" ) A__ = (self.timesteps == timestep).nonzero().item() A__ = timestep_index + 1 A__ = sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index] self.ets.append(__lowerCAmelCase ) if len(self.ets ) == 1: A__ = self.ets[-1] elif len(self.ets ) == 2: A__ = (3 * self.ets[-1] - self.ets[-2]) / 2 elif len(self.ets ) == 3: A__ = (23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12 else: A__ = (1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4]) A__ = self._get_prev_sample(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=__lowerCAmelCase ) def a_ ( self : Optional[Any] , __lowerCAmelCase : torch.FloatTensor , *__lowerCAmelCase : int , **__lowerCAmelCase : Dict ) -> torch.FloatTensor: """simple docstring""" return sample def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : int ) -> List[Any]: """simple docstring""" A__ = self.alphas[timestep_index] A__ = self.betas[timestep_index] A__ = self.alphas[prev_timestep_index] A__ = self.betas[prev_timestep_index] A__ = (sample - sigma * ets) / max(__lowerCAmelCase , 1e-8 ) A__ = next_alpha * pred + ets * next_sigma return prev_sample def __len__( self : str ) -> List[Any]: """simple docstring""" return self.config.num_train_timesteps
274
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A : Tuple = logging.getLogger(__name__) def __lowerCamelCase ( __a :Optional[int] , __a :List[str] ) -> Tuple: """simple docstring""" A__ = np.argmax(__a , axis=1 ) return np.sum(outputs == labels ) def __lowerCamelCase ( __a :Tuple ) -> Dict: """simple docstring""" with open(__a , encoding="""utf_8""" ) as f: A__ = csv.reader(__a ) A__ = [] next(__a ) # skip the first line for line in tqdm(__a ): output.append((""" """.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def __lowerCamelCase ( __a :Optional[int] , __a :List[Any] , __a :Dict , __a :Optional[Any] , __a :Optional[Any] , __a :int ) -> Union[str, Any]: """simple docstring""" A__ = [] for dataset in encoded_datasets: A__ = len(__a ) A__ = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) A__ = np.zeros((n_batch, 2) , dtype=np.intaa ) A__ = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) A__ = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(__a ): A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = with_conta A__ = with_conta A__ = len(__a ) - 1 A__ = len(__a ) - 1 A__ = with_conta A__ = with_conta A__ = mc_label A__ = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(__a ) for t in all_inputs ) ) return tensor_datasets def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = argparse.ArgumentParser() parser.add_argument("""--model_name""" , type=__a , default="""openai-gpt""" , help="""pretrained model name""" ) parser.add_argument("""--do_train""" , action="""store_true""" , help="""Whether to run training.""" ) parser.add_argument("""--do_eval""" , action="""store_true""" , help="""Whether to run eval on the dev set.""" ) parser.add_argument( """--output_dir""" , default=__a , type=__a , required=__a , help="""The output directory where the model predictions and checkpoints will be written.""" , ) parser.add_argument("""--train_dataset""" , type=__a , default="""""" ) parser.add_argument("""--eval_dataset""" , type=__a , default="""""" ) parser.add_argument("""--seed""" , type=__a , default=4_2 ) parser.add_argument("""--num_train_epochs""" , type=__a , default=3 ) parser.add_argument("""--train_batch_size""" , type=__a , default=8 ) parser.add_argument("""--eval_batch_size""" , type=__a , default=1_6 ) parser.add_argument("""--adam_epsilon""" , default=1E-8 , type=__a , help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--max_grad_norm""" , type=__a , default=1 ) parser.add_argument( """--max_steps""" , default=-1 , type=__a , help=( """If > 0: set total number of training steps to perform. Override num_train_epochs.""" ) , ) parser.add_argument( """--gradient_accumulation_steps""" , type=__a , default=1 , help="""Number of updates steps to accumulate before performing a backward/update pass.""" , ) parser.add_argument("""--learning_rate""" , type=__a , default=6.25E-5 ) parser.add_argument("""--warmup_steps""" , default=0 , type=__a , help="""Linear warmup over warmup_steps.""" ) parser.add_argument("""--lr_schedule""" , type=__a , default="""warmup_linear""" ) parser.add_argument("""--weight_decay""" , type=__a , default=0.01 ) parser.add_argument("""--lm_coef""" , type=__a , default=0.9 ) parser.add_argument("""--n_valid""" , type=__a , default=3_7_4 ) parser.add_argument("""--server_ip""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) parser.add_argument("""--server_port""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) A__ = parser.parse_args() print(__a ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("""Waiting for debugger attach""" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__a ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) A__ = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) A__ = torch.cuda.device_count() logger.info("""device: {}, n_gpu {}""".format(__a , __a ) ) if not args.do_train and not args.do_eval: raise ValueError("""At least one of `do_train` or `do_eval` must be True.""" ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset A__ = ["""_start_""", """_delimiter_""", """_classify_"""] A__ = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(__a ) A__ = tokenizer.convert_tokens_to_ids(__a ) A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(__a ) ) model.to(__a ) # Load and encode the datasets def tokenize_and_encode(__a :Tuple ): if isinstance(__a , __a ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(__a ) ) elif isinstance(__a , __a ): return obj return [tokenize_and_encode(__a ) for o in obj] logger.info("""Encoding dataset...""" ) A__ = load_rocstories_dataset(args.train_dataset ) A__ = load_rocstories_dataset(args.eval_dataset ) A__ = (train_dataset, eval_dataset) A__ = tokenize_and_encode(__a ) # Compute the max input length for the Transformer A__ = model.config.n_positions // 2 - 2 A__ = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) A__ = min(__a , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders A__ = pre_process_datasets(__a , __a , __a , *__a ) A__ , A__ = tensor_datasets[0], tensor_datasets[1] A__ = TensorDataset(*__a ) A__ = RandomSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.train_batch_size ) A__ = TensorDataset(*__a ) A__ = SequentialSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: A__ = args.max_steps A__ = args.max_steps // (len(__a ) // args.gradient_accumulation_steps) + 1 else: A__ = len(__a ) // args.gradient_accumulation_steps * args.num_train_epochs A__ = list(model.named_parameters() ) A__ = ["""bias""", """LayerNorm.bias""", """LayerNorm.weight"""] A__ = [ { """params""": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], """weight_decay""": args.weight_decay, }, {"""params""": [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], """weight_decay""": 0.0}, ] A__ = AdamW(__a , lr=args.learning_rate , eps=args.adam_epsilon ) A__ = get_linear_schedule_with_warmup( __a , num_warmup_steps=args.warmup_steps , num_training_steps=__a ) if args.do_train: A__ , A__ , A__ = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="""Epoch""" ): A__ = 0 A__ = 0 A__ = tqdm(__a , desc="""Training""" ) for step, batch in enumerate(__a ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch A__ = model(__a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() A__ = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 A__ = """Training loss: {:.2e} lr: {:.2e}""".format(__a , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer A__ = model.module if hasattr(__a , """module""" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` A__ = os.path.join(args.output_dir , __a ) A__ = os.path.join(args.output_dir , __a ) torch.save(model_to_save.state_dict() , __a ) model_to_save.config.to_json_file(__a ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) A__ = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(__a ) if args.do_eval: model.eval() A__ , A__ = 0, 0 A__ , A__ = 0, 0 for batch in tqdm(__a , desc="""Evaluating""" ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch with torch.no_grad(): A__ , A__ , A__ , A__ = model( __a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = mc_logits.detach().cpu().numpy() A__ = mc_labels.to("""cpu""" ).numpy() A__ = accuracy(__a , __a ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 A__ = eval_loss / nb_eval_steps A__ = eval_accuracy / nb_eval_examples A__ = tr_loss / nb_tr_steps if args.do_train else None A__ = {"""eval_loss""": eval_loss, """eval_accuracy""": eval_accuracy, """train_loss""": train_loss} A__ = os.path.join(args.output_dir , """eval_results.txt""" ) with open(__a , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key in sorted(result.keys() ): logger.info(""" %s = %s""" , __a , str(result[key] ) ) writer.write("""%s = %s\n""" % (key, str(result[key] )) ) if __name__ == "__main__": main()
274
1
import inspect import unittest import numpy as np from tests.test_modeling_common import floats_tensor from transformers import MaskaFormerConfig, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaskaFormerForUniversalSegmentation, MaskaFormerModel if is_vision_available(): from transformers import MaskaFormerImageProcessor if is_vision_available(): from PIL import Image class A : '''simple docstring''' def __init__( self : Dict , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : List[str]=2 , __lowerCAmelCase : str=True , __lowerCAmelCase : List[Any]=False , __lowerCAmelCase : str=10 , __lowerCAmelCase : List[str]=3 , __lowerCAmelCase : List[Any]=32 * 8 , __lowerCAmelCase : str=32 * 8 , __lowerCAmelCase : List[Any]=4 , __lowerCAmelCase : Optional[Any]=64 , ) -> int: """simple docstring""" A__ = parent A__ = batch_size A__ = is_training A__ = use_auxiliary_loss A__ = num_queries A__ = num_channels A__ = min_size A__ = max_size A__ = num_labels A__ = hidden_dim A__ = hidden_dim def a_ ( self : List[str] ) -> int: """simple docstring""" A__ = floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to( __lowerCAmelCase ) A__ = torch.ones([self.batch_size, self.min_size, self.max_size] , device=__lowerCAmelCase ) A__ = ( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] , device=__lowerCAmelCase ) > 0.5 ).float() A__ = (torch.rand((self.batch_size, self.num_labels) , device=__lowerCAmelCase ) > 0.5).long() A__ = self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def a_ ( self : Dict ) -> Optional[Any]: """simple docstring""" A__ = MaskaFormerConfig( hidden_size=self.hidden_dim , ) A__ = self.num_queries A__ = self.num_labels A__ = [1, 1, 1, 1] A__ = self.num_channels A__ = 64 A__ = 1_28 A__ = self.hidden_dim A__ = self.hidden_dim A__ = self.hidden_dim return config def a_ ( self : Any ) -> int: """simple docstring""" A__ , A__ , A__ , A__ , A__ = self.prepare_config_and_inputs() A__ = {"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask} return config, inputs_dict def a_ ( self : List[str] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Optional[Any] ) -> Dict: """simple docstring""" A__ = output.encoder_hidden_states A__ = output.pixel_decoder_hidden_states A__ = output.transformer_decoder_hidden_states self.parent.assertTrue(len(__lowerCAmelCase ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(__lowerCAmelCase ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(__lowerCAmelCase ) , config.decoder_layers ) def a_ ( self : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : Optional[Any]=False ) -> str: """simple docstring""" with torch.no_grad(): A__ = MaskaFormerModel(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(pixel_values=__lowerCAmelCase , pixel_mask=__lowerCAmelCase ) A__ = model(__lowerCAmelCase , output_hidden_states=__lowerCAmelCase ) self.parent.assertEqual( output.transformer_decoder_last_hidden_state.shape , (self.batch_size, self.num_queries, self.hidden_dim) , ) # let's ensure the other two hidden state exists self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(output.encoder_last_hidden_state is not None ) if output_hidden_states: self.check_output_hidden_state(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : str , __lowerCAmelCase : int , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : List[str] ) -> str: """simple docstring""" A__ = MaskaFormerForUniversalSegmentation(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() def comm_check_on_output(__lowerCAmelCase : Tuple ): # let's still check that all the required stuff is there self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.encoder_last_hidden_state is not None ) # okay, now we need to check the logits shape # due to the encoder compression, masks have a //4 spatial size self.parent.assertEqual( result.masks_queries_logits.shape , (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4) , ) # + 1 for null class self.parent.assertEqual( result.class_queries_logits.shape , (self.batch_size, self.num_queries, self.num_labels + 1) ) with torch.no_grad(): A__ = model(pixel_values=__lowerCAmelCase , pixel_mask=__lowerCAmelCase ) A__ = model(__lowerCAmelCase ) comm_check_on_output(__lowerCAmelCase ) A__ = model( pixel_values=__lowerCAmelCase , pixel_mask=__lowerCAmelCase , mask_labels=__lowerCAmelCase , class_labels=__lowerCAmelCase ) comm_check_on_output(__lowerCAmelCase ) self.parent.assertTrue(result.loss is not None ) self.parent.assertEqual(result.loss.shape , torch.Size([1] ) ) @require_torch class A (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Tuple = (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else () __lowerCamelCase : Tuple = {'''feature-extraction''': MaskaFormerModel} if is_torch_available() else {} __lowerCamelCase : Tuple = False __lowerCamelCase : Any = False __lowerCamelCase : Any = False __lowerCamelCase : Tuple = False def a_ ( self : Any ) -> Tuple: """simple docstring""" A__ = MaskaFormerModelTester(self ) A__ = ConfigTester(self , config_class=__lowerCAmelCase , has_text_modality=__lowerCAmelCase ) def a_ ( self : int ) -> List[str]: """simple docstring""" self.config_tester.run_common_tests() def a_ ( self : str ) -> str: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(__lowerCAmelCase , **__lowerCAmelCase , output_hidden_states=__lowerCAmelCase ) def a_ ( self : Tuple ) -> Union[str, Any]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*__lowerCAmelCase ) @unittest.skip(reason="""Mask2Former does not use inputs_embeds""" ) def a_ ( self : str ) -> int: """simple docstring""" pass @unittest.skip(reason="""Mask2Former does not have a get_input_embeddings method""" ) def a_ ( self : Union[str, Any] ) -> List[Any]: """simple docstring""" pass @unittest.skip(reason="""Mask2Former is not a generative model""" ) def a_ ( self : Tuple ) -> Optional[Any]: """simple docstring""" pass @unittest.skip(reason="""Mask2Former does not use token embeddings""" ) def a_ ( self : Optional[int] ) -> int: """simple docstring""" pass @require_torch_multi_gpu @unittest.skip( reason="""Mask2Former has some layers using `add_module` which doesn't work well with `nn.DataParallel`""" ) def a_ ( self : List[str] ) -> Union[str, Any]: """simple docstring""" pass @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def a_ ( self : Union[str, Any] ) -> Tuple: """simple docstring""" pass def a_ ( self : Union[str, Any] ) -> List[Any]: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(__lowerCAmelCase ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , __lowerCAmelCase ) @slow def a_ ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" for model_name in ["facebook/mask2former-swin-small-coco-instance"]: A__ = MaskaFormerModel.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) def a_ ( self : Dict ) -> str: """simple docstring""" A__ = (self.model_tester.min_size,) * 2 A__ = { """pixel_values""": torch.randn((2, 3, *size) , device=__lowerCAmelCase ), """mask_labels""": torch.randn((2, 10, *size) , device=__lowerCAmelCase ), """class_labels""": torch.zeros(2 , 10 , device=__lowerCAmelCase ).long(), } A__ = self.model_tester.get_config() A__ = MaskaFormerForUniversalSegmentation(__lowerCAmelCase ).to(__lowerCAmelCase ) A__ = model(**__lowerCAmelCase ) self.assertTrue(outputs.loss is not None ) def a_ ( self : int ) -> Any: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(__lowerCAmelCase , **__lowerCAmelCase , output_hidden_states=__lowerCAmelCase ) def a_ ( self : List[Any] ) -> Tuple: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(__lowerCAmelCase ).to(__lowerCAmelCase ) A__ = model(**__lowerCAmelCase , output_attentions=__lowerCAmelCase ) self.assertTrue(outputs.attentions is not None ) def a_ ( self : str ) -> int: """simple docstring""" if not self.model_tester.is_training: return A__ = self.all_model_classes[1] A__ , A__ , A__ , A__ , A__ = self.model_tester.prepare_config_and_inputs() A__ = model_class(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.train() A__ = model(__lowerCAmelCase , mask_labels=__lowerCAmelCase , class_labels=__lowerCAmelCase ).loss loss.backward() def a_ ( self : Any ) -> Optional[int]: """simple docstring""" A__ = self.all_model_classes[1] A__ , A__ , A__ , A__ , A__ = self.model_tester.prepare_config_and_inputs() A__ = True A__ = True A__ = model_class(__lowerCAmelCase ).to(__lowerCAmelCase ) model.train() A__ = model(__lowerCAmelCase , mask_labels=__lowerCAmelCase , class_labels=__lowerCAmelCase ) A__ = outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() A__ = outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() A__ = outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() A__ = outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=__lowerCAmelCase ) self.assertIsNotNone(encoder_hidden_states.grad ) self.assertIsNotNone(pixel_decoder_hidden_states.grad ) self.assertIsNotNone(transformer_decoder_hidden_states.grad ) self.assertIsNotNone(attentions.grad ) A : int = 1e-4 def __lowerCamelCase ( ) -> Optional[int]: """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_vision @slow class A (unittest.TestCase ): '''simple docstring''' @cached_property def a_ ( self : int ) -> List[Any]: """simple docstring""" return "facebook/mask2former-swin-small-coco-instance" @cached_property def a_ ( self : Any ) -> int: """simple docstring""" return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints ) if is_vision_available() else None def a_ ( self : Tuple ) -> Optional[Any]: """simple docstring""" A__ = MaskaFormerModel.from_pretrained(self.model_checkpoints ).to(__lowerCAmelCase ) A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(__lowerCAmelCase , return_tensors="""pt""" ).to(__lowerCAmelCase ) A__ = inputs["""pixel_values"""].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(__lowerCAmelCase , (1, 3, 3_84, 3_84) ) with torch.no_grad(): A__ = model(**__lowerCAmelCase ) A__ = torch.tensor( [[-0.2_7_9_0, -1.0_7_1_7, -1.1_6_6_8], [-0.5_1_2_8, -0.3_1_2_8, -0.4_9_8_7], [-0.5_8_3_2, 0.1_9_7_1, -0.0_1_9_7]] ).to(__lowerCAmelCase ) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3] , __lowerCAmelCase , atol=__lowerCAmelCase ) ) A__ = torch.tensor( [[0.8_9_7_3, 1.1_8_4_7, 1.1_7_7_6], [1.1_9_3_4, 1.5_0_4_0, 1.5_1_2_8], [1.1_1_5_3, 1.4_4_8_6, 1.4_9_5_1]] ).to(__lowerCAmelCase ) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] , __lowerCAmelCase , atol=__lowerCAmelCase ) ) A__ = torch.tensor( [[2.1_1_5_2, 1.7_0_0_0, -0.8_6_0_3], [1.5_8_0_8, 1.8_0_0_4, -0.9_3_5_3], [1.6_0_4_3, 1.7_4_9_5, -0.5_9_9_9]] ).to(__lowerCAmelCase ) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3] , __lowerCAmelCase , atol=__lowerCAmelCase ) ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" A__ = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(__lowerCAmelCase ).eval() A__ = self.default_image_processor A__ = prepare_img() A__ = image_processor(__lowerCAmelCase , return_tensors="""pt""" ).to(__lowerCAmelCase ) A__ = inputs["""pixel_values"""].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(__lowerCAmelCase , (1, 3, 3_84, 3_84) ) with torch.no_grad(): A__ = model(**__lowerCAmelCase ) # masks_queries_logits A__ = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) ) A__ = [ [-8.7_8_3_9, -9.0_0_5_6, -8.8_1_2_1], [-7.4_1_0_4, -7.0_3_1_3, -6.5_4_0_1], [-6.6_1_0_5, -6.3_4_2_7, -6.4_6_7_5], ] A__ = torch.tensor(__lowerCAmelCase ).to(__lowerCAmelCase ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , __lowerCAmelCase , atol=__lowerCAmelCase ) ) # class_queries_logits A__ = outputs.class_queries_logits self.assertEqual(class_queries_logits.shape , (1, model.config.num_queries, model.config.num_labels + 1) ) A__ = torch.tensor( [ [1.8_3_2_4, -8.0_8_3_5, -4.1_9_2_2], [0.8_4_5_0, -9.0_0_5_0, -3.6_0_5_3], [0.3_0_4_5, -7.7_2_9_3, -3.0_2_7_5], ] ).to(__lowerCAmelCase ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , __lowerCAmelCase , atol=__lowerCAmelCase ) ) def a_ ( self : Optional[int] ) -> List[Any]: """simple docstring""" A__ = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(__lowerCAmelCase ).eval() A__ = self.default_image_processor A__ = image_processor( [np.zeros((3, 8_00, 13_33) ), np.zeros((3, 8_00, 13_33) )] , segmentation_maps=[np.zeros((3_84, 3_84) ).astype(np.floataa ), np.zeros((3_84, 3_84) ).astype(np.floataa )] , return_tensors="""pt""" , ) A__ = inputs["""pixel_values"""].to(__lowerCAmelCase ) A__ = [el.to(__lowerCAmelCase ) for el in inputs["""mask_labels"""]] A__ = [el.to(__lowerCAmelCase ) for el in inputs["""class_labels"""]] with torch.no_grad(): A__ = model(**__lowerCAmelCase ) self.assertTrue(outputs.loss is not None )
274
import argparse from collections import defaultdict import yaml A : str = '''docs/source/en/_toctree.yml''' def __lowerCamelCase ( __a :str ) -> List[Any]: """simple docstring""" A__ = defaultdict(__a ) A__ = [] A__ = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__a ) A__ = new_doc_list A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__a ) > 1: raise ValueError( F'{duplicate_key} is present several times in the documentation table of content at ' """`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the """ """others.""" ) # Only add this once new_doc.append({"""local""": duplicate_key, """title""": titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) A__ = sorted(__a , key=lambda __a : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__a ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__a ) # Sort return overview_doc def __lowerCamelCase ( __a :Any=False ) -> List[str]: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 A__ = api_doc[scheduler_idx]["""sections"""] A__ = clean_doc_toc(__a ) A__ = False if new_scheduler_doc != scheduler_doc: A__ = True if overwrite: A__ = new_scheduler_doc if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def __lowerCamelCase ( __a :Optional[int]=False ) -> Dict: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 A__ = False A__ = api_doc[pipeline_idx]["""sections"""] A__ = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: A__ = pipeline_doc["""section"""] A__ = clean_doc_toc(__a ) if overwrite: A__ = new_sub_pipeline_doc new_pipeline_docs.append(__a ) # sort overall pipeline doc A__ = clean_doc_toc(__a ) if new_pipeline_docs != pipeline_docs: A__ = True if overwrite: A__ = new_pipeline_docs if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) if __name__ == "__main__": A : Tuple = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') A : Optional[Any] = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
274
1
import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, 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 OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class A : '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : List[str]=13 , __lowerCAmelCase : Optional[int]=7 , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : Optional[int]=True , __lowerCAmelCase : Optional[Any]=False , __lowerCAmelCase : Any=True , __lowerCAmelCase : str=99 , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Any=5 , __lowerCAmelCase : List[str]=4 , __lowerCAmelCase : Tuple=37 , __lowerCAmelCase : List[Any]="gelu" , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : List[str]=16 , __lowerCAmelCase : List[str]=2 , __lowerCAmelCase : Any=0.0_2 , __lowerCAmelCase : int=3 , __lowerCAmelCase : Union[str, Any]=4 , __lowerCAmelCase : List[Any]=None , ) -> Optional[int]: """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_input_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_labels A__ = num_choices A__ = scope def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_input_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ = None A__ = None A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A__ = ids_tensor([self.batch_size] , self.num_choices ) A__ = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a_ ( self : Optional[Any] ) -> Optional[int]: """simple docstring""" return OpenLlamaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__lowerCAmelCase , initializer_range=self.initializer_range , use_stable_embedding=__lowerCAmelCase , ) def a_ ( self : List[str] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : str ) -> int: """simple docstring""" A__ = OpenLlamaModel(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase ) A__ = model(__lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a_ ( self : Optional[int] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Tuple , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : str , __lowerCAmelCase : int , ) -> List[Any]: """simple docstring""" A__ = True A__ = OpenLlamaModel(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model( __lowerCAmelCase , attention_mask=__lowerCAmelCase , encoder_hidden_states=__lowerCAmelCase , encoder_attention_mask=__lowerCAmelCase , ) A__ = model( __lowerCAmelCase , attention_mask=__lowerCAmelCase , encoder_hidden_states=__lowerCAmelCase , ) A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a_ ( self : Optional[Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any] , ) -> List[str]: """simple docstring""" A__ = OpenLlamaForCausalLM(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a_ ( self : Optional[int] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Any , __lowerCAmelCase : Any , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Any , ) -> List[Any]: """simple docstring""" A__ = True A__ = True A__ = OpenLlamaForCausalLM(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() # first forward pass A__ = model( __lowerCAmelCase , attention_mask=__lowerCAmelCase , encoder_hidden_states=__lowerCAmelCase , encoder_attention_mask=__lowerCAmelCase , use_cache=__lowerCAmelCase , ) A__ = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids A__ = ids_tensor((self.batch_size, 3) , config.vocab_size ) A__ = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and A__ = torch.cat([input_ids, next_tokens] , dim=-1 ) A__ = torch.cat([input_mask, next_mask] , dim=-1 ) A__ = model( __lowerCAmelCase , attention_mask=__lowerCAmelCase , encoder_hidden_states=__lowerCAmelCase , encoder_attention_mask=__lowerCAmelCase , output_hidden_states=__lowerCAmelCase , )["""hidden_states"""][0] A__ = model( __lowerCAmelCase , attention_mask=__lowerCAmelCase , encoder_hidden_states=__lowerCAmelCase , encoder_attention_mask=__lowerCAmelCase , past_key_values=__lowerCAmelCase , output_hidden_states=__lowerCAmelCase , )["""hidden_states"""][0] # select random slice A__ = ids_tensor((1,) , output_from_past.shape[-1] ).item() A__ = output_from_no_past[:, -3:, random_slice_idx].detach() A__ = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-3 ) ) def a_ ( self : Tuple ) -> str: """simple docstring""" A__ = self.prepare_config_and_inputs() ( ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ) = config_and_inputs A__ = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class A (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[Any] = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) __lowerCamelCase : int = (OpenLlamaForCausalLM,) if is_torch_available() else () __lowerCamelCase : int = ( { '''feature-extraction''': OpenLlamaModel, '''text-classification''': OpenLlamaForSequenceClassification, '''text-generation''': OpenLlamaForCausalLM, '''zero-shot''': OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) __lowerCamelCase : int = False __lowerCamelCase : Union[str, Any] = False def a_ ( self : List[Any] ) -> Optional[int]: """simple docstring""" A__ = OpenLlamaModelTester(self ) A__ = ConfigTester(self , config_class=__lowerCAmelCase , hidden_size=37 ) def a_ ( self : List[Any] ) -> Any: """simple docstring""" self.config_tester.run_common_tests() def a_ ( self : List[Any] ) -> Dict: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCAmelCase ) def a_ ( self : Union[str, Any] ) -> Optional[int]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: A__ = type self.model_tester.create_and_check_model(*__lowerCAmelCase ) def a_ ( self : Union[str, Any] ) -> Any: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = 3 A__ = input_dict["""input_ids"""] A__ = input_ids.ne(1 ).to(__lowerCAmelCase ) A__ = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) A__ = OpenLlamaForSequenceClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a_ ( self : Dict ) -> str: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = 3 A__ = """single_label_classification""" A__ = input_dict["""input_ids"""] A__ = input_ids.ne(1 ).to(__lowerCAmelCase ) A__ = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) A__ = OpenLlamaForSequenceClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a_ ( self : Any ) -> Union[str, Any]: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = 3 A__ = """multi_label_classification""" A__ = input_dict["""input_ids"""] A__ = input_ids.ne(1 ).to(__lowerCAmelCase ) A__ = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) A__ = OpenLlamaForSequenceClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip("""Open-Llama buffers include complex numbers, which breaks this test""" ) def a_ ( self : List[Any] ) -> List[str]: """simple docstring""" pass @parameterized.expand([("""linear""",), ("""dynamic""",)] ) def a_ ( self : str , __lowerCAmelCase : Tuple ) -> Dict: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = ids_tensor([1, 10] , config.vocab_size ) A__ = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights A__ = OpenLlamaModel(__lowerCAmelCase ) original_model.to(__lowerCAmelCase ) original_model.eval() A__ = original_model(__lowerCAmelCase ).last_hidden_state A__ = original_model(__lowerCAmelCase ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights A__ = {"""type""": scaling_type, """factor""": 1_0.0} A__ = OpenLlamaModel(__lowerCAmelCase ) scaled_model.to(__lowerCAmelCase ) scaled_model.eval() A__ = scaled_model(__lowerCAmelCase ).last_hidden_state A__ = scaled_model(__lowerCAmelCase ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-5 ) ) else: self.assertFalse(torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-5 ) )
274
def __lowerCamelCase ( __a :str ) -> list: """simple docstring""" A__ = [0] * len(__a ) for i in range(1 , len(__a ) ): # use last results for better performance - dynamic programming A__ = prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: A__ = prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 A__ = j return prefix_result def __lowerCamelCase ( __a :str ) -> int: """simple docstring""" return max(prefix_function(__a ) ) if __name__ == "__main__": import doctest doctest.testmod()
274
1
# Lint as: python3 import sys from collections.abc import Mapping from typing import TYPE_CHECKING import numpy as np import pyarrow as pa from .. import config from ..utils.py_utils import map_nested from .formatting import TensorFormatter if TYPE_CHECKING: import torch class A (TensorFormatter[Mapping, '''torch.Tensor''', Mapping] ): '''simple docstring''' def __init__( self : Dict , __lowerCAmelCase : List[str]=None , **__lowerCAmelCase : List[Any] ) -> Tuple: """simple docstring""" super().__init__(features=__lowerCAmelCase ) A__ = torch_tensor_kwargs import torch # noqa import torch at initialization def a_ ( self : List[str] , __lowerCAmelCase : Optional[Any] ) -> Optional[Any]: """simple docstring""" import torch if isinstance(__lowerCAmelCase , __lowerCAmelCase ) and column: if all( isinstance(__lowerCAmelCase , torch.Tensor ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column ): return torch.stack(__lowerCAmelCase ) return column def a_ ( self : List[Any] , __lowerCAmelCase : int ) -> Dict: """simple docstring""" import torch if isinstance(__lowerCAmelCase , (str, bytes, type(__lowerCAmelCase )) ): return value elif isinstance(__lowerCAmelCase , (np.character, np.ndarray) ) and np.issubdtype(value.dtype , np.character ): return value.tolist() A__ = {} if isinstance(__lowerCAmelCase , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.integer ): A__ = {"""dtype""": torch.intaa} elif isinstance(__lowerCAmelCase , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.floating ): A__ = {"""dtype""": torch.floataa} elif config.PIL_AVAILABLE and "PIL" in sys.modules: import PIL.Image if isinstance(__lowerCAmelCase , PIL.Image.Image ): A__ = np.asarray(__lowerCAmelCase ) return torch.tensor(__lowerCAmelCase , **{**default_dtype, **self.torch_tensor_kwargs} ) def a_ ( self : Optional[Any] , __lowerCAmelCase : Dict ) -> Union[str, Any]: """simple docstring""" import torch # support for torch, tf, jax etc. if hasattr(__lowerCAmelCase , """__array__""" ) and not isinstance(__lowerCAmelCase , torch.Tensor ): A__ = data_struct.__array__() # support for nested types like struct of list of struct if isinstance(__lowerCAmelCase , np.ndarray ): if data_struct.dtype == object: # torch tensors cannot be instantied from an array of objects return self._consolidate([self.recursive_tensorize(__lowerCAmelCase ) for substruct in data_struct] ) elif isinstance(__lowerCAmelCase , (list, tuple) ): return self._consolidate([self.recursive_tensorize(__lowerCAmelCase ) for substruct in data_struct] ) return self._tensorize(__lowerCAmelCase ) def a_ ( self : Tuple , __lowerCAmelCase : dict ) -> Any: """simple docstring""" return map_nested(self._recursive_tensorize , __lowerCAmelCase , map_list=__lowerCAmelCase ) def a_ ( self : Union[str, Any] , __lowerCAmelCase : pa.Table ) -> Mapping: """simple docstring""" A__ = self.numpy_arrow_extractor().extract_row(__lowerCAmelCase ) A__ = self.python_features_decoder.decode_row(__lowerCAmelCase ) return self.recursive_tensorize(__lowerCAmelCase ) def a_ ( self : Union[str, Any] , __lowerCAmelCase : pa.Table ) -> "torch.Tensor": """simple docstring""" A__ = self.numpy_arrow_extractor().extract_column(__lowerCAmelCase ) A__ = self.python_features_decoder.decode_column(__lowerCAmelCase , pa_table.column_names[0] ) A__ = self.recursive_tensorize(__lowerCAmelCase ) A__ = self._consolidate(__lowerCAmelCase ) return column def a_ ( self : Dict , __lowerCAmelCase : pa.Table ) -> Mapping: """simple docstring""" A__ = self.numpy_arrow_extractor().extract_batch(__lowerCAmelCase ) A__ = self.python_features_decoder.decode_batch(__lowerCAmelCase ) A__ = self.recursive_tensorize(__lowerCAmelCase ) for column_name in batch: A__ = self._consolidate(batch[column_name] ) return batch
274
def __lowerCamelCase ( __a :int = 1_0_0_0_0_0_0 ) -> int: """simple docstring""" A__ = limit + 1 A__ = [0] * limit for first_term in range(1 , __a ): for n in range(__a , __a , __a ): A__ = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a A__ = sum(1 for x in frequency[1:limit] if x == 1_0 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
274
1
from typing import List, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging A : Any = logging.get_logger(__name__) A : Optional[int] = { '''huggingface/autoformer-tourism-monthly''': '''https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json''', } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[str] = '''autoformer''' __lowerCamelCase : List[str] = { '''hidden_size''': '''d_model''', '''num_attention_heads''': '''encoder_attention_heads''', '''num_hidden_layers''': '''encoder_layers''', } def __init__( self : List[Any] , __lowerCAmelCase : Optional[int] = None , __lowerCAmelCase : Optional[int] = None , __lowerCAmelCase : str = "student_t" , __lowerCAmelCase : str = "nll" , __lowerCAmelCase : int = 1 , __lowerCAmelCase : List[int] = [1, 2, 3, 4, 5, 6, 7] , __lowerCAmelCase : bool = True , __lowerCAmelCase : int = 0 , __lowerCAmelCase : int = 0 , __lowerCAmelCase : int = 0 , __lowerCAmelCase : int = 0 , __lowerCAmelCase : Optional[List[int]] = None , __lowerCAmelCase : Optional[List[int]] = None , __lowerCAmelCase : int = 64 , __lowerCAmelCase : int = 2 , __lowerCAmelCase : int = 2 , __lowerCAmelCase : int = 2 , __lowerCAmelCase : int = 2 , __lowerCAmelCase : int = 32 , __lowerCAmelCase : int = 32 , __lowerCAmelCase : str = "gelu" , __lowerCAmelCase : float = 0.1 , __lowerCAmelCase : float = 0.1 , __lowerCAmelCase : float = 0.1 , __lowerCAmelCase : float = 0.1 , __lowerCAmelCase : float = 0.1 , __lowerCAmelCase : int = 1_00 , __lowerCAmelCase : float = 0.0_2 , __lowerCAmelCase : bool = True , __lowerCAmelCase : str=True , __lowerCAmelCase : int = 10 , __lowerCAmelCase : int = 25 , __lowerCAmelCase : int = 3 , **__lowerCAmelCase : Optional[int] , ) -> Union[str, Any]: """simple docstring""" A__ = prediction_length A__ = context_length if context_length is not None else prediction_length A__ = distribution_output A__ = loss A__ = input_size A__ = num_time_features A__ = lags_sequence A__ = scaling A__ = num_dynamic_real_features A__ = num_static_real_features A__ = num_static_categorical_features if cardinality is not None and num_static_categorical_features > 0: if len(__lowerCAmelCase ) != num_static_categorical_features: raise ValueError( """The cardinality should be a list of the same length as `num_static_categorical_features`""" ) A__ = cardinality else: A__ = [0] if embedding_dimension is not None and num_static_categorical_features > 0: if len(__lowerCAmelCase ) != num_static_categorical_features: raise ValueError( """The embedding dimension should be a list of the same length as `num_static_categorical_features`""" ) A__ = embedding_dimension else: A__ = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] A__ = num_parallel_samples # Transformer architecture configuration A__ = input_size * len(self.lags_sequence ) + self._number_of_features A__ = d_model A__ = encoder_attention_heads A__ = decoder_attention_heads A__ = encoder_ffn_dim A__ = decoder_ffn_dim A__ = encoder_layers A__ = decoder_layers A__ = dropout A__ = attention_dropout A__ = activation_dropout A__ = encoder_layerdrop A__ = decoder_layerdrop A__ = activation_function A__ = init_std A__ = use_cache # Autoformer A__ = label_length A__ = moving_average A__ = autocorrelation_factor super().__init__(is_encoder_decoder=__lowerCAmelCase , **__lowerCAmelCase ) @property def a_ ( self : Optional[int] ) -> int: """simple docstring""" return ( sum(self.embedding_dimension ) + self.num_dynamic_real_features + self.num_time_features + self.num_static_real_features + self.input_size * 2 # the log1p(abs(loc)) and log(scale) features )
274
class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A : '''simple docstring''' def __init__( self : List[Any] ) -> str: """simple docstring""" A__ = [ [], [], [], ] def a_ ( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" try: if len(self.queues[priority] ) >= 1_00: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(__lowerCAmelCase ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def a_ ( self : Optional[Any] ) -> int: """simple docstring""" for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self : Tuple ) -> str: """simple docstring""" return "\n".join(f'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class A : '''simple docstring''' def __init__( self : int ) -> str: """simple docstring""" A__ = [] def a_ ( self : int , __lowerCAmelCase : int ) -> None: """simple docstring""" if len(self.queue ) == 1_00: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(__lowerCAmelCase ) def a_ ( self : List[str] ) -> int: """simple docstring""" if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: A__ = min(self.queue ) self.queue.remove(__lowerCAmelCase ) return data def __str__( self : List[Any] ) -> str: """simple docstring""" return str(self.queue ) def __lowerCamelCase ( ) -> Optional[Any]: """simple docstring""" A__ = FixedPriorityQueue() fpq.enqueue(0 , 1_0 ) fpq.enqueue(1 , 7_0 ) fpq.enqueue(0 , 1_0_0 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 6_4 ) fpq.enqueue(0 , 1_2_8 ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __lowerCamelCase ( ) -> int: """simple docstring""" A__ = ElementPriorityQueue() epq.enqueue(1_0 ) epq.enqueue(7_0 ) epq.enqueue(1_0_0 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(6_4 ) epq.enqueue(1_2_8 ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
274
1
import importlib.metadata import operator import re import sys from typing import Optional from packaging import version A : str = { '''<''': operator.lt, '''<=''': operator.le, '''==''': operator.eq, '''!=''': operator.ne, '''>=''': operator.ge, '''>''': operator.gt, } def __lowerCamelCase ( __a :Union[str, Any] , __a :Optional[Any] , __a :Dict , __a :str , __a :List[Any] , __a :Dict ) -> List[str]: """simple docstring""" if got_ver is None or want_ver is None: raise ValueError( F'Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider' F' reinstalling {pkg}.' ) if not ops[op](version.parse(__a ) , version.parse(__a ) ): raise ImportError( F'{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}' ) def __lowerCamelCase ( __a :str , __a :Optional[str] = None ) -> None: """simple docstring""" A__ = F'\n{hint}' if hint is not None else """""" # non-versioned check if re.match(R"""^[\w_\-\d]+$""" , __a ): A__ , A__ , A__ = requirement, None, None else: A__ = re.findall(R"""^([^!=<>\s]+)([\s!=<>]{1,2}.+)""" , __a ) if not match: raise ValueError( """requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but""" F' got {requirement}' ) A__ , A__ = match[0] A__ = want_full.split(""",""" ) # there could be multiple requirements A__ = {} for w in want_range: A__ = re.findall(R"""^([\s!=<>]{1,2})(.+)""" , __a ) if not match: raise ValueError( """requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,""" F' but got {requirement}' ) A__ , A__ = match[0] A__ = want_ver if op not in ops: raise ValueError(F'{requirement}: need one of {list(ops.keys() )}, but got {op}' ) # special case if pkg == "python": A__ = """.""".join([str(__a ) for x in sys.version_info[:3]] ) for op, want_ver in wanted.items(): _compare_versions(__a , __a , __a , __a , __a , __a ) return # check if any version is installed try: A__ = importlib.metadata.version(__a ) except importlib.metadata.PackageNotFoundError: raise importlib.metadata.PackageNotFoundError( F'The \'{requirement}\' distribution was not found and is required by this application. {hint}' ) # check that the right version is installed if version number or a range was provided if want_ver is not None: for op, want_ver in wanted.items(): _compare_versions(__a , __a , __a , __a , __a , __a ) def __lowerCamelCase ( __a :Optional[Any] ) -> int: """simple docstring""" A__ = """Try: pip install transformers -U or pip install -e '.[dev]' if you're working with git main""" return require_version(__a , __a )
274
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = tempfile.mkdtemp() # fmt: off A__ = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on A__ = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) A__ = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""] A__ = {"""unk_token""": """<unk>"""} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(__lowerCAmelCase ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__lowerCAmelCase ) ) A__ = { """do_resize""": True, """size""": 20, """do_center_crop""": True, """crop_size""": 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], } A__ = os.path.join(self.tmpdirname , __lowerCAmelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Tuple , **__lowerCAmelCase : Dict ) -> str: """simple docstring""" return CLIPTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : Union[str, Any] , **__lowerCAmelCase : Dict ) -> List[str]: """simple docstring""" return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : List[str] , **__lowerCAmelCase : Optional[Any] ) -> Dict: """simple docstring""" return CLIPImageProcessor.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : str ) -> Dict: """simple docstring""" shutil.rmtree(self.tmpdirname ) def a_ ( self : str ) -> Any: """simple docstring""" A__ = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] A__ = [Image.fromarray(np.moveaxis(__lowerCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def a_ ( self : Optional[int] ) -> Tuple: """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_rust_tokenizer() A__ = self.get_image_processor() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_slow.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname , use_fast=__lowerCAmelCase ) A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_fast.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , __lowerCAmelCase ) self.assertIsInstance(processor_fast.tokenizer , __lowerCAmelCase ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , __lowerCAmelCase ) self.assertIsInstance(processor_fast.image_processor , __lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" A__ = CLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=__lowerCAmelCase , padding_value=1.0 ) A__ = CLIPProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__lowerCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowerCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCAmelCase ) def a_ ( self : List[Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(__lowerCAmelCase , return_tensors="""np""" ) A__ = processor(images=__lowerCAmelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def a_ ( self : Optional[Any] ) -> Any: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = processor(text=__lowerCAmelCase ) A__ = tokenizer(__lowerCAmelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(__lowerCAmelCase ): processor() def a_ ( self : Tuple ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.batch_decode(__lowerCAmelCase ) A__ = tokenizer.batch_decode(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Optional[int] ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
274
1
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_xlnet import XLNetTokenizer else: A : List[str] = None A : List[Any] = logging.get_logger(__name__) A : int = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A : Tuple = { '''vocab_file''': { '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model''', '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json''', '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json''', }, } A : List[Any] = { '''xlnet-base-cased''': None, '''xlnet-large-cased''': None, } A : List[str] = '''▁''' # Segments (not really needed) A : List[Any] = 0 A : Union[str, Any] = 1 A : str = 2 A : int = 3 A : Optional[int] = 4 class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : str = VOCAB_FILES_NAMES __lowerCamelCase : str = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : int = '''left''' __lowerCamelCase : Any = XLNetTokenizer def __init__( self : List[Any] , __lowerCAmelCase : int=None , __lowerCAmelCase : List[Any]=None , __lowerCAmelCase : Optional[int]=False , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : int=False , __lowerCAmelCase : Optional[int]="<s>" , __lowerCAmelCase : Tuple="</s>" , __lowerCAmelCase : Any="<unk>" , __lowerCAmelCase : str="<sep>" , __lowerCAmelCase : int="<pad>" , __lowerCAmelCase : Union[str, Any]="<cls>" , __lowerCAmelCase : Union[str, Any]="<mask>" , __lowerCAmelCase : List[str]=["<eop>", "<eod>"] , **__lowerCAmelCase : str , ) -> Optional[int]: """simple docstring""" A__ = AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token super().__init__( vocab_file=__lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , additional_special_tokens=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = 3 A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def a_ ( self : Any , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def a_ ( self : Tuple , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def a_ ( self : Dict , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(__lowerCAmelCase ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return A__ = os.path.join( __lowerCAmelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCAmelCase ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
274
from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time A : Dict = Lock() def __lowerCamelCase ( __a :Dict , __a :List[str] , __a :Optional[int] , __a :Optional[int] , __a :Optional[Any] , __a :Optional[int] , __a :int ) -> Dict: """simple docstring""" global process_lock # we perform n swaps since after n swaps we know we are sorted # we *could* stop early if we are sorted already, but it takes as long to # find out we are sorted as it does to sort the list with this algorithm for i in range(0 , 1_0 ): if (i + position) % 2 == 0 and r_send is not None: # send your value to your right neighbor process_lock.acquire() r_send[1].send(__a ) process_lock.release() # receive your right neighbor's value process_lock.acquire() A__ = rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left A__ = min(__a , __a ) elif (i + position) % 2 != 0 and l_send is not None: # send your value to your left neighbor process_lock.acquire() l_send[1].send(__a ) process_lock.release() # receive your left neighbor's value process_lock.acquire() A__ = lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right A__ = max(__a , __a ) # after all swaps are performed, send the values back to main result_pipe[1].send(__a ) def __lowerCamelCase ( __a :List[str] ) -> int: """simple docstring""" A__ = [] A__ = [] # initialize the list of pipes where the values will be retrieved for _ in arr: result_pipe.append(Pipe() ) # creates the processes # the first and last process only have one neighbor so they are made outside # of the loop A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) A__ = temp_rs A__ = temp_rr for i in range(1 , len(__a ) - 1 ): A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) A__ = temp_rs A__ = temp_rr process_array_.append( Process( target=__a , args=( len(__a ) - 1, arr[len(__a ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(__a ) - 1], ) , ) ) # start the processes for p in process_array_: p.start() # wait for the processes to end and write their values to the list for p in range(0 , len(__a ) ): A__ = result_pipe[p][0].recv() process_array_[p].join() return arr def __lowerCamelCase ( ) -> str: """simple docstring""" A__ = list(range(1_0 , 0 , -1 ) ) print("""Initial List""" ) print(*__a ) A__ = odd_even_transposition(__a ) print("""Sorted List\n""" ) print(*__a ) if __name__ == "__main__": main()
274
1
import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor A : str = logging.get_logger(__name__) class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Tuple , *__lowerCAmelCase : int , **__lowerCAmelCase : Optional[Any] ) -> None: """simple docstring""" warnings.warn( """The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use SegformerImageProcessor instead.""" , __lowerCAmelCase , ) super().__init__(*__lowerCAmelCase , **__lowerCAmelCase )
274
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def __lowerCamelCase ( __a :Dict ) -> Any: """simple docstring""" A__ = [ """decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(__a , __a ) def __lowerCamelCase ( __a :str ) -> Union[str, Any]: """simple docstring""" A__ , A__ = emb.weight.shape A__ = nn.Linear(__a , __a , bias=__a ) A__ = emb.weight.data return lin_layer def __lowerCamelCase ( __a :str ) -> List[str]: """simple docstring""" A__ = torch.load(__a , map_location="""cpu""" ) A__ = Namespace(**checkpoint["""cfg"""]["""model"""] ) A__ = checkpoint["""model"""] remove_ignore_keys_(__a ) A__ = state_dict["""decoder.embed_tokens.weight"""].shape[0] A__ = {key.replace("""decoder""" , """model""" ): val for key, val in state_dict.items()} A__ = XGLMConfig( vocab_size=__a , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""gelu""" , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) A__ = XGLMForCausalLM(__a ) A__ = model.load_state_dict(__a , strict=__a ) print(__a ) A__ = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": A : int = argparse.ArgumentParser() # Required parameters parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') A : str = parser.parse_args() A : str = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
274
1
import flax.linen as nn import jax.numpy as jnp from .attention_flax import FlaxTransformeraDModel from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD class A (nn.Module ): '''simple docstring''' __lowerCamelCase : int __lowerCamelCase : int __lowerCamelCase : float = 0.0 __lowerCamelCase : int = 1 __lowerCamelCase : int = 1 __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : jnp.dtype = jnp.floataa def a_ ( self : Tuple ) -> int: """simple docstring""" A__ = [] A__ = [] for i in range(self.num_layers ): A__ = self.in_channels if i == 0 else self.out_channels A__ = FlaxResnetBlockaD( in_channels=__lowerCAmelCase , out_channels=self.out_channels , dropout_prob=self.dropout , dtype=self.dtype , ) resnets.append(__lowerCAmelCase ) A__ = FlaxTransformeraDModel( in_channels=self.out_channels , n_heads=self.num_attention_heads , d_head=self.out_channels // self.num_attention_heads , depth=1 , use_linear_projection=self.use_linear_projection , only_cross_attention=self.only_cross_attention , use_memory_efficient_attention=self.use_memory_efficient_attention , dtype=self.dtype , ) attentions.append(__lowerCAmelCase ) A__ = resnets A__ = attentions if self.add_downsample: A__ = FlaxDownsampleaD(self.out_channels , dtype=self.dtype ) def __call__( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Dict=True ) -> Optional[Any]: """simple docstring""" A__ = () for resnet, attn in zip(self.resnets , self.attentions ): A__ = resnet(__lowerCAmelCase , __lowerCAmelCase , deterministic=__lowerCAmelCase ) A__ = attn(__lowerCAmelCase , __lowerCAmelCase , deterministic=__lowerCAmelCase ) output_states += (hidden_states,) if self.add_downsample: A__ = self.downsamplers_a(__lowerCAmelCase ) output_states += (hidden_states,) return hidden_states, output_states class A (nn.Module ): '''simple docstring''' __lowerCamelCase : int __lowerCamelCase : int __lowerCamelCase : float = 0.0 __lowerCamelCase : int = 1 __lowerCamelCase : bool = True __lowerCamelCase : jnp.dtype = jnp.floataa def a_ ( self : Any ) -> Optional[int]: """simple docstring""" A__ = [] for i in range(self.num_layers ): A__ = self.in_channels if i == 0 else self.out_channels A__ = FlaxResnetBlockaD( in_channels=__lowerCAmelCase , out_channels=self.out_channels , dropout_prob=self.dropout , dtype=self.dtype , ) resnets.append(__lowerCAmelCase ) A__ = resnets if self.add_downsample: A__ = FlaxDownsampleaD(self.out_channels , dtype=self.dtype ) def __call__( self : Tuple , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : List[str]=True ) -> Optional[Any]: """simple docstring""" A__ = () for resnet in self.resnets: A__ = resnet(__lowerCAmelCase , __lowerCAmelCase , deterministic=__lowerCAmelCase ) output_states += (hidden_states,) if self.add_downsample: A__ = self.downsamplers_a(__lowerCAmelCase ) output_states += (hidden_states,) return hidden_states, output_states class A (nn.Module ): '''simple docstring''' __lowerCamelCase : int __lowerCamelCase : int __lowerCamelCase : int __lowerCamelCase : float = 0.0 __lowerCamelCase : int = 1 __lowerCamelCase : int = 1 __lowerCamelCase : bool = True __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : jnp.dtype = jnp.floataa def a_ ( self : Optional[Any] ) -> int: """simple docstring""" A__ = [] A__ = [] for i in range(self.num_layers ): A__ = self.in_channels if (i == self.num_layers - 1) else self.out_channels A__ = self.prev_output_channel if i == 0 else self.out_channels A__ = FlaxResnetBlockaD( in_channels=resnet_in_channels + res_skip_channels , out_channels=self.out_channels , dropout_prob=self.dropout , dtype=self.dtype , ) resnets.append(__lowerCAmelCase ) A__ = FlaxTransformeraDModel( in_channels=self.out_channels , n_heads=self.num_attention_heads , d_head=self.out_channels // self.num_attention_heads , depth=1 , use_linear_projection=self.use_linear_projection , only_cross_attention=self.only_cross_attention , use_memory_efficient_attention=self.use_memory_efficient_attention , dtype=self.dtype , ) attentions.append(__lowerCAmelCase ) A__ = resnets A__ = attentions if self.add_upsample: A__ = FlaxUpsampleaD(self.out_channels , dtype=self.dtype ) def __call__( self : Any , __lowerCAmelCase : List[str] , __lowerCAmelCase : Dict , __lowerCAmelCase : str , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : int=True ) -> str: """simple docstring""" for resnet, attn in zip(self.resnets , self.attentions ): # pop res hidden states A__ = res_hidden_states_tuple[-1] A__ = res_hidden_states_tuple[:-1] A__ = jnp.concatenate((hidden_states, res_hidden_states) , axis=-1 ) A__ = resnet(__lowerCAmelCase , __lowerCAmelCase , deterministic=__lowerCAmelCase ) A__ = attn(__lowerCAmelCase , __lowerCAmelCase , deterministic=__lowerCAmelCase ) if self.add_upsample: A__ = self.upsamplers_a(__lowerCAmelCase ) return hidden_states class A (nn.Module ): '''simple docstring''' __lowerCamelCase : int __lowerCamelCase : int __lowerCamelCase : int __lowerCamelCase : float = 0.0 __lowerCamelCase : int = 1 __lowerCamelCase : bool = True __lowerCamelCase : jnp.dtype = jnp.floataa def a_ ( self : Dict ) -> Optional[int]: """simple docstring""" A__ = [] for i in range(self.num_layers ): A__ = self.in_channels if (i == self.num_layers - 1) else self.out_channels A__ = self.prev_output_channel if i == 0 else self.out_channels A__ = FlaxResnetBlockaD( in_channels=resnet_in_channels + res_skip_channels , out_channels=self.out_channels , dropout_prob=self.dropout , dtype=self.dtype , ) resnets.append(__lowerCAmelCase ) A__ = resnets if self.add_upsample: A__ = FlaxUpsampleaD(self.out_channels , dtype=self.dtype ) def __call__( self : Tuple , __lowerCAmelCase : Any , __lowerCAmelCase : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : Optional[int]=True ) -> List[Any]: """simple docstring""" for resnet in self.resnets: # pop res hidden states A__ = res_hidden_states_tuple[-1] A__ = res_hidden_states_tuple[:-1] A__ = jnp.concatenate((hidden_states, res_hidden_states) , axis=-1 ) A__ = resnet(__lowerCAmelCase , __lowerCAmelCase , deterministic=__lowerCAmelCase ) if self.add_upsample: A__ = self.upsamplers_a(__lowerCAmelCase ) return hidden_states class A (nn.Module ): '''simple docstring''' __lowerCamelCase : int __lowerCamelCase : float = 0.0 __lowerCamelCase : int = 1 __lowerCamelCase : int = 1 __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : jnp.dtype = jnp.floataa def a_ ( self : int ) -> Optional[int]: """simple docstring""" A__ = [ FlaxResnetBlockaD( in_channels=self.in_channels , out_channels=self.in_channels , dropout_prob=self.dropout , dtype=self.dtype , ) ] A__ = [] for _ in range(self.num_layers ): A__ = FlaxTransformeraDModel( in_channels=self.in_channels , n_heads=self.num_attention_heads , d_head=self.in_channels // self.num_attention_heads , depth=1 , use_linear_projection=self.use_linear_projection , use_memory_efficient_attention=self.use_memory_efficient_attention , dtype=self.dtype , ) attentions.append(__lowerCAmelCase ) A__ = FlaxResnetBlockaD( in_channels=self.in_channels , out_channels=self.in_channels , dropout_prob=self.dropout , dtype=self.dtype , ) resnets.append(__lowerCAmelCase ) A__ = resnets A__ = attentions def __call__( self : Union[str, Any] , __lowerCAmelCase : Any , __lowerCAmelCase : List[str] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : List[str]=True ) -> Optional[Any]: """simple docstring""" A__ = self.resnets[0](__lowerCAmelCase , __lowerCAmelCase ) for attn, resnet in zip(self.attentions , self.resnets[1:] ): A__ = attn(__lowerCAmelCase , __lowerCAmelCase , deterministic=__lowerCAmelCase ) A__ = resnet(__lowerCAmelCase , __lowerCAmelCase , deterministic=__lowerCAmelCase ) return hidden_states
274
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 A (unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : List[str]=13 , __lowerCAmelCase : int=7 , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : int=True , __lowerCAmelCase : Dict=True , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : List[Any]=99 , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Optional[Any]=5 , __lowerCAmelCase : Tuple=4 , __lowerCAmelCase : Any=37 , __lowerCAmelCase : str="gelu" , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : Union[str, Any]=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : int=16 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : List[Any]=0.0_2 , __lowerCAmelCase : Tuple=4 , ) -> Dict: """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_attention_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_choices def a_ ( self : Any ) -> str: """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_attention_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ = 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=__lowerCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a_ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class A (SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a_ ( self : str ) -> Optional[int]: """simple docstring""" A__ = FlaxAlbertModelTester(self ) @slow def a_ ( self : int ) -> Tuple: """simple docstring""" for model_class_name in self.all_model_classes: A__ = model_class_name.from_pretrained("""albert-base-v2""" ) A__ = model(np.ones((1, 1) ) ) self.assertIsNotNone(__lowerCAmelCase ) @require_flax class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : Dict ) -> List[Any]: """simple docstring""" A__ = FlaxAlbertModel.from_pretrained("""albert-base-v2""" ) A__ = np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] ) A__ = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase )[0] A__ = (1, 11, 7_68) self.assertEqual(output.shape , __lowerCAmelCase ) A__ = 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] , __lowerCAmelCase , atol=1e-4 ) )
274
1
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging A : Optional[Any] = logging.get_logger(__name__) class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[Any] = ['''input_features''', '''is_longer'''] def __init__( self : str , __lowerCAmelCase : Union[str, Any]=64 , __lowerCAmelCase : Optional[Any]=4_80_00 , __lowerCAmelCase : str=4_80 , __lowerCAmelCase : Optional[int]=10 , __lowerCAmelCase : str=10_24 , __lowerCAmelCase : List[str]=0.0 , __lowerCAmelCase : int=False , __lowerCAmelCase : float = 0 , __lowerCAmelCase : float = 1_40_00 , __lowerCAmelCase : int = None , __lowerCAmelCase : str = "fusion" , __lowerCAmelCase : str = "repeatpad" , **__lowerCAmelCase : Optional[int] , ) -> str: """simple docstring""" super().__init__( feature_size=__lowerCAmelCase , sampling_rate=__lowerCAmelCase , padding_value=__lowerCAmelCase , return_attention_mask=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = top_db A__ = truncation A__ = padding A__ = fft_window_size A__ = (fft_window_size >> 1) + 1 A__ = hop_length A__ = max_length_s A__ = max_length_s * sampling_rate A__ = sampling_rate A__ = frequency_min A__ = frequency_max A__ = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=__lowerCAmelCase , min_frequency=__lowerCAmelCase , max_frequency=__lowerCAmelCase , sampling_rate=__lowerCAmelCase , norm=__lowerCAmelCase , mel_scale="""htk""" , ) A__ = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=__lowerCAmelCase , min_frequency=__lowerCAmelCase , max_frequency=__lowerCAmelCase , sampling_rate=__lowerCAmelCase , norm="""slaney""" , mel_scale="""slaney""" , ) def a_ ( self : List[str] ) -> Dict[str, Any]: """simple docstring""" A__ = copy.deepcopy(self.__dict__ ) A__ = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def a_ ( self : Union[str, Any] , __lowerCAmelCase : np.array , __lowerCAmelCase : Optional[np.array] = None ) -> np.ndarray: """simple docstring""" A__ = spectrogram( __lowerCAmelCase , window_function(self.fft_window_size , """hann""" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=__lowerCAmelCase , log_mel="""dB""" , ) return log_mel_spectrogram.T def a_ ( self : Dict , __lowerCAmelCase : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : List[Any] ) -> int: """simple docstring""" A__ = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk A__ = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk A__ = [0] # randomly choose index for each part A__ = np.random.choice(ranges[0] ) A__ = np.random.choice(ranges[1] ) A__ = np.random.choice(ranges[2] ) A__ = mel[idx_front : idx_front + chunk_frames, :] A__ = mel[idx_middle : idx_middle + chunk_frames, :] A__ = mel[idx_back : idx_back + chunk_frames, :] A__ = torch.tensor(mel[None, None, :] ) A__ = torch.nn.functional.interpolate( __lowerCAmelCase , size=[chunk_frames, 64] , mode="""bilinear""" , align_corners=__lowerCAmelCase ) A__ = mel_shrink[0][0].numpy() A__ = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def a_ ( self : Union[str, Any] , __lowerCAmelCase : np.array , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> np.array: """simple docstring""" if waveform.shape[0] > max_length: if truncation == "rand_trunc": A__ = True # random crop to max_length (for compatibility) -> this should be handled by self.pad A__ = len(__lowerCAmelCase ) - max_length A__ = np.random.randint(0 , overflow + 1 ) A__ = waveform[idx : idx + max_length] A__ = self._np_extract_fbank_features(__lowerCAmelCase , self.mel_filters_slaney )[None, :] elif truncation == "fusion": A__ = self._np_extract_fbank_features(__lowerCAmelCase , self.mel_filters ) A__ = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed A__ = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. A__ = np.stack([mel, mel, mel, mel] , axis=0 ) A__ = False else: A__ = self._random_mel_fusion(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = True else: raise NotImplementedError(f'data_truncating {truncation} not implemented' ) else: A__ = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": A__ = int(max_length / len(__lowerCAmelCase ) ) A__ = np.stack(np.tile(__lowerCAmelCase , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": A__ = int(max_length / len(__lowerCAmelCase ) ) A__ = np.stack(np.tile(__lowerCAmelCase , __lowerCAmelCase ) ) A__ = np.pad(__lowerCAmelCase , (0, max_length - waveform.shape[0]) , mode="""constant""" , constant_values=0 ) if truncation == "fusion": A__ = self._np_extract_fbank_features(__lowerCAmelCase , self.mel_filters ) A__ = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: A__ = self._np_extract_fbank_features(__lowerCAmelCase , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : int , __lowerCAmelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , __lowerCAmelCase : str = None , __lowerCAmelCase : Optional[str] = None , __lowerCAmelCase : Optional[int] = None , __lowerCAmelCase : Optional[int] = None , __lowerCAmelCase : Optional[Union[str, TensorType]] = None , **__lowerCAmelCase : Optional[int] , ) -> BatchFeature: """simple docstring""" A__ = truncation if truncation is not None else self.truncation A__ = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f'The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a' f' sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input' f' was sampled with {self.sampling_rate} and not {sampling_rate}.' ) else: logger.warning( """It is strongly recommended to pass the `sampling_rate` argument to this function. """ """Failing to do so can result in silent errors that might be hard to debug.""" ) A__ = isinstance(__lowerCAmelCase , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f'Only mono-channel audio is supported for input to {self}' ) A__ = is_batched_numpy or ( isinstance(__lowerCAmelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A__ = [np.asarray(__lowerCAmelCase , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(__lowerCAmelCase , np.ndarray ): A__ = np.asarray(__lowerCAmelCase , dtype=np.floataa ) elif isinstance(__lowerCAmelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): A__ = raw_speech.astype(np.floataa ) # always return batch if not is_batched: A__ = [np.asarray(__lowerCAmelCase )] # convert to mel spectrogram, truncate and pad if needed. A__ = [ self._get_input_mel(__lowerCAmelCase , max_length if max_length else self.nb_max_samples , __lowerCAmelCase , __lowerCAmelCase ) for waveform in raw_speech ] A__ = [] A__ = [] for mel, longer in padded_inputs: input_mel.append(__lowerCAmelCase ) is_longer.append(__lowerCAmelCase ) if truncation == "fusion" and sum(__lowerCAmelCase ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer A__ = np.random.randint(0 , len(__lowerCAmelCase ) ) A__ = True if isinstance(input_mel[0] , __lowerCAmelCase ): A__ = [np.asarray(__lowerCAmelCase , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool A__ = [[longer] for longer in is_longer] A__ = {"""input_features""": input_mel, """is_longer""": is_longer} A__ = BatchFeature(__lowerCAmelCase ) if return_tensors is not None: A__ = input_features.convert_to_tensors(__lowerCAmelCase ) return input_features
274
from sklearn.metrics import fa_score import datasets A : Any = ''' The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation: F1 = 2 * (precision * recall) / (precision + recall) ''' A : List[Any] = ''' Args: predictions (`list` of `int`): Predicted labels. references (`list` of `int`): Ground truth labels. labels (`list` of `int`): The set of labels to include when `average` is not set to `\'binary\'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None. pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1. average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`. - \'binary\': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary. - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives. - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - \'weighted\': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. This option can result in an F-score that is not between precision and recall. - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). sample_weight (`list` of `float`): Sample weights Defaults to None. Returns: f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better. Examples: Example 1-A simple binary example >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0]) >>> print(results) {\'f1\': 0.5} Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0) >>> print(round(results[\'f1\'], 2)) 0.67 Example 3-The same simple binary example as in Example 1, but with `sample_weight` included. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3]) >>> print(round(results[\'f1\'], 2)) 0.35 Example 4-A multiclass example, with different values for the `average` input. >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro") >>> print(round(results[\'f1\'], 2)) 0.33 >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {\'f1\': array([0.8, 0. , 0. ])} ''' A : List[Any] = ''' @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A (datasets.Metric ): '''simple docstring''' def a_ ( self : Optional[int] ) -> List[Any]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html"""] , ) def a_ ( self : Any , __lowerCAmelCase : Tuple , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict=None , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : Any="binary" , __lowerCAmelCase : Optional[int]=None ) -> List[Any]: """simple docstring""" A__ = fa_score( __lowerCAmelCase , __lowerCAmelCase , labels=__lowerCAmelCase , pos_label=__lowerCAmelCase , average=__lowerCAmelCase , sample_weight=__lowerCAmelCase ) return {"f1": float(__lowerCAmelCase ) if score.size == 1 else score}
274
1
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_fnet import FNetTokenizer else: A : Dict = None A : Union[str, Any] = logging.get_logger(__name__) A : Optional[Any] = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A : Tuple = { '''vocab_file''': { '''google/fnet-base''': '''https://huggingface.co/google/fnet-base/resolve/main/spiece.model''', '''google/fnet-large''': '''https://huggingface.co/google/fnet-large/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''google/fnet-base''': '''https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json''', '''google/fnet-large''': '''https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json''', }, } A : Optional[Any] = { '''google/fnet-base''': 5_1_2, '''google/fnet-large''': 5_1_2, } A : Tuple = '''▁''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Optional[Any] = VOCAB_FILES_NAMES __lowerCamelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : int = ['''input_ids''', '''token_type_ids'''] __lowerCamelCase : Optional[Any] = FNetTokenizer def __init__( self : Union[str, Any] , __lowerCAmelCase : Tuple=None , __lowerCAmelCase : str=None , __lowerCAmelCase : Dict=False , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : Optional[int]="<unk>" , __lowerCAmelCase : Optional[int]="[SEP]" , __lowerCAmelCase : Any="<pad>" , __lowerCAmelCase : Tuple="[CLS]" , __lowerCAmelCase : int="[MASK]" , **__lowerCAmelCase : Tuple , ) -> str: """simple docstring""" A__ = ( AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase , normalized=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token ) super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def a_ ( self : int , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a_ ( self : Optional[Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" if not os.path.isdir(__lowerCAmelCase ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return A__ = os.path.join( __lowerCAmelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCAmelCase ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
274
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Any = '''xlm-roberta''' def __init__( self : Optional[Any] , __lowerCAmelCase : List[Any]=3_05_22 , __lowerCAmelCase : int=7_68 , __lowerCAmelCase : Tuple=12 , __lowerCAmelCase : str=12 , __lowerCAmelCase : Union[str, Any]=30_72 , __lowerCAmelCase : Union[str, Any]="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-12 , __lowerCAmelCase : Union[str, Any]=1 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Tuple="absolute" , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=None , **__lowerCAmelCase : str , ) -> Optional[Any]: """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": A__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A__ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
274
1
# limitations under the License. from typing import Optional, Tuple, Union import torch from diffusers import DiffusionPipeline, ImagePipelineOutput class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Optional[int] ) -> List[Any]: """simple docstring""" super().__init__() self.register_modules(unet=__lowerCAmelCase , scheduler=__lowerCAmelCase ) @torch.no_grad() def __call__( self : int , __lowerCAmelCase : int = 1 , __lowerCAmelCase : Optional[torch.Generator] = None , __lowerCAmelCase : int = 50 , __lowerCAmelCase : Optional[str] = "pil" , __lowerCAmelCase : bool = True , **__lowerCAmelCase : int , ) -> Union[ImagePipelineOutput, Tuple]: """simple docstring""" A__ = torch.randn( (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size) , generator=__lowerCAmelCase , ) A__ = image.to(self.device ) # set step values self.scheduler.set_timesteps(__lowerCAmelCase ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output A__ = self.unet(__lowerCAmelCase , __lowerCAmelCase ).sample # 2. predict previous mean of image x_t-1 and add variance depending on eta # eta corresponds to η in paper and should be between [0, 1] # do x_t -> x_t-1 A__ = self.scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ).prev_sample A__ = (image / 2 + 0.5).clamp(0 , 1 ) A__ = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": A__ = self.numpy_to_pil(__lowerCAmelCase ) if not return_dict: return (image,), "This is a local test" return ImagePipelineOutput(images=__lowerCAmelCase ), "This is a local test"
274
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean A : str = 0 A : Any = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] A : Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right A : Union[str, Any] = tuple[int, int] class A : '''simple docstring''' def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Node | None , ) -> None: """simple docstring""" A__ = pos_x A__ = pos_y A__ = (pos_y, pos_x) A__ = goal_x A__ = goal_y A__ = g_cost A__ = parent A__ = self.calculate_heuristic() A__ = self.g_cost + self.h_cost def a_ ( self : Dict ) -> float: """simple docstring""" A__ = self.pos_x - self.goal_x A__ = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(__lowerCAmelCase ) + abs(__lowerCAmelCase ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : int , __lowerCAmelCase : Node ) -> bool: """simple docstring""" return self.f_cost < other.f_cost class A : '''simple docstring''' def __init__( self : Union[str, Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> Tuple: """simple docstring""" A__ = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) A__ = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , __lowerCAmelCase ) A__ = [self.start] A__ = [] A__ = False def a_ ( self : List[str] ) -> list[TPosition]: """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() A__ = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) A__ = self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) return [self.start.pos] def a_ ( self : Optional[Any] , __lowerCAmelCase : Node ) -> list[Node]: """simple docstring""" A__ = [] for action in delta: A__ = parent.pos_x + action[1] A__ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def a_ ( self : List[Any] , __lowerCAmelCase : Node | None ) -> list[TPosition]: """simple docstring""" A__ = node A__ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A__ = current_node.parent path.reverse() return path class A : '''simple docstring''' def __init__( self : Optional[Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> None: """simple docstring""" A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = False def a_ ( self : int ) -> list[TPosition]: """simple docstring""" while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() A__ = self.fwd_astar.open_nodes.pop(0 ) A__ = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( __lowerCAmelCase , __lowerCAmelCase ) self.fwd_astar.closed_nodes.append(__lowerCAmelCase ) self.bwd_astar.closed_nodes.append(__lowerCAmelCase ) A__ = current_bwd_node A__ = current_fwd_node A__ = { self.fwd_astar: self.fwd_astar.get_successors(__lowerCAmelCase ), self.bwd_astar: self.bwd_astar.get_successors(__lowerCAmelCase ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = astar.open_nodes.pop( astar.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(__lowerCAmelCase ) else: astar.open_nodes.append(__lowerCAmelCase ) return [self.fwd_astar.start.pos] def a_ ( self : List[str] , __lowerCAmelCase : Node , __lowerCAmelCase : Node ) -> list[TPosition]: """simple docstring""" A__ = self.fwd_astar.retrace_path(__lowerCAmelCase ) A__ = self.bwd_astar.retrace_path(__lowerCAmelCase ) bwd_path.pop() bwd_path.reverse() A__ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] A : Optional[int] = (0, 0) A : int = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) A : Dict = time.time() A : Optional[Any] = AStar(init, goal) A : Optional[int] = a_star.search() A : Optional[int] = time.time() - start_time print(F'''AStar execution time = {end_time:f} seconds''') A : Dict = time.time() A : Tuple = BidirectionalAStar(init, goal) A : List[Any] = time.time() - bd_start_time print(F'''BidirectionalAStar execution time = {bd_end_time:f} seconds''')
274
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available, is_vision_available, ) A : Union[str, Any] = {'''configuration_beit''': ['''BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BeitConfig''', '''BeitOnnxConfig''']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Tuple = ['''BeitFeatureExtractor'''] A : Tuple = ['''BeitImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Any = [ '''BEIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BeitForImageClassification''', '''BeitForMaskedImageModeling''', '''BeitForSemanticSegmentation''', '''BeitModel''', '''BeitPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : str = [ '''FlaxBeitForImageClassification''', '''FlaxBeitForMaskedImageModeling''', '''FlaxBeitModel''', '''FlaxBeitPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_beit import BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, BeitConfig, BeitOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_beit import BeitFeatureExtractor from .image_processing_beit import BeitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_beit import ( BEIT_PRETRAINED_MODEL_ARCHIVE_LIST, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, BeitPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_beit import ( FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel, FlaxBeitPreTrainedModel, ) else: import sys A : Union[str, Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
274
import unittest from transformers.utils.backbone_utils import ( BackboneMixin, get_aligned_output_features_output_indices, verify_out_features_out_indices, ) class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Any ) -> Union[str, Any]: """simple docstring""" A__ = ["""a""", """b""", """c"""] # Defaults to last layer if both are None A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""c"""] ) self.assertEqual(__lowerCAmelCase , [2] ) # Out indices set to match out features A__ , A__ = get_aligned_output_features_output_indices(["""a""", """c"""] , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features set to match out indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [0, 2] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features selected from negative indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [-3, -1] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [-3, -1] ) def a_ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , __lowerCAmelCase ) # Out features must be a list with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(("""a""", """b""") , (0, 1) , ["""a""", """b"""] ) # Out features must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , ["""a"""] ) # Out indices must be a list or tuple with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , 0 , ["""a""", """b"""] ) # Out indices must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , (0, 1) , ["""a"""] ) # Out features and out indices must be the same length with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0,) , ["""a""", """b""", """c"""] ) # Out features should match out indices with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 2) , ["""a""", """b""", """c"""] ) # Out features and out indices should be in order with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""b""", """a"""] , (0, 1) , ["""a""", """b"""] ) # Check passes with valid inputs verify_out_features_out_indices(["""a""", """b""", """d"""] , (0, 1, -1) , ["""a""", """b""", """c""", """d"""] ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" A__ = BackboneMixin() A__ = ["""a""", """b""", """c"""] A__ = ["""a""", """c"""] A__ = [0, 2] # Check that the output features and indices are set correctly self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [0, 2] ) # Check out features and indices are updated correctly A__ = ["""a""", """b"""] self.assertEqual(backbone.out_features , ["""a""", """b"""] ) self.assertEqual(backbone.out_indices , [0, 1] ) A__ = [-3, -1] self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [-3, -1] )
274
1
from __future__ import annotations import json import requests from bsa import BeautifulSoup from fake_useragent import UserAgent A : Any = {'''UserAgent''': UserAgent().random} def __lowerCamelCase ( __a :str ) -> dict: """simple docstring""" A__ = script.contents[0] A__ = json.loads(data[data.find("""{\"config\"""" ) : -1] ) return info["entry_data"]["ProfilePage"][0]["graphql"]["user"] class A : '''simple docstring''' def __init__( self : Tuple , __lowerCAmelCase : Union[str, Any] ) -> Any: """simple docstring""" A__ = f'https://www.instagram.com/{username}/' A__ = self.get_json() def a_ ( self : Tuple ) -> dict: """simple docstring""" A__ = requests.get(self.url , headers=__lowerCAmelCase ).text A__ = BeautifulSoup(__lowerCAmelCase , """html.parser""" ).find_all("""script""" ) try: return extract_user_profile(scripts[4] ) except (json.decoder.JSONDecodeError, KeyError): return extract_user_profile(scripts[3] ) def __repr__( self : List[str] ) -> str: """simple docstring""" return f'{self.__class__.__name__}(\'{self.username}\')' def __str__( self : Tuple ) -> str: """simple docstring""" return f'{self.fullname} ({self.username}) is {self.biography}' @property def a_ ( self : Union[str, Any] ) -> str: """simple docstring""" return self.user_data["username"] @property def a_ ( self : Dict ) -> str: """simple docstring""" return self.user_data["full_name"] @property def a_ ( self : str ) -> str: """simple docstring""" return self.user_data["biography"] @property def a_ ( self : Union[str, Any] ) -> str: """simple docstring""" return self.user_data["business_email"] @property def a_ ( self : Optional[Any] ) -> str: """simple docstring""" return self.user_data["external_url"] @property def a_ ( self : Optional[int] ) -> int: """simple docstring""" return self.user_data["edge_followed_by"]["count"] @property def a_ ( self : List[str] ) -> int: """simple docstring""" return self.user_data["edge_follow"]["count"] @property def a_ ( self : Tuple ) -> int: """simple docstring""" return self.user_data["edge_owner_to_timeline_media"]["count"] @property def a_ ( self : Tuple ) -> str: """simple docstring""" return self.user_data["profile_pic_url_hd"] @property def a_ ( self : Union[str, Any] ) -> bool: """simple docstring""" return self.user_data["is_verified"] @property def a_ ( self : Any ) -> bool: """simple docstring""" return self.user_data["is_private"] def __lowerCamelCase ( __a :str = "github" ) -> None: """simple docstring""" import os if os.environ.get("""CI""" ): return # test failing on GitHub Actions A__ = InstagramUser(__a ) assert instagram_user.user_data assert isinstance(instagram_user.user_data , __a ) assert instagram_user.username == username if username != "github": return assert instagram_user.fullname == "GitHub" assert instagram_user.biography == "Built for developers." assert instagram_user.number_of_posts > 1_5_0 assert instagram_user.number_of_followers > 1_2_0_0_0_0 assert instagram_user.number_of_followings > 1_5 assert instagram_user.email == "support@github.com" assert instagram_user.website == "https://github.com/readme" assert instagram_user.profile_picture_url.startswith("""https://instagram.""" ) assert instagram_user.is_verified is True assert instagram_user.is_private is False if __name__ == "__main__": import doctest doctest.testmod() A : Union[str, Any] = InstagramUser('''github''') print(instagram_user) print(F'''{instagram_user.number_of_posts = }''') print(F'''{instagram_user.number_of_followers = }''') print(F'''{instagram_user.number_of_followings = }''') print(F'''{instagram_user.email = }''') print(F'''{instagram_user.website = }''') print(F'''{instagram_user.profile_picture_url = }''') print(F'''{instagram_user.is_verified = }''') print(F'''{instagram_user.is_private = }''')
274
from collections import deque class A : '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" A__ = process_name # process name A__ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time A__ = arrival_time A__ = burst_time # remaining burst time A__ = 0 # total time of the process wait in ready queue A__ = 0 # time from arrival time to completion time class A : '''simple docstring''' def __init__( self : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : list[int] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int , ) -> None: """simple docstring""" A__ = number_of_queues # time slice of queues that round robin algorithm applied A__ = time_slices # unfinished process is in this ready_queue A__ = queue # current time A__ = current_time # finished process is in this sequence queue A__ = deque() def a_ ( self : Dict ) -> list[str]: """simple docstring""" A__ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def a_ ( self : Tuple , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def a_ ( self : Optional[Any] , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def a_ ( self : Dict , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): completion_times.append(queue[i].stop_time ) return completion_times def a_ ( self : int , __lowerCAmelCase : deque[Process] ) -> list[int]: """simple docstring""" return [q.burst_time for q in queue] def a_ ( self : Any , __lowerCAmelCase : Process ) -> int: """simple docstring""" process.waiting_time += self.current_time - process.stop_time return process.waiting_time def a_ ( self : Union[str, Any] , __lowerCAmelCase : deque[Process] ) -> deque[Process]: """simple docstring""" A__ = deque() # sequence deque of finished process while len(__lowerCAmelCase ) != 0: A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__lowerCAmelCase ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 A__ = 0 # set the process's turnaround time because it is finished A__ = self.current_time - cp.arrival_time # set the completion time A__ = self.current_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def a_ ( self : Optional[Any] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int ) -> tuple[deque[Process], deque[Process]]: """simple docstring""" A__ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__lowerCAmelCase ) ): A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__lowerCAmelCase ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time A__ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__lowerCAmelCase ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished A__ = 0 # set the finish time A__ = self.current_time # update the process' turnaround time because it is finished A__ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def a_ ( self : List[Any] ) -> deque[Process]: """simple docstring""" for i in range(self.number_of_queues - 1 ): A__ , A__ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest A : Union[str, Any] = Process('''P1''', 0, 5_3) A : Optional[Any] = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : int = Process('''P4''', 0, 2_4) A : Any = 3 A : List[Any] = [1_7, 2_5] A : Optional[Any] = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) A : Optional[Any] = Process('''P1''', 0, 5_3) A : int = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : Tuple = Process('''P4''', 0, 2_4) A : Union[str, Any] = 3 A : Optional[Any] = [1_7, 2_5] A : Tuple = deque([Pa, Pa, Pa, Pa]) A : Optional[int] = MLFQ(number_of_queues, time_slices, queue, 0) A : Dict = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( F'''waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print completion times of processes(P1, P2, P3, P4) print( F'''completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print total turnaround times of processes(P1, P2, P3, P4) print( F'''turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print sequence of finished processes print( F'''sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}''' )
274
1
from sklearn.metrics import fa_score import datasets A : Any = ''' The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation: F1 = 2 * (precision * recall) / (precision + recall) ''' A : List[Any] = ''' Args: predictions (`list` of `int`): Predicted labels. references (`list` of `int`): Ground truth labels. labels (`list` of `int`): The set of labels to include when `average` is not set to `\'binary\'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None. pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1. average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`. - \'binary\': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary. - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives. - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - \'weighted\': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. This option can result in an F-score that is not between precision and recall. - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). sample_weight (`list` of `float`): Sample weights Defaults to None. Returns: f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better. Examples: Example 1-A simple binary example >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0]) >>> print(results) {\'f1\': 0.5} Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0) >>> print(round(results[\'f1\'], 2)) 0.67 Example 3-The same simple binary example as in Example 1, but with `sample_weight` included. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3]) >>> print(round(results[\'f1\'], 2)) 0.35 Example 4-A multiclass example, with different values for the `average` input. >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro") >>> print(round(results[\'f1\'], 2)) 0.33 >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {\'f1\': array([0.8, 0. , 0. ])} ''' A : List[Any] = ''' @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A (datasets.Metric ): '''simple docstring''' def a_ ( self : Optional[int] ) -> List[Any]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html"""] , ) def a_ ( self : Any , __lowerCAmelCase : Tuple , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict=None , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : Any="binary" , __lowerCAmelCase : Optional[int]=None ) -> List[Any]: """simple docstring""" A__ = fa_score( __lowerCAmelCase , __lowerCAmelCase , labels=__lowerCAmelCase , pos_label=__lowerCAmelCase , average=__lowerCAmelCase , sample_weight=__lowerCAmelCase ) return {"f1": float(__lowerCAmelCase ) if score.size == 1 else score}
274
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType A : str = logging.get_logger(__name__) A : Union[str, Any] = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Optional[Any] = '''layoutlmv3''' def __init__( self : Tuple , __lowerCAmelCase : Optional[int]=5_02_65 , __lowerCAmelCase : Tuple=7_68 , __lowerCAmelCase : Union[str, Any]=12 , __lowerCAmelCase : Any=12 , __lowerCAmelCase : List[Any]=30_72 , __lowerCAmelCase : Optional[int]="gelu" , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[Any]=0.1 , __lowerCAmelCase : Dict=5_12 , __lowerCAmelCase : Any=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-5 , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : int=0 , __lowerCAmelCase : Dict=2 , __lowerCAmelCase : Tuple=10_24 , __lowerCAmelCase : List[str]=1_28 , __lowerCAmelCase : Optional[int]=1_28 , __lowerCAmelCase : Any=True , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Any=1_28 , __lowerCAmelCase : str=64 , __lowerCAmelCase : Optional[int]=2_56 , __lowerCAmelCase : int=True , __lowerCAmelCase : int=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : int=2_24 , __lowerCAmelCase : Dict=3 , __lowerCAmelCase : List[Any]=16 , __lowerCAmelCase : Dict=None , **__lowerCAmelCase : Optional[Any] , ) -> Dict: """simple docstring""" super().__init__( vocab_size=__lowerCAmelCase , hidden_size=__lowerCAmelCase , num_hidden_layers=__lowerCAmelCase , num_attention_heads=__lowerCAmelCase , intermediate_size=__lowerCAmelCase , hidden_act=__lowerCAmelCase , hidden_dropout_prob=__lowerCAmelCase , attention_probs_dropout_prob=__lowerCAmelCase , max_position_embeddings=__lowerCAmelCase , type_vocab_size=__lowerCAmelCase , initializer_range=__lowerCAmelCase , layer_norm_eps=__lowerCAmelCase , pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = max_ad_position_embeddings A__ = coordinate_size A__ = shape_size A__ = has_relative_attention_bias A__ = rel_pos_bins A__ = max_rel_pos A__ = has_spatial_attention_bias A__ = rel_ad_pos_bins A__ = max_rel_ad_pos A__ = text_embed A__ = visual_embed A__ = input_size A__ = num_channels A__ = patch_size A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[str] = version.parse('''1.12''' ) @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def a_ ( self : Optional[int] ) -> float: """simple docstring""" return 1e-5 @property def a_ ( self : Tuple ) -> int: """simple docstring""" return 12 def a_ ( self : str , __lowerCAmelCase : "ProcessorMixin" , __lowerCAmelCase : int = -1 , __lowerCAmelCase : int = -1 , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional["TensorType"] = None , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 40 , __lowerCAmelCase : int = 40 , ) -> Mapping[str, Any]: """simple docstring""" setattr(processor.image_processor , """apply_ocr""" , __lowerCAmelCase ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX A__ = processor.tokenizer.num_special_tokens_to_add(__lowerCAmelCase ) A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__lowerCAmelCase ) # Generate dummy inputs according to compute batch and sequence A__ = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes A__ = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) A__ = self._generate_dummy_images(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = dict( processor( __lowerCAmelCase , text=__lowerCAmelCase , boxes=__lowerCAmelCase , return_tensors=__lowerCAmelCase , ) ) return inputs
274
1
import math import time from typing import Dict, List, Optional from torch.utils.data import Dataset from transformers import SeqaSeqTrainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Union[str, Any] , *__lowerCAmelCase : List[Any] , __lowerCAmelCase : Optional[int]=None , __lowerCAmelCase : Dict=None , **__lowerCAmelCase : Tuple ) -> Union[str, Any]: """simple docstring""" super().__init__(*__lowerCAmelCase , **__lowerCAmelCase ) A__ = eval_examples A__ = post_process_function def a_ ( self : Dict , __lowerCAmelCase : Optional[Dataset] = None , __lowerCAmelCase : Union[str, Any]=None , __lowerCAmelCase : Optional[List[str]] = None , __lowerCAmelCase : str = "eval" , **__lowerCAmelCase : List[str] , ) -> Dict[str, float]: """simple docstring""" A__ = gen_kwargs.copy() A__ = ( gen_kwargs["""max_length"""] if gen_kwargs.get("""max_length""" ) is not None else self.args.generation_max_length ) A__ = ( gen_kwargs["""num_beams"""] if gen_kwargs.get("""num_beams""" ) is not None else self.args.generation_num_beams ) A__ = gen_kwargs A__ = self.eval_dataset if eval_dataset is None else eval_dataset A__ = self.get_eval_dataloader(__lowerCAmelCase ) A__ = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. A__ = self.compute_metrics A__ = None A__ = time.time() A__ = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: A__ = eval_loop( __lowerCAmelCase , description="""Evaluation""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=__lowerCAmelCase , metric_key_prefix=__lowerCAmelCase , ) finally: A__ = compute_metrics A__ = self.args.eval_batch_size * self.args.world_size if f'{metric_key_prefix}_jit_compilation_time' in output.metrics: start_time += output.metrics[f'{metric_key_prefix}_jit_compilation_time'] output.metrics.update( speed_metrics( __lowerCAmelCase , __lowerCAmelCase , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default A__ = self.post_process_function(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = self.compute_metrics(__lowerCAmelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f'{metric_key_prefix}_' ): A__ = metrics.pop(__lowerCAmelCase ) metrics.update(output.metrics ) else: A__ = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(__lowerCAmelCase ) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) A__ = self.callback_handler.on_evaluate(self.args , self.state , self.control , __lowerCAmelCase ) return metrics def a_ ( self : List[str] , __lowerCAmelCase : Dict , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : str = "test" , **__lowerCAmelCase : Union[str, Any] ) -> Any: """simple docstring""" A__ = gen_kwargs.copy() A__ = self.get_test_dataloader(__lowerCAmelCase ) # Temporarily disable metric computation, we will do it in the loop here. A__ = self.compute_metrics A__ = None A__ = time.time() A__ = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: A__ = eval_loop( __lowerCAmelCase , description="""Prediction""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=__lowerCAmelCase , metric_key_prefix=__lowerCAmelCase , ) finally: A__ = compute_metrics A__ = self.args.eval_batch_size * self.args.world_size if f'{metric_key_prefix}_jit_compilation_time' in output.metrics: start_time += output.metrics[f'{metric_key_prefix}_jit_compilation_time'] output.metrics.update( speed_metrics( __lowerCAmelCase , __lowerCAmelCase , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is None or self.compute_metrics is None: return output A__ = self.post_process_function(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , """predict""" ) A__ = self.compute_metrics(__lowerCAmelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f'{metric_key_prefix}_' ): A__ = metrics.pop(__lowerCAmelCase ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=__lowerCAmelCase )
274
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) A : str = '''▁''' A : Any = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', '''tokenizer_config_file''': '''tokenizer_config.json''', } A : List[Any] = { '''vocab_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json''', }, '''spm_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_config_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json''', }, } A : Tuple = { '''facebook/m2m100_418M''': 1_0_2_4, } # fmt: off A : Optional[int] = { '''m2m100''': ['''af''', '''am''', '''ar''', '''ast''', '''az''', '''ba''', '''be''', '''bg''', '''bn''', '''br''', '''bs''', '''ca''', '''ceb''', '''cs''', '''cy''', '''da''', '''de''', '''el''', '''en''', '''es''', '''et''', '''fa''', '''ff''', '''fi''', '''fr''', '''fy''', '''ga''', '''gd''', '''gl''', '''gu''', '''ha''', '''he''', '''hi''', '''hr''', '''ht''', '''hu''', '''hy''', '''id''', '''ig''', '''ilo''', '''is''', '''it''', '''ja''', '''jv''', '''ka''', '''kk''', '''km''', '''kn''', '''ko''', '''lb''', '''lg''', '''ln''', '''lo''', '''lt''', '''lv''', '''mg''', '''mk''', '''ml''', '''mn''', '''mr''', '''ms''', '''my''', '''ne''', '''nl''', '''no''', '''ns''', '''oc''', '''or''', '''pa''', '''pl''', '''ps''', '''pt''', '''ro''', '''ru''', '''sd''', '''si''', '''sk''', '''sl''', '''so''', '''sq''', '''sr''', '''ss''', '''su''', '''sv''', '''sw''', '''ta''', '''th''', '''tl''', '''tn''', '''tr''', '''uk''', '''ur''', '''uz''', '''vi''', '''wo''', '''xh''', '''yi''', '''yo''', '''zh''', '''zu'''], '''wmt21''': ['''en''', '''ha''', '''is''', '''ja''', '''cs''', '''ru''', '''zh''', '''de'''] } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = VOCAB_FILES_NAMES __lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = ['''input_ids''', '''attention_mask'''] __lowerCamelCase : List[int] = [] __lowerCamelCase : List[int] = [] def __init__( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any]=None , __lowerCAmelCase : str=None , __lowerCAmelCase : List[Any]="<s>" , __lowerCAmelCase : List[Any]="</s>" , __lowerCAmelCase : Optional[int]="</s>" , __lowerCAmelCase : Optional[Any]="<pad>" , __lowerCAmelCase : Any="<unk>" , __lowerCAmelCase : Any="m2m100" , __lowerCAmelCase : Optional[Dict[str, Any]] = None , __lowerCAmelCase : Dict=8 , **__lowerCAmelCase : Tuple , ) -> None: """simple docstring""" A__ = {} if sp_model_kwargs is None else sp_model_kwargs A__ = language_codes A__ = FAIRSEQ_LANGUAGE_CODES[language_codes] A__ = {lang_code: f'__{lang_code}__' for lang_code in fairseq_language_code} A__ = kwargs.get("""additional_special_tokens""" , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(__lowerCAmelCase ) for lang_code in fairseq_language_code if self.get_lang_token(__lowerCAmelCase ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=__lowerCAmelCase , tgt_lang=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , language_codes=__lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = vocab_file A__ = load_json(__lowerCAmelCase ) A__ = {v: k for k, v in self.encoder.items()} A__ = spm_file A__ = load_spm(__lowerCAmelCase , self.sp_model_kwargs ) A__ = len(self.encoder ) A__ = { self.get_lang_token(__lowerCAmelCase ): self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase ) } A__ = {lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase )} A__ = {v: k for k, v in self.lang_token_to_id.items()} A__ = src_lang if src_lang is not None else """en""" A__ = tgt_lang A__ = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) A__ = num_madeup_words @property def a_ ( self : Optional[int] ) -> int: """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def a_ ( self : Optional[Any] ) -> str: """simple docstring""" return self._src_lang @src_lang.setter def a_ ( self : List[Any] , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def a_ ( self : Optional[int] , __lowerCAmelCase : str ) -> List[str]: """simple docstring""" return self.sp_model.encode(__lowerCAmelCase , out_type=__lowerCAmelCase ) def a_ ( self : Optional[Any] , __lowerCAmelCase : Dict ) -> Optional[Any]: """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(__lowerCAmelCase , self.encoder[self.unk_token] ) def a_ ( self : Optional[int] , __lowerCAmelCase : int ) -> str: """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(__lowerCAmelCase , self.unk_token ) def a_ ( self : Optional[int] , __lowerCAmelCase : Dict ) -> str: """simple docstring""" A__ = [] A__ = """""" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(__lowerCAmelCase ) + token A__ = [] else: current_sub_tokens.append(__lowerCAmelCase ) out_string += self.sp_model.decode(__lowerCAmelCase ) return out_string.strip() def a_ ( self : List[str] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None , __lowerCAmelCase : bool = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCAmelCase , token_ids_a=__lowerCAmelCase , already_has_special_tokens=__lowerCAmelCase ) A__ = [1] * len(self.prefix_tokens ) A__ = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(__lowerCAmelCase )) + suffix_ones return prefix_ones + ([0] * len(__lowerCAmelCase )) + ([0] * len(__lowerCAmelCase )) + suffix_ones def a_ ( self : Tuple , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def a_ ( self : int ) -> Dict: """simple docstring""" A__ = {self.convert_ids_to_tokens(__lowerCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.__dict__.copy() A__ = None return state def __setstate__( self : str , __lowerCAmelCase : Dict ) -> None: """simple docstring""" A__ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): A__ = {} A__ = load_spm(self.spm_file , self.sp_model_kwargs ) def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" A__ = Path(__lowerCAmelCase ) if not save_dir.is_dir(): raise OSError(f'{save_directory} should be a directory' ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , __lowerCAmelCase ) if os.path.abspath(self.spm_file ) != os.path.abspath(__lowerCAmelCase ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , __lowerCAmelCase ) elif not os.path.isfile(self.spm_file ): with open(__lowerCAmelCase , """wb""" ) as fi: A__ = self.sp_model.serialized_model_proto() fi.write(__lowerCAmelCase ) return (str(__lowerCAmelCase ), str(__lowerCAmelCase )) def a_ ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : str = "en" , __lowerCAmelCase : Optional[List[str]] = None , __lowerCAmelCase : str = "ro" , **__lowerCAmelCase : List[Any] , ) -> BatchEncoding: """simple docstring""" A__ = src_lang A__ = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(__lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[str] , __lowerCAmelCase : Optional[str] , **__lowerCAmelCase : Tuple ) -> Tuple: """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""" ) A__ = src_lang A__ = self(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , **__lowerCAmelCase ) A__ = self.get_lang_id(__lowerCAmelCase ) A__ = tgt_lang_id return inputs def a_ ( self : Dict ) -> int: """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def a_ ( self : str , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Tuple , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> str: """simple docstring""" return self.lang_code_to_token[lang] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> int: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) return self.lang_token_to_id[lang_token] def __lowerCamelCase ( __a :str , __a :Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: """simple docstring""" A__ = sentencepiece.SentencePieceProcessor(**__a ) spm.Load(str(__a ) ) return spm def __lowerCamelCase ( __a :str ) -> Union[Dict, List]: """simple docstring""" with open(__a , """r""" ) as f: return json.load(__a ) def __lowerCamelCase ( __a :List[Any] , __a :str ) -> None: """simple docstring""" with open(__a , """w""" ) as f: json.dump(__a , __a , indent=2 )
274
1
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean A : str = 0 A : Any = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] A : Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right A : Union[str, Any] = tuple[int, int] class A : '''simple docstring''' def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Node | None , ) -> None: """simple docstring""" A__ = pos_x A__ = pos_y A__ = (pos_y, pos_x) A__ = goal_x A__ = goal_y A__ = g_cost A__ = parent A__ = self.calculate_heuristic() A__ = self.g_cost + self.h_cost def a_ ( self : Dict ) -> float: """simple docstring""" A__ = self.pos_x - self.goal_x A__ = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(__lowerCAmelCase ) + abs(__lowerCAmelCase ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : int , __lowerCAmelCase : Node ) -> bool: """simple docstring""" return self.f_cost < other.f_cost class A : '''simple docstring''' def __init__( self : Union[str, Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> Tuple: """simple docstring""" A__ = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) A__ = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , __lowerCAmelCase ) A__ = [self.start] A__ = [] A__ = False def a_ ( self : List[str] ) -> list[TPosition]: """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() A__ = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) A__ = self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) return [self.start.pos] def a_ ( self : Optional[Any] , __lowerCAmelCase : Node ) -> list[Node]: """simple docstring""" A__ = [] for action in delta: A__ = parent.pos_x + action[1] A__ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def a_ ( self : List[Any] , __lowerCAmelCase : Node | None ) -> list[TPosition]: """simple docstring""" A__ = node A__ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A__ = current_node.parent path.reverse() return path class A : '''simple docstring''' def __init__( self : Optional[Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> None: """simple docstring""" A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = False def a_ ( self : int ) -> list[TPosition]: """simple docstring""" while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() A__ = self.fwd_astar.open_nodes.pop(0 ) A__ = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( __lowerCAmelCase , __lowerCAmelCase ) self.fwd_astar.closed_nodes.append(__lowerCAmelCase ) self.bwd_astar.closed_nodes.append(__lowerCAmelCase ) A__ = current_bwd_node A__ = current_fwd_node A__ = { self.fwd_astar: self.fwd_astar.get_successors(__lowerCAmelCase ), self.bwd_astar: self.bwd_astar.get_successors(__lowerCAmelCase ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = astar.open_nodes.pop( astar.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(__lowerCAmelCase ) else: astar.open_nodes.append(__lowerCAmelCase ) return [self.fwd_astar.start.pos] def a_ ( self : List[str] , __lowerCAmelCase : Node , __lowerCAmelCase : Node ) -> list[TPosition]: """simple docstring""" A__ = self.fwd_astar.retrace_path(__lowerCAmelCase ) A__ = self.bwd_astar.retrace_path(__lowerCAmelCase ) bwd_path.pop() bwd_path.reverse() A__ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] A : Optional[int] = (0, 0) A : int = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) A : Dict = time.time() A : Optional[Any] = AStar(init, goal) A : Optional[int] = a_star.search() A : Optional[int] = time.time() - start_time print(F'''AStar execution time = {end_time:f} seconds''') A : Dict = time.time() A : Tuple = BidirectionalAStar(init, goal) A : List[Any] = time.time() - bd_start_time print(F'''BidirectionalAStar execution time = {bd_end_time:f} seconds''')
274
from __future__ import annotations from PIL import Image # Define glider example A : Any = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [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], ] # Define blinker example A : Optional[Any] = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def __lowerCamelCase ( __a :list[list[int]] ) -> list[list[int]]: """simple docstring""" A__ = [] for i in range(len(__a ) ): A__ = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours A__ = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(__a ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(__a ) - 1: neighbour_count += cells[i + 1][j] if i < len(__a ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. A__ = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(__a ) return next_generation def __lowerCamelCase ( __a :list[list[int]] , __a :int ) -> list[Image.Image]: """simple docstring""" A__ = [] for _ in range(__a ): # Create output image A__ = Image.new("""RGB""" , (len(cells[0] ), len(__a )) ) A__ = img.load() # Save cells to image for x in range(len(__a ) ): for y in range(len(cells[0] ) ): A__ = 2_5_5 - cells[y][x] * 2_5_5 A__ = (colour, colour, colour) # Save image images.append(__a ) A__ = new_generation(__a ) return images if __name__ == "__main__": A : str = generate_images(GLIDER, 1_6) images[0].save('''out.gif''', save_all=True, append_images=images[1:])
274
1
from sympy import diff, lambdify, symbols from sympy.functions import * # noqa: F403 def __lowerCamelCase ( __a :str , __a :complex , __a :str = "x" , __a :float = 1_0**-1_0 , __a :int = 1 , ) -> complex: """simple docstring""" A__ = symbols(__a ) A__ = lambdify(__a , __a ) A__ = lambdify(__a , diff(__a , __a ) ) A__ = starting_point while True: if diff_function(__a ) != 0: A__ = prev_guess - multiplicity * func(__a ) / diff_function( __a ) else: raise ZeroDivisionError("""Could not find root""" ) from None # Precision is checked by comparing the difference of consecutive guesses if abs(next_guess - prev_guess ) < precision: return next_guess A__ = next_guess # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(F'''The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}''') # Find root of polynomial # Find fourth Root of 5 print(F'''The root of x**4 - 5 = 0 is {newton_raphson("x**4 -5", 0.4 +5J)}''') # Find value of e print( '''The root of log(y) - 1 = 0 is ''', F'''{newton_raphson("log(y) - 1", 2, variable="y")}''', ) # Exponential Roots print( '''The root of exp(x) - 1 = 0 is''', F'''{newton_raphson("exp(x) - 1", 1_0, precision=0.005)}''', ) # Find root of cos(x) print(F'''The root of cos(x) = 0 is {newton_raphson("cos(x)", 0)}''')
274
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": A : List[str] = input('''Enter image url: ''').strip() print(F'''Downloading image from {url} ...''') A : Any = BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image A : List[Any] = soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] A : Dict = requests.get(image_url).content A : Tuple = F'''{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg''' with open(file_name, '''wb''') as fp: fp.write(image_data) print(F'''Done. Image saved to disk as {file_name}.''')
274
1
import json import os import unittest from typing import Tuple from transformers import WavaVecaPhonemeCTCTokenizer from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.models.wavaveca_phoneme.tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizerOutput from transformers.testing_utils import require_phonemizer from ...test_tokenization_common import TokenizerTesterMixin @require_phonemizer class A (SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = WavaVecaPhonemeCTCTokenizer __lowerCamelCase : Optional[Any] = False def a_ ( self : Tuple ) -> Any: """simple docstring""" super().setUp() A__ = ( """<s> <pad> </s> <unk> n s t ə l a i k d m ɛ ɾ e ɪ p o ɐ z ð f j v b ɹ ʁ ʊ iː r w ʌ u ɡ æ aɪ ʃ h ɔ ɑː """ """ŋ ɚ eɪ β uː y ɑ̃ oʊ ᵻ eː θ aʊ ts oː ɔ̃ ɣ ɜ ɑ dʒ əl x ɜː ç ʒ tʃ ɔː ɑːɹ ɛ̃ ʎ ɔːɹ ʋ aː ɕ œ ø oːɹ ɲ yː """ """ʔ iə i5 s. tɕ ?? nʲ ɛː œ̃ ɭ ɔø ʑ tʲ ɨ ɛɹ ts. rʲ ɪɹ ɭʲ i.5 ɔɪ q sʲ u5 ʊɹ iɜ a5 iɛ5 øː ʕ ja əɜ th ɑ5 """ """oɪ dʲ ə5 tɕh ts.h mʲ ɯ dʑ vʲ e̞ tʃʲ ei5 o5 onɡ5 ɑu5 iɑ5 ai5 aɪɚ kh ə1 ʐ i2 ʉ ħ t[ aɪə ʲ ju ə2 u2 oɜ """ """pː iɛɜ ou5 y5 uɜ tː uo5 d[ uoɜ tsh ɑɜ ɵ i̪5 uei5 ɟ aɜ ɑɨ i.ɜ eʊ o2 ɐ̃ ä pʲ kʲ n̩ ɒ ph ɑu2 uɨ əɪ ɫ ɬ """ """yɜ bʲ ɑ2 s̪ aiɜ χ ɐ̃ʊ̃ 1 ə4 yæɜ a2 ɨː t̪ iouɜ ũ onɡɜ aɨ iɛ2 ɔɨ ɑuɜ o̞ ei2 iou2 c kː y2 ɖ oe dˤ yɛɜ """ """əʊ S ɡʲ onɡ2 u\" eiɜ ʈ ɯᵝ iou5 dZ r̝̊ i.2 tS s^ ʝ yə5 iɑɜ uə5 pf ɨu iɑ2 ou2 ər2 fʲ ai2 r̝ uəɜ ɳ əɨ """ """ua5 uɪ ɽ bː yu5 uo2 yɛ5 l̩ ɻ ərɜ ʂ i̪2 ouɜ uaɜ a. a.ː yæ5 dː r̩ ee ɪu ər5 i̪ ɜ æi u: i.ː t^ o1 ɪ^ """ """ai ueiɜ æː ɛɪ eə i. ɴ ie ua2 ɑ1 o4 tʃː o: ɑ: u1 N i̪1 au yæ2 u. qː yəɜ y: kʰ tʃʰ iʊ sx õ uo tʰ """ """uai5 bʰ u.ː uə2 ʊə d^ s̪ː yiɜ dʰ r. oe: i1 ɟː yu2 nʲʲ i̪4 uei2 tsʲ ɸ ĩ ɑ4 t̪ː eɑ u4 e: tsː ʈʰ ɡʰ """ """ɯɯ dʒʲ ʂʲ X ɵː uaiɜ tɕʲ ã t^ː ẽː yɛ2 cː i.1 ɛʊ dˤdˤ dʒː i4 ɡː yi ɕʲ ɟʰ pʰ dʑʲ yuɜ ua1 ua4 æiː ɐɐ """ """ui iou1 ʊː a1 iou4 cʰ iɛ1 yə2 ɖʰ ẽ ʒʲ ää ər4 iːː ɪː iɑ1 ər1 œː øi ɪuː cʰcʰ əː1 iː1 ũ kʰː o̞o̞ xʲ """ """ou1 iɛ4 e̞e̞ y1 dzː dʲʲ dʰː ɯᵝɯᵝ lː uo1 i.4 i: yɛ5ʲ a4""" ).split(""" """ ) A__ = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) A__ = {"""pad_token""": """<pad>""", """unk_token""": """<unk>""", """bos_token""": """<s>""", """eos_token""": """</s>"""} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(__lowerCAmelCase ) + """\n""" ) def a_ ( self : Tuple , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Tuple=False , __lowerCAmelCase : str=20 , __lowerCAmelCase : Optional[Any]=5 ) -> Tuple[str, list]: """simple docstring""" A__ = [(i, tokenizer.decode([i] , clean_up_tokenization_spaces=__lowerCAmelCase )) for i in range(len(__lowerCAmelCase ) )] A__ = list(filter(lambda __lowerCAmelCase : [t[0]] == tokenizer.encode(t[1] , do_phonemize=__lowerCAmelCase ) , __lowerCAmelCase ) ) if max_length is not None and len(__lowerCAmelCase ) > max_length: A__ = toks[:max_length] if min_length is not None and len(__lowerCAmelCase ) < min_length and len(__lowerCAmelCase ) > 0: while len(__lowerCAmelCase ) < min_length: A__ = toks + toks # toks_str = [t[1] for t in toks] A__ = [t[0] for t in toks] # Ensure consistency A__ = tokenizer.decode(__lowerCAmelCase , clean_up_tokenization_spaces=__lowerCAmelCase ) if " " not in output_txt and len(__lowerCAmelCase ) > 1: A__ = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=__lowerCAmelCase ) + """ """ + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=__lowerCAmelCase ) ) if with_prefix_space: A__ = """ """ + output_txt A__ = tokenizer.encode(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase ) return output_txt, output_ids def a_ ( self : Any , **__lowerCAmelCase : str ) -> Tuple: """simple docstring""" kwargs.update(self.special_tokens_map ) return WavaVecaPhonemeCTCTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Optional[int]: """simple docstring""" A__ = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) # check adding a single token tokenizer.add_tokens("""xxx""" ) A__ = tokenizer("""m xxx ɪ""" , do_phonemize=__lowerCAmelCase ).input_ids self.assertEqual(__lowerCAmelCase , [13, 3_92, 17] ) # xxx should be last token tokenizer.add_tokens(["""aaa""", """bbb""", """ccc"""] ) A__ = tokenizer("""m aaa ɪ ccc""" , do_phonemize=__lowerCAmelCase ).input_ids self.assertEqual(__lowerCAmelCase , [13, 3_93, 17, 3_95] ) # aaa and ccc should be after xxx and 2 after aaa A__ = tokenizer("""maɪ c""" , do_phonemize=__lowerCAmelCase ).input_ids self.assertEqual(__lowerCAmelCase , [3, 2_00] ) # mai should be <unk> (=3) def a_ ( self : List[Any] ) -> Optional[int]: """simple docstring""" A__ = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) A__ = """Hello how are you""" A__ = tokenizer.phonemize(__lowerCAmelCase , phonemizer_lang="""en-us""" ) self.assertEqual(__lowerCAmelCase , """h ə l oʊ h aʊ ɑːɹ j uː""" ) def a_ ( self : Optional[Any] ) -> Dict: """simple docstring""" A__ = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) A__ = """Hello how are you""" A__ = tokenizer.phonemize(__lowerCAmelCase , phonemizer_lang="""en-us""" ) self.assertEqual(tokenizer(__lowerCAmelCase ).input_ids , tokenizer(__lowerCAmelCase , do_phonemize=__lowerCAmelCase ).input_ids ) def a_ ( self : List[str] ) -> Optional[Any]: """simple docstring""" A__ = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) A__ = """Hello how are you""" A__ = tokenizer.phonemize(__lowerCAmelCase , phonemizer_lang="""en-us""" ) A__ = tokenizer.decode(tokenizer(__lowerCAmelCase ).input_ids ) self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : List[str] ) -> Optional[int]: """simple docstring""" A__ = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) A__ = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98], [24, 22, 5, 24, 22, 5, 77], ] A__ = tokenizer.decode(sample_ids[0] ) A__ = tokenizer.batch_decode(__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , batch_tokens[0] ) self.assertEqual(__lowerCAmelCase , ["""k s ɾ ɾ l ɭʲ""", """j ð s j ð s oːɹ"""] ) def a_ ( self : List[Any] ) -> str: """simple docstring""" A__ = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) A__ = """Hello how are you""" A__ = tokenizer.phonemize(__lowerCAmelCase , phonemizer_lang="""en-us""" ) self.assertEqual(__lowerCAmelCase , """h ə l oʊ | h aʊ | ɑːɹ | j uː |""" ) def a_ ( self : List[Any] ) -> Tuple: """simple docstring""" A__ = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) A__ = """Hello how are you""" A__ = tokenizer.phonemize(__lowerCAmelCase , phonemizer_lang="""en-us""" ) self.assertEqual(tokenizer(__lowerCAmelCase ).input_ids , tokenizer(__lowerCAmelCase , do_phonemize=__lowerCAmelCase ).input_ids ) def a_ ( self : List[Any] ) -> Any: """simple docstring""" A__ = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) # fmt: off A__ = [ [11, 5, 15, tokenizer.pad_token_id, tokenizer.word_delimiter_token_id, 15, 8, tokenizer.word_delimiter_token_id, 98], [tokenizer.word_delimiter_token_id, 24, 22, tokenizer.word_delimiter_token_id, 5, 24, 22, 5, 77], ] # fmt: on # decode with word_del_token filter A__ = tokenizer.decode(sample_ids[0] ) A__ = tokenizer.batch_decode(__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , batch_tokens[0] ) self.assertEqual(__lowerCAmelCase , ["""k s ɾ ɾ l ɭʲ""", """j ð s j ð s oːɹ"""] ) # decode with no word_del_token filter A__ = tokenizer.decode(sample_ids[0] , filter_word_delimiter_token=__lowerCAmelCase ) A__ = tokenizer.batch_decode(__lowerCAmelCase , filter_word_delimiter_token=__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , batch_tokens[0] ) self.assertEqual(__lowerCAmelCase , ["""k s ɾ | ɾ l | ɭʲ""", """| j ð | s j ð s oːɹ"""] ) def a_ ( self : Dict ) -> Tuple: """simple docstring""" A__ = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) A__ = """Hello how are you""" A__ = tokenizer.phonemize(__lowerCAmelCase , phonemizer_lang="""en-us""" ) A__ = tokenizer.decode(tokenizer(__lowerCAmelCase ).input_ids , filter_word_delimiter_token=__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" A__ = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) A__ = """Hello how are you""" A__ = tokenizer.phonemize(__lowerCAmelCase , phonemizer_lang="""en-us""" ) A__ = tokenizer.decode(tokenizer(__lowerCAmelCase ).input_ids , filter_word_delimiter_token=__lowerCAmelCase ) self.assertEqual(""" """.join([p.strip() for p in phonemes.split(""" |""" )] ).strip() , __lowerCAmelCase ) def a_ ( self : List[str] ) -> Any: """simple docstring""" A__ = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token=__lowerCAmelCase ) A__ = """Hello how are you""" A__ = tokenizer(__lowerCAmelCase , phonemizer_lang="""en-us""" ).input_ids A__ = tokenizer(__lowerCAmelCase , phonemizer_lang="""fr-fr""" ).input_ids self.assertNotEqual(__lowerCAmelCase , __lowerCAmelCase ) A__ = tokenizer.decode(__lowerCAmelCase ) A__ = tokenizer.decode(__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , """h ə l oʊ h aʊ ɑːɹ j uː""" ) self.assertEqual(__lowerCAmelCase , """ɛ l o h aʊ a ʁ j u""" ) def a_ ( self : List[Any] ) -> Optional[Any]: """simple docstring""" A__ = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) A__ = """Hello how Are you""" A__ = """hello how are you""" A__ = tokenizer(__lowerCAmelCase ).input_ids A__ = tokenizer(__lowerCAmelCase ).input_ids self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Union[str, Any] ) -> Tuple: """simple docstring""" A__ = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) tokenizer.add_tokens(["""!""", """?"""] ) tokenizer.add_special_tokens({"""cls_token""": """$$$"""} ) # fmt: off A__ = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98, 3_92, 3_92, 3_93, 3_92, 3_92, 3_93, 3_94, 3_94], [24, 22, 5, 24, 22, 5, 77, tokenizer.pad_token_id, 3_94, 3_94], ] # fmt: on A__ = tokenizer.batch_decode(__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""k s ɾ ɾ l ɭʲ!?!? $$$""", """j ð s j ð s oːɹ $$$"""] ) @staticmethod def a_ ( __lowerCAmelCase : Tuple , __lowerCAmelCase : int ) -> List[Any]: """simple docstring""" A__ = [d[key] for d in offsets] return retrieved_list def a_ ( self : Dict ) -> int: """simple docstring""" A__ = self.get_tokenizer(word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) # fmt: off # ksssɾɾ|ɾɾ<pad>ɾɾ|<pad>ɾlll|ɭʲ -> k s ɾ ɾ | ɾ l | ɭʲ" A__ = [11, 5, 5, 5, 15, 15, tokenizer.pad_token_id, 15, 15, tokenizer.word_delimiter_token_id, tokenizer.pad_token_id, 15, 8, 8, 8, tokenizer.word_delimiter_token_id, 98] # fmt: on A__ = tokenizer.decode(__lowerCAmelCase , output_char_offsets=__lowerCAmelCase , filter_word_delimiter_token=__lowerCAmelCase ) # check Wav2Vec2CTCTokenizerOutput keys for char self.assertEqual(len(outputs.keys() ) , 2 ) self.assertTrue("""text""" in outputs ) self.assertTrue("""char_offsets""" in outputs ) self.assertTrue(isinstance(__lowerCAmelCase , __lowerCAmelCase ) ) # check that order of chars is correct and identical for both outputs self.assertEqual(""" """.join(self.get_from_offsets(outputs["""char_offsets"""] , """char""" ) ) , outputs.text ) self.assertListEqual( self.get_from_offsets(outputs["""char_offsets"""] , """char""" ) , ["""k""", """s""", """ɾ""", """ɾ""", """|""", """ɾ""", """l""", """|""", """ɭʲ"""] ) # check that offsets are actually correct for char # 0-1 is 11, 1-4 is 5, 4-6 is first 15, 6-7 is <pad> (thus not shown), 7-9 is second 15, 9-10 is word_delimiter_token, # 10-11 is <pad> (thus not shown), 11-12 is third 15, 12-15 is 8, 15-16 is word_delimiter_token, 16-17 is 98 self.assertListEqual( self.get_from_offsets(outputs["""char_offsets"""] , """start_offset""" ) , [0, 1, 4, 7, 9, 11, 12, 15, 16] ) self.assertListEqual( self.get_from_offsets(outputs["""char_offsets"""] , """end_offset""" ) , [1, 4, 6, 9, 10, 12, 15, 16, 17] ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" A__ = self.get_tokenizer(word_delimiter_token="""|""" ) def check_list_tuples_equal(__lowerCAmelCase : Any , __lowerCAmelCase : Tuple ): self.assertTrue(isinstance(__lowerCAmelCase , __lowerCAmelCase ) ) self.assertTrue(isinstance(outputs_list[0] , __lowerCAmelCase ) ) # transform list to ModelOutput A__ = WavaVecaPhonemeCTCTokenizerOutput( {k: [d[k] for d in outputs_list] for k in outputs_list[0]} ) self.assertListEqual(outputs_batch["""text"""] , outputs_batch_a["""text"""] ) def recursive_check(__lowerCAmelCase : List[str] , __lowerCAmelCase : int ): if isinstance(__lowerCAmelCase , __lowerCAmelCase ): [recursive_check(__lowerCAmelCase , __lowerCAmelCase ) for la, la in zip(__lowerCAmelCase , __lowerCAmelCase )] self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) if "char_offsets" in outputs_batch: recursive_check(outputs_batch["""char_offsets"""] , outputs_batch_a["""char_offsets"""] ) # fmt: off A__ = [ [11, 5, 15, tokenizer.pad_token_id, 15, 4, 8, 98, 32, 32, 32, 32, 4, 33, tokenizer.word_delimiter_token_id, 32, 32, 33, 34, 34], [24, 22, 5, tokenizer.word_delimiter_token_id, tokenizer.word_delimiter_token_id, 24, 22, 22, 22, 4, 5, 77, tokenizer.pad_token_id, 22, 22, 4, 34, 34, 34, 34], ] # fmt: on # We assume that `decode` works as expected. All we will check now is # the output type is correct and the output is identical to `decode` # char A__ = tokenizer.batch_decode(__lowerCAmelCase , output_char_offsets=__lowerCAmelCase ) A__ = [tokenizer.decode(__lowerCAmelCase , output_char_offsets=__lowerCAmelCase ) for ids in sample_ids] check_list_tuples_equal(__lowerCAmelCase , __lowerCAmelCase ) @unittest.skip("""Wav2Vec2PhonemeTokenizer always lower cases letters to correctly map to phonemes""" ) def a_ ( self : Any ) -> List[str]: """simple docstring""" pass @unittest.skip("""Wav2Vec2PhonemeTokenizer always puts spaces between phonemes""" ) def a_ ( self : Optional[int] ) -> List[str]: """simple docstring""" pass @unittest.skip("""encodes to text to ids, but decodes ids to phonemes -> not possible to have internal consistency""" ) def a_ ( self : int ) -> str: """simple docstring""" pass @unittest.skip("""Wav2Vec2PhonemeModel has no max model length => no testing""" ) def a_ ( self : Any ) -> Union[str, Any]: """simple docstring""" pass def a_ ( self : str ) -> str: """simple docstring""" A__ = self.get_tokenizers(do_lower_case=__lowerCAmelCase ) for tokenizer in tokenizers: with self.subTest(f'{tokenizer.__class__.__name__}' ): A__ = tokenizer.vocab_size A__ = len(__lowerCAmelCase ) self.assertNotEqual(__lowerCAmelCase , 0 ) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) A__ = ["""aaaaa bbbbbb""", """cccccccccdddddddd"""] A__ = tokenizer.add_tokens(__lowerCAmelCase ) A__ = tokenizer.vocab_size A__ = len(__lowerCAmelCase ) self.assertNotEqual(__lowerCAmelCase , 0 ) self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , len(__lowerCAmelCase ) ) self.assertEqual(__lowerCAmelCase , all_size + len(__lowerCAmelCase ) ) A__ = tokenizer.encode("""aaaaa bbbbbb low cccccccccdddddddd l""" , add_special_tokens=__lowerCAmelCase ) self.assertGreaterEqual(len(__lowerCAmelCase ) , 4 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) A__ = {"""eos_token""": """>>>>|||<||<<|<<""", """pad_token""": """<<<<<|||>|>>>>|>"""} A__ = tokenizer.add_special_tokens(__lowerCAmelCase ) A__ = tokenizer.vocab_size A__ = len(__lowerCAmelCase ) self.assertNotEqual(__lowerCAmelCase , 0 ) self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , len(__lowerCAmelCase ) ) self.assertEqual(__lowerCAmelCase , all_size_a + len(__lowerCAmelCase ) ) A__ = tokenizer.encode( """>>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l""" , add_special_tokens=__lowerCAmelCase ) self.assertGreaterEqual(len(__lowerCAmelCase ) , 6 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[0] , tokens[1] ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokens[-4] ) self.assertEqual(tokens[0] , tokenizer.eos_token_id ) self.assertEqual(tokens[-3] , tokenizer.pad_token_id ) @unittest.skip("""The tokenizer shouldn't be used to encode input IDs (except for labels), only to decode.""" ) def a_ ( self : Any ) -> Union[str, Any]: """simple docstring""" pass @unittest.skip("""The tokenizer shouldn't be used to encode input IDs (except for labels), only to decode.""" ) def a_ ( self : Any ) -> int: """simple docstring""" pass def a_ ( self : List[Any] ) -> Union[str, Any]: """simple docstring""" A__ = self.get_tokenizers(fast=__lowerCAmelCase , do_lower_case=__lowerCAmelCase ) for tokenizer in tokenizers: with self.subTest(f'{tokenizer.__class__.__name__}' ): A__ = ["""ð""", """ɪ""", """s""", """ɪ""", """z""", """ɐ""", """t""", """ɛ""", """k""", """s""", """t"""] A__ = tokenizer.convert_tokens_to_string(__lowerCAmelCase ) self.assertIsInstance(output["""text"""] , __lowerCAmelCase )
274
# This code is adapted from OpenAI's release # https://github.com/openai/human-eval/blob/master/human_eval/execution.py import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def __lowerCamelCase ( __a :List[str] , __a :List[Any] , __a :Union[str, Any] , __a :List[Any] ) -> Dict: """simple docstring""" A__ = multiprocessing.Manager() A__ = manager.list() A__ = multiprocessing.Process(target=__a , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append("""timed out""" ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def __lowerCamelCase ( __a :Optional[Any] , __a :Any , __a :List[Any] ) -> Union[str, Any]: """simple docstring""" with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil A__ = shutil.rmtree A__ = os.rmdir A__ = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: A__ = {} with swallow_io(): with time_limit(__a ): exec(__a , __a ) result.append("""passed""" ) except TimeoutException: result.append("""timed out""" ) except BaseException as e: result.append(F'failed: {e}' ) # Needed for cleaning up. A__ = rmtree A__ = rmdir A__ = chdir @contextlib.contextmanager def __lowerCamelCase ( __a :List[str] ) -> Dict: """simple docstring""" def signal_handler(__a :List[Any] , __a :Optional[Any] ): raise TimeoutException("""Timed out!""" ) signal.setitimer(signal.ITIMER_REAL , __a ) signal.signal(signal.SIGALRM , __a ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = WriteOnlyStringIO() with contextlib.redirect_stdout(__a ): with contextlib.redirect_stderr(__a ): with redirect_stdin(__a ): yield @contextlib.contextmanager def __lowerCamelCase ( ) -> Dict: """simple docstring""" with tempfile.TemporaryDirectory() as dirname: with chdir(__a ): yield dirname class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (io.StringIO ): '''simple docstring''' def a_ ( self : Any , *__lowerCAmelCase : List[str] , **__lowerCAmelCase : str ) -> Dict: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Optional[int] ) -> str: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Any ) -> int: """simple docstring""" raise OSError def a_ ( self : str , *__lowerCAmelCase : Any , **__lowerCAmelCase : Union[str, Any] ) -> int: """simple docstring""" return False class A (contextlib._RedirectStream ): # type: ignore '''simple docstring''' __lowerCamelCase : Union[str, Any] = '''stdin''' @contextlib.contextmanager def __lowerCamelCase ( __a :Union[str, Any] ) -> List[str]: """simple docstring""" if root == ".": yield return A__ = os.getcwd() os.chdir(__a ) try: yield except BaseException as exc: raise exc finally: os.chdir(__a ) def __lowerCamelCase ( __a :Union[str, Any]=None ) -> Dict: """simple docstring""" if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins A__ = None A__ = None import os A__ = """1""" A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None import shutil A__ = None A__ = None A__ = None import subprocess A__ = None # type: ignore A__ = None import sys A__ = None A__ = None A__ = None A__ = None A__ = None
274
1
import math def __lowerCamelCase ( __a :int = 1_0_0 ) -> int: """simple docstring""" A__ = sum(i * i for i in range(1 , n + 1 ) ) A__ = int(math.pow(sum(range(1 , n + 1 ) ) , 2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(F'''{solution() = }''')
274
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_albert import AlbertTokenizer else: A : Tuple = None A : Optional[Any] = logging.get_logger(__name__) A : Tuple = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A : List[str] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json''', }, } A : List[str] = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } A : Optional[int] = '''▁''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : str = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = AlbertTokenizer def __init__( self : Tuple , __lowerCAmelCase : Tuple=None , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : Optional[Any]=True , __lowerCAmelCase : List[Any]=True , __lowerCAmelCase : str=False , __lowerCAmelCase : Union[str, Any]="[CLS]" , __lowerCAmelCase : int="[SEP]" , __lowerCAmelCase : Dict="<unk>" , __lowerCAmelCase : Dict="[SEP]" , __lowerCAmelCase : Union[str, Any]="<pad>" , __lowerCAmelCase : str="[CLS]" , __lowerCAmelCase : int="[MASK]" , **__lowerCAmelCase : Optional[Any] , ) -> Optional[Any]: """simple docstring""" A__ = ( AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase , normalized=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token ) super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def a_ ( self : List[Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a_ ( self : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(__lowerCAmelCase ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return A__ = os.path.join( __lowerCAmelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCAmelCase ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
274
1
def __lowerCamelCase ( __a :int ) -> bool: """simple docstring""" if not isinstance(__a , __a ): A__ = F'Input value of [number={number}] must be an integer' raise TypeError(__a ) if number < 0: return False A__ = number * number while number > 0: if number % 1_0 != number_square % 1_0: return False number //= 1_0 number_square //= 1_0 return True if __name__ == "__main__": import doctest doctest.testmod()
274
import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging A : Dict = logging.get_logger(__name__) def __lowerCamelCase ( __a :int=None , __a :Optional[Any]=None ) -> int: """simple docstring""" return field(default_factory=lambda: default , metadata=__a ) @dataclass class A : '''simple docstring''' __lowerCamelCase : List[str] = list_field( default=[] , metadata={ '''help''': ( '''Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version''' ''' of all available models''' ) } , ) __lowerCamelCase : List[int] = list_field( default=[8] , metadata={'''help''': '''List of batch sizes for which memory and time performance will be evaluated'''} ) __lowerCamelCase : List[int] = list_field( default=[8, 32, 128, 512] , metadata={'''help''': '''List of sequence lengths for which memory and time performance will be evaluated'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to benchmark inference of model. Inference can be disabled via --no-inference.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available cuda devices. Cuda can be disabled via --no-cuda.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available tpu devices. TPU can be disabled via --no-tpu.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Use FP16 to accelerate inference.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Benchmark training of model'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Verbose memory tracing'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to perform speed measurements. Speed measurements can be disabled via --no-speed.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': '''Whether to perform memory measurements. Memory measurements can be disabled via --no-memory''' } , ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Trace memory line by line'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save result to a CSV file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save all print statements in a log file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to print environment information'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use''' ''' multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled''' ''' for debugging / testing and on TPU.''' ) } , ) __lowerCamelCase : str = field( default=F'''inference_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''train_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''train_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''env_info_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving environment information.'''} , ) __lowerCamelCase : str = field( default=F'''log_{round(time() )}.csv''' , metadata={'''help''': '''Log filename used if print statements are saved in log.'''} , ) __lowerCamelCase : int = field(default=3 , metadata={'''help''': '''Times an experiment will be run.'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain''' ''' model weights.''' ) } , ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" warnings.warn( f'The class {self.__class__} is deprecated. Hugging Face Benchmarking utils' """ are deprecated in general and it is advised to use external Benchmarking libraries """ """ to benchmark Transformer models.""" , __lowerCAmelCase , ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def a_ ( self : Tuple ) -> List[str]: """simple docstring""" if len(self.models ) <= 0: raise ValueError( """Please make sure you provide at least one model name / model identifier, *e.g.* `--models""" """ bert-base-cased` or `args.models = ['bert-base-cased'].""" ) return self.models @property def a_ ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" if not self.multi_process: return False elif self.is_tpu: logger.info("""Multiprocessing is currently not possible on TPU.""" ) return False else: return True
274
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available A : Tuple = { '''configuration_bloom''': ['''BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BloomConfig''', '''BloomOnnxConfig'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : int = ['''BloomTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[Any] = [ '''BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BloomForCausalLM''', '''BloomModel''', '''BloomPreTrainedModel''', '''BloomForSequenceClassification''', '''BloomForTokenClassification''', '''BloomForQuestionAnswering''', ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys A : List[str] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
274
from math import ceil def __lowerCamelCase ( __a :int = 1_0_0_1 ) -> int: """simple docstring""" A__ = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): A__ = 2 * i + 1 A__ = 2 * i A__ = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: A : List[str] = int(sys.argv[1]) print(solution(n)) except ValueError: print('''Invalid entry - please enter a number''')
274
1
# Copyright 2023 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 torch from accelerate import PartialState from accelerate.utils.operations import broadcast, gather, gather_object, pad_across_processes, reduce def __lowerCamelCase ( __a :Tuple ) -> int: """simple docstring""" return (torch.arange(state.num_processes ) + 1.0 + (state.num_processes * state.process_index)).to(state.device ) def __lowerCamelCase ( __a :Optional[Any] ) -> Tuple: """simple docstring""" A__ = create_tensor(__a ) A__ = gather(__a ) assert gathered_tensor.tolist() == list(range(1 , state.num_processes**2 + 1 ) ) def __lowerCamelCase ( __a :Dict ) -> Dict: """simple docstring""" A__ = [state.process_index] A__ = gather_object(__a ) assert len(__a ) == state.num_processes, F'{gathered_obj}, {len(__a )} != {state.num_processes}' assert gathered_obj == list(range(state.num_processes ) ), F'{gathered_obj} != {list(range(state.num_processes ) )}' def __lowerCamelCase ( __a :List[Any] ) -> int: """simple docstring""" A__ = create_tensor(__a ) A__ = broadcast(__a ) assert broadcasted_tensor.shape == torch.Size([state.num_processes] ) assert broadcasted_tensor.tolist() == list(range(1 , state.num_processes + 1 ) ) def __lowerCamelCase ( __a :List[str] ) -> Union[str, Any]: """simple docstring""" if state.is_main_process: A__ = torch.arange(state.num_processes + 1 ).to(state.device ) else: A__ = torch.arange(state.num_processes ).to(state.device ) A__ = pad_across_processes(__a ) assert padded_tensor.shape == torch.Size([state.num_processes + 1] ) if not state.is_main_process: assert padded_tensor.tolist() == list(range(0 , state.num_processes ) ) + [0] def __lowerCamelCase ( __a :List[str] ) -> Union[str, Any]: """simple docstring""" if state.num_processes != 2: return A__ = create_tensor(__a ) A__ = reduce(__a , """sum""" ) A__ = torch.tensor([4.0, 6] ).to(state.device ) assert torch.allclose(__a , __a ), F'{reduced_tensor} != {truth_tensor}' def __lowerCamelCase ( __a :List[Any] ) -> Dict: """simple docstring""" if state.num_processes != 2: return A__ = create_tensor(__a ) A__ = reduce(__a , """mean""" ) A__ = torch.tensor([2.0, 3] ).to(state.device ) assert torch.allclose(__a , __a ), F'{reduced_tensor} != {truth_tensor}' def __lowerCamelCase ( __a :Dict ) -> List[str]: """simple docstring""" main() def __lowerCamelCase ( ) -> Any: """simple docstring""" A__ = PartialState() state.print(F'State: {state}' ) state.print("""testing gather""" ) test_gather(__a ) state.print("""testing gather_object""" ) test_gather_object(__a ) state.print("""testing broadcast""" ) test_broadcast(__a ) state.print("""testing pad_across_processes""" ) test_pad_across_processes(__a ) state.print("""testing reduce_sum""" ) test_reduce_sum(__a ) state.print("""testing reduce_mean""" ) test_reduce_mean(__a ) if __name__ == "__main__": main()
274
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A : Tuple = logging.getLogger(__name__) def __lowerCamelCase ( __a :Optional[int] , __a :List[str] ) -> Tuple: """simple docstring""" A__ = np.argmax(__a , axis=1 ) return np.sum(outputs == labels ) def __lowerCamelCase ( __a :Tuple ) -> Dict: """simple docstring""" with open(__a , encoding="""utf_8""" ) as f: A__ = csv.reader(__a ) A__ = [] next(__a ) # skip the first line for line in tqdm(__a ): output.append((""" """.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def __lowerCamelCase ( __a :Optional[int] , __a :List[Any] , __a :Dict , __a :Optional[Any] , __a :Optional[Any] , __a :int ) -> Union[str, Any]: """simple docstring""" A__ = [] for dataset in encoded_datasets: A__ = len(__a ) A__ = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) A__ = np.zeros((n_batch, 2) , dtype=np.intaa ) A__ = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) A__ = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(__a ): A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = with_conta A__ = with_conta A__ = len(__a ) - 1 A__ = len(__a ) - 1 A__ = with_conta A__ = with_conta A__ = mc_label A__ = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(__a ) for t in all_inputs ) ) return tensor_datasets def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = argparse.ArgumentParser() parser.add_argument("""--model_name""" , type=__a , default="""openai-gpt""" , help="""pretrained model name""" ) parser.add_argument("""--do_train""" , action="""store_true""" , help="""Whether to run training.""" ) parser.add_argument("""--do_eval""" , action="""store_true""" , help="""Whether to run eval on the dev set.""" ) parser.add_argument( """--output_dir""" , default=__a , type=__a , required=__a , help="""The output directory where the model predictions and checkpoints will be written.""" , ) parser.add_argument("""--train_dataset""" , type=__a , default="""""" ) parser.add_argument("""--eval_dataset""" , type=__a , default="""""" ) parser.add_argument("""--seed""" , type=__a , default=4_2 ) parser.add_argument("""--num_train_epochs""" , type=__a , default=3 ) parser.add_argument("""--train_batch_size""" , type=__a , default=8 ) parser.add_argument("""--eval_batch_size""" , type=__a , default=1_6 ) parser.add_argument("""--adam_epsilon""" , default=1E-8 , type=__a , help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--max_grad_norm""" , type=__a , default=1 ) parser.add_argument( """--max_steps""" , default=-1 , type=__a , help=( """If > 0: set total number of training steps to perform. Override num_train_epochs.""" ) , ) parser.add_argument( """--gradient_accumulation_steps""" , type=__a , default=1 , help="""Number of updates steps to accumulate before performing a backward/update pass.""" , ) parser.add_argument("""--learning_rate""" , type=__a , default=6.25E-5 ) parser.add_argument("""--warmup_steps""" , default=0 , type=__a , help="""Linear warmup over warmup_steps.""" ) parser.add_argument("""--lr_schedule""" , type=__a , default="""warmup_linear""" ) parser.add_argument("""--weight_decay""" , type=__a , default=0.01 ) parser.add_argument("""--lm_coef""" , type=__a , default=0.9 ) parser.add_argument("""--n_valid""" , type=__a , default=3_7_4 ) parser.add_argument("""--server_ip""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) parser.add_argument("""--server_port""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) A__ = parser.parse_args() print(__a ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("""Waiting for debugger attach""" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__a ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) A__ = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) A__ = torch.cuda.device_count() logger.info("""device: {}, n_gpu {}""".format(__a , __a ) ) if not args.do_train and not args.do_eval: raise ValueError("""At least one of `do_train` or `do_eval` must be True.""" ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset A__ = ["""_start_""", """_delimiter_""", """_classify_"""] A__ = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(__a ) A__ = tokenizer.convert_tokens_to_ids(__a ) A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(__a ) ) model.to(__a ) # Load and encode the datasets def tokenize_and_encode(__a :Tuple ): if isinstance(__a , __a ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(__a ) ) elif isinstance(__a , __a ): return obj return [tokenize_and_encode(__a ) for o in obj] logger.info("""Encoding dataset...""" ) A__ = load_rocstories_dataset(args.train_dataset ) A__ = load_rocstories_dataset(args.eval_dataset ) A__ = (train_dataset, eval_dataset) A__ = tokenize_and_encode(__a ) # Compute the max input length for the Transformer A__ = model.config.n_positions // 2 - 2 A__ = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) A__ = min(__a , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders A__ = pre_process_datasets(__a , __a , __a , *__a ) A__ , A__ = tensor_datasets[0], tensor_datasets[1] A__ = TensorDataset(*__a ) A__ = RandomSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.train_batch_size ) A__ = TensorDataset(*__a ) A__ = SequentialSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: A__ = args.max_steps A__ = args.max_steps // (len(__a ) // args.gradient_accumulation_steps) + 1 else: A__ = len(__a ) // args.gradient_accumulation_steps * args.num_train_epochs A__ = list(model.named_parameters() ) A__ = ["""bias""", """LayerNorm.bias""", """LayerNorm.weight"""] A__ = [ { """params""": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], """weight_decay""": args.weight_decay, }, {"""params""": [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], """weight_decay""": 0.0}, ] A__ = AdamW(__a , lr=args.learning_rate , eps=args.adam_epsilon ) A__ = get_linear_schedule_with_warmup( __a , num_warmup_steps=args.warmup_steps , num_training_steps=__a ) if args.do_train: A__ , A__ , A__ = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="""Epoch""" ): A__ = 0 A__ = 0 A__ = tqdm(__a , desc="""Training""" ) for step, batch in enumerate(__a ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch A__ = model(__a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() A__ = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 A__ = """Training loss: {:.2e} lr: {:.2e}""".format(__a , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer A__ = model.module if hasattr(__a , """module""" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` A__ = os.path.join(args.output_dir , __a ) A__ = os.path.join(args.output_dir , __a ) torch.save(model_to_save.state_dict() , __a ) model_to_save.config.to_json_file(__a ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) A__ = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(__a ) if args.do_eval: model.eval() A__ , A__ = 0, 0 A__ , A__ = 0, 0 for batch in tqdm(__a , desc="""Evaluating""" ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch with torch.no_grad(): A__ , A__ , A__ , A__ = model( __a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = mc_logits.detach().cpu().numpy() A__ = mc_labels.to("""cpu""" ).numpy() A__ = accuracy(__a , __a ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 A__ = eval_loss / nb_eval_steps A__ = eval_accuracy / nb_eval_examples A__ = tr_loss / nb_tr_steps if args.do_train else None A__ = {"""eval_loss""": eval_loss, """eval_accuracy""": eval_accuracy, """train_loss""": train_loss} A__ = os.path.join(args.output_dir , """eval_results.txt""" ) with open(__a , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key in sorted(result.keys() ): logger.info(""" %s = %s""" , __a , str(result[key] ) ) writer.write("""%s = %s\n""" % (key, str(result[key] )) ) if __name__ == "__main__": main()
274
1
import contextlib import csv import json import os import sqlitea import tarfile import textwrap import zipfile import pyarrow as pa import pyarrow.parquet as pq import pytest import datasets import datasets.config @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( ) -> List[str]: """simple docstring""" A__ = 1_0 A__ = datasets.Features( { """tokens""": datasets.Sequence(datasets.Value("""string""" ) ), """labels""": datasets.Sequence(datasets.ClassLabel(names=["""negative""", """positive"""] ) ), """answers""": datasets.Sequence( { """text""": datasets.Value("""string""" ), """answer_start""": datasets.Value("""int32""" ), } ), """id""": datasets.Value("""int64""" ), } ) A__ = datasets.Dataset.from_dict( { """tokens""": [["""foo"""] * 5] * n, """labels""": [[1] * 5] * n, """answers""": [{"""answer_start""": [9_7], """text""": ["""1976"""]}] * 1_0, """id""": list(range(__a ) ), } , features=__a , ) return dataset @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple , __a :Tuple ) -> Union[str, Any]: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """file.arrow""" ) dataset.map(cache_file_name=__a ) return filename # FILE_CONTENT + files A : Union[str, Any] = '''\ Text data. Second line of data.''' @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Union[str, Any] ) -> List[Any]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt""" A__ = FILE_CONTENT with open(__a , """w""" ) as f: f.write(__a ) return filename @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[Any] ) -> Dict: """simple docstring""" import bza A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt.bz2""" A__ = bytes(__a , """utf-8""" ) with bza.open(__a , """wb""" ) as f: f.write(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :int ) -> int: """simple docstring""" import gzip A__ = str(tmp_path_factory.mktemp("""data""" ) / """file.txt.gz""" ) A__ = bytes(__a , """utf-8""" ) with gzip.open(__a , """wb""" ) as f: f.write(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :str ) -> str: """simple docstring""" if datasets.config.LZ4_AVAILABLE: import lza.frame A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt.lz4""" A__ = bytes(__a , """utf-8""" ) with lza.frame.open(__a , """wb""" ) as f: f.write(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] , __a :Dict ) -> Optional[Any]: """simple docstring""" if datasets.config.PY7ZR_AVAILABLE: import pyazr A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt.7z""" with pyazr.SevenZipFile(__a , """w""" ) as archive: archive.write(__a , arcname=os.path.basename(__a ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[Any] , __a :Optional[int] ) -> Union[str, Any]: """simple docstring""" import tarfile A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt.tar""" with tarfile.TarFile(__a , """w""" ) as f: f.add(__a , arcname=os.path.basename(__a ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Union[str, Any] ) -> Union[str, Any]: """simple docstring""" import lzma A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt.xz""" A__ = bytes(__a , """utf-8""" ) with lzma.open(__a , """wb""" ) as f: f.write(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[str] , __a :Optional[Any] ) -> Optional[int]: """simple docstring""" import zipfile A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.basename(__a ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[Any] ) -> int: """simple docstring""" if datasets.config.ZSTANDARD_AVAILABLE: import zstandard as zstd A__ = tmp_path_factory.mktemp("""data""" ) / """file.txt.zst""" A__ = bytes(__a , """utf-8""" ) with zstd.open(__a , """wb""" ) as f: f.write(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple ) -> int: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """file.xml""" A__ = textwrap.dedent( """\ <?xml version=\"1.0\" encoding=\"UTF-8\" ?> <tmx version=\"1.4\"> <header segtype=\"sentence\" srclang=\"ca\" /> <body> <tu> <tuv xml:lang=\"ca\"><seg>Contingut 1</seg></tuv> <tuv xml:lang=\"en\"><seg>Content 1</seg></tuv> </tu> <tu> <tuv xml:lang=\"ca\"><seg>Contingut 2</seg></tuv> <tuv xml:lang=\"en\"><seg>Content 2</seg></tuv> </tu> <tu> <tuv xml:lang=\"ca\"><seg>Contingut 3</seg></tuv> <tuv xml:lang=\"en\"><seg>Content 3</seg></tuv> </tu> <tu> <tuv xml:lang=\"ca\"><seg>Contingut 4</seg></tuv> <tuv xml:lang=\"en\"><seg>Content 4</seg></tuv> </tu> <tu> <tuv xml:lang=\"ca\"><seg>Contingut 5</seg></tuv> <tuv xml:lang=\"en\"><seg>Content 5</seg></tuv> </tu> </body> </tmx>""" ) with open(__a , """w""" ) as f: f.write(__a ) return filename A : Tuple = [ {'''col_1''': '''0''', '''col_2''': 0, '''col_3''': 0.0}, {'''col_1''': '''1''', '''col_2''': 1, '''col_3''': 1.0}, {'''col_1''': '''2''', '''col_2''': 2, '''col_3''': 2.0}, {'''col_1''': '''3''', '''col_2''': 3, '''col_3''': 3.0}, ] A : Optional[int] = [ {'''col_1''': '''4''', '''col_2''': 4, '''col_3''': 4.0}, {'''col_1''': '''5''', '''col_2''': 5, '''col_3''': 5.0}, ] A : Tuple = { '''col_1''': ['''0''', '''1''', '''2''', '''3'''], '''col_2''': [0, 1, 2, 3], '''col_3''': [0.0, 1.0, 2.0, 3.0], } A : Optional[Any] = [ {'''col_3''': 0.0, '''col_1''': '''0''', '''col_2''': 0}, {'''col_3''': 1.0, '''col_1''': '''1''', '''col_2''': 1}, ] A : Tuple = [ {'''col_1''': '''s0''', '''col_2''': 0, '''col_3''': 0.0}, {'''col_1''': '''s1''', '''col_2''': 1, '''col_3''': 1.0}, {'''col_1''': '''s2''', '''col_2''': 2, '''col_3''': 2.0}, {'''col_1''': '''s3''', '''col_2''': 3, '''col_3''': 3.0}, ] @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( ) -> Tuple: """simple docstring""" return DATA_DICT_OF_LISTS @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Any ) -> Union[str, Any]: """simple docstring""" A__ = datasets.Dataset.from_dict(__a ) A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.arrow""" ) dataset.map(cache_file_name=__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Dict ) -> Dict: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.sqlite""" ) with contextlib.closing(sqlitea.connect(__a ) ) as con: A__ = con.cursor() cur.execute("""CREATE TABLE dataset(col_1 text, col_2 int, col_3 real)""" ) for item in DATA: cur.execute("""INSERT INTO dataset(col_1, col_2, col_3) VALUES (?, ?, ?)""" , tuple(item.values() ) ) con.commit() return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] ) -> Optional[int]: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.csv""" ) with open(__a , """w""" , newline="""""" ) as f: A__ = csv.DictWriter(__a , fieldnames=["""col_1""", """col_2""", """col_3"""] ) writer.writeheader() for item in DATA: writer.writerow(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] ) -> Optional[int]: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset2.csv""" ) with open(__a , """w""" , newline="""""" ) as f: A__ = csv.DictWriter(__a , fieldnames=["""col_1""", """col_2""", """col_3"""] ) writer.writeheader() for item in DATA: writer.writerow(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[int] , __a :Dict ) -> Any: """simple docstring""" import bza A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.csv.bz2""" with open(__a , """rb""" ) as f: A__ = f.read() # data = bytes(FILE_CONTENT, "utf-8") with bza.open(__a , """wb""" ) as f: f.write(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] , __a :int , __a :List[str] ) -> List[str]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.csv.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.basename(__a ) ) f.write(__a , arcname=os.path.basename(__a ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Union[str, Any] , __a :Optional[Any] , __a :Any ) -> Union[str, Any]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.csv.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.basename(csv_path.replace(""".csv""" , """.CSV""" ) ) ) f.write(__a , arcname=os.path.basename(csva_path.replace(""".csv""" , """.CSV""" ) ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Dict , __a :List[Any] , __a :Optional[int] ) -> int: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset_with_dir.csv.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.join("""main_dir""" , os.path.basename(__a ) ) ) f.write(__a , arcname=os.path.join("""main_dir""" , os.path.basename(__a ) ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple ) -> Any: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.parquet""" ) A__ = pa.schema( { """col_1""": pa.string(), """col_2""": pa.intaa(), """col_3""": pa.floataa(), } ) with open(__a , """wb""" ) as f: A__ = pq.ParquetWriter(__a , schema=__a ) A__ = pa.Table.from_pydict({k: [DATA[i][k] for i in range(len(__a ) )] for k in DATA[0]} , schema=__a ) writer.write_table(__a ) writer.close() return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Any ) -> Union[str, Any]: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.json""" ) A__ = {"""data""": DATA} with open(__a , """w""" ) as f: json.dump(__a , __a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :int ) -> Any: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.json""" ) A__ = {"""data""": DATA_DICT_OF_LISTS} with open(__a , """w""" ) as f: json.dump(__a , __a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[int] ) -> Union[str, Any]: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl""" ) with open(__a , """w""" ) as f: for item in DATA: f.write(json.dumps(__a ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple ) -> Tuple: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset2.jsonl""" ) with open(__a , """w""" ) as f: for item in DATA: f.write(json.dumps(__a ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[Any] ) -> Dict: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset_312.jsonl""" ) with open(__a , """w""" ) as f: for item in DATA_312: f.write(json.dumps(__a ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] ) -> Tuple: """simple docstring""" A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset-str.jsonl""" ) with open(__a , """w""" ) as f: for item in DATA_STR: f.write(json.dumps(__a ) + """\n""" ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[Any] , __a :str ) -> Optional[Any]: """simple docstring""" import gzip A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.txt.gz""" ) with open(__a , """rb""" ) as orig_file: with gzip.open(__a , """wb""" ) as zipped_file: zipped_file.writelines(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple , __a :int ) -> Optional[int]: """simple docstring""" import gzip A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl.gz""" ) with open(__a , """rb""" ) as orig_file: with gzip.open(__a , """wb""" ) as zipped_file: zipped_file.writelines(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Any , __a :Dict , __a :int ) -> Optional[Any]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.basename(__a ) ) f.write(__a , arcname=os.path.basename(__a ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] , __a :List[Any] , __a :Any , __a :str ) -> Dict: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset_nested.jsonl.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.join("""nested""" , os.path.basename(__a ) ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :int , __a :Tuple , __a :str ) -> Any: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset_with_dir.jsonl.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.join("""main_dir""" , os.path.basename(__a ) ) ) f.write(__a , arcname=os.path.join("""main_dir""" , os.path.basename(__a ) ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple , __a :Optional[int] , __a :Tuple ) -> List[Any]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.jsonl.tar""" with tarfile.TarFile(__a , """w""" ) as f: f.add(__a , arcname=os.path.basename(__a ) ) f.add(__a , arcname=os.path.basename(__a ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :str , __a :Dict , __a :str , __a :Union[str, Any] ) -> Optional[Any]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset_nested.jsonl.tar""" with tarfile.TarFile(__a , """w""" ) as f: f.add(__a , arcname=os.path.join("""nested""" , os.path.basename(__a ) ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Union[str, Any] ) -> Optional[int]: """simple docstring""" A__ = ["""0""", """1""", """2""", """3"""] A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset.txt""" ) with open(__a , """w""" ) as f: for item in data: f.write(item + """\n""" ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] ) -> Tuple: """simple docstring""" A__ = ["""0""", """1""", """2""", """3"""] A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset2.txt""" ) with open(__a , """w""" ) as f: for item in data: f.write(item + """\n""" ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Optional[Any] ) -> int: """simple docstring""" A__ = ["""0""", """1""", """2""", """3"""] A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.abc""" with open(__a , """w""" ) as f: for item in data: f.write(item + """\n""" ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :str , __a :Any , __a :List[str] ) -> List[Any]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.text.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.basename(__a ) ) f.write(__a , arcname=os.path.basename(__a ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Any , __a :str , __a :int ) -> int: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset_with_dir.text.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.join("""main_dir""" , os.path.basename(__a ) ) ) f.write(__a , arcname=os.path.join("""main_dir""" , os.path.basename(__a ) ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple , __a :Any , __a :Dict ) -> List[Any]: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.ext.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.basename("""unsupported.ext""" ) ) f.write(__a , arcname=os.path.basename("""unsupported_2.ext""" ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :List[Any] ) -> Union[str, Any]: """simple docstring""" A__ = """\n""".join(["""First""", """Second\u2029with Unicode new line""", """Third"""] ) A__ = str(tmp_path_factory.mktemp("""data""" ) / """dataset_with_unicode_new_lines.txt""" ) with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(__a ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( ) -> Tuple: """simple docstring""" return os.path.join("""tests""" , """features""" , """data""" , """test_image_rgb.jpg""" ) @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( ) -> Dict: """simple docstring""" return os.path.join("""tests""" , """features""" , """data""" , """test_audio_44100.wav""" ) @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Tuple , __a :int ) -> Tuple: """simple docstring""" A__ = tmp_path_factory.mktemp("""data""" ) / """dataset.img.zip""" with zipfile.ZipFile(__a , """w""" ) as f: f.write(__a , arcname=os.path.basename(__a ) ) f.write(__a , arcname=os.path.basename(__a ).replace(""".jpg""" , """2.jpg""" ) ) return path @pytest.fixture(scope="""session""" ) def __lowerCamelCase ( __a :Dict ) -> Dict: """simple docstring""" A__ = tmp_path_factory.mktemp("""data_dir""" ) (data_dir / "subdir").mkdir() with open(data_dir / """subdir""" / """train.txt""" , """w""" ) as f: f.write("""foo\n""" * 1_0 ) with open(data_dir / """subdir""" / """test.txt""" , """w""" ) as f: f.write("""bar\n""" * 1_0 ) # hidden file with open(data_dir / """subdir""" / """.test.txt""" , """w""" ) as f: f.write("""bar\n""" * 1_0 ) # hidden directory (data_dir / ".subdir").mkdir() with open(data_dir / """.subdir""" / """train.txt""" , """w""" ) as f: f.write("""foo\n""" * 1_0 ) with open(data_dir / """.subdir""" / """test.txt""" , """w""" ) as f: f.write("""bar\n""" * 1_0 ) return data_dir
274
import argparse from collections import defaultdict import yaml A : str = '''docs/source/en/_toctree.yml''' def __lowerCamelCase ( __a :str ) -> List[Any]: """simple docstring""" A__ = defaultdict(__a ) A__ = [] A__ = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__a ) A__ = new_doc_list A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__a ) > 1: raise ValueError( F'{duplicate_key} is present several times in the documentation table of content at ' """`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the """ """others.""" ) # Only add this once new_doc.append({"""local""": duplicate_key, """title""": titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) A__ = sorted(__a , key=lambda __a : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__a ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__a ) # Sort return overview_doc def __lowerCamelCase ( __a :Any=False ) -> List[str]: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 A__ = api_doc[scheduler_idx]["""sections"""] A__ = clean_doc_toc(__a ) A__ = False if new_scheduler_doc != scheduler_doc: A__ = True if overwrite: A__ = new_scheduler_doc if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def __lowerCamelCase ( __a :Optional[int]=False ) -> Dict: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 A__ = False A__ = api_doc[pipeline_idx]["""sections"""] A__ = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: A__ = pipeline_doc["""section"""] A__ = clean_doc_toc(__a ) if overwrite: A__ = new_sub_pipeline_doc new_pipeline_docs.append(__a ) # sort overall pipeline doc A__ = clean_doc_toc(__a ) if new_pipeline_docs != pipeline_docs: A__ = True if overwrite: A__ = new_pipeline_docs if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) if __name__ == "__main__": A : Tuple = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') A : Optional[Any] = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
274
1
import argparse import os import torch from transformers import ( XLNetConfig, XLNetForQuestionAnswering, XLNetForSequenceClassification, XLNetLMHeadModel, load_tf_weights_in_xlnet, ) from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging A : List[Any] = { '''cola''': 2, '''mnli''': 3, '''mrpc''': 2, '''sst-2''': 2, '''sts-b''': 1, '''qqp''': 2, '''qnli''': 2, '''rte''': 2, '''wnli''': 2, } logging.set_verbosity_info() def __lowerCamelCase ( __a :Union[str, Any] , __a :Tuple , __a :str , __a :Any=None ) -> Dict: """simple docstring""" A__ = XLNetConfig.from_json_file(__a ) A__ = finetuning_task.lower() if finetuning_task is not None else """""" if finetuning_task in GLUE_TASKS_NUM_LABELS: print(F'Building PyTorch XLNetForSequenceClassification model from configuration: {config}' ) A__ = finetuning_task A__ = GLUE_TASKS_NUM_LABELS[finetuning_task] A__ = XLNetForSequenceClassification(__a ) elif "squad" in finetuning_task: A__ = finetuning_task A__ = XLNetForQuestionAnswering(__a ) else: A__ = XLNetLMHeadModel(__a ) # Load weights from tf checkpoint load_tf_weights_in_xlnet(__a , __a , __a ) # Save pytorch-model A__ = os.path.join(__a , __a ) A__ = os.path.join(__a , __a ) print(F'Save PyTorch model to {os.path.abspath(__a )}' ) torch.save(model.state_dict() , __a ) print(F'Save configuration file to {os.path.abspath(__a )}' ) with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": A : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--xlnet_config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained XLNet model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the folder to store the PyTorch model or dataset/vocab.''', ) parser.add_argument( '''--finetuning_task''', default=None, type=str, help='''Name of a task on which the XLNet TensorFlow model was fine-tuned''', ) A : str = parser.parse_args() print(args) convert_xlnet_checkpoint_to_pytorch( args.tf_checkpoint_path, args.xlnet_config_file, args.pytorch_dump_folder_path, args.finetuning_task )
274
def __lowerCamelCase ( __a :str ) -> list: """simple docstring""" A__ = [0] * len(__a ) for i in range(1 , len(__a ) ): # use last results for better performance - dynamic programming A__ = prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: A__ = prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 A__ = j return prefix_result def __lowerCamelCase ( __a :str ) -> int: """simple docstring""" return max(prefix_function(__a ) ) if __name__ == "__main__": import doctest doctest.testmod()
274
1
from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef import datasets A : Dict = '''\ @inproceedings{wang2019glue, title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding}, author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.}, note={In the Proceedings of ICLR.}, year={2019} } ''' A : List[Any] = '''\ GLUE, the General Language Understanding Evaluation benchmark (https://gluebenchmark.com/) is a collection of resources for training, evaluating, and analyzing natural language understanding systems. ''' A : Optional[Any] = ''' Compute GLUE evaluation metric associated to each GLUE dataset. Args: predictions: list of predictions to score. Each translation should be tokenized into a list of tokens. references: list of lists of references for each translation. Each reference should be tokenized into a list of tokens. Returns: depending on the GLUE subset, one or several of: "accuracy": Accuracy "f1": F1 score "pearson": Pearson Correlation "spearmanr": Spearman Correlation "matthews_correlation": Matthew Correlation Examples: >>> glue_metric = datasets.load_metric(\'glue\', \'sst2\') # \'sst2\' or any of ["mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"] >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0} >>> glue_metric = datasets.load_metric(\'glue\', \'mrpc\') # \'mrpc\' or \'qqp\' >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0, \'f1\': 1.0} >>> glue_metric = datasets.load_metric(\'glue\', \'stsb\') >>> references = [0., 1., 2., 3., 4., 5.] >>> predictions = [0., 1., 2., 3., 4., 5.] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print({"pearson": round(results["pearson"], 2), "spearmanr": round(results["spearmanr"], 2)}) {\'pearson\': 1.0, \'spearmanr\': 1.0} >>> glue_metric = datasets.load_metric(\'glue\', \'cola\') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'matthews_correlation\': 1.0} ''' def __lowerCamelCase ( __a :Dict , __a :Optional[int] ) -> Union[str, Any]: """simple docstring""" return float((preds == labels).mean() ) def __lowerCamelCase ( __a :Optional[Any] , __a :Any ) -> List[str]: """simple docstring""" A__ = simple_accuracy(__a , __a ) A__ = float(fa_score(y_true=__a , y_pred=__a ) ) return { "accuracy": acc, "f1": fa, } def __lowerCamelCase ( __a :Optional[Any] , __a :List[str] ) -> str: """simple docstring""" A__ = float(pearsonr(__a , __a )[0] ) A__ = float(spearmanr(__a , __a )[0] ) return { "pearson": pearson_corr, "spearmanr": spearman_corr, } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A (datasets.Metric ): '''simple docstring''' def a_ ( self : Any ) -> Dict: """simple docstring""" 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 a_ ( self : List[Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : str ) -> Dict: """simple docstring""" if self.config_name == "cola": return {"matthews_correlation": matthews_corrcoef(__lowerCAmelCase , __lowerCAmelCase )} elif self.config_name == "stsb": return pearson_and_spearman(__lowerCAmelCase , __lowerCAmelCase ) elif self.config_name in ["mrpc", "qqp"]: return acc_and_fa(__lowerCAmelCase , __lowerCAmelCase ) elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]: return {"accuracy": simple_accuracy(__lowerCAmelCase , __lowerCAmelCase )} 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\"]""" )
274
def __lowerCamelCase ( __a :int = 1_0_0_0_0_0_0 ) -> int: """simple docstring""" A__ = limit + 1 A__ = [0] * limit for first_term in range(1 , __a ): for n in range(__a , __a , __a ): A__ = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a A__ = sum(1 for x in frequency[1:limit] if x == 1_0 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
274
1
from __future__ import annotations from math import pi def __lowerCamelCase ( __a :float , __a :float , __a :float ) -> dict[str, float]: """simple docstring""" if (inductance, frequency, reactance).count(0 ) != 1: raise ValueError("""One and only one argument must be 0""" ) if inductance < 0: raise ValueError("""Inductance cannot be negative""" ) if frequency < 0: raise ValueError("""Frequency cannot be negative""" ) if reactance < 0: raise ValueError("""Inductive reactance cannot be negative""" ) if inductance == 0: return {"inductance": reactance / (2 * pi * frequency)} elif frequency == 0: return {"frequency": reactance / (2 * pi * inductance)} elif reactance == 0: return {"reactance": 2 * pi * frequency * inductance} else: raise ValueError("""Exactly one argument must be 0""" ) if __name__ == "__main__": import doctest doctest.testmod()
274
class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A : '''simple docstring''' def __init__( self : List[Any] ) -> str: """simple docstring""" A__ = [ [], [], [], ] def a_ ( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" try: if len(self.queues[priority] ) >= 1_00: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(__lowerCAmelCase ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def a_ ( self : Optional[Any] ) -> int: """simple docstring""" for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self : Tuple ) -> str: """simple docstring""" return "\n".join(f'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class A : '''simple docstring''' def __init__( self : int ) -> str: """simple docstring""" A__ = [] def a_ ( self : int , __lowerCAmelCase : int ) -> None: """simple docstring""" if len(self.queue ) == 1_00: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(__lowerCAmelCase ) def a_ ( self : List[str] ) -> int: """simple docstring""" if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: A__ = min(self.queue ) self.queue.remove(__lowerCAmelCase ) return data def __str__( self : List[Any] ) -> str: """simple docstring""" return str(self.queue ) def __lowerCamelCase ( ) -> Optional[Any]: """simple docstring""" A__ = FixedPriorityQueue() fpq.enqueue(0 , 1_0 ) fpq.enqueue(1 , 7_0 ) fpq.enqueue(0 , 1_0_0 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 6_4 ) fpq.enqueue(0 , 1_2_8 ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __lowerCamelCase ( ) -> int: """simple docstring""" A__ = ElementPriorityQueue() epq.enqueue(1_0 ) epq.enqueue(7_0 ) epq.enqueue(1_0_0 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(6_4 ) epq.enqueue(1_2_8 ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
274
1
def __lowerCamelCase ( __a :List[Any] ) -> Optional[int]: """simple docstring""" A__ = len(__a ) while cur > 1: # Find the maximum number in arr A__ = arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi A__ = arr[mi::-1] + arr[mi + 1 : len(__a )] # Reverse whole list A__ = arr[cur - 1 :: -1] + arr[cur : len(__a )] cur -= 1 return arr if __name__ == "__main__": A : List[str] = input('''Enter numbers separated by a comma:\n''').strip() A : str = [int(item) for item in user_input.split(''',''')] print(pancake_sort(unsorted))
274
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = tempfile.mkdtemp() # fmt: off A__ = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on A__ = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) A__ = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""] A__ = {"""unk_token""": """<unk>"""} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(__lowerCAmelCase ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__lowerCAmelCase ) ) A__ = { """do_resize""": True, """size""": 20, """do_center_crop""": True, """crop_size""": 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], } A__ = os.path.join(self.tmpdirname , __lowerCAmelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Tuple , **__lowerCAmelCase : Dict ) -> str: """simple docstring""" return CLIPTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : Union[str, Any] , **__lowerCAmelCase : Dict ) -> List[str]: """simple docstring""" return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : List[str] , **__lowerCAmelCase : Optional[Any] ) -> Dict: """simple docstring""" return CLIPImageProcessor.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : str ) -> Dict: """simple docstring""" shutil.rmtree(self.tmpdirname ) def a_ ( self : str ) -> Any: """simple docstring""" A__ = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] A__ = [Image.fromarray(np.moveaxis(__lowerCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def a_ ( self : Optional[int] ) -> Tuple: """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_rust_tokenizer() A__ = self.get_image_processor() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_slow.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname , use_fast=__lowerCAmelCase ) A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_fast.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , __lowerCAmelCase ) self.assertIsInstance(processor_fast.tokenizer , __lowerCAmelCase ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , __lowerCAmelCase ) self.assertIsInstance(processor_fast.image_processor , __lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" A__ = CLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=__lowerCAmelCase , padding_value=1.0 ) A__ = CLIPProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__lowerCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowerCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCAmelCase ) def a_ ( self : List[Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(__lowerCAmelCase , return_tensors="""np""" ) A__ = processor(images=__lowerCAmelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def a_ ( self : Optional[Any] ) -> Any: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = processor(text=__lowerCAmelCase ) A__ = tokenizer(__lowerCAmelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(__lowerCAmelCase ): processor() def a_ ( self : Tuple ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.batch_decode(__lowerCAmelCase ) A__ = tokenizer.batch_decode(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Optional[int] ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
274
1
import unittest import torch from diffusers import DDIMScheduler, DDPMScheduler, UNetaDModel from diffusers.training_utils import set_seed from diffusers.utils.testing_utils import slow A : Optional[Any] = False class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : str , __lowerCAmelCase : List[Any]=32 ) -> List[str]: """simple docstring""" set_seed(0 ) A__ = UNetaDModel(sample_size=__lowerCAmelCase , in_channels=3 , out_channels=3 ) A__ = torch.optim.SGD(model.parameters() , lr=0.0_0_0_1 ) return model, optimizer @slow def a_ ( self : Tuple ) -> List[str]: """simple docstring""" A__ = """cpu""" # ensure full determinism without setting the CUBLAS_WORKSPACE_CONFIG env variable A__ = DDPMScheduler( num_train_timesteps=10_00 , beta_start=0.0_0_0_1 , beta_end=0.0_2 , beta_schedule="""linear""" , clip_sample=__lowerCAmelCase , ) A__ = DDIMScheduler( num_train_timesteps=10_00 , beta_start=0.0_0_0_1 , beta_end=0.0_2 , beta_schedule="""linear""" , clip_sample=__lowerCAmelCase , ) assert ddpm_scheduler.config.num_train_timesteps == ddim_scheduler.config.num_train_timesteps # shared batches for DDPM and DDIM set_seed(0 ) A__ = [torch.randn((4, 3, 32, 32) ).clip(-1 , 1 ).to(__lowerCAmelCase ) for _ in range(4 )] A__ = [torch.randn((4, 3, 32, 32) ).to(__lowerCAmelCase ) for _ in range(4 )] A__ = [torch.randint(0 , 10_00 , (4,) ).long().to(__lowerCAmelCase ) for _ in range(4 )] # train with a DDPM scheduler A__ , A__ = self.get_model_optimizer(resolution=32 ) model.train().to(__lowerCAmelCase ) for i in range(4 ): optimizer.zero_grad() A__ = ddpm_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i] ) A__ = model(__lowerCAmelCase , timesteps[i] ).sample A__ = torch.nn.functional.mse_loss(__lowerCAmelCase , noise[i] ) loss.backward() optimizer.step() del model, optimizer # recreate the model and optimizer, and retry with DDIM A__ , A__ = self.get_model_optimizer(resolution=32 ) model.train().to(__lowerCAmelCase ) for i in range(4 ): optimizer.zero_grad() A__ = ddim_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i] ) A__ = model(__lowerCAmelCase , timesteps[i] ).sample A__ = torch.nn.functional.mse_loss(__lowerCAmelCase , noise[i] ) loss.backward() optimizer.step() del model, optimizer self.assertTrue(torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-5 ) ) self.assertTrue(torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-5 ) )
274
from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time A : Dict = Lock() def __lowerCamelCase ( __a :Dict , __a :List[str] , __a :Optional[int] , __a :Optional[int] , __a :Optional[Any] , __a :Optional[int] , __a :int ) -> Dict: """simple docstring""" global process_lock # we perform n swaps since after n swaps we know we are sorted # we *could* stop early if we are sorted already, but it takes as long to # find out we are sorted as it does to sort the list with this algorithm for i in range(0 , 1_0 ): if (i + position) % 2 == 0 and r_send is not None: # send your value to your right neighbor process_lock.acquire() r_send[1].send(__a ) process_lock.release() # receive your right neighbor's value process_lock.acquire() A__ = rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left A__ = min(__a , __a ) elif (i + position) % 2 != 0 and l_send is not None: # send your value to your left neighbor process_lock.acquire() l_send[1].send(__a ) process_lock.release() # receive your left neighbor's value process_lock.acquire() A__ = lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right A__ = max(__a , __a ) # after all swaps are performed, send the values back to main result_pipe[1].send(__a ) def __lowerCamelCase ( __a :List[str] ) -> int: """simple docstring""" A__ = [] A__ = [] # initialize the list of pipes where the values will be retrieved for _ in arr: result_pipe.append(Pipe() ) # creates the processes # the first and last process only have one neighbor so they are made outside # of the loop A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) A__ = temp_rs A__ = temp_rr for i in range(1 , len(__a ) - 1 ): A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) A__ = temp_rs A__ = temp_rr process_array_.append( Process( target=__a , args=( len(__a ) - 1, arr[len(__a ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(__a ) - 1], ) , ) ) # start the processes for p in process_array_: p.start() # wait for the processes to end and write their values to the list for p in range(0 , len(__a ) ): A__ = result_pipe[p][0].recv() process_array_[p].join() return arr def __lowerCamelCase ( ) -> str: """simple docstring""" A__ = list(range(1_0 , 0 , -1 ) ) print("""Initial List""" ) print(*__a ) A__ = odd_even_transposition(__a ) print("""Sorted List\n""" ) print(*__a ) if __name__ == "__main__": main()
274
1
from __future__ import annotations from PIL import Image # Define glider example A : Any = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [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], ] # Define blinker example A : Optional[Any] = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def __lowerCamelCase ( __a :list[list[int]] ) -> list[list[int]]: """simple docstring""" A__ = [] for i in range(len(__a ) ): A__ = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours A__ = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(__a ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(__a ) - 1: neighbour_count += cells[i + 1][j] if i < len(__a ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. A__ = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(__a ) return next_generation def __lowerCamelCase ( __a :list[list[int]] , __a :int ) -> list[Image.Image]: """simple docstring""" A__ = [] for _ in range(__a ): # Create output image A__ = Image.new("""RGB""" , (len(cells[0] ), len(__a )) ) A__ = img.load() # Save cells to image for x in range(len(__a ) ): for y in range(len(cells[0] ) ): A__ = 2_5_5 - cells[y][x] * 2_5_5 A__ = (colour, colour, colour) # Save image images.append(__a ) A__ = new_generation(__a ) return images if __name__ == "__main__": A : str = generate_images(GLIDER, 1_6) images[0].save('''out.gif''', save_all=True, append_images=images[1:])
274
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def __lowerCamelCase ( __a :Dict ) -> Any: """simple docstring""" A__ = [ """decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(__a , __a ) def __lowerCamelCase ( __a :str ) -> Union[str, Any]: """simple docstring""" A__ , A__ = emb.weight.shape A__ = nn.Linear(__a , __a , bias=__a ) A__ = emb.weight.data return lin_layer def __lowerCamelCase ( __a :str ) -> List[str]: """simple docstring""" A__ = torch.load(__a , map_location="""cpu""" ) A__ = Namespace(**checkpoint["""cfg"""]["""model"""] ) A__ = checkpoint["""model"""] remove_ignore_keys_(__a ) A__ = state_dict["""decoder.embed_tokens.weight"""].shape[0] A__ = {key.replace("""decoder""" , """model""" ): val for key, val in state_dict.items()} A__ = XGLMConfig( vocab_size=__a , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""gelu""" , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) A__ = XGLMForCausalLM(__a ) A__ = model.load_state_dict(__a , strict=__a ) print(__a ) A__ = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": A : int = argparse.ArgumentParser() # Required parameters parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') A : str = parser.parse_args() A : str = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
274
1
import re from filelock import FileLock try: import nltk A : Optional[Any] = True except (ImportError, ModuleNotFoundError): A : Union[str, Any] = False if NLTK_AVAILABLE: with FileLock('''.lock''') as lock: nltk.download('''punkt''', quiet=True) def __lowerCamelCase ( __a :str ) -> str: """simple docstring""" re.sub("""<n>""" , """""" , __a ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(__a ) )
274
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 A (unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : List[str]=13 , __lowerCAmelCase : int=7 , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : int=True , __lowerCAmelCase : Dict=True , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : List[Any]=99 , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Optional[Any]=5 , __lowerCAmelCase : Tuple=4 , __lowerCAmelCase : Any=37 , __lowerCAmelCase : str="gelu" , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : Union[str, Any]=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : int=16 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : List[Any]=0.0_2 , __lowerCAmelCase : Tuple=4 , ) -> Dict: """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_attention_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_choices def a_ ( self : Any ) -> str: """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_attention_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ = 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=__lowerCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a_ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class A (SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a_ ( self : str ) -> Optional[int]: """simple docstring""" A__ = FlaxAlbertModelTester(self ) @slow def a_ ( self : int ) -> Tuple: """simple docstring""" for model_class_name in self.all_model_classes: A__ = model_class_name.from_pretrained("""albert-base-v2""" ) A__ = model(np.ones((1, 1) ) ) self.assertIsNotNone(__lowerCAmelCase ) @require_flax class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : Dict ) -> List[Any]: """simple docstring""" A__ = FlaxAlbertModel.from_pretrained("""albert-base-v2""" ) A__ = np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] ) A__ = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase )[0] A__ = (1, 11, 7_68) self.assertEqual(output.shape , __lowerCAmelCase ) A__ = 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] , __lowerCAmelCase , atol=1e-4 ) )
274
1
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A : Tuple = logging.getLogger(__name__) def __lowerCamelCase ( __a :Optional[int] , __a :List[str] ) -> Tuple: """simple docstring""" A__ = np.argmax(__a , axis=1 ) return np.sum(outputs == labels ) def __lowerCamelCase ( __a :Tuple ) -> Dict: """simple docstring""" with open(__a , encoding="""utf_8""" ) as f: A__ = csv.reader(__a ) A__ = [] next(__a ) # skip the first line for line in tqdm(__a ): output.append((""" """.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def __lowerCamelCase ( __a :Optional[int] , __a :List[Any] , __a :Dict , __a :Optional[Any] , __a :Optional[Any] , __a :int ) -> Union[str, Any]: """simple docstring""" A__ = [] for dataset in encoded_datasets: A__ = len(__a ) A__ = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) A__ = np.zeros((n_batch, 2) , dtype=np.intaa ) A__ = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) A__ = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(__a ): A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = with_conta A__ = with_conta A__ = len(__a ) - 1 A__ = len(__a ) - 1 A__ = with_conta A__ = with_conta A__ = mc_label A__ = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(__a ) for t in all_inputs ) ) return tensor_datasets def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = argparse.ArgumentParser() parser.add_argument("""--model_name""" , type=__a , default="""openai-gpt""" , help="""pretrained model name""" ) parser.add_argument("""--do_train""" , action="""store_true""" , help="""Whether to run training.""" ) parser.add_argument("""--do_eval""" , action="""store_true""" , help="""Whether to run eval on the dev set.""" ) parser.add_argument( """--output_dir""" , default=__a , type=__a , required=__a , help="""The output directory where the model predictions and checkpoints will be written.""" , ) parser.add_argument("""--train_dataset""" , type=__a , default="""""" ) parser.add_argument("""--eval_dataset""" , type=__a , default="""""" ) parser.add_argument("""--seed""" , type=__a , default=4_2 ) parser.add_argument("""--num_train_epochs""" , type=__a , default=3 ) parser.add_argument("""--train_batch_size""" , type=__a , default=8 ) parser.add_argument("""--eval_batch_size""" , type=__a , default=1_6 ) parser.add_argument("""--adam_epsilon""" , default=1E-8 , type=__a , help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--max_grad_norm""" , type=__a , default=1 ) parser.add_argument( """--max_steps""" , default=-1 , type=__a , help=( """If > 0: set total number of training steps to perform. Override num_train_epochs.""" ) , ) parser.add_argument( """--gradient_accumulation_steps""" , type=__a , default=1 , help="""Number of updates steps to accumulate before performing a backward/update pass.""" , ) parser.add_argument("""--learning_rate""" , type=__a , default=6.25E-5 ) parser.add_argument("""--warmup_steps""" , default=0 , type=__a , help="""Linear warmup over warmup_steps.""" ) parser.add_argument("""--lr_schedule""" , type=__a , default="""warmup_linear""" ) parser.add_argument("""--weight_decay""" , type=__a , default=0.01 ) parser.add_argument("""--lm_coef""" , type=__a , default=0.9 ) parser.add_argument("""--n_valid""" , type=__a , default=3_7_4 ) parser.add_argument("""--server_ip""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) parser.add_argument("""--server_port""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) A__ = parser.parse_args() print(__a ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("""Waiting for debugger attach""" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__a ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) A__ = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) A__ = torch.cuda.device_count() logger.info("""device: {}, n_gpu {}""".format(__a , __a ) ) if not args.do_train and not args.do_eval: raise ValueError("""At least one of `do_train` or `do_eval` must be True.""" ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset A__ = ["""_start_""", """_delimiter_""", """_classify_"""] A__ = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(__a ) A__ = tokenizer.convert_tokens_to_ids(__a ) A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(__a ) ) model.to(__a ) # Load and encode the datasets def tokenize_and_encode(__a :Tuple ): if isinstance(__a , __a ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(__a ) ) elif isinstance(__a , __a ): return obj return [tokenize_and_encode(__a ) for o in obj] logger.info("""Encoding dataset...""" ) A__ = load_rocstories_dataset(args.train_dataset ) A__ = load_rocstories_dataset(args.eval_dataset ) A__ = (train_dataset, eval_dataset) A__ = tokenize_and_encode(__a ) # Compute the max input length for the Transformer A__ = model.config.n_positions // 2 - 2 A__ = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) A__ = min(__a , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders A__ = pre_process_datasets(__a , __a , __a , *__a ) A__ , A__ = tensor_datasets[0], tensor_datasets[1] A__ = TensorDataset(*__a ) A__ = RandomSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.train_batch_size ) A__ = TensorDataset(*__a ) A__ = SequentialSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: A__ = args.max_steps A__ = args.max_steps // (len(__a ) // args.gradient_accumulation_steps) + 1 else: A__ = len(__a ) // args.gradient_accumulation_steps * args.num_train_epochs A__ = list(model.named_parameters() ) A__ = ["""bias""", """LayerNorm.bias""", """LayerNorm.weight"""] A__ = [ { """params""": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], """weight_decay""": args.weight_decay, }, {"""params""": [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], """weight_decay""": 0.0}, ] A__ = AdamW(__a , lr=args.learning_rate , eps=args.adam_epsilon ) A__ = get_linear_schedule_with_warmup( __a , num_warmup_steps=args.warmup_steps , num_training_steps=__a ) if args.do_train: A__ , A__ , A__ = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="""Epoch""" ): A__ = 0 A__ = 0 A__ = tqdm(__a , desc="""Training""" ) for step, batch in enumerate(__a ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch A__ = model(__a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() A__ = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 A__ = """Training loss: {:.2e} lr: {:.2e}""".format(__a , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer A__ = model.module if hasattr(__a , """module""" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` A__ = os.path.join(args.output_dir , __a ) A__ = os.path.join(args.output_dir , __a ) torch.save(model_to_save.state_dict() , __a ) model_to_save.config.to_json_file(__a ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) A__ = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(__a ) if args.do_eval: model.eval() A__ , A__ = 0, 0 A__ , A__ = 0, 0 for batch in tqdm(__a , desc="""Evaluating""" ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch with torch.no_grad(): A__ , A__ , A__ , A__ = model( __a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = mc_logits.detach().cpu().numpy() A__ = mc_labels.to("""cpu""" ).numpy() A__ = accuracy(__a , __a ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 A__ = eval_loss / nb_eval_steps A__ = eval_accuracy / nb_eval_examples A__ = tr_loss / nb_tr_steps if args.do_train else None A__ = {"""eval_loss""": eval_loss, """eval_accuracy""": eval_accuracy, """train_loss""": train_loss} A__ = os.path.join(args.output_dir , """eval_results.txt""" ) with open(__a , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key in sorted(result.keys() ): logger.info(""" %s = %s""" , __a , str(result[key] ) ) writer.write("""%s = %s\n""" % (key, str(result[key] )) ) if __name__ == "__main__": main()
274
from sklearn.metrics import fa_score import datasets A : Any = ''' The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation: F1 = 2 * (precision * recall) / (precision + recall) ''' A : List[Any] = ''' Args: predictions (`list` of `int`): Predicted labels. references (`list` of `int`): Ground truth labels. labels (`list` of `int`): The set of labels to include when `average` is not set to `\'binary\'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None. pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1. average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`. - \'binary\': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary. - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives. - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - \'weighted\': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. This option can result in an F-score that is not between precision and recall. - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). sample_weight (`list` of `float`): Sample weights Defaults to None. Returns: f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better. Examples: Example 1-A simple binary example >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0]) >>> print(results) {\'f1\': 0.5} Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0) >>> print(round(results[\'f1\'], 2)) 0.67 Example 3-The same simple binary example as in Example 1, but with `sample_weight` included. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3]) >>> print(round(results[\'f1\'], 2)) 0.35 Example 4-A multiclass example, with different values for the `average` input. >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro") >>> print(round(results[\'f1\'], 2)) 0.33 >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {\'f1\': array([0.8, 0. , 0. ])} ''' A : List[Any] = ''' @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A (datasets.Metric ): '''simple docstring''' def a_ ( self : Optional[int] ) -> List[Any]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html"""] , ) def a_ ( self : Any , __lowerCAmelCase : Tuple , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict=None , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : Any="binary" , __lowerCAmelCase : Optional[int]=None ) -> List[Any]: """simple docstring""" A__ = fa_score( __lowerCAmelCase , __lowerCAmelCase , labels=__lowerCAmelCase , pos_label=__lowerCAmelCase , average=__lowerCAmelCase , sample_weight=__lowerCAmelCase ) return {"f1": float(__lowerCAmelCase ) if score.size == 1 else score}
274
1
import inspect import unittest from transformers import DecisionTransformerConfig, 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, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import DecisionTransformerModel from transformers.models.decision_transformer.modeling_decision_transformer import ( DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) class A : '''simple docstring''' def __init__( self : Optional[int] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[Any]=13 , __lowerCAmelCase : int=7 , __lowerCAmelCase : Optional[int]=6 , __lowerCAmelCase : List[str]=17 , __lowerCAmelCase : Tuple=23 , __lowerCAmelCase : Optional[int]=11 , __lowerCAmelCase : Any=True , ) -> str: """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = act_dim A__ = state_dim A__ = hidden_size A__ = max_length A__ = is_training def a_ ( self : str ) -> Optional[int]: """simple docstring""" A__ = floats_tensor((self.batch_size, self.seq_length, self.state_dim) ) A__ = floats_tensor((self.batch_size, self.seq_length, self.act_dim) ) A__ = floats_tensor((self.batch_size, self.seq_length, 1) ) A__ = floats_tensor((self.batch_size, self.seq_length, 1) ) A__ = ids_tensor((self.batch_size, self.seq_length) , vocab_size=10_00 ) A__ = random_attention_mask((self.batch_size, self.seq_length) ) A__ = self.get_config() return ( config, states, actions, rewards, returns_to_go, timesteps, attention_mask, ) def a_ ( self : List[Any] ) -> int: """simple docstring""" return DecisionTransformerConfig( batch_size=self.batch_size , seq_length=self.seq_length , act_dim=self.act_dim , state_dim=self.state_dim , hidden_size=self.hidden_size , max_length=self.max_length , ) def a_ ( self : Optional[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Dict , ) -> Optional[Any]: """simple docstring""" A__ = DecisionTransformerModel(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) self.parent.assertEqual(result.state_preds.shape , states.shape ) self.parent.assertEqual(result.action_preds.shape , actions.shape ) self.parent.assertEqual(result.return_preds.shape , returns_to_go.shape ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length * 3, self.hidden_size) ) # seq length *3 as there are 3 modelities: states, returns and actions def a_ ( self : Any ) -> str: """simple docstring""" A__ = self.prepare_config_and_inputs() ( ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ) = config_and_inputs A__ = { """states""": states, """actions""": actions, """rewards""": rewards, """returns_to_go""": returns_to_go, """timesteps""": timesteps, """attention_mask""": attention_mask, } return config, inputs_dict @require_torch class A (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[Any] = (DecisionTransformerModel,) if is_torch_available() else () __lowerCamelCase : List[Any] = () __lowerCamelCase : Dict = {'''feature-extraction''': DecisionTransformerModel} if is_torch_available() else {} # Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids __lowerCamelCase : List[Any] = False # Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features __lowerCamelCase : List[str] = False __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : List[Any] = False __lowerCamelCase : int = False __lowerCamelCase : Any = False __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : List[str] = False __lowerCamelCase : Optional[Any] = False __lowerCamelCase : Union[str, Any] = False def a_ ( self : Optional[int] ) -> Dict: """simple docstring""" A__ = DecisionTransformerModelTester(self ) A__ = ConfigTester(self , config_class=__lowerCAmelCase , hidden_size=37 ) def a_ ( self : List[Any] ) -> Tuple: """simple docstring""" self.config_tester.run_common_tests() def a_ ( self : Dict ) -> Dict: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCAmelCase ) @slow def a_ ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = DecisionTransformerModel.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) def a_ ( self : int ) -> int: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(__lowerCAmelCase ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = [ """states""", """actions""", """rewards""", """returns_to_go""", """timesteps""", """attention_mask""", ] self.assertListEqual(arg_names[: len(__lowerCAmelCase )] , __lowerCAmelCase ) @require_torch class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : Dict ) -> str: """simple docstring""" A__ = 2 # number of steps of autoregressive prediction we will perform A__ = 10 # defined by the RL environment, may be normalized A__ = DecisionTransformerModel.from_pretrained("""edbeeching/decision-transformer-gym-hopper-expert""" ) A__ = model.to(__lowerCAmelCase ) A__ = model.config torch.manual_seed(0 ) A__ = torch.randn(1 , 1 , config.state_dim ).to(device=__lowerCAmelCase , dtype=torch.floataa ) # env.reset() A__ = torch.tensor( [[0.2_4_2_7_9_3, -0.2_8_6_9_3_0_7_4, 0.8_7_4_2_6_1_3], [0.6_7_8_1_5_2_7_4, -0.0_8_1_0_1_0_8_5, -0.1_2_9_5_2_1_4_7]] , device=__lowerCAmelCase ) A__ = torch.tensor(__lowerCAmelCase , device=__lowerCAmelCase , dtype=torch.floataa ).reshape(1 , 1 , 1 ) A__ = state A__ = torch.zeros(1 , 0 , config.act_dim , device=__lowerCAmelCase , dtype=torch.floataa ) A__ = torch.zeros(1 , 0 , device=__lowerCAmelCase , dtype=torch.floataa ) A__ = torch.tensor(0 , device=__lowerCAmelCase , dtype=torch.long ).reshape(1 , 1 ) for step in range(__lowerCAmelCase ): A__ = torch.cat([actions, torch.zeros(1 , 1 , config.act_dim , device=__lowerCAmelCase )] , dim=1 ) A__ = torch.cat([rewards, torch.zeros(1 , 1 , device=__lowerCAmelCase )] , dim=1 ) A__ = torch.ones(1 , states.shape[1] ).to(dtype=torch.long , device=states.device ) with torch.no_grad(): A__ , A__ , A__ = model( states=__lowerCAmelCase , actions=__lowerCAmelCase , rewards=__lowerCAmelCase , returns_to_go=__lowerCAmelCase , timesteps=__lowerCAmelCase , attention_mask=__lowerCAmelCase , return_dict=__lowerCAmelCase , ) self.assertEqual(action_pred.shape , actions.shape ) self.assertTrue(torch.allclose(action_pred[0, -1] , expected_outputs[step] , atol=1e-4 ) ) A__ , A__ , A__ , A__ = ( # env.step(action) torch.randn(1 , 1 , config.state_dim ).to(device=__lowerCAmelCase , dtype=torch.floataa ), 1.0, False, {}, ) A__ = action_pred[0, -1] A__ = torch.cat([states, state] , dim=1 ) A__ = returns_to_go[0, -1] - reward A__ = torch.cat([returns_to_go, pred_return.reshape(1 , 1 , 1 )] , dim=1 ) A__ = torch.cat( [timesteps, torch.ones((1, 1) , device=__lowerCAmelCase , dtype=torch.long ) * (step + 1)] , dim=1 )
274
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Any = '''xlm-roberta''' def __init__( self : Optional[Any] , __lowerCAmelCase : List[Any]=3_05_22 , __lowerCAmelCase : int=7_68 , __lowerCAmelCase : Tuple=12 , __lowerCAmelCase : str=12 , __lowerCAmelCase : Union[str, Any]=30_72 , __lowerCAmelCase : Union[str, Any]="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-12 , __lowerCAmelCase : Union[str, Any]=1 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Tuple="absolute" , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=None , **__lowerCAmelCase : str , ) -> Optional[Any]: """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": A__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A__ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
274
1
import unittest from knapsack import knapsack as k class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Tuple ) -> int: """simple docstring""" A__ = 0 A__ = [0] A__ = [0] A__ = len(__lowerCAmelCase ) self.assertEqual(k.knapsack(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) , 0 ) A__ = [60] A__ = [10] A__ = len(__lowerCAmelCase ) self.assertEqual(k.knapsack(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) , 0 ) def a_ ( self : List[Any] ) -> Optional[Any]: """simple docstring""" A__ = 3 A__ = [1, 2, 3] A__ = [3, 2, 1] A__ = len(__lowerCAmelCase ) self.assertEqual(k.knapsack(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) , 5 ) def a_ ( self : Optional[Any] ) -> Optional[int]: """simple docstring""" A__ = 50 A__ = [60, 1_00, 1_20] A__ = [10, 20, 30] A__ = len(__lowerCAmelCase ) self.assertEqual(k.knapsack(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) , 2_20 ) if __name__ == "__main__": unittest.main()
274
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean A : str = 0 A : Any = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] A : Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right A : Union[str, Any] = tuple[int, int] class A : '''simple docstring''' def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Node | None , ) -> None: """simple docstring""" A__ = pos_x A__ = pos_y A__ = (pos_y, pos_x) A__ = goal_x A__ = goal_y A__ = g_cost A__ = parent A__ = self.calculate_heuristic() A__ = self.g_cost + self.h_cost def a_ ( self : Dict ) -> float: """simple docstring""" A__ = self.pos_x - self.goal_x A__ = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(__lowerCAmelCase ) + abs(__lowerCAmelCase ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : int , __lowerCAmelCase : Node ) -> bool: """simple docstring""" return self.f_cost < other.f_cost class A : '''simple docstring''' def __init__( self : Union[str, Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> Tuple: """simple docstring""" A__ = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) A__ = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , __lowerCAmelCase ) A__ = [self.start] A__ = [] A__ = False def a_ ( self : List[str] ) -> list[TPosition]: """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() A__ = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) A__ = self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) return [self.start.pos] def a_ ( self : Optional[Any] , __lowerCAmelCase : Node ) -> list[Node]: """simple docstring""" A__ = [] for action in delta: A__ = parent.pos_x + action[1] A__ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def a_ ( self : List[Any] , __lowerCAmelCase : Node | None ) -> list[TPosition]: """simple docstring""" A__ = node A__ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A__ = current_node.parent path.reverse() return path class A : '''simple docstring''' def __init__( self : Optional[Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> None: """simple docstring""" A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = False def a_ ( self : int ) -> list[TPosition]: """simple docstring""" while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() A__ = self.fwd_astar.open_nodes.pop(0 ) A__ = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( __lowerCAmelCase , __lowerCAmelCase ) self.fwd_astar.closed_nodes.append(__lowerCAmelCase ) self.bwd_astar.closed_nodes.append(__lowerCAmelCase ) A__ = current_bwd_node A__ = current_fwd_node A__ = { self.fwd_astar: self.fwd_astar.get_successors(__lowerCAmelCase ), self.bwd_astar: self.bwd_astar.get_successors(__lowerCAmelCase ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = astar.open_nodes.pop( astar.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(__lowerCAmelCase ) else: astar.open_nodes.append(__lowerCAmelCase ) return [self.fwd_astar.start.pos] def a_ ( self : List[str] , __lowerCAmelCase : Node , __lowerCAmelCase : Node ) -> list[TPosition]: """simple docstring""" A__ = self.fwd_astar.retrace_path(__lowerCAmelCase ) A__ = self.bwd_astar.retrace_path(__lowerCAmelCase ) bwd_path.pop() bwd_path.reverse() A__ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] A : Optional[int] = (0, 0) A : int = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) A : Dict = time.time() A : Optional[Any] = AStar(init, goal) A : Optional[int] = a_star.search() A : Optional[int] = time.time() - start_time print(F'''AStar execution time = {end_time:f} seconds''') A : Dict = time.time() A : Tuple = BidirectionalAStar(init, goal) A : List[Any] = time.time() - bd_start_time print(F'''BidirectionalAStar execution time = {bd_end_time:f} seconds''')
274
1