code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
"""simple docstring""" import argparse import json import os import time import zipfile from get_ci_error_statistics import download_artifact, get_artifacts_links from transformers import logging lowerCAmelCase__ = logging.get_logger(__name__) def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : List[str] = set() lowerCAmelCase : Any = [] def parse_line(SCREAMING_SNAKE_CASE : Dict ): for line in fp: if isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): lowerCAmelCase : Dict = line.decode("UTF-8" ) if "warnings summary (final)" in line: continue # This means we are outside the body of a warning elif not line.startswith(" " ): # process a single warning and move it to `selected_warnings`. if len(SCREAMING_SNAKE_CASE ) > 0: lowerCAmelCase : Optional[Any] = "\n".join(SCREAMING_SNAKE_CASE ) # Only keep the warnings specified in `targets` if any(f""": {x}: """ in warning for x in targets ): selected_warnings.add(SCREAMING_SNAKE_CASE ) buffer.clear() continue else: lowerCAmelCase : Union[str, Any] = line.strip() buffer.append(SCREAMING_SNAKE_CASE ) if from_gh: for filename in os.listdir(SCREAMING_SNAKE_CASE ): lowerCAmelCase : Optional[int] = os.path.join(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if not os.path.isdir(SCREAMING_SNAKE_CASE ): # read the file if filename != "warnings.txt": continue with open(SCREAMING_SNAKE_CASE ) as fp: parse_line(SCREAMING_SNAKE_CASE ) else: try: with zipfile.ZipFile(SCREAMING_SNAKE_CASE ) as z: for filename in z.namelist(): if not os.path.isdir(SCREAMING_SNAKE_CASE ): # read the file if filename != "warnings.txt": continue with z.open(SCREAMING_SNAKE_CASE ) as fp: parse_line(SCREAMING_SNAKE_CASE ) except Exception: logger.warning( f"""{artifact_path} is either an invalid zip file or something else wrong. This file is skipped.""" ) return selected_warnings def a__ ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Union[str, Any] ): '''simple docstring''' lowerCAmelCase : Any = set() lowerCAmelCase : Optional[int] = [os.path.join(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) for p in os.listdir(SCREAMING_SNAKE_CASE ) if (p.endswith(".zip" ) or from_gh)] for p in paths: selected_warnings.update(extract_warnings_from_single_artifact(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) return selected_warnings if __name__ == "__main__": def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' return values.split("," ) lowerCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument('''--workflow_run_id''', type=str, required=True, help='''A GitHub Actions workflow run id.''') parser.add_argument( '''--output_dir''', type=str, required=True, help='''Where to store the downloaded artifacts and other result files.''', ) parser.add_argument('''--token''', default=None, type=str, help='''A token that has actions:read permission.''') # optional parameters parser.add_argument( '''--targets''', default='''DeprecationWarning,UserWarning,FutureWarning''', type=list_str, help='''Comma-separated list of target warning(s) which we want to extract.''', ) parser.add_argument( '''--from_gh''', action='''store_true''', help='''If running from a GitHub action workflow and collecting warnings from its artifacts.''', ) lowerCAmelCase__ = parser.parse_args() lowerCAmelCase__ = args.from_gh if from_gh: # The artifacts have to be downloaded using `actions/download-artifact@v3` pass else: os.makedirs(args.output_dir, exist_ok=True) # get download links lowerCAmelCase__ = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, '''artifacts.json'''), '''w''', encoding='''UTF-8''') as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) # download artifacts for idx, (name, url) in enumerate(artifacts.items()): print(name) print(url) print('''=''' * 80) download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) # extract warnings from artifacts lowerCAmelCase__ = extract_warnings(args.output_dir, args.targets) lowerCAmelCase__ = sorted(selected_warnings) with open(os.path.join(args.output_dir, '''selected_warnings.json'''), '''w''', encoding='''UTF-8''') as fp: json.dump(selected_warnings, fp, ensure_ascii=False, indent=4)
709
"""simple docstring""" 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 ) lowerCAmelCase__ = logging.getLogger(__name__) def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : Optional[Any] = np.argmax(SCREAMING_SNAKE_CASE , axis=1 ) return np.sum(outputs == labels ) def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE , encoding="utf_8" ) as f: lowerCAmelCase : Tuple = csv.reader(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = [] next(SCREAMING_SNAKE_CASE ) # skip the first line for line in tqdm(SCREAMING_SNAKE_CASE ): output.append((" ".join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : str ): '''simple docstring''' lowerCAmelCase : List[Any] = [] for dataset in encoded_datasets: lowerCAmelCase : Dict = len(SCREAMING_SNAKE_CASE ) lowerCAmelCase : str = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) lowerCAmelCase : Tuple = np.zeros((n_batch, 2) , dtype=np.intaa ) lowerCAmelCase : int = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) lowerCAmelCase : List[Any] = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(SCREAMING_SNAKE_CASE ): lowerCAmelCase : int = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase : Union[str, Any] = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase : Tuple = with_conta lowerCAmelCase : Any = with_conta lowerCAmelCase : Optional[Any] = len(SCREAMING_SNAKE_CASE ) - 1 lowerCAmelCase : Dict = len(SCREAMING_SNAKE_CASE ) - 1 lowerCAmelCase : Optional[Any] = with_conta lowerCAmelCase : List[Any] = with_conta lowerCAmelCase : str = mc_label lowerCAmelCase : Dict = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(SCREAMING_SNAKE_CASE ) for t in all_inputs ) ) return tensor_datasets def a__ ( ): '''simple docstring''' lowerCAmelCase : Optional[int] = argparse.ArgumentParser() parser.add_argument("--model_name" , type=SCREAMING_SNAKE_CASE , 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=SCREAMING_SNAKE_CASE , type=SCREAMING_SNAKE_CASE , required=SCREAMING_SNAKE_CASE , help="The output directory where the model predictions and checkpoints will be written." , ) parser.add_argument("--train_dataset" , type=SCREAMING_SNAKE_CASE , default="" ) parser.add_argument("--eval_dataset" , type=SCREAMING_SNAKE_CASE , default="" ) parser.add_argument("--seed" , type=SCREAMING_SNAKE_CASE , default=4_2 ) parser.add_argument("--num_train_epochs" , type=SCREAMING_SNAKE_CASE , default=3 ) parser.add_argument("--train_batch_size" , type=SCREAMING_SNAKE_CASE , default=8 ) parser.add_argument("--eval_batch_size" , type=SCREAMING_SNAKE_CASE , default=1_6 ) parser.add_argument("--adam_epsilon" , default=1E-8 , type=SCREAMING_SNAKE_CASE , help="Epsilon for Adam optimizer." ) parser.add_argument("--max_grad_norm" , type=SCREAMING_SNAKE_CASE , default=1 ) parser.add_argument( "--max_steps" , default=-1 , type=SCREAMING_SNAKE_CASE , help=( "If > 0: set total number of training steps to perform. Override num_train_epochs." ) , ) parser.add_argument( "--gradient_accumulation_steps" , type=SCREAMING_SNAKE_CASE , default=1 , help="Number of updates steps to accumulate before performing a backward/update pass." , ) parser.add_argument("--learning_rate" , type=SCREAMING_SNAKE_CASE , default=6.2_5E-5 ) parser.add_argument("--warmup_steps" , default=0 , type=SCREAMING_SNAKE_CASE , help="Linear warmup over warmup_steps." ) parser.add_argument("--lr_schedule" , type=SCREAMING_SNAKE_CASE , default="warmup_linear" ) parser.add_argument("--weight_decay" , type=SCREAMING_SNAKE_CASE , default=0.01 ) parser.add_argument("--lm_coef" , type=SCREAMING_SNAKE_CASE , default=0.9 ) parser.add_argument("--n_valid" , type=SCREAMING_SNAKE_CASE , default=3_7_4 ) parser.add_argument("--server_ip" , type=SCREAMING_SNAKE_CASE , default="" , help="Can be used for distant debugging." ) parser.add_argument("--server_port" , type=SCREAMING_SNAKE_CASE , default="" , help="Can be used for distant debugging." ) lowerCAmelCase : Tuple = parser.parse_args() print(SCREAMING_SNAKE_CASE ) 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=SCREAMING_SNAKE_CASE ) 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 ) lowerCAmelCase : Optional[int] = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) lowerCAmelCase : Optional[int] = torch.cuda.device_count() logger.info("device: {}, n_gpu {}".format(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) 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 lowerCAmelCase : str = ["_start_", "_delimiter_", "_classify_"] lowerCAmelCase : Dict = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(SCREAMING_SNAKE_CASE ) ) model.to(SCREAMING_SNAKE_CASE ) # Load and encode the datasets def tokenize_and_encode(SCREAMING_SNAKE_CASE : Optional[Any] ): if isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(SCREAMING_SNAKE_CASE ) ) elif isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return obj return [tokenize_and_encode(SCREAMING_SNAKE_CASE ) for o in obj] logger.info("Encoding dataset..." ) lowerCAmelCase : Optional[Any] = load_rocstories_dataset(args.train_dataset ) lowerCAmelCase : int = load_rocstories_dataset(args.eval_dataset ) lowerCAmelCase : Tuple = (train_dataset, eval_dataset) lowerCAmelCase : Dict = tokenize_and_encode(SCREAMING_SNAKE_CASE ) # Compute the max input length for the Transformer lowerCAmelCase : Any = model.config.n_positions // 2 - 2 lowerCAmelCase : int = 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 ) lowerCAmelCase : Union[str, Any] = min(SCREAMING_SNAKE_CASE , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders lowerCAmelCase : Any = pre_process_datasets(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , *SCREAMING_SNAKE_CASE ) lowerCAmelCase , lowerCAmelCase : Tuple = tensor_datasets[0], tensor_datasets[1] lowerCAmelCase : List[str] = TensorDataset(*SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = RandomSampler(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = DataLoader(SCREAMING_SNAKE_CASE , sampler=SCREAMING_SNAKE_CASE , batch_size=args.train_batch_size ) lowerCAmelCase : int = TensorDataset(*SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = SequentialSampler(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = DataLoader(SCREAMING_SNAKE_CASE , sampler=SCREAMING_SNAKE_CASE , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: lowerCAmelCase : int = args.max_steps lowerCAmelCase : str = args.max_steps // (len(SCREAMING_SNAKE_CASE ) // args.gradient_accumulation_steps) + 1 else: lowerCAmelCase : Union[str, Any] = len(SCREAMING_SNAKE_CASE ) // args.gradient_accumulation_steps * args.num_train_epochs lowerCAmelCase : Dict = list(model.named_parameters() ) lowerCAmelCase : str = ["bias", "LayerNorm.bias", "LayerNorm.weight"] lowerCAmelCase : Tuple = [ { "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}, ] lowerCAmelCase : Tuple = AdamW(SCREAMING_SNAKE_CASE , lr=args.learning_rate , eps=args.adam_epsilon ) lowerCAmelCase : str = get_linear_schedule_with_warmup( SCREAMING_SNAKE_CASE , num_warmup_steps=args.warmup_steps , num_training_steps=SCREAMING_SNAKE_CASE ) if args.do_train: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Dict = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="Epoch" ): lowerCAmelCase : Optional[int] = 0 lowerCAmelCase : Union[str, Any] = 0 lowerCAmelCase : Tuple = tqdm(SCREAMING_SNAKE_CASE , desc="Training" ) for step, batch in enumerate(SCREAMING_SNAKE_CASE ): lowerCAmelCase : Tuple = tuple(t.to(SCREAMING_SNAKE_CASE ) for t in batch ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : int = batch lowerCAmelCase : Optional[int] = model(SCREAMING_SNAKE_CASE , mc_token_ids=SCREAMING_SNAKE_CASE , lm_labels=SCREAMING_SNAKE_CASE , mc_labels=SCREAMING_SNAKE_CASE ) lowerCAmelCase : int = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() lowerCAmelCase : Optional[Any] = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 lowerCAmelCase : int = "Training loss: {:.2e} lr: {:.2e}".format(SCREAMING_SNAKE_CASE , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer lowerCAmelCase : Optional[int] = model.module if hasattr(SCREAMING_SNAKE_CASE , "module" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` lowerCAmelCase : Any = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE ) torch.save(model_to_save.state_dict() , SCREAMING_SNAKE_CASE ) model_to_save.config.to_json_file(SCREAMING_SNAKE_CASE ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned lowerCAmelCase : List[str] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) lowerCAmelCase : Dict = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(SCREAMING_SNAKE_CASE ) if args.do_eval: model.eval() lowerCAmelCase , lowerCAmelCase : Optional[int] = 0, 0 lowerCAmelCase , lowerCAmelCase : Any = 0, 0 for batch in tqdm(SCREAMING_SNAKE_CASE , desc="Evaluating" ): lowerCAmelCase : List[Any] = tuple(t.to(SCREAMING_SNAKE_CASE ) for t in batch ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Tuple = batch with torch.no_grad(): lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Optional[Any] = model( SCREAMING_SNAKE_CASE , mc_token_ids=SCREAMING_SNAKE_CASE , lm_labels=SCREAMING_SNAKE_CASE , mc_labels=SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = mc_logits.detach().cpu().numpy() lowerCAmelCase : List[str] = mc_labels.to("cpu" ).numpy() lowerCAmelCase : Any = accuracy(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 lowerCAmelCase : List[Any] = eval_loss / nb_eval_steps lowerCAmelCase : List[Any] = eval_accuracy / nb_eval_examples lowerCAmelCase : Tuple = tr_loss / nb_tr_steps if args.do_train else None lowerCAmelCase : Any = {"eval_loss": eval_loss, "eval_accuracy": eval_accuracy, "train_loss": train_loss} lowerCAmelCase : List[str] = os.path.join(args.output_dir , "eval_results.txt" ) with open(SCREAMING_SNAKE_CASE , "w" ) as writer: logger.info("***** Eval results *****" ) for key in sorted(result.keys() ): logger.info(" %s = %s" , SCREAMING_SNAKE_CASE , str(result[key] ) ) writer.write("%s = %s\n" % (key, str(result[key] )) ) if __name__ == "__main__": main()
681
0
"""simple docstring""" from __future__ import annotations def a__ ( SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : int ) -> str: '''simple docstring''' lowerCAmelCase : Union[str, Any] = position lowerCAmelCase : Dict = [ (y + 1, x + 2), (y - 1, x + 2), (y + 1, x - 2), (y - 1, x - 2), (y + 2, x + 1), (y + 2, x - 1), (y - 2, x + 1), (y - 2, x - 1), ] lowerCAmelCase : int = [] for position in positions: lowerCAmelCase : Optional[int] = position if 0 <= y_test < n and 0 <= x_test < n: permissible_positions.append(snake_case_ ) return permissible_positions def a__ ( SCREAMING_SNAKE_CASE : Optional[int] ) -> Optional[int]: '''simple docstring''' return not any(elem == 0 for row in board for elem in row ) def a__ ( SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : Optional[int] ) -> Optional[int]: '''simple docstring''' if is_complete(snake_case_ ): return True for position in get_valid_pos(snake_case_ , len(snake_case_ ) ): lowerCAmelCase : List[Any] = position if board[y][x] == 0: lowerCAmelCase : Tuple = curr + 1 if open_knight_tour_helper(snake_case_ , snake_case_ , curr + 1 ): return True lowerCAmelCase : int = 0 return False def a__ ( SCREAMING_SNAKE_CASE : int ) -> List[str]: '''simple docstring''' lowerCAmelCase : int = [[0 for i in range(snake_case_ )] for j in range(snake_case_ )] for i in range(snake_case_ ): for j in range(snake_case_ ): lowerCAmelCase : int = 1 if open_knight_tour_helper(snake_case_ , (i, j) , 1 ): return board lowerCAmelCase : List[str] = 0 lowerCAmelCase : List[str] = f"""Open Kight Tour cannot be performed on a board of size {n}""" raise ValueError(snake_case_ ) if __name__ == "__main__": import doctest doctest.testmod()
710
"""simple docstring""" from typing import List, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''huggingface/informer-tourism-monthly''': ( '''https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json''' ), # See all Informer models at https://huggingface.co/models?filter=informer } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Optional[Any] ="informer" a : int ={ "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", "num_hidden_layers": "encoder_layers", } def __init__( self , snake_case__ = None , snake_case__ = None , snake_case__ = "student_t" , snake_case__ = "nll" , snake_case__ = 1 , snake_case__ = None , snake_case__ = "mean" , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = None , snake_case__ = None , snake_case__ = 64 , snake_case__ = 32 , snake_case__ = 32 , snake_case__ = 2 , snake_case__ = 2 , snake_case__ = 2 , snake_case__ = 2 , snake_case__ = True , snake_case__ = "gelu" , snake_case__ = 0.05 , snake_case__ = 0.1 , snake_case__ = 0.1 , snake_case__ = 0.1 , snake_case__ = 0.1 , snake_case__ = 100 , snake_case__ = 0.02 , snake_case__=True , snake_case__ = "prob" , snake_case__ = 5 , snake_case__ = True , **snake_case__ , ): """simple docstring""" lowerCAmelCase : Union[str, Any] = prediction_length lowerCAmelCase : Union[str, Any] = context_length or prediction_length lowerCAmelCase : List[Any] = distribution_output lowerCAmelCase : Optional[int] = loss lowerCAmelCase : Optional[int] = input_size lowerCAmelCase : str = num_time_features lowerCAmelCase : Any = lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7] lowerCAmelCase : Dict = scaling lowerCAmelCase : List[str] = num_dynamic_real_features lowerCAmelCase : Dict = num_static_real_features lowerCAmelCase : Dict = num_static_categorical_features # set cardinality if cardinality and num_static_categorical_features > 0: if len(snake_case__ ) != num_static_categorical_features: raise ValueError( "The cardinality should be a list of the same length as `num_static_categorical_features`" ) lowerCAmelCase : List[str] = cardinality else: lowerCAmelCase : Optional[Any] = [0] # set embedding_dimension if embedding_dimension and num_static_categorical_features > 0: if len(snake_case__ ) != num_static_categorical_features: raise ValueError( "The embedding dimension should be a list of the same length as `num_static_categorical_features`" ) lowerCAmelCase : List[Any] = embedding_dimension else: lowerCAmelCase : Dict = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] lowerCAmelCase : List[Any] = num_parallel_samples # Transformer architecture configuration lowerCAmelCase : Any = input_size * len(self.lags_sequence ) + self._number_of_features lowerCAmelCase : str = d_model lowerCAmelCase : List[str] = encoder_attention_heads lowerCAmelCase : int = decoder_attention_heads lowerCAmelCase : Optional[Any] = encoder_ffn_dim lowerCAmelCase : Dict = decoder_ffn_dim lowerCAmelCase : int = encoder_layers lowerCAmelCase : Union[str, Any] = decoder_layers lowerCAmelCase : Tuple = dropout lowerCAmelCase : List[Any] = attention_dropout lowerCAmelCase : int = activation_dropout lowerCAmelCase : Union[str, Any] = encoder_layerdrop lowerCAmelCase : int = decoder_layerdrop lowerCAmelCase : Optional[int] = activation_function lowerCAmelCase : int = init_std lowerCAmelCase : Optional[Any] = use_cache # Informer lowerCAmelCase : Dict = attention_type lowerCAmelCase : Any = sampling_factor lowerCAmelCase : Optional[int] = distil super().__init__(is_encoder_decoder=snake_case__ , **snake_case__ ) @property def lowercase__ ( self ): """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 )
681
0
"""simple docstring""" from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, flip_channel_order, get_resize_output_image_size, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging if is_vision_available(): import PIL if is_torch_available(): import torch lowerCAmelCase__ = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ ( SCREAMING_SNAKE_CASE_ ): """simple docstring""" a : Union[str, Any] =["pixel_values"] def __init__( self , snake_case__ = True , snake_case__ = None , snake_case__ = PILImageResampling.BILINEAR , snake_case__ = True , snake_case__ = 1 / 255 , snake_case__ = True , snake_case__ = None , snake_case__ = True , **snake_case__ , ): """simple docstring""" super().__init__(**UpperCamelCase__ ) lowerCAmelCase : Optional[Any] = size if size is not None else {'''shortest_edge''': 224} lowerCAmelCase : Optional[int] = get_size_dict(UpperCamelCase__ , default_to_square=UpperCamelCase__ ) lowerCAmelCase : Union[str, Any] = crop_size if crop_size is not None else {'''height''': 256, '''width''': 256} lowerCAmelCase : int = get_size_dict(UpperCamelCase__ , param_name="crop_size" ) lowerCAmelCase : Union[str, Any] = do_resize lowerCAmelCase : str = size lowerCAmelCase : List[str] = resample lowerCAmelCase : Dict = do_rescale lowerCAmelCase : Optional[Any] = rescale_factor lowerCAmelCase : List[str] = do_center_crop lowerCAmelCase : Tuple = crop_size lowerCAmelCase : Dict = do_flip_channel_order def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = PIL.Image.BILINEAR , snake_case__ = None , **snake_case__ , ): """simple docstring""" lowerCAmelCase : Tuple = get_size_dict(UpperCamelCase__ , default_to_square=UpperCamelCase__ ) if "shortest_edge" not in size: raise ValueError(f"""The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}""" ) lowerCAmelCase : Any = get_resize_output_image_size(UpperCamelCase__ , size=size["shortest_edge"] , default_to_square=UpperCamelCase__ ) return resize(UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = None , **snake_case__ , ): """simple docstring""" lowerCAmelCase : str = get_size_dict(UpperCamelCase__ ) if "height" not in size or "width" not in size: raise ValueError(f"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}""" ) return center_crop(UpperCamelCase__ , size=(size["height"], size["width"]) , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = None , **snake_case__ , ): """simple docstring""" return rescale(UpperCamelCase__ , scale=UpperCamelCase__ , data_format=UpperCamelCase__ , **UpperCamelCase__ ) def lowercase__ ( self , snake_case__ , snake_case__ = None ): """simple docstring""" return flip_channel_order(UpperCamelCase__ , data_format=UpperCamelCase__ ) def lowercase__ ( self , snake_case__ , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = ChannelDimension.FIRST , **snake_case__ , ): """simple docstring""" lowerCAmelCase : Optional[Any] = do_resize if do_resize is not None else self.do_resize lowerCAmelCase : List[Any] = resample if resample is not None else self.resample lowerCAmelCase : List[str] = do_rescale if do_rescale is not None else self.do_rescale lowerCAmelCase : List[str] = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCAmelCase : Optional[int] = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCAmelCase : Union[str, Any] = ( do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order ) lowerCAmelCase : List[Any] = size if size is not None else self.size lowerCAmelCase : Dict = get_size_dict(UpperCamelCase__ , default_to_square=UpperCamelCase__ ) lowerCAmelCase : Any = crop_size if crop_size is not None else self.crop_size lowerCAmelCase : Optional[Any] = get_size_dict(UpperCamelCase__ , param_name="crop_size" ) lowerCAmelCase : Optional[int] = make_list_of_images(UpperCamelCase__ ) if not valid_images(UpperCamelCase__ ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) # All transformations expect numpy arrays. lowerCAmelCase : List[Any] = [to_numpy_array(UpperCamelCase__ ) for image in images] if do_resize: lowerCAmelCase : List[Any] = [self.resize(image=UpperCamelCase__ , size=UpperCamelCase__ , resample=UpperCamelCase__ ) for image in images] if do_center_crop: lowerCAmelCase : Optional[Any] = [self.center_crop(image=UpperCamelCase__ , size=UpperCamelCase__ ) for image in images] if do_rescale: lowerCAmelCase : Union[str, Any] = [self.rescale(image=UpperCamelCase__ , scale=UpperCamelCase__ ) for image in images] # the pretrained checkpoints assume images are BGR, not RGB if do_flip_channel_order: lowerCAmelCase : Dict = [self.flip_channel_order(image=UpperCamelCase__ ) for image in images] lowerCAmelCase : Optional[Any] = [to_channel_dimension_format(UpperCamelCase__ , UpperCamelCase__ ) for image in images] lowerCAmelCase : Tuple = {'''pixel_values''': images} return BatchFeature(data=UpperCamelCase__ , tensor_type=UpperCamelCase__ ) def lowercase__ ( self , snake_case__ , snake_case__ = None ): """simple docstring""" lowerCAmelCase : List[str] = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(UpperCamelCase__ ) != len(UpperCamelCase__ ): raise ValueError( "Make sure that you pass in as many target sizes as the batch dimension of the logits" ) if is_torch_tensor(UpperCamelCase__ ): lowerCAmelCase : List[str] = target_sizes.numpy() lowerCAmelCase : int = [] for idx in range(len(UpperCamelCase__ ) ): lowerCAmelCase : List[str] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode="bilinear" , align_corners=UpperCamelCase__ ) lowerCAmelCase : Union[str, Any] = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(UpperCamelCase__ ) else: lowerCAmelCase : Dict = logits.argmax(dim=1 ) lowerCAmelCase : int = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
711
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' if num < 0: return False lowerCAmelCase : int = num lowerCAmelCase : int = 0 while num > 0: lowerCAmelCase : Dict = rev_num * 1_0 + (num % 1_0) num //= 1_0 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
681
0
from argparse import ArgumentParser from .env import EnvironmentCommand def a__ ( ): '''simple docstring''' lowerCAmelCase : List[Any] = ArgumentParser("Diffusers CLI tool" , usage="diffusers-cli <command> [<args>]" ) lowerCAmelCase : str = parser.add_subparsers(help="diffusers-cli command helpers" ) # Register commands EnvironmentCommand.register_subcommand(lowerCamelCase_ ) # Let's go lowerCAmelCase : str = parser.parse_args() if not hasattr(lowerCamelCase_ , "func" ): parser.print_help() exit(1 ) # Run lowerCAmelCase : Dict = args.func(lowerCamelCase_ ) service.run() if __name__ == "__main__": main()
712
"""simple docstring""" import os import re import shutil import sys import tempfile import unittest import black lowerCAmelCase__ = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, '''utils''')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If DDPMSchedulerOutput is changed in scheduling_ddpm.py, this code needs to be manually updated. lowerCAmelCase__ = ''' \""" Output class for the scheduler\'s step function output. Args: prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the denoising loop. pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): The predicted denoised sample (x_{0}) based on the model output from the current timestep. `pred_original_sample` can be used to preview progress or for guidance. \""" prev_sample: torch.FloatTensor pred_original_sample: Optional[torch.FloatTensor] = None ''' class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = tempfile.mkdtemp() os.makedirs(os.path.join(self.diffusers_dir , "schedulers/" ) ) lowerCAmelCase : List[str] = self.diffusers_dir shutil.copy( os.path.join(snake_case__ , "src/diffusers/schedulers/scheduling_ddpm.py" ) , os.path.join(self.diffusers_dir , "schedulers/scheduling_ddpm.py" ) , ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = "src/diffusers" shutil.rmtree(self.diffusers_dir ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__=None ): """simple docstring""" lowerCAmelCase : Union[str, Any] = comment + f"""\nclass {class_name}(nn.Module):\n""" + class_code if overwrite_result is not None: lowerCAmelCase : str = comment + f"""\nclass {class_name}(nn.Module):\n""" + overwrite_result lowerCAmelCase : int = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) lowerCAmelCase : int = black.format_str(snake_case__ , mode=snake_case__ ) lowerCAmelCase : Dict = os.path.join(self.diffusers_dir , "new_code.py" ) with open(snake_case__ , "w" , newline="\n" ) as f: f.write(snake_case__ ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(snake_case__ ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=snake_case__ ) with open(snake_case__ , "r" ) as f: self.assertTrue(f.read() , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = check_copies.find_code_in_diffusers("schedulers.scheduling_ddpm.DDPMSchedulerOutput" ) self.assertEqual(snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput" , "DDPMSchedulerOutput" , REFERENCE_CODE + "\n" , ) # With no empty line at the end self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput" , "DDPMSchedulerOutput" , snake_case__ , ) # Copy consistency with rename self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test" , "TestSchedulerOutput" , re.sub("DDPM" , "Test" , snake_case__ ) , ) # Copy consistency with a really long name lowerCAmelCase : Union[str, Any] = "TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason" self.check_copy_consistency( f"""# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}""" , f"""{long_class_name}SchedulerOutput""" , re.sub("Bert" , snake_case__ , snake_case__ ) , ) # Copy consistency with overwrite self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test" , "TestSchedulerOutput" , snake_case__ , overwrite_result=re.sub("DDPM" , "Test" , snake_case__ ) , )
681
0
"""simple docstring""" import unittest from transformers import XLMConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMWithLMHeadModel, ) from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__=13 , snake_case__=7 , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=False , snake_case__=False , snake_case__=False , snake_case__=2 , snake_case__=99 , snake_case__=0 , snake_case__=32 , snake_case__=5 , snake_case__=4 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=512 , snake_case__=2 , snake_case__=0.02 , snake_case__=2 , snake_case__=4 , snake_case__="last" , snake_case__=True , snake_case__=None , snake_case__=0 , ): """simple docstring""" lowerCAmelCase : int = parent lowerCAmelCase : Optional[Any] = batch_size lowerCAmelCase : Union[str, Any] = seq_length lowerCAmelCase : Optional[int] = is_training lowerCAmelCase : Any = use_input_lengths lowerCAmelCase : Any = use_token_type_ids lowerCAmelCase : List[Any] = use_labels lowerCAmelCase : List[Any] = gelu_activation lowerCAmelCase : Union[str, Any] = sinusoidal_embeddings lowerCAmelCase : List[str] = causal lowerCAmelCase : str = asm lowerCAmelCase : Union[str, Any] = n_langs lowerCAmelCase : Union[str, Any] = vocab_size lowerCAmelCase : Optional[Any] = n_special lowerCAmelCase : List[str] = hidden_size lowerCAmelCase : Dict = num_hidden_layers lowerCAmelCase : Tuple = num_attention_heads lowerCAmelCase : Dict = hidden_dropout_prob lowerCAmelCase : List[Any] = attention_probs_dropout_prob lowerCAmelCase : List[Any] = max_position_embeddings lowerCAmelCase : Optional[Any] = type_sequence_label_size lowerCAmelCase : Optional[int] = initializer_range lowerCAmelCase : List[str] = num_labels lowerCAmelCase : str = num_choices lowerCAmelCase : Optional[Any] = summary_type lowerCAmelCase : List[Any] = use_proj lowerCAmelCase : Optional[int] = scope lowerCAmelCase : Any = bos_token_id def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCAmelCase : Dict = random_attention_mask([self.batch_size, self.seq_length] ) lowerCAmelCase : str = None if self.use_input_lengths: lowerCAmelCase : Dict = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length lowerCAmelCase : Union[str, Any] = None if self.use_token_type_ids: lowerCAmelCase : Any = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) lowerCAmelCase : Optional[int] = None lowerCAmelCase : Optional[Any] = None lowerCAmelCase : List[str] = None if self.use_labels: lowerCAmelCase : List[str] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCAmelCase : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowerCAmelCase : Union[str, Any] = ids_tensor([self.batch_size] , 2 ).float() lowerCAmelCase : Union[str, Any] = ids_tensor([self.batch_size] , self.num_choices ) lowerCAmelCase : Tuple = self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def lowercase__ ( self ): """simple docstring""" return XLMConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , num_labels=self.num_labels , bos_token_id=self.bos_token_id , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" lowerCAmelCase : Dict = XLMModel(config=snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : Optional[int] = model(snake_case__ , lengths=snake_case__ , langs=snake_case__ ) lowerCAmelCase : Any = model(snake_case__ , langs=snake_case__ ) lowerCAmelCase : str = model(snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" lowerCAmelCase : Dict = XLMWithLMHeadModel(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : List[Any] = model(snake_case__ , token_type_ids=snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" lowerCAmelCase : Any = XLMForQuestionAnsweringSimple(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : List[Any] = model(snake_case__ ) lowerCAmelCase : Any = model(snake_case__ , start_positions=snake_case__ , end_positions=snake_case__ ) lowerCAmelCase : List[str] = outputs self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" lowerCAmelCase : List[str] = XLMForQuestionAnswering(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : str = model(snake_case__ ) lowerCAmelCase : Union[str, Any] = model( snake_case__ , start_positions=snake_case__ , end_positions=snake_case__ , cls_index=snake_case__ , is_impossible=snake_case__ , p_mask=snake_case__ , ) lowerCAmelCase : Any = model( snake_case__ , start_positions=snake_case__ , end_positions=snake_case__ , cls_index=snake_case__ , is_impossible=snake_case__ , ) (lowerCAmelCase ) : str = result_with_labels.to_tuple() lowerCAmelCase : Any = model(snake_case__ , start_positions=snake_case__ , end_positions=snake_case__ ) (lowerCAmelCase ) : List[str] = result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape , () ) self.parent.assertEqual(result.start_top_log_probs.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual(result.start_top_index.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual( result.end_top_log_probs.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual( result.end_top_index.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual(result.cls_logits.shape , (self.batch_size,) ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" lowerCAmelCase : List[str] = XLMForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : str = model(snake_case__ ) lowerCAmelCase : int = model(snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" lowerCAmelCase : Optional[int] = self.num_labels lowerCAmelCase : List[str] = XLMForTokenClassification(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : int = model(snake_case__ , attention_mask=snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" lowerCAmelCase : str = self.num_choices lowerCAmelCase : Union[str, Any] = XLMForMultipleChoice(config=snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : List[Any] = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCAmelCase : List[str] = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCAmelCase : List[str] = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCAmelCase : Optional[int] = model( snake_case__ , attention_mask=snake_case__ , token_type_ids=snake_case__ , labels=snake_case__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = self.prepare_config_and_inputs() ( lowerCAmelCase ) : int = config_and_inputs lowerCAmelCase : Any = {"input_ids": input_ids, "token_type_ids": token_type_ids, "lengths": input_lengths} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE__ ( _A , _A , _A , unittest.TestCase ): """simple docstring""" a : Dict =( ( XLMModel, XLMWithLMHeadModel, XLMForQuestionAnswering, XLMForSequenceClassification, XLMForQuestionAnsweringSimple, XLMForTokenClassification, XLMForMultipleChoice, ) if is_torch_available() else () ) a : List[Any] =( (XLMWithLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable a : Any =( { "feature-extraction": XLMModel, "fill-mask": XLMWithLMHeadModel, "question-answering": XLMForQuestionAnsweringSimple, "text-classification": XLMForSequenceClassification, "text-generation": XLMWithLMHeadModel, "token-classification": XLMForTokenClassification, "zero-shot": XLMForSequenceClassification, } if is_torch_available() else {} ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("Fast" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__=False ): """simple docstring""" lowerCAmelCase : Tuple = super()._prepare_for_class(snake_case__ , snake_case__ , return_labels=snake_case__ ) if return_labels: if model_class.__name__ == "XLMForQuestionAnswering": lowerCAmelCase : Optional[Any] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=snake_case__ ) lowerCAmelCase : List[str] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=snake_case__ ) return inputs_dict def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = XLMModelTester(self ) lowerCAmelCase : Union[str, Any] = ConfigTester(self , config_class=snake_case__ , emb_dim=37 ) def lowercase__ ( self ): """simple docstring""" self.config_tester.run_common_tests() def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_model(*snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_lm_head(*snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_simple_qa(*snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_qa(*snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_sequence_classif(*snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_token_classif(*snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_for_multiple_choice(*snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__=False , snake_case__=1 ): """simple docstring""" self.assertIsInstance(snake_case__ , snake_case__ ) self.assertListEqual( [isinstance(snake_case__ , snake_case__ ) for iter_attentions in attentions] , [True] * len(snake_case__ ) ) self.assertEqual(len(snake_case__ ) , (max_length - min_length) * num_beam_groups ) for idx, iter_attentions in enumerate(snake_case__ ): # adds PAD dummy token lowerCAmelCase : List[str] = min_length + idx + 1 lowerCAmelCase : Dict = min_length + idx + 1 lowerCAmelCase : List[str] = ( batch_size * num_beam_groups, config.num_attention_heads, tgt_len, src_len, ) # check attn size self.assertListEqual( [layer_attention.shape for layer_attention in iter_attentions] , [expected_shape] * len(snake_case__ ) ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__=False , snake_case__=1 ): """simple docstring""" self.assertIsInstance(snake_case__ , snake_case__ ) self.assertListEqual( [isinstance(snake_case__ , snake_case__ ) for iter_hidden_states in hidden_states] , [True] * len(snake_case__ ) , ) self.assertEqual(len(snake_case__ ) , (max_length - min_length) * num_beam_groups ) for idx, iter_hidden_states in enumerate(snake_case__ ): # adds PAD dummy token lowerCAmelCase : Dict = min_length + idx + 1 lowerCAmelCase : str = (batch_size * num_beam_groups, seq_len, config.hidden_size) # check hidden size self.assertListEqual( [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states] , [expected_shape] * len(snake_case__ ) , ) pass @slow def lowercase__ ( self ): """simple docstring""" for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase : str = XLMModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) @require_torch class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" @slow def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = XLMWithLMHeadModel.from_pretrained("xlm-mlm-en-2048" ) model.to(snake_case__ ) lowerCAmelCase : Optional[int] = torch.tensor([[14, 447]] , dtype=torch.long , device=snake_case__ ) # the president lowerCAmelCase : str = [ 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, ] # the president the president the president the president the president the president the president the president the president the president # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference lowerCAmelCase : Optional[Any] = model.generate(snake_case__ , do_sample=snake_case__ ) self.assertListEqual(output_ids[0].cpu().numpy().tolist() , snake_case__ )
713
"""simple docstring""" import re from flax.core.frozen_dict import freeze from flax.traverse_util import flatten_dict, unflatten_dict from jax.experimental import PartitionSpec as P # Sentinels lowerCAmelCase__ = object() # For specifying empty leaf dict `{}` lowerCAmelCase__ = object() def a__ ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' lowerCAmelCase : Optional[int] = tuple((re.compile(x + "$" ) for x in qs) ) for i in range(len(SCREAMING_SNAKE_CASE ) - len(SCREAMING_SNAKE_CASE ) + 1 ): lowerCAmelCase : int = [x.match(SCREAMING_SNAKE_CASE ) for x, y in zip(SCREAMING_SNAKE_CASE , ks[i:] )] if matches and all(SCREAMING_SNAKE_CASE ): return True return False def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' def replace(SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Dict ): for rule, replacement in rules: if _match(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return replacement return val return replace def a__ ( ): '''simple docstring''' return [ # embeddings (("transformer", "wpe", "embedding"), P("mp" , SCREAMING_SNAKE_CASE )), (("transformer", "wte", "embedding"), P("mp" , SCREAMING_SNAKE_CASE )), # atention (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(SCREAMING_SNAKE_CASE , "mp" )), (("attention", "out_proj", "kernel"), P("mp" , SCREAMING_SNAKE_CASE )), (("attention", "out_proj", "bias"), None), # mlp (("mlp", "c_fc", "kernel"), P(SCREAMING_SNAKE_CASE , "mp" )), (("mlp", "c_fc", "bias"), P("mp" )), (("mlp", "c_proj", "kernel"), P("mp" , SCREAMING_SNAKE_CASE )), (("mlp", "c_proj", "bias"), None), # layer norms ((r"ln_\d+", "bias"), None), ((r"\d+", r"ln_\d+", "scale"), None), (("ln_f", "bias"), None), (("ln_f", "scale"), None), ] def a__ ( SCREAMING_SNAKE_CASE : Union[str, Any] ): '''simple docstring''' lowerCAmelCase : Any = _get_partition_rules() lowerCAmelCase : Tuple = _replacement_rules(SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = {k: _unmatched for k in flatten_dict(SCREAMING_SNAKE_CASE )} lowerCAmelCase : List[Any] = {k: replace(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) for k, v in initd.items()} assert _unmatched not in result.values(), "Incomplete partition spec." return freeze(unflatten_dict(SCREAMING_SNAKE_CASE ) )
681
0
"""simple docstring""" from collections import OrderedDict from typing import Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...feature_extraction_utils import FeatureExtractionMixin from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType, logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''deepmind/language-perceiver''': '''https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json''', # See all Perceiver models at https://huggingface.co/models?filter=perceiver } class SCREAMING_SNAKE_CASE__ ( __lowercase ): """simple docstring""" a : List[str] ='perceiver' def __init__( self , snake_case__=256 , snake_case__=1_280 , snake_case__=768 , snake_case__=1 , snake_case__=26 , snake_case__=8 , snake_case__=8 , snake_case__=None , snake_case__=None , snake_case__="kv" , snake_case__=1 , snake_case__=1 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__=True , snake_case__=262 , snake_case__=2_048 , snake_case__=56 , snake_case__=[368, 496] , snake_case__=16 , snake_case__=1_920 , snake_case__=16 , snake_case__=[1, 16, 224, 224] , **snake_case__ , ): """simple docstring""" super().__init__(**__A ) lowerCAmelCase : List[str] = num_latents lowerCAmelCase : Union[str, Any] = d_latents lowerCAmelCase : List[str] = d_model lowerCAmelCase : str = num_blocks lowerCAmelCase : List[str] = num_self_attends_per_block lowerCAmelCase : Optional[int] = num_self_attention_heads lowerCAmelCase : List[Any] = num_cross_attention_heads lowerCAmelCase : Optional[int] = qk_channels lowerCAmelCase : Tuple = v_channels lowerCAmelCase : Dict = cross_attention_shape_for_attention lowerCAmelCase : Optional[int] = self_attention_widening_factor lowerCAmelCase : Optional[int] = cross_attention_widening_factor lowerCAmelCase : str = hidden_act lowerCAmelCase : Any = attention_probs_dropout_prob lowerCAmelCase : Union[str, Any] = initializer_range lowerCAmelCase : List[Any] = layer_norm_eps lowerCAmelCase : Dict = use_query_residual # masked language modeling attributes lowerCAmelCase : Any = vocab_size lowerCAmelCase : Tuple = max_position_embeddings # image classification attributes lowerCAmelCase : Any = image_size # flow attributes lowerCAmelCase : List[Any] = train_size # multimodal autoencoding attributes lowerCAmelCase : int = num_frames lowerCAmelCase : Optional[Any] = audio_samples_per_frame lowerCAmelCase : Optional[int] = samples_per_patch lowerCAmelCase : Optional[Any] = output_shape class SCREAMING_SNAKE_CASE__ ( __lowercase ): """simple docstring""" @property def lowercase__ ( self ): """simple docstring""" if self.task == "multiple-choice": lowerCAmelCase : List[Any] = {0: "batch", 1: "choice", 2: "sequence"} else: lowerCAmelCase : List[Any] = {0: "batch", 1: "sequence"} return OrderedDict( [ ("inputs", dynamic_axis), ("attention_mask", dynamic_axis), ] ) @property def lowercase__ ( self ): """simple docstring""" return 1e-4 def lowercase__ ( self , snake_case__ , snake_case__ = -1 , snake_case__ = -1 , snake_case__ = -1 , snake_case__ = False , snake_case__ = None , snake_case__ = 3 , snake_case__ = 40 , snake_case__ = 40 , ): """simple docstring""" if isinstance(__A , __A ): # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX lowerCAmelCase : str = compute_effective_axis_dimension( __A , 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 lowerCAmelCase : Tuple = preprocessor.num_special_tokens_to_add(__A ) lowerCAmelCase : Union[str, Any] = compute_effective_axis_dimension( __A , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__A ) # Generate dummy inputs according to compute batch and sequence lowerCAmelCase : Optional[int] = [" ".join(["a"] ) * seq_length] * batch_size lowerCAmelCase : List[Any] = dict(preprocessor(__A , return_tensors=__A ) ) lowerCAmelCase : Dict = inputs.pop("input_ids" ) return inputs elif isinstance(__A , __A ) and preprocessor.model_input_names[0] == "pixel_values": # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX lowerCAmelCase : List[str] = compute_effective_axis_dimension(__A , fixed_dimension=OnnxConfig.default_fixed_batch ) lowerCAmelCase : Tuple = self._generate_dummy_images(__A , __A , __A , __A ) lowerCAmelCase : Union[str, Any] = dict(preprocessor(images=__A , return_tensors=__A ) ) lowerCAmelCase : List[str] = inputs.pop("pixel_values" ) return inputs else: raise ValueError( "Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor." )
714
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : int = 1_0 , SCREAMING_SNAKE_CASE : int = 2_2 ): '''simple docstring''' lowerCAmelCase : Dict = range(1 , SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = range(1 , SCREAMING_SNAKE_CASE ) return sum( 1 for power in powers for base in bases if len(str(base**power ) ) == power ) if __name__ == "__main__": print(F"{solution(10, 22) = }")
681
0
"""simple docstring""" import argparse import os # New Code # import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## lowerCAmelCase__ = 16 lowerCAmelCase__ = 32 def a__ ( SCREAMING_SNAKE_CASE : Accelerator , SCREAMING_SNAKE_CASE : int = 1_6 ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = AutoTokenizer.from_pretrained("bert-base-cased" ) lowerCAmelCase : List[str] = load_dataset("glue" , "mrpc" ) def tokenize_function(SCREAMING_SNAKE_CASE : Optional[Any] ): # max_length=None => use the model max length (it's actually the default) lowerCAmelCase : int = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=UpperCamelCase__ , max_length=UpperCamelCase__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): lowerCAmelCase : Any = datasets.map( UpperCamelCase__ , batched=UpperCamelCase__ , remove_columns=["idx", "sentence1", "sentence2"] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library lowerCAmelCase : List[Any] = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(SCREAMING_SNAKE_CASE : str ): # On TPU it's best to pad everything to the same length or training will be very slow. lowerCAmelCase : Optional[Any] = 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": lowerCAmelCase : Union[str, Any] = 1_6 elif accelerator.mixed_precision != "no": lowerCAmelCase : Any = 8 else: lowerCAmelCase : Tuple = None return tokenizer.pad( UpperCamelCase__ , padding="longest" , max_length=UpperCamelCase__ , pad_to_multiple_of=UpperCamelCase__ , return_tensors="pt" , ) # Instantiate dataloaders. lowerCAmelCase : List[str] = DataLoader( tokenized_datasets["train"] , shuffle=UpperCamelCase__ , collate_fn=UpperCamelCase__ , batch_size=UpperCamelCase__ ) lowerCAmelCase : Tuple = DataLoader( tokenized_datasets["validation"] , shuffle=UpperCamelCase__ , collate_fn=UpperCamelCase__ , batch_size=UpperCamelCase__ ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('''TESTING_MOCKED_DATALOADERS''', None) == "1": from accelerate.test_utils.training import mocked_dataloaders lowerCAmelCase__ = mocked_dataloaders # noqa: F811 def a__ ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : str ): '''simple docstring''' if os.environ.get("TESTING_MOCKED_DATALOADERS" , UpperCamelCase__ ) == "1": lowerCAmelCase : Union[str, Any] = 2 # Initialize accelerator lowerCAmelCase : Any = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs lowerCAmelCase : str = config["lr"] lowerCAmelCase : Optional[Any] = int(config["num_epochs"] ) lowerCAmelCase : Union[str, Any] = int(config["seed"] ) lowerCAmelCase : Tuple = int(config["batch_size"] ) lowerCAmelCase : List[Any] = evaluate.load("glue" , "mrpc" ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=UpperCamelCase__ ) def inner_training_loop(SCREAMING_SNAKE_CASE : Any ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(UpperCamelCase__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) lowerCAmelCase : List[str] = AutoModelForSequenceClassification.from_pretrained("bert-base-cased" , return_dict=UpperCamelCase__ ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). lowerCAmelCase : List[Any] = model.to(accelerator.device ) # Instantiate optimizer lowerCAmelCase : Dict = AdamW(params=model.parameters() , lr=UpperCamelCase__ ) lowerCAmelCase , lowerCAmelCase : List[str] = get_dataloaders(UpperCamelCase__ , UpperCamelCase__ ) # Instantiate scheduler lowerCAmelCase : Any = get_linear_schedule_with_warmup( optimizer=UpperCamelCase__ , num_warmup_steps=1_0_0 , num_training_steps=(len(UpperCamelCase__ ) * num_epochs) , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Union[str, Any] = accelerator.prepare( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # Now we train the model for epoch in range(UpperCamelCase__ ): model.train() for step, batch in enumerate(UpperCamelCase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) lowerCAmelCase : Tuple = model(**UpperCamelCase__ ) lowerCAmelCase : Union[str, Any] = outputs.loss accelerator.backward(UpperCamelCase__ ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(UpperCamelCase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): lowerCAmelCase : Optional[Any] = model(**UpperCamelCase__ ) lowerCAmelCase : Dict = outputs.logits.argmax(dim=-1 ) lowerCAmelCase , lowerCAmelCase : Tuple = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=UpperCamelCase__ , references=UpperCamelCase__ , ) lowerCAmelCase : Tuple = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f"""epoch {epoch}:""" , UpperCamelCase__ ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def a__ ( ): '''simple docstring''' lowerCAmelCase : Optional[Any] = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=UpperCamelCase__ , default=UpperCamelCase__ , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) lowerCAmelCase : str = parser.parse_args() lowerCAmelCase : Tuple = {"lr": 2E-5, "num_epochs": 3, "seed": 4_2, "batch_size": 1_6} training_function(UpperCamelCase__ , UpperCamelCase__ ) if __name__ == "__main__": main()
715
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : Optional[int] = len(SCREAMING_SNAKE_CASE ) while cur > 1: # Find the maximum number in arr lowerCAmelCase : List[str] = arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi lowerCAmelCase : str = arr[mi::-1] + arr[mi + 1 : len(SCREAMING_SNAKE_CASE )] # Reverse whole list lowerCAmelCase : str = arr[cur - 1 :: -1] + arr[cur : len(SCREAMING_SNAKE_CASE )] cur -= 1 return arr if __name__ == "__main__": lowerCAmelCase__ = input('''Enter numbers separated by a comma:\n''').strip() lowerCAmelCase__ = [int(item) for item in user_input.split(''',''')] print(pancake_sort(unsorted))
681
0
"""simple docstring""" from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class SCREAMING_SNAKE_CASE__ ( __UpperCAmelCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = {"col_1": [3, 2, 1, 0], "col_2": ["a", "b", "c", "d"]} return Dataset.from_dict(__SCREAMING_SNAKE_CASE ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = self._create_example_records() lowerCAmelCase : Optional[Any] = Dataset.from_list(__SCREAMING_SNAKE_CASE ) self.assertListEqual(dset.column_names , ["col_1", "col_2"] ) for i, r in enumerate(__SCREAMING_SNAKE_CASE ): self.assertDictEqual(__SCREAMING_SNAKE_CASE , example_records[i] ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = self._create_example_records() lowerCAmelCase : Optional[int] = Dataset.from_list(__SCREAMING_SNAKE_CASE ) lowerCAmelCase : Any = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]} ) self.assertEqual(dset.info , dset_from_dict.info ) def lowercase__ ( self ): # checks what happens with missing columns """simple docstring""" lowerCAmelCase : Any = [{"col_1": 1}, {"col_2": "x"}] lowerCAmelCase : Optional[int] = Dataset.from_list(__SCREAMING_SNAKE_CASE ) self.assertDictEqual(dset[0] , {"col_1": 1} ) self.assertDictEqual(dset[1] , {"col_1": None} ) # NB: first record is used for columns def lowercase__ ( self ): # checks if the type can be inferred from the second record """simple docstring""" lowerCAmelCase : Tuple = [{"col_1": []}, {"col_1": [1, 2]}] lowerCAmelCase : List[Any] = Dataset.from_list(__SCREAMING_SNAKE_CASE ) self.assertEqual(dset.info.features["col_1"] , Sequence(Value("int64" ) ) ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = Dataset.from_list([] ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 0 ) self.assertListEqual(dset.column_names , [] )
716
"""simple docstring""" from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
681
0
from math import asin, atan, cos, radians, sin, sqrt, tan lowerCAmelCase__ = 6_378_137.0 lowerCAmelCase__ = 6_356_752.314_245 lowerCAmelCase__ = 6_378_137 def a__ ( SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float ): '''simple docstring''' lowerCAmelCase : List[Any] = (AXIS_A - AXIS_B) / AXIS_A lowerCAmelCase : Dict = atan((1 - flattening) * tan(radians(SCREAMING_SNAKE_CASE ) ) ) lowerCAmelCase : str = atan((1 - flattening) * tan(radians(SCREAMING_SNAKE_CASE ) ) ) lowerCAmelCase : List[Any] = radians(SCREAMING_SNAKE_CASE ) lowerCAmelCase : str = radians(SCREAMING_SNAKE_CASE ) # Equation lowerCAmelCase : str = sin((phi_a - phi_a) / 2 ) lowerCAmelCase : Union[str, Any] = sin((lambda_a - lambda_a) / 2 ) # Square both values sin_sq_phi *= sin_sq_phi sin_sq_lambda *= sin_sq_lambda lowerCAmelCase : str = sqrt(sin_sq_phi + (cos(SCREAMING_SNAKE_CASE ) * cos(SCREAMING_SNAKE_CASE ) * sin_sq_lambda) ) return 2 * RADIUS * asin(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod()
717
"""simple docstring""" import math def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' return math.sqrt(SCREAMING_SNAKE_CASE ) * math.sqrt(SCREAMING_SNAKE_CASE ) == num def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' lowerCAmelCase : Dict = 0 lowerCAmelCase : List[str] = n while left <= right: lowerCAmelCase : str = (left + right) // 2 if mid**2 == n: return True elif mid**2 > n: lowerCAmelCase : int = mid - 1 else: lowerCAmelCase : int = mid + 1 return False if __name__ == "__main__": import doctest doctest.testmod()
681
0
"""simple docstring""" import json import os import unittest from transformers.models.blenderbot_small.tokenization_blenderbot_small import ( VOCAB_FILES_NAMES, BlenderbotSmallTokenizer, ) from ...test_tokenization_common import TokenizerTesterMixin class SCREAMING_SNAKE_CASE__ ( lowercase , unittest.TestCase ): """simple docstring""" a : List[str] =BlenderbotSmallTokenizer a : str =False def lowercase__ ( self ): """simple docstring""" super().setUp() lowerCAmelCase : Optional[int] = ["__start__", "adapt", "act", "ap@@", "te", "__end__", "__unk__"] lowerCAmelCase : Optional[int] = dict(zip(__A , range(len(__A ) ) ) ) lowerCAmelCase : Optional[int] = ["#version: 0.2", "a p", "t e</w>", "ap t</w>", "a d", "ad apt</w>", "a c", "ac t</w>", ""] lowerCAmelCase : int = {"unk_token": "__unk__", "bos_token": "__start__", "eos_token": "__end__"} lowerCAmelCase : List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) lowerCAmelCase : List[Any] = 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(__A ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(__A ) ) def lowercase__ ( self , **snake_case__ ): """simple docstring""" kwargs.update(self.special_tokens_map ) return BlenderbotSmallTokenizer.from_pretrained(self.tmpdirname , **__A ) def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Any = "adapt act apte" lowerCAmelCase : Any = "adapt act apte" return input_text, output_text def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = BlenderbotSmallTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) lowerCAmelCase : Dict = "adapt act apte" lowerCAmelCase : Tuple = ["adapt", "act", "ap@@", "te"] lowerCAmelCase : Tuple = tokenizer.tokenize(__A ) self.assertListEqual(__A , __A ) lowerCAmelCase : Tuple = [tokenizer.bos_token] + tokens + [tokenizer.eos_token] lowerCAmelCase : int = [0, 1, 2, 3, 4, 5] self.assertListEqual(tokenizer.convert_tokens_to_ids(__A ) , __A ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = BlenderbotSmallTokenizer.from_pretrained("facebook/blenderbot-90M" ) assert tok("sam" ).input_ids == [1_384] lowerCAmelCase : str = "I am a small frog." lowerCAmelCase : Dict = tok([src_text] , padding=__A , truncation=__A )["input_ids"] lowerCAmelCase : Optional[int] = tok.batch_decode(__A , skip_special_tokens=__A , clean_up_tokenization_spaces=__A )[0] assert src_text != decoded # I wish it did! assert decoded == "i am a small frog ." def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = BlenderbotSmallTokenizer.from_pretrained("facebook/blenderbot-90M" ) lowerCAmelCase : Dict = "I am a small frog ." lowerCAmelCase : Any = "." lowerCAmelCase : Optional[Any] = tok(__A )["input_ids"] lowerCAmelCase : Optional[Any] = tok(__A )["input_ids"] assert encoded[-1] == encoded_dot[0]
718
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''google/vit-base-patch16-224''': '''https://huggingface.co/vit-base-patch16-224/resolve/main/config.json''', # See all ViT models at https://huggingface.co/models?filter=vit } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Union[str, Any] ="vit" def __init__( self , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3_072 , snake_case__="gelu" , snake_case__=0.0 , snake_case__=0.0 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__=224 , snake_case__=16 , snake_case__=3 , snake_case__=True , snake_case__=16 , **snake_case__ , ): """simple docstring""" super().__init__(**snake_case__ ) lowerCAmelCase : Optional[Any] = hidden_size lowerCAmelCase : List[Any] = num_hidden_layers lowerCAmelCase : List[Any] = num_attention_heads lowerCAmelCase : Union[str, Any] = intermediate_size lowerCAmelCase : Any = hidden_act lowerCAmelCase : Optional[Any] = hidden_dropout_prob lowerCAmelCase : Optional[Any] = attention_probs_dropout_prob lowerCAmelCase : List[str] = initializer_range lowerCAmelCase : Optional[Any] = layer_norm_eps lowerCAmelCase : Optional[int] = image_size lowerCAmelCase : Optional[Any] = patch_size lowerCAmelCase : Tuple = num_channels lowerCAmelCase : Optional[int] = qkv_bias lowerCAmelCase : str = encoder_stride class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : List[Any] =version.parse("1.11" ) @property def lowercase__ ( self ): """simple docstring""" return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowercase__ ( self ): """simple docstring""" return 1e-4
681
0
"""simple docstring""" import json import os import unittest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class SCREAMING_SNAKE_CASE__ ( __snake_case , unittest.TestCase ): """simple docstring""" a : str =MgpstrTokenizer a : int =False a : Union[str, Any] ={} a : Union[str, Any] =False def lowercase__ ( self ): """simple docstring""" super().setUp() # fmt: off lowerCAmelCase : int = ["""[GO]""", """[s]""", """0""", """1""", """2""", """3""", """4""", """5""", """6""", """7""", """8""", """9""", """a""", """b""", """c""", """d""", """e""", """f""", """g""", """h""", """i""", """j""", """k""", """l""", """m""", """n""", """o""", """p""", """q""", """r""", """s""", """t""", """u""", """v""", """w""", """x""", """y""", """z"""] # fmt: on lowerCAmelCase : List[Any] = dict(zip(_lowercase , range(len(_lowercase ) ) ) ) lowerCAmelCase : Optional[Any] = 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(_lowercase ) + "\n" ) def lowercase__ ( self , **snake_case__ ): """simple docstring""" return MgpstrTokenizer.from_pretrained(self.tmpdirname , **_lowercase ) def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : str = """tester""" lowerCAmelCase : int = """tester""" return input_text, output_text @unittest.skip("MGP-STR always lower cases letters." ) def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = self.get_tokenizers(do_lower_case=_lowercase ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): lowerCAmelCase : Any = """[SPECIAL_TOKEN]""" tokenizer.add_special_tokens({"cls_token": special_token} ) lowerCAmelCase : str = tokenizer.encode([special_token] , add_special_tokens=_lowercase ) self.assertEqual(len(_lowercase ) , 1 ) lowerCAmelCase : List[Any] = tokenizer.decode(_lowercase , skip_special_tokens=_lowercase ) self.assertTrue(special_token not in decoded ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): lowerCAmelCase : str = self.get_input_output_texts(_lowercase ) lowerCAmelCase : str = tokenizer.tokenize(_lowercase ) lowerCAmelCase : Dict = tokenizer.convert_tokens_to_ids(_lowercase ) lowerCAmelCase : List[Any] = tokenizer.encode(_lowercase , add_special_tokens=_lowercase ) self.assertListEqual(_lowercase , _lowercase ) lowerCAmelCase : Optional[int] = tokenizer.convert_ids_to_tokens(_lowercase ) self.assertNotEqual(len(_lowercase ) , 0 ) lowerCAmelCase : str = tokenizer.decode(_lowercase ) self.assertIsInstance(_lowercase , _lowercase ) self.assertEqual(text_a.replace(" " , "" ) , _lowercase ) @unittest.skip("MGP-STR tokenizer only handles one sequence." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("inputs cannot be pretokenized in MgpstrTokenizer" ) def lowercase__ ( self ): """simple docstring""" pass
719
"""simple docstring""" from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels
681
0
"""simple docstring""" from itertools import permutations def a__ ( SCREAMING_SNAKE_CASE : tuple ): '''simple docstring''' if num[3] % 2 != 0: return False if (num[2] + num[3] + num[4]) % 3 != 0: return False if num[5] % 5 != 0: return False lowerCAmelCase : Tuple = [7, 1_1, 1_3, 1_7] for i, test in enumerate(lowercase_ ): if (num[i + 4] * 1_0_0 + num[i + 5] * 1_0 + num[i + 6]) % test != 0: return False return True def a__ ( SCREAMING_SNAKE_CASE : int = 1_0 ): '''simple docstring''' return sum( int("".join(map(lowercase_ , lowercase_ ) ) ) for num in permutations(range(lowercase_ ) ) if is_substring_divisible(lowercase_ ) ) if __name__ == "__main__": print(F"{solution() = }")
720
"""simple docstring""" import copy import inspect import unittest from transformers import AutoBackbone from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import require_timm, require_torch, torch_device from transformers.utils.import_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 if is_torch_available(): import torch from transformers import TimmBackbone, TimmBackboneConfig from ...test_pipeline_mixin import PipelineTesterMixin class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__="resnet50" , snake_case__=3 , snake_case__=32 , snake_case__=3 , snake_case__=True , snake_case__=True , ): """simple docstring""" lowerCAmelCase : List[str] = parent lowerCAmelCase : Union[str, Any] = out_indices if out_indices is not None else [4] lowerCAmelCase : Tuple = stage_names lowerCAmelCase : Any = out_features lowerCAmelCase : Any = backbone lowerCAmelCase : Union[str, Any] = batch_size lowerCAmelCase : Optional[int] = image_size lowerCAmelCase : List[str] = num_channels lowerCAmelCase : int = use_pretrained_backbone lowerCAmelCase : Tuple = is_training def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase : Optional[int] = self.get_config() return config, pixel_values def lowercase__ ( self ): """simple docstring""" return TimmBackboneConfig( image_size=self.image_size , num_channels=self.num_channels , out_features=self.out_features , out_indices=self.out_indices , stage_names=self.stage_names , use_pretrained_backbone=self.use_pretrained_backbone , backbone=self.backbone , ) def lowercase__ ( self , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : List[Any] = TimmBackbone(config=snake_case__ ) model.to(snake_case__ ) model.eval() with torch.no_grad(): lowerCAmelCase : Union[str, Any] = model(snake_case__ ) self.parent.assertEqual( result.feature_map[-1].shape , (self.batch_size, model.channels[-1], 14, 14) , ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = self.prepare_config_and_inputs() lowerCAmelCase , lowerCAmelCase : Tuple = config_and_inputs lowerCAmelCase : str = {"pixel_values": pixel_values} return config, inputs_dict @require_torch @require_timm class SCREAMING_SNAKE_CASE__ ( lowercase , lowercase , lowercase , unittest.TestCase ): """simple docstring""" a : Optional[int] =(TimmBackbone,) if is_torch_available() else () a : Union[str, Any] ={"feature-extraction": TimmBackbone} if is_torch_available() else {} a : Tuple =False a : List[Any] =False a : Optional[Any] =False a : Dict =False def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = TimmBackboneModelTester(self ) lowerCAmelCase : List[str] = ConfigTester(self , config_class=snake_case__ , has_text_modality=snake_case__ ) def lowercase__ ( self ): """simple docstring""" 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 lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = "resnet18" lowerCAmelCase : str = "microsoft/resnet-18" lowerCAmelCase : List[Any] = AutoBackbone.from_pretrained(snake_case__ , use_timm_backbone=snake_case__ ) lowerCAmelCase : List[str] = AutoBackbone.from_pretrained(snake_case__ ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(len(timm_model.stage_names ) , len(transformers_model.stage_names ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) # Out indices are set to the last layer by default. For timm models, we don't know # the number of layers in advance, so we set it to (-1,), whereas for transformers # models, we set it to [len(stage_names) - 1] (kept for backward compatibility). self.assertEqual(timm_model.out_indices , (-1,) ) self.assertEqual(transformers_model.out_indices , [len(timm_model.stage_names ) - 1] ) lowerCAmelCase : Union[str, Any] = AutoBackbone.from_pretrained(snake_case__ , use_timm_backbone=snake_case__ , out_indices=[1, 2, 3] ) lowerCAmelCase : List[Any] = AutoBackbone.from_pretrained(snake_case__ , out_indices=[1, 2, 3] ) self.assertEqual(timm_model.out_indices , transformers_model.out_indices ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) @unittest.skip("TimmBackbone doesn't support feed forward chunking" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone doesn't have num_hidden_layers attribute" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone initialization is managed on the timm side" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone models doesn't have inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone models doesn't have inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone model cannot be created without specifying a backbone checkpoint" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("model weights aren't tied in TimmBackbone." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("model weights aren't tied in TimmBackbone." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone doesn't have hidden size info in its configuration." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone doesn't support output_attentions." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Safetensors is not supported by timm." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : Dict = model_class(snake_case__ ) lowerCAmelCase : Any = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase : Optional[int] = [*signature.parameters.keys()] lowerCAmelCase : Any = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase : int = True lowerCAmelCase : str = self.has_attentions # no need to test all models as different heads yield the same functionality lowerCAmelCase : Optional[int] = self.all_model_classes[0] lowerCAmelCase : Union[str, Any] = model_class(snake_case__ ) model.to(snake_case__ ) lowerCAmelCase : Optional[int] = self._prepare_for_class(snake_case__ , snake_case__ ) lowerCAmelCase : Dict = model(**snake_case__ ) lowerCAmelCase : Tuple = outputs[0][-1] # Encoder-/Decoder-only models lowerCAmelCase : Optional[int] = outputs.hidden_states[0] hidden_states.retain_grad() if self.has_attentions: lowerCAmelCase : Union[str, Any] = outputs.attentions[0] attentions.retain_grad() output.flatten()[0].backward(retain_graph=snake_case__ ) self.assertIsNotNone(hidden_states.grad ) if self.has_attentions: self.assertIsNotNone(attentions.grad ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : Dict = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : List[str] = model(**snake_case__ ) self.assertEqual(len(result.feature_maps ) , len(config.out_indices ) ) self.assertEqual(len(model.channels ) , len(config.out_indices ) ) # Check output of last stage is taken if out_features=None, out_indices=None lowerCAmelCase : Dict = copy.deepcopy(snake_case__ ) lowerCAmelCase : Optional[int] = None lowerCAmelCase : Dict = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : Optional[int] = model(**snake_case__ ) self.assertEqual(len(result.feature_maps ) , 1 ) self.assertEqual(len(model.channels ) , 1 ) # Check backbone can be initialized with fresh weights lowerCAmelCase : Optional[int] = copy.deepcopy(snake_case__ ) lowerCAmelCase : List[str] = False lowerCAmelCase : int = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : Optional[Any] = model(**snake_case__ )
681
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase__ = { '''configuration_convbert''': ['''CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ConvBertConfig''', '''ConvBertOnnxConfig'''], '''tokenization_convbert''': ['''ConvBertTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''ConvBertTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ConvBertForMaskedLM''', '''ConvBertForMultipleChoice''', '''ConvBertForQuestionAnswering''', '''ConvBertForSequenceClassification''', '''ConvBertForTokenClassification''', '''ConvBertLayer''', '''ConvBertModel''', '''ConvBertPreTrainedModel''', '''load_tf_weights_in_convbert''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFConvBertForMaskedLM''', '''TFConvBertForMultipleChoice''', '''TFConvBertForQuestionAnswering''', '''TFConvBertForSequenceClassification''', '''TFConvBertForTokenClassification''', '''TFConvBertLayer''', '''TFConvBertModel''', '''TFConvBertPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_convbert import CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvBertConfig, ConvBertOnnxConfig from .tokenization_convbert import ConvBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_convbert_fast import ConvBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convbert import ( CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvBertForMaskedLM, ConvBertForMultipleChoice, ConvBertForQuestionAnswering, ConvBertForSequenceClassification, ConvBertForTokenClassification, ConvBertLayer, ConvBertModel, ConvBertPreTrainedModel, load_tf_weights_in_convbert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convbert import ( TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertLayer, TFConvBertModel, TFConvBertPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
721
"""simple docstring""" import argparse import os import re lowerCAmelCase__ = '''src/transformers''' # Pattern that looks at the indentation in a line. lowerCAmelCase__ = re.compile(r'''^(\s*)\S''') # Pattern that matches `"key":" and puts `key` in group 0. lowerCAmelCase__ = re.compile(r'''^\s*"([^"]+)":''') # Pattern that matches `_import_structure["key"]` and puts `key` in group 0. lowerCAmelCase__ = re.compile(r'''^\s*_import_structure\["([^"]+)"\]''') # Pattern that matches `"key",` and puts `key` in group 0. lowerCAmelCase__ = re.compile(r'''^\s*"([^"]+)",\s*$''') # Pattern that matches any `[stuff]` and puts `stuff` in group 0. lowerCAmelCase__ = re.compile(r'''\[([^\]]+)\]''') def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = _re_indent.search(SCREAMING_SNAKE_CASE ) return "" if search is None else search.groups()[0] def a__ ( SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Optional[int]="" , SCREAMING_SNAKE_CASE : Optional[int]=None , SCREAMING_SNAKE_CASE : Dict=None ): '''simple docstring''' lowerCAmelCase : Tuple = 0 lowerCAmelCase : Optional[int] = code.split("\n" ) if start_prompt is not None: while not lines[index].startswith(SCREAMING_SNAKE_CASE ): index += 1 lowerCAmelCase : Dict = ["\n".join(lines[:index] )] else: lowerCAmelCase : Any = [] # We split into blocks until we get to the `end_prompt` (or the end of the block). lowerCAmelCase : Union[str, Any] = [lines[index]] index += 1 while index < len(SCREAMING_SNAKE_CASE ) and (end_prompt is None or not lines[index].startswith(SCREAMING_SNAKE_CASE )): if len(lines[index] ) > 0 and get_indent(lines[index] ) == indent_level: if len(SCREAMING_SNAKE_CASE ) > 0 and get_indent(current_block[-1] ).startswith(indent_level + " " ): current_block.append(lines[index] ) blocks.append("\n".join(SCREAMING_SNAKE_CASE ) ) if index < len(SCREAMING_SNAKE_CASE ) - 1: lowerCAmelCase : List[str] = [lines[index + 1]] index += 1 else: lowerCAmelCase : Optional[Any] = [] else: blocks.append("\n".join(SCREAMING_SNAKE_CASE ) ) lowerCAmelCase : str = [lines[index]] else: current_block.append(lines[index] ) index += 1 # Adds current block if it's nonempty. if len(SCREAMING_SNAKE_CASE ) > 0: blocks.append("\n".join(SCREAMING_SNAKE_CASE ) ) # Add final block after end_prompt if provided. if end_prompt is not None and index < len(SCREAMING_SNAKE_CASE ): blocks.append("\n".join(lines[index:] ) ) return blocks def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' def _inner(SCREAMING_SNAKE_CASE : Optional[Any] ): return key(SCREAMING_SNAKE_CASE ).lower().replace("_" , "" ) return _inner def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Dict=None ): '''simple docstring''' def noop(SCREAMING_SNAKE_CASE : List[Any] ): return x if key is None: lowerCAmelCase : int = noop # Constants are all uppercase, they go first. lowerCAmelCase : Dict = [obj for obj in objects if key(SCREAMING_SNAKE_CASE ).isupper()] # Classes are not all uppercase but start with a capital, they go second. lowerCAmelCase : List[Any] = [obj for obj in objects if key(SCREAMING_SNAKE_CASE )[0].isupper() and not key(SCREAMING_SNAKE_CASE ).isupper()] # Functions begin with a lowercase, they go last. lowerCAmelCase : List[Any] = [obj for obj in objects if not key(SCREAMING_SNAKE_CASE )[0].isupper()] lowerCAmelCase : Dict = ignore_underscore(SCREAMING_SNAKE_CASE ) return sorted(SCREAMING_SNAKE_CASE , key=SCREAMING_SNAKE_CASE ) + sorted(SCREAMING_SNAKE_CASE , key=SCREAMING_SNAKE_CASE ) + sorted(SCREAMING_SNAKE_CASE , key=SCREAMING_SNAKE_CASE ) def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' def _replace(SCREAMING_SNAKE_CASE : List[Any] ): lowerCAmelCase : List[str] = match.groups()[0] if "," not in imports: return f"""[{imports}]""" lowerCAmelCase : Dict = [part.strip().replace("\"" , "" ) for part in imports.split("," )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: lowerCAmelCase : Any = keys[:-1] return "[" + ", ".join([f"""\"{k}\"""" for k in sort_objects(SCREAMING_SNAKE_CASE )] ) + "]" lowerCAmelCase : List[Any] = import_statement.split("\n" ) if len(SCREAMING_SNAKE_CASE ) > 3: # Here we have to sort internal imports that are on several lines (one per name): # key: [ # "object1", # "object2", # ... # ] # We may have to ignore one or two lines on each side. lowerCAmelCase : Tuple = 2 if lines[1].strip() == "[" else 1 lowerCAmelCase : Optional[Any] = [(i, _re_strip_line.search(SCREAMING_SNAKE_CASE ).groups()[0]) for i, line in enumerate(lines[idx:-idx] )] lowerCAmelCase : Optional[Any] = sort_objects(SCREAMING_SNAKE_CASE , key=lambda SCREAMING_SNAKE_CASE : x[1] ) lowerCAmelCase : List[str] = [lines[x[0] + idx] for x in sorted_indices] return "\n".join(lines[:idx] + sorted_lines + lines[-idx:] ) elif len(SCREAMING_SNAKE_CASE ) == 3: # Here we have to sort internal imports that are on one separate line: # key: [ # "object1", "object2", ... # ] if _re_bracket_content.search(lines[1] ) is not None: lowerCAmelCase : Optional[int] = _re_bracket_content.sub(_replace , lines[1] ) else: lowerCAmelCase : List[str] = [part.strip().replace("\"" , "" ) for part in lines[1].split("," )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: lowerCAmelCase : Union[str, Any] = keys[:-1] lowerCAmelCase : str = get_indent(lines[1] ) + ", ".join([f"""\"{k}\"""" for k in sort_objects(SCREAMING_SNAKE_CASE )] ) return "\n".join(SCREAMING_SNAKE_CASE ) else: # Finally we have to deal with imports fitting on one line lowerCAmelCase : Any = _re_bracket_content.sub(_replace , SCREAMING_SNAKE_CASE ) return import_statement def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Tuple=True ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE , encoding="utf-8" ) as f: lowerCAmelCase : Union[str, Any] = f.read() if "_import_structure" not in code: return # Blocks of indent level 0 lowerCAmelCase : List[str] = split_code_in_indented_blocks( SCREAMING_SNAKE_CASE , start_prompt="_import_structure = {" , end_prompt="if TYPE_CHECKING:" ) # We ignore block 0 (everything untils start_prompt) and the last block (everything after end_prompt). for block_idx in range(1 , len(SCREAMING_SNAKE_CASE ) - 1 ): # Check if the block contains some `_import_structure`s thingy to sort. lowerCAmelCase : Tuple = main_blocks[block_idx] lowerCAmelCase : Optional[Any] = block.split("\n" ) # Get to the start of the imports. lowerCAmelCase : int = 0 while line_idx < len(SCREAMING_SNAKE_CASE ) and "_import_structure" not in block_lines[line_idx]: # Skip dummy import blocks if "import dummy" in block_lines[line_idx]: lowerCAmelCase : Optional[Any] = len(SCREAMING_SNAKE_CASE ) else: line_idx += 1 if line_idx >= len(SCREAMING_SNAKE_CASE ): continue # Ignore beginning and last line: they don't contain anything. lowerCAmelCase : Optional[Any] = "\n".join(block_lines[line_idx:-1] ) lowerCAmelCase : Dict = get_indent(block_lines[1] ) # Slit the internal block into blocks of indent level 1. lowerCAmelCase : Optional[Any] = split_code_in_indented_blocks(SCREAMING_SNAKE_CASE , indent_level=SCREAMING_SNAKE_CASE ) # We have two categories of import key: list or _import_structure[key].append/extend lowerCAmelCase : Tuple = _re_direct_key if "_import_structure = {" in block_lines[0] else _re_indirect_key # Grab the keys, but there is a trap: some lines are empty or just comments. lowerCAmelCase : Tuple = [(pattern.search(SCREAMING_SNAKE_CASE ).groups()[0] if pattern.search(SCREAMING_SNAKE_CASE ) is not None else None) for b in internal_blocks] # We only sort the lines with a key. lowerCAmelCase : int = [(i, key) for i, key in enumerate(SCREAMING_SNAKE_CASE ) if key is not None] lowerCAmelCase : Union[str, Any] = [x[0] for x in sorted(SCREAMING_SNAKE_CASE , key=lambda SCREAMING_SNAKE_CASE : x[1] )] # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. lowerCAmelCase : Tuple = 0 lowerCAmelCase : Any = [] for i in range(len(SCREAMING_SNAKE_CASE ) ): if keys[i] is None: reorderded_blocks.append(internal_blocks[i] ) else: lowerCAmelCase : Dict = sort_objects_in_import(internal_blocks[sorted_indices[count]] ) reorderded_blocks.append(SCREAMING_SNAKE_CASE ) count += 1 # And we put our main block back together with its first and last line. lowerCAmelCase : List[Any] = "\n".join(block_lines[:line_idx] + reorderded_blocks + [block_lines[-1]] ) if code != "\n".join(SCREAMING_SNAKE_CASE ): if check_only: return True else: print(f"""Overwriting {file}.""" ) with open(SCREAMING_SNAKE_CASE , "w" , encoding="utf-8" ) as f: f.write("\n".join(SCREAMING_SNAKE_CASE ) ) def a__ ( SCREAMING_SNAKE_CASE : List[str]=True ): '''simple docstring''' lowerCAmelCase : Optional[int] = [] for root, _, files in os.walk(SCREAMING_SNAKE_CASE ): if "__init__.py" in files: lowerCAmelCase : Tuple = sort_imports(os.path.join(SCREAMING_SNAKE_CASE , "__init__.py" ) , check_only=SCREAMING_SNAKE_CASE ) if result: lowerCAmelCase : Optional[Any] = [os.path.join(SCREAMING_SNAKE_CASE , "__init__.py" )] if len(SCREAMING_SNAKE_CASE ) > 0: raise ValueError(f"""Would overwrite {len(SCREAMING_SNAKE_CASE )} files, run `make style`.""" ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument('''--check_only''', action='''store_true''', help='''Whether to only check or fix style.''') lowerCAmelCase__ = parser.parse_args() sort_imports_in_all_inits(check_only=args.check_only)
681
0
"""simple docstring""" import os from distutils.util import strtobool def a__ ( SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : Any ): '''simple docstring''' for e in env_keys: lowerCAmelCase : Tuple = int(os.environ.get(_lowerCamelCase , -1 ) ) if val >= 0: return val return default def a__ ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : Optional[Any]=False ): '''simple docstring''' lowerCAmelCase : Optional[int] = os.environ.get(_lowerCamelCase , str(_lowerCamelCase ) ) return strtobool(_lowerCamelCase ) == 1 # As its name indicates `strtobool` actually returns an int... def a__ ( SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Tuple="no" ): '''simple docstring''' lowerCAmelCase : List[Any] = os.environ.get(_lowerCamelCase , str(_lowerCamelCase ) ) return value
700
"""simple docstring""" from __future__ import annotations import inspect import unittest import numpy as np from transformers import ResNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFResNetForImageClassification, TFResNetModel from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__=3 , snake_case__=32 , snake_case__=3 , snake_case__=10 , snake_case__=[10, 20, 30, 40] , snake_case__=[1, 1, 2, 1] , snake_case__=True , snake_case__=True , snake_case__="relu" , snake_case__=3 , snake_case__=None , ): """simple docstring""" lowerCAmelCase : Optional[Any] = parent lowerCAmelCase : List[Any] = batch_size lowerCAmelCase : Union[str, Any] = image_size lowerCAmelCase : Dict = num_channels lowerCAmelCase : List[Any] = embeddings_size lowerCAmelCase : List[Any] = hidden_sizes lowerCAmelCase : Optional[int] = depths lowerCAmelCase : str = is_training lowerCAmelCase : List[str] = use_labels lowerCAmelCase : List[Any] = hidden_act lowerCAmelCase : Optional[Any] = num_labels lowerCAmelCase : Tuple = scope lowerCAmelCase : int = len(snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase : Optional[Any] = None if self.use_labels: lowerCAmelCase : Optional[int] = ids_tensor([self.batch_size] , self.num_labels ) lowerCAmelCase : List[str] = self.get_config() return config, pixel_values, labels def lowercase__ ( self ): """simple docstring""" return ResNetConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , image_size=self.image_size , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Tuple = TFResNetModel(config=snake_case__ ) lowerCAmelCase : Union[str, Any] = model(snake_case__ ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = self.num_labels lowerCAmelCase : str = TFResNetForImageClassification(snake_case__ ) lowerCAmelCase : int = model(snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = self.prepare_config_and_inputs() lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Any = config_and_inputs lowerCAmelCase : Optional[Any] = {"pixel_values": pixel_values} return config, inputs_dict @require_tf class SCREAMING_SNAKE_CASE__ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" a : Any =(TFResNetModel, TFResNetForImageClassification) if is_tf_available() else () a : Tuple =( {"feature-extraction": TFResNetModel, "image-classification": TFResNetForImageClassification} if is_tf_available() else {} ) a : int =False a : List[str] =False a : Optional[int] =False a : Union[str, Any] =False a : Any =False def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = TFResNetModelTester(self ) lowerCAmelCase : str = ConfigTester(self , config_class=snake_case__ , has_text_modality=snake_case__ ) def lowercase__ ( self ): """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 lowercase__ ( self ): """simple docstring""" return @unittest.skip(reason="ResNet does not use inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip(reason="ResNet does not support input and output embeddings" ) def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : List[str] = model_class(snake_case__ ) lowerCAmelCase : Optional[Any] = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase : Dict = [*signature.parameters.keys()] lowerCAmelCase : List[Any] = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def lowercase__ ( self ): """simple docstring""" def check_hidden_states_output(snake_case__ , snake_case__ , snake_case__ ): lowerCAmelCase : int = model_class(snake_case__ ) lowerCAmelCase : Optional[Any] = model(**self._prepare_for_class(snake_case__ , snake_case__ ) ) lowerCAmelCase : List[str] = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states lowerCAmelCase : Tuple = self.model_tester.num_stages self.assertEqual(len(snake_case__ ) , expected_num_stages + 1 ) # ResNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) lowerCAmelCase , lowerCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase : Any = ["basic", "bottleneck"] for model_class in self.all_model_classes: for layer_type in layers_type: lowerCAmelCase : Optional[Any] = layer_type lowerCAmelCase : Dict = True check_hidden_states_output(snake_case__ , snake_case__ , snake_case__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowerCAmelCase : List[Any] = True check_hidden_states_output(snake_case__ , snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case__ ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase : int = TFResNetModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def a__ ( ): '''simple docstring''' lowerCAmelCase : str = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_tf @require_vision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" @cached_property def lowercase__ ( self ): """simple docstring""" return ( AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) lowerCAmelCase : Any = self.default_image_processor lowerCAmelCase : Optional[Any] = prepare_img() lowerCAmelCase : Dict = image_processor(images=snake_case__ , return_tensors="tf" ) # forward pass lowerCAmelCase : str = model(**snake_case__ ) # verify the logits lowerCAmelCase : str = tf.TensorShape((1, 1_000) ) self.assertEqual(outputs.logits.shape , snake_case__ ) lowerCAmelCase : str = tf.constant([-11.1069, -9.7877, -8.3777] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , snake_case__ , atol=1e-4 ) )
681
0
"""simple docstring""" import re def a__ ( SCREAMING_SNAKE_CASE : Union[str, Any] ): '''simple docstring''' if len(re.findall("[ATCG]" , SCREAMING_SNAKE_CASE ) ) != len(SCREAMING_SNAKE_CASE ): raise ValueError("Invalid Strand" ) return dna.translate(dna.maketrans("ATCG" , "TAGC" ) ) if __name__ == "__main__": import doctest doctest.testmod()
701
"""simple docstring""" import random from .binary_exp_mod import bin_exp_mod def a__ ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : int=1_0_0_0 ): '''simple docstring''' if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd lowerCAmelCase : int = n - 1 lowerCAmelCase : Optional[int] = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) lowerCAmelCase : Optional[Any] = 0 while count < prec: lowerCAmelCase : List[str] = random.randint(2 , n - 1 ) lowerCAmelCase : Tuple = bin_exp_mod(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if b != 1: lowerCAmelCase : List[str] = True for _ in range(SCREAMING_SNAKE_CASE ): if b == n - 1: lowerCAmelCase : List[str] = False break lowerCAmelCase : Optional[Any] = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": lowerCAmelCase__ = abs(int(input('''Enter bound : ''').strip())) print('''Here\'s the list of primes:''') print(''', '''.join(str(i) for i in range(n + 1) if is_prime_big(i)))
681
0
"""simple docstring""" import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def a__ ( SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : Tuple = tmp_path / "file.csv" lowerCAmelCase : str = textwrap.dedent( "\\n header1,header2\n 1,2\n 10,20\n " ) with open(SCREAMING_SNAKE_CASE_ , "w" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) return str(SCREAMING_SNAKE_CASE_ ) @pytest.fixture def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : List[Any] = tmp_path / "malformed_file.csv" lowerCAmelCase : Any = textwrap.dedent( "\\n header1,header2\n 1,2\n 10,20,\n " ) with open(SCREAMING_SNAKE_CASE_ , "w" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) return str(SCREAMING_SNAKE_CASE_ ) @pytest.fixture def a__ ( SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : int ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = tmp_path / "csv_with_image.csv" lowerCAmelCase : Any = textwrap.dedent( f"""\\n image\n {image_file}\n """ ) with open(SCREAMING_SNAKE_CASE_ , "w" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) return str(SCREAMING_SNAKE_CASE_ ) @pytest.fixture def a__ ( SCREAMING_SNAKE_CASE : Union[str, Any] ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = tmp_path / "csv_with_label.csv" lowerCAmelCase : Optional[int] = textwrap.dedent( "\\n label\n good\n bad\n good\n " ) with open(SCREAMING_SNAKE_CASE_ , "w" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) return str(SCREAMING_SNAKE_CASE_ ) @pytest.fixture def a__ ( SCREAMING_SNAKE_CASE : Tuple ): '''simple docstring''' lowerCAmelCase : Optional[Any] = tmp_path / "csv_with_int_list.csv" lowerCAmelCase : Optional[Any] = textwrap.dedent( "\\n int_list\n 1 2 3\n 4 5 6\n 7 8 9\n " ) with open(SCREAMING_SNAKE_CASE_ , "w" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) return str(SCREAMING_SNAKE_CASE_ ) def a__ ( SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : List[Any] ): '''simple docstring''' lowerCAmelCase : List[Any] = Csv() lowerCAmelCase : int = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(SCREAMING_SNAKE_CASE_ , match="Error tokenizing data" ): for _ in generator: pass assert any( record.levelname == "ERROR" and "Failed to read file" in record.message and os.path.basename(SCREAMING_SNAKE_CASE_ ) in record.message for record in caplog.records ) @require_pil def a__ ( SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE_ , encoding="utf-8" ) as f: lowerCAmelCase : Dict = f.read().splitlines()[1] lowerCAmelCase : List[Any] = Csv(encoding="utf-8" , features=Features({"image": Image()} ) ) lowerCAmelCase : List[Any] = csv._generate_tables([[csv_file_with_image]] ) lowerCAmelCase : Dict = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field("image" ).type == Image()() lowerCAmelCase : Tuple = pa_table.to_pydict()["image"] assert generated_content == [{"path": image_file, "bytes": None}] def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE_ , encoding="utf-8" ) as f: lowerCAmelCase : List[str] = f.read().splitlines()[1:] lowerCAmelCase : Optional[int] = Csv(encoding="utf-8" , features=Features({"label": ClassLabel(names=["good", "bad"] )} ) ) lowerCAmelCase : Optional[int] = csv._generate_tables([[csv_file_with_label]] ) lowerCAmelCase : Optional[int] = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field("label" ).type == ClassLabel(names=["good", "bad"] )() lowerCAmelCase : Optional[int] = pa_table.to_pydict()["label"] assert generated_content == [ClassLabel(names=["good", "bad"] ).straint(SCREAMING_SNAKE_CASE_ ) for label in labels] def a__ ( SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : str = Csv(encoding="utf-8" , sep="," , converters={"int_list": lambda SCREAMING_SNAKE_CASE : [int(SCREAMING_SNAKE_CASE_ ) for i in x.split()]} ) lowerCAmelCase : Dict = csv._generate_tables([[csv_file_with_int_list]] ) lowerCAmelCase : int = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field("int_list" ).type ) lowerCAmelCase : List[str] = pa_table.to_pydict()["int_list"] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
702
"""simple docstring""" # DISCLAIMER: This file is strongly influenced by https://github.com/ermongroup/ddim from dataclasses import dataclass from typing import Optional, Tuple, Union import flax import jax import jax.numpy as jnp from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils_flax import ( CommonSchedulerState, FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, add_noise_common, get_velocity_common, ) @flax.struct.dataclass class SCREAMING_SNAKE_CASE__ : """simple docstring""" a : CommonSchedulerState # setable values a : jnp.ndarray a : jnp.ndarray a : Optional[int] =None @classmethod def lowercase__ ( cls , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" return cls(common=snake_case__ , init_noise_sigma=snake_case__ , timesteps=snake_case__ ) @dataclass class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : DDPMSchedulerState class SCREAMING_SNAKE_CASE__ ( lowercase , lowercase ): """simple docstring""" a : Union[str, Any] =[e.name for e in FlaxKarrasDiffusionSchedulers] a : jnp.dtype @property def lowercase__ ( self ): """simple docstring""" return True @register_to_config def __init__( self , snake_case__ = 1_000 , snake_case__ = 0.0001 , snake_case__ = 0.02 , snake_case__ = "linear" , snake_case__ = None , snake_case__ = "fixed_small" , snake_case__ = True , snake_case__ = "epsilon" , snake_case__ = jnp.floataa , ): """simple docstring""" lowerCAmelCase : Any = dtype def lowercase__ ( self , snake_case__ = None ): """simple docstring""" if common is None: lowerCAmelCase : Dict = CommonSchedulerState.create(self ) # standard deviation of the initial noise distribution lowerCAmelCase : str = jnp.array(1.0 , dtype=self.dtype ) lowerCAmelCase : Any = jnp.arange(0 , self.config.num_train_timesteps ).round()[::-1] return DDPMSchedulerState.create( common=snake_case__ , init_noise_sigma=snake_case__ , timesteps=snake_case__ , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = None ): """simple docstring""" return sample def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = () ): """simple docstring""" lowerCAmelCase : List[Any] = self.config.num_train_timesteps // num_inference_steps # creates integer timesteps by multiplying by ratio # rounding to avoid issues when num_inference_step is power of 3 lowerCAmelCase : Any = (jnp.arange(0 , snake_case__ ) * step_ratio).round()[::-1] return state.replace( num_inference_steps=snake_case__ , timesteps=snake_case__ , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__=None , snake_case__=None ): """simple docstring""" lowerCAmelCase : Optional[Any] = state.common.alphas_cumprod[t] lowerCAmelCase : List[str] = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) # For t > 0, compute predicted variance βt (see formula (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf) # and sample from it to get previous sample # x_{t-1} ~ N(pred_prev_sample, variance) == add variance to pred_sample lowerCAmelCase : Union[str, Any] = (1 - alpha_prod_t_prev) / (1 - alpha_prod_t) * state.common.betas[t] if variance_type is None: lowerCAmelCase : List[str] = self.config.variance_type # hacks - were probably added for training stability if variance_type == "fixed_small": lowerCAmelCase : List[Any] = jnp.clip(snake_case__ , a_min=1e-20 ) # for rl-diffuser https://arxiv.org/abs/2205.09991 elif variance_type == "fixed_small_log": lowerCAmelCase : List[str] = jnp.log(jnp.clip(snake_case__ , a_min=1e-20 ) ) elif variance_type == "fixed_large": lowerCAmelCase : Optional[int] = state.common.betas[t] elif variance_type == "fixed_large_log": # Glide max_log lowerCAmelCase : List[str] = jnp.log(state.common.betas[t] ) elif variance_type == "learned": return predicted_variance elif variance_type == "learned_range": lowerCAmelCase : List[str] = variance lowerCAmelCase : Dict = state.common.betas[t] lowerCAmelCase : Optional[Any] = (predicted_variance + 1) / 2 lowerCAmelCase : List[str] = frac * max_log + (1 - frac) * min_log return variance def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ = None , snake_case__ = True , ): """simple docstring""" lowerCAmelCase : Optional[Any] = timestep if key is None: lowerCAmelCase : Tuple = jax.random.PRNGKey(0 ) if model_output.shape[1] == sample.shape[1] * 2 and self.config.variance_type in ["learned", "learned_range"]: lowerCAmelCase , lowerCAmelCase : Optional[Any] = jnp.split(snake_case__ , sample.shape[1] , axis=1 ) else: lowerCAmelCase : Tuple = None # 1. compute alphas, betas lowerCAmelCase : Optional[int] = state.common.alphas_cumprod[t] lowerCAmelCase : Optional[int] = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) lowerCAmelCase : Dict = 1 - alpha_prod_t lowerCAmelCase : Any = 1 - alpha_prod_t_prev # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if self.config.prediction_type == "epsilon": lowerCAmelCase : Union[str, Any] = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif self.config.prediction_type == "sample": lowerCAmelCase : List[Any] = model_output elif self.config.prediction_type == "v_prediction": lowerCAmelCase : Tuple = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output else: raise ValueError( f"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample` """ " for the FlaxDDPMScheduler." ) # 3. Clip "predicted x_0" if self.config.clip_sample: lowerCAmelCase : Optional[int] = jnp.clip(snake_case__ , -1 , 1 ) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowerCAmelCase : List[Any] = (alpha_prod_t_prev ** 0.5 * state.common.betas[t]) / beta_prod_t lowerCAmelCase : List[str] = state.common.alphas[t] ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowerCAmelCase : int = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise def random_variance(): lowerCAmelCase : Tuple = jax.random.split(snake_case__ , num=1 ) lowerCAmelCase : str = jax.random.normal(snake_case__ , shape=model_output.shape , dtype=self.dtype ) return (self._get_variance(snake_case__ , snake_case__ , predicted_variance=snake_case__ ) ** 0.5) * noise lowerCAmelCase : Union[str, Any] = jnp.where(t > 0 , random_variance() , jnp.zeros(model_output.shape , dtype=self.dtype ) ) lowerCAmelCase : Union[str, Any] = pred_prev_sample + variance if not return_dict: return (pred_prev_sample, state) return FlaxDDPMSchedulerOutput(prev_sample=snake_case__ , state=snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" return add_noise_common(state.common , snake_case__ , snake_case__ , snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" return get_velocity_common(state.common , snake_case__ , snake_case__ , snake_case__ ) def __len__( self ): """simple docstring""" return self.config.num_train_timesteps
681
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowerCAmelCase__ : Union[str, Any] = { '''configuration_rag''': ['''RagConfig'''], '''retrieval_rag''': ['''RagRetriever'''], '''tokenization_rag''': ['''RagTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : List[str] = [ '''RagModel''', '''RagPreTrainedModel''', '''RagSequenceForGeneration''', '''RagTokenForGeneration''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Union[str, Any] = [ '''TFRagModel''', '''TFRagPreTrainedModel''', '''TFRagSequenceForGeneration''', '''TFRagTokenForGeneration''', ] if TYPE_CHECKING: from .configuration_rag import RagConfig from .retrieval_rag import RagRetriever from .tokenization_rag import RagTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rag import RagModel, RagPreTrainedModel, RagSequenceForGeneration, RagTokenForGeneration try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rag import ( TFRagModel, TFRagPreTrainedModel, TFRagSequenceForGeneration, TFRagTokenForGeneration, ) else: import sys lowerCAmelCase__ : Any = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
703
"""simple docstring""" import argparse import OmegaConf import torch from diffusers import DDIMScheduler, LDMPipeline, UNetLDMModel, VQModel def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : Tuple = OmegaConf.load(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = torch.load(SCREAMING_SNAKE_CASE , map_location="cpu" )["model"] lowerCAmelCase : int = list(state_dict.keys() ) # extract state_dict for VQVAE lowerCAmelCase : Tuple = {} lowerCAmelCase : Dict = "first_stage_model." for key in keys: if key.startswith(SCREAMING_SNAKE_CASE ): lowerCAmelCase : List[str] = state_dict[key] # extract state_dict for UNetLDM lowerCAmelCase : List[Any] = {} lowerCAmelCase : Tuple = "model.diffusion_model." for key in keys: if key.startswith(SCREAMING_SNAKE_CASE ): lowerCAmelCase : str = state_dict[key] lowerCAmelCase : List[str] = config.model.params.first_stage_config.params lowerCAmelCase : List[Any] = config.model.params.unet_config.params lowerCAmelCase : Union[str, Any] = VQModel(**SCREAMING_SNAKE_CASE ).eval() vqvae.load_state_dict(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = UNetLDMModel(**SCREAMING_SNAKE_CASE ).eval() unet.load_state_dict(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[Any] = DDIMScheduler( timesteps=config.model.params.timesteps , beta_schedule="scaled_linear" , beta_start=config.model.params.linear_start , beta_end=config.model.params.linear_end , clip_sample=SCREAMING_SNAKE_CASE , ) lowerCAmelCase : Tuple = LDMPipeline(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) pipeline.save_pretrained(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument('''--checkpoint_path''', type=str, required=True) parser.add_argument('''--config_path''', type=str, required=True) parser.add_argument('''--output_path''', type=str, required=True) lowerCAmelCase__ = parser.parse_args() convert_ldm_original(args.checkpoint_path, args.config_path, args.output_path)
681
0
"""simple docstring""" from __future__ import annotations import inspect import unittest import numpy as np from transformers import DeiTConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, ) from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DeiTImageProcessor class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__=13 , snake_case__=30 , snake_case__=2 , snake_case__=3 , snake_case__=True , snake_case__=True , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=10 , snake_case__=0.02 , snake_case__=3 , snake_case__=None , snake_case__=2 , ): """simple docstring""" lowerCAmelCase : List[str] = parent lowerCAmelCase : int = batch_size lowerCAmelCase : Optional[int] = image_size lowerCAmelCase : Optional[Any] = patch_size lowerCAmelCase : Optional[int] = num_channels lowerCAmelCase : Dict = is_training lowerCAmelCase : List[Any] = use_labels lowerCAmelCase : List[Any] = hidden_size lowerCAmelCase : Optional[int] = num_hidden_layers lowerCAmelCase : int = num_attention_heads lowerCAmelCase : Tuple = intermediate_size lowerCAmelCase : Union[str, Any] = hidden_act lowerCAmelCase : Any = hidden_dropout_prob lowerCAmelCase : Dict = attention_probs_dropout_prob lowerCAmelCase : Tuple = type_sequence_label_size lowerCAmelCase : Dict = initializer_range lowerCAmelCase : Optional[int] = scope lowerCAmelCase : Union[str, Any] = encoder_stride # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens) lowerCAmelCase : Tuple = (image_size // patch_size) ** 2 lowerCAmelCase : Union[str, Any] = num_patches + 2 def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Tuple = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase : str = None if self.use_labels: lowerCAmelCase : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCAmelCase : List[Any] = self.get_config() return config, pixel_values, labels def lowercase__ ( self ): """simple docstring""" return DeiTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=snake_case__ , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Tuple = TFDeiTModel(config=snake_case__ ) lowerCAmelCase : Union[str, Any] = model(snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : List[Any] = TFDeiTForMaskedImageModeling(config=snake_case__ ) lowerCAmelCase : str = model(snake_case__ ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images lowerCAmelCase : str = 1 lowerCAmelCase : str = TFDeiTForMaskedImageModeling(snake_case__ ) lowerCAmelCase : List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCAmelCase : Any = model(snake_case__ ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Optional[int] = self.type_sequence_label_size lowerCAmelCase : Dict = TFDeiTForImageClassification(snake_case__ ) lowerCAmelCase : Tuple = model(snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowerCAmelCase : List[str] = 1 lowerCAmelCase : Union[str, Any] = TFDeiTForImageClassification(snake_case__ ) lowerCAmelCase : Union[str, Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCAmelCase : List[Any] = model(snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = self.prepare_config_and_inputs() lowerCAmelCase : Any = config_and_inputs lowerCAmelCase : Dict = {'pixel_values': pixel_values} return config, inputs_dict @require_tf class SCREAMING_SNAKE_CASE__ ( __A , __A , unittest.TestCase ): """simple docstring""" a : str =( ( TFDeiTModel, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, ) if is_tf_available() else () ) a : Union[str, Any] =( { "feature-extraction": TFDeiTModel, "image-classification": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher), } if is_tf_available() else {} ) a : Union[str, Any] =False a : List[str] =False a : Union[str, Any] =False a : Union[str, Any] =False def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = TFDeiTModelTester(self ) lowerCAmelCase : Optional[Any] = ConfigTester(self , config_class=snake_case__ , has_text_modality=snake_case__ , hidden_size=37 ) def lowercase__ ( self ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason="DeiT does not use inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : Union[str, Any] = model_class(snake_case__ ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) lowerCAmelCase : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case__ , tf.keras.layers.Dense ) ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : List[Any] = model_class(snake_case__ ) lowerCAmelCase : str = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase : List[Any] = [*signature.parameters.keys()] lowerCAmelCase : List[Any] = ['pixel_values'] self.assertListEqual(arg_names[:1] , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__=False ): """simple docstring""" lowerCAmelCase : List[str] = super()._prepare_for_class(snake_case__ , snake_case__ , return_labels=snake_case__ ) if return_labels: if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call ).parameters: del inputs_dict["labels"] return inputs_dict @slow def lowercase__ ( self ): """simple docstring""" for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase : Optional[int] = TFDeiTModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def a__ ( ): '''simple docstring''' lowerCAmelCase : Any = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_tf @require_vision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" @cached_property def lowercase__ ( self ): """simple docstring""" return ( DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224" ) if is_vision_available() else None ) @slow def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224" ) lowerCAmelCase : Union[str, Any] = self.default_image_processor lowerCAmelCase : List[Any] = prepare_img() lowerCAmelCase : Any = image_processor(images=snake_case__ , return_tensors="tf" ) # forward pass lowerCAmelCase : Optional[Any] = model(**snake_case__ ) # verify the logits lowerCAmelCase : str = tf.TensorShape((1, 1_000) ) self.assertEqual(outputs.logits.shape , snake_case__ ) lowerCAmelCase : Tuple = tf.constant([-1.0266, 0.1912, -1.2861] ) self.assertTrue(np.allclose(outputs.logits[0, :3] , snake_case__ , atol=1e-4 ) )
704
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : int = 1_0_0_0 ): '''simple docstring''' return sum(e for e in range(3 , SCREAMING_SNAKE_CASE ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"{solution() = }")
681
0
"""simple docstring""" 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_DEFAULT_MEAN, IMAGENET_DEFAULT_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 lowerCAmelCase__ = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ ( __lowercase ): """simple docstring""" a : int =["pixel_values"] def __init__( self , snake_case__ = True , snake_case__ = None , snake_case__ = 0.9 , snake_case__ = PILImageResampling.BICUBIC , snake_case__ = True , snake_case__ = None , snake_case__ = 1 / 255 , snake_case__ = True , snake_case__ = True , snake_case__ = None , snake_case__ = None , **snake_case__ , ): """simple docstring""" super().__init__(**_A ) lowerCAmelCase : int = size if size is not None else {"shortest_edge": 224} lowerCAmelCase : Optional[int] = get_size_dict(_A , default_to_square=_A ) lowerCAmelCase : List[str] = crop_size if crop_size is not None else {"height": 224, "width": 224} lowerCAmelCase : Dict = get_size_dict(_A , param_name="crop_size" ) lowerCAmelCase : List[Any] = do_resize lowerCAmelCase : List[Any] = size lowerCAmelCase : Dict = crop_pct lowerCAmelCase : int = resample lowerCAmelCase : Union[str, Any] = do_center_crop lowerCAmelCase : Union[str, Any] = crop_size lowerCAmelCase : Optional[int] = do_rescale lowerCAmelCase : Optional[Any] = rescale_factor lowerCAmelCase : str = do_normalize lowerCAmelCase : List[str] = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN lowerCAmelCase : List[Any] = image_std if image_std is not None else IMAGENET_DEFAULT_STD def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = None , snake_case__ = PILImageResampling.BICUBIC , snake_case__ = None , **snake_case__ , ): """simple docstring""" lowerCAmelCase : Union[str, Any] = get_size_dict(_A , default_to_square=_A ) if "shortest_edge" not in size and ("height" not in size or "width" not in size): raise ValueError(f"""size must contain 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}""" ) if crop_pct is not None: if "shortest_edge" in size: lowerCAmelCase : List[Any] = int(size["shortest_edge"] / crop_pct ) elif "height" in size and "width" in size: if size["height"] == size["width"]: lowerCAmelCase : str = int(size["height"] / crop_pct ) else: lowerCAmelCase : List[str] = (int(size["height"] / crop_pct ), int(size["width"] / crop_pct )) else: raise ValueError("Invalid size for resize: {}".format(_A ) ) lowerCAmelCase : Optional[Any] = get_resize_output_image_size(_A , size=_A , default_to_square=_A ) else: if "shortest_edge" in size: lowerCAmelCase : Optional[int] = get_resize_output_image_size(_A , size=size["shortest_edge"] , default_to_square=_A ) elif "height" in size and "width" in size: lowerCAmelCase : List[str] = (size["height"], size["width"]) else: raise ValueError("Invalid size for resize: {}".format(_A ) ) return resize(_A , size=_A , resample=_A , data_format=_A , **_A ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = None , **snake_case__ , ): """simple docstring""" lowerCAmelCase : Tuple = get_size_dict(_A ) if "height" not in size or "width" not in size: raise ValueError(f"""size must contain 'height' and 'width' as keys. Got {size.keys()}""" ) return center_crop(_A , size=(size["height"], size["width"]) , data_format=_A , **_A ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = None , **snake_case__ , ): """simple docstring""" return rescale(_A , scale=_A , data_format=_A , **_A ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ = None , **snake_case__ , ): """simple docstring""" return normalize(_A , mean=_A , std=_A , data_format=_A , **_A ) def lowercase__ ( self , snake_case__ , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = ChannelDimension.FIRST , **snake_case__ , ): """simple docstring""" lowerCAmelCase : Optional[Any] = do_resize if do_resize is not None else self.do_resize lowerCAmelCase : str = crop_pct if crop_pct is not None else self.crop_pct lowerCAmelCase : Union[str, Any] = resample if resample is not None else self.resample lowerCAmelCase : int = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCAmelCase : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale lowerCAmelCase : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCAmelCase : int = do_normalize if do_normalize is not None else self.do_normalize lowerCAmelCase : int = image_mean if image_mean is not None else self.image_mean lowerCAmelCase : Optional[Any] = image_std if image_std is not None else self.image_std lowerCAmelCase : Any = size if size is not None else self.size lowerCAmelCase : Optional[Any] = get_size_dict(_A , default_to_square=_A ) lowerCAmelCase : str = crop_size if crop_size is not None else self.crop_size lowerCAmelCase : List[Any] = get_size_dict(_A , param_name="crop_size" ) lowerCAmelCase : str = make_list_of_images(_A ) if not valid_images(_A ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True." ) if do_center_crop and crop_pct is None: raise ValueError("Crop_pct must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) # All transformations expect numpy arrays. lowerCAmelCase : int = [to_numpy_array(_A ) for image in images] if do_resize: lowerCAmelCase : int = [self.resize(image=_A , size=_A , crop_pct=_A , resample=_A ) for image in images] if do_center_crop: lowerCAmelCase : Optional[int] = [self.center_crop(image=_A , size=_A ) for image in images] if do_rescale: lowerCAmelCase : int = [self.rescale(image=_A , scale=_A ) for image in images] if do_normalize: lowerCAmelCase : Tuple = [self.normalize(image=_A , mean=_A , std=_A ) for image in images] lowerCAmelCase : Any = [to_channel_dimension_format(_A , _A ) for image in images] lowerCAmelCase : Optional[int] = {"pixel_values": images} return BatchFeature(data=_A , tensor_type=_A )
705
"""simple docstring""" import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized, parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv("TEST_SAGEMAKER" , "False" ) ) is not True , reason="Skipping test because should only be run when releasing minor transformers version" , ) @pytest.mark.usefixtures("sm_env" ) @parameterized_class( [ { "framework": "pytorch", "script": "run_glue_model_parallelism.py", "model_name_or_path": "roberta-large", "instance_type": "ml.p3dn.24xlarge", "results": {"train_runtime": 16_00, "eval_accuracy": 0.3, "eval_loss": 1.2}, }, { "framework": "pytorch", "script": "run_glue.py", "model_name_or_path": "roberta-large", "instance_type": "ml.p3dn.24xlarge", "results": {"train_runtime": 16_00, "eval_accuracy": 0.3, "eval_loss": 1.2}, }, ] ) class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" if self.framework == "pytorch": subprocess.run( f"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split() , encoding="utf-8" , check=snake_case__ , ) assert hasattr(self , "env" ) def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Optional[Any] = { "enabled": True, "processes_per_host": 8, } lowerCAmelCase : List[Any] = { "enabled": True, "parameters": { "microbatches": 4, "placement_strategy": "spread", "pipeline": "interleaved", "optimize": "speed", "partitions": 4, "ddp": True, }, } lowerCAmelCase : List[Any] = {"smdistributed": {"modelparallel": smp_options}, "mpi": mpi_options} lowerCAmelCase : Optional[Any] = "trainer" if self.script == "run_glue.py" else "smtrainer" # creates estimator return HuggingFace( entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=f"""{self.env.base_job_name}-{instance_count}-smp-{name_extension}""" , instance_count=snake_case__ , instance_type=self.instance_type , debugger_hook_config=snake_case__ , hyperparameters={ **self.env.hyperparameters, "model_name_or_path": self.model_name_or_path, "max_steps": 500, } , metric_definitions=self.env.metric_definitions , distribution=snake_case__ , py_version="py36" , ) def lowercase__ ( self , snake_case__ ): """simple docstring""" TrainingJobAnalytics(snake_case__ ).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv""" ) @parameterized.expand([(1,)] ) def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = self.create_estimator(snake_case__ ) # run training estimator.fit() # result dataframe lowerCAmelCase : Tuple = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis lowerCAmelCase : Tuple = list(result_metrics_df[result_metrics_df.metric_name == "eval_accuracy"]["value"] ) lowerCAmelCase : Optional[int] = list(result_metrics_df[result_metrics_df.metric_name == "eval_loss"]["value"] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping lowerCAmelCase : Dict = ( Session().describe_training_job(estimator.latest_training_job.name ).get("TrainingTimeInSeconds" , 999_999 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results["eval_accuracy"] for t in eval_accuracy ) assert all(t <= self.results["eval_loss"] for t in eval_loss ) # dump tests result into json file to share in PR with open(f"""{estimator.latest_training_job.name}.json""" , "w" ) as outfile: json.dump({"train_time": train_runtime, "eval_accuracy": eval_accuracy, "eval_loss": eval_loss} , snake_case__ )
681
0
"""simple docstring""" import os import tempfile import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from torch import nn from transformers 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_inverse_sqrt_schedule, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) def a__ ( SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : Tuple=1_0 ): '''simple docstring''' lowerCAmelCase : Dict = [] for _ in range(lowerCAmelCase__ ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() return lrs def a__ ( SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : Any=1_0 ): '''simple docstring''' lowerCAmelCase : str = [] for step in range(lowerCAmelCase__ ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() if step == num_steps // 2: with tempfile.TemporaryDirectory() as tmpdirname: lowerCAmelCase : Any = os.path.join(lowerCAmelCase__ , "schedule.bin" ) torch.save(scheduler.state_dict() , lowerCAmelCase__ ) lowerCAmelCase : Optional[int] = torch.load(lowerCAmelCase__ ) scheduler.load_state_dict(lowerCAmelCase__ ) return lrs @require_torch class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" self.assertEqual(len(snake_case__ ) , len(snake_case__ ) ) for a, b in zip(snake_case__ , snake_case__ ): self.assertAlmostEqual(snake_case__ , snake_case__ , delta=snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = torch.tensor([0.1, -0.2, -0.1] , requires_grad=snake_case__ ) lowerCAmelCase : List[str] = torch.tensor([0.4, 0.2, -0.5] ) lowerCAmelCase : List[str] = nn.MSELoss() # No warmup, constant schedule, no gradient clipping lowerCAmelCase : Optional[int] = AdamW(params=[w] , lr=2e-1 , weight_decay=0.0 ) for _ in range(100 ): lowerCAmelCase : Tuple = criterion(snake_case__ , snake_case__ ) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1e-2 ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[Any] = torch.tensor([0.1, -0.2, -0.1] , requires_grad=snake_case__ ) lowerCAmelCase : int = torch.tensor([0.4, 0.2, -0.5] ) lowerCAmelCase : List[Any] = nn.MSELoss() # No warmup, constant schedule, no gradient clipping lowerCAmelCase : List[Any] = Adafactor( params=[w] , lr=1e-2 , eps=(1e-30, 1e-3) , clip_threshold=1.0 , decay_rate=-0.8 , betaa=snake_case__ , weight_decay=0.0 , relative_step=snake_case__ , scale_parameter=snake_case__ , warmup_init=snake_case__ , ) for _ in range(1_000 ): lowerCAmelCase : Any = criterion(snake_case__ , snake_case__ ) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1e-2 ) @require_torch class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" a : int =nn.Linear(50 , 50 ) if is_torch_available() else None a : Tuple =AdamW(m.parameters() , lr=10.0 ) if is_torch_available() else None a : Dict =10 def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__=None ): """simple docstring""" self.assertEqual(len(snake_case__ ) , len(snake_case__ ) ) for a, b in zip(snake_case__ , snake_case__ ): self.assertAlmostEqual(snake_case__ , snake_case__ , delta=snake_case__ , msg=snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = {"num_warmup_steps": 2, "num_training_steps": 10} # schedulers doct format # function: (sched_args_dict, expected_learning_rates) lowerCAmelCase : Union[str, Any] = { get_constant_schedule: ({}, [10.0] * self.num_steps), get_constant_schedule_with_warmup: ( {"num_warmup_steps": 4}, [0.0, 2.5, 5.0, 7.5, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0], ), get_linear_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25], ), get_cosine_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 9.61, 8.53, 6.91, 5.0, 3.08, 1.46, 0.38], ), get_cosine_with_hard_restarts_schedule_with_warmup: ( {**common_kwargs, "num_cycles": 2}, [0.0, 5.0, 10.0, 8.53, 5.0, 1.46, 10.0, 8.53, 5.0, 1.46], ), get_polynomial_decay_schedule_with_warmup: ( {**common_kwargs, "power": 2.0, "lr_end": 1e-7}, [0.0, 5.0, 10.0, 7.656, 5.625, 3.906, 2.5, 1.406, 0.625, 0.156], ), get_inverse_sqrt_schedule: ( {"num_warmup_steps": 2}, [0.0, 5.0, 10.0, 8.165, 7.071, 6.325, 5.774, 5.345, 5.0, 4.714], ), } for scheduler_func, data in scheds.items(): lowerCAmelCase , lowerCAmelCase : int = data lowerCAmelCase : Optional[int] = scheduler_func(self.optimizer , **snake_case__ ) self.assertEqual(len([scheduler.get_lr()[0]] ) , 1 ) lowerCAmelCase : List[Any] = unwrap_schedule(snake_case__ , self.num_steps ) self.assertListAlmostEqual( snake_case__ , snake_case__ , tol=1e-2 , msg=f"""failed for {scheduler_func} in normal scheduler""" , ) lowerCAmelCase : Union[str, Any] = scheduler_func(self.optimizer , **snake_case__ ) if scheduler_func.__name__ != "get_constant_schedule": LambdaScheduleWrapper.wrap_scheduler(snake_case__ ) # wrap to test picklability of the schedule lowerCAmelCase : Dict = unwrap_and_save_reload_schedule(snake_case__ , self.num_steps ) self.assertListEqual(snake_case__ , snake_case__ , msg=f"""failed for {scheduler_func} in save and reload""" ) class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Union[str, Any] = fn def __call__( self , *snake_case__ , **snake_case__ ): """simple docstring""" return self.fn(*snake_case__ , **snake_case__ ) @classmethod def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : int = list(map(self , scheduler.lr_lambdas ) )
706
"""simple docstring""" from math import factorial def a__ ( SCREAMING_SNAKE_CASE : int = 1_0_0 ): '''simple docstring''' return sum(int(SCREAMING_SNAKE_CASE ) for x in str(factorial(SCREAMING_SNAKE_CASE ) ) ) if __name__ == "__main__": print(solution(int(input('''Enter the Number: ''').strip())))
681
0
"""simple docstring""" def a__ ( ): '''simple docstring''' return [ a * b * (1_0_0_0 - a - b) for a in range(1 , 9_9_9 ) for b in range(lowerCAmelCase_ , 9_9_9 ) if (a * a + b * b == (1_0_0_0 - a - b) ** 2) ][0] if __name__ == "__main__": print(F"{solution() = }")
707
"""simple docstring""" from typing import Any class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = data lowerCAmelCase : Any = None def __repr__( self ): """simple docstring""" return f"""Node({self.data})""" class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self ): """simple docstring""" lowerCAmelCase : Tuple = None def __iter__( self ): """simple docstring""" lowerCAmelCase : Any = self.head while node: yield node.data lowerCAmelCase : Optional[int] = node.next def __len__( self ): """simple docstring""" return sum(1 for _ in self ) def __repr__( self ): """simple docstring""" return "->".join([str(snake_case__ ) for item in self] ) def __getitem__( self , snake_case__ ): """simple docstring""" if not 0 <= index < len(self ): raise ValueError("list index out of range." ) for i, node in enumerate(self ): if i == index: return node return None def __setitem__( self , snake_case__ , snake_case__ ): """simple docstring""" if not 0 <= index < len(self ): raise ValueError("list index out of range." ) lowerCAmelCase : Union[str, Any] = self.head for _ in range(snake_case__ ): lowerCAmelCase : int = current.next lowerCAmelCase : List[str] = data def lowercase__ ( self , snake_case__ ): """simple docstring""" self.insert_nth(len(self ) , snake_case__ ) def lowercase__ ( self , snake_case__ ): """simple docstring""" self.insert_nth(0 , snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ ): """simple docstring""" if not 0 <= index <= len(self ): raise IndexError("list index out of range" ) lowerCAmelCase : Optional[int] = Node(snake_case__ ) if self.head is None: lowerCAmelCase : Any = new_node elif index == 0: lowerCAmelCase : Any = self.head # link new_node to head lowerCAmelCase : Union[str, Any] = new_node else: lowerCAmelCase : List[str] = self.head for _ in range(index - 1 ): lowerCAmelCase : int = temp.next lowerCAmelCase : int = temp.next lowerCAmelCase : Dict = new_node def lowercase__ ( self ): # print every node data """simple docstring""" print(self ) def lowercase__ ( self ): """simple docstring""" return self.delete_nth(0 ) def lowercase__ ( self ): # delete from tail """simple docstring""" return self.delete_nth(len(self ) - 1 ) def lowercase__ ( self , snake_case__ = 0 ): """simple docstring""" if not 0 <= index <= len(self ) - 1: # test if index is valid raise IndexError("List index out of range." ) lowerCAmelCase : List[Any] = self.head # default first node if index == 0: lowerCAmelCase : Optional[int] = self.head.next else: lowerCAmelCase : List[str] = self.head for _ in range(index - 1 ): lowerCAmelCase : Union[str, Any] = temp.next lowerCAmelCase : Optional[Any] = temp.next lowerCAmelCase : Any = temp.next.next return delete_node.data def lowercase__ ( self ): """simple docstring""" return self.head is None def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = None lowerCAmelCase : Optional[int] = self.head while current: # Store the current node's next node. lowerCAmelCase : List[Any] = current.next # Make the current node's next point backwards lowerCAmelCase : Dict = prev # Make the previous node be the current node lowerCAmelCase : List[str] = current # Make the current node the next node (to progress iteration) lowerCAmelCase : int = next_node # Return prev in order to put the head at the end lowerCAmelCase : Tuple = prev def a__ ( ): '''simple docstring''' lowerCAmelCase : Tuple = LinkedList() assert linked_list.is_empty() is True assert str(SCREAMING_SNAKE_CASE ) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(1_0 ): assert len(SCREAMING_SNAKE_CASE ) == i linked_list.insert_nth(SCREAMING_SNAKE_CASE , i + 1 ) assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(1 , 1_1 ) ) linked_list.insert_head(0 ) linked_list.insert_tail(1_1 ) assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(0 , 1_2 ) ) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9 ) == 1_0 assert linked_list.delete_tail() == 1_1 assert len(SCREAMING_SNAKE_CASE ) == 9 assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(1 , 1_0 ) ) assert all(linked_list[i] == i + 1 for i in range(0 , 9 ) ) is True for i in range(0 , 9 ): lowerCAmelCase : Optional[Any] = -i assert all(linked_list[i] == -i for i in range(0 , 9 ) ) is True linked_list.reverse() assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(-8 , 1 ) ) def a__ ( ): '''simple docstring''' lowerCAmelCase : List[str] = [ -9, 1_0_0, Node(7_7_3_4_5_1_1_2 ), "dlrow olleH", 7, 5_5_5_5, 0, -192.55_555, "Hello, world!", 77.9, Node(1_0 ), None, None, 12.20, ] lowerCAmelCase : List[str] = LinkedList() for i in test_input: linked_list.insert_tail(SCREAMING_SNAKE_CASE ) # Check if it's empty or not assert linked_list.is_empty() is False assert ( str(SCREAMING_SNAKE_CASE ) == "-9->100->Node(77345112)->dlrow olleH->7->5555->0->" "-192.55555->Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the head lowerCAmelCase : str = linked_list.delete_head() assert result == -9 assert ( str(SCREAMING_SNAKE_CASE ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the tail lowerCAmelCase : Union[str, Any] = linked_list.delete_tail() assert result == 12.2 assert ( str(SCREAMING_SNAKE_CASE ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None" ) # Delete a node in specific location in linked list lowerCAmelCase : List[str] = linked_list.delete_nth(1_0 ) assert result is None assert ( str(SCREAMING_SNAKE_CASE ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None" ) # Add a Node instance to its head linked_list.insert_head(Node("Hello again, world!" ) ) assert ( str(SCREAMING_SNAKE_CASE ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None" ) # Add None to its tail linked_list.insert_tail(SCREAMING_SNAKE_CASE ) assert ( str(SCREAMING_SNAKE_CASE ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None->None" ) # Reverse the linked list linked_list.reverse() assert ( str(SCREAMING_SNAKE_CASE ) == "None->None->Node(10)->77.9->Hello, world!->-192.55555->0->5555->" "7->dlrow olleH->Node(77345112)->100->Node(Hello again, world!)" ) def a__ ( ): '''simple docstring''' from doctest import testmod testmod() lowerCAmelCase : Optional[Any] = LinkedList() linked_list.insert_head(input("Inserting 1st at head " ).strip() ) linked_list.insert_head(input("Inserting 2nd at head " ).strip() ) print("\nPrint list:" ) linked_list.print_list() linked_list.insert_tail(input("\nInserting 1st at tail " ).strip() ) linked_list.insert_tail(input("Inserting 2nd at tail " ).strip() ) print("\nPrint list:" ) linked_list.print_list() print("\nDelete head" ) linked_list.delete_head() print("Delete tail" ) linked_list.delete_tail() print("\nPrint list:" ) linked_list.print_list() print("\nReverse linked list" ) linked_list.reverse() print("\nPrint list:" ) linked_list.print_list() print("\nString representation of linked list:" ) print(SCREAMING_SNAKE_CASE ) print("\nReading/changing Node data using indexing:" ) print(f"""Element at Position 1: {linked_list[1]}""" ) lowerCAmelCase : Any = input("Enter New Value: " ).strip() print("New list:" ) print(SCREAMING_SNAKE_CASE ) print(f"""length of linked_list is : {len(SCREAMING_SNAKE_CASE )}""" ) if __name__ == "__main__": main()
681
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) lowerCAmelCase__ = { '''configuration_layoutlmv2''': ['''LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''LayoutLMv2Config'''], '''processing_layoutlmv2''': ['''LayoutLMv2Processor'''], '''tokenization_layoutlmv2''': ['''LayoutLMv2Tokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''LayoutLMv2TokenizerFast'''] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''LayoutLMv2FeatureExtractor'''] lowerCAmelCase__ = ['''LayoutLMv2ImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST''', '''LayoutLMv2ForQuestionAnswering''', '''LayoutLMv2ForSequenceClassification''', '''LayoutLMv2ForTokenClassification''', '''LayoutLMv2Layer''', '''LayoutLMv2Model''', '''LayoutLMv2PreTrainedModel''', ] if TYPE_CHECKING: from .configuration_layoutlmva import LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig from .processing_layoutlmva import LayoutLMvaProcessor from .tokenization_layoutlmva import LayoutLMvaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor, LayoutLMvaImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_layoutlmva import ( LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaLayer, LayoutLMvaModel, LayoutLMvaPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
708
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowerCAmelCase__ = { '''configuration_efficientformer''': [ '''EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EfficientFormerConfig''', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''EfficientFormerImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EfficientFormerForImageClassification''', '''EfficientFormerForImageClassificationWithTeacher''', '''EfficientFormerModel''', '''EfficientFormerPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFEfficientFormerForImageClassification''', '''TFEfficientFormerForImageClassificationWithTeacher''', '''TFEfficientFormerModel''', '''TFEfficientFormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_efficientformer import EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientformer import EfficientFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientformer import ( EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientFormerForImageClassification, EfficientFormerForImageClassificationWithTeacher, EfficientFormerModel, EfficientFormerPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_efficientformer import ( TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFEfficientFormerForImageClassification, TFEfficientFormerForImageClassificationWithTeacher, TFEfficientFormerModel, TFEfficientFormerPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
681
0
"""simple docstring""" from __future__ import annotations import time lowerCAmelCase__ = list[tuple[int, int]] lowerCAmelCase__ = [ [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], ] lowerCAmelCase__ = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Optional[Any] = pos_x lowerCAmelCase : Tuple = pos_y lowerCAmelCase : Optional[Any] = (pos_y, pos_x) lowerCAmelCase : Optional[Any] = goal_x lowerCAmelCase : Optional[int] = goal_y lowerCAmelCase : Any = parent class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : int = Node(start[1] , start[0] , goal[1] , goal[0] , _SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = Node(goal[1] , goal[0] , goal[1] , goal[0] , _SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = [self.start] lowerCAmelCase : Dict = False def lowercase__ ( self ): """simple docstring""" while self.node_queue: lowerCAmelCase : Optional[int] = self.node_queue.pop(0 ) if current_node.pos == self.target.pos: lowerCAmelCase : List[Any] = True return self.retrace_path(_SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[Any] = self.get_successors(_SCREAMING_SNAKE_CASE ) for node in successors: self.node_queue.append(_SCREAMING_SNAKE_CASE ) if not self.reached: return [self.start.pos] return None def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : List[str] = [] for action in delta: lowerCAmelCase : Union[str, Any] = parent.pos_x + action[1] lowerCAmelCase : List[Any] = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(_SCREAMING_SNAKE_CASE ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , self.target.pos_y , self.target.pos_x , _SCREAMING_SNAKE_CASE ) ) return successors def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : List[str] = node lowerCAmelCase : Union[str, Any] = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) lowerCAmelCase : int = current_node.parent path.reverse() return path class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : List[Any] = BreadthFirstSearch(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCAmelCase : int = BreadthFirstSearch(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[Any] = False def lowercase__ ( self ): """simple docstring""" while self.fwd_bfs.node_queue or self.bwd_bfs.node_queue: lowerCAmelCase : Union[str, Any] = self.fwd_bfs.node_queue.pop(0 ) lowerCAmelCase : Dict = self.bwd_bfs.node_queue.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: lowerCAmelCase : Tuple = True return self.retrace_bidirectional_path( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCAmelCase : str = current_bwd_node lowerCAmelCase : Optional[Any] = current_fwd_node lowerCAmelCase : Optional[Any] = { self.fwd_bfs: self.fwd_bfs.get_successors(_SCREAMING_SNAKE_CASE ), self.bwd_bfs: self.bwd_bfs.get_successors(_SCREAMING_SNAKE_CASE ), } for bfs in [self.fwd_bfs, self.bwd_bfs]: for node in successors[bfs]: bfs.node_queue.append(_SCREAMING_SNAKE_CASE ) if not self.reached: return [self.fwd_bfs.start.pos] return None def lowercase__ ( self , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Optional[int] = self.fwd_bfs.retrace_path(_SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[Any] = self.bwd_bfs.retrace_path(_SCREAMING_SNAKE_CASE ) bwd_path.pop() bwd_path.reverse() lowerCAmelCase : str = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] import doctest doctest.testmod() lowerCAmelCase__ = (0, 0) lowerCAmelCase__ = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) lowerCAmelCase__ = time.time() lowerCAmelCase__ = BreadthFirstSearch(init, goal) lowerCAmelCase__ = bfs.search() lowerCAmelCase__ = time.time() - start_bfs_time print('''Unidirectional BFS computation time : ''', bfs_time) lowerCAmelCase__ = time.time() lowerCAmelCase__ = BidirectionalBreadthFirstSearch(init, goal) lowerCAmelCase__ = bd_bfs.search() lowerCAmelCase__ = time.time() - start_bd_bfs_time print('''Bidirectional BFS computation time : ''', bd_bfs_time)
709
"""simple docstring""" 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 ) lowerCAmelCase__ = logging.getLogger(__name__) def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : Optional[Any] = np.argmax(SCREAMING_SNAKE_CASE , axis=1 ) return np.sum(outputs == labels ) def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE , encoding="utf_8" ) as f: lowerCAmelCase : Tuple = csv.reader(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = [] next(SCREAMING_SNAKE_CASE ) # skip the first line for line in tqdm(SCREAMING_SNAKE_CASE ): output.append((" ".join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : str ): '''simple docstring''' lowerCAmelCase : List[Any] = [] for dataset in encoded_datasets: lowerCAmelCase : Dict = len(SCREAMING_SNAKE_CASE ) lowerCAmelCase : str = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) lowerCAmelCase : Tuple = np.zeros((n_batch, 2) , dtype=np.intaa ) lowerCAmelCase : int = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) lowerCAmelCase : List[Any] = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(SCREAMING_SNAKE_CASE ): lowerCAmelCase : int = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase : Union[str, Any] = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase : Tuple = with_conta lowerCAmelCase : Any = with_conta lowerCAmelCase : Optional[Any] = len(SCREAMING_SNAKE_CASE ) - 1 lowerCAmelCase : Dict = len(SCREAMING_SNAKE_CASE ) - 1 lowerCAmelCase : Optional[Any] = with_conta lowerCAmelCase : List[Any] = with_conta lowerCAmelCase : str = mc_label lowerCAmelCase : Dict = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(SCREAMING_SNAKE_CASE ) for t in all_inputs ) ) return tensor_datasets def a__ ( ): '''simple docstring''' lowerCAmelCase : Optional[int] = argparse.ArgumentParser() parser.add_argument("--model_name" , type=SCREAMING_SNAKE_CASE , 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=SCREAMING_SNAKE_CASE , type=SCREAMING_SNAKE_CASE , required=SCREAMING_SNAKE_CASE , help="The output directory where the model predictions and checkpoints will be written." , ) parser.add_argument("--train_dataset" , type=SCREAMING_SNAKE_CASE , default="" ) parser.add_argument("--eval_dataset" , type=SCREAMING_SNAKE_CASE , default="" ) parser.add_argument("--seed" , type=SCREAMING_SNAKE_CASE , default=4_2 ) parser.add_argument("--num_train_epochs" , type=SCREAMING_SNAKE_CASE , default=3 ) parser.add_argument("--train_batch_size" , type=SCREAMING_SNAKE_CASE , default=8 ) parser.add_argument("--eval_batch_size" , type=SCREAMING_SNAKE_CASE , default=1_6 ) parser.add_argument("--adam_epsilon" , default=1E-8 , type=SCREAMING_SNAKE_CASE , help="Epsilon for Adam optimizer." ) parser.add_argument("--max_grad_norm" , type=SCREAMING_SNAKE_CASE , default=1 ) parser.add_argument( "--max_steps" , default=-1 , type=SCREAMING_SNAKE_CASE , help=( "If > 0: set total number of training steps to perform. Override num_train_epochs." ) , ) parser.add_argument( "--gradient_accumulation_steps" , type=SCREAMING_SNAKE_CASE , default=1 , help="Number of updates steps to accumulate before performing a backward/update pass." , ) parser.add_argument("--learning_rate" , type=SCREAMING_SNAKE_CASE , default=6.2_5E-5 ) parser.add_argument("--warmup_steps" , default=0 , type=SCREAMING_SNAKE_CASE , help="Linear warmup over warmup_steps." ) parser.add_argument("--lr_schedule" , type=SCREAMING_SNAKE_CASE , default="warmup_linear" ) parser.add_argument("--weight_decay" , type=SCREAMING_SNAKE_CASE , default=0.01 ) parser.add_argument("--lm_coef" , type=SCREAMING_SNAKE_CASE , default=0.9 ) parser.add_argument("--n_valid" , type=SCREAMING_SNAKE_CASE , default=3_7_4 ) parser.add_argument("--server_ip" , type=SCREAMING_SNAKE_CASE , default="" , help="Can be used for distant debugging." ) parser.add_argument("--server_port" , type=SCREAMING_SNAKE_CASE , default="" , help="Can be used for distant debugging." ) lowerCAmelCase : Tuple = parser.parse_args() print(SCREAMING_SNAKE_CASE ) 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=SCREAMING_SNAKE_CASE ) 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 ) lowerCAmelCase : Optional[int] = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) lowerCAmelCase : Optional[int] = torch.cuda.device_count() logger.info("device: {}, n_gpu {}".format(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) 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 lowerCAmelCase : str = ["_start_", "_delimiter_", "_classify_"] lowerCAmelCase : Dict = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(SCREAMING_SNAKE_CASE ) ) model.to(SCREAMING_SNAKE_CASE ) # Load and encode the datasets def tokenize_and_encode(SCREAMING_SNAKE_CASE : Optional[Any] ): if isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(SCREAMING_SNAKE_CASE ) ) elif isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return obj return [tokenize_and_encode(SCREAMING_SNAKE_CASE ) for o in obj] logger.info("Encoding dataset..." ) lowerCAmelCase : Optional[Any] = load_rocstories_dataset(args.train_dataset ) lowerCAmelCase : int = load_rocstories_dataset(args.eval_dataset ) lowerCAmelCase : Tuple = (train_dataset, eval_dataset) lowerCAmelCase : Dict = tokenize_and_encode(SCREAMING_SNAKE_CASE ) # Compute the max input length for the Transformer lowerCAmelCase : Any = model.config.n_positions // 2 - 2 lowerCAmelCase : int = 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 ) lowerCAmelCase : Union[str, Any] = min(SCREAMING_SNAKE_CASE , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders lowerCAmelCase : Any = pre_process_datasets(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , *SCREAMING_SNAKE_CASE ) lowerCAmelCase , lowerCAmelCase : Tuple = tensor_datasets[0], tensor_datasets[1] lowerCAmelCase : List[str] = TensorDataset(*SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = RandomSampler(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = DataLoader(SCREAMING_SNAKE_CASE , sampler=SCREAMING_SNAKE_CASE , batch_size=args.train_batch_size ) lowerCAmelCase : int = TensorDataset(*SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = SequentialSampler(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = DataLoader(SCREAMING_SNAKE_CASE , sampler=SCREAMING_SNAKE_CASE , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: lowerCAmelCase : int = args.max_steps lowerCAmelCase : str = args.max_steps // (len(SCREAMING_SNAKE_CASE ) // args.gradient_accumulation_steps) + 1 else: lowerCAmelCase : Union[str, Any] = len(SCREAMING_SNAKE_CASE ) // args.gradient_accumulation_steps * args.num_train_epochs lowerCAmelCase : Dict = list(model.named_parameters() ) lowerCAmelCase : str = ["bias", "LayerNorm.bias", "LayerNorm.weight"] lowerCAmelCase : Tuple = [ { "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}, ] lowerCAmelCase : Tuple = AdamW(SCREAMING_SNAKE_CASE , lr=args.learning_rate , eps=args.adam_epsilon ) lowerCAmelCase : str = get_linear_schedule_with_warmup( SCREAMING_SNAKE_CASE , num_warmup_steps=args.warmup_steps , num_training_steps=SCREAMING_SNAKE_CASE ) if args.do_train: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Dict = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="Epoch" ): lowerCAmelCase : Optional[int] = 0 lowerCAmelCase : Union[str, Any] = 0 lowerCAmelCase : Tuple = tqdm(SCREAMING_SNAKE_CASE , desc="Training" ) for step, batch in enumerate(SCREAMING_SNAKE_CASE ): lowerCAmelCase : Tuple = tuple(t.to(SCREAMING_SNAKE_CASE ) for t in batch ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : int = batch lowerCAmelCase : Optional[int] = model(SCREAMING_SNAKE_CASE , mc_token_ids=SCREAMING_SNAKE_CASE , lm_labels=SCREAMING_SNAKE_CASE , mc_labels=SCREAMING_SNAKE_CASE ) lowerCAmelCase : int = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() lowerCAmelCase : Optional[Any] = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 lowerCAmelCase : int = "Training loss: {:.2e} lr: {:.2e}".format(SCREAMING_SNAKE_CASE , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer lowerCAmelCase : Optional[int] = model.module if hasattr(SCREAMING_SNAKE_CASE , "module" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` lowerCAmelCase : Any = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE ) torch.save(model_to_save.state_dict() , SCREAMING_SNAKE_CASE ) model_to_save.config.to_json_file(SCREAMING_SNAKE_CASE ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned lowerCAmelCase : List[str] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) lowerCAmelCase : Dict = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(SCREAMING_SNAKE_CASE ) if args.do_eval: model.eval() lowerCAmelCase , lowerCAmelCase : Optional[int] = 0, 0 lowerCAmelCase , lowerCAmelCase : Any = 0, 0 for batch in tqdm(SCREAMING_SNAKE_CASE , desc="Evaluating" ): lowerCAmelCase : List[Any] = tuple(t.to(SCREAMING_SNAKE_CASE ) for t in batch ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Tuple = batch with torch.no_grad(): lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Optional[Any] = model( SCREAMING_SNAKE_CASE , mc_token_ids=SCREAMING_SNAKE_CASE , lm_labels=SCREAMING_SNAKE_CASE , mc_labels=SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = mc_logits.detach().cpu().numpy() lowerCAmelCase : List[str] = mc_labels.to("cpu" ).numpy() lowerCAmelCase : Any = accuracy(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 lowerCAmelCase : List[Any] = eval_loss / nb_eval_steps lowerCAmelCase : List[Any] = eval_accuracy / nb_eval_examples lowerCAmelCase : Tuple = tr_loss / nb_tr_steps if args.do_train else None lowerCAmelCase : Any = {"eval_loss": eval_loss, "eval_accuracy": eval_accuracy, "train_loss": train_loss} lowerCAmelCase : List[str] = os.path.join(args.output_dir , "eval_results.txt" ) with open(SCREAMING_SNAKE_CASE , "w" ) as writer: logger.info("***** Eval results *****" ) for key in sorted(result.keys() ): logger.info(" %s = %s" , SCREAMING_SNAKE_CASE , str(result[key] ) ) writer.write("%s = %s\n" % (key, str(result[key] )) ) if __name__ == "__main__": main()
681
0
"""simple docstring""" from __future__ import annotations from typing import Any class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ = 6 ): """simple docstring""" lowerCAmelCase : List[Any] = None lowerCAmelCase : Tuple = None self.create_linked_list(UpperCamelCase__ ) def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = Node() lowerCAmelCase : List[Any] = current_node lowerCAmelCase : int = current_node lowerCAmelCase : List[Any] = current_node for _ in range(1 , UpperCamelCase__ ): lowerCAmelCase : str = Node() lowerCAmelCase : Union[str, Any] = current_node lowerCAmelCase : Tuple = previous_node lowerCAmelCase : Optional[int] = current_node lowerCAmelCase : Union[str, Any] = self.front lowerCAmelCase : Optional[Any] = previous_node def lowercase__ ( self ): """simple docstring""" return ( self.front == self.rear and self.front is not None and self.front.data is None ) def lowercase__ ( self ): """simple docstring""" self.check_can_perform_operation() return self.front.data if self.front else None def lowercase__ ( self , snake_case__ ): """simple docstring""" if self.rear is None: return self.check_is_full() if not self.is_empty(): lowerCAmelCase : Optional[int] = self.rear.next if self.rear: lowerCAmelCase : Optional[int] = data def lowercase__ ( self ): """simple docstring""" self.check_can_perform_operation() if self.rear is None or self.front is None: return None if self.front == self.rear: lowerCAmelCase : Optional[Any] = self.front.data lowerCAmelCase : Union[str, Any] = None return data lowerCAmelCase : Any = self.front lowerCAmelCase : Tuple = old_front.next lowerCAmelCase : Tuple = old_front.data lowerCAmelCase : List[str] = None return data def lowercase__ ( self ): """simple docstring""" if self.is_empty(): raise Exception("Empty Queue" ) def lowercase__ ( self ): """simple docstring""" if self.rear and self.rear.next == self.front: raise Exception("Full Queue" ) class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self ): """simple docstring""" lowerCAmelCase : int = None lowerCAmelCase : str = None lowerCAmelCase : int = None if __name__ == "__main__": import doctest doctest.testmod()
710
"""simple docstring""" from typing import List, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''huggingface/informer-tourism-monthly''': ( '''https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json''' ), # See all Informer models at https://huggingface.co/models?filter=informer } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Optional[Any] ="informer" a : int ={ "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", "num_hidden_layers": "encoder_layers", } def __init__( self , snake_case__ = None , snake_case__ = None , snake_case__ = "student_t" , snake_case__ = "nll" , snake_case__ = 1 , snake_case__ = None , snake_case__ = "mean" , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = None , snake_case__ = None , snake_case__ = 64 , snake_case__ = 32 , snake_case__ = 32 , snake_case__ = 2 , snake_case__ = 2 , snake_case__ = 2 , snake_case__ = 2 , snake_case__ = True , snake_case__ = "gelu" , snake_case__ = 0.05 , snake_case__ = 0.1 , snake_case__ = 0.1 , snake_case__ = 0.1 , snake_case__ = 0.1 , snake_case__ = 100 , snake_case__ = 0.02 , snake_case__=True , snake_case__ = "prob" , snake_case__ = 5 , snake_case__ = True , **snake_case__ , ): """simple docstring""" lowerCAmelCase : Union[str, Any] = prediction_length lowerCAmelCase : Union[str, Any] = context_length or prediction_length lowerCAmelCase : List[Any] = distribution_output lowerCAmelCase : Optional[int] = loss lowerCAmelCase : Optional[int] = input_size lowerCAmelCase : str = num_time_features lowerCAmelCase : Any = lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7] lowerCAmelCase : Dict = scaling lowerCAmelCase : List[str] = num_dynamic_real_features lowerCAmelCase : Dict = num_static_real_features lowerCAmelCase : Dict = num_static_categorical_features # set cardinality if cardinality and num_static_categorical_features > 0: if len(snake_case__ ) != num_static_categorical_features: raise ValueError( "The cardinality should be a list of the same length as `num_static_categorical_features`" ) lowerCAmelCase : List[str] = cardinality else: lowerCAmelCase : Optional[Any] = [0] # set embedding_dimension if embedding_dimension and num_static_categorical_features > 0: if len(snake_case__ ) != num_static_categorical_features: raise ValueError( "The embedding dimension should be a list of the same length as `num_static_categorical_features`" ) lowerCAmelCase : List[Any] = embedding_dimension else: lowerCAmelCase : Dict = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] lowerCAmelCase : List[Any] = num_parallel_samples # Transformer architecture configuration lowerCAmelCase : Any = input_size * len(self.lags_sequence ) + self._number_of_features lowerCAmelCase : str = d_model lowerCAmelCase : List[str] = encoder_attention_heads lowerCAmelCase : int = decoder_attention_heads lowerCAmelCase : Optional[Any] = encoder_ffn_dim lowerCAmelCase : Dict = decoder_ffn_dim lowerCAmelCase : int = encoder_layers lowerCAmelCase : Union[str, Any] = decoder_layers lowerCAmelCase : Tuple = dropout lowerCAmelCase : List[Any] = attention_dropout lowerCAmelCase : int = activation_dropout lowerCAmelCase : Union[str, Any] = encoder_layerdrop lowerCAmelCase : int = decoder_layerdrop lowerCAmelCase : Optional[int] = activation_function lowerCAmelCase : int = init_std lowerCAmelCase : Optional[Any] = use_cache # Informer lowerCAmelCase : Dict = attention_type lowerCAmelCase : Any = sampling_factor lowerCAmelCase : Optional[int] = distil super().__init__(is_encoder_decoder=snake_case__ , **snake_case__ ) @property def lowercase__ ( self ): """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 )
681
0
"""simple docstring""" import argparse from tax import checkpoints from transformers import AutoConfig, FlaxAutoModelForSeqaSeqLM def a__ ( SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : int ): '''simple docstring''' lowerCAmelCase : int = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = FlaxAutoModelForSeqaSeqLM.from_config(config=SCREAMING_SNAKE_CASE ) lowerCAmelCase : int = checkpoints.load_tax_checkpoint(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = 'wi_0' in tax_model['target']['encoder']['layers_0']['mlp'] if config.model_type == "t5": lowerCAmelCase : List[Any] = 'SelfAttention' if config.model_type == "longt5" and config.encoder_attention_type == "local": lowerCAmelCase : List[str] = 'LocalSelfAttention' elif config.model_type == "longt5" and config.encoder_attention_type == "transient-global": lowerCAmelCase : Any = 'TransientGlobalSelfAttention' else: raise ValueError( "Given config is expected to have `model_type=\'t5\'`, or `model_type=\'longt5` with `encoder_attention_type`" " attribute with a value from [\'local\', \'transient-global]." ) # Encoder for layer_index in range(config.num_layers ): lowerCAmelCase : int = f"""layers_{str(SCREAMING_SNAKE_CASE )}""" # Self-Attention lowerCAmelCase : Tuple = tax_model['target']['encoder'][layer_name]['attention']['key']['kernel'] lowerCAmelCase : Tuple = tax_model['target']['encoder'][layer_name]['attention']['out']['kernel'] lowerCAmelCase : List[str] = tax_model['target']['encoder'][layer_name]['attention']['query']['kernel'] lowerCAmelCase : List[Any] = tax_model['target']['encoder'][layer_name]['attention']['value']['kernel'] # Global input layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": lowerCAmelCase : Dict = tax_model['target']['encoder'][layer_name]['attention']['T5LayerNorm_0']['scale'] # Layer Normalization lowerCAmelCase : str = tax_model['target']['encoder'][layer_name]['pre_attention_layer_norm']['scale'] if split_mlp_wi: lowerCAmelCase : str = tax_model['target']['encoder'][layer_name]['mlp']['wi_0']['kernel'] lowerCAmelCase : int = tax_model['target']['encoder'][layer_name]['mlp']['wi_1']['kernel'] else: lowerCAmelCase : int = tax_model['target']['encoder'][layer_name]['mlp']['wi']['kernel'] lowerCAmelCase : Optional[int] = tax_model['target']['encoder'][layer_name]['mlp']['wo']['kernel'] # Layer Normalization lowerCAmelCase : Any = tax_model['target']['encoder'][layer_name]['pre_mlp_layer_norm']['scale'] # Assigning lowerCAmelCase : str = flax_model.params['encoder']['block'][str(SCREAMING_SNAKE_CASE )]['layer'] lowerCAmelCase : Dict = tax_attention_key lowerCAmelCase : List[str] = tax_attention_out lowerCAmelCase : List[str] = tax_attention_query lowerCAmelCase : Optional[Any] = tax_attention_value lowerCAmelCase : Dict = tax_attention_layer_norm # Global input layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": lowerCAmelCase : str = tax_global_layer_norm if split_mlp_wi: lowerCAmelCase : Union[str, Any] = tax_mlp_wi_a lowerCAmelCase : Dict = tax_mlp_wi_a else: lowerCAmelCase : Optional[Any] = tax_mlp_wi lowerCAmelCase : List[Any] = tax_mlp_wo lowerCAmelCase : Union[str, Any] = tax_mlp_layer_norm lowerCAmelCase : int = flax_model_encoder_layer_block # Only for layer 0: lowerCAmelCase : int = tax_model['target']['encoder']['relpos_bias']['rel_embedding'].T lowerCAmelCase : Tuple = tax_encoder_rel_embedding # Side/global relative position_bias + layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": lowerCAmelCase : Tuple = tax_model['target']['encoder']['side_relpos_bias']['rel_embedding'].T lowerCAmelCase : Tuple = tax_encoder_global_rel_embedding # Assigning lowerCAmelCase : List[str] = tax_model['target']['encoder']['encoder_norm']['scale'] lowerCAmelCase : Union[str, Any] = tax_encoder_norm # Decoder for layer_index in range(config.num_layers ): lowerCAmelCase : List[str] = f"""layers_{str(SCREAMING_SNAKE_CASE )}""" # Self-Attention lowerCAmelCase : Union[str, Any] = tax_model['target']['decoder'][layer_name]['self_attention']['key']['kernel'] lowerCAmelCase : int = tax_model['target']['decoder'][layer_name]['self_attention']['out']['kernel'] lowerCAmelCase : Any = tax_model['target']['decoder'][layer_name]['self_attention']['query']['kernel'] lowerCAmelCase : int = tax_model['target']['decoder'][layer_name]['self_attention']['value']['kernel'] # Layer Normalization lowerCAmelCase : Union[str, Any] = tax_model['target']['decoder'][layer_name]['pre_self_attention_layer_norm'][ 'scale' ] # Encoder-Decoder-Attention lowerCAmelCase : Optional[Any] = tax_model['target']['decoder'][layer_name]['encoder_decoder_attention'] lowerCAmelCase : Dict = tax_enc_dec_attention_module['key']['kernel'] lowerCAmelCase : Dict = tax_enc_dec_attention_module['out']['kernel'] lowerCAmelCase : Optional[Any] = tax_enc_dec_attention_module['query']['kernel'] lowerCAmelCase : Tuple = tax_enc_dec_attention_module['value']['kernel'] # Layer Normalization lowerCAmelCase : List[Any] = tax_model['target']['decoder'][layer_name]['pre_cross_attention_layer_norm']['scale'] # MLP if split_mlp_wi: lowerCAmelCase : str = tax_model['target']['decoder'][layer_name]['mlp']['wi_0']['kernel'] lowerCAmelCase : Tuple = tax_model['target']['decoder'][layer_name]['mlp']['wi_1']['kernel'] else: lowerCAmelCase : Optional[Any] = tax_model['target']['decoder'][layer_name]['mlp']['wi']['kernel'] lowerCAmelCase : Optional[Any] = tax_model['target']['decoder'][layer_name]['mlp']['wo']['kernel'] # Layer Normalization lowerCAmelCase : Union[str, Any] = tax_model['target']['decoder'][layer_name]['pre_mlp_layer_norm']['scale'] # Assigning lowerCAmelCase : Union[str, Any] = flax_model.params['decoder']['block'][str(SCREAMING_SNAKE_CASE )]['layer'] lowerCAmelCase : Any = tax_attention_key lowerCAmelCase : str = tax_attention_out lowerCAmelCase : Any = tax_attention_query lowerCAmelCase : List[Any] = tax_attention_value lowerCAmelCase : Tuple = tax_pre_attention_layer_norm lowerCAmelCase : List[Any] = tax_enc_dec_attention_key lowerCAmelCase : Optional[int] = tax_enc_dec_attention_out lowerCAmelCase : Union[str, Any] = tax_enc_dec_attention_query lowerCAmelCase : Union[str, Any] = tax_enc_dec_attention_value lowerCAmelCase : List[Any] = tax_cross_layer_norm if split_mlp_wi: lowerCAmelCase : List[Any] = tax_mlp_wi_a lowerCAmelCase : Optional[int] = tax_mlp_wi_a else: lowerCAmelCase : Union[str, Any] = tax_mlp_wi lowerCAmelCase : Optional[int] = tax_mlp_wo lowerCAmelCase : int = txa_mlp_layer_norm lowerCAmelCase : Optional[Any] = flax_model_decoder_layer_block # Decoder Normalization lowerCAmelCase : Tuple = tax_model['target']['decoder']['decoder_norm']['scale'] lowerCAmelCase : List[Any] = txa_decoder_norm # Only for layer 0: lowerCAmelCase : Optional[Any] = tax_model['target']['decoder']['relpos_bias']['rel_embedding'].T lowerCAmelCase : Dict = tax_decoder_rel_embedding # Token Embeddings lowerCAmelCase : str = tax_model['target']['token_embedder']['embedding'] lowerCAmelCase : Tuple = txa_token_embeddings # LM Head (only in v1.1 and LongT5 checkpoints) if "logits_dense" in tax_model["target"]["decoder"]: lowerCAmelCase : List[Any] = tax_model['target']['decoder']['logits_dense']['kernel'] flax_model.save_pretrained(SCREAMING_SNAKE_CASE ) print("T5X Model was sucessfully converted!" ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--t5x_checkpoint_path''', default=None, type=str, required=True, help='''Path the T5X checkpoint.''' ) parser.add_argument('''--config_name''', default=None, type=str, required=True, help='''Config name of LongT5/T5 model.''') parser.add_argument( '''--flax_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output FLAX model.''' ) lowerCAmelCase__ = parser.parse_args() convert_tax_checkpoint_to_flax(args.tax_checkpoint_path, args.config_name, args.flax_dump_folder_path)
711
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' if num < 0: return False lowerCAmelCase : int = num lowerCAmelCase : int = 0 while num > 0: lowerCAmelCase : Dict = rev_num * 1_0 + (num % 1_0) num //= 1_0 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
681
0
from math import acos, sin from typing import List, Tuple, Union import numpy as np import torch from PIL import Image from ...models import AutoencoderKL, UNetaDConditionModel from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import randn_tensor from ..pipeline_utils import AudioPipelineOutput, BaseOutput, DiffusionPipeline, ImagePipelineOutput from .mel import Mel class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Union[str, Any] =["vqvae"] def __init__( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" super().__init__() self.register_modules(unet=__a , scheduler=__a , mel=__a , vqvae=__a ) def lowercase__ ( self ): """simple docstring""" return 50 if isinstance(self.scheduler , __a ) else 1_000 @torch.no_grad() def __call__( self , snake_case__ = 1 , snake_case__ = None , snake_case__ = None , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = None , snake_case__ = None , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = None , snake_case__ = 0 , snake_case__ = None , snake_case__ = None , snake_case__=True , ): """simple docstring""" lowerCAmelCase : Any = steps or self.get_default_steps() self.scheduler.set_timesteps(__a ) lowerCAmelCase : Optional[int] = step_generator or generator # For backwards compatibility if type(self.unet.config.sample_size ) == int: lowerCAmelCase : str = (self.unet.config.sample_size, self.unet.config.sample_size) if noise is None: lowerCAmelCase : Any = randn_tensor( ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size[0], self.unet.config.sample_size[1], ) , generator=__a , device=self.device , ) lowerCAmelCase : List[str] = noise lowerCAmelCase : Any = None if audio_file is not None or raw_audio is not None: self.mel.load_audio(__a , __a ) lowerCAmelCase : List[str] = self.mel.audio_slice_to_image(__a ) lowerCAmelCase : Optional[int] = np.frombuffer(input_image.tobytes() , dtype="uint8" ).reshape( (input_image.height, input_image.width) ) lowerCAmelCase : Dict = (input_image / 255) * 2 - 1 lowerCAmelCase : Dict = torch.tensor(input_image[np.newaxis, :, :] , dtype=torch.float ).to(self.device ) if self.vqvae is not None: lowerCAmelCase : List[str] = self.vqvae.encode(torch.unsqueeze(__a , 0 ) ).latent_dist.sample( generator=__a )[0] lowerCAmelCase : str = self.vqvae.config.scaling_factor * input_images if start_step > 0: lowerCAmelCase : int = self.scheduler.add_noise(__a , __a , self.scheduler.timesteps[start_step - 1] ) lowerCAmelCase : int = ( self.unet.config.sample_size[1] * self.mel.get_sample_rate() / self.mel.x_res / self.mel.hop_length ) lowerCAmelCase : Optional[Any] = int(mask_start_secs * pixels_per_second ) lowerCAmelCase : Union[str, Any] = int(mask_end_secs * pixels_per_second ) lowerCAmelCase : str = self.scheduler.add_noise(__a , __a , torch.tensor(self.scheduler.timesteps[start_step:] ) ) for step, t in enumerate(self.progress_bar(self.scheduler.timesteps[start_step:] ) ): if isinstance(self.unet , __a ): lowerCAmelCase : Tuple = self.unet(__a , __a , __a )["sample"] else: lowerCAmelCase : Optional[int] = self.unet(__a , __a )["sample"] if isinstance(self.scheduler , __a ): lowerCAmelCase : str = self.scheduler.step( model_output=__a , timestep=__a , sample=__a , eta=__a , generator=__a , )["prev_sample"] else: lowerCAmelCase : int = self.scheduler.step( model_output=__a , timestep=__a , sample=__a , generator=__a , )["prev_sample"] if mask is not None: if mask_start > 0: lowerCAmelCase : Union[str, Any] = mask[:, step, :, :mask_start] if mask_end > 0: lowerCAmelCase : List[Any] = mask[:, step, :, -mask_end:] if self.vqvae is not None: # 0.18215 was scaling factor used in training to ensure unit variance lowerCAmelCase : int = 1 / self.vqvae.config.scaling_factor * images lowerCAmelCase : int = self.vqvae.decode(__a )["sample"] lowerCAmelCase : Dict = (images / 2 + 0.5).clamp(0 , 1 ) lowerCAmelCase : Optional[int] = images.cpu().permute(0 , 2 , 3 , 1 ).numpy() lowerCAmelCase : int = (images * 255).round().astype("uint8" ) lowerCAmelCase : Optional[int] = list( (Image.fromarray(_[:, :, 0] ) for _ in images) if images.shape[3] == 1 else (Image.fromarray(__a , mode="RGB" ).convert("L" ) for _ in images) ) lowerCAmelCase : List[str] = [self.mel.image_to_audio(__a ) for _ in images] if not return_dict: return images, (self.mel.get_sample_rate(), audios) return BaseOutput(**AudioPipelineOutput(np.array(__a )[:, np.newaxis, :] ) , **ImagePipelineOutput(__a ) ) @torch.no_grad() def lowercase__ ( self , snake_case__ , snake_case__ = 50 ): """simple docstring""" assert isinstance(self.scheduler , __a ) self.scheduler.set_timesteps(__a ) lowerCAmelCase : List[str] = np.array( [np.frombuffer(image.tobytes() , dtype="uint8" ).reshape((1, image.height, image.width) ) for image in images] ) lowerCAmelCase : Dict = (sample / 255) * 2 - 1 lowerCAmelCase : Dict = torch.Tensor(__a ).to(self.device ) for t in self.progress_bar(torch.flip(self.scheduler.timesteps , (0,) ) ): lowerCAmelCase : List[Any] = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps lowerCAmelCase : Tuple = self.scheduler.alphas_cumprod[t] lowerCAmelCase : Dict = ( self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod ) lowerCAmelCase : Tuple = 1 - alpha_prod_t lowerCAmelCase : Dict = self.unet(__a , __a )["sample"] lowerCAmelCase : int = (1 - alpha_prod_t_prev) ** 0.5 * model_output lowerCAmelCase : Union[str, Any] = (sample - pred_sample_direction) * alpha_prod_t_prev ** (-0.5) lowerCAmelCase : Optional[Any] = sample * alpha_prod_t ** 0.5 + beta_prod_t ** 0.5 * model_output return sample @staticmethod def lowercase__ ( snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Optional[int] = acos(torch.dot(torch.flatten(__a ) , torch.flatten(__a ) ) / torch.norm(__a ) / torch.norm(__a ) ) return sin((1 - alpha) * theta ) * xa / sin(__a ) + sin(alpha * theta ) * xa / sin(__a )
712
"""simple docstring""" import os import re import shutil import sys import tempfile import unittest import black lowerCAmelCase__ = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, '''utils''')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If DDPMSchedulerOutput is changed in scheduling_ddpm.py, this code needs to be manually updated. lowerCAmelCase__ = ''' \""" Output class for the scheduler\'s step function output. Args: prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the denoising loop. pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): The predicted denoised sample (x_{0}) based on the model output from the current timestep. `pred_original_sample` can be used to preview progress or for guidance. \""" prev_sample: torch.FloatTensor pred_original_sample: Optional[torch.FloatTensor] = None ''' class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = tempfile.mkdtemp() os.makedirs(os.path.join(self.diffusers_dir , "schedulers/" ) ) lowerCAmelCase : List[str] = self.diffusers_dir shutil.copy( os.path.join(snake_case__ , "src/diffusers/schedulers/scheduling_ddpm.py" ) , os.path.join(self.diffusers_dir , "schedulers/scheduling_ddpm.py" ) , ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = "src/diffusers" shutil.rmtree(self.diffusers_dir ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__=None ): """simple docstring""" lowerCAmelCase : Union[str, Any] = comment + f"""\nclass {class_name}(nn.Module):\n""" + class_code if overwrite_result is not None: lowerCAmelCase : str = comment + f"""\nclass {class_name}(nn.Module):\n""" + overwrite_result lowerCAmelCase : int = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) lowerCAmelCase : int = black.format_str(snake_case__ , mode=snake_case__ ) lowerCAmelCase : Dict = os.path.join(self.diffusers_dir , "new_code.py" ) with open(snake_case__ , "w" , newline="\n" ) as f: f.write(snake_case__ ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(snake_case__ ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=snake_case__ ) with open(snake_case__ , "r" ) as f: self.assertTrue(f.read() , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = check_copies.find_code_in_diffusers("schedulers.scheduling_ddpm.DDPMSchedulerOutput" ) self.assertEqual(snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput" , "DDPMSchedulerOutput" , REFERENCE_CODE + "\n" , ) # With no empty line at the end self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput" , "DDPMSchedulerOutput" , snake_case__ , ) # Copy consistency with rename self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test" , "TestSchedulerOutput" , re.sub("DDPM" , "Test" , snake_case__ ) , ) # Copy consistency with a really long name lowerCAmelCase : Union[str, Any] = "TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason" self.check_copy_consistency( f"""# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}""" , f"""{long_class_name}SchedulerOutput""" , re.sub("Bert" , snake_case__ , snake_case__ ) , ) # Copy consistency with overwrite self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test" , "TestSchedulerOutput" , snake_case__ , overwrite_result=re.sub("DDPM" , "Test" , snake_case__ ) , )
681
0
"""simple docstring""" import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class SCREAMING_SNAKE_CASE__ ( lowercase_ ): """simple docstring""" a : Union[str, Any] =(DEISMultistepScheduler,) a : List[Any] =(("num_inference_steps", 25),) def lowercase__ ( self , **snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = { """num_train_timesteps""": 1_000, """beta_start""": 0.0001, """beta_end""": 0.02, """beta_schedule""": """linear""", """solver_order""": 2, } config.update(**lowerCamelCase_ ) return config def lowercase__ ( self , snake_case__=0 , **snake_case__ ): """simple docstring""" lowerCAmelCase : Any = dict(self.forward_default_kwargs ) lowerCAmelCase : List[str] = kwargs.pop("num_inference_steps" , lowerCamelCase_ ) lowerCAmelCase : Dict = self.dummy_sample lowerCAmelCase : Any = 0.1 * sample lowerCAmelCase : str = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: lowerCAmelCase : int = self.get_scheduler_config(**lowerCamelCase_ ) lowerCAmelCase : Optional[int] = scheduler_class(**lowerCamelCase_ ) scheduler.set_timesteps(lowerCamelCase_ ) # copy over dummy past residuals lowerCAmelCase : Optional[Any] = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowerCamelCase_ ) lowerCAmelCase : Optional[int] = scheduler_class.from_pretrained(lowerCamelCase_ ) new_scheduler.set_timesteps(lowerCamelCase_ ) # copy over dummy past residuals lowerCAmelCase : List[str] = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase : str = sample, sample for t in range(lowerCamelCase_ , time_step + scheduler.config.solver_order + 1 ): lowerCAmelCase : Tuple = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample lowerCAmelCase : Optional[int] = new_scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self , snake_case__=0 , **snake_case__ ): """simple docstring""" lowerCAmelCase : Tuple = dict(self.forward_default_kwargs ) lowerCAmelCase : Optional[Any] = kwargs.pop("num_inference_steps" , lowerCamelCase_ ) lowerCAmelCase : Optional[int] = self.dummy_sample lowerCAmelCase : Optional[Any] = 0.1 * sample lowerCAmelCase : Optional[Any] = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: lowerCAmelCase : str = self.get_scheduler_config() lowerCAmelCase : Any = scheduler_class(**lowerCamelCase_ ) scheduler.set_timesteps(lowerCamelCase_ ) # copy over dummy past residuals (must be after setting timesteps) lowerCAmelCase : List[str] = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowerCamelCase_ ) lowerCAmelCase : List[str] = scheduler_class.from_pretrained(lowerCamelCase_ ) # copy over dummy past residuals new_scheduler.set_timesteps(lowerCamelCase_ ) # copy over dummy past residual (must be after setting timesteps) lowerCAmelCase : Dict = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase : List[Any] = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample lowerCAmelCase : Tuple = new_scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def lowercase__ ( self , snake_case__=None , **snake_case__ ): """simple docstring""" if scheduler is None: lowerCAmelCase : Optional[int] = self.scheduler_classes[0] lowerCAmelCase : Optional[Any] = self.get_scheduler_config(**lowerCamelCase_ ) lowerCAmelCase : int = scheduler_class(**lowerCamelCase_ ) lowerCAmelCase : List[Any] = self.scheduler_classes[0] lowerCAmelCase : str = self.get_scheduler_config(**lowerCamelCase_ ) lowerCAmelCase : str = scheduler_class(**lowerCamelCase_ ) lowerCAmelCase : Tuple = 10 lowerCAmelCase : Any = self.dummy_model() lowerCAmelCase : Union[str, Any] = self.dummy_sample_deter scheduler.set_timesteps(lowerCamelCase_ ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase : Dict = model(lowerCamelCase_ , lowerCamelCase_ ) lowerCAmelCase : Optional[int] = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ).prev_sample return sample def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Tuple = dict(self.forward_default_kwargs ) lowerCAmelCase : int = kwargs.pop("num_inference_steps" , lowerCamelCase_ ) for scheduler_class in self.scheduler_classes: lowerCAmelCase : str = self.get_scheduler_config() lowerCAmelCase : Optional[Any] = scheduler_class(**lowerCamelCase_ ) lowerCAmelCase : Optional[int] = self.dummy_sample lowerCAmelCase : Any = 0.1 * sample if num_inference_steps is not None and hasattr(lowerCamelCase_ , "set_timesteps" ): scheduler.set_timesteps(lowerCamelCase_ ) elif num_inference_steps is not None and not hasattr(lowerCamelCase_ , "set_timesteps" ): lowerCAmelCase : str = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) lowerCAmelCase : Optional[Any] = [residual + 0.2, residual + 0.15, residual + 0.10] lowerCAmelCase : int = dummy_past_residuals[: scheduler.config.solver_order] lowerCAmelCase : int = scheduler.timesteps[5] lowerCAmelCase : int = scheduler.timesteps[6] lowerCAmelCase : List[Any] = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample lowerCAmelCase : Tuple = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = DEISMultistepScheduler(**self.get_scheduler_config() ) lowerCAmelCase : Union[str, Any] = self.full_loop(scheduler=lowerCamelCase_ ) lowerCAmelCase : List[Any] = torch.mean(torch.abs(lowerCamelCase_ ) ) assert abs(result_mean.item() - 0.23916 ) < 1e-3 lowerCAmelCase : List[str] = DPMSolverSinglestepScheduler.from_config(scheduler.config ) lowerCAmelCase : Tuple = DPMSolverMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase : Dict = UniPCMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase : int = DEISMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase : Optional[Any] = self.full_loop(scheduler=lowerCamelCase_ ) lowerCAmelCase : List[str] = torch.mean(torch.abs(lowerCamelCase_ ) ) assert abs(result_mean.item() - 0.23916 ) < 1e-3 def lowercase__ ( self ): """simple docstring""" for timesteps in [25, 50, 100, 999, 1_000]: self.check_over_configs(num_train_timesteps=lowerCamelCase_ ) def lowercase__ ( self ): """simple docstring""" self.check_over_configs(thresholding=lowerCamelCase_ ) for order in [1, 2, 3]: for solver_type in ["logrho"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=lowerCamelCase_ , prediction_type=lowerCamelCase_ , sample_max_value=lowerCamelCase_ , algorithm_type="deis" , solver_order=lowerCamelCase_ , solver_type=lowerCamelCase_ , ) def lowercase__ ( self ): """simple docstring""" for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowerCamelCase_ ) def lowercase__ ( self ): """simple docstring""" for algorithm_type in ["deis"]: for solver_type in ["logrho"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=lowerCamelCase_ , solver_type=lowerCamelCase_ , prediction_type=lowerCamelCase_ , algorithm_type=lowerCamelCase_ , ) lowerCAmelCase : Dict = self.full_loop( solver_order=lowerCamelCase_ , solver_type=lowerCamelCase_ , prediction_type=lowerCamelCase_ , algorithm_type=lowerCamelCase_ , ) assert not torch.isnan(lowerCamelCase_ ).any(), "Samples have nan numbers" def lowercase__ ( self ): """simple docstring""" self.check_over_configs(lower_order_final=lowerCamelCase_ ) self.check_over_configs(lower_order_final=lowerCamelCase_ ) def lowercase__ ( self ): """simple docstring""" for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1_000]: self.check_over_forward(num_inference_steps=lowerCamelCase_ , time_step=0 ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[Any] = self.full_loop() lowerCAmelCase : List[Any] = torch.mean(torch.abs(lowerCamelCase_ ) ) assert abs(result_mean.item() - 0.23916 ) < 1e-3 def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = self.full_loop(prediction_type="v_prediction" ) lowerCAmelCase : Any = torch.mean(torch.abs(lowerCamelCase_ ) ) assert abs(result_mean.item() - 0.091 ) < 1e-3 def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = self.scheduler_classes[0] lowerCAmelCase : List[str] = self.get_scheduler_config(thresholding=lowerCamelCase_ , dynamic_thresholding_ratio=0 ) lowerCAmelCase : Dict = scheduler_class(**lowerCamelCase_ ) lowerCAmelCase : List[str] = 10 lowerCAmelCase : Optional[int] = self.dummy_model() lowerCAmelCase : Dict = self.dummy_sample_deter.half() scheduler.set_timesteps(lowerCamelCase_ ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase : List[Any] = model(lowerCamelCase_ , lowerCamelCase_ ) lowerCAmelCase : List[Any] = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ).prev_sample assert sample.dtype == torch.floataa
713
"""simple docstring""" import re from flax.core.frozen_dict import freeze from flax.traverse_util import flatten_dict, unflatten_dict from jax.experimental import PartitionSpec as P # Sentinels lowerCAmelCase__ = object() # For specifying empty leaf dict `{}` lowerCAmelCase__ = object() def a__ ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' lowerCAmelCase : Optional[int] = tuple((re.compile(x + "$" ) for x in qs) ) for i in range(len(SCREAMING_SNAKE_CASE ) - len(SCREAMING_SNAKE_CASE ) + 1 ): lowerCAmelCase : int = [x.match(SCREAMING_SNAKE_CASE ) for x, y in zip(SCREAMING_SNAKE_CASE , ks[i:] )] if matches and all(SCREAMING_SNAKE_CASE ): return True return False def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' def replace(SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Dict ): for rule, replacement in rules: if _match(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return replacement return val return replace def a__ ( ): '''simple docstring''' return [ # embeddings (("transformer", "wpe", "embedding"), P("mp" , SCREAMING_SNAKE_CASE )), (("transformer", "wte", "embedding"), P("mp" , SCREAMING_SNAKE_CASE )), # atention (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(SCREAMING_SNAKE_CASE , "mp" )), (("attention", "out_proj", "kernel"), P("mp" , SCREAMING_SNAKE_CASE )), (("attention", "out_proj", "bias"), None), # mlp (("mlp", "c_fc", "kernel"), P(SCREAMING_SNAKE_CASE , "mp" )), (("mlp", "c_fc", "bias"), P("mp" )), (("mlp", "c_proj", "kernel"), P("mp" , SCREAMING_SNAKE_CASE )), (("mlp", "c_proj", "bias"), None), # layer norms ((r"ln_\d+", "bias"), None), ((r"\d+", r"ln_\d+", "scale"), None), (("ln_f", "bias"), None), (("ln_f", "scale"), None), ] def a__ ( SCREAMING_SNAKE_CASE : Union[str, Any] ): '''simple docstring''' lowerCAmelCase : Any = _get_partition_rules() lowerCAmelCase : Tuple = _replacement_rules(SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = {k: _unmatched for k in flatten_dict(SCREAMING_SNAKE_CASE )} lowerCAmelCase : List[Any] = {k: replace(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) for k, v in initd.items()} assert _unmatched not in result.values(), "Incomplete partition spec." return freeze(unflatten_dict(SCREAMING_SNAKE_CASE ) )
681
0
"""simple docstring""" from sklearn.metrics import matthews_corrcoef import datasets lowerCAmelCase__ = '''\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction. The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n''' lowerCAmelCase__ = '''\nArgs:\n predictions (list of int): Predicted labels, as returned by a model.\n references (list of int): Ground truth labels.\n sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n Example 1, a basic example with only predictions and references as inputs:\n >>> matthews_metric = datasets.load_metric("matthews_correlation")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3])\n >>> print(round(results[\'matthews_correlation\'], 2))\n 0.54\n\n Example 2, the same example as above, but also including sample weights:\n >>> matthews_metric = datasets.load_metric("matthews_correlation")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 3, 1, 1, 1, 2])\n >>> print(round(results[\'matthews_correlation\'], 2))\n 0.1\n\n Example 3, the same example as above, but with sample weights that cause a negative correlation:\n >>> matthews_metric = datasets.load_metric("matthews_correlation")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 1, 0, 0, 0, 1])\n >>> print(round(results[\'matthews_correlation\'], 2))\n -0.25\n''' lowerCAmelCase__ = '''\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE__ ( datasets.Metric ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("int32" ), "references": datasets.Value("int32" ), } ) , reference_urls=[ "https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html" ] , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__=None ): """simple docstring""" return { "matthews_correlation": float(matthews_corrcoef(UpperCamelCase_ , UpperCamelCase_ , sample_weight=UpperCamelCase_ ) ), }
714
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : int = 1_0 , SCREAMING_SNAKE_CASE : int = 2_2 ): '''simple docstring''' lowerCAmelCase : Dict = range(1 , SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = range(1 , SCREAMING_SNAKE_CASE ) return sum( 1 for power in powers for base in bases if len(str(base**power ) ) == power ) if __name__ == "__main__": print(F"{solution(10, 22) = }")
681
0
"""simple docstring""" from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) # TODO Update this lowerCAmelCase__ = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class SCREAMING_SNAKE_CASE__ ( _UpperCamelCase ): """simple docstring""" a : Union[str, Any] ="esm" def __init__( self , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3_072 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=1_026 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__="absolute" , snake_case__=True , snake_case__=None , snake_case__=False , snake_case__=False , snake_case__=None , snake_case__=None , **snake_case__ , ): """simple docstring""" super().__init__(pad_token_id=snake_case__ , mask_token_id=snake_case__ , **snake_case__ ) lowerCAmelCase : List[str] = vocab_size lowerCAmelCase : int = hidden_size lowerCAmelCase : Union[str, Any] = num_hidden_layers lowerCAmelCase : List[str] = num_attention_heads lowerCAmelCase : List[str] = intermediate_size lowerCAmelCase : Union[str, Any] = hidden_dropout_prob lowerCAmelCase : List[str] = attention_probs_dropout_prob lowerCAmelCase : List[str] = max_position_embeddings lowerCAmelCase : List[str] = initializer_range lowerCAmelCase : Optional[Any] = layer_norm_eps lowerCAmelCase : Optional[int] = position_embedding_type lowerCAmelCase : Optional[int] = use_cache lowerCAmelCase : Optional[int] = emb_layer_norm_before lowerCAmelCase : List[str] = token_dropout lowerCAmelCase : Optional[int] = is_folding_model if is_folding_model: if esmfold_config is None: logger.info("No esmfold_config supplied for folding model, using default values." ) lowerCAmelCase : Dict = EsmFoldConfig() elif isinstance(snake_case__ , snake_case__ ): lowerCAmelCase : Optional[int] = EsmFoldConfig(**snake_case__ ) lowerCAmelCase : Dict = esmfold_config if vocab_list is None: logger.warning("No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!" ) lowerCAmelCase : List[str] = get_default_vocab_list() else: lowerCAmelCase : List[Any] = vocab_list else: lowerCAmelCase : List[Any] = None lowerCAmelCase : List[str] = None if self.esmfold_config is not None and getattr(self.esmfold_config , "use_esm_attn_map" , snake_case__ ): raise ValueError("The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!" ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = super().to_dict() if isinstance(self.esmfold_config , snake_case__ ): lowerCAmelCase : Dict = self.esmfold_config.to_dict() return output @dataclass class SCREAMING_SNAKE_CASE__ : """simple docstring""" a : List[Any] =None a : Optional[int] =True a : int =False a : int =False a : Union[str, Any] =False a : str =0 a : Union[str, Any] =True a : Optional[int] =False a : List[Any] =1_28 a : str =None def lowercase__ ( self ): """simple docstring""" if self.trunk is None: lowerCAmelCase : Dict = TrunkConfig() elif isinstance(self.trunk , snake_case__ ): lowerCAmelCase : int = TrunkConfig(**self.trunk ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = asdict(self ) lowerCAmelCase : Any = self.trunk.to_dict() return output @dataclass class SCREAMING_SNAKE_CASE__ : """simple docstring""" a : int =48 a : int =10_24 a : Union[str, Any] =1_28 a : Optional[int] =32 a : Optional[int] =32 a : int =32 a : Optional[int] =0 a : Any =0 a : Dict =False a : List[Any] =4 a : List[Any] =1_28 a : Any =None def lowercase__ ( self ): """simple docstring""" if self.structure_module is None: lowerCAmelCase : str = StructureModuleConfig() elif isinstance(self.structure_module , snake_case__ ): lowerCAmelCase : Optional[int] = StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f"""`max_recycles` should be positive, got {self.max_recycles}.""" ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( "`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got" f""" {self.sequence_state_dim} and {self.sequence_state_dim}.""" ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( "`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got" f""" {self.pairwise_state_dim} and {self.pairwise_state_dim}.""" ) lowerCAmelCase : Union[str, Any] = self.sequence_state_dim // self.sequence_head_width lowerCAmelCase : List[Any] = self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( "`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got" f""" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.""" ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( "`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got" f""" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.""" ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f"""`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.""" ) if self.dropout >= 0.4: raise ValueError(f"""`dropout` should not be greater than 0.4, got {self.dropout}.""" ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = asdict(self ) lowerCAmelCase : Optional[int] = self.structure_module.to_dict() return output @dataclass class SCREAMING_SNAKE_CASE__ : """simple docstring""" a : int =3_84 a : str =1_28 a : Union[str, Any] =16 a : Any =1_28 a : Optional[int] =12 a : Union[str, Any] =4 a : int =8 a : Tuple =0.1 a : Any =8 a : Union[str, Any] =1 a : List[Any] =2 a : Dict =7 a : Optional[int] =10 a : Dict =1E-8 a : Optional[int] =1E5 def lowercase__ ( self ): """simple docstring""" return asdict(self ) def a__ ( ): '''simple docstring''' return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
715
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : Optional[int] = len(SCREAMING_SNAKE_CASE ) while cur > 1: # Find the maximum number in arr lowerCAmelCase : List[str] = arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi lowerCAmelCase : str = arr[mi::-1] + arr[mi + 1 : len(SCREAMING_SNAKE_CASE )] # Reverse whole list lowerCAmelCase : str = arr[cur - 1 :: -1] + arr[cur : len(SCREAMING_SNAKE_CASE )] cur -= 1 return arr if __name__ == "__main__": lowerCAmelCase__ = input('''Enter numbers separated by a comma:\n''').strip() lowerCAmelCase__ = [int(item) for item in user_input.split(''',''')] print(pancake_sort(unsorted))
681
0
"""simple docstring""" import inspect import os import sys import unittest import accelerate from accelerate.test_utils import execute_subprocess_async, require_tpu class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[Any] = inspect.getfile(accelerate.test_utils ) lowerCAmelCase : Dict = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_script.py"] ) lowerCAmelCase : List[Any] = os.path.sep.join(inspect.getfile(self.__class__ ).split(os.path.sep )[:-1] ) @require_tpu def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = f"""\n {self.test_dir}/xla_spawn.py\n --num_cores 8\n {self.test_file_path}\n """.split() lowerCAmelCase : Dict = [sys.executable] + distributed_args execute_subprocess_async(lowercase_ , env=os.environ.copy() )
716
"""simple docstring""" from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
681
0
import torch from transformers import AutoModel class SCREAMING_SNAKE_CASE__ ( torch.nn.Module ): """simple docstring""" def __init__( self , snake_case__="sayef/fsner-bert-base-uncased" ): """simple docstring""" super(lowerCAmelCase_ , self ).__init__() lowerCAmelCase : int = AutoModel.from_pretrained(lowerCAmelCase_ , return_dict=lowerCAmelCase_ ) lowerCAmelCase : Any = torch.nn.CosineSimilarity(3 , 1e-08 ) lowerCAmelCase : Optional[int] = torch.nn.Softmax(dim=1 ) def lowercase__ ( self , **snake_case__ ): """simple docstring""" return self.bert(**lowerCAmelCase_ ).last_hidden_state def lowercase__ ( self , snake_case__ ): """simple docstring""" return token_embeddings.sum(2 , keepdim=lowerCAmelCase_ ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__=1 ): """simple docstring""" return self.softmax(T * self.cos(lowerCAmelCase_ , lowerCAmelCase_ ) ) def lowercase__ ( self , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : List[str] = W_supports["sizes"].tolist() lowerCAmelCase : Tuple = W_supports["start_token_id"].item() lowerCAmelCase : Optional[int] = W_supports["end_token_id"].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] lowerCAmelCase : Optional[int] = self.BERT(**lowerCAmelCase_ ) lowerCAmelCase : Optional[int] = self.BERT(**lowerCAmelCase_ ) lowerCAmelCase : str = None lowerCAmelCase : Dict = None lowerCAmelCase : List[str] = W_supports["input_ids"] == start_token_id lowerCAmelCase : Optional[int] = W_supports["input_ids"] == end_token_id for i, size in enumerate(lowerCAmelCase_ ): if i == 0: lowerCAmelCase : Optional[int] = 0 else: lowerCAmelCase : List[Any] = support_sizes[i - 1] lowerCAmelCase : List[str] = S[s : s + size][start_token_masks[s : s + size]] lowerCAmelCase : Any = S[s : s + size][end_token_masks[s : s + size]] lowerCAmelCase : Optional[int] = torch.matmul(q[i] , s_start.T ).sum(1 ).softmax(0 ) lowerCAmelCase : str = torch.matmul(q[i] , s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: lowerCAmelCase : Dict = torch.vstack((p_starts, p_start) ) lowerCAmelCase : Any = torch.vstack((p_ends, p_end) ) else: lowerCAmelCase : Optional[Any] = p_start lowerCAmelCase : Optional[Any] = p_end return p_starts, p_ends
717
"""simple docstring""" import math def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' return math.sqrt(SCREAMING_SNAKE_CASE ) * math.sqrt(SCREAMING_SNAKE_CASE ) == num def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' lowerCAmelCase : Dict = 0 lowerCAmelCase : List[str] = n while left <= right: lowerCAmelCase : str = (left + right) // 2 if mid**2 == n: return True elif mid**2 > n: lowerCAmelCase : int = mid - 1 else: lowerCAmelCase : int = mid + 1 return False if __name__ == "__main__": import doctest doctest.testmod()
681
0
"""simple docstring""" from typing import Optional, Tuple import jax import jax.numpy as jnp from flax import linen as nn from flax.core.frozen_dict import FrozenDict from transformers import CLIPConfig, FlaxPreTrainedModel from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule def a__ ( SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Dict=1E-1_2 ): '''simple docstring''' lowerCAmelCase : Optional[Any] = jnp.divide(emb_a.T , jnp.clip(jnp.linalg.norm(SCREAMING_SNAKE_CASE , axis=1 ) , a_min=SCREAMING_SNAKE_CASE ) ).T lowerCAmelCase : Dict = jnp.divide(emb_a.T , jnp.clip(jnp.linalg.norm(SCREAMING_SNAKE_CASE , axis=1 ) , a_min=SCREAMING_SNAKE_CASE ) ).T return jnp.matmul(SCREAMING_SNAKE_CASE , norm_emb_a.T ) class SCREAMING_SNAKE_CASE__ ( nn.Module ): """simple docstring""" a : CLIPConfig a : jnp.dtype =jnp.floataa def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = FlaxCLIPVisionModule(self.config.vision_config ) lowerCAmelCase : Optional[int] = nn.Dense(self.config.projection_dim , use_bias=UpperCAmelCase__ , dtype=self.dtype ) lowerCAmelCase : Tuple = self.param("concept_embeds" , jax.nn.initializers.ones , (17, self.config.projection_dim) ) lowerCAmelCase : Union[str, Any] = self.param( "special_care_embeds" , jax.nn.initializers.ones , (3, self.config.projection_dim) ) lowerCAmelCase : Union[str, Any] = self.param("concept_embeds_weights" , jax.nn.initializers.ones , (17,) ) lowerCAmelCase : List[str] = self.param("special_care_embeds_weights" , jax.nn.initializers.ones , (3,) ) def __call__( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Optional[int] = self.vision_model(UpperCAmelCase__ )[1] lowerCAmelCase : List[Any] = self.visual_projection(UpperCAmelCase__ ) lowerCAmelCase : Any = jax_cosine_distance(UpperCAmelCase__ , self.special_care_embeds ) lowerCAmelCase : Optional[int] = jax_cosine_distance(UpperCAmelCase__ , self.concept_embeds ) # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign image inputs lowerCAmelCase : Union[str, Any] = 0.0 lowerCAmelCase : Optional[int] = special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment lowerCAmelCase : Optional[int] = jnp.round(UpperCAmelCase__ , 3 ) lowerCAmelCase : str = jnp.any(special_scores > 0 , axis=1 , keepdims=UpperCAmelCase__ ) # Use a lower threshold if an image has any special care concept lowerCAmelCase : List[str] = is_special_care * 0.01 lowerCAmelCase : str = cos_dist - self.concept_embeds_weights[None, :] + special_adjustment lowerCAmelCase : Any = jnp.round(UpperCAmelCase__ , 3 ) lowerCAmelCase : List[Any] = jnp.any(concept_scores > 0 , axis=1 ) return has_nsfw_concepts class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Tuple =CLIPConfig a : str ='clip_input' a : Tuple =FlaxStableDiffusionSafetyCheckerModule def __init__( self , snake_case__ , snake_case__ = None , snake_case__ = 0 , snake_case__ = jnp.floataa , snake_case__ = True , **snake_case__ , ): """simple docstring""" if input_shape is None: lowerCAmelCase : Optional[Any] = (1, 224, 224, 3) lowerCAmelCase : str = self.module_class(config=UpperCAmelCase__ , dtype=UpperCAmelCase__ , **UpperCAmelCase__ ) super().__init__(UpperCAmelCase__ , UpperCAmelCase__ , input_shape=UpperCAmelCase__ , seed=UpperCAmelCase__ , dtype=UpperCAmelCase__ , _do_init=_do_init ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = None ): """simple docstring""" lowerCAmelCase : Any = jax.random.normal(UpperCAmelCase__ , UpperCAmelCase__ ) lowerCAmelCase : List[str] = jax.random.split(UpperCAmelCase__ ) lowerCAmelCase : Optional[Any] = {'''params''': params_rng, '''dropout''': dropout_rng} lowerCAmelCase : Any = self.module.init(UpperCAmelCase__ , UpperCAmelCase__ )['''params'''] return random_params def __call__( self , snake_case__ , snake_case__ = None , ): """simple docstring""" lowerCAmelCase : Union[str, Any] = jnp.transpose(UpperCAmelCase__ , (0, 2, 3, 1) ) return self.module.apply( {"params": params or self.params} , jnp.array(UpperCAmelCase__ , dtype=jnp.floataa ) , rngs={} , )
718
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''google/vit-base-patch16-224''': '''https://huggingface.co/vit-base-patch16-224/resolve/main/config.json''', # See all ViT models at https://huggingface.co/models?filter=vit } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Union[str, Any] ="vit" def __init__( self , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3_072 , snake_case__="gelu" , snake_case__=0.0 , snake_case__=0.0 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__=224 , snake_case__=16 , snake_case__=3 , snake_case__=True , snake_case__=16 , **snake_case__ , ): """simple docstring""" super().__init__(**snake_case__ ) lowerCAmelCase : Optional[Any] = hidden_size lowerCAmelCase : List[Any] = num_hidden_layers lowerCAmelCase : List[Any] = num_attention_heads lowerCAmelCase : Union[str, Any] = intermediate_size lowerCAmelCase : Any = hidden_act lowerCAmelCase : Optional[Any] = hidden_dropout_prob lowerCAmelCase : Optional[Any] = attention_probs_dropout_prob lowerCAmelCase : List[str] = initializer_range lowerCAmelCase : Optional[Any] = layer_norm_eps lowerCAmelCase : Optional[int] = image_size lowerCAmelCase : Optional[Any] = patch_size lowerCAmelCase : Tuple = num_channels lowerCAmelCase : Optional[int] = qkv_bias lowerCAmelCase : str = encoder_stride class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : List[Any] =version.parse("1.11" ) @property def lowercase__ ( self ): """simple docstring""" return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowercase__ ( self ): """simple docstring""" return 1e-4
681
0
"""simple docstring""" from __future__ import annotations import pandas as pd def a__ ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Any ): '''simple docstring''' lowerCAmelCase : Dict = [0] * no_of_processes lowerCAmelCase : Optional[Any] = [0] * no_of_processes # Copy the burst time into remaining_time[] for i in range(SCREAMING_SNAKE_CASE ): lowerCAmelCase : Dict = burst_time[i] lowerCAmelCase : Optional[int] = 0 lowerCAmelCase : List[Any] = 0 lowerCAmelCase : Any = 9_9_9_9_9_9_9_9_9 lowerCAmelCase : Optional[Any] = 0 lowerCAmelCase : Optional[Any] = False # Process until all processes are completed while complete != no_of_processes: for j in range(SCREAMING_SNAKE_CASE ): if arrival_time[j] <= increment_time and remaining_time[j] > 0: if remaining_time[j] < minm: lowerCAmelCase : str = remaining_time[j] lowerCAmelCase : Dict = j lowerCAmelCase : str = True if not check: increment_time += 1 continue remaining_time[short] -= 1 lowerCAmelCase : Dict = remaining_time[short] if minm == 0: lowerCAmelCase : Dict = 9_9_9_9_9_9_9_9_9 if remaining_time[short] == 0: complete += 1 lowerCAmelCase : Optional[int] = False # Find finish time of current process lowerCAmelCase : Optional[int] = increment_time + 1 # Calculate waiting time lowerCAmelCase : Any = finish_time - arrival_time[short] lowerCAmelCase : Optional[int] = finar - burst_time[short] if waiting_time[short] < 0: lowerCAmelCase : str = 0 # Increment time increment_time += 1 return waiting_time def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : List[Any] ): '''simple docstring''' lowerCAmelCase : Optional[Any] = [0] * no_of_processes for i in range(SCREAMING_SNAKE_CASE ): lowerCAmelCase : Optional[int] = burst_time[i] + waiting_time[i] return turn_around_time def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Union[str, Any] ): '''simple docstring''' lowerCAmelCase : str = 0 lowerCAmelCase : str = 0 for i in range(SCREAMING_SNAKE_CASE ): lowerCAmelCase : Any = total_waiting_time + waiting_time[i] lowerCAmelCase : List[Any] = total_turn_around_time + turn_around_time[i] print(f"""Average waiting time = {total_waiting_time / no_of_processes:.5f}""" ) print("Average turn around time =" , total_turn_around_time / no_of_processes ) if __name__ == "__main__": print('''Enter how many process you want to analyze''') lowerCAmelCase__ = int(input()) lowerCAmelCase__ = [0] * no_of_processes lowerCAmelCase__ = [0] * no_of_processes lowerCAmelCase__ = list(range(1, no_of_processes + 1)) for i in range(no_of_processes): print('''Enter the arrival time and burst time for process:--''' + str(i + 1)) lowerCAmelCase__ = map(int, input().split()) lowerCAmelCase__ = calculate_waitingtime(arrival_time, burst_time, no_of_processes) lowerCAmelCase__ = burst_time lowerCAmelCase__ = no_of_processes lowerCAmelCase__ = waiting_time lowerCAmelCase__ = calculate_turnaroundtime(bt, n, wt) calculate_average_times(waiting_time, turn_around_time, no_of_processes) lowerCAmelCase__ = pd.DataFrame( list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)), columns=[ '''Process''', '''BurstTime''', '''ArrivalTime''', '''WaitingTime''', '''TurnAroundTime''', ], ) # Printing the dataFrame pd.set_option('''display.max_rows''', fcfs.shape[0] + 1) print(fcfs)
719
"""simple docstring""" from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels
681
0
"""simple docstring""" from typing import Optional, Union import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models.modeling_utils import ModelMixin class SCREAMING_SNAKE_CASE__ ( __lowerCamelCase , __lowerCamelCase ): """simple docstring""" @register_to_config def __init__( self , snake_case__ = 768 , ): """simple docstring""" super().__init__() lowerCAmelCase : Union[str, Any] = nn.Parameter(torch.zeros(1 , SCREAMING_SNAKE_CASE_ ) ) lowerCAmelCase : str = nn.Parameter(torch.ones(1 , SCREAMING_SNAKE_CASE_ ) ) def lowercase__ ( self , snake_case__ = None , snake_case__ = None , ): """simple docstring""" lowerCAmelCase : Dict = nn.Parameter(self.mean.to(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) ) lowerCAmelCase : List[str] = nn.Parameter(self.std.to(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) ) return self def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Union[str, Any] = (embeds - self.mean) * 1.0 / self.std return embeds def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : int = (embeds * self.std) + self.mean return embeds
720
"""simple docstring""" import copy import inspect import unittest from transformers import AutoBackbone from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import require_timm, require_torch, torch_device from transformers.utils.import_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 if is_torch_available(): import torch from transformers import TimmBackbone, TimmBackboneConfig from ...test_pipeline_mixin import PipelineTesterMixin class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__="resnet50" , snake_case__=3 , snake_case__=32 , snake_case__=3 , snake_case__=True , snake_case__=True , ): """simple docstring""" lowerCAmelCase : List[str] = parent lowerCAmelCase : Union[str, Any] = out_indices if out_indices is not None else [4] lowerCAmelCase : Tuple = stage_names lowerCAmelCase : Any = out_features lowerCAmelCase : Any = backbone lowerCAmelCase : Union[str, Any] = batch_size lowerCAmelCase : Optional[int] = image_size lowerCAmelCase : List[str] = num_channels lowerCAmelCase : int = use_pretrained_backbone lowerCAmelCase : Tuple = is_training def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase : Optional[int] = self.get_config() return config, pixel_values def lowercase__ ( self ): """simple docstring""" return TimmBackboneConfig( image_size=self.image_size , num_channels=self.num_channels , out_features=self.out_features , out_indices=self.out_indices , stage_names=self.stage_names , use_pretrained_backbone=self.use_pretrained_backbone , backbone=self.backbone , ) def lowercase__ ( self , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : List[Any] = TimmBackbone(config=snake_case__ ) model.to(snake_case__ ) model.eval() with torch.no_grad(): lowerCAmelCase : Union[str, Any] = model(snake_case__ ) self.parent.assertEqual( result.feature_map[-1].shape , (self.batch_size, model.channels[-1], 14, 14) , ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = self.prepare_config_and_inputs() lowerCAmelCase , lowerCAmelCase : Tuple = config_and_inputs lowerCAmelCase : str = {"pixel_values": pixel_values} return config, inputs_dict @require_torch @require_timm class SCREAMING_SNAKE_CASE__ ( lowercase , lowercase , lowercase , unittest.TestCase ): """simple docstring""" a : Optional[int] =(TimmBackbone,) if is_torch_available() else () a : Union[str, Any] ={"feature-extraction": TimmBackbone} if is_torch_available() else {} a : Tuple =False a : List[Any] =False a : Optional[Any] =False a : Dict =False def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = TimmBackboneModelTester(self ) lowerCAmelCase : List[str] = ConfigTester(self , config_class=snake_case__ , has_text_modality=snake_case__ ) def lowercase__ ( self ): """simple docstring""" 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 lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = "resnet18" lowerCAmelCase : str = "microsoft/resnet-18" lowerCAmelCase : List[Any] = AutoBackbone.from_pretrained(snake_case__ , use_timm_backbone=snake_case__ ) lowerCAmelCase : List[str] = AutoBackbone.from_pretrained(snake_case__ ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(len(timm_model.stage_names ) , len(transformers_model.stage_names ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) # Out indices are set to the last layer by default. For timm models, we don't know # the number of layers in advance, so we set it to (-1,), whereas for transformers # models, we set it to [len(stage_names) - 1] (kept for backward compatibility). self.assertEqual(timm_model.out_indices , (-1,) ) self.assertEqual(transformers_model.out_indices , [len(timm_model.stage_names ) - 1] ) lowerCAmelCase : Union[str, Any] = AutoBackbone.from_pretrained(snake_case__ , use_timm_backbone=snake_case__ , out_indices=[1, 2, 3] ) lowerCAmelCase : List[Any] = AutoBackbone.from_pretrained(snake_case__ , out_indices=[1, 2, 3] ) self.assertEqual(timm_model.out_indices , transformers_model.out_indices ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) @unittest.skip("TimmBackbone doesn't support feed forward chunking" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone doesn't have num_hidden_layers attribute" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone initialization is managed on the timm side" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone models doesn't have inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone models doesn't have inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone model cannot be created without specifying a backbone checkpoint" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("model weights aren't tied in TimmBackbone." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("model weights aren't tied in TimmBackbone." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone doesn't have hidden size info in its configuration." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone doesn't support output_attentions." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Safetensors is not supported by timm." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : Dict = model_class(snake_case__ ) lowerCAmelCase : Any = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase : Optional[int] = [*signature.parameters.keys()] lowerCAmelCase : Any = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase : int = True lowerCAmelCase : str = self.has_attentions # no need to test all models as different heads yield the same functionality lowerCAmelCase : Optional[int] = self.all_model_classes[0] lowerCAmelCase : Union[str, Any] = model_class(snake_case__ ) model.to(snake_case__ ) lowerCAmelCase : Optional[int] = self._prepare_for_class(snake_case__ , snake_case__ ) lowerCAmelCase : Dict = model(**snake_case__ ) lowerCAmelCase : Tuple = outputs[0][-1] # Encoder-/Decoder-only models lowerCAmelCase : Optional[int] = outputs.hidden_states[0] hidden_states.retain_grad() if self.has_attentions: lowerCAmelCase : Union[str, Any] = outputs.attentions[0] attentions.retain_grad() output.flatten()[0].backward(retain_graph=snake_case__ ) self.assertIsNotNone(hidden_states.grad ) if self.has_attentions: self.assertIsNotNone(attentions.grad ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : Dict = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : List[str] = model(**snake_case__ ) self.assertEqual(len(result.feature_maps ) , len(config.out_indices ) ) self.assertEqual(len(model.channels ) , len(config.out_indices ) ) # Check output of last stage is taken if out_features=None, out_indices=None lowerCAmelCase : Dict = copy.deepcopy(snake_case__ ) lowerCAmelCase : Optional[int] = None lowerCAmelCase : Dict = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : Optional[int] = model(**snake_case__ ) self.assertEqual(len(result.feature_maps ) , 1 ) self.assertEqual(len(model.channels ) , 1 ) # Check backbone can be initialized with fresh weights lowerCAmelCase : Optional[int] = copy.deepcopy(snake_case__ ) lowerCAmelCase : List[str] = False lowerCAmelCase : int = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : Optional[Any] = model(**snake_case__ )
681
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tensorflow_text_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase__ = { """configuration_bert""": ["""BERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BertConfig""", """BertOnnxConfig"""], """tokenization_bert""": ["""BasicTokenizer""", """BertTokenizer""", """WordpieceTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ["""BertTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ """BERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """BertForMaskedLM""", """BertForMultipleChoice""", """BertForNextSentencePrediction""", """BertForPreTraining""", """BertForQuestionAnswering""", """BertForSequenceClassification""", """BertForTokenClassification""", """BertLayer""", """BertLMHeadModel""", """BertModel""", """BertPreTrainedModel""", """load_tf_weights_in_bert""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ """TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFBertEmbeddings""", """TFBertForMaskedLM""", """TFBertForMultipleChoice""", """TFBertForNextSentencePrediction""", """TFBertForPreTraining""", """TFBertForQuestionAnswering""", """TFBertForSequenceClassification""", """TFBertForTokenClassification""", """TFBertLMHeadModel""", """TFBertMainLayer""", """TFBertModel""", """TFBertPreTrainedModel""", ] try: if not is_tensorflow_text_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ["""TFBertTokenizer"""] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ """FlaxBertForCausalLM""", """FlaxBertForMaskedLM""", """FlaxBertForMultipleChoice""", """FlaxBertForNextSentencePrediction""", """FlaxBertForPreTraining""", """FlaxBertForQuestionAnswering""", """FlaxBertForSequenceClassification""", """FlaxBertForTokenClassification""", """FlaxBertModel""", """FlaxBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig, BertOnnxConfig from .tokenization_bert import BasicTokenizer, BertTokenizer, WordpieceTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bert_fast import BertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bert import ( BERT_PRETRAINED_MODEL_ARCHIVE_LIST, BertForMaskedLM, BertForMultipleChoice, BertForNextSentencePrediction, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, BertForTokenClassification, BertLayer, BertLMHeadModel, BertModel, BertPreTrainedModel, load_tf_weights_in_bert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_bert import ( TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFBertEmbeddings, TFBertForMaskedLM, TFBertForMultipleChoice, TFBertForNextSentencePrediction, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertForTokenClassification, TFBertLMHeadModel, TFBertMainLayer, TFBertModel, TFBertPreTrainedModel, ) try: if not is_tensorflow_text_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bert_tf import TFBertTokenizer try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_bert import ( FlaxBertForCausalLM, FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForNextSentencePrediction, FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertModel, FlaxBertPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
721
"""simple docstring""" import argparse import os import re lowerCAmelCase__ = '''src/transformers''' # Pattern that looks at the indentation in a line. lowerCAmelCase__ = re.compile(r'''^(\s*)\S''') # Pattern that matches `"key":" and puts `key` in group 0. lowerCAmelCase__ = re.compile(r'''^\s*"([^"]+)":''') # Pattern that matches `_import_structure["key"]` and puts `key` in group 0. lowerCAmelCase__ = re.compile(r'''^\s*_import_structure\["([^"]+)"\]''') # Pattern that matches `"key",` and puts `key` in group 0. lowerCAmelCase__ = re.compile(r'''^\s*"([^"]+)",\s*$''') # Pattern that matches any `[stuff]` and puts `stuff` in group 0. lowerCAmelCase__ = re.compile(r'''\[([^\]]+)\]''') def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = _re_indent.search(SCREAMING_SNAKE_CASE ) return "" if search is None else search.groups()[0] def a__ ( SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Optional[int]="" , SCREAMING_SNAKE_CASE : Optional[int]=None , SCREAMING_SNAKE_CASE : Dict=None ): '''simple docstring''' lowerCAmelCase : Tuple = 0 lowerCAmelCase : Optional[int] = code.split("\n" ) if start_prompt is not None: while not lines[index].startswith(SCREAMING_SNAKE_CASE ): index += 1 lowerCAmelCase : Dict = ["\n".join(lines[:index] )] else: lowerCAmelCase : Any = [] # We split into blocks until we get to the `end_prompt` (or the end of the block). lowerCAmelCase : Union[str, Any] = [lines[index]] index += 1 while index < len(SCREAMING_SNAKE_CASE ) and (end_prompt is None or not lines[index].startswith(SCREAMING_SNAKE_CASE )): if len(lines[index] ) > 0 and get_indent(lines[index] ) == indent_level: if len(SCREAMING_SNAKE_CASE ) > 0 and get_indent(current_block[-1] ).startswith(indent_level + " " ): current_block.append(lines[index] ) blocks.append("\n".join(SCREAMING_SNAKE_CASE ) ) if index < len(SCREAMING_SNAKE_CASE ) - 1: lowerCAmelCase : List[str] = [lines[index + 1]] index += 1 else: lowerCAmelCase : Optional[Any] = [] else: blocks.append("\n".join(SCREAMING_SNAKE_CASE ) ) lowerCAmelCase : str = [lines[index]] else: current_block.append(lines[index] ) index += 1 # Adds current block if it's nonempty. if len(SCREAMING_SNAKE_CASE ) > 0: blocks.append("\n".join(SCREAMING_SNAKE_CASE ) ) # Add final block after end_prompt if provided. if end_prompt is not None and index < len(SCREAMING_SNAKE_CASE ): blocks.append("\n".join(lines[index:] ) ) return blocks def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' def _inner(SCREAMING_SNAKE_CASE : Optional[Any] ): return key(SCREAMING_SNAKE_CASE ).lower().replace("_" , "" ) return _inner def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Dict=None ): '''simple docstring''' def noop(SCREAMING_SNAKE_CASE : List[Any] ): return x if key is None: lowerCAmelCase : int = noop # Constants are all uppercase, they go first. lowerCAmelCase : Dict = [obj for obj in objects if key(SCREAMING_SNAKE_CASE ).isupper()] # Classes are not all uppercase but start with a capital, they go second. lowerCAmelCase : List[Any] = [obj for obj in objects if key(SCREAMING_SNAKE_CASE )[0].isupper() and not key(SCREAMING_SNAKE_CASE ).isupper()] # Functions begin with a lowercase, they go last. lowerCAmelCase : List[Any] = [obj for obj in objects if not key(SCREAMING_SNAKE_CASE )[0].isupper()] lowerCAmelCase : Dict = ignore_underscore(SCREAMING_SNAKE_CASE ) return sorted(SCREAMING_SNAKE_CASE , key=SCREAMING_SNAKE_CASE ) + sorted(SCREAMING_SNAKE_CASE , key=SCREAMING_SNAKE_CASE ) + sorted(SCREAMING_SNAKE_CASE , key=SCREAMING_SNAKE_CASE ) def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' def _replace(SCREAMING_SNAKE_CASE : List[Any] ): lowerCAmelCase : List[str] = match.groups()[0] if "," not in imports: return f"""[{imports}]""" lowerCAmelCase : Dict = [part.strip().replace("\"" , "" ) for part in imports.split("," )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: lowerCAmelCase : Any = keys[:-1] return "[" + ", ".join([f"""\"{k}\"""" for k in sort_objects(SCREAMING_SNAKE_CASE )] ) + "]" lowerCAmelCase : List[Any] = import_statement.split("\n" ) if len(SCREAMING_SNAKE_CASE ) > 3: # Here we have to sort internal imports that are on several lines (one per name): # key: [ # "object1", # "object2", # ... # ] # We may have to ignore one or two lines on each side. lowerCAmelCase : Tuple = 2 if lines[1].strip() == "[" else 1 lowerCAmelCase : Optional[Any] = [(i, _re_strip_line.search(SCREAMING_SNAKE_CASE ).groups()[0]) for i, line in enumerate(lines[idx:-idx] )] lowerCAmelCase : Optional[Any] = sort_objects(SCREAMING_SNAKE_CASE , key=lambda SCREAMING_SNAKE_CASE : x[1] ) lowerCAmelCase : List[str] = [lines[x[0] + idx] for x in sorted_indices] return "\n".join(lines[:idx] + sorted_lines + lines[-idx:] ) elif len(SCREAMING_SNAKE_CASE ) == 3: # Here we have to sort internal imports that are on one separate line: # key: [ # "object1", "object2", ... # ] if _re_bracket_content.search(lines[1] ) is not None: lowerCAmelCase : Optional[int] = _re_bracket_content.sub(_replace , lines[1] ) else: lowerCAmelCase : List[str] = [part.strip().replace("\"" , "" ) for part in lines[1].split("," )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: lowerCAmelCase : Union[str, Any] = keys[:-1] lowerCAmelCase : str = get_indent(lines[1] ) + ", ".join([f"""\"{k}\"""" for k in sort_objects(SCREAMING_SNAKE_CASE )] ) return "\n".join(SCREAMING_SNAKE_CASE ) else: # Finally we have to deal with imports fitting on one line lowerCAmelCase : Any = _re_bracket_content.sub(_replace , SCREAMING_SNAKE_CASE ) return import_statement def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Tuple=True ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE , encoding="utf-8" ) as f: lowerCAmelCase : Union[str, Any] = f.read() if "_import_structure" not in code: return # Blocks of indent level 0 lowerCAmelCase : List[str] = split_code_in_indented_blocks( SCREAMING_SNAKE_CASE , start_prompt="_import_structure = {" , end_prompt="if TYPE_CHECKING:" ) # We ignore block 0 (everything untils start_prompt) and the last block (everything after end_prompt). for block_idx in range(1 , len(SCREAMING_SNAKE_CASE ) - 1 ): # Check if the block contains some `_import_structure`s thingy to sort. lowerCAmelCase : Tuple = main_blocks[block_idx] lowerCAmelCase : Optional[Any] = block.split("\n" ) # Get to the start of the imports. lowerCAmelCase : int = 0 while line_idx < len(SCREAMING_SNAKE_CASE ) and "_import_structure" not in block_lines[line_idx]: # Skip dummy import blocks if "import dummy" in block_lines[line_idx]: lowerCAmelCase : Optional[Any] = len(SCREAMING_SNAKE_CASE ) else: line_idx += 1 if line_idx >= len(SCREAMING_SNAKE_CASE ): continue # Ignore beginning and last line: they don't contain anything. lowerCAmelCase : Optional[Any] = "\n".join(block_lines[line_idx:-1] ) lowerCAmelCase : Dict = get_indent(block_lines[1] ) # Slit the internal block into blocks of indent level 1. lowerCAmelCase : Optional[Any] = split_code_in_indented_blocks(SCREAMING_SNAKE_CASE , indent_level=SCREAMING_SNAKE_CASE ) # We have two categories of import key: list or _import_structure[key].append/extend lowerCAmelCase : Tuple = _re_direct_key if "_import_structure = {" in block_lines[0] else _re_indirect_key # Grab the keys, but there is a trap: some lines are empty or just comments. lowerCAmelCase : Tuple = [(pattern.search(SCREAMING_SNAKE_CASE ).groups()[0] if pattern.search(SCREAMING_SNAKE_CASE ) is not None else None) for b in internal_blocks] # We only sort the lines with a key. lowerCAmelCase : int = [(i, key) for i, key in enumerate(SCREAMING_SNAKE_CASE ) if key is not None] lowerCAmelCase : Union[str, Any] = [x[0] for x in sorted(SCREAMING_SNAKE_CASE , key=lambda SCREAMING_SNAKE_CASE : x[1] )] # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. lowerCAmelCase : Tuple = 0 lowerCAmelCase : Any = [] for i in range(len(SCREAMING_SNAKE_CASE ) ): if keys[i] is None: reorderded_blocks.append(internal_blocks[i] ) else: lowerCAmelCase : Dict = sort_objects_in_import(internal_blocks[sorted_indices[count]] ) reorderded_blocks.append(SCREAMING_SNAKE_CASE ) count += 1 # And we put our main block back together with its first and last line. lowerCAmelCase : List[Any] = "\n".join(block_lines[:line_idx] + reorderded_blocks + [block_lines[-1]] ) if code != "\n".join(SCREAMING_SNAKE_CASE ): if check_only: return True else: print(f"""Overwriting {file}.""" ) with open(SCREAMING_SNAKE_CASE , "w" , encoding="utf-8" ) as f: f.write("\n".join(SCREAMING_SNAKE_CASE ) ) def a__ ( SCREAMING_SNAKE_CASE : List[str]=True ): '''simple docstring''' lowerCAmelCase : Optional[int] = [] for root, _, files in os.walk(SCREAMING_SNAKE_CASE ): if "__init__.py" in files: lowerCAmelCase : Tuple = sort_imports(os.path.join(SCREAMING_SNAKE_CASE , "__init__.py" ) , check_only=SCREAMING_SNAKE_CASE ) if result: lowerCAmelCase : Optional[Any] = [os.path.join(SCREAMING_SNAKE_CASE , "__init__.py" )] if len(SCREAMING_SNAKE_CASE ) > 0: raise ValueError(f"""Would overwrite {len(SCREAMING_SNAKE_CASE )} files, run `make style`.""" ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument('''--check_only''', action='''store_true''', help='''Whether to only check or fix style.''') lowerCAmelCase__ = parser.parse_args() sort_imports_in_all_inits(check_only=args.check_only)
681
0
"""simple docstring""" from ..utils import DummyObject, requires_backends class SCREAMING_SNAKE_CASE__ ( metaclass=lowercase__ ): """simple docstring""" a : Any =["torch", "scipy"] def __init__( self , *snake_case__ , **snake_case__ ): """simple docstring""" requires_backends(self , ["torch", "scipy"] ) @classmethod def lowercase__ ( cls , *snake_case__ , **snake_case__ ): """simple docstring""" requires_backends(cls , ["torch", "scipy"] ) @classmethod def lowercase__ ( cls , *snake_case__ , **snake_case__ ): """simple docstring""" requires_backends(cls , ["torch", "scipy"] )
700
"""simple docstring""" from __future__ import annotations import inspect import unittest import numpy as np from transformers import ResNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFResNetForImageClassification, TFResNetModel from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__=3 , snake_case__=32 , snake_case__=3 , snake_case__=10 , snake_case__=[10, 20, 30, 40] , snake_case__=[1, 1, 2, 1] , snake_case__=True , snake_case__=True , snake_case__="relu" , snake_case__=3 , snake_case__=None , ): """simple docstring""" lowerCAmelCase : Optional[Any] = parent lowerCAmelCase : List[Any] = batch_size lowerCAmelCase : Union[str, Any] = image_size lowerCAmelCase : Dict = num_channels lowerCAmelCase : List[Any] = embeddings_size lowerCAmelCase : List[Any] = hidden_sizes lowerCAmelCase : Optional[int] = depths lowerCAmelCase : str = is_training lowerCAmelCase : List[str] = use_labels lowerCAmelCase : List[Any] = hidden_act lowerCAmelCase : Optional[Any] = num_labels lowerCAmelCase : Tuple = scope lowerCAmelCase : int = len(snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase : Optional[Any] = None if self.use_labels: lowerCAmelCase : Optional[int] = ids_tensor([self.batch_size] , self.num_labels ) lowerCAmelCase : List[str] = self.get_config() return config, pixel_values, labels def lowercase__ ( self ): """simple docstring""" return ResNetConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , image_size=self.image_size , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Tuple = TFResNetModel(config=snake_case__ ) lowerCAmelCase : Union[str, Any] = model(snake_case__ ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = self.num_labels lowerCAmelCase : str = TFResNetForImageClassification(snake_case__ ) lowerCAmelCase : int = model(snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = self.prepare_config_and_inputs() lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Any = config_and_inputs lowerCAmelCase : Optional[Any] = {"pixel_values": pixel_values} return config, inputs_dict @require_tf class SCREAMING_SNAKE_CASE__ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" a : Any =(TFResNetModel, TFResNetForImageClassification) if is_tf_available() else () a : Tuple =( {"feature-extraction": TFResNetModel, "image-classification": TFResNetForImageClassification} if is_tf_available() else {} ) a : int =False a : List[str] =False a : Optional[int] =False a : Union[str, Any] =False a : Any =False def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = TFResNetModelTester(self ) lowerCAmelCase : str = ConfigTester(self , config_class=snake_case__ , has_text_modality=snake_case__ ) def lowercase__ ( self ): """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 lowercase__ ( self ): """simple docstring""" return @unittest.skip(reason="ResNet does not use inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip(reason="ResNet does not support input and output embeddings" ) def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : List[str] = model_class(snake_case__ ) lowerCAmelCase : Optional[Any] = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase : Dict = [*signature.parameters.keys()] lowerCAmelCase : List[Any] = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def lowercase__ ( self ): """simple docstring""" def check_hidden_states_output(snake_case__ , snake_case__ , snake_case__ ): lowerCAmelCase : int = model_class(snake_case__ ) lowerCAmelCase : Optional[Any] = model(**self._prepare_for_class(snake_case__ , snake_case__ ) ) lowerCAmelCase : List[str] = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states lowerCAmelCase : Tuple = self.model_tester.num_stages self.assertEqual(len(snake_case__ ) , expected_num_stages + 1 ) # ResNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) lowerCAmelCase , lowerCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase : Any = ["basic", "bottleneck"] for model_class in self.all_model_classes: for layer_type in layers_type: lowerCAmelCase : Optional[Any] = layer_type lowerCAmelCase : Dict = True check_hidden_states_output(snake_case__ , snake_case__ , snake_case__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowerCAmelCase : List[Any] = True check_hidden_states_output(snake_case__ , snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case__ ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase : int = TFResNetModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def a__ ( ): '''simple docstring''' lowerCAmelCase : str = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_tf @require_vision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" @cached_property def lowercase__ ( self ): """simple docstring""" return ( AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) lowerCAmelCase : Any = self.default_image_processor lowerCAmelCase : Optional[Any] = prepare_img() lowerCAmelCase : Dict = image_processor(images=snake_case__ , return_tensors="tf" ) # forward pass lowerCAmelCase : str = model(**snake_case__ ) # verify the logits lowerCAmelCase : str = tf.TensorShape((1, 1_000) ) self.assertEqual(outputs.logits.shape , snake_case__ ) lowerCAmelCase : str = tf.constant([-11.1069, -9.7877, -8.3777] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , snake_case__ , atol=1e-4 ) )
681
0
"""simple docstring""" import argparse import os import torch from transformers.utils import WEIGHTS_NAME lowerCAmelCase__ = ['''small''', '''medium''', '''large'''] lowerCAmelCase__ = '''lm_head.decoder.weight''' lowerCAmelCase__ = '''lm_head.weight''' def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Tuple ): '''simple docstring''' lowerCAmelCase : List[str] = torch.load(_lowerCAmelCase ) lowerCAmelCase : Optional[int] = d.pop(_lowerCAmelCase ) os.makedirs(_lowerCAmelCase , exist_ok=_lowerCAmelCase ) torch.save(_lowerCAmelCase , os.path.join(_lowerCAmelCase , _lowerCAmelCase ) ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument('''--dialogpt_path''', default='''.''', type=str) lowerCAmelCase__ = parser.parse_args() for MODEL in DIALOGPT_MODELS: lowerCAmelCase__ = os.path.join(args.dialogpt_path, F"{MODEL}_ft.pkl") lowerCAmelCase__ = F"./DialoGPT-{MODEL}" convert_dialogpt_checkpoint( checkpoint_path, pytorch_dump_folder_path, )
701
"""simple docstring""" import random from .binary_exp_mod import bin_exp_mod def a__ ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : int=1_0_0_0 ): '''simple docstring''' if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd lowerCAmelCase : int = n - 1 lowerCAmelCase : Optional[int] = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) lowerCAmelCase : Optional[Any] = 0 while count < prec: lowerCAmelCase : List[str] = random.randint(2 , n - 1 ) lowerCAmelCase : Tuple = bin_exp_mod(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if b != 1: lowerCAmelCase : List[str] = True for _ in range(SCREAMING_SNAKE_CASE ): if b == n - 1: lowerCAmelCase : List[str] = False break lowerCAmelCase : Optional[Any] = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": lowerCAmelCase__ = abs(int(input('''Enter bound : ''').strip())) print('''Here\'s the list of primes:''') print(''', '''.join(str(i) for i in range(n + 1) if is_prime_big(i)))
681
0
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float ): '''simple docstring''' return round(float(moles / volume ) * nfactor ) def a__ ( SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float ): '''simple docstring''' return round(float((moles * 0.0_821 * temperature) / (volume) ) ) def a__ ( SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float ): '''simple docstring''' return round(float((moles * 0.0_821 * temperature) / (pressure) ) ) def a__ ( SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float ): '''simple docstring''' return round(float((pressure * volume) / (0.0_821 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
702
"""simple docstring""" # DISCLAIMER: This file is strongly influenced by https://github.com/ermongroup/ddim from dataclasses import dataclass from typing import Optional, Tuple, Union import flax import jax import jax.numpy as jnp from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils_flax import ( CommonSchedulerState, FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, add_noise_common, get_velocity_common, ) @flax.struct.dataclass class SCREAMING_SNAKE_CASE__ : """simple docstring""" a : CommonSchedulerState # setable values a : jnp.ndarray a : jnp.ndarray a : Optional[int] =None @classmethod def lowercase__ ( cls , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" return cls(common=snake_case__ , init_noise_sigma=snake_case__ , timesteps=snake_case__ ) @dataclass class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : DDPMSchedulerState class SCREAMING_SNAKE_CASE__ ( lowercase , lowercase ): """simple docstring""" a : Union[str, Any] =[e.name for e in FlaxKarrasDiffusionSchedulers] a : jnp.dtype @property def lowercase__ ( self ): """simple docstring""" return True @register_to_config def __init__( self , snake_case__ = 1_000 , snake_case__ = 0.0001 , snake_case__ = 0.02 , snake_case__ = "linear" , snake_case__ = None , snake_case__ = "fixed_small" , snake_case__ = True , snake_case__ = "epsilon" , snake_case__ = jnp.floataa , ): """simple docstring""" lowerCAmelCase : Any = dtype def lowercase__ ( self , snake_case__ = None ): """simple docstring""" if common is None: lowerCAmelCase : Dict = CommonSchedulerState.create(self ) # standard deviation of the initial noise distribution lowerCAmelCase : str = jnp.array(1.0 , dtype=self.dtype ) lowerCAmelCase : Any = jnp.arange(0 , self.config.num_train_timesteps ).round()[::-1] return DDPMSchedulerState.create( common=snake_case__ , init_noise_sigma=snake_case__ , timesteps=snake_case__ , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = None ): """simple docstring""" return sample def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = () ): """simple docstring""" lowerCAmelCase : List[Any] = self.config.num_train_timesteps // num_inference_steps # creates integer timesteps by multiplying by ratio # rounding to avoid issues when num_inference_step is power of 3 lowerCAmelCase : Any = (jnp.arange(0 , snake_case__ ) * step_ratio).round()[::-1] return state.replace( num_inference_steps=snake_case__ , timesteps=snake_case__ , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__=None , snake_case__=None ): """simple docstring""" lowerCAmelCase : Optional[Any] = state.common.alphas_cumprod[t] lowerCAmelCase : List[str] = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) # For t > 0, compute predicted variance βt (see formula (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf) # and sample from it to get previous sample # x_{t-1} ~ N(pred_prev_sample, variance) == add variance to pred_sample lowerCAmelCase : Union[str, Any] = (1 - alpha_prod_t_prev) / (1 - alpha_prod_t) * state.common.betas[t] if variance_type is None: lowerCAmelCase : List[str] = self.config.variance_type # hacks - were probably added for training stability if variance_type == "fixed_small": lowerCAmelCase : List[Any] = jnp.clip(snake_case__ , a_min=1e-20 ) # for rl-diffuser https://arxiv.org/abs/2205.09991 elif variance_type == "fixed_small_log": lowerCAmelCase : List[str] = jnp.log(jnp.clip(snake_case__ , a_min=1e-20 ) ) elif variance_type == "fixed_large": lowerCAmelCase : Optional[int] = state.common.betas[t] elif variance_type == "fixed_large_log": # Glide max_log lowerCAmelCase : List[str] = jnp.log(state.common.betas[t] ) elif variance_type == "learned": return predicted_variance elif variance_type == "learned_range": lowerCAmelCase : List[str] = variance lowerCAmelCase : Dict = state.common.betas[t] lowerCAmelCase : Optional[Any] = (predicted_variance + 1) / 2 lowerCAmelCase : List[str] = frac * max_log + (1 - frac) * min_log return variance def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ = None , snake_case__ = True , ): """simple docstring""" lowerCAmelCase : Optional[Any] = timestep if key is None: lowerCAmelCase : Tuple = jax.random.PRNGKey(0 ) if model_output.shape[1] == sample.shape[1] * 2 and self.config.variance_type in ["learned", "learned_range"]: lowerCAmelCase , lowerCAmelCase : Optional[Any] = jnp.split(snake_case__ , sample.shape[1] , axis=1 ) else: lowerCAmelCase : Tuple = None # 1. compute alphas, betas lowerCAmelCase : Optional[int] = state.common.alphas_cumprod[t] lowerCAmelCase : Optional[int] = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) lowerCAmelCase : Dict = 1 - alpha_prod_t lowerCAmelCase : Any = 1 - alpha_prod_t_prev # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if self.config.prediction_type == "epsilon": lowerCAmelCase : Union[str, Any] = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif self.config.prediction_type == "sample": lowerCAmelCase : List[Any] = model_output elif self.config.prediction_type == "v_prediction": lowerCAmelCase : Tuple = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output else: raise ValueError( f"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample` """ " for the FlaxDDPMScheduler." ) # 3. Clip "predicted x_0" if self.config.clip_sample: lowerCAmelCase : Optional[int] = jnp.clip(snake_case__ , -1 , 1 ) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowerCAmelCase : List[Any] = (alpha_prod_t_prev ** 0.5 * state.common.betas[t]) / beta_prod_t lowerCAmelCase : List[str] = state.common.alphas[t] ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowerCAmelCase : int = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise def random_variance(): lowerCAmelCase : Tuple = jax.random.split(snake_case__ , num=1 ) lowerCAmelCase : str = jax.random.normal(snake_case__ , shape=model_output.shape , dtype=self.dtype ) return (self._get_variance(snake_case__ , snake_case__ , predicted_variance=snake_case__ ) ** 0.5) * noise lowerCAmelCase : Union[str, Any] = jnp.where(t > 0 , random_variance() , jnp.zeros(model_output.shape , dtype=self.dtype ) ) lowerCAmelCase : Union[str, Any] = pred_prev_sample + variance if not return_dict: return (pred_prev_sample, state) return FlaxDDPMSchedulerOutput(prev_sample=snake_case__ , state=snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" return add_noise_common(state.common , snake_case__ , snake_case__ , snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" return get_velocity_common(state.common , snake_case__ , snake_case__ , snake_case__ ) def __len__( self ): """simple docstring""" return self.config.num_train_timesteps
681
0
"""simple docstring""" import os from collections.abc import Iterator def a__ ( SCREAMING_SNAKE_CASE : Dict = "." ): '''simple docstring''' for dir_path, dir_names, filenames in os.walk(UpperCamelCase__ ): lowerCAmelCase : str = [d for d in dir_names if d != "scripts" and d[0] not in "._"] for filename in filenames: if filename == "__init__.py": continue if os.path.splitext(UpperCamelCase__ )[1] in (".py", ".ipynb"): yield os.path.join(UpperCamelCase__ , UpperCamelCase__ ).lstrip("./" ) def a__ ( SCREAMING_SNAKE_CASE : Tuple ): '''simple docstring''' return f"""{i * " "}*""" if i else "\n##" def a__ ( SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : Optional[Any] = old_path.split(os.sep ) for i, new_part in enumerate(new_path.split(os.sep ) ): if (i + 1 > len(UpperCamelCase__ ) or old_parts[i] != new_part) and new_part: print(f"""{md_prefix(UpperCamelCase__ )} {new_part.replace("_" , " " ).title()}""" ) return new_path def a__ ( SCREAMING_SNAKE_CASE : int = "." ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = "" for filepath in sorted(good_file_paths(UpperCamelCase__ ) ): lowerCAmelCase , lowerCAmelCase : str = os.path.split(UpperCamelCase__ ) if filepath != old_path: lowerCAmelCase : List[str] = print_path(UpperCamelCase__ , UpperCamelCase__ ) lowerCAmelCase : Tuple = (filepath.count(os.sep ) + 1) if filepath else 0 lowerCAmelCase : Any = f"""{filepath}/{filename}""".replace(" " , "%20" ) lowerCAmelCase : Optional[int] = os.path.splitext(filename.replace("_" , " " ).title() )[0] print(f"""{md_prefix(UpperCamelCase__ )} [{filename}]({url})""" ) if __name__ == "__main__": print_directory_md('''.''')
703
"""simple docstring""" import argparse import OmegaConf import torch from diffusers import DDIMScheduler, LDMPipeline, UNetLDMModel, VQModel def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : Tuple = OmegaConf.load(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = torch.load(SCREAMING_SNAKE_CASE , map_location="cpu" )["model"] lowerCAmelCase : int = list(state_dict.keys() ) # extract state_dict for VQVAE lowerCAmelCase : Tuple = {} lowerCAmelCase : Dict = "first_stage_model." for key in keys: if key.startswith(SCREAMING_SNAKE_CASE ): lowerCAmelCase : List[str] = state_dict[key] # extract state_dict for UNetLDM lowerCAmelCase : List[Any] = {} lowerCAmelCase : Tuple = "model.diffusion_model." for key in keys: if key.startswith(SCREAMING_SNAKE_CASE ): lowerCAmelCase : str = state_dict[key] lowerCAmelCase : List[str] = config.model.params.first_stage_config.params lowerCAmelCase : List[Any] = config.model.params.unet_config.params lowerCAmelCase : Union[str, Any] = VQModel(**SCREAMING_SNAKE_CASE ).eval() vqvae.load_state_dict(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = UNetLDMModel(**SCREAMING_SNAKE_CASE ).eval() unet.load_state_dict(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[Any] = DDIMScheduler( timesteps=config.model.params.timesteps , beta_schedule="scaled_linear" , beta_start=config.model.params.linear_start , beta_end=config.model.params.linear_end , clip_sample=SCREAMING_SNAKE_CASE , ) lowerCAmelCase : Tuple = LDMPipeline(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) pipeline.save_pretrained(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument('''--checkpoint_path''', type=str, required=True) parser.add_argument('''--config_path''', type=str, required=True) parser.add_argument('''--output_path''', type=str, required=True) lowerCAmelCase__ = parser.parse_args() convert_ldm_original(args.checkpoint_path, args.config_path, args.output_path)
681
0
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : Any = 4_0_0_0_0_0_0 ): '''simple docstring''' lowerCAmelCase : Optional[int] = [] lowerCAmelCase : Optional[int] = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = b, a + b return sum(__SCREAMING_SNAKE_CASE ) if __name__ == "__main__": print(F"{solution() = }")
704
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : int = 1_0_0_0 ): '''simple docstring''' return sum(e for e in range(3 , SCREAMING_SNAKE_CASE ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"{solution() = }")
681
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "microsoft/biogpt": "https://huggingface.co/microsoft/biogpt/resolve/main/config.json", # See all BioGPT models at https://huggingface.co/models?filter=biogpt } class SCREAMING_SNAKE_CASE__ ( _UpperCAmelCase ): """simple docstring""" a : List[str] ='''biogpt''' def __init__( self , snake_case__=42_384 , snake_case__=1_024 , snake_case__=24 , snake_case__=16 , snake_case__=4_096 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=1_024 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__=True , snake_case__=True , snake_case__=0.0 , snake_case__=0.0 , snake_case__=1 , snake_case__=0 , snake_case__=2 , **snake_case__ , ): """simple docstring""" lowerCAmelCase : Dict = vocab_size lowerCAmelCase : int = max_position_embeddings lowerCAmelCase : Optional[Any] = hidden_size lowerCAmelCase : Optional[int] = num_hidden_layers lowerCAmelCase : List[str] = num_attention_heads lowerCAmelCase : Optional[int] = intermediate_size lowerCAmelCase : Tuple = hidden_act lowerCAmelCase : Optional[Any] = hidden_dropout_prob lowerCAmelCase : str = attention_probs_dropout_prob lowerCAmelCase : Tuple = initializer_range lowerCAmelCase : int = layer_norm_eps lowerCAmelCase : Optional[int] = scale_embedding lowerCAmelCase : Tuple = use_cache lowerCAmelCase : List[str] = layerdrop lowerCAmelCase : List[str] = activation_dropout super().__init__(pad_token_id=A_ , bos_token_id=A_ , eos_token_id=A_ , **A_ )
705
"""simple docstring""" import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized, parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv("TEST_SAGEMAKER" , "False" ) ) is not True , reason="Skipping test because should only be run when releasing minor transformers version" , ) @pytest.mark.usefixtures("sm_env" ) @parameterized_class( [ { "framework": "pytorch", "script": "run_glue_model_parallelism.py", "model_name_or_path": "roberta-large", "instance_type": "ml.p3dn.24xlarge", "results": {"train_runtime": 16_00, "eval_accuracy": 0.3, "eval_loss": 1.2}, }, { "framework": "pytorch", "script": "run_glue.py", "model_name_or_path": "roberta-large", "instance_type": "ml.p3dn.24xlarge", "results": {"train_runtime": 16_00, "eval_accuracy": 0.3, "eval_loss": 1.2}, }, ] ) class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" if self.framework == "pytorch": subprocess.run( f"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split() , encoding="utf-8" , check=snake_case__ , ) assert hasattr(self , "env" ) def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Optional[Any] = { "enabled": True, "processes_per_host": 8, } lowerCAmelCase : List[Any] = { "enabled": True, "parameters": { "microbatches": 4, "placement_strategy": "spread", "pipeline": "interleaved", "optimize": "speed", "partitions": 4, "ddp": True, }, } lowerCAmelCase : List[Any] = {"smdistributed": {"modelparallel": smp_options}, "mpi": mpi_options} lowerCAmelCase : Optional[Any] = "trainer" if self.script == "run_glue.py" else "smtrainer" # creates estimator return HuggingFace( entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=f"""{self.env.base_job_name}-{instance_count}-smp-{name_extension}""" , instance_count=snake_case__ , instance_type=self.instance_type , debugger_hook_config=snake_case__ , hyperparameters={ **self.env.hyperparameters, "model_name_or_path": self.model_name_or_path, "max_steps": 500, } , metric_definitions=self.env.metric_definitions , distribution=snake_case__ , py_version="py36" , ) def lowercase__ ( self , snake_case__ ): """simple docstring""" TrainingJobAnalytics(snake_case__ ).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv""" ) @parameterized.expand([(1,)] ) def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = self.create_estimator(snake_case__ ) # run training estimator.fit() # result dataframe lowerCAmelCase : Tuple = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis lowerCAmelCase : Tuple = list(result_metrics_df[result_metrics_df.metric_name == "eval_accuracy"]["value"] ) lowerCAmelCase : Optional[int] = list(result_metrics_df[result_metrics_df.metric_name == "eval_loss"]["value"] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping lowerCAmelCase : Dict = ( Session().describe_training_job(estimator.latest_training_job.name ).get("TrainingTimeInSeconds" , 999_999 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results["eval_accuracy"] for t in eval_accuracy ) assert all(t <= self.results["eval_loss"] for t in eval_loss ) # dump tests result into json file to share in PR with open(f"""{estimator.latest_training_job.name}.json""" , "w" ) as outfile: json.dump({"train_time": train_runtime, "eval_accuracy": eval_accuracy, "eval_loss": eval_loss} , snake_case__ )
681
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase__ = { 'configuration_rembert': ['REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RemBertConfig', 'RemBertOnnxConfig'] } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['RemBertTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['RemBertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ 'REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'RemBertForCausalLM', 'RemBertForMaskedLM', 'RemBertForMultipleChoice', 'RemBertForQuestionAnswering', 'RemBertForSequenceClassification', 'RemBertForTokenClassification', 'RemBertLayer', 'RemBertModel', 'RemBertPreTrainedModel', 'load_tf_weights_in_rembert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ 'TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFRemBertForCausalLM', 'TFRemBertForMaskedLM', 'TFRemBertForMultipleChoice', 'TFRemBertForQuestionAnswering', 'TFRemBertForSequenceClassification', 'TFRemBertForTokenClassification', 'TFRemBertLayer', 'TFRemBertModel', 'TFRemBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert import RemBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert_fast import RemBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rembert import ( REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, RemBertForCausalLM, RemBertForMaskedLM, RemBertForMultipleChoice, RemBertForQuestionAnswering, RemBertForSequenceClassification, RemBertForTokenClassification, RemBertLayer, RemBertModel, RemBertPreTrainedModel, load_tf_weights_in_rembert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rembert import ( TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFRemBertForCausalLM, TFRemBertForMaskedLM, TFRemBertForMultipleChoice, TFRemBertForQuestionAnswering, TFRemBertForSequenceClassification, TFRemBertForTokenClassification, TFRemBertLayer, TFRemBertModel, TFRemBertPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
706
"""simple docstring""" from math import factorial def a__ ( SCREAMING_SNAKE_CASE : int = 1_0_0 ): '''simple docstring''' return sum(int(SCREAMING_SNAKE_CASE ) for x in str(factorial(SCREAMING_SNAKE_CASE ) ) ) if __name__ == "__main__": print(solution(int(input('''Enter the Number: ''').strip())))
681
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "RWKV/rwkv-4-169m-pile": "https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json", "RWKV/rwkv-4-430m-pile": "https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json", "RWKV/rwkv-4-1b5-pile": "https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json", "RWKV/rwkv-4-3b-pile": "https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json", "RWKV/rwkv-4-7b-pile": "https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json", "RWKV/rwkv-4-14b-pile": "https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json", "RWKV/rwkv-raven-1b5": "https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json", "RWKV/rwkv-raven-3b": "https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json", "RWKV/rwkv-raven-7b": "https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json", "RWKV/rwkv-raven-14b": "https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json", } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Union[str, Any] ="rwkv" a : List[str] ={"max_position_embeddings": "context_length"} def __init__( self , snake_case__=50_277 , snake_case__=1_024 , snake_case__=4_096 , snake_case__=32 , snake_case__=None , snake_case__=None , snake_case__=1e-5 , snake_case__=0 , snake_case__=0 , snake_case__=6 , snake_case__=False , snake_case__=True , **snake_case__ , ): """simple docstring""" lowerCAmelCase : int = vocab_size lowerCAmelCase : List[Any] = context_length lowerCAmelCase : int = hidden_size lowerCAmelCase : List[Any] = num_hidden_layers lowerCAmelCase : List[str] = attention_hidden_size if attention_hidden_size is not None else hidden_size lowerCAmelCase : Optional[Any] = intermediate_size if intermediate_size is not None else 4 * hidden_size lowerCAmelCase : Any = layer_norm_epsilon lowerCAmelCase : int = rescale_every lowerCAmelCase : Union[str, Any] = use_cache lowerCAmelCase : Any = bos_token_id lowerCAmelCase : Tuple = eos_token_id super().__init__( tie_word_embeddings=__UpperCamelCase , bos_token_id=__UpperCamelCase , eos_token_id=__UpperCamelCase , **__UpperCamelCase )
707
"""simple docstring""" from typing import Any class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = data lowerCAmelCase : Any = None def __repr__( self ): """simple docstring""" return f"""Node({self.data})""" class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self ): """simple docstring""" lowerCAmelCase : Tuple = None def __iter__( self ): """simple docstring""" lowerCAmelCase : Any = self.head while node: yield node.data lowerCAmelCase : Optional[int] = node.next def __len__( self ): """simple docstring""" return sum(1 for _ in self ) def __repr__( self ): """simple docstring""" return "->".join([str(snake_case__ ) for item in self] ) def __getitem__( self , snake_case__ ): """simple docstring""" if not 0 <= index < len(self ): raise ValueError("list index out of range." ) for i, node in enumerate(self ): if i == index: return node return None def __setitem__( self , snake_case__ , snake_case__ ): """simple docstring""" if not 0 <= index < len(self ): raise ValueError("list index out of range." ) lowerCAmelCase : Union[str, Any] = self.head for _ in range(snake_case__ ): lowerCAmelCase : int = current.next lowerCAmelCase : List[str] = data def lowercase__ ( self , snake_case__ ): """simple docstring""" self.insert_nth(len(self ) , snake_case__ ) def lowercase__ ( self , snake_case__ ): """simple docstring""" self.insert_nth(0 , snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ ): """simple docstring""" if not 0 <= index <= len(self ): raise IndexError("list index out of range" ) lowerCAmelCase : Optional[int] = Node(snake_case__ ) if self.head is None: lowerCAmelCase : Any = new_node elif index == 0: lowerCAmelCase : Any = self.head # link new_node to head lowerCAmelCase : Union[str, Any] = new_node else: lowerCAmelCase : List[str] = self.head for _ in range(index - 1 ): lowerCAmelCase : int = temp.next lowerCAmelCase : int = temp.next lowerCAmelCase : Dict = new_node def lowercase__ ( self ): # print every node data """simple docstring""" print(self ) def lowercase__ ( self ): """simple docstring""" return self.delete_nth(0 ) def lowercase__ ( self ): # delete from tail """simple docstring""" return self.delete_nth(len(self ) - 1 ) def lowercase__ ( self , snake_case__ = 0 ): """simple docstring""" if not 0 <= index <= len(self ) - 1: # test if index is valid raise IndexError("List index out of range." ) lowerCAmelCase : List[Any] = self.head # default first node if index == 0: lowerCAmelCase : Optional[int] = self.head.next else: lowerCAmelCase : List[str] = self.head for _ in range(index - 1 ): lowerCAmelCase : Union[str, Any] = temp.next lowerCAmelCase : Optional[Any] = temp.next lowerCAmelCase : Any = temp.next.next return delete_node.data def lowercase__ ( self ): """simple docstring""" return self.head is None def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = None lowerCAmelCase : Optional[int] = self.head while current: # Store the current node's next node. lowerCAmelCase : List[Any] = current.next # Make the current node's next point backwards lowerCAmelCase : Dict = prev # Make the previous node be the current node lowerCAmelCase : List[str] = current # Make the current node the next node (to progress iteration) lowerCAmelCase : int = next_node # Return prev in order to put the head at the end lowerCAmelCase : Tuple = prev def a__ ( ): '''simple docstring''' lowerCAmelCase : Tuple = LinkedList() assert linked_list.is_empty() is True assert str(SCREAMING_SNAKE_CASE ) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(1_0 ): assert len(SCREAMING_SNAKE_CASE ) == i linked_list.insert_nth(SCREAMING_SNAKE_CASE , i + 1 ) assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(1 , 1_1 ) ) linked_list.insert_head(0 ) linked_list.insert_tail(1_1 ) assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(0 , 1_2 ) ) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9 ) == 1_0 assert linked_list.delete_tail() == 1_1 assert len(SCREAMING_SNAKE_CASE ) == 9 assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(1 , 1_0 ) ) assert all(linked_list[i] == i + 1 for i in range(0 , 9 ) ) is True for i in range(0 , 9 ): lowerCAmelCase : Optional[Any] = -i assert all(linked_list[i] == -i for i in range(0 , 9 ) ) is True linked_list.reverse() assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(-8 , 1 ) ) def a__ ( ): '''simple docstring''' lowerCAmelCase : List[str] = [ -9, 1_0_0, Node(7_7_3_4_5_1_1_2 ), "dlrow olleH", 7, 5_5_5_5, 0, -192.55_555, "Hello, world!", 77.9, Node(1_0 ), None, None, 12.20, ] lowerCAmelCase : List[str] = LinkedList() for i in test_input: linked_list.insert_tail(SCREAMING_SNAKE_CASE ) # Check if it's empty or not assert linked_list.is_empty() is False assert ( str(SCREAMING_SNAKE_CASE ) == "-9->100->Node(77345112)->dlrow olleH->7->5555->0->" "-192.55555->Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the head lowerCAmelCase : str = linked_list.delete_head() assert result == -9 assert ( str(SCREAMING_SNAKE_CASE ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the tail lowerCAmelCase : Union[str, Any] = linked_list.delete_tail() assert result == 12.2 assert ( str(SCREAMING_SNAKE_CASE ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None" ) # Delete a node in specific location in linked list lowerCAmelCase : List[str] = linked_list.delete_nth(1_0 ) assert result is None assert ( str(SCREAMING_SNAKE_CASE ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None" ) # Add a Node instance to its head linked_list.insert_head(Node("Hello again, world!" ) ) assert ( str(SCREAMING_SNAKE_CASE ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None" ) # Add None to its tail linked_list.insert_tail(SCREAMING_SNAKE_CASE ) assert ( str(SCREAMING_SNAKE_CASE ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None->None" ) # Reverse the linked list linked_list.reverse() assert ( str(SCREAMING_SNAKE_CASE ) == "None->None->Node(10)->77.9->Hello, world!->-192.55555->0->5555->" "7->dlrow olleH->Node(77345112)->100->Node(Hello again, world!)" ) def a__ ( ): '''simple docstring''' from doctest import testmod testmod() lowerCAmelCase : Optional[Any] = LinkedList() linked_list.insert_head(input("Inserting 1st at head " ).strip() ) linked_list.insert_head(input("Inserting 2nd at head " ).strip() ) print("\nPrint list:" ) linked_list.print_list() linked_list.insert_tail(input("\nInserting 1st at tail " ).strip() ) linked_list.insert_tail(input("Inserting 2nd at tail " ).strip() ) print("\nPrint list:" ) linked_list.print_list() print("\nDelete head" ) linked_list.delete_head() print("Delete tail" ) linked_list.delete_tail() print("\nPrint list:" ) linked_list.print_list() print("\nReverse linked list" ) linked_list.reverse() print("\nPrint list:" ) linked_list.print_list() print("\nString representation of linked list:" ) print(SCREAMING_SNAKE_CASE ) print("\nReading/changing Node data using indexing:" ) print(f"""Element at Position 1: {linked_list[1]}""" ) lowerCAmelCase : Any = input("Enter New Value: " ).strip() print("New list:" ) print(SCREAMING_SNAKE_CASE ) print(f"""length of linked_list is : {len(SCREAMING_SNAKE_CASE )}""" ) if __name__ == "__main__": main()
681
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase__ = { '''configuration_lxmert''': ['''LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''LxmertConfig'''], '''tokenization_lxmert''': ['''LxmertTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''LxmertTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''LxmertEncoder''', '''LxmertForPreTraining''', '''LxmertForQuestionAnswering''', '''LxmertModel''', '''LxmertPreTrainedModel''', '''LxmertVisualFeatureEncoder''', '''LxmertXLayer''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFLxmertForPreTraining''', '''TFLxmertMainLayer''', '''TFLxmertModel''', '''TFLxmertPreTrainedModel''', '''TFLxmertVisualFeatureEncoder''', ] if TYPE_CHECKING: from .configuration_lxmert import LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, LxmertConfig from .tokenization_lxmert import LxmertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_lxmert_fast import LxmertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_lxmert import ( LxmertEncoder, LxmertForPreTraining, LxmertForQuestionAnswering, LxmertModel, LxmertPreTrainedModel, LxmertVisualFeatureEncoder, LxmertXLayer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_lxmert import ( TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFLxmertForPreTraining, TFLxmertMainLayer, TFLxmertModel, TFLxmertPreTrainedModel, TFLxmertVisualFeatureEncoder, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
708
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowerCAmelCase__ = { '''configuration_efficientformer''': [ '''EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EfficientFormerConfig''', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''EfficientFormerImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EfficientFormerForImageClassification''', '''EfficientFormerForImageClassificationWithTeacher''', '''EfficientFormerModel''', '''EfficientFormerPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFEfficientFormerForImageClassification''', '''TFEfficientFormerForImageClassificationWithTeacher''', '''TFEfficientFormerModel''', '''TFEfficientFormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_efficientformer import EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientformer import EfficientFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientformer import ( EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientFormerForImageClassification, EfficientFormerForImageClassificationWithTeacher, EfficientFormerModel, EfficientFormerPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_efficientformer import ( TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFEfficientFormerForImageClassification, TFEfficientFormerForImageClassificationWithTeacher, TFEfficientFormerModel, TFEfficientFormerPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
681
0
"""simple docstring""" from __future__ import annotations def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' lowerCAmelCase : List[Any] = 2 lowerCAmelCase : Union[str, Any] = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(lowerCAmelCase__ ) if n > 1: factors.append(lowerCAmelCase__ ) return factors if __name__ == "__main__": import doctest doctest.testmod()
709
"""simple docstring""" 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 ) lowerCAmelCase__ = logging.getLogger(__name__) def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : Optional[Any] = np.argmax(SCREAMING_SNAKE_CASE , axis=1 ) return np.sum(outputs == labels ) def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE , encoding="utf_8" ) as f: lowerCAmelCase : Tuple = csv.reader(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = [] next(SCREAMING_SNAKE_CASE ) # skip the first line for line in tqdm(SCREAMING_SNAKE_CASE ): output.append((" ".join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : str ): '''simple docstring''' lowerCAmelCase : List[Any] = [] for dataset in encoded_datasets: lowerCAmelCase : Dict = len(SCREAMING_SNAKE_CASE ) lowerCAmelCase : str = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) lowerCAmelCase : Tuple = np.zeros((n_batch, 2) , dtype=np.intaa ) lowerCAmelCase : int = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) lowerCAmelCase : List[Any] = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(SCREAMING_SNAKE_CASE ): lowerCAmelCase : int = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase : Union[str, Any] = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase : Tuple = with_conta lowerCAmelCase : Any = with_conta lowerCAmelCase : Optional[Any] = len(SCREAMING_SNAKE_CASE ) - 1 lowerCAmelCase : Dict = len(SCREAMING_SNAKE_CASE ) - 1 lowerCAmelCase : Optional[Any] = with_conta lowerCAmelCase : List[Any] = with_conta lowerCAmelCase : str = mc_label lowerCAmelCase : Dict = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(SCREAMING_SNAKE_CASE ) for t in all_inputs ) ) return tensor_datasets def a__ ( ): '''simple docstring''' lowerCAmelCase : Optional[int] = argparse.ArgumentParser() parser.add_argument("--model_name" , type=SCREAMING_SNAKE_CASE , 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=SCREAMING_SNAKE_CASE , type=SCREAMING_SNAKE_CASE , required=SCREAMING_SNAKE_CASE , help="The output directory where the model predictions and checkpoints will be written." , ) parser.add_argument("--train_dataset" , type=SCREAMING_SNAKE_CASE , default="" ) parser.add_argument("--eval_dataset" , type=SCREAMING_SNAKE_CASE , default="" ) parser.add_argument("--seed" , type=SCREAMING_SNAKE_CASE , default=4_2 ) parser.add_argument("--num_train_epochs" , type=SCREAMING_SNAKE_CASE , default=3 ) parser.add_argument("--train_batch_size" , type=SCREAMING_SNAKE_CASE , default=8 ) parser.add_argument("--eval_batch_size" , type=SCREAMING_SNAKE_CASE , default=1_6 ) parser.add_argument("--adam_epsilon" , default=1E-8 , type=SCREAMING_SNAKE_CASE , help="Epsilon for Adam optimizer." ) parser.add_argument("--max_grad_norm" , type=SCREAMING_SNAKE_CASE , default=1 ) parser.add_argument( "--max_steps" , default=-1 , type=SCREAMING_SNAKE_CASE , help=( "If > 0: set total number of training steps to perform. Override num_train_epochs." ) , ) parser.add_argument( "--gradient_accumulation_steps" , type=SCREAMING_SNAKE_CASE , default=1 , help="Number of updates steps to accumulate before performing a backward/update pass." , ) parser.add_argument("--learning_rate" , type=SCREAMING_SNAKE_CASE , default=6.2_5E-5 ) parser.add_argument("--warmup_steps" , default=0 , type=SCREAMING_SNAKE_CASE , help="Linear warmup over warmup_steps." ) parser.add_argument("--lr_schedule" , type=SCREAMING_SNAKE_CASE , default="warmup_linear" ) parser.add_argument("--weight_decay" , type=SCREAMING_SNAKE_CASE , default=0.01 ) parser.add_argument("--lm_coef" , type=SCREAMING_SNAKE_CASE , default=0.9 ) parser.add_argument("--n_valid" , type=SCREAMING_SNAKE_CASE , default=3_7_4 ) parser.add_argument("--server_ip" , type=SCREAMING_SNAKE_CASE , default="" , help="Can be used for distant debugging." ) parser.add_argument("--server_port" , type=SCREAMING_SNAKE_CASE , default="" , help="Can be used for distant debugging." ) lowerCAmelCase : Tuple = parser.parse_args() print(SCREAMING_SNAKE_CASE ) 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=SCREAMING_SNAKE_CASE ) 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 ) lowerCAmelCase : Optional[int] = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) lowerCAmelCase : Optional[int] = torch.cuda.device_count() logger.info("device: {}, n_gpu {}".format(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) 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 lowerCAmelCase : str = ["_start_", "_delimiter_", "_classify_"] lowerCAmelCase : Dict = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(SCREAMING_SNAKE_CASE ) ) model.to(SCREAMING_SNAKE_CASE ) # Load and encode the datasets def tokenize_and_encode(SCREAMING_SNAKE_CASE : Optional[Any] ): if isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(SCREAMING_SNAKE_CASE ) ) elif isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return obj return [tokenize_and_encode(SCREAMING_SNAKE_CASE ) for o in obj] logger.info("Encoding dataset..." ) lowerCAmelCase : Optional[Any] = load_rocstories_dataset(args.train_dataset ) lowerCAmelCase : int = load_rocstories_dataset(args.eval_dataset ) lowerCAmelCase : Tuple = (train_dataset, eval_dataset) lowerCAmelCase : Dict = tokenize_and_encode(SCREAMING_SNAKE_CASE ) # Compute the max input length for the Transformer lowerCAmelCase : Any = model.config.n_positions // 2 - 2 lowerCAmelCase : int = 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 ) lowerCAmelCase : Union[str, Any] = min(SCREAMING_SNAKE_CASE , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders lowerCAmelCase : Any = pre_process_datasets(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , *SCREAMING_SNAKE_CASE ) lowerCAmelCase , lowerCAmelCase : Tuple = tensor_datasets[0], tensor_datasets[1] lowerCAmelCase : List[str] = TensorDataset(*SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = RandomSampler(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = DataLoader(SCREAMING_SNAKE_CASE , sampler=SCREAMING_SNAKE_CASE , batch_size=args.train_batch_size ) lowerCAmelCase : int = TensorDataset(*SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = SequentialSampler(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = DataLoader(SCREAMING_SNAKE_CASE , sampler=SCREAMING_SNAKE_CASE , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: lowerCAmelCase : int = args.max_steps lowerCAmelCase : str = args.max_steps // (len(SCREAMING_SNAKE_CASE ) // args.gradient_accumulation_steps) + 1 else: lowerCAmelCase : Union[str, Any] = len(SCREAMING_SNAKE_CASE ) // args.gradient_accumulation_steps * args.num_train_epochs lowerCAmelCase : Dict = list(model.named_parameters() ) lowerCAmelCase : str = ["bias", "LayerNorm.bias", "LayerNorm.weight"] lowerCAmelCase : Tuple = [ { "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}, ] lowerCAmelCase : Tuple = AdamW(SCREAMING_SNAKE_CASE , lr=args.learning_rate , eps=args.adam_epsilon ) lowerCAmelCase : str = get_linear_schedule_with_warmup( SCREAMING_SNAKE_CASE , num_warmup_steps=args.warmup_steps , num_training_steps=SCREAMING_SNAKE_CASE ) if args.do_train: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Dict = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="Epoch" ): lowerCAmelCase : Optional[int] = 0 lowerCAmelCase : Union[str, Any] = 0 lowerCAmelCase : Tuple = tqdm(SCREAMING_SNAKE_CASE , desc="Training" ) for step, batch in enumerate(SCREAMING_SNAKE_CASE ): lowerCAmelCase : Tuple = tuple(t.to(SCREAMING_SNAKE_CASE ) for t in batch ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : int = batch lowerCAmelCase : Optional[int] = model(SCREAMING_SNAKE_CASE , mc_token_ids=SCREAMING_SNAKE_CASE , lm_labels=SCREAMING_SNAKE_CASE , mc_labels=SCREAMING_SNAKE_CASE ) lowerCAmelCase : int = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() lowerCAmelCase : Optional[Any] = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 lowerCAmelCase : int = "Training loss: {:.2e} lr: {:.2e}".format(SCREAMING_SNAKE_CASE , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer lowerCAmelCase : Optional[int] = model.module if hasattr(SCREAMING_SNAKE_CASE , "module" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` lowerCAmelCase : Any = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE ) torch.save(model_to_save.state_dict() , SCREAMING_SNAKE_CASE ) model_to_save.config.to_json_file(SCREAMING_SNAKE_CASE ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned lowerCAmelCase : List[str] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) lowerCAmelCase : Dict = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(SCREAMING_SNAKE_CASE ) if args.do_eval: model.eval() lowerCAmelCase , lowerCAmelCase : Optional[int] = 0, 0 lowerCAmelCase , lowerCAmelCase : Any = 0, 0 for batch in tqdm(SCREAMING_SNAKE_CASE , desc="Evaluating" ): lowerCAmelCase : List[Any] = tuple(t.to(SCREAMING_SNAKE_CASE ) for t in batch ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Tuple = batch with torch.no_grad(): lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Optional[Any] = model( SCREAMING_SNAKE_CASE , mc_token_ids=SCREAMING_SNAKE_CASE , lm_labels=SCREAMING_SNAKE_CASE , mc_labels=SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = mc_logits.detach().cpu().numpy() lowerCAmelCase : List[str] = mc_labels.to("cpu" ).numpy() lowerCAmelCase : Any = accuracy(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 lowerCAmelCase : List[Any] = eval_loss / nb_eval_steps lowerCAmelCase : List[Any] = eval_accuracy / nb_eval_examples lowerCAmelCase : Tuple = tr_loss / nb_tr_steps if args.do_train else None lowerCAmelCase : Any = {"eval_loss": eval_loss, "eval_accuracy": eval_accuracy, "train_loss": train_loss} lowerCAmelCase : List[str] = os.path.join(args.output_dir , "eval_results.txt" ) with open(SCREAMING_SNAKE_CASE , "w" ) as writer: logger.info("***** Eval results *****" ) for key in sorted(result.keys() ): logger.info(" %s = %s" , SCREAMING_SNAKE_CASE , str(result[key] ) ) writer.write("%s = %s\n" % (key, str(result[key] )) ) if __name__ == "__main__": main()
681
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowerCAmelCase__ = { '''configuration_mvp''': ['''MVP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MvpConfig''', '''MvpOnnxConfig'''], '''tokenization_mvp''': ['''MvpTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''MvpTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''MVP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MvpForCausalLM''', '''MvpForConditionalGeneration''', '''MvpForQuestionAnswering''', '''MvpForSequenceClassification''', '''MvpModel''', '''MvpPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mvp import MVP_PRETRAINED_CONFIG_ARCHIVE_MAP, MvpConfig, MvpOnnxConfig from .tokenization_mvp import MvpTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mvp_fast import MvpTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mvp import ( MVP_PRETRAINED_MODEL_ARCHIVE_LIST, MvpForCausalLM, MvpForConditionalGeneration, MvpForQuestionAnswering, MvpForSequenceClassification, MvpModel, MvpPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
710
"""simple docstring""" from typing import List, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''huggingface/informer-tourism-monthly''': ( '''https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json''' ), # See all Informer models at https://huggingface.co/models?filter=informer } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Optional[Any] ="informer" a : int ={ "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", "num_hidden_layers": "encoder_layers", } def __init__( self , snake_case__ = None , snake_case__ = None , snake_case__ = "student_t" , snake_case__ = "nll" , snake_case__ = 1 , snake_case__ = None , snake_case__ = "mean" , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = None , snake_case__ = None , snake_case__ = 64 , snake_case__ = 32 , snake_case__ = 32 , snake_case__ = 2 , snake_case__ = 2 , snake_case__ = 2 , snake_case__ = 2 , snake_case__ = True , snake_case__ = "gelu" , snake_case__ = 0.05 , snake_case__ = 0.1 , snake_case__ = 0.1 , snake_case__ = 0.1 , snake_case__ = 0.1 , snake_case__ = 100 , snake_case__ = 0.02 , snake_case__=True , snake_case__ = "prob" , snake_case__ = 5 , snake_case__ = True , **snake_case__ , ): """simple docstring""" lowerCAmelCase : Union[str, Any] = prediction_length lowerCAmelCase : Union[str, Any] = context_length or prediction_length lowerCAmelCase : List[Any] = distribution_output lowerCAmelCase : Optional[int] = loss lowerCAmelCase : Optional[int] = input_size lowerCAmelCase : str = num_time_features lowerCAmelCase : Any = lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7] lowerCAmelCase : Dict = scaling lowerCAmelCase : List[str] = num_dynamic_real_features lowerCAmelCase : Dict = num_static_real_features lowerCAmelCase : Dict = num_static_categorical_features # set cardinality if cardinality and num_static_categorical_features > 0: if len(snake_case__ ) != num_static_categorical_features: raise ValueError( "The cardinality should be a list of the same length as `num_static_categorical_features`" ) lowerCAmelCase : List[str] = cardinality else: lowerCAmelCase : Optional[Any] = [0] # set embedding_dimension if embedding_dimension and num_static_categorical_features > 0: if len(snake_case__ ) != num_static_categorical_features: raise ValueError( "The embedding dimension should be a list of the same length as `num_static_categorical_features`" ) lowerCAmelCase : List[Any] = embedding_dimension else: lowerCAmelCase : Dict = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] lowerCAmelCase : List[Any] = num_parallel_samples # Transformer architecture configuration lowerCAmelCase : Any = input_size * len(self.lags_sequence ) + self._number_of_features lowerCAmelCase : str = d_model lowerCAmelCase : List[str] = encoder_attention_heads lowerCAmelCase : int = decoder_attention_heads lowerCAmelCase : Optional[Any] = encoder_ffn_dim lowerCAmelCase : Dict = decoder_ffn_dim lowerCAmelCase : int = encoder_layers lowerCAmelCase : Union[str, Any] = decoder_layers lowerCAmelCase : Tuple = dropout lowerCAmelCase : List[Any] = attention_dropout lowerCAmelCase : int = activation_dropout lowerCAmelCase : Union[str, Any] = encoder_layerdrop lowerCAmelCase : int = decoder_layerdrop lowerCAmelCase : Optional[int] = activation_function lowerCAmelCase : int = init_std lowerCAmelCase : Optional[Any] = use_cache # Informer lowerCAmelCase : Dict = attention_type lowerCAmelCase : Any = sampling_factor lowerCAmelCase : Optional[int] = distil super().__init__(is_encoder_decoder=snake_case__ , **snake_case__ ) @property def lowercase__ ( self ): """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 )
681
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''microsoft/swinv2-tiny-patch4-window8-256''': ( '''https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json''' ), } class SCREAMING_SNAKE_CASE__ ( lowercase_ ): """simple docstring""" a : Tuple ='''swinv2''' a : str ={ '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self , snake_case__=224 , snake_case__=4 , snake_case__=3 , snake_case__=96 , snake_case__=[2, 2, 6, 2] , snake_case__=[3, 6, 12, 24] , snake_case__=7 , snake_case__=4.0 , snake_case__=True , snake_case__=0.0 , snake_case__=0.0 , snake_case__=0.1 , snake_case__="gelu" , snake_case__=False , snake_case__=0.02 , snake_case__=1e-5 , snake_case__=32 , **snake_case__ , ): """simple docstring""" super().__init__(**UpperCamelCase__ ) lowerCAmelCase : Optional[int] = image_size lowerCAmelCase : Any = patch_size lowerCAmelCase : Optional[Any] = num_channels lowerCAmelCase : Optional[int] = embed_dim lowerCAmelCase : Dict = depths lowerCAmelCase : int = len(UpperCamelCase__ ) lowerCAmelCase : List[str] = num_heads lowerCAmelCase : Optional[int] = window_size lowerCAmelCase : Dict = mlp_ratio lowerCAmelCase : int = qkv_bias lowerCAmelCase : Tuple = hidden_dropout_prob lowerCAmelCase : List[Any] = attention_probs_dropout_prob lowerCAmelCase : int = drop_path_rate lowerCAmelCase : str = hidden_act lowerCAmelCase : Optional[int] = use_absolute_embeddings lowerCAmelCase : List[Any] = layer_norm_eps lowerCAmelCase : Any = initializer_range lowerCAmelCase : Any = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowerCAmelCase : str = int(embed_dim * 2 ** (len(UpperCamelCase__ ) - 1) ) lowerCAmelCase : Dict = (0, 0, 0, 0)
711
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' if num < 0: return False lowerCAmelCase : int = num lowerCAmelCase : int = 0 while num > 0: lowerCAmelCase : Dict = rev_num * 1_0 + (num % 1_0) num //= 1_0 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
681
0
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "hustvl/yolos-small": "https://huggingface.co/hustvl/yolos-small/resolve/main/config.json", # See all YOLOS models at https://huggingface.co/models?filter=yolos } class SCREAMING_SNAKE_CASE__ ( __UpperCAmelCase ): """simple docstring""" a : str ="yolos" def __init__( self , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3_072 , snake_case__="gelu" , snake_case__=0.0 , snake_case__=0.0 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__=[512, 864] , snake_case__=16 , snake_case__=3 , snake_case__=True , snake_case__=100 , snake_case__=True , snake_case__=False , snake_case__=1 , snake_case__=5 , snake_case__=2 , snake_case__=5 , snake_case__=2 , snake_case__=0.1 , **snake_case__ , ): """simple docstring""" super().__init__(**lowerCAmelCase_ ) lowerCAmelCase : Any = hidden_size lowerCAmelCase : Dict = num_hidden_layers lowerCAmelCase : List[str] = num_attention_heads lowerCAmelCase : List[Any] = intermediate_size lowerCAmelCase : List[Any] = hidden_act lowerCAmelCase : Optional[Any] = hidden_dropout_prob lowerCAmelCase : Optional[int] = attention_probs_dropout_prob lowerCAmelCase : Dict = initializer_range lowerCAmelCase : Union[str, Any] = layer_norm_eps lowerCAmelCase : Any = image_size lowerCAmelCase : Optional[Any] = patch_size lowerCAmelCase : Optional[Any] = num_channels lowerCAmelCase : Dict = qkv_bias lowerCAmelCase : Optional[Any] = num_detection_tokens lowerCAmelCase : List[Any] = use_mid_position_embeddings lowerCAmelCase : Tuple = auxiliary_loss # Hungarian matcher lowerCAmelCase : Dict = class_cost lowerCAmelCase : Optional[Any] = bbox_cost lowerCAmelCase : Tuple = giou_cost # Loss coefficients lowerCAmelCase : Dict = bbox_loss_coefficient lowerCAmelCase : Union[str, Any] = giou_loss_coefficient lowerCAmelCase : List[str] = eos_coefficient class SCREAMING_SNAKE_CASE__ ( __UpperCAmelCase ): """simple docstring""" a : int =version.parse("1.11" ) @property def lowercase__ ( self ): """simple docstring""" return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowercase__ ( self ): """simple docstring""" return 1e-4 @property def lowercase__ ( self ): """simple docstring""" return 12
712
"""simple docstring""" import os import re import shutil import sys import tempfile import unittest import black lowerCAmelCase__ = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, '''utils''')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If DDPMSchedulerOutput is changed in scheduling_ddpm.py, this code needs to be manually updated. lowerCAmelCase__ = ''' \""" Output class for the scheduler\'s step function output. Args: prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the denoising loop. pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): The predicted denoised sample (x_{0}) based on the model output from the current timestep. `pred_original_sample` can be used to preview progress or for guidance. \""" prev_sample: torch.FloatTensor pred_original_sample: Optional[torch.FloatTensor] = None ''' class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = tempfile.mkdtemp() os.makedirs(os.path.join(self.diffusers_dir , "schedulers/" ) ) lowerCAmelCase : List[str] = self.diffusers_dir shutil.copy( os.path.join(snake_case__ , "src/diffusers/schedulers/scheduling_ddpm.py" ) , os.path.join(self.diffusers_dir , "schedulers/scheduling_ddpm.py" ) , ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = "src/diffusers" shutil.rmtree(self.diffusers_dir ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__=None ): """simple docstring""" lowerCAmelCase : Union[str, Any] = comment + f"""\nclass {class_name}(nn.Module):\n""" + class_code if overwrite_result is not None: lowerCAmelCase : str = comment + f"""\nclass {class_name}(nn.Module):\n""" + overwrite_result lowerCAmelCase : int = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) lowerCAmelCase : int = black.format_str(snake_case__ , mode=snake_case__ ) lowerCAmelCase : Dict = os.path.join(self.diffusers_dir , "new_code.py" ) with open(snake_case__ , "w" , newline="\n" ) as f: f.write(snake_case__ ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(snake_case__ ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=snake_case__ ) with open(snake_case__ , "r" ) as f: self.assertTrue(f.read() , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = check_copies.find_code_in_diffusers("schedulers.scheduling_ddpm.DDPMSchedulerOutput" ) self.assertEqual(snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput" , "DDPMSchedulerOutput" , REFERENCE_CODE + "\n" , ) # With no empty line at the end self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput" , "DDPMSchedulerOutput" , snake_case__ , ) # Copy consistency with rename self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test" , "TestSchedulerOutput" , re.sub("DDPM" , "Test" , snake_case__ ) , ) # Copy consistency with a really long name lowerCAmelCase : Union[str, Any] = "TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason" self.check_copy_consistency( f"""# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}""" , f"""{long_class_name}SchedulerOutput""" , re.sub("Bert" , snake_case__ , snake_case__ ) , ) # Copy consistency with overwrite self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test" , "TestSchedulerOutput" , snake_case__ , overwrite_result=re.sub("DDPM" , "Test" , snake_case__ ) , )
681
0
"""simple docstring""" import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XLMRobertaTokenizer, XLMRobertaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase__ = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece @require_tokenizers class SCREAMING_SNAKE_CASE__ ( _UpperCamelCase , unittest.TestCase ): """simple docstring""" a : List[str] =XLMRobertaTokenizer a : Optional[Any] =XLMRobertaTokenizerFast a : int =True a : str =True def lowercase__ ( self ): """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing lowerCAmelCase : Union[str, Any] = XLMRobertaTokenizer(_UpperCAmelCase , keep_accents=_UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = '''<pad>''' lowerCAmelCase : Optional[int] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_UpperCAmelCase ) , _UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_UpperCAmelCase ) , _UpperCAmelCase ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<s>" ) self.assertEqual(vocab_keys[1] , "<pad>" ) self.assertEqual(vocab_keys[-1] , "<mask>" ) self.assertEqual(len(_UpperCAmelCase ) , 1_002 ) def lowercase__ ( self ): """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1_002 ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = XLMRobertaTokenizer(_UpperCAmelCase , keep_accents=_UpperCAmelCase ) lowerCAmelCase : Any = tokenizer.tokenize("This is a test" ) self.assertListEqual(_UpperCAmelCase , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) lowerCAmelCase : Dict = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( _UpperCAmelCase , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) lowerCAmelCase : Dict = tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) self.assertListEqual( _UpperCAmelCase , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] , ) lowerCAmelCase : Dict = tokenizer.convert_ids_to_tokens(_UpperCAmelCase ) self.assertListEqual( _UpperCAmelCase , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) def lowercase__ ( self ): """simple docstring""" if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return lowerCAmelCase : Tuple = (self.rust_tokenizer_class, '''hf-internal-testing/tiny-xlm-roberta''', {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): lowerCAmelCase : Any = self.rust_tokenizer_class.from_pretrained(_UpperCAmelCase , **_UpperCAmelCase ) lowerCAmelCase : Optional[int] = self.tokenizer_class.from_pretrained(_UpperCAmelCase , **_UpperCAmelCase ) lowerCAmelCase : Union[str, Any] = tempfile.mkdtemp() lowerCAmelCase : Optional[int] = tokenizer_r.save_pretrained(_UpperCAmelCase ) lowerCAmelCase : Tuple = tokenizer_p.save_pretrained(_UpperCAmelCase ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) lowerCAmelCase : Tuple = tuple(f for f in tokenizer_r_files if "tokenizer.json" not in f ) self.assertSequenceEqual(_UpperCAmelCase , _UpperCAmelCase ) # Checks everything loads correctly in the same way lowerCAmelCase : Tuple = tokenizer_r.from_pretrained(_UpperCAmelCase ) lowerCAmelCase : Union[str, Any] = tokenizer_p.from_pretrained(_UpperCAmelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(_UpperCAmelCase , _UpperCAmelCase ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(_UpperCAmelCase ) # Save tokenizer rust, legacy_format=True lowerCAmelCase : Tuple = tempfile.mkdtemp() lowerCAmelCase : Dict = tokenizer_r.save_pretrained(_UpperCAmelCase , legacy_format=_UpperCAmelCase ) lowerCAmelCase : Any = tokenizer_p.save_pretrained(_UpperCAmelCase ) # Checks it save with the same files self.assertSequenceEqual(_UpperCAmelCase , _UpperCAmelCase ) # Checks everything loads correctly in the same way lowerCAmelCase : int = tokenizer_r.from_pretrained(_UpperCAmelCase ) lowerCAmelCase : int = tokenizer_p.from_pretrained(_UpperCAmelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(_UpperCAmelCase , _UpperCAmelCase ) ) shutil.rmtree(_UpperCAmelCase ) # Save tokenizer rust, legacy_format=False lowerCAmelCase : List[Any] = tempfile.mkdtemp() lowerCAmelCase : Union[str, Any] = tokenizer_r.save_pretrained(_UpperCAmelCase , legacy_format=_UpperCAmelCase ) lowerCAmelCase : int = tokenizer_p.save_pretrained(_UpperCAmelCase ) # Checks it saved the tokenizer.json file self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way lowerCAmelCase : List[Any] = tokenizer_r.from_pretrained(_UpperCAmelCase ) lowerCAmelCase : List[str] = tokenizer_p.from_pretrained(_UpperCAmelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(_UpperCAmelCase , _UpperCAmelCase ) ) shutil.rmtree(_UpperCAmelCase ) @cached_property def lowercase__ ( self ): """simple docstring""" return XLMRobertaTokenizer.from_pretrained("xlm-roberta-base" ) def lowercase__ ( self ): """simple docstring""" with tempfile.NamedTemporaryFile() as f: shutil.copyfile(_UpperCAmelCase , f.name ) lowerCAmelCase : int = XLMRobertaTokenizer(f.name , keep_accents=_UpperCAmelCase ) lowerCAmelCase : List[Any] = pickle.dumps(_UpperCAmelCase ) pickle.loads(_UpperCAmelCase ) def lowercase__ ( self ): """simple docstring""" if not self.test_rust_tokenizer: return lowerCAmelCase : str = self.get_tokenizer() lowerCAmelCase : Union[str, Any] = self.get_rust_tokenizer() lowerCAmelCase : int = '''I was born in 92000, and this is falsé.''' lowerCAmelCase : Optional[int] = tokenizer.tokenize(_UpperCAmelCase ) lowerCAmelCase : str = rust_tokenizer.tokenize(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase : str = tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) lowerCAmelCase : Tuple = rust_tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase : Optional[int] = self.get_rust_tokenizer() lowerCAmelCase : str = tokenizer.encode(_UpperCAmelCase ) lowerCAmelCase : Any = rust_tokenizer.encode(_UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) @slow def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = '''Hello World!''' lowerCAmelCase : str = [0, 35_378, 6_661, 38, 2] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(_UpperCAmelCase , self.big_tokenizer.encode(_UpperCAmelCase ) ) @slow def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = ( '''This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will''' ''' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth''' ) lowerCAmelCase : Optional[Any] = [ 0, 3_293, 83, 10, 4_552, 4_989, 7_986, 678, 10, 5_915, 111, 179_459, 124_850, 4, 6_044, 237, 12, 6, 5, 6, 4, 6_780, 705, 15, 1_388, 44, 378, 10_114, 711, 152, 20, 6, 5, 22_376, 642, 1_221, 15_190, 34_153, 450, 5_608, 959, 1_119, 57_702, 136, 186, 47, 1_098, 29_367, 47, # 4426, # What fairseq tokenizes from "<unk>": "_<" # 3678, # What fairseq tokenizes from "<unk>": "unk" # 2740, # What fairseq tokenizes from "<unk>": ">" 3, # What we tokenize from "<unk>": "<unk>" 6, # Residue from the tokenization: an extra sentencepiece underline 4, 6_044, 237, 6_284, 50_901, 528, 31, 90, 34, 927, 2, ] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(_UpperCAmelCase , self.big_tokenizer.encode(_UpperCAmelCase ) ) @slow def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = {'''input_ids''': [[0, 11_062, 82_772, 7, 15, 82_772, 538, 51_529, 237, 17_198, 1_290, 206, 9, 215_175, 1_314, 136, 17_198, 1_290, 206, 9, 56_359, 42, 122_009, 9, 16_466, 16, 87_344, 4_537, 9, 4_717, 78_381, 6, 159_958, 7, 15, 24_480, 618, 4, 527, 22_693, 5_428, 4, 2_777, 24_480, 9_874, 4, 43_523, 594, 4, 803, 18_392, 33_189, 18, 4, 43_523, 24_447, 12_399, 100, 24_955, 83_658, 9_626, 144_057, 15, 839, 22_335, 16, 136, 24_955, 83_658, 83_479, 15, 39_102, 724, 16, 678, 645, 2_789, 1_328, 4_589, 42, 122_009, 115_774, 23, 805, 1_328, 46_876, 7, 136, 53_894, 1_940, 42_227, 41_159, 17_721, 823, 425, 4, 27_512, 98_722, 206, 136, 5_531, 4_970, 919, 17_336, 5, 2], [0, 20_080, 618, 83, 82_775, 47, 479, 9, 1_517, 73, 53_894, 333, 80_581, 110_117, 18_811, 5_256, 1_295, 51, 152_526, 297, 7_986, 390, 124_416, 538, 35_431, 214, 98, 15_044, 25_737, 136, 7_108, 43_701, 23, 756, 135_355, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 581, 63_773, 119_455, 6, 147_797, 88_203, 7, 645, 70, 21, 3_285, 10_269, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_UpperCAmelCase , model_name="xlm-roberta-base" , revision="d9d8a8ea5eb94b1c6654ae9249df7793cd2933d3" , )
713
"""simple docstring""" import re from flax.core.frozen_dict import freeze from flax.traverse_util import flatten_dict, unflatten_dict from jax.experimental import PartitionSpec as P # Sentinels lowerCAmelCase__ = object() # For specifying empty leaf dict `{}` lowerCAmelCase__ = object() def a__ ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' lowerCAmelCase : Optional[int] = tuple((re.compile(x + "$" ) for x in qs) ) for i in range(len(SCREAMING_SNAKE_CASE ) - len(SCREAMING_SNAKE_CASE ) + 1 ): lowerCAmelCase : int = [x.match(SCREAMING_SNAKE_CASE ) for x, y in zip(SCREAMING_SNAKE_CASE , ks[i:] )] if matches and all(SCREAMING_SNAKE_CASE ): return True return False def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' def replace(SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Dict ): for rule, replacement in rules: if _match(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return replacement return val return replace def a__ ( ): '''simple docstring''' return [ # embeddings (("transformer", "wpe", "embedding"), P("mp" , SCREAMING_SNAKE_CASE )), (("transformer", "wte", "embedding"), P("mp" , SCREAMING_SNAKE_CASE )), # atention (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(SCREAMING_SNAKE_CASE , "mp" )), (("attention", "out_proj", "kernel"), P("mp" , SCREAMING_SNAKE_CASE )), (("attention", "out_proj", "bias"), None), # mlp (("mlp", "c_fc", "kernel"), P(SCREAMING_SNAKE_CASE , "mp" )), (("mlp", "c_fc", "bias"), P("mp" )), (("mlp", "c_proj", "kernel"), P("mp" , SCREAMING_SNAKE_CASE )), (("mlp", "c_proj", "bias"), None), # layer norms ((r"ln_\d+", "bias"), None), ((r"\d+", r"ln_\d+", "scale"), None), (("ln_f", "bias"), None), (("ln_f", "scale"), None), ] def a__ ( SCREAMING_SNAKE_CASE : Union[str, Any] ): '''simple docstring''' lowerCAmelCase : Any = _get_partition_rules() lowerCAmelCase : Tuple = _replacement_rules(SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = {k: _unmatched for k in flatten_dict(SCREAMING_SNAKE_CASE )} lowerCAmelCase : List[Any] = {k: replace(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) for k, v in initd.items()} assert _unmatched not in result.values(), "Incomplete partition spec." return freeze(unflatten_dict(SCREAMING_SNAKE_CASE ) )
681
0
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPanoramaPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() @skip_mps class SCREAMING_SNAKE_CASE__ ( _a , _a , unittest.TestCase ): """simple docstring""" a : Any =StableDiffusionPanoramaPipeline a : int =TEXT_TO_IMAGE_PARAMS a : Dict =TEXT_TO_IMAGE_BATCH_PARAMS a : int =TEXT_TO_IMAGE_IMAGE_PARAMS a : List[str] =TEXT_TO_IMAGE_IMAGE_PARAMS def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCAmelCase : Union[str, Any] = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=1 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("DownBlock2D", "CrossAttnDownBlock2D") , up_block_types=("CrossAttnUpBlock2D", "UpBlock2D") , cross_attention_dim=32 , ) lowerCAmelCase : Optional[int] = DDIMScheduler() torch.manual_seed(0 ) lowerCAmelCase : List[Any] = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , ) torch.manual_seed(0 ) lowerCAmelCase : Any = 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=1_000 , ) lowerCAmelCase : Optional[int] = CLIPTextModel(_A ) lowerCAmelCase : Optional[Any] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) lowerCAmelCase : Optional[Any] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def lowercase__ ( self , snake_case__ , snake_case__=0 ): """simple docstring""" lowerCAmelCase : int = torch.manual_seed(_A ) lowerCAmelCase : Dict = { "prompt": "a photo of the dolomites", "generator": generator, # Setting height and width to None to prevent OOMs on CPU. "height": None, "width": None, "num_inference_steps": 1, "guidance_scale": 6.0, "output_type": "numpy", } return inputs def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = "cpu" # ensure determinism for the device-dependent torch.Generator lowerCAmelCase : Tuple = self.get_dummy_components() lowerCAmelCase : List[Any] = StableDiffusionPanoramaPipeline(**_A ) lowerCAmelCase : Any = sd_pipe.to(_A ) sd_pipe.set_progress_bar_config(disable=_A ) lowerCAmelCase : Dict = self.get_dummy_inputs(_A ) lowerCAmelCase : Optional[int] = sd_pipe(**_A ).images lowerCAmelCase : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase : Optional[Any] = np.array([0.6186, 0.5374, 0.4915, 0.4135, 0.4114, 0.4563, 0.5128, 0.4977, 0.4757] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowercase__ ( self ): """simple docstring""" super().test_inference_batch_consistent(batch_sizes=[1, 2] ) def lowercase__ ( self ): """simple docstring""" super().test_inference_batch_single_identical(batch_size=2 , expected_max_diff=3.25e-3 ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = "cpu" # ensure determinism for the device-dependent torch.Generator lowerCAmelCase : List[Any] = self.get_dummy_components() lowerCAmelCase : List[str] = StableDiffusionPanoramaPipeline(**_A ) lowerCAmelCase : str = sd_pipe.to(_A ) sd_pipe.set_progress_bar_config(disable=_A ) lowerCAmelCase : Optional[Any] = self.get_dummy_inputs(_A ) lowerCAmelCase : Any = "french fries" lowerCAmelCase : Optional[int] = sd_pipe(**_A , negative_prompt=_A ) lowerCAmelCase : Optional[int] = output.images lowerCAmelCase : List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase : str = np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = "cpu" # ensure determinism for the device-dependent torch.Generator lowerCAmelCase : Any = self.get_dummy_components() lowerCAmelCase : Any = StableDiffusionPanoramaPipeline(**_A ) lowerCAmelCase : Optional[Any] = sd_pipe.to(_A ) sd_pipe.set_progress_bar_config(disable=_A ) lowerCAmelCase : Dict = self.get_dummy_inputs(_A ) lowerCAmelCase : str = sd_pipe(**_A , view_batch_size=2 ) lowerCAmelCase : Optional[Any] = output.images lowerCAmelCase : int = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase : str = np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = "cpu" # ensure determinism for the device-dependent torch.Generator lowerCAmelCase : Dict = self.get_dummy_components() lowerCAmelCase : List[str] = EulerAncestralDiscreteScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule="scaled_linear" ) lowerCAmelCase : Tuple = StableDiffusionPanoramaPipeline(**_A ) lowerCAmelCase : int = sd_pipe.to(_A ) sd_pipe.set_progress_bar_config(disable=_A ) lowerCAmelCase : Dict = self.get_dummy_inputs(_A ) lowerCAmelCase : List[str] = sd_pipe(**_A ).images lowerCAmelCase : Optional[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase : Union[str, Any] = np.array([0.4024, 0.6510, 0.4901, 0.5378, 0.5813, 0.5622, 0.4795, 0.4467, 0.4952] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = "cpu" # ensure determinism for the device-dependent torch.Generator lowerCAmelCase : Optional[Any] = self.get_dummy_components() lowerCAmelCase : Dict = PNDMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule="scaled_linear" , skip_prk_steps=_A ) lowerCAmelCase : List[Any] = StableDiffusionPanoramaPipeline(**_A ) lowerCAmelCase : Dict = sd_pipe.to(_A ) sd_pipe.set_progress_bar_config(disable=_A ) lowerCAmelCase : Union[str, Any] = self.get_dummy_inputs(_A ) lowerCAmelCase : Tuple = sd_pipe(**_A ).images lowerCAmelCase : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase : Union[str, Any] = np.array([0.6391, 0.6291, 0.4861, 0.5134, 0.5552, 0.4578, 0.5032, 0.5023, 0.4539] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def lowercase__ ( self , snake_case__=0 ): """simple docstring""" lowerCAmelCase : int = torch.manual_seed(_A ) lowerCAmelCase : str = { "prompt": "a photo of the dolomites", "generator": generator, "num_inference_steps": 3, "guidance_scale": 7.5, "output_type": "numpy", } return inputs def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = "stabilityai/stable-diffusion-2-base" lowerCAmelCase : Optional[Any] = DDIMScheduler.from_pretrained(_A , subfolder="scheduler" ) lowerCAmelCase : Union[str, Any] = StableDiffusionPanoramaPipeline.from_pretrained(_A , scheduler=_A , safety_checker=_A ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) pipe.enable_attention_slicing() lowerCAmelCase : Tuple = self.get_inputs() lowerCAmelCase : Any = pipe(**_A ).images lowerCAmelCase : List[Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 2_048, 3) lowerCAmelCase : int = np.array( [ 0.36968392, 0.27025372, 0.32446766, 0.28379387, 0.36363274, 0.30733347, 0.27100027, 0.27054125, 0.25536096, ] ) assert np.abs(expected_slice - image_slice ).max() < 1e-2 def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Tuple = StableDiffusionPanoramaPipeline.from_pretrained( "stabilityai/stable-diffusion-2-base" , safety_checker=_A ) lowerCAmelCase : int = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) pipe.enable_attention_slicing() lowerCAmelCase : Optional[Any] = self.get_inputs() lowerCAmelCase : Tuple = pipe(**_A ).images lowerCAmelCase : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 2_048, 3) lowerCAmelCase : str = np.array( [ [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ] ] ) assert np.abs(expected_slice - image_slice ).max() < 1e-3 def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = 0 def callback_fn(snake_case__ , snake_case__ , snake_case__ ) -> None: lowerCAmelCase : Tuple = True nonlocal number_of_steps number_of_steps += 1 if step == 1: lowerCAmelCase : List[Any] = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 256) lowerCAmelCase : Any = latents[0, -3:, -3:, -1] lowerCAmelCase : Optional[int] = np.array( [ 0.18681869, 0.33907816, 0.5361276, 0.14432865, -0.02856611, -0.73941123, 0.23397987, 0.47322682, -0.37823164, ] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 elif step == 2: lowerCAmelCase : List[Any] = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 256) lowerCAmelCase : int = latents[0, -3:, -3:, -1] lowerCAmelCase : List[str] = np.array( [ 0.18539645, 0.33987248, 0.5378559, 0.14437142, -0.02455261, -0.7338317, 0.23990755, 0.47356272, -0.3786505, ] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 lowerCAmelCase : Tuple = False lowerCAmelCase : List[str] = "stabilityai/stable-diffusion-2-base" lowerCAmelCase : Any = DDIMScheduler.from_pretrained(_A , subfolder="scheduler" ) lowerCAmelCase : List[str] = StableDiffusionPanoramaPipeline.from_pretrained(_A , scheduler=_A , safety_checker=_A ) lowerCAmelCase : List[Any] = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) pipe.enable_attention_slicing() lowerCAmelCase : Tuple = self.get_inputs() pipe(**_A , callback=_A , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def lowercase__ ( self ): """simple docstring""" torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() lowerCAmelCase : Optional[int] = "stabilityai/stable-diffusion-2-base" lowerCAmelCase : List[str] = DDIMScheduler.from_pretrained(_A , subfolder="scheduler" ) lowerCAmelCase : Tuple = StableDiffusionPanoramaPipeline.from_pretrained(_A , scheduler=_A , safety_checker=_A ) lowerCAmelCase : str = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() lowerCAmelCase : str = self.get_inputs() lowerCAmelCase : Tuple = pipe(**_A ) lowerCAmelCase : Dict = torch.cuda.max_memory_allocated() # make sure that less than 5.2 GB is allocated assert mem_bytes < 5.5 * 10**9
714
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : int = 1_0 , SCREAMING_SNAKE_CASE : int = 2_2 ): '''simple docstring''' lowerCAmelCase : Dict = range(1 , SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = range(1 , SCREAMING_SNAKE_CASE ) return sum( 1 for power in powers for base in bases if len(str(base**power ) ) == power ) if __name__ == "__main__": print(F"{solution(10, 22) = }")
681
0
"""simple docstring""" import os from pathlib import Path from unittest.mock import patch import pytest import zstandard as zstd from datasets.download.download_config import DownloadConfig from datasets.utils.file_utils import ( OfflineModeIsEnabled, cached_path, fsspec_get, fsspec_head, ftp_get, ftp_head, get_from_cache, http_get, http_head, ) lowerCAmelCase__ = '''\ Text data. Second line of data.''' lowerCAmelCase__ = '''file''' @pytest.fixture(scope="session" ) def a__ ( SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = tmp_path_factory.mktemp("data" ) / (FILE_PATH + ".zstd") lowerCAmelCase : str = bytes(UpperCAmelCase__ , "utf-8" ) with zstd.open(UpperCAmelCase__ , "wb" ) as f: f.write(UpperCAmelCase__ ) return path @pytest.fixture def a__ ( SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' with open(os.path.join(tmpfs.local_root_dir , UpperCAmelCase__ ) , "w" ) as f: f.write(UpperCAmelCase__ ) return FILE_PATH @pytest.mark.parametrize("compression_format" , ["gzip", "xz", "zstd"] ) def a__ ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = {"gzip": gz_file, "xz": xz_file, "zstd": zstd_path} lowerCAmelCase : Optional[Any] = input_paths[compression_format] lowerCAmelCase : str = tmp_path / "cache" lowerCAmelCase : Optional[int] = DownloadConfig(cache_dir=UpperCAmelCase__ , extract_compressed_file=UpperCAmelCase__ ) lowerCAmelCase : Tuple = cached_path(UpperCAmelCase__ , download_config=UpperCAmelCase__ ) with open(UpperCAmelCase__ ) as f: lowerCAmelCase : List[Any] = f.read() with open(UpperCAmelCase__ ) as f: lowerCAmelCase : Optional[Any] = f.read() assert extracted_file_content == expected_file_content @pytest.mark.parametrize("default_extracted" , [True, False] ) @pytest.mark.parametrize("default_cache_dir" , [True, False] ) def a__ ( SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : int ): '''simple docstring''' lowerCAmelCase : List[str] = "custom_cache" lowerCAmelCase : List[str] = "custom_extracted_dir" lowerCAmelCase : int = tmp_path / "custom_extracted_path" if default_extracted: lowerCAmelCase : int = ("downloads" if default_cache_dir else custom_cache_dir, "extracted") else: monkeypatch.setattr("datasets.config.EXTRACTED_DATASETS_DIR" , UpperCAmelCase__ ) monkeypatch.setattr("datasets.config.EXTRACTED_DATASETS_PATH" , str(UpperCAmelCase__ ) ) lowerCAmelCase : int = custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir) lowerCAmelCase : Any = xz_file lowerCAmelCase : Tuple = ( DownloadConfig(extract_compressed_file=UpperCAmelCase__ ) if default_cache_dir else DownloadConfig(cache_dir=tmp_path / custom_cache_dir , extract_compressed_file=UpperCAmelCase__ ) ) lowerCAmelCase : Optional[Any] = cached_path(UpperCAmelCase__ , download_config=UpperCAmelCase__ ) assert Path(UpperCAmelCase__ ).parent.parts[-2:] == expected def a__ ( SCREAMING_SNAKE_CASE : str ): '''simple docstring''' lowerCAmelCase : Optional[int] = str(Path(UpperCAmelCase__ ).resolve() ) assert cached_path(UpperCAmelCase__ ) == text_file # relative path lowerCAmelCase : Optional[int] = str(Path(UpperCAmelCase__ ).resolve().relative_to(Path(os.getcwd() ) ) ) assert cached_path(UpperCAmelCase__ ) == text_file def a__ ( SCREAMING_SNAKE_CASE : Tuple ): '''simple docstring''' lowerCAmelCase : str = str(tmp_path.resolve() / "__missing_file__.txt" ) with pytest.raises(UpperCAmelCase__ ): cached_path(UpperCAmelCase__ ) # relative path lowerCAmelCase : Optional[int] = "./__missing_file__.txt" with pytest.raises(UpperCAmelCase__ ): cached_path(UpperCAmelCase__ ) def a__ ( SCREAMING_SNAKE_CASE : List[Any] ): '''simple docstring''' lowerCAmelCase : Tuple = get_from_cache(f"""tmp://{tmpfs_file}""" ) with open(UpperCAmelCase__ ) as f: lowerCAmelCase : List[str] = f.read() assert output_file_content == FILE_CONTENT @patch("datasets.config.HF_DATASETS_OFFLINE" , UpperCAmelCase__ ) def a__ ( ): '''simple docstring''' with pytest.raises(UpperCAmelCase__ ): cached_path("https://huggingface.co" ) @patch("datasets.config.HF_DATASETS_OFFLINE" , UpperCAmelCase__ ) def a__ ( SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : Dict = tmp_path_factory.mktemp("data" ) / "file.html" with pytest.raises(UpperCAmelCase__ ): http_get("https://huggingface.co" , temp_file=UpperCAmelCase__ ) with pytest.raises(UpperCAmelCase__ ): http_head("https://huggingface.co" ) @patch("datasets.config.HF_DATASETS_OFFLINE" , UpperCAmelCase__ ) def a__ ( SCREAMING_SNAKE_CASE : List[Any] ): '''simple docstring''' lowerCAmelCase : Optional[int] = tmp_path_factory.mktemp("data" ) / "file.html" with pytest.raises(UpperCAmelCase__ ): ftp_get("ftp://huggingface.co" , temp_file=UpperCAmelCase__ ) with pytest.raises(UpperCAmelCase__ ): ftp_head("ftp://huggingface.co" ) @patch("datasets.config.HF_DATASETS_OFFLINE" , UpperCAmelCase__ ) def a__ ( SCREAMING_SNAKE_CASE : Union[str, Any] ): '''simple docstring''' lowerCAmelCase : Optional[int] = tmp_path_factory.mktemp("data" ) / "file.html" with pytest.raises(UpperCAmelCase__ ): fsspec_get("s3://huggingface.co" , temp_file=UpperCAmelCase__ ) with pytest.raises(UpperCAmelCase__ ): fsspec_head("s3://huggingface.co" )
715
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : Optional[int] = len(SCREAMING_SNAKE_CASE ) while cur > 1: # Find the maximum number in arr lowerCAmelCase : List[str] = arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi lowerCAmelCase : str = arr[mi::-1] + arr[mi + 1 : len(SCREAMING_SNAKE_CASE )] # Reverse whole list lowerCAmelCase : str = arr[cur - 1 :: -1] + arr[cur : len(SCREAMING_SNAKE_CASE )] cur -= 1 return arr if __name__ == "__main__": lowerCAmelCase__ = input('''Enter numbers separated by a comma:\n''').strip() lowerCAmelCase__ = [int(item) for item in user_input.split(''',''')] print(pancake_sort(unsorted))
681
0
"""simple docstring""" lowerCAmelCase__ = '''Tobias Carryer''' from time import time class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__ , snake_case__ , snake_case__=int(time() ) ): # noqa: B008 """simple docstring""" lowerCAmelCase : Optional[int] = multiplier lowerCAmelCase : List[str] = increment lowerCAmelCase : Union[str, Any] = modulo lowerCAmelCase : str = seed def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Tuple = (self.multiplier * self.seed + self.increment) % self.modulo return self.seed if __name__ == "__main__": # Show the LCG in action. lowerCAmelCase__ = LinearCongruentialGenerator(1_664_525, 1_013_904_223, 2 << 31) while True: print(lcg.next_number())
716
"""simple docstring""" from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
681
0
import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset lowerCAmelCase__ = random.Random() def a__ ( SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Dict=1.0 , SCREAMING_SNAKE_CASE : Union[str, Any]=None , SCREAMING_SNAKE_CASE : Optional[Any]=None ): '''simple docstring''' if rng is None: lowerCAmelCase : Any = global_rng lowerCAmelCase : Tuple = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def __init__( self , snake_case__ , snake_case__=7 , snake_case__=400 , snake_case__=2_000 , snake_case__=2_048 , snake_case__=128 , snake_case__=1 , snake_case__=512 , snake_case__=30 , snake_case__=44_100 , ): """simple docstring""" lowerCAmelCase : Union[str, Any] = parent lowerCAmelCase : Any = batch_size lowerCAmelCase : str = min_seq_length lowerCAmelCase : Dict = max_seq_length lowerCAmelCase : Optional[int] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) lowerCAmelCase : Optional[Any] = spectrogram_length lowerCAmelCase : Optional[Any] = feature_size lowerCAmelCase : Tuple = num_audio_channels lowerCAmelCase : str = hop_length lowerCAmelCase : str = chunk_length lowerCAmelCase : List[str] = sampling_rate def lowercase__ ( self ): """simple docstring""" return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def lowercase__ ( self , snake_case__=False , snake_case__=False ): """simple docstring""" def _flatten(snake_case__ ): return list(itertools.chain(*snake_case__ ) ) if equal_length: lowerCAmelCase : List[Any] = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size lowerCAmelCase : str = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: lowerCAmelCase : int = [np.asarray(snake_case__ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class SCREAMING_SNAKE_CASE__ ( __UpperCAmelCase , unittest.TestCase ): """simple docstring""" a : Tuple =TvltFeatureExtractor def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = TvltFeatureExtractionTester(self ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(snake_case__ , "spectrogram_length" ) ) self.assertTrue(hasattr(snake_case__ , "feature_size" ) ) self.assertTrue(hasattr(snake_case__ , "num_audio_channels" ) ) self.assertTrue(hasattr(snake_case__ , "hop_length" ) ) self.assertTrue(hasattr(snake_case__ , "chunk_length" ) ) self.assertTrue(hasattr(snake_case__ , "sampling_rate" ) ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: lowerCAmelCase : List[str] = feat_extract_first.save_pretrained(snake_case__ )[0] check_json_file_has_correct_format(snake_case__ ) lowerCAmelCase : Dict = self.feature_extraction_class.from_pretrained(snake_case__ ) lowerCAmelCase : Dict = feat_extract_first.to_dict() lowerCAmelCase : Tuple = feat_extract_second.to_dict() lowerCAmelCase : Any = dict_first.pop("mel_filters" ) lowerCAmelCase : Optional[Any] = dict_second.pop("mel_filters" ) self.assertTrue(np.allclose(snake_case__ , snake_case__ ) ) self.assertEqual(snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: lowerCAmelCase : Optional[Any] = os.path.join(snake_case__ , "feat_extract.json" ) feat_extract_first.to_json_file(snake_case__ ) lowerCAmelCase : Dict = self.feature_extraction_class.from_json_file(snake_case__ ) lowerCAmelCase : Any = feat_extract_first.to_dict() lowerCAmelCase : Dict = feat_extract_second.to_dict() lowerCAmelCase : Optional[Any] = dict_first.pop("mel_filters" ) lowerCAmelCase : List[Any] = dict_second.pop("mel_filters" ) self.assertTrue(np.allclose(snake_case__ , snake_case__ ) ) self.assertEqual(snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[Any] = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 lowerCAmelCase : List[str] = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] lowerCAmelCase : Tuple = [np.asarray(snake_case__ ) for speech_input in speech_inputs] # Test not batched input lowerCAmelCase : Tuple = feature_extractor(np_speech_inputs[0] , return_tensors="np" , sampling_rate=44_100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched lowerCAmelCase : Union[str, Any] = feature_extractor(snake_case__ , return_tensors="np" , sampling_rate=44_100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking lowerCAmelCase : int = feature_extractor( snake_case__ , return_tensors="np" , sampling_rate=44_100 , mask_audio=snake_case__ ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. lowerCAmelCase : Union[str, Any] = [floats_list((1, x) )[0] for x in (800, 800, 800)] lowerCAmelCase : Optional[Any] = np.asarray(snake_case__ ) lowerCAmelCase : Any = feature_extractor(snake_case__ , return_tensors="np" , sampling_rate=44_100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Optional[int] = load_dataset("hf-internal-testing/librispeech_asr_dummy" , "clean" , split="validation" ) # automatic decoding with librispeech lowerCAmelCase : Optional[Any] = ds.sort("id" ).select(range(snake_case__ ) )[:num_samples]["audio"] return [x["array"] for x in speech_samples] def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = self._load_datasamples(1 ) lowerCAmelCase : Union[str, Any] = TvltFeatureExtractor() lowerCAmelCase : Dict = feature_extractor(snake_case__ , return_tensors="pt" ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) lowerCAmelCase : Optional[Any] = torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , snake_case__ , atol=1e-4 ) )
717
"""simple docstring""" import math def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' return math.sqrt(SCREAMING_SNAKE_CASE ) * math.sqrt(SCREAMING_SNAKE_CASE ) == num def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' lowerCAmelCase : Dict = 0 lowerCAmelCase : List[str] = n while left <= right: lowerCAmelCase : str = (left + right) // 2 if mid**2 == n: return True elif mid**2 > n: lowerCAmelCase : int = mid - 1 else: lowerCAmelCase : int = mid + 1 return False if __name__ == "__main__": import doctest doctest.testmod()
681
0
"""simple docstring""" import argparse import json import pickle from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import MaskFormerConfig, MaskFormerForInstanceSegmentation, MaskFormerImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ = logging.get_logger(__name__) def a__ ( SCREAMING_SNAKE_CASE : str ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = SwinConfig.from_pretrained( "microsoft/swin-tiny-patch4-window7-224" , out_features=["stage1", "stage2", "stage3", "stage4"] ) lowerCAmelCase : str = MaskFormerConfig(backbone_config=SCREAMING_SNAKE_CASE ) lowerCAmelCase : Any = '''huggingface/label-files''' if "ade20k-full" in model_name: # this should be ok lowerCAmelCase : List[str] = 8_4_7 lowerCAmelCase : str = '''maskformer-ade20k-full-id2label.json''' elif "ade" in model_name: # this should be ok lowerCAmelCase : Any = 1_5_0 lowerCAmelCase : Union[str, Any] = '''ade20k-id2label.json''' elif "coco-stuff" in model_name: # this should be ok lowerCAmelCase : List[str] = 1_7_1 lowerCAmelCase : str = '''maskformer-coco-stuff-id2label.json''' elif "coco" in model_name: # TODO lowerCAmelCase : Tuple = 1_3_3 lowerCAmelCase : List[str] = '''coco-panoptic-id2label.json''' elif "cityscapes" in model_name: # this should be ok lowerCAmelCase : Dict = 1_9 lowerCAmelCase : Tuple = '''cityscapes-id2label.json''' elif "vistas" in model_name: # this should be ok lowerCAmelCase : List[str] = 6_5 lowerCAmelCase : str = '''mapillary-vistas-id2label.json''' lowerCAmelCase : Union[str, Any] = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , repo_type="dataset" ) , "r" ) ) lowerCAmelCase : str = {int(SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} return config def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' lowerCAmelCase : str = [] # stem # fmt: off rename_keys.append(("backbone.patch_embed.proj.weight", "model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.weight") ) rename_keys.append(("backbone.patch_embed.proj.bias", "model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.bias") ) rename_keys.append(("backbone.patch_embed.norm.weight", "model.pixel_level_module.encoder.model.embeddings.norm.weight") ) rename_keys.append(("backbone.patch_embed.norm.bias", "model.pixel_level_module.encoder.model.embeddings.norm.bias") ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.norm1.weight""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.norm1.bias""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.attn.relative_position_bias_table""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.attn.relative_position_index""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.attn.proj.weight""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.attn.proj.bias""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.norm2.weight""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.norm2.bias""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.mlp.fc1.weight""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.mlp.fc1.bias""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.mlp.fc2.weight""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.weight""") ) rename_keys.append((f"""backbone.layers.{i}.blocks.{j}.mlp.fc2.bias""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.bias""") ) if i < 3: rename_keys.append((f"""backbone.layers.{i}.downsample.reduction.weight""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.reduction.weight""") ) rename_keys.append((f"""backbone.layers.{i}.downsample.norm.weight""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.weight""") ) rename_keys.append((f"""backbone.layers.{i}.downsample.norm.bias""", f"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.bias""") ) rename_keys.append((f"""backbone.norm{i}.weight""", f"""model.pixel_level_module.encoder.hidden_states_norms.{i}.weight""") ) rename_keys.append((f"""backbone.norm{i}.bias""", f"""model.pixel_level_module.encoder.hidden_states_norms.{i}.bias""") ) # FPN rename_keys.append(("sem_seg_head.layer_4.weight", "model.pixel_level_module.decoder.fpn.stem.0.weight") ) rename_keys.append(("sem_seg_head.layer_4.norm.weight", "model.pixel_level_module.decoder.fpn.stem.1.weight") ) rename_keys.append(("sem_seg_head.layer_4.norm.bias", "model.pixel_level_module.decoder.fpn.stem.1.bias") ) for source_index, target_index in zip(range(3 , 0 , -1 ) , range(0 , 3 ) ): rename_keys.append((f"""sem_seg_head.adapter_{source_index}.weight""", f"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.0.weight""") ) rename_keys.append((f"""sem_seg_head.adapter_{source_index}.norm.weight""", f"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.weight""") ) rename_keys.append((f"""sem_seg_head.adapter_{source_index}.norm.bias""", f"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.bias""") ) rename_keys.append((f"""sem_seg_head.layer_{source_index}.weight""", f"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.0.weight""") ) rename_keys.append((f"""sem_seg_head.layer_{source_index}.norm.weight""", f"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.weight""") ) rename_keys.append((f"""sem_seg_head.layer_{source_index}.norm.bias""", f"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.bias""") ) rename_keys.append(("sem_seg_head.mask_features.weight", "model.pixel_level_module.decoder.mask_projection.weight") ) rename_keys.append(("sem_seg_head.mask_features.bias", "model.pixel_level_module.decoder.mask_projection.bias") ) # Transformer decoder for idx in range(config.decoder_config.decoder_layers ): # self-attention out projection rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.weight""", f"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.bias""", f"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.bias""") ) # cross-attention out projection rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.weight""", f"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.bias""", f"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.bias""") ) # MLP 1 rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.weight""", f"""model.transformer_module.decoder.layers.{idx}.fc1.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.bias""", f"""model.transformer_module.decoder.layers.{idx}.fc1.bias""") ) # MLP 2 rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.weight""", f"""model.transformer_module.decoder.layers.{idx}.fc2.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.bias""", f"""model.transformer_module.decoder.layers.{idx}.fc2.bias""") ) # layernorm 1 (self-attention layernorm) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.weight""", f"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.bias""", f"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.bias""") ) # layernorm 2 (cross-attention layernorm) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.weight""", f"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.bias""", f"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.bias""") ) # layernorm 3 (final layernorm) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.weight""", f"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.bias""", f"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.bias""") ) rename_keys.append(("sem_seg_head.predictor.transformer.decoder.norm.weight", "model.transformer_module.decoder.layernorm.weight") ) rename_keys.append(("sem_seg_head.predictor.transformer.decoder.norm.bias", "model.transformer_module.decoder.layernorm.bias") ) # heads on top rename_keys.append(("sem_seg_head.predictor.query_embed.weight", "model.transformer_module.queries_embedder.weight") ) rename_keys.append(("sem_seg_head.predictor.input_proj.weight", "model.transformer_module.input_projection.weight") ) rename_keys.append(("sem_seg_head.predictor.input_proj.bias", "model.transformer_module.input_projection.bias") ) rename_keys.append(("sem_seg_head.predictor.class_embed.weight", "class_predictor.weight") ) rename_keys.append(("sem_seg_head.predictor.class_embed.bias", "class_predictor.bias") ) for i in range(3 ): rename_keys.append((f"""sem_seg_head.predictor.mask_embed.layers.{i}.weight""", f"""mask_embedder.{i}.0.weight""") ) rename_keys.append((f"""sem_seg_head.predictor.mask_embed.layers.{i}.bias""", f"""mask_embedder.{i}.0.bias""") ) # fmt: on return rename_keys def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : List[Any] ): '''simple docstring''' lowerCAmelCase : str = dct.pop(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = val def a__ ( SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : Dict ): '''simple docstring''' lowerCAmelCase : int = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): lowerCAmelCase : List[str] = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) lowerCAmelCase : int = state_dict.pop(f"""backbone.layers.{i}.blocks.{j}.attn.qkv.weight""" ) lowerCAmelCase : List[Any] = state_dict.pop(f"""backbone.layers.{i}.blocks.{j}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict lowerCAmelCase : List[Any] = in_proj_weight[:dim, :] lowerCAmelCase : Optional[Any] = in_proj_bias[: dim] lowerCAmelCase : Union[str, Any] = in_proj_weight[ dim : dim * 2, : ] lowerCAmelCase : int = in_proj_bias[ dim : dim * 2 ] lowerCAmelCase : Any = in_proj_weight[ -dim :, : ] lowerCAmelCase : str = in_proj_bias[-dim :] # fmt: on def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = config.decoder_config.hidden_size for idx in range(config.decoder_config.decoder_layers ): # read in weights + bias of self-attention input projection layer (in the original implementation, this is a single matrix + bias) lowerCAmelCase : Tuple = state_dict.pop(f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_weight""" ) lowerCAmelCase : Optional[Any] = state_dict.pop(f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict lowerCAmelCase : int = in_proj_weight[: hidden_size, :] lowerCAmelCase : Any = in_proj_bias[:config.hidden_size] lowerCAmelCase : Any = in_proj_weight[hidden_size : hidden_size * 2, :] lowerCAmelCase : Any = in_proj_bias[hidden_size : hidden_size * 2] lowerCAmelCase : int = in_proj_weight[-hidden_size :, :] lowerCAmelCase : str = in_proj_bias[-hidden_size :] # read in weights + bias of cross-attention input projection layer (in the original implementation, this is a single matrix + bias) lowerCAmelCase : Dict = state_dict.pop(f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_weight""" ) lowerCAmelCase : Optional[Any] = state_dict.pop(f"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict lowerCAmelCase : Dict = in_proj_weight[: hidden_size, :] lowerCAmelCase : Tuple = in_proj_bias[:config.hidden_size] lowerCAmelCase : Optional[Any] = in_proj_weight[hidden_size : hidden_size * 2, :] lowerCAmelCase : Tuple = in_proj_bias[hidden_size : hidden_size * 2] lowerCAmelCase : Union[str, Any] = in_proj_weight[-hidden_size :, :] lowerCAmelCase : int = in_proj_bias[-hidden_size :] # fmt: on def a__ ( ): '''simple docstring''' lowerCAmelCase : Optional[Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' lowerCAmelCase : Optional[Any] = Image.open(requests.get(SCREAMING_SNAKE_CASE , stream=SCREAMING_SNAKE_CASE ).raw ) return im @torch.no_grad() def a__ ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : bool = False ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = get_maskformer_config(SCREAMING_SNAKE_CASE ) # load original state_dict with open(SCREAMING_SNAKE_CASE , "rb" ) as f: lowerCAmelCase : Dict = pickle.load(SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[Any] = data['''model'''] # for name, param in state_dict.items(): # print(name, param.shape) # rename keys lowerCAmelCase : Optional[int] = create_rename_keys(SCREAMING_SNAKE_CASE ) for src, dest in rename_keys: rename_key(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) read_in_swin_q_k_v(SCREAMING_SNAKE_CASE , config.backbone_config ) read_in_decoder_q_k_v(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # update to torch tensors for key, value in state_dict.items(): lowerCAmelCase : int = torch.from_numpy(SCREAMING_SNAKE_CASE ) # load 🤗 model lowerCAmelCase : Dict = MaskFormerForInstanceSegmentation(SCREAMING_SNAKE_CASE ) model.eval() for name, param in model.named_parameters(): print(SCREAMING_SNAKE_CASE , param.shape ) lowerCAmelCase : Any = model.load_state_dict(SCREAMING_SNAKE_CASE , strict=SCREAMING_SNAKE_CASE ) assert missing_keys == [ "model.pixel_level_module.encoder.model.layernorm.weight", "model.pixel_level_module.encoder.model.layernorm.bias", ] assert len(SCREAMING_SNAKE_CASE ) == 0, f"""Unexpected keys: {unexpected_keys}""" # verify results lowerCAmelCase : List[Any] = prepare_img() if "vistas" in model_name: lowerCAmelCase : Optional[Any] = 6_5 elif "cityscapes" in model_name: lowerCAmelCase : int = 6_5_5_3_5 else: lowerCAmelCase : str = 2_5_5 lowerCAmelCase : Optional[Any] = True if '''ade''' in model_name else False lowerCAmelCase : Tuple = MaskFormerImageProcessor(ignore_index=SCREAMING_SNAKE_CASE , reduce_labels=SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = image_processor(SCREAMING_SNAKE_CASE , return_tensors="pt" ) lowerCAmelCase : Any = model(**SCREAMING_SNAKE_CASE ) print("Logits:" , outputs.class_queries_logits[0, :3, :3] ) if model_name == "maskformer-swin-tiny-ade": lowerCAmelCase : Dict = torch.tensor( [[3.6_353, -4.4_770, -2.6_065], [0.5_081, -4.2_394, -3.5_343], [2.1_909, -5.0_353, -1.9_323]] ) assert torch.allclose(outputs.class_queries_logits[0, :3, :3] , SCREAMING_SNAKE_CASE , atol=1E-4 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: print(f"""Saving model and image processor to {pytorch_dump_folder_path}""" ) Path(SCREAMING_SNAKE_CASE ).mkdir(exist_ok=SCREAMING_SNAKE_CASE ) model.save_pretrained(SCREAMING_SNAKE_CASE ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE ) if push_to_hub: print("Pushing model and image processor to the hub..." ) model.push_to_hub(f"""nielsr/{model_name}""" ) image_processor.push_to_hub(f"""nielsr/{model_name}""" ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''maskformer-swin-tiny-ade''', type=str, help=('''Name of the MaskFormer model you\'d like to convert''',), ) parser.add_argument( '''--checkpoint_path''', default='''/Users/nielsrogge/Documents/MaskFormer_checkpoints/MaskFormer-Swin-tiny-ADE20k/model.pkl''', type=str, help='''Path to the original state dict (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) lowerCAmelCase__ = parser.parse_args() convert_maskformer_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
718
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''google/vit-base-patch16-224''': '''https://huggingface.co/vit-base-patch16-224/resolve/main/config.json''', # See all ViT models at https://huggingface.co/models?filter=vit } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Union[str, Any] ="vit" def __init__( self , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3_072 , snake_case__="gelu" , snake_case__=0.0 , snake_case__=0.0 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__=224 , snake_case__=16 , snake_case__=3 , snake_case__=True , snake_case__=16 , **snake_case__ , ): """simple docstring""" super().__init__(**snake_case__ ) lowerCAmelCase : Optional[Any] = hidden_size lowerCAmelCase : List[Any] = num_hidden_layers lowerCAmelCase : List[Any] = num_attention_heads lowerCAmelCase : Union[str, Any] = intermediate_size lowerCAmelCase : Any = hidden_act lowerCAmelCase : Optional[Any] = hidden_dropout_prob lowerCAmelCase : Optional[Any] = attention_probs_dropout_prob lowerCAmelCase : List[str] = initializer_range lowerCAmelCase : Optional[Any] = layer_norm_eps lowerCAmelCase : Optional[int] = image_size lowerCAmelCase : Optional[Any] = patch_size lowerCAmelCase : Tuple = num_channels lowerCAmelCase : Optional[int] = qkv_bias lowerCAmelCase : str = encoder_stride class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : List[Any] =version.parse("1.11" ) @property def lowercase__ ( self ): """simple docstring""" return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowercase__ ( self ): """simple docstring""" return 1e-4
681
0
"""simple docstring""" from collections.abc import Generator from math import sin def a__ ( SCREAMING_SNAKE_CASE : bytes ): '''simple docstring''' if len(snake_case_ ) != 3_2: raise ValueError("Input must be of length 32" ) lowerCAmelCase : Any = b'''''' for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' if i < 0: raise ValueError("Input must be non-negative" ) lowerCAmelCase : List[Any] = format(snake_case_ , "08x" )[-8:] lowerCAmelCase : Optional[Any] = b'''''' for i in [3, 2, 1, 0]: little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode("utf-8" ) return little_endian_hex def a__ ( SCREAMING_SNAKE_CASE : bytes ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = b'''''' for char in message: bit_string += format(snake_case_ , "08b" ).encode("utf-8" ) lowerCAmelCase : List[str] = format(len(snake_case_ ) , "064b" ).encode("utf-8" ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(snake_case_ ) % 5_1_2 != 4_4_8: bit_string += b"0" bit_string += to_little_endian(start_len[3_2:] ) + to_little_endian(start_len[:3_2] ) return bit_string def a__ ( SCREAMING_SNAKE_CASE : bytes ): '''simple docstring''' if len(snake_case_ ) % 5_1_2 != 0: raise ValueError("Input must have length that\'s a multiple of 512" ) for pos in range(0 , len(snake_case_ ) , 5_1_2 ): lowerCAmelCase : Tuple = bit_string[pos : pos + 5_1_2] lowerCAmelCase : List[str] = [] for i in range(0 , 5_1_2 , 3_2 ): block_words.append(int(to_little_endian(block[i : i + 3_2] ) , 2 ) ) yield block_words def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' if i < 0: raise ValueError("Input must be non-negative" ) lowerCAmelCase : str = format(snake_case_ , "032b" ) lowerCAmelCase : int = '''''' for c in i_str: new_str += "1" if c == "0" else "0" return int(snake_case_ , 2 ) def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ): '''simple docstring''' return (a + b) % 2**3_2 def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ): '''simple docstring''' if i < 0: raise ValueError("Input must be non-negative" ) if shift < 0: raise ValueError("Shift must be non-negative" ) return ((i << shift) ^ (i >> (3_2 - shift))) % 2**3_2 def a__ ( SCREAMING_SNAKE_CASE : bytes ): '''simple docstring''' lowerCAmelCase : List[str] = preprocess(snake_case_ ) lowerCAmelCase : int = [int(2**3_2 * abs(sin(i + 1 ) ) ) for i in range(6_4 )] # Starting states lowerCAmelCase : Tuple = 0x6_7_4_5_2_3_0_1 lowerCAmelCase : Union[str, Any] = 0xE_F_C_D_A_B_8_9 lowerCAmelCase : List[Any] = 0x9_8_B_A_D_C_F_E lowerCAmelCase : str = 0x1_0_3_2_5_4_7_6 lowerCAmelCase : Tuple = [ 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, ] # Process bit string in chunks, each with 16 32-char words for block_words in get_block_words(snake_case_ ): lowerCAmelCase : str = aa lowerCAmelCase : Optional[Any] = ba lowerCAmelCase : Tuple = ca lowerCAmelCase : Tuple = da # Hash current chunk for i in range(6_4 ): if i <= 1_5: # f = (b & c) | (not_32(b) & d) # Alternate definition for f lowerCAmelCase : Union[str, Any] = d ^ (b & (c ^ d)) lowerCAmelCase : int = i elif i <= 3_1: # f = (d & b) | (not_32(d) & c) # Alternate definition for f lowerCAmelCase : str = c ^ (d & (b ^ c)) lowerCAmelCase : List[Any] = (5 * i + 1) % 1_6 elif i <= 4_7: lowerCAmelCase : Union[str, Any] = b ^ c ^ d lowerCAmelCase : List[Any] = (3 * i + 5) % 1_6 else: lowerCAmelCase : Dict = c ^ (b | not_aa(snake_case_ )) lowerCAmelCase : Dict = (7 * i) % 1_6 lowerCAmelCase : Any = (f + a + added_consts[i] + block_words[g]) % 2**3_2 lowerCAmelCase : Tuple = d lowerCAmelCase : Union[str, Any] = c lowerCAmelCase : Dict = b lowerCAmelCase : Union[str, Any] = sum_aa(snake_case_ , left_rotate_aa(snake_case_ , shift_amounts[i] ) ) # Add hashed chunk to running total lowerCAmelCase : List[str] = sum_aa(snake_case_ , snake_case_ ) lowerCAmelCase : Union[str, Any] = sum_aa(snake_case_ , snake_case_ ) lowerCAmelCase : Optional[Any] = sum_aa(snake_case_ , snake_case_ ) lowerCAmelCase : Any = sum_aa(snake_case_ , snake_case_ ) lowerCAmelCase : Optional[int] = reformat_hex(snake_case_ ) + reformat_hex(snake_case_ ) + reformat_hex(snake_case_ ) + reformat_hex(snake_case_ ) return digest if __name__ == "__main__": import doctest doctest.testmod()
719
"""simple docstring""" from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels
681
0
"""simple docstring""" import shutil import tempfile import unittest from transformers import ClapFeatureExtractor, ClapProcessor, RobertaTokenizer, RobertaTokenizerFast from transformers.testing_utils import require_sentencepiece, require_torchaudio from .test_feature_extraction_clap import floats_list @require_torchaudio @require_sentencepiece class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = "laion/clap-htsat-unfused" lowerCAmelCase : Union[str, Any] = tempfile.mkdtemp() def lowercase__ ( self , **snake_case__ ): """simple docstring""" return RobertaTokenizer.from_pretrained(self.checkpoint , **snake_case__ ) def lowercase__ ( self , **snake_case__ ): """simple docstring""" return ClapFeatureExtractor.from_pretrained(self.checkpoint , **snake_case__ ) def lowercase__ ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = self.get_tokenizer() lowerCAmelCase : Union[str, Any] = self.get_feature_extractor() lowerCAmelCase : int = ClapProcessor(tokenizer=snake_case__ , feature_extractor=snake_case__ ) processor.save_pretrained(self.tmpdirname ) lowerCAmelCase : Optional[Any] = ClapProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , snake_case__ ) self.assertEqual(processor.feature_extractor.to_json_string() , feature_extractor.to_json_string() ) self.assertIsInstance(processor.feature_extractor , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = ClapProcessor(tokenizer=self.get_tokenizer() , feature_extractor=self.get_feature_extractor() ) processor.save_pretrained(self.tmpdirname ) lowerCAmelCase : Dict = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) lowerCAmelCase : Any = self.get_feature_extractor(do_normalize=snake_case__ , padding_value=1.0 ) lowerCAmelCase : Dict = ClapProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=snake_case__ , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , snake_case__ ) self.assertEqual(processor.feature_extractor.to_json_string() , feature_extractor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.feature_extractor , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Tuple = self.get_feature_extractor() lowerCAmelCase : List[Any] = self.get_tokenizer() lowerCAmelCase : Optional[Any] = ClapProcessor(tokenizer=snake_case__ , feature_extractor=snake_case__ ) lowerCAmelCase : Union[str, Any] = floats_list((3, 1_000) ) lowerCAmelCase : Tuple = feature_extractor(snake_case__ , return_tensors="np" ) lowerCAmelCase : Optional[Any] = processor(audios=snake_case__ , return_tensors="np" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = self.get_feature_extractor() lowerCAmelCase : Union[str, Any] = self.get_tokenizer() lowerCAmelCase : Optional[Any] = ClapProcessor(tokenizer=snake_case__ , feature_extractor=snake_case__ ) lowerCAmelCase : str = "This is a test string" lowerCAmelCase : List[str] = processor(text=snake_case__ ) lowerCAmelCase : Optional[Any] = tokenizer(snake_case__ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = self.get_feature_extractor() lowerCAmelCase : List[str] = self.get_tokenizer() lowerCAmelCase : Tuple = ClapProcessor(tokenizer=snake_case__ , feature_extractor=snake_case__ ) lowerCAmelCase : Any = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] lowerCAmelCase : Dict = processor.batch_decode(snake_case__ ) lowerCAmelCase : List[Any] = tokenizer.batch_decode(snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = self.get_feature_extractor() lowerCAmelCase : Optional[Any] = self.get_tokenizer() lowerCAmelCase : str = ClapProcessor(tokenizer=snake_case__ , feature_extractor=snake_case__ ) self.assertListEqual( processor.model_input_names[2:] , feature_extractor.model_input_names , msg="`processor` and `feature_extractor` model input names do not match" , )
720
"""simple docstring""" import copy import inspect import unittest from transformers import AutoBackbone from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import require_timm, require_torch, torch_device from transformers.utils.import_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 if is_torch_available(): import torch from transformers import TimmBackbone, TimmBackboneConfig from ...test_pipeline_mixin import PipelineTesterMixin class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__="resnet50" , snake_case__=3 , snake_case__=32 , snake_case__=3 , snake_case__=True , snake_case__=True , ): """simple docstring""" lowerCAmelCase : List[str] = parent lowerCAmelCase : Union[str, Any] = out_indices if out_indices is not None else [4] lowerCAmelCase : Tuple = stage_names lowerCAmelCase : Any = out_features lowerCAmelCase : Any = backbone lowerCAmelCase : Union[str, Any] = batch_size lowerCAmelCase : Optional[int] = image_size lowerCAmelCase : List[str] = num_channels lowerCAmelCase : int = use_pretrained_backbone lowerCAmelCase : Tuple = is_training def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase : Optional[int] = self.get_config() return config, pixel_values def lowercase__ ( self ): """simple docstring""" return TimmBackboneConfig( image_size=self.image_size , num_channels=self.num_channels , out_features=self.out_features , out_indices=self.out_indices , stage_names=self.stage_names , use_pretrained_backbone=self.use_pretrained_backbone , backbone=self.backbone , ) def lowercase__ ( self , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : List[Any] = TimmBackbone(config=snake_case__ ) model.to(snake_case__ ) model.eval() with torch.no_grad(): lowerCAmelCase : Union[str, Any] = model(snake_case__ ) self.parent.assertEqual( result.feature_map[-1].shape , (self.batch_size, model.channels[-1], 14, 14) , ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = self.prepare_config_and_inputs() lowerCAmelCase , lowerCAmelCase : Tuple = config_and_inputs lowerCAmelCase : str = {"pixel_values": pixel_values} return config, inputs_dict @require_torch @require_timm class SCREAMING_SNAKE_CASE__ ( lowercase , lowercase , lowercase , unittest.TestCase ): """simple docstring""" a : Optional[int] =(TimmBackbone,) if is_torch_available() else () a : Union[str, Any] ={"feature-extraction": TimmBackbone} if is_torch_available() else {} a : Tuple =False a : List[Any] =False a : Optional[Any] =False a : Dict =False def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = TimmBackboneModelTester(self ) lowerCAmelCase : List[str] = ConfigTester(self , config_class=snake_case__ , has_text_modality=snake_case__ ) def lowercase__ ( self ): """simple docstring""" 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 lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = "resnet18" lowerCAmelCase : str = "microsoft/resnet-18" lowerCAmelCase : List[Any] = AutoBackbone.from_pretrained(snake_case__ , use_timm_backbone=snake_case__ ) lowerCAmelCase : List[str] = AutoBackbone.from_pretrained(snake_case__ ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(len(timm_model.stage_names ) , len(transformers_model.stage_names ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) # Out indices are set to the last layer by default. For timm models, we don't know # the number of layers in advance, so we set it to (-1,), whereas for transformers # models, we set it to [len(stage_names) - 1] (kept for backward compatibility). self.assertEqual(timm_model.out_indices , (-1,) ) self.assertEqual(transformers_model.out_indices , [len(timm_model.stage_names ) - 1] ) lowerCAmelCase : Union[str, Any] = AutoBackbone.from_pretrained(snake_case__ , use_timm_backbone=snake_case__ , out_indices=[1, 2, 3] ) lowerCAmelCase : List[Any] = AutoBackbone.from_pretrained(snake_case__ , out_indices=[1, 2, 3] ) self.assertEqual(timm_model.out_indices , transformers_model.out_indices ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) @unittest.skip("TimmBackbone doesn't support feed forward chunking" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone doesn't have num_hidden_layers attribute" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone initialization is managed on the timm side" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone models doesn't have inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone models doesn't have inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone model cannot be created without specifying a backbone checkpoint" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("model weights aren't tied in TimmBackbone." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("model weights aren't tied in TimmBackbone." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone doesn't have hidden size info in its configuration." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone doesn't support output_attentions." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Safetensors is not supported by timm." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : Dict = model_class(snake_case__ ) lowerCAmelCase : Any = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase : Optional[int] = [*signature.parameters.keys()] lowerCAmelCase : Any = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase : int = True lowerCAmelCase : str = self.has_attentions # no need to test all models as different heads yield the same functionality lowerCAmelCase : Optional[int] = self.all_model_classes[0] lowerCAmelCase : Union[str, Any] = model_class(snake_case__ ) model.to(snake_case__ ) lowerCAmelCase : Optional[int] = self._prepare_for_class(snake_case__ , snake_case__ ) lowerCAmelCase : Dict = model(**snake_case__ ) lowerCAmelCase : Tuple = outputs[0][-1] # Encoder-/Decoder-only models lowerCAmelCase : Optional[int] = outputs.hidden_states[0] hidden_states.retain_grad() if self.has_attentions: lowerCAmelCase : Union[str, Any] = outputs.attentions[0] attentions.retain_grad() output.flatten()[0].backward(retain_graph=snake_case__ ) self.assertIsNotNone(hidden_states.grad ) if self.has_attentions: self.assertIsNotNone(attentions.grad ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : Dict = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : List[str] = model(**snake_case__ ) self.assertEqual(len(result.feature_maps ) , len(config.out_indices ) ) self.assertEqual(len(model.channels ) , len(config.out_indices ) ) # Check output of last stage is taken if out_features=None, out_indices=None lowerCAmelCase : Dict = copy.deepcopy(snake_case__ ) lowerCAmelCase : Optional[int] = None lowerCAmelCase : Dict = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : Optional[int] = model(**snake_case__ ) self.assertEqual(len(result.feature_maps ) , 1 ) self.assertEqual(len(model.channels ) , 1 ) # Check backbone can be initialized with fresh weights lowerCAmelCase : Optional[int] = copy.deepcopy(snake_case__ ) lowerCAmelCase : List[str] = False lowerCAmelCase : int = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : Optional[Any] = model(**snake_case__ )
681
0
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' if exponent == 1: return base if exponent % 2 == 0: lowerCAmelCase : Dict = _modexpt(lowerCamelCase__ , exponent // 2 , lowerCamelCase__ ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(lowerCamelCase__ , exponent - 1 , lowerCamelCase__ )) % modulo_value def a__ ( SCREAMING_SNAKE_CASE : Dict = 1_7_7_7 , SCREAMING_SNAKE_CASE : List[Any] = 1_8_5_5 , SCREAMING_SNAKE_CASE : Dict = 8 ): '''simple docstring''' lowerCAmelCase : int = base for _ in range(1 , lowerCamelCase__ ): lowerCAmelCase : Union[str, Any] = _modexpt(lowerCamelCase__ , lowerCamelCase__ , 1_0**digits ) return result if __name__ == "__main__": print(F"{solution() = }")
721
"""simple docstring""" import argparse import os import re lowerCAmelCase__ = '''src/transformers''' # Pattern that looks at the indentation in a line. lowerCAmelCase__ = re.compile(r'''^(\s*)\S''') # Pattern that matches `"key":" and puts `key` in group 0. lowerCAmelCase__ = re.compile(r'''^\s*"([^"]+)":''') # Pattern that matches `_import_structure["key"]` and puts `key` in group 0. lowerCAmelCase__ = re.compile(r'''^\s*_import_structure\["([^"]+)"\]''') # Pattern that matches `"key",` and puts `key` in group 0. lowerCAmelCase__ = re.compile(r'''^\s*"([^"]+)",\s*$''') # Pattern that matches any `[stuff]` and puts `stuff` in group 0. lowerCAmelCase__ = re.compile(r'''\[([^\]]+)\]''') def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = _re_indent.search(SCREAMING_SNAKE_CASE ) return "" if search is None else search.groups()[0] def a__ ( SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Optional[int]="" , SCREAMING_SNAKE_CASE : Optional[int]=None , SCREAMING_SNAKE_CASE : Dict=None ): '''simple docstring''' lowerCAmelCase : Tuple = 0 lowerCAmelCase : Optional[int] = code.split("\n" ) if start_prompt is not None: while not lines[index].startswith(SCREAMING_SNAKE_CASE ): index += 1 lowerCAmelCase : Dict = ["\n".join(lines[:index] )] else: lowerCAmelCase : Any = [] # We split into blocks until we get to the `end_prompt` (or the end of the block). lowerCAmelCase : Union[str, Any] = [lines[index]] index += 1 while index < len(SCREAMING_SNAKE_CASE ) and (end_prompt is None or not lines[index].startswith(SCREAMING_SNAKE_CASE )): if len(lines[index] ) > 0 and get_indent(lines[index] ) == indent_level: if len(SCREAMING_SNAKE_CASE ) > 0 and get_indent(current_block[-1] ).startswith(indent_level + " " ): current_block.append(lines[index] ) blocks.append("\n".join(SCREAMING_SNAKE_CASE ) ) if index < len(SCREAMING_SNAKE_CASE ) - 1: lowerCAmelCase : List[str] = [lines[index + 1]] index += 1 else: lowerCAmelCase : Optional[Any] = [] else: blocks.append("\n".join(SCREAMING_SNAKE_CASE ) ) lowerCAmelCase : str = [lines[index]] else: current_block.append(lines[index] ) index += 1 # Adds current block if it's nonempty. if len(SCREAMING_SNAKE_CASE ) > 0: blocks.append("\n".join(SCREAMING_SNAKE_CASE ) ) # Add final block after end_prompt if provided. if end_prompt is not None and index < len(SCREAMING_SNAKE_CASE ): blocks.append("\n".join(lines[index:] ) ) return blocks def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' def _inner(SCREAMING_SNAKE_CASE : Optional[Any] ): return key(SCREAMING_SNAKE_CASE ).lower().replace("_" , "" ) return _inner def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Dict=None ): '''simple docstring''' def noop(SCREAMING_SNAKE_CASE : List[Any] ): return x if key is None: lowerCAmelCase : int = noop # Constants are all uppercase, they go first. lowerCAmelCase : Dict = [obj for obj in objects if key(SCREAMING_SNAKE_CASE ).isupper()] # Classes are not all uppercase but start with a capital, they go second. lowerCAmelCase : List[Any] = [obj for obj in objects if key(SCREAMING_SNAKE_CASE )[0].isupper() and not key(SCREAMING_SNAKE_CASE ).isupper()] # Functions begin with a lowercase, they go last. lowerCAmelCase : List[Any] = [obj for obj in objects if not key(SCREAMING_SNAKE_CASE )[0].isupper()] lowerCAmelCase : Dict = ignore_underscore(SCREAMING_SNAKE_CASE ) return sorted(SCREAMING_SNAKE_CASE , key=SCREAMING_SNAKE_CASE ) + sorted(SCREAMING_SNAKE_CASE , key=SCREAMING_SNAKE_CASE ) + sorted(SCREAMING_SNAKE_CASE , key=SCREAMING_SNAKE_CASE ) def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' def _replace(SCREAMING_SNAKE_CASE : List[Any] ): lowerCAmelCase : List[str] = match.groups()[0] if "," not in imports: return f"""[{imports}]""" lowerCAmelCase : Dict = [part.strip().replace("\"" , "" ) for part in imports.split("," )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: lowerCAmelCase : Any = keys[:-1] return "[" + ", ".join([f"""\"{k}\"""" for k in sort_objects(SCREAMING_SNAKE_CASE )] ) + "]" lowerCAmelCase : List[Any] = import_statement.split("\n" ) if len(SCREAMING_SNAKE_CASE ) > 3: # Here we have to sort internal imports that are on several lines (one per name): # key: [ # "object1", # "object2", # ... # ] # We may have to ignore one or two lines on each side. lowerCAmelCase : Tuple = 2 if lines[1].strip() == "[" else 1 lowerCAmelCase : Optional[Any] = [(i, _re_strip_line.search(SCREAMING_SNAKE_CASE ).groups()[0]) for i, line in enumerate(lines[idx:-idx] )] lowerCAmelCase : Optional[Any] = sort_objects(SCREAMING_SNAKE_CASE , key=lambda SCREAMING_SNAKE_CASE : x[1] ) lowerCAmelCase : List[str] = [lines[x[0] + idx] for x in sorted_indices] return "\n".join(lines[:idx] + sorted_lines + lines[-idx:] ) elif len(SCREAMING_SNAKE_CASE ) == 3: # Here we have to sort internal imports that are on one separate line: # key: [ # "object1", "object2", ... # ] if _re_bracket_content.search(lines[1] ) is not None: lowerCAmelCase : Optional[int] = _re_bracket_content.sub(_replace , lines[1] ) else: lowerCAmelCase : List[str] = [part.strip().replace("\"" , "" ) for part in lines[1].split("," )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: lowerCAmelCase : Union[str, Any] = keys[:-1] lowerCAmelCase : str = get_indent(lines[1] ) + ", ".join([f"""\"{k}\"""" for k in sort_objects(SCREAMING_SNAKE_CASE )] ) return "\n".join(SCREAMING_SNAKE_CASE ) else: # Finally we have to deal with imports fitting on one line lowerCAmelCase : Any = _re_bracket_content.sub(_replace , SCREAMING_SNAKE_CASE ) return import_statement def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Tuple=True ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE , encoding="utf-8" ) as f: lowerCAmelCase : Union[str, Any] = f.read() if "_import_structure" not in code: return # Blocks of indent level 0 lowerCAmelCase : List[str] = split_code_in_indented_blocks( SCREAMING_SNAKE_CASE , start_prompt="_import_structure = {" , end_prompt="if TYPE_CHECKING:" ) # We ignore block 0 (everything untils start_prompt) and the last block (everything after end_prompt). for block_idx in range(1 , len(SCREAMING_SNAKE_CASE ) - 1 ): # Check if the block contains some `_import_structure`s thingy to sort. lowerCAmelCase : Tuple = main_blocks[block_idx] lowerCAmelCase : Optional[Any] = block.split("\n" ) # Get to the start of the imports. lowerCAmelCase : int = 0 while line_idx < len(SCREAMING_SNAKE_CASE ) and "_import_structure" not in block_lines[line_idx]: # Skip dummy import blocks if "import dummy" in block_lines[line_idx]: lowerCAmelCase : Optional[Any] = len(SCREAMING_SNAKE_CASE ) else: line_idx += 1 if line_idx >= len(SCREAMING_SNAKE_CASE ): continue # Ignore beginning and last line: they don't contain anything. lowerCAmelCase : Optional[Any] = "\n".join(block_lines[line_idx:-1] ) lowerCAmelCase : Dict = get_indent(block_lines[1] ) # Slit the internal block into blocks of indent level 1. lowerCAmelCase : Optional[Any] = split_code_in_indented_blocks(SCREAMING_SNAKE_CASE , indent_level=SCREAMING_SNAKE_CASE ) # We have two categories of import key: list or _import_structure[key].append/extend lowerCAmelCase : Tuple = _re_direct_key if "_import_structure = {" in block_lines[0] else _re_indirect_key # Grab the keys, but there is a trap: some lines are empty or just comments. lowerCAmelCase : Tuple = [(pattern.search(SCREAMING_SNAKE_CASE ).groups()[0] if pattern.search(SCREAMING_SNAKE_CASE ) is not None else None) for b in internal_blocks] # We only sort the lines with a key. lowerCAmelCase : int = [(i, key) for i, key in enumerate(SCREAMING_SNAKE_CASE ) if key is not None] lowerCAmelCase : Union[str, Any] = [x[0] for x in sorted(SCREAMING_SNAKE_CASE , key=lambda SCREAMING_SNAKE_CASE : x[1] )] # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. lowerCAmelCase : Tuple = 0 lowerCAmelCase : Any = [] for i in range(len(SCREAMING_SNAKE_CASE ) ): if keys[i] is None: reorderded_blocks.append(internal_blocks[i] ) else: lowerCAmelCase : Dict = sort_objects_in_import(internal_blocks[sorted_indices[count]] ) reorderded_blocks.append(SCREAMING_SNAKE_CASE ) count += 1 # And we put our main block back together with its first and last line. lowerCAmelCase : List[Any] = "\n".join(block_lines[:line_idx] + reorderded_blocks + [block_lines[-1]] ) if code != "\n".join(SCREAMING_SNAKE_CASE ): if check_only: return True else: print(f"""Overwriting {file}.""" ) with open(SCREAMING_SNAKE_CASE , "w" , encoding="utf-8" ) as f: f.write("\n".join(SCREAMING_SNAKE_CASE ) ) def a__ ( SCREAMING_SNAKE_CASE : List[str]=True ): '''simple docstring''' lowerCAmelCase : Optional[int] = [] for root, _, files in os.walk(SCREAMING_SNAKE_CASE ): if "__init__.py" in files: lowerCAmelCase : Tuple = sort_imports(os.path.join(SCREAMING_SNAKE_CASE , "__init__.py" ) , check_only=SCREAMING_SNAKE_CASE ) if result: lowerCAmelCase : Optional[Any] = [os.path.join(SCREAMING_SNAKE_CASE , "__init__.py" )] if len(SCREAMING_SNAKE_CASE ) > 0: raise ValueError(f"""Would overwrite {len(SCREAMING_SNAKE_CASE )} files, run `make style`.""" ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument('''--check_only''', action='''store_true''', help='''Whether to only check or fix style.''') lowerCAmelCase__ = parser.parse_args() sort_imports_in_all_inits(check_only=args.check_only)
681
0
"""simple docstring""" import logging import re import pytorch_quantization import pytorch_quantization.nn as quant_nn import torch from pytorch_quantization import calib from pytorch_quantization.tensor_quant import QuantDescriptor lowerCAmelCase__ = logging.getLogger(__name__) lowerCAmelCase__ = 50 # max width of layer names lowerCAmelCase__ = 70 # max width of quantizer names def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : List[str] = parser.add_argument_group("quant_trainer arguments" ) group.add_argument("--wprec" , type=SCREAMING_SNAKE_CASE , default=8 , help="weight precision" ) group.add_argument("--aprec" , type=SCREAMING_SNAKE_CASE , default=8 , help="activation precision" ) group.add_argument("--quant-per-tensor" , action="store_true" , help="per tensor weight scaling" ) group.add_argument("--quant-disable" , action="store_true" , help="disable all quantizers" ) group.add_argument("--quant-disable-embeddings" , action="store_true" , help="disable all embeddings quantizers" ) group.add_argument("--quant-disable-keyword" , type=SCREAMING_SNAKE_CASE , nargs="+" , help="disable quantizers by keyword" ) group.add_argument("--quant-disable-layer-module" , type=SCREAMING_SNAKE_CASE , help="disable quantizers by keyword under layer." ) group.add_argument("--quant-enable-layer-module" , type=SCREAMING_SNAKE_CASE , help="enable quantizers by keyword under layer" ) group.add_argument("--calibrator" , default="max" , help="which quantization range calibrator to use" ) group.add_argument("--percentile" , default=SCREAMING_SNAKE_CASE , type=SCREAMING_SNAKE_CASE , help="percentile for PercentileCalibrator" ) group.add_argument("--fuse-qkv" , action="store_true" , help="use the same scale factor for qkv" ) group.add_argument("--clip-gelu" , metavar="N" , type=SCREAMING_SNAKE_CASE , help="clip gelu output maximum value to N" ) group.add_argument( "--recalibrate-weights" , action="store_true" , help=( "recalibrate weight amaxes by taking the max of the weights." " amaxes will be computed with the current quantization granularity (axis)." ) , ) def a__ ( SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' if args.calibrator == "max": lowerCAmelCase : List[Any] = "max" elif args.calibrator == "percentile": if args.percentile is None: raise ValueError("Specify --percentile when using percentile calibrator" ) lowerCAmelCase : Any = "histogram" elif args.calibrator == "mse": lowerCAmelCase : Dict = "histogram" else: raise ValueError(f"""Invalid calibrator {args.calibrator}""" ) lowerCAmelCase : str = QuantDescriptor(num_bits=args.aprec , calib_method=SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = QuantDescriptor(num_bits=args.wprec , axis=(None if args.quant_per_tensor else (0,)) ) quant_nn.QuantLinear.set_default_quant_desc_input(SCREAMING_SNAKE_CASE ) quant_nn.QuantLinear.set_default_quant_desc_weight(SCREAMING_SNAKE_CASE ) def a__ ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Tuple=False , SCREAMING_SNAKE_CASE : Optional[int]=False ): '''simple docstring''' logger.info("Configuring Model for Quantization" ) logger.info(f"""using quantization package {pytorch_quantization.__file__}""" ) if not calib: if args.quant_disable_embeddings: set_quantizer_by_name(SCREAMING_SNAKE_CASE , ["embeddings"] , which="weight" , _disabled=SCREAMING_SNAKE_CASE ) if args.quant_disable: set_quantizer_by_name(SCREAMING_SNAKE_CASE , [""] , _disabled=SCREAMING_SNAKE_CASE ) if args.quant_disable_keyword: set_quantizer_by_name(SCREAMING_SNAKE_CASE , args.quant_disable_keyword , _disabled=SCREAMING_SNAKE_CASE ) if args.quant_disable_layer_module: set_quantizer_by_name(SCREAMING_SNAKE_CASE , [r"layer.\d+." + args.quant_disable_layer_module] , _disabled=SCREAMING_SNAKE_CASE ) if args.quant_enable_layer_module: set_quantizer_by_name(SCREAMING_SNAKE_CASE , [r"layer.\d+." + args.quant_enable_layer_module] , _disabled=SCREAMING_SNAKE_CASE ) if args.recalibrate_weights: recalibrate_weights(SCREAMING_SNAKE_CASE ) if args.fuse_qkv: fuse_qkv(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if args.clip_gelu: clip_gelu(SCREAMING_SNAKE_CASE , args.clip_gelu ) # if args.local_rank in [-1, 0] and not calib: print_quant_summary(SCREAMING_SNAKE_CASE ) def a__ ( SCREAMING_SNAKE_CASE : str ): '''simple docstring''' logger.info("Enabling Calibration" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: module.disable_quant() module.enable_calib() else: module.disable() logger.info(f"""{name:80}: {module}""" ) def a__ ( SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : Dict ): '''simple docstring''' logger.info("Loading calibrated amax" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: if isinstance(module._calibrator , calib.MaxCalibrator ): module.load_calib_amax() else: module.load_calib_amax("percentile" , percentile=args.percentile ) module.enable_quant() module.disable_calib() else: module.enable() model.cuda() print_quant_summary(SCREAMING_SNAKE_CASE ) def a__ ( SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Union[str, Any] ): '''simple docstring''' def fusea(SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Union[str, Any] ): for mod in [qq, qk, qv]: if not hasattr(SCREAMING_SNAKE_CASE , "_amax" ): print(" WARNING: NO AMAX BUFFER" ) return lowerCAmelCase : List[Any] = qq._amax.detach().item() lowerCAmelCase : List[Any] = qk._amax.detach().item() lowerCAmelCase : List[Any] = qv._amax.detach().item() lowerCAmelCase : Optional[int] = max(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) qq._amax.fill_(SCREAMING_SNAKE_CASE ) qk._amax.fill_(SCREAMING_SNAKE_CASE ) qv._amax.fill_(SCREAMING_SNAKE_CASE ) logger.info(f""" q={q:5.2f} k={k:5.2f} v={v:5.2f} -> {amax:5.2f}""" ) for name, mod in model.named_modules(): if name.endswith(".attention.self" ): logger.info(f"""FUSE_QKV: {name:{name_width}}""" ) fusea(mod.matmul_q_input_quantizer , mod.matmul_k_input_quantizer , mod.matmul_v_input_quantizer ) if args.quant_per_tensor: fusea(mod.query._weight_quantizer , mod.key._weight_quantizer , mod.value._weight_quantizer ) def a__ ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : Dict ): '''simple docstring''' for name, mod in model.named_modules(): if name.endswith(".output.dense" ) and not name.endswith("attention.output.dense" ): lowerCAmelCase : Tuple = mod._input_quantizer._amax.data.detach().item() mod._input_quantizer._amax.data.detach().clamp_(max=SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = mod._input_quantizer._amax.data.detach().item() logger.info(f"""CLIP_GELU: {name:{name_width}} amax: {amax_init:5.2f} -> {amax:5.2f}""" ) def a__ ( SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' for name, mod in model.named_modules(): if hasattr(SCREAMING_SNAKE_CASE , "_weight_quantizer" ) and mod._weight_quantizer.axis is not None: lowerCAmelCase : List[str] = mod.weight.shape[0] lowerCAmelCase : Any = mod._weight_quantizer._amax.detach() lowerCAmelCase : Tuple = torch.ones(SCREAMING_SNAKE_CASE , dtype=amax.dtype , device=amax.device ) * amax print(f"""expanding {name} {amax} -> {mod._weight_quantizer._amax}""" ) def a__ ( SCREAMING_SNAKE_CASE : List[Any] ): '''simple docstring''' for name, mod in model.named_modules(): if hasattr(SCREAMING_SNAKE_CASE , "_weight_quantizer" ): if not hasattr(mod.weight_quantizer , "_amax" ): print("RECALIB: {name:{name_width}} WARNING: NO AMAX BUFFER" ) continue # determine which axes to reduce across # e.g. a 4D tensor quantized per axis 0 should reduce over (1,2,3) lowerCAmelCase : List[Any] = set() if mod._weight_quantizer.axis is None else set(mod._weight_quantizer.axis ) lowerCAmelCase : List[Any] = set(range(len(mod.weight.size() ) ) ) - axis_set lowerCAmelCase : Dict = pytorch_quantization.utils.reduce_amax(mod.weight , axis=SCREAMING_SNAKE_CASE , keepdims=SCREAMING_SNAKE_CASE ).detach() logger.info(f"""RECALIB: {name:{name_width}} {mod._weight_quantizer._amax.flatten()} -> {amax.flatten()}""" ) lowerCAmelCase : Tuple = amax def a__ ( SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : List[Any]=2_5 , SCREAMING_SNAKE_CASE : Optional[int]=1_8_0 , SCREAMING_SNAKE_CASE : str=None ): '''simple docstring''' if ignore is None: lowerCAmelCase : List[str] = [] elif not isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): lowerCAmelCase : Any = [ignore] lowerCAmelCase : List[Any] = 0 for name, mod in model.named_modules(): if not hasattr(SCREAMING_SNAKE_CASE , "weight" ): continue lowerCAmelCase : Optional[int] = max(SCREAMING_SNAKE_CASE , len(SCREAMING_SNAKE_CASE ) ) for name, mod in model.named_modules(): lowerCAmelCase : Dict = getattr(SCREAMING_SNAKE_CASE , "_input_quantizer" , SCREAMING_SNAKE_CASE ) lowerCAmelCase : int = getattr(SCREAMING_SNAKE_CASE , "_weight_quantizer" , SCREAMING_SNAKE_CASE ) if not hasattr(SCREAMING_SNAKE_CASE , "weight" ): continue if type(SCREAMING_SNAKE_CASE ) in ignore: continue if [True for s in ignore if type(SCREAMING_SNAKE_CASE ) is str and s in name]: continue lowerCAmelCase : Any = f"""Act:{input_q.extra_repr()}""" lowerCAmelCase : str = f"""Wgt:{weight_q.extra_repr()}""" lowerCAmelCase : Optional[int] = f"""{name:{name_width}} {act_str} {wgt_str}""" if len(SCREAMING_SNAKE_CASE ) <= line_width: logger.info(SCREAMING_SNAKE_CASE ) else: logger.info(f"""{name:{name_width}} {act_str}""" ) logger.info(f"""{" ":{name_width}} {wgt_str}""" ) def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' lowerCAmelCase : Dict = 0 for name, mod in model.named_modules(): if isinstance(SCREAMING_SNAKE_CASE , pytorch_quantization.nn.TensorQuantizer ): print(f"""{name:80} {mod}""" ) count += 1 print(f"""{count} TensorQuantizers found in model""" ) def a__ ( SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : Tuple = getattr(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if quantizer_mod is not None: assert hasattr(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) setattr(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) else: logger.warning(f"""{name} has no {quantizer}""" ) def a__ ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : Optional[int]="both" , **SCREAMING_SNAKE_CASE : Any ): '''simple docstring''' lowerCAmelCase : List[str] = f"""Warning: changing {which} quantizers of {name:{qname_width}}""" for k, v in kwargs.items(): s += f""" {k}={v}""" if which in ["input", "both"]: set_quantizer(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , "_input_quantizer" , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if which in ["weight", "both"]: set_quantizer(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , "_weight_quantizer" , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) logger.info(SCREAMING_SNAKE_CASE ) def a__ ( SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : List[str] , **SCREAMING_SNAKE_CASE : Tuple ): '''simple docstring''' for name, mod in model.named_modules(): if hasattr(SCREAMING_SNAKE_CASE , "_input_quantizer" ) or hasattr(SCREAMING_SNAKE_CASE , "_weight_quantizer" ): for n in names: if re.search(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): set_quantizers(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) elif name.endswith("_quantizer" ): for n in names: if re.search(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): lowerCAmelCase : Optional[int] = f"""Warning: changing {name:{name_width}}""" for k, v in kwargs.items(): s += f""" {k}={v}""" setattr(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) logger.info(SCREAMING_SNAKE_CASE )
700
"""simple docstring""" from __future__ import annotations import inspect import unittest import numpy as np from transformers import ResNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFResNetForImageClassification, TFResNetModel from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__=3 , snake_case__=32 , snake_case__=3 , snake_case__=10 , snake_case__=[10, 20, 30, 40] , snake_case__=[1, 1, 2, 1] , snake_case__=True , snake_case__=True , snake_case__="relu" , snake_case__=3 , snake_case__=None , ): """simple docstring""" lowerCAmelCase : Optional[Any] = parent lowerCAmelCase : List[Any] = batch_size lowerCAmelCase : Union[str, Any] = image_size lowerCAmelCase : Dict = num_channels lowerCAmelCase : List[Any] = embeddings_size lowerCAmelCase : List[Any] = hidden_sizes lowerCAmelCase : Optional[int] = depths lowerCAmelCase : str = is_training lowerCAmelCase : List[str] = use_labels lowerCAmelCase : List[Any] = hidden_act lowerCAmelCase : Optional[Any] = num_labels lowerCAmelCase : Tuple = scope lowerCAmelCase : int = len(snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase : Optional[Any] = None if self.use_labels: lowerCAmelCase : Optional[int] = ids_tensor([self.batch_size] , self.num_labels ) lowerCAmelCase : List[str] = self.get_config() return config, pixel_values, labels def lowercase__ ( self ): """simple docstring""" return ResNetConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , image_size=self.image_size , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Tuple = TFResNetModel(config=snake_case__ ) lowerCAmelCase : Union[str, Any] = model(snake_case__ ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = self.num_labels lowerCAmelCase : str = TFResNetForImageClassification(snake_case__ ) lowerCAmelCase : int = model(snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = self.prepare_config_and_inputs() lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Any = config_and_inputs lowerCAmelCase : Optional[Any] = {"pixel_values": pixel_values} return config, inputs_dict @require_tf class SCREAMING_SNAKE_CASE__ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" a : Any =(TFResNetModel, TFResNetForImageClassification) if is_tf_available() else () a : Tuple =( {"feature-extraction": TFResNetModel, "image-classification": TFResNetForImageClassification} if is_tf_available() else {} ) a : int =False a : List[str] =False a : Optional[int] =False a : Union[str, Any] =False a : Any =False def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = TFResNetModelTester(self ) lowerCAmelCase : str = ConfigTester(self , config_class=snake_case__ , has_text_modality=snake_case__ ) def lowercase__ ( self ): """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 lowercase__ ( self ): """simple docstring""" return @unittest.skip(reason="ResNet does not use inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip(reason="ResNet does not support input and output embeddings" ) def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : List[str] = model_class(snake_case__ ) lowerCAmelCase : Optional[Any] = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase : Dict = [*signature.parameters.keys()] lowerCAmelCase : List[Any] = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def lowercase__ ( self ): """simple docstring""" def check_hidden_states_output(snake_case__ , snake_case__ , snake_case__ ): lowerCAmelCase : int = model_class(snake_case__ ) lowerCAmelCase : Optional[Any] = model(**self._prepare_for_class(snake_case__ , snake_case__ ) ) lowerCAmelCase : List[str] = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states lowerCAmelCase : Tuple = self.model_tester.num_stages self.assertEqual(len(snake_case__ ) , expected_num_stages + 1 ) # ResNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) lowerCAmelCase , lowerCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase : Any = ["basic", "bottleneck"] for model_class in self.all_model_classes: for layer_type in layers_type: lowerCAmelCase : Optional[Any] = layer_type lowerCAmelCase : Dict = True check_hidden_states_output(snake_case__ , snake_case__ , snake_case__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowerCAmelCase : List[Any] = True check_hidden_states_output(snake_case__ , snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case__ ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase : int = TFResNetModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def a__ ( ): '''simple docstring''' lowerCAmelCase : str = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_tf @require_vision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" @cached_property def lowercase__ ( self ): """simple docstring""" return ( AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) lowerCAmelCase : Any = self.default_image_processor lowerCAmelCase : Optional[Any] = prepare_img() lowerCAmelCase : Dict = image_processor(images=snake_case__ , return_tensors="tf" ) # forward pass lowerCAmelCase : str = model(**snake_case__ ) # verify the logits lowerCAmelCase : str = tf.TensorShape((1, 1_000) ) self.assertEqual(outputs.logits.shape , snake_case__ ) lowerCAmelCase : str = tf.constant([-11.1069, -9.7877, -8.3777] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , snake_case__ , atol=1e-4 ) )
681
0
"""simple docstring""" from sklearn.metrics import fa_score, matthews_corrcoef import datasets from .record_evaluation import evaluate as evaluate_record lowerCAmelCase__ = '''\ @article{wang2019superglue, title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems}, author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R}, journal={arXiv preprint arXiv:1905.00537}, year={2019} } ''' lowerCAmelCase__ = '''\ SuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after GLUE with a new set of more difficult language understanding tasks, improved resources, and a new public leaderboard. ''' lowerCAmelCase__ = ''' Compute SuperGLUE evaluation metric associated to each SuperGLUE dataset. Args: predictions: list of predictions to score. Depending on the SuperGlUE subset: - for \'record\': list of question-answer dictionaries with the following keys: - \'idx\': index of the question as specified by the dataset - \'prediction_text\': the predicted answer text - for \'multirc\': list of question-answer dictionaries with the following keys: - \'idx\': index of the question-answer pair as specified by the dataset - \'prediction\': the predicted answer label - otherwise: list of predicted labels references: list of reference labels. Depending on the SuperGLUE subset: - for \'record\': list of question-answers dictionaries with the following keys: - \'idx\': index of the question as specified by the dataset - \'answers\': list of possible answers - otherwise: list of reference labels Returns: depending on the SuperGLUE subset: - for \'record\': - \'exact_match\': Exact match between answer and gold answer - \'f1\': F1 score - for \'multirc\': - \'exact_match\': Exact match between answer and gold answer - \'f1_m\': Per-question macro-F1 score - \'f1_a\': Average F1 score over all answers - for \'axb\': \'matthews_correlation\': Matthew Correlation - for \'cb\': - \'accuracy\': Accuracy - \'f1\': F1 score - for all others: - \'accuracy\': Accuracy Examples: >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'copa\') # any of ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"] >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0} >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'cb\') >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0, \'f1\': 1.0} >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'record\') >>> predictions = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'prediction_text\': \'answer\'}] >>> references = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'answers\': [\'answer\', \'another_answer\']}] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'exact_match\': 1.0, \'f1\': 1.0} >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'multirc\') >>> predictions = [{\'idx\': {\'answer\': 0, \'paragraph\': 0, \'question\': 0}, \'prediction\': 0}, {\'idx\': {\'answer\': 1, \'paragraph\': 2, \'question\': 3}, \'prediction\': 1}] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'exact_match\': 1.0, \'f1_m\': 1.0, \'f1_a\': 1.0} >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'axb\') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'matthews_correlation\': 1.0} ''' def a__ ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' return float((preds == labels).mean() ) def a__ ( SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : Optional[int]="binary" ): '''simple docstring''' lowerCAmelCase : Dict = simple_accuracy(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = float(fa_score(y_true=SCREAMING_SNAKE_CASE , y_pred=SCREAMING_SNAKE_CASE , average=SCREAMING_SNAKE_CASE ) ) return { "accuracy": acc, "f1": fa, } def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Tuple ): '''simple docstring''' lowerCAmelCase : Optional[Any] = {} for id_pred, label in zip(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): lowerCAmelCase : Optional[int] = f"""{id_pred["idx"]["paragraph"]}-{id_pred["idx"]["question"]}""" lowerCAmelCase : str = id_pred["prediction"] if question_id in question_map: question_map[question_id].append((pred, label) ) else: lowerCAmelCase : str = [(pred, label)] lowerCAmelCase : List[str] = [], [] for question, preds_labels in question_map.items(): lowerCAmelCase : str = zip(*SCREAMING_SNAKE_CASE ) lowerCAmelCase : str = fa_score(y_true=SCREAMING_SNAKE_CASE , y_pred=SCREAMING_SNAKE_CASE , average="macro" ) fas.append(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Any = int(sum(pred == label for pred, label in preds_labels ) == len(SCREAMING_SNAKE_CASE ) ) ems.append(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = float(sum(SCREAMING_SNAKE_CASE ) / len(SCREAMING_SNAKE_CASE ) ) lowerCAmelCase : Optional[int] = sum(SCREAMING_SNAKE_CASE ) / len(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Any = float(fa_score(y_true=SCREAMING_SNAKE_CASE , y_pred=[id_pred["prediction"] for id_pred in ids_preds] ) ) return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a} @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE__ ( datasets.Metric ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" if self.config_name not in [ "boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg", ]: raise KeyError( "You should supply a configuration name selected in " "[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , codebase_urls=[] , reference_urls=[] , format="numpy" if not self.config_name == "record" and not self.config_name == "multirc" else None , ) def lowercase__ ( self ): """simple docstring""" if self.config_name == "record": return { "predictions": { "idx": { "passage": datasets.Value("int64" ), "query": datasets.Value("int64" ), }, "prediction_text": datasets.Value("string" ), }, "references": { "idx": { "passage": datasets.Value("int64" ), "query": datasets.Value("int64" ), }, "answers": datasets.Sequence(datasets.Value("string" ) ), }, } elif self.config_name == "multirc": return { "predictions": { "idx": { "answer": datasets.Value("int64" ), "paragraph": datasets.Value("int64" ), "question": datasets.Value("int64" ), }, "prediction": datasets.Value("int64" ), }, "references": datasets.Value("int64" ), } else: return { "predictions": datasets.Value("int64" ), "references": datasets.Value("int64" ), } def lowercase__ ( self , snake_case__ , snake_case__ ): """simple docstring""" if self.config_name == "axb": return {"matthews_correlation": matthews_corrcoef(snake_case__ , snake_case__ )} elif self.config_name == "cb": return acc_and_fa(snake_case__ , snake_case__ , fa_avg="macro" ) elif self.config_name == "record": lowerCAmelCase : List[str] = [ { "qas": [ {"id": ref["idx"]["query"], "answers": [{"text": ans} for ans in ref["answers"]]} for ref in references ] } ] lowerCAmelCase : Tuple = {pred["idx"]["query"]: pred["prediction_text"] for pred in predictions} return evaluate_record(snake_case__ , snake_case__ )[0] elif self.config_name == "multirc": return evaluate_multirc(snake_case__ , snake_case__ ) elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]: return {"accuracy": simple_accuracy(snake_case__ , snake_case__ )} else: raise KeyError( "You should supply a configuration name selected in " "[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]" )
701
"""simple docstring""" import random from .binary_exp_mod import bin_exp_mod def a__ ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : int=1_0_0_0 ): '''simple docstring''' if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd lowerCAmelCase : int = n - 1 lowerCAmelCase : Optional[int] = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) lowerCAmelCase : Optional[Any] = 0 while count < prec: lowerCAmelCase : List[str] = random.randint(2 , n - 1 ) lowerCAmelCase : Tuple = bin_exp_mod(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if b != 1: lowerCAmelCase : List[str] = True for _ in range(SCREAMING_SNAKE_CASE ): if b == n - 1: lowerCAmelCase : List[str] = False break lowerCAmelCase : Optional[Any] = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": lowerCAmelCase__ = abs(int(input('''Enter bound : ''').strip())) print('''Here\'s the list of primes:''') print(''', '''.join(str(i) for i in range(n + 1) if is_prime_big(i)))
681
0
"""simple docstring""" from __future__ import annotations def a__ ( SCREAMING_SNAKE_CASE : list[list[int]] ): '''simple docstring''' for i in range(1 , len(matrix[0] ) ): matrix[0][i] += matrix[0][i - 1] # preprocessing the first column for i in range(1 , len(SCREAMING_SNAKE_CASE ) ): matrix[i][0] += matrix[i - 1][0] # updating the path cost for current position for i in range(1 , len(SCREAMING_SNAKE_CASE ) ): for j in range(1 , len(matrix[0] ) ): matrix[i][j] += min(matrix[i - 1][j] , matrix[i][j - 1] ) return matrix[-1][-1] if __name__ == "__main__": import doctest doctest.testmod()
702
"""simple docstring""" # DISCLAIMER: This file is strongly influenced by https://github.com/ermongroup/ddim from dataclasses import dataclass from typing import Optional, Tuple, Union import flax import jax import jax.numpy as jnp from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils_flax import ( CommonSchedulerState, FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, add_noise_common, get_velocity_common, ) @flax.struct.dataclass class SCREAMING_SNAKE_CASE__ : """simple docstring""" a : CommonSchedulerState # setable values a : jnp.ndarray a : jnp.ndarray a : Optional[int] =None @classmethod def lowercase__ ( cls , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" return cls(common=snake_case__ , init_noise_sigma=snake_case__ , timesteps=snake_case__ ) @dataclass class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : DDPMSchedulerState class SCREAMING_SNAKE_CASE__ ( lowercase , lowercase ): """simple docstring""" a : Union[str, Any] =[e.name for e in FlaxKarrasDiffusionSchedulers] a : jnp.dtype @property def lowercase__ ( self ): """simple docstring""" return True @register_to_config def __init__( self , snake_case__ = 1_000 , snake_case__ = 0.0001 , snake_case__ = 0.02 , snake_case__ = "linear" , snake_case__ = None , snake_case__ = "fixed_small" , snake_case__ = True , snake_case__ = "epsilon" , snake_case__ = jnp.floataa , ): """simple docstring""" lowerCAmelCase : Any = dtype def lowercase__ ( self , snake_case__ = None ): """simple docstring""" if common is None: lowerCAmelCase : Dict = CommonSchedulerState.create(self ) # standard deviation of the initial noise distribution lowerCAmelCase : str = jnp.array(1.0 , dtype=self.dtype ) lowerCAmelCase : Any = jnp.arange(0 , self.config.num_train_timesteps ).round()[::-1] return DDPMSchedulerState.create( common=snake_case__ , init_noise_sigma=snake_case__ , timesteps=snake_case__ , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = None ): """simple docstring""" return sample def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = () ): """simple docstring""" lowerCAmelCase : List[Any] = self.config.num_train_timesteps // num_inference_steps # creates integer timesteps by multiplying by ratio # rounding to avoid issues when num_inference_step is power of 3 lowerCAmelCase : Any = (jnp.arange(0 , snake_case__ ) * step_ratio).round()[::-1] return state.replace( num_inference_steps=snake_case__ , timesteps=snake_case__ , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__=None , snake_case__=None ): """simple docstring""" lowerCAmelCase : Optional[Any] = state.common.alphas_cumprod[t] lowerCAmelCase : List[str] = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) # For t > 0, compute predicted variance βt (see formula (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf) # and sample from it to get previous sample # x_{t-1} ~ N(pred_prev_sample, variance) == add variance to pred_sample lowerCAmelCase : Union[str, Any] = (1 - alpha_prod_t_prev) / (1 - alpha_prod_t) * state.common.betas[t] if variance_type is None: lowerCAmelCase : List[str] = self.config.variance_type # hacks - were probably added for training stability if variance_type == "fixed_small": lowerCAmelCase : List[Any] = jnp.clip(snake_case__ , a_min=1e-20 ) # for rl-diffuser https://arxiv.org/abs/2205.09991 elif variance_type == "fixed_small_log": lowerCAmelCase : List[str] = jnp.log(jnp.clip(snake_case__ , a_min=1e-20 ) ) elif variance_type == "fixed_large": lowerCAmelCase : Optional[int] = state.common.betas[t] elif variance_type == "fixed_large_log": # Glide max_log lowerCAmelCase : List[str] = jnp.log(state.common.betas[t] ) elif variance_type == "learned": return predicted_variance elif variance_type == "learned_range": lowerCAmelCase : List[str] = variance lowerCAmelCase : Dict = state.common.betas[t] lowerCAmelCase : Optional[Any] = (predicted_variance + 1) / 2 lowerCAmelCase : List[str] = frac * max_log + (1 - frac) * min_log return variance def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ = None , snake_case__ = True , ): """simple docstring""" lowerCAmelCase : Optional[Any] = timestep if key is None: lowerCAmelCase : Tuple = jax.random.PRNGKey(0 ) if model_output.shape[1] == sample.shape[1] * 2 and self.config.variance_type in ["learned", "learned_range"]: lowerCAmelCase , lowerCAmelCase : Optional[Any] = jnp.split(snake_case__ , sample.shape[1] , axis=1 ) else: lowerCAmelCase : Tuple = None # 1. compute alphas, betas lowerCAmelCase : Optional[int] = state.common.alphas_cumprod[t] lowerCAmelCase : Optional[int] = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) lowerCAmelCase : Dict = 1 - alpha_prod_t lowerCAmelCase : Any = 1 - alpha_prod_t_prev # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if self.config.prediction_type == "epsilon": lowerCAmelCase : Union[str, Any] = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif self.config.prediction_type == "sample": lowerCAmelCase : List[Any] = model_output elif self.config.prediction_type == "v_prediction": lowerCAmelCase : Tuple = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output else: raise ValueError( f"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample` """ " for the FlaxDDPMScheduler." ) # 3. Clip "predicted x_0" if self.config.clip_sample: lowerCAmelCase : Optional[int] = jnp.clip(snake_case__ , -1 , 1 ) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowerCAmelCase : List[Any] = (alpha_prod_t_prev ** 0.5 * state.common.betas[t]) / beta_prod_t lowerCAmelCase : List[str] = state.common.alphas[t] ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowerCAmelCase : int = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise def random_variance(): lowerCAmelCase : Tuple = jax.random.split(snake_case__ , num=1 ) lowerCAmelCase : str = jax.random.normal(snake_case__ , shape=model_output.shape , dtype=self.dtype ) return (self._get_variance(snake_case__ , snake_case__ , predicted_variance=snake_case__ ) ** 0.5) * noise lowerCAmelCase : Union[str, Any] = jnp.where(t > 0 , random_variance() , jnp.zeros(model_output.shape , dtype=self.dtype ) ) lowerCAmelCase : Union[str, Any] = pred_prev_sample + variance if not return_dict: return (pred_prev_sample, state) return FlaxDDPMSchedulerOutput(prev_sample=snake_case__ , state=snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" return add_noise_common(state.common , snake_case__ , snake_case__ , snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" return get_velocity_common(state.common , snake_case__ , snake_case__ , snake_case__ ) def __len__( self ): """simple docstring""" return self.config.num_train_timesteps
681
0
"""simple docstring""" import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized, parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv("TEST_SAGEMAKER" , "False" ) ) is not True , reason="Skipping test because should only be run when releasing minor transformers version" , ) @pytest.mark.usefixtures("sm_env" ) @parameterized_class( [ { "framework": "pytorch", "script": "run_glue_model_parallelism.py", "model_name_or_path": "roberta-large", "instance_type": "ml.p3dn.24xlarge", "results": {"train_runtime": 16_00, "eval_accuracy": 0.3, "eval_loss": 1.2}, }, { "framework": "pytorch", "script": "run_glue.py", "model_name_or_path": "roberta-large", "instance_type": "ml.p3dn.24xlarge", "results": {"train_runtime": 16_00, "eval_accuracy": 0.3, "eval_loss": 1.2}, }, ] ) class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" if self.framework == "pytorch": subprocess.run( f"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split() , encoding="utf-8" , check=snake_case__ , ) assert hasattr(self , "env" ) def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Optional[Any] = { "enabled": True, "processes_per_host": 8, } lowerCAmelCase : List[Any] = { "enabled": True, "parameters": { "microbatches": 4, "placement_strategy": "spread", "pipeline": "interleaved", "optimize": "speed", "partitions": 4, "ddp": True, }, } lowerCAmelCase : List[Any] = {"smdistributed": {"modelparallel": smp_options}, "mpi": mpi_options} lowerCAmelCase : Optional[Any] = "trainer" if self.script == "run_glue.py" else "smtrainer" # creates estimator return HuggingFace( entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=f"""{self.env.base_job_name}-{instance_count}-smp-{name_extension}""" , instance_count=snake_case__ , instance_type=self.instance_type , debugger_hook_config=snake_case__ , hyperparameters={ **self.env.hyperparameters, "model_name_or_path": self.model_name_or_path, "max_steps": 500, } , metric_definitions=self.env.metric_definitions , distribution=snake_case__ , py_version="py36" , ) def lowercase__ ( self , snake_case__ ): """simple docstring""" TrainingJobAnalytics(snake_case__ ).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv""" ) @parameterized.expand([(1,)] ) def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = self.create_estimator(snake_case__ ) # run training estimator.fit() # result dataframe lowerCAmelCase : Tuple = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis lowerCAmelCase : Tuple = list(result_metrics_df[result_metrics_df.metric_name == "eval_accuracy"]["value"] ) lowerCAmelCase : Optional[int] = list(result_metrics_df[result_metrics_df.metric_name == "eval_loss"]["value"] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping lowerCAmelCase : Dict = ( Session().describe_training_job(estimator.latest_training_job.name ).get("TrainingTimeInSeconds" , 999_999 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results["eval_accuracy"] for t in eval_accuracy ) assert all(t <= self.results["eval_loss"] for t in eval_loss ) # dump tests result into json file to share in PR with open(f"""{estimator.latest_training_job.name}.json""" , "w" ) as outfile: json.dump({"train_time": train_runtime, "eval_accuracy": eval_accuracy, "eval_loss": eval_loss} , snake_case__ )
703
"""simple docstring""" import argparse import OmegaConf import torch from diffusers import DDIMScheduler, LDMPipeline, UNetLDMModel, VQModel def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : Tuple = OmegaConf.load(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = torch.load(SCREAMING_SNAKE_CASE , map_location="cpu" )["model"] lowerCAmelCase : int = list(state_dict.keys() ) # extract state_dict for VQVAE lowerCAmelCase : Tuple = {} lowerCAmelCase : Dict = "first_stage_model." for key in keys: if key.startswith(SCREAMING_SNAKE_CASE ): lowerCAmelCase : List[str] = state_dict[key] # extract state_dict for UNetLDM lowerCAmelCase : List[Any] = {} lowerCAmelCase : Tuple = "model.diffusion_model." for key in keys: if key.startswith(SCREAMING_SNAKE_CASE ): lowerCAmelCase : str = state_dict[key] lowerCAmelCase : List[str] = config.model.params.first_stage_config.params lowerCAmelCase : List[Any] = config.model.params.unet_config.params lowerCAmelCase : Union[str, Any] = VQModel(**SCREAMING_SNAKE_CASE ).eval() vqvae.load_state_dict(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = UNetLDMModel(**SCREAMING_SNAKE_CASE ).eval() unet.load_state_dict(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[Any] = DDIMScheduler( timesteps=config.model.params.timesteps , beta_schedule="scaled_linear" , beta_start=config.model.params.linear_start , beta_end=config.model.params.linear_end , clip_sample=SCREAMING_SNAKE_CASE , ) lowerCAmelCase : Tuple = LDMPipeline(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) pipeline.save_pretrained(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument('''--checkpoint_path''', type=str, required=True) parser.add_argument('''--config_path''', type=str, required=True) parser.add_argument('''--output_path''', type=str, required=True) lowerCAmelCase__ = parser.parse_args() convert_ldm_original(args.checkpoint_path, args.config_path, args.output_path)
681
0
"""simple docstring""" import re import string from collections import Counter import sacrebleu import sacremoses from packaging import version import datasets lowerCAmelCase__ = ''' @inproceedings{xu-etal-2016-optimizing, title = {Optimizing Statistical Machine Translation for Text Simplification}, authors={Xu, Wei and Napoles, Courtney and Pavlick, Ellie and Chen, Quanze and Callison-Burch, Chris}, journal = {Transactions of the Association for Computational Linguistics}, volume = {4}, year={2016}, url = {https://www.aclweb.org/anthology/Q16-1029}, pages = {401--415 }, @inproceedings{post-2018-call, title = "A Call for Clarity in Reporting {BLEU} Scores", author = "Post, Matt", booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers", month = oct, year = "2018", address = "Belgium, Brussels", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/W18-6319", pages = "186--191", } ''' lowerCAmelCase__ = '''\ WIKI_SPLIT is the combination of three metrics SARI, EXACT and SACREBLEU It can be used to evaluate the quality of machine-generated texts. ''' lowerCAmelCase__ = ''' Calculates sari score (between 0 and 100) given a list of source and predicted sentences, and a list of lists of reference sentences. It also computes the BLEU score as well as the exact match score. Args: sources: list of source sentences where each sentence should be a string. predictions: list of predicted sentences where each sentence should be a string. references: list of lists of reference sentences where each sentence should be a string. Returns: sari: sari score sacrebleu: sacrebleu score exact: exact score Examples: >>> sources=["About 95 species are currently accepted ."] >>> predictions=["About 95 you now get in ."] >>> references=[["About 95 species are currently known ."]] >>> wiki_split = datasets.load_metric("wiki_split") >>> results = wiki_split.compute(sources=sources, predictions=predictions, references=references) >>> print(results) {\'sari\': 21.805555555555557, \'sacrebleu\': 14.535768424205482, \'exact\': 0.0} ''' def a__ ( SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' def remove_articles(SCREAMING_SNAKE_CASE : Optional[Any] ): lowerCAmelCase : int = re.compile(r"\b(a|an|the)\b" , re.UNICODE ) return re.sub(SCREAMING_SNAKE_CASE , " " , SCREAMING_SNAKE_CASE ) def white_space_fix(SCREAMING_SNAKE_CASE : List[str] ): return " ".join(text.split() ) def remove_punc(SCREAMING_SNAKE_CASE : Optional[Any] ): lowerCAmelCase : Union[str, Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(SCREAMING_SNAKE_CASE : int ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(SCREAMING_SNAKE_CASE ) ) ) ) def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Dict ): '''simple docstring''' return int(normalize_answer(SCREAMING_SNAKE_CASE ) == normalize_answer(SCREAMING_SNAKE_CASE ) ) def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Any ): '''simple docstring''' lowerCAmelCase : List[str] = [any(compute_exact(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) for ref in refs ) for pred, refs in zip(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE )] return (sum(SCREAMING_SNAKE_CASE ) / len(SCREAMING_SNAKE_CASE )) * 1_0_0 def a__ ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : str ): '''simple docstring''' lowerCAmelCase : Optional[int] = [rgram for rgrams in rgramslist for rgram in rgrams] lowerCAmelCase : List[str] = Counter(SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[Any] = Counter(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = Counter() for sgram, scount in sgramcounter.items(): lowerCAmelCase : Union[str, Any] = scount * numref lowerCAmelCase : Dict = Counter(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[int] = Counter() for cgram, ccount in cgramcounter.items(): lowerCAmelCase : Optional[Any] = ccount * numref # KEEP lowerCAmelCase : Optional[Any] = sgramcounter_rep & cgramcounter_rep lowerCAmelCase : List[str] = keepgramcounter_rep & rgramcounter lowerCAmelCase : List[Any] = sgramcounter_rep & rgramcounter lowerCAmelCase : str = 0 lowerCAmelCase : List[str] = 0 for keepgram in keepgramcountergood_rep: keeptmpscorea += keepgramcountergood_rep[keepgram] / keepgramcounter_rep[keepgram] # Fix an alleged bug [2] in the keep score computation. # keeptmpscore2 += keepgramcountergood_rep[keepgram] / keepgramcounterall_rep[keepgram] keeptmpscorea += keepgramcountergood_rep[keepgram] # Define 0/0=1 instead of 0 to give higher scores for predictions that match # a target exactly. lowerCAmelCase : Optional[Any] = 1 lowerCAmelCase : int = 1 if len(SCREAMING_SNAKE_CASE ) > 0: lowerCAmelCase : Union[str, Any] = keeptmpscorea / len(SCREAMING_SNAKE_CASE ) if len(SCREAMING_SNAKE_CASE ) > 0: # Fix an alleged bug [2] in the keep score computation. # keepscore_recall = keeptmpscore2 / len(keepgramcounterall_rep) lowerCAmelCase : Optional[Any] = keeptmpscorea / sum(keepgramcounterall_rep.values() ) lowerCAmelCase : List[str] = 0 if keepscore_precision > 0 or keepscore_recall > 0: lowerCAmelCase : Any = 2 * keepscore_precision * keepscore_recall / (keepscore_precision + keepscore_recall) # DELETION lowerCAmelCase : Any = sgramcounter_rep - cgramcounter_rep lowerCAmelCase : Optional[Any] = delgramcounter_rep - rgramcounter lowerCAmelCase : Union[str, Any] = sgramcounter_rep - rgramcounter lowerCAmelCase : str = 0 lowerCAmelCase : Union[str, Any] = 0 for delgram in delgramcountergood_rep: deltmpscorea += delgramcountergood_rep[delgram] / delgramcounter_rep[delgram] deltmpscorea += delgramcountergood_rep[delgram] / delgramcounterall_rep[delgram] # Define 0/0=1 instead of 0 to give higher scores for predictions that match # a target exactly. lowerCAmelCase : Any = 1 if len(SCREAMING_SNAKE_CASE ) > 0: lowerCAmelCase : List[str] = deltmpscorea / len(SCREAMING_SNAKE_CASE ) # ADDITION lowerCAmelCase : Union[str, Any] = set(SCREAMING_SNAKE_CASE ) - set(SCREAMING_SNAKE_CASE ) lowerCAmelCase : str = set(SCREAMING_SNAKE_CASE ) & set(SCREAMING_SNAKE_CASE ) lowerCAmelCase : str = set(SCREAMING_SNAKE_CASE ) - set(SCREAMING_SNAKE_CASE ) lowerCAmelCase : str = 0 for addgram in addgramcountergood: addtmpscore += 1 # Define 0/0=1 instead of 0 to give higher scores for predictions that match # a target exactly. lowerCAmelCase : List[str] = 1 lowerCAmelCase : str = 1 if len(SCREAMING_SNAKE_CASE ) > 0: lowerCAmelCase : str = addtmpscore / len(SCREAMING_SNAKE_CASE ) if len(SCREAMING_SNAKE_CASE ) > 0: lowerCAmelCase : str = addtmpscore / len(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Any = 0 if addscore_precision > 0 or addscore_recall > 0: lowerCAmelCase : List[Any] = 2 * addscore_precision * addscore_recall / (addscore_precision + addscore_recall) return (keepscore, delscore_precision, addscore) def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Any ): '''simple docstring''' lowerCAmelCase : List[str] = len(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[int] = ssent.split(" " ) lowerCAmelCase : Dict = csent.split(" " ) lowerCAmelCase : List[str] = [] lowerCAmelCase : List[str] = [] lowerCAmelCase : Tuple = [] lowerCAmelCase : Dict = [] lowerCAmelCase : Any = [] lowerCAmelCase : int = [] lowerCAmelCase : List[Any] = [] lowerCAmelCase : Optional[int] = [] lowerCAmelCase : Optional[Any] = [] lowerCAmelCase : List[str] = [] for rsent in rsents: lowerCAmelCase : int = rsent.split(" " ) lowerCAmelCase : Union[str, Any] = [] lowerCAmelCase : Optional[int] = [] lowerCAmelCase : Optional[Any] = [] ragramslist.append(SCREAMING_SNAKE_CASE ) for i in range(0 , len(SCREAMING_SNAKE_CASE ) - 1 ): if i < len(SCREAMING_SNAKE_CASE ) - 1: lowerCAmelCase : int = ragrams[i] + " " + ragrams[i + 1] ragrams.append(SCREAMING_SNAKE_CASE ) if i < len(SCREAMING_SNAKE_CASE ) - 2: lowerCAmelCase : Union[str, Any] = ragrams[i] + " " + ragrams[i + 1] + " " + ragrams[i + 2] ragrams.append(SCREAMING_SNAKE_CASE ) if i < len(SCREAMING_SNAKE_CASE ) - 3: lowerCAmelCase : Optional[int] = ragrams[i] + " " + ragrams[i + 1] + " " + ragrams[i + 2] + " " + ragrams[i + 3] ragrams.append(SCREAMING_SNAKE_CASE ) ragramslist.append(SCREAMING_SNAKE_CASE ) ragramslist.append(SCREAMING_SNAKE_CASE ) ragramslist.append(SCREAMING_SNAKE_CASE ) for i in range(0 , len(SCREAMING_SNAKE_CASE ) - 1 ): if i < len(SCREAMING_SNAKE_CASE ) - 1: lowerCAmelCase : List[Any] = sagrams[i] + " " + sagrams[i + 1] sagrams.append(SCREAMING_SNAKE_CASE ) if i < len(SCREAMING_SNAKE_CASE ) - 2: lowerCAmelCase : Tuple = sagrams[i] + " " + sagrams[i + 1] + " " + sagrams[i + 2] sagrams.append(SCREAMING_SNAKE_CASE ) if i < len(SCREAMING_SNAKE_CASE ) - 3: lowerCAmelCase : int = sagrams[i] + " " + sagrams[i + 1] + " " + sagrams[i + 2] + " " + sagrams[i + 3] sagrams.append(SCREAMING_SNAKE_CASE ) for i in range(0 , len(SCREAMING_SNAKE_CASE ) - 1 ): if i < len(SCREAMING_SNAKE_CASE ) - 1: lowerCAmelCase : str = cagrams[i] + " " + cagrams[i + 1] cagrams.append(SCREAMING_SNAKE_CASE ) if i < len(SCREAMING_SNAKE_CASE ) - 2: lowerCAmelCase : List[str] = cagrams[i] + " " + cagrams[i + 1] + " " + cagrams[i + 2] cagrams.append(SCREAMING_SNAKE_CASE ) if i < len(SCREAMING_SNAKE_CASE ) - 3: lowerCAmelCase : List[Any] = cagrams[i] + " " + cagrams[i + 1] + " " + cagrams[i + 2] + " " + cagrams[i + 3] cagrams.append(SCREAMING_SNAKE_CASE ) (lowerCAmelCase) : List[str] = SARIngram(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) (lowerCAmelCase) : Any = SARIngram(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) (lowerCAmelCase) : Optional[Any] = SARIngram(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) (lowerCAmelCase) : Union[str, Any] = SARIngram(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) lowerCAmelCase : str = sum([keepascore, keepascore, keepascore, keepascore] ) / 4 lowerCAmelCase : int = sum([delascore, delascore, delascore, delascore] ) / 4 lowerCAmelCase : Optional[int] = sum([addascore, addascore, addascore, addascore] ) / 4 lowerCAmelCase : List[Any] = (avgkeepscore + avgdelscore + avgaddscore) / 3 return finalscore def a__ ( SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : bool = True , SCREAMING_SNAKE_CASE : str = "13a" , SCREAMING_SNAKE_CASE : bool = True ): '''simple docstring''' if lowercase: lowerCAmelCase : Any = sentence.lower() if tokenizer in ["13a", "intl"]: if version.parse(sacrebleu.__version__ ).major >= 2: lowerCAmelCase : str = sacrebleu.metrics.bleu._get_tokenizer(SCREAMING_SNAKE_CASE )()(SCREAMING_SNAKE_CASE ) else: lowerCAmelCase : Dict = sacrebleu.TOKENIZERS[tokenizer]()(SCREAMING_SNAKE_CASE ) elif tokenizer == "moses": lowerCAmelCase : Optional[Any] = sacremoses.MosesTokenizer().tokenize(SCREAMING_SNAKE_CASE , return_str=SCREAMING_SNAKE_CASE , escape=SCREAMING_SNAKE_CASE ) elif tokenizer == "penn": lowerCAmelCase : List[str] = sacremoses.MosesTokenizer().penn_tokenize(SCREAMING_SNAKE_CASE , return_str=SCREAMING_SNAKE_CASE ) else: lowerCAmelCase : Optional[int] = sentence if not return_str: lowerCAmelCase : str = normalized_sent.split() return normalized_sent def a__ ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : str ): '''simple docstring''' if not (len(SCREAMING_SNAKE_CASE ) == len(SCREAMING_SNAKE_CASE ) == len(SCREAMING_SNAKE_CASE )): raise ValueError("Sources length must match predictions and references lengths." ) lowerCAmelCase : Union[str, Any] = 0 for src, pred, refs in zip(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): sari_score += SARIsent(normalize(SCREAMING_SNAKE_CASE ) , normalize(SCREAMING_SNAKE_CASE ) , [normalize(SCREAMING_SNAKE_CASE ) for sent in refs] ) lowerCAmelCase : List[Any] = sari_score / len(SCREAMING_SNAKE_CASE ) return 1_0_0 * sari_score def a__ ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : int="exp" , SCREAMING_SNAKE_CASE : List[Any]=None , SCREAMING_SNAKE_CASE : List[str]=False , SCREAMING_SNAKE_CASE : int=False , SCREAMING_SNAKE_CASE : Optional[Any]=False , ): '''simple docstring''' lowerCAmelCase : List[str] = len(references[0] ) if any(len(SCREAMING_SNAKE_CASE ) != references_per_prediction for refs in references ): raise ValueError("Sacrebleu requires the same number of references for each prediction" ) lowerCAmelCase : List[str] = [[refs[i] for refs in references] for i in range(SCREAMING_SNAKE_CASE )] lowerCAmelCase : Tuple = sacrebleu.corpus_bleu( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , smooth_method=SCREAMING_SNAKE_CASE , smooth_value=SCREAMING_SNAKE_CASE , force=SCREAMING_SNAKE_CASE , lowercase=SCREAMING_SNAKE_CASE , use_effective_order=SCREAMING_SNAKE_CASE , ) return output.score @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE__ ( datasets.Metric ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Sequence(datasets.Value("string" , id="sequence" ) , id="references" ), } ) , codebase_urls=[ "https://github.com/huggingface/transformers/blob/master/src/transformers/data/metrics/squad_metrics.py", "https://github.com/cocoxu/simplification/blob/master/SARI.py", "https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/utils/sari_hook.py", "https://github.com/mjpost/sacreBLEU", ] , reference_urls=[ "https://www.aclweb.org/anthology/Q16-1029.pdf", "https://github.com/mjpost/sacreBLEU", "https://en.wikipedia.org/wiki/BLEU", "https://towardsdatascience.com/evaluating-text-output-in-nlp-bleu-at-your-own-risk-e8609665a213", ] , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Any = {} result.update({"sari": compute_sari(sources=snake_case__ , predictions=snake_case__ , references=snake_case__ )} ) result.update({"sacrebleu": compute_sacrebleu(predictions=snake_case__ , references=snake_case__ )} ) result.update({"exact": compute_em(predictions=snake_case__ , references=snake_case__ )} ) return result
704
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : int = 1_0_0_0 ): '''simple docstring''' return sum(e for e in range(3 , SCREAMING_SNAKE_CASE ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"{solution() = }")
681
0
"""simple docstring""" import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( '''The `image_to_image.py` script is outdated. Please use directly `from diffusers import''' ''' StableDiffusionImg2ImgPipeline` instead.''' )
705
"""simple docstring""" import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized, parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv("TEST_SAGEMAKER" , "False" ) ) is not True , reason="Skipping test because should only be run when releasing minor transformers version" , ) @pytest.mark.usefixtures("sm_env" ) @parameterized_class( [ { "framework": "pytorch", "script": "run_glue_model_parallelism.py", "model_name_or_path": "roberta-large", "instance_type": "ml.p3dn.24xlarge", "results": {"train_runtime": 16_00, "eval_accuracy": 0.3, "eval_loss": 1.2}, }, { "framework": "pytorch", "script": "run_glue.py", "model_name_or_path": "roberta-large", "instance_type": "ml.p3dn.24xlarge", "results": {"train_runtime": 16_00, "eval_accuracy": 0.3, "eval_loss": 1.2}, }, ] ) class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" if self.framework == "pytorch": subprocess.run( f"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split() , encoding="utf-8" , check=snake_case__ , ) assert hasattr(self , "env" ) def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Optional[Any] = { "enabled": True, "processes_per_host": 8, } lowerCAmelCase : List[Any] = { "enabled": True, "parameters": { "microbatches": 4, "placement_strategy": "spread", "pipeline": "interleaved", "optimize": "speed", "partitions": 4, "ddp": True, }, } lowerCAmelCase : List[Any] = {"smdistributed": {"modelparallel": smp_options}, "mpi": mpi_options} lowerCAmelCase : Optional[Any] = "trainer" if self.script == "run_glue.py" else "smtrainer" # creates estimator return HuggingFace( entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=f"""{self.env.base_job_name}-{instance_count}-smp-{name_extension}""" , instance_count=snake_case__ , instance_type=self.instance_type , debugger_hook_config=snake_case__ , hyperparameters={ **self.env.hyperparameters, "model_name_or_path": self.model_name_or_path, "max_steps": 500, } , metric_definitions=self.env.metric_definitions , distribution=snake_case__ , py_version="py36" , ) def lowercase__ ( self , snake_case__ ): """simple docstring""" TrainingJobAnalytics(snake_case__ ).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv""" ) @parameterized.expand([(1,)] ) def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = self.create_estimator(snake_case__ ) # run training estimator.fit() # result dataframe lowerCAmelCase : Tuple = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis lowerCAmelCase : Tuple = list(result_metrics_df[result_metrics_df.metric_name == "eval_accuracy"]["value"] ) lowerCAmelCase : Optional[int] = list(result_metrics_df[result_metrics_df.metric_name == "eval_loss"]["value"] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping lowerCAmelCase : Dict = ( Session().describe_training_job(estimator.latest_training_job.name ).get("TrainingTimeInSeconds" , 999_999 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results["eval_accuracy"] for t in eval_accuracy ) assert all(t <= self.results["eval_loss"] for t in eval_loss ) # dump tests result into json file to share in PR with open(f"""{estimator.latest_training_job.name}.json""" , "w" ) as outfile: json.dump({"train_time": train_runtime, "eval_accuracy": eval_accuracy, "eval_loss": eval_loss} , snake_case__ )
681
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowerCAmelCase__ = { '''configuration_blip''': [ '''BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlipConfig''', '''BlipTextConfig''', '''BlipVisionConfig''', ], '''processing_blip''': ['''BlipProcessor'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''BlipImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''BLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlipModel''', '''BlipPreTrainedModel''', '''BlipForConditionalGeneration''', '''BlipForQuestionAnswering''', '''BlipVisionModel''', '''BlipTextModel''', '''BlipForImageTextRetrieval''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFBlipModel''', '''TFBlipPreTrainedModel''', '''TFBlipForConditionalGeneration''', '''TFBlipForQuestionAnswering''', '''TFBlipVisionModel''', '''TFBlipTextModel''', '''TFBlipForImageTextRetrieval''', ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
706
"""simple docstring""" from math import factorial def a__ ( SCREAMING_SNAKE_CASE : int = 1_0_0 ): '''simple docstring''' return sum(int(SCREAMING_SNAKE_CASE ) for x in str(factorial(SCREAMING_SNAKE_CASE ) ) ) if __name__ == "__main__": print(solution(int(input('''Enter the Number: ''').strip())))
681
0
"""simple docstring""" lowerCAmelCase__ = [ (1_000, '''M'''), (900, '''CM'''), (500, '''D'''), (400, '''CD'''), (100, '''C'''), (90, '''XC'''), (50, '''L'''), (40, '''XL'''), (10, '''X'''), (9, '''IX'''), (5, '''V'''), (4, '''IV'''), (1, '''I'''), ] def a__ ( SCREAMING_SNAKE_CASE : str ): '''simple docstring''' lowerCAmelCase : List[str] = {"I": 1, "V": 5, "X": 1_0, "L": 5_0, "C": 1_0_0, "D": 5_0_0, "M": 1_0_0_0} lowerCAmelCase : str = 0 lowerCAmelCase : Optional[Any] = 0 while place < len(SCREAMING_SNAKE_CASE ): if (place + 1 < len(SCREAMING_SNAKE_CASE )) and (vals[roman[place]] < vals[roman[place + 1]]): total += vals[roman[place + 1]] - vals[roman[place]] place += 2 else: total += vals[roman[place]] place += 1 return total def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' lowerCAmelCase : str = [] for arabic, roman in ROMAN: (lowerCAmelCase) : int = divmod(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) result.append(roman * factor ) if number == 0: break return "".join(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod()
707
"""simple docstring""" from typing import Any class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = data lowerCAmelCase : Any = None def __repr__( self ): """simple docstring""" return f"""Node({self.data})""" class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self ): """simple docstring""" lowerCAmelCase : Tuple = None def __iter__( self ): """simple docstring""" lowerCAmelCase : Any = self.head while node: yield node.data lowerCAmelCase : Optional[int] = node.next def __len__( self ): """simple docstring""" return sum(1 for _ in self ) def __repr__( self ): """simple docstring""" return "->".join([str(snake_case__ ) for item in self] ) def __getitem__( self , snake_case__ ): """simple docstring""" if not 0 <= index < len(self ): raise ValueError("list index out of range." ) for i, node in enumerate(self ): if i == index: return node return None def __setitem__( self , snake_case__ , snake_case__ ): """simple docstring""" if not 0 <= index < len(self ): raise ValueError("list index out of range." ) lowerCAmelCase : Union[str, Any] = self.head for _ in range(snake_case__ ): lowerCAmelCase : int = current.next lowerCAmelCase : List[str] = data def lowercase__ ( self , snake_case__ ): """simple docstring""" self.insert_nth(len(self ) , snake_case__ ) def lowercase__ ( self , snake_case__ ): """simple docstring""" self.insert_nth(0 , snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ ): """simple docstring""" if not 0 <= index <= len(self ): raise IndexError("list index out of range" ) lowerCAmelCase : Optional[int] = Node(snake_case__ ) if self.head is None: lowerCAmelCase : Any = new_node elif index == 0: lowerCAmelCase : Any = self.head # link new_node to head lowerCAmelCase : Union[str, Any] = new_node else: lowerCAmelCase : List[str] = self.head for _ in range(index - 1 ): lowerCAmelCase : int = temp.next lowerCAmelCase : int = temp.next lowerCAmelCase : Dict = new_node def lowercase__ ( self ): # print every node data """simple docstring""" print(self ) def lowercase__ ( self ): """simple docstring""" return self.delete_nth(0 ) def lowercase__ ( self ): # delete from tail """simple docstring""" return self.delete_nth(len(self ) - 1 ) def lowercase__ ( self , snake_case__ = 0 ): """simple docstring""" if not 0 <= index <= len(self ) - 1: # test if index is valid raise IndexError("List index out of range." ) lowerCAmelCase : List[Any] = self.head # default first node if index == 0: lowerCAmelCase : Optional[int] = self.head.next else: lowerCAmelCase : List[str] = self.head for _ in range(index - 1 ): lowerCAmelCase : Union[str, Any] = temp.next lowerCAmelCase : Optional[Any] = temp.next lowerCAmelCase : Any = temp.next.next return delete_node.data def lowercase__ ( self ): """simple docstring""" return self.head is None def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = None lowerCAmelCase : Optional[int] = self.head while current: # Store the current node's next node. lowerCAmelCase : List[Any] = current.next # Make the current node's next point backwards lowerCAmelCase : Dict = prev # Make the previous node be the current node lowerCAmelCase : List[str] = current # Make the current node the next node (to progress iteration) lowerCAmelCase : int = next_node # Return prev in order to put the head at the end lowerCAmelCase : Tuple = prev def a__ ( ): '''simple docstring''' lowerCAmelCase : Tuple = LinkedList() assert linked_list.is_empty() is True assert str(SCREAMING_SNAKE_CASE ) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(1_0 ): assert len(SCREAMING_SNAKE_CASE ) == i linked_list.insert_nth(SCREAMING_SNAKE_CASE , i + 1 ) assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(1 , 1_1 ) ) linked_list.insert_head(0 ) linked_list.insert_tail(1_1 ) assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(0 , 1_2 ) ) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9 ) == 1_0 assert linked_list.delete_tail() == 1_1 assert len(SCREAMING_SNAKE_CASE ) == 9 assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(1 , 1_0 ) ) assert all(linked_list[i] == i + 1 for i in range(0 , 9 ) ) is True for i in range(0 , 9 ): lowerCAmelCase : Optional[Any] = -i assert all(linked_list[i] == -i for i in range(0 , 9 ) ) is True linked_list.reverse() assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(-8 , 1 ) ) def a__ ( ): '''simple docstring''' lowerCAmelCase : List[str] = [ -9, 1_0_0, Node(7_7_3_4_5_1_1_2 ), "dlrow olleH", 7, 5_5_5_5, 0, -192.55_555, "Hello, world!", 77.9, Node(1_0 ), None, None, 12.20, ] lowerCAmelCase : List[str] = LinkedList() for i in test_input: linked_list.insert_tail(SCREAMING_SNAKE_CASE ) # Check if it's empty or not assert linked_list.is_empty() is False assert ( str(SCREAMING_SNAKE_CASE ) == "-9->100->Node(77345112)->dlrow olleH->7->5555->0->" "-192.55555->Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the head lowerCAmelCase : str = linked_list.delete_head() assert result == -9 assert ( str(SCREAMING_SNAKE_CASE ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the tail lowerCAmelCase : Union[str, Any] = linked_list.delete_tail() assert result == 12.2 assert ( str(SCREAMING_SNAKE_CASE ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None" ) # Delete a node in specific location in linked list lowerCAmelCase : List[str] = linked_list.delete_nth(1_0 ) assert result is None assert ( str(SCREAMING_SNAKE_CASE ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None" ) # Add a Node instance to its head linked_list.insert_head(Node("Hello again, world!" ) ) assert ( str(SCREAMING_SNAKE_CASE ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None" ) # Add None to its tail linked_list.insert_tail(SCREAMING_SNAKE_CASE ) assert ( str(SCREAMING_SNAKE_CASE ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None->None" ) # Reverse the linked list linked_list.reverse() assert ( str(SCREAMING_SNAKE_CASE ) == "None->None->Node(10)->77.9->Hello, world!->-192.55555->0->5555->" "7->dlrow olleH->Node(77345112)->100->Node(Hello again, world!)" ) def a__ ( ): '''simple docstring''' from doctest import testmod testmod() lowerCAmelCase : Optional[Any] = LinkedList() linked_list.insert_head(input("Inserting 1st at head " ).strip() ) linked_list.insert_head(input("Inserting 2nd at head " ).strip() ) print("\nPrint list:" ) linked_list.print_list() linked_list.insert_tail(input("\nInserting 1st at tail " ).strip() ) linked_list.insert_tail(input("Inserting 2nd at tail " ).strip() ) print("\nPrint list:" ) linked_list.print_list() print("\nDelete head" ) linked_list.delete_head() print("Delete tail" ) linked_list.delete_tail() print("\nPrint list:" ) linked_list.print_list() print("\nReverse linked list" ) linked_list.reverse() print("\nPrint list:" ) linked_list.print_list() print("\nString representation of linked list:" ) print(SCREAMING_SNAKE_CASE ) print("\nReading/changing Node data using indexing:" ) print(f"""Element at Position 1: {linked_list[1]}""" ) lowerCAmelCase : Any = input("Enter New Value: " ).strip() print("New list:" ) print(SCREAMING_SNAKE_CASE ) print(f"""length of linked_list is : {len(SCREAMING_SNAKE_CASE )}""" ) if __name__ == "__main__": main()
681
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''edbeeching/decision-transformer-gym-hopper-medium''': ( '''https://huggingface.co/edbeeching/decision-transformer-gym-hopper-medium/resolve/main/config.json''' ), # See all DecisionTransformer models at https://huggingface.co/models?filter=decision_transformer } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : int ="decision_transformer" a : Optional[int] =["past_key_values"] a : int ={ "max_position_embeddings": "n_positions", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self , snake_case__=17 , snake_case__=4 , snake_case__=128 , snake_case__=4_096 , snake_case__=True , snake_case__=1 , snake_case__=1_024 , snake_case__=3 , snake_case__=1 , snake_case__=None , snake_case__="relu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=1e-5 , snake_case__=0.02 , snake_case__=True , snake_case__=True , snake_case__=50_256 , snake_case__=50_256 , snake_case__=False , snake_case__=False , **snake_case__ , ): """simple docstring""" lowerCAmelCase : str = state_dim lowerCAmelCase : List[Any] = act_dim lowerCAmelCase : Optional[Any] = hidden_size lowerCAmelCase : List[Any] = max_ep_len lowerCAmelCase : Union[str, Any] = action_tanh lowerCAmelCase : List[str] = vocab_size lowerCAmelCase : Dict = n_positions lowerCAmelCase : List[str] = n_layer lowerCAmelCase : Dict = n_head lowerCAmelCase : Union[str, Any] = n_inner lowerCAmelCase : Union[str, Any] = activation_function lowerCAmelCase : List[Any] = resid_pdrop lowerCAmelCase : Tuple = embd_pdrop lowerCAmelCase : List[Any] = attn_pdrop lowerCAmelCase : List[Any] = layer_norm_epsilon lowerCAmelCase : Dict = initializer_range lowerCAmelCase : str = scale_attn_weights lowerCAmelCase : Any = use_cache lowerCAmelCase : Union[str, Any] = scale_attn_by_inverse_layer_idx lowerCAmelCase : Union[str, Any] = reorder_and_upcast_attn lowerCAmelCase : Optional[Any] = bos_token_id lowerCAmelCase : Optional[int] = eos_token_id super().__init__(bos_token_id=snake_case__ , eos_token_id=snake_case__ , **snake_case__ )
708
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowerCAmelCase__ = { '''configuration_efficientformer''': [ '''EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EfficientFormerConfig''', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''EfficientFormerImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EfficientFormerForImageClassification''', '''EfficientFormerForImageClassificationWithTeacher''', '''EfficientFormerModel''', '''EfficientFormerPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFEfficientFormerForImageClassification''', '''TFEfficientFormerForImageClassificationWithTeacher''', '''TFEfficientFormerModel''', '''TFEfficientFormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_efficientformer import EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientformer import EfficientFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientformer import ( EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientFormerForImageClassification, EfficientFormerForImageClassificationWithTeacher, EfficientFormerModel, EfficientFormerPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_efficientformer import ( TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFEfficientFormerForImageClassification, TFEfficientFormerForImageClassificationWithTeacher, TFEfficientFormerModel, TFEfficientFormerPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
681
0
"""simple docstring""" import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast @require_vision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = tempfile.mkdtemp() lowerCAmelCase : List[str] = BlipImageProcessor() lowerCAmelCase : Optional[Any] = BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-BertModel" ) lowerCAmelCase : str = BlipProcessor(snake_case__ , snake_case__ ) processor.save_pretrained(self.tmpdirname ) def lowercase__ ( self , **snake_case__ ): """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **snake_case__ ).tokenizer def lowercase__ ( self , **snake_case__ ): """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **snake_case__ ).image_processor def lowercase__ ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] lowerCAmelCase : Dict = [Image.fromarray(np.moveaxis(snake_case__ , 0 , -1 ) ) for x in image_inputs] return image_inputs def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Tuple = BlipProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) lowerCAmelCase : Any = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) lowerCAmelCase : Optional[int] = self.get_image_processor(do_normalize=snake_case__ , padding_value=1.0 ) lowerCAmelCase : List[str] = BlipProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=snake_case__ , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , snake_case__ ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = self.get_image_processor() lowerCAmelCase : str = self.get_tokenizer() lowerCAmelCase : int = BlipProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) lowerCAmelCase : Tuple = self.prepare_image_inputs() lowerCAmelCase : str = image_processor(snake_case__ , return_tensors="np" ) lowerCAmelCase : Optional[Any] = processor(images=snake_case__ , return_tensors="np" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = self.get_image_processor() lowerCAmelCase : Union[str, Any] = self.get_tokenizer() lowerCAmelCase : Optional[Any] = BlipProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) lowerCAmelCase : Union[str, Any] = "lower newer" lowerCAmelCase : List[str] = processor(text=snake_case__ ) lowerCAmelCase : List[Any] = tokenizer(snake_case__ , return_token_type_ids=snake_case__ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = self.get_image_processor() lowerCAmelCase : Optional[Any] = self.get_tokenizer() lowerCAmelCase : Optional[Any] = BlipProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) lowerCAmelCase : int = "lower newer" lowerCAmelCase : List[Any] = self.prepare_image_inputs() lowerCAmelCase : str = processor(text=snake_case__ , images=snake_case__ ) self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] ) # test if it raises when no input is passed with pytest.raises(snake_case__ ): processor() def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = self.get_image_processor() lowerCAmelCase : Union[str, Any] = self.get_tokenizer() lowerCAmelCase : str = BlipProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) lowerCAmelCase : List[str] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] lowerCAmelCase : str = processor.batch_decode(snake_case__ ) lowerCAmelCase : Optional[int] = tokenizer.batch_decode(snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = self.get_image_processor() lowerCAmelCase : List[str] = self.get_tokenizer() lowerCAmelCase : Union[str, Any] = BlipProcessor(tokenizer=snake_case__ , image_processor=snake_case__ ) lowerCAmelCase : str = "lower newer" lowerCAmelCase : Optional[Any] = self.prepare_image_inputs() lowerCAmelCase : List[str] = processor(text=snake_case__ , images=snake_case__ ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] )
709
"""simple docstring""" 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 ) lowerCAmelCase__ = logging.getLogger(__name__) def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : Optional[Any] = np.argmax(SCREAMING_SNAKE_CASE , axis=1 ) return np.sum(outputs == labels ) def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE , encoding="utf_8" ) as f: lowerCAmelCase : Tuple = csv.reader(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = [] next(SCREAMING_SNAKE_CASE ) # skip the first line for line in tqdm(SCREAMING_SNAKE_CASE ): output.append((" ".join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : str ): '''simple docstring''' lowerCAmelCase : List[Any] = [] for dataset in encoded_datasets: lowerCAmelCase : Dict = len(SCREAMING_SNAKE_CASE ) lowerCAmelCase : str = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) lowerCAmelCase : Tuple = np.zeros((n_batch, 2) , dtype=np.intaa ) lowerCAmelCase : int = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) lowerCAmelCase : List[Any] = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(SCREAMING_SNAKE_CASE ): lowerCAmelCase : int = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase : Union[str, Any] = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase : Tuple = with_conta lowerCAmelCase : Any = with_conta lowerCAmelCase : Optional[Any] = len(SCREAMING_SNAKE_CASE ) - 1 lowerCAmelCase : Dict = len(SCREAMING_SNAKE_CASE ) - 1 lowerCAmelCase : Optional[Any] = with_conta lowerCAmelCase : List[Any] = with_conta lowerCAmelCase : str = mc_label lowerCAmelCase : Dict = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(SCREAMING_SNAKE_CASE ) for t in all_inputs ) ) return tensor_datasets def a__ ( ): '''simple docstring''' lowerCAmelCase : Optional[int] = argparse.ArgumentParser() parser.add_argument("--model_name" , type=SCREAMING_SNAKE_CASE , 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=SCREAMING_SNAKE_CASE , type=SCREAMING_SNAKE_CASE , required=SCREAMING_SNAKE_CASE , help="The output directory where the model predictions and checkpoints will be written." , ) parser.add_argument("--train_dataset" , type=SCREAMING_SNAKE_CASE , default="" ) parser.add_argument("--eval_dataset" , type=SCREAMING_SNAKE_CASE , default="" ) parser.add_argument("--seed" , type=SCREAMING_SNAKE_CASE , default=4_2 ) parser.add_argument("--num_train_epochs" , type=SCREAMING_SNAKE_CASE , default=3 ) parser.add_argument("--train_batch_size" , type=SCREAMING_SNAKE_CASE , default=8 ) parser.add_argument("--eval_batch_size" , type=SCREAMING_SNAKE_CASE , default=1_6 ) parser.add_argument("--adam_epsilon" , default=1E-8 , type=SCREAMING_SNAKE_CASE , help="Epsilon for Adam optimizer." ) parser.add_argument("--max_grad_norm" , type=SCREAMING_SNAKE_CASE , default=1 ) parser.add_argument( "--max_steps" , default=-1 , type=SCREAMING_SNAKE_CASE , help=( "If > 0: set total number of training steps to perform. Override num_train_epochs." ) , ) parser.add_argument( "--gradient_accumulation_steps" , type=SCREAMING_SNAKE_CASE , default=1 , help="Number of updates steps to accumulate before performing a backward/update pass." , ) parser.add_argument("--learning_rate" , type=SCREAMING_SNAKE_CASE , default=6.2_5E-5 ) parser.add_argument("--warmup_steps" , default=0 , type=SCREAMING_SNAKE_CASE , help="Linear warmup over warmup_steps." ) parser.add_argument("--lr_schedule" , type=SCREAMING_SNAKE_CASE , default="warmup_linear" ) parser.add_argument("--weight_decay" , type=SCREAMING_SNAKE_CASE , default=0.01 ) parser.add_argument("--lm_coef" , type=SCREAMING_SNAKE_CASE , default=0.9 ) parser.add_argument("--n_valid" , type=SCREAMING_SNAKE_CASE , default=3_7_4 ) parser.add_argument("--server_ip" , type=SCREAMING_SNAKE_CASE , default="" , help="Can be used for distant debugging." ) parser.add_argument("--server_port" , type=SCREAMING_SNAKE_CASE , default="" , help="Can be used for distant debugging." ) lowerCAmelCase : Tuple = parser.parse_args() print(SCREAMING_SNAKE_CASE ) 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=SCREAMING_SNAKE_CASE ) 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 ) lowerCAmelCase : Optional[int] = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) lowerCAmelCase : Optional[int] = torch.cuda.device_count() logger.info("device: {}, n_gpu {}".format(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) 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 lowerCAmelCase : str = ["_start_", "_delimiter_", "_classify_"] lowerCAmelCase : Dict = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(SCREAMING_SNAKE_CASE ) ) model.to(SCREAMING_SNAKE_CASE ) # Load and encode the datasets def tokenize_and_encode(SCREAMING_SNAKE_CASE : Optional[Any] ): if isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(SCREAMING_SNAKE_CASE ) ) elif isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return obj return [tokenize_and_encode(SCREAMING_SNAKE_CASE ) for o in obj] logger.info("Encoding dataset..." ) lowerCAmelCase : Optional[Any] = load_rocstories_dataset(args.train_dataset ) lowerCAmelCase : int = load_rocstories_dataset(args.eval_dataset ) lowerCAmelCase : Tuple = (train_dataset, eval_dataset) lowerCAmelCase : Dict = tokenize_and_encode(SCREAMING_SNAKE_CASE ) # Compute the max input length for the Transformer lowerCAmelCase : Any = model.config.n_positions // 2 - 2 lowerCAmelCase : int = 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 ) lowerCAmelCase : Union[str, Any] = min(SCREAMING_SNAKE_CASE , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders lowerCAmelCase : Any = pre_process_datasets(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , *SCREAMING_SNAKE_CASE ) lowerCAmelCase , lowerCAmelCase : Tuple = tensor_datasets[0], tensor_datasets[1] lowerCAmelCase : List[str] = TensorDataset(*SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = RandomSampler(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = DataLoader(SCREAMING_SNAKE_CASE , sampler=SCREAMING_SNAKE_CASE , batch_size=args.train_batch_size ) lowerCAmelCase : int = TensorDataset(*SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = SequentialSampler(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = DataLoader(SCREAMING_SNAKE_CASE , sampler=SCREAMING_SNAKE_CASE , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: lowerCAmelCase : int = args.max_steps lowerCAmelCase : str = args.max_steps // (len(SCREAMING_SNAKE_CASE ) // args.gradient_accumulation_steps) + 1 else: lowerCAmelCase : Union[str, Any] = len(SCREAMING_SNAKE_CASE ) // args.gradient_accumulation_steps * args.num_train_epochs lowerCAmelCase : Dict = list(model.named_parameters() ) lowerCAmelCase : str = ["bias", "LayerNorm.bias", "LayerNorm.weight"] lowerCAmelCase : Tuple = [ { "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}, ] lowerCAmelCase : Tuple = AdamW(SCREAMING_SNAKE_CASE , lr=args.learning_rate , eps=args.adam_epsilon ) lowerCAmelCase : str = get_linear_schedule_with_warmup( SCREAMING_SNAKE_CASE , num_warmup_steps=args.warmup_steps , num_training_steps=SCREAMING_SNAKE_CASE ) if args.do_train: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Dict = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="Epoch" ): lowerCAmelCase : Optional[int] = 0 lowerCAmelCase : Union[str, Any] = 0 lowerCAmelCase : Tuple = tqdm(SCREAMING_SNAKE_CASE , desc="Training" ) for step, batch in enumerate(SCREAMING_SNAKE_CASE ): lowerCAmelCase : Tuple = tuple(t.to(SCREAMING_SNAKE_CASE ) for t in batch ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : int = batch lowerCAmelCase : Optional[int] = model(SCREAMING_SNAKE_CASE , mc_token_ids=SCREAMING_SNAKE_CASE , lm_labels=SCREAMING_SNAKE_CASE , mc_labels=SCREAMING_SNAKE_CASE ) lowerCAmelCase : int = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() lowerCAmelCase : Optional[Any] = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 lowerCAmelCase : int = "Training loss: {:.2e} lr: {:.2e}".format(SCREAMING_SNAKE_CASE , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer lowerCAmelCase : Optional[int] = model.module if hasattr(SCREAMING_SNAKE_CASE , "module" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` lowerCAmelCase : Any = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE ) torch.save(model_to_save.state_dict() , SCREAMING_SNAKE_CASE ) model_to_save.config.to_json_file(SCREAMING_SNAKE_CASE ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned lowerCAmelCase : List[str] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) lowerCAmelCase : Dict = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(SCREAMING_SNAKE_CASE ) if args.do_eval: model.eval() lowerCAmelCase , lowerCAmelCase : Optional[int] = 0, 0 lowerCAmelCase , lowerCAmelCase : Any = 0, 0 for batch in tqdm(SCREAMING_SNAKE_CASE , desc="Evaluating" ): lowerCAmelCase : List[Any] = tuple(t.to(SCREAMING_SNAKE_CASE ) for t in batch ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Tuple = batch with torch.no_grad(): lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Optional[Any] = model( SCREAMING_SNAKE_CASE , mc_token_ids=SCREAMING_SNAKE_CASE , lm_labels=SCREAMING_SNAKE_CASE , mc_labels=SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = mc_logits.detach().cpu().numpy() lowerCAmelCase : List[str] = mc_labels.to("cpu" ).numpy() lowerCAmelCase : Any = accuracy(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 lowerCAmelCase : List[Any] = eval_loss / nb_eval_steps lowerCAmelCase : List[Any] = eval_accuracy / nb_eval_examples lowerCAmelCase : Tuple = tr_loss / nb_tr_steps if args.do_train else None lowerCAmelCase : Any = {"eval_loss": eval_loss, "eval_accuracy": eval_accuracy, "train_loss": train_loss} lowerCAmelCase : List[str] = os.path.join(args.output_dir , "eval_results.txt" ) with open(SCREAMING_SNAKE_CASE , "w" ) as writer: logger.info("***** Eval results *****" ) for key in sorted(result.keys() ): logger.info(" %s = %s" , SCREAMING_SNAKE_CASE , str(result[key] ) ) writer.write("%s = %s\n" % (key, str(result[key] )) ) if __name__ == "__main__": main()
681
0
"""simple docstring""" import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def a__ ( ) -> Optional[Any]: '''simple docstring''' with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(SCREAMING_SNAKE_CASE ): requests.request("GET" , "https://huggingface.co" ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request("GET" , "https://huggingface.co" , timeout=1.0 ) @pytest.mark.integration def a__ ( ) -> Optional[int]: '''simple docstring''' with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request("GET" , "https://huggingface.co" ) def a__ ( ) -> List[str]: '''simple docstring''' with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(SCREAMING_SNAKE_CASE ): http_head("https://huggingface.co" )
710
"""simple docstring""" from typing import List, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''huggingface/informer-tourism-monthly''': ( '''https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json''' ), # See all Informer models at https://huggingface.co/models?filter=informer } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Optional[Any] ="informer" a : int ={ "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", "num_hidden_layers": "encoder_layers", } def __init__( self , snake_case__ = None , snake_case__ = None , snake_case__ = "student_t" , snake_case__ = "nll" , snake_case__ = 1 , snake_case__ = None , snake_case__ = "mean" , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = None , snake_case__ = None , snake_case__ = 64 , snake_case__ = 32 , snake_case__ = 32 , snake_case__ = 2 , snake_case__ = 2 , snake_case__ = 2 , snake_case__ = 2 , snake_case__ = True , snake_case__ = "gelu" , snake_case__ = 0.05 , snake_case__ = 0.1 , snake_case__ = 0.1 , snake_case__ = 0.1 , snake_case__ = 0.1 , snake_case__ = 100 , snake_case__ = 0.02 , snake_case__=True , snake_case__ = "prob" , snake_case__ = 5 , snake_case__ = True , **snake_case__ , ): """simple docstring""" lowerCAmelCase : Union[str, Any] = prediction_length lowerCAmelCase : Union[str, Any] = context_length or prediction_length lowerCAmelCase : List[Any] = distribution_output lowerCAmelCase : Optional[int] = loss lowerCAmelCase : Optional[int] = input_size lowerCAmelCase : str = num_time_features lowerCAmelCase : Any = lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7] lowerCAmelCase : Dict = scaling lowerCAmelCase : List[str] = num_dynamic_real_features lowerCAmelCase : Dict = num_static_real_features lowerCAmelCase : Dict = num_static_categorical_features # set cardinality if cardinality and num_static_categorical_features > 0: if len(snake_case__ ) != num_static_categorical_features: raise ValueError( "The cardinality should be a list of the same length as `num_static_categorical_features`" ) lowerCAmelCase : List[str] = cardinality else: lowerCAmelCase : Optional[Any] = [0] # set embedding_dimension if embedding_dimension and num_static_categorical_features > 0: if len(snake_case__ ) != num_static_categorical_features: raise ValueError( "The embedding dimension should be a list of the same length as `num_static_categorical_features`" ) lowerCAmelCase : List[Any] = embedding_dimension else: lowerCAmelCase : Dict = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] lowerCAmelCase : List[Any] = num_parallel_samples # Transformer architecture configuration lowerCAmelCase : Any = input_size * len(self.lags_sequence ) + self._number_of_features lowerCAmelCase : str = d_model lowerCAmelCase : List[str] = encoder_attention_heads lowerCAmelCase : int = decoder_attention_heads lowerCAmelCase : Optional[Any] = encoder_ffn_dim lowerCAmelCase : Dict = decoder_ffn_dim lowerCAmelCase : int = encoder_layers lowerCAmelCase : Union[str, Any] = decoder_layers lowerCAmelCase : Tuple = dropout lowerCAmelCase : List[Any] = attention_dropout lowerCAmelCase : int = activation_dropout lowerCAmelCase : Union[str, Any] = encoder_layerdrop lowerCAmelCase : int = decoder_layerdrop lowerCAmelCase : Optional[int] = activation_function lowerCAmelCase : int = init_std lowerCAmelCase : Optional[Any] = use_cache # Informer lowerCAmelCase : Dict = attention_type lowerCAmelCase : Any = sampling_factor lowerCAmelCase : Optional[int] = distil super().__init__(is_encoder_decoder=snake_case__ , **snake_case__ ) @property def lowercase__ ( self ): """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 )
681
0
"""simple docstring""" from __future__ import annotations import inspect import unittest import numpy as np from transformers import ResNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFResNetForImageClassification, TFResNetModel from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__=3 , snake_case__=32 , snake_case__=3 , snake_case__=10 , snake_case__=[10, 20, 30, 40] , snake_case__=[1, 1, 2, 1] , snake_case__=True , snake_case__=True , snake_case__="relu" , snake_case__=3 , snake_case__=None , ): """simple docstring""" lowerCAmelCase : Optional[Any] = parent lowerCAmelCase : List[Any] = batch_size lowerCAmelCase : Union[str, Any] = image_size lowerCAmelCase : Dict = num_channels lowerCAmelCase : List[Any] = embeddings_size lowerCAmelCase : List[Any] = hidden_sizes lowerCAmelCase : Optional[int] = depths lowerCAmelCase : str = is_training lowerCAmelCase : List[str] = use_labels lowerCAmelCase : List[Any] = hidden_act lowerCAmelCase : Optional[Any] = num_labels lowerCAmelCase : Tuple = scope lowerCAmelCase : int = len(snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase : Optional[Any] = None if self.use_labels: lowerCAmelCase : Optional[int] = ids_tensor([self.batch_size] , self.num_labels ) lowerCAmelCase : List[str] = self.get_config() return config, pixel_values, labels def lowercase__ ( self ): """simple docstring""" return ResNetConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , image_size=self.image_size , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Tuple = TFResNetModel(config=snake_case__ ) lowerCAmelCase : Union[str, Any] = model(snake_case__ ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = self.num_labels lowerCAmelCase : str = TFResNetForImageClassification(snake_case__ ) lowerCAmelCase : int = model(snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = self.prepare_config_and_inputs() lowerCAmelCase : Any = config_and_inputs lowerCAmelCase : Optional[Any] = {"pixel_values": pixel_values} return config, inputs_dict @require_tf class SCREAMING_SNAKE_CASE__ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" a : Any =(TFResNetModel, TFResNetForImageClassification) if is_tf_available() else () a : Tuple =( {"feature-extraction": TFResNetModel, "image-classification": TFResNetForImageClassification} if is_tf_available() else {} ) a : int =False a : List[str] =False a : Optional[int] =False a : Union[str, Any] =False a : Any =False def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = TFResNetModelTester(self ) lowerCAmelCase : str = ConfigTester(self , config_class=snake_case__ , has_text_modality=snake_case__ ) def lowercase__ ( self ): """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 lowercase__ ( self ): """simple docstring""" return @unittest.skip(reason="ResNet does not use inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip(reason="ResNet does not support input and output embeddings" ) def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : List[str] = model_class(snake_case__ ) lowerCAmelCase : Optional[Any] = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase : Dict = [*signature.parameters.keys()] lowerCAmelCase : List[Any] = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def lowercase__ ( self ): """simple docstring""" def check_hidden_states_output(snake_case__ , snake_case__ , snake_case__ ): lowerCAmelCase : int = model_class(snake_case__ ) lowerCAmelCase : Optional[Any] = model(**self._prepare_for_class(snake_case__ , snake_case__ ) ) lowerCAmelCase : List[str] = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states lowerCAmelCase : Tuple = self.model_tester.num_stages self.assertEqual(len(snake_case__ ) , expected_num_stages + 1 ) # ResNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) lowerCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase : Any = ["basic", "bottleneck"] for model_class in self.all_model_classes: for layer_type in layers_type: lowerCAmelCase : Optional[Any] = layer_type lowerCAmelCase : Dict = True check_hidden_states_output(snake_case__ , snake_case__ , snake_case__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowerCAmelCase : List[Any] = True check_hidden_states_output(snake_case__ , snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case__ ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase : int = TFResNetModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def a__ ( ): '''simple docstring''' lowerCAmelCase : str = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_tf @require_vision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" @cached_property def lowercase__ ( self ): """simple docstring""" return ( AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) lowerCAmelCase : Any = self.default_image_processor lowerCAmelCase : Optional[Any] = prepare_img() lowerCAmelCase : Dict = image_processor(images=snake_case__ , return_tensors="tf" ) # forward pass lowerCAmelCase : str = model(**snake_case__ ) # verify the logits lowerCAmelCase : str = tf.TensorShape((1, 1_000) ) self.assertEqual(outputs.logits.shape , snake_case__ ) lowerCAmelCase : str = tf.constant([-11.1069, -9.7877, -8.3777] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , snake_case__ , atol=1e-4 ) )
711
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' if num < 0: return False lowerCAmelCase : int = num lowerCAmelCase : int = 0 while num > 0: lowerCAmelCase : Dict = rev_num * 1_0 + (num % 1_0) num //= 1_0 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
681
0
import random from .binary_exp_mod import bin_exp_mod def a__ ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : int=1_0_0_0 ): '''simple docstring''' if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd lowerCAmelCase : int = n - 1 lowerCAmelCase : Optional[int] = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) lowerCAmelCase : Optional[Any] = 0 while count < prec: lowerCAmelCase : List[str] = random.randint(2 , n - 1 ) lowerCAmelCase : Tuple = bin_exp_mod(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if b != 1: lowerCAmelCase : List[str] = True for _ in range(SCREAMING_SNAKE_CASE ): if b == n - 1: lowerCAmelCase : List[str] = False break lowerCAmelCase : Optional[Any] = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": lowerCAmelCase__ = abs(int(input('''Enter bound : ''').strip())) print('''Here\'s the list of primes:''') print(''', '''.join(str(i) for i in range(n + 1) if is_prime_big(i)))
712
"""simple docstring""" import os import re import shutil import sys import tempfile import unittest import black lowerCAmelCase__ = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, '''utils''')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If DDPMSchedulerOutput is changed in scheduling_ddpm.py, this code needs to be manually updated. lowerCAmelCase__ = ''' \""" Output class for the scheduler\'s step function output. Args: prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the denoising loop. pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): The predicted denoised sample (x_{0}) based on the model output from the current timestep. `pred_original_sample` can be used to preview progress or for guidance. \""" prev_sample: torch.FloatTensor pred_original_sample: Optional[torch.FloatTensor] = None ''' class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = tempfile.mkdtemp() os.makedirs(os.path.join(self.diffusers_dir , "schedulers/" ) ) lowerCAmelCase : List[str] = self.diffusers_dir shutil.copy( os.path.join(snake_case__ , "src/diffusers/schedulers/scheduling_ddpm.py" ) , os.path.join(self.diffusers_dir , "schedulers/scheduling_ddpm.py" ) , ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = "src/diffusers" shutil.rmtree(self.diffusers_dir ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__=None ): """simple docstring""" lowerCAmelCase : Union[str, Any] = comment + f"""\nclass {class_name}(nn.Module):\n""" + class_code if overwrite_result is not None: lowerCAmelCase : str = comment + f"""\nclass {class_name}(nn.Module):\n""" + overwrite_result lowerCAmelCase : int = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) lowerCAmelCase : int = black.format_str(snake_case__ , mode=snake_case__ ) lowerCAmelCase : Dict = os.path.join(self.diffusers_dir , "new_code.py" ) with open(snake_case__ , "w" , newline="\n" ) as f: f.write(snake_case__ ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(snake_case__ ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=snake_case__ ) with open(snake_case__ , "r" ) as f: self.assertTrue(f.read() , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = check_copies.find_code_in_diffusers("schedulers.scheduling_ddpm.DDPMSchedulerOutput" ) self.assertEqual(snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput" , "DDPMSchedulerOutput" , REFERENCE_CODE + "\n" , ) # With no empty line at the end self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput" , "DDPMSchedulerOutput" , snake_case__ , ) # Copy consistency with rename self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test" , "TestSchedulerOutput" , re.sub("DDPM" , "Test" , snake_case__ ) , ) # Copy consistency with a really long name lowerCAmelCase : Union[str, Any] = "TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason" self.check_copy_consistency( f"""# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}""" , f"""{long_class_name}SchedulerOutput""" , re.sub("Bert" , snake_case__ , snake_case__ ) , ) # Copy consistency with overwrite self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test" , "TestSchedulerOutput" , snake_case__ , overwrite_result=re.sub("DDPM" , "Test" , snake_case__ ) , )
681
0
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_pegasus import PegasusTokenizer else: lowerCAmelCase__ = None lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = '''▁''' lowerCAmelCase__ = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} lowerCAmelCase__ = { '''vocab_file''': {'''google/pegasus-xsum''': '''https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model'''}, '''tokenizer_file''': { '''google/pegasus-xsum''': '''https://huggingface.co/google/pegasus-xsum/resolve/main/tokenizer.json''' }, } lowerCAmelCase__ = { '''google/pegasus-xsum''': 512, } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Optional[Any] =VOCAB_FILES_NAMES a : Union[str, Any] =PRETRAINED_VOCAB_FILES_MAP a : List[str] =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a : Tuple =PegasusTokenizer a : Any =["input_ids", "attention_mask"] def __init__( self , snake_case__=None , snake_case__=None , snake_case__="<pad>" , snake_case__="</s>" , snake_case__="<unk>" , snake_case__="<mask_2>" , snake_case__="<mask_1>" , snake_case__=None , snake_case__=103 , **snake_case__ , ): """simple docstring""" lowerCAmelCase : Dict = offset if additional_special_tokens is not None: if not isinstance(snake_case__ , snake_case__ ): raise TypeError( f"""additional_special_tokens should be of type {type(snake_case__ )}, but is""" f""" {type(snake_case__ )}""" ) lowerCAmelCase : Optional[Any] = ( ([mask_token_sent] + additional_special_tokens) if mask_token_sent not in additional_special_tokens and mask_token_sent is not None else additional_special_tokens ) # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken additional_special_tokens_extended += [ f"""<unk_{i}>""" for i in range(len(snake_case__ ) , self.offset - 1 ) ] if len(set(snake_case__ ) ) != len(snake_case__ ): raise ValueError( "Please make sure that the provided additional_special_tokens do not contain an incorrectly" f""" shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}.""" ) lowerCAmelCase : Tuple = additional_special_tokens_extended else: lowerCAmelCase : Optional[int] = [mask_token_sent] if mask_token_sent is not None else [] additional_special_tokens += [f"""<unk_{i}>""" for i in range(2 , self.offset )] super().__init__( snake_case__ , tokenizer_file=snake_case__ , pad_token=snake_case__ , eos_token=snake_case__ , unk_token=snake_case__ , mask_token=snake_case__ , mask_token_sent=snake_case__ , offset=snake_case__ , additional_special_tokens=snake_case__ , **snake_case__ , ) lowerCAmelCase : Any = vocab_file lowerCAmelCase : Optional[int] = False if not self.vocab_file else True def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : List[Any] = set(self.all_special_ids ) # call it once instead of inside list comp all_special_ids.remove(self.unk_token_id ) # <unk> is only sometimes special if all_special_ids != set(range(len(self.additional_special_tokens ) + 3 ) ): raise ValueError( "There should be 3 special tokens: mask_token, pad_token, and eos_token +" f""" {len(self.additional_special_tokens )} additional_special_tokens, but got {all_special_ids}""" ) return [1 if x in all_special_ids else 0 for x in seq] def lowercase__ ( self , snake_case__ , snake_case__ = None , snake_case__ = False ): """simple docstring""" if already_has_special_tokens: return self._special_token_mask(snake_case__ ) elif token_ids_a is None: return self._special_token_mask(snake_case__ ) + [1] else: return self._special_token_mask(token_ids_a + token_ids_a ) + [1] def lowercase__ ( self , snake_case__ , snake_case__=None ): """simple docstring""" if token_ids_a is None: return token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_a + token_ids_a + [self.eos_token_id] def lowercase__ ( self , snake_case__ , snake_case__ = None ): """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " "tokenizer." ) if not os.path.isdir(snake_case__ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return lowerCAmelCase : Optional[int] = os.path.join( snake_case__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case__ ): copyfile(self.vocab_file , snake_case__ ) return (out_vocab_file,)
713
"""simple docstring""" import re from flax.core.frozen_dict import freeze from flax.traverse_util import flatten_dict, unflatten_dict from jax.experimental import PartitionSpec as P # Sentinels lowerCAmelCase__ = object() # For specifying empty leaf dict `{}` lowerCAmelCase__ = object() def a__ ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' lowerCAmelCase : Optional[int] = tuple((re.compile(x + "$" ) for x in qs) ) for i in range(len(SCREAMING_SNAKE_CASE ) - len(SCREAMING_SNAKE_CASE ) + 1 ): lowerCAmelCase : int = [x.match(SCREAMING_SNAKE_CASE ) for x, y in zip(SCREAMING_SNAKE_CASE , ks[i:] )] if matches and all(SCREAMING_SNAKE_CASE ): return True return False def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' def replace(SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Dict ): for rule, replacement in rules: if _match(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return replacement return val return replace def a__ ( ): '''simple docstring''' return [ # embeddings (("transformer", "wpe", "embedding"), P("mp" , SCREAMING_SNAKE_CASE )), (("transformer", "wte", "embedding"), P("mp" , SCREAMING_SNAKE_CASE )), # atention (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(SCREAMING_SNAKE_CASE , "mp" )), (("attention", "out_proj", "kernel"), P("mp" , SCREAMING_SNAKE_CASE )), (("attention", "out_proj", "bias"), None), # mlp (("mlp", "c_fc", "kernel"), P(SCREAMING_SNAKE_CASE , "mp" )), (("mlp", "c_fc", "bias"), P("mp" )), (("mlp", "c_proj", "kernel"), P("mp" , SCREAMING_SNAKE_CASE )), (("mlp", "c_proj", "bias"), None), # layer norms ((r"ln_\d+", "bias"), None), ((r"\d+", r"ln_\d+", "scale"), None), (("ln_f", "bias"), None), (("ln_f", "scale"), None), ] def a__ ( SCREAMING_SNAKE_CASE : Union[str, Any] ): '''simple docstring''' lowerCAmelCase : Any = _get_partition_rules() lowerCAmelCase : Tuple = _replacement_rules(SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = {k: _unmatched for k in flatten_dict(SCREAMING_SNAKE_CASE )} lowerCAmelCase : List[Any] = {k: replace(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) for k, v in initd.items()} assert _unmatched not in result.values(), "Incomplete partition spec." return freeze(unflatten_dict(SCREAMING_SNAKE_CASE ) )
681
0
"""simple docstring""" from functools import reduce lowerCAmelCase__ = ( '''73167176531330624919225119674426574742355349194934''' '''96983520312774506326239578318016984801869478851843''' '''85861560789112949495459501737958331952853208805511''' '''12540698747158523863050715693290963295227443043557''' '''66896648950445244523161731856403098711121722383113''' '''62229893423380308135336276614282806444486645238749''' '''30358907296290491560440772390713810515859307960866''' '''70172427121883998797908792274921901699720888093776''' '''65727333001053367881220235421809751254540594752243''' '''52584907711670556013604839586446706324415722155397''' '''53697817977846174064955149290862569321978468622482''' '''83972241375657056057490261407972968652414535100474''' '''82166370484403199890008895243450658541227588666881''' '''16427171479924442928230863465674813919123162824586''' '''17866458359124566529476545682848912883142607690042''' '''24219022671055626321111109370544217506941658960408''' '''07198403850962455444362981230987879927244284909188''' '''84580156166097919133875499200524063689912560717606''' '''05886116467109405077541002256983155200055935729725''' '''71636269561882670428252483600823257530420752963450''' ) def a__ ( SCREAMING_SNAKE_CASE : str = N ): '''simple docstring''' return max( # mypy cannot properly interpret reduce int(reduce(lambda SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : str(int(SCREAMING_SNAKE_CASE ) * int(SCREAMING_SNAKE_CASE ) ) , n[i : i + 1_3] ) ) for i in range(len(SCREAMING_SNAKE_CASE ) - 1_2 ) ) if __name__ == "__main__": print(F"{solution() = }")
714
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : int = 1_0 , SCREAMING_SNAKE_CASE : int = 2_2 ): '''simple docstring''' lowerCAmelCase : Dict = range(1 , SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = range(1 , SCREAMING_SNAKE_CASE ) return sum( 1 for power in powers for base in bases if len(str(base**power ) ) == power ) if __name__ == "__main__": print(F"{solution(10, 22) = }")
681
0
"""simple docstring""" from torch import nn class SCREAMING_SNAKE_CASE__ ( nn.Module ): """simple docstring""" def __init__( self , snake_case__ , snake_case__ ): """simple docstring""" super().__init__() lowerCAmelCase : str = class_size lowerCAmelCase : Any = embed_size # self.mlp1 = nn.Linear(embed_size, embed_size) # self.mlp2 = (nn.Linear(embed_size, class_size)) lowerCAmelCase : str = nn.Linear(snake_case__ , snake_case__ ) def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Optional[int] = self.mlp(snake_case__ ) return logits
715
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : Optional[int] = len(SCREAMING_SNAKE_CASE ) while cur > 1: # Find the maximum number in arr lowerCAmelCase : List[str] = arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi lowerCAmelCase : str = arr[mi::-1] + arr[mi + 1 : len(SCREAMING_SNAKE_CASE )] # Reverse whole list lowerCAmelCase : str = arr[cur - 1 :: -1] + arr[cur : len(SCREAMING_SNAKE_CASE )] cur -= 1 return arr if __name__ == "__main__": lowerCAmelCase__ = input('''Enter numbers separated by a comma:\n''').strip() lowerCAmelCase__ = [int(item) for item in user_input.split(''',''')] print(pancake_sort(unsorted))
681
0
"""simple docstring""" from __future__ import annotations import math def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : bool , SCREAMING_SNAKE_CASE : list[int] , SCREAMING_SNAKE_CASE : float ): '''simple docstring''' if depth < 0: raise ValueError("Depth cannot be less than 0" ) if len(SCREAMING_SNAKE_CASE ) == 0: raise ValueError("Scores cannot be empty" ) if depth == height: return scores[node_index] if is_max: return max( minimax(depth + 1 , node_index * 2 , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) , minimax(depth + 1 , node_index * 2 + 1 , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) , ) return min( minimax(depth + 1 , node_index * 2 , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) , minimax(depth + 1 , node_index * 2 + 1 , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) , ) def a__ ( ): '''simple docstring''' lowerCAmelCase : Optional[int] = [9_0, 2_3, 6, 3_3, 2_1, 6_5, 1_2_3, 3_4_4_2_3] lowerCAmelCase : Union[str, Any] = math.log(len(SCREAMING_SNAKE_CASE ) , 2 ) print("Optimal value : " , end="" ) print(minimax(0 , 0 , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
716
"""simple docstring""" from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
681
0
import argparse import pickle import numpy as np import torch from torch import nn from transformers import ReformerConfig, ReformerModelWithLMHead from transformers.utils import logging logging.set_verbosity_info() def a__ ( SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : Optional[int]=None ): '''simple docstring''' assert torch_layer.weight.shape == weight.shape, f"""{torch_layer} layer.weight does not match""" lowerCAmelCase : str = nn.Parameter(SCREAMING_SNAKE_CASE ) if bias is not None: assert torch_layer.bias.shape == bias.shape, f"""{torch_layer} layer.bias does not match""" lowerCAmelCase : List[str] = nn.Parameter(SCREAMING_SNAKE_CASE ) def a__ ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : str ): '''simple docstring''' lowerCAmelCase : int = np.asarray(weights[0] ) lowerCAmelCase : Any = np.asarray(weights[1] ) lowerCAmelCase : Dict = np.asarray(weights[2] ) set_param( torch_layer.self_attention.query_key , torch.tensor(SCREAMING_SNAKE_CASE ).transpose(1 , 2 ).contiguous().view(-1 , SCREAMING_SNAKE_CASE ) , ) set_param( torch_layer.self_attention.value , torch.tensor(SCREAMING_SNAKE_CASE ).transpose(1 , 2 ).contiguous().view(-1 , SCREAMING_SNAKE_CASE ) , ) set_param( torch_layer.output.dense , torch.tensor(SCREAMING_SNAKE_CASE ).view(-1 , SCREAMING_SNAKE_CASE ).contiguous().transpose(0 , 1 ) , ) def a__ ( SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : Optional[int] = np.asarray(weights[0] ) lowerCAmelCase : Tuple = np.asarray(weights[1] ) lowerCAmelCase : Union[str, Any] = np.asarray(weights[2] ) lowerCAmelCase : str = np.asarray(weights[3] ) set_param( torch_layer.self_attention.query , torch.tensor(SCREAMING_SNAKE_CASE ).transpose(1 , 2 ).contiguous().view(-1 , SCREAMING_SNAKE_CASE ) , ) set_param( torch_layer.self_attention.key , torch.tensor(SCREAMING_SNAKE_CASE ).transpose(1 , 2 ).contiguous().view(-1 , SCREAMING_SNAKE_CASE ) , ) set_param( torch_layer.self_attention.value , torch.tensor(SCREAMING_SNAKE_CASE ).transpose(1 , 2 ).contiguous().view(-1 , SCREAMING_SNAKE_CASE ) , ) set_param( torch_layer.output.dense , torch.tensor(SCREAMING_SNAKE_CASE ).view(-1 , SCREAMING_SNAKE_CASE ).contiguous().transpose(0 , 1 ) , ) def a__ ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : List[Any] ): '''simple docstring''' lowerCAmelCase : Any = weights[0][0][0] lowerCAmelCase : List[str] = np.asarray(layer_norm_a[0] ) lowerCAmelCase : Dict = np.asarray(layer_norm_a[1] ) set_param( torch_block.attention.layer_norm , torch.tensor(SCREAMING_SNAKE_CASE ) , torch.tensor(SCREAMING_SNAKE_CASE ) , ) # lsh weights + output lowerCAmelCase : Tuple = weights[0][1] if len(SCREAMING_SNAKE_CASE ) < 4: set_layer_weights_in_torch_lsh(SCREAMING_SNAKE_CASE , torch_block.attention , SCREAMING_SNAKE_CASE ) else: set_layer_weights_in_torch_local(SCREAMING_SNAKE_CASE , torch_block.attention , SCREAMING_SNAKE_CASE ) # intermediate weighs lowerCAmelCase : List[str] = weights[2][0][1][2] # Chunked Feed Forward if len(SCREAMING_SNAKE_CASE ) == 4: lowerCAmelCase : Optional[int] = intermediate_weights[2] # layernorm 2 lowerCAmelCase : int = np.asarray(intermediate_weights[0][0] ) lowerCAmelCase : List[str] = np.asarray(intermediate_weights[0][1] ) set_param( torch_block.feed_forward.layer_norm , torch.tensor(SCREAMING_SNAKE_CASE ) , torch.tensor(SCREAMING_SNAKE_CASE ) , ) # intermediate dense lowerCAmelCase : Dict = np.asarray(intermediate_weights[1][0] ) lowerCAmelCase : List[str] = np.asarray(intermediate_weights[1][1] ) set_param( torch_block.feed_forward.dense.dense , torch.tensor(SCREAMING_SNAKE_CASE ).transpose(0 , 1 ).contiguous() , torch.tensor(SCREAMING_SNAKE_CASE ) , ) # intermediate out lowerCAmelCase : int = np.asarray(intermediate_weights[4][0] ) lowerCAmelCase : Any = np.asarray(intermediate_weights[4][1] ) set_param( torch_block.feed_forward.output.dense , torch.tensor(SCREAMING_SNAKE_CASE ).transpose(0 , 1 ).contiguous() , torch.tensor(SCREAMING_SNAKE_CASE ) , ) def a__ ( SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' lowerCAmelCase : Optional[Any] = torch_model.reformer # word embeds lowerCAmelCase : List[Any] = np.asarray(weights[1] ) set_param( torch_model_reformer.embeddings.word_embeddings , torch.tensor(SCREAMING_SNAKE_CASE ) , ) if isinstance(weights[3] , SCREAMING_SNAKE_CASE ): lowerCAmelCase : Optional[Any] = torch_model_reformer.embeddings.position_embeddings for emb_idx in range(len(position_embeddings.weights ) ): lowerCAmelCase : List[str] = np.asarray(weights[3][emb_idx][0] ) assert ( position_embeddings.weights[emb_idx].shape == emb_weights.shape ), f"""{position_embeddings[emb_idx]} emb does not match""" lowerCAmelCase : List[Any] = nn.Parameter(torch.tensor(SCREAMING_SNAKE_CASE ) ) lowerCAmelCase : List[Any] = weights[5] assert len(torch_model_reformer.encoder.layers ) * 4 == len( SCREAMING_SNAKE_CASE ), "HF and trax model do not have the same number of layers" for layer_idx, layer in enumerate(torch_model_reformer.encoder.layers ): lowerCAmelCase : str = trax_layer_weights[4 * layer_idx : 4 * (layer_idx + 1)] set_block_weights_in_torch(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # output layer norm lowerCAmelCase : int = np.asarray(weights[7][0] ) lowerCAmelCase : Optional[Any] = np.asarray(weights[7][1] ) set_param( torch_model_reformer.encoder.layer_norm , torch.tensor(SCREAMING_SNAKE_CASE ) , torch.tensor(SCREAMING_SNAKE_CASE ) , ) # output embeddings lowerCAmelCase : str = np.asarray(weights[9][0] ) lowerCAmelCase : List[Any] = np.asarray(weights[9][1] ) set_param( torch_model.lm_head.decoder , torch.tensor(SCREAMING_SNAKE_CASE ).transpose(0 , 1 ).contiguous() , torch.tensor(SCREAMING_SNAKE_CASE ) , ) def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Tuple ): '''simple docstring''' lowerCAmelCase : Optional[int] = ReformerConfig.from_json_file(SCREAMING_SNAKE_CASE ) print(f"""Building PyTorch model from configuration: {config}""" ) lowerCAmelCase : Optional[int] = ReformerModelWithLMHead(SCREAMING_SNAKE_CASE ) with open(SCREAMING_SNAKE_CASE , "rb" ) as f: lowerCAmelCase : int = pickle.load(SCREAMING_SNAKE_CASE )["weights"] set_model_weights_in_torch(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , config.hidden_size ) # Save pytorch-model print(f"""Save PyTorch model to {pytorch_dump_path}""" ) torch.save(model.state_dict() , SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--trax_model_pkl_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained Reformer model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) lowerCAmelCase__ = parser.parse_args() convert_trax_checkpoint_to_pytorch(args.trax_model_pkl_path, args.config_file, args.pytorch_dump_path)
717
"""simple docstring""" import math def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' return math.sqrt(SCREAMING_SNAKE_CASE ) * math.sqrt(SCREAMING_SNAKE_CASE ) == num def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' lowerCAmelCase : Dict = 0 lowerCAmelCase : List[str] = n while left <= right: lowerCAmelCase : str = (left + right) // 2 if mid**2 == n: return True elif mid**2 > n: lowerCAmelCase : int = mid - 1 else: lowerCAmelCase : int = mid + 1 return False if __name__ == "__main__": import doctest doctest.testmod()
681
0
"""simple docstring""" def a__ ( ): '''simple docstring''' return 1 def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' return 0 if x < 0 else two_pence(x - 2 ) + one_pence() def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' return 0 if x < 0 else five_pence(x - 5 ) + two_pence(SCREAMING_SNAKE_CASE ) def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' return 0 if x < 0 else ten_pence(x - 1_0 ) + five_pence(SCREAMING_SNAKE_CASE ) def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' return 0 if x < 0 else twenty_pence(x - 2_0 ) + ten_pence(SCREAMING_SNAKE_CASE ) def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' return 0 if x < 0 else fifty_pence(x - 5_0 ) + twenty_pence(SCREAMING_SNAKE_CASE ) def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' return 0 if x < 0 else one_pound(x - 1_0_0 ) + fifty_pence(SCREAMING_SNAKE_CASE ) def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' return 0 if x < 0 else two_pound(x - 2_0_0 ) + one_pound(SCREAMING_SNAKE_CASE ) def a__ ( SCREAMING_SNAKE_CASE : int = 2_0_0 ): '''simple docstring''' return two_pound(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": print(solution(int(input().strip())))
718
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''google/vit-base-patch16-224''': '''https://huggingface.co/vit-base-patch16-224/resolve/main/config.json''', # See all ViT models at https://huggingface.co/models?filter=vit } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Union[str, Any] ="vit" def __init__( self , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3_072 , snake_case__="gelu" , snake_case__=0.0 , snake_case__=0.0 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__=224 , snake_case__=16 , snake_case__=3 , snake_case__=True , snake_case__=16 , **snake_case__ , ): """simple docstring""" super().__init__(**snake_case__ ) lowerCAmelCase : Optional[Any] = hidden_size lowerCAmelCase : List[Any] = num_hidden_layers lowerCAmelCase : List[Any] = num_attention_heads lowerCAmelCase : Union[str, Any] = intermediate_size lowerCAmelCase : Any = hidden_act lowerCAmelCase : Optional[Any] = hidden_dropout_prob lowerCAmelCase : Optional[Any] = attention_probs_dropout_prob lowerCAmelCase : List[str] = initializer_range lowerCAmelCase : Optional[Any] = layer_norm_eps lowerCAmelCase : Optional[int] = image_size lowerCAmelCase : Optional[Any] = patch_size lowerCAmelCase : Tuple = num_channels lowerCAmelCase : Optional[int] = qkv_bias lowerCAmelCase : str = encoder_stride class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : List[Any] =version.parse("1.11" ) @property def lowercase__ ( self ): """simple docstring""" return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowercase__ ( self ): """simple docstring""" return 1e-4
681
0
"""simple docstring""" from __future__ import annotations from cmath import sqrt def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ): '''simple docstring''' if a == 0: raise ValueError("Coefficient 'a' must not be zero." ) lowerCAmelCase : Union[str, Any] = b * b - 4 * a * c lowerCAmelCase : Optional[Any] = (-b + sqrt(SCREAMING_SNAKE_CASE )) / (2 * a) lowerCAmelCase : List[str] = (-b - sqrt(SCREAMING_SNAKE_CASE )) / (2 * a) return ( root_a.real if not root_a.imag else root_a, root_a.real if not root_a.imag else root_a, ) def a__ ( ): '''simple docstring''' lowerCAmelCase : Dict = quadratic_roots(a=5 , b=6 , c=1 ) print(f"""The solutions are: {solutiona} and {solutiona}""" ) if __name__ == "__main__": main()
719
"""simple docstring""" from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels
681
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''google/pegasus-large''': '''https://huggingface.co/google/pegasus-large/resolve/main/config.json''', # See all PEGASUS models at https://huggingface.co/models?filter=pegasus } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Optional[Any] ="pegasus" a : str =["past_key_values"] a : Dict ={"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"} def __init__( self , snake_case__=50_265 , snake_case__=1_024 , snake_case__=12 , snake_case__=4_096 , snake_case__=16 , snake_case__=12 , snake_case__=4_096 , snake_case__=16 , snake_case__=0.0 , snake_case__=0.0 , snake_case__=True , snake_case__=True , snake_case__="gelu" , snake_case__=1_024 , snake_case__=0.1 , snake_case__=0.0 , snake_case__=0.0 , snake_case__=0.02 , snake_case__=0 , snake_case__=False , snake_case__=0 , snake_case__=1 , snake_case__=1 , **snake_case__ , ): """simple docstring""" lowerCAmelCase : Optional[Any] = vocab_size lowerCAmelCase : List[Any] = max_position_embeddings lowerCAmelCase : List[str] = d_model lowerCAmelCase : List[str] = encoder_ffn_dim lowerCAmelCase : int = encoder_layers lowerCAmelCase : Dict = encoder_attention_heads lowerCAmelCase : Any = decoder_ffn_dim lowerCAmelCase : Optional[int] = decoder_layers lowerCAmelCase : Dict = decoder_attention_heads lowerCAmelCase : Dict = dropout lowerCAmelCase : Optional[Any] = attention_dropout lowerCAmelCase : str = activation_dropout lowerCAmelCase : Tuple = activation_function lowerCAmelCase : Union[str, Any] = init_std lowerCAmelCase : Optional[int] = encoder_layerdrop lowerCAmelCase : List[Any] = decoder_layerdrop lowerCAmelCase : Optional[int] = use_cache lowerCAmelCase : int = encoder_layers lowerCAmelCase : Optional[Any] = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=snake_case__ , eos_token_id=snake_case__ , is_encoder_decoder=snake_case__ , decoder_start_token_id=snake_case__ , forced_eos_token_id=snake_case__ , **snake_case__ , ) @property def lowercase__ ( self ): """simple docstring""" return self.encoder_attention_heads @property def lowercase__ ( self ): """simple docstring""" return self.d_model
720
"""simple docstring""" import copy import inspect import unittest from transformers import AutoBackbone from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import require_timm, require_torch, torch_device from transformers.utils.import_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 if is_torch_available(): import torch from transformers import TimmBackbone, TimmBackboneConfig from ...test_pipeline_mixin import PipelineTesterMixin class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__="resnet50" , snake_case__=3 , snake_case__=32 , snake_case__=3 , snake_case__=True , snake_case__=True , ): """simple docstring""" lowerCAmelCase : List[str] = parent lowerCAmelCase : Union[str, Any] = out_indices if out_indices is not None else [4] lowerCAmelCase : Tuple = stage_names lowerCAmelCase : Any = out_features lowerCAmelCase : Any = backbone lowerCAmelCase : Union[str, Any] = batch_size lowerCAmelCase : Optional[int] = image_size lowerCAmelCase : List[str] = num_channels lowerCAmelCase : int = use_pretrained_backbone lowerCAmelCase : Tuple = is_training def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase : Optional[int] = self.get_config() return config, pixel_values def lowercase__ ( self ): """simple docstring""" return TimmBackboneConfig( image_size=self.image_size , num_channels=self.num_channels , out_features=self.out_features , out_indices=self.out_indices , stage_names=self.stage_names , use_pretrained_backbone=self.use_pretrained_backbone , backbone=self.backbone , ) def lowercase__ ( self , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : List[Any] = TimmBackbone(config=snake_case__ ) model.to(snake_case__ ) model.eval() with torch.no_grad(): lowerCAmelCase : Union[str, Any] = model(snake_case__ ) self.parent.assertEqual( result.feature_map[-1].shape , (self.batch_size, model.channels[-1], 14, 14) , ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = self.prepare_config_and_inputs() lowerCAmelCase , lowerCAmelCase : Tuple = config_and_inputs lowerCAmelCase : str = {"pixel_values": pixel_values} return config, inputs_dict @require_torch @require_timm class SCREAMING_SNAKE_CASE__ ( lowercase , lowercase , lowercase , unittest.TestCase ): """simple docstring""" a : Optional[int] =(TimmBackbone,) if is_torch_available() else () a : Union[str, Any] ={"feature-extraction": TimmBackbone} if is_torch_available() else {} a : Tuple =False a : List[Any] =False a : Optional[Any] =False a : Dict =False def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = TimmBackboneModelTester(self ) lowerCAmelCase : List[str] = ConfigTester(self , config_class=snake_case__ , has_text_modality=snake_case__ ) def lowercase__ ( self ): """simple docstring""" 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 lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = "resnet18" lowerCAmelCase : str = "microsoft/resnet-18" lowerCAmelCase : List[Any] = AutoBackbone.from_pretrained(snake_case__ , use_timm_backbone=snake_case__ ) lowerCAmelCase : List[str] = AutoBackbone.from_pretrained(snake_case__ ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(len(timm_model.stage_names ) , len(transformers_model.stage_names ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) # Out indices are set to the last layer by default. For timm models, we don't know # the number of layers in advance, so we set it to (-1,), whereas for transformers # models, we set it to [len(stage_names) - 1] (kept for backward compatibility). self.assertEqual(timm_model.out_indices , (-1,) ) self.assertEqual(transformers_model.out_indices , [len(timm_model.stage_names ) - 1] ) lowerCAmelCase : Union[str, Any] = AutoBackbone.from_pretrained(snake_case__ , use_timm_backbone=snake_case__ , out_indices=[1, 2, 3] ) lowerCAmelCase : List[Any] = AutoBackbone.from_pretrained(snake_case__ , out_indices=[1, 2, 3] ) self.assertEqual(timm_model.out_indices , transformers_model.out_indices ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) @unittest.skip("TimmBackbone doesn't support feed forward chunking" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone doesn't have num_hidden_layers attribute" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone initialization is managed on the timm side" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone models doesn't have inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone models doesn't have inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone model cannot be created without specifying a backbone checkpoint" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("model weights aren't tied in TimmBackbone." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("model weights aren't tied in TimmBackbone." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone doesn't have hidden size info in its configuration." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone doesn't support output_attentions." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Safetensors is not supported by timm." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : Dict = model_class(snake_case__ ) lowerCAmelCase : Any = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase : Optional[int] = [*signature.parameters.keys()] lowerCAmelCase : Any = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase : int = True lowerCAmelCase : str = self.has_attentions # no need to test all models as different heads yield the same functionality lowerCAmelCase : Optional[int] = self.all_model_classes[0] lowerCAmelCase : Union[str, Any] = model_class(snake_case__ ) model.to(snake_case__ ) lowerCAmelCase : Optional[int] = self._prepare_for_class(snake_case__ , snake_case__ ) lowerCAmelCase : Dict = model(**snake_case__ ) lowerCAmelCase : Tuple = outputs[0][-1] # Encoder-/Decoder-only models lowerCAmelCase : Optional[int] = outputs.hidden_states[0] hidden_states.retain_grad() if self.has_attentions: lowerCAmelCase : Union[str, Any] = outputs.attentions[0] attentions.retain_grad() output.flatten()[0].backward(retain_graph=snake_case__ ) self.assertIsNotNone(hidden_states.grad ) if self.has_attentions: self.assertIsNotNone(attentions.grad ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : Dict = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : List[str] = model(**snake_case__ ) self.assertEqual(len(result.feature_maps ) , len(config.out_indices ) ) self.assertEqual(len(model.channels ) , len(config.out_indices ) ) # Check output of last stage is taken if out_features=None, out_indices=None lowerCAmelCase : Dict = copy.deepcopy(snake_case__ ) lowerCAmelCase : Optional[int] = None lowerCAmelCase : Dict = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : Optional[int] = model(**snake_case__ ) self.assertEqual(len(result.feature_maps ) , 1 ) self.assertEqual(len(model.channels ) , 1 ) # Check backbone can be initialized with fresh weights lowerCAmelCase : Optional[int] = copy.deepcopy(snake_case__ ) lowerCAmelCase : List[str] = False lowerCAmelCase : int = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : Optional[Any] = model(**snake_case__ )
681
0
"""simple docstring""" from __future__ import annotations def a__ ( SCREAMING_SNAKE_CASE : int | str ): '''simple docstring''' lowerCAmelCase : Optional[Any] = str(SCREAMING_SNAKE_CASE ) return n == n[::-1] def a__ ( SCREAMING_SNAKE_CASE : int = 1_0_0_0_0_0_0 ): '''simple docstring''' lowerCAmelCase : Any = 0 for i in range(1 , SCREAMING_SNAKE_CASE ): if is_palindrome(SCREAMING_SNAKE_CASE ) and is_palindrome(bin(SCREAMING_SNAKE_CASE ).split("b" )[1] ): total += i return total if __name__ == "__main__": print(solution(int(str(input().strip()))))
721
"""simple docstring""" import argparse import os import re lowerCAmelCase__ = '''src/transformers''' # Pattern that looks at the indentation in a line. lowerCAmelCase__ = re.compile(r'''^(\s*)\S''') # Pattern that matches `"key":" and puts `key` in group 0. lowerCAmelCase__ = re.compile(r'''^\s*"([^"]+)":''') # Pattern that matches `_import_structure["key"]` and puts `key` in group 0. lowerCAmelCase__ = re.compile(r'''^\s*_import_structure\["([^"]+)"\]''') # Pattern that matches `"key",` and puts `key` in group 0. lowerCAmelCase__ = re.compile(r'''^\s*"([^"]+)",\s*$''') # Pattern that matches any `[stuff]` and puts `stuff` in group 0. lowerCAmelCase__ = re.compile(r'''\[([^\]]+)\]''') def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = _re_indent.search(SCREAMING_SNAKE_CASE ) return "" if search is None else search.groups()[0] def a__ ( SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Optional[int]="" , SCREAMING_SNAKE_CASE : Optional[int]=None , SCREAMING_SNAKE_CASE : Dict=None ): '''simple docstring''' lowerCAmelCase : Tuple = 0 lowerCAmelCase : Optional[int] = code.split("\n" ) if start_prompt is not None: while not lines[index].startswith(SCREAMING_SNAKE_CASE ): index += 1 lowerCAmelCase : Dict = ["\n".join(lines[:index] )] else: lowerCAmelCase : Any = [] # We split into blocks until we get to the `end_prompt` (or the end of the block). lowerCAmelCase : Union[str, Any] = [lines[index]] index += 1 while index < len(SCREAMING_SNAKE_CASE ) and (end_prompt is None or not lines[index].startswith(SCREAMING_SNAKE_CASE )): if len(lines[index] ) > 0 and get_indent(lines[index] ) == indent_level: if len(SCREAMING_SNAKE_CASE ) > 0 and get_indent(current_block[-1] ).startswith(indent_level + " " ): current_block.append(lines[index] ) blocks.append("\n".join(SCREAMING_SNAKE_CASE ) ) if index < len(SCREAMING_SNAKE_CASE ) - 1: lowerCAmelCase : List[str] = [lines[index + 1]] index += 1 else: lowerCAmelCase : Optional[Any] = [] else: blocks.append("\n".join(SCREAMING_SNAKE_CASE ) ) lowerCAmelCase : str = [lines[index]] else: current_block.append(lines[index] ) index += 1 # Adds current block if it's nonempty. if len(SCREAMING_SNAKE_CASE ) > 0: blocks.append("\n".join(SCREAMING_SNAKE_CASE ) ) # Add final block after end_prompt if provided. if end_prompt is not None and index < len(SCREAMING_SNAKE_CASE ): blocks.append("\n".join(lines[index:] ) ) return blocks def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' def _inner(SCREAMING_SNAKE_CASE : Optional[Any] ): return key(SCREAMING_SNAKE_CASE ).lower().replace("_" , "" ) return _inner def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Dict=None ): '''simple docstring''' def noop(SCREAMING_SNAKE_CASE : List[Any] ): return x if key is None: lowerCAmelCase : int = noop # Constants are all uppercase, they go first. lowerCAmelCase : Dict = [obj for obj in objects if key(SCREAMING_SNAKE_CASE ).isupper()] # Classes are not all uppercase but start with a capital, they go second. lowerCAmelCase : List[Any] = [obj for obj in objects if key(SCREAMING_SNAKE_CASE )[0].isupper() and not key(SCREAMING_SNAKE_CASE ).isupper()] # Functions begin with a lowercase, they go last. lowerCAmelCase : List[Any] = [obj for obj in objects if not key(SCREAMING_SNAKE_CASE )[0].isupper()] lowerCAmelCase : Dict = ignore_underscore(SCREAMING_SNAKE_CASE ) return sorted(SCREAMING_SNAKE_CASE , key=SCREAMING_SNAKE_CASE ) + sorted(SCREAMING_SNAKE_CASE , key=SCREAMING_SNAKE_CASE ) + sorted(SCREAMING_SNAKE_CASE , key=SCREAMING_SNAKE_CASE ) def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' def _replace(SCREAMING_SNAKE_CASE : List[Any] ): lowerCAmelCase : List[str] = match.groups()[0] if "," not in imports: return f"""[{imports}]""" lowerCAmelCase : Dict = [part.strip().replace("\"" , "" ) for part in imports.split("," )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: lowerCAmelCase : Any = keys[:-1] return "[" + ", ".join([f"""\"{k}\"""" for k in sort_objects(SCREAMING_SNAKE_CASE )] ) + "]" lowerCAmelCase : List[Any] = import_statement.split("\n" ) if len(SCREAMING_SNAKE_CASE ) > 3: # Here we have to sort internal imports that are on several lines (one per name): # key: [ # "object1", # "object2", # ... # ] # We may have to ignore one or two lines on each side. lowerCAmelCase : Tuple = 2 if lines[1].strip() == "[" else 1 lowerCAmelCase : Optional[Any] = [(i, _re_strip_line.search(SCREAMING_SNAKE_CASE ).groups()[0]) for i, line in enumerate(lines[idx:-idx] )] lowerCAmelCase : Optional[Any] = sort_objects(SCREAMING_SNAKE_CASE , key=lambda SCREAMING_SNAKE_CASE : x[1] ) lowerCAmelCase : List[str] = [lines[x[0] + idx] for x in sorted_indices] return "\n".join(lines[:idx] + sorted_lines + lines[-idx:] ) elif len(SCREAMING_SNAKE_CASE ) == 3: # Here we have to sort internal imports that are on one separate line: # key: [ # "object1", "object2", ... # ] if _re_bracket_content.search(lines[1] ) is not None: lowerCAmelCase : Optional[int] = _re_bracket_content.sub(_replace , lines[1] ) else: lowerCAmelCase : List[str] = [part.strip().replace("\"" , "" ) for part in lines[1].split("," )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: lowerCAmelCase : Union[str, Any] = keys[:-1] lowerCAmelCase : str = get_indent(lines[1] ) + ", ".join([f"""\"{k}\"""" for k in sort_objects(SCREAMING_SNAKE_CASE )] ) return "\n".join(SCREAMING_SNAKE_CASE ) else: # Finally we have to deal with imports fitting on one line lowerCAmelCase : Any = _re_bracket_content.sub(_replace , SCREAMING_SNAKE_CASE ) return import_statement def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Tuple=True ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE , encoding="utf-8" ) as f: lowerCAmelCase : Union[str, Any] = f.read() if "_import_structure" not in code: return # Blocks of indent level 0 lowerCAmelCase : List[str] = split_code_in_indented_blocks( SCREAMING_SNAKE_CASE , start_prompt="_import_structure = {" , end_prompt="if TYPE_CHECKING:" ) # We ignore block 0 (everything untils start_prompt) and the last block (everything after end_prompt). for block_idx in range(1 , len(SCREAMING_SNAKE_CASE ) - 1 ): # Check if the block contains some `_import_structure`s thingy to sort. lowerCAmelCase : Tuple = main_blocks[block_idx] lowerCAmelCase : Optional[Any] = block.split("\n" ) # Get to the start of the imports. lowerCAmelCase : int = 0 while line_idx < len(SCREAMING_SNAKE_CASE ) and "_import_structure" not in block_lines[line_idx]: # Skip dummy import blocks if "import dummy" in block_lines[line_idx]: lowerCAmelCase : Optional[Any] = len(SCREAMING_SNAKE_CASE ) else: line_idx += 1 if line_idx >= len(SCREAMING_SNAKE_CASE ): continue # Ignore beginning and last line: they don't contain anything. lowerCAmelCase : Optional[Any] = "\n".join(block_lines[line_idx:-1] ) lowerCAmelCase : Dict = get_indent(block_lines[1] ) # Slit the internal block into blocks of indent level 1. lowerCAmelCase : Optional[Any] = split_code_in_indented_blocks(SCREAMING_SNAKE_CASE , indent_level=SCREAMING_SNAKE_CASE ) # We have two categories of import key: list or _import_structure[key].append/extend lowerCAmelCase : Tuple = _re_direct_key if "_import_structure = {" in block_lines[0] else _re_indirect_key # Grab the keys, but there is a trap: some lines are empty or just comments. lowerCAmelCase : Tuple = [(pattern.search(SCREAMING_SNAKE_CASE ).groups()[0] if pattern.search(SCREAMING_SNAKE_CASE ) is not None else None) for b in internal_blocks] # We only sort the lines with a key. lowerCAmelCase : int = [(i, key) for i, key in enumerate(SCREAMING_SNAKE_CASE ) if key is not None] lowerCAmelCase : Union[str, Any] = [x[0] for x in sorted(SCREAMING_SNAKE_CASE , key=lambda SCREAMING_SNAKE_CASE : x[1] )] # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. lowerCAmelCase : Tuple = 0 lowerCAmelCase : Any = [] for i in range(len(SCREAMING_SNAKE_CASE ) ): if keys[i] is None: reorderded_blocks.append(internal_blocks[i] ) else: lowerCAmelCase : Dict = sort_objects_in_import(internal_blocks[sorted_indices[count]] ) reorderded_blocks.append(SCREAMING_SNAKE_CASE ) count += 1 # And we put our main block back together with its first and last line. lowerCAmelCase : List[Any] = "\n".join(block_lines[:line_idx] + reorderded_blocks + [block_lines[-1]] ) if code != "\n".join(SCREAMING_SNAKE_CASE ): if check_only: return True else: print(f"""Overwriting {file}.""" ) with open(SCREAMING_SNAKE_CASE , "w" , encoding="utf-8" ) as f: f.write("\n".join(SCREAMING_SNAKE_CASE ) ) def a__ ( SCREAMING_SNAKE_CASE : List[str]=True ): '''simple docstring''' lowerCAmelCase : Optional[int] = [] for root, _, files in os.walk(SCREAMING_SNAKE_CASE ): if "__init__.py" in files: lowerCAmelCase : Tuple = sort_imports(os.path.join(SCREAMING_SNAKE_CASE , "__init__.py" ) , check_only=SCREAMING_SNAKE_CASE ) if result: lowerCAmelCase : Optional[Any] = [os.path.join(SCREAMING_SNAKE_CASE , "__init__.py" )] if len(SCREAMING_SNAKE_CASE ) > 0: raise ValueError(f"""Would overwrite {len(SCREAMING_SNAKE_CASE )} files, run `make style`.""" ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument('''--check_only''', action='''store_true''', help='''Whether to only check or fix style.''') lowerCAmelCase__ = parser.parse_args() sort_imports_in_all_inits(check_only=args.check_only)
681
0
"""simple docstring""" import warnings from ...utils import logging from .image_processing_deit import DeiTImageProcessor lowerCAmelCase__ = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" def __init__( self , *snake_case__ , **snake_case__ ): """simple docstring""" warnings.warn( "The class DeiTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use DeiTImageProcessor instead." , snake_case__ , ) super().__init__(*snake_case__ , **snake_case__ )
700
"""simple docstring""" from __future__ import annotations import inspect import unittest import numpy as np from transformers import ResNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFResNetForImageClassification, TFResNetModel from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__=3 , snake_case__=32 , snake_case__=3 , snake_case__=10 , snake_case__=[10, 20, 30, 40] , snake_case__=[1, 1, 2, 1] , snake_case__=True , snake_case__=True , snake_case__="relu" , snake_case__=3 , snake_case__=None , ): """simple docstring""" lowerCAmelCase : Optional[Any] = parent lowerCAmelCase : List[Any] = batch_size lowerCAmelCase : Union[str, Any] = image_size lowerCAmelCase : Dict = num_channels lowerCAmelCase : List[Any] = embeddings_size lowerCAmelCase : List[Any] = hidden_sizes lowerCAmelCase : Optional[int] = depths lowerCAmelCase : str = is_training lowerCAmelCase : List[str] = use_labels lowerCAmelCase : List[Any] = hidden_act lowerCAmelCase : Optional[Any] = num_labels lowerCAmelCase : Tuple = scope lowerCAmelCase : int = len(snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase : Optional[Any] = None if self.use_labels: lowerCAmelCase : Optional[int] = ids_tensor([self.batch_size] , self.num_labels ) lowerCAmelCase : List[str] = self.get_config() return config, pixel_values, labels def lowercase__ ( self ): """simple docstring""" return ResNetConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , image_size=self.image_size , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Tuple = TFResNetModel(config=snake_case__ ) lowerCAmelCase : Union[str, Any] = model(snake_case__ ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = self.num_labels lowerCAmelCase : str = TFResNetForImageClassification(snake_case__ ) lowerCAmelCase : int = model(snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = self.prepare_config_and_inputs() lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Any = config_and_inputs lowerCAmelCase : Optional[Any] = {"pixel_values": pixel_values} return config, inputs_dict @require_tf class SCREAMING_SNAKE_CASE__ ( lowercase , lowercase , unittest.TestCase ): """simple docstring""" a : Any =(TFResNetModel, TFResNetForImageClassification) if is_tf_available() else () a : Tuple =( {"feature-extraction": TFResNetModel, "image-classification": TFResNetForImageClassification} if is_tf_available() else {} ) a : int =False a : List[str] =False a : Optional[int] =False a : Union[str, Any] =False a : Any =False def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = TFResNetModelTester(self ) lowerCAmelCase : str = ConfigTester(self , config_class=snake_case__ , has_text_modality=snake_case__ ) def lowercase__ ( self ): """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 lowercase__ ( self ): """simple docstring""" return @unittest.skip(reason="ResNet does not use inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip(reason="ResNet does not support input and output embeddings" ) def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : List[str] = model_class(snake_case__ ) lowerCAmelCase : Optional[Any] = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase : Dict = [*signature.parameters.keys()] lowerCAmelCase : List[Any] = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def lowercase__ ( self ): """simple docstring""" def check_hidden_states_output(snake_case__ , snake_case__ , snake_case__ ): lowerCAmelCase : int = model_class(snake_case__ ) lowerCAmelCase : Optional[Any] = model(**self._prepare_for_class(snake_case__ , snake_case__ ) ) lowerCAmelCase : List[str] = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states lowerCAmelCase : Tuple = self.model_tester.num_stages self.assertEqual(len(snake_case__ ) , expected_num_stages + 1 ) # ResNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) lowerCAmelCase , lowerCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase : Any = ["basic", "bottleneck"] for model_class in self.all_model_classes: for layer_type in layers_type: lowerCAmelCase : Optional[Any] = layer_type lowerCAmelCase : Dict = True check_hidden_states_output(snake_case__ , snake_case__ , snake_case__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowerCAmelCase : List[Any] = True check_hidden_states_output(snake_case__ , snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case__ ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase : int = TFResNetModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def a__ ( ): '''simple docstring''' lowerCAmelCase : str = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_tf @require_vision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" @cached_property def lowercase__ ( self ): """simple docstring""" return ( AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) lowerCAmelCase : Any = self.default_image_processor lowerCAmelCase : Optional[Any] = prepare_img() lowerCAmelCase : Dict = image_processor(images=snake_case__ , return_tensors="tf" ) # forward pass lowerCAmelCase : str = model(**snake_case__ ) # verify the logits lowerCAmelCase : str = tf.TensorShape((1, 1_000) ) self.assertEqual(outputs.logits.shape , snake_case__ ) lowerCAmelCase : str = tf.constant([-11.1069, -9.7877, -8.3777] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , snake_case__ , atol=1e-4 ) )
681
0
"""simple docstring""" import math def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : List[Any] ): '''simple docstring''' if 0 not in (x, y): # We use the relation x^y = y*log10(x), where 10 is the base. return y * math.logaa(SCREAMING_SNAKE_CASE ) else: if x == 0: # 0 raised to any number is 0 return 0 elif y == 0: return 1 # any number raised to 0 is 1 raise AssertionError("This should never happen" ) if __name__ == "__main__": # Main function # Read two numbers from input and typecast them to int using map function. # Here x is the base and y is the power. lowerCAmelCase__ = '''Enter the base and the power separated by a comma: ''' lowerCAmelCase__ ,lowerCAmelCase__ = map(int, input(prompt).split(''',''')) lowerCAmelCase__ ,lowerCAmelCase__ = map(int, input(prompt).split(''',''')) # We find the log of each number, using the function res(), which takes two # arguments. lowerCAmelCase__ = res(xa, ya) lowerCAmelCase__ = res(xa, ya) # We check for the largest number if resa > resa: print('''Largest number is''', xa, '''^''', ya) elif resa > resa: print('''Largest number is''', xa, '''^''', ya) else: print('''Both are equal''')
701
"""simple docstring""" import random from .binary_exp_mod import bin_exp_mod def a__ ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : int=1_0_0_0 ): '''simple docstring''' if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd lowerCAmelCase : int = n - 1 lowerCAmelCase : Optional[int] = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) lowerCAmelCase : Optional[Any] = 0 while count < prec: lowerCAmelCase : List[str] = random.randint(2 , n - 1 ) lowerCAmelCase : Tuple = bin_exp_mod(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if b != 1: lowerCAmelCase : List[str] = True for _ in range(SCREAMING_SNAKE_CASE ): if b == n - 1: lowerCAmelCase : List[str] = False break lowerCAmelCase : Optional[Any] = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": lowerCAmelCase__ = abs(int(input('''Enter bound : ''').strip())) print('''Here\'s the list of primes:''') print(''', '''.join(str(i) for i in range(n + 1) if is_prime_big(i)))
681
0
"""simple docstring""" from __future__ import annotations import math class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ ): """simple docstring""" lowerCAmelCase : int = size # approximate the overall size of segment tree with given value lowerCAmelCase : Optional[Any] = [0 for i in range(0 , 4 * size )] # create array to store lazy update lowerCAmelCase : Tuple = [0 for i in range(0 , 4 * size )] lowerCAmelCase : List[Any] = [0 for i in range(0 , 4 * size )] # flag for lazy update def lowercase__ ( self , snake_case__ ): """simple docstring""" return idx * 2 def lowercase__ ( self , snake_case__ ): """simple docstring""" return idx * 2 + 1 def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" if left_element == right_element: lowerCAmelCase : Tuple = a[left_element - 1] else: lowerCAmelCase : Any = (left_element + right_element) // 2 self.build(self.left(snake_case__ ) , snake_case__ , snake_case__ , snake_case__ ) self.build(self.right(snake_case__ ) , mid + 1 , snake_case__ , snake_case__ ) lowerCAmelCase : Union[str, Any] = max( self.segment_tree[self.left(snake_case__ )] , self.segment_tree[self.right(snake_case__ )] ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" if self.flag[idx] is True: lowerCAmelCase : Any = self.lazy[idx] lowerCAmelCase : List[Any] = False if left_element != right_element: lowerCAmelCase : Dict = self.lazy[idx] lowerCAmelCase : Tuple = self.lazy[idx] lowerCAmelCase : int = True lowerCAmelCase : Any = True if right_element < a or left_element > b: return True if left_element >= a and right_element <= b: lowerCAmelCase : List[str] = val if left_element != right_element: lowerCAmelCase : Union[str, Any] = val lowerCAmelCase : Dict = val lowerCAmelCase : Optional[Any] = True lowerCAmelCase : Optional[Any] = True return True lowerCAmelCase : List[str] = (left_element + right_element) // 2 self.update(self.left(snake_case__ ) , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) self.update(self.right(snake_case__ ) , mid + 1 , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) lowerCAmelCase : Optional[int] = max( self.segment_tree[self.left(snake_case__ )] , self.segment_tree[self.right(snake_case__ )] ) return True def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" if self.flag[idx] is True: lowerCAmelCase : Union[str, Any] = self.lazy[idx] lowerCAmelCase : Any = False if left_element != right_element: lowerCAmelCase : Union[str, Any] = self.lazy[idx] lowerCAmelCase : List[Any] = self.lazy[idx] lowerCAmelCase : Union[str, Any] = True lowerCAmelCase : int = True if right_element < a or left_element > b: return -math.inf if left_element >= a and right_element <= b: return self.segment_tree[idx] lowerCAmelCase : List[Any] = (left_element + right_element) // 2 lowerCAmelCase : Optional[int] = self.query(self.left(snake_case__ ) , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) lowerCAmelCase : Union[str, Any] = self.query(self.right(snake_case__ ) , mid + 1 , snake_case__ , snake_case__ , snake_case__ ) return max(snake_case__ , snake_case__ ) def __str__( self ): """simple docstring""" return str([self.query(1 , 1 , self.size , snake_case__ , snake_case__ ) for i in range(1 , self.size + 1 )] ) if __name__ == "__main__": lowerCAmelCase__ = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8] lowerCAmelCase__ = 15 lowerCAmelCase__ = SegmentTree(size) segt.build(1, 1, size, A) print(segt.query(1, 1, size, 4, 6)) print(segt.query(1, 1, size, 7, 11)) print(segt.query(1, 1, size, 7, 12)) segt.update(1, 1, size, 1, 3, 111) print(segt.query(1, 1, size, 1, 15)) segt.update(1, 1, size, 7, 8, 235) print(segt)
702
"""simple docstring""" # DISCLAIMER: This file is strongly influenced by https://github.com/ermongroup/ddim from dataclasses import dataclass from typing import Optional, Tuple, Union import flax import jax import jax.numpy as jnp from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils_flax import ( CommonSchedulerState, FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, add_noise_common, get_velocity_common, ) @flax.struct.dataclass class SCREAMING_SNAKE_CASE__ : """simple docstring""" a : CommonSchedulerState # setable values a : jnp.ndarray a : jnp.ndarray a : Optional[int] =None @classmethod def lowercase__ ( cls , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" return cls(common=snake_case__ , init_noise_sigma=snake_case__ , timesteps=snake_case__ ) @dataclass class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : DDPMSchedulerState class SCREAMING_SNAKE_CASE__ ( lowercase , lowercase ): """simple docstring""" a : Union[str, Any] =[e.name for e in FlaxKarrasDiffusionSchedulers] a : jnp.dtype @property def lowercase__ ( self ): """simple docstring""" return True @register_to_config def __init__( self , snake_case__ = 1_000 , snake_case__ = 0.0001 , snake_case__ = 0.02 , snake_case__ = "linear" , snake_case__ = None , snake_case__ = "fixed_small" , snake_case__ = True , snake_case__ = "epsilon" , snake_case__ = jnp.floataa , ): """simple docstring""" lowerCAmelCase : Any = dtype def lowercase__ ( self , snake_case__ = None ): """simple docstring""" if common is None: lowerCAmelCase : Dict = CommonSchedulerState.create(self ) # standard deviation of the initial noise distribution lowerCAmelCase : str = jnp.array(1.0 , dtype=self.dtype ) lowerCAmelCase : Any = jnp.arange(0 , self.config.num_train_timesteps ).round()[::-1] return DDPMSchedulerState.create( common=snake_case__ , init_noise_sigma=snake_case__ , timesteps=snake_case__ , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = None ): """simple docstring""" return sample def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = () ): """simple docstring""" lowerCAmelCase : List[Any] = self.config.num_train_timesteps // num_inference_steps # creates integer timesteps by multiplying by ratio # rounding to avoid issues when num_inference_step is power of 3 lowerCAmelCase : Any = (jnp.arange(0 , snake_case__ ) * step_ratio).round()[::-1] return state.replace( num_inference_steps=snake_case__ , timesteps=snake_case__ , ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__=None , snake_case__=None ): """simple docstring""" lowerCAmelCase : Optional[Any] = state.common.alphas_cumprod[t] lowerCAmelCase : List[str] = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) # For t > 0, compute predicted variance βt (see formula (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf) # and sample from it to get previous sample # x_{t-1} ~ N(pred_prev_sample, variance) == add variance to pred_sample lowerCAmelCase : Union[str, Any] = (1 - alpha_prod_t_prev) / (1 - alpha_prod_t) * state.common.betas[t] if variance_type is None: lowerCAmelCase : List[str] = self.config.variance_type # hacks - were probably added for training stability if variance_type == "fixed_small": lowerCAmelCase : List[Any] = jnp.clip(snake_case__ , a_min=1e-20 ) # for rl-diffuser https://arxiv.org/abs/2205.09991 elif variance_type == "fixed_small_log": lowerCAmelCase : List[str] = jnp.log(jnp.clip(snake_case__ , a_min=1e-20 ) ) elif variance_type == "fixed_large": lowerCAmelCase : Optional[int] = state.common.betas[t] elif variance_type == "fixed_large_log": # Glide max_log lowerCAmelCase : List[str] = jnp.log(state.common.betas[t] ) elif variance_type == "learned": return predicted_variance elif variance_type == "learned_range": lowerCAmelCase : List[str] = variance lowerCAmelCase : Dict = state.common.betas[t] lowerCAmelCase : Optional[Any] = (predicted_variance + 1) / 2 lowerCAmelCase : List[str] = frac * max_log + (1 - frac) * min_log return variance def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ = None , snake_case__ = True , ): """simple docstring""" lowerCAmelCase : Optional[Any] = timestep if key is None: lowerCAmelCase : Tuple = jax.random.PRNGKey(0 ) if model_output.shape[1] == sample.shape[1] * 2 and self.config.variance_type in ["learned", "learned_range"]: lowerCAmelCase , lowerCAmelCase : Optional[Any] = jnp.split(snake_case__ , sample.shape[1] , axis=1 ) else: lowerCAmelCase : Tuple = None # 1. compute alphas, betas lowerCAmelCase : Optional[int] = state.common.alphas_cumprod[t] lowerCAmelCase : Optional[int] = jnp.where(t > 0 , state.common.alphas_cumprod[t - 1] , jnp.array(1.0 , dtype=self.dtype ) ) lowerCAmelCase : Dict = 1 - alpha_prod_t lowerCAmelCase : Any = 1 - alpha_prod_t_prev # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if self.config.prediction_type == "epsilon": lowerCAmelCase : Union[str, Any] = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif self.config.prediction_type == "sample": lowerCAmelCase : List[Any] = model_output elif self.config.prediction_type == "v_prediction": lowerCAmelCase : Tuple = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output else: raise ValueError( f"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample` """ " for the FlaxDDPMScheduler." ) # 3. Clip "predicted x_0" if self.config.clip_sample: lowerCAmelCase : Optional[int] = jnp.clip(snake_case__ , -1 , 1 ) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowerCAmelCase : List[Any] = (alpha_prod_t_prev ** 0.5 * state.common.betas[t]) / beta_prod_t lowerCAmelCase : List[str] = state.common.alphas[t] ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowerCAmelCase : int = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise def random_variance(): lowerCAmelCase : Tuple = jax.random.split(snake_case__ , num=1 ) lowerCAmelCase : str = jax.random.normal(snake_case__ , shape=model_output.shape , dtype=self.dtype ) return (self._get_variance(snake_case__ , snake_case__ , predicted_variance=snake_case__ ) ** 0.5) * noise lowerCAmelCase : Union[str, Any] = jnp.where(t > 0 , random_variance() , jnp.zeros(model_output.shape , dtype=self.dtype ) ) lowerCAmelCase : Union[str, Any] = pred_prev_sample + variance if not return_dict: return (pred_prev_sample, state) return FlaxDDPMSchedulerOutput(prev_sample=snake_case__ , state=snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" return add_noise_common(state.common , snake_case__ , snake_case__ , snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): """simple docstring""" return get_velocity_common(state.common , snake_case__ , snake_case__ , snake_case__ ) def __len__( self ): """simple docstring""" return self.config.num_train_timesteps
681
0
"""simple docstring""" from pathlib import Path import torch from ...utils import is_npu_available, is_xpu_available from .config_args import ClusterConfig, default_json_config_file from .config_utils import SubcommandHelpFormatter lowerCAmelCase__ : Dict = '''Create a default config file for Accelerate with only a few flags set.''' def a__ ( SCREAMING_SNAKE_CASE : Any="no" , SCREAMING_SNAKE_CASE : str = default_json_config_file , SCREAMING_SNAKE_CASE : bool = False ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = Path(SCREAMING_SNAKE_CASE ) path.parent.mkdir(parents=SCREAMING_SNAKE_CASE , exist_ok=SCREAMING_SNAKE_CASE ) if path.exists(): print( f"""Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.""" ) return False lowerCAmelCase : int = mixed_precision.lower() if mixed_precision not in ["no", "fp16", "bf16", "fp8"]: raise ValueError( f"""`mixed_precision` should be one of 'no', 'fp16', 'bf16', or 'fp8'. Received {mixed_precision}""" ) lowerCAmelCase : Optional[Any] = { "compute_environment": "LOCAL_MACHINE", "mixed_precision": mixed_precision, } if torch.cuda.is_available(): lowerCAmelCase : List[Any] = torch.cuda.device_count() lowerCAmelCase : Optional[int] = num_gpus lowerCAmelCase : Optional[Any] = False if num_gpus > 1: lowerCAmelCase : str = "MULTI_GPU" else: lowerCAmelCase : Union[str, Any] = "NO" elif is_xpu_available() and use_xpu: lowerCAmelCase : List[Any] = torch.xpu.device_count() lowerCAmelCase : Optional[int] = num_xpus lowerCAmelCase : List[str] = False if num_xpus > 1: lowerCAmelCase : Tuple = "MULTI_XPU" else: lowerCAmelCase : Dict = "NO" elif is_npu_available(): lowerCAmelCase : List[Any] = torch.npu.device_count() lowerCAmelCase : str = num_npus lowerCAmelCase : Any = False if num_npus > 1: lowerCAmelCase : Optional[int] = "MULTI_NPU" else: lowerCAmelCase : Tuple = "NO" else: lowerCAmelCase : Tuple = 0 lowerCAmelCase : Any = True lowerCAmelCase : Union[str, Any] = 1 lowerCAmelCase : Optional[int] = "NO" lowerCAmelCase : Optional[Any] = ClusterConfig(**SCREAMING_SNAKE_CASE ) config.to_json_file(SCREAMING_SNAKE_CASE ) return path def a__ ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : List[Any] ): '''simple docstring''' lowerCAmelCase : Dict = parser.add_parser("default" , parents=SCREAMING_SNAKE_CASE , help=SCREAMING_SNAKE_CASE , formatter_class=SCREAMING_SNAKE_CASE ) parser.add_argument( "--config_file" , default=SCREAMING_SNAKE_CASE , help=( "The path to use to store the config file. Will default to a file named default_config.yaml in the cache " "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have " "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed " "with 'huggingface'." ) , dest="save_location" , ) parser.add_argument( "--mixed_precision" , choices=["no", "fp16", "bf16"] , type=SCREAMING_SNAKE_CASE , help="Whether or not to use mixed precision training. " "Choose between FP16 and BF16 (bfloat16) training. " "BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later." , default="no" , ) parser.set_defaults(func=SCREAMING_SNAKE_CASE ) return parser def a__ ( SCREAMING_SNAKE_CASE : str ): '''simple docstring''' lowerCAmelCase : List[str] = write_basic_config(args.mixed_precision , args.save_location ) if config_file: print(f"""accelerate configuration saved at {config_file}""" )
703
"""simple docstring""" import argparse import OmegaConf import torch from diffusers import DDIMScheduler, LDMPipeline, UNetLDMModel, VQModel def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : Tuple = OmegaConf.load(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = torch.load(SCREAMING_SNAKE_CASE , map_location="cpu" )["model"] lowerCAmelCase : int = list(state_dict.keys() ) # extract state_dict for VQVAE lowerCAmelCase : Tuple = {} lowerCAmelCase : Dict = "first_stage_model." for key in keys: if key.startswith(SCREAMING_SNAKE_CASE ): lowerCAmelCase : List[str] = state_dict[key] # extract state_dict for UNetLDM lowerCAmelCase : List[Any] = {} lowerCAmelCase : Tuple = "model.diffusion_model." for key in keys: if key.startswith(SCREAMING_SNAKE_CASE ): lowerCAmelCase : str = state_dict[key] lowerCAmelCase : List[str] = config.model.params.first_stage_config.params lowerCAmelCase : List[Any] = config.model.params.unet_config.params lowerCAmelCase : Union[str, Any] = VQModel(**SCREAMING_SNAKE_CASE ).eval() vqvae.load_state_dict(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = UNetLDMModel(**SCREAMING_SNAKE_CASE ).eval() unet.load_state_dict(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[Any] = DDIMScheduler( timesteps=config.model.params.timesteps , beta_schedule="scaled_linear" , beta_start=config.model.params.linear_start , beta_end=config.model.params.linear_end , clip_sample=SCREAMING_SNAKE_CASE , ) lowerCAmelCase : Tuple = LDMPipeline(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) pipeline.save_pretrained(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument('''--checkpoint_path''', type=str, required=True) parser.add_argument('''--config_path''', type=str, required=True) parser.add_argument('''--output_path''', type=str, required=True) lowerCAmelCase__ = parser.parse_args() convert_ldm_original(args.checkpoint_path, args.config_path, args.output_path)
681
0
"""simple docstring""" import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''b0''': efficientnet.EfficientNetBa, '''b1''': efficientnet.EfficientNetBa, '''b2''': efficientnet.EfficientNetBa, '''b3''': efficientnet.EfficientNetBa, '''b4''': efficientnet.EfficientNetBa, '''b5''': efficientnet.EfficientNetBa, '''b6''': efficientnet.EfficientNetBa, '''b7''': efficientnet.EfficientNetBa, } lowerCAmelCase__ = { '''b0''': { '''hidden_dim''': 1_280, '''width_coef''': 1.0, '''depth_coef''': 1.0, '''image_size''': 224, '''dropout_rate''': 0.2, '''dw_padding''': [], }, '''b1''': { '''hidden_dim''': 1_280, '''width_coef''': 1.0, '''depth_coef''': 1.1, '''image_size''': 240, '''dropout_rate''': 0.2, '''dw_padding''': [16], }, '''b2''': { '''hidden_dim''': 1_408, '''width_coef''': 1.1, '''depth_coef''': 1.2, '''image_size''': 260, '''dropout_rate''': 0.3, '''dw_padding''': [5, 8, 16], }, '''b3''': { '''hidden_dim''': 1_536, '''width_coef''': 1.2, '''depth_coef''': 1.4, '''image_size''': 300, '''dropout_rate''': 0.3, '''dw_padding''': [5, 18], }, '''b4''': { '''hidden_dim''': 1_792, '''width_coef''': 1.4, '''depth_coef''': 1.8, '''image_size''': 380, '''dropout_rate''': 0.4, '''dw_padding''': [6], }, '''b5''': { '''hidden_dim''': 2_048, '''width_coef''': 1.6, '''depth_coef''': 2.2, '''image_size''': 456, '''dropout_rate''': 0.4, '''dw_padding''': [13, 27], }, '''b6''': { '''hidden_dim''': 2_304, '''width_coef''': 1.8, '''depth_coef''': 2.6, '''image_size''': 528, '''dropout_rate''': 0.5, '''dw_padding''': [31], }, '''b7''': { '''hidden_dim''': 2_560, '''width_coef''': 2.0, '''depth_coef''': 3.1, '''image_size''': 600, '''dropout_rate''': 0.5, '''dw_padding''': [18], }, } def a__ ( SCREAMING_SNAKE_CASE : Dict ): '''simple docstring''' lowerCAmelCase : int = EfficientNetConfig() lowerCAmelCase : Optional[int] = CONFIG_MAP[model_name]["hidden_dim"] lowerCAmelCase : List[str] = CONFIG_MAP[model_name]["width_coef"] lowerCAmelCase : int = CONFIG_MAP[model_name]["depth_coef"] lowerCAmelCase : Union[str, Any] = CONFIG_MAP[model_name]["image_size"] lowerCAmelCase : List[Any] = CONFIG_MAP[model_name]["dropout_rate"] lowerCAmelCase : Dict = CONFIG_MAP[model_name]["dw_padding"] lowerCAmelCase : str = "huggingface/label-files" lowerCAmelCase : Optional[int] = "imagenet-1k-id2label.json" lowerCAmelCase : Dict = 1_0_0_0 lowerCAmelCase : Tuple = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , repo_type="dataset" ) , "r" ) ) lowerCAmelCase : List[str] = {int(SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} lowerCAmelCase : str = idalabel lowerCAmelCase : str = {v: k for k, v in idalabel.items()} return config def a__ ( ): '''simple docstring''' lowerCAmelCase : List[Any] = "http://images.cocodataset.org/val2017/000000039769.jpg" lowerCAmelCase : Any = Image.open(requests.get(SCREAMING_SNAKE_CASE , stream=SCREAMING_SNAKE_CASE ).raw ) return im def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' lowerCAmelCase : List[str] = CONFIG_MAP[model_name]["image_size"] lowerCAmelCase : Optional[int] = EfficientNetImageProcessor( size={"height": size, "width": size} , image_mean=[0.485, 0.456, 0.406] , image_std=[0.47_853_944, 0.4_732_864, 0.47_434_163] , do_center_crop=SCREAMING_SNAKE_CASE , ) return preprocessor def a__ ( SCREAMING_SNAKE_CASE : Dict ): '''simple docstring''' lowerCAmelCase : Tuple = [v.split("_" )[0].split("block" )[1] for v in original_param_names if v.startswith("block" )] lowerCAmelCase : Optional[int] = sorted(set(SCREAMING_SNAKE_CASE ) ) lowerCAmelCase : int = len(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[Any] = {b: str(SCREAMING_SNAKE_CASE ) for b, i in zip(SCREAMING_SNAKE_CASE , range(SCREAMING_SNAKE_CASE ) )} lowerCAmelCase : Dict = [] rename_keys.append(("stem_conv/kernel:0", "embeddings.convolution.weight") ) rename_keys.append(("stem_bn/gamma:0", "embeddings.batchnorm.weight") ) rename_keys.append(("stem_bn/beta:0", "embeddings.batchnorm.bias") ) rename_keys.append(("stem_bn/moving_mean:0", "embeddings.batchnorm.running_mean") ) rename_keys.append(("stem_bn/moving_variance:0", "embeddings.batchnorm.running_var") ) for b in block_names: lowerCAmelCase : List[Any] = block_name_mapping[b] rename_keys.append((f"""block{b}_expand_conv/kernel:0""", f"""encoder.blocks.{hf_b}.expansion.expand_conv.weight""") ) rename_keys.append((f"""block{b}_expand_bn/gamma:0""", f"""encoder.blocks.{hf_b}.expansion.expand_bn.weight""") ) rename_keys.append((f"""block{b}_expand_bn/beta:0""", f"""encoder.blocks.{hf_b}.expansion.expand_bn.bias""") ) rename_keys.append( (f"""block{b}_expand_bn/moving_mean:0""", f"""encoder.blocks.{hf_b}.expansion.expand_bn.running_mean""") ) rename_keys.append( (f"""block{b}_expand_bn/moving_variance:0""", f"""encoder.blocks.{hf_b}.expansion.expand_bn.running_var""") ) rename_keys.append( (f"""block{b}_dwconv/depthwise_kernel:0""", f"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight""") ) rename_keys.append((f"""block{b}_bn/gamma:0""", f"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight""") ) rename_keys.append((f"""block{b}_bn/beta:0""", f"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias""") ) rename_keys.append( (f"""block{b}_bn/moving_mean:0""", f"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean""") ) rename_keys.append( (f"""block{b}_bn/moving_variance:0""", f"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var""") ) rename_keys.append((f"""block{b}_se_reduce/kernel:0""", f"""encoder.blocks.{hf_b}.squeeze_excite.reduce.weight""") ) rename_keys.append((f"""block{b}_se_reduce/bias:0""", f"""encoder.blocks.{hf_b}.squeeze_excite.reduce.bias""") ) rename_keys.append((f"""block{b}_se_expand/kernel:0""", f"""encoder.blocks.{hf_b}.squeeze_excite.expand.weight""") ) rename_keys.append((f"""block{b}_se_expand/bias:0""", f"""encoder.blocks.{hf_b}.squeeze_excite.expand.bias""") ) rename_keys.append( (f"""block{b}_project_conv/kernel:0""", f"""encoder.blocks.{hf_b}.projection.project_conv.weight""") ) rename_keys.append((f"""block{b}_project_bn/gamma:0""", f"""encoder.blocks.{hf_b}.projection.project_bn.weight""") ) rename_keys.append((f"""block{b}_project_bn/beta:0""", f"""encoder.blocks.{hf_b}.projection.project_bn.bias""") ) rename_keys.append( (f"""block{b}_project_bn/moving_mean:0""", f"""encoder.blocks.{hf_b}.projection.project_bn.running_mean""") ) rename_keys.append( (f"""block{b}_project_bn/moving_variance:0""", f"""encoder.blocks.{hf_b}.projection.project_bn.running_var""") ) rename_keys.append(("top_conv/kernel:0", "encoder.top_conv.weight") ) rename_keys.append(("top_bn/gamma:0", "encoder.top_bn.weight") ) rename_keys.append(("top_bn/beta:0", "encoder.top_bn.bias") ) rename_keys.append(("top_bn/moving_mean:0", "encoder.top_bn.running_mean") ) rename_keys.append(("top_bn/moving_variance:0", "encoder.top_bn.running_var") ) lowerCAmelCase : str = {} for item in rename_keys: if item[0] in original_param_names: lowerCAmelCase : Optional[int] = "efficientnet." + item[1] lowerCAmelCase : int = "classifier.weight" lowerCAmelCase : Union[str, Any] = "classifier.bias" return key_mapping def a__ ( SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' for key, value in tf_params.items(): if "normalization" in key: continue lowerCAmelCase : str = key_mapping[key] if "_conv" in key and "kernel" in key: lowerCAmelCase : Union[str, Any] = torch.from_numpy(SCREAMING_SNAKE_CASE ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: lowerCAmelCase : List[Any] = torch.from_numpy(SCREAMING_SNAKE_CASE ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: lowerCAmelCase : Any = torch.from_numpy(np.transpose(SCREAMING_SNAKE_CASE ) ) else: lowerCAmelCase : Any = torch.from_numpy(SCREAMING_SNAKE_CASE ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(SCREAMING_SNAKE_CASE ) @torch.no_grad() def a__ ( SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Any ): '''simple docstring''' lowerCAmelCase : Optional[int] = model_classes[model_name]( include_top=SCREAMING_SNAKE_CASE , weights="imagenet" , input_tensor=SCREAMING_SNAKE_CASE , input_shape=SCREAMING_SNAKE_CASE , pooling=SCREAMING_SNAKE_CASE , classes=1_0_0_0 , classifier_activation="softmax" , ) lowerCAmelCase : str = original_model.trainable_variables lowerCAmelCase : Union[str, Any] = original_model.non_trainable_variables lowerCAmelCase : int = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: lowerCAmelCase : int = param.numpy() lowerCAmelCase : Optional[int] = list(tf_params.keys() ) # Load HuggingFace model lowerCAmelCase : Tuple = get_efficientnet_config(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[Any] = EfficientNetForImageClassification(SCREAMING_SNAKE_CASE ).eval() lowerCAmelCase : List[Any] = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print("Converting parameters..." ) lowerCAmelCase : Optional[int] = rename_keys(SCREAMING_SNAKE_CASE ) replace_params(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # Initialize preprocessor and preprocess input image lowerCAmelCase : int = convert_image_processor(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[int] = preprocessor(images=prepare_img() , return_tensors="pt" ) # HF model inference hf_model.eval() with torch.no_grad(): lowerCAmelCase : Union[str, Any] = hf_model(**SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = outputs.logits.detach().numpy() # Original model inference lowerCAmelCase : int = False lowerCAmelCase : Any = CONFIG_MAP[model_name]["image_size"] lowerCAmelCase : Any = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) lowerCAmelCase : Dict = image.img_to_array(SCREAMING_SNAKE_CASE ) lowerCAmelCase : str = np.expand_dims(SCREAMING_SNAKE_CASE , axis=0 ) lowerCAmelCase : Any = original_model.predict(SCREAMING_SNAKE_CASE ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , atol=1E-3 ), "The predicted logits are not the same." print("Model outputs match!" ) if save_model: # Create folder to save model if not os.path.isdir(SCREAMING_SNAKE_CASE ): os.mkdir(SCREAMING_SNAKE_CASE ) # Save converted model and image processor hf_model.save_pretrained(SCREAMING_SNAKE_CASE ) preprocessor.save_pretrained(SCREAMING_SNAKE_CASE ) if push_to_hub: # Push model and image processor to hub print(f"""Pushing converted {model_name} to the hub...""" ) lowerCAmelCase : Union[str, Any] = f"""efficientnet-{model_name}""" preprocessor.push_to_hub(SCREAMING_SNAKE_CASE ) hf_model.push_to_hub(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''b0''', type=str, help='''Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''hf_model''', type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--save_model''', action='''store_true''', help='''Save model to local''') parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') lowerCAmelCase__ = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
704
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : int = 1_0_0_0 ): '''simple docstring''' return sum(e for e in range(3 , SCREAMING_SNAKE_CASE ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"{solution() = }")
681
0
"""simple docstring""" import unittest from transformers import ( MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TextClassificationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow from .test_pipelines_common import ANY # These 2 model types require different inputs than those of the usual text models. lowerCAmelCase__ = {'''LayoutLMv2Config''', '''LayoutLMv3Config'''} @is_pipeline_test class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" a : List[str] =MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING a : str =TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if model_mapping is not None: a : Tuple ={config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP} if tf_model_mapping is not None: a : Optional[int] ={ config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP } @require_torch def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = pipeline( task="text-classification" , model="hf-internal-testing/tiny-random-distilbert" , framework="pt" ) lowerCAmelCase : Union[str, Any] = text_classifier("This is great !" ) self.assertEqual(nested_simplify(snake_case__ ) , [{"label": "LABEL_0", "score": 0.504}] ) lowerCAmelCase : Dict = text_classifier("This is great !" , top_k=2 ) self.assertEqual( nested_simplify(snake_case__ ) , [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}] ) lowerCAmelCase : Dict = text_classifier(["This is great !", "This is bad"] , top_k=2 ) self.assertEqual( nested_simplify(snake_case__ ) , [ [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}], [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}], ] , ) lowerCAmelCase : Optional[Any] = text_classifier("This is great !" , top_k=1 ) self.assertEqual(nested_simplify(snake_case__ ) , [{"label": "LABEL_0", "score": 0.504}] ) # Legacy behavior lowerCAmelCase : Optional[int] = text_classifier("This is great !" , return_all_scores=snake_case__ ) self.assertEqual(nested_simplify(snake_case__ ) , [{"label": "LABEL_0", "score": 0.504}] ) lowerCAmelCase : Optional[int] = text_classifier("This is great !" , return_all_scores=snake_case__ ) self.assertEqual( nested_simplify(snake_case__ ) , [[{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}]] ) lowerCAmelCase : List[str] = text_classifier(["This is great !", "Something else"] , return_all_scores=snake_case__ ) self.assertEqual( nested_simplify(snake_case__ ) , [ [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}], [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}], ] , ) lowerCAmelCase : Any = text_classifier(["This is great !", "Something else"] , return_all_scores=snake_case__ ) self.assertEqual( nested_simplify(snake_case__ ) , [ {"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_0", "score": 0.504}, ] , ) @require_torch def lowercase__ ( self ): """simple docstring""" import torch lowerCAmelCase : Tuple = pipeline( task="text-classification" , model="hf-internal-testing/tiny-random-distilbert" , framework="pt" , device=torch.device("cpu" ) , ) lowerCAmelCase : Optional[Any] = text_classifier("This is great !" ) self.assertEqual(nested_simplify(snake_case__ ) , [{"label": "LABEL_0", "score": 0.504}] ) @require_tf def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[Any] = pipeline( task="text-classification" , model="hf-internal-testing/tiny-random-distilbert" , framework="tf" ) lowerCAmelCase : Any = text_classifier("This is great !" ) self.assertEqual(nested_simplify(snake_case__ ) , [{"label": "LABEL_0", "score": 0.504}] ) @slow @require_torch def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Tuple = pipeline("text-classification" ) lowerCAmelCase : List[Any] = text_classifier("This is great !" ) self.assertEqual(nested_simplify(snake_case__ ) , [{"label": "POSITIVE", "score": 1.0}] ) lowerCAmelCase : str = text_classifier("This is bad !" ) self.assertEqual(nested_simplify(snake_case__ ) , [{"label": "NEGATIVE", "score": 1.0}] ) lowerCAmelCase : List[str] = text_classifier("Birds are a type of animal" ) self.assertEqual(nested_simplify(snake_case__ ) , [{"label": "POSITIVE", "score": 0.988}] ) @slow @require_tf def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Tuple = pipeline("text-classification" , framework="tf" ) lowerCAmelCase : List[str] = text_classifier("This is great !" ) self.assertEqual(nested_simplify(snake_case__ ) , [{"label": "POSITIVE", "score": 1.0}] ) lowerCAmelCase : List[str] = text_classifier("This is bad !" ) self.assertEqual(nested_simplify(snake_case__ ) , [{"label": "NEGATIVE", "score": 1.0}] ) lowerCAmelCase : List[str] = text_classifier("Birds are a type of animal" ) self.assertEqual(nested_simplify(snake_case__ ) , [{"label": "POSITIVE", "score": 0.988}] ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : List[Any] = TextClassificationPipeline(model=snake_case__ , tokenizer=snake_case__ ) return text_classifier, ["HuggingFace is in", "This is another test"] def lowercase__ ( self , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = text_classifier.model # Small inputs because BartTokenizer tiny has maximum position embeddings = 22 lowerCAmelCase : List[str] = "HuggingFace is in" lowerCAmelCase : Optional[Any] = text_classifier(snake_case__ ) self.assertEqual(nested_simplify(snake_case__ ) , [{"label": ANY(snake_case__ ), "score": ANY(snake_case__ )}] ) self.assertTrue(outputs[0]["label"] in model.config.idalabel.values() ) lowerCAmelCase : Any = ["HuggingFace is in ", "Paris is in France"] lowerCAmelCase : Tuple = text_classifier(snake_case__ ) self.assertEqual( nested_simplify(snake_case__ ) , [{"label": ANY(snake_case__ ), "score": ANY(snake_case__ )}, {"label": ANY(snake_case__ ), "score": ANY(snake_case__ )}] , ) self.assertTrue(outputs[0]["label"] in model.config.idalabel.values() ) self.assertTrue(outputs[1]["label"] in model.config.idalabel.values() ) # Forcing to get all results with `top_k=None` # This is NOT the legacy format lowerCAmelCase : str = text_classifier(snake_case__ , top_k=snake_case__ ) lowerCAmelCase : Dict = len(model.config.idalabel.values() ) self.assertEqual( nested_simplify(snake_case__ ) , [[{"label": ANY(snake_case__ ), "score": ANY(snake_case__ )}] * N, [{"label": ANY(snake_case__ ), "score": ANY(snake_case__ )}] * N] , ) lowerCAmelCase : Union[str, Any] = {"text": "HuggingFace is in ", "text_pair": "Paris is in France"} lowerCAmelCase : Union[str, Any] = text_classifier(snake_case__ ) self.assertEqual( nested_simplify(snake_case__ ) , {"label": ANY(snake_case__ ), "score": ANY(snake_case__ )} , ) self.assertTrue(outputs["label"] in model.config.idalabel.values() ) # This might be used a text pair, but tokenizer + pipe interaction # makes it hard to understand that it's not using the pair properly # https://github.com/huggingface/transformers/issues/17305 # We disabled this usage instead as it was outputting wrong outputs. lowerCAmelCase : Union[str, Any] = [["HuggingFace is in ", "Paris is in France"]] with self.assertRaises(snake_case__ ): text_classifier(snake_case__ ) # This used to be valid for doing text pairs # We're keeping it working because of backward compatibility lowerCAmelCase : List[str] = text_classifier([[["HuggingFace is in ", "Paris is in France"]]] ) self.assertEqual( nested_simplify(snake_case__ ) , [{"label": ANY(snake_case__ ), "score": ANY(snake_case__ )}] , ) self.assertTrue(outputs[0]["label"] in model.config.idalabel.values() )
705
"""simple docstring""" import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized, parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv("TEST_SAGEMAKER" , "False" ) ) is not True , reason="Skipping test because should only be run when releasing minor transformers version" , ) @pytest.mark.usefixtures("sm_env" ) @parameterized_class( [ { "framework": "pytorch", "script": "run_glue_model_parallelism.py", "model_name_or_path": "roberta-large", "instance_type": "ml.p3dn.24xlarge", "results": {"train_runtime": 16_00, "eval_accuracy": 0.3, "eval_loss": 1.2}, }, { "framework": "pytorch", "script": "run_glue.py", "model_name_or_path": "roberta-large", "instance_type": "ml.p3dn.24xlarge", "results": {"train_runtime": 16_00, "eval_accuracy": 0.3, "eval_loss": 1.2}, }, ] ) class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" if self.framework == "pytorch": subprocess.run( f"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split() , encoding="utf-8" , check=snake_case__ , ) assert hasattr(self , "env" ) def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Optional[Any] = { "enabled": True, "processes_per_host": 8, } lowerCAmelCase : List[Any] = { "enabled": True, "parameters": { "microbatches": 4, "placement_strategy": "spread", "pipeline": "interleaved", "optimize": "speed", "partitions": 4, "ddp": True, }, } lowerCAmelCase : List[Any] = {"smdistributed": {"modelparallel": smp_options}, "mpi": mpi_options} lowerCAmelCase : Optional[Any] = "trainer" if self.script == "run_glue.py" else "smtrainer" # creates estimator return HuggingFace( entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=f"""{self.env.base_job_name}-{instance_count}-smp-{name_extension}""" , instance_count=snake_case__ , instance_type=self.instance_type , debugger_hook_config=snake_case__ , hyperparameters={ **self.env.hyperparameters, "model_name_or_path": self.model_name_or_path, "max_steps": 500, } , metric_definitions=self.env.metric_definitions , distribution=snake_case__ , py_version="py36" , ) def lowercase__ ( self , snake_case__ ): """simple docstring""" TrainingJobAnalytics(snake_case__ ).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv""" ) @parameterized.expand([(1,)] ) def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = self.create_estimator(snake_case__ ) # run training estimator.fit() # result dataframe lowerCAmelCase : Tuple = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis lowerCAmelCase : Tuple = list(result_metrics_df[result_metrics_df.metric_name == "eval_accuracy"]["value"] ) lowerCAmelCase : Optional[int] = list(result_metrics_df[result_metrics_df.metric_name == "eval_loss"]["value"] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping lowerCAmelCase : Dict = ( Session().describe_training_job(estimator.latest_training_job.name ).get("TrainingTimeInSeconds" , 999_999 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results["eval_accuracy"] for t in eval_accuracy ) assert all(t <= self.results["eval_loss"] for t in eval_loss ) # dump tests result into json file to share in PR with open(f"""{estimator.latest_training_job.name}.json""" , "w" ) as outfile: json.dump({"train_time": train_runtime, "eval_accuracy": eval_accuracy, "eval_loss": eval_loss} , snake_case__ )
681
0
"""simple docstring""" import logging import os from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional from tqdm import auto as tqdm_lib lowerCAmelCase__ = { '''debug''': logging.DEBUG, '''info''': logging.INFO, '''warning''': logging.WARNING, '''error''': logging.ERROR, '''critical''': logging.CRITICAL, } lowerCAmelCase__ = logging.WARNING def a__ ( ): '''simple docstring''' lowerCAmelCase : List[Any] = os.getenv("DATASETS_VERBOSITY" , SCREAMING_SNAKE_CASE ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( f"""Unknown option DATASETS_VERBOSITY={env_level_str}, """ f"""has to be one of: { ", ".join(log_levels.keys() ) }""" ) return _default_log_level def a__ ( ): '''simple docstring''' return __name__.split("." )[0] def a__ ( ): '''simple docstring''' return logging.getLogger(_get_library_name() ) def a__ ( ): '''simple docstring''' lowerCAmelCase : List[Any] = _get_library_root_logger() library_root_logger.setLevel(_get_default_logging_level() ) def a__ ( ): '''simple docstring''' lowerCAmelCase : List[str] = _get_library_root_logger() library_root_logger.setLevel(logging.NOTSET ) def a__ ( SCREAMING_SNAKE_CASE : Optional[str] = None ): '''simple docstring''' if name is None: lowerCAmelCase : Optional[int] = _get_library_name() return logging.getLogger(SCREAMING_SNAKE_CASE ) def a__ ( ): '''simple docstring''' return _get_library_root_logger().getEffectiveLevel() def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' _get_library_root_logger().setLevel(SCREAMING_SNAKE_CASE ) def a__ ( ): '''simple docstring''' return set_verbosity(SCREAMING_SNAKE_CASE ) def a__ ( ): '''simple docstring''' return set_verbosity(SCREAMING_SNAKE_CASE ) def a__ ( ): '''simple docstring''' return set_verbosity(SCREAMING_SNAKE_CASE ) def a__ ( ): '''simple docstring''' return set_verbosity(SCREAMING_SNAKE_CASE ) def a__ ( ): '''simple docstring''' lowerCAmelCase : Dict = False def a__ ( ): '''simple docstring''' lowerCAmelCase : Optional[int] = True # Configure the library root logger at the module level (singleton-like) _configure_library_root_logger() class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , *snake_case__ , **snake_case__ ): # pylint: disable=unused-argument """simple docstring""" lowerCAmelCase : str = args[0] if args else None def __iter__( self ): """simple docstring""" return iter(self._iterator ) def __getattr__( self , snake_case__ ): """simple docstring""" def empty_fn(*snake_case__ , **snake_case__ ): # pylint: disable=unused-argument return return empty_fn def __enter__( self ): """simple docstring""" return self def __exit__( self , snake_case__ , snake_case__ , snake_case__ ): """simple docstring""" return lowerCAmelCase__ = True class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __call__( self , *snake_case__ , snake_case__=False , **snake_case__ ): """simple docstring""" if _tqdm_active and not disable: return tqdm_lib.tqdm(*snake_case__ , **snake_case__ ) else: return EmptyTqdm(*snake_case__ , **snake_case__ ) def lowercase__ ( self , *snake_case__ , **snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*snake_case__ , **snake_case__ ) def lowercase__ ( self ): """simple docstring""" if _tqdm_active: return tqdm_lib.tqdm.get_lock() lowerCAmelCase__ = _tqdm_cls() def a__ ( ): '''simple docstring''' global _tqdm_active return bool(_tqdm_active ) def a__ ( ): '''simple docstring''' global _tqdm_active lowerCAmelCase : List[str] = True def a__ ( ): '''simple docstring''' global _tqdm_active lowerCAmelCase : int = False
706
"""simple docstring""" from math import factorial def a__ ( SCREAMING_SNAKE_CASE : int = 1_0_0 ): '''simple docstring''' return sum(int(SCREAMING_SNAKE_CASE ) for x in str(factorial(SCREAMING_SNAKE_CASE ) ) ) if __name__ == "__main__": print(solution(int(input('''Enter the Number: ''').strip())))
681
0
"""simple docstring""" from math import ceil def a__ ( SCREAMING_SNAKE_CASE : int = 1_0_0_1 ): '''simple docstring''' lowerCAmelCase : int = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): lowerCAmelCase : Optional[int] = 2 * i + 1 lowerCAmelCase : Optional[int] = 2 * i lowerCAmelCase : str = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: lowerCAmelCase__ = int(sys.argv[1]) print(solution(n)) except ValueError: print('''Invalid entry - please enter a number''')
707
"""simple docstring""" from typing import Any class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = data lowerCAmelCase : Any = None def __repr__( self ): """simple docstring""" return f"""Node({self.data})""" class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self ): """simple docstring""" lowerCAmelCase : Tuple = None def __iter__( self ): """simple docstring""" lowerCAmelCase : Any = self.head while node: yield node.data lowerCAmelCase : Optional[int] = node.next def __len__( self ): """simple docstring""" return sum(1 for _ in self ) def __repr__( self ): """simple docstring""" return "->".join([str(snake_case__ ) for item in self] ) def __getitem__( self , snake_case__ ): """simple docstring""" if not 0 <= index < len(self ): raise ValueError("list index out of range." ) for i, node in enumerate(self ): if i == index: return node return None def __setitem__( self , snake_case__ , snake_case__ ): """simple docstring""" if not 0 <= index < len(self ): raise ValueError("list index out of range." ) lowerCAmelCase : Union[str, Any] = self.head for _ in range(snake_case__ ): lowerCAmelCase : int = current.next lowerCAmelCase : List[str] = data def lowercase__ ( self , snake_case__ ): """simple docstring""" self.insert_nth(len(self ) , snake_case__ ) def lowercase__ ( self , snake_case__ ): """simple docstring""" self.insert_nth(0 , snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ ): """simple docstring""" if not 0 <= index <= len(self ): raise IndexError("list index out of range" ) lowerCAmelCase : Optional[int] = Node(snake_case__ ) if self.head is None: lowerCAmelCase : Any = new_node elif index == 0: lowerCAmelCase : Any = self.head # link new_node to head lowerCAmelCase : Union[str, Any] = new_node else: lowerCAmelCase : List[str] = self.head for _ in range(index - 1 ): lowerCAmelCase : int = temp.next lowerCAmelCase : int = temp.next lowerCAmelCase : Dict = new_node def lowercase__ ( self ): # print every node data """simple docstring""" print(self ) def lowercase__ ( self ): """simple docstring""" return self.delete_nth(0 ) def lowercase__ ( self ): # delete from tail """simple docstring""" return self.delete_nth(len(self ) - 1 ) def lowercase__ ( self , snake_case__ = 0 ): """simple docstring""" if not 0 <= index <= len(self ) - 1: # test if index is valid raise IndexError("List index out of range." ) lowerCAmelCase : List[Any] = self.head # default first node if index == 0: lowerCAmelCase : Optional[int] = self.head.next else: lowerCAmelCase : List[str] = self.head for _ in range(index - 1 ): lowerCAmelCase : Union[str, Any] = temp.next lowerCAmelCase : Optional[Any] = temp.next lowerCAmelCase : Any = temp.next.next return delete_node.data def lowercase__ ( self ): """simple docstring""" return self.head is None def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = None lowerCAmelCase : Optional[int] = self.head while current: # Store the current node's next node. lowerCAmelCase : List[Any] = current.next # Make the current node's next point backwards lowerCAmelCase : Dict = prev # Make the previous node be the current node lowerCAmelCase : List[str] = current # Make the current node the next node (to progress iteration) lowerCAmelCase : int = next_node # Return prev in order to put the head at the end lowerCAmelCase : Tuple = prev def a__ ( ): '''simple docstring''' lowerCAmelCase : Tuple = LinkedList() assert linked_list.is_empty() is True assert str(SCREAMING_SNAKE_CASE ) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(1_0 ): assert len(SCREAMING_SNAKE_CASE ) == i linked_list.insert_nth(SCREAMING_SNAKE_CASE , i + 1 ) assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(1 , 1_1 ) ) linked_list.insert_head(0 ) linked_list.insert_tail(1_1 ) assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(0 , 1_2 ) ) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9 ) == 1_0 assert linked_list.delete_tail() == 1_1 assert len(SCREAMING_SNAKE_CASE ) == 9 assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(1 , 1_0 ) ) assert all(linked_list[i] == i + 1 for i in range(0 , 9 ) ) is True for i in range(0 , 9 ): lowerCAmelCase : Optional[Any] = -i assert all(linked_list[i] == -i for i in range(0 , 9 ) ) is True linked_list.reverse() assert str(SCREAMING_SNAKE_CASE ) == "->".join(str(SCREAMING_SNAKE_CASE ) for i in range(-8 , 1 ) ) def a__ ( ): '''simple docstring''' lowerCAmelCase : List[str] = [ -9, 1_0_0, Node(7_7_3_4_5_1_1_2 ), "dlrow olleH", 7, 5_5_5_5, 0, -192.55_555, "Hello, world!", 77.9, Node(1_0 ), None, None, 12.20, ] lowerCAmelCase : List[str] = LinkedList() for i in test_input: linked_list.insert_tail(SCREAMING_SNAKE_CASE ) # Check if it's empty or not assert linked_list.is_empty() is False assert ( str(SCREAMING_SNAKE_CASE ) == "-9->100->Node(77345112)->dlrow olleH->7->5555->0->" "-192.55555->Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the head lowerCAmelCase : str = linked_list.delete_head() assert result == -9 assert ( str(SCREAMING_SNAKE_CASE ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the tail lowerCAmelCase : Union[str, Any] = linked_list.delete_tail() assert result == 12.2 assert ( str(SCREAMING_SNAKE_CASE ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None" ) # Delete a node in specific location in linked list lowerCAmelCase : List[str] = linked_list.delete_nth(1_0 ) assert result is None assert ( str(SCREAMING_SNAKE_CASE ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None" ) # Add a Node instance to its head linked_list.insert_head(Node("Hello again, world!" ) ) assert ( str(SCREAMING_SNAKE_CASE ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None" ) # Add None to its tail linked_list.insert_tail(SCREAMING_SNAKE_CASE ) assert ( str(SCREAMING_SNAKE_CASE ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None->None" ) # Reverse the linked list linked_list.reverse() assert ( str(SCREAMING_SNAKE_CASE ) == "None->None->Node(10)->77.9->Hello, world!->-192.55555->0->5555->" "7->dlrow olleH->Node(77345112)->100->Node(Hello again, world!)" ) def a__ ( ): '''simple docstring''' from doctest import testmod testmod() lowerCAmelCase : Optional[Any] = LinkedList() linked_list.insert_head(input("Inserting 1st at head " ).strip() ) linked_list.insert_head(input("Inserting 2nd at head " ).strip() ) print("\nPrint list:" ) linked_list.print_list() linked_list.insert_tail(input("\nInserting 1st at tail " ).strip() ) linked_list.insert_tail(input("Inserting 2nd at tail " ).strip() ) print("\nPrint list:" ) linked_list.print_list() print("\nDelete head" ) linked_list.delete_head() print("Delete tail" ) linked_list.delete_tail() print("\nPrint list:" ) linked_list.print_list() print("\nReverse linked list" ) linked_list.reverse() print("\nPrint list:" ) linked_list.print_list() print("\nString representation of linked list:" ) print(SCREAMING_SNAKE_CASE ) print("\nReading/changing Node data using indexing:" ) print(f"""Element at Position 1: {linked_list[1]}""" ) lowerCAmelCase : Any = input("Enter New Value: " ).strip() print("New list:" ) print(SCREAMING_SNAKE_CASE ) print(f"""length of linked_list is : {len(SCREAMING_SNAKE_CASE )}""" ) if __name__ == "__main__": main()
681
0
from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def a__ ( SCREAMING_SNAKE_CASE : str = "laptop" ): '''simple docstring''' lowerCAmelCase : Optional[Any] = f"""https://www.amazon.in/laptop/s?k={product}""" lowerCAmelCase : str = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36\n (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36", "Accept-Language": "en-US, en;q=0.5", } lowerCAmelCase : Union[str, Any] = BeautifulSoup(requests.get(SCREAMING_SNAKE_CASE , headers=SCREAMING_SNAKE_CASE ).text ) # Initialize a Pandas dataframe with the column titles lowerCAmelCase : int = DataFrame( columns=[ "Product Title", "Product Link", "Current Price of the product", "Product Rating", "MRP of the product", "Discount", ] ) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( "div" , attrs={"class": "s-result-item", "data-component-type": "s-search-result"} , ) , soup.find_all("div" , attrs={"class": "a-row a-size-base a-color-base"} ) , ): try: lowerCAmelCase : Any = item.ha.text lowerCAmelCase : List[str] = "https://www.amazon.in/" + item.ha.a["href"] lowerCAmelCase : Tuple = item.find("span" , attrs={"class": "a-offscreen"} ).text try: lowerCAmelCase : List[Any] = item.find("span" , attrs={"class": "a-icon-alt"} ).text except AttributeError: lowerCAmelCase : List[Any] = "Not available" try: lowerCAmelCase : Any = ( "₹" + item.find( "span" , attrs={"class": "a-price a-text-price"} ).text.split("₹" )[1] ) except AttributeError: lowerCAmelCase : Tuple = "" try: lowerCAmelCase : int = float( ( ( float(product_mrp.strip("₹" ).replace("," , "" ) ) - float(product_price.strip("₹" ).replace("," , "" ) ) ) / float(product_mrp.strip("₹" ).replace("," , "" ) ) ) * 1_0_0 ) except ValueError: lowerCAmelCase : Tuple = float("nan" ) except AttributeError: pass lowerCAmelCase : Tuple = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] lowerCAmelCase : int = " " lowerCAmelCase : List[Any] = " " data_frame.index += 1 return data_frame if __name__ == "__main__": lowerCAmelCase__ = '''headphones''' get_amazon_product_data(product).to_csv(F"Amazon Product Data for {product}.csv")
708
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowerCAmelCase__ = { '''configuration_efficientformer''': [ '''EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EfficientFormerConfig''', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''EfficientFormerImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EfficientFormerForImageClassification''', '''EfficientFormerForImageClassificationWithTeacher''', '''EfficientFormerModel''', '''EfficientFormerPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFEfficientFormerForImageClassification''', '''TFEfficientFormerForImageClassificationWithTeacher''', '''TFEfficientFormerModel''', '''TFEfficientFormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_efficientformer import EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientformer import EfficientFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientformer import ( EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientFormerForImageClassification, EfficientFormerForImageClassificationWithTeacher, EfficientFormerModel, EfficientFormerPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_efficientformer import ( TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFEfficientFormerForImageClassification, TFEfficientFormerForImageClassificationWithTeacher, TFEfficientFormerModel, TFEfficientFormerPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
681
0
"""simple docstring""" from math import factorial def a__ ( SCREAMING_SNAKE_CASE : int = 1_0_0 ): '''simple docstring''' return sum(map(SCREAMING_SNAKE_CASE , str(factorial(SCREAMING_SNAKE_CASE ) ) ) ) if __name__ == "__main__": print(solution(int(input('''Enter the Number: ''').strip())))
709
"""simple docstring""" 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 ) lowerCAmelCase__ = logging.getLogger(__name__) def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : Optional[Any] = np.argmax(SCREAMING_SNAKE_CASE , axis=1 ) return np.sum(outputs == labels ) def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE , encoding="utf_8" ) as f: lowerCAmelCase : Tuple = csv.reader(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = [] next(SCREAMING_SNAKE_CASE ) # skip the first line for line in tqdm(SCREAMING_SNAKE_CASE ): output.append((" ".join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : str ): '''simple docstring''' lowerCAmelCase : List[Any] = [] for dataset in encoded_datasets: lowerCAmelCase : Dict = len(SCREAMING_SNAKE_CASE ) lowerCAmelCase : str = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) lowerCAmelCase : Tuple = np.zeros((n_batch, 2) , dtype=np.intaa ) lowerCAmelCase : int = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) lowerCAmelCase : List[Any] = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(SCREAMING_SNAKE_CASE ): lowerCAmelCase : int = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase : Union[str, Any] = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase : Tuple = with_conta lowerCAmelCase : Any = with_conta lowerCAmelCase : Optional[Any] = len(SCREAMING_SNAKE_CASE ) - 1 lowerCAmelCase : Dict = len(SCREAMING_SNAKE_CASE ) - 1 lowerCAmelCase : Optional[Any] = with_conta lowerCAmelCase : List[Any] = with_conta lowerCAmelCase : str = mc_label lowerCAmelCase : Dict = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(SCREAMING_SNAKE_CASE ) for t in all_inputs ) ) return tensor_datasets def a__ ( ): '''simple docstring''' lowerCAmelCase : Optional[int] = argparse.ArgumentParser() parser.add_argument("--model_name" , type=SCREAMING_SNAKE_CASE , 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=SCREAMING_SNAKE_CASE , type=SCREAMING_SNAKE_CASE , required=SCREAMING_SNAKE_CASE , help="The output directory where the model predictions and checkpoints will be written." , ) parser.add_argument("--train_dataset" , type=SCREAMING_SNAKE_CASE , default="" ) parser.add_argument("--eval_dataset" , type=SCREAMING_SNAKE_CASE , default="" ) parser.add_argument("--seed" , type=SCREAMING_SNAKE_CASE , default=4_2 ) parser.add_argument("--num_train_epochs" , type=SCREAMING_SNAKE_CASE , default=3 ) parser.add_argument("--train_batch_size" , type=SCREAMING_SNAKE_CASE , default=8 ) parser.add_argument("--eval_batch_size" , type=SCREAMING_SNAKE_CASE , default=1_6 ) parser.add_argument("--adam_epsilon" , default=1E-8 , type=SCREAMING_SNAKE_CASE , help="Epsilon for Adam optimizer." ) parser.add_argument("--max_grad_norm" , type=SCREAMING_SNAKE_CASE , default=1 ) parser.add_argument( "--max_steps" , default=-1 , type=SCREAMING_SNAKE_CASE , help=( "If > 0: set total number of training steps to perform. Override num_train_epochs." ) , ) parser.add_argument( "--gradient_accumulation_steps" , type=SCREAMING_SNAKE_CASE , default=1 , help="Number of updates steps to accumulate before performing a backward/update pass." , ) parser.add_argument("--learning_rate" , type=SCREAMING_SNAKE_CASE , default=6.2_5E-5 ) parser.add_argument("--warmup_steps" , default=0 , type=SCREAMING_SNAKE_CASE , help="Linear warmup over warmup_steps." ) parser.add_argument("--lr_schedule" , type=SCREAMING_SNAKE_CASE , default="warmup_linear" ) parser.add_argument("--weight_decay" , type=SCREAMING_SNAKE_CASE , default=0.01 ) parser.add_argument("--lm_coef" , type=SCREAMING_SNAKE_CASE , default=0.9 ) parser.add_argument("--n_valid" , type=SCREAMING_SNAKE_CASE , default=3_7_4 ) parser.add_argument("--server_ip" , type=SCREAMING_SNAKE_CASE , default="" , help="Can be used for distant debugging." ) parser.add_argument("--server_port" , type=SCREAMING_SNAKE_CASE , default="" , help="Can be used for distant debugging." ) lowerCAmelCase : Tuple = parser.parse_args() print(SCREAMING_SNAKE_CASE ) 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=SCREAMING_SNAKE_CASE ) 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 ) lowerCAmelCase : Optional[int] = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) lowerCAmelCase : Optional[int] = torch.cuda.device_count() logger.info("device: {}, n_gpu {}".format(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) 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 lowerCAmelCase : str = ["_start_", "_delimiter_", "_classify_"] lowerCAmelCase : Dict = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(SCREAMING_SNAKE_CASE ) ) model.to(SCREAMING_SNAKE_CASE ) # Load and encode the datasets def tokenize_and_encode(SCREAMING_SNAKE_CASE : Optional[Any] ): if isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(SCREAMING_SNAKE_CASE ) ) elif isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return obj return [tokenize_and_encode(SCREAMING_SNAKE_CASE ) for o in obj] logger.info("Encoding dataset..." ) lowerCAmelCase : Optional[Any] = load_rocstories_dataset(args.train_dataset ) lowerCAmelCase : int = load_rocstories_dataset(args.eval_dataset ) lowerCAmelCase : Tuple = (train_dataset, eval_dataset) lowerCAmelCase : Dict = tokenize_and_encode(SCREAMING_SNAKE_CASE ) # Compute the max input length for the Transformer lowerCAmelCase : Any = model.config.n_positions // 2 - 2 lowerCAmelCase : int = 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 ) lowerCAmelCase : Union[str, Any] = min(SCREAMING_SNAKE_CASE , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders lowerCAmelCase : Any = pre_process_datasets(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , *SCREAMING_SNAKE_CASE ) lowerCAmelCase , lowerCAmelCase : Tuple = tensor_datasets[0], tensor_datasets[1] lowerCAmelCase : List[str] = TensorDataset(*SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = RandomSampler(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = DataLoader(SCREAMING_SNAKE_CASE , sampler=SCREAMING_SNAKE_CASE , batch_size=args.train_batch_size ) lowerCAmelCase : int = TensorDataset(*SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = SequentialSampler(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = DataLoader(SCREAMING_SNAKE_CASE , sampler=SCREAMING_SNAKE_CASE , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: lowerCAmelCase : int = args.max_steps lowerCAmelCase : str = args.max_steps // (len(SCREAMING_SNAKE_CASE ) // args.gradient_accumulation_steps) + 1 else: lowerCAmelCase : Union[str, Any] = len(SCREAMING_SNAKE_CASE ) // args.gradient_accumulation_steps * args.num_train_epochs lowerCAmelCase : Dict = list(model.named_parameters() ) lowerCAmelCase : str = ["bias", "LayerNorm.bias", "LayerNorm.weight"] lowerCAmelCase : Tuple = [ { "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}, ] lowerCAmelCase : Tuple = AdamW(SCREAMING_SNAKE_CASE , lr=args.learning_rate , eps=args.adam_epsilon ) lowerCAmelCase : str = get_linear_schedule_with_warmup( SCREAMING_SNAKE_CASE , num_warmup_steps=args.warmup_steps , num_training_steps=SCREAMING_SNAKE_CASE ) if args.do_train: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Dict = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="Epoch" ): lowerCAmelCase : Optional[int] = 0 lowerCAmelCase : Union[str, Any] = 0 lowerCAmelCase : Tuple = tqdm(SCREAMING_SNAKE_CASE , desc="Training" ) for step, batch in enumerate(SCREAMING_SNAKE_CASE ): lowerCAmelCase : Tuple = tuple(t.to(SCREAMING_SNAKE_CASE ) for t in batch ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : int = batch lowerCAmelCase : Optional[int] = model(SCREAMING_SNAKE_CASE , mc_token_ids=SCREAMING_SNAKE_CASE , lm_labels=SCREAMING_SNAKE_CASE , mc_labels=SCREAMING_SNAKE_CASE ) lowerCAmelCase : int = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() lowerCAmelCase : Optional[Any] = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 lowerCAmelCase : int = "Training loss: {:.2e} lr: {:.2e}".format(SCREAMING_SNAKE_CASE , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer lowerCAmelCase : Optional[int] = model.module if hasattr(SCREAMING_SNAKE_CASE , "module" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` lowerCAmelCase : Any = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE ) torch.save(model_to_save.state_dict() , SCREAMING_SNAKE_CASE ) model_to_save.config.to_json_file(SCREAMING_SNAKE_CASE ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned lowerCAmelCase : List[str] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) lowerCAmelCase : Dict = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(SCREAMING_SNAKE_CASE ) if args.do_eval: model.eval() lowerCAmelCase , lowerCAmelCase : Optional[int] = 0, 0 lowerCAmelCase , lowerCAmelCase : Any = 0, 0 for batch in tqdm(SCREAMING_SNAKE_CASE , desc="Evaluating" ): lowerCAmelCase : List[Any] = tuple(t.to(SCREAMING_SNAKE_CASE ) for t in batch ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Tuple = batch with torch.no_grad(): lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Optional[Any] = model( SCREAMING_SNAKE_CASE , mc_token_ids=SCREAMING_SNAKE_CASE , lm_labels=SCREAMING_SNAKE_CASE , mc_labels=SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = mc_logits.detach().cpu().numpy() lowerCAmelCase : List[str] = mc_labels.to("cpu" ).numpy() lowerCAmelCase : Any = accuracy(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 lowerCAmelCase : List[Any] = eval_loss / nb_eval_steps lowerCAmelCase : List[Any] = eval_accuracy / nb_eval_examples lowerCAmelCase : Tuple = tr_loss / nb_tr_steps if args.do_train else None lowerCAmelCase : Any = {"eval_loss": eval_loss, "eval_accuracy": eval_accuracy, "train_loss": train_loss} lowerCAmelCase : List[str] = os.path.join(args.output_dir , "eval_results.txt" ) with open(SCREAMING_SNAKE_CASE , "w" ) as writer: logger.info("***** Eval results *****" ) for key in sorted(result.keys() ): logger.info(" %s = %s" , SCREAMING_SNAKE_CASE , str(result[key] ) ) writer.write("%s = %s\n" % (key, str(result[key] )) ) if __name__ == "__main__": main()
681
0
"""simple docstring""" from __future__ import annotations def a__ ( SCREAMING_SNAKE_CASE : list[int] , SCREAMING_SNAKE_CASE : int ) -> Optional[int]: '''simple docstring''' if len(SCREAMING_SNAKE_CASE ) < k or k < 0: raise ValueError("Invalid Input" ) lowerCAmelCase : Optional[int] = sum(array[:k] ) for i in range(len(SCREAMING_SNAKE_CASE ) - k ): lowerCAmelCase : str = current_sum - array[i] + array[i + k] lowerCAmelCase : Optional[int] = max(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) return max_sum if __name__ == "__main__": from doctest import testmod from random import randint testmod() lowerCAmelCase__ = [randint(-1_000, 1_000) for i in range(100)] lowerCAmelCase__ = randint(0, 110) print(F"The maximum sum of {k} consecutive elements is {max_sum_in_array(array,k)}")
710
"""simple docstring""" from typing import List, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''huggingface/informer-tourism-monthly''': ( '''https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json''' ), # See all Informer models at https://huggingface.co/models?filter=informer } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Optional[Any] ="informer" a : int ={ "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", "num_hidden_layers": "encoder_layers", } def __init__( self , snake_case__ = None , snake_case__ = None , snake_case__ = "student_t" , snake_case__ = "nll" , snake_case__ = 1 , snake_case__ = None , snake_case__ = "mean" , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = 0 , snake_case__ = None , snake_case__ = None , snake_case__ = 64 , snake_case__ = 32 , snake_case__ = 32 , snake_case__ = 2 , snake_case__ = 2 , snake_case__ = 2 , snake_case__ = 2 , snake_case__ = True , snake_case__ = "gelu" , snake_case__ = 0.05 , snake_case__ = 0.1 , snake_case__ = 0.1 , snake_case__ = 0.1 , snake_case__ = 0.1 , snake_case__ = 100 , snake_case__ = 0.02 , snake_case__=True , snake_case__ = "prob" , snake_case__ = 5 , snake_case__ = True , **snake_case__ , ): """simple docstring""" lowerCAmelCase : Union[str, Any] = prediction_length lowerCAmelCase : Union[str, Any] = context_length or prediction_length lowerCAmelCase : List[Any] = distribution_output lowerCAmelCase : Optional[int] = loss lowerCAmelCase : Optional[int] = input_size lowerCAmelCase : str = num_time_features lowerCAmelCase : Any = lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7] lowerCAmelCase : Dict = scaling lowerCAmelCase : List[str] = num_dynamic_real_features lowerCAmelCase : Dict = num_static_real_features lowerCAmelCase : Dict = num_static_categorical_features # set cardinality if cardinality and num_static_categorical_features > 0: if len(snake_case__ ) != num_static_categorical_features: raise ValueError( "The cardinality should be a list of the same length as `num_static_categorical_features`" ) lowerCAmelCase : List[str] = cardinality else: lowerCAmelCase : Optional[Any] = [0] # set embedding_dimension if embedding_dimension and num_static_categorical_features > 0: if len(snake_case__ ) != num_static_categorical_features: raise ValueError( "The embedding dimension should be a list of the same length as `num_static_categorical_features`" ) lowerCAmelCase : List[Any] = embedding_dimension else: lowerCAmelCase : Dict = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] lowerCAmelCase : List[Any] = num_parallel_samples # Transformer architecture configuration lowerCAmelCase : Any = input_size * len(self.lags_sequence ) + self._number_of_features lowerCAmelCase : str = d_model lowerCAmelCase : List[str] = encoder_attention_heads lowerCAmelCase : int = decoder_attention_heads lowerCAmelCase : Optional[Any] = encoder_ffn_dim lowerCAmelCase : Dict = decoder_ffn_dim lowerCAmelCase : int = encoder_layers lowerCAmelCase : Union[str, Any] = decoder_layers lowerCAmelCase : Tuple = dropout lowerCAmelCase : List[Any] = attention_dropout lowerCAmelCase : int = activation_dropout lowerCAmelCase : Union[str, Any] = encoder_layerdrop lowerCAmelCase : int = decoder_layerdrop lowerCAmelCase : Optional[int] = activation_function lowerCAmelCase : int = init_std lowerCAmelCase : Optional[Any] = use_cache # Informer lowerCAmelCase : Dict = attention_type lowerCAmelCase : Any = sampling_factor lowerCAmelCase : Optional[int] = distil super().__init__(is_encoder_decoder=snake_case__ , **snake_case__ ) @property def lowercase__ ( self ): """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 )
681
0
"""simple docstring""" 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 ) lowerCAmelCase__ = logging.getLogger(__name__) def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' lowerCAmelCase : Optional[Any] = np.argmax(SCREAMING_SNAKE_CASE , axis=1 ) return np.sum(outputs == labels ) def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' with open(SCREAMING_SNAKE_CASE , encoding="utf_8" ) as f: lowerCAmelCase : Tuple = csv.reader(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = [] next(SCREAMING_SNAKE_CASE ) # skip the first line for line in tqdm(SCREAMING_SNAKE_CASE ): output.append((" ".join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : str ): '''simple docstring''' lowerCAmelCase : List[Any] = [] for dataset in encoded_datasets: lowerCAmelCase : Dict = len(SCREAMING_SNAKE_CASE ) lowerCAmelCase : str = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) lowerCAmelCase : Tuple = np.zeros((n_batch, 2) , dtype=np.intaa ) lowerCAmelCase : int = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) lowerCAmelCase : List[Any] = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(SCREAMING_SNAKE_CASE ): lowerCAmelCase : int = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase : Union[str, Any] = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] lowerCAmelCase : Tuple = with_conta lowerCAmelCase : Any = with_conta lowerCAmelCase : Optional[Any] = len(SCREAMING_SNAKE_CASE ) - 1 lowerCAmelCase : Dict = len(SCREAMING_SNAKE_CASE ) - 1 lowerCAmelCase : Optional[Any] = with_conta lowerCAmelCase : List[Any] = with_conta lowerCAmelCase : str = mc_label lowerCAmelCase : Dict = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(SCREAMING_SNAKE_CASE ) for t in all_inputs ) ) return tensor_datasets def a__ ( ): '''simple docstring''' lowerCAmelCase : Optional[int] = argparse.ArgumentParser() parser.add_argument("--model_name" , type=SCREAMING_SNAKE_CASE , 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=SCREAMING_SNAKE_CASE , type=SCREAMING_SNAKE_CASE , required=SCREAMING_SNAKE_CASE , help="The output directory where the model predictions and checkpoints will be written." , ) parser.add_argument("--train_dataset" , type=SCREAMING_SNAKE_CASE , default="" ) parser.add_argument("--eval_dataset" , type=SCREAMING_SNAKE_CASE , default="" ) parser.add_argument("--seed" , type=SCREAMING_SNAKE_CASE , default=4_2 ) parser.add_argument("--num_train_epochs" , type=SCREAMING_SNAKE_CASE , default=3 ) parser.add_argument("--train_batch_size" , type=SCREAMING_SNAKE_CASE , default=8 ) parser.add_argument("--eval_batch_size" , type=SCREAMING_SNAKE_CASE , default=1_6 ) parser.add_argument("--adam_epsilon" , default=1E-8 , type=SCREAMING_SNAKE_CASE , help="Epsilon for Adam optimizer." ) parser.add_argument("--max_grad_norm" , type=SCREAMING_SNAKE_CASE , default=1 ) parser.add_argument( "--max_steps" , default=-1 , type=SCREAMING_SNAKE_CASE , help=( "If > 0: set total number of training steps to perform. Override num_train_epochs." ) , ) parser.add_argument( "--gradient_accumulation_steps" , type=SCREAMING_SNAKE_CASE , default=1 , help="Number of updates steps to accumulate before performing a backward/update pass." , ) parser.add_argument("--learning_rate" , type=SCREAMING_SNAKE_CASE , default=6.2_5E-5 ) parser.add_argument("--warmup_steps" , default=0 , type=SCREAMING_SNAKE_CASE , help="Linear warmup over warmup_steps." ) parser.add_argument("--lr_schedule" , type=SCREAMING_SNAKE_CASE , default="warmup_linear" ) parser.add_argument("--weight_decay" , type=SCREAMING_SNAKE_CASE , default=0.01 ) parser.add_argument("--lm_coef" , type=SCREAMING_SNAKE_CASE , default=0.9 ) parser.add_argument("--n_valid" , type=SCREAMING_SNAKE_CASE , default=3_7_4 ) parser.add_argument("--server_ip" , type=SCREAMING_SNAKE_CASE , default="" , help="Can be used for distant debugging." ) parser.add_argument("--server_port" , type=SCREAMING_SNAKE_CASE , default="" , help="Can be used for distant debugging." ) lowerCAmelCase : Tuple = parser.parse_args() print(SCREAMING_SNAKE_CASE ) 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=SCREAMING_SNAKE_CASE ) 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 ) lowerCAmelCase : Optional[int] = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) lowerCAmelCase : Optional[int] = torch.cuda.device_count() logger.info("device: {}, n_gpu {}".format(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) 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 lowerCAmelCase : str = ["_start_", "_delimiter_", "_classify_"] lowerCAmelCase : Dict = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(SCREAMING_SNAKE_CASE ) ) model.to(SCREAMING_SNAKE_CASE ) # Load and encode the datasets def tokenize_and_encode(SCREAMING_SNAKE_CASE : Optional[Any] ): if isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(SCREAMING_SNAKE_CASE ) ) elif isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return obj return [tokenize_and_encode(SCREAMING_SNAKE_CASE ) for o in obj] logger.info("Encoding dataset..." ) lowerCAmelCase : Optional[Any] = load_rocstories_dataset(args.train_dataset ) lowerCAmelCase : int = load_rocstories_dataset(args.eval_dataset ) lowerCAmelCase : Tuple = (train_dataset, eval_dataset) lowerCAmelCase : Dict = tokenize_and_encode(SCREAMING_SNAKE_CASE ) # Compute the max input length for the Transformer lowerCAmelCase : Any = model.config.n_positions // 2 - 2 lowerCAmelCase : int = 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 ) lowerCAmelCase : Union[str, Any] = min(SCREAMING_SNAKE_CASE , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders lowerCAmelCase : Any = pre_process_datasets(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , *SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = tensor_datasets[0], tensor_datasets[1] lowerCAmelCase : List[str] = TensorDataset(*SCREAMING_SNAKE_CASE ) lowerCAmelCase : Union[str, Any] = RandomSampler(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = DataLoader(SCREAMING_SNAKE_CASE , sampler=SCREAMING_SNAKE_CASE , batch_size=args.train_batch_size ) lowerCAmelCase : int = TensorDataset(*SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = SequentialSampler(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Tuple = DataLoader(SCREAMING_SNAKE_CASE , sampler=SCREAMING_SNAKE_CASE , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: lowerCAmelCase : int = args.max_steps lowerCAmelCase : str = args.max_steps // (len(SCREAMING_SNAKE_CASE ) // args.gradient_accumulation_steps) + 1 else: lowerCAmelCase : Union[str, Any] = len(SCREAMING_SNAKE_CASE ) // args.gradient_accumulation_steps * args.num_train_epochs lowerCAmelCase : Dict = list(model.named_parameters() ) lowerCAmelCase : str = ["bias", "LayerNorm.bias", "LayerNorm.weight"] lowerCAmelCase : Tuple = [ { "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}, ] lowerCAmelCase : Tuple = AdamW(SCREAMING_SNAKE_CASE , lr=args.learning_rate , eps=args.adam_epsilon ) lowerCAmelCase : str = get_linear_schedule_with_warmup( SCREAMING_SNAKE_CASE , num_warmup_steps=args.warmup_steps , num_training_steps=SCREAMING_SNAKE_CASE ) if args.do_train: lowerCAmelCase : Dict = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="Epoch" ): lowerCAmelCase : Optional[int] = 0 lowerCAmelCase : Union[str, Any] = 0 lowerCAmelCase : Tuple = tqdm(SCREAMING_SNAKE_CASE , desc="Training" ) for step, batch in enumerate(SCREAMING_SNAKE_CASE ): lowerCAmelCase : Tuple = tuple(t.to(SCREAMING_SNAKE_CASE ) for t in batch ) lowerCAmelCase : int = batch lowerCAmelCase : Optional[int] = model(SCREAMING_SNAKE_CASE , mc_token_ids=SCREAMING_SNAKE_CASE , lm_labels=SCREAMING_SNAKE_CASE , mc_labels=SCREAMING_SNAKE_CASE ) lowerCAmelCase : int = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() lowerCAmelCase : Optional[Any] = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 lowerCAmelCase : int = "Training loss: {:.2e} lr: {:.2e}".format(SCREAMING_SNAKE_CASE , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer lowerCAmelCase : Optional[int] = model.module if hasattr(SCREAMING_SNAKE_CASE , "module" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` lowerCAmelCase : Any = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = os.path.join(args.output_dir , SCREAMING_SNAKE_CASE ) torch.save(model_to_save.state_dict() , SCREAMING_SNAKE_CASE ) model_to_save.config.to_json_file(SCREAMING_SNAKE_CASE ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned lowerCAmelCase : List[str] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) lowerCAmelCase : Dict = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(SCREAMING_SNAKE_CASE ) if args.do_eval: model.eval() lowerCAmelCase : Optional[int] = 0, 0 lowerCAmelCase : Any = 0, 0 for batch in tqdm(SCREAMING_SNAKE_CASE , desc="Evaluating" ): lowerCAmelCase : List[Any] = tuple(t.to(SCREAMING_SNAKE_CASE ) for t in batch ) lowerCAmelCase : Tuple = batch with torch.no_grad(): lowerCAmelCase : Optional[Any] = model( SCREAMING_SNAKE_CASE , mc_token_ids=SCREAMING_SNAKE_CASE , lm_labels=SCREAMING_SNAKE_CASE , mc_labels=SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = mc_logits.detach().cpu().numpy() lowerCAmelCase : List[str] = mc_labels.to("cpu" ).numpy() lowerCAmelCase : Any = accuracy(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 lowerCAmelCase : List[Any] = eval_loss / nb_eval_steps lowerCAmelCase : List[Any] = eval_accuracy / nb_eval_examples lowerCAmelCase : Tuple = tr_loss / nb_tr_steps if args.do_train else None lowerCAmelCase : Any = {"eval_loss": eval_loss, "eval_accuracy": eval_accuracy, "train_loss": train_loss} lowerCAmelCase : List[str] = os.path.join(args.output_dir , "eval_results.txt" ) with open(SCREAMING_SNAKE_CASE , "w" ) as writer: logger.info("***** Eval results *****" ) for key in sorted(result.keys() ): logger.info(" %s = %s" , SCREAMING_SNAKE_CASE , str(result[key] ) ) writer.write("%s = %s\n" % (key, str(result[key] )) ) if __name__ == "__main__": main()
711
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' if num < 0: return False lowerCAmelCase : int = num lowerCAmelCase : int = 0 while num > 0: lowerCAmelCase : Dict = rev_num * 1_0 + (num % 1_0) num //= 1_0 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
681
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase__ = { '''configuration_pegasus_x''': ['''PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''PegasusXConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST''', '''PegasusXForConditionalGeneration''', '''PegasusXModel''', '''PegasusXPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_pegasus_x import PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusXConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_pegasus_x import ( PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST, PegasusXForConditionalGeneration, PegasusXModel, PegasusXPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
712
"""simple docstring""" import os import re import shutil import sys import tempfile import unittest import black lowerCAmelCase__ = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, '''utils''')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If DDPMSchedulerOutput is changed in scheduling_ddpm.py, this code needs to be manually updated. lowerCAmelCase__ = ''' \""" Output class for the scheduler\'s step function output. Args: prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the denoising loop. pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): The predicted denoised sample (x_{0}) based on the model output from the current timestep. `pred_original_sample` can be used to preview progress or for guidance. \""" prev_sample: torch.FloatTensor pred_original_sample: Optional[torch.FloatTensor] = None ''' class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = tempfile.mkdtemp() os.makedirs(os.path.join(self.diffusers_dir , "schedulers/" ) ) lowerCAmelCase : List[str] = self.diffusers_dir shutil.copy( os.path.join(snake_case__ , "src/diffusers/schedulers/scheduling_ddpm.py" ) , os.path.join(self.diffusers_dir , "schedulers/scheduling_ddpm.py" ) , ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = "src/diffusers" shutil.rmtree(self.diffusers_dir ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ , snake_case__=None ): """simple docstring""" lowerCAmelCase : Union[str, Any] = comment + f"""\nclass {class_name}(nn.Module):\n""" + class_code if overwrite_result is not None: lowerCAmelCase : str = comment + f"""\nclass {class_name}(nn.Module):\n""" + overwrite_result lowerCAmelCase : int = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) lowerCAmelCase : int = black.format_str(snake_case__ , mode=snake_case__ ) lowerCAmelCase : Dict = os.path.join(self.diffusers_dir , "new_code.py" ) with open(snake_case__ , "w" , newline="\n" ) as f: f.write(snake_case__ ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(snake_case__ ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=snake_case__ ) with open(snake_case__ , "r" ) as f: self.assertTrue(f.read() , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = check_copies.find_code_in_diffusers("schedulers.scheduling_ddpm.DDPMSchedulerOutput" ) self.assertEqual(snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput" , "DDPMSchedulerOutput" , REFERENCE_CODE + "\n" , ) # With no empty line at the end self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput" , "DDPMSchedulerOutput" , snake_case__ , ) # Copy consistency with rename self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test" , "TestSchedulerOutput" , re.sub("DDPM" , "Test" , snake_case__ ) , ) # Copy consistency with a really long name lowerCAmelCase : Union[str, Any] = "TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason" self.check_copy_consistency( f"""# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}""" , f"""{long_class_name}SchedulerOutput""" , re.sub("Bert" , snake_case__ , snake_case__ ) , ) # Copy consistency with overwrite self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test" , "TestSchedulerOutput" , snake_case__ , overwrite_result=re.sub("DDPM" , "Test" , snake_case__ ) , )
681
0
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : Optional[int] = len(SCREAMING_SNAKE_CASE ) while cur > 1: # Find the maximum number in arr lowerCAmelCase : List[str] = arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi lowerCAmelCase : str = arr[mi::-1] + arr[mi + 1 : len(SCREAMING_SNAKE_CASE )] # Reverse whole list lowerCAmelCase : str = arr[cur - 1 :: -1] + arr[cur : len(SCREAMING_SNAKE_CASE )] cur -= 1 return arr if __name__ == "__main__": lowerCAmelCase__ = input('''Enter numbers separated by a comma:\n''').strip() lowerCAmelCase__ = [int(item) for item in user_input.split(''',''')] print(pancake_sort(unsorted))
713
"""simple docstring""" import re from flax.core.frozen_dict import freeze from flax.traverse_util import flatten_dict, unflatten_dict from jax.experimental import PartitionSpec as P # Sentinels lowerCAmelCase__ = object() # For specifying empty leaf dict `{}` lowerCAmelCase__ = object() def a__ ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' lowerCAmelCase : Optional[int] = tuple((re.compile(x + "$" ) for x in qs) ) for i in range(len(SCREAMING_SNAKE_CASE ) - len(SCREAMING_SNAKE_CASE ) + 1 ): lowerCAmelCase : int = [x.match(SCREAMING_SNAKE_CASE ) for x, y in zip(SCREAMING_SNAKE_CASE , ks[i:] )] if matches and all(SCREAMING_SNAKE_CASE ): return True return False def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' def replace(SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Dict ): for rule, replacement in rules: if _match(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return replacement return val return replace def a__ ( ): '''simple docstring''' return [ # embeddings (("transformer", "wpe", "embedding"), P("mp" , SCREAMING_SNAKE_CASE )), (("transformer", "wte", "embedding"), P("mp" , SCREAMING_SNAKE_CASE )), # atention (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(SCREAMING_SNAKE_CASE , "mp" )), (("attention", "out_proj", "kernel"), P("mp" , SCREAMING_SNAKE_CASE )), (("attention", "out_proj", "bias"), None), # mlp (("mlp", "c_fc", "kernel"), P(SCREAMING_SNAKE_CASE , "mp" )), (("mlp", "c_fc", "bias"), P("mp" )), (("mlp", "c_proj", "kernel"), P("mp" , SCREAMING_SNAKE_CASE )), (("mlp", "c_proj", "bias"), None), # layer norms ((r"ln_\d+", "bias"), None), ((r"\d+", r"ln_\d+", "scale"), None), (("ln_f", "bias"), None), (("ln_f", "scale"), None), ] def a__ ( SCREAMING_SNAKE_CASE : Union[str, Any] ): '''simple docstring''' lowerCAmelCase : Any = _get_partition_rules() lowerCAmelCase : Tuple = _replacement_rules(SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = {k: _unmatched for k in flatten_dict(SCREAMING_SNAKE_CASE )} lowerCAmelCase : List[Any] = {k: replace(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) for k, v in initd.items()} assert _unmatched not in result.values(), "Incomplete partition spec." return freeze(unflatten_dict(SCREAMING_SNAKE_CASE ) )
681
0
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class SCREAMING_SNAKE_CASE__ ( lowercase , lowercase , lowercase , unittest.TestCase ): """simple docstring""" a : List[Any] =AltDiffusionPipeline a : int =TEXT_TO_IMAGE_PARAMS a : int =TEXT_TO_IMAGE_BATCH_PARAMS a : Union[str, Any] =TEXT_TO_IMAGE_IMAGE_PARAMS a : Dict =TEXT_TO_IMAGE_IMAGE_PARAMS def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCAmelCase : int = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("DownBlock2D", "CrossAttnDownBlock2D") , up_block_types=("CrossAttnUpBlock2D", "UpBlock2D") , cross_attention_dim=32 , ) lowerCAmelCase : Optional[Any] = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule="scaled_linear" , clip_sample=snake_case__ , set_alpha_to_one=snake_case__ , ) torch.manual_seed(0 ) lowerCAmelCase : int = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , ) # TODO: address the non-deterministic text encoder (fails for save-load tests) # torch.manual_seed(0) # text_encoder_config = RobertaSeriesConfig( # hidden_size=32, # project_dim=32, # intermediate_size=37, # layer_norm_eps=1e-05, # num_attention_heads=4, # num_hidden_layers=5, # vocab_size=5002, # ) # text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config) torch.manual_seed(0 ) lowerCAmelCase : Optional[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5_002 , ) lowerCAmelCase : List[str] = CLIPTextModel(snake_case__ ) lowerCAmelCase : List[Any] = XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta" ) lowerCAmelCase : Optional[Any] = 77 lowerCAmelCase : List[Any] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def lowercase__ ( self , snake_case__ , snake_case__=0 ): """simple docstring""" if str(snake_case__ ).startswith("mps" ): lowerCAmelCase : int = torch.manual_seed(snake_case__ ) else: lowerCAmelCase : Dict = torch.Generator(device=snake_case__ ).manual_seed(snake_case__ ) lowerCAmelCase : Optional[int] = { "prompt": "A painting of a squirrel eating a burger", "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "output_type": "numpy", } return inputs def lowercase__ ( self ): """simple docstring""" super().test_attention_slicing_forward_pass(expected_max_diff=3e-3 ) def lowercase__ ( self ): """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[Any] = "cpu" # ensure determinism for the device-dependent torch.Generator lowerCAmelCase : List[str] = self.get_dummy_components() torch.manual_seed(0 ) lowerCAmelCase : Union[str, Any] = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5_002 , ) # TODO: remove after fixing the non-deterministic text encoder lowerCAmelCase : int = RobertaSeriesModelWithTransformation(snake_case__ ) lowerCAmelCase : Optional[int] = text_encoder lowerCAmelCase : str = AltDiffusionPipeline(**snake_case__ ) lowerCAmelCase : Any = alt_pipe.to(snake_case__ ) alt_pipe.set_progress_bar_config(disable=snake_case__ ) lowerCAmelCase : str = self.get_dummy_inputs(snake_case__ ) lowerCAmelCase : Tuple = "A photo of an astronaut" lowerCAmelCase : List[Any] = alt_pipe(**snake_case__ ) lowerCAmelCase : Union[str, Any] = output.images lowerCAmelCase : List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase : int = np.array( [0.5748162, 0.60447145, 0.48821217, 0.50100636, 0.5431185, 0.45763683, 0.49657696, 0.48132733, 0.47573093] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = "cpu" # ensure determinism for the device-dependent torch.Generator lowerCAmelCase : List[Any] = self.get_dummy_components() lowerCAmelCase : int = PNDMScheduler(skip_prk_steps=snake_case__ ) torch.manual_seed(0 ) lowerCAmelCase : Optional[Any] = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5_002 , ) # TODO: remove after fixing the non-deterministic text encoder lowerCAmelCase : Union[str, Any] = RobertaSeriesModelWithTransformation(snake_case__ ) lowerCAmelCase : Union[str, Any] = text_encoder lowerCAmelCase : Any = AltDiffusionPipeline(**snake_case__ ) lowerCAmelCase : Any = alt_pipe.to(snake_case__ ) alt_pipe.set_progress_bar_config(disable=snake_case__ ) lowerCAmelCase : Any = self.get_dummy_inputs(snake_case__ ) lowerCAmelCase : List[str] = alt_pipe(**snake_case__ ) lowerCAmelCase : Optional[int] = output.images lowerCAmelCase : List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase : Union[str, Any] = np.array( [0.51605093, 0.5707241, 0.47365507, 0.50578886, 0.5633877, 0.4642503, 0.5182081, 0.48763484, 0.49084237] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = AltDiffusionPipeline.from_pretrained("BAAI/AltDiffusion" , safety_checker=snake_case__ ) lowerCAmelCase : Union[str, Any] = alt_pipe.to(snake_case__ ) alt_pipe.set_progress_bar_config(disable=snake_case__ ) lowerCAmelCase : Tuple = "A painting of a squirrel eating a burger" lowerCAmelCase : Optional[int] = torch.manual_seed(0 ) lowerCAmelCase : str = alt_pipe([prompt] , generator=snake_case__ , guidance_scale=6.0 , num_inference_steps=20 , output_type="np" ) lowerCAmelCase : Union[str, Any] = output.images lowerCAmelCase : List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowerCAmelCase : str = np.array([0.1010, 0.0800, 0.0794, 0.0885, 0.0843, 0.0762, 0.0769, 0.0729, 0.0586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = DDIMScheduler.from_pretrained("BAAI/AltDiffusion" , subfolder="scheduler" ) lowerCAmelCase : Tuple = AltDiffusionPipeline.from_pretrained("BAAI/AltDiffusion" , scheduler=snake_case__ , safety_checker=snake_case__ ) lowerCAmelCase : Any = alt_pipe.to(snake_case__ ) alt_pipe.set_progress_bar_config(disable=snake_case__ ) lowerCAmelCase : List[str] = "A painting of a squirrel eating a burger" lowerCAmelCase : Dict = torch.manual_seed(0 ) lowerCAmelCase : Tuple = alt_pipe([prompt] , generator=snake_case__ , num_inference_steps=2 , output_type="numpy" ) lowerCAmelCase : Tuple = output.images lowerCAmelCase : str = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowerCAmelCase : Optional[Any] = np.array([0.4019, 0.4052, 0.3810, 0.4119, 0.3916, 0.3982, 0.4651, 0.4195, 0.5323] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
714
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : int = 1_0 , SCREAMING_SNAKE_CASE : int = 2_2 ): '''simple docstring''' lowerCAmelCase : Dict = range(1 , SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[str] = range(1 , SCREAMING_SNAKE_CASE ) return sum( 1 for power in powers for base in bases if len(str(base**power ) ) == power ) if __name__ == "__main__": print(F"{solution(10, 22) = }")
681
0
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('''>=''', '''4.25.0''')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ( VersatileDiffusionDualGuidedPipeline, VersatileDiffusionImageVariationPipeline, VersatileDiffusionPipeline, VersatileDiffusionTextToImagePipeline, ) else: from .modeling_text_unet import UNetFlatConditionModel from .pipeline_versatile_diffusion import VersatileDiffusionPipeline from .pipeline_versatile_diffusion_dual_guided import VersatileDiffusionDualGuidedPipeline from .pipeline_versatile_diffusion_image_variation import VersatileDiffusionImageVariationPipeline from .pipeline_versatile_diffusion_text_to_image import VersatileDiffusionTextToImagePipeline
715
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' lowerCAmelCase : Optional[int] = len(SCREAMING_SNAKE_CASE ) while cur > 1: # Find the maximum number in arr lowerCAmelCase : List[str] = arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi lowerCAmelCase : str = arr[mi::-1] + arr[mi + 1 : len(SCREAMING_SNAKE_CASE )] # Reverse whole list lowerCAmelCase : str = arr[cur - 1 :: -1] + arr[cur : len(SCREAMING_SNAKE_CASE )] cur -= 1 return arr if __name__ == "__main__": lowerCAmelCase__ = input('''Enter numbers separated by a comma:\n''').strip() lowerCAmelCase__ = [int(item) for item in user_input.split(''',''')] print(pancake_sort(unsorted))
681
0
"""simple docstring""" from collections import OrderedDict from typing import List, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''google/efficientnet-b7''': '''https://huggingface.co/google/efficientnet-b7/resolve/main/config.json''', } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : int ="efficientnet" def __init__( self , snake_case__ = 3 , snake_case__ = 600 , snake_case__ = 2.0 , snake_case__ = 3.1 , snake_case__ = 8 , snake_case__ = [3, 3, 5, 3, 5, 5, 3] , snake_case__ = [32, 16, 24, 40, 80, 112, 192] , snake_case__ = [16, 24, 40, 80, 112, 192, 320] , snake_case__ = [] , snake_case__ = [1, 2, 2, 2, 1, 2, 1] , snake_case__ = [1, 2, 2, 3, 3, 4, 1] , snake_case__ = [1, 6, 6, 6, 6, 6, 6] , snake_case__ = 0.25 , snake_case__ = "swish" , snake_case__ = 2_560 , snake_case__ = "mean" , snake_case__ = 0.02 , snake_case__ = 0.001 , snake_case__ = 0.99 , snake_case__ = 0.5 , snake_case__ = 0.2 , **snake_case__ , ): """simple docstring""" super().__init__(**snake_case__ ) lowerCAmelCase : str = num_channels lowerCAmelCase : Dict = image_size lowerCAmelCase : Optional[int] = width_coefficient lowerCAmelCase : Optional[Any] = depth_coefficient lowerCAmelCase : Tuple = depth_divisor lowerCAmelCase : Any = kernel_sizes lowerCAmelCase : Optional[int] = in_channels lowerCAmelCase : List[Any] = out_channels lowerCAmelCase : int = depthwise_padding lowerCAmelCase : Union[str, Any] = strides lowerCAmelCase : Union[str, Any] = num_block_repeats lowerCAmelCase : Optional[Any] = expand_ratios lowerCAmelCase : int = squeeze_expansion_ratio lowerCAmelCase : List[str] = hidden_act lowerCAmelCase : Optional[Any] = hidden_dim lowerCAmelCase : Tuple = pooling_type lowerCAmelCase : str = initializer_range lowerCAmelCase : Union[str, Any] = batch_norm_eps lowerCAmelCase : Dict = batch_norm_momentum lowerCAmelCase : str = dropout_rate lowerCAmelCase : Dict = drop_connect_rate lowerCAmelCase : Optional[int] = sum(snake_case__ ) * 4 class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Tuple =version.parse("1.11" ) @property def lowercase__ ( self ): """simple docstring""" return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowercase__ ( self ): """simple docstring""" return 1e-5
716
"""simple docstring""" from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
681
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowerCAmelCase__ = { '''configuration_tapas''': ['''TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TapasConfig'''], '''tokenization_tapas''': ['''TapasTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TapasForMaskedLM''', '''TapasForQuestionAnswering''', '''TapasForSequenceClassification''', '''TapasModel''', '''TapasPreTrainedModel''', '''load_tf_weights_in_tapas''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFTapasForMaskedLM''', '''TFTapasForQuestionAnswering''', '''TFTapasForSequenceClassification''', '''TFTapasModel''', '''TFTapasPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_tapas import TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP, TapasConfig from .tokenization_tapas import TapasTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tapas import ( TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasPreTrainedModel, load_tf_weights_in_tapas, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_tapas import ( TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TFTapasForMaskedLM, TFTapasForQuestionAnswering, TFTapasForSequenceClassification, TFTapasModel, TFTapasPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
717
"""simple docstring""" import math def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' return math.sqrt(SCREAMING_SNAKE_CASE ) * math.sqrt(SCREAMING_SNAKE_CASE ) == num def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' lowerCAmelCase : Dict = 0 lowerCAmelCase : List[str] = n while left <= right: lowerCAmelCase : str = (left + right) // 2 if mid**2 == n: return True elif mid**2 > n: lowerCAmelCase : int = mid - 1 else: lowerCAmelCase : int = mid + 1 return False if __name__ == "__main__": import doctest doctest.testmod()
681
0
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''google/vit-base-patch16-224''': '''https://huggingface.co/vit-base-patch16-224/resolve/main/config.json''', # See all ViT models at https://huggingface.co/models?filter=vit } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Union[str, Any] ="vit" def __init__( self , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3_072 , snake_case__="gelu" , snake_case__=0.0 , snake_case__=0.0 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__=224 , snake_case__=16 , snake_case__=3 , snake_case__=True , snake_case__=16 , **snake_case__ , ): """simple docstring""" super().__init__(**snake_case__ ) lowerCAmelCase : Optional[Any] = hidden_size lowerCAmelCase : List[Any] = num_hidden_layers lowerCAmelCase : List[Any] = num_attention_heads lowerCAmelCase : Union[str, Any] = intermediate_size lowerCAmelCase : Any = hidden_act lowerCAmelCase : Optional[Any] = hidden_dropout_prob lowerCAmelCase : Optional[Any] = attention_probs_dropout_prob lowerCAmelCase : List[str] = initializer_range lowerCAmelCase : Optional[Any] = layer_norm_eps lowerCAmelCase : Optional[int] = image_size lowerCAmelCase : Optional[Any] = patch_size lowerCAmelCase : Tuple = num_channels lowerCAmelCase : Optional[int] = qkv_bias lowerCAmelCase : str = encoder_stride class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : List[Any] =version.parse("1.11" ) @property def lowercase__ ( self ): """simple docstring""" return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowercase__ ( self ): """simple docstring""" return 1e-4
718
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''google/vit-base-patch16-224''': '''https://huggingface.co/vit-base-patch16-224/resolve/main/config.json''', # See all ViT models at https://huggingface.co/models?filter=vit } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Union[str, Any] ="vit" def __init__( self , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3_072 , snake_case__="gelu" , snake_case__=0.0 , snake_case__=0.0 , snake_case__=0.02 , snake_case__=1e-12 , snake_case__=224 , snake_case__=16 , snake_case__=3 , snake_case__=True , snake_case__=16 , **snake_case__ , ): """simple docstring""" super().__init__(**snake_case__ ) lowerCAmelCase : Optional[Any] = hidden_size lowerCAmelCase : List[Any] = num_hidden_layers lowerCAmelCase : List[Any] = num_attention_heads lowerCAmelCase : Union[str, Any] = intermediate_size lowerCAmelCase : Any = hidden_act lowerCAmelCase : Optional[Any] = hidden_dropout_prob lowerCAmelCase : Optional[Any] = attention_probs_dropout_prob lowerCAmelCase : List[str] = initializer_range lowerCAmelCase : Optional[Any] = layer_norm_eps lowerCAmelCase : Optional[int] = image_size lowerCAmelCase : Optional[Any] = patch_size lowerCAmelCase : Tuple = num_channels lowerCAmelCase : Optional[int] = qkv_bias lowerCAmelCase : str = encoder_stride class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : List[Any] =version.parse("1.11" ) @property def lowercase__ ( self ): """simple docstring""" return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowercase__ ( self ): """simple docstring""" return 1e-4
681
0
"""simple docstring""" from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow if is_tf_available(): import tensorflow as tf from transformers import AutoTokenizer, TFAutoModelForSeqaSeqLM @require_tf @require_sentencepiece @require_tokenizers class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" @slow def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = TFAutoModelForSeqaSeqLM.from_pretrained("google/mt5-small" ) lowerCAmelCase : str = AutoTokenizer.from_pretrained("google/mt5-small" ) lowerCAmelCase : Tuple = tokenizer("Hello there" , return_tensors="tf" ).input_ids lowerCAmelCase : Any = tokenizer("Hi I am" , return_tensors="tf" ).input_ids lowerCAmelCase : List[str] = model(snake_case__ , labels=snake_case__ ).loss lowerCAmelCase : Union[str, Any] = -tf.math.reduce_mean(snake_case__ ).numpy() lowerCAmelCase : Dict = -21.228168 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 2e-4 )
719
"""simple docstring""" from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels
681
0
"""simple docstring""" import logging import math import os from dataclasses import dataclass, field from glob import glob from typing import Optional from torch.utils.data import ConcatDataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_WITH_LM_HEAD_MAPPING, AutoConfig, AutoModelWithLMHead, AutoTokenizer, DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForWholeWordMask, HfArgumentParser, LineByLineTextDataset, LineByLineWithRefDataset, PreTrainedTokenizer, TextDataset, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process lowerCAmelCase__ = logging.getLogger(__name__) lowerCAmelCase__ = list(MODEL_WITH_LM_HEAD_MAPPING.keys()) lowerCAmelCase__ = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class SCREAMING_SNAKE_CASE__ : """simple docstring""" a : Optional[str] =field( default=lowercase , metadata={ "help": ( "The model checkpoint for weights initialization. Leave None if you want to train a model from" " scratch." ) } , ) a : Optional[str] =field( default=lowercase , metadata={"help": "If training from scratch, pass a model type from the list: " + ", ".join(lowercase )} , ) a : Optional[str] =field( default=lowercase , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) a : Optional[str] =field( default=lowercase , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) a : Optional[str] =field( default=lowercase , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) @dataclass class SCREAMING_SNAKE_CASE__ : """simple docstring""" a : Optional[str] =field( default=lowercase , metadata={"help": "The input training data file (a text file)."} ) a : Optional[str] =field( default=lowercase , metadata={ "help": ( "The input training data files (multiple files in glob format). " "Very often splitting large files to smaller files can prevent tokenizer going out of memory" ) } , ) a : Optional[str] =field( default=lowercase , metadata={"help": "An optional input evaluation data file to evaluate the perplexity on (a text file)."} , ) a : Optional[str] =field( default=lowercase , metadata={"help": "An optional input train ref data file for whole word mask in Chinese."} , ) a : Optional[str] =field( default=lowercase , metadata={"help": "An optional input eval ref data file for whole word mask in Chinese."} , ) a : bool =field( default=lowercase , metadata={"help": "Whether distinct lines of text in the dataset are to be handled as distinct sequences."} , ) a : bool =field( default=lowercase , metadata={"help": "Train with masked-language modeling loss instead of language modeling."} ) a : bool =field(default=lowercase , metadata={"help": "Whether ot not to use whole word mask."} ) a : float =field( default=0.15 , metadata={"help": "Ratio of tokens to mask for masked language modeling loss"} ) a : float =field( default=1 / 6 , metadata={ "help": ( "Ratio of length of a span of masked tokens to surrounding context length for permutation language" " modeling." ) } , ) a : int =field( default=5 , metadata={"help": "Maximum length of a span of masked tokens for permutation language modeling."} ) a : int =field( default=-1 , metadata={ "help": ( "Optional input sequence length after tokenization." "The training dataset will be truncated in block of this size for training." "Default to the model max input length for single sentence inputs (take into account special tokens)." ) } , ) a : bool =field( default=lowercase , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def a__ ( SCREAMING_SNAKE_CASE : DataTrainingArguments , SCREAMING_SNAKE_CASE : PreTrainedTokenizer , SCREAMING_SNAKE_CASE : bool = False , SCREAMING_SNAKE_CASE : Optional[str] = None , ): '''simple docstring''' def _dataset(SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : List[str]=None ): if args.line_by_line: if ref_path is not None: if not args.whole_word_mask or not args.mlm: raise ValueError("You need to set world whole masking and mlm to True for Chinese Whole Word Mask" ) return LineByLineWithRefDataset( tokenizer=SCREAMING_SNAKE_CASE , file_path=SCREAMING_SNAKE_CASE , block_size=args.block_size , ref_path=SCREAMING_SNAKE_CASE , ) return LineByLineTextDataset(tokenizer=SCREAMING_SNAKE_CASE , file_path=SCREAMING_SNAKE_CASE , block_size=args.block_size ) else: return TextDataset( tokenizer=SCREAMING_SNAKE_CASE , file_path=SCREAMING_SNAKE_CASE , block_size=args.block_size , overwrite_cache=args.overwrite_cache , cache_dir=SCREAMING_SNAKE_CASE , ) if evaluate: return _dataset(args.eval_data_file , args.eval_ref_file ) elif args.train_data_files: return ConcatDataset([_dataset(SCREAMING_SNAKE_CASE ) for f in glob(args.train_data_files )] ) else: return _dataset(args.train_data_file , args.train_ref_file ) def a__ ( ): '''simple docstring''' lowerCAmelCase : List[str] = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) lowerCAmelCase : List[Any] = parser.parse_args_into_dataclasses() if data_args.eval_data_file is None and training_args.do_eval: raise ValueError( "Cannot do evaluation without an evaluation data file. Either supply a file to --eval_data_file " "or remove the --do_eval argument." ) if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. Use""" " --overwrite_output_dir to overcome." ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s" , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info("Training/evaluation parameters %s" , SCREAMING_SNAKE_CASE ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if model_args.config_name: lowerCAmelCase : int = AutoConfig.from_pretrained(model_args.config_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: lowerCAmelCase : Union[str, Any] = AutoConfig.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir ) else: lowerCAmelCase : Tuple = CONFIG_MAPPING[model_args.model_type]() logger.warning("You are instantiating a new config instance from scratch." ) if model_args.tokenizer_name: lowerCAmelCase : Optional[Any] = AutoTokenizer.from_pretrained(model_args.tokenizer_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: lowerCAmelCase : List[str] = AutoTokenizer.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir ) else: raise ValueError( "You are instantiating a new tokenizer from scratch. This is not supported, but you can do it from another" " script, save it,and load it from here, using --tokenizer_name" ) if model_args.model_name_or_path: lowerCAmelCase : str = AutoModelWithLMHead.from_pretrained( model_args.model_name_or_path , from_tf=bool(".ckpt" in model_args.model_name_or_path ) , config=SCREAMING_SNAKE_CASE , cache_dir=model_args.cache_dir , ) else: logger.info("Training new model from scratch" ) lowerCAmelCase : Union[str, Any] = AutoModelWithLMHead.from_config(SCREAMING_SNAKE_CASE ) model.resize_token_embeddings(len(SCREAMING_SNAKE_CASE ) ) if config.model_type in ["bert", "roberta", "distilbert", "camembert"] and not data_args.mlm: raise ValueError( "BERT and RoBERTa-like models do not have LM heads but masked LM heads. They must be run using the" "--mlm flag (masked language modeling)." ) if data_args.block_size <= 0: lowerCAmelCase : List[Any] = tokenizer.max_len # Our input block size will be the max possible for the model else: lowerCAmelCase : int = min(data_args.block_size , tokenizer.max_len ) # Get datasets lowerCAmelCase : Optional[int] = ( get_dataset(SCREAMING_SNAKE_CASE , tokenizer=SCREAMING_SNAKE_CASE , cache_dir=model_args.cache_dir ) if training_args.do_train else None ) lowerCAmelCase : Tuple = ( get_dataset(SCREAMING_SNAKE_CASE , tokenizer=SCREAMING_SNAKE_CASE , evaluate=SCREAMING_SNAKE_CASE , cache_dir=model_args.cache_dir ) if training_args.do_eval else None ) if config.model_type == "xlnet": lowerCAmelCase : int = DataCollatorForPermutationLanguageModeling( tokenizer=SCREAMING_SNAKE_CASE , plm_probability=data_args.plm_probability , max_span_length=data_args.max_span_length , ) else: if data_args.mlm and data_args.whole_word_mask: lowerCAmelCase : str = DataCollatorForWholeWordMask( tokenizer=SCREAMING_SNAKE_CASE , mlm_probability=data_args.mlm_probability ) else: lowerCAmelCase : Union[str, Any] = DataCollatorForLanguageModeling( tokenizer=SCREAMING_SNAKE_CASE , mlm=data_args.mlm , mlm_probability=data_args.mlm_probability ) # Initialize our Trainer lowerCAmelCase : Optional[Any] = Trainer( model=SCREAMING_SNAKE_CASE , args=SCREAMING_SNAKE_CASE , data_collator=SCREAMING_SNAKE_CASE , train_dataset=SCREAMING_SNAKE_CASE , eval_dataset=SCREAMING_SNAKE_CASE , prediction_loss_only=SCREAMING_SNAKE_CASE , ) # Training if training_args.do_train: lowerCAmelCase : Any = ( model_args.model_name_or_path if model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path ) else None ) trainer.train(model_path=SCREAMING_SNAKE_CASE ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation lowerCAmelCase : List[str] = {} if training_args.do_eval: logger.info("*** Evaluate ***" ) lowerCAmelCase : Dict = trainer.evaluate() lowerCAmelCase : Optional[int] = math.exp(eval_output["eval_loss"] ) lowerCAmelCase : Dict = {"perplexity": perplexity} lowerCAmelCase : Optional[int] = os.path.join(training_args.output_dir , "eval_results_lm.txt" ) if trainer.is_world_master(): with open(SCREAMING_SNAKE_CASE , "w" ) as writer: logger.info("***** Eval results *****" ) for key in sorted(result.keys() ): logger.info(" %s = %s" , SCREAMING_SNAKE_CASE , str(result[key] ) ) writer.write("%s = %s\n" % (key, str(result[key] )) ) results.update(SCREAMING_SNAKE_CASE ) return results def a__ ( SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' main() if __name__ == "__main__": main()
720
"""simple docstring""" import copy import inspect import unittest from transformers import AutoBackbone from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import require_timm, require_torch, torch_device from transformers.utils.import_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 if is_torch_available(): import torch from transformers import TimmBackbone, TimmBackboneConfig from ...test_pipeline_mixin import PipelineTesterMixin class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self , snake_case__ , snake_case__=None , snake_case__=None , snake_case__=None , snake_case__="resnet50" , snake_case__=3 , snake_case__=32 , snake_case__=3 , snake_case__=True , snake_case__=True , ): """simple docstring""" lowerCAmelCase : List[str] = parent lowerCAmelCase : Union[str, Any] = out_indices if out_indices is not None else [4] lowerCAmelCase : Tuple = stage_names lowerCAmelCase : Any = out_features lowerCAmelCase : Any = backbone lowerCAmelCase : Union[str, Any] = batch_size lowerCAmelCase : Optional[int] = image_size lowerCAmelCase : List[str] = num_channels lowerCAmelCase : int = use_pretrained_backbone lowerCAmelCase : Tuple = is_training def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase : Optional[int] = self.get_config() return config, pixel_values def lowercase__ ( self ): """simple docstring""" return TimmBackboneConfig( image_size=self.image_size , num_channels=self.num_channels , out_features=self.out_features , out_indices=self.out_indices , stage_names=self.stage_names , use_pretrained_backbone=self.use_pretrained_backbone , backbone=self.backbone , ) def lowercase__ ( self , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : List[Any] = TimmBackbone(config=snake_case__ ) model.to(snake_case__ ) model.eval() with torch.no_grad(): lowerCAmelCase : Union[str, Any] = model(snake_case__ ) self.parent.assertEqual( result.feature_map[-1].shape , (self.batch_size, model.channels[-1], 14, 14) , ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Union[str, Any] = self.prepare_config_and_inputs() lowerCAmelCase , lowerCAmelCase : Tuple = config_and_inputs lowerCAmelCase : str = {"pixel_values": pixel_values} return config, inputs_dict @require_torch @require_timm class SCREAMING_SNAKE_CASE__ ( lowercase , lowercase , lowercase , unittest.TestCase ): """simple docstring""" a : Optional[int] =(TimmBackbone,) if is_torch_available() else () a : Union[str, Any] ={"feature-extraction": TimmBackbone} if is_torch_available() else {} a : Tuple =False a : List[Any] =False a : Optional[Any] =False a : Dict =False def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = TimmBackboneModelTester(self ) lowerCAmelCase : List[str] = ConfigTester(self , config_class=snake_case__ , has_text_modality=snake_case__ ) def lowercase__ ( self ): """simple docstring""" 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 lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = "resnet18" lowerCAmelCase : str = "microsoft/resnet-18" lowerCAmelCase : List[Any] = AutoBackbone.from_pretrained(snake_case__ , use_timm_backbone=snake_case__ ) lowerCAmelCase : List[str] = AutoBackbone.from_pretrained(snake_case__ ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(len(timm_model.stage_names ) , len(transformers_model.stage_names ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) # Out indices are set to the last layer by default. For timm models, we don't know # the number of layers in advance, so we set it to (-1,), whereas for transformers # models, we set it to [len(stage_names) - 1] (kept for backward compatibility). self.assertEqual(timm_model.out_indices , (-1,) ) self.assertEqual(transformers_model.out_indices , [len(timm_model.stage_names ) - 1] ) lowerCAmelCase : Union[str, Any] = AutoBackbone.from_pretrained(snake_case__ , use_timm_backbone=snake_case__ , out_indices=[1, 2, 3] ) lowerCAmelCase : List[Any] = AutoBackbone.from_pretrained(snake_case__ , out_indices=[1, 2, 3] ) self.assertEqual(timm_model.out_indices , transformers_model.out_indices ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) @unittest.skip("TimmBackbone doesn't support feed forward chunking" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone doesn't have num_hidden_layers attribute" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone initialization is managed on the timm side" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone models doesn't have inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone models doesn't have inputs_embeds" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone model cannot be created without specifying a backbone checkpoint" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("model weights aren't tied in TimmBackbone." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("model weights aren't tied in TimmBackbone." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone" ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone doesn't have hidden size info in its configuration." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("TimmBackbone doesn't support output_attentions." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Safetensors is not supported by timm." ) def lowercase__ ( self ): """simple docstring""" pass @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : Dict = model_class(snake_case__ ) lowerCAmelCase : Any = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase : Optional[int] = [*signature.parameters.keys()] lowerCAmelCase : Any = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase : int = True lowerCAmelCase : str = self.has_attentions # no need to test all models as different heads yield the same functionality lowerCAmelCase : Optional[int] = self.all_model_classes[0] lowerCAmelCase : Union[str, Any] = model_class(snake_case__ ) model.to(snake_case__ ) lowerCAmelCase : Optional[int] = self._prepare_for_class(snake_case__ , snake_case__ ) lowerCAmelCase : Dict = model(**snake_case__ ) lowerCAmelCase : Tuple = outputs[0][-1] # Encoder-/Decoder-only models lowerCAmelCase : Optional[int] = outputs.hidden_states[0] hidden_states.retain_grad() if self.has_attentions: lowerCAmelCase : Union[str, Any] = outputs.attentions[0] attentions.retain_grad() output.flatten()[0].backward(retain_graph=snake_case__ ) self.assertIsNotNone(hidden_states.grad ) if self.has_attentions: self.assertIsNotNone(attentions.grad ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase , lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase : Dict = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : List[str] = model(**snake_case__ ) self.assertEqual(len(result.feature_maps ) , len(config.out_indices ) ) self.assertEqual(len(model.channels ) , len(config.out_indices ) ) # Check output of last stage is taken if out_features=None, out_indices=None lowerCAmelCase : Dict = copy.deepcopy(snake_case__ ) lowerCAmelCase : Optional[int] = None lowerCAmelCase : Dict = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : Optional[int] = model(**snake_case__ ) self.assertEqual(len(result.feature_maps ) , 1 ) self.assertEqual(len(model.channels ) , 1 ) # Check backbone can be initialized with fresh weights lowerCAmelCase : Optional[int] = copy.deepcopy(snake_case__ ) lowerCAmelCase : List[str] = False lowerCAmelCase : int = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() lowerCAmelCase : Optional[Any] = model(**snake_case__ )
681
0